nhttp_hpack (nhttp_lib v1.0.0)

View Source

HPACK header compression for HTTP/2 (RFC 7541).

This module implements the HPACK header compression format used by HTTP/2. It provides stateful encoding and decoding of header fields using static and dynamic tables.

Usage

{ok, EncState0} = nhttp_hpack:new(),
{ok, DecState0} = nhttp_hpack:new(),

Headers = [{<<":method">>, <<"GET">>}, {<<":path">>, <<"/">>}],
{ok, HeaderBlock, EncState1} = nhttp_hpack:encode(Headers, EncState0),

{ok, DecodedHeaders, DecState1} = nhttp_hpack:decode(HeaderBlock, DecState0).

Summary

Functions

Decode a header block.

Decode a header block, aborting with {error, header_list_too_large} once the cumulative decoded list size exceeds max_list_size. The check matches the RFC 9113 §10.5.1 octet count (name + value + 32 per entry).

Encode headers without Huffman encoding.

Encode headers with options.

Check if the dynamic table is empty.

Create a new HPACK state with default max size (4096 bytes).

Create a new HPACK state with specified max size.

Update the maximum table size (from SETTINGS_HEADER_TABLE_SIZE). Immediately evicts entries if the new size is smaller than current table size.

Get the current dynamic table size in bytes.

Types

decode_error()

-type decode_error() ::
          dynamic_table_size_exceeded | invalid_table_index | integer_overflow | invalid_huffman |
          incomplete_header_block | header_list_too_large | uppercase_header_name.

decode_opts()

-type decode_opts() :: #{max_list_size => pos_integer() | infinity}.

encode_opts()

-type encode_opts() :: #{huffman => boolean()}.

headers()

-type headers() :: [{Name :: binary(), Value :: binary()}].

state()

-opaque state()

Functions

decode(Data, State)

-spec decode(Data :: binary(), State :: state()) ->
                {ok, Headers :: headers(), NewState :: state()} | {error, decode_error()}.

Decode a header block.

decode(Data, State, Opts)

-spec decode(Data :: binary(), State :: state(), Opts :: decode_opts()) ->
                {ok, Headers :: headers(), NewState :: state()} | {error, decode_error()}.

Decode a header block, aborting with {error, header_list_too_large} once the cumulative decoded list size exceeds max_list_size. The check matches the RFC 9113 §10.5.1 octet count (name + value + 32 per entry).

encode(Headers, State)

-spec encode(Headers :: headers(), State :: state()) -> {ok, iodata(), state()}.

Encode headers without Huffman encoding.

encode(Headers, State, Opts)

-spec encode(Headers :: headers(), State :: state(), Opts :: encode_opts()) -> {ok, iodata(), state()}.

Encode headers with options.

is_empty(State)

-spec is_empty(State :: state()) -> boolean().

Check if the dynamic table is empty.

new()

-spec new() -> {ok, state()}.

Create a new HPACK state with default max size (4096 bytes).

new(MaxSize)

-spec new(MaxSize :: non_neg_integer()) -> {ok, state()}.

Create a new HPACK state with specified max size.

set_max_table_size(MaxSize, State)

-spec set_max_table_size(MaxSize :: non_neg_integer(), State :: state()) -> {ok, state()}.

Update the maximum table size (from SETTINGS_HEADER_TABLE_SIZE). Immediately evicts entries if the new size is smaller than current table size.

table_size(State)

-spec table_size(State :: state()) -> non_neg_integer().

Get the current dynamic table size in bytes.