/rust/registry/src/index.crates.io-1949cf8c6b5b557f/h2-0.4.11/src/frame/data.rs
Line | Count | Source |
1 | | use crate::frame::{util, Error, Frame, Head, Kind, StreamId}; |
2 | | use bytes::{Buf, BufMut, Bytes}; |
3 | | |
4 | | use std::fmt; |
5 | | |
6 | | /// Data frame |
7 | | /// |
8 | | /// Data frames convey arbitrary, variable-length sequences of octets associated |
9 | | /// with a stream. One or more DATA frames are used, for instance, to carry HTTP |
10 | | /// request or response payloads. |
11 | | #[derive(Eq, PartialEq)] |
12 | | pub struct Data<T = Bytes> { |
13 | | stream_id: StreamId, |
14 | | data: T, |
15 | | flags: DataFlags, |
16 | | pad_len: Option<u8>, |
17 | | } |
18 | | |
19 | | #[derive(Copy, Clone, Default, Eq, PartialEq)] |
20 | | struct DataFlags(u8); |
21 | | |
22 | | const END_STREAM: u8 = 0x1; |
23 | | const PADDED: u8 = 0x8; |
24 | | const ALL: u8 = END_STREAM | PADDED; |
25 | | |
26 | | impl<T> Data<T> { |
27 | | /// Creates a new DATA frame. |
28 | 0 | pub fn new(stream_id: StreamId, payload: T) -> Self { |
29 | 0 | assert!(!stream_id.is_zero()); |
30 | | |
31 | 0 | Data { |
32 | 0 | stream_id, |
33 | 0 | data: payload, |
34 | 0 | flags: DataFlags::default(), |
35 | 0 | pad_len: None, |
36 | 0 | } |
37 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>::new Unexecuted instantiation: <h2::frame::data::Data<_>>::new |
38 | | |
39 | | /// Returns the stream identifier that this frame is associated with. |
40 | | /// |
41 | | /// This cannot be a zero stream identifier. |
42 | 0 | pub fn stream_id(&self) -> StreamId { |
43 | 0 | self.stream_id |
44 | 0 | } Unexecuted instantiation: <h2::frame::data::Data>::stream_id Unexecuted instantiation: <h2::frame::data::Data<_>>::stream_id |
45 | | |
46 | | /// Gets the value of the `END_STREAM` flag for this frame. |
47 | | /// |
48 | | /// If true, this frame is the last that the endpoint will send for the |
49 | | /// identified stream. |
50 | | /// |
51 | | /// Setting this flag causes the stream to enter one of the "half-closed" |
52 | | /// states or the "closed" state (Section 5.1). |
53 | 0 | pub fn is_end_stream(&self) -> bool { |
54 | 0 | self.flags.is_end_stream() |
55 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>::is_end_stream Unexecuted instantiation: <h2::frame::data::Data>::is_end_stream |
56 | | |
57 | | /// Sets the value for the `END_STREAM` flag on this frame. |
58 | 0 | pub fn set_end_stream(&mut self, val: bool) { |
59 | 0 | if val { |
60 | 0 | self.flags.set_end_stream(); |
61 | 0 | } else { |
62 | 0 | self.flags.unset_end_stream(); |
63 | 0 | } |
64 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>::set_end_stream Unexecuted instantiation: <h2::frame::data::Data<_>>::set_end_stream |
65 | | |
66 | | /// Returns whether the `PADDED` flag is set on this frame. |
67 | | #[cfg(feature = "unstable")] |
68 | | pub fn is_padded(&self) -> bool { |
69 | | self.flags.is_padded() |
70 | | } |
71 | | |
72 | | /// Sets the value for the `PADDED` flag on this frame. |
73 | | #[cfg(feature = "unstable")] |
74 | | pub fn set_padded(&mut self) { |
75 | | self.flags.set_padded(); |
76 | | } |
77 | | |
78 | | /// Returns a reference to this frame's payload. |
79 | | /// |
80 | | /// This does **not** include any padding that might have been originally |
81 | | /// included. |
82 | 0 | pub fn payload(&self) -> &T { |
83 | 0 | &self.data |
84 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>::payload Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>>::payload Unexecuted instantiation: <h2::frame::data::Data>::payload |
85 | | |
86 | | /// Returns a mutable reference to this frame's payload. |
87 | | /// |
88 | | /// This does **not** include any padding that might have been originally |
89 | | /// included. |
90 | 0 | pub fn payload_mut(&mut self) -> &mut T { |
91 | 0 | &mut self.data |
92 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>>::payload_mut Unexecuted instantiation: <h2::frame::data::Data<_>>::payload_mut |
93 | | |
94 | | /// Consumes `self` and returns the frame's payload. |
95 | | /// |
96 | | /// This does **not** include any padding that might have been originally |
97 | | /// included. |
98 | 0 | pub fn into_payload(self) -> T { |
99 | 0 | self.data |
100 | 0 | } |
101 | | |
102 | 0 | pub(crate) fn head(&self) -> Head { |
103 | 0 | Head::new(Kind::Data, self.flags.into(), self.stream_id) |
104 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>>::head Unexecuted instantiation: <h2::frame::data::Data<_>>::head |
105 | | |
106 | 0 | pub(crate) fn map<F, U>(self, f: F) -> Data<U> |
107 | 0 | where |
108 | 0 | F: FnOnce(T) -> U, |
109 | | { |
110 | 0 | Data { |
111 | 0 | stream_id: self.stream_id, |
112 | 0 | data: f(self.data), |
113 | 0 | flags: self.flags, |
114 | 0 | pad_len: self.pad_len, |
115 | 0 | } |
116 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>::map::<<h2::proto::streams::prioritize::Prioritize>::pop_frame<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>::{closure#2}, h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>Unexecuted instantiation: <h2::frame::data::Data<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>::map::<<h2::proto::streams::prioritize::Prioritize>::pop_frame<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>::{closure#3}, h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>>::map::<<h2::proto::streams::prioritize::Prioritize>::reclaim_frame_inner<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>::{closure#0}, hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>Unexecuted instantiation: <h2::frame::data::Data<_>>::map::<_, _> |
117 | | } |
118 | | |
119 | | impl Data<Bytes> { |
120 | 0 | pub(crate) fn load(head: Head, mut payload: Bytes) -> Result<Self, Error> { |
121 | 0 | let flags = DataFlags::load(head.flag()); |
122 | | |
123 | | // The stream identifier must not be zero |
124 | 0 | if head.stream_id().is_zero() { |
125 | 0 | return Err(Error::InvalidStreamId); |
126 | 0 | } |
127 | | |
128 | 0 | let pad_len = if flags.is_padded() { |
129 | 0 | let len = util::strip_padding(&mut payload)?; |
130 | 0 | Some(len) |
131 | | } else { |
132 | 0 | None |
133 | | }; |
134 | | |
135 | 0 | Ok(Data { |
136 | 0 | stream_id: head.stream_id(), |
137 | 0 | data: payload, |
138 | 0 | flags, |
139 | 0 | pad_len, |
140 | 0 | }) |
141 | 0 | } |
142 | | } |
143 | | |
144 | | impl<T: Buf> Data<T> { |
145 | | /// Encode the data frame into the `dst` buffer. |
146 | | /// |
147 | | /// # Panics |
148 | | /// |
149 | | /// Panics if `dst` cannot contain the data frame. |
150 | 0 | pub(crate) fn encode_chunk<U: BufMut>(&mut self, dst: &mut U) { |
151 | 0 | let len = self.data.remaining(); |
152 | | |
153 | 0 | assert!(dst.remaining_mut() >= len); |
154 | | |
155 | 0 | self.head().encode(len, dst); |
156 | 0 | dst.put(&mut self.data); |
157 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>>::encode_chunk::<bytes::bytes_mut::BytesMut> Unexecuted instantiation: <h2::frame::data::Data<_>>::encode_chunk::<_> |
158 | | } |
159 | | |
160 | | impl<T> From<Data<T>> for Frame<T> { |
161 | 0 | fn from(src: Data<T>) -> Self { |
162 | 0 | Frame::Data(src) |
163 | 0 | } Unexecuted instantiation: <h2::frame::Frame<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>> as core::convert::From<h2::frame::data::Data<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>>::from Unexecuted instantiation: <h2::frame::Frame<h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>> as core::convert::From<h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>>>>::from Unexecuted instantiation: <h2::frame::Frame as core::convert::From<h2::frame::data::Data>>::from |
164 | | } |
165 | | |
166 | | impl<T> fmt::Debug for Data<T> { |
167 | 0 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
168 | 0 | let mut f = fmt.debug_struct("Data"); |
169 | 0 | f.field("stream_id", &self.stream_id); |
170 | 0 | if !self.flags.is_empty() { |
171 | 0 | f.field("flags", &self.flags); |
172 | 0 | } |
173 | 0 | if let Some(ref pad_len) = self.pad_len { |
174 | 0 | f.field("pad_len", pad_len); |
175 | 0 | } |
176 | | // `data` bytes purposefully excluded |
177 | 0 | f.finish() |
178 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>> as core::fmt::Debug>::fmt Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<hyper::proto::h2::SendBuf<bytes::bytes::Bytes>>> as core::fmt::Debug>::fmt Unexecuted instantiation: <h2::frame::data::Data as core::fmt::Debug>::fmt Unexecuted instantiation: <h2::frame::data::Data<_> as core::fmt::Debug>::fmt |
179 | | } |
180 | | |
181 | | // ===== impl DataFlags ===== |
182 | | |
183 | | impl DataFlags { |
184 | 0 | fn load(bits: u8) -> DataFlags { |
185 | 0 | DataFlags(bits & ALL) |
186 | 0 | } |
187 | | |
188 | 0 | fn is_empty(&self) -> bool { |
189 | 0 | self.0 == 0 |
190 | 0 | } |
191 | | |
192 | 0 | fn is_end_stream(&self) -> bool { |
193 | 0 | self.0 & END_STREAM == END_STREAM |
194 | 0 | } |
195 | | |
196 | 0 | fn set_end_stream(&mut self) { |
197 | 0 | self.0 |= END_STREAM |
198 | 0 | } |
199 | | |
200 | 0 | fn unset_end_stream(&mut self) { |
201 | 0 | self.0 &= !END_STREAM |
202 | 0 | } |
203 | | |
204 | 0 | fn is_padded(&self) -> bool { |
205 | 0 | self.0 & PADDED == PADDED |
206 | 0 | } |
207 | | |
208 | | #[cfg(feature = "unstable")] |
209 | | fn set_padded(&mut self) { |
210 | | self.0 |= PADDED |
211 | | } |
212 | | } |
213 | | |
214 | | impl From<DataFlags> for u8 { |
215 | 0 | fn from(src: DataFlags) -> u8 { |
216 | 0 | src.0 |
217 | 0 | } |
218 | | } |
219 | | |
220 | | impl fmt::Debug for DataFlags { |
221 | 0 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
222 | 0 | util::debug_flags(fmt, self.0) |
223 | 0 | .flag_if(self.is_end_stream(), "END_STREAM") |
224 | 0 | .flag_if(self.is_padded(), "PADDED") |
225 | 0 | .finish() |
226 | 0 | } |
227 | | } |