shiguredo_http2 2026.1.0-canary.2

Sans I/O HTTP/2 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
//! HTTP/2 フレーム (RFC 9113 Section 4, 6)
//!
//! HTTP/2 で使用される各種フレームの型定義とエンコード/デコードを提供する。

pub mod decoder;
pub mod encoder;
pub mod flags;

pub use decoder::FrameDecoder;
pub use encoder::FrameEncoder;
pub use flags::FrameFlags;

use crate::settings::Setting;

/// フレームヘッダーサイズ(9 バイト)
pub const FRAME_HEADER_SIZE: usize = 9;

/// ストリーム ID の型
pub type StreamId = u32;

/// 接続レベルのストリーム ID
pub const CONNECTION_STREAM_ID: StreamId = 0;

/// フレームタイプ (RFC 9113 Section 6, RFC 9218 Section 4)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum FrameType {
    /// DATA フレーム
    Data = 0x00,
    /// HEADERS フレーム
    Headers = 0x01,
    /// PRIORITY フレーム (RFC 9113 Section 6.3)
    ///
    /// # 非推奨 (Deprecated)
    ///
    /// RFC 9113 で優先度シグナリングは非推奨となった。
    /// ただし、相互運用性のため受信と minimal processing は必須。
    /// 送信側での使用は推奨されない。
    Priority = 0x02,
    /// RST_STREAM フレーム
    RstStream = 0x03,
    /// SETTINGS フレーム
    Settings = 0x04,
    /// PUSH_PROMISE フレーム (RFC 9113 Section 6.6)
    ///
    /// # 非サポート
    ///
    /// サーバープッシュは主要ブラウザでサポートが削除されているため、
    /// このライブラリでは送信機能を提供しない。
    /// ただし、RFC 9113 に従い受信時は PROTOCOL_ERROR を返す。
    PushPromise = 0x05,
    /// PING フレーム
    Ping = 0x06,
    /// GOAWAY フレーム
    Goaway = 0x07,
    /// WINDOW_UPDATE フレーム
    WindowUpdate = 0x08,
    /// CONTINUATION フレーム
    Continuation = 0x09,
    /// PRIORITY_UPDATE フレーム (RFC 9218 Section 4)
    ///
    /// Extensible Priorities (RFC 9218) で定義される優先度更新フレーム。
    /// 非推奨の PRIORITY フレームの代替として使用される。
    PriorityUpdate = 0x10,
}

impl FrameType {
    /// u8 から `FrameType` を生成する
    #[must_use]
    pub const fn from_u8(value: u8) -> Option<Self> {
        match value {
            0x00 => Some(Self::Data),
            0x01 => Some(Self::Headers),
            0x02 => Some(Self::Priority),
            0x03 => Some(Self::RstStream),
            0x04 => Some(Self::Settings),
            0x05 => Some(Self::PushPromise),
            0x06 => Some(Self::Ping),
            0x07 => Some(Self::Goaway),
            0x08 => Some(Self::WindowUpdate),
            0x09 => Some(Self::Continuation),
            0x10 => Some(Self::PriorityUpdate),
            _ => None,
        }
    }

    /// `FrameType` を u8 に変換する
    #[must_use]
    pub const fn as_u8(self) -> u8 {
        self as u8
    }
}

/// フレームヘッダー
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameHeader {
    /// ペイロード長(24 ビット)
    pub length: u32,
    /// フレームタイプ
    pub frame_type: u8,
    /// フレームフラグ
    pub flags: FrameFlags,
    /// ストリーム ID(31 ビット)
    pub stream_id: StreamId,
}

impl FrameHeader {
    /// 新しい `FrameHeader` を生成する
    #[must_use]
    pub const fn new(frame_type: FrameType, flags: FrameFlags, stream_id: StreamId) -> Self {
        Self {
            length: 0,
            frame_type: frame_type.as_u8(),
            flags,
            stream_id,
        }
    }

    /// ペイロード長を設定する
    #[must_use]
    pub const fn with_length(mut self, length: u32) -> Self {
        self.length = length;
        self
    }

    /// フレームタイプを取得する
    #[must_use]
    pub const fn get_frame_type(&self) -> Option<FrameType> {
        FrameType::from_u8(self.frame_type)
    }
}

