1
+ use std:: sync:: Arc ;
2
+
1
3
use hyper_util:: client:: legacy:: connect:: HttpConnector ;
2
4
#[ cfg( any( feature = "rustls-native-certs" , feature = "webpki-roots" ) ) ]
3
5
use rustls:: crypto:: CryptoProvider ;
@@ -44,6 +46,27 @@ impl ConnectorBuilder<WantsTlsConfig> {
44
46
/// [`enable_http2`](ConnectorBuilder::enable_http2)) before the
45
47
/// connector is built.
46
48
pub fn with_tls_config ( self , config : ClientConfig ) -> ConnectorBuilder < WantsSchemes > {
49
+ assert ! (
50
+ config. alpn_protocols. is_empty( ) ,
51
+ "ALPN protocols should not be pre-defined"
52
+ ) ;
53
+ ConnectorBuilder ( WantsSchemes {
54
+ tls_config : Arc :: new ( config) ,
55
+ } )
56
+ }
57
+
58
+ /// Passes an [`Arc`]-shared rustls [`ClientConfig`] to configure the TLS connection
59
+ ///
60
+ /// The [`alpn_protocols`](ClientConfig::alpn_protocols) field is
61
+ /// required to be empty (or the function will panic) and will be
62
+ /// rewritten to match the enabled schemes (see
63
+ /// [`enable_http1`](ConnectorBuilder::enable_http1),
64
+ /// [`enable_http2`](ConnectorBuilder::enable_http2)) before the
65
+ /// connector is built.
66
+ pub fn with_shared_tls_config (
67
+ self ,
68
+ config : Arc < ClientConfig > ,
69
+ ) -> ConnectorBuilder < WantsSchemes > {
47
70
assert ! (
48
71
config. alpn_protocols. is_empty( ) ,
49
72
"ALPN protocols should not be pre-defined"
@@ -133,7 +156,7 @@ impl Default for ConnectorBuilder<WantsTlsConfig> {
133
156
/// State of a builder that needs schemes (https:// and http://) to be
134
157
/// configured next
135
158
pub struct WantsSchemes {
136
- tls_config : ClientConfig ,
159
+ tls_config : Arc < ClientConfig > ,
137
160
}
138
161
139
162
impl ConnectorBuilder < WantsSchemes > {
@@ -166,7 +189,7 @@ impl ConnectorBuilder<WantsSchemes> {
166
189
///
167
190
/// No protocol has been enabled at this point.
168
191
pub struct WantsProtocols1 {
169
- tls_config : ClientConfig ,
192
+ tls_config : Arc < ClientConfig > ,
170
193
https_only : bool ,
171
194
override_server_name : Option < String > ,
172
195
}
@@ -176,7 +199,7 @@ impl WantsProtocols1 {
176
199
HttpsConnector {
177
200
force_https : self . https_only ,
178
201
http : conn,
179
- tls_config : std :: sync :: Arc :: new ( self . tls_config ) ,
202
+ tls_config : self . tls_config ,
180
203
override_server_name : self . override_server_name ,
181
204
}
182
205
}
@@ -203,7 +226,7 @@ impl ConnectorBuilder<WantsProtocols1> {
203
226
/// This needs to be called explicitly, no protocol is enabled by default
204
227
#[ cfg( feature = "http2" ) ]
205
228
pub fn enable_http2 ( mut self ) -> ConnectorBuilder < WantsProtocols3 > {
206
- self . 0 . tls_config . alpn_protocols = vec ! [ b"h2" . to_vec( ) ] ;
229
+ Arc :: make_mut ( & mut self . 0 . tls_config ) . alpn_protocols = vec ! [ b"h2" . to_vec( ) ] ;
207
230
ConnectorBuilder ( WantsProtocols3 {
208
231
inner : self . 0 ,
209
232
enable_http1 : false ,
@@ -221,7 +244,7 @@ impl ConnectorBuilder<WantsProtocols1> {
221
244
#[ cfg( not( feature = "http1" ) ) ]
222
245
let alpn_protocols = vec ! [ b"h2" . to_vec( ) ] ;
223
246
224
- self . 0 . tls_config . alpn_protocols = alpn_protocols;
247
+ Arc :: make_mut ( & mut self . 0 . tls_config ) . alpn_protocols = alpn_protocols;
225
248
ConnectorBuilder ( WantsProtocols3 {
226
249
inner : self . 0 ,
227
250
enable_http1 : cfg ! ( feature = "http1" ) ,
@@ -259,7 +282,8 @@ impl ConnectorBuilder<WantsProtocols2> {
259
282
/// This needs to be called explicitly, no protocol is enabled by default
260
283
#[ cfg( feature = "http2" ) ]
261
284
pub fn enable_http2 ( mut self ) -> ConnectorBuilder < WantsProtocols3 > {
262
- self . 0 . inner . tls_config . alpn_protocols = vec ! [ b"h2" . to_vec( ) , b"http/1.1" . to_vec( ) ] ;
285
+ Arc :: make_mut ( & mut self . 0 . inner . tls_config ) . alpn_protocols =
286
+ vec ! [ b"h2" . to_vec( ) , b"http/1.1" . to_vec( ) ] ;
263
287
ConnectorBuilder ( WantsProtocols3 {
264
288
inner : self . 0 . inner ,
265
289
enable_http1 : true ,
0 commit comments