h3x 0.2.0

High-performance zero-copy DHTTP/3 implementation
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
use std::{
    collections::{BTreeMap, BTreeSet, VecDeque},
    ops::DerefMut,
    pin::{Pin, pin},
    sync::Arc,
};

use bytes::Bytes;
use futures::{Sink, SinkExt, Stream, StreamExt, stream};
use snafu::{Report, Snafu};
use tokio::{
    io::{AsyncBufRead, AsyncReadExt, AsyncWrite},
    sync::Mutex as AsyncMutex,
};

use crate::{
    buflist::BufList,
    codec::{
        DecodeFrom, DecodeStreamError, EncodeError, EncodeExt, EncodeInto, EncodeStreamError, Feed,
    },
    connection::StreamError,
    dhttp::{frame::Frame, settings::Settings},
    error::{
        Code, ErrorScope, H3CriticalStreamClosed, H3Error, H3FrameDecodeError, H3InternalError,
    },
    qpack::{
        algorithm::Algorithm,
        decoder::DecoderInstruction,
        dynamic::DynamicTable,
        field::FieldLine,
        integer::{decode_integer, encode_integer},
        r#static,
        string::{decode_string, encode_string},
    },
    quic::{self, GetStreamId, GetStreamIdExt},
};

#[derive(Debug)]
pub struct EncoderState {
    pub(crate) settings: Arc<Settings>,
    pub(crate) dynamic_table: DynamicTable,
    /// Multiple entries may have the same name. The encoder can choose to use the most appropriate one.
    // ∀ n: Bytes: entries[name_indices[n]].name == n
    pub(crate) name_indices: BTreeMap<Bytes, BTreeSet<u64>>,
    /// Multiple entries may have the same value. The encoder can choose to use the most appropriate one.
    // ∀ v: Bytes: entries[value_indices[n]].value == v
    pub(crate) value_indices: BTreeMap<Bytes, BTreeSet<u64>>,

    // stream_id -> unacknowledged_sections: [section_max_referenced_index]
    pub(crate) blocking_streams: BTreeMap<u64, VecDeque<u64>>,

    pub(crate) pending_instructions: VecDeque<EncoderInstruction>,
}

impl EncoderState {
    pub const fn new(settings: Arc<Settings>) -> Self {
        Self {
            settings,
            dynamic_table: DynamicTable::new(),
            name_indices: BTreeMap::new(),
            value_indices: BTreeMap::new(),
            blocking_streams: BTreeMap::new(),
            pending_instructions: VecDeque::new(),
        }
    }
}

#[derive(Debug, Snafu)]
#[snafu(module)]
pub enum QPackDecoderStreamError {
    #[snafu(display("acknowledging non-existent or non-blocking stream"))]
    AcknowledgeNonExistSection { stream_id: u64 },
    #[snafu(display(
        "known received count increment overflow (known_received_count + increment > inserted_count)"
    ))]
    IncrementKnownReceivedCountOverflow,
    #[snafu(display("insert count increment of zero is not allowed"))]
    IncrementZero,
}

impl H3Error for QPackDecoderStreamError {
    fn code(&self) -> Code {
        Code::QPACK_DECODER_STREAM_ERROR
    }
    fn scope(&self) -> ErrorScope {
        ErrorScope::Connection
    }
}

#[derive(Debug, Snafu)]
#[snafu(module)]
pub enum QPackEncoderError {
    #[snafu(display(
        "dynamic table capacity {capacity} exceeds SETTINGS_QPACK_MAX_TABLE_CAPACITY {max}"
    ))]
    CapacityExceedsMax { capacity: u64, max: u64 },
    #[snafu(display("cannot evict entries: no evictable entries in dynamic table"))]
    CannotEvict,
}

impl EncoderState {
    pub(crate) fn emit(&mut self, instruction: EncoderInstruction) {
        self.pending_instructions.push_back(instruction);
    }

    pub fn settings(&self) -> &Settings {
        &self.settings
    }

    pub fn table_capacity(&self) -> u64 {
        self.dynamic_table.capacity
    }

    pub fn table_size(&self) -> u64 {
        self.dynamic_table.size
    }

    pub fn table_inserted_count(&self) -> u64 {
        self.dynamic_table.inserted_count
    }

