Skip to content

Commit 5ec094c

Browse files
authoredFeb 8, 2022
feat(client): implement the HTTP/2 extended CONNECT protocol from RFC 8441 (#2682)
1 parent 6932896 commit 5ec094c

File tree

7 files changed

+120
-3
lines changed

7 files changed

+120
-3
lines changed
 

‎src/client/conn.rs

+17
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,23 @@ where
487487
Poll::Ready(Ok(conn.take().unwrap().into_parts()))
488488
})
489489
}
490+
491+
/// Returns whether the [extended CONNECT protocol][1] is enabled or not.
492+
///
493+
/// This setting is configured by the server peer by sending the
494+
/// [`SETTINGS_ENABLE_CONNECT_PROTOCOL` parameter][2] in a `SETTINGS` frame.
495+
/// This method returns the currently acknowledged value recieved from the
496+
/// remote.
497+
///
498+
/// [1]: https://datatracker.ietf.org/doc/html/rfc8441#section-4
499+
/// [2]: https://datatracker.ietf.org/doc/html/rfc8441#section-3
500+
#[cfg(feature = "http2")]
501+
pub fn http2_is_extended_connect_protocol_enabled(&self) -> bool {
502+
match self.inner.as_ref().unwrap() {
503+
ProtoClient::H1 { .. } => false,
504+
ProtoClient::H2 { h2 } => h2.is_extended_connect_protocol_enabled(),
505+
}
506+
}
490507
}
491508

492509
impl<T, B> Future for Connection<T, B>

‎src/ext.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,67 @@
1-
//! HTTP extensions
1+
//! HTTP extensions.
22
33
use bytes::Bytes;
44
#[cfg(feature = "http1")]
55
use http::header::{HeaderName, IntoHeaderName, ValueIter};
66
use http::HeaderMap;
7+
#[cfg(feature = "http2")]
8+
use std::fmt;
9+
10+
#[cfg(feature = "http2")]
11+
/// Represents the `:protocol` pseudo-header used by
12+
/// the [Extended CONNECT Protocol].
13+
///
14+
/// [Extended CONNECT Protocol]: https://datatracker.ietf.org/doc/html/rfc8441#section-4
15+
#[derive(Clone, Eq, PartialEq)]
16+
pub struct Protocol {
17+
inner: h2::ext::Protocol,
18+
}
19+
20+
#[cfg(feature = "http2")]
21+
impl Protocol {
22+
/// Converts a static string to a protocol name.
23+
pub const fn from_static(value: &'static str) -> Self {
24+
Self {
25+
inner: h2::ext::Protocol::from_static(value),
26+
}
27+
}
28+
29+
/// Returns a str representation of the header.
30+
pub fn as_str(&self) -> &str {
31+
self.inner.as_str()
32+
}
33+
34+
pub(crate) fn from_inner(inner: h2::ext::Protocol) -> Self {
35+
Self { inner }
36+
}
37+
38+
pub(crate) fn into_inner(self) -> h2::ext::Protocol {
39+
self.inner
40+
}
41+
}
42+
43+
#[cfg(feature = "http2")]
44+
impl<'a> From<&'a str> for Protocol {
45+
fn from(value: &'a str) -> Self {
46+
Self {
47+
inner: h2::ext::Protocol::from(value),
48+
}
49+
}
50+
}
51+
52+
#[cfg(feature = "http2")]
53+
impl AsRef<[u8]> for Protocol {
54+
fn as_ref(&self) -> &[u8] {
55+
self.inner.as_ref()
56+
}
57+
}
58+
59+
#[cfg(feature = "http2")]
60+
impl fmt::Debug for Protocol {
61+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62+
self.inner.fmt(f)
63+
}
64+
}
765

866
/// A map from header names to their original casing as received in an HTTP message.
967
///

‎src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod cfg;
7676
mod common;
7777
pub mod body;
7878
mod error;
79-
mod ext;
79+
pub mod ext;
8080
#[cfg(test)]
8181
mod mock;
8282
pub mod rt;

‎src/proto/h2/client.rs

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use tracing::{debug, trace, warn};
1414
use super::{ping, H2Upgraded, PipeToSendStream, SendBuf};
1515
use crate::body::HttpBody;
1616
use crate::common::{exec::Exec, task, Future, Never, Pin, Poll};
17+
use crate::ext::Protocol;
1718
use crate::headers;
1819
use crate::proto::h2::UpgradedSendStream;
1920
use crate::proto::Dispatched;
@@ -204,6 +205,15 @@ where
204205
req_rx: ClientRx<B>,
205206
}
206207

