Skip to content

Commit 6437122

Browse files
committed
Invert Connector scheme matching to reduce rightward drift
1 parent 5650a59 commit 6437122

File tree

1 file changed

+56
-53
lines changed

1 file changed

+56
-53
lines changed

src/connector.rs

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -60,61 +60,64 @@ 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-
if let Some(sch) = dst.scheme() {
64-
if sch == &http::uri::Scheme::HTTP && !self.force_https {
65-
let connecting_future = self.http.call(dst);
66-
67-
let f = async move {
68-
let tcp = connecting_future
69-
.await
70-
.map_err(Into::into)?;
71-
72-
Ok(MaybeHttpsStream::Http(tcp))
73-
};
74-
Box::pin(f)
75-
} else if sch == &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-
}
89-
90-
let hostname = match ServerName::try_from(hostname) {
91-
Ok(dnsname) => dnsname.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 =
113-
io::Error::new(io::ErrorKind::Other, format!("Unsupported scheme {}", sch));
114-
Box::pin(async move { Err(err.into()) })
63+
let scheme = match dst.scheme() {
64+
Some(scheme) => scheme,
65+
None => {
66+
return Box::pin(async move {
67+
Err(io::Error::new(io::ErrorKind::Other, "missing scheme").into())
68+
})
69+
}
70+
};
71+
72+
if scheme == &http::uri::Scheme::HTTP && !self.force_https {
73+
let connecting_future = self.http.call(dst);
74+
75+
let f = async move {
76+
let tcp = connecting_future
77+
.await
78+
.map_err(Into::into)?;
79+
80+
Ok(MaybeHttpsStream::Http(tcp))
81+
};
82+
Box::pin(f)
83+
} else if scheme == &http::uri::Scheme::HTTPS {
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+
};
89+
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;
11596
}
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)
116119
} else {
117-
let err = io::Error::new(io::ErrorKind::Other, "Missing scheme");
120+
let err = io::Error::new(io::ErrorKind::Other, format!("Unsupported scheme {scheme}"));
118121
Box::pin(async move { Err(err.into()) })
119122
}
120123
}

0 commit comments

Comments
 (0)