shiguredo_http3 2026.1.0-canary.3

Sans I/O HTTP/3 Library
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
//! QPACK 動的テーブル (RFC 9204 Section 3.2)
//!
//! HTTP/3 で使用される QPACK の動的テーブルを提供。
//!
//! ## 機能
//!
//! - エントリの挿入と削除 (FIFO 順序)
//! - 容量制限とエビクション
//! - 絶対インデックスと相対インデックスのサポート
//!
//! ## エントリサイズ
//!
//! RFC 9204 Section 3.2.1 に基づき、エントリサイズは:
//! `32 + name.len() + value.len()`

use std::collections::VecDeque;

/// エントリオーバーヘッド (RFC 9204 Section 3.2.1)
const ENTRY_OVERHEAD: u64 = 32;

/// 動的テーブルエントリ
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DynamicEntry {
    /// ヘッダー名
    pub name: Vec<u8>,
    /// ヘッダー値
    pub value: Vec<u8>,
    /// 絶対インデックス (挿入時に割り当て)
    pub absolute_index: u64,
}

impl DynamicEntry {
    /// 新しいエントリを作成
    pub fn new(name: Vec<u8>, value: Vec<u8>, absolute_index: u64) -> Self {
        Self {
            name,
            value,
            absolute_index,
        }
    }

    /// エントリサイズを計算 (RFC 9204 Section 3.2.1)
    ///
    /// サイズ = 32 + name.len() + value.len()
    #[inline]
    pub fn size(&self) -> u64 {
        ENTRY_OVERHEAD + self.name.len() as u64 + self.value.len() as u64
    }
}

/// 動的テーブル (RFC 9204 Section 3.2)
///
/// FIFO 順序でエントリを管理するリングバッファ。
/// 新しいエントリは先頭に挿入され、古いエントリは末尾から削除される。
#[derive(Debug)]
pub struct DynamicTable {
    /// エントリ (VecDeque: 先頭が最新、末尾が最古)
    entries: VecDeque<DynamicEntry>,
    /// 最大容量 (バイト)
    max_capacity: u64,
    /// 現在のサイズ (バイト)
    current_size: u64,
    /// 挿入カウント (次の絶対インデックス)
    insert_count: u64,
    /// 削除されたエントリ数 (dropped count)
    dropped_count: u64,
    /// eviction 制限 (この値未満の absolute_index を持つエントリは evict 不可)
    /// (RFC 9204 Section 2.1.1)
    ///
    /// エンコーダーが設定する。未 ack のフィールドセクションから参照されている
    /// エントリの eviction を防ぐ。
    eviction_limit: u64,
}

impl Default for DynamicTable {
    fn default() -> Self {
        Self::new()
    }
}

impl DynamicTable {
    /// 新しい動的テーブルを作成 (容量 0)
    pub fn new() -> Self {
        Self {
            entries: VecDeque::new(),
            max_capacity: 0,
            current_size: 0,
            insert_count: 0,
            dropped_count: 0,
            eviction_limit: 0,
        }
    }

    /// 指定された容量で動的テーブルを作成
    pub fn with_capacity(capacity: u64) -> Self {
        Self {
            entries: VecDeque::new(),
            max_capacity: capacity,
            current_size: 0,
            insert_count: 0,
            dropped_count: 0,
            eviction_limit: 0,
        }
    }

    /// 最大容量を取得
    #[inline]
    pub fn max_capacity(&self) -> u64 {
        self.max_capacity
    }

    /// 現在のサイズを取得
    #[inline]
    pub fn current_size(&self) -> u64 {
        self.current_size
    }

    /// 挿入カウントを取得 (次の絶対インデックス)
    #[inline]
    pub fn insert_count(&self) -> u64 {
        self.insert_count
    }

    /// 削除されたエントリ数を取得
    #[inline]
    pub fn dropped_count(&self) -> u64 {
        self.dropped_count
    }

    /// エントリ数を取得
    #[inline]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// テーブルが空かどうか
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// eviction 制限を設定 (RFC 9204 Section 2.1.1)
    ///
    /// この値未満の absolute_index を持つエントリは evict できない。
    /// エンコーダーが未 ack フィールドセクションの参照状態に基づいて設定する。
    pub fn set_eviction_limit(&mut self, limit: u64) {
        self.eviction_limit = limit;
    }

