@@ -2,6 +2,7 @@ use futures_util::FutureExt;
22#[ cfg( feature = "tokio-runtime" ) ]
33use hyper:: client:: connect:: HttpConnector ;
44use hyper:: { client:: connect:: Connection , service:: Service , Uri } ;
5+ use log:: warn;
56use rustls:: ClientConfig ;
67use std:: future:: Future ;
78use std:: pin:: Pin ;
@@ -11,7 +12,6 @@ use std::{fmt, io};
1112use tokio:: io:: { AsyncRead , AsyncWrite } ;
1213use tokio_rustls:: TlsConnector ;
1314use webpki:: DNSNameRef ;
14- use log:: warn;
1515
1616use crate :: stream:: MaybeHttpsStream ;
1717
@@ -24,47 +24,48 @@ pub struct HttpsConnector<T> {
2424 tls_config : Arc < ClientConfig > ,
2525}
2626
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+ ) ) ]
2831impl HttpsConnector < HttpConnector > {
29- /// Construct a new `HttpsConnector`.
30- ///
31- /// Takes number of DNS worker threads.
32- pub fn new ( ) -> Self {
33- let mut http = HttpConnector :: new ( ) ;
34- http. enforce_http ( false ) ;
32+ /// Construct a new `HttpsConnector` using the OS root store
33+ #[ cfg( feature = "rustls-native-certs" ) ]
34+ #[ cfg_attr( docsrs, doc( cfg( feature = "rustls-native-certs" ) ) ) ]
35+ pub fn with_native_roots ( ) -> Self {
3536 let mut config = ClientConfig :: new ( ) ;
36- config. alpn_protocols = vec ! [ b"h2" . to_vec( ) , b"http/1.1" . to_vec( ) ] ;
37- #[ cfg( feature = "rustls-native-certs" ) ]
38- {
39- config. root_store = match rustls_native_certs:: load_native_certs ( ) {
40- Ok ( store) => store,
41- Err ( ( Some ( store) , err) ) => {
42- warn ! ( "Could not load all certificates: {:?}" , err) ;
43- store
44- }
45- Err ( ( None , err) ) => {
46- Err ( err) . expect ( "cannot access native cert store" )
47- }
48- } ;
49- }
50- #[ cfg( feature = "webpki-roots" ) ]
51- {
52- config
53- . root_store
54- . add_server_trust_anchors ( & webpki_roots:: TLS_SERVER_ROOTS ) ;
55- }
56- config. ct_logs = Some ( & ct_logs:: LOGS ) ;
37+ config. root_store = match rustls_native_certs:: load_native_certs ( ) {
38+ Ok ( store) => store,
39+ Err ( ( Some ( store) , err) ) => {
40+ warn ! ( "Could not load all certificates: {:?}" , err) ;
41+ store
42+ }
43+ Err ( ( None , err) ) => Err ( err) . expect ( "cannot access native cert store" ) ,
44+ } ;
5745 if config. root_store . is_empty ( ) {
5846 panic ! ( "no CA certificates found" ) ;
5947 }
60- ( http , config) . into ( )
48+ Self :: build ( config)
6149 }
62- }
6350
64- #[ cfg( all( any( feature = "rustls-native-certs" , feature = "webpki-roots" ) , feature = "tokio-runtime" ) ) ]
65- impl Default for HttpsConnector < HttpConnector > {
66- fn default ( ) -> Self {
67- Self :: new ( )
51+ /// Construct a new `HttpsConnector` using the `webpki_roots`
52+ #[ cfg( feature = "webpki-roots" ) ]
53+ #[ cfg_attr( docsrs, doc( cfg( feature = "webpki-roots" ) ) ) ]
54+ pub fn with_webpki_roots ( ) -> Self {
55+ let mut config = ClientConfig :: new ( ) ;
56+ config
57+ . root_store
58+ . add_server_trust_anchors ( & webpki_roots:: TLS_SERVER_ROOTS ) ;
59+ Self :: build ( config)
60+ }
61+
62+ fn build ( mut config : ClientConfig ) -> Self {
63+ let mut http = HttpConnector :: new ( ) ;
64+ http. enforce_http ( false ) ;
65+
66+ config. alpn_protocols = vec ! [ b"h2" . to_vec( ) , b"http/1.1" . to_vec( ) ] ;
67+ config. ct_logs = Some ( & ct_logs:: LOGS ) ;
68+ ( http, config) . into ( )
6869 }
6970}
7071
@@ -76,7 +77,7 @@ impl<T> fmt::Debug for HttpsConnector<T> {
7677
7778impl < H , C > From < ( H , C ) > for HttpsConnector < H >
7879where
79- C : Into < Arc < ClientConfig > >
80+ C : Into < Arc < ClientConfig > > ,
8081{
8182 fn from ( ( http, cfg) : ( H , C ) ) -> Self {
8283 HttpsConnector {
0 commit comments