vibeio-http 0.4.0

High-performance HTTP server primitives for the `vibeio` runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! HPACK encoder (RFC 7541 Section 6).
//!
//! Encodes a header list into a single header block, maintaining the
//! dynamic table across blocks. The encoder's table mirrors the peer's
//! decoder table: it honors the peer's `SETTINGS_HEADER_TABLE_SIZE` via
//! [`Encoder::queue_size_update`].
//!
//! Unlike the API sketch in `CUSTOM_HTTP2_IMPL.md` (`&[(HeaderName,
//! &[u8])]`), [`Encoder::encode`] takes [`Header`] pairs, because
//! `http::HeaderName` rejects HTTP/2 pseudo-headers (`:method`, ...)
//! while they are a first-class part of HPACK header blocks; it also
//! mirrors the [`Decoder`](super::decode::Decoder) API.

use super::{
    integer, string,
    table::{Header, Table},
};

/// The representation prefixes defined in RFC 7541 Section 6.
const INDEXED: u8 = 0b1000_0000;
const LITERAL_WITH_INDEXING: u8 = 0b0100_0000;
const LITERAL_NEVER_INDEXED: u8 = 0b0001_0000;
const SIZE_UPDATE: u8 = 0b0010_0000;

/// Header names that must never be added to the indexed table
/// (RFC 7541 Section 7.1.3).
const NEVER_INDEXED: [&[u8]; 3] = [b"authorization", b"proxy-authorization", b"cookie"];

/// Encodes HPACK header blocks.
#[derive(Debug)]
pub struct Encoder {
    /// Static + dynamic header table, mirroring the peer decoder.
    table: Table,
    /// A size update queued by the protocol layer (SETTINGS) and emitted
    /// at the start of the next header block.
    queued_size_update: Option<usize>,
    /// Whether string literals are Huffman-coded when shorter.
    use_huffman: bool,
}

impl Encoder {
    /// Creates an encoder whose table allows `max_table_size` octets
    /// (RFC 7541 Section 4.2: 4096 by default). This must match the
    /// peer's initial `SETTINGS_HEADER_TABLE_SIZE`.
    #[inline]
    pub fn new(max_table_size: usize) -> Self {
        Encoder {
            table: Table::with_max_size(max_table_size),
            queued_size_update: None,
            use_huffman: true,
        }
    }

    /// Queues a protocol-level table size update (from SETTINGS) to be
    /// applied and emitted at the start of the next header block.
    #[inline]
    pub fn queue_size_update(&mut self, size: usize) {
        self.queued_size_update = Some(match self.queued_size_update {
            Some(current) => current.max(size),
            None => size,
        });
    }

    /// Controls Huffman-coded string literals. Enabled by default, in
    /// which case a literal is Huffman-coded when that is shorter.
    #[inline]
    pub fn set_use_huffman(&mut self, use_huffman: bool) {
        self.use_huffman = use_huffman;
    }

    /// The current table, for inspection by in-crate tests.
    #[cfg(test)]
    #[inline]
    pub(crate) fn table(&self) -> &Table {
        &self.table
    }

    /// Encodes `headers` as a single header block appended to `out`.
    ///
    /// Non-sensitive headers are encoded with incremental indexing
    /// (RFC 7541 Section 6.2.1) and added to the dynamic table; exact
    /// matches use the indexed representation (Section 6.1). Sensitive
    /// headers ([`NEVER_INDEXED`]) are encoded never-indexed (Section
    /// 6.2.3) and never added to the table.
    #[inline]
    pub fn encode(&mut self, headers: &[Header], out: &mut Vec<u8>) {
        if let Some(size) = self.queued_size_update.take() {
            self.table.set_max_size(size);
            integer::encode(out, size as u64, 5, SIZE_UPDATE);
        }

        for header in headers {
            let name = header.name();
            if NEVER_INDEXED.contains(&name) {
                match self.table.find_name(name) {
                    Some(index) => integer::encode(out, index as u64, 4, LITERAL_NEVER_INDEXED),
                    None => {
                        out.push(LITERAL_NEVER_INDEXED);
                        self.encode_string(out, name);
                    }
                }
                self.encode_string(out, header.value());
                continue;
            }

            match self.table.find(name, header.value()) {
                Some(index) => {
                    integer::encode(out, index as u64, 7, INDEXED);
                }
                None => {
                    match self.table.find_name(name) {
                        Some(index) => integer::encode(out, index as u64, 6, LITERAL_WITH_INDEXING),
                        None => {
                            out.push(LITERAL_WITH_INDEXING);
                            self.encode_string(out, name);
                        }
                    }
                    self.encode_string(out, header.value());
                    // Store the header by cloning its already-owned `Bytes`
                    // (a refcount bump) instead of re-copying the name and
                    // value into fresh heap allocations.
                    self.table.add(header.clone());
                }
            }
        }
    }

