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,288 +1,288 @@
1
- // Copyright (C) 2019, 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
- use super::Error;
28
- use super::Result;
29
-
30
- use crate::h3::Header;
31
-
32
- use super::INDEXED;
33
- use super::INDEXED_WITH_POST_BASE;
34
- use super::LITERAL;
35
- use super::LITERAL_WITH_NAME_REF;
36
-
37
- #[derive(Clone, Copy, Debug, PartialEq)]
38
- enum Representation {
39
- Indexed,
40
- IndexedWithPostBase,
41
- Literal,
42
- LiteralWithNameRef,
43
- LiteralWithPostBase,
44
- }
45
-
46
- impl Representation {
47
- pub fn from_byte(b: u8) -> Representation {
48
- if b & INDEXED == INDEXED {
49
- return Representation::Indexed;
50
- }
51
-
52
- if b & LITERAL_WITH_NAME_REF == LITERAL_WITH_NAME_REF {
53
- return Representation::LiteralWithNameRef;
54
- }
55
-
56
- if b & LITERAL == LITERAL {
57
- return Representation::Literal;
58
- }
59
-
60
- if b & INDEXED_WITH_POST_BASE == INDEXED_WITH_POST_BASE {
61
- return Representation::IndexedWithPostBase;
62
- }
63
-
64
- Representation::LiteralWithPostBase
65
- }
66
- }
67
-
68
- /// A QPACK decoder.
69
- #[derive(Default)]
70
- pub struct Decoder {}
71
-
72
- impl Decoder {
73
- /// Creates a new QPACK decoder.
74
- pub fn new() -> Decoder {
75
- Decoder::default()
76
- }
77
-
78
- /// Processes control instructions from the encoder.
79
- pub fn control(&mut self, _buf: &mut [u8]) -> Result<()> {
80
- // TODO: process control instructions
81
- Ok(())
82
- }
83
-
84
- /// Decodes a QPACK header block into a list of headers.
85
- pub fn decode(&mut self, buf: &[u8], max_size: u64) -> Result<Vec<Header>> {
86
- let mut b = octets::Octets::with_slice(buf);
87
-
88
- let mut out = Vec::new();
89
-
90
- let mut left = max_size;
91
-
92
- let req_insert_count = decode_int(&mut b, 8)?;
93
- let base = decode_int(&mut b, 7)?;
94
-
95
- trace!("Header count={req_insert_count} base={base}");
96
-
97
- while b.cap() > 0 {
98
- let first = b.peek_u8()?;
99
-
100
- match Representation::from_byte(first) {
101
- Representation::Indexed => {
102
- const STATIC: u8 = 0x40;
103
-
104
- let s = first & STATIC == STATIC;
105
- let index = decode_int(&mut b, 6)?;
106
-
107
- trace!("Indexed index={index} static={s}");
108
-
109
- if !s {
110
- // TODO: implement dynamic table
111
- return Err(Error::InvalidHeaderValue);
112
- }
113
-
114
- let (name, value) = lookup_static(index)?;
115
-
116
- left = left
117
- .checked_sub((name.len() + value.len()) as u64)
118
- .ok_or(Error::HeaderListTooLarge)?;
119
-
120
- let hdr = Header::new(name, value);
121
- out.push(hdr);
122
- },
123
-
124
- Representation::IndexedWithPostBase => {
125
- let index = decode_int(&mut b, 4)?;
126
-
127
- trace!("Indexed With Post Base index={index}");
128
-
129
- // TODO: implement dynamic table
130
- return Err(Error::InvalidHeaderValue);
131
- },
132
-
133
- Representation::Literal => {
134
- let name_huff = b.as_ref()[0] & 0x08 == 0x08;
135
- let name_len = decode_int(&mut b, 3)? as usize;
136
-
137
- let mut name = b.get_bytes(name_len)?;
138
-
139
- let name = if name_huff {
140
- name.get_huffman_decoded()?
141
- } else {
142
- name.to_vec()
143
- };
144
-
145
- let name = name.to_vec();
146
- let value = decode_str(&mut b)?;
147
-
148
- trace!(
149
- "Literal Without Name Reference name={name:?} value={value:?}",
150
- );
151
-
152
- left = left
153
- .checked_sub((name.len() + value.len()) as u64)
154
- .ok_or(Error::HeaderListTooLarge)?;
155
-
156
- // Instead of calling Header::new(), create Header directly
157
- // from `name` and `value`, which are already String.
158
- let hdr = Header(name, value);
159
- out.push(hdr);
160
- },
161
-
162
- Representation::LiteralWithNameRef => {
163
- const STATIC: u8 = 0x10;
164
-
165
- let s = first & STATIC == STATIC;
166
- let name_idx = decode_int(&mut b, 4)?;
167
- let value = decode_str(&mut b)?;
168
-
169
- trace!(
170
- "Literal name_idx={name_idx} static={s} value={value:?}"
171
- );
172
-
173
- if !s {
174
- // TODO: implement dynamic table
175
- return Err(Error::InvalidHeaderValue);
176
- }
177
-
178
- let (name, _) = lookup_static(name_idx)?;
179
-
180
- left = left
181
- .checked_sub((name.len() + value.len()) as u64)
182
- .ok_or(Error::HeaderListTooLarge)?;
183
-
184
- // Instead of calling Header::new(), create Header directly
185
- // from `value`, which is already String, but clone `name`
186
- // as it is just a reference.
187
- let hdr = Header(name.to_vec(), value);
188
- out.push(hdr);
189
- },
190
-
191
- Representation::LiteralWithPostBase => {
192
- trace!("Literal With Post Base");
193
-
194
- // TODO: implement dynamic table
195
- return Err(Error::InvalidHeaderValue);
196
- },
197
- }
198
- }
199
-
200
- Ok(out)
201
- }
202
- }
203
-
204
- fn lookup_static(idx: u64) -> Result<(&'static [u8], &'static [u8])> {
205
- if idx >= super::static_table::STATIC_DECODE_TABLE.len() as u64 {
206
- return Err(Error::InvalidStaticTableIndex);
207
- }
208
-
209
- Ok(super::static_table::STATIC_DECODE_TABLE[idx as usize])
210
- }
211
-
212
- fn decode_int(b: &mut octets::Octets, prefix: usize) -> Result<u64> {
213
- let mask = 2u64.pow(prefix as u32) - 1;
214
-
215
- let mut val = u64::from(b.get_u8()?);
216
- val &= mask;
217
-
218
- if val < mask {
219
- return Ok(val);
220
- }
221
-
222
- let mut shift = 0;
223
-
224
- while b.cap() > 0 {
225
- let byte = b.get_u8()?;
226
-
227
- let inc = u64::from(byte & 0x7f)
228
- .checked_shl(shift)
229
- .ok_or(Error::BufferTooShort)?;
230
-
231
- val = val.checked_add(inc).ok_or(Error::BufferTooShort)?;
232
-
233
- shift += 7;
234
-
235
- if byte & 0x80 == 0 {
236
- return Ok(val);
237
- }
238
- }
239
-
240
- Err(Error::BufferTooShort)
241
- }
242
-
243
- fn decode_str(b: &mut octets::Octets) -> Result<Vec<u8>> {
244
- let first = b.peek_u8()?;
245
-
246
- let huff = first & 0x80 == 0x80;
247
-
248
- let len = decode_int(b, 7)? as usize;
249
-
250
- let mut val = b.get_bytes(len)?;
251
-
252
- let val = if huff {
253
- val.get_huffman_decoded()?
254
- } else {
255
- val.to_vec()
256
- };
257
-
258
- Ok(val)
259
- }
260
-
261
- #[cfg(test)]
262
- mod tests {
263
- use super::*;
264
-
265
- #[test]
266
- fn decode_int1() {
267
- let encoded = [0b01010, 0x02];
268
- let mut b = octets::Octets::with_slice(&encoded);
269
-
270
- assert_eq!(decode_int(&mut b, 5), Ok(10));
271
- }
272
-
273
- #[test]
274
- fn decode_int2() {
275
- let encoded = [0b11111, 0b10011010, 0b00001010];
276
- let mut b = octets::Octets::with_slice(&encoded);
277
-
278
- assert_eq!(decode_int(&mut b, 5), Ok(1337));
279
- }
280
-
281
- #[test]
282
- fn decode_int3() {
283
- let encoded = [0b101010];
284
- let mut b = octets::Octets::with_slice(&encoded);
285
-
286
- assert_eq!(decode_int(&mut b, 8), Ok(42));
287
- }
288
- }
1
+ // Copyright (C) 2019, 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
+ use super::Error;
28
+ use super::Result;
29
+
30
+ use crate::h3::Header;
31
+
32
+ use super::INDEXED;
33
+ use super::INDEXED_WITH_POST_BASE;
34
+ use super::LITERAL;
35
+ use super::LITERAL_WITH_NAME_REF;
36
+
37
+ #[derive(Clone, Copy, Debug, PartialEq)]
38
+ enum Representation {
39
+ Indexed,
40
+ IndexedWithPostBase,
41
+ Literal,
42
+ LiteralWithNameRef,
43
+ LiteralWithPostBase,
44
+ }
45
+
46
+ impl Representation {
47
+ pub fn from_byte(b: u8) -> Representation {
48
+ if b & INDEXED == INDEXED {
49
+ return Representation::Indexed;
50
+ }
51
+
52
+ if b & LITERAL_WITH_NAME_REF == LITERAL_WITH_NAME_REF {
53
+ return Representation::LiteralWithNameRef;
54
+ }
55
+
56
+ if b & LITERAL == LITERAL {
57
+ return Representation::Literal;
58
+ }
59
+
60
+ if b & INDEXED_WITH_POST_BASE == INDEXED_WITH_POST_BASE {
61
+ return Representation::IndexedWithPostBase;
62
+ }
63
+
64
+ Representation::LiteralWithPostBase
65
+ }
66
+ }
67
+
68
+ /// A QPACK decoder.
69
+ #[derive(Default)]
70
+ pub struct Decoder {}
71
+
72
+ impl Decoder {
73
+ /// Creates a new QPACK decoder.
74
+ pub fn new() -> Decoder {
75
+ Decoder::default()
76
+ }
77
+
78
+ /// Processes control instructions from the encoder.
79
+ pub fn control(&mut self, _buf: &mut [u8]) -> Result<()> {
80
+ // TODO: process control instructions
81
+ Ok(())
82
+ }
83
+
84
+ /// Decodes a QPACK header block into a list of headers.
85
+ pub fn decode(&mut self, buf: &[u8], max_size: u64) -> Result<Vec<Header>> {
86
+ let mut b = octets::Octets::with_slice(buf);
87
+
88
+ let mut out = Vec::new();
89
+
90
+ let mut left = max_size;
91
+
92
+ let req_insert_count = decode_int(&mut b, 8)?;
93
+ let base = decode_int(&mut b, 7)?;
94
+
95
+ trace!("Header count={req_insert_count} base={base}");
96
+
97
+ while b.cap() > 0 {
98
+ let first = b.peek_u8()?;
99
+
100
+ match Representation::from_byte(first) {
101
+ Representation::Indexed => {
102
+ const STATIC: u8 = 0x40;
103
+
104
+ let s = first & STATIC == STATIC;
105
+ let index = decode_int(&mut b, 6)?;
106
+
107
+ trace!("Indexed index={index} static={s}");
108
+
109
+ if !s {
110
+ // TODO: implement dynamic table
111
+ return Err(Error::InvalidHeaderValue);
112
+ }
113
+
114
+ let (name, value) = lookup_static(index)?;
115
+
116
+ left = left
117
+ .checked_sub((name.len() + value.len()) as u64)
118
+ .ok_or(Error::HeaderListTooLarge)?;
119
+
120
+ let hdr = Header::new(name, value);
121
+ out.push(hdr);
122
+ },
123
+
124
+ Representation::IndexedWithPostBase => {
125
+ let index = decode_int(&mut b, 4)?;
126
+
127
+ trace!("Indexed With Post Base index={index}");
128
+
129
+ // TODO: implement dynamic table
130
+ return Err(Error::InvalidHeaderValue);
131
+ },
132
+
133
+ Representation::Literal => {
134
+ let name_huff = b.as_ref()[0] & 0x08 == 0x08;
135
+ let name_len = decode_int(&mut b, 3)? as usize;
136
+
137
+ let mut name = b.get_bytes(name_len)?;
138
+
139
+ let name = if name_huff {
140
+ name.get_huffman_decoded()?
141
+ } else {
142
+ name.to_vec()
143
+ };
144
+
145
+ let name = name.to_vec();
146
+ let value = decode_str(&mut b)?;
147
+
148
+ trace!(
149
+ "Literal Without Name Reference name={name:?} value={value:?}",
150
+ );
151
+
152
+ left = left
153
+ .checked_sub((name.len() + value.len()) as u64)
154
+ .ok_or(Error::HeaderListTooLarge)?;
155
+
156
+ // Instead of calling Header::new(), create Header directly
157
+ // from `name` and `value`, which are already String.
158
+ let hdr = Header(name, value);
159
+ out.push(hdr);
160
+ },
161
+
162
+ Representation::LiteralWithNameRef => {
163
+ const STATIC: u8 = 0x10;
164
+
165
+ let s = first & STATIC == STATIC;
166
+ let name_idx = decode_int(&mut b, 4)?;
167
+ let value = decode_str(&mut b)?;
168
+
169
+ trace!(
170
+ "Literal name_idx={name_idx} static={s} value={value:?}"
171
+ );
172
+
173
+ if !s {
174
+ // TODO: implement dynamic table
175
+ return Err(Error::InvalidHeaderValue);
176
+ }
177
+
178
+ let (name, _) = lookup_static(name_idx)?;
179
+
180
+ left = left
181
+ .checked_sub((name.len() + value.len()) as u64)
182
+ .ok_or(Error::HeaderListTooLarge)?;
183
+
184
+ // Instead of calling Header::new(), create Header directly
185
+ // from `value`, which is already String, but clone `name`
186
+ // as it is just a reference.
187
+ let hdr = Header(name.to_vec(), value);
188
+ out.push(hdr);
189
+ },
190
+
191
+ Representation::LiteralWithPostBase => {
192
+ trace!("Literal With Post Base");
193
+
194
+ // TODO: implement dynamic table
195
+ return Err(Error::InvalidHeaderValue);
196
+ },
197
+ }
198
+ }
199
+
200
+ Ok(out)
201
+ }
202
+ }
203
+
204
+ fn lookup_static(idx: u64) -> Result<(&'static [u8], &'static [u8])> {
205
+ if idx >= super::static_table::STATIC_DECODE_TABLE.len() as u64 {
206
+ return Err(Error::InvalidStaticTableIndex);
207
+ }
208
+
209
+ Ok(super::static_table::STATIC_DECODE_TABLE[idx as usize])
210
+ }
211
+
212
+ fn decode_int(b: &mut octets::Octets, prefix: usize) -> Result<u64> {
213
+ let mask = 2u64.pow(prefix as u32) - 1;
214
+
215
+ let mut val = u64::from(b.get_u8()?);
216
+ val &= mask;
217
+
218
+ if val < mask {
219
+ return Ok(val);
220
+ }
221
+
222
+ let mut shift = 0;
223
+
224
+ while b.cap() > 0 {
225
+ let byte = b.get_u8()?;
226
+
227
+ let inc = u64::from(byte & 0x7f)
228
+ .checked_shl(shift)
229
+ .ok_or(Error::BufferTooShort)?;
230
+
231
+ val = val.checked_add(inc).ok_or(Error::BufferTooShort)?;
232
+
233
+ shift += 7;
234
+
235
+ if byte & 0x80 == 0 {
236
+ return Ok(val);
237
+ }
238
+ }
239
+
240
+ Err(Error::BufferTooShort)
241
+ }
242
+
243
+ fn decode_str(b: &mut octets::Octets) -> Result<Vec<u8>> {
244
+ let first = b.peek_u8()?;
245
+
246
+ let huff = first & 0x80 == 0x80;
247
+
248
+ let len = decode_int(b, 7)? as usize;
249
+
250
+ let mut val = b.get_bytes(len)?;
251
+
252
+ let val = if huff {
253
+ val.get_huffman_decoded()?
254
+ } else {
255
+ val.to_vec()
256
+ };
257
+
258
+ Ok(val)
259
+ }
260
+
261
+ #[cfg(test)]
262
+ mod tests {
263
+ use super::*;
264
+
265
+ #[test]
266
+ fn decode_int1() {
267
+ let encoded = [0b01010, 0x02];
268
+ let mut b = octets::Octets::with_slice(&encoded);
269
+
270
+ assert_eq!(decode_int(&mut b, 5), Ok(10));
271
+ }
272
+
273
+ #[test]
274
+ fn decode_int2() {
275
+ let encoded = [0b11111, 0b10011010, 0b00001010];
276
+ let mut b = octets::Octets::with_slice(&encoded);
277
+
278
+ assert_eq!(decode_int(&mut b, 5), Ok(1337));
279
+ }
280
+
281
+ #[test]
282
+ fn decode_int3() {
283
+ let encoded = [0b101010];
284
+ let mut b = octets::Octets::with_slice(&encoded);
285
+
286
+ assert_eq!(decode_int(&mut b, 8), Ok(42));
287
+ }
288
+ }