Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http2.h File Reference

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.

Detailed Description

HTTP/2 (RFC 7540) wire framing plus HPACK header compression (RFC 7541).

Author
Castagnier Mickael
Version
1.0
Date
2026

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.


Data Structure Documentation

◆ N_H2_FRAME

struct N_H2_FRAME

a parsed HTTP/2 frame; payload aliases the input buffer

Examples
ex_http2.c, ex_http2_server.c, and ex_network_alpn.c.

Definition at line 91 of file n_http2.h.

Collaboration diagram for N_H2_FRAME:
Data Fields
int flags frame flags
uint32_t length payload length (24-bit)
const unsigned char * payload pointer into the input buffer (may be NULL when length is 0)
uint32_t stream_id stream identifier (31-bit; the reserved bit is cleared)
int type frame type (N_H2_*)

◆ N_H2_HEADER

struct N_H2_HEADER

one HPACK header field; after a decode both strings are owned (heap)

Examples
ex_http2.c, ex_http2_server.c, and ex_network_alpn.c.

Definition at line 123 of file n_http2.h.

Collaboration diagram for N_H2_HEADER:
Data Fields
char * name header field name (owned after decode)
char * value header field value (owned after decode)

◆ N_H2_SETTING

struct N_H2_SETTING

one SETTINGS parameter (identifier and value)

Examples
ex_http2.c, and ex_http2_server.c.

Definition at line 100 of file n_http2.h.

Collaboration diagram for N_H2_SETTING:
Data Fields
uint16_t id parameter identifier (N_H2_SETTINGS_*)
uint32_t value parameter value

Macro Definition Documentation

◆ N_H2_CONTINUATION

#define N_H2_CONTINUATION   0x9

CONTINUATION.

Examples
ex_http2_server.c.

Definition at line 68 of file n_http2.h.

Referenced by n_http2_server_respond(), srv_handle_frame(), and test_continuation().

◆ N_H2_DATA

◆ N_H2_FLAG_ACK

#define N_H2_FLAG_ACK   0x1

SETTINGS/PING: acknowledgement.

Examples
ex_http2_server.c.

Definition at line 74 of file n_http2.h.

Referenced by n_http2_server_handshake(), srv_apply_settings(), srv_handle_frame(), test_handshake(), and test_ping_goaway().

◆ N_H2_FLAG_END_HEADERS

#define N_H2_FLAG_END_HEADERS   0x4

HEADERS/PUSH_PROMISE/CONTINUATION: header block complete.

Examples
ex_http2.c, ex_http2_server.c, and ex_network_alpn.c.

Definition at line 75 of file n_http2.h.

Referenced by h2_client_get(), n_http2_server_respond(), peer_headers(), srv_apply_headers(), test_continuation(), test_get(), and test_roundtrip().

◆ N_H2_FLAG_END_STREAM

#define N_H2_FLAG_END_STREAM   0x1

◆ N_H2_FLAG_PADDED

#define N_H2_FLAG_PADDED   0x8

DATA/HEADERS/PUSH_PROMISE: a pad-length prefix is present.

Definition at line 76 of file n_http2.h.

Referenced by srv_apply_data(), and srv_headers_payload().

◆ N_H2_FLAG_PRIORITY

#define N_H2_FLAG_PRIORITY   0x20

HEADERS: priority fields are present.

Definition at line 77 of file n_http2.h.

Referenced by srv_headers_payload().

◆ N_H2_FRAME_HEADER_LEN

#define N_H2_FRAME_HEADER_LEN   9

length of the fixed frame header in bytes

Examples
ex_http2.c, ex_http2_server.c, and ex_network_alpn.c.

Definition at line 53 of file n_http2.h.

Referenced by h2_client_get(), n_http2_frame_build_header(), n_http2_frame_parse(), peer_frame(), srv_send(), test_boundaries(), and test_roundtrip().

◆ N_H2_GOAWAY

#define N_H2_GOAWAY   0x7

◆ N_H2_HEADERS

◆ N_H2_HPACK_DEFAULT_TABLE_SIZE

#define N_H2_HPACK_DEFAULT_TABLE_SIZE   4096

the default HPACK dynamic table size (SETTINGS_HEADER_TABLE_SIZE default)

Definition at line 120 of file n_http2.h.

Referenced by n_http2_hpack_new().

◆ N_H2_MAX_FRAME_PAYLOAD

#define N_H2_MAX_FRAME_PAYLOAD   16384

largest frame payload the parser accepts (the default SETTINGS_MAX_FRAME_SIZE)

