Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
frame.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
8#include <array>
9#include <cstdint>
10#include <span>
11#include <vector>
12#include <memory>
13#include <string>
14
16{
21 enum class frame_type : uint8_t
22 {
23 data = 0x0,
24 headers = 0x1,
25 priority = 0x2,
26 rst_stream = 0x3,
27 settings = 0x4,
28 push_promise = 0x5,
29 ping = 0x6,
30 goaway = 0x7,
31 window_update = 0x8,
32 continuation = 0x9
33 };
34
39 namespace frame_flags
40 {
41 constexpr uint8_t none = 0x0;
42 constexpr uint8_t end_stream = 0x1; // DATA, HEADERS
43 constexpr uint8_t ack = 0x1; // SETTINGS, PING
44 constexpr uint8_t end_headers = 0x4; // HEADERS, PUSH_PROMISE, CONTINUATION
45 constexpr uint8_t padded = 0x8; // DATA, HEADERS, PUSH_PROMISE
46 constexpr uint8_t priority = 0x20; // HEADERS
47 }
48
54 {
55 uint32_t length;
57 uint8_t flags;
58 uint32_t stream_id;
59
65 static auto parse(std::span<const uint8_t> data) -> Result<frame_header>;
66
71 auto serialize() const -> std::vector<uint8_t>;
72 };
73
81 class frame
82 {
83 public:
87 frame() = default;
88
94 frame(const frame_header& hdr, std::vector<uint8_t> payload);
95
101 static auto parse(std::span<const uint8_t> data) -> Result<std::unique_ptr<frame>>;
102
107 auto serialize() const -> std::vector<uint8_t>;
108
113 auto header() const -> const frame_header&;
114
119 auto payload() const -> std::span<const uint8_t>;
120
124 virtual ~frame() = default;
125
126 protected:
127 frame_header header_;
128 std::vector<uint8_t> payload_;
129 };
130
138 class data_frame : public frame
139 {
140 public:
148 data_frame(uint32_t stream_id, std::vector<uint8_t> data,
149 bool end_stream = false, bool padded = false);
150
157 static auto parse(const frame_header& hdr, std::span<const uint8_t> payload)
159
164 auto is_end_stream() const -> bool;
165
170 auto is_padded() const -> bool;
171
176 auto data() const -> std::span<const uint8_t>;
177
178 private:
179 std::vector<uint8_t> data_;
180 };
181
188 class headers_frame : public frame
189 {
190 public:
199 headers_frame(uint32_t stream_id, std::vector<uint8_t> header_block,
200 bool end_stream = false, bool end_headers = true);
201
208 static auto parse(const frame_header& hdr, std::span<const uint8_t> payload)
210
215 auto is_end_stream() const -> bool;
216
221 auto is_end_headers() const -> bool;
222
227 auto header_block() const -> std::span<const uint8_t>;
228
229 private:
230 std::vector<uint8_t> header_block_;
231 };
232
238 {
239 uint16_t identifier;
240 uint32_t value;
241 };
242
247 enum class setting_identifier : uint16_t
248 {
249 header_table_size = 0x1,
250 enable_push = 0x2,
252 initial_window_size = 0x4,
253 max_frame_size = 0x5,
255 };
256
264 class settings_frame : public frame
265 {
266 public:
272 explicit settings_frame(std::vector<setting_parameter> settings = {},
273 bool ack = false);
274
281 static auto parse(const frame_header& hdr, std::span<const uint8_t> payload)
283
288 auto settings() const -> const std::vector<setting_parameter>&;
289
294 auto is_ack() const -> bool;
295
296 private:
297 std::vector<setting_parameter> settings_;
298 };
299
306 class rst_stream_frame : public frame
307 {
308 public:
314 rst_stream_frame(uint32_t stream_id, uint32_t error_code);
315
322 static auto parse(const frame_header& hdr, std::span<const uint8_t> payload)
324
329 auto error_code() const -> uint32_t;
330
331 private:
332 uint32_t error_code_;
333 };
334
343 class ping_frame : public frame
344 {
345 public:
351 explicit ping_frame(std::array<uint8_t, 8> opaque_data = {},
352 bool ack = false);
353
360 static auto parse(const frame_header& hdr, std::span<const uint8_t> payload)
362
367 auto opaque_data() const -> const std::array<uint8_t, 8>&;
368
373 auto is_ack() const -> bool;
374
375 private:
376 std::array<uint8_t, 8> opaque_data_;
377 };
378
386 class goaway_frame : public frame
387 {
388 public:
395 goaway_frame(uint32_t last_stream_id, uint32_t error_code,
396 std::vector<uint8_t> additional_data = {});
397
404 static auto parse(const frame_header& hdr, std::span<const uint8_t> payload)
406
411 auto last_stream_id() const -> uint32_t;
412
417 auto error_code() const -> uint32_t;
418
423 auto additional_data() const -> std::span<const uint8_t>;
424
425 private:
427 uint32_t error_code_;
428 std::vector<uint8_t> additional_data_;
429 };
430
438 {
439 public:
445 window_update_frame(uint32_t stream_id, uint32_t window_size_increment);
446
453 static auto parse(const frame_header& hdr, std::span<const uint8_t> payload)
455
460 auto window_size_increment() const -> uint32_t;
461
462 private:
464 };
465
470 enum class error_code : uint32_t
471 {
472 no_error = 0x0,
473 protocol_error = 0x1,
474 internal_error = 0x2,
475 flow_control_error = 0x3,
476 settings_timeout = 0x4,
477 stream_closed = 0x5,
478 frame_size_error = 0x6,
479 refused_stream = 0x7,
480 cancel = 0x8,
481 compression_error = 0x9,
482 connect_error = 0xa,
483 enhance_your_calm = 0xb,
484 inadequate_security = 0xc,
485 http_1_1_required = 0xd
486 };
487
488} // namespace kcenon::network::protocols::http2
DATA frame (RFC 7540 Section 6.1)
Definition frame.h:139
std::vector< uint8_t > data_
Actual data without padding.
Definition frame.h:179
Base class for HTTP/2 frames.
Definition frame.h:82
frame()=default
Default constructor.
GOAWAY frame (RFC 7540 Section 6.8)
Definition frame.h:387
std::vector< uint8_t > additional_data_
Debug data.
Definition frame.h:428
uint32_t last_stream_id_
Last stream ID.
Definition frame.h:426
HEADERS frame (RFC 7540 Section 6.2)
Definition frame.h:189
std::vector< uint8_t > header_block_
Header block fragment.
Definition frame.h:230
PING frame (RFC 7540 Section 6.7)
Definition frame.h:344
std::array< uint8_t, 8 > opaque_data_
8-byte opaque data
Definition frame.h:376
RST_STREAM frame (RFC 7540 Section 6.4)
Definition frame.h:307
SETTINGS frame (RFC 7540 Section 6.5)
Definition frame.h:265
std::vector< setting_parameter > settings_
Settings parameters.
Definition frame.h:297
WINDOW_UPDATE frame (RFC 7540 Section 6.9)
Definition frame.h:438
uint32_t window_size_increment_
Window size increment.
Definition frame.h:463
error_code
HTTP/2 error codes (RFC 7540 Section 7)
Definition frame.h:471
@ compression_error
Compression state not updated.
@ settings_timeout
Settings not acknowledged.
@ connect_error
TCP connection error for CONNECT.
@ flow_control_error
Flow-control limits exceeded.
@ inadequate_security
Negotiated TLS parameters not acceptable.
@ stream_closed
Frame received for closed stream.
@ enhance_your_calm
Processing capacity exceeded.
@ http_1_1_required
Use HTTP/1.1 for the request.
setting_identifier
SETTINGS frame parameter identifiers (RFC 7540 Section 6.5.2)
Definition frame.h:248
@ max_header_list_size
SETTINGS_MAX_HEADER_LIST_SIZE.
@ max_concurrent_streams
SETTINGS_MAX_CONCURRENT_STREAMS.
@ initial_window_size
SETTINGS_INITIAL_WINDOW_SIZE.
frame_type
HTTP/2 frame types (RFC 7540 Section 6)
Definition frame.h:22
Network-specific error and result type definitions.
HTTP/2 frame header (9 bytes)
Definition frame.h:54
uint32_t stream_id
Stream identifier (31 bits, MSB reserved)
Definition frame.h:58
static auto parse(std::span< const uint8_t > data) -> Result< frame_header >
Parse frame header from raw bytes.
Definition frame.cpp:52
auto serialize() const -> std::vector< uint8_t >
Serialize frame header to bytes.
Definition frame.cpp:73
uint32_t length
Payload length (24 bits)
Definition frame.h:55