/rust/registry/src/index.crates.io-1949cf8c6b5b557f/h2-0.4.13/src/frame/head.rs
Line | Count | Source |
1 | | use super::StreamId; |
2 | | |
3 | | use bytes::BufMut; |
4 | | |
5 | | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
6 | | pub struct Head { |
7 | | kind: Kind, |
8 | | flag: u8, |
9 | | stream_id: StreamId, |
10 | | } |
11 | | |
12 | | #[repr(u8)] |
13 | | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
14 | | pub enum Kind { |
15 | | Data = 0, |
16 | | Headers = 1, |
17 | | Priority = 2, |
18 | | Reset = 3, |
19 | | Settings = 4, |
20 | | PushPromise = 5, |
21 | | Ping = 6, |
22 | | GoAway = 7, |
23 | | WindowUpdate = 8, |
24 | | Continuation = 9, |
25 | | Unknown, |
26 | | } |
27 | | |
28 | | // ===== impl Head ===== |
29 | | |
30 | | impl Head { |
31 | 0 | pub fn new(kind: Kind, flag: u8, stream_id: StreamId) -> Head { |
32 | 0 | Head { |
33 | 0 | kind, |
34 | 0 | flag, |
35 | 0 | stream_id, |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | | /// Parse an HTTP/2 frame header |
40 | 0 | pub fn parse(header: &[u8]) -> Head { |
41 | 0 | let (stream_id, _) = StreamId::parse(&header[5..]); |
42 | | |
43 | 0 | Head { |
44 | 0 | kind: Kind::new(header[3]), |
45 | 0 | flag: header[4], |
46 | 0 | stream_id, |
47 | 0 | } |
48 | 0 | } |
49 | | |
50 | 0 | pub fn stream_id(&self) -> StreamId { |
51 | 0 | self.stream_id |
52 | 0 | } |
53 | | |
54 | 0 | pub fn kind(&self) -> Kind { |
55 | 0 | self.kind |
56 | 0 | } |
57 | | |
58 | 0 | pub fn flag(&self) -> u8 { |
59 | 0 | self.flag |
60 | 0 | } |
61 | | |
62 | 0 | pub fn encode_len(&self) -> usize { |
63 | 0 | super::HEADER_LEN |
64 | 0 | } |
65 | | |
66 | 0 | pub fn encode<T: BufMut>(&self, payload_len: usize, dst: &mut T) { |
67 | 0 | debug_assert!(self.encode_len() <= dst.remaining_mut()); |
68 | | |
69 | 0 | dst.put_uint(payload_len as u64, 3); |
70 | 0 | dst.put_u8(self.kind as u8); |
71 | 0 | dst.put_u8(self.flag); |
72 | 0 | dst.put_u32(self.stream_id.into()); |
73 | 0 | } Unexecuted instantiation: <h2::frame::head::Head>::encode::<bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut>> Unexecuted instantiation: <h2::frame::head::Head>::encode::<bytes::bytes_mut::BytesMut> |
74 | | } |
75 | | |
76 | | // ===== impl Kind ===== |
77 | | |
78 | | impl Kind { |
79 | 0 | pub fn new(byte: u8) -> Kind { |
80 | 0 | match byte { |
81 | 0 | 0 => Kind::Data, |
82 | 0 | 1 => Kind::Headers, |
83 | 0 | 2 => Kind::Priority, |
84 | 0 | 3 => Kind::Reset, |
85 | 0 | 4 => Kind::Settings, |
86 | 0 | 5 => Kind::PushPromise, |
87 | 0 | 6 => Kind::Ping, |
88 | 0 | 7 => Kind::GoAway, |
89 | 0 | 8 => Kind::WindowUpdate, |
90 | 0 | 9 => Kind::Continuation, |
91 | 0 | _ => Kind::Unknown, |
92 | | } |
93 | 0 | } |
94 | | } |