rwebtransport 0.2.3 → 0.2.5

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.
Files changed (96) hide show
  1. package/Cargo.lock +153 -226
  2. package/Cargo.toml +5 -5
  3. package/README.md +4 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +1 -1
  6. package/crates/native/src/config.rs +13 -1
  7. package/crates/native/src/driver.rs +249 -91
  8. package/crates/native/src/lib.rs +9 -10
  9. package/crates/native/src/parser_memory.rs +1 -0
  10. package/crates/native/src/server.rs +197 -182
  11. package/crates/native/src/session.rs +329 -177
  12. package/crates/native/src/test_support.rs +2 -2
  13. package/crates/wtcore/src/h3.rs +166 -10
  14. package/dist/index.js +92 -45
  15. package/dist/index.js.map +2 -2
  16. package/dist/index.mjs +92 -45
  17. package/dist/index.mjs.map +2 -2
  18. package/dist/native.d.ts +9 -4
  19. package/package.json +14 -11
  20. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  21. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  23. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  24. package/scripts/build-linux-prebuilt.sh +46 -0
  25. package/scripts/build.js +10 -3
  26. package/vendor/quiche/AGENTS.md +10 -0
  27. package/vendor/quiche/Cargo.lock +239 -248
  28. package/vendor/quiche/Cargo.toml +23 -10
  29. package/vendor/quiche/examples/Makefile +4 -4
  30. package/vendor/quiche/examples/client.rs +10 -28
  31. package/vendor/quiche/examples/http3-client.rs +20 -34
  32. package/vendor/quiche/examples/http3-server.rs +47 -78
  33. package/vendor/quiche/examples/server.rs +19 -49
  34. package/vendor/quiche/include/quiche.h +35 -4
  35. package/vendor/quiche/src/build.rs +17 -0
  36. package/vendor/quiche/src/cid.rs +129 -61
  37. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  38. package/vendor/quiche/src/crypto/mod.rs +64 -77
  39. package/vendor/quiche/src/dgram.rs +2 -3
  40. package/vendor/quiche/src/error.rs +22 -20
  41. package/vendor/quiche/src/ffi.rs +558 -280
  42. package/vendor/quiche/src/flowcontrol.rs +0 -7
  43. package/vendor/quiche/src/frame.rs +134 -135
  44. package/vendor/quiche/src/h3/ffi.rs +73 -60
  45. package/vendor/quiche/src/h3/frame.rs +70 -90
  46. package/vendor/quiche/src/h3/mod.rs +1024 -593
  47. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  48. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  49. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  50. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  51. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  52. package/vendor/quiche/src/h3/stream.rs +1009 -194
  53. package/vendor/quiche/src/lib.rs +1346 -1195
  54. package/vendor/quiche/src/minmax.rs +2 -6
  55. package/vendor/quiche/src/packet.rs +420 -552
  56. package/vendor/quiche/src/path.rs +202 -72
  57. package/vendor/quiche/src/pmtud.rs +18 -16
  58. package/vendor/quiche/src/ranges.rs +184 -82
  59. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  60. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  61. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  62. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  63. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  64. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  65. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  66. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  67. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  68. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  69. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  70. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  71. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  72. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  73. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  74. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  84. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  85. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  86. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  87. package/vendor/quiche/src/recovery/mod.rs +414 -173
  88. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  89. package/vendor/quiche/src/stream/mod.rs +864 -133
  90. package/vendor/quiche/src/stream/recv_buf.rs +471 -172
  91. package/vendor/quiche/src/stream/send_buf.rs +338 -81
  92. package/vendor/quiche/src/test_utils.rs +78 -64
  93. package/vendor/quiche/src/tests.rs +2559 -1335
  94. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  95. package/vendor/quiche/src/tls/mod.rs +146 -193
  96. package/vendor/quiche/src/transport_params.rs +87 -103
