rwebtransport 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +666 -0
- package/Cargo.toml +31 -0
- package/LICENSE +202 -0
- package/NOTICE +41 -0
- package/README.md +550 -0
- package/crates/echo-server/Cargo.toml +20 -0
- package/crates/echo-server/src/main.rs +666 -0
- package/crates/native/Cargo.toml +23 -0
- package/crates/native/src/config.rs +129 -0
- package/crates/native/src/driver.rs +463 -0
- package/crates/native/src/lib.rs +821 -0
- package/crates/native/src/server.rs +718 -0
- package/crates/native/src/session.rs +1191 -0
- package/crates/native/src/tls.rs +96 -0
- package/crates/wtcore/Cargo.toml +15 -0
- package/crates/wtcore/src/h3.rs +226 -0
- package/crates/wtcore/src/lib.rs +9 -0
- package/dist/datagrams.d.ts +16 -0
- package/dist/errors.d.ts +10 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +1512 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1473 -0
- package/dist/index.mjs.map +1 -0
- package/dist/loader.d.ts +2 -0
- package/dist/native.d.ts +226 -0
- package/dist/server.d.ts +44 -0
- package/dist/streams.d.ts +14 -0
- package/dist/types.d.ts +24 -0
- package/dist/webtransport.d.ts +20 -0
- package/package.json +79 -0
- package/prebuilds/win32-x64/rwebtransport.node +0 -0
- package/scripts/bench.mjs +180 -0
- package/scripts/build.js +67 -0
- package/scripts/postinstall.js +67 -0
- package/scripts/setup-unix.sh +129 -0
- package/scripts/setup-windows.ps1 +229 -0
- package/vendor/quiche/AGENTS.md +75 -0
- package/vendor/quiche/COPYING +23 -0
- package/vendor/quiche/Cargo.lock +1212 -0
- package/vendor/quiche/Cargo.toml +215 -0
- package/vendor/quiche/README.md +466 -0
- package/vendor/quiche/examples/Makefile +46 -0
- package/vendor/quiche/examples/README.md +20 -0
- package/vendor/quiche/examples/cert-big.crt +90 -0
- package/vendor/quiche/examples/cert.crt +18 -0
- package/vendor/quiche/examples/cert.key +28 -0
- package/vendor/quiche/examples/client.c +343 -0
- package/vendor/quiche/examples/client.rs +281 -0
- package/vendor/quiche/examples/gen-certs.sh +15 -0
- package/vendor/quiche/examples/http3-client.c +473 -0
- package/vendor/quiche/examples/http3-client.rs +349 -0
- package/vendor/quiche/examples/http3-server.c +614 -0
- package/vendor/quiche/examples/http3-server.rs +680 -0
- package/vendor/quiche/examples/qpack-decode.rs +87 -0
- package/vendor/quiche/examples/qpack-encode.rs +88 -0
- package/vendor/quiche/examples/rootca.crt +21 -0
- package/vendor/quiche/examples/server.c +529 -0
- package/vendor/quiche/examples/server.rs +529 -0
- package/vendor/quiche/include/quiche.h +1282 -0
- package/vendor/quiche/quiche.svg +1 -0
- package/vendor/quiche/src/buffers.rs +105 -0
- package/vendor/quiche/src/build.rs +66 -0
- package/vendor/quiche/src/cid.rs +1079 -0
- package/vendor/quiche/src/crypto/boringssl.rs +373 -0
- package/vendor/quiche/src/crypto/mod.rs +671 -0
- package/vendor/quiche/src/dgram.rs +118 -0
- package/vendor/quiche/src/error.rs +263 -0
- package/vendor/quiche/src/ffi.rs +2290 -0
- package/vendor/quiche/src/flowcontrol.rs +246 -0
- package/vendor/quiche/src/frame.rs +2172 -0
- package/vendor/quiche/src/h3/AGENTS.md +54 -0
- package/vendor/quiche/src/h3/ffi.rs +442 -0
- package/vendor/quiche/src/h3/frame.rs +1404 -0
- package/vendor/quiche/src/h3/mod.rs +8223 -0
- package/vendor/quiche/src/h3/qpack/decoder.rs +288 -0
- package/vendor/quiche/src/h3/qpack/encoder.rs +235 -0
- package/vendor/quiche/src/h3/qpack/mod.rs +202 -0
- package/vendor/quiche/src/h3/qpack/static_table.rs +323 -0
- package/vendor/quiche/src/h3/stream.rs +1595 -0
- package/vendor/quiche/src/lib.rs +9556 -0
- package/vendor/quiche/src/minmax.rs +446 -0
- package/vendor/quiche/src/packet.rs +2334 -0
- package/vendor/quiche/src/path.rs +1374 -0
- package/vendor/quiche/src/pmtud.rs +652 -0
- package/vendor/quiche/src/rand.rs +64 -0
- package/vendor/quiche/src/range_buf.rs +186 -0
- package/vendor/quiche/src/ranges.rs +755 -0
- package/vendor/quiche/src/recovery/AGENTS.md +80 -0
- package/vendor/quiche/src/recovery/bandwidth.rs +565 -0
- package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -0
- package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -0
- package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -0
- package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -0
- package/vendor/quiche/src/recovery/congestion/mod.rs +361 -0
- package/vendor/quiche/src/recovery/congestion/prr.rs +238 -0
- package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -0
- package/vendor/quiche/src/recovery/congestion/reno.rs +291 -0
- package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -0
- package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -0
- package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -0
- package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -0
- package/vendor/quiche/src/recovery/mod.rs +3086 -0
- package/vendor/quiche/src/recovery/rtt.rs +152 -0
- package/vendor/quiche/src/stream/mod.rs +2210 -0
- package/vendor/quiche/src/stream/recv_buf.rs +1236 -0
- package/vendor/quiche/src/stream/send_buf.rs +849 -0
- package/vendor/quiche/src/test_utils.rs +681 -0
- package/vendor/quiche/src/tests.rs +12922 -0
- package/vendor/quiche/src/tls/boringssl.rs +375 -0
- package/vendor/quiche/src/tls/mod.rs +1245 -0
- package/vendor/quiche/src/transport_params.rs +628 -0
|
@@ -0,0 +1,1191 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
//! WebTransport client session state machine over a single quiche connection.
|
|
3
|
+
//!
|
|
4
|
+
//! One `WtSession` drives exactly one WebTransport session (one HTTP/3 Extended
|
|
5
|
+
//! CONNECT) over one QUIC connection. It owns no I/O: the [`crate::driver`]
|
|
6
|
+
//! event loop feeds it readable-stream / datagram / timer notifications and asks
|
|
7
|
+
//! it to flush. Everything the session wants to tell JS is returned as [`Ev`]s.
|
|
8
|
+
//!
|
|
9
|
+
//! Stream-id layout (client-initiated):
|
|
10
|
+
//! * bidi `0` is the CONNECT stream (this is also the WebTransport session id).
|
|
11
|
+
//! * uni `2`, `6`, `10` are HTTP/3 control, QPACK encoder, QPACK decoder.
|
|
12
|
+
//! * bidi `4, 8, ...` are WebTransport bidirectional streams.
|
|
13
|
+
//! * uni `14, 18, ...` are WebTransport unidirectional streams.
|
|
14
|
+
|
|
15
|
+
use std::collections::{HashMap, VecDeque};
|
|
16
|
+
|
|
17
|
+
use wtcore::h3;
|
|
18
|
+
|
|
19
|
+
fn dbg_on() -> bool {
|
|
20
|
+
use std::sync::OnceLock;
|
|
21
|
+
static ENABLED: OnceLock<bool> = OnceLock::new();
|
|
22
|
+
*ENABLED.get_or_init(|| std::env::var("RWT_DEBUG").is_ok())
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/// The session id / CONNECT stream id (always the client-initiated bidi 0).
|
|
26
|
+
const CONNECT_ID: u64 = 0;
|
|
27
|
+
|
|
28
|
+
// Client-role local stream ids (client-initiated).
|
|
29
|
+
const CLIENT_CONTROL_ID: u64 = 2;
|
|
30
|
+
const CLIENT_QPACK_ENC_ID: u64 = 6;
|
|
31
|
+
const CLIENT_QPACK_DEC_ID: u64 = 10;
|
|
32
|
+
const CLIENT_FIRST_WT_BIDI_ID: u64 = 4;
|
|
33
|
+
const CLIENT_FIRST_WT_UNI_ID: u64 = 14;
|
|
34
|
+
|
|
35
|
+
// Server-role local stream ids (server-initiated).
|
|
36
|
+
const SERVER_CONTROL_ID: u64 = 3;
|
|
37
|
+
const SERVER_QPACK_ENC_ID: u64 = 7;
|
|
38
|
+
const SERVER_QPACK_DEC_ID: u64 = 11;
|
|
39
|
+
const SERVER_FIRST_WT_BIDI_ID: u64 = 1;
|
|
40
|
+
const SERVER_FIRST_WT_UNI_ID: u64 = 15;
|
|
41
|
+
|
|
42
|
+
/// Cap on a single buffered HTTP/3 control/CONNECT frame (protects against a
|
|
43
|
+
/// hostile peer advertising a huge length).
|
|
44
|
+
const MAX_CONTROL_FRAME: usize = 1 << 20;
|
|
45
|
+
|
|
46
|
+
/// Events the session emits for the driver to forward to JS.
|
|
47
|
+
#[derive(Debug)]
|
|
48
|
+
pub enum Ev {
|
|
49
|
+
/// The WebTransport session is established (client role: CONNECT got a 2xx).
|
|
50
|
+
Ready,
|
|
51
|
+
/// A server-role session was established: a valid Extended CONNECT arrived
|
|
52
|
+
/// and we answered 200. Carries the request details for the application.
|
|
53
|
+
ServerReady {
|
|
54
|
+
authority: String,
|
|
55
|
+
path: String,
|
|
56
|
+
origin: Option<String>,
|
|
57
|
+
headers: Vec<(String, String)>,
|
|
58
|
+
/// The client's remote IP (as a string) at session establishment.
|
|
59
|
+
remote_addr: String,
|
|
60
|
+
/// The client's remote UDP port at session establishment.
|
|
61
|
+
remote_port: u16,
|
|
62
|
+
},
|
|
63
|
+
/// The session ended. `remote` is true if the peer initiated it.
|
|
64
|
+
Closed {
|
|
65
|
+
code: u32,
|
|
66
|
+
reason: Vec<u8>,
|
|
67
|
+
remote: bool,
|
|
68
|
+
},
|
|
69
|
+
/// A fatal error before or during the session.
|
|
70
|
+
Error(String),
|
|
71
|
+
/// A WebTransport datagram was received.
|
|
72
|
+
Datagram(Vec<u8>),
|
|
73
|
+
/// The peer opened a WebTransport stream.
|
|
74
|
+
IncomingStream { id: u64, bidi: bool },
|
|
75
|
+
/// Application data arrived on a WebTransport stream.
|
|
76
|
+
StreamData { id: u64, data: Vec<u8> },
|
|
77
|
+
/// The peer finished (FIN) its side of a stream.
|
|
78
|
+
StreamFinished { id: u64 },
|
|
79
|
+
/// The peer reset a stream.
|
|
80
|
+
StreamReset { id: u64, code: u64 },
|
|
81
|
+
/// The peer sent STOP_SENDING for a stream we write to.
|
|
82
|
+
StreamStopSending { id: u64, code: u64 },
|
|
83
|
+
/// A locally requested stream open completed; `id` is the QUIC stream id.
|
|
84
|
+
StreamOpened { request_id: u64, id: u64 },
|
|
85
|
+
/// A write's bytes were fully handed to quiche (backpressure signal).
|
|
86
|
+
WriteAck { request_id: u64 },
|
|
87
|
+
/// A datagram send request was processed (sent or intentionally dropped).
|
|
88
|
+
DatagramAck { request_id: u64, sent: bool },
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// One queued outbound chunk on a stream.
|
|
92
|
+
struct OutChunk {
|
|
93
|
+
data: Vec<u8>,
|
|
94
|
+
off: usize,
|
|
95
|
+
request_id: Option<u64>,
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Per-stream classification / role.
|
|
99
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
100
|
+
enum Role {
|
|
101
|
+
/// Our send-only HTTP/3 control / QPACK uni streams. Never read.
|
|
102
|
+
LocalControlPlane,
|
|
103
|
+
/// The CONNECT (session) bidi stream.
|
|
104
|
+
Connect,
|
|
105
|
+
/// The peer's HTTP/3 control stream: parse SETTINGS/frames.
|
|
106
|
+
PeerControl,
|
|
107
|
+
/// A peer uni stream we drain and discard (QPACK/push/unknown).
|
|
108
|
+
Ignored,
|
|
109
|
+
/// A resolved WebTransport data stream.
|
|
110
|
+
WtData { bidi: bool },
|
|
111
|
+
/// A peer-initiated uni stream whose type varint isn't fully read yet.
|
|
112
|
+
PendingUni,
|
|
113
|
+
/// A peer-initiated bidi stream whose WT signal isn't fully read yet.
|
|
114
|
+
PendingBidi,
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
struct Stream {
|
|
118
|
+
role: Role,
|
|
119
|
+
/// Classification accumulator (pending server streams) / control byte carry.
|
|
120
|
+
class_buf: Vec<u8>,
|
|
121
|
+
/// HTTP/3 frame parser (Connect + PeerControl).
|
|
122
|
+
frames: FrameParser,
|
|
123
|
+
/// CONNECT stream: response HEADERS already parsed.
|
|
124
|
+
headers_done: bool,
|
|
125
|
+
/// Read backpressure (WtData only).
|
|
126
|
+
paused: bool,
|
|
127
|
+
/// Outbound queue.
|
|
128
|
+
out: VecDeque<OutChunk>,
|
|
129
|
+
/// Application asked to FIN once `out` drains.
|
|
130
|
+
fin_queued: bool,
|
|
131
|
+
fin_sent: bool,
|
|
132
|
+
/// Peer already reset/finished, stop touching recv.
|
|
133
|
+
recv_dead: bool,
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
impl Stream {
|
|
137
|
+
fn new(role: Role) -> Self {
|
|
138
|
+
Self {
|
|
139
|
+
role,
|
|
140
|
+
class_buf: Vec::new(),
|
|
141
|
+
frames: FrameParser::default(),
|
|
142
|
+
headers_done: false,
|
|
143
|
+
paused: false,
|
|
144
|
+
out: VecDeque::new(),
|
|
145
|
+
fin_queued: false,
|
|
146
|
+
fin_sent: false,
|
|
147
|
+
recv_dead: false,
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
fn with_prefix(role: Role, prefix: Vec<u8>) -> Self {
|
|
152
|
+
let mut s = Self::new(role);
|
|
153
|
+
if !prefix.is_empty() {
|
|
154
|
+
s.out.push_back(OutChunk {
|
|
155
|
+
data: prefix,
|
|
156
|
+
off: 0,
|
|
157
|
+
request_id: None,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
s
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/// Incremental HTTP/3 frame reassembler.
|
|
165
|
+
#[derive(Default)]
|
|
166
|
+
struct FrameParser {
|
|
167
|
+
buf: Vec<u8>,
|
|
168
|
+
error: bool,
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
impl FrameParser {
|
|
172
|
+
fn push(&mut self, data: &[u8]) {
|
|
173
|
+
self.buf.extend_from_slice(data);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/// Pop the next complete `(type, payload)` frame, or `None` if incomplete.
|
|
177
|
+
fn next_frame(&mut self) -> Option<(u64, Vec<u8>)> {
|
|
178
|
+
if self.error {
|
|
179
|
+
return None;
|
|
180
|
+
}
|
|
181
|
+
let (ty, n1) = h3::read_varint(&self.buf)?;
|
|
182
|
+
let (len, n2) = h3::read_varint(&self.buf[n1..])?;
|
|
183
|
+
let len = len as usize;
|
|
184
|
+
if len > MAX_CONTROL_FRAME {
|
|
185
|
+
self.error = true;
|
|
186
|
+
return None;
|
|
187
|
+
}
|
|
188
|
+
let header = n1 + n2;
|
|
189
|
+
let total = header + len;
|
|
190
|
+
if self.buf.len() < total {
|
|
191
|
+
return None;
|
|
192
|
+
}
|
|
193
|
+
let payload = self.buf[header..total].to_vec();
|
|
194
|
+
self.buf.drain(..total);
|
|
195
|
+
Some((ty, payload))
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/// Deferred graceful close.
|
|
200
|
+
struct CloseReq {
|
|
201
|
+
code: u32,
|
|
202
|
+
reason: Vec<u8>,
|
|
203
|
+
capsule_queued: bool,
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
pub struct WtSession {
|
|
207
|
+
is_server: bool,
|
|
208
|
+
|
|
209
|
+
// Client role only: the Extended CONNECT request we send.
|
|
210
|
+
authority: String,
|
|
211
|
+
path: String,
|
|
212
|
+
origin: Option<String>,
|
|
213
|
+
extra_headers: Vec<(String, String)>,
|
|
214
|
+
|
|
215
|
+
next_bidi: u64,
|
|
216
|
+
next_uni: u64,
|
|
217
|
+
|
|
218
|
+
setup_sent: bool,
|
|
219
|
+
ready: bool,
|
|
220
|
+
closed: bool,
|
|
221
|
+
|
|
222
|
+
peer_settings: Option<h3::PeerSettings>,
|
|
223
|
+
|
|
224
|
+
streams: HashMap<u64, Stream>,
|
|
225
|
+
|
|
226
|
+
close_req: Option<CloseReq>,
|
|
227
|
+
/// Set once the QUIC connection should be closed (after connect FIN flush).
|
|
228
|
+
want_quic_close: Option<(u32, Vec<u8>)>,
|
|
229
|
+
/// Server role only: the client's remote address, captured when the
|
|
230
|
+
/// connection was accepted and surfaced on the `serverReady` event.
|
|
231
|
+
remote_addr: Option<std::net::SocketAddr>,
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
impl WtSession {
|
|
235
|
+
/// A client-role session that will send an Extended CONNECT.
|
|
236
|
+
pub fn new_client(
|
|
237
|
+
authority: String,
|
|
238
|
+
path: String,
|
|
239
|
+
origin: Option<String>,
|
|
240
|
+
extra_headers: Vec<(String, String)>,
|
|
241
|
+
) -> Self {
|
|
242
|
+
Self {
|
|
243
|
+
is_server: false,
|
|
244
|
+
authority,
|
|
245
|
+
path,
|
|
246
|
+
origin,
|
|
247
|
+
extra_headers,
|
|
248
|
+
next_bidi: CLIENT_FIRST_WT_BIDI_ID,
|
|
249
|
+
next_uni: CLIENT_FIRST_WT_UNI_ID,
|
|
250
|
+
setup_sent: false,
|
|
251
|
+
ready: false,
|
|
252
|
+
closed: false,
|
|
253
|
+
peer_settings: None,
|
|
254
|
+
streams: HashMap::new(),
|
|
255
|
+
close_req: None,
|
|
256
|
+
want_quic_close: None,
|
|
257
|
+
remote_addr: None,
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/// A server-role session that will accept an incoming Extended CONNECT.
|
|
262
|
+
/// `remote` is the client's address, surfaced to the application via the
|
|
263
|
+
/// `serverReady` event.
|
|
264
|
+
pub fn new_server(remote: std::net::SocketAddr) -> Self {
|
|
265
|
+
Self {
|
|
266
|
+
is_server: true,
|
|
267
|
+
authority: String::new(),
|
|
268
|
+
path: String::new(),
|
|
269
|
+
origin: None,
|
|
270
|
+
extra_headers: Vec::new(),
|
|
271
|
+
next_bidi: SERVER_FIRST_WT_BIDI_ID,
|
|
272
|
+
next_uni: SERVER_FIRST_WT_UNI_ID,
|
|
273
|
+
setup_sent: false,
|
|
274
|
+
ready: false,
|
|
275
|
+
closed: false,
|
|
276
|
+
peer_settings: None,
|
|
277
|
+
streams: HashMap::new(),
|
|
278
|
+
close_req: None,
|
|
279
|
+
want_quic_close: None,
|
|
280
|
+
remote_addr: Some(remote),
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
pub fn is_ready(&self) -> bool {
|
|
285
|
+
self.ready
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/// Called once the QUIC connection is established: open the HTTP/3
|
|
289
|
+
/// control-plane streams and advertise SETTINGS. A client also sends the
|
|
290
|
+
/// Extended CONNECT immediately; a server waits for the client's CONNECT on
|
|
291
|
+
/// bidi stream 0.
|
|
292
|
+
pub fn on_established(&mut self, ev: &mut Vec<Ev>) {
|
|
293
|
+
if self.setup_sent {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
self.setup_sent = true;
|
|
297
|
+
|
|
298
|
+
let (control_id, qpack_enc_id, qpack_dec_id) = if self.is_server {
|
|
299
|
+
(SERVER_CONTROL_ID, SERVER_QPACK_ENC_ID, SERVER_QPACK_DEC_ID)
|
|
300
|
+
} else {
|
|
301
|
+
(CLIENT_CONTROL_ID, CLIENT_QPACK_ENC_ID, CLIENT_QPACK_DEC_ID)
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// HTTP/3 control stream: type prefix + SETTINGS.
|
|
305
|
+
self.streams.insert(
|
|
306
|
+
control_id,
|
|
307
|
+
Stream::with_prefix(Role::LocalControlPlane, h3::control_stream_prefix()),
|
|
308
|
+
);
|
|
309
|
+
// QPACK encoder / decoder streams (dynamic table disabled → type only).
|
|
310
|
+
let mut enc = Vec::new();
|
|
311
|
+
h3::put_varint(h3::QPACK_ENCODER_STREAM_TYPE, &mut enc);
|
|
312
|
+
self.streams.insert(
|
|
313
|
+
qpack_enc_id,
|
|
314
|
+
Stream::with_prefix(Role::LocalControlPlane, enc),
|
|
315
|
+
);
|
|
316
|
+
let mut dec = Vec::new();
|
|
317
|
+
h3::put_varint(h3::QPACK_DECODER_STREAM_TYPE, &mut dec);
|
|
318
|
+
self.streams.insert(
|
|
319
|
+
qpack_dec_id,
|
|
320
|
+
Stream::with_prefix(Role::LocalControlPlane, dec),
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
if !self.is_server {
|
|
324
|
+
// CONNECT request on the session (bidi 0) stream.
|
|
325
|
+
let headers = h3::connect_headers(
|
|
326
|
+
&self.authority,
|
|
327
|
+
&self.path,
|
|
328
|
+
self.origin.as_deref(),
|
|
329
|
+
&self.extra_headers,
|
|
330
|
+
);
|
|
331
|
+
match h3::encode_headers_frame(&headers) {
|
|
332
|
+
Ok(frame) => {
|
|
333
|
+
self.streams
|
|
334
|
+
.insert(CONNECT_ID, Stream::with_prefix(Role::Connect, frame));
|
|
335
|
+
}
|
|
336
|
+
Err(e) => ev.push(Ev::Error(format!("failed to encode CONNECT: {e}"))),
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if dbg_on() {
|
|
340
|
+
eprintln!(
|
|
341
|
+
"[rwt-session] on_established (server={}): queued setup, {} streams",
|
|
342
|
+
self.is_server,
|
|
343
|
+
self.streams.len()
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/// Process a readable QUIC stream. `backpressured` is a global signal that
|
|
349
|
+
/// the JS thread is behind; when set, resolved WebTransport data streams are
|
|
350
|
+
/// left unread (flow-controlling the peer) while control-plane streams still
|
|
351
|
+
/// drain.
|
|
352
|
+
pub fn on_readable(
|
|
353
|
+
&mut self,
|
|
354
|
+
conn: &mut quiche::Connection,
|
|
355
|
+
id: u64,
|
|
356
|
+
backpressured: bool,
|
|
357
|
+
ev: &mut Vec<Ev>,
|
|
358
|
+
) {
|
|
359
|
+
let is_server = self.is_server;
|
|
360
|
+
self.streams
|
|
361
|
+
.entry(id)
|
|
362
|
+
.or_insert_with(|| Stream::new(classify_new(id, is_server)));
|
|
363
|
+
|
|
364
|
+
// Respect per-stream and global read backpressure for resolved data
|
|
365
|
+
// streams (leave the bytes in quiche so the peer is flow-controlled).
|
|
366
|
+
let (paused, is_data, recv_dead) = {
|
|
367
|
+
let st = &self.streams[&id];
|
|
368
|
+
(
|
|
369
|
+
st.paused,
|
|
370
|
+
matches!(st.role, Role::WtData { .. }),
|
|
371
|
+
st.recv_dead,
|
|
372
|
+
)
|
|
373
|
+
};
|
|
374
|
+
if recv_dead {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (paused || backpressured) && is_data {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Drain everything quiche currently has for this stream.
|
|
382
|
+
let mut chunk = Vec::new();
|
|
383
|
+
let mut fin = false;
|
|
384
|
+
let mut reset: Option<u64> = None;
|
|
385
|
+
let mut buf = [0u8; 16 * 1024];
|
|
386
|
+
loop {
|
|
387
|
+
match conn.stream_recv(id, &mut buf) {
|
|
388
|
+
Ok((n, f)) => {
|
|
389
|
+
chunk.extend_from_slice(&buf[..n]);
|
|
390
|
+
if f {
|
|
391
|
+
fin = true;
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
if n == 0 {
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
Err(quiche::Error::Done) => break,
|
|
399
|
+
Err(quiche::Error::StreamReset(code)) => {
|
|
400
|
+
reset = Some(code);
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
Err(_) => break,
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if let Some(code) = reset {
|
|
408
|
+
let role = self.streams.get(&id).map(|s| s.role);
|
|
409
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
410
|
+
st.recv_dead = true;
|
|
411
|
+
}
|
|
412
|
+
match role {
|
|
413
|
+
Some(Role::WtData { .. }) => ev.push(Ev::StreamReset { id, code }),
|
|
414
|
+
Some(Role::Connect) if !self.closed => {
|
|
415
|
+
// A reset of the session (CONNECT) stream ends the session;
|
|
416
|
+
// if we swallowed it, ready/closed would hang forever.
|
|
417
|
+
if !self.ready {
|
|
418
|
+
ev.push(Ev::Error(format!(
|
|
419
|
+
"server reset the WebTransport session stream (code {code})"
|
|
420
|
+
)));
|
|
421
|
+
}
|
|
422
|
+
self.mark_closed(ev, code as u32, Vec::new(), true);
|
|
423
|
+
}
|
|
424
|
+
_ => {}
|
|
425
|
+
}
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if dbg_on() {
|
|
430
|
+
let role = self
|
|
431
|
+
.streams
|
|
432
|
+
.get(&id)
|
|
433
|
+
.map(|s| role_name(s.role))
|
|
434
|
+
.unwrap_or("?");
|
|
435
|
+
eprintln!(
|
|
436
|
+
"[rwt-session] readable id={id} role={role} chunk={} fin={fin}",
|
|
437
|
+
chunk.len()
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
self.process_recv(conn, id, chunk, fin, ev);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
fn process_recv(
|
|
444
|
+
&mut self,
|
|
445
|
+
conn: &mut quiche::Connection,
|
|
446
|
+
id: u64,
|
|
447
|
+
chunk: Vec<u8>,
|
|
448
|
+
fin: bool,
|
|
449
|
+
ev: &mut Vec<Ev>,
|
|
450
|
+
) {
|
|
451
|
+
let role = self.streams.get(&id).map(|s| s.role);
|
|
452
|
+
match role {
|
|
453
|
+
Some(Role::LocalControlPlane) | None => {
|
|
454
|
+
// Our send-only control-plane streams / unknown: discard reads.
|
|
455
|
+
}
|
|
456
|
+
Some(Role::Ignored) => {
|
|
457
|
+
// A drained peer stream (QPACK/push/unknown): mark it finished so
|
|
458
|
+
// it can be pruned, otherwise stream churn grows the map.
|
|
459
|
+
if fin {
|
|
460
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
461
|
+
st.recv_dead = true;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
Some(Role::PeerControl) => {
|
|
466
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
467
|
+
st.frames.push(&chunk);
|
|
468
|
+
}
|
|
469
|
+
self.parse_control_frames(id);
|
|
470
|
+
// A malformed control stream is a fatal H3 error; stop reading it
|
|
471
|
+
// so a hostile server cannot keep us draining (and re-crediting)
|
|
472
|
+
// an oversized frame into an unbounded buffer.
|
|
473
|
+
if self.frame_error(id) {
|
|
474
|
+
self.kill_recv(conn, id);
|
|
475
|
+
if !self.closed {
|
|
476
|
+
ev.push(Ev::Error("malformed HTTP/3 control stream".to_string()));
|
|
477
|
+
self.mark_closed(ev, 0, Vec::new(), true);
|
|
478
|
+
let _ = conn.close(true, 0, b"h3 control error");
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
Some(Role::Connect) => {
|
|
483
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
484
|
+
st.frames.push(&chunk);
|
|
485
|
+
}
|
|
486
|
+
if self.is_server {
|
|
487
|
+
self.parse_server_connect_frames(conn, id, ev);
|
|
488
|
+
} else {
|
|
489
|
+
self.parse_connect_frames(conn, id, ev);
|
|
490
|
+
}
|
|
491
|
+
if self.frame_error(id) && !self.closed {
|
|
492
|
+
self.kill_recv(conn, id);
|
|
493
|
+
ev.push(Ev::Error("malformed HTTP/3 CONNECT stream".to_string()));
|
|
494
|
+
self.mark_closed(ev, 0, Vec::new(), true);
|
|
495
|
+
let _ = conn.close(true, 0, b"h3 connect error");
|
|
496
|
+
}
|
|
497
|
+
if fin && !self.closed {
|
|
498
|
+
self.mark_closed(ev, 0, Vec::new(), true);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
Some(Role::WtData { .. }) => {
|
|
502
|
+
if !chunk.is_empty() {
|
|
503
|
+
ev.push(Ev::StreamData { id, data: chunk });
|
|
504
|
+
}
|
|
505
|
+
if fin {
|
|
506
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
507
|
+
st.recv_dead = true;
|
|
508
|
+
}
|
|
509
|
+
ev.push(Ev::StreamFinished { id });
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
Some(Role::PendingUni) => {
|
|
513
|
+
self.resolve_pending_uni(conn, id, chunk, fin, ev);
|
|
514
|
+
}
|
|
515
|
+
Some(Role::PendingBidi) => {
|
|
516
|
+
self.resolve_pending_bidi(conn, id, chunk, fin, ev);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
fn parse_control_frames(&mut self, id: u64) {
|
|
522
|
+
loop {
|
|
523
|
+
let frame = self
|
|
524
|
+
.streams
|
|
525
|
+
.get_mut(&id)
|
|
526
|
+
.and_then(|s| s.frames.next_frame());
|
|
527
|
+
let Some((ty, payload)) = frame else { break };
|
|
528
|
+
if ty == h3::FRAME_SETTINGS {
|
|
529
|
+
self.peer_settings = Some(h3::parse_settings(&payload));
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
fn parse_connect_frames(&mut self, conn: &mut quiche::Connection, id: u64, ev: &mut Vec<Ev>) {
|
|
535
|
+
loop {
|
|
536
|
+
let (frame, headers_done) = {
|
|
537
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
538
|
+
return;
|
|
539
|
+
};
|
|
540
|
+
(st.frames.next_frame(), st.headers_done)
|
|
541
|
+
};
|
|
542
|
+
let Some((ty, payload)) = frame else { break };
|
|
543
|
+
|
|
544
|
+
if dbg_on() {
|
|
545
|
+
eprintln!("[rwt-session] connect frame ty={ty} len={}", payload.len());
|
|
546
|
+
}
|
|
547
|
+
if ty == h3::FRAME_HEADERS && !headers_done {
|
|
548
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
549
|
+
st.headers_done = true;
|
|
550
|
+
}
|
|
551
|
+
match h3::decode_header_block(&payload) {
|
|
552
|
+
Ok(headers) => {
|
|
553
|
+
let status = h3::status_of(&headers).unwrap_or(0);
|
|
554
|
+
if dbg_on() {
|
|
555
|
+
eprintln!("[rwt-session] CONNECT response status={status}");
|
|
556
|
+
}
|
|
557
|
+
if (200..300).contains(&status) {
|
|
558
|
+
if !self.ready {
|
|
559
|
+
self.ready = true;
|
|
560
|
+
ev.push(Ev::Ready);
|
|
561
|
+
}
|
|
562
|
+
} else {
|
|
563
|
+
ev.push(Ev::Error(format!(
|
|
564
|
+
"WebTransport CONNECT rejected with status {status}"
|
|
565
|
+
)));
|
|
566
|
+
self.mark_closed(ev, 0, Vec::new(), true);
|
|
567
|
+
let _ = conn.close(true, 0, b"connect rejected");
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
Err(e) => {
|
|
571
|
+
ev.push(Ev::Error(format!("failed to decode CONNECT response: {e}")));
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
} else if ty == h3::FRAME_DATA {
|
|
575
|
+
// Capsule-protocol content. Best-effort scan for a session close.
|
|
576
|
+
self.scan_capsules(&payload, ev);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
fn scan_capsules(&mut self, mut buf: &[u8], ev: &mut Vec<Ev>) {
|
|
582
|
+
while !buf.is_empty() {
|
|
583
|
+
let Some((ty, n1)) = h3::read_varint(buf) else {
|
|
584
|
+
break;
|
|
585
|
+
};
|
|
586
|
+
let Some((len, n2)) = h3::read_varint(&buf[n1..]) else {
|
|
587
|
+
break;
|
|
588
|
+
};
|
|
589
|
+
let len = len as usize;
|
|
590
|
+
let start = n1 + n2;
|
|
591
|
+
if buf.len() < start + len {
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
let value = &buf[start..start + len];
|
|
595
|
+
if ty == h3::WT_CLOSE_SESSION_CAPSULE && !self.closed {
|
|
596
|
+
let (code, reason) = if value.len() >= 4 {
|
|
597
|
+
(
|
|
598
|
+
u32::from_be_bytes([value[0], value[1], value[2], value[3]]),
|
|
599
|
+
value[4..].to_vec(),
|
|
600
|
+
)
|
|
601
|
+
} else {
|
|
602
|
+
(0, Vec::new())
|
|
603
|
+
};
|
|
604
|
+
self.mark_closed(ev, code, reason, true);
|
|
605
|
+
}
|
|
606
|
+
buf = &buf[start + len..];
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/// Server role: parse the client's Extended CONNECT request on the session
|
|
611
|
+
/// stream, validate it, respond `200`, and surface the request to the app.
|
|
612
|
+
fn parse_server_connect_frames(
|
|
613
|
+
&mut self,
|
|
614
|
+
conn: &mut quiche::Connection,
|
|
615
|
+
id: u64,
|
|
616
|
+
ev: &mut Vec<Ev>,
|
|
617
|
+
) {
|
|
618
|
+
loop {
|
|
619
|
+
let (frame, headers_done) = {
|
|
620
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
621
|
+
return;
|
|
622
|
+
};
|
|
623
|
+
(st.frames.next_frame(), st.headers_done)
|
|
624
|
+
};
|
|
625
|
+
let Some((ty, payload)) = frame else { break };
|
|
626
|
+
|
|
627
|
+
if ty == h3::FRAME_HEADERS && !headers_done {
|
|
628
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
629
|
+
st.headers_done = true;
|
|
630
|
+
}
|
|
631
|
+
match h3::decode_header_block(&payload) {
|
|
632
|
+
Ok(headers) => self.accept_connect(conn, id, &headers, ev),
|
|
633
|
+
Err(e) => {
|
|
634
|
+
ev.push(Ev::Error(format!("failed to decode CONNECT request: {e}")));
|
|
635
|
+
self.reject_connect(conn, id, 400, ev);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
} else if ty == h3::FRAME_DATA {
|
|
639
|
+
self.scan_capsules(&payload, ev);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
fn accept_connect(
|
|
645
|
+
&mut self,
|
|
646
|
+
conn: &mut quiche::Connection,
|
|
647
|
+
id: u64,
|
|
648
|
+
headers: &[quiche::h3::Header],
|
|
649
|
+
ev: &mut Vec<Ev>,
|
|
650
|
+
) {
|
|
651
|
+
use quiche::h3::NameValue;
|
|
652
|
+
let mut method: Option<&[u8]> = None;
|
|
653
|
+
let mut protocol: Option<&[u8]> = None;
|
|
654
|
+
let mut authority = String::new();
|
|
655
|
+
let mut path = String::from("/");
|
|
656
|
+
let mut origin: Option<String> = None;
|
|
657
|
+
let mut extra: Vec<(String, String)> = Vec::new();
|
|
658
|
+
for h in headers {
|
|
659
|
+
let name = h.name();
|
|
660
|
+
let value = h.value();
|
|
661
|
+
match name {
|
|
662
|
+
b":method" => method = Some(value),
|
|
663
|
+
b":protocol" => protocol = Some(value),
|
|
664
|
+
b":authority" => authority = String::from_utf8_lossy(value).into_owned(),
|
|
665
|
+
b":path" => path = String::from_utf8_lossy(value).into_owned(),
|
|
666
|
+
b"origin" => origin = Some(String::from_utf8_lossy(value).into_owned()),
|
|
667
|
+
_ if name.starts_with(b":") => {} // other pseudo-headers
|
|
668
|
+
_ => extra.push((
|
|
669
|
+
String::from_utf8_lossy(name).into_owned(),
|
|
670
|
+
String::from_utf8_lossy(value).into_owned(),
|
|
671
|
+
)),
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
let ok = method == Some(b"CONNECT".as_ref()) && protocol == Some(b"webtransport".as_ref());
|
|
676
|
+
if !ok {
|
|
677
|
+
self.reject_connect(conn, id, 400, ev);
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
let resp = vec![quiche::h3::Header::new(b":status", b"200")];
|
|
682
|
+
match h3::encode_headers_frame(&resp) {
|
|
683
|
+
Ok(frame) => {
|
|
684
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
685
|
+
st.out.push_back(OutChunk {
|
|
686
|
+
data: frame,
|
|
687
|
+
off: 0,
|
|
688
|
+
request_id: None,
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
self.ready = true;
|
|
692
|
+
let (remote_addr, remote_port) = match self.remote_addr {
|
|
693
|
+
Some(a) => (a.ip().to_string(), a.port()),
|
|
694
|
+
None => (String::new(), 0),
|
|
695
|
+
};
|
|
696
|
+
ev.push(Ev::ServerReady {
|
|
697
|
+
authority,
|
|
698
|
+
path,
|
|
699
|
+
origin,
|
|
700
|
+
headers: extra,
|
|
701
|
+
remote_addr,
|
|
702
|
+
remote_port,
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
Err(e) => ev.push(Ev::Error(format!("failed to encode CONNECT response: {e}"))),
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
fn reject_connect(
|
|
710
|
+
&mut self,
|
|
711
|
+
conn: &mut quiche::Connection,
|
|
712
|
+
id: u64,
|
|
713
|
+
status: u16,
|
|
714
|
+
ev: &mut Vec<Ev>,
|
|
715
|
+
) {
|
|
716
|
+
let status_str = status.to_string();
|
|
717
|
+
let resp = vec![quiche::h3::Header::new(b":status", status_str.as_bytes())];
|
|
718
|
+
if let Ok(frame) = h3::encode_headers_frame(&resp) {
|
|
719
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
720
|
+
st.out.push_back(OutChunk {
|
|
721
|
+
data: frame,
|
|
722
|
+
off: 0,
|
|
723
|
+
request_id: None,
|
|
724
|
+
});
|
|
725
|
+
st.fin_queued = true;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
if !self.ready && !self.closed {
|
|
729
|
+
ev.push(Ev::Error(format!("rejected CONNECT with status {status}")));
|
|
730
|
+
}
|
|
731
|
+
self.mark_closed(ev, 0, Vec::new(), false);
|
|
732
|
+
let _ = conn;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
fn resolve_pending_uni(
|
|
736
|
+
&mut self,
|
|
737
|
+
conn: &mut quiche::Connection,
|
|
738
|
+
id: u64,
|
|
739
|
+
chunk: Vec<u8>,
|
|
740
|
+
fin: bool,
|
|
741
|
+
ev: &mut Vec<Ev>,
|
|
742
|
+
) {
|
|
743
|
+
let buf = {
|
|
744
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
745
|
+
return;
|
|
746
|
+
};
|
|
747
|
+
st.class_buf.extend_from_slice(&chunk);
|
|
748
|
+
st.class_buf.clone()
|
|
749
|
+
};
|
|
750
|
+
let Some((ty, n)) = h3::read_varint(&buf) else {
|
|
751
|
+
return; // need more bytes for the type varint
|
|
752
|
+
};
|
|
753
|
+
match ty {
|
|
754
|
+
t if t == h3::H3_CONTROL_STREAM_TYPE => {
|
|
755
|
+
let rest = buf[n..].to_vec();
|
|
756
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
757
|
+
return;
|
|
758
|
+
};
|
|
759
|
+
st.role = Role::PeerControl;
|
|
760
|
+
st.class_buf.clear();
|
|
761
|
+
st.frames.push(&rest);
|
|
762
|
+
self.parse_control_frames(id);
|
|
763
|
+
}
|
|
764
|
+
t if t == h3::QPACK_ENCODER_STREAM_TYPE || t == h3::QPACK_DECODER_STREAM_TYPE => {
|
|
765
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
766
|
+
return;
|
|
767
|
+
};
|
|
768
|
+
st.role = Role::Ignored;
|
|
769
|
+
st.class_buf.clear();
|
|
770
|
+
}
|
|
771
|
+
t if t == h3::WT_UNI_STREAM_TYPE => {
|
|
772
|
+
let Some((session, n2)) = h3::read_varint(&buf[n..]) else {
|
|
773
|
+
return; // need session id
|
|
774
|
+
};
|
|
775
|
+
if session != CONNECT_ID {
|
|
776
|
+
let _ = conn.stream_shutdown(id, quiche::Shutdown::Read, 0);
|
|
777
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
778
|
+
return;
|
|
779
|
+
};
|
|
780
|
+
st.role = Role::Ignored;
|
|
781
|
+
st.class_buf.clear();
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
let data = buf[n + n2..].to_vec();
|
|
785
|
+
{
|
|
786
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
787
|
+
return;
|
|
788
|
+
};
|
|
789
|
+
st.role = Role::WtData { bidi: false };
|
|
790
|
+
st.class_buf.clear();
|
|
791
|
+
}
|
|
792
|
+
ev.push(Ev::IncomingStream { id, bidi: false });
|
|
793
|
+
if !data.is_empty() {
|
|
794
|
+
ev.push(Ev::StreamData { id, data });
|
|
795
|
+
}
|
|
796
|
+
if fin {
|
|
797
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
798
|
+
st.recv_dead = true;
|
|
799
|
+
}
|
|
800
|
+
ev.push(Ev::StreamFinished { id });
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
_ => {
|
|
804
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
805
|
+
return;
|
|
806
|
+
};
|
|
807
|
+
st.role = Role::Ignored;
|
|
808
|
+
st.class_buf.clear();
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
fn resolve_pending_bidi(
|
|
814
|
+
&mut self,
|
|
815
|
+
conn: &mut quiche::Connection,
|
|
816
|
+
id: u64,
|
|
817
|
+
chunk: Vec<u8>,
|
|
818
|
+
fin: bool,
|
|
819
|
+
ev: &mut Vec<Ev>,
|
|
820
|
+
) {
|
|
821
|
+
let buf = {
|
|
822
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
823
|
+
return;
|
|
824
|
+
};
|
|
825
|
+
st.class_buf.extend_from_slice(&chunk);
|
|
826
|
+
st.class_buf.clone()
|
|
827
|
+
};
|
|
828
|
+
let Some((signal, n)) = h3::read_varint(&buf) else {
|
|
829
|
+
return;
|
|
830
|
+
};
|
|
831
|
+
if signal != h3::WT_BIDI_FRAME_TYPE {
|
|
832
|
+
let _ = conn.stream_shutdown(id, quiche::Shutdown::Read, 0);
|
|
833
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
834
|
+
return;
|
|
835
|
+
};
|
|
836
|
+
st.role = Role::Ignored;
|
|
837
|
+
st.class_buf.clear();
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
let Some((session, n2)) = h3::read_varint(&buf[n..]) else {
|
|
841
|
+
return;
|
|
842
|
+
};
|
|
843
|
+
if session != CONNECT_ID {
|
|
844
|
+
let _ = conn.stream_shutdown(id, quiche::Shutdown::Read, 0);
|
|
845
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
846
|
+
return;
|
|
847
|
+
};
|
|
848
|
+
st.role = Role::Ignored;
|
|
849
|
+
st.class_buf.clear();
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
let data = buf[n + n2..].to_vec();
|
|
853
|
+
{
|
|
854
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
855
|
+
return;
|
|
856
|
+
};
|
|
857
|
+
st.role = Role::WtData { bidi: true };
|
|
858
|
+
st.class_buf.clear();
|
|
859
|
+
}
|
|
860
|
+
ev.push(Ev::IncomingStream { id, bidi: true });
|
|
861
|
+
if !data.is_empty() {
|
|
862
|
+
ev.push(Ev::StreamData { id, data });
|
|
863
|
+
}
|
|
864
|
+
if fin {
|
|
865
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
866
|
+
st.recv_dead = true;
|
|
867
|
+
}
|
|
868
|
+
ev.push(Ev::StreamFinished { id });
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
pub fn on_datagrams(&mut self, conn: &mut quiche::Connection, ev: &mut Vec<Ev>) {
|
|
873
|
+
let mut buf = [0u8; 65_536];
|
|
874
|
+
loop {
|
|
875
|
+
match conn.dgram_recv(&mut buf) {
|
|
876
|
+
Ok(n) => {
|
|
877
|
+
let data = &buf[..n];
|
|
878
|
+
if let Some((qsi, off)) = h3::read_varint(data) {
|
|
879
|
+
// quarter-stream-id for our session is CONNECT_ID / 4 = 0.
|
|
880
|
+
if qsi == CONNECT_ID / 4 {
|
|
881
|
+
ev.push(Ev::Datagram(data[off..].to_vec()));
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
Err(quiche::Error::Done) => break,
|
|
886
|
+
Err(_) => break,
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
pub fn send_datagram(
|
|
892
|
+
&mut self,
|
|
893
|
+
conn: &mut quiche::Connection,
|
|
894
|
+
data: &[u8],
|
|
895
|
+
request_id: u64,
|
|
896
|
+
ev: &mut Vec<Ev>,
|
|
897
|
+
) {
|
|
898
|
+
let mut framed = Vec::with_capacity(data.len() + 1);
|
|
899
|
+
h3::put_varint(CONNECT_ID / 4, &mut framed);
|
|
900
|
+
framed.extend_from_slice(data);
|
|
901
|
+
let sent = conn.dgram_send(&framed).is_ok();
|
|
902
|
+
ev.push(Ev::DatagramAck { request_id, sent });
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
pub fn max_datagram_size(&self, conn: &quiche::Connection) -> usize {
|
|
906
|
+
// Subtract the 1-byte quarter-stream-id prefix (session id 0 → varint 0).
|
|
907
|
+
conn.dgram_max_writable_len().unwrap_or(0).saturating_sub(1)
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
pub fn open_stream(&mut self, bidi: bool, request_id: u64, ev: &mut Vec<Ev>) {
|
|
911
|
+
let id = if bidi {
|
|
912
|
+
let id = self.next_bidi;
|
|
913
|
+
self.next_bidi += 4;
|
|
914
|
+
id
|
|
915
|
+
} else {
|
|
916
|
+
let id = self.next_uni;
|
|
917
|
+
self.next_uni += 4;
|
|
918
|
+
id
|
|
919
|
+
};
|
|
920
|
+
// Seed the WebTransport stream signal prefix.
|
|
921
|
+
let mut prefix = Vec::new();
|
|
922
|
+
if bidi {
|
|
923
|
+
h3::put_varint(h3::WT_BIDI_FRAME_TYPE, &mut prefix);
|
|
924
|
+
} else {
|
|
925
|
+
h3::put_varint(h3::WT_UNI_STREAM_TYPE, &mut prefix);
|
|
926
|
+
}
|
|
927
|
+
h3::put_varint(CONNECT_ID, &mut prefix);
|
|
928
|
+
self.streams
|
|
929
|
+
.insert(id, Stream::with_prefix(Role::WtData { bidi }, prefix));
|
|
930
|
+
ev.push(Ev::StreamOpened { request_id, id });
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
pub fn stream_write(&mut self, id: u64, data: Vec<u8>, request_id: u64, ev: &mut Vec<Ev>) {
|
|
934
|
+
match self.streams.get_mut(&id) {
|
|
935
|
+
Some(st) => st.out.push_back(OutChunk {
|
|
936
|
+
data,
|
|
937
|
+
off: 0,
|
|
938
|
+
request_id: Some(request_id),
|
|
939
|
+
}),
|
|
940
|
+
None => ev.push(Ev::WriteAck { request_id }),
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
pub fn stream_fin(&mut self, id: u64) {
|
|
945
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
946
|
+
st.fin_queued = true;
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
pub fn stream_reset(&mut self, conn: &mut quiche::Connection, id: u64, code: u64) {
|
|
951
|
+
if self.streams.contains_key(&id) {
|
|
952
|
+
let _ = conn.stream_shutdown(id, quiche::Shutdown::Write, code);
|
|
953
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
954
|
+
st.out.clear();
|
|
955
|
+
st.fin_sent = true;
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
pub fn stream_stop_sending(&mut self, conn: &mut quiche::Connection, id: u64, code: u64) {
|
|
961
|
+
if self.streams.contains_key(&id) {
|
|
962
|
+
let _ = conn.stream_shutdown(id, quiche::Shutdown::Read, code);
|
|
963
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
964
|
+
st.recv_dead = true;
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
pub fn set_paused(&mut self, id: u64, paused: bool) {
|
|
970
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
971
|
+
st.paused = paused;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
pub fn close(&mut self, code: u32, reason: Vec<u8>) {
|
|
976
|
+
if self.close_req.is_none() {
|
|
977
|
+
self.close_req = Some(CloseReq {
|
|
978
|
+
code,
|
|
979
|
+
reason,
|
|
980
|
+
capsule_queued: false,
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/// Flush pending outbound stream data and drive the graceful-close sequence.
|
|
986
|
+
/// Returns an optional `(code, reason)` if the QUIC connection should now be
|
|
987
|
+
/// closed.
|
|
988
|
+
pub fn flush(&mut self, conn: &mut quiche::Connection, ev: &mut Vec<Ev>) {
|
|
989
|
+
// Queue the CLOSE_WEBTRANSPORT_SESSION capsule + FIN once, if requested.
|
|
990
|
+
if let Some(req) = self.close_req.as_mut() {
|
|
991
|
+
if !req.capsule_queued {
|
|
992
|
+
req.capsule_queued = true;
|
|
993
|
+
let mut value = Vec::with_capacity(4 + req.reason.len());
|
|
994
|
+
value.extend_from_slice(&req.code.to_be_bytes());
|
|
995
|
+
value.extend_from_slice(&req.reason);
|
|
996
|
+
let mut capsule = Vec::new();
|
|
997
|
+
h3::put_varint(h3::WT_CLOSE_SESSION_CAPSULE, &mut capsule);
|
|
998
|
+
h3::put_varint(value.len() as u64, &mut capsule);
|
|
999
|
+
capsule.extend_from_slice(&value);
|
|
1000
|
+
let body = h3::frame(h3::FRAME_DATA, &capsule);
|
|
1001
|
+
if let Some(st) = self.streams.get_mut(&CONNECT_ID) {
|
|
1002
|
+
st.out.push_back(OutChunk {
|
|
1003
|
+
data: body,
|
|
1004
|
+
off: 0,
|
|
1005
|
+
request_id: None,
|
|
1006
|
+
});
|
|
1007
|
+
st.fin_queued = true;
|
|
1008
|
+
} else {
|
|
1009
|
+
// No CONNECT stream (never established) → close QUIC directly.
|
|
1010
|
+
self.want_quic_close = Some((req.code, req.reason.clone()));
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
let ids: Vec<u64> = self.streams.keys().copied().collect();
|
|
1016
|
+
for id in ids {
|
|
1017
|
+
self.flush_stream(conn, id, ev);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
// Prune fully-terminated streams so a hostile peer churning streams
|
|
1021
|
+
// cannot grow the map without bound. Keep the CONNECT (session) stream
|
|
1022
|
+
// and our send-only control-plane streams for the session's lifetime.
|
|
1023
|
+
let is_server = self.is_server;
|
|
1024
|
+
self.streams.retain(|&id, st| {
|
|
1025
|
+
if id == CONNECT_ID || matches!(st.role, Role::LocalControlPlane) {
|
|
1026
|
+
return true;
|
|
1027
|
+
}
|
|
1028
|
+
// Direction from OUR perspective: a uni stream we initiated is
|
|
1029
|
+
// send-only; a uni stream the peer initiated is recv-only; bidi is
|
|
1030
|
+
// both. (id & 0x1 == 0 is client-initiated.)
|
|
1031
|
+
let is_uni = (id & 0x2) != 0;
|
|
1032
|
+
let our_init = if is_server {
|
|
1033
|
+
(id & 0x1) == 1
|
|
1034
|
+
} else {
|
|
1035
|
+
(id & 0x1) == 0
|
|
1036
|
+
};
|
|
1037
|
+
let can_recv = !is_uni || !our_init;
|
|
1038
|
+
let can_send = !is_uni || our_init;
|
|
1039
|
+
let recv_done = !can_recv || st.recv_dead;
|
|
1040
|
+
let send_done = !can_send || (st.fin_sent && st.out.is_empty());
|
|
1041
|
+
!(recv_done && send_done)
|
|
1042
|
+
});
|
|
1043
|
+
|
|
1044
|
+
// Opens blocked on stream credit simply keep their WT-signal prefix queued
|
|
1045
|
+
// in `out`; `flush_stream` retries them on every loop until credit lands.
|
|
1046
|
+
|
|
1047
|
+
// If a graceful close finished flushing the CONNECT stream, close QUIC.
|
|
1048
|
+
if self.want_quic_close.is_none() {
|
|
1049
|
+
if let Some(req) = self.close_req.as_ref() {
|
|
1050
|
+
let connect_drained = self
|
|
1051
|
+
.streams
|
|
1052
|
+
.get(&CONNECT_ID)
|
|
1053
|
+
.map(|s| s.out.is_empty() && s.fin_sent)
|
|
1054
|
+
.unwrap_or(true);
|
|
1055
|
+
if req.capsule_queued && connect_drained {
|
|
1056
|
+
self.want_quic_close = Some((req.code, req.reason.clone()));
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
if let Some((code, reason)) = self.want_quic_close.take() {
|
|
1062
|
+
let _ = conn.close(true, code as u64, &reason);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
fn flush_stream(&mut self, conn: &mut quiche::Connection, id: u64, ev: &mut Vec<Ev>) {
|
|
1067
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
1068
|
+
return;
|
|
1069
|
+
};
|
|
1070
|
+
// Note: we call `stream_send` directly rather than pre-checking
|
|
1071
|
+
// `stream_capacity`, because a stream does not exist in quiche until its
|
|
1072
|
+
// first `stream_send`, and `stream_capacity` errors (InvalidStreamState)
|
|
1073
|
+
// on a not-yet-created stream.
|
|
1074
|
+
while let Some(front) = st.out.front_mut() {
|
|
1075
|
+
let remaining_len = front.data.len() - front.off;
|
|
1076
|
+
match conn.stream_send(id, &front.data[front.off..], false) {
|
|
1077
|
+
Ok(sent) => {
|
|
1078
|
+
front.off += sent;
|
|
1079
|
+
if front.off >= front.data.len() {
|
|
1080
|
+
let done = st.out.pop_front().unwrap();
|
|
1081
|
+
if let Some(rid) = done.request_id {
|
|
1082
|
+
ev.push(Ev::WriteAck { request_id: rid });
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
if sent < remaining_len {
|
|
1086
|
+
return; // no more capacity right now
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
Err(quiche::Error::Done) => return,
|
|
1090
|
+
Err(quiche::Error::StreamLimit) => return, // no stream credit yet; retry next loop
|
|
1091
|
+
Err(quiche::Error::StreamStopped(code)) => {
|
|
1092
|
+
ev.push(Ev::StreamStopSending { id, code });
|
|
1093
|
+
// Settle every queued write's promise so a stalled write()
|
|
1094
|
+
// (and its WritableStream) cannot hang forever.
|
|
1095
|
+
for chunk in st.out.drain(..) {
|
|
1096
|
+
if let Some(rid) = chunk.request_id {
|
|
1097
|
+
ev.push(Ev::WriteAck { request_id: rid });
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
st.fin_sent = true;
|
|
1101
|
+
return;
|
|
1102
|
+
}
|
|
1103
|
+
Err(_) => return,
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
if st.out.is_empty()
|
|
1108
|
+
&& st.fin_queued
|
|
1109
|
+
&& !st.fin_sent
|
|
1110
|
+
&& conn.stream_send(id, &[], true).is_ok()
|
|
1111
|
+
{
|
|
1112
|
+
st.fin_sent = true;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
pub fn mark_closed(&mut self, ev: &mut Vec<Ev>, code: u32, reason: Vec<u8>, remote: bool) {
|
|
1117
|
+
if self.closed {
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
self.closed = true;
|
|
1121
|
+
ev.push(Ev::Closed {
|
|
1122
|
+
code,
|
|
1123
|
+
reason,
|
|
1124
|
+
remote,
|
|
1125
|
+
});
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
pub fn is_closed(&self) -> bool {
|
|
1129
|
+
self.closed
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
/// Whether a stream's HTTP/3 frame parser has latched a fatal error.
|
|
1133
|
+
fn frame_error(&self, id: u64) -> bool {
|
|
1134
|
+
self.streams
|
|
1135
|
+
.get(&id)
|
|
1136
|
+
.map(|s| s.frames.error)
|
|
1137
|
+
.unwrap_or(false)
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/// Stop reading a stream and release its receive buffers (used when a stream
|
|
1141
|
+
/// is finished, reset, or has produced a fatal protocol error), so a hostile
|
|
1142
|
+
/// server cannot keep us draining it into an unbounded buffer.
|
|
1143
|
+
fn kill_recv(&mut self, conn: &mut quiche::Connection, id: u64) {
|
|
1144
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
1145
|
+
st.recv_dead = true;
|
|
1146
|
+
st.frames.buf = Vec::new();
|
|
1147
|
+
st.class_buf = Vec::new();
|
|
1148
|
+
}
|
|
1149
|
+
let _ = conn.stream_shutdown(id, quiche::Shutdown::Read, 0);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
fn role_name(role: Role) -> &'static str {
|
|
1154
|
+
match role {
|
|
1155
|
+
Role::LocalControlPlane => "LocalControlPlane",
|
|
1156
|
+
Role::Connect => "Connect",
|
|
1157
|
+
Role::PeerControl => "PeerControl",
|
|
1158
|
+
Role::Ignored => "Ignored",
|
|
1159
|
+
Role::WtData { .. } => "WtData",
|
|
1160
|
+
Role::PendingUni => "PendingUni",
|
|
1161
|
+
Role::PendingBidi => "PendingBidi",
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
/// Classify a not-yet-seen stream id by its QUIC stream-type bits.
|
|
1166
|
+
/// Classify a not-yet-seen peer-initiated stream by its QUIC stream-type bits.
|
|
1167
|
+
/// The peer is the server (for a client-role session) or the client (for a
|
|
1168
|
+
/// server-role session).
|
|
1169
|
+
fn classify_new(id: u64, is_server: bool) -> Role {
|
|
1170
|
+
if is_server {
|
|
1171
|
+
// Peer = client (id & 0x1 == 0).
|
|
1172
|
+
match id & 0x3 {
|
|
1173
|
+
0x0 => {
|
|
1174
|
+
if id == CONNECT_ID {
|
|
1175
|
+
Role::Connect // the incoming Extended CONNECT
|
|
1176
|
+
} else {
|
|
1177
|
+
Role::PendingBidi // a client-opened WebTransport bidi stream
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
0x2 => Role::PendingUni, // client control/QPACK or WT uni stream
|
|
1181
|
+
_ => Role::Ignored,
|
|
1182
|
+
}
|
|
1183
|
+
} else {
|
|
1184
|
+
// Peer = server (id & 0x1 == 1).
|
|
1185
|
+
match id & 0x3 {
|
|
1186
|
+
0x3 => Role::PendingUni, // server-initiated unidirectional
|
|
1187
|
+
0x1 => Role::PendingBidi, // server-initiated bidirectional
|
|
1188
|
+
_ => Role::Ignored, // an unexpected client-initiated id
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
}
|