Coverage Report

Created: 2026-05-30 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/h2/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
486k
    pub fn new(stream_id: StreamId, payload: T) -> Self {
29
486k
        assert!(!stream_id.is_zero());
30
31
486k
        Data {
32
486k
            stream_id,
33
486k
            data: payload,
34
486k
            flags: DataFlags::default(),
35
486k
            pad_len: None,
36
486k
        }
37
486k
    }
38
39
    /// Returns the stream identifier that this frame is associated with.
40
    ///
41
    /// This cannot be a zero stream identifier.
42
56.4k
    pub fn stream_id(&self) -> StreamId {
43
56.4k
        self.stream_id
44
56.4k
    }
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
502k
    pub fn is_end_stream(&self) -> bool {
54
502k
        self.flags.is_end_stream()
55
502k
    }
56
57
    /// Sets the value for the `END_STREAM` flag on this frame.
58
516k
    pub fn set_end_stream(&mut self, val: bool) {
59
516k
        if val {
60
501k
            self.flags.set_end_stream();
61
501k
        } else {
62
15.3k
            self.flags.unset_end_stream();
63
15.3k
        }
64
516k
    }
Unexecuted instantiation: <h2::frame::data::Data>::set_end_stream
<h2::frame::data::Data>::set_end_stream
Line
Count
Source
58
516k
    pub fn set_end_stream(&mut self, val: bool) {
59
516k
        if val {
60
501k
            self.flags.set_end_stream();
61
501k
        } else {
62
15.3k
            self.flags.unset_end_stream();
63
15.3k
        }
64
516k
    }
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
0
    pub fn set_padded(&mut self) {
75
0
        self.flags.set_padded();
76
0
    }
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
931k
    pub fn payload(&self) -> &T {
83
931k
        &self.data
84
931k
    }
<h2::frame::data::Data>::payload
Line
Count
Source
82
558k
    pub fn payload(&self) -> &T {
83
558k
        &self.data
84
558k
    }
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
372k
    pub fn payload(&self) -> &T {
83
372k
        &self.data
84
372k
    }
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
313k
    pub fn payload_mut(&mut self) -> &mut T {
91
313k
        &mut self.data
92
313k
    }
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
313k
    pub fn payload_mut(&mut self) -> &mut T {
91
313k
        &mut self.data
92
313k
    }
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
7
    pub fn into_payload(self) -> T {
99
7
        self.data
100
7
    }
101
102
15.9k
    pub(crate) fn head(&self) -> Head {
103
15.9k
        Head::new(Kind::Data, self.flags.into(), self.stream_id)
104
15.9k
    }
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
15.9k
    pub(crate) fn head(&self) -> Head {
103
15.9k
        Head::new(Kind::Data, self.flags.into(), self.stream_id)
104
15.9k
    }
105
106
30.7k
    pub(crate) fn map<F, U>(self, f: F) -> Data<U>
107
30.7k
    where
108
30.7k
        F: FnOnce(T) -> U,
109
    {
110
30.7k
        Data {
111
30.7k
            stream_id: self.stream_id,
112
30.7k
            data: f(self.data),
113
30.7k
            flags: self.flags,
114
30.7k
            pad_len: self.pad_len,
115
30.7k
        }
116
30.7k
    }
<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
14.7k
    pub(crate) fn map<F, U>(self, f: F) -> Data<U>
107
14.7k
    where
108
14.7k
        F: FnOnce(T) -> U,
109
    {
110
14.7k
        Data {
111
14.7k
            stream_id: self.stream_id,
112
14.7k
            data: f(self.data),
113
14.7k
            flags: self.flags,
114
14.7k
            pad_len: self.pad_len,
115
14.7k
        }
116
14.7k
    }
<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
15.9k
    pub(crate) fn map<F, U>(self, f: F) -> Data<U>
107
15.9k
    where
108
15.9k
        F: FnOnce(T) -> U,