    pub fn table_known_received_count(&self) -> u64 {
        self.dynamic_table.known_received_count
    }

    pub fn table_dropped_count(&self) -> u64 {
        self.dynamic_table.dropped_count
    }

    pub fn table_remaining(&self) -> u64 {
        self.table_capacity() - self.table_size()
    }

    pub fn get_entry(&self, index: u64) -> Option<&FieldLine> {
        self.dynamic_table.get(index)
    }

    pub fn find_name(&self, name: &Bytes) -> Option<&BTreeSet<u64>> {
        self.name_indices.get(name)
    }

    pub fn find_value(&self, value: &Bytes) -> Option<&BTreeSet<u64>> {
        self.value_indices.get(value)
    }

    pub fn pending_instructions(&self) -> &VecDeque<EncoderInstruction> {
        &self.pending_instructions
    }

    pub(crate) fn index(&mut self, field_line: FieldLine) -> u64 {
        let index = self.dynamic_table.index(field_line.clone());
        // update name_indices
        self.name_indices
            .entry(field_line.name)
            .or_default()
            .insert(index);
        // update value_indices
        self.value_indices
            .entry(field_line.value)
            .or_default()
            .insert(index);
        index
    }

    pub fn entries_evictable(&self) -> bool {
        self.dynamic_table.evictable()
    }

    pub(crate) fn evict_entry(&mut self) -> (u64, FieldLine) {
        use std::collections::btree_map::Entry::Occupied;
        let (absolute_index, entry) = self.dynamic_table.evict();
        // update name_indices
        if let Occupied(mut indices) = self.name_indices.entry(entry.name.clone()) {
            indices.get_mut().remove(&absolute_index);
            if indices.get().is_empty() {
                self.name_indices.remove(&entry.name);
            }
        }
        // update value_indices
        if let Occupied(mut indices) = self.value_indices.entry(entry.value.clone()) {
            indices.get_mut().remove(&absolute_index);
            if indices.get().is_empty() {
                self.value_indices.remove(&entry.value);
            }
        }

        (absolute_index, entry)
    }

    pub fn set_max_table_capacity(&mut self, new_capacity: u64) -> Result<(), QPackEncoderError> {
        // RFC 9204 §4.3.1: The encoder MUST NOT set a dynamic table capacity that exceeds SETTINGS_QPACK_MAX_TABLE_CAPACITY
        let max = self.settings.qpack_max_table_capacity().into_inner();
        if new_capacity > max {
            return Err(QPackEncoderError::CapacityExceedsMax {
                capacity: new_capacity,
                max,
            });
        }
        self.dynamic_table.capacity = new_capacity;
        while self.table_size() > new_capacity {
            // RFC 9204 §2.1.1: entries are evictable only when acknowledged
            if !self.entries_evictable() {
                return Err(QPackEncoderError::CannotEvict);
            }
            self.evict_entry();
        }
        self.emit(EncoderInstruction::SetDynamicTableCapacity {
            capacity: new_capacity,
        });
        Ok(())
    }

    pub fn insert_with_name_reference(
        &mut self,
        is_static: bool,
        name_index: u64,
        huffman: bool,
        value: Bytes,
    ) -> Result<u64, QPackEncoderError> {
        let name = match is_static {
            true => Bytes::from_static(
                r#static::get_name(name_index)
                    .unwrap_or_else(|| {
                        panic!(
                            "Referenced name index {name_index} does not exist in the static table"
                        )
                    })
                    .as_bytes(),
            ),
            false => self
                .get_entry(name_index)
                .unwrap_or_else(|| {
                    panic!("Referenced name index {name_index} does not exist in the dynamic table")
                })
                .name
                .clone(),
        };

        let entry = FieldLine { name, value };
        while self.table_remaining() < entry.size() {
            // RFC 9204 §2.1.1: entries are evictable only when acknowledged
            if !self.entries_evictable() {
                return Err(QPackEncoderError::CannotEvict);
            }
            self.evict_entry();
        }
        let new_entry_index = self.index(entry.clone());
        // RFC 9204 §3.2.4: Encoder instruction uses relative index.
        // relative = new_entry_index - absolute_name_index - 1
        let wire_name_index = if is_static {
            name_index
        } else {
            new_entry_index - name_index - 1
        };
        self.emit(EncoderInstruction::InsertWithNameReference {
            is_static,
            name_index: wire_name_index,
            huffman,
            value: entry.value,
        });
        Ok(new_entry_index)
    }