@@ -0,0 +1,859 @@
1
+ // Copyright (C) 2026, Cloudflare, Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright notice,
9
+ // this list of conditions and the following disclaimer.
10
+ //
11
+ // * Redistributions in binary form must reproduce the above copyright
12
+ // notice, this list of conditions and the following disclaimer in the
13
+ // documentation and/or other materials provided with the distribution.
14
+ //
15
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ //! The QPACK decoder's dynamic table.
28
+ //!
29
+ //! See <https://www.rfc-editor.org/rfc/rfc9204.html#section-3.2>.
30
+
31
+ // TODO: the decoder doesn't use this table yet; see the TODOs in decoder.rs.
32
+
33
+ use std::collections::VecDeque;
34
+ use std::sync::Arc;
35
+
36
+ use super::static_table::STATIC_DECODE_TABLE;
37
+
38
+ /// The overhead, in bytes, added to the lengths of an entry's name and value
39
+ /// to compute its size (RFC 9204, Section 3.2.1).
40
+ const ENTRY_OVERHEAD: usize = 32;
41
+
42
+ /// A dynamic table error.
43
+ ///
44
+ /// When caused by an encoder instruction, each of these is a connection error
45
+ /// of type `QPACK_ENCODER_STREAM_ERROR`.
46
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
47
+ pub(crate) enum TableError {
48
+ /// The capacity exceeds the table's maximum capacity.
49
+ CapacityExceedsMaximum,
50
+
51
+ /// The entry's size exceeds the table's capacity.
52
+ EntryTooLarge,
53
+
54
+ /// The index doesn't refer to an entry in the static or dynamic table.
55
+ InvalidReference,
56
+ }
57
+
58
+ /// The name of a dynamic table entry.
59
+ #[derive(Clone)]
60
+ enum EntryName {
61
+ /// A name borrowed from the static table.
62
+ Static(&'static [u8]),
63
+
64
+ /// A name received from the encoder, shared by all entries that use it.
65
+ Shared(Arc<[u8]>),
66
+ }
67
+
68
+ impl EntryName {
69
+ fn as_bytes(&self) -> &[u8] {
70
+ match self {
71
+ EntryName::Static(name) => name,
72
+
73
+ EntryName::Shared(name) => name,
74
+ }
75
+ }
76
+ }
77
+
78
+ /// A dynamic table entry.
79
+ ///
80
+ /// Cloning an entry clones the handles to its name and value, not their bytes.
81
+ #[derive(Clone)]
82
+ pub(crate) struct Entry {
83
+ name: EntryName,
84
+
85
+ value: Arc<[u8]>,
86
+
87
+ /// The entry's QPACK table size: name.len() + value.len() + 32 bytes
88
+ /// (RFC 9204, Section 3.2.1).
89
+ size: usize,
90
+ }
91
+
92
+ impl Entry {
93
+ fn new(name: EntryName, value: Arc<[u8]>) -> Result<Entry, TableError> {
94
+ let size = name
95
+ .as_bytes()
96
+ .len()
97
+ .checked_add(value.len())
98
+ .and_then(|len| len.checked_add(ENTRY_OVERHEAD))
99
+ .ok_or(TableError::EntryTooLarge)?;
100
+
101
+ Ok(Entry { name, value, size })
102
+ }
103
+
104
+ /// Returns the entry's name.
105
+ pub(crate) fn name(&self) -> &[u8] {
106
+ self.name.as_bytes()
107
+ }
108
+
109
+ /// Returns the entry's value.
110
+ pub(crate) fn value(&self) -> &[u8] {
111
+ &self.value
112
+ }
113
+ }
114
+
115
+ /// A QPACK decoder's dynamic table (RFC 9204, Section 3.2).
116
+ ///
117
+ /// The table starts empty, with a capacity of zero.
118
+ /// Invariant: `size <= capacity <= max_capacity`.
119
+ pub(crate) struct DynamicTable {
120
+ /// The entries, from oldest to newest.
121
+ entries: VecDeque<Entry>,
122
+
123
+ /// Equal to the sum of `entry.size` for all entries in `entries`.
124
+ size: usize,
125
+
126
+ /// The capacity set by the encoder.
127
+ capacity: usize,
128
+
129
+ /// The decoder's locally configured maximum, advertised to the encoder via
130
+ /// `SETTINGS_QPACK_MAX_TABLE_CAPACITY` (RFC 9204, Section 3.2.3).
131
+ max_capacity: usize,
132
+
133
+ /// The number of entries ever inserted, including duplicates. This is also
134
+ /// the absolute index of the next inserted entry.
135
+ insert_count: u64,
136
+ }
137
+
138
+ impl DynamicTable {
139
+ /// Creates an empty table with a capacity of zero.
140
+ ///
141
+ /// `max_capacity` must be the `SETTINGS_QPACK_MAX_TABLE_CAPACITY` value
142
+ /// advertised to the peer. It bounds the memory charged to the table and
143
+ /// can't change during the table's lifetime.
144
+ pub(crate) fn new(max_capacity: usize) -> DynamicTable {
145
+ DynamicTable {
146
+ entries: VecDeque::new(),
147
+ size: 0,
148
+ capacity: 0,
149
+ max_capacity,
150
+ insert_count: 0,
151
+ }
152
+ }
153
+
154
+ /// Returns the table's capacity.
155
+ fn capacity(&self) -> usize {
156
+ self.capacity
157
+ }
158
+
159
+ /// Returns the maximum capacity the encoder may set.
160
+ fn max_capacity(&self) -> usize {
161
+ self.max_capacity
162
+ }
163
+
164
+ /// Returns the sum of the sizes of the table's entries.
165
+ fn size(&self) -> usize {
166
+ self.size
167
+ }
168
+
169
+ /// Returns the number of entries ever inserted, including duplicates.
170
+ ///
171
+ /// Evictions and capacity changes don't decrease it.
172
+ fn insert_count(&self) -> u64 {
173
+ self.insert_count
174
+ }
175
+
176
+ /// Returns the entry with the given absolute index, unless it was evicted
177
+ /// or hasn't been inserted yet (RFC 9204, Section 3.2.4).
178
+ pub(crate) fn get_by_absolute_index(&self, absolute_index: u64) -> Option<&Entry> {
179
+ // The absolute index of the oldest entry.
180
+ let oldest = self.insert_count.checked_sub(self.entries.len() as u64)?;
181
+ let pos = absolute_index.checked_sub(oldest)?;
182
+
183
+ self.entries.get(usize::try_from(pos).ok()?)
184
+ }
185
+
186
+ /// Sets the table's capacity, evicting the oldest entries that no longer
187
+ /// fit (RFC 9204, Section 4.3.1).
188
+ fn set_dynamic_table_capacity(&mut self, capacity: u64) -> Result<(), TableError> {
189
+ let capacity = usize::try_from(capacity).map_err(|_| TableError::CapacityExceedsMaximum)?;
190
+
191
+ if capacity > self.max_capacity {
192
+ return Err(TableError::CapacityExceedsMaximum);
193
+ }
194
+
195
+ self.evict_to(capacity);
196
+ self.capacity = capacity;
197
+
198
+ Ok(())
199
+ }
200
+
201
+ /// Inserts an entry named after the static table entry at `static_index`
202
+ /// (RFC 9204, Section 4.3.2).
203
+ ///
204
+ /// The name is borrowed from the static table, not copied.
205
+ fn insert_with_static_name_reference(
206
+ &mut self,
207
+ static_index: u64,
208
+ value: Arc<[u8]>,
209
+ ) -> Result<(), TableError> {
210
+ let index = usize::try_from(static_index).map_err(|_| TableError::InvalidReference)?;
211
+ let &(name, _) = STATIC_DECODE_TABLE
212
+ .get(index)
213
+ .ok_or(TableError::InvalidReference)?;
214
+
215
+ self.insert(Entry::new(EntryName::Static(name), value)?)
216
+ }
217
+
218
+ /// Inserts an entry named after the dynamic table entry at
219
+ /// `encoder_relative_index` (RFC 9204, Section 4.3.2).
220
+ ///
221
+ /// The name is shared with the referenced entry, not copied.
222
+ fn insert_with_dynamic_name_reference(
223
+ &mut self,
224
+ encoder_relative_index: u64,
225
+ value: Arc<[u8]>,
226
+ ) -> Result<(), TableError> {
227
+ // Take a handle to the name first, as inserting may evict the
228
+ // referenced entry (RFC 9204, Section 3.2.2).
229
+ let name = self
230
+ .get_by_encoder_relative_index(encoder_relative_index)
231
+ .ok_or(TableError::InvalidReference)?
232
+ .name
233
+ .clone();
234
+
235
+ self.insert(Entry::new(name, value)?)
236
+ }
237
+
238
+ /// Inserts an entry with a literal name (RFC 9204, Section 4.3.3).
239
+ fn insert_with_literal_name(
240
+ &mut self,
241
+ name: Arc<[u8]>,
242
+ value: Arc<[u8]>,
243
+ ) -> Result<(), TableError> {
244
+ self.insert(Entry::new(EntryName::Shared(name), value)?)
245
+ }
246
+
247
+ /// Re-inserts the dynamic table entry at `encoder_relative_index` (RFC
248
+ /// 9204, Section 4.3.4).
249
+ ///
250
+ /// The new entry shares its name and value with the existing one.
251
+ fn duplicate(&mut self, encoder_relative_index: u64) -> Result<(), TableError> {
252
+ // Take handles to the entry's name and value first, as inserting may
253
+ // evict the entry (RFC 9204, Section 3.2.2).
254
+ let entry = self
255
+ .get_by_encoder_relative_index(encoder_relative_index)
256
+ .ok_or(TableError::InvalidReference)?
257
+ .clone();
258
+
259
+ self.insert(entry)
260
+ }
261
+
262
+ /// Returns the entry with the given encoder-instruction relative index,
263
+ /// where 0 is the most recently inserted entry (RFC 9204, Section 3.2.5).
264
+ fn get_by_encoder_relative_index(&self, encoder_relative_index: u64) -> Option<&Entry> {
265
+ let absolute_index = self
266
+ .insert_count
267
+ .checked_sub(encoder_relative_index)?
268
+ .checked_sub(1)?;
269
+
270
+ self.get_by_absolute_index(absolute_index)
271
+ }
272
+
273
+ /// Adds `entry` as the newest entry, first evicting the oldest entries to
274
+ /// make room for it (RFC 9204, Section 3.2.2).
275
+ fn insert(&mut self, entry: Entry) -> Result<(), TableError> {
276
+ // The size that the existing entries must fit in alongside `entry`.
277
+ let limit = self
278
+ .capacity
279
+ .checked_sub(entry.size)
280
+ .ok_or(TableError::EntryTooLarge)?;
281
+
282
+ self.evict_to(limit);
283
+
284
+ self.size += entry.size;
285
+ self.entries.push_back(entry);
286
+
287
+ // Can't overflow, as a QUIC stream carries fewer than 2^62 bytes and
288
+ // each encoder instruction takes at least one.
289
+ self.insert_count += 1;
290
+
291
+ Ok(())
292
+ }
293
+
294
+ /// Evicts the oldest entries until the table's size is at most `limit`.
295
+ fn evict_to(&mut self, limit: usize) {
296
+ while self.size > limit {
297
+ let entry = self
298
+ .entries
299
+ .pop_front()
300
+ .expect("nonzero size requires an entry");
301
+
302
+ self.size -= entry.size;
303
+ }
304
+ }
305
+ }
306
+
307
+ #[cfg(test)]
308
+ mod tests {
309
+ use super::*;
310
+
311
+ /// The maximum capacity used by most tests.
312
+ const MAX_CAPACITY: usize = 4096;
313
+
314
+ /// The size of an entry with a 4-byte name and a 4-byte value, such as
315
+ /// ("key0", "val0").
316
+ const SMALL_ENTRY_SIZE: usize = 4 + 4 + 32;
317
+
318
+ fn bytes(s: &str) -> Arc<[u8]> {
319
+ Arc::from(s.as_bytes())
320
+ }
321
+
322
+ /// Returns a value that makes an entry named `name` exactly `size` bytes.
323
+ fn value_of_size(name: &str, size: usize) -> String {
324
+ "x".repeat(size - name.len() - 32)
325
+ }
326
+
327
+ fn insert(table: &mut DynamicTable, name: &str, value: &str) -> Result<(), TableError> {
328
+ table.insert_with_literal_name(bytes(name), bytes(value))
329
+ }
330
+
331
+ /// Inserts the entries ("key{i}", "val{i}") for each `i` in `range`.
332
+ fn insert_small(table: &mut DynamicTable, range: std::ops::Range<usize>) {
333
+ assert!(
334
+ range.end <= 10,
335
+ "insert_small requires single-digit indices"
336
+ );
337
+
338
+ for i in range {
339
+ insert(table, &format!("key{i}"), &format!("val{i}")).unwrap();
340
+ }
341
+ }
342
+
343
+ fn name_str(entry: &Entry) -> &str {
344
+ std::str::from_utf8(entry.name()).unwrap()
345
+ }
346
+
347
+ fn absolute(table: &DynamicTable, index: u64) -> Option<&str> {
348
+ table.get_by_absolute_index(index).map(name_str)
349
+ }
350
+
351
+ fn encoder_relative(table: &DynamicTable, index: u64) -> Option<&str> {
352
+ table.get_by_encoder_relative_index(index).map(name_str)
353
+ }
354
+
355
+ /// Returns the names and values of the entries, from oldest to newest.
356
+ fn contents(table: &DynamicTable) -> Vec<(&str, &str)> {
357
+ table
358
+ .entries
359
+ .iter()
360
+ .map(|e| (name_str(e), std::str::from_utf8(e.value()).unwrap()))
361
+ .collect()
362
+ }
363
+
364
+ /// The table's observable state.
365
+ #[derive(Debug, PartialEq)]
366
+ struct Snapshot {
367
+ capacity: usize,
368
+ size: usize,
369
+ insert_count: u64,
370
+ entries: Vec<(String, String)>,
371
+ }
372
+
373
+ fn snapshot(table: &DynamicTable) -> Snapshot {
374
+ Snapshot {
375
+ capacity: table.capacity(),
376
+ size: table.size(),
377
+ insert_count: table.insert_count(),
378
+ entries: contents(table)
379
+ .into_iter()
380
+ .map(|(name, value)| (name.to_owned(), value.to_owned()))
381
+ .collect(),
382
+ }
383
+ }
384
+
385
+ fn shared_name(entry: &Entry) -> &Arc<[u8]> {
386
+ match &entry.name {
387
+ EntryName::Shared(name) => name,
388
+
389
+ EntryName::Static(_) => panic!("expected a shared name"),
390
+ }
391
+ }
392
+
393
+ #[test]
394
+ fn entry_new_size_and_buffer_sharing() {
395
+ let name = bytes("x-name");
396
+ let value = bytes("value");
397
+
398
+ let shared_entry = Entry::new(EntryName::Shared(name.clone()), value.clone()).unwrap();
399
+ assert_eq!(shared_entry.size, 6 + 5 + 32);
400
+ assert!(Arc::ptr_eq(shared_name(&shared_entry), &name));
401
+ assert!(Arc::ptr_eq(&shared_entry.value, &value));
402
+
403
+ let static_name = STATIC_DECODE_TABLE[0].0;
404
+ let static_entry = Entry::new(EntryName::Static(static_name), value.clone()).unwrap();
405
+ assert_eq!(static_entry.size, 10 + 5 + 32);
406
+ assert!(std::ptr::eq(static_entry.name(), static_name));
407
+ assert!(Arc::ptr_eq(&static_entry.value, &value));
408
+ }
409
+
410
+ #[test]
411
+ fn new_table_has_zero_capacity() {
412
+ let mut table = DynamicTable::new(MAX_CAPACITY);
413
+
414
+ assert_eq!(table.max_capacity(), MAX_CAPACITY);
415
+ assert_eq!(table.capacity(), 0);
416
+ assert_eq!(table.size(), 0);
417
+ assert_eq!(table.insert_count(), 0);
418
+ assert_eq!(absolute(&table, 0), None);
419
+
420
+ // Even the smallest entry doesn't fit until the encoder sets a
421
+ // capacity.
422
+ assert_eq!(insert(&mut table, "", ""), Err(TableError::EntryTooLarge));
423
+ assert_eq!(table.insert_count(), 0);
424
+ }
425
+
426
+ #[test]
427
+ fn max_capacity_is_configurable() {
428
+ for max_capacity in [0, 100, 4096, 8192] {
429
+ let mut table = DynamicTable::new(max_capacity);
430
+ assert_eq!(table.max_capacity(), max_capacity);
431
+ assert_eq!(table.capacity(), 0);
432
+
433
+ assert_eq!(
434
+ table.set_dynamic_table_capacity((max_capacity + 1) as u64),
435
+ Err(TableError::CapacityExceedsMaximum)
436
+ );
437
+ assert_eq!(
438
+ table.set_dynamic_table_capacity(max_capacity as u64),
439
+ Ok(())
440
+ );
441
+ assert_eq!(table.capacity(), max_capacity);
442
+ }
443
+
444
+ // With a maximum of zero, the encoder can't insert anything.
445
+ let mut table = DynamicTable::new(0);
446
+ table.set_dynamic_table_capacity(0).unwrap();
447
+ assert_eq!(insert(&mut table, "", ""), Err(TableError::EntryTooLarge));
448
+
449
+ // Sizes are computed without overflow, whatever the maximum.
450
+ let mut table = DynamicTable::new(usize::MAX);
451
+ assert_eq!(table.set_dynamic_table_capacity(usize::MAX as u64), Ok(()));
452
+ insert_small(&mut table, 0..2);
453
+ assert_eq!(table.size(), 2 * SMALL_ENTRY_SIZE);
454
+ }
455
+
456
+ #[cfg(target_pointer_width = "32")]
457
+ #[test]
458
+ fn capacity_above_usize_max_is_rejected() {
459
+ let mut table = DynamicTable::new(usize::MAX);
460
+
461
+ assert_eq!(
462
+ table.set_dynamic_table_capacity(usize::MAX as u64 + 1),
463
+ Err(TableError::CapacityExceedsMaximum)
464
+ );
465
+ assert_eq!(table.capacity(), 0);
466
+ }
467
+
468
+ #[test]
469
+ fn set_dynamic_table_capacity() {
470
+ let mut table = DynamicTable::new(MAX_CAPACITY);
471
+
472
+ assert_eq!(
473
+ table.set_dynamic_table_capacity(MAX_CAPACITY as u64),
474
+ Ok(())
475
+ );
476
+ assert_eq!(table.capacity(), MAX_CAPACITY);
477
+
478
+ assert_eq!(
479
+ table.set_dynamic_table_capacity((3 * SMALL_ENTRY_SIZE) as u64),
480
+ Ok(())
481
+ );
482
+ insert_small(&mut table, 0..3);
483
+
484
+ // Capacities above the maximum are rejected without evicting entries.
485
+ let before = snapshot(&table);
486
+ assert_eq!(
487
+ table.set_dynamic_table_capacity((MAX_CAPACITY + 1) as u64),
488
+ Err(TableError::CapacityExceedsMaximum)
489
+ );
490
+ assert_eq!(
491
+ table.set_dynamic_table_capacity(u64::MAX),
492
+ Err(TableError::CapacityExceedsMaximum)
493
+ );
494
+ assert_eq!(snapshot(&table), before);
495
+
496
+ // Reducing the capacity evicts the oldest entries until the rest fit.
497
+ assert_eq!(
498
+ table.set_dynamic_table_capacity((3 * SMALL_ENTRY_SIZE - 1) as u64),
499
+ Ok(())
500
+ );
501
+ assert_eq!(contents(&table), [("key1", "val1"), ("key2", "val2")]);
502
+ assert_eq!(table.size(), 2 * SMALL_ENTRY_SIZE);
503
+
504
+ // A capacity equal to the table's size evicts nothing.
505
+ assert_eq!(
506
+ table.set_dynamic_table_capacity((2 * SMALL_ENTRY_SIZE) as u64),
507
+ Ok(())
508
+ );
509
+ assert_eq!(contents(&table), [("key1", "val1"), ("key2", "val2")]);
510
+
511
+ // A capacity of zero evicts every entry.
512
+ assert_eq!(table.set_dynamic_table_capacity(0), Ok(()));
513
+ assert!(contents(&table).is_empty());
514
+ assert_eq!(table.size(), 0);
515
+
516
+ // The capacity can be raised again, and absolute indexing continues.
517
+ assert_eq!(
518
+ table.set_dynamic_table_capacity(SMALL_ENTRY_SIZE as u64),
519
+ Ok(())
520
+ );
521
+ insert_small(&mut table, 3..4);
522
+ assert_eq!(absolute(&table, 3), Some("key3"));
523
+ }
524
+
525
+ #[test]
526
+ fn insert_exact_fit() {
527
+ let mut table = DynamicTable::new(MAX_CAPACITY);
528
+ table
529
+ .set_dynamic_table_capacity(MAX_CAPACITY as u64)
530
+ .unwrap();
531
+
532
+ // Entries whose sizes add up to exactly the capacity all fit.
533
+ let value0 = value_of_size("key0", 1000);
534
+ let value1 = value_of_size("key1", MAX_CAPACITY - 1000);
535
+ assert_eq!(insert(&mut table, "key0", &value0), Ok(()));
536
+ assert_eq!(insert(&mut table, "key1", &value1), Ok(()));
537
+ assert_eq!(
538
+ contents(&table),
539
+ [("key0", value0.as_str()), ("key1", value1.as_str())]
540
+ );
541
+ assert_eq!(table.size(), MAX_CAPACITY);
542
+
543
+ // An entry as large as the capacity fits by evicting all others.
544
+ let value2 = value_of_size("key2", MAX_CAPACITY);
545
+ assert_eq!(insert(&mut table, "key2", &value2), Ok(()));
546
+ assert_eq!(contents(&table), [("key2", value2.as_str())]);
547
+ assert_eq!(table.size(), MAX_CAPACITY);
548
+
549
+ // One byte more doesn't fit, and evicts nothing.
550
+ let before = snapshot(&table);
551
+ let value3 = value_of_size("key3", MAX_CAPACITY + 1);
552
+ assert_eq!(
553
+ insert(&mut table, "key3", &value3),
554
+ Err(TableError::EntryTooLarge)
555
+ );
556
+ assert_eq!(snapshot(&table), before);
557
+ }
558
+
559
+ #[test]
560
+ fn insert_evicts_oldest_entries_first() {
561
+ let mut table = DynamicTable::new(MAX_CAPACITY);
562
+ table
563
+ .set_dynamic_table_capacity((3 * SMALL_ENTRY_SIZE) as u64)
564
+ .unwrap();
565
+ insert_small(&mut table, 0..3);
566
+
567
+ // Making room for one more entry evicts only the oldest one.
568
+ insert_small(&mut table, 3..4);
569
+ assert_eq!(
570
+ contents(&table),
571
+ [("key1", "val1"), ("key2", "val2"), ("key3", "val3")]
572
+ );
573
+ assert_eq!(table.size(), 3 * SMALL_ENTRY_SIZE);
574
+
575
+ // A larger entry evicts as many of the oldest entries as needed.
576
+ let value = value_of_size("key4", 2 * SMALL_ENTRY_SIZE);
577
+ assert_eq!(insert(&mut table, "key4", &value), Ok(()));
578
+ assert_eq!(
579
+ contents(&table),
580
+ [("key3", "val3"), ("key4", value.as_str())]
581
+ );
582
+ assert_eq!(table.size(), 3 * SMALL_ENTRY_SIZE);
583
+ }
584
+
585
+ #[test]
586
+ #[should_panic(expected = "nonzero size requires an entry")]
587
+ fn evict_panics_if_size_is_nonzero_without_entries() {
588
+ let mut table = DynamicTable::new(MAX_CAPACITY);
589
+ table.size = 40;
590
+
591
+ table.evict_to(0);
592
+ }
593
+
594
+ #[test]
595
+ fn insert_rejects_entry_larger_than_capacity() {
596
+ let mut table = DynamicTable::new(MAX_CAPACITY);
597
+ table
598
+ .set_dynamic_table_capacity((3 * SMALL_ENTRY_SIZE) as u64)
599
+ .unwrap();
600
+ insert_small(&mut table, 0..2);
601
+ let before = snapshot(&table);
602
+
603
+ let too_large = 3 * SMALL_ENTRY_SIZE + 1;
604
+
605
+ // Rejected before evicting anything, however the name is given.
606
+ assert_eq!(
607
+ insert(&mut table, "key2", &value_of_size("key2", too_large)),
608
+ Err(TableError::EntryTooLarge)
609
+ );
610
+ assert_eq!(
611
+ table.insert_with_dynamic_name_reference(0, bytes(&value_of_size("key1", too_large))),
612
+ Err(TableError::EntryTooLarge)
613
+ );
614
+ // Static table entry 2 is named "age".
615
+ assert_eq!(
616
+ table.insert_with_static_name_reference(2, bytes(&value_of_size("age", too_large))),
617
+ Err(TableError::EntryTooLarge)
618
+ );
619
+ assert_eq!(snapshot(&table), before);
620
+ }
621
+
622
+ #[test]
623
+ fn absolute_and_encoder_relative_indexing() {
624
+ let mut table = DynamicTable::new(MAX_CAPACITY);
625
+ table
626
+ .set_dynamic_table_capacity((3 * SMALL_ENTRY_SIZE) as u64)
627
+ .unwrap();
628
+ // Evicts "key0".
629
+ insert_small(&mut table, 0..4);
630
+
631
+ // Absolute indices count insertions from zero.
632
+ assert_eq!(absolute(&table, 0), None);
633
+ assert_eq!(absolute(&table, 1), Some("key1"));
634
+ assert_eq!(absolute(&table, 3), Some("key3"));
635
+ assert_eq!(absolute(&table, 4), None);
636
+ assert_eq!(absolute(&table, u64::MAX), None);
637
+
638
+ // Encoder-relative index 0 is the most recently inserted entry.
639
+ assert_eq!(encoder_relative(&table, 0), Some("key3"));
640
+ assert_eq!(encoder_relative(&table, 2), Some("key1"));
641
+ assert_eq!(encoder_relative(&table, 3), None);
642
+ assert_eq!(encoder_relative(&table, 4), None);
643
+ assert_eq!(encoder_relative(&table, u64::MAX), None);
644
+
645
+ // An insertion shifts relative indices but not absolute ones. This
646
+ // one duplicates "key1", evicting the original.
647
+ assert_eq!(table.duplicate(2), Ok(()));
648
+ assert_eq!(encoder_relative(&table, 0), Some("key1"));
649
+ assert_eq!(encoder_relative(&table, 1), Some("key3"));
650
+ assert_eq!(encoder_relative(&table, 2), Some("key2"));
651
+ assert_eq!(absolute(&table, 1), None);
652
+ assert_eq!(absolute(&table, 3), Some("key3"));
653
+ assert_eq!(absolute(&table, 4), Some("key1"));
654
+ }
655
+
656
+ #[test]
657
+ fn insert_count_is_monotonic() {
658
+ let mut table = DynamicTable::new(MAX_CAPACITY);
659
+ table
660
+ .set_dynamic_table_capacity((2 * SMALL_ENTRY_SIZE) as u64)
661
+ .unwrap();
662
+
663
+ // Each successful insertion increments the insert count by one,
664
+ // including those that evict entries.
665
+ insert_small(&mut table, 0..1);
666
+ assert_eq!(table.insert_count(), 1);
667
+
668
+ assert_eq!(
669
+ table.insert_with_static_name_reference(2, bytes("12345")),
670
+ Ok(())
671
+ );
672
+ assert_eq!(table.insert_count(), 2);
673
+
674
+ // Evicts "key0".
675
+ assert_eq!(
676
+ table.insert_with_dynamic_name_reference(0, bytes("val2")),
677
+ Ok(())
678
+ );
679
+ assert_eq!(table.insert_count(), 3);
680
+
681
+ // Evicts the duplicated entry.
682
+ assert_eq!(table.duplicate(1), Ok(()));
683
+ assert_eq!(table.insert_count(), 4);
684
+ assert_eq!(contents(&table), [("age", "val2"), ("age", "12345")]);
685
+
686
+ // Failed operations leave it unchanged.
687
+ let too_large = value_of_size("key9", 2 * SMALL_ENTRY_SIZE + 1);
688
+ assert!(insert(&mut table, "key9", &too_large).is_err());
689
+ assert!(table.duplicate(2).is_err());
690
+ assert!(table
691
+ .insert_with_static_name_reference(99, bytes(""))
692
+ .is_err());
693
+ assert!(table
694
+ .set_dynamic_table_capacity((MAX_CAPACITY + 1) as u64)
695
+ .is_err());
696
+ assert_eq!(table.insert_count(), 4);
697
+
698
+ // So do capacity changes, even when they evict every entry.
699
+ table.set_dynamic_table_capacity(0).unwrap();
700
+ table
701
+ .set_dynamic_table_capacity(MAX_CAPACITY as u64)
702
+ .unwrap();
703
+ assert_eq!(table.insert_count(), 4);
704
+
705
+ insert_small(&mut table, 4..5);
706
+ assert_eq!(table.insert_count(), 5);
707
+ assert_eq!(absolute(&table, 4), Some("key4"));
708
+ }
709
+
710
+ #[test]
711
+ fn invalid_references() {
712
+ let mut table = DynamicTable::new(MAX_CAPACITY);
713
+ table
714
+ .set_dynamic_table_capacity((2 * SMALL_ENTRY_SIZE) as u64)
715
+ .unwrap();
716
+
717
+ // An empty table has no entries to refer to.
718
+ assert_eq!(table.duplicate(0), Err(TableError::InvalidReference));
719
+ assert_eq!(
720
+ table.insert_with_dynamic_name_reference(0, bytes("val")),
721
+ Err(TableError::InvalidReference)
722
+ );
723
+
724
+ // Evicts "key0".
725
+ insert_small(&mut table, 0..3);
726
+ let before = snapshot(&table);
727
+
728
+ // Relative index 2 refers to the evicted "key0", and larger ones to
729
+ // entries that never existed.
730
+ for index in [2, 3, u64::MAX] {
731
+ assert_eq!(table.duplicate(index), Err(TableError::InvalidReference));
732
+ assert_eq!(
733
+ table.insert_with_dynamic_name_reference(index, bytes("val")),
734
+ Err(TableError::InvalidReference)
735
+ );
736
+ }
737
+
738
+ // The static table has 99 entries.
739
+ for index in [99, u64::MAX] {
740
+ assert_eq!(
741
+ table.insert_with_static_name_reference(index, bytes("val")),
742
+ Err(TableError::InvalidReference)
743
+ );
744
+ }
745
+
746
+ assert_eq!(snapshot(&table), before);
747
+ }
748
+
749
+ #[test]
750
+ fn static_name_reference_borrows_static_name() {
751
+ let mut table = DynamicTable::new(MAX_CAPACITY);
752
+ table
753
+ .set_dynamic_table_capacity(MAX_CAPACITY as u64)
754
+ .unwrap();
755
+
756
+ // The first and last static table entries.
757
+ assert_eq!(
758
+ table.insert_with_static_name_reference(0, bytes("example.com")),
759
+ Ok(())
760
+ );
761
+ assert_eq!(
762
+ table.insert_with_static_name_reference(98, bytes("deny")),
763
+ Ok(())
764
+ );
765
+ assert_eq!(
766
+ contents(&table),
767
+ [(":authority", "example.com"), ("x-frame-options", "deny")]
768
+ );
769
+ assert_eq!(table.size(), (10 + 11 + 32) + (15 + 4 + 32));
770
+
771
+ // Names taken from these entries keep borrowing the static table.
772
+ table.duplicate(1).unwrap();
773
+ table
774
+ .insert_with_dynamic_name_reference(1, bytes("sameorigin"))
775
+ .unwrap();
776
+ assert_eq!(
777
+ contents(&table)[2..],
778
+ [
779
+ (":authority", "example.com"),
780
+ ("x-frame-options", "sameorigin")
781
+ ]
782
+ );
783
+
784
+ assert!(table
785
+ .entries
786
+ .iter()
787
+ .all(|e| matches!(e.name, EntryName::Static(_))));
788
+ }
789
+
790
+ #[test]
791
+ fn entries_share_buffers() {
792
+ let mut table = DynamicTable::new(MAX_CAPACITY);
793
+ table
794
+ .set_dynamic_table_capacity(MAX_CAPACITY as u64)
795
+ .unwrap();
796
+
797
+ let name = bytes("x-name");
798
+ let value = bytes("x-value");
799
+ table
800
+ .insert_with_literal_name(name.clone(), value.clone())
801
+ .unwrap();
802
+
803
+ // A duplicate shares the name and the value with the original.
804
+ table.duplicate(0).unwrap();
805
+
806
+ // A dynamic name reference shares only the name.
807
+ let other_value = bytes("other");
808
+ table
809
+ .insert_with_dynamic_name_reference(0, other_value.clone())
810
+ .unwrap();
811
+
812
+ for entry in &table.entries {
813
+ assert!(Arc::ptr_eq(shared_name(entry), &name));
814
+ }
815
+ assert!(Arc::ptr_eq(&table.entries[0].value, &value));
816
+ assert!(Arc::ptr_eq(&table.entries[1].value, &value));
817
+ assert!(Arc::ptr_eq(&table.entries[2].value, &other_value));
818
+
819
+ // Each entry is charged its full size, even with shared buffers.
820
+ assert_eq!(table.size(), 2 * (6 + 7 + 32) + (6 + 5 + 32));
821
+ }
822
+
823
+ #[test]
824
+ fn insert_evicting_referenced_entry_keeps_its_buffers() {
825
+ let name = bytes("key0");
826
+ let value = bytes("val0");
827
+
828
+ let mut table = DynamicTable::new(MAX_CAPACITY);
829
+ // With room for a single entry, each insertion below evicts the entry
830
+ // it refers to.
831
+ table
832
+ .set_dynamic_table_capacity(SMALL_ENTRY_SIZE as u64)
833
+ .unwrap();
834
+ table
835
+ .insert_with_literal_name(name.clone(), value.clone())
836
+ .unwrap();
837
+
838
+ assert_eq!(table.duplicate(0), Ok(()));
839
+ assert_eq!(absolute(&table, 0), None);
840
+ let entry = table.get_by_absolute_index(1).unwrap();
841
+ assert!(Arc::ptr_eq(shared_name(entry), &name));
842
+ assert!(Arc::ptr_eq(&entry.value, &value));
843
+ // The evicted original released its handles, leaving only this test
844
+ // and the duplicate holding the buffers.
845
+ assert_eq!(Arc::strong_count(&name), 2);
846
+ assert_eq!(Arc::strong_count(&value), 2);
847
+
848
+ assert_eq!(
849
+ table.insert_with_dynamic_name_reference(0, bytes("val1")),
850
+ Ok(())
851
+ );
852
+ assert_eq!(absolute(&table, 1), None);
853
+ let entry = table.get_by_absolute_index(2).unwrap();
854
+ assert!(Arc::ptr_eq(shared_name(entry), &name));
855
+ assert_eq!(entry.value(), b"val1");
856
+ assert_eq!(Arc::strong_count(&name), 2);
857
+ assert_eq!(Arc::strong_count(&value), 1);
858
+ }
859
+ }