shiguredo_http2 2026.1.0-canary.6

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
//! HTTP/2 ストリーム識別子 (RFC 9113 §5.1.1)
//!
//! 奇偶ルールと値範囲を型レベルで強制するための newtype 群。

use core::num::NonZeroU32;

/// ストリーム ID の奇偶 (RFC 9113 §5.1.1)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Parity {
    /// クライアント開始ストリーム (奇数)
    Odd,
    /// サーバー開始ストリーム (偶数)
    Even,
}

/// ストリーム ID 構築時検査エラー
///
/// RFC 9113 §5.1.1: stream identifier は unsigned 31-bit integer、
/// クライアント開始は奇数、サーバー開始は偶数、0 は接続制御用。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StreamIdError {
    /// 0 が指定された (本来許可しない構築点で)
    Reserved,

    /// 期待する奇偶と一致しない
    ParityMismatch {
        /// 期待する奇偶
        expected: Parity,
        /// 受け取った値
        got: u32,
    },

    /// 31-bit 範囲を超える値が指定された
    OutOfRange {
        /// 違反した値
        value: u32,
    },
}

impl std::fmt::Display for StreamIdError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Reserved => write!(f, "stream ID 0 is reserved for connection control"),
            Self::ParityMismatch { expected, got } => {
                let expected_str = match expected {
                    Parity::Odd => "odd",
                    Parity::Even => "even",
                };
                write!(
                    f,
                    "stream ID {got} parity mismatch: expected {expected_str}"
                )
            }
            Self::OutOfRange { value } => write!(f, "stream ID {value} exceeds 31-bit range"),
        }
    }
}

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

/// 31-bit 上限 (2^31 - 1)
pub(crate) const STREAM_ID_MAX: u32 = (1u32 << 31) - 1;

/// クライアント開始ストリーム ID (奇数、1..=2^31-1)
///
/// RFC 9113 §5.1.1: クライアント開始ストリームは奇数 ID。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ClientStreamId(NonZeroU32);

impl ClientStreamId {
    /// 構築時検査つきで生成する
    pub fn new(id: u32) -> Result<Self, StreamIdError> {
        if id == 0 {
            return Err(StreamIdError::Reserved);
        }
        if id > STREAM_ID_MAX {
            return Err(StreamIdError::OutOfRange { value: id });
        }
        if id.is_multiple_of(2) {
            return Err(StreamIdError::ParityMismatch {
                expected: Parity::Odd,
                got: id,
            });
        }
        Ok(Self(NonZeroU32::new(id).expect("non-zero checked above")))
    }

    /// const 文脈で生成する
    ///
    /// # 不正リテラルの compile-fail 例
    ///
    /// 0 は接続制御用なのでクライアントストリーム ID には使えない:
    ///
    /// ```compile_fail
    /// const _BAD: shiguredo_http2::ClientStreamId =
    ///     shiguredo_http2::ClientStreamId::from_static(0);
    /// ```
    ///
    /// 偶数 ID はサーバー開始の領域なのでクライアントには使えない:
    ///
    /// ```compile_fail
    /// const _BAD: shiguredo_http2::ClientStreamId =
    ///     shiguredo_http2::ClientStreamId::from_static(2);
    /// ```
    pub const fn from_static(id: u32) -> Self {
        assert!(
            id <= STREAM_ID_MAX,
            "ClientStreamId::from_static: id must be <= 2^31-1"
        );
        assert!(
            !id.is_multiple_of(2),
            "ClientStreamId::from_static: id must be odd (RFC 9113 §5.1.1)"
        );
        // NonZeroU32::new(0) で None になるため 0 もこの match で弾く
        match NonZeroU32::new(id) {
            Some(v) => Self(v),
            None => panic!("ClientStreamId::from_static: id must not be 0 (RFC 9113 §5.1.1)"),
        }
    }