    /// 容量を設定 (RFC 9204 Section 4.3.1)
    ///
    /// 容量が減少した場合、エントリをエビクトする。
    pub fn set_capacity(&mut self, capacity: u64) {
        self.max_capacity = capacity;
        self.evict_to_fit(0);
    }

    /// エントリを挿入 (RFC 9204 Section 3.2.2)
    ///
    /// 新しいエントリは先頭に挿入される。
    /// 容量を超える場合は古いエントリをエビクトする。
    ///
    /// # 戻り値
    ///
    /// 成功した場合は挿入されたエントリの絶対インデックスを返す。
    /// エントリが容量を超える場合は `None` を返す。
    pub fn insert(&mut self, name: Vec<u8>, value: Vec<u8>) -> Option<u64> {
        let entry_size = ENTRY_OVERHEAD + name.len() as u64 + value.len() as u64;

        // エントリが容量を超える場合はエラー
        if entry_size > self.max_capacity {
            return None;
        }

        // 容量に収まるようにエビクト
        self.evict_to_fit(entry_size);

        // evict 後もスペースが不足する場合は挿入不可 (RFC 9204 Section 2.1.1)
        if self.current_size + entry_size > self.max_capacity {
            return None;
        }

        let absolute_index = self.insert_count;
        let entry = DynamicEntry::new(name, value, absolute_index);
        self.current_size += entry.size();
        self.insert_count += 1;
        self.entries.push_front(entry);

        Some(absolute_index)
    }

    /// 名前参照で挿入 (RFC 9204 Section 4.3.2)
    ///
    /// 既存のエントリの名前を参照して新しいエントリを挿入。
    pub fn insert_with_name_ref(
        &mut self,
        name_index: u64,
        is_static: bool,
        value: Vec<u8>,
        static_table: &[crate::qpack::Header],
    ) -> Option<u64> {
        let name = if is_static {
            static_table.get(name_index as usize)?.name().to_vec()
        } else {
            self.get_by_absolute_index(name_index)?.name.clone()
        };

        self.insert(name, value)
    }

    /// 複製 (RFC 9204 Section 4.3.4)
    ///
    /// 既存のエントリを複製して新しいエントリとして挿入。
    pub fn duplicate(&mut self, relative_index: u64) -> Option<u64> {
        let entry = self.get_by_relative_index_encoder(relative_index)?;
        let name = entry.name.clone();
        let value = entry.value.clone();
        self.insert(name, value)
    }

    /// 絶対インデックスでエントリを取得
    pub fn get_by_absolute_index(&self, absolute_index: u64) -> Option<&DynamicEntry> {
        if absolute_index < self.dropped_count {
            return None;
        }
        if absolute_index >= self.insert_count {
            return None;
        }

        // entries は先頭が最新 (insert_count - 1)、末尾が最古 (dropped_count)
        let offset = (self.insert_count - 1 - absolute_index) as usize;
        self.entries.get(offset)
    }

    /// エンコーダー命令での相対インデックスでエントリを取得 (RFC 9204 Section 3.2.5)
    ///
    /// 相対インデックス 0 は最新エントリを指す。
    pub fn get_by_relative_index_encoder(&self, relative_index: u64) -> Option<&DynamicEntry> {
        self.entries.get(relative_index as usize)
    }

    /// フィールドライン表現での相対インデックスでエントリを取得 (RFC 9204 Section 3.2.5)
    ///
    /// Base からの相対インデックスで取得。
    /// absolute_index = base - relative_index - 1
    pub fn get_by_relative_index_repr(
        &self,
        relative_index: u64,
        base: u64,
    ) -> Option<&DynamicEntry> {
        if relative_index >= base {
            return None;
        }
        let absolute_index = base - relative_index - 1;
        self.get_by_absolute_index(absolute_index)
    }

