Skip to content

Commit 2740e26

Browse files
committed
feat: add tls_config to Config
Only supports the h1 client, but has two different options, one each for native-tls and rustls.
1 parent 06249b8 commit 2740e26

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ wasm_client = ["js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures", "fut
3030
hyper_client = ["hyper", "hyper-tls", "http-types/hyperium_http", "futures-util", "tokio"]
3131

3232
native-tls = ["async-native-tls"]
33-
rustls = ["async-tls"]
33+
rustls = ["async-tls", "rustls_crate"]
3434

3535
unstable-config = []
3636

@@ -50,6 +50,7 @@ futures = { version = "0.3.8", optional = true }
5050

5151
# h1_client_rustls
5252
async-tls = { version = "0.10.0", optional = true }
53+
rustls_crate = { version = "0.18", optional = true, package = "rustls" }
5354

5455
# hyper_client
5556
hyper = { version = "0.13.6", features = ["tcp"], optional = true }

src/config.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Configuration for `HttpClient`s.
22
3+
use std::fmt::Debug;
34
use std::time::Duration;
45

56
/// Configuration for `HttpClient`s.
67
#[non_exhaustive]
7-
#[derive(Clone, Debug)]
8+
#[derive(Clone)]
89
pub struct Config {
910
/// HTTP/1.1 `keep-alive` (connection pooling).
1011
///
@@ -18,6 +19,37 @@ pub struct Config {
1819
///
1920
/// Default: `Some(Duration::from_secs(60))`.
2021
pub timeout: Option<Duration>,
22+
/// TLS Configuration (Rustls)
23+
#[cfg(all(feature = "h1_client", feature = "rustls"))]
24+
pub tls_config: Option<std::sync::Arc<rustls_crate::ClientConfig>>,
25+
/// TLS Configuration (Native TLS)
26+
#[cfg(all(feature = "h1_client", feature = "native-tls", not(feature = "rustls")))]
27+
pub tls_config: Option<std::sync::Arc<async_native_tls::TlsConnector>>,
28+
}
29+
30+
impl Debug for Config {
31+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32+
let mut dbg_struct = f.debug_struct("Config");
33+
dbg_struct
34+
.field("http_keep_alive", &self.http_keep_alive)
35+
.field("tcp_no_delay", &self.tcp_no_delay)
36+
.field("timeout", &self.timeout);
37+
38+
#[cfg(all(feature = "h1_client", feature = "rustls"))]
39+
{
40+
if self.tls_config.is_some() {
41+
dbg_struct.field("tls_config", &"Some(rustls::ClientConfig)");
42+
} else {
43+
dbg_struct.field("tls_config", &"None");
44+
}
45+
}
46+
#[cfg(all(feature = "h1_client", feature = "native-tls", not(feature = "rustls")))]
47+
{
48+
dbg_struct.field("tls_config", &self.tls_config);
49+
}
50+
51+
dbg_struct.finish()
52+
}
2153
}
2254

2355
impl Config {
@@ -27,6 +59,8 @@ impl Config {
2759
http_keep_alive: true,
2860
tcp_no_delay: false,
2961
timeout: Some(Duration::from_secs(60)),
62+
#[cfg(all(feature = "h1_client", any(feature = "rustls", feature = "native-tls")))]
63+
tls_config: None,
3064
}
3165
}
3266
}
@@ -55,4 +89,23 @@ impl Config {
5589
self.timeout = timeout;
5690
self
5791
}
92+
93+
/// Set TLS Configuration (Rustls)
94+
#[cfg(all(feature = "h1_client", feature = "rustls"))]
95+
pub fn set_tls_config(
96+
mut self,
97+
tls_config: Option<std::sync::Arc<rustls_crate::ClientConfig>>,
98+
) -> Self {
99+
self.tls_config = tls_config;
100+
self
101+
}
102+
/// Set TLS Configuration (Native TLS)
103+
#[cfg(all(feature = "h1_client", feature = "native-tls", not(feature = "rustls")))]
104+
pub fn set_tls_config(
105+
mut self,
106+
tls_config: Option<std::sync::Arc<async_native_tls::TlsConnector>>,
107+
) -> Self {
108+
self.tls_config = tls_config;
109+
self
110+
}
58111
}

