Module

whiplash/hpack

src/whiplash/hpack.pooka

Headers, folded small for the journey: HPACK, the compression HTTP/2 uses for its fields (RFC 7541).

A Decoder and an Encoder each keep a dynamic table that both ends of a connection grow in step, so they are values you pass along: decoding a block returns the fields and the decoder to use for the next block.

The package is sans-IO: it never touches a socket. HTTP/2 servers and clients feed it header blocks and get fields back.

API reference

class

Field

src/whiplash/hpack.pooka:23
class Field(name: String, value: String, never_index: Bool)

One header field. never_index marks a value that must never be stored in a compression table, such as a password or a cookie with a secret (RFC 7541, section 7.1).

field

name

src/whiplash/hpack.pooka:23
name: String
INFERREDString
field

value

src/whiplash/hpack.pooka:23
value: String
INFERREDString
field

never_index

src/whiplash/hpack.pooka:23
never_index: Bool
INFERREDBool
constructor

new

src/whiplash/hpack.pooka:23
INFERREDfn(String, String, Bool) -> whiplash/hpack.Field
class

HpackError

src/whiplash/hpack.pooka:27
class HpackError(message: String)

Why a header block could not be decoded. Every one of them is a connection error of type COMPRESSION_ERROR in HTTP/2.

field

message

src/whiplash/hpack.pooka:27
message: String
INFERREDString
constructor

new

src/whiplash/hpack.pooka:27
INFERREDfn(String) -> whiplash/hpack.HpackError
class

Decoder

src/whiplash/hpack.pooka:31
class Decoder(table: Table, max_table_size: Int, tainted: Bool)

The decoding side of one connection: its dynamic table, and the largest size the peer may ask that table to be.

field

table

src/whiplash/hpack.pooka:31
table: Table
field

max_table_size

src/whiplash/hpack.pooka:31
max_table_size: Int
INFERREDInt
field

tainted

src/whiplash/hpack.pooka:31
tainted: Bool
INFERREDBool
constructor

new

src/whiplash/hpack.pooka:31
class

Encoder

src/whiplash/hpack.pooka:35
class Encoder(table: Table, max_table_size: Int, huffman: Bool, pending: Int)

The encoding side of one connection. pending is a table size the next block must announce first, or -1.

field

table

src/whiplash/hpack.pooka:35
table: Table
field

max_table_size

src/whiplash/hpack.pooka:35
max_table_size: Int
INFERREDInt
field

huffman

src/whiplash/hpack.pooka:35
huffman: Bool
INFERREDBool
field

pending

src/whiplash/hpack.pooka:35
pending: Int
INFERREDInt
constructor

new

src/whiplash/hpack.pooka:35
INFERREDfn(whiplash/hpack.Table, Int, Bool, Int) -> whiplash/hpack.Encoder
class

Entry

src/whiplash/hpack.pooka:41
class Entry(name: String, value: String, name_size: Int, value_size: Int)

The dynamic table: newest entry first, with its size in octets as RFC 7541 section 4.1 counts it (name, value and 32 per entry). Retain original octet counts: a non-UTF-8 field mapped to text can have a different UTF-8 size. Eviction must use the wire bytes (RFC 7541 4.1).

field

name

src/whiplash/hpack.pooka:41
name: String
INFERREDString
field

value

src/whiplash/hpack.pooka:41
value: String
INFERREDString
field

name_size

src/whiplash/hpack.pooka:41
name_size: Int
INFERREDInt
field

value_size

src/whiplash/hpack.pooka:41
value_size: Int
INFERREDInt
constructor

new

src/whiplash/hpack.pooka:41
INFERREDfn(String, String, Int, Int) -> whiplash/hpack.Entry
class

Table

src/whiplash/hpack.pooka:43
class Table(entries: List(Entry), size: Int, capacity: Int)
field

entries

src/whiplash/hpack.pooka:43
entries: List(Entry)
INFERREDList(whiplash/hpack.Entry)
field

size

src/whiplash/hpack.pooka:43
size: Int
INFERREDInt
field

capacity

src/whiplash/hpack.pooka:43
capacity: Int
INFERREDInt
constructor

new

