rwebtransport 0.1.0 → 0.1.2

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 (113) hide show
  1. package/Cargo.lock +666 -666
  2. package/Cargo.toml +31 -31
  3. package/LICENSE +202 -202
  4. package/NOTICE +41 -41
  5. package/README.md +550 -550
  6. package/crates/echo-server/Cargo.toml +20 -20
  7. package/crates/echo-server/src/main.rs +666 -666
  8. package/crates/native/Cargo.toml +23 -23
  9. package/crates/native/src/config.rs +129 -129
  10. package/crates/native/src/driver.rs +463 -463
  11. package/crates/native/src/lib.rs +821 -821
  12. package/crates/native/src/server.rs +718 -718
  13. package/crates/native/src/session.rs +1191 -1191
  14. package/crates/native/src/tls.rs +96 -96
  15. package/crates/wtcore/Cargo.toml +15 -15
  16. package/crates/wtcore/src/h3.rs +226 -226
  17. package/crates/wtcore/src/lib.rs +9 -9
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs.map +1 -1
  20. package/package.json +80 -79
  21. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  24. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  25. package/scripts/bench.mjs +180 -180
  26. package/scripts/build.js +67 -67
  27. package/scripts/postinstall.js +67 -67
  28. package/scripts/setup-unix.sh +129 -129
  29. package/scripts/setup-windows.ps1 +229 -229
  30. package/vendor/quiche/AGENTS.md +75 -75
  31. package/vendor/quiche/COPYING +23 -23
  32. package/vendor/quiche/Cargo.lock +1212 -1212
  33. package/vendor/quiche/Cargo.toml +215 -215
  34. package/vendor/quiche/README.md +466 -466
  35. package/vendor/quiche/examples/Makefile +46 -46
  36. package/vendor/quiche/examples/README.md +20 -20
  37. package/vendor/quiche/examples/cert-big.crt +90 -90
  38. package/vendor/quiche/examples/cert.crt +18 -18
  39. package/vendor/quiche/examples/cert.key +28 -28
  40. package/vendor/quiche/examples/client.c +343 -343
  41. package/vendor/quiche/examples/client.rs +281 -281
  42. package/vendor/quiche/examples/gen-certs.sh +15 -15
  43. package/vendor/quiche/examples/http3-client.c +473 -473
  44. package/vendor/quiche/examples/http3-client.rs +349 -349
  45. package/vendor/quiche/examples/http3-server.c +614 -614
  46. package/vendor/quiche/examples/http3-server.rs +680 -680
  47. package/vendor/quiche/examples/qpack-decode.rs +87 -87
  48. package/vendor/quiche/examples/qpack-encode.rs +88 -88
  49. package/vendor/quiche/examples/rootca.crt +21 -21
  50. package/vendor/quiche/examples/server.c +529 -529
  51. package/vendor/quiche/examples/server.rs +529 -529
  52. package/vendor/quiche/include/quiche.h +1282 -1282
  53. package/vendor/quiche/src/buffers.rs +105 -105
  54. package/vendor/quiche/src/build.rs +66 -66
  55. package/vendor/quiche/src/cid.rs +1079 -1079
  56. package/vendor/quiche/src/crypto/boringssl.rs +373 -373
  57. package/vendor/quiche/src/crypto/mod.rs +671 -671
  58. package/vendor/quiche/src/dgram.rs +118 -118
  59. package/vendor/quiche/src/error.rs +263 -263
  60. package/vendor/quiche/src/ffi.rs +2290 -2290
  61. package/vendor/quiche/src/flowcontrol.rs +246 -246
  62. package/vendor/quiche/src/frame.rs +2172 -2172
  63. package/vendor/quiche/src/h3/AGENTS.md +54 -54
  64. package/vendor/quiche/src/h3/ffi.rs +442 -442
  65. package/vendor/quiche/src/h3/frame.rs +1404 -1404
  66. package/vendor/quiche/src/h3/mod.rs +8223 -8223
  67. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
  68. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
  69. package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
  70. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
  71. package/vendor/quiche/src/h3/stream.rs +1595 -1595
  72. package/vendor/quiche/src/lib.rs +9556 -9556
  73. package/vendor/quiche/src/minmax.rs +446 -446
  74. package/vendor/quiche/src/packet.rs +2334 -2334
  75. package/vendor/quiche/src/path.rs +1374 -1374
  76. package/vendor/quiche/src/pmtud.rs +652 -652
  77. package/vendor/quiche/src/rand.rs +64 -64
  78. package/vendor/quiche/src/range_buf.rs +186 -186
  79. package/vendor/quiche/src/ranges.rs +755 -755
  80. package/vendor/quiche/src/recovery/AGENTS.md +80 -80
  81. package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
  82. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
  83. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
  84. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
  85. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
  86. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
  87. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
  88. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
  89. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
  90. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
  91. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
  92. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
  93. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
  94. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
  95. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
  96. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
  97. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
  98. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
  99. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
  100. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
  101. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
  102. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
  103. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
  104. package/vendor/quiche/src/recovery/mod.rs +3086 -3086
  105. package/vendor/quiche/src/recovery/rtt.rs +152 -152
  106. package/vendor/quiche/src/stream/mod.rs +2210 -2210
  107. package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
  108. package/vendor/quiche/src/stream/send_buf.rs +849 -849
  109. package/vendor/quiche/src/test_utils.rs +681 -681
  110. package/vendor/quiche/src/tests.rs +12922 -12922
  111. package/vendor/quiche/src/tls/boringssl.rs +375 -375
  112. package/vendor/quiche/src/tls/mod.rs +1245 -1245
  113. package/vendor/quiche/src/transport_params.rs +628 -628
