1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! WebTransport session wrapper that maps Quinn connections to WebTransport semantics.
use std::{
fmt,
future::{Future, poll_fn},
io::Cursor,
ops::Deref,
pin::Pin,
sync::{Arc, Mutex},
task::{Context, Poll, ready},
};
use bytes::{Bytes, BytesMut};
use futures::stream::{FuturesUnordered, Stream, StreamExt};
use url::Url;
use crate::{
ClientError, Connect, RecvStream, SendStream, SessionError, Settings, WebTransportError,
};
use webtrans_proto::{Frame, UniStream, VarInt};
/// An established WebTransport session, acting like a full QUIC connection. See [`quinn::Connection`].
///
/// Remember that WebTransport is layered on top of QUIC:
/// 1. Each stream begins with bytes identifying the stream type and session ID.
/// 2. Error codes are encoded with the session ID, so they are not full QUIC error codes.
/// 3. Stream IDs may have gaps introduced by HTTP/3, transparent to the application.
///
/// Deref is used to expose non-overloaded methods on [`quinn::Connection`].
/// These should be safe with WebTransport; please file an issue if you find otherwise.
#[derive(Clone)]
pub struct Session {
conn: quinn::Connection,
// The session ID derived from the CONNECT request stream ID.
session_id: Option<VarInt>,
// The accept logic is stateful, so share it with Arc<Mutex>.
accept: Option<Arc<Mutex<SessionAccept>>>,
// Cached headers that prefix each stream we open.
header_uni: Vec<u8>,
header_bi: Vec<u8>,
header_datagram: Vec<u8>,
// Keep references to settings and connect streams so they remain open until drop.
#[allow(dead_code)]
settings: Option<Arc<Settings>>,
// The URL used to create the session.
url: Url,
}
impl Session {
pub(crate) fn new(conn: quinn::Connection, settings: Settings, connect: Connect) -> Self {
// The session ID is the stream ID of the CONNECT request.
let session_id = connect.session_id();
// Cache the small header that prefixes each stream we open.
let mut header_uni = Vec::new();
UniStream::WEBTRANSPORT.encode(&mut header_uni);
session_id.encode(&mut header_uni);
let mut header_bi = Vec::new();
Frame::WEBTRANSPORT.encode(&mut header_bi);
session_id.encode(&mut header_bi);
let mut header_datagram = Vec::new();
session_id.encode(&mut header_datagram);
// Accept logic is stateful, so use an Arc<Mutex> to share it.
let accept = SessionAccept::new(conn.clone(), session_id);
let this = Self {
conn,
accept: Some(Arc::new(Mutex::new(accept))),
session_id: Some(session_id),
header_uni,
header_bi,
header_datagram,
url: connect.url().clone(),
settings: Some(Arc::new(settings)),
};
// Run a background task to detect CONNECT stream closure.
let mut this2 = this.clone();
tokio::spawn(async move {
let (code, reason) = this2.run_closed(connect).await;
tracing::debug!(
"closing QUIC connection after WebTransport close: code={code} reason={reason}"
);
if this2.conn.close_reason().is_none() {
this2.conn.close(0u32.into(), reason.as_bytes());
}
});
this
}
// Keep reading from the control stream until it closes.
async fn run_closed(&mut self, connect: Connect) -> (u32, String) {
let (_send, mut recv) = connect.into_inner();
loop {
match webtrans_proto::Capsule::read(&mut recv).await {
Ok(webtrans_proto::Capsule::CloseWebTransportSession { code, reason }) => {
return (code, reason);
}
Ok(webtrans_proto::Capsule::Unknown { typ, payload }) => {
tracing::warn!("unknown capsule: type={typ} size={}", payload.len());
}
Err(_) => {
return (1, "capsule error".to_string());
}
}
}
}
/// Connect using an established QUIC connection when creating the connection manually.
/// This only works with a fresh QUIC connection negotiated with the HTTP/3 ALPN.
pub async fn connect(conn: quinn::Connection, url: Url) -> Result<Session, ClientError> {
// Perform the HTTP/3 handshake by sending/receiving SETTINGS frames.
let settings = Settings::connect(&conn).await?;
// Send the HTTP/3 CONNECT request.
let connect = Connect::open(&conn, url).await?;
// Return the session while retaining control/connect streams.
// If either stream closes, the session ends, so keep references alive.
let session = Session::new(conn, settings, connect);
Ok(session)
}
/// Accept a new unidirectional stream. See [`quinn::Connection::accept_uni`].
pub async fn accept_uni(&self) -> Result<RecvStream, SessionError> {
if let Some(accept) = &self.accept {
poll_fn(|cx| accept.lock().unwrap().poll_accept_uni(cx)).await
} else {
self.conn
.accept_uni()
.await
.map(RecvStream::new)
.map_err(Into::into)
}
}
/// Accept a new bidirectional stream. See [`quinn::Connection::accept_bi`].
pub async fn accept_bi(&self) -> Result<(SendStream, RecvStream), SessionError> {
if let Some(accept) = &self.accept {
poll_fn(|cx| accept.lock().unwrap().poll_accept_bi(cx)).await
} else {
self.conn
.accept_bi()
.await
.map(|(send, recv)| (SendStream::new(send), RecvStream::new(recv)))
.map_err(Into::into)
}
}
/// Open a new unidirectional stream. See [`quinn::Connection::open_uni`].
pub async fn open_uni(&self) -> Result<SendStream, SessionError> {
let mut send = self.conn.open_uni().await?;
// Set max priority, then write the stream header.
// Otherwise application data could be queued ahead of the header.
// The header is required to determine the session ID without reliable reset.
send.set_priority(i32::MAX).ok();
Self::write_full(&mut send, &self.header_uni).await?;
// Reset stream priority to the default of 0.
send.set_priority(0).ok();
Ok(SendStream::new(send))
}
/// Open a new bidirectional stream. See [`quinn::Connection::open_bi`].
pub async fn open_bi(&self) -> Result<(SendStream, RecvStream), SessionError> {
let (mut send, recv) = self.conn.open_bi().await?;
// Set max priority, then write the stream header.
// Otherwise application data could be queued ahead of the header.
// The header is required to determine the session ID without reliable reset.
send.set_priority(i32::MAX).ok();
Self::write_full(&mut send, &self.header_bi).await?;
// Reset stream priority to the default of 0.
send.set_priority(0).ok();
Ok((SendStream::new(send), RecvStream::new(recv)))
}
/// Asynchronously receive an application datagram from the remote peer.
///
/// Waits for a datagram to become available and returns the received bytes.
pub async fn read_datagram(&self) -> Result<Bytes, SessionError> {
let mut datagram = self
.conn
.read_datagram()
.await
.map_err(SessionError::from)?;
let mut cursor = Cursor::new(&datagram);
if let Some(session_id) = self.session_id {
// Validate and strip the session ID from the datagram.
let actual_id =
VarInt::decode(&mut cursor).map_err(|_| WebTransportError::UnknownSession)?;
if actual_id != session_id {
return Err(WebTransportError::UnknownSession.into());
}
}
// Return the datagram without the session ID.
let datagram = datagram.split_off(cursor.position() as usize);
Ok(datagram)
}
/// Send an application datagram to the remote peer.
///
/// Datagrams are unreliable and may be dropped or delivered out of order.
/// The data must be smaller than [`max_datagram_size`](Self::max_datagram_size).
pub fn send_datagram(&self, data: Bytes) -> Result<(), SessionError> {
if !self.header_datagram.is_empty() {
// Quinn requires allocation to prepend the session header.
// Tracking issue: https://github.com/quinn-rs/quinn/issues/1724
let mut buf = BytesMut::with_capacity(self.header_datagram.len() + data.len());
// Prepend the session ID header to the datagram payload.
buf.extend_from_slice(&self.header_datagram);
buf.extend_from_slice(&data);
self.conn.send_datagram(buf.into())?;
} else {
self.conn.send_datagram(data)?;
}
Ok(())
}
/// Compute the maximum size of datagrams that may be passed to
/// [`send_datagram`](Self::send_datagram).
pub fn max_datagram_size(&self) -> usize {
let mtu = self
.conn
.max_datagram_size()
.expect("datagram support is required");
mtu.saturating_sub(self.header_datagram.len())
}
/// Immediately close the connection with an error code and reason. See [`quinn::Connection::close`].
pub fn close(&self, code: u32, reason: &[u8]) {
let code = if self.session_id.is_some() {
webtrans_proto::error_to_http3(code).try_into().unwrap()
} else {
code.into()
};
self.conn.close(code, reason)
}
/// Wait until the session is closed and return the error. See [`quinn::Connection::closed`].
pub async fn closed(&self) -> SessionError {
self.conn.closed().await.into()
}
/// Return the close reason, or `None` if the session is still open. See [`quinn::Connection::close_reason`].
pub fn close_reason(&self) -> Option<SessionError> {
self.conn.close_reason().map(Into::into)
}
async fn write_full(send: &mut quinn::SendStream, buf: &[u8]) -> Result<(), SessionError> {
match send.write_all(buf).await {
Ok(_) => Ok(()),
Err(quinn::WriteError::ConnectionLost(err)) => Err(err.into()),
Err(err) => Err(WebTransportError::WriteError(err).into()),
}
}
/// Create a new session from a raw QUIC connection and a URL.
///
/// This adapts a QUIC connection to a WebTransport session, which simplifies
/// supporting WebTransport and raw QUIC side by side.
pub fn raw(conn: quinn::Connection, url: Url) -> Self {
Self {
conn,
session_id: None,
header_uni: Default::default(),
header_bi: Default::default(),
header_datagram: Default::default(),
accept: None,
settings: None,
url,
}
}
pub fn url(&self) -> &Url {
&self.url
}
}
impl Deref for Session {
type Target = quinn::Connection;
fn deref(&self) -> &Self::Target {
&self.conn
}
}
impl fmt::Debug for Session {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.conn.fmt(f)
}
}
impl PartialEq for Session {
fn eq(&self, other: &Self) -> bool {
self.conn.stable_id() == other.conn.stable_id()
}
}
impl Eq for Session {}
// Type aliases to keep clippy from flagging overly complex types.
type AcceptUni = dyn Stream<Item = Result<quinn::RecvStream, quinn::ConnectionError>> + Send;
type AcceptBi = dyn Stream<Item = Result<(quinn::SendStream, quinn::RecvStream), quinn::ConnectionError>>
+ Send;
type PendingUni = dyn Future<Output = Result<(UniStream, quinn::RecvStream), SessionError>> + Send;
type PendingBi = dyn Future<Output = Result<Option<(quinn::SendStream, quinn::RecvStream)>, SessionError>>
+ Send;
// Stream-accept logic, needed because streams include a WebTransport header.
pub struct SessionAccept {
session_id: VarInt,
// Keep QPACK streams alive if the peer creates them, to prevent premature closure.
qpack_encoder: Option<quinn::RecvStream>,
qpack_decoder: Option<quinn::RecvStream>,
accept_uni: Pin<Box<AcceptUni>>,
accept_bi: Pin<Box<AcceptBi>>,
// Track in-flight work to read/write WebTransport stream headers.
pending_uni: FuturesUnordered<Pin<Box<PendingUni>>>,
pending_bi: FuturesUnordered<Pin<Box<PendingBi>>>,
}
impl SessionAccept {
pub(crate) fn new(conn: quinn::Connection, session_id: VarInt) -> Self {
// Create a stream that yields new incoming streams for polling.
let accept_uni = Box::pin(futures::stream::unfold(conn.clone(), |conn| async {
Some((conn.accept_uni().await, conn))
}));
let accept_bi = Box::pin(futures::stream::unfold(conn, |conn| async {
Some((conn.accept_bi().await, conn))
}));
Self {
session_id,
qpack_decoder: None,
qpack_encoder: None,
accept_uni,
accept_bi,
pending_uni: FuturesUnordered::new(),
pending_bi: FuturesUnordered::new(),
}
}
// Poll-based so we can accept and decode streams in parallel.
// FuturesUnordered keeps the implementation runtime-agnostic.
pub fn poll_accept_uni(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<RecvStream, SessionError>> {
loop {
// Accept any new streams.
if let Poll::Ready(Some(res)) = self.accept_uni.poll_next_unpin(cx) {
// Start decoding the header and track the pending future.
let recv = res?;
let pending = Self::decode_uni(recv, self.session_id);
self.pending_uni.push(Box::pin(pending));
continue;
}
// Poll pending stream decodes.
let (typ, recv) = match ready!(self.pending_uni.poll_next_unpin(cx)) {
Some(Ok(res)) => res,
Some(Err(err)) => {
// Ignore errors; the stream may have been reset early.
tracing::warn!("failed to decode unidirectional stream: {err:?}");
continue;
}
None => return Poll::Pending,
};
// Decide whether to continue based on the stream type.
match typ {
UniStream::WEBTRANSPORT => {
let recv = RecvStream::new(recv);
return Poll::Ready(Ok(recv));
}
UniStream::QPACK_DECODER => {
self.qpack_decoder = Some(recv);
}
UniStream::QPACK_ENCODER => {
self.qpack_encoder = Some(recv);
}
_ => {
// Ignore unknown streams.
tracing::debug!("ignoring unknown unidirectional stream: {typ:?}");
}
}
}
}
// Read the stream header and return the stream type.
async fn decode_uni(
mut recv: quinn::RecvStream,
expected_session: VarInt,
) -> Result<(UniStream, quinn::RecvStream), SessionError> {
// Read the VarInt at the start of the stream.
let typ = VarInt::read(&mut recv)
.await
.map_err(|_| WebTransportError::UnknownSession)?;
let typ = UniStream(typ);
if typ == UniStream::WEBTRANSPORT {
// Read and validate the session ID.
let session_id = VarInt::read(&mut recv)
.await
.map_err(|_| WebTransportError::UnknownSession)?;
if session_id != expected_session {
return Err(WebTransportError::UnknownSession.into());
}
}
// Return everything so QPACK streams can be retained if the peer created them.
Ok((typ, recv))
}
pub fn poll_accept_bi(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(SendStream, RecvStream), SessionError>> {
loop {
// Accept any new streams.
if let Poll::Ready(Some(res)) = self.accept_bi.poll_next_unpin(cx) {
// Start decoding the header and track the pending future.
let (send, recv) = res?;
let pending = Self::decode_bi(send, recv, self.session_id);
self.pending_bi.push(Box::pin(pending));
continue;
}
// Poll pending stream decodes.
let res = match ready!(self.pending_bi.poll_next_unpin(cx)) {
Some(Ok(res)) => res,
Some(Err(err)) => {
// Ignore errors; the stream may have been reset early.
tracing::warn!("failed to decode bidirectional stream: {err:?}");
continue;
}
None => return Poll::Pending,
};
if let Some((send, recv)) = res {
// Wrap streams in WebTransport types for correct error handling.
let send = SendStream::new(send);
let recv = RecvStream::new(recv);
return Poll::Ready(Ok((send, recv)));
}
// Continue looping when the stream should be ignored.
}
}
// Read the stream header and return `Some` if it is a WebTransport stream.
async fn decode_bi(
send: quinn::SendStream,
mut recv: quinn::RecvStream,
expected_session: VarInt,
) -> Result<Option<(quinn::SendStream, quinn::RecvStream)>, SessionError> {
let typ = VarInt::read(&mut recv)
.await
.map_err(|_| WebTransportError::UnknownSession)?;
if Frame(typ) != Frame::WEBTRANSPORT {
tracing::debug!("ignoring unknown bidirectional stream: {typ:?}");
return Ok(None);
}
// Read and validate the session ID.
let session_id = VarInt::read(&mut recv)
.await
.map_err(|_| WebTransportError::UnknownSession)?;
if session_id != expected_session {
return Err(WebTransportError::UnknownSession.into());
}
Ok(Some((send, recv)))
}
}
impl webtrans_trait::Session for Session {
type SendStream = SendStream;
type RecvStream = RecvStream;
type Error = SessionError;
async fn accept_uni(&self) -> Result<Self::RecvStream, Self::Error> {
Self::accept_uni(self).await
}
async fn accept_bi(&self) -> Result<(Self::SendStream, Self::RecvStream), Self::Error> {
Self::accept_bi(self).await
}
async fn open_bi(&self) -> Result<(Self::SendStream, Self::RecvStream), Self::Error> {
Self::open_bi(self).await
}
async fn open_uni(&self) -> Result<Self::SendStream, Self::Error> {
Self::open_uni(self).await
}
fn close(&self, code: u32, reason: &str) {
Self::close(self, code, reason.as_bytes());
}
async fn closed(&self) -> Self::Error {
Self::closed(self).await
}
fn send_datagram(&self, data: Bytes) -> Result<(), Self::Error> {
Self::send_datagram(self, data)
}
async fn recv_datagram(&self) -> Result<Bytes, Self::Error> {
Self::read_datagram(self).await
}
fn max_datagram_size(&self) -> usize {
Self::max_datagram_size(self)
}
}