From cb58cd34f876ddc8cf65b47d61a27e7b88c62ae2 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 4 Jul 2025 13:54:55 +0200 Subject: [PATCH 1/2] Expose the DefaultClient API --- src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1653656..e0f1942 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -184,12 +184,14 @@ fn retry_after(rsp: &BytesResponse) -> Option { }) } +/// A default HTTP client implementation using hyper-rustls #[cfg(feature = "hyper-rustls")] -struct DefaultClient(HyperClient, Full>); +pub struct DefaultClient(HyperClient, Full>); #[cfg(feature = "hyper-rustls")] impl DefaultClient { - fn try_new() -> Result { + /// Create a new default client using rustls-platform-verifier + pub fn try_new() -> Result { Ok(Self( HyperClient::builder(TokioExecutor::new()).build( hyper_rustls::HttpsConnectorBuilder::new() From 0e55e6d15f1bc6cef04782bef8f17be7d2804bb0 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 4 Jul 2025 13:55:49 +0200 Subject: [PATCH 2/2] Add DefaultClient::with_config() --- Cargo.toml | 1 + src/lib.rs | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 96d1900..c322a90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ hyper-rustls = { version = "0.27.7", default-features = false, features = ["http hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2", "tokio"], optional = true } rcgen = { version = "0.14", default-features = false, features = ["pem"], optional = true } ring = { version = "0.17", features = ["std"], optional = true } +rustls = { version = "0.23", default-features = false } rustls-pki-types = "1.1.0" serde = { version = "1.0.104", features = ["derive"] } serde_json = "1.0.78" diff --git a/src/lib.rs b/src/lib.rs index e0f1942..5c0d6e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,10 @@ use http::{Method, Request, Response, StatusCode}; use http_body_util::{BodyExt, Full}; use httpdate::HttpDate; #[cfg(feature = "hyper-rustls")] +use hyper_rustls::HttpsConnectorBuilder; +#[cfg(feature = "hyper-rustls")] +use hyper_rustls::builderstates::WantsSchemes; +#[cfg(feature = "hyper-rustls")] use hyper_util::client::legacy::Client as HyperClient; #[cfg(feature = "hyper-rustls")] use hyper_util::client::legacy::connect::{Connect, HttpConnector}; @@ -190,20 +194,26 @@ pub struct DefaultClient(HyperClient #[cfg(feature = "hyper-rustls")] impl DefaultClient { + /// Create a new default HTTP client with the given TLS configuration + pub fn with_config(config: rustls::ClientConfig) -> Self { + Self::new(hyper_rustls::HttpsConnectorBuilder::new().with_tls_config(config)) + } + /// Create a new default client using rustls-platform-verifier pub fn try_new() -> Result { - Ok(Self( - HyperClient::builder(TokioExecutor::new()).build( - hyper_rustls::HttpsConnectorBuilder::new() - .try_with_platform_verifier() - .map_err(|e| Error::Other(Box::new(e)))? - .https_only() - .enable_http1() - .enable_http2() - .build(), - ), + Ok(Self::new( + hyper_rustls::HttpsConnectorBuilder::new() + .try_with_platform_verifier() + .map_err(|e| Error::Other(Box::new(e)))?, )) } + + fn new(builder: HttpsConnectorBuilder) -> Self { + Self( + HyperClient::builder(TokioExecutor::new()) + .build(builder.https_only().enable_http1().enable_http2().build()), + ) + } } #[cfg(feature = "hyper-rustls")]