vakya 0.1.0-alpha.1

Completion-native HTTP building blocks for the Karmaio 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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
use super::syntax::{is_tchar as tchar, quoted_string_len, trim_ows as trim};
use super::{
    BodyMode,
    head::{HeadError, HeaderWorkspace, own_headers},
};
use crate::error::{Error, ErrorKind};
use http::{
    HeaderMap,
    header::{
        AUTHORIZATION, CONNECTION, CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, EXPECT, HOST,
        PROXY_AUTHENTICATE, PROXY_AUTHORIZATION, TE, TRAILER, TRANSFER_ENCODING, UPGRADE, WWW_AUTHENTICATE,
    },
};
use std::ops::Range;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct DecodeLimits {
    max_chunk_line_bytes: usize,
    max_trailer_bytes: usize,
    max_trailers: usize,
}

impl DecodeLimits {
    pub(super) const fn new(chunk_line: usize, trailer_bytes: usize, trailers: usize) -> Self {
        assert!(
            chunk_line > 0 && trailer_bytes > 0 && trailers > 0,
            "decoder limits must be nonzero"
        );
        Self {
            max_chunk_line_bytes: chunk_line,
            max_trailer_bytes: trailer_bytes,
            max_trailers: trailers,
        }
    }
}

impl Default for DecodeLimits {
    fn default() -> Self {
        Self::new(8 * 1024, 16 * 1024, 32)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum DecodeLimit {
    ChunkLineBytes,
    TrailerBytes,
    Trailers,
    TrailerMap,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum DecodeError {
    UnexpectedEof,
    InvalidChunkSize,
    InvalidChunkExtension,
    InvalidChunkTerminator,
    InvalidTrailer,
    ForbiddenTrailer,
    Limit(DecodeLimit),
}

impl std::fmt::Display for DecodeError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{self:?}")
    }
}

impl std::error::Error for DecodeError {}

impl From<DecodeError> for Error {
    fn from(error: DecodeError) -> Self {
        let kind = match error {
            DecodeError::Limit(_) => ErrorKind::Limit,
            _ => ErrorKind::InvalidMessage,
        };
        Self::with_source(kind, "received body framing failed", error)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) enum DecodeOutcome {
    NeedMore { consumed: usize },
    Data { payload: Range<usize>, consumed: usize },
    Trailers { trailers: HeaderMap, consumed: usize },
    End { consumed: usize },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ChunkState {
    Size { scanned: usize },
    Data { remaining: u64 },
    DataCrlf,
    Trailers { scanned: usize },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum DecoderState {
    Empty,
    Fixed { remaining: u64 },
    Chunked(ChunkState),
    UntilEof,
    Done,
    Failed(DecodeError),
}

#[derive(Debug)]
pub(super) struct BodyDecoder {
    state: DecoderState,
    limits: DecodeLimits,
    trailer_workspace: Option<HeaderWorkspace>,
}

impl BodyDecoder {
    #[cfg(test)]
    pub(super) fn new(mode: BodyMode) -> Self {
        Self::with_limits(mode, DecodeLimits::default())
    }

    pub(super) fn with_limits(mode: BodyMode, limits: DecodeLimits) -> Self {
        let state = match mode {
            BodyMode::None => DecoderState::Empty,
            BodyMode::Fixed(remaining) => DecoderState::Fixed { remaining },
            BodyMode::Chunked => DecoderState::Chunked(ChunkState::Size { scanned: 0 }),
            BodyMode::UntilEof => DecoderState::UntilEof,
        };
        Self {
            state,
            limits,
            trailer_workspace: None,
        }
    }

    /// Whether framing is already complete without consuming more input.
    /// This permits closing a delivered fixed body or trailers without asking
    /// the application for another frame solely to acknowledge EOF.
    pub(super) fn is_complete(&self) -> bool {
        matches!(
            self.state,
            DecoderState::Empty | DecoderState::Fixed { remaining: 0 } | DecoderState::Done
        )
    }

    /// Consume only the reported prefix before calling again. Payload ranges
    /// refer to this input and must be transferred or copied before its storage
    /// is reused. EOF applies after all supplied bytes, so a final data range can
    /// precede `End` or an incomplete-framing error on the next call.
    pub(super) fn decode(&mut self, input: &[u8], eof: bool) -> Result<DecodeOutcome, DecodeError> {
        match self.state {
            DecoderState::Empty | DecoderState::Fixed { remaining: 0 } => {
                self.state = DecoderState::Done;
                Ok(DecodeOutcome::End { consumed: 0 })
            }
            DecoderState::Fixed { remaining } if !input.is_empty() => {
                let count64 = u64::try_from(input.len()).unwrap_or(u64::MAX).min(remaining);
                let count = usize::try_from(count64).expect("count is bounded by input length");
                self.state = DecoderState::Fixed {
                    remaining: remaining - count64,
                };
                Ok(DecodeOutcome::Data {
                    payload: 0..count,
                    consumed: count,
                })
            }
            DecoderState::Fixed { .. } if eof => self.fail(DecodeError::UnexpectedEof),
            DecoderState::Fixed { .. } => Ok(DecodeOutcome::NeedMore { consumed: 0 }),
            DecoderState::Chunked(state) => self.decode_chunked(state, input, eof),
            DecoderState::UntilEof if !input.is_empty() => {
                if eof {
                    self.state = DecoderState::Done;
                }
                Ok(DecodeOutcome::Data {
                    payload: 0..input.len(),
                    consumed: input.len(),
                })
            }
            DecoderState::UntilEof if eof => {
                self.state = DecoderState::Done;
                Ok(DecodeOutcome::End { consumed: 0 })
            }
            DecoderState::UntilEof => Ok(DecodeOutcome::NeedMore { consumed: 0 }),
            DecoderState::Done => Ok(DecodeOutcome::End { consumed: 0 }),
            DecoderState::Failed(error) => Err(error),
        }
    }

    fn decode_chunked(&mut self, mut state: ChunkState, input: &[u8], eof: bool) -> Result<DecodeOutcome, DecodeError> {
        let mut cursor = 0;
        loop {
            let before = (state, cursor);
            match state {
                ChunkState::Size { scanned } => {
                    let (parsed, scanned) = match self.parse_chunk_line(&input[cursor..], scanned) {
                        Ok(parsed) => parsed,
                        Err(error) => return self.fail(error),
                    };
                    let Some((size, consumed)) = parsed else {
                        if eof {
                            return self.fail(DecodeError::UnexpectedEof);
                        }
                        self.state = DecoderState::Chunked(ChunkState::Size { scanned });
                        return Ok(DecodeOutcome::NeedMore { consumed: cursor });
                    };
                    cursor += consumed;
                    state = if size == 0 {
                        ChunkState::Trailers { scanned: 0 }
                    } else {
                        ChunkState::Data { remaining: size }
                    };
                }
                ChunkState::Data { remaining } => {
                    if cursor == input.len() {
                        if eof {
                            return self.fail(DecodeError::UnexpectedEof);
                        }
                        self.state = DecoderState::Chunked(state);
                        return Ok(DecodeOutcome::NeedMore { consumed: cursor });
                    }
                    let count64 = u64::try_from(input.len() - cursor).unwrap_or(u64::MAX).min(remaining);
                    let count = usize::try_from(count64).expect("count is bounded by input length");
                    let payload = cursor..cursor + count;
                    cursor += count;
                    let remaining = remaining - count64;
                    self.state = DecoderState::Chunked(if remaining == 0 {
                        ChunkState::DataCrlf
                    } else {
                        ChunkState::Data { remaining }
                    });
                    return Ok(DecodeOutcome::Data {
                        payload,
                        consumed: cursor,
                    });
                }
                ChunkState::DataCrlf => {
                    let remaining = &input[cursor..];
                    if remaining.len() < 2 {
                        if remaining.first().is_some_and(|byte| *byte != b'\r') {
                            return self.fail(DecodeError::InvalidChunkTerminator);
                        }
                        if eof {
                            return self.fail(DecodeError::UnexpectedEof);
                        }
                        self.state = DecoderState::Chunked(state);
                        return Ok(DecodeOutcome::NeedMore { consumed: cursor });
                    }
                    if &remaining[..2] != b"\r\n" {
                        return self.fail(DecodeError::InvalidChunkTerminator);
                    }
                    cursor += 2;
                    state = ChunkState::Size { scanned: 0 };
                }
                ChunkState::Trailers { scanned } => return self.decode_trailers(input, cursor, eof, scanned),
            }
            debug_assert!(state != before.0 || cursor > before.1, "chunk decoder made no progress");
        }
    }

    fn parse_chunk_line(
        &mut self,
        input: &[u8],
        mut scanned: usize,
    ) -> Result<(Option<(u64, usize)>, usize), DecodeError> {
        if input.len() < scanned {
            scanned = 0;
        }
        for index in scanned..input.len() {
            let byte = input[index];
            if byte == b'\n' {
                if index == 0 || input[index - 1] != b'\r' {
                    return Err(DecodeError::InvalidChunkSize);
                }
                let consumed = index + 1;
                if consumed > self.limits.max_chunk_line_bytes {
                    return Err(DecodeError::Limit(DecodeLimit::ChunkLineBytes));
                }
                return parse_chunk_line(&input[..index - 1]).map(|size| (Some((size, consumed)), 0));
            }
            if index > 0 && input[index - 1] == b'\r' {
                return Err(DecodeError::InvalidChunkSize);
            }
        }
        if input.len() > self.limits.max_chunk_line_bytes {
            Err(DecodeError::Limit(DecodeLimit::ChunkLineBytes))
        } else {
            Ok((None, input.len()))
        }
    }

    fn decode_trailers(
        &mut self,
        input: &[u8],
        cursor: usize,
        eof: bool,
        scanned: usize,
    ) -> Result<DecodeOutcome, DecodeError> {
        let available = &input[cursor..];
        let (trailer_bytes, scanned) = match self.trailer_end(available, scanned) {
            Ok(result) => result,
            Err(error) => return self.fail(error),
        };
        let Some(trailer_bytes) = trailer_bytes else {
            if eof {
                return self.fail(DecodeError::UnexpectedEof);
            }
            self.state = DecoderState::Chunked(ChunkState::Trailers { scanned });
            return Ok(DecodeOutcome::NeedMore { consumed: cursor });
        };
        let parsed = {
            let workspace = self
                .trailer_workspace
                .get_or_insert_with(|| HeaderWorkspace::new(self.limits.max_trailers));
            match httparse::parse_headers(&available[..trailer_bytes], workspace.for_headers()) {
                Ok(httparse::Status::Complete((consumed, headers))) => match own_headers(headers) {
                    Ok(trailers) => validate_trailers(&trailers).map(|()| Some((consumed, trailers))),
                    Err(HeadError::Limit(_)) => Err(DecodeError::Limit(DecodeLimit::TrailerMap)),
                    Err(_) => Err(DecodeError::InvalidTrailer),
                },
                Ok(httparse::Status::Partial) => Err(DecodeError::InvalidTrailer),
                Err(httparse::Error::TooManyHeaders) => Err(DecodeError::Limit(DecodeLimit::Trailers)),
                Err(_) => Err(DecodeError::InvalidTrailer),
            }
        };
        self.trailer_workspace
            .as_mut()
            .expect("chunked decoder has trailer workspace")
            .clear();
        match parsed {
            Ok(Some((trailer_bytes, trailers))) => {
                self.state = DecoderState::Done;
                Ok(DecodeOutcome::Trailers {
                    trailers,
                    consumed: cursor + trailer_bytes,
                })
            }
            Ok(None) => unreachable!("a complete trailer block must parse completely"),
            Err(error) => self.fail(error),
        }
    }

    fn trailer_end(&self, input: &[u8], mut scanned: usize) -> Result<(Option<usize>, usize), DecodeError> {
        if input.len() < scanned {
            scanned = 0;
        }
        let bounded = input.len().min(self.limits.max_trailer_bytes);
        for index in scanned.min(bounded)..bounded {
            if input[index] != b'\n' {
                continue;
            }
            let empty_line = index == 0
                || (index == 1 && input[0] == b'\r')
                || input[index - 1] == b'\n'
                || (index >= 2 && input[index - 2] == b'\n' && input[index - 1] == b'\r');
            if empty_line {
                return Ok((Some(index + 1), 0));
            }
        }
        if input.len() > self.limits.max_trailer_bytes {
            Err(DecodeError::Limit(DecodeLimit::TrailerBytes))
        } else {
            Ok((None, bounded))
        }
    }

    fn fail<T>(&mut self, error: DecodeError) -> Result<T, DecodeError> {
        self.state = DecoderState::Failed(error);
        Err(error)
    }
}

fn parse_chunk_line(line: &[u8]) -> Result<u64, DecodeError> {
    let extension = line.iter().position(|byte| *byte == b';').unwrap_or(line.len());
    let size_text = trim_end(&line[..extension]);
    if size_text.is_empty() || !size_text.iter().all(u8::is_ascii_hexdigit) {
        return Err(DecodeError::InvalidChunkSize);
    }
    let size = size_text.iter().try_fold(0_u64, |size, digit| {
        size.checked_mul(16)
            .and_then(|size| size.checked_add(u64::from(hex(*digit))))
            .ok_or(DecodeError::InvalidChunkSize)
    })?;
    validate_extensions(&line[extension..])?;
    Ok(size)
}

fn validate_extensions(mut input: &[u8]) -> Result<(), DecodeError> {
    while !input.is_empty() {
        input = trim(input);
        if input.first() != Some(&b';') {
            return Err(DecodeError::InvalidChunkExtension);
        }
        input = trim(&input[1..]);
        let name = input.iter().take_while(|byte| tchar(**byte)).count();
        if name == 0 {
            return Err(DecodeError::InvalidChunkExtension);
        }
        input = trim(&input[name..]);
        if input.first() == Some(&b'=') {
            input = trim(&input[1..]);
            let value = if input.first() == Some(&b'"') {
                quoted_string_len(input).ok_or(DecodeError::InvalidChunkExtension)?
            } else {
                let value = input.iter().take_while(|byte| tchar(**byte)).count();
                if value == 0 {
                    return Err(DecodeError::InvalidChunkExtension);
                }
                value
            };
            input = trim(&input[value..]);
        }
        if !input.is_empty() && input.first() != Some(&b';') {
            return Err(DecodeError::InvalidChunkExtension);
        }
    }
    Ok(())
}

pub(super) fn validate_trailers(trailers: &HeaderMap) -> Result<(), DecodeError> {
    for name in trailers.keys() {
        if [
            TRANSFER_ENCODING,
            CONTENT_LENGTH,
            HOST,
            CONNECTION,
            TRAILER,
            TE,
            UPGRADE,
            EXPECT,
            AUTHORIZATION,
            PROXY_AUTHORIZATION,
            PROXY_AUTHENTICATE,
            WWW_AUTHENTICATE,
            CONTENT_ENCODING,
            CONTENT_TYPE,
            CONTENT_RANGE,
        ]
        .contains(name)
        {
            return Err(DecodeError::ForbiddenTrailer);
        }
    }
    Ok(())
}

fn trim_end(mut value: &[u8]) -> &[u8] {
    while value.last().is_some_and(|byte| matches!(byte, b' ' | b'\t')) {
        value = &value[..value.len() - 1];
    }
    value
}

fn hex(byte: u8) -> u8 {
    match byte {
        b'0'..=b'9' => byte - b'0',
        b'a'..=b'f' => byte - b'a' + 10,
        b'A'..=b'F' => byte - b'A' + 10,
        _ => unreachable!(),
    }
}

#[cfg(test)]
mod tests {
    use super::{BodyDecoder, DecodeError, DecodeLimit, DecodeLimits, DecodeOutcome};
    use crate::proto::h1::BodyMode;
    use http::HeaderMap;

    #[test]
    fn non_chunked_modes_preserve_boundaries_and_eof() {
        let mut empty = BodyDecoder::new(BodyMode::None);
        assert_eq!(
            empty.decode(b"next", false).unwrap(),
            DecodeOutcome::End { consumed: 0 }
        );

        let mut fixed = BodyDecoder::new(BodyMode::Fixed(4));
        assert_eq!(
            fixed.decode(b"bodyNEXT", false).unwrap(),
            DecodeOutcome::Data {
                payload: 0..4,
                consumed: 4
            }
        );
        assert_eq!(
            fixed.decode(b"NEXT", false).unwrap(),
            DecodeOutcome::End { consumed: 0 }
        );

        let mut incomplete = BodyDecoder::new(BodyMode::Fixed(1));
        assert_eq!(
            incomplete.decode(&[], false).unwrap(),
            DecodeOutcome::NeedMore { consumed: 0 }
        );
        assert_eq!(incomplete.decode(&[], true).unwrap_err(), DecodeError::UnexpectedEof);
        assert_eq!(incomplete.decode(b"x", false).unwrap_err(), DecodeError::UnexpectedEof);

        let mut eof = BodyDecoder::new(BodyMode::UntilEof);
        assert_eq!(
            eof.decode(b"last", true).unwrap(),
            DecodeOutcome::Data {
                payload: 0..4,
                consumed: 4
            }
        );
        assert_eq!(eof.decode(&[], false).unwrap(), DecodeOutcome::End { consumed: 0 });
    }

    #[test]
    fn fixed_body_decodes_at_every_fragment_size() {
        let body = b"fragmented fixed body";
        for width in 1..=body.len() {
            let mut decoder = BodyDecoder::new(BodyMode::Fixed(body.len() as u64));
            let mut data = Vec::new();
            for input in body.chunks(width) {
                let DecodeOutcome::Data { payload, .. } = decoder.decode(input, false).unwrap() else {
                    panic!("expected data")
                };
                data.extend_from_slice(&input[payload]);
            }
            assert_eq!(decoder.decode(&[], false).unwrap(), DecodeOutcome::End { consumed: 0 });
            assert_eq!(data, body);
        }
    }

    #[test]
    fn chunked_data_trailers_and_excess_survive_every_fragment_size() {
        let wire = b"4;name=token\r\nWiki\r\n5\r\npedia\r\n0;done=\"yes\"\r\nX-Checksum: good\r\n\r\nNEXT";
        for width in 1..=wire.len() {
            let decoded = fragmented(wire, width).unwrap();
            assert_eq!(decoded.data, b"Wikipedia");
            assert_eq!(decoded.trailers["x-checksum"], "good");
            assert_eq!(decoded.remaining, b"NEXT");
        }
    }

    #[test]
    fn chunk_sizes_accept_case_and_empty_trailers() {
        for size in *b"aA" {
            let mut wire = vec![size];
            wire.extend_from_slice(b"\r\n0123456789\r\n0\r\n\r\nafter");
            let decoded = fragmented(&wire, 3).unwrap();
            assert_eq!(decoded.data, b"0123456789");
            assert!(decoded.trailers.is_empty());
            assert_eq!(decoded.remaining, b"after");
        }
    }

    #[test]
    fn fragmented_chunk_metadata_advances_incremental_scans() {
        let mut decoder = BodyDecoder::new(BodyMode::Chunked);
        let mut line = b"1;name=".to_vec();
        line.extend(std::iter::repeat_n(b'a', 256));
        line.push(b'\r');

        let mut pending = Vec::new();
        for byte in line {
            pending.push(byte);
            assert_eq!(
                decoder.decode(&pending, false).unwrap(),
                DecodeOutcome::NeedMore { consumed: 0 }
            );
            assert!(matches!(
                decoder.state,
                super::DecoderState::Chunked(super::ChunkState::Size { scanned })
                    if scanned == pending.len()
            ));
        }
        pending.push(b'\n');
        assert_eq!(
            decoder.decode(&pending, false).unwrap(),
            DecodeOutcome::NeedMore {
                consumed: pending.len()
            }
        );

        let mut decoder = BodyDecoder::new(BodyMode::Chunked);
        assert_eq!(
            decoder.decode(b"0\r\n", false).unwrap(),
            DecodeOutcome::NeedMore { consumed: 3 }
        );
        let mut trailer = b"X-Long: ".to_vec();
        trailer.extend(std::iter::repeat_n(b'a', 256));
        trailer.extend_from_slice(b"\r\n\r");
        pending.clear();
        for byte in trailer {
            pending.push(byte);
            assert_eq!(
                decoder.decode(&pending, false).unwrap(),
                DecodeOutcome::NeedMore { consumed: 0 }
            );
            assert!(matches!(
                decoder.state,
                super::DecoderState::Chunked(super::ChunkState::Trailers { scanned })
                    if scanned == pending.len()
            ));
            assert!(decoder.trailer_workspace.is_none());
        }
        pending.push(b'\n');
        assert!(matches!(
            decoder.decode(&pending, false).unwrap(),
            DecodeOutcome::Trailers { consumed, .. } if consumed == pending.len()
        ));
        assert!(decoder.trailer_workspace.is_some());
    }

    #[test]
    fn malformed_chunks_fail_terminally() {
        for wire in [
            b"10000000000000000\r\n".as_slice(),
            b"x\r\n".as_slice(),
            b"1;=bad\r\n".as_slice(),
            b"1;name=\r\n".as_slice(),
        ] {
            let mut decoder = BodyDecoder::new(BodyMode::Chunked);
            assert!(matches!(
                decoder.decode(wire, false),
                Err(DecodeError::InvalidChunkSize | DecodeError::InvalidChunkExtension)
            ));
        }
        let mut terminator = BodyDecoder::new(BodyMode::Chunked);
        assert!(matches!(
            terminator.decode(b"1\r\naX", false).unwrap(),
            DecodeOutcome::Data { .. }
        ));
        assert_eq!(
            terminator.decode(b"X", false).unwrap_err(),
            DecodeError::InvalidChunkTerminator
        );
        assert_eq!(
            terminator.decode(b"ignored", false).unwrap_err(),
            DecodeError::InvalidChunkTerminator
        );
    }

    #[test]
    fn premature_eof_is_distinct_from_waiting() {
        for wire in [b"1\r\n".as_slice(), b"1\r\na\r".as_slice(), b"0\r\nX".as_slice()] {
            let mut decoder = BodyDecoder::new(BodyMode::Chunked);
            let mut input = wire;
            loop {
                match decoder.decode(input, true) {
                    Ok(DecodeOutcome::Data { consumed, .. }) => input = &input[consumed..],
                    Err(error) => {
                        assert_eq!(error, DecodeError::UnexpectedEof);
                        break;
                    }
                    other => panic!("unexpected EOF outcome: {other:?}"),
                }
            }
        }
    }

    #[test]
    fn decoder_enforces_chunk_and_trailer_limits() {
        let limits = DecodeLimits::new(4, 8, 1);
        let mut line = BodyDecoder::with_limits(BodyMode::Chunked, limits);
        assert_eq!(
            line.decode(b"1;x\r\n", false).unwrap_err(),
            DecodeError::Limit(DecodeLimit::ChunkLineBytes)
        );
        let mut bytes = BodyDecoder::with_limits(BodyMode::Chunked, limits);
        assert_eq!(
            bytes.decode(b"0\r\nLong: value\r\n\r\n", false).unwrap_err(),
            DecodeError::Limit(DecodeLimit::TrailerBytes)
        );
        let count_limits = DecodeLimits::new(4, 64, 1);
        let mut count = BodyDecoder::with_limits(BodyMode::Chunked, count_limits);
        assert_eq!(
            count.decode(b"0\r\nA: 1\r\nB: 2\r\n\r\n", false).unwrap_err(),
            DecodeError::Limit(DecodeLimit::Trailers)
        );

        let exact_limits = DecodeLimits::new(3, 8, 1);
        let mut exact = BodyDecoder::with_limits(BodyMode::Chunked, exact_limits);
        assert!(matches!(
            exact.decode(b"1\r\nx\r\n0\r\nA: 1\r\n\r\n", false).unwrap(),
            DecodeOutcome::Data { .. }
        ));
        assert!(matches!(
            exact.decode(b"\r\n0\r\nA: 1\r\n\r\n", false).unwrap(),
            DecodeOutcome::Trailers { .. }
        ));
    }

    #[test]
    fn forbidden_and_malformed_trailers_are_rejected() {
        let mut forbidden = BodyDecoder::new(BodyMode::Chunked);
        assert_eq!(
            forbidden.decode(b"0\r\nContent-Length: 1\r\n\r\n", false).unwrap_err(),
            DecodeError::ForbiddenTrailer
        );
        let mut malformed = BodyDecoder::new(BodyMode::Chunked);
        assert_eq!(
            malformed.decode(b"0\r\nBad trailer\r\n\r\n", false).unwrap_err(),
            DecodeError::InvalidTrailer
        );
    }

    struct Decoded {
        data: Vec<u8>,
        trailers: HeaderMap,
        remaining: Vec<u8>,
    }

    fn fragmented(wire: &[u8], width: usize) -> Result<Decoded, DecodeError> {
        let mut decoder = BodyDecoder::new(BodyMode::Chunked);
        let (mut pending, mut source, mut data) = (Vec::new(), 0, Vec::new());
        let trailers = loop {
            if source < wire.len() {
                let end = (source + width).min(wire.len());
                pending.extend_from_slice(&wire[source..end]);
                source = end;
            }
            match decoder.decode(&pending, false)? {
                DecodeOutcome::NeedMore { consumed } => {
                    pending.drain(..consumed);
                    assert!(source < wire.len());
                }
                DecodeOutcome::Data { payload, consumed } => {
                    data.extend_from_slice(&pending[payload]);
                    pending.drain(..consumed);
                }
                DecodeOutcome::Trailers { trailers, consumed } => {
                    pending.drain(..consumed);
                    break trailers;
                }
                DecodeOutcome::End { .. } => panic!("body ended before trailer outcome"),
            }
        };
        assert_eq!(decoder.decode(&pending, false)?, DecodeOutcome::End { consumed: 0 });
        pending.extend_from_slice(&wire[source..]);
        Ok(Decoded {
            data,
            trailers,
            remaining: pending,
        })
    }
}