    /// 検証済み値から構築する (crate 内部専用)
    pub(crate) fn from_validated_parts(id: NonZeroU32) -> Self {
        debug_assert!(
            id.get() <= STREAM_ID_MAX,
            "ClientStreamId::from_validated_parts: id must be <= 2^31-1"
        );
        debug_assert!(
            !id.get().is_multiple_of(2),
            "ClientStreamId::from_validated_parts: id must be odd"
        );
        Self(id)
    }

    /// `NonZeroU32` として取得する
    pub const fn get(self) -> NonZeroU32 {
        self.0
    }

    /// `u32` として取得する
    pub const fn as_u32(self) -> u32 {
        self.0.get()
    }
}

/// サーバー開始ストリーム ID (偶数、2..=2^31-2)
///
/// RFC 9113 §5.1.1: サーバー開始ストリームは偶数 ID。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ServerStreamId(NonZeroU32);

impl ServerStreamId {
    /// 構築時検査つきで生成する
    pub fn new(id: u32) -> Result<Self, StreamIdError> {
        if id == 0 {
            return Err(StreamIdError::Reserved);
        }
        if id > STREAM_ID_MAX {
            return Err(StreamIdError::OutOfRange { value: id });
        }
        if !id.is_multiple_of(2) {
            return Err(StreamIdError::ParityMismatch {
                expected: Parity::Even,
                got: id,
            });
        }
        Ok(Self(NonZeroU32::new(id).expect("non-zero checked above")))
    }

    /// const 文脈で生成する
    ///
    /// # 不正リテラルの compile-fail 例
    ///
    /// 0 は接続制御用なのでサーバーストリーム ID には使えない:
    ///
    /// ```compile_fail
    /// const _BAD: shiguredo_http2::ServerStreamId =
    ///     shiguredo_http2::ServerStreamId::from_static(0);
    /// ```
    ///
    /// 奇数 ID はクライアント開始の領域なのでサーバーには使えない:
    ///
    /// ```compile_fail
    /// const _BAD: shiguredo_http2::ServerStreamId =
    ///     shiguredo_http2::ServerStreamId::from_static(1);
    /// ```
    pub const fn from_static(id: u32) -> Self {
        assert!(
            id <= STREAM_ID_MAX,
            "ServerStreamId::from_static: id must be <= 2^31-1"
        );
        assert!(
            id.is_multiple_of(2),
            "ServerStreamId::from_static: id must be even (RFC 9113 §5.1.1)"
        );
        // NonZeroU32::new(0) で None になるため 0 もこの match で弾く
        match NonZeroU32::new(id) {
            Some(v) => Self(v),
            None => panic!("ServerStreamId::from_static: id must not be 0 (RFC 9113 §5.1.1)"),
        }
    }

    /// 検証済み値から構築する (crate 内部専用)
    pub(crate) fn from_validated_parts(id: NonZeroU32) -> Self {
        debug_assert!(
            id.get() <= STREAM_ID_MAX,
            "ServerStreamId::from_validated_parts: id must be <= 2^31-1"
        );
        debug_assert!(
            id.get().is_multiple_of(2),
            "ServerStreamId::from_validated_parts: id must be even"
        );
        Self(id)
    }

    /// `NonZeroU32` として取得する
    pub const fn get(self) -> NonZeroU32 {
        self.0
    }

    /// `u32` として取得する
    pub const fn as_u32(self) -> u32 {
        self.0.get()
    }
}

/// 非ゼロストリーム ID (クライアントまたはサーバー開始)
///
/// stream_id = 0 を構造的に持てないフレーム構築点で使用される。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NonZeroStreamId {
    /// クライアント開始 (奇数)
    Client(ClientStreamId),
    /// サーバー開始 (偶数)
    Server(ServerStreamId),
}

impl NonZeroStreamId {
    /// wire 上の u32 を奇偶で分類しながら検査する
    pub fn new(id: u32) -> Result<Self, StreamIdError> {
        if id == 0 {
            return Err(StreamIdError::Reserved);
        }
        if id > STREAM_ID_MAX {
            return Err(StreamIdError::OutOfRange { value: id });
        }
        if id.is_multiple_of(2) {
            Ok(Self::Server(ServerStreamId::from_validated_parts(
                NonZeroU32::new(id).expect("non-zero checked above"),
            )))
        } else {
            Ok(Self::Client(ClientStreamId::from_validated_parts(
                NonZeroU32::new(id).expect("non-zero checked above"),
            )))
        }
    }

