Restore HTTP client transition, but use reqwest everywhere (#19055)

Release Notes:

- N/A
This commit is contained in:
Mikayla Maki 2024-10-11 14:58:58 -07:00 committed by GitHub
parent c709b66f35
commit 22ac178f9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 838 additions and 418 deletions

View file

@ -16,11 +16,13 @@ path = "src/http_client.rs"
doctest = true
[dependencies]
http = "0.2"
anyhow.workspace = true
derive_more.workspace = true
futures.workspace = true
http = "1.1"
log.workspace = true
rustls-native-certs.workspace = true
rustls.workspace = true
serde.workspace = true
serde_json.workspace = true
smol.workspace = true

View file

@ -11,13 +11,22 @@ use http::request::Builder;
#[cfg(feature = "test-support")]
use std::fmt;
use std::{
sync::{Arc, Mutex},
any::type_name,
sync::{Arc, LazyLock, Mutex},
time::Duration,
};
pub use url::Url;
#[derive(Clone)]
pub struct ReadTimeout(pub Duration);
#[derive(Default, Debug, Clone)]
impl Default for ReadTimeout {
fn default() -> Self {
Self(Duration::from_secs(5))
}
}
#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
pub enum RedirectPolicy {
#[default]
NoFollow,
@ -26,6 +35,23 @@ pub enum RedirectPolicy {
}
pub struct FollowRedirects(pub bool);
pub static TLS_CONFIG: LazyLock<Arc<rustls::ClientConfig>> = LazyLock::new(|| {
let mut root_store = rustls::RootCertStore::empty();
let root_certs = rustls_native_certs::load_native_certs();
for error in root_certs.errors {
log::warn!("error loading native certs: {:?}", error);
}
root_store.add_parsable_certificates(&root_certs.certs);
Arc::new(
rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(root_store)
.with_no_client_auth(),
)
});
pub trait HttpRequestExt {
/// Set a read timeout on the request.
/// For isahc, this is the low_speed_timeout.
@ -47,6 +73,8 @@ impl HttpRequestExt for http::request::Builder {
}
pub trait HttpClient: 'static + Send + Sync {
fn type_name(&self) -> &'static str;
fn send(
&self,
req: http::Request<AsyncBody>,
@ -129,6 +157,10 @@ impl HttpClient for HttpClientWithProxy {
fn proxy(&self) -> Option<&Uri> {
self.proxy.as_ref()
}
fn type_name(&self) -> &'static str {
self.client.type_name()
}
}
impl HttpClient for Arc<HttpClientWithProxy> {
@ -142,6 +174,10 @@ impl HttpClient for Arc<HttpClientWithProxy> {
fn proxy(&self) -> Option<&Uri> {
self.proxy.as_ref()
}
fn type_name(&self) -> &'static str {
self.client.type_name()
}
}
/// An [`HttpClient`] that has a base URL.
@ -253,6 +289,10 @@ impl HttpClient for Arc<HttpClientWithUrl> {
fn proxy(&self) -> Option<&Uri> {
self.client.proxy.as_ref()
}
fn type_name(&self) -> &'static str {
self.client.type_name()
}
}
impl HttpClient for HttpClientWithUrl {
@ -266,6 +306,10 @@ impl HttpClient for HttpClientWithUrl {
fn proxy(&self) -> Option<&Uri> {
self.client.proxy.as_ref()
}
fn type_name(&self) -> &'static str {
self.client.type_name()
}
}
pub fn read_proxy_from_env() -> Option<Uri> {
@ -306,6 +350,10 @@ impl HttpClient for BlockedHttpClient {
fn proxy(&self) -> Option<&Uri> {
None
}
fn type_name(&self) -> &'static str {
type_name::<Self>()
}
}
#[cfg(feature = "test-support")]
@ -378,4 +426,8 @@ impl HttpClient for FakeHttpClient {
fn proxy(&self) -> Option<&Uri> {
None
}
fn type_name(&self) -> &'static str {
type_name::<Self>()
}
}