http_box/http2/
frame_type.rs

1// +-----------------------------------------------------------------------------------------------+
2// | Copyright 2016 Sean Kerr                                                                      |
3// |                                                                                               |
4// | Licensed under the Apache License, Version 2.0 (the "License");                               |
5// | you may not use this file except in compliance with the License.                              |
6// | You may obtain a copy of the License at                                                       |
7// |                                                                                               |
8// |  http://www.apache.org/licenses/LICENSE-2.0                                                   |
9// |                                                                                               |
10// | Unless required by applicable law or agreed to in writing, software                           |
11// | distributed under the License is distributed on an "AS IS" BASIS,                             |
12// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                      |
13// | See the License for the specific language governing permissions and                           |
14// | limitations under the License.                                                                |
15// +-----------------------------------------------------------------------------------------------+
16
17use std::fmt;
18
19/// Continuation frame type.
20pub const FR_CONTINUATION: u8 = 0x9;
21
22/// Data frame type.
23pub const FR_DATA: u8 = 0x0;
24
25/// Go away frame type.
26pub const FR_GO_AWAY: u8 = 0x7;
27
28/// Headers frame type.
29pub const FR_HEADERS: u8 = 0x1;
30
31/// Ping frame type.
32pub const FR_PING: u8 = 0x6;
33
34/// Priority frame type.
35pub const FR_PRIORITY: u8 = 0x2;
36
37/// Push promise frame type.
38pub const FR_PUSH_PROMISE: u8 = 0x5;
39
40/// Reset stream frame type.
41pub const FR_RST_STREAM: u8 = 0x3;
42
43/// Settings frame type.
44pub const FR_SETTINGS: u8 = 0x4;
45
46/// Window update frame type.
47pub const FR_WINDOW_UPDATE: u8 = 0x8;
48
49/// Unsupported frame type.
50pub const FR_UNSUPPORTED: u8 = 0xFF;
51
52/// Frame types.
53#[derive(Clone,Copy,PartialEq)]
54#[repr(u8)]
55pub enum FrameType {
56    /// Continuation frame.
57    Continuation = FR_CONTINUATION,
58
59    /// Data frame.
60    Data = FR_DATA,
61
62    /// Go away frame.
63    GoAway = FR_GO_AWAY,
64
65    /// Headers frame.
66    Headers = FR_HEADERS,
67
68    /// Ping frame.
69    Ping = FR_PING,
70
71    /// Priority frame.
72    Priority = FR_PRIORITY,
73
74    /// Push promise frame.
75    PushPromise = FR_PUSH_PROMISE,
76
77    /// Reset stream frame.
78    RstStream = FR_RST_STREAM,
79
80    /// Settings frame.
81    Settings = FR_SETTINGS,
82
83    /// Window update frame.
84    WindowUpdate = FR_WINDOW_UPDATE,
85
86    /// Unsupported frame.
87    Unsupported = FR_UNSUPPORTED
88}
89
90impl FrameType {
91    /// Create a new `FrameType` from a `u8`.
92    pub fn from_u8(byte: u8) -> FrameType {
93        match byte {
94            FR_DATA          => FrameType::Data,
95            FR_HEADERS       => FrameType::Headers,
96            FR_PRIORITY      => FrameType::Priority,
97            FR_RST_STREAM    => FrameType::RstStream,
98            FR_SETTINGS      => FrameType::Settings,
99            FR_PUSH_PROMISE  => FrameType::PushPromise,
100            FR_PING          => FrameType::Ping,
101            FR_GO_AWAY       => FrameType::GoAway,
102            FR_WINDOW_UPDATE => FrameType::WindowUpdate,
103            FR_CONTINUATION  => FrameType::Continuation,
104            _                => FrameType::Unsupported
105        }
106    }
107
108    /// Convert this frame type to a byte value.
109    pub fn as_byte(&self) -> u8 {
110        match *self {
111            FrameType::Continuation => FR_CONTINUATION,
112            FrameType::Data         => FR_DATA,
113            FrameType::GoAway       => FR_GO_AWAY,
114            FrameType::Headers      => FR_HEADERS,
115            FrameType::Ping         => FR_PING,
116            FrameType::PushPromise  => FR_PUSH_PROMISE,
117            FrameType::Priority     => FR_PRIORITY,
118            FrameType::RstStream    => FR_RST_STREAM,
119            FrameType::Settings     => FR_SETTINGS,
120            FrameType::WindowUpdate => FR_WINDOW_UPDATE,
121            _                       => FR_UNSUPPORTED
122        }
123    }
124
125    /// Format this for debug and display purposes.
126    fn format(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
127        match *self {
128            FrameType::Continuation => {
129                write!(formatter, "<FrameType::Continuation>")
130            },
131            FrameType::Data => {
132                write!(formatter, "<FrameType::Data>")
133            },
134            FrameType::GoAway => {
135                write!(formatter, "<FrameType::GoAway>")
136            },
137            FrameType::Headers => {
138                write!(formatter, "<FrameType::Headers>")
139            },
140            FrameType::Ping => {
141                write!(formatter, "<FrameType::Ping>")
142            },
143            FrameType::Priority => {
144                write!(formatter, "<FrameType::Priority>")
145            },
146            FrameType::PushPromise => {
147                write!(formatter, "<FrameType::PushPromise>")
148            },
149            FrameType::RstStream => {
150                write!(formatter, "<FrameType::RstStream>")
151            },
152            FrameType::Settings => {
153                write!(formatter, "<FrameType::Settings>")
154            },
155            FrameType::WindowUpdate => {
156                write!(formatter, "<FrameType::WindowUpdate>")
157            },
158            FrameType::Unsupported => {
159                write!(formatter, "<FrameType::Unsupported>")
160            }
161        }
162    }
163
164    /// Indicates that this is a `FrameType::Continuation`.
165    pub fn is_continuation(&self) -> bool {
166        match *self {
167            FrameType::Continuation => true,
168            _ => false
169        }
170    }
171
172    /// Indicates that this is a `FrameType::Data`.
173    pub fn is_data(&self) -> bool {
174        match *self {
175            FrameType::Data => true,
176            _ => false
177        }
178    }
179
180    /// Indicates that this is a `FrameType::GoAway`.
181    pub fn is_go_away(&self) -> bool {
182        match *self {
183            FrameType::GoAway => true,
184            _ => false
185        }
186    }
187
188    /// Indicates that this is a `FrameType::Headers`.
189    pub fn is_headers(&self) -> bool {
190        match *self {
191            FrameType::Headers => true,
192            _ => false
193        }
194    }
195
196    /// Indicates that this is a `FrameType::Ping`.
197    pub fn is_push_ping(&self) -> bool {
198        match *self {
199            FrameType::Ping => true,
200            _ => false
201        }
202    }
203
204    /// Indicates that this is a `FrameType::Priority`.
205    pub fn is_priority(&self) -> bool {
206        match *self {
207            FrameType::Priority => true,
208            _ => false
209        }
210    }
211
212    /// Indicates that this is a `FrameType::PushPromise`.
213    pub fn is_push_promise(&self) -> bool {
214        match *self {
215            FrameType::PushPromise => true,
216            _ => false
217        }
218    }
219
220    /// Indicates that this is a `FrameType::RstStream`.
221    pub fn is_rst_stream(&self) -> bool {
222        match *self {
223            FrameType::RstStream => true,
224            _ => false
225        }
226    }
227
228    /// Indicates that this is a `FrameType::Settings`.
229    pub fn is_settings(&self) -> bool {
230        match *self {
231            FrameType::Settings => true,
232            _ => false
233        }
234    }
235
236    /// Indicates that this is a `FrameType::Unsupported`.
237    pub fn is_unsupported(&self) -> bool {
238        match *self {
239            FrameType::Unsupported => true,
240            _ => false
241        }
242    }
243
244    /// Indicates that this is a `FrameType::WindowUpdate`.
245    pub fn is_window_update(&self) -> bool {
246        match *self {
247            FrameType::WindowUpdate => true,
248            _ => false
249        }
250    }
251}
252
253impl fmt::Debug for FrameType {
254    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
255        self.format(formatter)
256    }
257}
258
259impl fmt::Display for FrameType {
260    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
261        self.format(formatter)
262    }
263}