    pub fn insert_with_literal_name(
        &mut self,
        name_huffman: bool,
        name: Bytes,
        value_huffman: bool,
        value: Bytes,
    ) -> Result<u64, QPackEncoderError> {
        let entry = FieldLine { name, value };
        while self.table_remaining() < entry.size() {
            // RFC 9204 §2.1.1: entries are evictable only when acknowledged
            if !self.entries_evictable() {
                return Err(QPackEncoderError::CannotEvict);
            }
            self.evict_entry();
        }
        let new_entry_index = self.index(entry.clone());
        self.emit(EncoderInstruction::InsertWithLiteralName {
            name_huffman,
            name: entry.name,
            value_huffman,
            value: entry.value,
        });
        Ok(new_entry_index)
    }

    pub fn duplicate(&mut self, index: u64) -> Result<u64, QPackEncoderError> {
        let duplicated_entry = self
            .get_entry(index)
            .unwrap_or_else(|| {
                panic!("Referenced entry {index} does not exist in the dynamic table")
            })
            .clone();
        while self.table_remaining() < duplicated_entry.size() {
            // RFC 9204 §2.1.1: entries are evictable only when acknowledged
            if !self.entries_evictable() {
                return Err(QPackEncoderError::CannotEvict);
            }
            self.evict_entry();
        }
        let new_entry_index = self.index(duplicated_entry);
        // RFC 9204 §3.2.4: Encoder instruction uses relative index.
        // relative = new_entry_index - absolute_index - 1
        let wire_index = new_entry_index - index - 1;
        self.pending_instructions
            .push_back(EncoderInstruction::Duplicate { index: wire_index });
        Ok(new_entry_index)
    }

    pub fn entries(&self) -> impl Iterator<Item = (u64, &FieldLine)> {
        self.dynamic_table.entries()
    }

    pub(crate) fn on_section_acknowledgment(
        &mut self,
        stream_id: u64,
    ) -> Result<(), QPackDecoderStreamError> {
        use std::collections::btree_map::Entry::Occupied;

        let Occupied(mut blocking_stream) = self.blocking_streams.entry(stream_id) else {
            return Err(QPackDecoderStreamError::AcknowledgeNonExistSection { stream_id });
        };
        let max_referenced_index = blocking_stream
            .get_mut()
            .pop_front()
            .expect("blocking stream entry is non-empty");
        if blocking_stream.get().is_empty() {
            blocking_stream.remove();
        }

        if max_referenced_index >= self.table_known_received_count() {
            self.dynamic_table.known_received_count = max_referenced_index + 1;
        }

        Ok(())
    }

    pub(crate) fn on_stream_cancellation(&mut self, stream_id: u64) {
        self.blocking_streams.remove(&stream_id);
    }

    pub(crate) fn on_insert_count_increment(
        &mut self,
        increment: u64,
    ) -> Result<(), QPackDecoderStreamError> {
        // RFC 9204 §4.4.3: An increment of 0 is a connection error of type QPACK_DECODER_STREAM_ERROR
        if increment == 0 {
            return Err(QPackDecoderStreamError::IncrementZero);
        }
        if self.table_inserted_count() - self.table_known_received_count() < increment {
            return Err(QPackDecoderStreamError::IncrementKnownReceivedCountOverflow);
        }
        self.dynamic_table.increment_known_received_count(increment);
        Ok(())
    }
}

pub struct Encoder<Es, Ds> {
    pub(crate) state: AsyncMutex<EncoderState>,
    pub(crate) encoder_stream: AsyncMutex<Feed<Es, EncoderInstruction>>,
    pub(crate) decoder_stream: AsyncMutex<Ds>,
}

#[derive(Debug, Snafu)]
pub enum EncodeHeaderSectionError {
    #[snafu(transparent)]
    Stream { source: StreamError },
    #[snafu(transparent)]
    Encode { source: EncodeError },
}

impl From<quic::StreamError> for EncodeHeaderSectionError {
    fn from(error: quic::StreamError) -> Self {
        EncodeHeaderSectionError::Stream {
            source: error.into(),
        }
    }
}

