Skip to content

Commit 57f34ba

Browse files
yukiiiterudjc
authored andcommitted
feat: add alpn_protocols for TlsConnector::connect_with
1 parent dd5f27d commit 57f34ba

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ rust-version = "1.71"
1313
exclude = ["/.github", "/examples", "/scripts"]
1414

1515
[dependencies]
16-
rustls = { version = "0.23.22", default-features = false, features = ["std"] }
16+
rustls = { version = "0.23.27", default-features = false, features = ["std"] }
1717
tokio = "1.0"
1818

1919
[features]

src/lib.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,31 @@ impl TlsConnector {
115115
where
116116
IO: AsyncRead + AsyncWrite + Unpin,
117117
{
118-
self.connect_with(domain, stream, |_| ())
118+
self.connect_impl(domain, stream, None, |_| ())
119119
}
120120

121+
#[inline]
121122
pub fn connect_with<IO, F>(&self, domain: ServerName<'static>, stream: IO, f: F) -> Connect<IO>
122123
where
123124
IO: AsyncRead + AsyncWrite + Unpin,
124125
F: FnOnce(&mut ClientConnection),
125126
{
126-
let mut session = match ClientConnection::new(self.inner.clone(), domain) {
127+
self.connect_impl(domain, stream, None, f)
128+
}
129+
130+
fn connect_impl<IO, F>(
131+
&self,
132+
domain: ServerName<'static>,
133+
stream: IO,
134+
alpn_protocols: Option<Vec<Vec<u8>>>,
135+
f: F,
136+
) -> Connect<IO>
137+
where
138+
IO: AsyncRead + AsyncWrite + Unpin,
139+
F: FnOnce(&mut ClientConnection),
140+
{
141+
let alpn = alpn_protocols.unwrap_or_else(|| self.inner.alpn_protocols.clone());
142+
let mut session = match ClientConnection::new_with_alpn(self.inner.clone(), domain, alpn) {
127143
Ok(session) => session,
128144
Err(error) => {
129145
return Connect(MidHandshake::Error {
@@ -158,12 +174,45 @@ impl TlsConnector {
158174
}))
159175
}
160176

177+
pub fn with_alpn(&self, alpn_protocols: Vec<Vec<u8>>) -> TlsConnectorWithAlpn<'_> {
178+
TlsConnectorWithAlpn {
179+
inner: self,
180+
alpn_protocols,
181+
}
182+
}
183+
161184
/// Get a read-only reference to underlying config
162185
pub fn config(&self) -> &Arc<ClientConfig> {
163186
&self.inner
164187
}
165188
}
166189

190+
pub struct TlsConnectorWithAlpn<'c> {
191+
inner: &'c TlsConnector,
192+
alpn_protocols: Vec<Vec<u8>>,
193+
}
194+
195+
impl TlsConnectorWithAlpn<'_> {
196+
#[inline]
197+
pub fn connect<IO>(self, domain: ServerName<'static>, stream: IO) -> Connect<IO>
198+
where
199+
IO: AsyncRead + AsyncWrite + Unpin,
200+
{
201+
self.inner
202+
.connect_impl(domain, stream, Some(self.alpn_protocols), |_| ())
203+
}
204+
205+
#[inline]
206+
pub fn connect_with<IO, F>(self, domain: ServerName<'static>, stream: IO, f: F) -> Connect<IO>
207+
where
208+
IO: AsyncRead + AsyncWrite + Unpin,
209+
F: FnOnce(&mut ClientConnection),
210+
{
211+
self.inner
212+
.connect_impl(domain, stream, Some(self.alpn_protocols), f)
213+
}
214+
}
215+
167216
impl TlsAcceptor {
168217
#[inline]
169218
pub fn accept<IO>(&self, stream: IO) -> Accept<IO>

0 commit comments

Comments
 (0)