    /// Post-Base インデックスでエントリを取得 (RFC 9204 Section 3.2.6)
    ///
    /// absolute_index = base + post_base_index
    pub fn get_by_post_base_index(&self, post_base_index: u64, base: u64) -> Option<&DynamicEntry> {
        let absolute_index = base + post_base_index;
        self.get_by_absolute_index(absolute_index)
    }

    /// 名前と値のペアで動的テーブルを検索
    ///
    /// 完全一致のインデックス、または名前のみ一致のインデックスを返す。
    pub fn find_entry(&self, name: &[u8], value: &[u8]) -> (Option<u64>, Option<u64>) {
        let mut name_match = None;

        for entry in &self.entries {
            if entry.name == name {
                if entry.value == value {
                    return (Some(entry.absolute_index), Some(entry.absolute_index));
                }
                if name_match.is_none() {
                    name_match = Some(entry.absolute_index);
                }
            }
        }

        (None, name_match)
    }

    /// 指定サイズに収まるようにエビクト (RFC 9204 Section 3.2.2)
    ///
    /// eviction_limit 未満の absolute_index を持つエントリは evict しない
    /// (RFC 9204 Section 2.1.1)。
    fn evict_to_fit(&mut self, required_size: u64) {
        while self.current_size + required_size > self.max_capacity {
            // 末尾エントリが evictable か確認
            if let Some(entry) = self.entries.back() {
                if entry.absolute_index < self.eviction_limit {
                    // evict 不可: 未 ack のフィールドセクションから参照されている可能性がある
                    break;
                }
                let size = entry.size();
                self.entries.pop_back();
                self.current_size -= size;
                self.dropped_count += 1;
            } else {
                break;
            }
        }
    }

    /// すべてのエントリをクリア
    pub fn clear(&mut self) {
        self.dropped_count += self.entries.len() as u64;
        self.entries.clear();
        self.current_size = 0;
    }

