/src/h2/src/frame/data.rs
Line | Count | Source (jump to first uncovered line) |
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 | 413k | pub fn new(stream_id: StreamId, payload: T) -> Self { |
29 | 413k | assert!(!stream_id.is_zero()); |
30 | | |
31 | 413k | Data { |
32 | 413k | stream_id, |
33 | 413k | data: payload, |
34 | 413k | flags: DataFlags::default(), |
35 | 413k | pad_len: None, |
36 | 413k | } |
37 | 413k | } Unexecuted instantiation: <h2::frame::data::Data<_>>::new <h2::frame::data::Data>::new Line | Count | Source | 28 | 413k | pub fn new(stream_id: StreamId, payload: T) -> Self { | 29 | 413k | assert!(!stream_id.is_zero()); | 30 | | | 31 | 413k | Data { | 32 | 413k | stream_id, | 33 | 413k | data: payload, | 34 | 413k | flags: DataFlags::default(), | 35 | 413k | pad_len: None, | 36 | 413k | } | 37 | 413k | } |
|
38 | | |
39 | | /// Returns the stream identifier that this frame is associated with. |
40 | | /// |
41 | | /// This cannot be a zero stream identifier. |
42 | 65.3k | pub fn stream_id(&self) -> StreamId { |
43 | 65.3k | self.stream_id |
44 | 65.3k | } Unexecuted instantiation: <h2::frame::data::Data<_>>::stream_id <h2::frame::data::Data>::stream_id Line | Count | Source | 42 | 65.3k | pub fn stream_id(&self) -> StreamId { | 43 | 65.3k | self.stream_id | 44 | 65.3k | } |
|
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 | 434k | pub fn is_end_stream(&self) -> bool { |
54 | 434k | self.flags.is_end_stream() |
55 | 434k | } |
56 | | |
57 | | /// Sets the value for the `END_STREAM` flag on this frame. |
58 | 447k | pub fn set_end_stream(&mut self, val: bool) { |
59 | 447k | if val { |
60 | 429k | self.flags.set_end_stream(); |
61 | 429k | } else { |
62 | 17.3k | self.flags.unset_end_stream(); |
63 | 17.3k | } |
64 | 447k | } Unexecuted instantiation: <h2::frame::data::Data<_>>::set_end_stream Unexecuted instantiation: <h2::frame::data::Data>::set_end_stream <h2::frame::data::Data>::set_end_stream Line | Count | Source | 58 | 447k | pub fn set_end_stream(&mut self, val: bool) { | 59 | 447k | if val { | 60 | 429k | self.flags.set_end_stream(); | 61 | 429k | } else { | 62 | 17.3k | self.flags.unset_end_stream(); | 63 | 17.3k | } | 64 | 447k | } |
|
65 | | |
66 | | /// Returns whether the `PADDED` flag is set on this frame. |
67 | | #[cfg(feature = "unstable")] |
68 | 0 | pub fn is_padded(&self) -> bool { |
69 | 0 | self.flags.is_padded() |
70 | 0 | } |
71 | | |
72 | | /// Sets the value for the `PADDED` flag on this frame. |
73 | | #[cfg(feature = "unstable")] |
74 | 0 | pub fn set_padded(&mut self) { |
75 | 0 | self.flags.set_padded(); |
76 | 0 | } Unexecuted instantiation: <h2::frame::data::Data<_>>::set_padded Unexecuted instantiation: <h2::frame::data::Data>::set_padded |
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 | 1.50M | pub fn payload(&self) -> &T { |
83 | 1.50M | &self.data |
84 | 1.50M | } <h2::frame::data::Data>::payload Line | Count | Source | 82 | 576k | pub fn payload(&self) -> &T { | 83 | 576k | &self.data | 84 | 576k | } |
Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>::payload <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>::payload Line | Count | Source | 82 | 924k | pub fn payload(&self) -> &T { | 83 | 924k | &self.data | 84 | 924k | } |
|
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 | 856k | pub fn payload_mut(&mut self) -> &mut T { |
91 | 856k | &mut self.data |
92 | 856k | } Unexecuted instantiation: <h2::frame::data::Data<_>>::payload_mut Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>::payload_mut Unexecuted instantiation: <h2::frame::data::Data>::payload_mut <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>::payload_mut Line | Count | Source | 90 | 856k | pub fn payload_mut(&mut self) -> &mut T { | 91 | 856k | &mut self.data | 92 | 856k | } |
|
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 | 2.45k | pub fn into_payload(self) -> T { |
99 | 2.45k | self.data |
100 | 2.45k | } |
101 | | |
102 | 18.2k | pub(crate) fn head(&self) -> Head { |
103 | 18.2k | Head::new(Kind::Data, self.flags.into(), self.stream_id) |
104 | 18.2k | } Unexecuted instantiation: <h2::frame::data::Data<_>>::head Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>::head <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>::head Line | Count | Source | 102 | 18.2k | pub(crate) fn head(&self) -> Head { | 103 | 18.2k | Head::new(Kind::Data, self.flags.into(), self.stream_id) | 104 | 18.2k | } |
|
105 | | |
106 | 35.0k | pub(crate) fn map<F, U>(self, f: F) -> Data<U> |
107 | 35.0k | where |
108 | 35.0k | F: FnOnce(T) -> U, |
109 | 35.0k | { |
110 | 35.0k | Data { |
111 | 35.0k | stream_id: self.stream_id, |
112 | 35.0k | data: f(self.data), |
113 | 35.0k | flags: self.flags, |
114 | 35.0k | pad_len: self.pad_len, |
115 | 35.0k | } |
116 | 35.0k | } Unexecuted instantiation: <h2::frame::data::Data<_>>::map::<_, _> <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>::map::<<h2::proto::streams::prioritize::Prioritize>::reclaim_frame_inner<bytes::bytes::Bytes>::{closure#0}, bytes::bytes::Bytes> Line | Count | Source | 106 | 16.8k | pub(crate) fn map<F, U>(self, f: F) -> Data<U> | 107 | 16.8k | where | 108 | 16.8k | F: FnOnce(T) -> U, | 109 | 16.8k | { | 110 | 16.8k | Data { | 111 | 16.8k | stream_id: self.stream_id, | 112 | 16.8k | data: f(self.data), | 113 | 16.8k | flags: self.flags, | 114 | 16.8k | pad_len: self.pad_len, | 115 | 16.8k | } | 116 | 16.8k | } |
<h2::frame::data::Data>::map::<<h2::proto::streams::prioritize::Prioritize>::pop_frame<bytes::bytes::Bytes>::{closure#2}, h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>> Line | Count | Source | 106 | 18.2k | pub(crate) fn map<F, U>(self, f: F) -> Data<U> | 107 | 18.2k | where | 108 | 18.2k | F: FnOnce(T) -> U, | 109 | 18.2k | { | 110 | 18.2k | Data { | 111 | 18.2k | stream_id: self.stream_id, | 112 | 18.2k | data: f(self.data), | 113 | 18.2k | flags: self.flags, | 114 | 18.2k | pad_len: self.pad_len, | 115 | 18.2k | } | 116 | 18.2k | } |
Unexecuted instantiation: <h2::frame::data::Data>::map::<<h2::proto::streams::prioritize::Prioritize>::pop_frame<bytes::bytes::Bytes>::{closure#3}, h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>> |
117 | | } |
118 | | |
119 | | impl Data<Bytes> { |
120 | 65.4k | pub(crate) fn load(head: Head, mut payload: Bytes) -> Result<Self, Error> { |
121 | 65.4k | let flags = DataFlags::load(head.flag()); |
122 | 65.4k | |
123 | 65.4k | // The stream identifier must not be zero |
124 | 65.4k | if head.stream_id().is_zero() { |
125 | 56 | return Err(Error::InvalidStreamId); |
126 | 65.3k | } |
127 | | |
128 | 65.3k | let pad_len = if flags.is_padded() { |
129 | 401 | let len = util::strip_padding(&mut payload)?; |
130 | 373 | Some(len) |
131 | | } else { |
132 | 64.9k | None |
133 | | }; |
134 | | |
135 | 65.3k | Ok(Data { |
136 | 65.3k | stream_id: head.stream_id(), |
137 | 65.3k | data: payload, |
138 | 65.3k | flags, |
139 | 65.3k | pad_len, |
140 | 65.3k | }) |
141 | 65.4k | } |
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 | 14.4k | pub(crate) fn encode_chunk<U: BufMut>(&mut self, dst: &mut U) { |
151 | 14.4k | let len = self.data.remaining(); |
152 | 14.4k | |
153 | 14.4k | assert!(dst.remaining_mut() >= len); |
154 | | |
155 | 14.4k | self.head().encode(len, dst); |
156 | 14.4k | dst.put(&mut self.data); |
157 | 14.4k | } Unexecuted instantiation: <h2::frame::data::Data<_>>::encode_chunk::<_> Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>::encode_chunk::<bytes::bytes_mut::BytesMut> <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>::encode_chunk::<bytes::bytes_mut::BytesMut> Line | Count | Source | 150 | 14.4k | pub(crate) fn encode_chunk<U: BufMut>(&mut self, dst: &mut U) { | 151 | 14.4k | let len = self.data.remaining(); | 152 | 14.4k | | 153 | 14.4k | assert!(dst.remaining_mut() >= len); | 154 | | | 155 | 14.4k | self.head().encode(len, dst); | 156 | 14.4k | dst.put(&mut self.data); | 157 | 14.4k | } |
|
158 | | } |
159 | | |
160 | | impl<T> From<Data<T>> for Frame<T> { |
161 | 524k | fn from(src: Data<T>) -> Self { |
162 | 524k | Frame::Data(src) |
163 | 524k | } <h2::frame::Frame as core::convert::From<h2::frame::data::Data>>::from Line | Count | Source | 161 | 524k | fn from(src: Data<T>) -> Self { | 162 | 524k | Frame::Data(src) | 163 | 524k | } |
Unexecuted instantiation: <h2::frame::Frame<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>> as core::convert::From<h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>>>>::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<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <h2::frame::data::Data<h2::proto::streams::prioritize::Prioritized<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<h2::proto::streams::prioritize::Prioritized<bytes::bytes::Bytes>> 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 | 65.4k | fn load(bits: u8) -> DataFlags { |
185 | 65.4k | DataFlags(bits & ALL) |
186 | 65.4k | } |
187 | | |
188 | 0 | fn is_empty(&self) -> bool { |
189 | 0 | self.0 == 0 |
190 | 0 | } |
191 | | |
192 | 434k | fn is_end_stream(&self) -> bool { |
193 | 434k | self.0 & END_STREAM == END_STREAM |
194 | 434k | } |
195 | | |
196 | 429k | fn set_end_stream(&mut self) { |
197 | 429k | self.0 |= END_STREAM |
198 | 429k | } |
199 | | |
200 | 17.3k | fn unset_end_stream(&mut self) { |
201 | 17.3k | self.0 &= !END_STREAM |
202 | 17.3k | } |
203 | | |
204 | 65.3k | fn is_padded(&self) -> bool { |
205 | 65.3k | self.0 & PADDED == PADDED |
206 | 65.3k | } |
207 | | |
208 | | #[cfg(feature = "unstable")] |
209 | 0 | fn set_padded(&mut self) { |
210 | 0 | self.0 |= PADDED |
211 | 0 | } |
212 | | } |
213 | | |
214 | | impl From<DataFlags> for u8 { |
215 | 18.2k | fn from(src: DataFlags) -> u8 { |
216 | 18.2k | src.0 |
217 | 18.2k | } |
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 | | } |