    #[inline]
    fn encode_string(&self, out: &mut Vec<u8>, value: &[u8]) {
        let huffman_len = self
            .use_huffman
            .then(|| string::huffman_encoded_len_if_shorter(value))
            .flatten();
        string::encode(out, value, huffman_len);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::h2::hpack::decode::Decoder;

    /// Builds a `Header` list from string tuples.
    #[inline]
    fn headers(list: &[(&str, &str)]) -> Vec<Header> {
        list.iter()
            .map(|(name, value)| Header::new(name.as_bytes().to_vec(), value.as_bytes().to_vec()))
            .collect()
    }

    #[inline]
    fn encode(encoder: &mut Encoder, list: &[(&str, &str)]) -> Vec<u8> {
        let list = headers(list);
        let mut out = Vec::new();
        encoder.encode(&list, &mut out);
        out
    }

    #[inline]
    fn decode(encoder: &Encoder, wire: &[u8]) -> Vec<(String, String)> {
        let mut decoder = Decoder::new(encoder.table().max_size());
        decoder
            .decode(wire, &mut 0)
            .unwrap()
            .into_iter()
            .map(|h| {
                (
                    String::from_utf8(h.name().to_vec()).unwrap(),
                    String::from_utf8(h.value().to_vec()).unwrap(),
                )
            })
            .collect()
    }

    #[inline]
    fn hex_to_bytes(hex: &str) -> Vec<u8> {
        hex.as_bytes()
            .chunks_exact(2)
            .map(|pair| {
                let hi = (pair[0] as char).to_digit(16).unwrap() as u8;
                let lo = (pair[1] as char).to_digit(16).unwrap() as u8;
                (hi << 4) | lo
            })
            .collect()
    }

    /// RFC 7541 C.2.1: literal with incremental indexing, new name.
    #[test]
    fn literal_new_name_indexed() {
        let mut encoder = Encoder::new(4096);
        encoder.set_use_huffman(false);
        let out = encode(&mut encoder, &[("custom-key", "custom-header")]);
        assert_eq!(
            out,
            hex_to_bytes("400a637573746f6d2d6b65790d637573746f6d2d686561646572")
        );
        assert_eq!(encoder.table().dynamic_len(), 1);
    }

    /// RFC 7541 C.3: full request walkthrough without Huffman.
    #[test]
    fn c3_request_walkthrough() {
        let mut encoder = Encoder::new(4096);
        encoder.set_use_huffman(false);

        // C.3.1: first request; :authority added at index 62.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":method", "GET"),
                    (":scheme", "http"),
                    (":path", "/"),
                    (":authority", "www.example.com"),
                ]
            ),
            hex_to_bytes("828684410f7777772e6578616d706c652e636f6d")
        );

        // C.3.2: :authority referenced at dynamic 62; cache-control added.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":method", "GET"),
                    (":scheme", "http"),
                    (":path", "/"),
                    (":authority", "www.example.com"),
                    ("cache-control", "no-cache"),
                ]
            ),
            hex_to_bytes("828684be58086e6f2d6361636865")
        );

        // C.3.3: cache-control at 62, :authority at 63, new literal name.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":method", "GET"),
                    (":scheme", "https"),
                    (":path", "/index.html"),
                    (":authority", "www.example.com"),
                    ("custom-key", "custom-value"),
                ]
            ),
            hex_to_bytes("828785bf400a637573746f6d2d6b65790c637573746f6d2d76616c7565")
        );
    }

    /// RFC 7541 C.4: full request walkthrough with Huffman.
    #[test]
    fn c4_request_walkthrough() {
        let mut encoder = Encoder::new(4096);

        // C.4.1.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":method", "GET"),
                    (":scheme", "http"),
                    (":path", "/"),
                    (":authority", "www.example.com"),
                ]
            ),
            hex_to_bytes("828684418cf1e3c2e5f23a6ba0ab90f4ff")
        );

        // C.4.2.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":method", "GET"),
                    (":scheme", "http"),
                    (":path", "/"),
                    (":authority", "www.example.com"),
                    ("cache-control", "no-cache"),
                ]
            ),
            hex_to_bytes("828684be5886a8eb10649cbf")
        );

        // C.4.3.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":method", "GET"),
                    (":scheme", "https"),
                    (":path", "/index.html"),
                    (":authority", "www.example.com"),
                    ("custom-key", "custom-value"),
                ]
            ),
            hex_to_bytes("828785bf408825a849e95ba97d7f8925a849e95bb8e8b4bf")
        );
    }

    /// RFC 7541 C.5: response walkthrough without Huffman, 256-octet
    /// table so evictions occur.
    #[test]
    fn c5_response_walkthrough() {
        let mut encoder = Encoder::new(256);
        encoder.set_use_huffman(false);

        // C.5.1.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":status", "302"),
                    ("cache-control", "private"),
                    ("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
                    ("location", "https://www.example.com"),
                ]
            ),
            hex_to_bytes(
                "4803333032580770726976617465611d4d6f6e2c203231204f637420323031332032303a31333a323120474d546e1768747470733a2f2f7777772e6578616d706c652e636f6d"
            )
        );
        assert_eq!(encoder.table().dynamic_len(), 4);

        // C.5.2: :status: 302 evicted to make room.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":status", "307"),
                    ("cache-control", "private"),
                    ("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
                    ("location", "https://www.example.com"),
                ]
            ),
            hex_to_bytes("4803333037c1c0bf")
        );
        assert_eq!(encoder.table().dynamic_len(), 4);

        // C.5.3: several entries evicted.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":status", "200"),
                    ("cache-control", "private"),
                    ("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
                    ("location", "https://www.example.com"),
                    ("content-encoding", "gzip"),
                    (
                        "set-cookie",
                        "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1",
                    ),
                ]
            ),
            hex_to_bytes(
                "88c1611d4d6f6e2c203231204f637420323031332032303a31333a323220474d54c05a04677a69707738666f6f3d4153444a4b48514b425a584f5157454f50495541585157454f49553b206d61782d6167653d333630303b2076657273696f6e3d31"
            )
        );
        assert_eq!(encoder.table().dynamic_len(), 3);
    }

    /// RFC 7541 C.6: response walkthrough with Huffman, same table
    /// dynamics as C.5.
    #[test]
    fn c6_response_walkthrough() {
        let mut encoder = Encoder::new(256);

        // C.6.1.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":status", "302"),
                    ("cache-control", "private"),
                    ("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
                    ("location", "https://www.example.com"),
                ]
            ),
            hex_to_bytes(
                "488264025885aec3771a4b6196d07abe941054d444a8200595040b8166e082a62d1bff6e919d29ad171863c78f0b97c8e9ae82ae43d3"
            )
        );
        assert_eq!(encoder.table().dynamic_len(), 4);

        // C.6.2.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":status", "307"),
                    ("cache-control", "private"),
                    ("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
                    ("location", "https://www.example.com"),
                ]
            ),
            hex_to_bytes("4883640effc1c0bf")
        );
        assert_eq!(encoder.table().dynamic_len(), 4);

        // C.6.3.
        assert_eq!(
            encode(
                &mut encoder,
                &[
                    (":status", "200"),
                    ("cache-control", "private"),
                    ("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
                    ("location", "https://www.example.com"),
                    ("content-encoding", "gzip"),
                    (
                        "set-cookie",
                        "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1",
                    ),
                ]
            ),
            hex_to_bytes(
                "88c16196d07abe941054d444a8200595040b8166e084a62d1bffc05a839bd9ab77ad94e7821dd7f2e6c7b335dfdfcd5b3960d5af27087f3672c1ab270fb5291f9587316065c003ed4ee5b1063d5007"
            )
        );
        assert_eq!(encoder.table().dynamic_len(), 3);
    }

    /// Non-sensitive headers are indexed even when never-indexed would
    /// be a valid choice; only [`NEVER_INDEXED`] names are excluded.
    #[test]
    fn non_sensitive_is_indexed() {
        let mut encoder = Encoder::new(4096);
        encoder.set_use_huffman(false);
        let out = encode(&mut encoder, &[("password", "secret")]);
        // Literal with incremental indexing (0x40), new name.
        assert_eq!(out, hex_to_bytes("400870617373776f726406736563726574"));
        assert_eq!(encoder.table().dynamic_len(), 1);
    }

    /// RFC 7541 Section 7.1.3: sensitive headers are encoded never
    /// indexed and never enter the dynamic table.
    #[test]
    fn sensitive_headers_never_indexed() {
        for name in ["authorization", "proxy-authorization", "cookie"] {
            let mut encoder = Encoder::new(4096);
            encoder.set_use_huffman(false);
            let out = encode(&mut encoder, &[(name, "secret-value")]);

            // Literal never indexed (0x10) with the static-table name
            // index; all three names are static entries above 15, so the
            // index is split across a 0x1f first octet + continuation.
            assert_eq!(out[0], 0x1f, "{name}");
            let decoded = decode(&encoder, &out);
            assert_eq!(
                decoded,
                vec![(name.to_string(), "secret-value".into())],
                "{name}"
            );
            // Sensitive entries never enter the table.
            assert_eq!(encoder.table().dynamic_len(), 0, "{name}");
        }
    }

    /// The static-table name index for a sensitive header is emitted
    /// verbatim: authorization is static index 23 (0x1f 0x08).
    #[test]
    fn sensitive_uses_static_name_index() {
        let mut encoder = Encoder::new(4096);
        encoder.set_use_huffman(false);
        let out = encode(&mut encoder, &[("authorization", "Bearer xyz")]);
        assert_eq!(out, hex_to_bytes("1f080a4265617265722078797a"));
        assert_eq!(encoder.table().dynamic_len(), 0);
    }

    /// A queued table size update is emitted before the first field and
    /// applied to the encoder's table (RFC 7541 Section 6.3).
    #[test]
    fn size_update_emitted_at_block_start() {
        let mut encoder = Encoder::new(4096);
        encoder.set_use_huffman(false);
        encoder.queue_size_update(0);
        let out = encode(&mut encoder, &[(":method", "GET")]);
        assert_eq!(out, hex_to_bytes("2082"));
        assert_eq!(encoder.table().max_size(), 0);

        // Shrinking evicts entries so indices stay in sync with the peer.
        let mut encoder = Encoder::new(4096);
        encoder.set_use_huffman(false);
        let _ = encode(&mut encoder, &[("custom-key", "custom-value")]);
        assert_eq!(encoder.table().dynamic_len(), 1);
        encoder.queue_size_update(32);
        let _ = encode(&mut encoder, &[(":method", "GET")]);
        assert_eq!(encoder.table().dynamic_len(), 0);
    }

    /// Growing the table again restarts indexing at 62.
    #[test]
    fn size_update_grow_reindexes() {
        let mut encoder = Encoder::new(4096);
        encoder.set_use_huffman(false);
        encoder.queue_size_update(0);
        let _ = encode(&mut encoder, &[("a", "1")]);
        encoder.queue_size_update(4096);
        let _ = encode(&mut encoder, &[("b", "2")]);
        assert_eq!(encoder.table().get(62).unwrap().name(), b"b");
        assert_eq!(encoder.table().get(63), None);
    }

    /// Round-trip: encoding then decoding reproduces the original list,
    /// for a range of table sizes and Huffman settings.
    #[test]
    fn round_trip() {
        let lists: [&[(&str, &str)]; 5] = [
            &[(":method", "GET"), (":scheme", "https")],
            &[
                (":status", "302"),
                ("cache-control", "private"),
                ("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
                ("location", "https://www.example.com"),
                ("content-encoding", "gzip"),
                ("authorization", "Bearer sekrit"),
                ("x-empty", ""),
            ],
            &[("x-long", &"v".repeat(300))],
            &[("x-huffman-ok", "custom-value"), ("cookie", "a=b; c=d")],
            &[("accept-encoding", "gzip, deflate"), ("te", "trailers")],
        ];

        for (i, list) in lists.iter().enumerate() {
            for table_size in [0, 16, 256, 4096] {
                for use_huffman in [false, true] {
                    let mut encoder = Encoder::new(table_size);
                    encoder.set_use_huffman(use_huffman);
                    let wire = encode(&mut encoder, list);
                    let decoded = decode(&encoder, &wire);
                    let expected = list
                        .iter()
                        .map(|(name, value)| (name.to_string(), value.to_string()))
                        .collect::<Vec<_>>();
                    assert_eq!(
                        decoded, expected,
                        "list {i}, table {table_size}, huffman {use_huffman}"
                    );
                }
            }
        }
    }

    /// Empty values round-trip with Huffman enabled.
    #[test]
    fn empty_value_round_trip() {
        let mut encoder = Encoder::new(4096);
        let out = encode(&mut encoder, &[("x-empty", "")]);
        assert_eq!(decode(&encoder, &out), vec![("x-empty".into(), "".into())]);
    }
}