![]() |
Nilorea Library
C utilities for networking, threading, graphics
|
HTTP/2 (RFC 7540) wire framing and HPACK (RFC 7541) header compression. More...
Include dependency graph for n_http2.c:Go to the source code of this file.
Data Structures | |
| struct | H2_DYN_ENTRY |
| one HPACK dynamic table entry (owned strings and its accounted size) More... | |
| struct | N_H2_HPACK |
| HPACK codec context: the dynamic table (index 0 = most recently added) More... | |
Functions | |
| static int | hpack_dyn_add (N_H2_HPACK *ctx, const char *name, const char *value) |
| insert (name, value) at the front of the dynamic table, evicting as needed; both strings are copied. | |
| static void | hpack_evict (N_H2_HPACK *ctx) |
| drop the oldest dynamic entries until the table fits max_size | |
| static int | hpack_huff_decode (const unsigned char *s, size_t len, char **out) |
| decode a Huffman-coded string of len bytes into a fresh C string | |
| static int | hpack_huff_lookup (uint32_t code, int nbits) |
| find the Huffman symbol for an accumulated code of nbits bits, or -1 | |
| static int | hpack_int_decode (const unsigned char *buf, size_t len, size_t *pos, int prefix_bits, uint64_t *out) |
| decode an HPACK integer with an N-bit prefix; advances *pos | |
| static size_t | hpack_int_encode (unsigned char *out, size_t cap, int prefix_bits, unsigned char flags, uint64_t value) |
| encode an HPACK integer with an N-bit prefix and high-bit flags | |
| static int | hpack_lookup (const N_H2_HPACK *ctx, uint64_t idx, const char **name, const char **value) |
| resolve a table index to its name/value (const, not owned). | |
| static int | hpack_str_decode (const unsigned char *buf, size_t len, size_t *pos, char **out) |
| decode an HPACK string (Huffman or literal); allocates *out | |
| static size_t | hpack_str_encode (unsigned char *out, size_t cap, const char *s) |
| encode a literal (non-Huffman) string with its 7-bit length prefix | |
| size_t | n_http2_frame_build_header (unsigned char *out, size_t out_cap, uint32_t length, int type, int flags, uint32_t stream_id) |
| write a 9-byte HTTP/2 frame header into out. | |
| int | n_http2_frame_parse (const unsigned char *buf, size_t len, N_H2_FRAME *frame, size_t *consumed) |
| parse one HTTP/2 frame from a byte buffer (stateless). | |
| int | n_http2_hpack_decode (N_H2_HPACK *ctx, const unsigned char *block, size_t len, N_H2_HEADER *out, size_t max_out, size_t *count) |
| decode an HPACK header block into out[] (owned name/value strings). | |
| size_t | n_http2_hpack_encode (const N_H2_HEADER *headers, size_t n, unsigned char *out, size_t out_cap) |
| encode a header list into a block using literal representations without indexing and without Huffman (a valid, interoperable, self-contained encoding that needs no dynamic-table state). | |
| void | n_http2_hpack_free (N_H2_HPACK **pctx) |
| free an HPACK codec context and NULL the caller's pointer | |
| void | n_http2_hpack_headers_free (N_H2_HEADER *headers, size_t n) |
| free the owned name/value strings of a decoded header array (does not free the array itself) | |
| N_H2_HPACK * | n_http2_hpack_new (size_t max_table_size) |
| create an HPACK codec context with the given dynamic table size limit (0 uses N_H2_HPACK_DEFAULT_TABLE_SIZE). | |
| void | n_http2_hpack_set_max_size (N_H2_HPACK *ctx, size_t max_table_size) |
| change the dynamic table size limit (a SETTINGS_HEADER_TABLE_SIZE change), evicting entries as needed so the table fits. | |
| int | n_http2_settings_parse (const unsigned char *payload, size_t len, N_H2_SETTING *out, size_t max, size_t *count) |
| parse a SETTINGS frame payload (6-byte id/value entries) into out. | |
Variables | |
| static const uint32_t | hpack_huff_code [256] |
| HPACK Huffman codes (RFC 7541 Appendix B), index = byte value 0..255. | |
| static const uint8_t | hpack_huff_len [256] |
| HPACK Huffman code lengths in bits (RFC 7541 Appendix B) | |
| static const char *const | hpack_static_name [61] |
| HPACK static table names (RFC 7541 Appendix A, index 1..61) | |
| static const char *const | hpack_static_value [61] |
| HPACK static table values (empty where the entry has no value) | |
HTTP/2 (RFC 7540) wire framing and HPACK (RFC 7541) header compression.
Definition in file n_http2.c.
| struct H2_DYN_ENTRY |
| struct N_H2_HPACK |
HPACK codec context: the dynamic table (index 0 = most recently added)
opaque HPACK codec context holding one direction's dynamic table (RFC 7541)
Collaboration diagram for N_H2_HPACK:| Data Fields | ||
|---|---|---|
| size_t | cap | |
| size_t | cur_size | |
| H2_DYN_ENTRY * | ents | |
| size_t | limit | |
| size_t | max_size | |
| size_t | nents | |
|
static |
insert (name, value) at the front of the dynamic table, evicting as needed; both strings are copied.
An entry larger than max_size empties the table without being stored (RFC 7541 4.4).
Definition at line 402 of file n_http2.c.
References N_H2_HPACK::cap, N_H2_HPACK::cur_size, N_H2_HPACK::ents, hpack_evict(), N_H2_HPACK::max_size, H2_DYN_ENTRY::name, N_H2_HPACK::nents, H2_DYN_ENTRY::size, and H2_DYN_ENTRY::value.
Referenced by n_http2_hpack_decode().
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
drop the oldest dynamic entries until the table fits max_size
Definition at line 365 of file n_http2.c.
References N_H2_HPACK::cur_size, N_H2_HPACK::ents, N_H2_HPACK::max_size, H2_DYN_ENTRY::name, N_H2_HPACK::nents, H2_DYN_ENTRY::size, and H2_DYN_ENTRY::value.
Referenced by hpack_dyn_add(), n_http2_hpack_decode(), and n_http2_hpack_set_max_size().
Here is the caller graph for this function:
|
static |
decode a Huffman-coded string of len bytes into a fresh C string
Definition at line 202 of file n_http2.c.
References hpack_huff_lookup().
Referenced by hpack_str_decode().
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
find the Huffman symbol for an accumulated code of nbits bits, or -1
Definition at line 192 of file n_http2.c.
References hpack_huff_code, and hpack_huff_len.
Referenced by hpack_huff_decode().
Here is the caller graph for this function:
|
static |
decode an HPACK integer with an N-bit prefix; advances *pos
Definition at line 257 of file n_http2.c.
Referenced by hpack_str_decode(), and n_http2_hpack_decode().
Here is the caller graph for this function:
|
static |
encode an HPACK integer with an N-bit prefix and high-bit flags
Definition at line 315 of file n_http2.c.
Referenced by hpack_str_encode().
Here is the caller graph for this function:
|
static |
resolve a table index to its name/value (const, not owned).
Returns 0 on success, -1 when the index is out of range.
Definition at line 444 of file n_http2.c.
References N_H2_HPACK::ents, hpack_static_name, hpack_static_value, H2_DYN_ENTRY::name, N_H2_HPACK::nents, and H2_DYN_ENTRY::value.
Referenced by n_http2_hpack_decode().
Here is the caller graph for this function:
|
static |
decode an HPACK string (Huffman or literal); allocates *out
Definition at line 287 of file n_http2.c.
References hpack_huff_decode(), and hpack_int_decode().
Referenced by n_http2_hpack_decode().
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
encode a literal (non-Huffman) string with its 7-bit length prefix
Definition at line 341 of file n_http2.c.
References hpack_int_encode().
Referenced by n_http2_hpack_encode().
Here is the call graph for this function:
Here is the caller graph for this function:| size_t n_http2_frame_build_header | ( | unsigned char * | out, |
| size_t | out_cap, | ||
| uint32_t | length, | ||
| int | type, | ||
| int | flags, | ||
| uint32_t | stream_id | ||
| ) |
write a 9-byte HTTP/2 frame header into out.
Returns N_H2_FRAME_HEADER_LEN, or 0 when out_cap is too small or length exceeds 24 bits.
Definition at line 52 of file n_http2.c.
References N_H2_FRAME_HEADER_LEN.
Referenced by test_boundaries(), and test_roundtrip().
Here is the caller graph for this function:| int n_http2_frame_parse | ( | const unsigned char * | buf, |
| size_t | len, | ||
| N_H2_FRAME * | frame, | ||
| size_t * | consumed | ||
| ) |
parse one HTTP/2 frame from a byte buffer (stateless).
Returns 1 and fills frame + *consumed (header + payload) on a complete frame, 0 when more bytes are needed, -1 when the payload exceeds N_H2_MAX_FRAME_PAYLOAD.
Definition at line 32 of file n_http2.c.
References N_H2_FRAME::flags, N_H2_FRAME::length, N_H2_FRAME_HEADER_LEN, N_H2_MAX_FRAME_PAYLOAD, N_H2_FRAME::payload, N_H2_FRAME::stream_id, and N_H2_FRAME::type.
Referenced by test_boundaries(), and test_roundtrip().
Here is the caller graph for this function:| int n_http2_hpack_decode | ( | N_H2_HPACK * | ctx, |
| const unsigned char * | block, | ||
| size_t | len, | ||
| N_H2_HEADER * | out, | ||
| size_t | max_out, | ||
| size_t * | count | ||
| ) |
decode an HPACK header block into out[] (owned name/value strings).
Handles indexed fields (static and dynamic table), the three literal forms (with/without/never indexing), Huffman-coded and literal strings, and dynamic table size updates. On success *count holds the number of headers written (<= max_out) and the dynamic table is updated. Returns 0 on success, -1 on a malformed block or when more than max_out headers are produced. Free the decoded headers with n_http2_hpack_headers_free.
| ctx | codec context (its dynamic table is read and updated) |
| block | the header block fragment bytes |
| len | length of block |
| out | array receiving the decoded headers |
| max_out | capacity of out |
| count | receives the number of headers decoded |
Definition at line 472 of file n_http2.c.
References hpack_dyn_add(), hpack_evict(), hpack_int_decode(), hpack_lookup(), hpack_str_decode(), N_H2_HPACK::limit, N_H2_HPACK::max_size, n_http2_hpack_headers_free(), N_H2_HEADER::name, and N_H2_HEADER::value.
Referenced by test_hpack_guards(), test_hpack_overflow(), test_hpack_rfc_huffman(), test_hpack_rfc_requests(), and test_hpack_roundtrip().
Here is the call graph for this function:
Here is the caller graph for this function:| size_t n_http2_hpack_encode | ( | const N_H2_HEADER * | headers, |
| size_t | n, | ||
| unsigned char * | out, | ||
| size_t | out_cap | ||
| ) |
encode a header list into a block using literal representations without indexing and without Huffman (a valid, interoperable, self-contained encoding that needs no dynamic-table state).
Returns the number of bytes written, or 0 when out_cap is too small or an argument is invalid.
| headers | the header fields to encode (name/value C strings) |
| n | number of headers |
| out | destination buffer |
| out_cap | capacity of out |
Definition at line 554 of file n_http2.c.
References hpack_str_encode().
Referenced by test_hpack_roundtrip().
Here is the call graph for this function:
Here is the caller graph for this function:| void n_http2_hpack_free | ( | N_H2_HPACK ** | pctx | ) |
free an HPACK codec context and NULL the caller's pointer
Definition at line 375 of file n_http2.c.
References N_H2_HPACK::ents, H2_DYN_ENTRY::name, N_H2_HPACK::nents, and H2_DYN_ENTRY::value.
Referenced by test_hpack_guards(), test_hpack_overflow(), test_hpack_rfc_huffman(), test_hpack_rfc_requests(), and test_hpack_roundtrip().
Here is the caller graph for this function:| void n_http2_hpack_headers_free | ( | N_H2_HEADER * | headers, |
| size_t | n | ||
| ) |
free the owned name/value strings of a decoded header array (does not free the array itself)
Definition at line 460 of file n_http2.c.
References N_H2_HEADER::name, and N_H2_HEADER::value.
Referenced by n_http2_hpack_decode(), test_hpack_rfc_huffman(), test_hpack_rfc_requests(), and test_hpack_roundtrip().
Here is the caller graph for this function:| N_H2_HPACK * n_http2_hpack_new | ( | size_t | max_table_size | ) |
create an HPACK codec context with the given dynamic table size limit (0 uses N_H2_HPACK_DEFAULT_TABLE_SIZE).
Returns NULL on allocation failure.
Definition at line 353 of file n_http2.c.
References N_H2_HPACK::limit, N_H2_HPACK::max_size, and N_H2_HPACK_DEFAULT_TABLE_SIZE.
Referenced by test_hpack_guards(), test_hpack_overflow(), test_hpack_rfc_huffman(), test_hpack_rfc_requests(), and test_hpack_roundtrip().
Here is the caller graph for this function:| void n_http2_hpack_set_max_size | ( | N_H2_HPACK * | ctx, |
| size_t | max_table_size | ||
| ) |
change the dynamic table size limit (a SETTINGS_HEADER_TABLE_SIZE change), evicting entries as needed so the table fits.
Definition at line 390 of file n_http2.c.
References hpack_evict(), N_H2_HPACK::limit, and N_H2_HPACK::max_size.
Here is the call graph for this function:| int n_http2_settings_parse | ( | const unsigned char * | payload, |
| size_t | len, | ||
| N_H2_SETTING * | out, | ||
| size_t | max, | ||
| size_t * | count | ||
| ) |
parse a SETTINGS frame payload (6-byte id/value entries) into out.
Returns 0 on success (*count set, <= max), -1 when the payload length is not a multiple of 6.
Definition at line 70 of file n_http2.c.
References N_H2_SETTING::id, and N_H2_SETTING::value.
Referenced by test_settings().
Here is the caller graph for this function:
|
static |
HPACK Huffman codes (RFC 7541 Appendix B), index = byte value 0..255.
Definition at line 121 of file n_http2.c.
Referenced by hpack_huff_lookup().
|
static |
HPACK Huffman code lengths in bits (RFC 7541 Appendix B)
Definition at line 156 of file n_http2.c.
Referenced by hpack_huff_lookup().
|
static |
HPACK static table names (RFC 7541 Appendix A, index 1..61)
Definition at line 93 of file n_http2.c.
Referenced by hpack_lookup().
|
static |
HPACK static table values (empty where the entry has no value)
Definition at line 107 of file n_http2.c.
Referenced by hpack_lookup().