@@ -18,6 +18,7 @@ type BoxError = Box<dyn std::error::Error + Send + Sync>;
18
18
/// A Connector for the `https` scheme.
19
19
#[ derive( Clone ) ]
20
20
pub struct HttpsConnector < T > {
21
+ force_https : bool ,
21
22
http : T ,
22
23
tls_config : Arc < ClientConfig > ,
23
24
}
@@ -57,6 +58,13 @@ impl HttpsConnector<HttpConnector> {
57
58
Self :: build ( config)
58
59
}
59
60
61
+ /// Force the use of HTTPS when connecting.
62
+ ///
63
+ /// If a URL is not `https` when connecting, an error is returned. Disabled by default.
64
+ pub fn https_only ( & mut self , enable : bool ) {
65
+ self . force_https = enable;
66
+ }
67
+
60
68
fn build ( mut config : ClientConfig ) -> Self {
61
69
let mut http = HttpConnector :: new ( ) ;
62
70
http. enforce_http ( false ) ;
@@ -79,7 +87,9 @@ impl HttpsConnector<HttpConnector> {
79
87
80
88
impl < T > fmt:: Debug for HttpsConnector < T > {
81
89
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
82
- f. debug_struct ( "HttpsConnector" ) . finish ( )
90
+ f. debug_struct ( "HttpsConnector" )
91
+ . field ( "force_https" , & self . force_https )
92
+ . finish ( )
83
93
}
84
94
}
85
95
89
99
{
90
100
fn from ( ( http, cfg) : ( H , C ) ) -> Self {
91
101
HttpsConnector {
102
+ force_https : false ,
92
103
http,
93
104
tls_config : cfg. into ( ) ,
94
105
}
@@ -120,7 +131,11 @@ where
120
131
fn call ( & mut self , dst : Uri ) -> Self :: Future {
121
132
let is_https = dst. scheme_str ( ) == Some ( "https" ) ;
122
133
123
- if !is_https {
134
+ if !is_https && self . force_https {
135
+ // Early abort if HTTPS is forced but can't be used
136
+ let err = io:: Error :: new ( io:: ErrorKind :: Other , "https required but URI was not https" ) ;
137
+ Box :: pin ( async move { Err ( err. into ( ) ) } )
138
+ } else if !is_https {
124
139
let connecting_future = self . http . call ( dst) ;
125
140
126
141
let f = async move {
0 commit comments