diff --git a/Cargo.toml b/Cargo.toml index cfdb9a7..ee93925 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ documentation = "https://docs.rs/hyper-tls" edition = "2018" [features] +alpn = ["native-tls/alpn"] vendored = ["native-tls/vendored"] [dependencies] diff --git a/src/lib.rs b/src/lib.rs index c832c1c..5c6369e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,10 @@ //! Ok(()) //! } //! ``` +//! +//! ## Crate Features +//! +//! - `alpn`: Enables `native-tls/alpn`, and if `h2` is negotiated, tells hyper. #![doc(html_root_url = "https://docs.rs/hyper-tls/0.5.0")] #![cfg_attr(test, deny(warnings))] #![deny(missing_docs)] diff --git a/src/stream.rs b/src/stream.rs index 13dec57..a78c331 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -111,13 +111,28 @@ impl Write for MaybeHttpsStream { } } -impl Connection for MaybeHttpsStream { +impl Connection for MaybeHttpsStream { fn connected(&self) -> Connected { match self { MaybeHttpsStream::Http(s) => s.connected(), MaybeHttpsStream::Https(s) => { - s.inner().get_ref().get_ref().get_ref().inner().connected() + let c = s.inner().get_ref().get_ref().get_ref().inner().connected(); + #[cfg(feature = "alpn")] + { + if negotiated_h2(s.inner().get_ref()) { + return c.negotiated_h2(); + } + } + c } } } } + +#[cfg(feature = "alpn")] +fn negotiated_h2(s: &native_tls::TlsStream) -> bool { + s.negotiated_alpn() + .unwrap_or(None) + .map(|list| list == &b"h2"[..]) + .unwrap_or(false) +}