Skip to content

Commit

Permalink
add alpn support
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Nov 27, 2023
1 parent 95ebd8d commit 722c85a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ documentation = "https://docs.rs/hyper-tls"
edition = "2018"

[features]
alpn = ["native-tls/alpn"]
vendored = ["native-tls/vendored"]

[dependencies]
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
19 changes: 17 additions & 2 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,28 @@ impl<T: Write + Read + Unpin> Write for MaybeHttpsStream<T> {
}
}

impl<T: Connection + Unpin> Connection for MaybeHttpsStream<T> {
impl<T: Write + Read + Connection + Unpin> Connection for MaybeHttpsStream<T> {
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<T: std::io::Read + std::io::Write>(s: &native_tls::TlsStream<T>) -> bool {
s.negotiated_alpn()
.unwrap_or(None)
.map(|list| list == &b"h2"[..])
.unwrap_or(false)
}

0 comments on commit 722c85a

Please sign in to comment.