109
    {
110
15.9k
        Data {
111
15.9k
            stream_id: self.stream_id,
112
15.9k
            data: f(self.data),
113
15.9k
            flags: self.flags,
114
15.9k
            pad_len: self.pad_len,
115
15.9k
        }
116
15.9k
    }
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
56.5k
    pub(crate) fn load(head: Head, mut payload: Bytes) -> Result<Self, Error> {
121
56.5k
        let flags = DataFlags::load(head.flag());
122
123
        // The stream identifier must not be zero
124
56.5k
        if head.stream_id().is_zero() {
125
26
            return Err(Error::InvalidStreamId);
126
56.4k
        }
127
128
56.4k
        let pad_len = if flags.is_padded() {
129
1.55k
            let len = util::strip_padding(&mut payload)?;
130
1.52k
            Some(len)
131
        } else {
132
54.9k
            None
133
        };
134
135
56.4k
        Ok(Data {
136
56.4k
            stream_id: head.stream_id(),
137
56.4k
            data: payload,
138
56.4k
            flags,
139
56.4k
            pad_len,
140
56.4k
        })
141
56.5k
    }
142
143
97.5k
    pub(crate) fn flow_controlled_len(&self) -> usize {
144
97.5k
        if let Some(pad_len) = self.pad_len {
145
            // if PADDED, pad length field counts too (the + 1)
146
2.05k
            self.data.len() + usize::from(pad_len) + 1
147
        } else {
148
95.4k
            self.data.len()
149
        }
150
97.5k
    }
151
152
    /// If this frame is PADDED, it returns the pad len + 1 (length field).
153
7
    pub(crate) fn padded_len(&self) -> Option<u8> {
154
7
        self.pad_len.map(|n| n + 1)
155
7
    }
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
12.3k
    pub(crate) fn encode_chunk<U: BufMut>(&mut self, dst: &mut U) {
165
12.3k
        let len = self.data.remaining();
166
167
12.3k
        assert!(dst.remaining_mut() >= len);
168
169
12.3k
        self.head().encode(len, dst);
170
12.3k
        dst.put(&mut self.data);
171
12.3k
    }
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
164
12.3k
    pub(crate) fn encode_chunk<U: BufMut>(&mut self, dst: &mut U) {
165
12.3k
        let len = self.data.remaining();
166
167
12.3k
        assert!(dst.remaining_mut() >= len);
168
169
12.3k
        self.head().encode(len, dst);
170
12.3k
        dst.put(&mut self.data);
171
12.3k
    }
172
}
173
174
impl<T> From<Data<T>> for Frame<T> {
175
582k
    fn from(src: Data<T>) -> Self {
176
582k
        Frame::Data(src)
177
582k
    }
<h2::frame::Frame as core::convert::From<h2::frame::data::Data>>::from
Line
Count
Source
175
582k
    fn from(src: Data<T>) -> Self {
176
582k
        Frame::Data(src)
177
582k
    }
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
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<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
193
}
194
195
// ===== impl DataFlags =====
196
197
impl DataFlags {
198
56.5k
    fn load(bits: u8) -> DataFlags {
199
56.5k
        DataFlags(bits & ALL)
200
56.5k
    }
201
202
0
    fn is_empty(&self) -> bool {
203
0
        self.0 == 0
204
0
    }
205
206
502k
    fn is_end_stream(&self) -> bool {
207
502k
        self.0 & END_STREAM == END_STREAM
208
502k
    }
209
210
501k
    fn set_end_stream(&mut self) {
211
501k
        self.0 |= END_STREAM
212
501k
    }
213
214
15.3k
    fn unset_end_stream(&mut self) {
215
15.3k
        self.0 &= !END_STREAM
216
15.3k
    }
217
218
56.4k
    fn is_padded(&self) -> bool {
219
56.4k
        self.0 & PADDED == PADDED
220
56.4k
    }
221
222
    #[cfg(feature = "unstable")]
223
0
    fn set_padded(&mut self) {
224
0
        self.0 |= PADDED
225
0
    }
226
}
227
228
impl From<DataFlags> for u8 {
229
15.9k
    fn from(src: DataFlags) -> u8 {
230
15.9k
        src.0
231
15.9k
    }
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
}