Skip to content

Commit

Permalink
Add native-tls-alpn feature (#1283)
Browse files Browse the repository at this point in the history
  • Loading branch information
ducaale authored Jun 10, 2021
1 parent bbeb1ed commit b48cb4a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
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

0 comments on commit b48cb4a

Please sign in to comment.