![]() |
Nilorea Library
C utilities for networking, threading, graphics
|
HTTP/2 (RFC 7540) wire framing plus HPACK header compression (RFC 7541) More...
#include <stddef.h>#include <stdint.h>
Include dependency graph for n_http2.h:
This graph shows which files directly or indirectly include this file:Go to the source code of this file.
Data Structures | |
| struct | N_H2_FRAME |
| a parsed HTTP/2 frame; payload aliases the input buffer More... | |
| struct | N_H2_HEADER |
| one HPACK header field; after a decode both strings are owned (heap) More... | |
| struct | N_H2_SETTING |
| one SETTINGS parameter (identifier and value) More... | |
Macros | |
| #define | N_H2_FRAME_HEADER_LEN 9 |
| length of the fixed frame header in bytes | |
| #define | N_H2_HPACK_DEFAULT_TABLE_SIZE 4096 |
| the default HPACK dynamic table size (SETTINGS_HEADER_TABLE_SIZE default) | |
| #define | N_H2_MAX_FRAME_PAYLOAD 16384 |
| largest frame payload the parser accepts (the default SETTINGS_MAX_FRAME_SIZE) | |
| #define | N_H2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" |
| the client connection preface sent before any HTTP/2 frame (RFC 7540 3.5) | |
HTTP/2 frame types (RFC 7540 6) | |
| #define | N_H2_CONTINUATION 0x9 |
| CONTINUATION. | |
| #define | N_H2_DATA 0x0 |
| DATA. | |
| #define | N_H2_GOAWAY 0x7 |
| GOAWAY. | |
| #define | N_H2_HEADERS 0x1 |
| HEADERS. | |
| #define | N_H2_PING 0x6 |
| PING. | |
| #define | N_H2_PRIORITY 0x2 |
| PRIORITY. | |
| #define | N_H2_PUSH_PROMISE 0x5 |
| PUSH_PROMISE. | |
| #define | N_H2_RST_STREAM 0x3 |
| RST_STREAM. | |
| #define | N_H2_SETTINGS 0x4 |
| SETTINGS. | |
| #define | N_H2_WINDOW_UPDATE 0x8 |
| WINDOW_UPDATE. | |
HTTP/2 frame flags (RFC 7540 6) | |
| #define | N_H2_FLAG_ACK 0x1 |
| SETTINGS/PING: acknowledgement. | |
| #define | N_H2_FLAG_END_HEADERS 0x4 |
| HEADERS/PUSH_PROMISE/CONTINUATION: header block complete. | |
| #define | N_H2_FLAG_END_STREAM 0x1 |
| DATA/HEADERS: last frame of the stream. | |
| #define | N_H2_FLAG_PADDED 0x8 |
| DATA/HEADERS/PUSH_PROMISE: a pad-length prefix is present. | |
| #define | N_H2_FLAG_PRIORITY 0x20 |
| HEADERS: priority fields are present. | |
SETTINGS parameter identifiers (RFC 7540 6.5.2) | |
| #define | N_H2_SETTINGS_ENABLE_PUSH 0x2 |
| #define | N_H2_SETTINGS_HEADER_TABLE_SIZE 0x1 |
| #define | N_H2_SETTINGS_INITIAL_WINDOW_SIZE 0x4 |
| #define | N_H2_SETTINGS_MAX_CONCURRENT_STREAMS 0x3 |
| #define | N_H2_SETTINGS_MAX_FRAME_SIZE 0x5 |
| #define | N_H2_SETTINGS_MAX_HEADER_LIST_SIZE 0x6 |
Functions | |
| 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 **ctx) |
| 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. | |
HTTP/2 (RFC 7540) wire framing plus HPACK header compression (RFC 7541)
The framing layer of HTTP/2: the client connection preface, the frame-type and flag constants, a 9-byte frame-header parse/build, and a SETTINGS payload reader. The frame codec is stateless and does no allocation, so a caller drives its own reassembly buffer.
On top of the framing sits HPACK (RFC 7541): an N_H2_HPACK codec context with a per-direction dynamic table, a full decoder (indexed and literal fields, the static and dynamic tables, Huffman-coded and literal strings, and dynamic table size updates), and a self-contained encoder (literal fields without indexing, no Huffman) that needs no shared state. The stream/connection state machine builds on these two layers.
Definition in file n_http2.h.
| struct N_H2_FRAME |
a parsed HTTP/2 frame; payload aliases the input buffer
Collaboration diagram for N_H2_FRAME:| struct N_H2_HEADER |
one HPACK header field; after a decode both strings are owned (heap)
Collaboration diagram for N_H2_HEADER:| Data Fields | ||
|---|---|---|
| char * | name | header field name (owned after decode) |
| char * | value | header field value (owned after decode) |
| struct N_H2_SETTING |
one SETTINGS parameter (identifier and value)
Collaboration diagram for N_H2_SETTING:| Data Fields | ||
|---|---|---|
| uint16_t | id | parameter identifier (N_H2_SETTINGS_*) |
| uint32_t | value | parameter value |
| #define N_H2_DATA 0x0 |
| #define N_H2_FLAG_END_HEADERS 0x4 |
HEADERS/PUSH_PROMISE/CONTINUATION: header block complete.
| #define N_H2_FLAG_END_STREAM 0x1 |
DATA/HEADERS: last frame of the stream.
| #define N_H2_FLAG_PADDED 0x8 |
| #define N_H2_FLAG_PRIORITY 0x20 |
| #define N_H2_FRAME_HEADER_LEN 9 |
length of the fixed frame header in bytes
| #define N_H2_HEADERS 0x1 |
| #define N_H2_HPACK_DEFAULT_TABLE_SIZE 4096 |
| #define N_H2_MAX_FRAME_PAYLOAD 16384 |
| #define N_H2_PING 0x6 |
| #define N_H2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" |
the client connection preface sent before any HTTP/2 frame (RFC 7540 3.5)
| #define N_H2_SETTINGS_INITIAL_WINDOW_SIZE 0x4 |
| #define N_H2_SETTINGS_MAX_CONCURRENT_STREAMS 0x3 |
| #define N_H2_WINDOW_UPDATE 0x8 |
| 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 ** | ctx | ) |
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: