Skip to main content

net_wire/http2/
types.rs

1//! HTTP/2 scalar wire values.
2
3use super::error::Http2StreamIdError;
4
5/// An HTTP/2 frame type, preserving extension and unknown values.
6#[repr(transparent)]
7#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
8pub struct Http2FrameType(u8);
9
10impl Http2FrameType {
11    /// DATA (`0x0`).
12    pub const DATA: Self = Self(0x0);
13    /// HEADERS (`0x1`).
14    pub const HEADERS: Self = Self(0x1);
15    /// PRIORITY (`0x2`).
16    pub const PRIORITY: Self = Self(0x2);
17    /// RST_STREAM (`0x3`).
18    pub const RST_STREAM: Self = Self(0x3);
19    /// SETTINGS (`0x4`).
20    pub const SETTINGS: Self = Self(0x4);
21    /// PUSH_PROMISE (`0x5`).
22    pub const PUSH_PROMISE: Self = Self(0x5);
23    /// PING (`0x6`).
24    pub const PING: Self = Self(0x6);
25    /// GOAWAY (`0x7`).
26    pub const GOAWAY: Self = Self(0x7);
27    /// WINDOW_UPDATE (`0x8`).
28    pub const WINDOW_UPDATE: Self = Self(0x8);
29    /// CONTINUATION (`0x9`).
30    pub const CONTINUATION: Self = Self(0x9);
31
32    /// Creates a raw frame type without restricting unknown extensions.
33    pub const fn new(raw: u8) -> Self {
34        Self(raw)
35    }
36
37    /// Returns the encoded frame type.
38    pub const fn raw(self) -> u8 {
39        self.0
40    }
41}
42
43/// An HTTP/2 stream identifier preserving its reserved high bit.
44#[repr(transparent)]
45#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
46pub struct Http2StreamId(u32);
47
48impl Http2StreamId {
49    /// Creates an identifier from its raw encoded field, including the reserved bit.
50    pub const fn new(raw: u32) -> Self {
51        Self(raw)
52    }
53
54    /// Creates an outgoing identifier after rejecting a set reserved bit.
55    pub const fn from_value(value: u32) -> Result<Self, Http2StreamIdError> {
56        if value & 0x8000_0000 != 0 {
57            Err(Http2StreamIdError::ReservedBitSet)
58        } else {
59            Ok(Self(value))
60        }
61    }
62
63    /// Returns the raw encoded stream identifier, including its reserved bit.
64    pub const fn raw(self) -> u32 {
65        self.0
66    }
67
68    /// Returns the 31-bit stream identifier value.
69    pub const fn value(self) -> u32 {
70        self.0 & 0x7fff_ffff
71    }
72
73    /// Returns whether the raw encoded identifier has its reserved bit set.
74    pub const fn has_reserved_bit(self) -> bool {
75        self.0 & 0x8000_0000 != 0
76    }
77}
78
79/// An HTTP/2 error code, preserving extension and unknown values.
80#[repr(transparent)]
81#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
82pub struct Http2ErrorCode(u32);
83
84impl Http2ErrorCode {
85    /// NO_ERROR (`0x0`).
86    pub const NO_ERROR: Self = Self(0x0);
87    /// PROTOCOL_ERROR (`0x1`).
88    pub const PROTOCOL_ERROR: Self = Self(0x1);
89    /// INTERNAL_ERROR (`0x2`).
90    pub const INTERNAL_ERROR: Self = Self(0x2);
91    /// FLOW_CONTROL_ERROR (`0x3`).
92    pub const FLOW_CONTROL_ERROR: Self = Self(0x3);
93    /// SETTINGS_TIMEOUT (`0x4`).
94    pub const SETTINGS_TIMEOUT: Self = Self(0x4);
95    /// STREAM_CLOSED (`0x5`).
96    pub const STREAM_CLOSED: Self = Self(0x5);
97    /// FRAME_SIZE_ERROR (`0x6`).
98    pub const FRAME_SIZE_ERROR: Self = Self(0x6);
99    /// REFUSED_STREAM (`0x7`).
100    pub const REFUSED_STREAM: Self = Self(0x7);
101    /// CANCEL (`0x8`).
102    pub const CANCEL: Self = Self(0x8);
103    /// COMPRESSION_ERROR (`0x9`).
104    pub const COMPRESSION_ERROR: Self = Self(0x9);
105    /// CONNECT_ERROR (`0xa`).
106    pub const CONNECT_ERROR: Self = Self(0xa);
107    /// ENHANCE_YOUR_CALM (`0xb`).
108    pub const ENHANCE_YOUR_CALM: Self = Self(0xb);
109    /// INADEQUATE_SECURITY (`0xc`).
110    pub const INADEQUATE_SECURITY: Self = Self(0xc);
111    /// HTTP_1_1_REQUIRED (`0xd`).
112    pub const HTTP_1_1_REQUIRED: Self = Self(0xd);
113
114    /// Creates a raw error code without restricting extensions.
115    pub const fn new(raw: u32) -> Self {
116        Self(raw)
117    }
118
119    /// Returns the encoded error code.
120    pub const fn raw(self) -> u32 {
121        self.0
122    }
123}
124
125/// An HTTP/2 SETTINGS parameter identifier, preserving extension and unknown values.
126#[repr(transparent)]
127#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
128pub struct Http2SettingId(u16);
129
130impl Http2SettingId {
131    /// SETTINGS_HEADER_TABLE_SIZE (`0x1`).
132    pub const HEADER_TABLE_SIZE: Self = Self(0x1);
133    /// SETTINGS_ENABLE_PUSH (`0x2`).
134    pub const ENABLE_PUSH: Self = Self(0x2);
135    /// SETTINGS_MAX_CONCURRENT_STREAMS (`0x3`).
136    pub const MAX_CONCURRENT_STREAMS: Self = Self(0x3);
137    /// SETTINGS_INITIAL_WINDOW_SIZE (`0x4`).
138    pub const INITIAL_WINDOW_SIZE: Self = Self(0x4);
139    /// SETTINGS_MAX_FRAME_SIZE (`0x5`).
140    pub const MAX_FRAME_SIZE: Self = Self(0x5);
141    /// SETTINGS_MAX_HEADER_LIST_SIZE (`0x6`).
142    pub const MAX_HEADER_LIST_SIZE: Self = Self(0x6);
143    /// SETTINGS_ENABLE_CONNECT_PROTOCOL (`0x8`).
144    pub const ENABLE_CONNECT_PROTOCOL: Self = Self(0x8);
145
146    /// Creates a raw setting identifier without restricting extensions.
147    pub const fn new(raw: u16) -> Self {
148        Self(raw)
149    }
150
151    /// Returns the encoded setting identifier.
152    pub const fn raw(self) -> u16 {
153        self.0
154    }
155}