src/whiplash/hpack.pooka:43
INFERREDfn(List(whiplash/hpack.Entry), Int, Int) -> whiplash/hpack.Table
function

decoder

src/whiplash/hpack.pooka:47
decoder(max_table_size: Int) -> Decoder
INFERREDfn(Int) -> whiplash/hpack.Decoder

A decoder whose dynamic table may grow to max_table_size octets: 4096 unless SETTINGS_HEADER_TABLE_SIZE says otherwise.

function

encoder

src/whiplash/hpack.pooka:53
encoder(max_table_size: Int) -> Encoder
INFERREDfn(Int) -> whiplash/hpack.Encoder

An encoder that may use max_table_size octets of dynamic table, and Huffman-codes strings when that makes them shorter.

function

without_huffman

src/whiplash/hpack.pooka:58
without_huffman(encoder: Encoder) -> Encoder

The same encoder, writing every string literally.

function

decode

src/whiplash/hpack.pooka:63
decode(decoder: Decoder, block: BitArray) -> Result(
  #(List(Field), Decoder),
  HpackError
)

Decode one complete header block.

function

decode_checked

src/whiplash/hpack.pooka:79
decode_checked(decoder: Decoder, block: BitArray) -> Result(
  #(List(Field), Decoder, Bool),
  HpackError
)

Decode a block, and say whether every field is a valid HTTP/2 field (RFC 9113 section 8.2.1): a lowercase token name, optionally after a colon, and a value without controls or edge whitespace. Literals are checked as they are decoded; static entries are valid by construction, and dynamic entries were checked when they were inserted. Once an invalid field has entered the table, every later block reports False, so the caller checks it in full. An invalid field is a malformed message, not a decoding error: the tables stay in step either way.

function

encode

src/whiplash/hpack.pooka:95
encode(encoder: Encoder, fields: List(Field)) -> #(BitArray, Encoder)

Encode fields as one header block.

function

with_max_table_size

src/whiplash/hpack.pooka:111
with_max_table_size(decoder: Decoder, size: Int) -> Decoder

Change the size the decoder accepts, after the local SETTINGS frame that announced it has been acknowledged. A smaller size evicts at once.

function

with_peer_table_size

src/whiplash/hpack.pooka:123
with_peer_table_size(encoder: Encoder, size: Int) -> Encoder

The peer's SETTINGS_HEADER_TABLE_SIZE changed. The encoder shrinks its table to fit, and tells the decoder so at the start of the next block.

function

static_entry

src/whiplash/hpack.pooka:130
static_entry(index: Int) -> Option(#(String, String))
INFERREDfn(Int) -> Option(#(String, String))

The indexth entry of the static table, from 1 to 61 (Appendix A).

function

encode_integer

src/whiplash/hpack.pooka:136
encode_integer(value: Int, prefix: Int, flags: Int) -> BitArray
INFERREDfn(Int, Int, Int) -> BitArray

An integer with an prefix-bit prefix, the first octet's other bits set from flags (RFC 7541 section 5.1).

function

decode_integer

src/whiplash/hpack.pooka:158
decode_integer(bytes: BitArray, prefix: Int) -> Result(
  #(Int, BitArray),
  HpackError
)
INFERREDfn(BitArray, Int) -> Result(#(Int, BitArray), whiplash/hpack.HpackError)

An integer with a prefix-bit prefix, and what follows it.

function

huffman_encode

src/whiplash/hpack.pooka:203
huffman_encode(text: String) -> BitArray
INFERREDfn(String) -> BitArray

Text as the Huffman code of Appendix B.

function

huffman_decode

src/whiplash/hpack.pooka:238
huffman_decode(bytes: BitArray) -> Result(String, HpackError)
INFERREDfn(BitArray) -> Result(String, whiplash/hpack.HpackError)

Huffman-coded octets as text. Padding longer than 7 bits, padding that is not the most significant bits of EOS, and an encoded EOS are errors.

function

huffman_decode_bytes

src/whiplash/hpack.pooka:245
huffman_decode_bytes(bytes: BitArray) -> Result(BitArray, HpackError)
INFERREDfn(BitArray) -> Result(BitArray, whiplash/hpack.HpackError)

Decode to the original octets, for protocols that must account table size independently of text conversion.