    /// エントリのイテレータを取得 (最新から最古の順)
    pub fn iter(&self) -> impl Iterator<Item = &DynamicEntry> {
        self.entries.iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_table() {
        let table = DynamicTable::new();
        assert_eq!(table.max_capacity(), 0);
        assert_eq!(table.current_size(), 0);
        assert_eq!(table.insert_count(), 0);
        assert_eq!(table.len(), 0);
        assert!(table.is_empty());
    }

    #[test]
    fn test_insert_entry() {
        let mut table = DynamicTable::with_capacity(1024);

        let idx = table.insert(b":authority".to_vec(), b"www.example.com".to_vec());
        assert_eq!(idx, Some(0));
        assert_eq!(table.len(), 1);
        assert_eq!(table.insert_count(), 1);

        let entry = table.get_by_absolute_index(0).unwrap();
        assert_eq!(entry.name, b":authority");
        assert_eq!(entry.value, b"www.example.com");
    }

    #[test]
    fn test_insert_multiple_entries() {
        let mut table = DynamicTable::with_capacity(1024);

        let idx1 = table.insert(b"name1".to_vec(), b"value1".to_vec());
        let idx2 = table.insert(b"name2".to_vec(), b"value2".to_vec());
        let idx3 = table.insert(b"name3".to_vec(), b"value3".to_vec());

        assert_eq!(idx1, Some(0));
        assert_eq!(idx2, Some(1));
        assert_eq!(idx3, Some(2));
        assert_eq!(table.len(), 3);

        // 絶対インデックスでアクセス
        assert_eq!(table.get_by_absolute_index(0).unwrap().name, b"name1");
        assert_eq!(table.get_by_absolute_index(1).unwrap().name, b"name2");
        assert_eq!(table.get_by_absolute_index(2).unwrap().name, b"name3");

        // エンコーダー相対インデックスでアクセス (0 = 最新)
        assert_eq!(
            table.get_by_relative_index_encoder(0).unwrap().name,
            b"name3"
        );
        assert_eq!(
            table.get_by_relative_index_encoder(1).unwrap().name,
            b"name2"
        );
        assert_eq!(
            table.get_by_relative_index_encoder(2).unwrap().name,
            b"name1"
        );
    }

    #[test]
    fn test_eviction() {
        // 容量を小さく設定 (エントリ 1 つ分程度)
        let mut table = DynamicTable::with_capacity(50);

        // 最初のエントリを挿入 (32 + 5 + 6 = 43)
        let idx1 = table.insert(b"name1".to_vec(), b"value1".to_vec());
        assert_eq!(idx1, Some(0));
        assert_eq!(table.len(), 1);

        // 2 つ目のエントリを挿入すると 1 つ目がエビクトされる
        let idx2 = table.insert(b"name2".to_vec(), b"value2".to_vec());
        assert_eq!(idx2, Some(1));
        assert_eq!(table.len(), 1);
        assert_eq!(table.dropped_count(), 1);

        // 古いエントリにはアクセスできない
        assert!(table.get_by_absolute_index(0).is_none());
        // 新しいエントリにはアクセスできる
        assert!(table.get_by_absolute_index(1).is_some());
    }

    #[test]
    fn test_entry_too_large() {
        let mut table = DynamicTable::with_capacity(50);

        // 容量を超えるエントリは挿入できない
        let idx = table.insert(b"very_long_name".to_vec(), b"very_long_value".to_vec());
        assert_eq!(idx, None);
        assert!(table.is_empty());
    }

    #[test]
    fn test_set_capacity() {
        let mut table = DynamicTable::with_capacity(200);

        table.insert(b"name1".to_vec(), b"value1".to_vec());
        table.insert(b"name2".to_vec(), b"value2".to_vec());
        table.insert(b"name3".to_vec(), b"value3".to_vec());
        assert_eq!(table.len(), 3);

        // 容量を減らすとエビクトが発生
        table.set_capacity(50);
        assert_eq!(table.len(), 1);
        assert_eq!(table.dropped_count(), 2);
    }

    #[test]
    fn test_find_entry() {
        let mut table = DynamicTable::with_capacity(1024);

        table.insert(b":method".to_vec(), b"GET".to_vec());
        table.insert(b":method".to_vec(), b"POST".to_vec());
        table.insert(b":path".to_vec(), b"/".to_vec());

        // 完全一致
        let (exact, name_only) = table.find_entry(b":method", b"GET");
        assert_eq!(exact, Some(0));
        assert_eq!(name_only, Some(0));

        // 名前のみ一致 (最新の一致を返す)
        let (exact, name_only) = table.find_entry(b":method", b"PUT");
        assert_eq!(exact, None);
        assert_eq!(name_only, Some(1)); // POST が最新

        // 一致なし
        let (exact, name_only) = table.find_entry(b"x-custom", b"value");
        assert_eq!(exact, None);
        assert_eq!(name_only, None);
    }

    #[test]
    fn test_relative_index_repr() {
        let mut table = DynamicTable::with_capacity(1024);

        table.insert(b"name0".to_vec(), b"value0".to_vec()); // abs=0
        table.insert(b"name1".to_vec(), b"value1".to_vec()); // abs=1
        table.insert(b"name2".to_vec(), b"value2".to_vec()); // abs=2
        table.insert(b"name3".to_vec(), b"value3".to_vec()); // abs=3

        // Base = 3 の場合
        // relative=0 -> abs=3-0-1=2 -> name2
        // relative=1 -> abs=3-1-1=1 -> name1
        // relative=2 -> abs=3-2-1=0 -> name0
        assert_eq!(
            table.get_by_relative_index_repr(0, 3).unwrap().name,
            b"name2"
        );
        assert_eq!(
            table.get_by_relative_index_repr(1, 3).unwrap().name,
            b"name1"
        );
        assert_eq!(
            table.get_by_relative_index_repr(2, 3).unwrap().name,
            b"name0"
        );

        // 範囲外
        assert!(table.get_by_relative_index_repr(3, 3).is_none());
    }

    #[test]
    fn test_post_base_index() {
        let mut table = DynamicTable::with_capacity(1024);

        table.insert(b"name0".to_vec(), b"value0".to_vec()); // abs=0
        table.insert(b"name1".to_vec(), b"value1".to_vec()); // abs=1
        table.insert(b"name2".to_vec(), b"value2".to_vec()); // abs=2
        table.insert(b"name3".to_vec(), b"value3".to_vec()); // abs=3

        // Base = 2 の場合
        // post_base=0 -> abs=2+0=2 -> name2
        // post_base=1 -> abs=2+1=3 -> name3
        assert_eq!(table.get_by_post_base_index(0, 2).unwrap().name, b"name2");
        assert_eq!(table.get_by_post_base_index(1, 2).unwrap().name, b"name3");

        // 範囲外
        assert!(table.get_by_post_base_index(2, 2).is_none());
    }

    #[test]
    fn test_duplicate() {
        let mut table = DynamicTable::with_capacity(1024);

        table.insert(b"name".to_vec(), b"value".to_vec()); // abs=0, rel=0
        table.insert(b"other".to_vec(), b"data".to_vec()); // abs=1, rel=0, old rel=1

        // 相対インデックス 1 (最古のエントリ) を複製
        let idx = table.duplicate(1);
        assert_eq!(idx, Some(2));
        assert_eq!(table.len(), 3);

        let entry = table.get_by_absolute_index(2).unwrap();
        assert_eq!(entry.name, b"name");
        assert_eq!(entry.value, b"value");
    }

    #[test]
    fn test_clear() {
        let mut table = DynamicTable::with_capacity(1024);

        table.insert(b"name1".to_vec(), b"value1".to_vec());
        table.insert(b"name2".to_vec(), b"value2".to_vec());
        assert_eq!(table.len(), 2);

        table.clear();
        assert!(table.is_empty());
        assert_eq!(table.current_size(), 0);
        assert_eq!(table.dropped_count(), 2);
        assert_eq!(table.insert_count(), 2);
    }

    /// RFC 9204 Section 2.1.1: eviction_limit 未満のエントリは evict されない
    #[test]
    fn test_eviction_limit_prevents_eviction() {
        // 容量を小さく設定 (エントリ 1 つ分程度)
        let mut table = DynamicTable::with_capacity(50);

        // 最初のエントリを挿入 (32 + 5 + 6 = 43)
        let idx1 = table.insert(b"name1".to_vec(), b"value1".to_vec());
        assert_eq!(idx1, Some(0));

        // eviction_limit を設定: abs_index 0 は evict 不可にする
        // (eviction_limit = 1 なら abs_index < 1 = abs_index 0 が保護される)
        table.set_eviction_limit(1);

        // 2 つ目のエントリを挿入しようとするが、1 つ目を evict できないので失敗
        let idx2 = table.insert(b"name2".to_vec(), b"value2".to_vec());
        assert_eq!(idx2, None);
        assert_eq!(table.len(), 1);
        assert_eq!(table.dropped_count(), 0);

        // eviction_limit を解除
        table.set_eviction_limit(0);

        // 今度は eviction が成功
        let idx3 = table.insert(b"name3".to_vec(), b"value3".to_vec());
        assert_eq!(idx3, Some(1));
        assert_eq!(table.len(), 1);
        assert_eq!(table.dropped_count(), 1);
    }

    /// RFC 9204 Section 2.1.1: eviction_limit は set_capacity にも適用される
    #[test]
    fn test_eviction_limit_on_set_capacity() {
        let mut table = DynamicTable::with_capacity(200);

        table.insert(b"name1".to_vec(), b"value1".to_vec()); // abs=0 (最古、末尾)
        table.insert(b"name2".to_vec(), b"value2".to_vec()); // abs=1
        table.insert(b"name3".to_vec(), b"value3".to_vec()); // abs=2 (最新、先頭)
        assert_eq!(table.len(), 3);

        // abs_index < 1 のエントリ (abs=0) を保護
        table.set_eviction_limit(1);

        // 容量を縮小: eviction は末尾 (最古) から行われる
        // abs=0 は eviction_limit で保護されているので evict できない
        // → 容量オーバーのまま全エントリが残る
        table.set_capacity(50);
        assert_eq!(table.len(), 3);
        assert_eq!(table.dropped_count(), 0);

        // eviction_limit を解除すると eviction が進む
        table.set_eviction_limit(0);
        table.set_capacity(50);
        assert_eq!(table.len(), 1);
        assert_eq!(table.dropped_count(), 2);
    }
}