@@ -2,6 +2,7 @@ use futures_util::FutureExt;
2
2
#[ cfg( feature = "tokio-runtime" ) ]
3
3
use hyper:: client:: connect:: HttpConnector ;
4
4
use hyper:: { client:: connect:: Connection , service:: Service , Uri } ;
5
+ use log:: warn;
5
6
use rustls:: ClientConfig ;
6
7
use std:: future:: Future ;
7
8
use std:: pin:: Pin ;
@@ -11,7 +12,6 @@ use std::{fmt, io};
11
12
use tokio:: io:: { AsyncRead , AsyncWrite } ;
12
13
use tokio_rustls:: TlsConnector ;
13
14
use webpki:: DNSNameRef ;
14
- use log:: warn;
15
15
16
16
use crate :: stream:: MaybeHttpsStream ;
17
17
@@ -24,41 +24,52 @@ pub struct HttpsConnector<T> {
24
24
tls_config : Arc < ClientConfig > ,
25
25
}
26
26
27
- #[ cfg( all( any( feature = "rustls-native-certs" , feature = "webpki-roots" ) , feature = "tokio-runtime" ) ) ]
27
+ #[ cfg( all(
28
+ any( feature = "rustls-native-certs" , feature = "webpki-roots" ) ,
29
+ feature = "tokio-runtime"
30
+ ) ) ]
28
31
impl HttpsConnector < HttpConnector > {
29
32
/// Construct a new `HttpsConnector`.
30
- ///
31
- /// Takes number of DNS worker threads.
32
33
pub fn new ( ) -> Self {
33
34
let mut http = HttpConnector :: new ( ) ;
35
+
34
36
http. enforce_http ( false ) ;
37
+
38
+ ( http, Self :: default_client_config ( ) ) . into ( )
39
+ }
40
+
41
+ /// Constructs default `ClientConfig` which later can be used for
42
+ /// construction of `HttpsConnector` with custom `HttpConnector`.
43
+ pub fn default_client_config ( ) -> ClientConfig {
35
44
let mut config = ClientConfig :: new ( ) ;
36
45
config. alpn_protocols = vec ! [ b"h2" . to_vec( ) , b"http/1.1" . to_vec( ) ] ;
37
- #[ cfg( feature = "rustls-native-certs" ) ]
46
+ #[ cfg( feature = "rustls-native-certs" ) ]
38
47
{
39
48
config. root_store = match rustls_native_certs:: load_native_certs ( ) {
40
49
Ok ( store) => store,
41
50
Err ( ( Some ( store) , err) ) => {
42
51
warn ! ( "Could not load all certificates: {:?}" , err) ;
43
52
store
44
53
}
45
- Err ( ( None , err) ) => {
46
- Err ( err) . expect ( "cannot access native cert store" )
47
- }
54
+ Err ( ( None , err) ) => Err ( err) . expect ( "cannot access native cert store" ) ,
48
55
} ;
49
56
}
50
- #[ cfg( feature = "webpki-roots" ) ]
57
+ #[ cfg( feature = "webpki-roots" ) ]
51
58
{
52
59
config
53
60
. root_store
54
61
. add_server_trust_anchors ( & webpki_roots:: TLS_SERVER_ROOTS ) ;
55
62
}
56
63
config. ct_logs = Some ( & ct_logs:: LOGS ) ;
57
- ( http, config) . into ( )
64
+
65
+ config
58
66
}
59
67
}
60
68
61
- #[ cfg( all( any( feature = "rustls-native-certs" , feature = "webpki-roots" ) , feature = "tokio-runtime" ) ) ]
69
+ #[ cfg( all(
70
+ any( feature = "rustls-native-certs" , feature = "webpki-roots" ) ,
71
+ feature = "tokio-runtime"
72
+ ) ) ]
62
73
impl Default for HttpsConnector < HttpConnector > {
63
74
fn default ( ) -> Self {
64
75
Self :: new ( )
@@ -73,7 +84,7 @@ impl<T> fmt::Debug for HttpsConnector<T> {
73
84
74
85
impl < H , C > From < ( H , C ) > for HttpsConnector < H >
75
86
where
76
- C : Into < Arc < ClientConfig > >
87
+ C : Into < Arc < ClientConfig > > ,
77
88
{
78
89
fn from ( ( http, cfg) : ( H , C ) ) -> Self {
79
90
HttpsConnector {
83
94
}
84
95
}
85
96
97
+ #[ cfg( all(
98
+ any( feature = "rustls-native-certs" , feature = "webpki-roots" ) ,
99
+ feature = "tokio-runtime"
100
+ ) ) ]
101
+ impl < H > From < H > for HttpsConnector < H > {
102
+ fn from ( http : H ) -> Self {
103
+ let mut config = ClientConfig :: new ( ) ;
104
+ config. alpn_protocols = vec ! [ b"h2" . to_vec( ) , b"http/1.1" . to_vec( ) ] ;
105
+ #[ cfg( feature = "rustls-native-certs" ) ]
106
+ {
107
+ config. root_store = match rustls_native_certs:: load_native_certs ( ) {
108
+ Ok ( store) => store,
109
+ Err ( ( Some ( store) , err) ) => {
110
+ warn ! ( "Could not load all certificates: {:?}" , err) ;
111
+ store
112
+ }
113
+ Err ( ( None , err) ) => Err ( err) . expect ( "cannot access native cert store" ) ,
114
+ } ;
115
+ }
116
+ #[ cfg( feature = "webpki-roots" ) ]
117
+ {
118
+ config
119
+ . root_store
120
+ . add_server_trust_anchors ( & webpki_roots:: TLS_SERVER_ROOTS ) ;
121
+ }
122
+ config. ct_logs = Some ( & ct_logs:: LOGS ) ;
123
+
124
+ HttpsConnector {
125
+ http,
126
+ tls_config : config. into ( ) ,
127
+ }
128
+ }
129
+ }
130
+
86
131
impl < T > Service < Uri > for HttpsConnector < T >
87
132
where
88
133
T : Service < Uri > ,
0 commit comments