Skip to content

Commit

Permalink
Invert Connector scheme matching to reduce rightward drift
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Dec 11, 2023
1 parent 5650a59 commit 6437122
Showing 1 changed file with 56 additions and 53 deletions.
109 changes: 56 additions & 53 deletions src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,61 +60,64 @@ where
fn call(&mut self, dst: Uri) -> Self::Future {
// dst.scheme() would need to derive Eq to be matchable;
// use an if cascade instead
if let Some(sch) = dst.scheme() {
if sch == &http::uri::Scheme::HTTP && !self.force_https {
let connecting_future = self.http.call(dst);

let f = async move {
let tcp = connecting_future
.await
.map_err(Into::into)?;

Ok(MaybeHttpsStream::Http(tcp))
};
Box::pin(f)
} else if sch == &http::uri::Scheme::HTTPS {
let cfg = self.tls_config.clone();
let mut hostname = match self.override_server_name.as_deref() {
Some(h) => h,
None => dst.host().unwrap_or_default(),
};

// Remove square brackets around IPv6 address.
if let Some(trimmed) = hostname
.strip_prefix('[')
.and_then(|h| h.strip_suffix(']'))
{
hostname = trimmed;
}

let hostname = match ServerName::try_from(hostname) {
Ok(dnsname) => dnsname.to_owned(),
Err(_) => {
let err = io::Error::new(io::ErrorKind::Other, "invalid dnsname");
return Box::pin(async move { Err(Box::new(err).into()) });
}
};
let connecting_future = self.http.call(dst);

let f = async move {
let tcp = connecting_future
.await
.map_err(Into::into)?;
let connector = TlsConnector::from(cfg);
let tls = connector
.connect(hostname, tcp)
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
Ok(MaybeHttpsStream::Https(tls))
};
Box::pin(f)
} else {
let err =
io::Error::new(io::ErrorKind::Other, format!("Unsupported scheme {}", sch));
Box::pin(async move { Err(err.into()) })
let scheme = match dst.scheme() {
Some(scheme) => scheme,
None => {
return Box::pin(async move {
Err(io::Error::new(io::ErrorKind::Other, "missing scheme").into())
})
}
};

if scheme == &http::uri::Scheme::HTTP && !self.force_https {
let connecting_future = self.http.call(dst);

let f = async move {
let tcp = connecting_future
.await
.map_err(Into::into)?;

Ok(MaybeHttpsStream::Http(tcp))
};
Box::pin(f)
} else if scheme == &http::uri::Scheme::HTTPS {
let cfg = self.tls_config.clone();
let mut hostname = match self.override_server_name.as_deref() {
Some(h) => h,
None => dst.host().unwrap_or_default(),
};

// Remove square brackets around IPv6 address.
if let Some(trimmed) = hostname
.strip_prefix('[')
.and_then(|h| h.strip_suffix(']'))
{
hostname = trimmed;
}

let hostname = match ServerName::try_from(hostname) {
Ok(dns_name) => dns_name.to_owned(),
Err(_) => {
let err = io::Error::new(io::ErrorKind::Other, "invalid dnsname");
return Box::pin(async move { Err(Box::new(err).into()) });
}
};
let connecting_future = self.http.call(dst);

let f = async move {
let tcp = connecting_future
.await
.map_err(Into::into)?;
let connector = TlsConnector::from(cfg);
let tls = connector
.connect(hostname, tcp)
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
Ok(MaybeHttpsStream::Https(tls))
};
Box::pin(f)
} else {
let err = io::Error::new(io::ErrorKind::Other, "Missing scheme");
let err = io::Error::new(io::ErrorKind::Other, format!("Unsupported scheme {scheme}"));
Box::pin(async move { Err(err.into()) })
}
}
Expand Down

0 comments on commit 6437122

Please sign in to comment.