From 722c85ab4c2dd39d0c51bd7d1bc5f885d8eb28bc Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 27 Nov 2023 10:29:27 -0500 Subject: [PATCH] add alpn support --- Cargo.toml | 1 + src/lib.rs | 4 ++++ src/stream.rs | 19 +++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) 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) +}