Skip to content

Commit eaecb76

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 eaecb76

File tree

5 files changed

+69
-17
lines changed

5 files changed

+69
-17
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ curl_client = ["isahc", "async-std"]
2929
wasm_client = ["js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures", "futures"]
3030
hyper_client = ["hyper", "hyper-tls", "http-types/hyperium_http", "futures-util", "tokio"]
3131

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

3535
unstable-config = []
3636

@@ -47,9 +47,11 @@ async-std = { version = "1.6.0", default-features = false, optional = true }
4747
async-native-tls = { version = "0.3.1", optional = true }
4848
deadpool = { version = "0.7.0", optional = true }
4949
futures = { version = "0.3.8", optional = true }
50+
native-tls_crate = { version = "0.2", optional = true, package = "native-tls" }
5051

5152
# h1_client_rustls
5253
async-tls = { version = "0.10.0", optional = true }
54+
rustls_crate = { version = "0.18", optional = true, package = "rustls" }
5355

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

src/config.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use std::time::Duration;
44

55
/// Configuration for `HttpClient`s.
66
#[non_exhaustive]
7-
#[derive(Clone, Debug)]
7+
#[derive(Clone)]
8+
#[cfg_attr(not(feature = "rustls"), derive(Debug))]
9+
#[cfg_attr(feature = "rustls", allow(missing_debug_implementations))]
810
pub struct Config {
911
/// HTTP/1.1 `keep-alive` (connection pooling).
1012
///
@@ -18,6 +20,12 @@ pub struct Config {
1820
///
1921
/// Default: `Some(Duration::from_secs(60))`.
2022
pub timeout: Option<Duration>,
23+
/// TLS Configuration (Rustls)
24+
#[cfg(all(feature = "h1_client", feature = "rustls"))]
25+
pub tls_config: Option<std::sync::Arc<rustls_crate::ClientConfig>>,
26+
/// TLS Configuration (Native TLS)
27+
#[cfg(all(feature = "h1_client", feature = "native-tls"))]
28+
pub tls_config: Option<native_tls_crate::TlsConnectorBuilder>,
2129
}
2230

2331
impl Config {
@@ -27,6 +35,10 @@ impl Config {
2735
http_keep_alive: true,
2836
tcp_no_delay: false,
2937
timeout: Some(Duration::from_secs(60)),
38+
#[cfg(all(feature = "h1_client", feature = "rustls"))]
39+
tls_config: None,
40+
#[cfg(all(feature = "h1_client", feature = "native-tls"))]
41+
tls_config: None,
3042
}
3143
}
3244
}
@@ -55,4 +67,17 @@ impl Config {
5567
self.timeout = timeout;
5668
self
5769
}
70+
71+
/// Set TLS Configuration (Rustls)
72+
#[cfg(all(feature = "h1_client", feature = "rustls"))]
73+
pub fn set_tls_config(mut self, tls_config: Option<std::sync::Arc<rustls_crate::ClientConfig>>) -> Self {
74+
self.tls_config = tls_config;
75+
self
76+
}
77+
/// Set TLS Configuration (Native TLS)
78+
#[cfg(all(feature = "h1_client", feature = "native-tls"))]
79+
pub fn set_tls_config(mut self, tls_config: Option<native_tls_crate::TlsConnectorBuilder>) -> Self {
80+
self.tls_config = tls_config;
81+
self
82+
}
5883
}

src/h1/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ impl Debug for H1Client {
6565
vec![]
6666
};
6767

68-
f.debug_struct("H1Client")
69-
.field(
68+
let mut dbg_struct = f.debug_struct("H1Client");
69+
dbg_struct.field(
7070
"http_pools",
7171
&self
7272
.http_pools
@@ -81,12 +81,17 @@ 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,
88-
)
89-
.finish()
87+
);
88+
89+
#[cfg(not(feature = "rustls"))]
90+
{
91+
dbg_struct.field("config", &self.config);
92+
}
93+
94+
dbg_struct.finish()
9095
}
9196
}
9297

@@ -175,7 +180,7 @@ impl HttpClient for H1Client {
175180
let raw_stream = async_std::net::TcpStream::connect(addr).await?;
176181
req.set_peer_addr(raw_stream.peer_addr().ok());
177182
req.set_local_addr(raw_stream.local_addr().ok());
178-
let tls_stream = tls::add_tls(&host, raw_stream).await?;
183+
let tls_stream = tls::add_tls(&host, raw_stream, &self.config).await?;
179184
let tsl_conn = client::connect(tls_stream, req);
180185
return if let Some(timeout) = self.config.timeout {
181186
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: 26 additions & 6 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,36 @@ 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> {
109-
let connector = async_tls::TlsConnector::default();
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(ref tls_config) = config.tls_config {
112+
tls_config.clone().into()
113+
} else {
114+
async_tls::TlsConnector::default()
115+
};
116+
#[cfg(not(feature = "unstable-config"))]
117+
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 = if let Some(ref tls_config) = config.tls_config {
130+
tls_config.clone().into()
131+
} else {
132+
async_native_tls::TlsConnector::new()
133+
};
134+
#[cfg(not(feature = "unstable-config"))]
135+
let connector = async_native_tls::TlsConnector::new();
136+
137+
connector.connect(host, stream).await
118138
}
119139
}
120140
}

0 commit comments

Comments
 (0)