src/h1/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ impl Debug for H1Client {
8181
.collect::<Vec<String>>(),
8282
)
8383
.field("https_pools", &https_pools)
84-
.field("config", &self.config)
8584
.field(
8685
"max_concurrent_connections",
8786
&self.max_concurrent_connections,
8887
)
88+
.field("config", &self.config)
8989
.finish()
9090
}
9191
}
@@ -175,7 +175,7 @@ impl HttpClient for H1Client {
175175
let raw_stream = async_std::net::TcpStream::connect(addr).await?;
176176
req.set_peer_addr(raw_stream.peer_addr().ok());
177177
req.set_local_addr(raw_stream.local_addr().ok());
178-
let tls_stream = tls::add_tls(&host, raw_stream).await?;
178+
let tls_stream = tls::add_tls(&host, raw_stream, &self.config).await?;
179179
let tsl_conn = client::connect(tls_stream, req);
180180
return if let Some(timeout) = self.config.timeout {
181181
async_std::future::timeout(timeout, tsl_conn).await?

src/h1/tcp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::fmt::Debug;
21
use std::net::SocketAddr;
32
use std::pin::Pin;
43

@@ -10,7 +9,8 @@ use futures::task::{Context, Poll};
109

1110
use crate::Config;
1211

13-
#[derive(Clone, Debug)]
12+
#[derive(Clone)]
13+
#[cfg_attr(not(feature = "rustls"), derive(std::fmt::Debug))]
1414
pub(crate) struct TcpConnection {
1515
addr: SocketAddr,
1616
config: Config,

src/h1/tls.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::fmt::Debug;
21
use std::net::SocketAddr;
32
use std::pin::Pin;
43

@@ -18,7 +17,8 @@ cfg_if::cfg_if! {
1817

1918
use crate::{Config, Error};
2019

21-
#[derive(Clone, Debug)]
20+
#[derive(Clone)]
21+
#[cfg_attr(not(feature = "rustls"), derive(std::fmt::Debug))]
2222
pub(crate) struct TlsConnection {
2323
host: String,
2424
addr: SocketAddr,
@@ -76,7 +76,7 @@ impl Manager<TlsStream<TcpStream>, Error> for TlsConnection {
7676
#[cfg(feature = "unstable-config")]
7777
raw_stream.set_nodelay(self.config.tcp_no_delay)?;
7878

79-
let tls_stream = add_tls(&self.host, raw_stream).await?;
79+
let tls_stream = add_tls(&self.host, raw_stream, &self.config).await?;
8080
Ok(tls_stream)
8181
}
8282

@@ -105,16 +105,32 @@ impl Manager<TlsStream<TcpStream>, Error> for TlsConnection {
105105

106106
cfg_if::cfg_if! {
107107
if #[cfg(feature = "rustls")] {
108-
pub(crate) async fn add_tls(host: &str, stream: TcpStream) -> Result<TlsStream<TcpStream>, std::io::Error> {
108+
#[allow(unused_variables)]
109+
pub(crate) async fn add_tls(host: &str, stream: TcpStream, config: &Config) -> Result<TlsStream<TcpStream>, std::io::Error> {
110+
#[cfg(all(feature = "h1_client", feature = "unstable-config"))]
111+
let connector = if let Some(tls_config) = config.tls_config.as_ref().cloned() {
112+
tls_config.into()
113+
} else {
114+
async_tls::TlsConnector::default()
115+
};
116+
#[cfg(not(feature = "unstable-config"))]
109117
let connector = async_tls::TlsConnector::default();
118+
110119
connector.connect(host, stream).await
111120
}
112121
} else if #[cfg(feature = "native-tls")] {
122+
#[allow(unused_variables)]
113123
pub(crate) async fn add_tls(
114124
host: &str,
115125
stream: TcpStream,
126+
config: &Config,
116127
) -> Result<TlsStream<TcpStream>, async_native_tls::Error> {
117-
async_native_tls::connect(host, stream).await
128+
#[cfg(feature = "unstable-config")]
129+
let connector = config.tls_config.as_ref().cloned().unwrap_or_default();
130+
#[cfg(not(feature = "unstable-config"))]
131+
let connector = async_native_tls::TlsConnector::new();
132+
133+
connector.connect(host, stream).await
118134
}
119135
}
120136
}

0 commit comments

Comments
 (0)