Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::protocols::http2::data_frame Class Reference

DATA frame (RFC 7540 Section 6.1) More...

#include <frame.h>

Inheritance diagram for kcenon::network::protocols::http2::data_frame:
Inheritance graph
Collaboration diagram for kcenon::network::protocols::http2::data_frame:
Collaboration graph

Public Member Functions

 data_frame (uint32_t stream_id, std::vector< uint8_t > data, bool end_stream=false, bool padded=false)
 Construct DATA frame.
 
auto is_end_stream () const -> bool
 Check if END_STREAM flag is set.
 
auto is_padded () const -> bool
 Check if frame is padded.
 
auto data () const -> std::span< const uint8_t >
 Get actual data (without padding)
 
- Public Member Functions inherited from kcenon::network::protocols::http2::frame
 frame ()=default
 Default constructor.
 
 frame (const frame_header &hdr, std::vector< uint8_t > payload)
 Construct frame with header and payload.
 
auto serialize () const -> std::vector< uint8_t >
 Serialize frame to bytes.
 
auto header () const -> const frame_header &
 Get frame header.
 
auto payload () const -> std::span< const uint8_t >
 Get frame payload.
 
virtual ~frame ()=default
 Virtual destructor.
 

Static Public Member Functions

static auto parse (const frame_header &hdr, std::span< const uint8_t > payload) -> Result< std::unique_ptr< data_frame > >
 Parse DATA frame from raw bytes.
 
- Static Public Member Functions inherited from kcenon::network::protocols::http2::frame
static auto parse (std::span< const uint8_t > data) -> Result< std::unique_ptr< frame > >
 Parse frame from raw bytes.
 

Private Attributes

std::vector< uint8_t > data_
 Actual data without padding.
 

Additional Inherited Members

- Protected Attributes inherited from kcenon::network::protocols::http2::frame
frame_header header_
 Frame header.
 
std::vector< uint8_t > payload_
 Frame payload.
 

Detailed Description

DATA frame (RFC 7540 Section 6.1)

DATA frames convey arbitrary, variable-length sequences of octets associated with a stream.

Definition at line 138 of file frame.h.

Constructor & Destructor Documentation

◆ data_frame()

kcenon::network::protocols::http2::data_frame::data_frame ( uint32_t stream_id,
std::vector< uint8_t > data,
bool end_stream = false,
bool padded = false )

Construct DATA frame.

Parameters
stream_idStream identifier
dataPayload data
end_streamTrue if this is the last frame in stream
paddedTrue if frame is padded

Definition at line 190 of file frame.cpp.

192 : data_(std::move(data))
193 {
194 header_.stream_id = stream_id;
197
198 if (end_stream)
199 {
201 }
202
203 if (padded)
204 {
206 payload_.push_back(0); // Pad length = 0 for now
207 }
208
209 payload_.insert(payload_.end(), data_.begin(), data_.end());
210 header_.length = static_cast<uint32_t>(payload_.size());
211 }
std::vector< uint8_t > data_
Actual data without padding.
Definition frame.h:179
auto data() const -> std::span< const uint8_t >
Get actual data (without padding)
Definition frame.cpp:267
std::vector< uint8_t > payload_
Frame payload.
Definition frame.h:128
frame_header header_
Frame header.
Definition frame.h:127
uint32_t stream_id
Stream identifier (31 bits, MSB reserved)
Definition frame.h:58
uint32_t length
Payload length (24 bits)
Definition frame.h:55

References kcenon::network::protocols::http2::data, data_, kcenon::network::protocols::http2::frame_flags::end_stream, kcenon::network::protocols::http2::frame_header::flags, kcenon::network::protocols::http2::frame::header_, kcenon::network::protocols::http2::frame_header::length, kcenon::network::protocols::http2::frame_flags::none, kcenon::network::protocols::http2::frame_flags::padded, kcenon::network::protocols::http2::frame::payload_, kcenon::network::protocols::http2::frame_header::stream_id, and kcenon::network::protocols::http2::frame_header::type.

Member Function Documentation

◆ data()

auto kcenon::network::protocols::http2::data_frame::data ( ) const -> std::span<const uint8_t>

Get actual data (without padding)

Returns
Span of data bytes

Definition at line 267 of file frame.cpp.

268 {
269 return data_;
270 }

References data_.

◆ is_end_stream()

auto kcenon::network::protocols::http2::data_frame::is_end_stream ( ) const -> bool

Check if END_STREAM flag is set.

Returns
True if this is the last frame in stream

Definition at line 257 of file frame.cpp.

258 {
260 }

References kcenon::network::protocols::http2::frame_flags::end_stream, kcenon::network::protocols::http2::frame_header::flags, and kcenon::network::protocols::http2::frame::header_.

◆ is_padded()

auto kcenon::network::protocols::http2::data_frame::is_padded ( ) const -> bool

Check if frame is padded.

Returns
True if frame has padding

Definition at line 262 of file frame.cpp.

263 {
265 }

References kcenon::network::protocols::http2::frame_header::flags, kcenon::network::protocols::http2::frame::header_, and kcenon::network::protocols::http2::frame_flags::padded.

◆ parse()

auto kcenon::network::protocols::http2::data_frame::parse ( const frame_header & hdr,
std::span< const uint8_t > payload ) -> Result<std::unique_ptr<data_frame>>
static

Parse DATA frame from raw bytes.

Parameters
hdrFrame header (must be DATA type)
payloadFrame payload
Returns
Result containing parsed DATA frame or error

Definition at line 213 of file frame.cpp.

215 {
216 if (hdr.stream_id == 0)
217 {
218 return error_info(4, "DATA frame must have non-zero stream ID", "http2");
219 }
220
221 std::vector<uint8_t> data;
222 size_t data_offset = 0;
223
224 if (hdr.flags & frame_flags::padded)
225 {
226 if (payload.empty())
227 {
228 return error_info(5, "Padded DATA frame must have pad length", "http2");
229 }
230
231 uint8_t pad_length = payload[0];
232 data_offset = 1;
233
234 if (payload.size() < data_offset + pad_length)
235 {
236 return error_info(6, "Invalid padding in DATA frame", "http2");
237 }
238
239 data.assign(payload.begin() + data_offset,
240 payload.end() - pad_length);
241 }
242 else
243 {
244 data.assign(payload.begin(), payload.end());
245 }
246
247 auto frame = std::make_unique<data_frame>(
248 hdr.stream_id,
249 std::move(data),
250 hdr.flags & frame_flags::end_stream,
251 hdr.flags & frame_flags::padded
252 );
253
254 return frame;
255 }
frame()=default
Default constructor.
auto payload() const -> std::span< const uint8_t >
Get frame payload.
Definition frame.cpp:185
simple_error error_info

References kcenon::network::protocols::http2::data, kcenon::network::protocols::http2::frame_flags::end_stream, and kcenon::network::protocols::http2::frame_flags::padded.

Referenced by kcenon::network::protocols::http2::frame::parse().

Here is the caller graph for this function:

Member Data Documentation

◆ data_

std::vector<uint8_t> kcenon::network::protocols::http2::data_frame::data_
private

Actual data without padding.

Definition at line 179 of file frame.h.

Referenced by data(), and data_frame().


The documentation for this class was generated from the following files: