Skip to content

Commit a6ee2fe

Browse files
committed
Reduce rightward drift by handling simple schemes
1 parent b8d0c2f commit a6ee2fe

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

src/connector.rs

+47-43
Original file line numberDiff line numberDiff line change
@@ -60,58 +60,62 @@ where
6060
fn call(&mut self, dst: Uri) -> Self::Future {
6161
// dst.scheme() would need to derive Eq to be matchable;
6262
// use an if cascade instead
63-
let scheme = match dst.scheme() {
64-
Some(scheme) => scheme,
63+
match dst.scheme() {
64+
Some(scheme) if scheme == &http::uri::Scheme::HTTP => {
65+
let future = self.http.call(dst);
66+
return Box::pin(async move {
67+
Ok(MaybeHttpsStream::Http(future.await.map_err(Into::into)?))
68+
});
69+
}
70+
Some(scheme) if scheme != &http::uri::Scheme::HTTPS => {
71+
let message = format!("unsupported scheme {scheme}");
72+
return Box::pin(async move {
73+
Err(io::Error::new(io::ErrorKind::Other, message).into())
74+
});
75+
}
76+
Some(_) => {}
6577
None => {
6678
return Box::pin(async move {
6779
Err(io::Error::new(io::ErrorKind::Other, "missing scheme").into())
6880
})
6981
}
7082
};
7183

72-
if scheme == &http::uri::Scheme::HTTP && !self.force_https {
73-
let future = self.http.call(dst);
74-
Box::pin(async move { Ok(MaybeHttpsStream::Http(future.await.map_err(Into::into)?)) })
75-
} else if scheme == &http::uri::Scheme::HTTPS {
76-
let cfg = self.tls_config.clone();
77-
let mut hostname = match self.override_server_name.as_deref() {
78-
Some(h) => h,
79-
None => dst.host().unwrap_or_default(),
80-
};
81-
82-
// Remove square brackets around IPv6 address.
83-
if let Some(trimmed) = hostname
84-
.strip_prefix('[')
85-
.and_then(|h| h.strip_suffix(']'))
86-
{
87-
hostname = trimmed;
88-
}
84+
let cfg = self.tls_config.clone();
85+
let mut hostname = match self.override_server_name.as_deref() {
86+
Some(h) => h,
87+
None => dst.host().unwrap_or_default(),
88+
};
8989

90-
let hostname = match ServerName::try_from(hostname) {
91-
Ok(dns_name) => dns_name.to_owned(),
92-
Err(_) => {
93-
let err = io::Error::new(io::ErrorKind::Other, "invalid dnsname");
94-
return Box::pin(async move { Err(Box::new(err).into()) });
95-
}
96-
};
97-
let connecting_future = self.http.call(dst);
98-
99-
let f = async move {
100-
let tcp = connecting_future
101-
.await
102-
.map_err(Into::into)?;
103-
let connector = TlsConnector::from(cfg);
104-
let tls = connector
105-
.connect(hostname, tcp)
106-
.await
107-
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
108-
Ok(MaybeHttpsStream::Https(tls))
109-
};
110-
Box::pin(f)
111-
} else {
112-
let err = io::Error::new(io::ErrorKind::Other, format!("Unsupported scheme {scheme}"));
113-
Box::pin(async move { Err(err.into()) })
90+
// Remove square brackets around IPv6 address.
91+
if let Some(trimmed) = hostname
92+
.strip_prefix('[')
93+
.and_then(|h| h.strip_suffix(']'))
94+
{
95+
hostname = trimmed;
11496
}
97+
98+
let hostname = match ServerName::try_from(hostname) {
99+
Ok(dns_name) => dns_name.to_owned(),
100+
Err(_) => {
101+
let err = io::Error::new(io::ErrorKind::Other, "invalid dnsname");
102+
return Box::pin(async move { Err(Box::new(err).into()) });
103+
}
104+
};
105+
let connecting_future = self.http.call(dst);
106+
107+
let f = async move {
108+
let tcp = connecting_future
109+
.await
110+
.map_err(Into::into)?;
111+
let connector = TlsConnector::from(cfg);
112+
let tls = connector
113+
.connect(hostname, tcp)
114+
.await
115+
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
116+
Ok(MaybeHttpsStream::Https(tls))
117+
};
118+
Box::pin(f)
115119
}
116120
}
117121

0 commit comments

Comments
 (0)