/// DATA フレーム (RFC 9113 Section 6.1)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataFrame {
    /// ストリーム ID
    pub stream_id: StreamId,
    /// END_STREAM フラグ
    pub end_stream: bool,
    /// データ
    pub data: Vec<u8>,
    /// パディング長 (RFC 9113 Section 6.1)
    ///
    /// Some の場合、PADDED フラグが設定され、指定されたバイト数のパディングが追加される。
    /// パディングはデータ長の曖昧化やトラフィック解析対策に使用される。
    pub pad_length: Option<u8>,
}

impl DataFrame {
    /// 新しい `DataFrame` を生成する
    #[must_use]
    pub fn new(stream_id: StreamId, data: Vec<u8>) -> Self {
        Self {
            stream_id,
            end_stream: false,
            data,
            pad_length: None,
        }
    }

    /// END_STREAM フラグを設定する
    #[must_use]
    pub const fn with_end_stream(mut self, end_stream: bool) -> Self {
        self.end_stream = end_stream;
        self
    }

    /// パディング長を設定する
    ///
    /// # パラメータ
    ///
    /// - `pad_length`: パディングバイト数 (0-255)
    #[must_use]
    pub const fn with_padding(mut self, pad_length: u8) -> Self {
        self.pad_length = Some(pad_length);
        self
    }
}

/// HEADERS フレーム (RFC 9113 Section 6.2)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HeadersFrame {
    /// ストリーム ID
    pub stream_id: StreamId,
    /// END_STREAM フラグ
    pub end_stream: bool,
    /// END_HEADERS フラグ
    pub end_headers: bool,
    /// 優先度フィールド (非推奨、受信時のみ)
    ///
    /// # 非推奨 (Deprecated)
    ///
    /// RFC 9113 で非推奨となった。相互運用性のため受信は処理する。
    pub priority_fields: Option<PriorityFields>,
    /// ヘッダーブロックフラグメント(HPACK エンコード済み)
    pub header_block_fragment: Vec<u8>,
    /// パディング長 (RFC 9113 Section 6.2)
    ///
    /// Some の場合、PADDED フラグが設定され、指定されたバイト数のパディングが追加される。
    pub pad_length: Option<u8>,
}

impl HeadersFrame {
    /// 新しい `HeadersFrame` を生成する
    #[must_use]
    pub fn new(stream_id: StreamId, header_block_fragment: Vec<u8>) -> Self {
        Self {
            stream_id,
            end_stream: false,
            end_headers: true,
            priority_fields: None,
            header_block_fragment,
            pad_length: None,
        }
    }

    /// END_STREAM フラグを設定する
    #[must_use]
    pub const fn with_end_stream(mut self, end_stream: bool) -> Self {
        self.end_stream = end_stream;
        self
    }

    /// END_HEADERS フラグを設定する
    #[must_use]
    pub const fn with_end_headers(mut self, end_headers: bool) -> Self {
        self.end_headers = end_headers;
        self
    }

    /// パディング長を設定する
    #[must_use]
    pub const fn with_padding(mut self, pad_length: u8) -> Self {
        self.pad_length = Some(pad_length);
        self
    }
}

/// HEADERS フレーム内の優先度フィールド (RFC 9113 Section 6.2)
///
/// # 非推奨 (Deprecated)
///
/// RFC 9113 で非推奨となった。相互運用性のため受信は処理する。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PriorityFields {
    /// Exclusive フラグ
    pub exclusive: bool,
    /// 依存するストリーム ID
    pub stream_dependency: StreamId,
    /// 重み (1-256、ワイヤーフォーマットでは 0-255)
    pub weight: u8,
}

/// PRIORITY フレーム (RFC 9113 Section 6.3)
///
/// # 非推奨 (Deprecated)
///
/// RFC 9113 で優先度シグナリングは非推奨となった。
/// 相互運用性のため受信は処理するが、送信は行わない。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PriorityFrame {
    /// ストリーム ID
    pub stream_id: StreamId,
    /// Exclusive フラグ
    pub exclusive: bool,
    /// 依存するストリーム ID
    pub stream_dependency: StreamId,
    /// 重み (1-256、ワイヤーフォーマットでは 0-255)
    pub weight: u8,
}

/// RST_STREAM フレーム (RFC 9113 Section 6.4)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RstStreamFrame {
    /// ストリーム ID
    pub stream_id: StreamId,
    /// エラーコード
    pub error_code: u32,
}