    /// const 文脈で生成する (奇偶を自動分類)
    ///
    /// # 不正リテラルの compile-fail 例
    ///
    /// 0 は接続制御用なので `NonZeroStreamId` には使えない:
    ///
    /// ```compile_fail
    /// const _BAD: shiguredo_http2::NonZeroStreamId =
    ///     shiguredo_http2::NonZeroStreamId::from_static(0);
    /// ```
    pub const fn from_static(id: u32) -> Self {
        assert!(id != 0, "NonZeroStreamId::from_static: id must not be 0");
        assert!(
            id <= STREAM_ID_MAX,
            "NonZeroStreamId::from_static: id must be <= 2^31-1"
        );
        if id.is_multiple_of(2) {
            Self::Server(ServerStreamId::from_static(id))
        } else {
            Self::Client(ClientStreamId::from_static(id))
        }
    }

    /// decoder 内部で検証済みの値から構築する
    ///
    /// 呼び出し側が「非ゼロかつ 31-bit 範囲」を保証していること。
    pub(crate) fn from_validated_parts(id: NonZeroU32) -> Self {
        debug_assert!(
            id.get() <= STREAM_ID_MAX,
            "NonZeroStreamId::from_validated_parts: id must be <= 2^31-1"
        );
        if id.get().is_multiple_of(2) {
            Self::Server(ServerStreamId::from_validated_parts(id))
        } else {
            Self::Client(ClientStreamId::from_validated_parts(id))
        }
    }

    /// `u32` として取得する
    pub const fn as_u32(self) -> u32 {
        match self {
            Self::Client(id) => id.as_u32(),
            Self::Server(id) => id.as_u32(),
        }
    }

    /// `ClientStreamId` として取得する (該当する場合)
    pub const fn client(self) -> Option<ClientStreamId> {
        match self {
            Self::Client(id) => Some(id),
            Self::Server(_) => None,
        }
    }

    /// `ServerStreamId` として取得する (該当する場合)
    pub const fn server(self) -> Option<ServerStreamId> {
        match self {
            Self::Client(_) => None,
            Self::Server(id) => Some(id),
        }
    }
}

impl From<ClientStreamId> for NonZeroStreamId {
    fn from(id: ClientStreamId) -> Self {
        Self::Client(id)
    }
}

impl From<ServerStreamId> for NonZeroStreamId {
    fn from(id: ServerStreamId) -> Self {
        Self::Server(id)
    }
}

impl std::fmt::Display for NonZeroStreamId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_u32())
    }
}

/// HTTP/2 ストリーム識別子 (RFC 9113 §5.1.1)
///
/// 接続制御用 (0) / クライアント開始 (奇数) / サーバー開始 (偶数) の 3 分類を型で表現する。
///
/// `PartialOrd` / `Ord` は意図的に derive しない。variant をまたいだ順序比較は
/// 意味的に不適切であり、順序比較が必要な箇所では `as_u32()` 経由で行う。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StreamId {
    /// 接続レベル制御用 (0)
    Connection,
    /// クライアント開始ストリーム (奇数)
    Client(ClientStreamId),
    /// サーバー開始ストリーム (偶数)
    Server(ServerStreamId),
}

impl StreamId {
    /// wire 上の u32 を分類する
    ///
    /// 呼び出し元が 31-bit マスク済み (id < 2^31) であることを前提とする。
    /// decoder の `decode_header` が上位 1 ビットをマスクするため、この前提は常に成立する。
    pub fn from_wire(id: u32) -> Self {
        debug_assert!(
            id <= STREAM_ID_MAX,
            "StreamId::from_wire: id must be <= 2^31-1, got {id}"
        );
        if id == 0 {
            Self::Connection
        } else if id.is_multiple_of(2) {
            Self::Server(ServerStreamId::from_validated_parts(
                NonZeroU32::new(id).expect("non-zero checked above"),
            ))
        } else {
            Self::Client(ClientStreamId::from_validated_parts(
                NonZeroU32::new(id).expect("non-zero checked above"),
            ))
        }
    }