208+
impl<B> ClientTask<B>
209+
where
210+
B: HttpBody + 'static,
211+
{
212+
pub(crate) fn is_extended_connect_protocol_enabled(&self) -> bool {
213+
self.h2_tx.is_extended_connect_protocol_enabled()
214+
}
215+
}
216+
207217
impl<B> Future for ClientTask<B>
208218
where
209219
B: HttpBody + Send + 'static,
@@ -260,6 +270,10 @@ where
260270
}
261271
}
262272

273+
if let Some(protocol) = req.extensions_mut().remove::<Protocol>() {
274+
req.extensions_mut().insert(protocol.into_inner());
275+
}
276+
263277
let (fut, body_tx) = match self.h2_tx.send_request(req, !is_connect && eos) {
264278
Ok(ok) => ok,
265279
Err(err) => {

‎src/proto/h2/server.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use super::{ping, PipeToSendStream, SendBuf};
1515
use crate::body::HttpBody;
1616
use crate::common::exec::ConnStreamExec;
1717
use crate::common::{date, task, Future, Pin, Poll};
18+
use crate::ext::Protocol;
1819
use crate::headers;
1920
use crate::proto::h2::ping::Recorder;
2021
use crate::proto::h2::{H2Upgraded, UpgradedSendStream};
@@ -41,6 +42,7 @@ pub(crate) struct Config {
4142
pub(crate) initial_conn_window_size: u32,
4243
pub(crate) initial_stream_window_size: u32,
4344
pub(crate) max_frame_size: u32,
45+
pub(crate) enable_connect_protocol: bool,
4446
pub(crate) max_concurrent_streams: Option<u32>,
4547
#[cfg(feature = "runtime")]
4648
pub(crate) keep_alive_interval: Option<Duration>,
@@ -56,6 +58,7 @@ impl Default for Config {
5658
initial_conn_window_size: DEFAULT_CONN_WINDOW,
5759
initial_stream_window_size: DEFAULT_STREAM_WINDOW,
5860
max_frame_size: DEFAULT_MAX_FRAME_SIZE,
61+
enable_connect_protocol: false,
5962
max_concurrent_streams: None,
6063
#[cfg(feature = "runtime")]
6164
keep_alive_interval: None,
@@ -117,6 +120,9 @@ where
117120
if let Some(max) = config.max_concurrent_streams {
118121
builder.max_concurrent_streams(max);
119122
}
123+
if config.enable_connect_protocol {
124+
builder.enable_connect_protocol();
125+
}
120126
let handshake = builder.handshake(io);
121127

122128
let bdp = if config.adaptive_window {
@@ -280,7 +286,7 @@ where
280286

281287
let is_connect = req.method() == Method::CONNECT;
282288
let (mut parts, stream) = req.into_parts();
283-
let (req, connect_parts) = if !is_connect {
289+
let (mut req, connect_parts) = if !is_connect {
284290
(
285291
Request::from_parts(
286292
parts,
@@ -307,6 +313,10 @@ where
307313
)
308314
};
309315

316+
if let Some(protocol) = req.extensions_mut().remove::<h2::ext::Protocol>() {
317+
req.extensions_mut().insert(Protocol::from_inner(protocol));
318+
}
319+
310320
let fut = H2Stream::new(service.call(req), connect_parts, respond);
311321
exec.execute_h2stream(fut);
312322
}

‎src/server/conn.rs

+9
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,15 @@ impl<E> Http<E> {
558558
self
559559
}
560560

561+
/// Enables the [extended CONNECT protocol].
562+
///
563+
/// [extended CONNECT protocol]: https://datatracker.ietf.org/doc/html/rfc8441#section-4
564+
#[cfg(feature = "http2")]
565+
pub fn http2_enable_connect_protocol(&mut self) -> &mut Self {
566+
self.h2_builder.enable_connect_protocol = true;
567+
self
568+
}
569+
561570
/// Set the maximum buffer size for the connection.
562571
///
563572
/// Default is ~400kb.

‎src/server/server.rs

+9
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,15 @@ impl<I, E> Builder<I, E> {
453453
self
454454
}
455455

456+
/// Enables the [extended CONNECT protocol].
457+
///
458+
/// [extended CONNECT protocol]: https://datatracker.ietf.org/doc/html/rfc8441#section-4
459+
#[cfg(feature = "http2")]
460+
pub fn http2_enable_connect_protocol(mut self) -> Self {
461+
self.protocol.http2_enable_connect_protocol();
462+
self
463+
}
464+
456465
/// Sets the `Executor` to deal with connection tasks.
457466
///
458467
/// Default is `tokio::spawn`.

0 commit comments

Comments
 (0)
Please sign in to comment.