@@ -1,226 +1,226 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- //! HTTP/3 + WebTransport framing primitives.
3
- //!
4
- //! quiche 0.29 knows nothing about WebTransport, so this module supplies the
5
- //! minimum HTTP/3 machinery a WebTransport *client* needs on top of quiche's raw
6
- //! QUIC streams: variable-length integers, the handful of HTTP/3 frames used
7
- //! during setup, the SETTINGS we advertise, the Extended CONNECT request codec
8
- //! (built on quiche's public QPACK), and the WebTransport stream/capsule
9
- //! constants. Header (de)compression reuses `quiche::h3::qpack`.
10
-
11
- use quiche::h3::{Header, NameValue};
12
-
13
- pub const H3_CONTROL_STREAM_TYPE: u64 = 0x00;
14
- pub const QPACK_ENCODER_STREAM_TYPE: u64 = 0x02;
15
- pub const QPACK_DECODER_STREAM_TYPE: u64 = 0x03;
16
-
17
- pub const FRAME_DATA: u64 = 0x00;
18
- pub const FRAME_HEADERS: u64 = 0x01;
19
- pub const FRAME_SETTINGS: u64 = 0x04;
20
- pub const FRAME_GOAWAY: u64 = 0x07;
21
-
22
- pub const SETTINGS_QPACK_MAX_TABLE_CAPACITY: u64 = 0x01;
23
- pub const SETTINGS_MAX_FIELD_SECTION_SIZE: u64 = 0x06;
24
- pub const SETTINGS_QPACK_BLOCKED_STREAMS: u64 = 0x07;
25
- pub const SETTINGS_ENABLE_CONNECT_PROTOCOL: u64 = 0x08;
26
- pub const SETTINGS_H3_DATAGRAM: u64 = 0x33;
27
- /// draft-ietf-webtrans-http3 `SETTINGS_WEBTRANSPORT_MAX_SESSIONS`.
28
- pub const SETTINGS_WT_MAX_SESSIONS: u64 = 0x14e9_cd29;
29
- /// Older draft `SETTINGS_ENABLE_WEBTRANSPORT` (kept for maximum compatibility).
30
- pub const SETTINGS_ENABLE_WEBTRANSPORT: u64 = 0x2b60_3742;
31
-
32
- /// Unidirectional WebTransport stream type prefix.
33
- pub const WT_UNI_STREAM_TYPE: u64 = 0x54;
34
- /// Bidirectional WebTransport stream signal (first frame type on the stream).
35
- pub const WT_BIDI_FRAME_TYPE: u64 = 0x41;
36
- /// `CLOSE_WEBTRANSPORT_SESSION` capsule type.
37
- pub const WT_CLOSE_SESSION_CAPSULE: u64 = 0x2843;
38
- /// `DRAIN_WEBTRANSPORT_SESSION` capsule type.
39
- pub const WT_DRAIN_SESSION_CAPSULE: u64 = 0x78ae;
40
-
41
- /// Largest QPACK header block we will decode from a peer.
42
- pub const MAX_HEADER_BLOCK: u64 = 64 * 1024;
43
-
44
- /// Try to read a varint from the front of `buf`. Returns `(value, len)` or
45
- /// `None` if the buffer does not yet hold a complete varint.
46
- pub fn read_varint(buf: &[u8]) -> Option<(u64, usize)> {
47
- let first = *buf.first()?;
48
- let len = 1usize << (first >> 6);
49
- if buf.len() < len {
50
- return None;
51
- }
52
- let mut v = u64::from(first & 0x3f);
53
- for &b in &buf[1..len] {
54
- v = (v << 8) | u64::from(b);
55
- }
56
- Some((v, len))
57
- }
58
-
59
- /// Append `v` to `out` using the shortest varint encoding.
60
- pub fn put_varint(v: u64, out: &mut Vec<u8>) {
61
- if v < (1 << 6) {
62
- out.push(v as u8);
63
- } else if v < (1 << 14) {
64
- out.push(0x40 | (v >> 8) as u8);
65
- out.push(v as u8);
66
- } else if v < (1 << 30) {
67
- out.push(0x80 | (v >> 24) as u8);
68
- out.push((v >> 16) as u8);
69
- out.push((v >> 8) as u8);
70
- out.push(v as u8);
71
- } else {
72
- out.push(0xc0 | (v >> 56) as u8);
73
- for shift in [48u32, 40, 32, 24, 16, 8, 0] {
74
- out.push((v >> shift) as u8);
75
- }
76
- }
77
- }
78
-
79
- /// Number of bytes `put_varint` would emit for `v`.
80
- pub fn varint_len(v: u64) -> usize {
81
- if v < (1 << 6) {
82
- 1
83
- } else if v < (1 << 14) {
84
- 2
85
- } else if v < (1 << 30) {
86
- 4
87
- } else {
88
- 8
89
- }
90
- }
91
-
92
- /// Encode a complete HTTP/3 frame (`type`, `length`, `payload`).
93
- pub fn frame(ty: u64, payload: &[u8]) -> Vec<u8> {
94
- let mut out = Vec::with_capacity(payload.len() + 16);
95
- put_varint(ty, &mut out);
96
- put_varint(payload.len() as u64, &mut out);
97
- out.extend_from_slice(payload);
98
- out
99
- }
100
-
101
- /// Bytes for the client control stream: the stream-type prefix followed by our
102
- /// SETTINGS frame.
103
- pub fn control_stream_prefix() -> Vec<u8> {
104
- let mut out = Vec::new();
105
- put_varint(H3_CONTROL_STREAM_TYPE, &mut out);
106
- out.extend_from_slice(&frame(FRAME_SETTINGS, &settings_payload()));
107
- out
108
- }
109
-
110
- /// The SETTINGS we advertise: QPACK static-only, plus everything a WebTransport
111
- /// client needs (Extended CONNECT, H3 datagrams, and both the modern and legacy
112
- /// WebTransport capability settings).
113
- pub fn settings_payload() -> Vec<u8> {
114
- let mut p = Vec::new();
115
- let pairs: [(u64, u64); 6] = [
116
- (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0),
117
- (SETTINGS_QPACK_BLOCKED_STREAMS, 0),
118
- (SETTINGS_ENABLE_CONNECT_PROTOCOL, 1),
119
- (SETTINGS_H3_DATAGRAM, 1),
120
- (SETTINGS_ENABLE_WEBTRANSPORT, 1),
121
- (SETTINGS_WT_MAX_SESSIONS, 256),
122
- ];
123
- for (id, val) in pairs {
124
- put_varint(id, &mut p);
125
- put_varint(val, &mut p);
126
- }
127
- p
128
- }
129
-
130
- /// Peer SETTINGS relevant to WebTransport.
131
- #[derive(Clone, Copy, Debug, Default)]
132
- pub struct PeerSettings {
133
- pub enable_connect_protocol: bool,
134
- pub h3_datagram: bool,
135
- pub enable_webtransport: bool,
136
- pub wt_max_sessions: u64,
137
- }
138
-
139
- impl PeerSettings {
140
- /// Whether the peer advertises enough to run a WebTransport session.
141
- pub fn webtransport_ok(&self) -> bool {
142
- self.enable_connect_protocol
143
- && self.h3_datagram
144
- && (self.enable_webtransport || self.wt_max_sessions > 0)
145
- }
146
- }
147
-
148
- /// Parse a SETTINGS frame payload. Unknown identifiers are ignored (as required).
149
- pub fn parse_settings(mut payload: &[u8]) -> PeerSettings {
150
- let mut s = PeerSettings::default();
151
- while !payload.is_empty() {
152
- let Some((id, n1)) = read_varint(payload) else {
153
- break;
154
- };
155
- let Some((val, n2)) = read_varint(&payload[n1..]) else {
156
- break;
157
- };
158
- match id {
159
- SETTINGS_ENABLE_CONNECT_PROTOCOL => s.enable_connect_protocol = val != 0,
160
- SETTINGS_H3_DATAGRAM => s.h3_datagram = val != 0,
161
- SETTINGS_ENABLE_WEBTRANSPORT => s.enable_webtransport = val != 0,
162
- SETTINGS_WT_MAX_SESSIONS => s.wt_max_sessions = val,
163
- _ => {}
164
- }
165
- payload = &payload[n1 + n2..];
166
- }
167
- s
168
- }
169
-
170
- /// Build the Extended CONNECT request header list for a WebTransport session.
171
- pub fn connect_headers(
172
- authority: &str,
173
- path: &str,
174
- origin: Option<&str>,
175
- extra: &[(String, String)],
176
- ) -> Vec<Header> {
177
- let mut h = vec![
178
- Header::new(b":method", b"CONNECT"),
179
- Header::new(b":protocol", b"webtransport"),
180
- Header::new(b":scheme", b"https"),
181
- Header::new(b":authority", authority.as_bytes()),
182
- Header::new(b":path", path.as_bytes()),
183
- ];
184
- if let Some(o) = origin {
185
- h.push(Header::new(b"origin", o.as_bytes()));
186
- }
187
- for (k, v) in extra {
188
- h.push(Header::new(k.as_bytes(), v.as_bytes()));
189
- }
190
- h
191
- }
192
-
193
- /// QPACK-encode a header list and wrap it in an HTTP/3 HEADERS frame.
194
- pub fn encode_headers_frame(headers: &[Header]) -> Result<Vec<u8>, String> {
195
- // Upper bound: static-only encoding never exceeds name+value+8 per field,
196
- // plus the 2-byte required-insert-count/base prefix.
197
- let cap: usize = headers
198
- .iter()
199
- .map(|h| h.name().len() + h.value().len() + 8)
200
- .sum::<usize>()
201
- + 16;
202
- let mut block = vec![0u8; cap];
203
- let mut enc = quiche::h3::qpack::Encoder::new();
204
- let n = enc
205
- .encode(headers, &mut block)
206
- .map_err(|e| format!("qpack encode: {e:?}"))?;
207
- block.truncate(n);
208
- Ok(frame(FRAME_HEADERS, &block))
209
- }
210
-
211
- /// QPACK-decode a HEADERS frame payload into a header list.
212
- pub fn decode_header_block(block: &[u8]) -> Result<Vec<Header>, String> {
213
- let mut dec = quiche::h3::qpack::Decoder::new();
214
- dec.decode(block, MAX_HEADER_BLOCK)
215
- .map_err(|e| format!("qpack decode: {e:?}"))
216
- }
217
-
218
- /// Extract the numeric `:status` from a decoded response header list.
219
- pub fn status_of(headers: &[Header]) -> Option<u16> {
220
- for h in headers {
221
- if h.name() == b":status" {
222
- return std::str::from_utf8(h.value()).ok()?.trim().parse().ok();
223
- }
224
- }
225
- None
226
- }
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //! HTTP/3 + WebTransport framing primitives.
3
+ //!
4
+ //! quiche 0.29 knows nothing about WebTransport, so this module supplies the
5
+ //! minimum HTTP/3 machinery a WebTransport *client* needs on top of quiche's raw
6
+ //! QUIC streams: variable-length integers, the handful of HTTP/3 frames used
7
+ //! during setup, the SETTINGS we advertise, the Extended CONNECT request codec
8
+ //! (built on quiche's public QPACK), and the WebTransport stream/capsule
9
+ //! constants. Header (de)compression reuses `quiche::h3::qpack`.
10
+
11
+ use quiche::h3::{Header, NameValue};
12
+
13
+ pub const H3_CONTROL_STREAM_TYPE: u64 = 0x00;
14
+ pub const QPACK_ENCODER_STREAM_TYPE: u64 = 0x02;
15
+ pub const QPACK_DECODER_STREAM_TYPE: u64 = 0x03;
16
+
17
+ pub const FRAME_DATA: u64 = 0x00;
18
+ pub const FRAME_HEADERS: u64 = 0x01;
19
+ pub const FRAME_SETTINGS: u64 = 0x04;
20
+ pub const FRAME_GOAWAY: u64 = 0x07;
21
+
22
+ pub const SETTINGS_QPACK_MAX_TABLE_CAPACITY: u64 = 0x01;
23
+ pub const SETTINGS_MAX_FIELD_SECTION_SIZE: u64 = 0x06;
24
+ pub const SETTINGS_QPACK_BLOCKED_STREAMS: u64 = 0x07;
25
+ pub const SETTINGS_ENABLE_CONNECT_PROTOCOL: u64 = 0x08;
26
+ pub const SETTINGS_H3_DATAGRAM: u64 = 0x33;
27
+ /// draft-ietf-webtrans-http3 `SETTINGS_WEBTRANSPORT_MAX_SESSIONS`.
28
+ pub const SETTINGS_WT_MAX_SESSIONS: u64 = 0x14e9_cd29;
29
+ /// Older draft `SETTINGS_ENABLE_WEBTRANSPORT` (kept for maximum compatibility).
30
+ pub const SETTINGS_ENABLE_WEBTRANSPORT: u64 = 0x2b60_3742;
31
+
32
+ /// Unidirectional WebTransport stream type prefix.
33
+ pub const WT_UNI_STREAM_TYPE: u64 = 0x54;
34
+ /// Bidirectional WebTransport stream signal (first frame type on the stream).
35
+ pub const WT_BIDI_FRAME_TYPE: u64 = 0x41;
36
+ /// `CLOSE_WEBTRANSPORT_SESSION` capsule type.
37
+ pub const WT_CLOSE_SESSION_CAPSULE: u64 = 0x2843;
38
+ /// `DRAIN_WEBTRANSPORT_SESSION` capsule type.
39
+ pub const WT_DRAIN_SESSION_CAPSULE: u64 = 0x78ae;
40
+
41
+ /// Largest QPACK header block we will decode from a peer.
42
+ pub const MAX_HEADER_BLOCK: u64 = 64 * 1024;
43
+
44
+ /// Try to read a varint from the front of `buf`. Returns `(value, len)` or
45
+ /// `None` if the buffer does not yet hold a complete varint.
46
+ pub fn read_varint(buf: &[u8]) -> Option<(u64, usize)> {
47
+ let first = *buf.first()?;
48
+ let len = 1usize << (first >> 6);
49
+ if buf.len() < len {
50
+ return None;
51
+ }
52
+ let mut v = u64::from(first & 0x3f);
53
+ for &b in &buf[1..len] {
54
+ v = (v << 8) | u64::from(b);
55
+ }
56
+ Some((v, len))
57
+ }
58
+
59
+ /// Append `v` to `out` using the shortest varint encoding.
60
+ pub fn put_varint(v: u64, out: &mut Vec<u8>) {
61
+ if v < (1 << 6) {
62
+ out.push(v as u8);
63
+ } else if v < (1 << 14) {
64
+ out.push(0x40 | (v >> 8) as u8);
65
+ out.push(v as u8);
66
+ } else if v < (1 << 30) {
67
+ out.push(0x80 | (v >> 24) as u8);
68
+ out.push((v >> 16) as u8);
69
+ out.push((v >> 8) as u8);
70
+ out.push(v as u8);
71
+ } else {
72
+ out.push(0xc0 | (v >> 56) as u8);
73
+ for shift in [48u32, 40, 32, 24, 16, 8, 0] {
74
+ out.push((v >> shift) as u8);
75
+ }
76
+ }
77
+ }
78
+
79
+ /// Number of bytes `put_varint` would emit for `v`.
80
+ pub fn varint_len(v: u64) -> usize {
81
+ if v < (1 << 6) {
82
+ 1
83
+ } else if v < (1 << 14) {
84
+ 2
85
+ } else if v < (1 << 30) {
86
+ 4
87
+ } else {
88
+ 8
89
+ }
90
+ }
91
+
92
+ /// Encode a complete HTTP/3 frame (`type`, `length`, `payload`).
93
+ pub fn frame(ty: u64, payload: &[u8]) -> Vec<u8> {
94
+ let mut out = Vec::with_capacity(payload.len() + 16);
95
+ put_varint(ty, &mut out);
96
+ put_varint(payload.len() as u64, &mut out);
97
+ out.extend_from_slice(payload);
98
+ out
99
+ }
100
+
101
+ /// Bytes for the client control stream: the stream-type prefix followed by our
102
+ /// SETTINGS frame.
103
+ pub fn control_stream_prefix() -> Vec<u8> {
104
+ let mut out = Vec::new();
105
+ put_varint(H3_CONTROL_STREAM_TYPE, &mut out);
106
+ out.extend_from_slice(&frame(FRAME_SETTINGS, &settings_payload()));
107
+ out
108
+ }
109
+
110
+ /// The SETTINGS we advertise: QPACK static-only, plus everything a WebTransport
111
+ /// client needs (Extended CONNECT, H3 datagrams, and both the modern and legacy
112
+ /// WebTransport capability settings).
113
+ pub fn settings_payload() -> Vec<u8> {
114
+ let mut p = Vec::new();
115
+ let pairs: [(u64, u64); 6] = [
116
+ (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0),
117
+ (SETTINGS_QPACK_BLOCKED_STREAMS, 0),
118
+ (SETTINGS_ENABLE_CONNECT_PROTOCOL, 1),
119
+ (SETTINGS_H3_DATAGRAM, 1),
120
+ (SETTINGS_ENABLE_WEBTRANSPORT, 1),
121
+ (SETTINGS_WT_MAX_SESSIONS, 256),
122
+ ];
123
+ for (id, val) in pairs {
124
+ put_varint(id, &mut p);
125
+ put_varint(val, &mut p);
126
+ }
127
+ p
128
+ }
129
+
130
+ /// Peer SETTINGS relevant to WebTransport.
131
+ #[derive(Clone, Copy, Debug, Default)]
132
+ pub struct PeerSettings {
133
+ pub enable_connect_protocol: bool,
134
+ pub h3_datagram: bool,
135
+ pub enable_webtransport: bool,
136
+ pub wt_max_sessions: u64,
137
+ }
138
+
139
+ impl PeerSettings {
140
+ /// Whether the peer advertises enough to run a WebTransport session.
141
+ pub fn webtransport_ok(&self) -> bool {
142
+ self.enable_connect_protocol
143
+ && self.h3_datagram
144
+ && (self.enable_webtransport || self.wt_max_sessions > 0)
145
+ }
146
+ }
147
+
148
+ /// Parse a SETTINGS frame payload. Unknown identifiers are ignored (as required).
149
+ pub fn parse_settings(mut payload: &[u8]) -> PeerSettings {
150
+ let mut s = PeerSettings::default();
151
+ while !payload.is_empty() {
152
+ let Some((id, n1)) = read_varint(payload) else {
153
+ break;
154
+ };
155
+ let Some((val, n2)) = read_varint(&payload[n1..]) else {
156
+ break;
157
+ };
158
+ match id {
159
+ SETTINGS_ENABLE_CONNECT_PROTOCOL => s.enable_connect_protocol = val != 0,
160
+ SETTINGS_H3_DATAGRAM => s.h3_datagram = val != 0,
161
+ SETTINGS_ENABLE_WEBTRANSPORT => s.enable_webtransport = val != 0,
162
+ SETTINGS_WT_MAX_SESSIONS => s.wt_max_sessions = val,
163
+ _ => {}
164
+ }
165
+ payload = &payload[n1 + n2..];
166
+ }
167
+ s
168
+ }
169
+
170
+ /// Build the Extended CONNECT request header list for a WebTransport session.
171
+ pub fn connect_headers(
172
+ authority: &str,
173
+ path: &str,
174
+ origin: Option<&str>,
175
+ extra: &[(String, String)],
176
+ ) -> Vec<Header> {
177
+ let mut h = vec![
178
+ Header::new(b":method", b"CONNECT"),
179
+ Header::new(b":protocol", b"webtransport"),
180
+ Header::new(b":scheme", b"https"),
181
+ Header::new(b":authority", authority.as_bytes()),
182
+ Header::new(b":path", path.as_bytes()),
183
+ ];
184
+ if let Some(o) = origin {
185
+ h.push(Header::new(b"origin", o.as_bytes()));
186
+ }
187
+ for (k, v) in extra {
188
+ h.push(Header::new(k.as_bytes(), v.as_bytes()));
189
+ }
190
+ h
191
+ }
192
+
193
+ /// QPACK-encode a header list and wrap it in an HTTP/3 HEADERS frame.
194
+ pub fn encode_headers_frame(headers: &[Header]) -> Result<Vec<u8>, String> {
195
+ // Upper bound: static-only encoding never exceeds name+value+8 per field,
196
+ // plus the 2-byte required-insert-count/base prefix.
197
+ let cap: usize = headers
198
+ .iter()
199
+ .map(|h| h.name().len() + h.value().len() + 8)
200
+ .sum::<usize>()
201
+ + 16;
202
+ let mut block = vec![0u8; cap];
203
+ let mut enc = quiche::h3::qpack::Encoder::new();
204
+ let n = enc
205
+ .encode(headers, &mut block)
206
+ .map_err(|e| format!("qpack encode: {e:?}"))?;
207
+ block.truncate(n);
208
+ Ok(frame(FRAME_HEADERS, &block))
209
+ }
210
+
211
+ /// QPACK-decode a HEADERS frame payload into a header list.
212
+ pub fn decode_header_block(block: &[u8]) -> Result<Vec<Header>, String> {
213
+ let mut dec = quiche::h3::qpack::Decoder::new();
214
+ dec.decode(block, MAX_HEADER_BLOCK)
215
+ .map_err(|e| format!("qpack decode: {e:?}"))
216
+ }
217
+
218
+ /// Extract the numeric `:status` from a decoded response header list.
219
+ pub fn status_of(headers: &[Header]) -> Option<u16> {
220
+ for h in headers {
221
+ if h.name() == b":status" {
222
+ return std::str::from_utf8(h.value()).ok()?.trim().parse().ok();
223
+ }
224
+ }
225
+ None
226
+ }
@@ -1,9 +1,9 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- //! Shared WebTransport-over-HTTP/3 protocol primitives.
3
- //!
4
- //! Framing, SETTINGS, the Extended CONNECT header codec, and the WebTransport
5
- //! stream/capsule constants — everything both the client (`rwebtransport-native`)
6
- //! and the test echo server (`rwebtransport-echo-server`) need on top of quiche's
7
- //! raw QUIC streams. Header (de)compression reuses `quiche::h3::qpack`.
8
-
9
- pub mod h3;
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //! Shared WebTransport-over-HTTP/3 protocol primitives.
3
+ //!
4
+ //! Framing, SETTINGS, the Extended CONNECT header codec, and the WebTransport
5
+ //! stream/capsule constants — everything both the client (`rwebtransport-native`)
6
+ //! and the test echo server (`rwebtransport-echo-server`) need on top of quiche's
7
+ //! raw QUIC streams. Header (de)compression reuses `quiche::h3::qpack`.
8
+
9
+ pub mod h3;