Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 76 additions & 116 deletions examples/webtransport_server.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use anyhow::{Context, Result};
use bytes::Bytes;
use futures::StreamExt;
use h3::{
ext::Protocol,
quic::{self},
server::Connection,
};
use h3_quinn::quinn::{self, crypto::rustls::QuicServerConfig};
use h3_webtransport::server::{self, WebTransportSession};
use h3_webtransport::server::{self, IncomingStream, WebTransportConnection, WebTransportSession};
use http::Method;
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use std::{net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};
Expand Down Expand Up @@ -121,21 +122,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await
.unwrap();

// tracing::info!("Establishing WebTransport session");
// // 3. TODO: Conditionally, if the client indicated that this is a webtransport session, we should accept it here, else use regular h3.
// // if this is a webtransport session, then h3 needs to stop handing the datagrams, bidirectional streams, and unidirectional streams and give them
// // to the webtransport session.

if let Err(err) = handle_connection(h3_conn).await {
tracing::error!("Failed to handle connection: {err:?}");
}

// let mut session: WebTransportSession<_, Bytes> =
// WebTransportSession::accept(h3_conn).await.unwrap();
// tracing::info!("Finished establishing webtransport session");
// // 4. Get datagrams, bidirectional streams, and unidirectional streams and wait for client requests here.
// // h3_conn needs to hand over the datagrams, bidirectional streams, and unidirectional streams to the webtransport session.
// let result = handle.await;
}
Err(err) => {
error!("accepting connection failed: {:?}", err);
Expand All @@ -151,64 +140,93 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

async fn handle_connection(mut conn: Connection<h3_quinn::Connection, Bytes>) -> Result<()> {
// 3. TODO: Conditionally, if the client indicated that this is a webtransport session, we should accept it here, else use regular h3.
// if this is a webtransport session, then h3 needs to stop handing the datagrams, bidirectional streams, and unidirectional streams and give them
// to the webtransport session.
macro_rules! log_result {
($expr:expr) => {
if let Err(err) = $expr {
tracing::error!("{err:?}");
}
};
}

async fn handle_connection(conn: Connection<h3_quinn::Connection, Bytes>) -> Result<()> {
type Session = WebTransportSession<h3_quinn::Connection, Bytes>;
let mut driver = WebTransportConnection::new(conn);
let mut datagrams = driver.datagram_reader();
let mut resolving = futures::stream::FuturesUnordered::new();
let mut accepting = futures::stream::FuturesUnordered::new();
let mut tasks = tokio::task::JoinSet::new();
let mut sessions: std::collections::HashMap<_, Arc<Session>> = std::collections::HashMap::new();

loop {
match conn.accept().await {
Ok(Some(resolver)) => {
// TODO: resolve request in a different task to not block the accept loop
let (req, stream) = match resolver.resolve_request().await {
Ok(request) => request,
Err(err) => {
error!("error resolving request: {err:?}");
continue;
tokio::select! {
biased;
// Register completed sessions before processing their input streams.
Some(session) = accepting.next() => {
let session: Arc<Session> = Arc::new(session?);
let id = session.session_id();
sessions.insert(id, session.clone());
tasks.spawn(async move {
open_bidi_test(session.open_bi().await?).await
});
}
incoming = driver.accept() => match incoming? {
Some(IncomingStream::Bidirectional(resolver)) => {
resolving.push(resolver.resolve());
}
Some(IncomingStream::Unidirectional(id, stream)) => {
if let Some(session) = sessions.get(&id).cloned() {
tasks.spawn(async move {
let send = session.open_uni().await?;
echo_stream(send, stream).await
});
}
};
info!("new request: {:#?}", req);

let ext = req.extensions();
match req.method() {
&Method::CONNECT if ext.get::<Protocol>() == Some(&Protocol::WEB_TRANSPORT) => {
tracing::info!("Peer wants to initiate a webtransport session");

tracing::info!("Handing over connection to WebTransport");
let session = WebTransportSession::accept(req, stream, conn).await?;
tracing::info!("Established webtransport session");
// 4. Get datagrams, bidirectional streams, and unidirectional streams and wait for client requests here.
// h3_conn needs to hand over the datagrams, bidirectional streams, and unidirectional streams to the webtransport session.
handle_session_and_echo_all_inbound_messages(session).await?;

return Ok(());
}
None => break,
},
Some(resolved) = resolving.next() => match resolved? {
server::AcceptedBi::Request(req, mut stream) => {
if req.method() == Method::CONNECT
&& req.extensions().get::<Protocol>() == Some(&Protocol::WEB_TRANSPORT)
{
// The future owns only session resources; acceptance keeps running.
accepting.push(driver.accept_session(req, stream));
} else {
tasks.spawn(async move {
stream.send_response(http::Response::new(())).await?;
stream.finish().await?;
Ok(())
});
}
_ => {
tracing::info!(?req, "Received request");
}
server::AcceptedBi::BidiStream(id, stream) => {
if sessions.contains_key(&id) {
let (send, recv) = quic::BidiStream::split(stream);
tasks.spawn(echo_stream(send, recv));
}
}
},
datagram = datagrams.read_datagram() => {
let datagram = datagram?;
let id = datagram.stream_id().into();
if let Some(session) = sessions.get(&id) {
session.send_datagram(datagram.into_payload())?;
}
}
// indicating no more streams to be received
Ok(None) => {
break;
}
Err(err) => {
error!("Connection errored with {}", err);
break;
Some(result) = tasks.join_next() => {
log_result!(result?);
}
}
}
while let Some(session) = accepting.next().await {
let session = session?;
sessions.insert(session.session_id(), Arc::new(session));
}
while let Some(result) = tasks.join_next().await {
log_result!(result?);
}
Ok(())
}

macro_rules! log_result {
($expr:expr) => {
if let Err(err) = $expr {
tracing::error!("{err:?}");
}
};
}

async fn echo_stream<T, R>(send: T, recv: R) -> anyhow::Result<()>
where
T: AsyncWrite,
Expand Down Expand Up @@ -259,61 +277,3 @@ where

Ok(())
}

/// This method will echo all inbound datagrams, unidirectional and bidirectional streams.
async fn handle_session_and_echo_all_inbound_messages(
session: WebTransportSession<h3_quinn::Connection, Bytes>,
) -> anyhow::Result<()> {
let session_id = session.session_id();

// This will open a bidirectional stream and send a message to the client right after connecting!
let stream = session.open_bi(session_id).await?;

tokio::spawn(async move { log_result!(open_bidi_test(stream).await) });

let mut datagram_reader = session.datagram_reader();
let mut datagram_sender = session.datagram_sender();

loop {
tokio::select! {
datagram = datagram_reader.read_datagram() => {
let datagram = match datagram {
Ok(datagram) => datagram,
Err(err) => {
tracing::error!("Failed to read datagram: {err:?}");
break;
}
};
tracing::info!("Received datagram: {datagram:?}");
let datagram = datagram.into_payload();
datagram_sender.send_datagram(datagram)?;
}
uni_stream = session.accept_uni() => {
let (id, stream) = uni_stream?.unwrap();

let send = session.open_uni(id).await?;
tokio::spawn( async move { log_result!(echo_stream(send, stream).await); });
}
stream = session.accept_bi() => {
if let Some(resolver) = stream? {
tokio::spawn(async move {
log_result!(async move {
if let server::AcceptedBi::BidiStream(_, stream) = resolver.resolve().await? {
let (send, recv) = quic::BidiStream::split(stream);
echo_stream(send, recv).await?;
}
Ok::<_, anyhow::Error>(())
}.await);
});
}
}
else => {
break
}
}
}

tracing::info!("Finished handling session");

Ok(())
}
9 changes: 8 additions & 1 deletion h3-webtransport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ futures-util = { version = "0.3", default-features = false }
http = "1"
pin-project-lite = { version = "0.2", default-features = false }
tracing = "0.1.37"
tokio = { version = "1.28", default-features = false }
tokio = { version = "1.28", default-features = false, features = ["sync"] }
h3-datagram = { version = "0.0.2", path = "../h3-datagram" }

[dependencies.h3]
version = "0.0.8"
path = "../h3"
features = ["i-implement-a-third-party-backend-and-opt-into-breaking-changes"]

[dev-dependencies]
h3-quinn = { path = "../h3-quinn", features = ["datagram"] }
quinn = { version = "0.11", default-features = false, features = ["runtime-tokio", "rustls", "ring"] }
rcgen = "0.14"
rustls = { version = "0.23", default-features = false, features = ["ring", "std"] }
tokio = { version = "1", features = ["rt", "macros", "io-util"] }
30 changes: 30 additions & 0 deletions h3-webtransport/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,33 @@ Implementation of Webtransport protocol as extension of the h3 crate

# Status
This crate is still in experimental. The API is subject to change. It may contain bugs and is not yet complete. Use with caution.

## Connection and session ownership

The connection driver and WebTransport sessions have separate lifetimes:

- `WebTransportConnection::new(h3_connection)` retains the HTTP/3 connection.
Keep calling `accept()` to process control traffic and receive both ordinary
HTTP and WebTransport streams. Resolve each bidirectional stream independently
so incomplete HEADERS do not block other requests.
- `accept_session(request, stream)` returns an owned acceptance future. Sending
the CONNECT response does not borrow the driver. An accepted
`WebTransportSession` holds its CONNECT stream and outgoing capabilities;
dropping it does not close the HTTP/3 connection.
- Dispatch incoming WT bidi/uni streams by session ID. Obtain one datagram
reader from the driver and dispatch datagrams by their associated stream ID;
independent per-session readers would compete for the same input.
- `session.open_bi()`, `session.open_uni()` and `session.send_datagram()` are
bound to that session. `driver.shutdown(max_requests)` initiates HTTP/3
GOAWAY; the caller still owns session tasks and their completion.
- `session.closed().await` observes receive-side CONNECT FIN, reset or connection
loss independently of the driver and outgoing operations. It is cancel-safe;
concurrent/repeated waiters receive the retained result. It discards CONNECT
body data, without interpreting capsules or their close codes. GOAWAY alone
is not closure. The signal does not terminate admitted application streams:
their owner stops admission and retains its own graceful drain policy.

The driver does not spawn tasks or buffer streams for applications. The caller
owns concurrent resolvers, session admission, routing (including streams that
arrive before acceptance), session closure, and drain policy. Keep the driver
alive and polled while session work is active.
Loading
Loading