    /// `u32` として取得する
    pub const fn as_u32(self) -> u32 {
        match self {
            Self::Connection => 0,
            Self::Client(id) => id.as_u32(),
            Self::Server(id) => id.as_u32(),
        }
    }

    /// `NonZeroStreamId` として取得する (`Connection` の場合は `None`)
    pub const fn non_zero(self) -> Option<NonZeroStreamId> {
        match self {
            Self::Connection => None,
            Self::Client(id) => Some(NonZeroStreamId::Client(id)),
            Self::Server(id) => Some(NonZeroStreamId::Server(id)),
        }
    }
}

impl std::fmt::Display for StreamId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_u32())
    }
}

impl From<ClientStreamId> for StreamId {
    fn from(id: ClientStreamId) -> Self {
        Self::Client(id)
    }
}

impl From<ServerStreamId> for StreamId {
    fn from(id: ServerStreamId) -> Self {
        Self::Server(id)
    }
}

impl From<NonZeroStreamId> for StreamId {
    fn from(id: NonZeroStreamId) -> Self {
        match id {
            NonZeroStreamId::Client(c) => Self::Client(c),
            NonZeroStreamId::Server(s) => Self::Server(s),
        }
    }
}

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

    #[test]
    fn client_stream_id_new_out_of_range() {
        assert_eq!(
            ClientStreamId::new(STREAM_ID_MAX + 1),
            Err(StreamIdError::OutOfRange {
                value: STREAM_ID_MAX + 1,
            })
        );
    }

    #[test]
    fn non_zero_stream_id_new_out_of_range() {
        assert_eq!(
            NonZeroStreamId::new(STREAM_ID_MAX + 1),
            Err(StreamIdError::OutOfRange {
                value: STREAM_ID_MAX + 1,
            })
        );
    }

    #[test]
    fn client_stream_id_from_validated_parts() {
        let raw = NonZeroU32::new(5).unwrap();
        let id = ClientStreamId::from_validated_parts(raw);
        assert_eq!(id.as_u32(), 5);
    }

    #[test]
    fn server_stream_id_from_validated_parts() {
        let raw = NonZeroU32::new(6).unwrap();
        let id = ServerStreamId::from_validated_parts(raw);
        assert_eq!(id.as_u32(), 6);
    }

    mod validated_parts {
        use proptest::prelude::*;

        use super::{ClientStreamId, NonZeroStreamId, NonZeroU32, STREAM_ID_MAX, ServerStreamId};

        proptest! {
            #[test]
            fn client_stream_id_validated_matches_new(
                id in (1u32..=STREAM_ID_MAX).prop_filter(
                    "奇数のみ",
                    |id| id % 2 == 1,
                ),
            ) {
                let via_new = ClientStreamId::new(id).unwrap();
                let nz = NonZeroU32::new(id).unwrap();
                let via_validated = ClientStreamId::from_validated_parts(nz);
                prop_assert_eq!(via_new, via_validated);
            }

            #[test]
            fn server_stream_id_validated_matches_new(
                id in (2u32..=STREAM_ID_MAX).prop_filter(
                    "偶数のみ",
                    |id| id % 2 == 0,
                ),
            ) {
                let via_new = ServerStreamId::new(id).unwrap();
                let nz = NonZeroU32::new(id).unwrap();
                let via_validated = ServerStreamId::from_validated_parts(nz);
                prop_assert_eq!(via_new, via_validated);
            }

            #[test]
            fn non_zero_stream_id_validated_matches_new(
                id in 1u32..=STREAM_ID_MAX,
            ) {
                let via_new = NonZeroStreamId::new(id).unwrap();
                let nz = NonZeroU32::new(id).unwrap();
                let via_validated = NonZeroStreamId::from_validated_parts(nz);
                prop_assert_eq!(via_new, via_validated);
            }
        }
    }
}