Coverage Report

Created: 2025-10-13 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/h2/src/hpack/decoder.rs
Line
Count
Source
1
use super::{header::BytesStr, huffman, Header};
2
use crate::frame;
3
4
use bytes::{Buf, Bytes, BytesMut};
5
use http::header;
6
use http::method::{self, Method};
7
use http::status::{self, StatusCode};
8
9
use std::cmp;
10
use std::collections::VecDeque;
11
use std::io::Cursor;
12
use std::str::Utf8Error;
13
14
/// Decodes headers using HPACK
15
#[derive(Debug)]
16
pub struct Decoder {
17
    // Protocol indicated that the max table size will update
18
    max_size_update: Option<usize>,
19
    last_max_update: usize,
20
    table: Table,
21
    buffer: BytesMut,
22
}
23
24
/// Represents all errors that can be encountered while performing the decoding
25
/// of an HPACK header set.
26
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
27
pub enum DecoderError {
28
    InvalidRepresentation,
29
    InvalidIntegerPrefix,
30
    InvalidTableIndex,
31
    InvalidHuffmanCode,
32
    InvalidUtf8,
33
    InvalidStatusCode,
34
    InvalidPseudoheader,
35
    InvalidMaxDynamicSize,
36
    IntegerOverflow,
37
    NeedMore(NeedMore),
38
}
39
40
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
41
pub enum NeedMore {
42
    UnexpectedEndOfStream,
43
    IntegerUnderflow,
44
    StringUnderflow,
45
}
46
47
enum Representation {
48
    /// Indexed header field representation
49
    ///
50
    /// An indexed header field representation identifies an entry in either the
51
    /// static table or the dynamic table (see Section 2.3).
52
    ///
53
    /// # Header encoding
54
    ///
55
    /// ```text
56
    ///   0   1   2   3   4   5   6   7
57
    /// +---+---+---+---+---+---+---+---+
58
    /// | 1 |        Index (7+)         |
59
    /// +---+---------------------------+
60
    /// ```
61
    Indexed,
62
63
    /// Literal Header Field with Incremental Indexing
64
    ///
65
    /// A literal header field with incremental indexing representation results
66
    /// in appending a header field to the decoded header list and inserting it
67
    /// as a new entry into the dynamic table.
68
    ///
69
    /// # Header encoding
70
    ///
71
    /// ```text
72
    ///   0   1   2   3   4   5   6   7
73
    /// +---+---+---+---+---+---+---+---+
74
    /// | 0 | 1 |      Index (6+)       |
75
    /// +---+---+-----------------------+
76
    /// | H |     Value Length (7+)     |
77
    /// +---+---------------------------+
78
    /// | Value String (Length octets)  |
79
    /// +-------------------------------+
80
    /// ```
81
    LiteralWithIndexing,
82
83
    /// Literal Header Field without Indexing
84
    ///
85
    /// A literal header field without indexing representation results in
86
    /// appending a header field to the decoded header list without altering the
87
    /// dynamic table.
88
    ///
89
    /// # Header encoding
90
    ///
91
    /// ```text
92
    ///   0   1   2   3   4   5   6   7
93
    /// +---+---+---+---+---+---+---+---+
94
    /// | 0 | 0 | 0 | 0 |  Index (4+)   |
95
    /// +---+---+-----------------------+
96
    /// | H |     Value Length (7+)     |
97
    /// +---+---------------------------+
98
    /// | Value String (Length octets)  |
99
    /// +-------------------------------+
100
    /// ```
101
    LiteralWithoutIndexing,
102
103
    /// Literal Header Field Never Indexed
104
    ///
105
    /// A literal header field never-indexed representation results in appending
106
    /// a header field to the decoded header list without altering the dynamic
107
    /// table. Intermediaries MUST use the same representation for encoding this
108
    /// header field.
109
    ///
110
    /// ```text
111
    ///   0   1   2   3   4   5   6   7
112
    /// +---+---+---+---+---+---+---+---+
113
    /// | 0 | 0 | 0 | 1 |  Index (4+)   |
114
    /// +---+---+-----------------------+
115
    /// | H |     Value Length (7+)     |
116
    /// +---+---------------------------+
117
    /// | Value String (Length octets)  |
118
    /// +-------------------------------+
119
    /// ```
120
    LiteralNeverIndexed,
121
122
    /// Dynamic Table Size Update
123
    ///
124
    /// A dynamic table size update signals a change to the size of the dynamic
125
    /// table.
126
    ///
127
    /// # Header encoding
128
    ///
129
    /// ```text
130
    ///   0   1   2   3   4   5   6   7
131
    /// +---+---+---+---+---+---+---+---+
132
    /// | 0 | 0 | 1 |   Max size (5+)   |
133
    /// +---+---------------------------+
134
    /// ```
135
    SizeUpdate,
136
}
137
138
#[derive(Debug)]
139
struct Table {
140
    entries: VecDeque<Header>,
141
    size: usize,
142
    max_size: usize,
143
}
144
145
struct StringMarker {
146
    offset: usize,
147
    len: usize,
148
    string: Option<Bytes>,
149
}
150
151
// ===== impl Decoder =====
152
153
impl Decoder {
154
    /// Creates a new `Decoder` with all settings set to default values.
155
28.4k
    pub fn new(size: usize) -> Decoder {
156
28.4k
        Decoder {
157
28.4k
            max_size_update: None,
158
28.4k
            last_max_update: size,
159
28.4k
            table: Table::new(size),
160
28.4k
            buffer: BytesMut::with_capacity(4096),
161
28.4k
        }
162
28.4k
    }
163
164
    /// Queues a potential size update
165
    #[allow(dead_code)]
166
0
    pub fn queue_size_update(&mut self, size: usize) {
167
0
        let size = match self.max_size_update {
168
0
            Some(v) => cmp::max(v, size),
169
0
            None => size,
170
        };
171
172
0
        self.max_size_update = Some(size);
173
0
    }
174
175
    /// Decodes the headers found in the given buffer.
176
71.0k
    pub fn decode<F>(
177
71.0k
        &mut self,
178
71.0k
        src: &mut Cursor<&mut BytesMut>,
179
71.0k
        mut f: F,
180
71.0k
    ) -> Result<(), DecoderError>
181
71.0k
    where
182
71.0k
        F: FnMut(Header),
183
    {
184
        use self::Representation::*;
185
186
71.0k
        let mut can_resize = true;
187
188
71.0k
        if let Some(size) = self.max_size_update.take() {
189
0
            self.last_max_update = size;
190
71.0k
        }
191
192
71.0k
        let span = tracing::trace_span!("hpack::decode");
193
71.0k
        let _e = span.enter();
194
195
71.0k
        tracing::trace!("decode");
196
197
13.4M
        while let Some(ty) = peek_u8(src) {
198
            // At this point we are always at the beginning of the next block
199
            // within the HPACK data. The type of the block can always be
200
            // determined from the first byte.
201
13.4M
            match Representation::load(ty)? {
202
                Indexed => {
203
10.6M
                    tracing::trace!(rem = src.remaining(), kind = %"Indexed");
204
10.6M
                    can_resize = false;
205
10.6M
                    let entry = self.decode_indexed(src)?;
206
10.6M
                    consume(src);
207
10.6M
                    f(entry);
208
                }
209
                LiteralWithIndexing => {
210
1.27M
                    tracing::trace!(rem = src.remaining(), kind = %"LiteralWithIndexing");
211
1.27M
                    can_resize = false;
212
1.27M
                    let entry = self.decode_literal(src, true)?;
213
214
                    // Insert the header into the table
215
1.26M
                    self.table.insert(entry.clone());
216
1.26M
                    consume(src);
217
218
1.26M
                    f(entry);
219
                }
220
                LiteralWithoutIndexing => {
221
1.23M
                    tracing::trace!(rem = src.remaining(), kind = %"LiteralWithoutIndexing");
222
1.23M
                    can_resize = false;
223
1.23M
                    let entry = self.decode_literal(src, false)?;
224
1.23M
                    consume(src);
225
1.23M
                    f(entry);
226
                }
227
                LiteralNeverIndexed => {
228
262k
                    tracing::trace!(rem = src.remaining(), kind = %"LiteralNeverIndexed");
229
262k
                    can_resize = false;
230
262k
                    let entry = self.decode_literal(src, false)?;
231
261k
                    consume(src);
232
233
                    // TODO: Track that this should never be indexed
234
235
261k
                    f(entry);
236
                }
237
                SizeUpdate => {
238
9.57k
                    tracing::trace!(rem = src.remaining(), kind = %"SizeUpdate");
239
9.57k
                    if !can_resize {
240
438
                        return Err(DecoderError::InvalidMaxDynamicSize);
241
9.13k
                    }
242
243
                    // Handle the dynamic table size update
244
9.13k
                    self.process_size_update(src)?;
245
8.10k
                    consume(src);
246
                }
247
            }
248
        }
249
250
49.7k
        Ok(())
251
71.0k
    }
<h2::hpack::decoder::Decoder>::decode::<<h2::frame::headers::HeaderBlock>::load::{closure#0}>
Line
Count
Source
176
56.4k
    pub fn decode<F>(
177
56.4k
        &mut self,
178
56.4k
        src: &mut Cursor<&mut BytesMut>,
179
56.4k
        mut f: F,
180
56.4k
    ) -> Result<(), DecoderError>
181
56.4k
    where
182
56.4k
        F: FnMut(Header),
183
    {
184
        use self::Representation::*;
185
186
56.4k
        let mut can_resize = true;
187
188
56.4k
        if let Some(size) = self.max_size_update.take() {
189
0
            self.last_max_update = size;
190
56.4k
        }
191
192
56.4k
        let span = tracing::trace_span!("hpack::decode");
193
56.4k
        let _e = span.enter();
194
195
56.4k
        tracing::trace!("decode");
196
197
693k
        while let Some(ty) = peek_u8(src) {
198
            // At this point we are always at the beginning of the next block
199
            // within the HPACK data. The type of the block can always be
200
            // determined from the first byte.
201
651k
            match Representation::load(ty)? {
202
                Indexed => {
203
504k
                    tracing::trace!(rem = src.remaining(), kind = %"Indexed");
204
504k
                    can_resize = false;
205
504k
                    let entry = self.decode_indexed(src)?;
206
503k
                    consume(src);
207
503k
                    f(entry);
208
                }
209
                LiteralWithIndexing => {
210
101k
                    tracing::trace!(rem = src.remaining(), kind = %"LiteralWithIndexing");
211
101k
                    can_resize = false;
212
101k
                    let entry = self.decode_literal(src, true)?;
213
214
                    // Insert the header into the table
215
97.0k
                    self.table.insert(entry.clone());
216
97.0k
                    consume(src);
217
218
97.0k
                    f(entry);
219
                }
220
                LiteralWithoutIndexing => {
221
33.9k
                    tracing::trace!(rem = src.remaining(), kind = %"LiteralWithoutIndexing");
222
33.9k
                    can_resize = false;
223
33.9k
                    let entry = self.decode_literal(src, false)?;
224
27.4k
                    consume(src);
225
27.4k
                    f(entry);
226
                }
227
                LiteralNeverIndexed => {
228
2.85k
                    tracing::trace!(rem = src.remaining(), kind = %"LiteralNeverIndexed");
229
2.85k
                    can_resize = false;
230
2.85k
                    let entry = self.decode_literal(src, false)?;
231
1.58k
                    consume(src);
232
233
                    // TODO: Track that this should never be indexed
234
235
1.58k
                    f(entry);
236
                }
237
                SizeUpdate => {
238
8.59k
                    tracing::trace!(rem = src.remaining(), kind = %"SizeUpdate");
239
8.59k
                    if !can_resize {
240
289
                        return Err(DecoderError::InvalidMaxDynamicSize);
241
8.30k
                    }
242
243
                    // Handle the dynamic table size update
244
8.30k
                    self.process_size_update(src)?;
245
7.54k
                    consume(src);
246
                }
247
            }
248
        }
249
250
41.5k
        Ok(())
251
56.4k
    }
<h2::hpack::decoder::Decoder>::decode::<h2::fuzz_bridge::fuzz_logic::fuzz_hpack::{closure#0}>
Line
Count
Source
176
14.5k
    pub fn decode<F>(
177
14.5k
        &mut self,
178
14.5k
        src: &mut Cursor<&mut BytesMut>,
179
14.5k
        mut f: F,
180
14.5k
    ) -> Result<(), DecoderError>
181
14.5k
    where
182
14.5k
        F: FnMut(Header),
183
    {
184
        use self::Representation::*;
185
186
14.5k
        let mut can_resize = true;
187
188
14.5k
        if let Some(size) = self.max_size_update.take() {
189
0
            self.last_max_update = size;
190
14.5k
        }
191
192
14.5k
        let span = tracing::trace_span!("hpack::decode");
193
14.5k
        let _e = span.enter();
194
195
14.5k
        tracing::trace!("decode");
196
197
12.7M
        while let Some(ty) = peek_u8(src) {
198
            // At this point we are always at the beginning of the next block
199
            // within the HPACK data. The type of the block can always be
200
            // determined from the first byte.
201
12.7M
            match Representation::load(ty)? {
202
                Indexed => {
203
10.1M
                    tracing::trace!(rem = src.remaining(), kind = %"Indexed");
204
10.1M
                    can_resize = false;
205
10.1M
                    let entry = self.decode_indexed(src)?;
206
10.1M
                    consume(src);
207
10.1M
                    f(entry);
208
                }
209
                LiteralWithIndexing => {
210
1.17M
                    tracing::trace!(rem = src.remaining(), kind = %"LiteralWithIndexing");
211
1.17M
                    can_resize = false;
212
1.17M
                    let entry = self.decode_literal(src, true)?;
213
214
                    // Insert the header into the table
215
1.16M
                    self.table.insert(entry.clone());
216
1.16M
                    consume(src);
217
218
1.16M
                    f(entry);
219
                }
220
                LiteralWithoutIndexing => {
221
1.20M
                    tracing::trace!(rem = src.remaining(), kind = %"LiteralWithoutIndexing");
222
1.20M
                    can_resize = false;
223
1.20M
                    let entry = self.decode_literal(src, false)?;
224
1.20M
                    consume(src);
225
1.20M
                    f(entry);
226
                }
227
                LiteralNeverIndexed => {
228
260k
                    tracing::trace!(rem = src.remaining(), kind = %"LiteralNeverIndexed");
229
260k
                    can_resize = false;
230
260k
                    let entry = self.decode_literal(src, false)?;
231
259k
                    consume(src);
232
233
                    // TODO: Track that this should never be indexed
234
235
259k
                    f(entry);
236
                }
237
                SizeUpdate => {
238
983
                    tracing::trace!(rem = src.remaining(), kind = %"SizeUpdate");
239
983
                    if !can_resize {
240
149
                        return Err(DecoderError::InvalidMaxDynamicSize);
241
834
                    }
242
243
                    // Handle the dynamic table size update
244
834
                    self.process_size_update(src)?;
245
556
                    consume(src);
246
                }
247
            }
248
        }
249
250
8.20k
        Ok(())
251
14.5k
    }
252
253
9.13k
    fn process_size_update(&mut self, buf: &mut Cursor<&mut BytesMut>) -> Result<(), DecoderError> {
254
9.13k
        let new_size = decode_int(buf, 5)?;
255
256
8.34k
        if new_size > self.last_max_update {
257
243
            return Err(DecoderError::InvalidMaxDynamicSize);
258
8.10k
        }
259
260
8.10k
        tracing::debug!(
261
0
            from = self.table.size(),
262
            to = new_size,
263
0
            "Decoder changed max table size"
264
        );
265
266
8.10k
        self.table.set_max_size(new_size);
267
268
8.10k
        Ok(())
269
9.13k
    }
270
271
10.6M
    fn decode_indexed(&self, buf: &mut Cursor<&mut BytesMut>) -> Result<Header, DecoderError> {
272
10.6M
        let index = decode_int(buf, 7)?;
273
10.6M
        self.table.get(index)
274
10.6M
    }
275
276
2.77M
    fn decode_literal(
277
2.77M
        &mut self,
278
2.77M
        buf: &mut Cursor<&mut BytesMut>,
279
2.77M
        index: bool,
280
2.77M
    ) -> Result<Header, DecoderError> {
281
2.77M
        let prefix = if index { 6 } else { 4 };
282
283
        // Extract the table index for the name, or 0 if not indexed
284
2.77M
        let table_idx = decode_int(buf, prefix)?;
285
286
        // First, read the header name
287
2.77M
        if table_idx == 0 {
288
1.00M
            let old_pos = buf.position();
289
1.00M
            let name_marker = self.try_decode_string(buf)?;
290
1.00M
            let value_marker = self.try_decode_string(buf)?;
291
1.00M
            buf.set_position(old_pos);
292
            // Read the name as a literal
293
1.00M
            let name = name_marker.consume(buf);
294
1.00M
            let value = value_marker.consume(buf);
295
1.00M
            Header::new(name, value)
296
        } else {
297
1.76M
            let e = self.table.get(table_idx)?;
298
1.76M
            let value = self.decode_string(buf)?;
299
300
1.76M
            e.name().into_entry(value)
301
        }
302
2.77M
    }
303
304
3.77M
    fn try_decode_string(
305
3.77M
        &mut self,
306
3.77M
        buf: &mut Cursor<&mut BytesMut>,
307
3.77M
    ) -> Result<StringMarker, DecoderError> {
308
3.77M
        let old_pos = buf.position();
309
        const HUFF_FLAG: u8 = 0b1000_0000;
310
311
        // The first bit in the first byte contains the huffman encoded flag.
312
3.77M
        let huff = match peek_u8(buf) {
313
3.77M
            Some(hdr) => (hdr & HUFF_FLAG) == HUFF_FLAG,
314
1.72k
            None => return Err(DecoderError::NeedMore(NeedMore::UnexpectedEndOfStream)),
315
        };
316
317
        // Decode the string length using 7 bit prefix
318
3.77M
        let len = decode_int(buf, 7)?;
319
320
3.77M
        if len > buf.remaining() {
321
5.11k
            tracing::trace!(len, remaining = buf.remaining(), "decode_string underflow",);
322
5.11k
            return Err(DecoderError::NeedMore(NeedMore::StringUnderflow));
323
3.76M
        }
324
325
3.76M
        let offset = (buf.position() - old_pos) as usize;
326
3.76M
        if huff {
327
2.79M
            let ret = {
328
2.79M
                let raw = &buf.chunk()[..len];
329
2.79M
                huffman::decode(raw, &mut self.buffer).map(|buf| StringMarker {
330
2.79M
                    offset,
331
2.79M
                    len,
332
2.79M
                    string: Some(BytesMut::freeze(buf)),
333
2.79M
                })
334
            };
335
336
2.79M
            buf.advance(len);
337
2.79M
            ret
338
        } else {
339
972k
            buf.advance(len);
340
972k
            Ok(StringMarker {
341
972k
                offset,
342
972k
                len,
343
972k
                string: None,
344
972k
            })
345
        }
346
3.77M
    }
347
348
1.76M
    fn decode_string(&mut self, buf: &mut Cursor<&mut BytesMut>) -> Result<Bytes, DecoderError> {
349
1.76M
        let old_pos = buf.position();
350
1.76M
        let marker = self.try_decode_string(buf)?;
351
1.76M
        buf.set_position(old_pos);
352
1.76M
        Ok(marker.consume(buf))
353
1.76M
    }
354
}
355
356
impl Default for Decoder {
357
0
    fn default() -> Decoder {
358
0
        Decoder::new(4096)
359
0
    }
360
}
361
362
// ===== impl Representation =====
363
364
impl Representation {
365
13.4M
    pub fn load(byte: u8) -> Result<Representation, DecoderError> {
366
        const INDEXED: u8 = 0b1000_0000;
367
        const LITERAL_WITH_INDEXING: u8 = 0b0100_0000;
368
        const LITERAL_WITHOUT_INDEXING: u8 = 0b1111_0000;
369
        const LITERAL_NEVER_INDEXED: u8 = 0b0001_0000;
370
        const SIZE_UPDATE_MASK: u8 = 0b1110_0000;
371
        const SIZE_UPDATE: u8 = 0b0010_0000;
372
373
        // TODO: What did I even write here?
374
375
13.4M
        if byte & INDEXED == INDEXED {
376
10.6M
            Ok(Representation::Indexed)
377
2.78M
        } else if byte & LITERAL_WITH_INDEXING == LITERAL_WITH_INDEXING {
378
1.27M
            Ok(Representation::LiteralWithIndexing)
379
1.51M
        } else if byte & LITERAL_WITHOUT_INDEXING == 0 {
380
1.23M
            Ok(Representation::LiteralWithoutIndexing)
381
272k
        } else if byte & LITERAL_WITHOUT_INDEXING == LITERAL_NEVER_INDEXED {
382
262k
            Ok(Representation::LiteralNeverIndexed)
383
9.57k
        } else if byte & SIZE_UPDATE_MASK == SIZE_UPDATE {
384
9.57k
            Ok(Representation::SizeUpdate)
385
        } else {
386
0
            Err(DecoderError::InvalidRepresentation)
387
        }
388
13.4M
    }
389
}
390
391
17.2M
fn decode_int<B: Buf>(buf: &mut B, prefix_size: u8) -> Result<usize, DecoderError> {
392
    // The octet limit is chosen such that the maximum allowed *value* can
393
    // never overflow an unsigned 32-bit integer. The maximum value of any
394
    // integer that can be encoded with 5 octets is ~2^28
395
    const MAX_BYTES: usize = 5;
396
    const VARINT_MASK: u8 = 0b0111_1111;
397
    const VARINT_FLAG: u8 = 0b1000_0000;
398
399
17.2M
    if prefix_size < 1 || prefix_size > 8 {
400
0
        return Err(DecoderError::InvalidIntegerPrefix);
401
17.2M
    }
402
403
17.2M
    if !buf.has_remaining() {
404
0
        return Err(DecoderError::NeedMore(NeedMore::IntegerUnderflow));
405
17.2M
    }
406
407
17.2M
    let mask = if prefix_size == 8 {
408
0
        0xFF
409
    } else {
410
17.2M
        (1u8 << prefix_size).wrapping_sub(1)
411
    };
412
413
17.2M
    let mut ret = (buf.get_u8() & mask) as usize;
414
415
17.2M
    if ret < mask as usize {
416
        // Value fits in the prefix bits
417
17.1M
        return Ok(ret);
418
23.6k
    }
419
420
    // The int did not fit in the prefix bits, so continue reading.
421
    //
422
    // The total number of bytes used to represent the int. The first byte was
423
    // the prefix, so start at 1.
424
23.6k
    let mut bytes = 1;
425
426
    // The rest of the int is stored as a varint -- 7 bits for the value and 1
427
    // bit to indicate if it is the last byte.
428
23.6k
    let mut shift = 0;
429
430
49.3k
    while buf.has_remaining() {
431
45.9k
        let b = buf.get_u8();
432
433
45.9k
        bytes += 1;
434
45.9k
        ret += ((b & VARINT_MASK) as usize) << shift;
435
45.9k
        shift += 7;
436
437
45.9k
        if b & VARINT_FLAG == 0 {
438
19.7k
            return Ok(ret);
439
26.1k
        }
440
441
26.1k
        if bytes == MAX_BYTES {
442
            // The spec requires that this situation is an error
443
418
            return Err(DecoderError::IntegerOverflow);
444
25.7k
        }
445
    }
446
447
3.40k
    Err(DecoderError::NeedMore(NeedMore::IntegerUnderflow))
448
17.2M
}
449
450
17.2M
fn peek_u8<B: Buf>(buf: &B) -> Option<u8> {
451
17.2M
    if buf.has_remaining() {
452
17.2M
        Some(buf.chunk()[0])
453
    } else {
454
51.4k
        None
455
    }
456
17.2M
}
457
458
14.3M
fn take(buf: &mut Cursor<&mut BytesMut>, n: usize) -> Bytes {
459
14.3M
    let pos = buf.position() as usize;
460
14.3M
    let mut head = buf.get_mut().split_to(pos + n);
461
14.3M
    buf.set_position(0);
462
14.3M
    head.advance(pos);
463
14.3M
    head.freeze()
464
14.3M
}
465
466
impl StringMarker {
467
3.76M
    fn consume(self, buf: &mut Cursor<&mut BytesMut>) -> Bytes {
468
3.76M
        buf.advance(self.offset);
469
3.76M
        match self.string {
470
2.79M
            Some(string) => {
471
2.79M
                buf.advance(self.len);
472
2.79M
                string
473
            }
474
972k
            None => take(buf, self.len),
475
        }
476
3.76M
    }
477
}
478
479
13.4M
fn consume(buf: &mut Cursor<&mut BytesMut>) {
480
    // remove bytes from the internal BytesMut when they have been successfully
481
    // decoded. This is a more permanent cursor position, which will be
482
    // used to resume if decoding was only partial.
483
13.4M
    take(buf, 0);
484
13.4M
}
485
486
// ===== impl Table =====
487
488
impl Table {
489
28.4k
    fn new(max_size: usize) -> Table {
490
28.4k
        Table {
491
28.4k
            entries: VecDeque::new(),
492
28.4k
            size: 0,
493
28.4k
            max_size,
494
28.4k
        }
495
28.4k
    }
496
497
0
    fn size(&self) -> usize {
498
0
        self.size
499
0
    }
500
501
    /// Returns the entry located at the given index.
502
    ///
503
    /// The table is 1-indexed and constructed in such a way that the first
504
    /// entries belong to the static table, followed by entries in the dynamic
505
    /// table. They are merged into a single index address space, though.
506
    ///
507
    /// This is according to the [HPACK spec, section 2.3.3.]
508
    /// (http://http2.github.io/http2-spec/compression.html#index.address.space)
509
12.4M
    pub fn get(&self, index: usize) -> Result<Header, DecoderError> {
510
12.4M
        if index == 0 {
511
35
            return Err(DecoderError::InvalidTableIndex);
512
12.4M
        }
513
514
12.4M
        if index <= 61 {
515
12.3M
            return Ok(get_static(index));
516
77.9k
        }
517
518
        // Convert the index for lookup in the entries structure.
519
77.9k
        match self.entries.get(index - 62) {
520
77.1k
            Some(e) => Ok(e.clone()),
521
796
            None => Err(DecoderError::InvalidTableIndex),
522
        }
523
12.4M
    }
524
525
1.26M
    fn insert(&mut self, entry: Header) {
526
1.26M
        let len = entry.len();
527
528
1.26M
        self.reserve(len);
529
530
1.26M
        if self.size + len <= self.max_size {
531
90.1k
            self.size += len;
532
90.1k
533
90.1k
            // Track the entry
534
90.1k
            self.entries.push_front(entry);
535
1.17M
        }
536
1.26M
    }
537
538
8.10k
    fn set_max_size(&mut self, size: usize) {
539
8.10k
        self.max_size = size;
540
        // Make the table size fit within the new constraints.
541
8.10k
        self.consolidate();
542
8.10k
    }
543
544
1.26M
    fn reserve(&mut self, size: usize) {
545
1.29M
        while self.size + size > self.max_size {
546
1.20M
            match self.entries.pop_back() {
547
29.6k
                Some(last) => {
548
29.6k
                    self.size -= last.len();
549
29.6k
                }
550
1.17M
                None => return,
551
            }
552
        }
553
1.26M
    }
554
555
8.10k
    fn consolidate(&mut self) {
556
12.8k
        while self.size > self.max_size {
557
            {
558
4.77k
                let last = match self.entries.back() {
559
4.77k
                    Some(x) => x,
560
                    None => {
561
                        // Can never happen as the size of the table must reach
562
                        // 0 by the time we've exhausted all elements.
563
0
                        panic!("Size of table != 0, but no headers left!");
564
                    }
565
                };
566
567
4.77k
                self.size -= last.len();
568
            }
569
570
4.77k
            self.entries.pop_back();
571
        }
572
8.10k
    }
573
}
574
575
// ===== impl DecoderError =====
576
577
impl From<Utf8Error> for DecoderError {
578
44
    fn from(_: Utf8Error) -> DecoderError {
579
        // TODO: Better error?
580
44
        DecoderError::InvalidUtf8
581
44
    }
582
}
583
584
impl From<header::InvalidHeaderValue> for DecoderError {
585
322
    fn from(_: header::InvalidHeaderValue) -> DecoderError {
586
        // TODO: Better error?
587
322
        DecoderError::InvalidUtf8
588
322
    }
589
}
590
591
impl From<header::InvalidHeaderName> for DecoderError {
592
4.49k
    fn from(_: header::InvalidHeaderName) -> DecoderError {
593
        // TODO: Better error
594
4.49k
        DecoderError::InvalidUtf8
595
4.49k
    }
596
}
597
598
impl From<method::InvalidMethod> for DecoderError {
599
360
    fn from(_: method::InvalidMethod) -> DecoderError {
600
        // TODO: Better error
601
360
        DecoderError::InvalidUtf8
602
360
    }
603
}
604
605
impl From<status::InvalidStatusCode> for DecoderError {
606
7
    fn from(_: status::InvalidStatusCode) -> DecoderError {
607
        // TODO: Better error
608
7
        DecoderError::InvalidUtf8
609
7
    }
610
}
611
612
impl From<DecoderError> for frame::Error {
613
14.9k
    fn from(src: DecoderError) -> Self {
614
14.9k
        frame::Error::Hpack(src)
615
14.9k
    }
616
}
617
618
/// Get an entry from the static table
619
12.3M
pub fn get_static(idx: usize) -> Header {
620
    use http::header::HeaderValue;
621
622
12.3M
    match idx {
623
128k
        1 => Header::Authority(BytesStr::from_static("")),
624
561k
        2 => Header::Method(Method::GET),
625
1.17M
        3 => Header::Method(Method::POST),
626
1.68M
        4 => Header::Path(BytesStr::from_static("/")),
627
32.9k
        5 => Header::Path(BytesStr::from_static("/index.html")),
628
54.2k
        6 => Header::Scheme(BytesStr::from_static("http")),
629
105k
        7 => Header::Scheme(BytesStr::from_static("https")),
630
1.38M
        8 => Header::Status(StatusCode::OK),
631
253k
        9 => Header::Status(StatusCode::NO_CONTENT),
632
28.8k
        10 => Header::Status(StatusCode::PARTIAL_CONTENT),
633
39.6k
        11 => Header::Status(StatusCode::NOT_MODIFIED),
634
70.1k
        12 => Header::Status(StatusCode::BAD_REQUEST),
635
18.0k
        13 => Header::Status(StatusCode::NOT_FOUND),
636
585k
        14 => Header::Status(StatusCode::INTERNAL_SERVER_ERROR),
637
1.26M
        15 => Header::Field {
638
1.26M
            name: header::ACCEPT_CHARSET,
639
1.26M
            value: HeaderValue::from_static(""),
640
1.26M
        },
641
71.2k
        16 => Header::Field {
642
71.2k
            name: header::ACCEPT_ENCODING,
643
71.2k
            value: HeaderValue::from_static("gzip, deflate"),
644
71.2k
        },
645
130k
        17 => Header::Field {
646
130k
            name: header::ACCEPT_LANGUAGE,
647
130k
            value: HeaderValue::from_static(""),
648
130k
        },
649
141k
        18 => Header::Field {
650
141k
            name: header::ACCEPT_RANGES,
651
141k
            value: HeaderValue::from_static(""),
652
141k
        },
653
112k
        19 => Header::Field {
654
112k
            name: header::ACCEPT,
655
112k
            value: HeaderValue::from_static(""),
656
112k
        },
657
117k
        20 => Header::Field {
658
117k
            name: header::ACCESS_CONTROL_ALLOW_ORIGIN,
659
117k
            value: HeaderValue::from_static(""),
660
117k
        },
661
175k
        21 => Header::Field {
662
175k
            name: header::AGE,
663
175k
            value: HeaderValue::from_static(""),
664
175k
        },
665
24.4k
        22 => Header::Field {
666
24.4k
            name: header::ALLOW,
667
24.4k
            value: HeaderValue::from_static(""),
668
24.4k
        },
669
247k
        23 => Header::Field {
670
247k
            name: header::AUTHORIZATION,
671
247k
            value: HeaderValue::from_static(""),
672
247k
        },
673
80.3k
        24 => Header::Field {
674
80.3k
            name: header::CACHE_CONTROL,
675
80.3k
            value: HeaderValue::from_static(""),
676
80.3k
        },
677
109k
        25 => Header::Field {
678
109k
            name: header::CONTENT_DISPOSITION,
679
109k
            value: HeaderValue::from_static(""),
680
109k
        },
681
48.4k
        26 => Header::Field {
682
48.4k
            name: header::CONTENT_ENCODING,
683
48.4k
            value: HeaderValue::from_static(""),
684
48.4k
        },
685
165k
        27 => Header::Field {
686
165k
            name: header::CONTENT_LANGUAGE,
687
165k
            value: HeaderValue::from_static(""),
688
165k
        },
689
47.1k
        28 => Header::Field {
690
47.1k
            name: header::CONTENT_LENGTH,
691
47.1k
            value: HeaderValue::from_static(""),
692
47.1k
        },
693
76.4k
        29 => Header::Field {
694
76.4k
            name: header::CONTENT_LOCATION,
695
76.4k
            value: HeaderValue::from_static(""),
696
76.4k
        },
697
23.0k
        30 => Header::Field {
698
23.0k
            name: header::CONTENT_RANGE,
699
23.0k
            value: HeaderValue::from_static(""),
700
23.0k
        },
701
750k
        31 => Header::Field {
702
750k
            name: header::CONTENT_TYPE,
703
750k
            value: HeaderValue::from_static(""),
704
750k
        },
705
635k
        32 => Header::Field {
706
635k
            name: header::COOKIE,
707
635k
            value: HeaderValue::from_static(""),
708
635k
        },
709
32.0k
        33 => Header::Field {
710
32.0k
            name: header::DATE,
711
32.0k
            value: HeaderValue::from_static(""),
712
32.0k
        },
713
34.6k
        34 => Header::Field {
714
34.6k
            name: header::ETAG,
715
34.6k
            value: HeaderValue::from_static(""),
716
34.6k
        },
717
141k
        35 => Header::Field {
718
141k
            name: header::EXPECT,
719
141k
            value: HeaderValue::from_static(""),
720
141k
        },
721
97.5k
        36 => Header::Field {
722
97.5k
            name: header::EXPIRES,
723
97.5k
            value: HeaderValue::from_static(""),
724
97.5k
        },
725
31.2k
        37 => Header::Field {
726
31.2k
            name: header::FROM,
727
31.2k
            value: HeaderValue::from_static(""),
728
31.2k
        },
729
37.8k
        38 => Header::Field {
730
37.8k
            name: header::HOST,
731
37.8k
            value: HeaderValue::from_static(""),
732
37.8k
        },
733
123k
        39 => Header::Field {
734
123k
            name: header::IF_MATCH,
735
123k
            value: HeaderValue::from_static(""),
736
123k
        },
737
58.5k
        40 => Header::Field {
738
58.5k
            name: header::IF_MODIFIED_SINCE,
739
58.5k
            value: HeaderValue::from_static(""),
740
58.5k
        },
741
125k
        41 => Header::Field {
742
125k
            name: header::IF_NONE_MATCH,
743
125k
            value: HeaderValue::from_static(""),
744
125k
        },
745
25.6k
        42 => Header::Field {
746
25.6k
            name: header::IF_RANGE,
747
25.6k
            value: HeaderValue::from_static(""),
748
25.6k
        },
749
39.8k
        43 => Header::Field {
750
39.8k
            name: header::IF_UNMODIFIED_SINCE,
751
39.8k
            value: HeaderValue::from_static(""),
752
39.8k
        },
753
82.5k
        44 => Header::Field {
754
82.5k
            name: header::LAST_MODIFIED,
755
82.5k
            value: HeaderValue::from_static(""),
756
82.5k
        },
757
36.5k
        45 => Header::Field {
758
36.5k
            name: header::LINK,
759
36.5k
            value: HeaderValue::from_static(""),
760
36.5k
        },
761
208k
        46 => Header::Field {
762
208k
            name: header::LOCATION,
763
208k
            value: HeaderValue::from_static(""),
764
208k
        },
765
68.9k
        47 => Header::Field {
766
68.9k
            name: header::MAX_FORWARDS,
767
68.9k
            value: HeaderValue::from_static(""),
768
68.9k
        },
769
50.8k
        48 => Header::Field {
770
50.8k
            name: header::PROXY_AUTHENTICATE,
771
50.8k
            value: HeaderValue::from_static(""),
772
50.8k
        },
773
40.8k
        49 => Header::Field {
774
40.8k
            name: header::PROXY_AUTHORIZATION,
775
40.8k
            value: HeaderValue::from_static(""),
776
40.8k
        },
777
52.0k
        50 => Header::Field {
778
52.0k
            name: header::RANGE,
779
52.0k
            value: HeaderValue::from_static(""),
780
52.0k
        },
781
249k
        51 => Header::Field {
782
249k
            name: header::REFERER,
783
249k
            value: HeaderValue::from_static(""),
784
249k
        },
785
29.1k
        52 => Header::Field {
786
29.1k
            name: header::REFRESH,
787
29.1k
            value: HeaderValue::from_static(""),
788
29.1k
        },
789
66.5k
        53 => Header::Field {
790
66.5k
            name: header::RETRY_AFTER,
791
66.5k
            value: HeaderValue::from_static(""),
792
66.5k
        },
793
47.9k
        54 => Header::Field {
794
47.9k
            name: header::SERVER,
795
47.9k
            value: HeaderValue::from_static(""),
796
47.9k
        },
797
53.3k
        55 => Header::Field {
798
53.3k
            name: header::SET_COOKIE,
799
53.3k
            value: HeaderValue::from_static(""),
800
53.3k
        },
801
85.5k
        56 => Header::Field {
802
85.5k
            name: header::STRICT_TRANSPORT_SECURITY,
803
85.5k
            value: HeaderValue::from_static(""),
804
85.5k
        },
805
28.8k
        57 => Header::Field {
806
28.8k
            name: header::TRANSFER_ENCODING,
807
28.8k
            value: HeaderValue::from_static(""),
808
28.8k
        },
809
40.3k
        58 => Header::Field {
810
40.3k
            name: header::USER_AGENT,
811
40.3k
            value: HeaderValue::from_static(""),
812
40.3k
        },
813
35.5k
        59 => Header::Field {
814
35.5k
            name: header::VARY,
815
35.5k
            value: HeaderValue::from_static(""),
816
35.5k
        },
817
18.9k
        60 => Header::Field {
818
18.9k
            name: header::VIA,
819
18.9k
            value: HeaderValue::from_static(""),
820
18.9k
        },
821
54.9k
        61 => Header::Field {
822
54.9k
            name: header::WWW_AUTHENTICATE,
823
54.9k
            value: HeaderValue::from_static(""),
824
54.9k
        },
825
0
        _ => unreachable!(),
826
    }
827
12.3M
}
828
829
#[cfg(test)]
830
mod test {
831
    use super::*;
832
833
    #[test]
834
    fn test_peek_u8() {
835
        let b = 0xff;
836
        let mut buf = Cursor::new(vec![b]);
837
        assert_eq!(peek_u8(&buf), Some(b));
838
        assert_eq!(buf.get_u8(), b);
839
        assert_eq!(peek_u8(&buf), None);
840
    }
841
842
    #[test]
843
    fn test_decode_string_empty() {
844
        let mut de = Decoder::new(0);
845
        let mut buf = BytesMut::new();
846
        let err = de.decode_string(&mut Cursor::new(&mut buf)).unwrap_err();
847
        assert_eq!(err, DecoderError::NeedMore(NeedMore::UnexpectedEndOfStream));
848
    }
849
850
    #[test]
851
    fn test_decode_empty() {
852
        let mut de = Decoder::new(0);
853
        let mut buf = BytesMut::new();
854
        let _: () = de.decode(&mut Cursor::new(&mut buf), |_| {}).unwrap();
855
    }
856
857
    #[test]
858
    fn test_decode_indexed_larger_than_table() {
859
        let mut de = Decoder::new(0);
860
861
        let mut buf = BytesMut::new();
862
        buf.extend([0b01000000, 0x80 | 2]);
863
        buf.extend(huff_encode(b"foo"));
864
        buf.extend([0x80 | 3]);
865
        buf.extend(huff_encode(b"bar"));
866
867
        let mut res = vec![];
868
        de.decode(&mut Cursor::new(&mut buf), |h| {
869
            res.push(h);
870
        })
871
        .unwrap();
872
873
        assert_eq!(res.len(), 1);
874
        assert_eq!(de.table.size(), 0);
875
876
        match res[0] {
877
            Header::Field {
878
                ref name,
879
                ref value,
880
            } => {
881
                assert_eq!(name, "foo");
882
                assert_eq!(value, "bar");
883
            }
884
            _ => panic!(),
885
        }
886
    }
887
888
    fn huff_encode(src: &[u8]) -> BytesMut {
889
        let mut buf = BytesMut::new();
890
        huffman::encode(src, &mut buf);
891
        buf
892
    }
893
894
    #[test]
895
    fn test_decode_continuation_header_with_non_huff_encoded_name() {
896
        let mut de = Decoder::new(0);
897
        let value = huff_encode(b"bar");
898
        let mut buf = BytesMut::new();
899
        // header name is non_huff encoded
900
        buf.extend([0b01000000, 3]);
901
        buf.extend(b"foo");
902
        // header value is partial
903
        buf.extend([0x80 | 3]);
904
        buf.extend(&value[0..1]);
905
906
        let mut res = vec![];
907
        let e = de
908
            .decode(&mut Cursor::new(&mut buf), |h| {
909
                res.push(h);
910
            })
911
            .unwrap_err();
912
        // decode error because the header value is partial
913
        assert_eq!(e, DecoderError::NeedMore(NeedMore::StringUnderflow));
914
915
        // extend buf with the remaining header value
916
        buf.extend(&value[1..]);
917
        de.decode(&mut Cursor::new(&mut buf), |h| {
918
            res.push(h);
919
        })
920
        .unwrap();
921
922
        assert_eq!(res.len(), 1);
923
        assert_eq!(de.table.size(), 0);
924
925
        match res[0] {
926
            Header::Field {
927
                ref name,
928
                ref value,
929
            } => {
930
                assert_eq!(name, "foo");
931
                assert_eq!(value, "bar");
932
            }
933
            _ => panic!(),
934
        }
935
    }
936
}