Coverage Report

Created: 2026-08-14 08:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/h2-0.4.13/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
0
    pub(crate) fn flow_controlled_len(&self) -> usize {
144
0
        if let Some(pad_len) = self.pad_len {
145
            // if PADDED, pad length field counts too (the + 1)
146
0
            self.data.len() + usize::from(pad_len) + 1
147
        } else {
148
0
            self.data.len()
149
        }
150
0
    }
151
152
    /// If this frame is PADDED, it returns the pad len + 1 (length field).
153
0
    pub(crate) fn padded_len(&self) -> Option<u8> {
154
0
        self.pad_len.map(|n| n + 1)
155
0
    }
156
}
157
158
impl<T: Buf> Data<T> {
159
    /// Encode the data frame into the `dst` buffer.
160
    ///
161
    /// # Panics
162
    ///
163
    /// Panics if `dst` cannot contain the data frame.
164
0
    pub(crate) fn encode_chunk<U: BufMut>(&mut self, dst: &mut U) {
165
0
        let len = self.data.remaining();
166
167
0
        assert!(dst.remaining_mut() >= len);
168
169
0
        self.head().encode(len, dst);
170
0
        dst.put(&mut self.data);
171
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::<_>
172
}
173
174
impl<T> From<Data<T>> for Frame<T> {
175
0
    fn from(src: Data<T>) -> Self {
176
0
        Frame::Data(src)
177
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
178
}
179
180
impl<T> fmt::Debug for Data<T> {
181
0
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
182
0
        let mut f = fmt.debug_struct("Data");
183
0
        f.field("stream_id", &self.stream_id);
184
0
        if !self.flags.is_empty() {
185
0
            f.field("flags", &self.flags);
186
0
        }
187
0
        if let Some(ref pad_len) = self.pad_len {
188
0
            f.field("pad_len", pad_len);
189
0
        }
190
        // `data` bytes purposefully excluded
191
0
        f.finish()
192
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
193
}
194
195
// ===== impl DataFlags =====
196
197
impl DataFlags {
198
0
    fn load(bits: u8) -> DataFlags {
199
0
        DataFlags(bits & ALL)
200
0
    }
201
202
0
    fn is_empty(&self) -> bool {
203
0
        self.0 == 0
204
0
    }
205
206
0
    fn is_end_stream(&self) -> bool {
207
0
        self.0 & END_STREAM == END_STREAM
208
0
    }
209
210
0
    fn set_end_stream(&mut self) {
211
0
        self.0 |= END_STREAM
212
0
    }
213
214
0
    fn unset_end_stream(&mut self) {
215
0
        self.0 &= !END_STREAM
216
0
    }
217
218
0
    fn is_padded(&self) -> bool {
219
0
        self.0 & PADDED == PADDED
220
0
    }
221
222
    #[cfg(feature = "unstable")]
223
    fn set_padded(&mut self) {
224
        self.0 |= PADDED
225
    }
226
}
227
228
impl From<DataFlags> for u8 {
229
0
    fn from(src: DataFlags) -> u8 {
230
0
        src.0
231
0
    }
232
}
233
234
impl fmt::Debug for DataFlags {
235
0
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
236
0
        util::debug_flags(fmt, self.0)
237
0
            .flag_if(self.is_end_stream(), "END_STREAM")
238
0
            .flag_if(self.is_padded(), "PADDED")
239
0
            .finish()
240
0
    }
241
}