Definition at line 55 of file n_http2.h.

Referenced by n_http2_frame_parse(), and srv_apply_settings().

◆ N_H2_PING

#define N_H2_PING   0x6

◆ N_H2_PREFACE

#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)

Examples
ex_http2.c, ex_http2_server.c, and ex_network_alpn.c.

Definition at line 51 of file n_http2.h.

Referenced by h2_client_get(), n_http2_server_handshake(), peer_prologue(), test_handshake_errors(), and test_preface().

◆ N_H2_PRIORITY

#define N_H2_PRIORITY   0x2

PRIORITY.

Definition at line 61 of file n_http2.h.

Referenced by srv_handle_frame().

◆ N_H2_PUSH_PROMISE

#define N_H2_PUSH_PROMISE   0x5

PUSH_PROMISE.

Definition at line 64 of file n_http2.h.

Referenced by srv_handle_frame().

◆ N_H2_RST_STREAM

#define N_H2_RST_STREAM   0x3

RST_STREAM.

Examples
ex_http2_server.c.

Definition at line 62 of file n_http2.h.

Referenced by n_http2_server_rst_stream(), srv_handle_frame(), and test_rst_stream().

◆ N_H2_SETTINGS

#define N_H2_SETTINGS   0x4

◆ N_H2_SETTINGS_ENABLE_PUSH

#define N_H2_SETTINGS_ENABLE_PUSH   0x2

Definition at line 83 of file n_http2.h.

Referenced by n_http2_server_handshake().

◆ N_H2_SETTINGS_HEADER_TABLE_SIZE

#define N_H2_SETTINGS_HEADER_TABLE_SIZE   0x1

Definition at line 82 of file n_http2.h.

◆ N_H2_SETTINGS_INITIAL_WINDOW_SIZE

#define N_H2_SETTINGS_INITIAL_WINDOW_SIZE   0x4

◆ N_H2_SETTINGS_MAX_CONCURRENT_STREAMS

#define N_H2_SETTINGS_MAX_CONCURRENT_STREAMS   0x3
Examples
ex_http2.c.

Definition at line 84 of file n_http2.h.

Referenced by n_http2_server_handshake(), and test_settings().

◆ N_H2_SETTINGS_MAX_FRAME_SIZE

#define N_H2_SETTINGS_MAX_FRAME_SIZE   0x5

Definition at line 86 of file n_http2.h.

Referenced by srv_apply_settings().

◆ N_H2_SETTINGS_MAX_HEADER_LIST_SIZE

#define N_H2_SETTINGS_MAX_HEADER_LIST_SIZE   0x6

Definition at line 87 of file n_http2.h.

◆ N_H2_WINDOW_UPDATE

#define N_H2_WINDOW_UPDATE   0x8

Function Documentation

◆ n_http2_frame_build_header()

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, and type().

Referenced by h2_client_get(), peer_frame(), srv_send(), test_boundaries(), and test_roundtrip().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ n_http2_frame_parse()

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 h2_client_get(), out_count(), out_find(), srv_next_frame(), test_boundaries(), test_post_body(), and test_roundtrip().

Here is the caller graph for this function:

◆ n_http2_hpack_decode()

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.

Parameters
ctxcodec context (its dynamic table is read and updated)
blockthe header block fragment bytes
lenlength of block
outarray receiving the decoded headers
max_outcapacity of out
countreceives the number of headers decoded
Returns
0 on success, -1 on error

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 h2_client_get(), srv_finish_headers(), 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:

◆ n_http2_hpack_encode()

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.

Parameters
headersthe header fields to encode (name/value C strings)
nnumber of headers
outdestination buffer
out_capcapacity of out
Returns
bytes written, or 0 on error

Definition at line 554 of file n_http2.c.

References hpack_str_encode().

Referenced by h2_client_get(), n_http2_server_respond(), peer_headers(), test_continuation(), and test_hpack_roundtrip().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ n_http2_hpack_free()

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 h2_client_get(), n_http2_server_free(), 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:

◆ n_http2_hpack_headers_free()

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 h2_client_get(), n_http2_hpack_decode(), srv_finish_headers(), srv_stream_close(), test_hpack_rfc_huffman(), test_hpack_rfc_requests(), and test_hpack_roundtrip().

Here is the caller graph for this function:

◆ n_http2_hpack_new()

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 h2_client_get(), n_http2_server_new(), 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:

◆ n_http2_hpack_set_max_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.

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:

◆ n_http2_settings_parse()

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 srv_apply_settings(), test_handshake(), and test_settings().

Here is the caller graph for this function: