rwebtransport 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +666 -0
- package/Cargo.toml +31 -0
- package/LICENSE +202 -0
- package/NOTICE +41 -0
- package/README.md +550 -0
- package/crates/echo-server/Cargo.toml +20 -0
- package/crates/echo-server/src/main.rs +666 -0
- package/crates/native/Cargo.toml +23 -0
- package/crates/native/src/config.rs +129 -0
- package/crates/native/src/driver.rs +463 -0
- package/crates/native/src/lib.rs +821 -0
- package/crates/native/src/server.rs +718 -0
- package/crates/native/src/session.rs +1191 -0
- package/crates/native/src/tls.rs +96 -0
- package/crates/wtcore/Cargo.toml +15 -0
- package/crates/wtcore/src/h3.rs +226 -0
- package/crates/wtcore/src/lib.rs +9 -0
- package/dist/datagrams.d.ts +16 -0
- package/dist/errors.d.ts +10 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +1512 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1473 -0
- package/dist/index.mjs.map +1 -0
- package/dist/loader.d.ts +2 -0
- package/dist/native.d.ts +226 -0
- package/dist/server.d.ts +44 -0
- package/dist/streams.d.ts +14 -0
- package/dist/types.d.ts +24 -0
- package/dist/webtransport.d.ts +20 -0
- package/package.json +79 -0
- package/prebuilds/win32-x64/rwebtransport.node +0 -0
- package/scripts/bench.mjs +180 -0
- package/scripts/build.js +67 -0
- package/scripts/postinstall.js +67 -0
- package/scripts/setup-unix.sh +129 -0
- package/scripts/setup-windows.ps1 +229 -0
- package/vendor/quiche/AGENTS.md +75 -0
- package/vendor/quiche/COPYING +23 -0
- package/vendor/quiche/Cargo.lock +1212 -0
- package/vendor/quiche/Cargo.toml +215 -0
- package/vendor/quiche/README.md +466 -0
- package/vendor/quiche/examples/Makefile +46 -0
- package/vendor/quiche/examples/README.md +20 -0
- package/vendor/quiche/examples/cert-big.crt +90 -0
- package/vendor/quiche/examples/cert.crt +18 -0
- package/vendor/quiche/examples/cert.key +28 -0
- package/vendor/quiche/examples/client.c +343 -0
- package/vendor/quiche/examples/client.rs +281 -0
- package/vendor/quiche/examples/gen-certs.sh +15 -0
- package/vendor/quiche/examples/http3-client.c +473 -0
- package/vendor/quiche/examples/http3-client.rs +349 -0
- package/vendor/quiche/examples/http3-server.c +614 -0
- package/vendor/quiche/examples/http3-server.rs +680 -0
- package/vendor/quiche/examples/qpack-decode.rs +87 -0
- package/vendor/quiche/examples/qpack-encode.rs +88 -0
- package/vendor/quiche/examples/rootca.crt +21 -0
- package/vendor/quiche/examples/server.c +529 -0
- package/vendor/quiche/examples/server.rs +529 -0
- package/vendor/quiche/include/quiche.h +1282 -0
- package/vendor/quiche/quiche.svg +1 -0
- package/vendor/quiche/src/buffers.rs +105 -0
- package/vendor/quiche/src/build.rs +66 -0
- package/vendor/quiche/src/cid.rs +1079 -0
- package/vendor/quiche/src/crypto/boringssl.rs +373 -0
- package/vendor/quiche/src/crypto/mod.rs +671 -0
- package/vendor/quiche/src/dgram.rs +118 -0
- package/vendor/quiche/src/error.rs +263 -0
- package/vendor/quiche/src/ffi.rs +2290 -0
- package/vendor/quiche/src/flowcontrol.rs +246 -0
- package/vendor/quiche/src/frame.rs +2172 -0
- package/vendor/quiche/src/h3/AGENTS.md +54 -0
- package/vendor/quiche/src/h3/ffi.rs +442 -0
- package/vendor/quiche/src/h3/frame.rs +1404 -0
- package/vendor/quiche/src/h3/mod.rs +8223 -0
- package/vendor/quiche/src/h3/qpack/decoder.rs +288 -0
- package/vendor/quiche/src/h3/qpack/encoder.rs +235 -0
- package/vendor/quiche/src/h3/qpack/mod.rs +202 -0
- package/vendor/quiche/src/h3/qpack/static_table.rs +323 -0
- package/vendor/quiche/src/h3/stream.rs +1595 -0
- package/vendor/quiche/src/lib.rs +9556 -0
- package/vendor/quiche/src/minmax.rs +446 -0
- package/vendor/quiche/src/packet.rs +2334 -0
- package/vendor/quiche/src/path.rs +1374 -0
- package/vendor/quiche/src/pmtud.rs +652 -0
- package/vendor/quiche/src/rand.rs +64 -0
- package/vendor/quiche/src/range_buf.rs +186 -0
- package/vendor/quiche/src/ranges.rs +755 -0
- package/vendor/quiche/src/recovery/AGENTS.md +80 -0
- package/vendor/quiche/src/recovery/bandwidth.rs +565 -0
- package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -0
- package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -0
- package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -0
- package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -0
- package/vendor/quiche/src/recovery/congestion/mod.rs +361 -0
- package/vendor/quiche/src/recovery/congestion/prr.rs +238 -0
- package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -0
- package/vendor/quiche/src/recovery/congestion/reno.rs +291 -0
- package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -0
- package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -0
- package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -0
- package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -0
- package/vendor/quiche/src/recovery/mod.rs +3086 -0
- package/vendor/quiche/src/recovery/rtt.rs +152 -0
- package/vendor/quiche/src/stream/mod.rs +2210 -0
- package/vendor/quiche/src/stream/recv_buf.rs +1236 -0
- package/vendor/quiche/src/stream/send_buf.rs +849 -0
- package/vendor/quiche/src/test_utils.rs +681 -0
- package/vendor/quiche/src/tests.rs +12922 -0
- package/vendor/quiche/src/tls/boringssl.rs +375 -0
- package/vendor/quiche/src/tls/mod.rs +1245 -0
- package/vendor/quiche/src/transport_params.rs +628 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
//! The default "Google Chrome" TLS profile for the QUIC (TLS 1.3) handshake.
|
|
3
|
+
//!
|
|
4
|
+
//! QUIC mandates TLS 1.3, so — unlike a TCP/TLS stack — there is no TLS 1.2
|
|
5
|
+
//! cipher list to negotiate. What still shapes the ClientHello, and what servers
|
|
6
|
+
//! (Cloudflare in particular) fingerprint, is the group preference, the
|
|
7
|
+
//! signature-algorithm list, GREASE, and extension ordering. We mirror Chrome's
|
|
8
|
+
//! choices so the handshake is browser-indistinguishable. The TLS 1.3
|
|
9
|
+
//! cipher-suite preference order is fixed inside BoringSSL and already matches
|
|
10
|
+
//! Chrome (AES-128-GCM, AES-256-GCM, ChaCha20-Poly1305).
|
|
11
|
+
|
|
12
|
+
use boring::hash::MessageDigest;
|
|
13
|
+
use boring::ssl::{
|
|
14
|
+
SslAlert, SslContextBuilder, SslMethod, SslVerifyError, SslVerifyMode, SslVersion,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/// How the server certificate is verified.
|
|
18
|
+
#[derive(Clone, Debug)]
|
|
19
|
+
pub enum CertVerification {
|
|
20
|
+
/// Full PKI validation against the system trust store, with hostname
|
|
21
|
+
/// checking (the default). The `verify_peer(true)` + root loading half is
|
|
22
|
+
/// applied on the quiche `Config` in [`crate::config`]; nothing is set on
|
|
23
|
+
/// the SSL context here.
|
|
24
|
+
PkiDefault,
|
|
25
|
+
/// Accept the server certificate iff its SHA-256 fingerprint matches one of
|
|
26
|
+
/// these. This mirrors the WebTransport `serverCertificateHashes` option:
|
|
27
|
+
/// no CA chain and no hostname check, just a pinned leaf fingerprint.
|
|
28
|
+
Hashes(Vec<[u8; 32]>),
|
|
29
|
+
/// Accept any certificate. Development only.
|
|
30
|
+
Insecure,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// Chrome's TLS 1.3 signature-algorithm preference.
|
|
34
|
+
const CHROME_SIGALGS: &str = "ecdsa_secp256r1_sha256:\
|
|
35
|
+
rsa_pss_rsae_sha256:\
|
|
36
|
+
rsa_pkcs1_sha256:\
|
|
37
|
+
ecdsa_secp384r1_sha384:\
|
|
38
|
+
rsa_pss_rsae_sha384:\
|
|
39
|
+
rsa_pkcs1_sha384:\
|
|
40
|
+
rsa_pss_rsae_sha512:\
|
|
41
|
+
rsa_pkcs1_sha512:\
|
|
42
|
+
rsa_pkcs1_sha1";
|
|
43
|
+
|
|
44
|
+
/// Build a BoringSSL client context configured with the Chrome TLS profile and
|
|
45
|
+
/// the requested certificate-verification policy.
|
|
46
|
+
pub fn build_client_tls(verify: &CertVerification) -> Result<SslContextBuilder, String> {
|
|
47
|
+
let mut b =
|
|
48
|
+
SslContextBuilder::new(SslMethod::tls()).map_err(|e| format!("SslContextBuilder: {e}"))?;
|
|
49
|
+
|
|
50
|
+
// QUIC mandates TLS 1.3.
|
|
51
|
+
b.set_min_proto_version(Some(SslVersion::TLS1_3))
|
|
52
|
+
.map_err(|e| format!("set_min_proto_version: {e}"))?;
|
|
53
|
+
b.set_max_proto_version(Some(SslVersion::TLS1_3))
|
|
54
|
+
.map_err(|e| format!("set_max_proto_version: {e}"))?;
|
|
55
|
+
|
|
56
|
+
// Chrome's group (named curve) preference.
|
|
57
|
+
b.set_curves_list("X25519:P-256:P-384")
|
|
58
|
+
.map_err(|e| format!("set_curves_list: {e}"))?;
|
|
59
|
+
|
|
60
|
+
// Chrome's signature-algorithm list.
|
|
61
|
+
b.set_sigalgs_list(CHROME_SIGALGS)
|
|
62
|
+
.map_err(|e| format!("set_sigalgs_list: {e}"))?;
|
|
63
|
+
|
|
64
|
+
// Browser-like ClientHello shape: GREASE values and permuted extensions.
|
|
65
|
+
b.set_grease_enabled(true);
|
|
66
|
+
b.set_permute_extensions(true);
|
|
67
|
+
|
|
68
|
+
match verify {
|
|
69
|
+
CertVerification::PkiDefault => {
|
|
70
|
+
// Applied on the quiche Config (verify_peer + system roots). Leaving
|
|
71
|
+
// the SSL context untouched keeps quiche's built-in verification
|
|
72
|
+
// (including the hostname check wired by `set_host_name`).
|
|
73
|
+
}
|
|
74
|
+
CertVerification::Insecure => {
|
|
75
|
+
b.set_verify(SslVerifyMode::NONE);
|
|
76
|
+
}
|
|
77
|
+
CertVerification::Hashes(hashes) => {
|
|
78
|
+
let hashes = hashes.clone();
|
|
79
|
+
b.set_custom_verify_callback(SslVerifyMode::PEER, move |ssl| {
|
|
80
|
+
let cert = ssl
|
|
81
|
+
.peer_certificate()
|
|
82
|
+
.ok_or(SslVerifyError::Invalid(SslAlert::CERTIFICATE_UNKNOWN))?;
|
|
83
|
+
let digest = cert
|
|
84
|
+
.digest(MessageDigest::sha256())
|
|
85
|
+
.map_err(|_| SslVerifyError::Invalid(SslAlert::CERTIFICATE_UNKNOWN))?;
|
|
86
|
+
if hashes.iter().any(|h| digest.as_ref() == h.as_slice()) {
|
|
87
|
+
Ok(())
|
|
88
|
+
} else {
|
|
89
|
+
Err(SslVerifyError::Invalid(SslAlert::BAD_CERTIFICATE))
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
Ok(b)
|
|
96
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
[package]
|
|
3
|
+
name = "rwebtransport-wtcore"
|
|
4
|
+
version.workspace = true
|
|
5
|
+
edition.workspace = true
|
|
6
|
+
license.workspace = true
|
|
7
|
+
publish = false
|
|
8
|
+
|
|
9
|
+
[lib]
|
|
10
|
+
name = "wtcore"
|
|
11
|
+
path = "src/lib.rs"
|
|
12
|
+
|
|
13
|
+
[dependencies]
|
|
14
|
+
quiche.workspace = true
|
|
15
|
+
octets.workspace = true
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SessionCore } from './native.js';
|
|
2
|
+
export declare class WebTransportDatagramDuplexStream {
|
|
3
|
+
private readonly session;
|
|
4
|
+
readonly readable: ReadableStream<Uint8Array>;
|
|
5
|
+
readonly writable: WritableStream<Uint8Array>;
|
|
6
|
+
private incomingHwm;
|
|
7
|
+
private outgoingHwm;
|
|
8
|
+
incomingMaxAge: number | null;
|
|
9
|
+
outgoingMaxAge: number | null;
|
|
10
|
+
constructor(session: SessionCore);
|
|
11
|
+
get maxDatagramSize(): number;
|
|
12
|
+
get incomingHighWaterMark(): number;
|
|
13
|
+
set incomingHighWaterMark(value: number);
|
|
14
|
+
get outgoingHighWaterMark(): number;
|
|
15
|
+
set outgoingHighWaterMark(value: number);
|
|
16
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type WebTransportErrorSource = 'stream' | 'session';
|
|
2
|
+
export interface WebTransportErrorOptions {
|
|
3
|
+
source?: WebTransportErrorSource;
|
|
4
|
+
streamErrorCode?: number | null;
|
|
5
|
+
}
|
|
6
|
+
export declare class WebTransportError extends Error {
|
|
7
|
+
readonly source: WebTransportErrorSource;
|
|
8
|
+
readonly streamErrorCode: number | null;
|
|
9
|
+
constructor(message?: string, options?: WebTransportErrorOptions);
|
|
10
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { WebTransport, WebTransportSession } from './webtransport.js';
|
|
2
|
+
export { WebTransportServer, WebTransportServerSession, type WebTransportServerOptions, type WebTransportSessionRequest, } from './server.js';
|
|
3
|
+
export { WebTransportError } from './errors.js';
|
|
4
|
+
export { WebTransportBidirectionalStream, WebTransportReceiveStream, WebTransportSendStream, } from './streams.js';
|
|
5
|
+
export { WebTransportDatagramDuplexStream } from './datagrams.js';
|
|
6
|
+
export type { WebTransportOptions, WebTransportHash, WebTransportCongestionControl, WebTransportCloseInfo, WebTransportCloseOptions, BinarySource, } from './types.js';
|
|
7
|
+
export type { WebTransportErrorSource, WebTransportErrorOptions } from './errors.js';
|