@@ -22,12 +22,18 @@ pub struct HttpsConnector<T> {
22
22
tls_config : Arc < ClientConfig > ,
23
23
}
24
24
25
- #[ cfg( all(
26
- any( feature = "rustls-native-certs" , feature = "webpki-roots" ) ,
27
- feature = "tokio-runtime"
28
- ) ) ]
29
- impl HttpsConnector < HttpConnector > {
30
- /// Construct a new `HttpsConnector` using the OS root store
25
+ /// A builder that will configure an `HttpsConnector`
26
+ ///
27
+ /// This builder ensures configuration is consistent.
28
+ ///
29
+ /// An alternative way of building an `HttpsConnector`
30
+ /// is to use From/Into.
31
+ pub struct HttpsConnectorBuilder {
32
+ tls_config : ClientConfig ,
33
+ }
34
+
35
+ impl HttpsConnectorBuilder {
36
+ /// Configure using the OS root store for certificate trust
31
37
#[ cfg( feature = "rustls-native-certs" ) ]
32
38
#[ cfg_attr( docsrs, doc( cfg( feature = "rustls-native-certs" ) ) ) ]
33
39
pub fn with_native_roots ( ) -> Self {
@@ -43,27 +49,54 @@ impl HttpsConnector<HttpConnector> {
43
49
if config. root_store . is_empty ( ) {
44
50
panic ! ( "no CA certificates found" ) ;
45
51
}
46
- Self :: build ( config)
52
+ Self { tls_config : config }
53
+ }
54
+
55
+ /// Configure using a custom `rustls::RootCertStore` for certificate trust
56
+ pub fn with_custom_roots ( roots : rustls:: RootCertStore ) -> Self {
57
+ let mut config = ClientConfig :: new ( ) ;
58
+ config. root_store = roots;
59
+ Self { tls_config : config }
47
60
}
48
61
49
- /// Construct a new `HttpsConnector` using the `webpki_roots`
62
+ /// Configure using `webpki_roots` for certificate trust
50
63
#[ cfg( feature = "webpki-roots" ) ]
51
64
#[ cfg_attr( docsrs, doc( cfg( feature = "webpki-roots" ) ) ) ]
52
65
pub fn with_webpki_roots ( ) -> Self {
53
66
let mut config = ClientConfig :: new ( ) ;
54
67
config
55
68
. root_store
56
69
. add_server_trust_anchors ( & webpki_roots:: TLS_SERVER_ROOTS ) ;
57
- Self :: build ( config)
70
+ Self { tls_config : config }
58
71
}
59
72
60
- fn build ( mut config : ClientConfig ) -> Self {
73
+ /// Enable HTTP2
74
+ /// This advertises http2 support in ALPN
75
+ #[ cfg( feature = "http2" ) ]
76
+ #[ cfg_attr( docsrs, doc( cfg( feature = "http2" ) ) ) ]
77
+ pub fn enable_http2 ( mut self ) -> Self {
78
+ self . tls_config . alpn_protocols = vec ! [ b"h2" . to_vec( ) , b"http/1.1" . to_vec( ) ] ;
79
+ self
80
+ }
81
+
82
+ /// Enable certificate transparency
83
+ #[ cfg( feature = "ct-logs" ) ]
84
+ pub fn enable_ct_logs ( mut self ) -> Self {
85
+ self . tls_config . ct_logs = Some ( & ct_logs:: LOGS ) ;
86
+ self
87
+ }
88
+
89
+ /// Built an HttpsConnector<HttpConnector>
90
+ #[ cfg( feature = "tokio-runtime" ) ]
91
+ pub fn build ( self ) -> HttpsConnector < HttpConnector > {
61
92
let mut http = HttpConnector :: new ( ) ;
62
93
http. enforce_http ( false ) ;
94
+ self . wrap_connector ( http)
95
+ }
63
96
64
- config . alpn_protocols = vec ! [ b"h2" . to_vec ( ) , b"http/1.1" . to_vec ( ) ] ;
65
- config . ct_logs = Some ( & ct_logs :: LOGS ) ;
66
- ( http , config ) . into ( )
97
+ /// Built an HttpsConnector with a custom lower-level connector
98
+ pub fn wrap_connector < H > ( self , conn : H ) -> HttpsConnector < H > {
99
+ ( conn , self . tls_config ) . into ( )
67
100
}
68
101
}
69
102
0 commit comments