rwebtransport 0.2.1 → 0.2.3
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 +462 -6
- package/README.md +5 -1
- package/crates/native/Cargo.toml +2 -0
- package/crates/native/src/config.rs +56 -7
- package/crates/native/src/driver.rs +11 -1
- package/crates/native/src/lib.rs +134 -47
- package/crates/native/src/parser_memory.rs +202 -0
- package/crates/native/src/server.rs +19 -2
- package/crates/native/src/session.rs +1068 -122
- package/crates/native/src/test_support.rs +119 -0
- package/crates/wtcore/Cargo.toml +1 -0
- package/crates/wtcore/src/h3.rs +85 -37
- package/dist/index.js +133 -37
- package/dist/index.js.map +7 -1
- package/dist/index.mjs +129 -31
- package/dist/index.mjs.map +7 -1
- package/dist/native.d.ts +1 -0
- package/dist/server.d.ts +1 -0
- package/package.json +81 -78
- package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-x64/rwebtransport.node +0 -0
- package/prebuilds/win32-x64/rwebtransport.node +0 -0
- package/scripts/build-ts.js +41 -0
- package/vendor/quiche/src/lib.rs +15 -0
- package/vendor/quiche/src/stream/recv_buf.rs +5 -0
- package/vendor/quiche/src/stream/send_buf.rs +5 -0
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
//! * uni `14, 18, ...` are WebTransport unidirectional streams.
|
|
14
14
|
|
|
15
15
|
use std::collections::{HashMap, VecDeque};
|
|
16
|
+
use std::sync::Arc;
|
|
17
|
+
|
|
18
|
+
use crate::parser_memory::{ParserBuffer, ParserMemory};
|
|
16
19
|
|
|
17
20
|
use wtcore::h3;
|
|
18
21
|
|
|
@@ -43,6 +46,10 @@ const SERVER_FIRST_WT_UNI_ID: u64 = 15;
|
|
|
43
46
|
/// hostile peer advertising a huge length).
|
|
44
47
|
const MAX_CONTROL_FRAME: usize = 1 << 20;
|
|
45
48
|
|
|
49
|
+
/// Bound all parser allocations on a connection as well as the shared server
|
|
50
|
+
/// total, so opening many control streams cannot consume the entire budget.
|
|
51
|
+
const MAX_SESSION_PARSER_MEMORY: usize = 8 * 1024 * 1024;
|
|
52
|
+
|
|
46
53
|
/// Per-session flow-control credit we grant the peer once established. Large,
|
|
47
54
|
/// because QUIC's connection flow control is the real limit (one session per
|
|
48
55
|
/// connection); this only keeps a strict peer from stalling on our advertised
|
|
@@ -160,6 +167,12 @@ pub fn build_keying_material(
|
|
|
160
167
|
context: &[u8],
|
|
161
168
|
length: u32,
|
|
162
169
|
) -> Ev {
|
|
170
|
+
if !(1..=4096).contains(&length) {
|
|
171
|
+
return Ev::KeyingMaterial {
|
|
172
|
+
request_id,
|
|
173
|
+
data: None,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
163
176
|
let mut out = vec![0u8; length as usize];
|
|
164
177
|
let data = match conn.export_keying_material(&mut out, label, Some(context)) {
|
|
165
178
|
Ok(()) => Some(out),
|
|
@@ -186,8 +199,13 @@ enum Role {
|
|
|
186
199
|
PeerControl,
|
|
187
200
|
/// A peer uni stream we drain and discard (QPACK/push/unknown).
|
|
188
201
|
Ignored,
|
|
202
|
+
PeerQpack {
|
|
203
|
+
encoder: bool,
|
|
204
|
+
},
|
|
189
205
|
/// A resolved WebTransport data stream.
|
|
190
|
-
WtData {
|
|
206
|
+
WtData {
|
|
207
|
+
bidi: bool,
|
|
208
|
+
},
|
|
191
209
|
/// A peer-initiated uni stream whose type varint isn't fully read yet.
|
|
192
210
|
PendingUni,
|
|
193
211
|
/// A peer-initiated bidi stream whose WT signal isn't fully read yet.
|
|
@@ -197,7 +215,7 @@ enum Role {
|
|
|
197
215
|
struct Stream {
|
|
198
216
|
role: Role,
|
|
199
217
|
/// Classification accumulator (pending server streams) / control byte carry.
|
|
200
|
-
class_buf:
|
|
218
|
+
class_buf: ParserBuffer,
|
|
201
219
|
/// HTTP/3 frame parser (Connect + PeerControl).
|
|
202
220
|
frames: FrameParser,
|
|
203
221
|
/// CONNECT stream: response HEADERS already parsed.
|
|
@@ -211,25 +229,36 @@ struct Stream {
|
|
|
211
229
|
fin_sent: bool,
|
|
212
230
|
/// Peer already reset/finished, stop touching recv.
|
|
213
231
|
recv_dead: bool,
|
|
232
|
+
recv_payload: u64,
|
|
233
|
+
recv_prefix: u64,
|
|
234
|
+
send_payload: u64,
|
|
235
|
+
send_prefix: u64,
|
|
214
236
|
}
|
|
215
237
|
|
|
216
238
|
impl Stream {
|
|
217
|
-
fn new(role: Role) -> Self {
|
|
239
|
+
fn new(role: Role, memory: Arc<ParserMemory>) -> Self {
|
|
218
240
|
Self {
|
|
219
241
|
role,
|
|
220
|
-
class_buf:
|
|
221
|
-
frames: FrameParser::
|
|
242
|
+
class_buf: ParserBuffer::new(memory.clone()),
|
|
243
|
+
frames: FrameParser::new(memory),
|
|
222
244
|
headers_done: false,
|
|
223
245
|
paused: false,
|
|
224
246
|
out: VecDeque::new(),
|
|
225
247
|
fin_queued: false,
|
|
226
248
|
fin_sent: false,
|
|
227
249
|
recv_dead: false,
|
|
250
|
+
recv_payload: 0,
|
|
251
|
+
recv_prefix: 0,
|
|
252
|
+
send_payload: 0,
|
|
253
|
+
send_prefix: 0,
|
|
228
254
|
}
|
|
229
255
|
}
|
|
230
256
|
|
|
231
|
-
fn with_prefix(role: Role, prefix: Vec<u8>) -> Self {
|
|
232
|
-
let mut s = Self::new(role);
|
|
257
|
+
fn with_prefix(role: Role, prefix: Vec<u8>, memory: Arc<ParserMemory>) -> Self {
|
|
258
|
+
let mut s = Self::new(role, memory);
|
|
259
|
+
if matches!(role, Role::WtData { .. }) {
|
|
260
|
+
s.send_prefix = prefix.len() as u64;
|
|
261
|
+
}
|
|
233
262
|
if !prefix.is_empty() {
|
|
234
263
|
s.out.push_back(OutChunk {
|
|
235
264
|
data: prefix,
|
|
@@ -242,36 +271,53 @@ impl Stream {
|
|
|
242
271
|
}
|
|
243
272
|
|
|
244
273
|
/// Incremental HTTP/3 frame reassembler.
|
|
245
|
-
#[derive(Default)]
|
|
246
274
|
struct FrameParser {
|
|
247
|
-
buf:
|
|
275
|
+
buf: ParserBuffer,
|
|
248
276
|
error: bool,
|
|
249
277
|
}
|
|
250
278
|
|
|
251
279
|
impl FrameParser {
|
|
280
|
+
fn new(memory: Arc<ParserMemory>) -> Self {
|
|
281
|
+
Self {
|
|
282
|
+
buf: ParserBuffer::new(memory),
|
|
283
|
+
error: false,
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
252
287
|
fn push(&mut self, data: &[u8]) {
|
|
253
|
-
self.
|
|
288
|
+
if self.error {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
if self.buf.extend_from_slice(data).is_err() {
|
|
292
|
+
self.error = true;
|
|
293
|
+
}
|
|
254
294
|
}
|
|
255
295
|
|
|
256
296
|
/// Pop the next complete `(type, payload)` frame, or `None` if incomplete.
|
|
257
|
-
fn next_frame(&mut self) -> Option<(u64,
|
|
297
|
+
fn next_frame(&mut self) -> Option<(u64, ParserBuffer)> {
|
|
258
298
|
if self.error {
|
|
259
299
|
return None;
|
|
260
300
|
}
|
|
261
301
|
let (ty, n1) = h3::read_varint(&self.buf)?;
|
|
262
302
|
let (len, n2) = h3::read_varint(&self.buf[n1..])?;
|
|
263
|
-
|
|
264
|
-
if len > MAX_CONTROL_FRAME {
|
|
303
|
+
if len > MAX_CONTROL_FRAME as u64 {
|
|
265
304
|
self.error = true;
|
|
266
305
|
return None;
|
|
267
306
|
}
|
|
307
|
+
let len = len as usize;
|
|
268
308
|
let header = n1 + n2;
|
|
269
309
|
let total = header + len;
|
|
270
310
|
if self.buf.len() < total {
|
|
271
311
|
return None;
|
|
272
312
|
}
|
|
273
|
-
let payload = self.buf
|
|
274
|
-
|
|
313
|
+
let payload = match self.buf.copy_range(header..total) {
|
|
314
|
+
Ok(payload) => payload,
|
|
315
|
+
Err(()) => {
|
|
316
|
+
self.error = true;
|
|
317
|
+
return None;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
self.buf.consume(total);
|
|
275
321
|
Some((ty, payload))
|
|
276
322
|
}
|
|
277
323
|
}
|
|
@@ -304,6 +350,7 @@ pub struct WtSession {
|
|
|
304
350
|
|
|
305
351
|
next_bidi: u64,
|
|
306
352
|
next_uni: u64,
|
|
353
|
+
pending_opens: VecDeque<(bool, u64)>,
|
|
307
354
|
|
|
308
355
|
setup_sent: bool,
|
|
309
356
|
ready: bool,
|
|
@@ -319,24 +366,28 @@ pub struct WtSession {
|
|
|
319
366
|
/// Set once we granted the peer per-session flow-control credit.
|
|
320
367
|
fc_credit_granted: bool,
|
|
321
368
|
/// Peer's send-side session limits (from SETTINGS_WT_INITIAL_* and WT_MAX_*
|
|
322
|
-
/// capsules)
|
|
323
|
-
/// one-session-per-connection model; these drive only the WT_*_BLOCKED
|
|
324
|
-
/// signaling below.
|
|
369
|
+
/// capsules), enforced in addition to QUIC's transport-level limits.
|
|
325
370
|
fc_max_data_remote: u64,
|
|
326
371
|
fc_max_streams_bidi_remote: u64,
|
|
327
372
|
fc_max_streams_uni_remote: u64,
|
|
328
373
|
/// Cumulative WebTransport stream payload bytes handed to quiche, compared
|
|
329
374
|
/// against `fc_max_data_remote` to detect a WT_DATA_BLOCKED condition.
|
|
330
375
|
fc_data_sent: u64,
|
|
376
|
+
fc_data_received: u64,
|
|
377
|
+
fc_max_data_local: u64,
|
|
378
|
+
fc_max_streams_local: [u64; 2],
|
|
379
|
+
fc_streams_received: [u64; 2],
|
|
380
|
+
terminal_error: Option<(u64, Vec<u8>)>,
|
|
331
381
|
/// The WT_MAX_DATA / WT_MAX_STREAMS_* limit value we last announced as blocked
|
|
332
|
-
/// at (
|
|
333
|
-
data_blocked_at: u64
|
|
334
|
-
streams_bidi_blocked_at: u64
|
|
335
|
-
streams_uni_blocked_at: u64
|
|
382
|
+
/// at (None = no signal yet), so a BLOCKED capsule is re-sent only when the limit changes.
|
|
383
|
+
data_blocked_at: Option<u64>,
|
|
384
|
+
streams_bidi_blocked_at: Option<u64>,
|
|
385
|
+
streams_uni_blocked_at: Option<u64>,
|
|
336
386
|
/// Reassembly buffer for capsules on the session (CONNECT) stream: RFC 9297
|
|
337
387
|
/// lets a capsule span multiple HTTP/3 DATA frames, so bytes are accumulated
|
|
338
388
|
/// here and only whole capsules are consumed.
|
|
339
|
-
capsule_buf:
|
|
389
|
+
capsule_buf: ParserBuffer,
|
|
390
|
+
parser_memory: Arc<ParserMemory>,
|
|
340
391
|
|
|
341
392
|
peer_settings: Option<h3::PeerSettings>,
|
|
342
393
|
|
|
@@ -365,6 +416,7 @@ impl WtSession {
|
|
|
365
416
|
extra_headers: Vec<(String, String)>,
|
|
366
417
|
protocols: Vec<String>,
|
|
367
418
|
) -> Self {
|
|
419
|
+
let parser_memory = ParserMemory::new(MAX_SESSION_PARSER_MEMORY);
|
|
368
420
|
Self {
|
|
369
421
|
is_server: false,
|
|
370
422
|
authority,
|
|
@@ -377,6 +429,7 @@ impl WtSession {
|
|
|
377
429
|
response_headers: Vec::new(),
|
|
378
430
|
next_bidi: CLIENT_FIRST_WT_BIDI_ID,
|
|
379
431
|
next_uni: CLIENT_FIRST_WT_UNI_ID,
|
|
432
|
+
pending_opens: VecDeque::new(),
|
|
380
433
|
setup_sent: false,
|
|
381
434
|
ready: false,
|
|
382
435
|
closed: false,
|
|
@@ -388,10 +441,16 @@ impl WtSession {
|
|
|
388
441
|
fc_max_streams_bidi_remote: 0,
|
|
389
442
|
fc_max_streams_uni_remote: 0,
|
|
390
443
|
fc_data_sent: 0,
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
444
|
+
fc_data_received: 0,
|
|
445
|
+
fc_max_data_local: h3::WT_INITIAL_MAX_DATA,
|
|
446
|
+
fc_max_streams_local: [h3::WT_INITIAL_MAX_STREAMS; 2],
|
|
447
|
+
fc_streams_received: [0; 2],
|
|
448
|
+
terminal_error: None,
|
|
449
|
+
data_blocked_at: None,
|
|
450
|
+
streams_bidi_blocked_at: None,
|
|
451
|
+
streams_uni_blocked_at: None,
|
|
452
|
+
capsule_buf: ParserBuffer::new(parser_memory.clone()),
|
|
453
|
+
parser_memory,
|
|
395
454
|
peer_settings: None,
|
|
396
455
|
streams: HashMap::new(),
|
|
397
456
|
close_req: None,
|
|
@@ -410,7 +469,9 @@ impl WtSession {
|
|
|
410
469
|
supported_protocols: Vec<String>,
|
|
411
470
|
allowed_origins: Option<Vec<String>>,
|
|
412
471
|
response_headers: Vec<(String, String)>,
|
|
472
|
+
parser_memory: Arc<ParserMemory>,
|
|
413
473
|
) -> Self {
|
|
474
|
+
let parser_memory = ParserMemory::child(parser_memory, MAX_SESSION_PARSER_MEMORY);
|
|
414
475
|
Self {
|
|
415
476
|
is_server: true,
|
|
416
477
|
authority: String::new(),
|
|
@@ -423,6 +484,7 @@ impl WtSession {
|
|
|
423
484
|
response_headers,
|
|
424
485
|
next_bidi: SERVER_FIRST_WT_BIDI_ID,
|
|
425
486
|
next_uni: SERVER_FIRST_WT_UNI_ID,
|
|
487
|
+
pending_opens: VecDeque::new(),
|
|
426
488
|
setup_sent: false,
|
|
427
489
|
ready: false,
|
|
428
490
|
closed: false,
|
|
@@ -434,10 +496,16 @@ impl WtSession {
|
|
|
434
496
|
fc_max_streams_bidi_remote: 0,
|
|
435
497
|
fc_max_streams_uni_remote: 0,
|
|
436
498
|
fc_data_sent: 0,
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
499
|
+
fc_data_received: 0,
|
|
500
|
+
fc_max_data_local: h3::WT_INITIAL_MAX_DATA,
|
|
501
|
+
fc_max_streams_local: [h3::WT_INITIAL_MAX_STREAMS; 2],
|
|
502
|
+
fc_streams_received: [0; 2],
|
|
503
|
+
terminal_error: None,
|
|
504
|
+
data_blocked_at: None,
|
|
505
|
+
streams_bidi_blocked_at: None,
|
|
506
|
+
streams_uni_blocked_at: None,
|
|
507
|
+
capsule_buf: ParserBuffer::new(parser_memory.clone()),
|
|
508
|
+
parser_memory,
|
|
441
509
|
peer_settings: None,
|
|
442
510
|
streams: HashMap::new(),
|
|
443
511
|
close_req: None,
|
|
@@ -453,9 +521,9 @@ impl WtSession {
|
|
|
453
521
|
}
|
|
454
522
|
|
|
455
523
|
/// Called once the QUIC connection is established: open the HTTP/3
|
|
456
|
-
/// control-plane streams and advertise SETTINGS. A client
|
|
457
|
-
///
|
|
458
|
-
/// bidi stream 0.
|
|
524
|
+
/// control-plane streams and advertise SETTINGS. A client waits for the
|
|
525
|
+
/// server's SETTINGS before sending CONNECT; a server waits for the client's
|
|
526
|
+
/// CONNECT on bidi stream 0.
|
|
459
527
|
pub fn on_established(&mut self) {
|
|
460
528
|
if self.setup_sent {
|
|
461
529
|
return;
|
|
@@ -471,20 +539,24 @@ impl WtSession {
|
|
|
471
539
|
// HTTP/3 control stream: type prefix + SETTINGS.
|
|
472
540
|
self.streams.insert(
|
|
473
541
|
control_id,
|
|
474
|
-
Stream::with_prefix(
|
|
542
|
+
Stream::with_prefix(
|
|
543
|
+
Role::LocalControlPlane,
|
|
544
|
+
h3::control_stream_prefix(),
|
|
545
|
+
self.parser_memory.clone(),
|
|
546
|
+
),
|
|
475
547
|
);
|
|
476
548
|
// QPACK encoder / decoder streams (dynamic table disabled → type only).
|
|
477
549
|
let mut enc = Vec::new();
|
|
478
550
|
h3::put_varint(h3::QPACK_ENCODER_STREAM_TYPE, &mut enc);
|
|
479
551
|
self.streams.insert(
|
|
480
552
|
qpack_enc_id,
|
|
481
|
-
Stream::with_prefix(Role::LocalControlPlane, enc),
|
|
553
|
+
Stream::with_prefix(Role::LocalControlPlane, enc, self.parser_memory.clone()),
|
|
482
554
|
);
|
|
483
555
|
let mut dec = Vec::new();
|
|
484
556
|
h3::put_varint(h3::QPACK_DECODER_STREAM_TYPE, &mut dec);
|
|
485
557
|
self.streams.insert(
|
|
486
558
|
qpack_dec_id,
|
|
487
|
-
Stream::with_prefix(Role::LocalControlPlane, dec),
|
|
559
|
+
Stream::with_prefix(Role::LocalControlPlane, dec, self.parser_memory.clone()),
|
|
488
560
|
);
|
|
489
561
|
|
|
490
562
|
if !self.is_server {
|
|
@@ -513,10 +585,13 @@ impl WtSession {
|
|
|
513
585
|
backpressured: bool,
|
|
514
586
|
ev: &mut Vec<Ev>,
|
|
515
587
|
) {
|
|
588
|
+
if self.closed {
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
516
591
|
let is_server = self.is_server;
|
|
517
|
-
self.streams
|
|
518
|
-
|
|
519
|
-
|
|
592
|
+
self.streams.entry(id).or_insert_with(|| {
|
|
593
|
+
Stream::new(classify_new(id, is_server), self.parser_memory.clone())
|
|
594
|
+
});
|
|
520
595
|
|
|
521
596
|
// Respect per-stream and global read backpressure for resolved data
|
|
522
597
|
// streams (leave the bytes in quiche so the peer is flow-controlled).
|
|
@@ -535,17 +610,41 @@ impl WtSession {
|
|
|
535
610
|
return;
|
|
536
611
|
}
|
|
537
612
|
|
|
613
|
+
// Pending bidi streams are application data. Under global pressure,
|
|
614
|
+
// classify uni stream types one byte at a time, then leave WT headers
|
|
615
|
+
// and payload in QUIC while allowing critical streams to drain.
|
|
616
|
+
if backpressured && matches!(self.streams[&id].role, Role::PendingBidi) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
|
|
538
620
|
// Drain everything quiche currently has for this stream.
|
|
539
|
-
let mut chunk = Vec::new();
|
|
540
|
-
let mut fin = false;
|
|
541
621
|
let mut reset: Option<u64> = None;
|
|
542
622
|
let mut buf = [0u8; 16 * 1024];
|
|
543
623
|
loop {
|
|
544
|
-
|
|
624
|
+
let st = &self.streams[&id];
|
|
625
|
+
if backpressured
|
|
626
|
+
&& (matches!(st.role, Role::WtData { .. })
|
|
627
|
+
|| (matches!(st.role, Role::PendingUni)
|
|
628
|
+
&& h3::read_varint(&st.class_buf)
|
|
629
|
+
.is_some_and(|(ty, _)| ty == h3::WT_UNI_STREAM_TYPE)))
|
|
630
|
+
{
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
let read_len = if backpressured && matches!(st.role, Role::PendingUni) {
|
|
634
|
+
1
|
|
635
|
+
} else {
|
|
636
|
+
buf.len()
|
|
637
|
+
};
|
|
638
|
+
let final_size = conn.stream_recv_final_size(id);
|
|
639
|
+
match conn.stream_recv(id, &mut buf[..read_len]) {
|
|
545
640
|
Ok((n, f)) => {
|
|
546
|
-
|
|
641
|
+
// Parse bounded chunks immediately. Do not build an entire
|
|
642
|
+
// stream-sized scratch allocation outside the parser budget.
|
|
643
|
+
self.process_recv(conn, id, buf[..n].to_vec(), f, ev);
|
|
644
|
+
if self.closed {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
547
647
|
if f {
|
|
548
|
-
fin = true;
|
|
549
648
|
break;
|
|
550
649
|
}
|
|
551
650
|
if n == 0 {
|
|
@@ -554,6 +653,17 @@ impl WtSession {
|
|
|
554
653
|
}
|
|
555
654
|
Err(quiche::Error::Done) => break,
|
|
556
655
|
Err(quiche::Error::StreamReset(code)) => {
|
|
656
|
+
if let Some(size) = final_size {
|
|
657
|
+
let st = &self.streams[&id];
|
|
658
|
+
if matches!(st.role, Role::WtData { .. }) {
|
|
659
|
+
let missing = size
|
|
660
|
+
.saturating_sub(st.recv_prefix)
|
|
661
|
+
.saturating_sub(st.recv_payload);
|
|
662
|
+
if !self.account_receive(id, missing, ev) {
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
557
667
|
reset = Some(code);
|
|
558
668
|
break;
|
|
559
669
|
}
|
|
@@ -573,6 +683,9 @@ impl WtSession {
|
|
|
573
683
|
let app = h3::http_code_to_webtransport_code(code).unwrap_or(code);
|
|
574
684
|
ev.push(Ev::StreamReset { id, code: app })
|
|
575
685
|
}
|
|
686
|
+
Some(Role::PeerControl | Role::PeerQpack { .. }) => {
|
|
687
|
+
self.protocol_error(conn, ev, 0x104, "peer reset a critical HTTP/3 stream");
|
|
688
|
+
}
|
|
576
689
|
Some(Role::Connect) if !self.closed => {
|
|
577
690
|
// A reset of the session (CONNECT) stream ends the session;
|
|
578
691
|
// if we swallowed it, ready/closed would hang forever.
|
|
@@ -587,19 +700,6 @@ impl WtSession {
|
|
|
587
700
|
}
|
|
588
701
|
return;
|
|
589
702
|
}
|
|
590
|
-
|
|
591
|
-
if dbg_on() {
|
|
592
|
-
let role = self
|
|
593
|
-
.streams
|
|
594
|
-
.get(&id)
|
|
595
|
-
.map(|s| role_name(s.role))
|
|
596
|
-
.unwrap_or("?");
|
|
597
|
-
eprintln!(
|
|
598
|
-
"[rwt-session] readable id={id} role={role} chunk={} fin={fin}",
|
|
599
|
-
chunk.len()
|
|
600
|
-
);
|
|
601
|
-
}
|
|
602
|
-
self.process_recv(conn, id, chunk, fin, ev);
|
|
603
703
|
}
|
|
604
704
|
|
|
605
705
|
fn process_recv(
|
|
@@ -611,10 +711,22 @@ impl WtSession {
|
|
|
611
711
|
ev: &mut Vec<Ev>,
|
|
612
712
|
) {
|
|
613
713
|
let role = self.streams.get(&id).map(|s| s.role);
|
|
714
|
+
if dbg_on() {
|
|
715
|
+
eprintln!(
|
|
716
|
+
"[rwt-session] readable id={id} role={} chunk={} fin={fin}",
|
|
717
|
+
role.map(role_name).unwrap_or("?"),
|
|
718
|
+
chunk.len()
|
|
719
|
+
);
|
|
720
|
+
}
|
|
614
721
|
match role {
|
|
615
722
|
Some(Role::LocalControlPlane) | None => {
|
|
616
723
|
// Our send-only control-plane streams / unknown: discard reads.
|
|
617
724
|
}
|
|
725
|
+
Some(Role::PeerQpack { .. }) => {
|
|
726
|
+
if fin {
|
|
727
|
+
self.protocol_error(conn, ev, 0x104, "peer closed a critical QPACK stream");
|
|
728
|
+
}
|
|
729
|
+
}
|
|
618
730
|
Some(Role::Ignored) => {
|
|
619
731
|
// A drained peer stream (QPACK/push/unknown): mark it finished so
|
|
620
732
|
// it can be pruned, otherwise stream churn grows the map.
|
|
@@ -628,7 +740,10 @@ impl WtSession {
|
|
|
628
740
|
if let Some(st) = self.streams.get_mut(&id) {
|
|
629
741
|
st.frames.push(&chunk);
|
|
630
742
|
}
|
|
631
|
-
self.parse_control_frames(id, ev);
|
|
743
|
+
self.parse_control_frames(conn, id, ev);
|
|
744
|
+
if fin && !self.closed {
|
|
745
|
+
self.protocol_error(conn, ev, 0x104, "peer closed the HTTP/3 control stream");
|
|
746
|
+
}
|
|
632
747
|
// A malformed control stream is a fatal H3 error; stop reading it
|
|
633
748
|
// so a hostile server cannot keep us draining (and re-crediting)
|
|
634
749
|
// an oversized frame into an unbounded buffer.
|
|
@@ -657,10 +772,22 @@ impl WtSession {
|
|
|
657
772
|
let _ = conn.close(true, 0, b"h3 connect error");
|
|
658
773
|
}
|
|
659
774
|
if fin && !self.closed {
|
|
660
|
-
self
|
|
775
|
+
if self
|
|
776
|
+
.streams
|
|
777
|
+
.get(&id)
|
|
778
|
+
.is_some_and(|st| !st.frames.buf.is_empty())
|
|
779
|
+
|| !self.capsule_buf.is_empty()
|
|
780
|
+
{
|
|
781
|
+
self.protocol_error(conn, ev, 0x106, "truncated CONNECT frame or capsule");
|
|
782
|
+
} else {
|
|
783
|
+
self.mark_closed(ev, 0, Vec::new(), true);
|
|
784
|
+
}
|
|
661
785
|
}
|
|
662
786
|
}
|
|
663
787
|
Some(Role::WtData { .. }) => {
|
|
788
|
+
if !self.account_receive(id, chunk.len() as u64, ev) {
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
664
791
|
if !chunk.is_empty() {
|
|
665
792
|
ev.push(Ev::StreamData { id, data: chunk });
|
|
666
793
|
}
|
|
@@ -680,15 +807,34 @@ impl WtSession {
|
|
|
680
807
|
}
|
|
681
808
|
}
|
|
682
809
|
|
|
683
|
-
fn parse_control_frames(&mut self, id: u64, ev: &mut Vec<Ev>) {
|
|
684
|
-
|
|
810
|
+
fn parse_control_frames(&mut self, conn: &mut quiche::Connection, id: u64, ev: &mut Vec<Ev>) {
|
|
811
|
+
while !self.closed {
|
|
685
812
|
let frame = self
|
|
686
813
|
.streams
|
|
687
814
|
.get_mut(&id)
|
|
688
815
|
.and_then(|s| s.frames.next_frame());
|
|
689
816
|
let Some((ty, payload)) = frame else { break };
|
|
817
|
+
let first = !self.streams[&id].headers_done;
|
|
818
|
+
if first && ty != h3::FRAME_SETTINGS {
|
|
819
|
+
self.protocol_error(conn, ev, 0x10a, "SETTINGS must be the first control frame");
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
690
822
|
if ty == h3::FRAME_SETTINGS {
|
|
691
|
-
|
|
823
|
+
if !first {
|
|
824
|
+
self.protocol_error(conn, ev, 0x105, "repeated SETTINGS frame");
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
self.streams.get_mut(&id).unwrap().headers_done = true;
|
|
828
|
+
let settings = match h3::parse_settings(&payload) {
|
|
829
|
+
Ok(settings) => settings,
|
|
830
|
+
Err(message) => {
|
|
831
|
+
self.protocol_error(conn, ev, 0x109, &message);
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
};
|
|
835
|
+
if dbg_on() {
|
|
836
|
+
eprintln!("[rwt-session] peer settings={settings:?}");
|
|
837
|
+
}
|
|
692
838
|
self.peer_settings = Some(settings);
|
|
693
839
|
// Seed the peer's per-session flow-control limits from its SETTINGS.
|
|
694
840
|
self.fc_max_data_remote = settings.wt_initial_max_data;
|
|
@@ -708,6 +854,15 @@ impl WtSession {
|
|
|
708
854
|
self.mark_closed(ev, 0, b"webtransport not supported".to_vec(), false);
|
|
709
855
|
}
|
|
710
856
|
}
|
|
857
|
+
if self.is_server && self.streams.contains_key(&CONNECT_ID) {
|
|
858
|
+
self.parse_server_connect_frames(conn, CONNECT_ID, ev);
|
|
859
|
+
}
|
|
860
|
+
} else if matches!(
|
|
861
|
+
ty,
|
|
862
|
+
h3::FRAME_DATA | h3::FRAME_HEADERS | 0x2 | 0x6 | 0x8 | 0x9
|
|
863
|
+
) {
|
|
864
|
+
self.protocol_error(conn, ev, 0x105, "unexpected frame on control stream");
|
|
865
|
+
return;
|
|
711
866
|
} else if ty == h3::FRAME_GOAWAY && !self.drain_emitted && !self.closed {
|
|
712
867
|
// The peer is gracefully shutting down the HTTP/3 connection.
|
|
713
868
|
// Exactly one WebTransport session rides it, so surface GOAWAY as
|
|
@@ -730,15 +885,17 @@ impl WtSession {
|
|
|
730
885
|
);
|
|
731
886
|
match h3::encode_headers_frame(&headers) {
|
|
732
887
|
Ok(frame) => {
|
|
733
|
-
self.streams
|
|
734
|
-
|
|
888
|
+
self.streams.insert(
|
|
889
|
+
CONNECT_ID,
|
|
890
|
+
Stream::with_prefix(Role::Connect, frame, self.parser_memory.clone()),
|
|
891
|
+
);
|
|
735
892
|
}
|
|
736
893
|
Err(e) => ev.push(Ev::Error(format!("failed to encode CONNECT: {e}"))),
|
|
737
894
|
}
|
|
738
895
|
}
|
|
739
896
|
|
|
740
897
|
fn parse_connect_frames(&mut self, conn: &mut quiche::Connection, id: u64, ev: &mut Vec<Ev>) {
|
|
741
|
-
|
|
898
|
+
while !self.closed {
|
|
742
899
|
let (frame, headers_done) = {
|
|
743
900
|
let Some(st) = self.streams.get_mut(&id) else {
|
|
744
901
|
return;
|
|
@@ -762,8 +919,20 @@ impl WtSession {
|
|
|
762
919
|
}
|
|
763
920
|
if (200..300).contains(&status) {
|
|
764
921
|
if !self.ready {
|
|
765
|
-
self.ready = true;
|
|
766
922
|
let protocol = h3::wt_protocol_of(&headers);
|
|
923
|
+
if protocol
|
|
924
|
+
.as_ref()
|
|
925
|
+
.is_some_and(|p| !self.protocols.contains(p))
|
|
926
|
+
{
|
|
927
|
+
self.protocol_error(
|
|
928
|
+
conn,
|
|
929
|
+
ev,
|
|
930
|
+
0x0817b3dd,
|
|
931
|
+
"server selected an unoffered or malformed subprotocol",
|
|
932
|
+
);
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
self.ready = true;
|
|
767
936
|
let resp_headers = h3::response_headers(&headers);
|
|
768
937
|
ev.push(Ev::Ready {
|
|
769
938
|
protocol,
|
|
@@ -792,8 +961,14 @@ impl WtSession {
|
|
|
792
961
|
fn scan_capsules(&mut self, payload: &[u8], ev: &mut Vec<Ev>) {
|
|
793
962
|
// A capsule may span multiple HTTP/3 DATA frames (RFC 9297), so append
|
|
794
963
|
// this frame's payload and consume only whole capsules from the buffer.
|
|
795
|
-
self.
|
|
796
|
-
|
|
964
|
+
if self.closed {
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
if self.capsule_buf.extend_from_slice(payload).is_err() {
|
|
968
|
+
self.parser_limit_exceeded(ev);
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
while !self.closed {
|
|
797
972
|
let (ty, start, len) = {
|
|
798
973
|
let buf = &self.capsule_buf;
|
|
799
974
|
let Some((ty, n1)) = h3::read_varint(buf) else {
|
|
@@ -802,18 +977,26 @@ impl WtSession {
|
|
|
802
977
|
let Some((len, n2)) = h3::read_varint(&buf[n1..]) else {
|
|
803
978
|
break;
|
|
804
979
|
};
|
|
805
|
-
(ty, n1 + n2, len
|
|
980
|
+
(ty, n1 + n2, len)
|
|
806
981
|
};
|
|
807
982
|
// Guard against a hostile/oversized declared length.
|
|
808
|
-
if len > MAX_CONTROL_FRAME {
|
|
809
|
-
|
|
983
|
+
if len > MAX_CONTROL_FRAME as u64 {
|
|
984
|
+
ev.push(Ev::Error("oversized WebTransport capsule".to_string()));
|
|
985
|
+
self.mark_closed(ev, 0, Vec::new(), true);
|
|
810
986
|
break;
|
|
811
987
|
}
|
|
988
|
+
let len = len as usize;
|
|
812
989
|
if self.capsule_buf.len() < start + len {
|
|
813
990
|
break; // capsule not fully arrived; wait for the next DATA frame
|
|
814
991
|
}
|
|
815
|
-
let value
|
|
816
|
-
|
|
992
|
+
let value = match self.capsule_buf.copy_range(start..start + len) {
|
|
993
|
+
Ok(value) => value,
|
|
994
|
+
Err(()) => {
|
|
995
|
+
self.parser_limit_exceeded(ev);
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
};
|
|
999
|
+
self.capsule_buf.consume(start + len);
|
|
817
1000
|
|
|
818
1001
|
if ty == h3::WT_CLOSE_SESSION_CAPSULE && !self.closed {
|
|
819
1002
|
let (code, reason) = if value.len() >= 4 {
|
|
@@ -828,17 +1011,37 @@ impl WtSession {
|
|
|
828
1011
|
} else if ty == h3::WT_DRAIN_SESSION_CAPSULE && !self.drain_emitted && !self.closed {
|
|
829
1012
|
self.drain_emitted = true;
|
|
830
1013
|
ev.push(Ev::Draining);
|
|
831
|
-
} else if
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
1014
|
+
} else if matches!(
|
|
1015
|
+
ty,
|
|
1016
|
+
h3::WT_MAX_DATA_CAPSULE
|
|
1017
|
+
| h3::WT_MAX_STREAMS_BIDI_CAPSULE
|
|
1018
|
+
| h3::WT_MAX_STREAMS_UNI_CAPSULE
|
|
1019
|
+
| h3::WT_DATA_BLOCKED_CAPSULE
|
|
1020
|
+
| h3::WT_STREAMS_BLOCKED_BIDI_CAPSULE
|
|
1021
|
+
| h3::WT_STREAMS_BLOCKED_UNI_CAPSULE
|
|
1022
|
+
) {
|
|
1023
|
+
let Some((v, n)) = h3::read_varint(&value) else {
|
|
1024
|
+
self.flow_error(ev, "truncated flow-control capsule");
|
|
1025
|
+
return;
|
|
1026
|
+
};
|
|
1027
|
+
let stream_limit =
|
|
1028
|
+
ty != h3::WT_MAX_DATA_CAPSULE && ty != h3::WT_DATA_BLOCKED_CAPSULE;
|
|
1029
|
+
if n != value.len() || (stream_limit && v > (1 << 60)) {
|
|
1030
|
+
self.flow_error(ev, "invalid flow-control capsule");
|
|
1031
|
+
return;
|
|
838
1032
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
1033
|
+
let limit = match ty {
|
|
1034
|
+
h3::WT_MAX_DATA_CAPSULE => Some(&mut self.fc_max_data_remote),
|
|
1035
|
+
h3::WT_MAX_STREAMS_BIDI_CAPSULE => Some(&mut self.fc_max_streams_bidi_remote),
|
|
1036
|
+
h3::WT_MAX_STREAMS_UNI_CAPSULE => Some(&mut self.fc_max_streams_uni_remote),
|
|
1037
|
+
_ => None,
|
|
1038
|
+
};
|
|
1039
|
+
if let Some(limit) = limit {
|
|
1040
|
+
if v <= *limit {
|
|
1041
|
+
self.flow_error(ev, "flow-control credit must increase");
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
*limit = v;
|
|
842
1045
|
}
|
|
843
1046
|
}
|
|
844
1047
|
}
|
|
@@ -852,7 +1055,20 @@ impl WtSession {
|
|
|
852
1055
|
id: u64,
|
|
853
1056
|
ev: &mut Vec<Ev>,
|
|
854
1057
|
) {
|
|
855
|
-
|
|
1058
|
+
// Streams can arrive before SETTINGS because QUIC orders each stream
|
|
1059
|
+
// independently. Keep CONNECT buffered until negotiation is validated.
|
|
1060
|
+
if self.peer_settings.is_none() {
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
let settings = self.peer_settings.unwrap();
|
|
1064
|
+
// ENABLE_CONNECT_PROTOCOL is advertised by the server; browsers need
|
|
1065
|
+
// not advertise it when acting as clients.
|
|
1066
|
+
if !settings.h3_datagram || !(settings.enable_webtransport || settings.wt_max_sessions > 0)
|
|
1067
|
+
{
|
|
1068
|
+
self.reject_connect(conn, id, 400, ev);
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
while !self.closed {
|
|
856
1072
|
let (frame, headers_done) = {
|
|
857
1073
|
let Some(st) = self.streams.get_mut(&id) else {
|
|
858
1074
|
return;
|
|
@@ -891,7 +1107,7 @@ impl WtSession {
|
|
|
891
1107
|
let mut authority = String::new();
|
|
892
1108
|
let mut path = String::from("/");
|
|
893
1109
|
let mut origin: Option<String> = None;
|
|
894
|
-
let mut
|
|
1110
|
+
let mut protocol_fields: Vec<String> = Vec::new();
|
|
895
1111
|
let mut extra: Vec<(String, String)> = Vec::new();
|
|
896
1112
|
for h in headers {
|
|
897
1113
|
let name = h.name();
|
|
@@ -903,7 +1119,7 @@ impl WtSession {
|
|
|
903
1119
|
b":path" => path = String::from_utf8_lossy(value).into_owned(),
|
|
904
1120
|
b"origin" => origin = Some(String::from_utf8_lossy(value).into_owned()),
|
|
905
1121
|
_ if name == h3::WT_AVAILABLE_PROTOCOLS => {
|
|
906
|
-
|
|
1122
|
+
protocol_fields.push(String::from_utf8_lossy(value).into_owned());
|
|
907
1123
|
}
|
|
908
1124
|
_ if name.starts_with(b":") => {} // other pseudo-headers
|
|
909
1125
|
_ => extra.push((
|
|
@@ -913,6 +1129,7 @@ impl WtSession {
|
|
|
913
1129
|
}
|
|
914
1130
|
}
|
|
915
1131
|
|
|
1132
|
+
let requested_protocols = h3::parse_sf_string_list(&protocol_fields.join(", "));
|
|
916
1133
|
let ok = method == Some(b"CONNECT".as_ref()) && protocol == Some(b"webtransport".as_ref());
|
|
917
1134
|
if !ok {
|
|
918
1135
|
self.reject_connect(conn, id, 400, ev);
|
|
@@ -1020,8 +1237,11 @@ impl WtSession {
|
|
|
1020
1237
|
let Some(st) = self.streams.get_mut(&id) else {
|
|
1021
1238
|
return;
|
|
1022
1239
|
};
|
|
1023
|
-
st.class_buf.extend_from_slice(&chunk)
|
|
1024
|
-
|
|
1240
|
+
if st.class_buf.extend_from_slice(&chunk).is_err() {
|
|
1241
|
+
self.parser_limit_exceeded(ev);
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
st.class_buf.to_vec()
|
|
1025
1245
|
};
|
|
1026
1246
|
let Some((ty, n)) = h3::read_varint(&buf) else {
|
|
1027
1247
|
// Peer FIN'd before we could classify: no more bytes will ever
|
|
@@ -1033,21 +1253,40 @@ impl WtSession {
|
|
|
1033
1253
|
};
|
|
1034
1254
|
match ty {
|
|
1035
1255
|
t if t == h3::H3_CONTROL_STREAM_TYPE => {
|
|
1256
|
+
if self
|
|
1257
|
+
.streams
|
|
1258
|
+
.iter()
|
|
1259
|
+
.any(|(&other, st)| other != id && st.role == Role::PeerControl)
|
|
1260
|
+
{
|
|
1261
|
+
self.protocol_error(conn, ev, 0x103, "duplicate HTTP/3 control stream");
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1036
1264
|
let rest = buf[n..].to_vec();
|
|
1037
1265
|
let Some(st) = self.streams.get_mut(&id) else {
|
|
1038
1266
|
return;
|
|
1039
1267
|
};
|
|
1040
1268
|
st.role = Role::PeerControl;
|
|
1041
1269
|
st.class_buf.clear();
|
|
1042
|
-
|
|
1043
|
-
|
|
1270
|
+
// Use the common control path so a budget failure in the very
|
|
1271
|
+
// first chunk is fatal even if the peer sends nothing else.
|
|
1272
|
+
self.process_recv(conn, id, rest, fin, ev);
|
|
1044
1273
|
}
|
|
1045
1274
|
t if t == h3::QPACK_ENCODER_STREAM_TYPE || t == h3::QPACK_DECODER_STREAM_TYPE => {
|
|
1046
|
-
let
|
|
1275
|
+
let encoder = t == h3::QPACK_ENCODER_STREAM_TYPE;
|
|
1276
|
+
if self
|
|
1277
|
+
.streams
|
|
1278
|
+
.iter()
|
|
1279
|
+
.any(|(&other, st)| other != id && st.role == (Role::PeerQpack { encoder }))
|
|
1280
|
+
{
|
|
1281
|
+
self.protocol_error(conn, ev, 0x103, "duplicate QPACK stream");
|
|
1047
1282
|
return;
|
|
1048
|
-
}
|
|
1049
|
-
st
|
|
1283
|
+
}
|
|
1284
|
+
let st = self.streams.get_mut(&id).unwrap();
|
|
1285
|
+
st.role = Role::PeerQpack { encoder };
|
|
1050
1286
|
st.class_buf.clear();
|
|
1287
|
+
if fin {
|
|
1288
|
+
self.protocol_error(conn, ev, 0x104, "peer closed a critical QPACK stream");
|
|
1289
|
+
}
|
|
1051
1290
|
}
|
|
1052
1291
|
t if t == h3::WT_UNI_STREAM_TYPE => {
|
|
1053
1292
|
let Some((session, n2)) = h3::read_varint(&buf[n..]) else {
|
|
@@ -1074,6 +1313,12 @@ impl WtSession {
|
|
|
1074
1313
|
st.role = Role::WtData { bidi: false };
|
|
1075
1314
|
st.class_buf.clear();
|
|
1076
1315
|
}
|
|
1316
|
+
self.streams.get_mut(&id).unwrap().recv_prefix = (n + n2) as u64;
|
|
1317
|
+
if !self.account_incoming(false, ev)
|
|
1318
|
+
|| !self.account_receive(id, data.len() as u64, ev)
|
|
1319
|
+
{
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1077
1322
|
ev.push(Ev::IncomingStream { id, bidi: false });
|
|
1078
1323
|
if !data.is_empty() {
|
|
1079
1324
|
ev.push(Ev::StreamData { id, data });
|
|
@@ -1107,8 +1352,11 @@ impl WtSession {
|
|
|
1107
1352
|
let Some(st) = self.streams.get_mut(&id) else {
|
|
1108
1353
|
return;
|
|
1109
1354
|
};
|
|
1110
|
-
st.class_buf.extend_from_slice(&chunk)
|
|
1111
|
-
|
|
1355
|
+
if st.class_buf.extend_from_slice(&chunk).is_err() {
|
|
1356
|
+
self.parser_limit_exceeded(ev);
|
|
1357
|
+
return;
|
|
1358
|
+
}
|
|
1359
|
+
st.class_buf.to_vec()
|
|
1112
1360
|
};
|
|
1113
1361
|
let Some((signal, n)) = h3::read_varint(&buf) else {
|
|
1114
1362
|
// Peer FIN'd before we could classify: reclaim the entry.
|
|
@@ -1150,6 +1398,10 @@ impl WtSession {
|
|
|
1150
1398
|
st.role = Role::WtData { bidi: true };
|
|
1151
1399
|
st.class_buf.clear();
|
|
1152
1400
|
}
|
|
1401
|
+
self.streams.get_mut(&id).unwrap().recv_prefix = (n + n2) as u64;
|
|
1402
|
+
if !self.account_incoming(true, ev) || !self.account_receive(id, data.len() as u64, ev) {
|
|
1403
|
+
return;
|
|
1404
|
+
}
|
|
1153
1405
|
ev.push(Ev::IncomingStream { id, bidi: true });
|
|
1154
1406
|
if !data.is_empty() {
|
|
1155
1407
|
ev.push(Ev::StreamData { id, data });
|
|
@@ -1201,6 +1453,47 @@ impl WtSession {
|
|
|
1201
1453
|
}
|
|
1202
1454
|
|
|
1203
1455
|
pub fn open_stream(&mut self, bidi: bool, request_id: u64, ev: &mut Vec<Ev>) {
|
|
1456
|
+
self.pending_opens.push_back((bidi, request_id));
|
|
1457
|
+
self.release_pending_opens(ev);
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
fn uses_session_flow_control(&self) -> bool {
|
|
1461
|
+
self.peer_settings
|
|
1462
|
+
.is_some_and(|settings| settings.wt_max_sessions > 0)
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
fn release_pending_opens(&mut self, ev: &mut Vec<Ev>) {
|
|
1466
|
+
if !self.ready || self.closed {
|
|
1467
|
+
return;
|
|
1468
|
+
}
|
|
1469
|
+
let mut pending = std::mem::take(&mut self.pending_opens);
|
|
1470
|
+
while let Some((bidi, request_id)) = pending.pop_front() {
|
|
1471
|
+
let first = if self.is_server {
|
|
1472
|
+
if bidi {
|
|
1473
|
+
SERVER_FIRST_WT_BIDI_ID
|
|
1474
|
+
} else {
|
|
1475
|
+
SERVER_FIRST_WT_UNI_ID
|
|
1476
|
+
}
|
|
1477
|
+
} else if bidi {
|
|
1478
|
+
CLIENT_FIRST_WT_BIDI_ID
|
|
1479
|
+
} else {
|
|
1480
|
+
CLIENT_FIRST_WT_UNI_ID
|
|
1481
|
+
};
|
|
1482
|
+
let next = if bidi { self.next_bidi } else { self.next_uni };
|
|
1483
|
+
let limit = if bidi {
|
|
1484
|
+
self.fc_max_streams_bidi_remote
|
|
1485
|
+
} else {
|
|
1486
|
+
self.fc_max_streams_uni_remote
|
|
1487
|
+
};
|
|
1488
|
+
if self.uses_session_flow_control() && (next - first) / 4 >= limit {
|
|
1489
|
+
self.pending_opens.push_back((bidi, request_id));
|
|
1490
|
+
continue;
|
|
1491
|
+
}
|
|
1492
|
+
self.allocate_stream(bidi, request_id, ev);
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
fn allocate_stream(&mut self, bidi: bool, request_id: u64, ev: &mut Vec<Ev>) {
|
|
1204
1497
|
let id = if bidi {
|
|
1205
1498
|
let id = self.next_bidi;
|
|
1206
1499
|
self.next_bidi += 4;
|
|
@@ -1218,8 +1511,10 @@ impl WtSession {
|
|
|
1218
1511
|
h3::put_varint(h3::WT_UNI_STREAM_TYPE, &mut prefix);
|
|
1219
1512
|
}
|
|
1220
1513
|
h3::put_varint(CONNECT_ID, &mut prefix);
|
|
1221
|
-
self.streams
|
|
1222
|
-
|
|
1514
|
+
self.streams.insert(
|
|
1515
|
+
id,
|
|
1516
|
+
Stream::with_prefix(Role::WtData { bidi }, prefix, self.parser_memory.clone()),
|
|
1517
|
+
);
|
|
1223
1518
|
ev.push(Ev::StreamOpened { request_id, id });
|
|
1224
1519
|
}
|
|
1225
1520
|
|
|
@@ -1246,6 +1541,7 @@ impl WtSession {
|
|
|
1246
1541
|
// mapped into the HTTP/3 error space.
|
|
1247
1542
|
let h = h3::webtransport_code_to_http_code(code);
|
|
1248
1543
|
let _ = conn.stream_shutdown(id, quiche::Shutdown::Write, h);
|
|
1544
|
+
self.reconcile_send_final_size(conn, id);
|
|
1249
1545
|
if let Some(st) = self.streams.get_mut(&id) {
|
|
1250
1546
|
st.out.clear();
|
|
1251
1547
|
st.fin_sent = true;
|
|
@@ -1325,6 +1621,9 @@ impl WtSession {
|
|
|
1325
1621
|
/// Returns an optional `(code, reason)` if the QUIC connection should now be
|
|
1326
1622
|
/// closed.
|
|
1327
1623
|
pub fn flush(&mut self, conn: &mut quiche::Connection, ev: &mut Vec<Ev>) {
|
|
1624
|
+
if let Some((code, message)) = self.terminal_error.take() {
|
|
1625
|
+
let _ = conn.close(true, code, &message);
|
|
1626
|
+
}
|
|
1328
1627
|
// Once established, grant the peer ample per-session flow-control credit.
|
|
1329
1628
|
// Exactly one WebTransport session rides this QUIC connection, so QUIC's
|
|
1330
1629
|
// connection-level flow control is the real limit; granting a large WT
|
|
@@ -1332,10 +1631,49 @@ impl WtSession {
|
|
|
1332
1631
|
// limits from stalling, while QUIC does the actual enforcement.
|
|
1333
1632
|
if self.ready && !self.fc_credit_granted && !self.closed {
|
|
1334
1633
|
self.fc_credit_granted = true;
|
|
1634
|
+
self.fc_max_data_local = WT_GRANT_MAX_DATA;
|
|
1635
|
+
self.fc_max_streams_local = [WT_GRANT_MAX_STREAMS; 2];
|
|
1335
1636
|
self.queue_session_capsule(h3::WT_MAX_DATA_CAPSULE, WT_GRANT_MAX_DATA);
|
|
1336
1637
|
self.queue_session_capsule(h3::WT_MAX_STREAMS_BIDI_CAPSULE, WT_GRANT_MAX_STREAMS);
|
|
1337
1638
|
self.queue_session_capsule(h3::WT_MAX_STREAMS_UNI_CAPSULE, WT_GRANT_MAX_STREAMS);
|
|
1338
1639
|
}
|
|
1640
|
+
// Replenish cumulative credit for long-lived sessions before the peer
|
|
1641
|
+
// reaches the advertised limit. QUIC independently bounds buffering.
|
|
1642
|
+
if self.ready && self.fc_credit_granted && self.uses_session_flow_control() && !self.closed
|
|
1643
|
+
{
|
|
1644
|
+
if self.fc_max_data_local.saturating_sub(self.fc_data_received) <= WT_GRANT_MAX_DATA / 2
|
|
1645
|
+
{
|
|
1646
|
+
let limit = self
|
|
1647
|
+
.fc_data_received
|
|
1648
|
+
.saturating_add(WT_GRANT_MAX_DATA)
|
|
1649
|
+
.min((1 << 62) - 1);
|
|
1650
|
+
if limit > self.fc_max_data_local {
|
|
1651
|
+
self.fc_max_data_local = limit;
|
|
1652
|
+
self.queue_session_capsule(h3::WT_MAX_DATA_CAPSULE, limit);
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
for direction in 0..2 {
|
|
1656
|
+
if self.fc_max_streams_local[direction]
|
|
1657
|
+
.saturating_sub(self.fc_streams_received[direction])
|
|
1658
|
+
<= WT_GRANT_MAX_STREAMS / 2
|
|
1659
|
+
{
|
|
1660
|
+
let limit = self.fc_streams_received[direction]
|
|
1661
|
+
.saturating_add(WT_GRANT_MAX_STREAMS)
|
|
1662
|
+
.min(1 << 60);
|
|
1663
|
+
if limit > self.fc_max_streams_local[direction] {
|
|
1664
|
+
self.fc_max_streams_local[direction] = limit;
|
|
1665
|
+
self.queue_session_capsule(
|
|
1666
|
+
if direction == 0 {
|
|
1667
|
+
h3::WT_MAX_STREAMS_UNI_CAPSULE
|
|
1668
|
+
} else {
|
|
1669
|
+
h3::WT_MAX_STREAMS_BIDI_CAPSULE
|
|
1670
|
+
},
|
|
1671
|
+
limit,
|
|
1672
|
+
);
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1339
1677
|
// Queue the CLOSE_WEBTRANSPORT_SESSION capsule + FIN once, if requested.
|
|
1340
1678
|
if let Some(req) = self.close_req.as_mut() {
|
|
1341
1679
|
if !req.capsule_queued {
|
|
@@ -1362,6 +1700,7 @@ impl WtSession {
|
|
|
1362
1700
|
}
|
|
1363
1701
|
}
|
|
1364
1702
|
|
|
1703
|
+
self.release_pending_opens(ev);
|
|
1365
1704
|
let ids: Vec<u64> = self.streams.keys().copied().collect();
|
|
1366
1705
|
let mut wt_sent = 0u64;
|
|
1367
1706
|
for id in ids {
|
|
@@ -1442,18 +1781,16 @@ impl WtSession {
|
|
|
1442
1781
|
|
|
1443
1782
|
/// Emit WT_DATA_BLOCKED / WT_STREAMS_BLOCKED_* capsules when the peer's
|
|
1444
1783
|
/// advertised WebTransport limits are holding back a send, once per distinct
|
|
1445
|
-
/// limit value
|
|
1446
|
-
/// one-session-per-connection model; this is the protocol-conformant signal
|
|
1447
|
-
/// asking the peer to raise the limit.
|
|
1784
|
+
/// limit value, asking the peer to raise the limit.
|
|
1448
1785
|
fn signal_flow_control_blocked(&mut self) {
|
|
1449
1786
|
// Limits are only meaningful once the peer's SETTINGS have seeded them.
|
|
1450
|
-
if self.
|
|
1787
|
+
if !self.uses_session_flow_control() || self.closed {
|
|
1451
1788
|
return;
|
|
1452
1789
|
}
|
|
1453
1790
|
|
|
1454
1791
|
// Data-blocked: unsent WT stream payload while at the peer's WT_MAX_DATA.
|
|
1455
1792
|
if self.fc_data_sent >= self.fc_max_data_remote
|
|
1456
|
-
&& self.data_blocked_at != self.fc_max_data_remote
|
|
1793
|
+
&& self.data_blocked_at != Some(self.fc_max_data_remote)
|
|
1457
1794
|
{
|
|
1458
1795
|
let has_pending = self.streams.values().any(|s| {
|
|
1459
1796
|
matches!(s.role, Role::WtData { .. })
|
|
@@ -1462,11 +1799,11 @@ impl WtSession {
|
|
|
1462
1799
|
if has_pending {
|
|
1463
1800
|
let limit = self.fc_max_data_remote;
|
|
1464
1801
|
self.queue_session_capsule(h3::WT_DATA_BLOCKED_CAPSULE, limit);
|
|
1465
|
-
self.data_blocked_at = limit;
|
|
1802
|
+
self.data_blocked_at = Some(limit);
|
|
1466
1803
|
}
|
|
1467
1804
|
}
|
|
1468
1805
|
|
|
1469
|
-
// Streams-blocked:
|
|
1806
|
+
// Streams-blocked: a requested open is waiting at WT_MAX_STREAMS_*.
|
|
1470
1807
|
let (first_bidi, first_uni) = if self.is_server {
|
|
1471
1808
|
(SERVER_FIRST_WT_BIDI_ID, SERVER_FIRST_WT_UNI_ID)
|
|
1472
1809
|
} else {
|
|
@@ -1475,19 +1812,21 @@ impl WtSession {
|
|
|
1475
1812
|
let opened_bidi = (self.next_bidi - first_bidi) / 4;
|
|
1476
1813
|
let opened_uni = (self.next_uni - first_uni) / 4;
|
|
1477
1814
|
|
|
1478
|
-
if opened_bidi
|
|
1479
|
-
&& self.
|
|
1815
|
+
if opened_bidi >= self.fc_max_streams_bidi_remote
|
|
1816
|
+
&& self.pending_opens.iter().any(|(bidi, _)| *bidi)
|
|
1817
|
+
&& self.streams_bidi_blocked_at != Some(self.fc_max_streams_bidi_remote)
|
|
1480
1818
|
{
|
|
1481
1819
|
let limit = self.fc_max_streams_bidi_remote;
|
|
1482
1820
|
self.queue_session_capsule(h3::WT_STREAMS_BLOCKED_BIDI_CAPSULE, limit);
|
|
1483
|
-
self.streams_bidi_blocked_at = limit;
|
|
1821
|
+
self.streams_bidi_blocked_at = Some(limit);
|
|
1484
1822
|
}
|
|
1485
|
-
if opened_uni
|
|
1486
|
-
&& self.
|
|
1823
|
+
if opened_uni >= self.fc_max_streams_uni_remote
|
|
1824
|
+
&& self.pending_opens.iter().any(|(bidi, _)| !*bidi)
|
|
1825
|
+
&& self.streams_uni_blocked_at != Some(self.fc_max_streams_uni_remote)
|
|
1487
1826
|
{
|
|
1488
1827
|
let limit = self.fc_max_streams_uni_remote;
|
|
1489
1828
|
self.queue_session_capsule(h3::WT_STREAMS_BLOCKED_UNI_CAPSULE, limit);
|
|
1490
|
-
self.streams_uni_blocked_at = limit;
|
|
1829
|
+
self.streams_uni_blocked_at = Some(limit);
|
|
1491
1830
|
}
|
|
1492
1831
|
}
|
|
1493
1832
|
|
|
@@ -1498,6 +1837,8 @@ impl WtSession {
|
|
|
1498
1837
|
ev: &mut Vec<Ev>,
|
|
1499
1838
|
wt_sent: &mut u64,
|
|
1500
1839
|
) {
|
|
1840
|
+
self.reconcile_send_final_size(conn, id);
|
|
1841
|
+
let enforce = self.uses_session_flow_control();
|
|
1501
1842
|
let Some(st) = self.streams.get_mut(&id) else {
|
|
1502
1843
|
return;
|
|
1503
1844
|
};
|
|
@@ -1512,10 +1853,22 @@ impl WtSession {
|
|
|
1512
1853
|
while let Some(front) = st.out.front_mut() {
|
|
1513
1854
|
let remaining_len = front.data.len() - front.off;
|
|
1514
1855
|
let is_payload = is_wt_data && front.request_id.is_some();
|
|
1515
|
-
|
|
1856
|
+
let allowance = self
|
|
1857
|
+
.fc_max_data_remote
|
|
1858
|
+
.saturating_sub(self.fc_data_sent.saturating_add(*wt_sent));
|
|
1859
|
+
let amount = if enforce && is_payload {
|
|
1860
|
+
remaining_len.min(allowance as usize)
|
|
1861
|
+
} else {
|
|
1862
|
+
remaining_len
|
|
1863
|
+
};
|
|
1864
|
+
if amount == 0 && remaining_len != 0 {
|
|
1865
|
+
return;
|
|
1866
|
+
}
|
|
1867
|
+
match conn.stream_send(id, &front.data[front.off..front.off + amount], false) {
|
|
1516
1868
|
Ok(sent) => {
|
|
1517
1869
|
if is_payload {
|
|
1518
1870
|
*wt_sent += sent as u64;
|
|
1871
|
+
st.send_payload += sent as u64;
|
|
1519
1872
|
}
|
|
1520
1873
|
front.off += sent;
|
|
1521
1874
|
if front.off >= front.data.len() {
|
|
@@ -1561,6 +1914,14 @@ impl WtSession {
|
|
|
1561
1914
|
return;
|
|
1562
1915
|
}
|
|
1563
1916
|
self.closed = true;
|
|
1917
|
+
// Closed connections can linger during QUIC draining. Release receive
|
|
1918
|
+
// allocations immediately rather than tying the budget to that timer.
|
|
1919
|
+
self.capsule_buf.clear();
|
|
1920
|
+
for stream in self.streams.values_mut() {
|
|
1921
|
+
stream.frames.buf.clear();
|
|
1922
|
+
stream.class_buf.clear();
|
|
1923
|
+
stream.recv_dead = true;
|
|
1924
|
+
}
|
|
1564
1925
|
ev.push(Ev::Closed {
|
|
1565
1926
|
code,
|
|
1566
1927
|
reason,
|
|
@@ -1572,6 +1933,13 @@ impl WtSession {
|
|
|
1572
1933
|
self.closed
|
|
1573
1934
|
}
|
|
1574
1935
|
|
|
1936
|
+
fn parser_limit_exceeded(&mut self, ev: &mut Vec<Ev>) {
|
|
1937
|
+
if !self.closed {
|
|
1938
|
+
ev.push(Ev::Error("parser memory limit exceeded".to_string()));
|
|
1939
|
+
self.mark_closed(ev, 0, Vec::new(), true);
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1575
1943
|
/// Whether a stream's HTTP/3 frame parser has latched a fatal error.
|
|
1576
1944
|
fn frame_error(&self, id: u64) -> bool {
|
|
1577
1945
|
self.streams
|
|
@@ -1580,14 +1948,82 @@ impl WtSession {
|
|
|
1580
1948
|
.unwrap_or(false)
|
|
1581
1949
|
}
|
|
1582
1950
|
|
|
1583
|
-
///
|
|
1584
|
-
|
|
1585
|
-
|
|
1951
|
+
/// Return credit for buffered payload omitted from a reset's final size.
|
|
1952
|
+
fn reconcile_send_final_size(&mut self, conn: &quiche::Connection, id: u64) {
|
|
1953
|
+
let Some(final_size) = conn.stream_send_final_size(id) else {
|
|
1954
|
+
return;
|
|
1955
|
+
};
|
|
1956
|
+
let Some(st) = self.streams.get_mut(&id) else {
|
|
1957
|
+
return;
|
|
1958
|
+
};
|
|
1959
|
+
if !matches!(st.role, Role::WtData { .. }) {
|
|
1960
|
+
return;
|
|
1961
|
+
}
|
|
1962
|
+
let final_payload = final_size.saturating_sub(st.send_prefix);
|
|
1963
|
+
// RESET_STREAM discards bytes accepted by quiche but never serialized.
|
|
1964
|
+
// Those bytes are absent from the final size and consume no WT credit.
|
|
1965
|
+
if final_payload < st.send_payload {
|
|
1966
|
+
self.fc_data_sent = self
|
|
1967
|
+
.fc_data_sent
|
|
1968
|
+
.saturating_sub(st.send_payload - final_payload);
|
|
1969
|
+
st.send_payload = final_payload;
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
fn flow_error(&mut self, ev: &mut Vec<Ev>, message: &str) {
|
|
1974
|
+
if self.closed {
|
|
1975
|
+
return;
|
|
1976
|
+
}
|
|
1977
|
+
self.terminal_error = Some((0x045d4487, message.as_bytes().to_vec()));
|
|
1978
|
+
ev.push(Ev::Error(message.to_string()));
|
|
1979
|
+
self.mark_closed(ev, 0, Vec::new(), true);
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1982
|
+
fn account_incoming(&mut self, bidi: bool, ev: &mut Vec<Ev>) -> bool {
|
|
1983
|
+
let direction = usize::from(bidi);
|
|
1984
|
+
self.fc_streams_received[direction] += 1;
|
|
1985
|
+
let limit = self.fc_max_streams_local[direction];
|
|
1986
|
+
if self.uses_session_flow_control() && self.fc_streams_received[direction] > limit {
|
|
1987
|
+
self.flow_error(ev, "peer exceeded the session stream limit");
|
|
1988
|
+
return false;
|
|
1989
|
+
}
|
|
1990
|
+
true
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
fn account_receive(&mut self, id: u64, amount: u64, ev: &mut Vec<Ev>) -> bool {
|
|
1994
|
+
self.fc_data_received = self.fc_data_received.saturating_add(amount);
|
|
1995
|
+
if let Some(st) = self.streams.get_mut(&id) {
|
|
1996
|
+
st.recv_payload = st.recv_payload.saturating_add(amount);
|
|
1997
|
+
}
|
|
1998
|
+
let limit = self.fc_max_data_local;
|
|
1999
|
+
if self.uses_session_flow_control() && self.fc_data_received > limit {
|
|
2000
|
+
self.flow_error(ev, "peer exceeded the session data limit");
|
|
2001
|
+
return false;
|
|
2002
|
+
}
|
|
2003
|
+
true
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
fn protocol_error(
|
|
2007
|
+
&mut self,
|
|
2008
|
+
conn: &mut quiche::Connection,
|
|
2009
|
+
ev: &mut Vec<Ev>,
|
|
2010
|
+
code: u64,
|
|
2011
|
+
message: &str,
|
|
2012
|
+
) {
|
|
2013
|
+
if self.closed {
|
|
2014
|
+
return;
|
|
2015
|
+
}
|
|
2016
|
+
ev.push(Ev::Error(message.to_string()));
|
|
2017
|
+
self.mark_closed(ev, 0, Vec::new(), true);
|
|
2018
|
+
let _ = conn.close(true, code, message.as_bytes());
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
/// Stop reading and release parser buffers after a fatal stream error.
|
|
1586
2022
|
fn kill_recv(&mut self, conn: &mut quiche::Connection, id: u64) {
|
|
1587
2023
|
if let Some(st) = self.streams.get_mut(&id) {
|
|
1588
2024
|
st.recv_dead = true;
|
|
1589
|
-
st.frames.buf
|
|
1590
|
-
st.class_buf
|
|
2025
|
+
st.frames.buf.clear();
|
|
2026
|
+
st.class_buf.clear();
|
|
1591
2027
|
}
|
|
1592
2028
|
let _ = conn.stream_shutdown(id, quiche::Shutdown::Read, 0);
|
|
1593
2029
|
}
|
|
@@ -1599,6 +2035,7 @@ fn role_name(role: Role) -> &'static str {
|
|
|
1599
2035
|
Role::Connect => "Connect",
|
|
1600
2036
|
Role::PeerControl => "PeerControl",
|
|
1601
2037
|
Role::Ignored => "Ignored",
|
|
2038
|
+
Role::PeerQpack { .. } => "PeerQpack",
|
|
1602
2039
|
Role::WtData { .. } => "WtData",
|
|
1603
2040
|
Role::PendingUni => "PendingUni",
|
|
1604
2041
|
Role::PendingBidi => "PendingBidi",
|
|
@@ -1637,12 +2074,520 @@ fn classify_new(id: u64, is_server: bool) -> Role {
|
|
|
1637
2074
|
mod tests {
|
|
1638
2075
|
use super::*;
|
|
1639
2076
|
|
|
2077
|
+
fn established_server(
|
|
2078
|
+
settings: Vec<u8>,
|
|
2079
|
+
) -> (quiche::Connection, quiche::Connection, WtSession) {
|
|
2080
|
+
let (mut client, mut server) =
|
|
2081
|
+
crate::test_support::pair(None, Some("wrong.example"), false);
|
|
2082
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2083
|
+
assert!(server.is_established());
|
|
2084
|
+
let mut session = server_with_connect(ParserMemory::new(64 * 1024 * 1024));
|
|
2085
|
+
session.ready = false;
|
|
2086
|
+
session.peer_settings = None;
|
|
2087
|
+
session.on_established();
|
|
2088
|
+
let mut control = vec![0];
|
|
2089
|
+
control.extend_from_slice(&h3::frame(h3::FRAME_SETTINGS, &settings));
|
|
2090
|
+
client.stream_send(2, &control, false).unwrap();
|
|
2091
|
+
let headers = h3::connect_headers("wrong.example", "/", None, &[], &[]);
|
|
2092
|
+
client
|
|
2093
|
+
.stream_send(0, &h3::encode_headers_frame(&headers).unwrap(), false)
|
|
2094
|
+
.unwrap();
|
|
2095
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2096
|
+
let mut events = Vec::new();
|
|
2097
|
+
// Exercise out-of-order arrival: CONNECT must wait for SETTINGS.
|
|
2098
|
+
session.on_readable(&mut server, 0, false, &mut events);
|
|
2099
|
+
assert!(!session.ready);
|
|
2100
|
+
session.on_readable(&mut server, 2, false, &mut events);
|
|
2101
|
+
assert!(session.ready);
|
|
2102
|
+
(client, server, session)
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
fn capsule(ty: u64, value: u64) -> Vec<u8> {
|
|
2106
|
+
let mut body = Vec::new();
|
|
2107
|
+
h3::put_varint(value, &mut body);
|
|
2108
|
+
let mut out = Vec::new();
|
|
2109
|
+
h3::put_varint(ty, &mut out);
|
|
2110
|
+
h3::put_varint(body.len() as u64, &mut out);
|
|
2111
|
+
out.extend_from_slice(&body);
|
|
2112
|
+
out
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
#[test]
|
|
2116
|
+
fn resetting_unsent_data_restores_session_send_credit() {
|
|
2117
|
+
let (_, mut conn, mut session) = established_server(h3::settings_payload());
|
|
2118
|
+
session.fc_max_data_remote = 8;
|
|
2119
|
+
session.open_stream(false, 1, &mut Vec::new());
|
|
2120
|
+
let id = SERVER_FIRST_WT_UNI_ID;
|
|
2121
|
+
session.stream_write(id, vec![1; 8], 2, &mut Vec::new());
|
|
2122
|
+
session.flush(&mut conn, &mut Vec::new());
|
|
2123
|
+
assert_eq!(session.fc_data_sent, 8);
|
|
2124
|
+
// Reset before conn.send serializes any of this stream's bytes.
|
|
2125
|
+
session.stream_reset(&mut conn, id, 0);
|
|
2126
|
+
assert_eq!(session.fc_data_sent, 0);
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
#[test]
|
|
2130
|
+
fn reset_final_size_consumes_session_credit_without_delivered_payload() {
|
|
2131
|
+
let (mut client, mut server, mut session) = established_server(h3::settings_payload());
|
|
2132
|
+
let mut prefix = Vec::new();
|
|
2133
|
+
h3::put_varint(h3::WT_UNI_STREAM_TYPE, &mut prefix);
|
|
2134
|
+
h3::put_varint(0, &mut prefix);
|
|
2135
|
+
client.stream_send(14, &prefix, false).unwrap();
|
|
2136
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2137
|
+
session.on_readable(&mut server, 14, false, &mut Vec::new());
|
|
2138
|
+
client.stream_send(14, &[5; 8192], false).unwrap();
|
|
2139
|
+
// Serialize the bytes into QUIC without delivering them to the app.
|
|
2140
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2141
|
+
client
|
|
2142
|
+
.stream_shutdown(14, quiche::Shutdown::Write, 0)
|
|
2143
|
+
.unwrap();
|
|
2144
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2145
|
+
let mut events = Vec::new();
|
|
2146
|
+
session.on_readable(&mut server, 14, false, &mut events);
|
|
2147
|
+
assert_eq!(session.fc_data_received, 8192);
|
|
2148
|
+
assert!(events
|
|
2149
|
+
.iter()
|
|
2150
|
+
.any(|e| matches!(e, Ev::StreamReset { id: 14, .. })));
|
|
2151
|
+
assert!(!events.iter().any(|e| matches!(e, Ev::StreamData { .. })));
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
#[test]
|
|
2155
|
+
fn peer_exceeding_advertised_data_or_stream_credit_is_terminal() {
|
|
2156
|
+
for data_limit in [false, true] {
|
|
2157
|
+
let (mut client, mut server, mut session) = established_server(h3::settings_payload());
|
|
2158
|
+
if data_limit {
|
|
2159
|
+
session.fc_data_received = session.fc_max_data_local;
|
|
2160
|
+
} else {
|
|
2161
|
+
session.fc_streams_received[0] = session.fc_max_streams_local[0];
|
|
2162
|
+
}
|
|
2163
|
+
let mut stream = Vec::new();
|
|
2164
|
+
h3::put_varint(h3::WT_UNI_STREAM_TYPE, &mut stream);
|
|
2165
|
+
h3::put_varint(0, &mut stream);
|
|
2166
|
+
stream.push(1);
|
|
2167
|
+
client.stream_send(14, &stream, true).unwrap();
|
|
2168
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2169
|
+
let mut events = Vec::new();
|
|
2170
|
+
session.on_readable(&mut server, 14, false, &mut events);
|
|
2171
|
+
assert!(session.closed);
|
|
2172
|
+
assert!(!events
|
|
2173
|
+
.iter()
|
|
2174
|
+
.any(|e| matches!(e, Ev::IncomingStream { .. } | Ev::StreamData { .. })));
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
|
|
2178
|
+
#[test]
|
|
2179
|
+
fn new_uni_and_bidi_streams_wait_for_global_read_backpressure_to_clear() {
|
|
2180
|
+
for bidi in [false, true] {
|
|
2181
|
+
let (mut client, mut server, mut session) = established_server(h3::settings_payload());
|
|
2182
|
+
let mut prefix = Vec::new();
|
|
2183
|
+
h3::put_varint(
|
|
2184
|
+
if bidi {
|
|
2185
|
+
h3::WT_BIDI_FRAME_TYPE
|
|
2186
|
+
} else {
|
|
2187
|
+
h3::WT_UNI_STREAM_TYPE
|
|
2188
|
+
},
|
|
2189
|
+
&mut prefix,
|
|
2190
|
+
);
|
|
2191
|
+
h3::put_varint(0, &mut prefix);
|
|
2192
|
+
prefix.extend_from_slice(&[9; 1024]);
|
|
2193
|
+
let mut events = Vec::new();
|
|
2194
|
+
for i in 0..20 {
|
|
2195
|
+
let id = if bidi { 4 + i * 4 } else { 14 + i * 4 };
|
|
2196
|
+
client.stream_send(id, &prefix, true).unwrap();
|
|
2197
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2198
|
+
session.on_readable(&mut server, id, true, &mut events);
|
|
2199
|
+
session.flush(&mut server, &mut Vec::new());
|
|
2200
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2201
|
+
}
|
|
2202
|
+
assert!(
|
|
2203
|
+
events.is_empty(),
|
|
2204
|
+
"application events leaked while backpressured"
|
|
2205
|
+
);
|
|
2206
|
+
for i in 0..20 {
|
|
2207
|
+
let id = if bidi { 4 + i * 4 } else { 14 + i * 4 };
|
|
2208
|
+
session.on_readable(&mut server, id, false, &mut events);
|
|
2209
|
+
}
|
|
2210
|
+
let bytes: usize = events
|
|
2211
|
+
.iter()
|
|
2212
|
+
.filter_map(|e| match e {
|
|
2213
|
+
Ev::StreamData { data, .. } => Some(data.len()),
|
|
2214
|
+
_ => None,
|
|
2215
|
+
})
|
|
2216
|
+
.sum();
|
|
2217
|
+
assert_eq!(bytes, 20 * 1024);
|
|
2218
|
+
assert_eq!(
|
|
2219
|
+
events
|
|
2220
|
+
.iter()
|
|
2221
|
+
.filter(|e| matches!(e, Ev::IncomingStream { .. }))
|
|
2222
|
+
.count(),
|
|
2223
|
+
20
|
|
2224
|
+
);
|
|
2225
|
+
assert_eq!(
|
|
2226
|
+
events
|
|
2227
|
+
.iter()
|
|
2228
|
+
.filter(|e| matches!(e, Ev::StreamFinished { .. }))
|
|
2229
|
+
.count(),
|
|
2230
|
+
20
|
|
2231
|
+
);
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
|
|
2235
|
+
#[test]
|
|
2236
|
+
fn zero_session_credit_blocks_opens_and_writes_until_capsules_raise_limits() {
|
|
2237
|
+
let mut settings = Vec::new();
|
|
2238
|
+
for (id, value) in [
|
|
2239
|
+
(h3::SETTINGS_ENABLE_CONNECT_PROTOCOL, 1),
|
|
2240
|
+
(h3::SETTINGS_H3_DATAGRAM, 1),
|
|
2241
|
+
(h3::SETTINGS_WT_MAX_SESSIONS, 1),
|
|
2242
|
+
] {
|
|
2243
|
+
h3::put_varint(id, &mut settings);
|
|
2244
|
+
h3::put_varint(value, &mut settings);
|
|
2245
|
+
}
|
|
2246
|
+
let (mut client, mut server, mut session) = established_server(settings);
|
|
2247
|
+
let mut events = Vec::new();
|
|
2248
|
+
session.open_stream(false, 11, &mut events);
|
|
2249
|
+
session.open_stream(true, 12, &mut events);
|
|
2250
|
+
session.flush(&mut server, &mut events);
|
|
2251
|
+
assert!(!events.iter().any(|e| matches!(e, Ev::StreamOpened { .. })));
|
|
2252
|
+
assert_eq!(session.pending_opens.len(), 2);
|
|
2253
|
+
session.scan_capsules(&capsule(h3::WT_MAX_STREAMS_UNI_CAPSULE, 1), &mut events);
|
|
2254
|
+
session.flush(&mut server, &mut events);
|
|
2255
|
+
let id = events
|
|
2256
|
+
.iter()
|
|
2257
|
+
.find_map(|e| match e {
|
|
2258
|
+
Ev::StreamOpened { request_id: 11, id } => Some(*id),
|
|
2259
|
+
_ => None,
|
|
2260
|
+
})
|
|
2261
|
+
.unwrap();
|
|
2262
|
+
assert!(!events
|
|
2263
|
+
.iter()
|
|
2264
|
+
.any(|e| matches!(e, Ev::StreamOpened { request_id: 12, .. })));
|
|
2265
|
+
session.stream_write(id, vec![7; 8], 13, &mut events);
|
|
2266
|
+
session.stream_fin(id);
|
|
2267
|
+
session.flush(&mut server, &mut events);
|
|
2268
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2269
|
+
let mut buf = [0; 100];
|
|
2270
|
+
let (n, fin) = client.stream_recv(id, &mut buf).unwrap();
|
|
2271
|
+
assert_eq!(n, 3); // stream type and session ID, no payload
|
|
2272
|
+
assert!(!fin);
|
|
2273
|
+
assert_eq!(session.fc_data_sent, 0);
|
|
2274
|
+
session.scan_capsules(&capsule(h3::WT_MAX_DATA_CAPSULE, 3), &mut events);
|
|
2275
|
+
session.flush(&mut server, &mut events);
|
|
2276
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2277
|
+
let (n, fin) = client.stream_recv(id, &mut buf).unwrap();
|
|
2278
|
+
assert_eq!(&buf[..n], &[7; 3]);
|
|
2279
|
+
assert!(!fin);
|
|
2280
|
+
assert!(!events
|
|
2281
|
+
.iter()
|
|
2282
|
+
.any(|e| matches!(e, Ev::WriteAck { request_id: 13 })));
|
|
2283
|
+
session.scan_capsules(&capsule(h3::WT_MAX_DATA_CAPSULE, 8), &mut events);
|
|
2284
|
+
session.scan_capsules(&capsule(h3::WT_MAX_STREAMS_BIDI_CAPSULE, 1), &mut events);
|
|
2285
|
+
session.flush(&mut server, &mut events);
|
|
2286
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2287
|
+
let (n, fin) = client.stream_recv(id, &mut buf).unwrap();
|
|
2288
|
+
assert_eq!(&buf[..n], &[7; 5]);
|
|
2289
|
+
assert!(fin);
|
|
2290
|
+
assert_eq!(session.fc_data_sent, 8);
|
|
2291
|
+
assert_eq!(
|
|
2292
|
+
events
|
|
2293
|
+
.iter()
|
|
2294
|
+
.filter(|e| matches!(e, Ev::WriteAck { request_id: 13 }))
|
|
2295
|
+
.count(),
|
|
2296
|
+
1
|
|
2297
|
+
);
|
|
2298
|
+
assert!(events
|
|
2299
|
+
.iter()
|
|
2300
|
+
.any(|e| matches!(e, Ev::StreamOpened { request_id: 12, .. })));
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
#[test]
|
|
2304
|
+
fn session_credit_rejects_non_increasing_oversized_and_truncated_values() {
|
|
2305
|
+
for bytes in [
|
|
2306
|
+
capsule(h3::WT_MAX_DATA_CAPSULE, 0),
|
|
2307
|
+
capsule(h3::WT_MAX_STREAMS_UNI_CAPSULE, (1 << 60) + 1),
|
|
2308
|
+
capsule(h3::WT_STREAMS_BLOCKED_BIDI_CAPSULE, (1 << 60) + 1),
|
|
2309
|
+
vec![0x99, 0x0b, 0x4d, 0x3d, 1, 0x40],
|
|
2310
|
+
] {
|
|
2311
|
+
let mut session = client_with_connect();
|
|
2312
|
+
session.scan_capsules(&bytes, &mut Vec::new());
|
|
2313
|
+
assert!(session.closed);
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
#[test]
|
|
2318
|
+
fn critical_streams_reject_fin_duplicates_and_settings_order_errors() {
|
|
2319
|
+
for (first, second, fin) in [
|
|
2320
|
+
(h3::control_stream_prefix(), None, true),
|
|
2321
|
+
(
|
|
2322
|
+
h3::control_stream_prefix(),
|
|
2323
|
+
Some(h3::frame(h3::FRAME_SETTINGS, &h3::settings_payload())),
|
|
2324
|
+
false,
|
|
2325
|
+
),
|
|
2326
|
+
(vec![0, 0, 0], None, false),
|
|
2327
|
+
(vec![2], None, true),
|
|
2328
|
+
(vec![3], None, true),
|
|
2329
|
+
] {
|
|
2330
|
+
let mut session = server_with_connect(ParserMemory::new(4096));
|
|
2331
|
+
session.streams.insert(
|
|
2332
|
+
2,
|
|
2333
|
+
Stream::new(Role::PendingUni, session.parser_memory.clone()),
|
|
2334
|
+
);
|
|
2335
|
+
let mut conn = connection();
|
|
2336
|
+
let mut events = Vec::new();
|
|
2337
|
+
session.process_recv(&mut conn, 2, first, fin, &mut events);
|
|
2338
|
+
if let Some(second) = second {
|
|
2339
|
+
session.process_recv(&mut conn, 2, second, false, &mut events);
|
|
2340
|
+
}
|
|
2341
|
+
assert!(session.closed);
|
|
2342
|
+
assert!(events.iter().any(|e| matches!(e, Ev::Error(_))));
|
|
2343
|
+
}
|
|
2344
|
+
let mut session = server_with_connect(ParserMemory::new(4096));
|
|
2345
|
+
let mut conn = connection();
|
|
2346
|
+
for id in [2, 6] {
|
|
2347
|
+
session.streams.insert(
|
|
2348
|
+
id,
|
|
2349
|
+
Stream::new(Role::PendingUni, session.parser_memory.clone()),
|
|
2350
|
+
);
|
|
2351
|
+
session.process_recv(
|
|
2352
|
+
&mut conn,
|
|
2353
|
+
id,
|
|
2354
|
+
h3::control_stream_prefix(),
|
|
2355
|
+
false,
|
|
2356
|
+
&mut Vec::new(),
|
|
2357
|
+
);
|
|
2358
|
+
}
|
|
2359
|
+
assert!(session.closed);
|
|
2360
|
+
}
|
|
2361
|
+
|
|
2362
|
+
#[test]
|
|
2363
|
+
fn peer_reset_of_critical_stream_is_terminal() {
|
|
2364
|
+
let (mut client, mut server, mut session) = established_server(h3::settings_payload());
|
|
2365
|
+
client
|
|
2366
|
+
.stream_shutdown(2, quiche::Shutdown::Write, 0)
|
|
2367
|
+
.unwrap();
|
|
2368
|
+
crate::test_support::exchange(&mut client, &mut server);
|
|
2369
|
+
let mut events = Vec::new();
|
|
2370
|
+
session.on_readable(&mut server, 2, false, &mut events);
|
|
2371
|
+
assert!(session.closed);
|
|
2372
|
+
assert!(events
|
|
2373
|
+
.iter()
|
|
2374
|
+
.any(|e| matches!(e, Ev::Error(message) if message.contains("critical"))));
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
#[test]
|
|
2378
|
+
fn unoffered_response_protocol_rejects_the_session() {
|
|
2379
|
+
let mut session = client_with_connect();
|
|
2380
|
+
session.ready = false;
|
|
2381
|
+
session.protocols = vec!["offered".into()];
|
|
2382
|
+
let frame = h3::encode_headers_frame(&[
|
|
2383
|
+
quiche::h3::Header::new(b":status", b"200"),
|
|
2384
|
+
quiche::h3::Header::new(h3::WT_PROTOCOL, b"\"unoffered\""),
|
|
2385
|
+
])
|
|
2386
|
+
.unwrap();
|
|
2387
|
+
let mut events = Vec::new();
|
|
2388
|
+
session.process_recv(&mut connection(), CONNECT_ID, frame, false, &mut events);
|
|
2389
|
+
assert!(session.closed);
|
|
2390
|
+
assert!(!events.iter().any(|e| matches!(e, Ev::Ready { .. })));
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
#[test]
|
|
2394
|
+
fn complete_capsules_release_retained_storage() {
|
|
2395
|
+
let mut session = client_with_connect();
|
|
2396
|
+
let mut capsule = Vec::new();
|
|
2397
|
+
h3::put_varint(0x42, &mut capsule);
|
|
2398
|
+
h3::put_varint(512 * 1024, &mut capsule);
|
|
2399
|
+
capsule.resize(capsule.len() + 512 * 1024, 0);
|
|
2400
|
+
session.scan_capsules(&capsule, &mut Vec::new());
|
|
2401
|
+
assert_eq!(session.capsule_buf.capacity(), 0);
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
#[test]
|
|
2405
|
+
fn aggregate_incomplete_capsules_are_bounded() {
|
|
2406
|
+
let memory = ParserMemory::new(64 * 1024 * 1024);
|
|
2407
|
+
let mut sessions = Vec::new();
|
|
2408
|
+
let mut fragment = Vec::new();
|
|
2409
|
+
h3::put_varint(0x42, &mut fragment);
|
|
2410
|
+
h3::put_varint(MAX_CONTROL_FRAME as u64, &mut fragment);
|
|
2411
|
+
fragment.resize(512 * 1024, 0);
|
|
2412
|
+
for _ in 0..160 {
|
|
2413
|
+
let mut session = server_with_connect(memory.clone());
|
|
2414
|
+
session.scan_capsules(&fragment, &mut Vec::new());
|
|
2415
|
+
sessions.push(session);
|
|
2416
|
+
}
|
|
2417
|
+
let retained: usize = sessions.iter().map(|s| s.capsule_buf.capacity()).sum();
|
|
2418
|
+
assert!(
|
|
2419
|
+
retained <= 64 * 1024 * 1024,
|
|
2420
|
+
"retained {retained} parser bytes"
|
|
2421
|
+
);
|
|
2422
|
+
assert_eq!(memory.used(), retained);
|
|
2423
|
+
assert!(sessions.iter().any(WtSession::is_closed));
|
|
2424
|
+
assert!(sessions.iter().any(|s| !s.is_closed()));
|
|
2425
|
+
drop(sessions);
|
|
2426
|
+
assert_eq!(memory.used(), 0);
|
|
2427
|
+
}
|
|
2428
|
+
|
|
2429
|
+
#[test]
|
|
2430
|
+
fn equal_byte_complete_capsules_do_not_accumulate_across_sessions() {
|
|
2431
|
+
let memory = ParserMemory::new(4 * 1024 * 1024);
|
|
2432
|
+
let mut sessions = Vec::new();
|
|
2433
|
+
let mut capsule = Vec::new();
|
|
2434
|
+
h3::put_varint(0x42, &mut capsule);
|
|
2435
|
+
h3::put_varint(512 * 1024 - 6, &mut capsule);
|
|
2436
|
+
capsule.resize(512 * 1024, 0);
|
|
2437
|
+
for _ in 0..160 {
|
|
2438
|
+
let mut session = server_with_connect(memory.clone());
|
|
2439
|
+
session.scan_capsules(&capsule, &mut Vec::new());
|
|
2440
|
+
assert!(!session.is_closed());
|
|
2441
|
+
assert_eq!(memory.used(), 0);
|
|
2442
|
+
sessions.push(session);
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
|
|
2446
|
+
fn connection() -> quiche::Connection {
|
|
2447
|
+
let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
|
|
2448
|
+
config.set_application_protos(&[b"h3"]).unwrap();
|
|
2449
|
+
quiche::connect(
|
|
2450
|
+
Some("localhost"),
|
|
2451
|
+
&quiche::ConnectionId::from_ref(&[0; 16]),
|
|
2452
|
+
"127.0.0.1:1234".parse().unwrap(),
|
|
2453
|
+
"127.0.0.1:4433".parse().unwrap(),
|
|
2454
|
+
&mut config,
|
|
2455
|
+
)
|
|
2456
|
+
.unwrap()
|
|
2457
|
+
}
|
|
2458
|
+
|
|
2459
|
+
#[test]
|
|
2460
|
+
fn fragmented_capsule_reassembles_across_data_frames_and_releases_budget() {
|
|
2461
|
+
let memory = ParserMemory::new(4096);
|
|
2462
|
+
let mut session = server_with_connect(memory.clone());
|
|
2463
|
+
let mut capsule = Vec::new();
|
|
2464
|
+
h3::put_varint(h3::WT_DRAIN_SESSION_CAPSULE, &mut capsule);
|
|
2465
|
+
h3::put_varint(0, &mut capsule);
|
|
2466
|
+
let mut conn = connection();
|
|
2467
|
+
let mut events = Vec::new();
|
|
2468
|
+
for byte in capsule {
|
|
2469
|
+
let frame = h3::frame(h3::FRAME_DATA, &[byte]);
|
|
2470
|
+
// Also split the HTTP/3 frame header across QUIC reads.
|
|
2471
|
+
for byte in frame {
|
|
2472
|
+
session.process_recv(&mut conn, CONNECT_ID, vec![byte], false, &mut events);
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
assert_eq!(
|
|
2476
|
+
events.iter().filter(|e| matches!(e, Ev::Draining)).count(),
|
|
2477
|
+
1
|
|
2478
|
+
);
|
|
2479
|
+
assert!(!session.is_closed());
|
|
2480
|
+
assert_eq!(memory.used(), 0);
|
|
2481
|
+
}
|
|
2482
|
+
|
|
2483
|
+
#[test]
|
|
2484
|
+
fn budget_failure_releases_all_offender_buffers_and_preserves_other_sessions() {
|
|
2485
|
+
let memory = ParserMemory::new(4096);
|
|
2486
|
+
let mut healthy = server_with_connect(memory.clone());
|
|
2487
|
+
healthy
|
|
2488
|
+
.streams
|
|
2489
|
+
.get_mut(&CONNECT_ID)
|
|
2490
|
+
.unwrap()
|
|
2491
|
+
.frames
|
|
2492
|
+
.push(&[0; 1000]);
|
|
2493
|
+
let mut offender = server_with_connect(memory.clone());
|
|
2494
|
+
let mut fragment = Vec::new();
|
|
2495
|
+
h3::put_varint(0x42, &mut fragment);
|
|
2496
|
+
h3::put_varint(MAX_CONTROL_FRAME as u64, &mut fragment);
|
|
2497
|
+
fragment.resize(1500, 0);
|
|
2498
|
+
offender.scan_capsules(&fragment, &mut Vec::new());
|
|
2499
|
+
offender
|
|
2500
|
+
.streams
|
|
2501
|
+
.get_mut(&CONNECT_ID)
|
|
2502
|
+
.unwrap()
|
|
2503
|
+
.class_buf
|
|
2504
|
+
.extend_from_slice(&[0; 500])
|
|
2505
|
+
.unwrap();
|
|
2506
|
+
assert_eq!(memory.used(), 1024 + 2048 + 512);
|
|
2507
|
+
let mut events = Vec::new();
|
|
2508
|
+
offender.scan_capsules(&[0; 2048], &mut events);
|
|
2509
|
+
assert!(offender.is_closed());
|
|
2510
|
+
assert!(events
|
|
2511
|
+
.iter()
|
|
2512
|
+
.any(|e| matches!(e, Ev::Error(message) if message.contains("memory limit"))));
|
|
2513
|
+
assert_eq!(memory.used(), 1024);
|
|
2514
|
+
assert!(!healthy.is_closed());
|
|
2515
|
+
let mut replacement = server_with_connect(memory.clone());
|
|
2516
|
+
replacement.scan_capsules(&[0x00, 0x00], &mut Vec::new());
|
|
2517
|
+
assert!(!replacement.is_closed());
|
|
2518
|
+
assert_eq!(memory.used(), 1024);
|
|
2519
|
+
healthy.mark_closed(&mut Vec::new(), 0, Vec::new(), false);
|
|
2520
|
+
assert_eq!(memory.used(), 0);
|
|
2521
|
+
}
|
|
2522
|
+
|
|
2523
|
+
#[test]
|
|
2524
|
+
fn initial_control_chunk_budget_failure_is_terminal() {
|
|
2525
|
+
let memory = ParserMemory::new(64);
|
|
2526
|
+
let mut session = server_with_connect(memory.clone());
|
|
2527
|
+
session.streams.insert(
|
|
2528
|
+
2,
|
|
2529
|
+
Stream::new(Role::PendingUni, session.parser_memory.clone()),
|
|
2530
|
+
);
|
|
2531
|
+
let mut conn = connection();
|
|
2532
|
+
let mut events = Vec::new();
|
|
2533
|
+
session.process_recv(&mut conn, 2, vec![0; 65], false, &mut events);
|
|
2534
|
+
assert!(session.is_closed());
|
|
2535
|
+
assert_eq!(memory.used(), 0);
|
|
2536
|
+
}
|
|
2537
|
+
|
|
2538
|
+
#[test]
|
|
2539
|
+
fn oversized_lengths_are_rejected_without_truncation() {
|
|
2540
|
+
let memory = ParserMemory::new(4096);
|
|
2541
|
+
let mut session = server_with_connect(memory.clone());
|
|
2542
|
+
let mut header = Vec::new();
|
|
2543
|
+
h3::put_varint(0x42, &mut header);
|
|
2544
|
+
h3::put_varint(1 << 40, &mut header);
|
|
2545
|
+
session.scan_capsules(&header, &mut Vec::new());
|
|
2546
|
+
assert!(session.is_closed());
|
|
2547
|
+
assert_eq!(memory.used(), 0);
|
|
2548
|
+
let mut frames = FrameParser::new(memory.clone());
|
|
2549
|
+
frames.push(&header);
|
|
2550
|
+
assert!(frames.next_frame().is_none());
|
|
2551
|
+
assert!(frames.error);
|
|
2552
|
+
drop(frames);
|
|
2553
|
+
assert_eq!(memory.used(), 0);
|
|
2554
|
+
}
|
|
2555
|
+
|
|
2556
|
+
fn server_with_connect(memory: Arc<ParserMemory>) -> WtSession {
|
|
2557
|
+
let mut s = WtSession::new_server(
|
|
2558
|
+
"127.0.0.1:4433".parse().unwrap(),
|
|
2559
|
+
Vec::new(),
|
|
2560
|
+
None,
|
|
2561
|
+
Vec::new(),
|
|
2562
|
+
memory,
|
|
2563
|
+
);
|
|
2564
|
+
s.streams.insert(
|
|
2565
|
+
CONNECT_ID,
|
|
2566
|
+
Stream::new(Role::Connect, s.parser_memory.clone()),
|
|
2567
|
+
);
|
|
2568
|
+
s.ready = true;
|
|
2569
|
+
s.peer_settings = Some(h3::PeerSettings {
|
|
2570
|
+
enable_connect_protocol: true,
|
|
2571
|
+
h3_datagram: true,
|
|
2572
|
+
wt_max_sessions: 1,
|
|
2573
|
+
..Default::default()
|
|
2574
|
+
});
|
|
2575
|
+
s
|
|
2576
|
+
}
|
|
2577
|
+
|
|
1640
2578
|
/// A minimal established-looking client session with a CONNECT stream (so
|
|
1641
2579
|
/// queued session capsules have somewhere to go) and seeded peer settings.
|
|
1642
2580
|
fn client_with_connect() -> WtSession {
|
|
1643
2581
|
let mut s = WtSession::new_client("h".into(), "/".into(), None, Vec::new(), Vec::new());
|
|
1644
|
-
s.streams.insert(
|
|
1645
|
-
|
|
2582
|
+
s.streams.insert(
|
|
2583
|
+
CONNECT_ID,
|
|
2584
|
+
Stream::new(Role::Connect, s.parser_memory.clone()),
|
|
2585
|
+
);
|
|
2586
|
+
s.peer_settings = Some(h3::PeerSettings {
|
|
2587
|
+
wt_max_sessions: 1,
|
|
2588
|
+
..Default::default()
|
|
2589
|
+
});
|
|
2590
|
+
s.ready = true;
|
|
1646
2591
|
s
|
|
1647
2592
|
}
|
|
1648
2593
|
|
|
@@ -1658,7 +2603,7 @@ mod tests {
|
|
|
1658
2603
|
let mut s = client_with_connect();
|
|
1659
2604
|
s.fc_max_data_remote = 100;
|
|
1660
2605
|
s.fc_data_sent = 100;
|
|
1661
|
-
let mut st = Stream::new(Role::WtData { bidi: true });
|
|
2606
|
+
let mut st = Stream::new(Role::WtData { bidi: true }, s.parser_memory.clone());
|
|
1662
2607
|
st.out.push_back(OutChunk {
|
|
1663
2608
|
data: vec![1, 2, 3],
|
|
1664
2609
|
off: 0,
|
|
@@ -1684,7 +2629,7 @@ mod tests {
|
|
|
1684
2629
|
s.fc_max_data_remote = 100;
|
|
1685
2630
|
s.fc_data_sent = 100;
|
|
1686
2631
|
// A WtData stream whose only queued chunk is the prefix (request_id None).
|
|
1687
|
-
let mut st = Stream::new(Role::WtData { bidi: false });
|
|
2632
|
+
let mut st = Stream::new(Role::WtData { bidi: false }, s.parser_memory.clone());
|
|
1688
2633
|
st.out.push_back(OutChunk {
|
|
1689
2634
|
data: vec![0x54],
|
|
1690
2635
|
off: 0,
|
|
@@ -1700,7 +2645,8 @@ mod tests {
|
|
|
1700
2645
|
let mut s = client_with_connect();
|
|
1701
2646
|
s.fc_max_streams_bidi_remote = 1;
|
|
1702
2647
|
// Simulate having opened 2 bidi WT streams (next_bidi advanced twice).
|
|
1703
|
-
s.next_bidi = CLIENT_FIRST_WT_BIDI_ID +
|
|
2648
|
+
s.next_bidi = CLIENT_FIRST_WT_BIDI_ID + 4;
|
|
2649
|
+
s.pending_opens.push_back((true, 99));
|
|
1704
2650
|
s.signal_flow_control_blocked();
|
|
1705
2651
|
assert_eq!(connect_out_len(&s), 1); // WT_STREAMS_BLOCKED_BIDI
|
|
1706
2652
|
s.signal_flow_control_blocked();
|