Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
hpack.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 <cstdint>
9#include <string>
10#include <string_view>
11#include <vector>
12#include <deque>
13#include <optional>
14#include <span>
15#include <map>
16
18{
24 {
25 std::string name;
26 std::string value;
27
28 http_header() = default;
29 http_header(std::string n, std::string v)
30 : name(std::move(n)), value(std::move(v)) {}
31
32 auto size() const -> size_t
33 {
34 return name.size() + value.size() + 32; // RFC 7541: 32 byte overhead
35 }
36
37 bool operator==(const http_header& other) const
38 {
39 return name == other.name && value == other.value;
40 }
41 };
42
50 {
51 public:
57 static auto get(size_t index) -> std::optional<http_header>;
58
65 static auto find(std::string_view name, std::string_view value = "")
66 -> size_t;
67
72 static constexpr auto size() -> size_t { return 61; }
73 };
74
84 {
85 public:
90 explicit dynamic_table(size_t max_size = 4096);
91
97 auto insert(std::string_view name, std::string_view value) -> void;
98
104 auto get(size_t index) const -> std::optional<http_header>;
105
112 auto find(std::string_view name, std::string_view value = "") const
113 -> std::optional<size_t>;
114
119 auto set_max_size(size_t size) -> void;
120
125 auto current_size() const -> size_t;
126
131 auto max_size() const -> size_t;
132
137 auto entry_count() const -> size_t;
138
142 auto clear() -> void;
143
144 private:
145 auto evict_to_size(size_t target_size) -> void;
146
147 std::deque<http_header> entries_;
148 size_t current_size_ = 0;
149 size_t max_size_;
150 };
151
159 {
160 public:
165 explicit hpack_encoder(size_t max_table_size = 4096);
166
172 auto encode(const std::vector<http_header>& headers) -> std::vector<uint8_t>;
173
178 auto set_max_table_size(size_t size) -> void;
179
184 auto table_size() const -> size_t;
185
186 private:
187 auto encode_integer(uint64_t value, uint8_t prefix_bits) -> std::vector<uint8_t>;
188 auto encode_string(std::string_view str, bool huffman = false) -> std::vector<uint8_t>;
189 auto encode_indexed(size_t index) -> std::vector<uint8_t>;
190 auto encode_literal_with_indexing(std::string_view name, std::string_view value)
191 -> std::vector<uint8_t>;
192 auto encode_literal_with_indexing(size_t name_index, std::string_view value)
193 -> std::vector<uint8_t>;
194 auto encode_literal_without_indexing(std::string_view name, std::string_view value)
195 -> std::vector<uint8_t>;
196 auto encode_literal_without_indexing(size_t name_index, std::string_view value)
197 -> std::vector<uint8_t>;
198
200 };
201
209 {
210 public:
215 explicit hpack_decoder(size_t max_table_size = 4096);
216
222 auto decode(std::span<const uint8_t> data) -> Result<std::vector<http_header>>;
223
228 auto set_max_table_size(size_t size) -> void;
229
234 auto table_size() const -> size_t;
235
236 private:
237 auto decode_integer(std::span<const uint8_t>& data, uint8_t prefix_bits)
239 auto decode_string(std::span<const uint8_t>& data) -> Result<std::string>;
240 auto get_indexed_header(size_t index) const -> Result<http_header>;
241
243 };
244
251 namespace huffman
252 {
258 auto encode(std::string_view input) -> std::vector<uint8_t>;
259
265 auto decode(std::span<const uint8_t> data) -> Result<std::string>;
266
272 auto encoded_size(std::string_view input) -> size_t;
273 }
274
275} // namespace kcenon::network::protocols::http2
HPACK dynamic table for header compression.
Definition hpack.h:84
auto entry_count() const -> size_t
Get number of entries.
Definition hpack.cpp:170
auto max_size() const -> size_t
Get maximum table size.
Definition hpack.cpp:165
auto clear() -> void
Clear all entries.
Definition hpack.cpp:175
auto set_max_size(size_t size) -> void
Set maximum table size.
Definition hpack.cpp:154
auto evict_to_size(size_t target_size) -> void
Definition hpack.cpp:181
auto get(size_t index) const -> std::optional< http_header >
Get header by dynamic table index.
Definition hpack.cpp:128
auto find(std::string_view name, std::string_view value="") const -> std::optional< size_t >
Find header in dynamic table.
Definition hpack.cpp:137
auto insert(std::string_view name, std::string_view value) -> void
Insert header at beginning of table.
Definition hpack.cpp:115
auto current_size() const -> size_t
Get current table size.
Definition hpack.cpp:160
dynamic_table(size_t max_size=4096)
Construct dynamic table with max size.
Definition hpack.cpp:110
HPACK header decoder (RFC 7541)
Definition hpack.h:209
auto decode(std::span< const uint8_t > data) -> Result< std::vector< http_header > >
Decode HPACK binary to headers.
Definition hpack.cpp:407
auto decode_string(std::span< const uint8_t > &data) -> Result< std::string >
Definition hpack.cpp:583
auto set_max_table_size(size_t size) -> void
Set maximum dynamic table size.
Definition hpack.cpp:526
auto table_size() const -> size_t
Get current dynamic table size.
Definition hpack.cpp:531
auto decode_integer(std::span< const uint8_t > &data, uint8_t prefix_bits) -> Result< uint64_t >
Definition hpack.cpp:536
auto get_indexed_header(size_t index) const -> Result< http_header >
Definition hpack.cpp:621
hpack_decoder(size_t max_table_size=4096)
Construct decoder with max table size.
Definition hpack.cpp:402
HPACK header encoder (RFC 7541)
Definition hpack.h:159
auto encode_literal_with_indexing(std::string_view name, std::string_view value) -> std::vector< uint8_t >
Definition hpack.cpp:325
auto set_max_table_size(size_t size) -> void
Set maximum dynamic table size.
Definition hpack.cpp:257
auto encode(const std::vector< http_header > &headers) -> std::vector< uint8_t >
Encode headers to HPACK binary format.
Definition hpack.cpp:196
hpack_encoder(size_t max_table_size=4096)
Construct encoder with max table size.
Definition hpack.cpp:191
auto table_size() const -> size_t
Get current dynamic table size.
Definition hpack.cpp:262
auto encode_indexed(size_t index) -> std::vector< uint8_t >
Definition hpack.cpp:318
auto encode_literal_without_indexing(std::string_view name, std::string_view value) -> std::vector< uint8_t >
Definition hpack.cpp:363
auto encode_integer(uint64_t value, uint8_t prefix_bits) -> std::vector< uint8_t >
Definition hpack.cpp:267
auto encode_string(std::string_view str, bool huffman=false) -> std::vector< uint8_t >
Definition hpack.cpp:293
HPACK static table (RFC 7541 Appendix A)
Definition hpack.h:50
static constexpr auto size() -> size_t
Get static table size.
Definition hpack.h:72
static auto get(size_t index) -> std::optional< http_header >
Get static table entry by index.
Definition hpack.cpp:83
static auto find(std::string_view name, std::string_view value="") -> size_t
Find index of header in static table.
Definition hpack.cpp:92
Huffman coding for HPACK string compression.
auto decode(std::span< const uint8_t > data) -> Result< std::string >
Decode Huffman encoded string.
Definition hpack.cpp:660
auto encode(std::string_view input) -> std::vector< uint8_t >
Encode string using Huffman coding.
Definition hpack.cpp:654
auto encoded_size(std::string_view input) -> size_t
Get encoded size for string.
Definition hpack.cpp:666
Network-specific error and result type definitions.
HTTP header name-value pair.
Definition hpack.h:24
http_header(std::string n, std::string v)
Definition hpack.h:29
bool operator==(const http_header &other) const
Definition hpack.h:37