rwebtransport 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/crates/echo-server/src/bin/gen-test-cert.rs +82 -0
- package/crates/echo-server/src/main.rs +7 -7
- package/crates/native/src/config.rs +1 -1
- package/crates/native/src/driver.rs +91 -13
- package/crates/native/src/lib.rs +196 -4
- package/crates/native/src/server.rs +111 -10
- package/crates/native/src/session.rs +585 -56
- package/crates/native/src/tls.rs +68 -5
- package/crates/wtcore/src/h3.rs +303 -1
- package/crates/wtcore/src/lib.rs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +299 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +298 -11
- package/dist/index.mjs.map +1 -1
- package/dist/native.d.ts +63 -4
- package/dist/server.d.ts +6 -0
- package/dist/streams.d.ts +11 -2
- package/dist/types.d.ts +21 -1
- package/dist/webtransport.d.ts +12 -4
- package/package.json +1 -1
- 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/vendor/quiche/src/lib.rs +11 -0
- package/vendor/quiche/src/tls/mod.rs +38 -0
package/crates/native/src/tls.rs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
//! The default "Google Chrome" TLS profile for the QUIC (TLS 1.3) handshake.
|
|
3
3
|
//!
|
|
4
|
-
//! QUIC mandates TLS 1.3, so
|
|
4
|
+
//! QUIC mandates TLS 1.3, so, unlike a TCP/TLS stack, there is no TLS 1.2
|
|
5
5
|
//! cipher list to negotiate. What still shapes the ClientHello, and what servers
|
|
6
6
|
//! (Cloudflare in particular) fingerprint, is the group preference, the
|
|
7
7
|
//! signature-algorithm list, GREASE, and extension ordering. We mirror Chrome's
|
|
@@ -9,10 +9,16 @@
|
|
|
9
9
|
//! cipher-suite preference order is fixed inside BoringSSL and already matches
|
|
10
10
|
//! Chrome (AES-128-GCM, AES-256-GCM, ChaCha20-Poly1305).
|
|
11
11
|
|
|
12
|
+
use std::cmp::Ordering;
|
|
13
|
+
|
|
14
|
+
use boring::asn1::Asn1Time;
|
|
12
15
|
use boring::hash::MessageDigest;
|
|
16
|
+
use boring::nid::Nid;
|
|
17
|
+
use boring::pkey::Id;
|
|
13
18
|
use boring::ssl::{
|
|
14
19
|
SslAlert, SslContextBuilder, SslMethod, SslVerifyError, SslVerifyMode, SslVersion,
|
|
15
20
|
};
|
|
21
|
+
use boring::x509::X509Ref;
|
|
16
22
|
|
|
17
23
|
/// How the server certificate is verified.
|
|
18
24
|
#[derive(Clone, Debug)]
|
|
@@ -83,14 +89,71 @@ pub fn build_client_tls(verify: &CertVerification) -> Result<SslContextBuilder,
|
|
|
83
89
|
let digest = cert
|
|
84
90
|
.digest(MessageDigest::sha256())
|
|
85
91
|
.map_err(|_| SslVerifyError::Invalid(SslAlert::CERTIFICATE_UNKNOWN))?;
|
|
86
|
-
if hashes.iter().any(|h| digest.as_ref() == h.as_slice()) {
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
92
|
+
if !hashes.iter().any(|h| digest.as_ref() == h.as_slice()) {
|
|
93
|
+
return Err(SslVerifyError::Invalid(SslAlert::BAD_CERTIFICATE));
|
|
94
|
+
}
|
|
95
|
+
// The WebTransport serverCertificateHashes contract requires the
|
|
96
|
+
// pinned leaf to be currently valid and to span at most two weeks;
|
|
97
|
+
// a matching fingerprint alone is not sufficient.
|
|
98
|
+
verify_pinned_cert_validity(&cert)
|
|
99
|
+
.map_err(|_| SslVerifyError::Invalid(SslAlert::CERTIFICATE_EXPIRED))?;
|
|
100
|
+
// It must also use an ECDSA key on the NIST P-256 curve.
|
|
101
|
+
if !is_ecdsa_p256(&cert) {
|
|
102
|
+
return Err(SslVerifyError::Invalid(SslAlert::BAD_CERTIFICATE));
|
|
90
103
|
}
|
|
104
|
+
Ok(())
|
|
91
105
|
});
|
|
92
106
|
}
|
|
93
107
|
}
|
|
94
108
|
|
|
95
109
|
Ok(b)
|
|
96
110
|
}
|
|
111
|
+
|
|
112
|
+
/// Maximum validity span (in seconds) allowed for a pinned `serverCertificateHashes`
|
|
113
|
+
/// leaf: two weeks, matching the WebTransport / browser constraint.
|
|
114
|
+
const MAX_PINNED_VALIDITY_SECS: i64 = 14 * 24 * 60 * 60;
|
|
115
|
+
|
|
116
|
+
/// Enforce the WebTransport `serverCertificateHashes` validity constraints on a
|
|
117
|
+
/// pinned leaf certificate: the current time must be within
|
|
118
|
+
/// `[notBefore, notAfter]`, and the total validity span must not exceed two
|
|
119
|
+
/// weeks. Returns `Err(())` if either check fails (or a time cannot be read).
|
|
120
|
+
fn verify_pinned_cert_validity(cert: &X509Ref) -> Result<(), ()> {
|
|
121
|
+
let now = Asn1Time::days_from_now(0).map_err(|_| ())?;
|
|
122
|
+
let not_before = cert.not_before();
|
|
123
|
+
let not_after = cert.not_after();
|
|
124
|
+
|
|
125
|
+
// Reject a not-yet-valid cert (notBefore is in the future).
|
|
126
|
+
if not_before.compare(&now).map_err(|_| ())? == Ordering::Greater {
|
|
127
|
+
return Err(());
|
|
128
|
+
}
|
|
129
|
+
// Reject an expired cert (notAfter is in the past).
|
|
130
|
+
if not_after.compare(&now).map_err(|_| ())? == Ordering::Less {
|
|
131
|
+
return Err(());
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Reject a cert whose total validity span exceeds two weeks. `diff` computes
|
|
135
|
+
// `notAfter - notBefore`.
|
|
136
|
+
let span = not_before.diff(not_after).map_err(|_| ())?;
|
|
137
|
+
let span_secs = i64::from(span.days) * 86_400 + i64::from(span.secs);
|
|
138
|
+
if !(0..=MAX_PINNED_VALIDITY_SECS).contains(&span_secs) {
|
|
139
|
+
return Err(());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
Ok(())
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// Whether a pinned leaf certificate uses an ECDSA key on the NIST P-256
|
|
146
|
+
/// (secp256r1) curve, as the WebTransport `serverCertificateHashes` contract
|
|
147
|
+
/// requires. Any other key type or curve returns `false`.
|
|
148
|
+
fn is_ecdsa_p256(cert: &X509Ref) -> bool {
|
|
149
|
+
let Ok(pkey) = cert.public_key() else {
|
|
150
|
+
return false;
|
|
151
|
+
};
|
|
152
|
+
if pkey.id() != Id::EC {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
match pkey.ec_key() {
|
|
156
|
+
Ok(ec) => ec.group().curve_name() == Some(Nid::X9_62_PRIME256V1),
|
|
157
|
+
Err(_) => false,
|
|
158
|
+
}
|
|
159
|
+
}
|
package/crates/wtcore/src/h3.rs
CHANGED
|
@@ -19,6 +19,11 @@ pub const FRAME_HEADERS: u64 = 0x01;
|
|
|
19
19
|
pub const FRAME_SETTINGS: u64 = 0x04;
|
|
20
20
|
pub const FRAME_GOAWAY: u64 = 0x07;
|
|
21
21
|
|
|
22
|
+
/// HTTP/3 `H3_NO_ERROR`: the application error code for a graceful, error-free
|
|
23
|
+
/// connection close. A WebTransport session close carries its own code/reason in
|
|
24
|
+
/// the CLOSE_WEBTRANSPORT_SESSION capsule, so the QUIC close uses this.
|
|
25
|
+
pub const H3_NO_ERROR: u64 = 0x100;
|
|
26
|
+
|
|
22
27
|
pub const SETTINGS_QPACK_MAX_TABLE_CAPACITY: u64 = 0x01;
|
|
23
28
|
pub const SETTINGS_MAX_FIELD_SECTION_SIZE: u64 = 0x06;
|
|
24
29
|
pub const SETTINGS_QPACK_BLOCKED_STREAMS: u64 = 0x07;
|
|
@@ -28,6 +33,26 @@ pub const SETTINGS_H3_DATAGRAM: u64 = 0x33;
|
|
|
28
33
|
pub const SETTINGS_WT_MAX_SESSIONS: u64 = 0x14e9_cd29;
|
|
29
34
|
/// Older draft `SETTINGS_ENABLE_WEBTRANSPORT` (kept for maximum compatibility).
|
|
30
35
|
pub const SETTINGS_ENABLE_WEBTRANSPORT: u64 = 0x2b60_3742;
|
|
36
|
+
/// `SETTINGS_WT_INITIAL_MAX_DATA`: initial per-session send-data allowance.
|
|
37
|
+
pub const SETTINGS_WT_INITIAL_MAX_DATA: u64 = 0x2b61;
|
|
38
|
+
/// `SETTINGS_WT_INITIAL_MAX_STREAMS_UNI`: initial per-session uni stream limit.
|
|
39
|
+
pub const SETTINGS_WT_INITIAL_MAX_STREAMS_UNI: u64 = 0x2b64;
|
|
40
|
+
/// `SETTINGS_WT_INITIAL_MAX_STREAMS_BIDI`: initial per-session bidi stream limit.
|
|
41
|
+
pub const SETTINGS_WT_INITIAL_MAX_STREAMS_BIDI: u64 = 0x2b65;
|
|
42
|
+
|
|
43
|
+
/// Per-session flow-control capsule types (draft-ietf-webtrans-http3 §5).
|
|
44
|
+
pub const WT_MAX_DATA_CAPSULE: u64 = 0x190b_4d3d;
|
|
45
|
+
pub const WT_MAX_STREAMS_BIDI_CAPSULE: u64 = 0x190b_4d3f;
|
|
46
|
+
pub const WT_MAX_STREAMS_UNI_CAPSULE: u64 = 0x190b_4d40;
|
|
47
|
+
pub const WT_DATA_BLOCKED_CAPSULE: u64 = 0x190b_4d41;
|
|
48
|
+
pub const WT_STREAMS_BLOCKED_BIDI_CAPSULE: u64 = 0x190b_4d43;
|
|
49
|
+
pub const WT_STREAMS_BLOCKED_UNI_CAPSULE: u64 = 0x190b_4d44;
|
|
50
|
+
|
|
51
|
+
/// Our advertised per-session flow-control limits. They match the QUIC transport
|
|
52
|
+
/// limits in `config.rs` because exactly one WebTransport session rides each QUIC
|
|
53
|
+
/// connection, so the session budget and the connection budget coincide.
|
|
54
|
+
pub const WT_INITIAL_MAX_DATA: u64 = 10 * 1024 * 1024;
|
|
55
|
+
pub const WT_INITIAL_MAX_STREAMS: u64 = 256;
|
|
31
56
|
|
|
32
57
|
/// Unidirectional WebTransport stream type prefix.
|
|
33
58
|
pub const WT_UNI_STREAM_TYPE: u64 = 0x54;
|
|
@@ -38,6 +63,51 @@ pub const WT_CLOSE_SESSION_CAPSULE: u64 = 0x2843;
|
|
|
38
63
|
/// `DRAIN_WEBTRANSPORT_SESSION` capsule type.
|
|
39
64
|
pub const WT_DRAIN_SESSION_CAPSULE: u64 = 0x78ae;
|
|
40
65
|
|
|
66
|
+
/// Maximum length in bytes of a `CLOSE_WEBTRANSPORT_SESSION` reason phrase.
|
|
67
|
+
pub const WT_MAX_CLOSE_REASON: usize = 1024;
|
|
68
|
+
|
|
69
|
+
/// First code of the HTTP/3 error range reserved for WebTransport application
|
|
70
|
+
/// errors (draft-ietf-webtrans-http3 §4.3).
|
|
71
|
+
pub const WT_APP_ERROR_FIRST: u64 = 0x52e4_a40f_a8db;
|
|
72
|
+
/// Last code of that range: `webtransport_code_to_http_code(0xffff_ffff)`.
|
|
73
|
+
pub const WT_APP_ERROR_LAST: u64 = 0x52e5_ac98_3162;
|
|
74
|
+
|
|
75
|
+
/// Map a 32-bit WebTransport application error code into the HTTP/3 error-code
|
|
76
|
+
/// space, skipping the HTTP/3 GREASE codepoints (`0x1f * N + 0x21`). This is the
|
|
77
|
+
/// code an endpoint MUST put in a QUIC RESET_STREAM / STOP_SENDING for a
|
|
78
|
+
/// WebTransport data stream.
|
|
79
|
+
pub fn webtransport_code_to_http_code(n: u64) -> u64 {
|
|
80
|
+
WT_APP_ERROR_FIRST + n + n / 0x1e
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// Inverse of [`webtransport_code_to_http_code`]. Returns `None` when `h` is
|
|
84
|
+
/// outside the reserved range or lands on a reserved GREASE codepoint, i.e. it
|
|
85
|
+
/// is not a valid WebTransport-mapped code.
|
|
86
|
+
pub fn http_code_to_webtransport_code(h: u64) -> Option<u64> {
|
|
87
|
+
if !(WT_APP_ERROR_FIRST..=WT_APP_ERROR_LAST).contains(&h) {
|
|
88
|
+
return None;
|
|
89
|
+
}
|
|
90
|
+
if h.wrapping_sub(0x21).is_multiple_of(0x1f) {
|
|
91
|
+
return None;
|
|
92
|
+
}
|
|
93
|
+
let shifted = h - WT_APP_ERROR_FIRST;
|
|
94
|
+
Some(shifted - shifted / 0x1f)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// Truncate a close reason to the longest valid UTF-8 prefix not exceeding
|
|
98
|
+
/// [`WT_MAX_CLOSE_REASON`] bytes, without splitting a multi-byte character.
|
|
99
|
+
pub fn truncate_close_reason(mut reason: Vec<u8>) -> Vec<u8> {
|
|
100
|
+
if reason.len() <= WT_MAX_CLOSE_REASON {
|
|
101
|
+
return reason;
|
|
102
|
+
}
|
|
103
|
+
let mut end = WT_MAX_CLOSE_REASON;
|
|
104
|
+
while end > 0 && (reason[end] & 0xc0) == 0x80 {
|
|
105
|
+
end -= 1;
|
|
106
|
+
}
|
|
107
|
+
reason.truncate(end);
|
|
108
|
+
reason
|
|
109
|
+
}
|
|
110
|
+
|
|
41
111
|
/// Largest QPACK header block we will decode from a peer.
|
|
42
112
|
pub const MAX_HEADER_BLOCK: u64 = 64 * 1024;
|
|
43
113
|
|
|
@@ -112,13 +182,16 @@ pub fn control_stream_prefix() -> Vec<u8> {
|
|
|
112
182
|
/// WebTransport capability settings).
|
|
113
183
|
pub fn settings_payload() -> Vec<u8> {
|
|
114
184
|
let mut p = Vec::new();
|
|
115
|
-
let pairs: [(u64, u64);
|
|
185
|
+
let pairs: [(u64, u64); 9] = [
|
|
116
186
|
(SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0),
|
|
117
187
|
(SETTINGS_QPACK_BLOCKED_STREAMS, 0),
|
|
118
188
|
(SETTINGS_ENABLE_CONNECT_PROTOCOL, 1),
|
|
119
189
|
(SETTINGS_H3_DATAGRAM, 1),
|
|
120
190
|
(SETTINGS_ENABLE_WEBTRANSPORT, 1),
|
|
121
191
|
(SETTINGS_WT_MAX_SESSIONS, 256),
|
|
192
|
+
(SETTINGS_WT_INITIAL_MAX_DATA, WT_INITIAL_MAX_DATA),
|
|
193
|
+
(SETTINGS_WT_INITIAL_MAX_STREAMS_BIDI, WT_INITIAL_MAX_STREAMS),
|
|
194
|
+
(SETTINGS_WT_INITIAL_MAX_STREAMS_UNI, WT_INITIAL_MAX_STREAMS),
|
|
122
195
|
];
|
|
123
196
|
for (id, val) in pairs {
|
|
124
197
|
put_varint(id, &mut p);
|
|
@@ -134,6 +207,12 @@ pub struct PeerSettings {
|
|
|
134
207
|
pub h3_datagram: bool,
|
|
135
208
|
pub enable_webtransport: bool,
|
|
136
209
|
pub wt_max_sessions: u64,
|
|
210
|
+
/// Peer's initial per-session send-data allowance (SETTINGS_WT_INITIAL_MAX_DATA).
|
|
211
|
+
pub wt_initial_max_data: u64,
|
|
212
|
+
/// Peer's initial per-session bidi stream limit.
|
|
213
|
+
pub wt_initial_max_streams_bidi: u64,
|
|
214
|
+
/// Peer's initial per-session uni stream limit.
|
|
215
|
+
pub wt_initial_max_streams_uni: u64,
|
|
137
216
|
}
|
|
138
217
|
|
|
139
218
|
impl PeerSettings {
|
|
@@ -160,6 +239,9 @@ pub fn parse_settings(mut payload: &[u8]) -> PeerSettings {
|
|
|
160
239
|
SETTINGS_H3_DATAGRAM => s.h3_datagram = val != 0,
|
|
161
240
|
SETTINGS_ENABLE_WEBTRANSPORT => s.enable_webtransport = val != 0,
|
|
162
241
|
SETTINGS_WT_MAX_SESSIONS => s.wt_max_sessions = val,
|
|
242
|
+
SETTINGS_WT_INITIAL_MAX_DATA => s.wt_initial_max_data = val,
|
|
243
|
+
SETTINGS_WT_INITIAL_MAX_STREAMS_BIDI => s.wt_initial_max_streams_bidi = val,
|
|
244
|
+
SETTINGS_WT_INITIAL_MAX_STREAMS_UNI => s.wt_initial_max_streams_uni = val,
|
|
163
245
|
_ => {}
|
|
164
246
|
}
|
|
165
247
|
payload = &payload[n1 + n2..];
|
|
@@ -167,12 +249,20 @@ pub fn parse_settings(mut payload: &[u8]) -> PeerSettings {
|
|
|
167
249
|
s
|
|
168
250
|
}
|
|
169
251
|
|
|
252
|
+
/// Request header carrying the client's offered WebTransport subprotocols
|
|
253
|
+
/// (draft-ietf-webtrans-http3), an RFC 9651 Structured-Field List of Strings.
|
|
254
|
+
pub const WT_AVAILABLE_PROTOCOLS: &[u8] = b"wt-available-protocols";
|
|
255
|
+
/// Response header carrying the server's selected subprotocol, an RFC 9651
|
|
256
|
+
/// Structured-Field Item String.
|
|
257
|
+
pub const WT_PROTOCOL: &[u8] = b"wt-protocol";
|
|
258
|
+
|
|
170
259
|
/// Build the Extended CONNECT request header list for a WebTransport session.
|
|
171
260
|
pub fn connect_headers(
|
|
172
261
|
authority: &str,
|
|
173
262
|
path: &str,
|
|
174
263
|
origin: Option<&str>,
|
|
175
264
|
extra: &[(String, String)],
|
|
265
|
+
protocols: &[String],
|
|
176
266
|
) -> Vec<Header> {
|
|
177
267
|
let mut h = vec![
|
|
178
268
|
Header::new(b":method", b"CONNECT"),
|
|
@@ -184,12 +274,103 @@ pub fn connect_headers(
|
|
|
184
274
|
if let Some(o) = origin {
|
|
185
275
|
h.push(Header::new(b"origin", o.as_bytes()));
|
|
186
276
|
}
|
|
277
|
+
if !protocols.is_empty() {
|
|
278
|
+
h.push(Header::new(
|
|
279
|
+
WT_AVAILABLE_PROTOCOLS,
|
|
280
|
+
serialize_sf_string_list(protocols).as_bytes(),
|
|
281
|
+
));
|
|
282
|
+
}
|
|
187
283
|
for (k, v) in extra {
|
|
188
284
|
h.push(Header::new(k.as_bytes(), v.as_bytes()));
|
|
189
285
|
}
|
|
190
286
|
h
|
|
191
287
|
}
|
|
192
288
|
|
|
289
|
+
/// Serialize one token as an RFC 9651 Structured-Field String (double-quoted,
|
|
290
|
+
/// with `"` and `\` backslash-escaped).
|
|
291
|
+
pub fn serialize_sf_string(s: &str) -> String {
|
|
292
|
+
let mut out = String::with_capacity(s.len() + 2);
|
|
293
|
+
out.push('"');
|
|
294
|
+
for c in s.chars() {
|
|
295
|
+
if c == '"' || c == '\\' {
|
|
296
|
+
out.push('\\');
|
|
297
|
+
}
|
|
298
|
+
out.push(c);
|
|
299
|
+
}
|
|
300
|
+
out.push('"');
|
|
301
|
+
out
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/// Serialize a list of tokens as an RFC 9651 Structured-Field List of Strings.
|
|
305
|
+
pub fn serialize_sf_string_list(items: &[String]) -> String {
|
|
306
|
+
items
|
|
307
|
+
.iter()
|
|
308
|
+
.map(|s| serialize_sf_string(s))
|
|
309
|
+
.collect::<Vec<_>>()
|
|
310
|
+
.join(", ")
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/// Parse a single RFC 9651 Structured-Field String item (a double-quoted,
|
|
314
|
+
/// backslash-escaped token). Returns `None` if it is not a well-formed sf-string.
|
|
315
|
+
pub fn parse_sf_string_item(value: &str) -> Option<String> {
|
|
316
|
+
let bytes = value.as_bytes();
|
|
317
|
+
if bytes.len() < 2 || bytes[0] != b'"' {
|
|
318
|
+
return None;
|
|
319
|
+
}
|
|
320
|
+
let mut out = String::new();
|
|
321
|
+
let mut i = 1;
|
|
322
|
+
while i < bytes.len() {
|
|
323
|
+
match bytes[i] {
|
|
324
|
+
b'\\' => {
|
|
325
|
+
i += 1;
|
|
326
|
+
match bytes.get(i) {
|
|
327
|
+
Some(&e) if e == b'"' || e == b'\\' => out.push(e as char),
|
|
328
|
+
_ => return None,
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
b'"' => return (i == bytes.len() - 1).then_some(out),
|
|
332
|
+
b @ 0x20..=0x7e => out.push(b as char),
|
|
333
|
+
_ => return None,
|
|
334
|
+
}
|
|
335
|
+
i += 1;
|
|
336
|
+
}
|
|
337
|
+
None
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/// Parse an RFC 9651 Structured-Field List of Strings into tokens, tolerating
|
|
341
|
+
/// surrounding whitespace and skipping malformed items.
|
|
342
|
+
pub fn parse_sf_string_list(value: &str) -> Vec<String> {
|
|
343
|
+
value
|
|
344
|
+
.split(',')
|
|
345
|
+
.filter_map(|part| parse_sf_string_item(part.trim()))
|
|
346
|
+
.collect()
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/// Find the first header value for a (lowercase) name.
|
|
350
|
+
pub fn header_value<'a>(headers: &'a [Header], name: &[u8]) -> Option<&'a [u8]> {
|
|
351
|
+
headers.iter().find(|h| h.name() == name).map(|h| h.value())
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/// Parse the `wt-protocol` response header into the selected subprotocol token.
|
|
355
|
+
pub fn wt_protocol_of(headers: &[Header]) -> Option<String> {
|
|
356
|
+
let v = header_value(headers, WT_PROTOCOL)?;
|
|
357
|
+
parse_sf_string_item(std::str::from_utf8(v).ok()?.trim())
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/// Collect the non-pseudo response headers as owned UTF-8 (name, value) pairs,
|
|
361
|
+
/// skipping any header whose name or value is not valid UTF-8.
|
|
362
|
+
pub fn response_headers(headers: &[Header]) -> Vec<(String, String)> {
|
|
363
|
+
headers
|
|
364
|
+
.iter()
|
|
365
|
+
.filter(|h| !h.name().starts_with(b":"))
|
|
366
|
+
.filter_map(|h| {
|
|
367
|
+
let n = std::str::from_utf8(h.name()).ok()?;
|
|
368
|
+
let v = std::str::from_utf8(h.value()).ok()?;
|
|
369
|
+
Some((n.to_string(), v.to_string()))
|
|
370
|
+
})
|
|
371
|
+
.collect()
|
|
372
|
+
}
|
|
373
|
+
|
|
193
374
|
/// QPACK-encode a header list and wrap it in an HTTP/3 HEADERS frame.
|
|
194
375
|
pub fn encode_headers_frame(headers: &[Header]) -> Result<Vec<u8>, String> {
|
|
195
376
|
// Upper bound: static-only encoding never exceeds name+value+8 per field,
|
|
@@ -224,3 +405,124 @@ pub fn status_of(headers: &[Header]) -> Option<u16> {
|
|
|
224
405
|
}
|
|
225
406
|
None
|
|
226
407
|
}
|
|
408
|
+
|
|
409
|
+
#[cfg(test)]
|
|
410
|
+
mod tests {
|
|
411
|
+
use super::*;
|
|
412
|
+
|
|
413
|
+
#[test]
|
|
414
|
+
fn error_code_mapping_roundtrips() {
|
|
415
|
+
assert_eq!(webtransport_code_to_http_code(0), WT_APP_ERROR_FIRST);
|
|
416
|
+
assert_eq!(
|
|
417
|
+
webtransport_code_to_http_code(0xffff_ffff),
|
|
418
|
+
WT_APP_ERROR_LAST
|
|
419
|
+
);
|
|
420
|
+
for n in [
|
|
421
|
+
0u64,
|
|
422
|
+
1,
|
|
423
|
+
29,
|
|
424
|
+
30,
|
|
425
|
+
31,
|
|
426
|
+
100,
|
|
427
|
+
1000,
|
|
428
|
+
0x1e,
|
|
429
|
+
0x1f,
|
|
430
|
+
65_535,
|
|
431
|
+
0xffff_ffff,
|
|
432
|
+
] {
|
|
433
|
+
let h = webtransport_code_to_http_code(n);
|
|
434
|
+
assert!((WT_APP_ERROR_FIRST..=WT_APP_ERROR_LAST).contains(&h));
|
|
435
|
+
assert_eq!(http_code_to_webtransport_code(h), Some(n), "n={n} h={h:#x}");
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
#[test]
|
|
440
|
+
fn mapping_never_lands_on_a_grease_codepoint() {
|
|
441
|
+
for n in 0u64..3000 {
|
|
442
|
+
let h = webtransport_code_to_http_code(n);
|
|
443
|
+
assert_ne!(
|
|
444
|
+
h.wrapping_sub(0x21) % 0x1f,
|
|
445
|
+
0,
|
|
446
|
+
"n={n} mapped onto a GREASE code"
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
#[test]
|
|
452
|
+
fn out_of_range_and_grease_have_no_inverse() {
|
|
453
|
+
assert_eq!(http_code_to_webtransport_code(WT_APP_ERROR_FIRST - 1), None);
|
|
454
|
+
assert_eq!(http_code_to_webtransport_code(WT_APP_ERROR_LAST + 1), None);
|
|
455
|
+
let mut grease = WT_APP_ERROR_FIRST;
|
|
456
|
+
while !grease.wrapping_sub(0x21).is_multiple_of(0x1f) {
|
|
457
|
+
grease += 1;
|
|
458
|
+
}
|
|
459
|
+
assert!(grease <= WT_APP_ERROR_LAST);
|
|
460
|
+
assert_eq!(http_code_to_webtransport_code(grease), None);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
#[test]
|
|
464
|
+
fn reason_truncation_keeps_utf8_boundary() {
|
|
465
|
+
assert_eq!(truncate_close_reason(b"hi".to_vec()), b"hi");
|
|
466
|
+
// 3-byte chars so 1024 does not fall on a boundary; must back up.
|
|
467
|
+
let big = "\u{20ac}".repeat(500).into_bytes(); // 1500 bytes
|
|
468
|
+
let out = truncate_close_reason(big);
|
|
469
|
+
assert!(out.len() <= WT_MAX_CLOSE_REASON);
|
|
470
|
+
assert!(
|
|
471
|
+
std::str::from_utf8(&out).is_ok(),
|
|
472
|
+
"truncation split a character"
|
|
473
|
+
);
|
|
474
|
+
assert_eq!(out.len(), 1023); // 341 * 3
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
#[test]
|
|
478
|
+
fn settings_advertise_and_parse_wt_flow_control() {
|
|
479
|
+
let s = parse_settings(&settings_payload());
|
|
480
|
+
assert!(s.webtransport_ok());
|
|
481
|
+
assert_eq!(s.wt_max_sessions, 256);
|
|
482
|
+
assert_eq!(s.wt_initial_max_data, WT_INITIAL_MAX_DATA);
|
|
483
|
+
assert_eq!(s.wt_initial_max_streams_bidi, WT_INITIAL_MAX_STREAMS);
|
|
484
|
+
assert_eq!(s.wt_initial_max_streams_uni, WT_INITIAL_MAX_STREAMS);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
#[test]
|
|
488
|
+
fn sf_string_list_roundtrips() {
|
|
489
|
+
let items = vec!["chat".to_string(), "chat-v2".to_string()];
|
|
490
|
+
let ser = serialize_sf_string_list(&items);
|
|
491
|
+
assert_eq!(ser, "\"chat\", \"chat-v2\"");
|
|
492
|
+
assert_eq!(parse_sf_string_list(&ser), items);
|
|
493
|
+
// Empty list serializes to empty and parses back to empty.
|
|
494
|
+
assert_eq!(serialize_sf_string_list(&[]), "");
|
|
495
|
+
assert!(parse_sf_string_list("").is_empty());
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
#[test]
|
|
499
|
+
fn sf_string_escapes_quote_and_backslash() {
|
|
500
|
+
let items = vec!["a\"b\\c".to_string()];
|
|
501
|
+
let ser = serialize_sf_string_list(&items);
|
|
502
|
+
assert_eq!(ser, "\"a\\\"b\\\\c\"");
|
|
503
|
+
assert_eq!(parse_sf_string_list(&ser), items);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
#[test]
|
|
507
|
+
fn sf_string_item_rejects_malformed() {
|
|
508
|
+
assert_eq!(parse_sf_string_item("chat"), None); // unquoted
|
|
509
|
+
assert_eq!(parse_sf_string_item("\"chat"), None); // no closing quote
|
|
510
|
+
assert_eq!(parse_sf_string_item("\"a\"b\""), None); // trailing junk
|
|
511
|
+
assert_eq!(parse_sf_string_item("\"a\\x\""), None); // bad escape
|
|
512
|
+
assert_eq!(parse_sf_string_item("\"ok\""), Some("ok".to_string()));
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
#[test]
|
|
516
|
+
fn wt_protocol_and_response_headers_from_list() {
|
|
517
|
+
let headers = vec![
|
|
518
|
+
Header::new(b":status", b"200"),
|
|
519
|
+
Header::new(WT_PROTOCOL, b"\"chat-v2\""),
|
|
520
|
+
Header::new(b"x-app", b"hello"),
|
|
521
|
+
];
|
|
522
|
+
assert_eq!(wt_protocol_of(&headers), Some("chat-v2".to_string()));
|
|
523
|
+
let rh = response_headers(&headers);
|
|
524
|
+
assert!(rh.contains(&("wt-protocol".to_string(), "\"chat-v2\"".to_string())));
|
|
525
|
+
assert!(rh.contains(&("x-app".to_string(), "hello".to_string())));
|
|
526
|
+
assert!(!rh.iter().any(|(n, _)| n == ":status"));
|
|
527
|
+
}
|
|
528
|
+
}
|
package/crates/wtcore/src/lib.rs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
//! Shared WebTransport-over-HTTP/3 protocol primitives.
|
|
3
3
|
//!
|
|
4
4
|
//! Framing, SETTINGS, the Extended CONNECT header codec, and the WebTransport
|
|
5
|
-
//! stream/capsule constants
|
|
5
|
+
//! stream/capsule constants: everything both the client (`rwebtransport-native`)
|
|
6
6
|
//! and the test echo server (`rwebtransport-echo-server`) need on top of quiche's
|
|
7
7
|
//! raw QUIC streams. Header (de)compression reuses `quiche::h3::qpack`.
|
|
8
8
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { WebTransport, WebTransportSession } from './webtransport.js';
|
|
2
2
|
export { WebTransportServer, WebTransportServerSession, type WebTransportServerOptions, type WebTransportSessionRequest, } from './server.js';
|
|
3
3
|
export { WebTransportError } from './errors.js';
|
|
4
|
-
export { WebTransportBidirectionalStream, WebTransportReceiveStream, WebTransportSendStream, } from './streams.js';
|
|
4
|
+
export { WebTransportBidirectionalStream, WebTransportReceiveStream, WebTransportSendStream, WebTransportSendGroup, type WebTransportSendStreamOptions, } from './streams.js';
|
|
5
5
|
export { WebTransportDatagramDuplexStream } from './datagrams.js';
|
|
6
|
-
export type { WebTransportOptions, WebTransportHash, WebTransportCongestionControl, WebTransportCloseInfo, WebTransportCloseOptions, BinarySource, } from './types.js';
|
|
6
|
+
export type { WebTransportOptions, WebTransportHash, WebTransportCongestionControl, WebTransportReliabilityMode, WebTransportConnectionStats, WebTransportDatagramStats, WebTransportCloseInfo, WebTransportCloseOptions, BinarySource, } from './types.js';
|
|
7
7
|
export type { WebTransportErrorSource, WebTransportErrorOptions } from './errors.js';
|