impl RstStreamFrame {
    /// 新しい `RstStreamFrame` を生成する
    #[must_use]
    pub const fn new(stream_id: StreamId, error_code: u32) -> Self {
        Self {
            stream_id,
            error_code,
        }
    }
}

/// SETTINGS フレーム (RFC 9113 Section 6.5)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SettingsFrame {
    /// ACK フラグ
    pub ack: bool,
    /// 設定パラメータのリスト
    pub settings: Vec<Setting>,
}

impl SettingsFrame {
    /// 空の SETTINGS フレームを生成する
    #[must_use]
    pub fn new() -> Self {
        Self {
            ack: false,
            settings: Vec::new(),
        }
    }

    /// ACK フレームを生成する
    #[must_use]
    pub fn ack() -> Self {
        Self {
            ack: true,
            settings: Vec::new(),
        }
    }

    /// 設定を追加する
    pub fn add_setting(&mut self, setting: Setting) {
        self.settings.push(setting);
    }
}

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

/// PING フレーム (RFC 9113 Section 6.7)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PingFrame {
    /// ACK フラグ
    pub ack: bool,
    /// 不透明データ(8 バイト)
    pub opaque_data: [u8; 8],
}

impl PingFrame {
    /// 新しい `PingFrame` を生成する
    #[must_use]
    pub const fn new(opaque_data: [u8; 8]) -> Self {
        Self {
            ack: false,
            opaque_data,
        }
    }

    /// ACK フレームを生成する
    #[must_use]
    pub const fn ack(opaque_data: [u8; 8]) -> Self {
        Self {
            ack: true,
            opaque_data,
        }
    }
}

/// GOAWAY フレーム (RFC 9113 Section 6.8)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GoawayFrame {
    /// 最後に処理したストリーム ID
    pub last_stream_id: StreamId,
    /// エラーコード
    pub error_code: u32,
    /// 追加のデバッグデータ
    pub debug_data: Vec<u8>,
}

impl GoawayFrame {
    /// 新しい `GoawayFrame` を生成する
    #[must_use]
    pub fn new(last_stream_id: StreamId, error_code: u32) -> Self {
        Self {
            last_stream_id,
            error_code,
            debug_data: Vec::new(),
        }
    }

    /// デバッグデータを追加する
    #[must_use]
    pub fn with_debug_data(mut self, debug_data: Vec<u8>) -> Self {
        self.debug_data = debug_data;
        self
    }
}

/// WINDOW_UPDATE フレーム (RFC 9113 Section 6.9)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WindowUpdateFrame {
    /// ストリーム ID(0 の場合は接続レベル)
    pub stream_id: StreamId,
    /// ウィンドウサイズ増分(31 ビット)
    pub window_size_increment: u32,
}

impl WindowUpdateFrame {
    /// 新しい `WindowUpdateFrame` を生成する
    #[must_use]
    pub const fn new(stream_id: StreamId, window_size_increment: u32) -> Self {
        Self {
            stream_id,
            window_size_increment,
        }
    }
}

/// CONTINUATION フレーム (RFC 9113 Section 6.10)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContinuationFrame {
    /// ストリーム ID
    pub stream_id: StreamId,
    /// END_HEADERS フラグ
    pub end_headers: bool,
    /// ヘッダーブロックフラグメント
    pub header_block_fragment: Vec<u8>,
}

impl ContinuationFrame {
    /// 新しい `ContinuationFrame` を生成する
    #[must_use]
    pub fn new(stream_id: StreamId, header_block_fragment: Vec<u8>) -> Self {
        Self {
            stream_id,
            end_headers: false,
            header_block_fragment,
        }
    }

    /// END_HEADERS フラグを設定する
    #[must_use]
    pub const fn with_end_headers(mut self, end_headers: bool) -> Self {
        self.end_headers = end_headers;
        self
    }
}

/// PRIORITY_UPDATE フレーム (RFC 9218 Section 4)
///
/// Extensible Priorities で定義される優先度更新フレーム。
/// クライアントがサーバーに対してストリームの優先度を通知するために使用する。
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PriorityUpdateFrame {
    /// 優先度を更新するストリーム ID
    ///
    /// RFC 9218 Section 4: Prioritized Element ID
    /// クライアント開始ストリーム (奇数) の ID を指定する。
    pub prioritized_element_id: StreamId,
    /// Priority Field Value
    ///
    /// RFC 9218 Section 4: Structured Fields (RFC 8941) の Dictionary 形式。
    /// 空の場合はデフォルト優先度を使用。
    pub priority_field_value: Vec<u8>,
}

