From 089f67b1da1c71c6d472ad88235b8a6c8db4c86c 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 | 22 ++++++++++++++++++++-- 3 files changed, 25 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..4e34800 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -111,13 +111,31 @@ 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) +}