Skip to content

Commit 59948c6

Browse files
boxdotdjc
authored andcommitted
Add force_https flag which enforces https only connections.
1 parent 2c673c0 commit 59948c6

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/connector.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type BoxError = Box<dyn std::error::Error + Send + Sync>;
1818
/// A Connector for the `https` scheme.
1919
#[derive(Clone)]
2020
pub struct HttpsConnector<T> {
21+
force_https: bool,
2122
http: T,
2223
tls_config: Arc<ClientConfig>,
2324
}
@@ -57,6 +58,13 @@ impl HttpsConnector<HttpConnector> {
5758
Self::build(config)
5859
}
5960

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+
6068
fn build(mut config: ClientConfig) -> Self {
6169
let mut http = HttpConnector::new();
6270
http.enforce_http(false);
@@ -79,7 +87,9 @@ impl HttpsConnector<HttpConnector> {
7987

8088
impl<T> fmt::Debug for HttpsConnector<T> {
8189
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()
8393
}
8494
}
8595

@@ -89,6 +99,7 @@ where
8999
{
90100
fn from((http, cfg): (H, C)) -> Self {
91101
HttpsConnector {
102+
force_https: false,
92103
http,
93104
tls_config: cfg.into(),
94105
}
@@ -120,7 +131,11 @@ where
120131
fn call(&mut self, dst: Uri) -> Self::Future {
121132
let is_https = dst.scheme_str() == Some("https");
122133

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 {
124139
let connecting_future = self.http.call(dst);
125140

126141
let f = async move {

0 commit comments

Comments
 (0)