Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ALPN for native-tls users #1283

Merged
merged 4 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ default-tls = ["hyper-tls", "native-tls-crate", "__tls", "tokio-native-tls"]

# Enables native-tls specific functionality not available by default.
native-tls = ["default-tls"]
native-tls-alpn = ["native-tls", "native-tls-crate/alpn"]
native-tls-vendored = ["native-tls", "native-tls-crate/vendored"]

rustls-tls = ["rustls-tls-webpki-roots"]
Expand Down Expand Up @@ -197,7 +198,6 @@ path = "examples/form.rs"
[[example]]
name = "simple"
path = "examples/simple.rs"
required-features = ["deflate"]

[[test]]
name = "blocking"
Expand Down
9 changes: 9 additions & 0 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ impl ClientBuilder {
TlsBackend::Default => {
let mut tls = TlsConnector::builder();

#[cfg(feature = "native-tls-alpn")]
{
if config.http2_only {
tls.request_alpns(&["h2"]);
} else {
tls.request_alpns(&["h2", "http/1.1"]);
}
}

#[cfg(feature = "native-tls")]
{
tls.danger_accept_invalid_hostnames(!config.hostname_verification);
Expand Down
31 changes: 25 additions & 6 deletions src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,20 @@ impl Connector {
let mut http = hyper_tls::HttpsConnector::from((http, tls_connector));
let io = http.call(dst).await?;

if let hyper_tls::MaybeHttpsStream::Https(stream) = &io {
if let hyper_tls::MaybeHttpsStream::Https(stream) = io {
if !self.nodelay {
stream.get_ref().get_ref().get_ref().set_nodelay(false)?;
}
Ok(Conn {
inner: self.verbose.wrap(NativeTlsConn { inner: stream }),
is_proxy,
})
} else {
Ok(Conn {
inner: self.verbose.wrap(io),
is_proxy,
})
}

Ok(Conn {
inner: self.verbose.wrap(io),
is_proxy,
})
}
#[cfg(feature = "__rustls")]
Inner::RustlsTls { http, tls, .. } => {
Expand Down Expand Up @@ -686,6 +690,21 @@ mod native_tls_conn {
}

impl<T: Connection + AsyncRead + AsyncWrite + Unpin> Connection for NativeTlsConn<T> {
#[cfg(feature = "native-tls-alpn")]
fn connected(&self) -> Connected {
match self.inner.get_ref().negotiated_alpn().ok() {
Some(Some(alpn_protocol)) if alpn_protocol == b"h2" => self
.inner
.get_ref()
.get_ref()
.get_ref()
.connected()
.negotiated_h2(),
_ => self.inner.get_ref().get_ref().get_ref().connected(),
}
}

#[cfg(not(feature = "native-tls-alpn"))]
fn connected(&self) -> Connected {
self.inner.get_ref().get_ref().get_ref().connected()
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
//! over HTTPS.
//! - **native-tls**: Enables TLS functionality provided by `native-tls`.
//! - **native-tls-vendored**: Enables the `vendored` feature of `native-tls`.
//! - **native-tls-alpn**: Enables the `alpn` feature of `native-tls`.
//! - **rustls-tls**: Enables TLS functionality provided by `rustls`.
//! Equivalent to `rustls-tls-webpki-roots`.
//! - **rustls-tls-manual-roots**: Enables TLS functionality provided by `rustls`,
Expand Down