Skip to content

Commit

Permalink
Merge pull request #299 from Berrysoft/dev/alpn
Browse files Browse the repository at this point in the history
feat(tls): add ALPN support
  • Loading branch information
Berrysoft authored Sep 23, 2024
2 parents 8b80ec0 + 85bdb43 commit 0048966
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions compio-tls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compio-tls"
version = "0.3.0"
version = "0.3.1"
description = "TLS adaptor with compio"
categories = ["asynchronous", "network-programming"]
keywords = ["async", "net", "tls"]
Expand All @@ -18,7 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
compio-buf = { workspace = true }
compio-io = { workspace = true, features = ["compat"] }

native-tls = { version = "0.2.11", optional = true }
native-tls = { version = "0.2.11", optional = true, features = ["alpn"] }
rustls = { workspace = true, default-features = false, optional = true, features = [
"logging",
"std",
Expand Down
16 changes: 15 additions & 1 deletion compio-tls/src/stream/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io, mem::MaybeUninit};
use std::{borrow::Cow, io, mem::MaybeUninit};

use compio_buf::{BufResult, IoBuf, IoBufMut};
use compio_io::{compat::SyncStream, AsyncRead, AsyncWrite};
Expand All @@ -24,6 +24,15 @@ impl<S> TlsStreamInner<S> {
Self::Rustls(s) => s.get_mut(),
}
}

pub fn negotiated_alpn(&self) -> Option<Cow<[u8]>> {
match self {
#[cfg(feature = "native-tls")]
Self::NativeTls(s) => s.negotiated_alpn().ok().flatten().map(Cow::from),
#[cfg(feature = "rustls")]
Self::Rustls(s) => s.negotiated_alpn().map(Cow::from),
}
}
}

impl<S> io::Read for TlsStreamInner<S> {
Expand Down Expand Up @@ -87,6 +96,11 @@ impl<S> TlsStream<S> {
pub(crate) fn new_rustls_server(s: SyncStream<S>, conn: rustls::ServerConnection) -> Self {
Self(TlsStreamInner::Rustls(rtls::TlsStream::new_server(s, conn)))
}

/// Returns the negotiated ALPN protocol.
pub fn negotiated_alpn(&self) -> Option<Cow<[u8]>> {
self.0.negotiated_alpn()
}
}

#[cfg(feature = "native-tls")]
Expand Down
7 changes: 7 additions & 0 deletions compio-tls/src/stream/rtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ impl<S> TlsStream<S> {
pub fn get_mut(&mut self) -> &mut S {
&mut self.inner
}

pub fn negotiated_alpn(&self) -> Option<&[u8]> {
match &self.conn {
TlsConnection::Client(client) => client.alpn_protocol(),
TlsConnection::Server(server) => server.alpn_protocol(),
}
}
}

impl<S: io::Read> TlsStream<S> {
Expand Down

0 comments on commit 0048966

Please sign in to comment.