impl PriorityUpdateFrame {
    /// 新しい `PriorityUpdateFrame` を生成する
    #[must_use]
    pub fn new(prioritized_element_id: StreamId, priority_field_value: Vec<u8>) -> Self {
        Self {
            prioritized_element_id,
            priority_field_value,
        }
    }

    /// デフォルト優先度の `PriorityUpdateFrame` を生成する
    #[must_use]
    pub fn default_priority(prioritized_element_id: StreamId) -> Self {
        Self {
            prioritized_element_id,
            priority_field_value: Vec::new(),
        }
    }
}

/// HTTP/2 フレーム
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Frame {
    /// DATA フレーム
    Data(DataFrame),
    /// HEADERS フレーム
    Headers(HeadersFrame),
    /// PRIORITY フレーム (非推奨)
    ///
    /// # 非推奨 (Deprecated)
    ///
    /// RFC 9113 で優先度シグナリングは非推奨となった。
    /// 相互運用性のため受信は処理するが、送信は行わない。
    Priority(PriorityFrame),
    /// RST_STREAM フレーム
    RstStream(RstStreamFrame),
    /// SETTINGS フレーム
    Settings(SettingsFrame),
    /// PUSH_PROMISE フレーム (非サポート)
    ///
    /// # 非サポート
    ///
    /// サーバープッシュは主要ブラウザでサポートが削除されているため、
    /// 受信時は PROTOCOL_ERROR を返す。ストリーム ID のみ保持。
    PushPromise {
        /// ストリーム ID
        stream_id: StreamId,
    },
    /// PING フレーム
    Ping(PingFrame),
    /// GOAWAY フレーム
    Goaway(GoawayFrame),
    /// WINDOW_UPDATE フレーム
    WindowUpdate(WindowUpdateFrame),
    /// CONTINUATION フレーム
    Continuation(ContinuationFrame),
    /// PRIORITY_UPDATE フレーム (RFC 9218)
    ///
    /// Extensible Priorities で定義される優先度更新フレーム。
    PriorityUpdate(PriorityUpdateFrame),
    /// 未知のフレームタイプ
    Unknown {
        /// フレームヘッダー
        header: FrameHeader,
        /// ペイロード
        payload: Vec<u8>,
    },
}

impl Frame {
    /// フレームのストリーム ID を取得する
    #[must_use]
    pub const fn stream_id(&self) -> StreamId {
        match self {
            Self::Data(f) => f.stream_id,
            Self::Headers(f) => f.stream_id,
            Self::Priority(f) => f.stream_id,
            Self::RstStream(f) => f.stream_id,
            Self::Settings(_) => CONNECTION_STREAM_ID,
            Self::PushPromise { stream_id } => *stream_id,
            Self::Ping(_) => CONNECTION_STREAM_ID,
            Self::Goaway(_) => CONNECTION_STREAM_ID,
            Self::WindowUpdate(f) => f.stream_id,
            Self::Continuation(f) => f.stream_id,
            // RFC 9218 Section 4: PRIORITY_UPDATE は stream identifier 0 で送信される
            Self::PriorityUpdate(_) => CONNECTION_STREAM_ID,
            Self::Unknown { header, .. } => header.stream_id,
        }
    }

    /// フレームタイプを取得する
    #[must_use]
    pub const fn frame_type(&self) -> Option<FrameType> {
        match self {
            Self::Data(_) => Some(FrameType::Data),
            Self::Headers(_) => Some(FrameType::Headers),
            Self::Priority(_) => Some(FrameType::Priority),
            Self::RstStream(_) => Some(FrameType::RstStream),
            Self::Settings(_) => Some(FrameType::Settings),
            Self::PushPromise { .. } => Some(FrameType::PushPromise),
            Self::Ping(_) => Some(FrameType::Ping),
            Self::Goaway(_) => Some(FrameType::Goaway),
            Self::WindowUpdate(_) => Some(FrameType::WindowUpdate),
            Self::Continuation(_) => Some(FrameType::Continuation),
            Self::PriorityUpdate(_) => Some(FrameType::PriorityUpdate),
            Self::Unknown { .. } => None,
        }
    }
}