impl<Es, Ds> Encoder<Es, Ds>
where
    Es: Sink<EncoderInstruction, Error = StreamError> + Unpin,
    Ds: Stream<Item = Result<DecoderInstruction, StreamError>> + Unpin,
{
    pub fn new(settings: Arc<Settings>, encoder_stream: Es, decoder_stream: Ds) -> Self {
        Self {
            state: AsyncMutex::new(EncoderState::new(settings)),
            encoder_stream: AsyncMutex::new(Feed::new(encoder_stream)),
            decoder_stream: AsyncMutex::new(decoder_stream),
        }
    }

    pub async fn apply_settings(&self, settings: Arc<Settings>) {
        {
            let mut state = self.state.lock().await;
            let peer_max_capacity = settings.qpack_max_table_capacity().into_inner();
            state.settings = settings;
            if peer_max_capacity > 0 && state.table_capacity() == 0 {
                let _ = state.set_max_table_capacity(peer_max_capacity);
            }
        }
        // Flush the SetDynamicTableCapacity instruction immediately so the
        // decoder knows the new capacity before any header sections arrive.
        let _ = self.flush_instructions().await;
    }

    pub async fn encode(
        &self,
        field_section: impl IntoIterator<Item = FieldLine> + Send,
        algorithm: &(impl Algorithm + ?Sized),
        stream: impl GetStreamId,
    ) -> Result<Frame<BufList>, EncodeHeaderSectionError> {
        tokio::pin!(stream);
        let stream_id = stream.stream_id().await?.into_inner();

        let (field_section_prefix, field_line_representations);
        {
            let mut state = self.state.lock().await;

            // RFC 9204 §2.1.2: Determine whether this stream may block the decoder.
            let already_tracked = state.blocking_streams.contains_key(&stream_id);
            let peer_max = state.settings.qpack_blocked_streams().into_inner();
            let may_block = already_tracked || (state.blocking_streams.len() as u64) < peer_max;

            let output = algorithm
                .compress(&mut state, field_section, may_block)
                .await;
            field_section_prefix = output.prefix;
            field_line_representations = output.representations;

            if let Some(max_referenced_index) = output.max_referenced_index {
                state
                    .blocking_streams
                    .entry(stream_id)
                    .or_default()
                    .push_back(max_referenced_index);
            }
        };

        let header_frame = BufList::new()
            .encode((field_section_prefix, field_line_representations))
            .await?;

        Ok(header_frame)
    }

    fn pending_instructions(&self) -> impl Stream<Item = EncoderInstruction> + '_ {
        stream::unfold(self, |this| async move {
            let instruction = {
                let mut state = this.state.lock().await;
                state.pending_instructions.pop_front()
            };
            instruction.map(|inst| (inst, this))
        })
    }

    pub async fn flush_instructions(&self) -> Result<(), StreamError> {
        let mut encoder_stream = self.encoder_stream.lock().await;
        let mut encoder_stream = Pin::new(encoder_stream.deref_mut());
        let instructions = self.pending_instructions();
        encoder_stream.as_mut().send_all(instructions).await?;
        encoder_stream.as_mut().flush().await?;
        Ok(())
    }

    pub async fn receive_instruction(&self) -> Result<(), StreamError> {
        let instruction = {
            let mut decoder_stream = self.decoder_stream.lock().await;
            let instruction = decoder_stream.next().await;
            instruction.ok_or(H3CriticalStreamClosed::QPackDecoder)??
        };
        let mut state = self.state.lock().await;
        match instruction {
            DecoderInstruction::SectionAcknowledgment { stream_id } => {
                state.on_section_acknowledgment(stream_id)?
            }
            DecoderInstruction::StreamCancellation { stream_id } => {
                state.on_stream_cancellation(stream_id)
            }
            DecoderInstruction::InsertCountIncrement { increment } => {
                state.on_insert_count_increment(increment)?
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EncoderInstruction {
    /// ``` ignore
    ///   0   1   2   3   4   5   6   7
    /// +---+---+---+---+---+---+---+---+
    /// | 0 | 0 | 1 |   Capacity (5+)   |
    /// +---+---+---+-------------------+
    /// ```
    SetDynamicTableCapacity { capacity: u64 },
    /// ``` ignore
    ///   0   1   2   3   4   5   6   7
    /// +---+---+---+---+---+---+---+---+
    /// | 1 | T |    Name Index (6+)    |
    /// +---+---+-----------------------+
    /// | H |     Value Length (7+)     |
    /// +---+---------------------------+
    /// |  Value String (Length bytes)  |
    /// +-------------------------------+
    /// ```
    InsertWithNameReference {
        is_static: bool,
        name_index: u64,
        huffman: bool,
        value: Bytes,
    },
    /// ``` ignore
    ///   0   1   2   3   4   5   6   7
    /// +---+---+---+---+---+---+---+---+
    /// | 0 | 1 | H | Name Length (5+)  |
    /// +---+---+---+-------------------+
    /// |  Name String (Length bytes)   |
    /// +---+---------------------------+
    /// | H |     Value Length (7+)     |
    /// +---+---------------------------+
    /// |  Value String (Length bytes)  |
    /// +-------------------------------+
    /// ```
    InsertWithLiteralName {
        name_huffman: bool,
        name: Bytes,
        value_huffman: bool,
        value: Bytes,
    },
    /// ``` ignore
    ///   0   1   2   3   4   5   6   7
    /// +---+---+---+---+---+---+---+---+
    /// | 0 | 0 | 0 |    Index (5+)     |
    /// +---+---+---+-------------------+
    /// ```
    Duplicate { index: u64 },
}

impl<S: AsyncBufRead + Send> DecodeFrom<S> for EncoderInstruction {
    type Error = StreamError;

    async fn decode_from(stream: S) -> Result<Self, Self::Error> {
        let decode = async move {
            let mut stream = pin!(stream);
            let prefix = stream.read_u8().await?;
            match prefix {
                // 1xxxxxxx — Insert with Name Reference (6-bit prefix)
                prefix if prefix & 0b1000_0000 == 0b1000_0000 => {
                    let is_static = (prefix & 0b0100_0000) != 0;
                    let name_index = decode_integer(stream.as_mut(), prefix, 6).await?;
                    let value_prefix = stream.read_u8().await?;
                    let (huffman, value) =
                        decode_string(stream.as_mut(), value_prefix, 1 + 7).await?;
                    Ok(EncoderInstruction::InsertWithNameReference {
                        is_static,
                        name_index,
                        huffman,
                        value,
                    })
                }
                // 01xxxxxx — Insert with Literal Name (5-bit name prefix)
                prefix if prefix & 0b1100_0000 == 0b0100_0000 => {
                    let name_prefix = prefix;
                    let (name_huffman, name) =
                        decode_string(stream.as_mut(), name_prefix, 1 + 5).await?;
                    let value_prefix = stream.read_u8().await?;
                    let (value_huffman, value) =
                        decode_string(stream.as_mut(), value_prefix, 1 + 7).await?;
                    Ok(EncoderInstruction::InsertWithLiteralName {
                        name_huffman,
                        name,
                        value_huffman,
                        value,
                    })
                }
                // 001xxxxx — Set Dynamic Table Capacity (5-bit prefix)
                prefix if prefix & 0b1110_0000 == 0b0010_0000 => {
                    let capacity = decode_integer(stream, prefix, 5).await?;
                    Ok(EncoderInstruction::SetDynamicTableCapacity { capacity })
                }
                // 000xxxxx — Duplicate (5-bit prefix)
                _ => {
                    let index = decode_integer(stream, prefix, 5).await?;
                    Ok(EncoderInstruction::Duplicate { index })
                }
            }
        };
        decode.await.map_err(|error: DecodeStreamError| {
            error.map_stream_closed(
                |_reset_code| H3CriticalStreamClosed::QPackEncoder.into(),
                |decode_error| {
                    H3FrameDecodeError {
                        source: decode_error,
                    }
                    .into()
                },
            )
        })
    }
}

impl<S> EncodeInto<S> for EncoderInstruction
where
    S: AsyncWrite + Sink<Bytes, Error = quic::StreamError> + Send,
{
    type Output = ();

    type Error = StreamError;

    async fn encode_into(self, stream: S) -> Result<Self::Output, Self::Error> {
        let inst = self;
        let encode = async move {
            let mut stream = pin!(stream);
            match inst {
                EncoderInstruction::SetDynamicTableCapacity { capacity } => {
                    let prefix = 0b0010_0000;
                    encode_integer(stream, prefix, 5, capacity).await?;
                    Ok(())
                }
                EncoderInstruction::InsertWithNameReference {
                    is_static,
                    name_index,
                    huffman,
                    value,
                } => {
                    let mut prefix = 0b1000_0000;
                    if is_static {
                        prefix |= 0b0100_0000;
                    }
                    encode_integer(stream.as_mut(), prefix, 6, name_index).await?;
                    encode_string(stream.as_mut(), 0, 1 + 7, huffman, value).await?;
                    Ok(())
                }
                EncoderInstruction::InsertWithLiteralName {
                    name_huffman,
                    name,
                    value_huffman,
                    value,
                } => {
                    let name_prefix = 0b0100_0000;
                    encode_string(stream.as_mut(), name_prefix, 1 + 5, name_huffman, name).await?;
                    encode_string(stream.as_mut(), 0, 1 + 7, value_huffman, value).await?;
                    Ok(())
                }
                EncoderInstruction::Duplicate { index } => {
                    let prefix = 0b0000_0000;
                    encode_integer(stream.as_mut(), prefix, 5, index).await?;
                    Ok(())
                }
            }
        };
        encode.await.map_err(|error: EncodeStreamError| {
            error.map_stream_closed(
                |_reset_code| H3CriticalStreamClosed::QPackEncoder.into(),
                |encode_error| {
                    tracing::error!(error = %Report::from_error(&encode_error), "this is likely a bug");
                    H3InternalError::QPackEncoderEncode { source: EncodeStreamError::Encode { source: encode_error } }.into()
                },
            )
        })
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use bytes::Bytes;

    use crate::{
        dhttp::settings::{QpackBlockedStreams, QpackMaxTableCapacity, Settings},
        qpack::{
            encoder::{EncoderState, QPackDecoderStreamError, QPackEncoderError},
            field::FieldLineRepresentation,
        },
        varint::VarInt,
    };

    fn get_dynamic_references<'r>(
        field_line_representations: impl IntoIterator<Item = &'r FieldLineRepresentation>,
    ) -> impl Iterator<Item = u64> {
        field_line_representations
            .into_iter()
            .filter_map(
                |field_line_representation| match field_line_representation {
                    FieldLineRepresentation::IndexedFieldLine {
                        is_static, index, ..
                    } => (!is_static).then_some(*index),
                    FieldLineRepresentation::LiteralFieldLineWithNameReference {
                        is_static,
                        name_index,
                        ..
                    } => (!is_static).then_some(*name_index),
                    FieldLineRepresentation::LiteralFieldLineWithPostBaseNameReference {
                        name_index,
                        ..
                    } => Some(*name_index),
                    _ => None,
                },
            )
    }

    fn settings_with_capacity(capacity: u32) -> Arc<Settings> {
        let mut settings = Settings::default();
        settings.set(QpackMaxTableCapacity::setting(VarInt::from_u32(capacity)));
        settings.set(QpackBlockedStreams::setting(VarInt::from_u32(10)));
        Arc::new(settings)
    }

    // --- Fix 2: zero increment rejection ---

    #[test]
    fn on_insert_count_increment_zero_returns_err() {
        let mut state = EncoderState::new(settings_with_capacity(256));
        let result = state.on_insert_count_increment(0);
        assert!(
            matches!(result, Err(QPackDecoderStreamError::IncrementZero)),
            "Expected IncrementZero, got {result:?}"
        );
    }

    #[test]
    fn on_insert_count_increment_valid_returns_ok() {
        let mut state = EncoderState::new(settings_with_capacity(256));
        // Set up: capacity, then insert one entry so inserted_count == 1
        state
            .set_max_table_capacity(64)
            .expect("set capacity failed");
        state
            .insert_with_literal_name(
                false,
                Bytes::from_static(b"x-custom"),
                false,
                Bytes::from_static(b"val"),
            )
            .expect("insert failed");
        // inserted_count == 1, known_received_count == 0 → increment of 1 is valid
        let result = state.on_insert_count_increment(1);
        assert!(result.is_ok(), "Expected Ok, got {result:?}");
    }

    // --- Fix 3: panic→error for capacity ---

    #[test]
    fn set_max_table_capacity_exceeds_max_returns_err() {
        let mut state = EncoderState::new(settings_with_capacity(50));
        let result = state.set_max_table_capacity(100);
        assert!(
            matches!(
                result,
                Err(QPackEncoderError::CapacityExceedsMax {
                    capacity: 100,
                    max: 50
                })
            ),
            "Expected CapacityExceedsMax, got {result:?}"
        );
    }

    #[test]
    fn set_max_table_capacity_equal_to_max_returns_ok() {
        let mut state = EncoderState::new(settings_with_capacity(50));
        let result = state.set_max_table_capacity(50);
        assert!(
            result.is_ok(),
            "Expected Ok for capacity == max, got {result:?}"
        );
    }

    #[test]
    fn set_max_table_capacity_below_max_returns_ok() {
        let mut state = EncoderState::new(settings_with_capacity(50));
        let result = state.set_max_table_capacity(30);
        assert!(
            result.is_ok(),
            "Expected Ok for capacity < max, got {result:?}"
        );
    }

    // --- Fix 4: eviction guard ---

    #[test]
    fn insert_with_literal_name_cannot_evict_when_unacknowledged() {
        // Fill the table with an entry that uses the full capacity
        // known_received_count stays 0, so no entries are evictable
        let mut state = EncoderState::new(settings_with_capacity(256));
        state
            .set_max_table_capacity(64)
            .expect("set capacity failed");
        // Entry size = name.len() + value.len() + 32 = 8 + 8 + 32 + 32 = 64 (fills table)
        // name = b"xxxxxxxx" (8), value = b"yyyyyyyy" (8) → size = 48, not enough
        // Use name = b"x" * 16 (16) + value = b"y" * 16 (16) → size = 64 (exact fill)
        state
            .insert_with_literal_name(
                false,
                Bytes::from(vec![b'x'; 16]),
                false,
                Bytes::from(vec![b'y'; 16]),
            )
            .expect("first insert failed");
        // Table is now full; trying to insert another entry should fail with CannotEvict
        let result = state.insert_with_literal_name(
            false,
            Bytes::from_static(b"overflow"),
            false,
            Bytes::from_static(b"data"),
        );
        assert!(
            matches!(result, Err(QPackEncoderError::CannotEvict)),
            "Expected CannotEvict, got {result:?}"
        );
    }

    // --- Fix 7: blocked streams — get_dynamic_references ---

    #[test]
    fn get_dynamic_references_finds_dynamic_indexed_field() {
        let reprs = vec![FieldLineRepresentation::IndexedFieldLine {
            is_static: false,
            index: 5,
        }];
        let refs: Vec<u64> = get_dynamic_references(&reprs).collect();
        assert_eq!(refs, vec![5]);
    }

    #[test]
    fn get_dynamic_references_ignores_static_indexed_field() {
        let reprs = vec![FieldLineRepresentation::IndexedFieldLine {
            is_static: true,
            index: 5,
        }];
        let refs: Vec<u64> = get_dynamic_references(&reprs).collect();
        assert!(refs.is_empty(), "Static references should be ignored");
    }

    #[test]
    fn get_dynamic_references_finds_dynamic_name_reference() {
        let reprs = vec![FieldLineRepresentation::LiteralFieldLineWithNameReference {
            never_dynamic: false,
            is_static: false,
            name_index: 7,
            huffman: false,
            value: Bytes::from_static(b"val"),
        }];
        let refs: Vec<u64> = get_dynamic_references(&reprs).collect();
        assert_eq!(refs, vec![7]);
    }

    #[test]
    fn get_dynamic_references_empty_for_literal_name() {
        let reprs = vec![FieldLineRepresentation::LiteralFieldLineWithLiteralName {
            never_dynamic: false,
            name_huffman: false,
            name: Bytes::from_static(b"x-header"),
            value_huffman: false,
            value: Bytes::from_static(b"value"),
        }];
        let refs: Vec<u64> = get_dynamic_references(&reprs).collect();
        assert!(refs.is_empty(), "Literal name has no dynamic reference");
    }
}