Module
whiplash/hpack
src/whiplash/hpack.pookaHeaders, 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
- classField
- classHpackError
- classDecoder
- classEncoder
- classEntry
- classTable
- functiondecoder
- functionencoder
- functionwithout_huffman
- functiondecode
- functiondecode_checked
- functionencode
- functionwith_max_table_size
- functionwith_peer_table_size
- functionstatic_entry
- functionencode_integer
- functiondecode_integer
- functionhuffman_encode
- functionhuffman_decode
- functionhuffman_decode_bytes
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).
name: String
Stringvalue: String
Stringnever_index: Bool
Bool
fn(String, String, Bool) -> whiplash/hpack.Fieldclass 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.
message: String
String
fn(String) -> whiplash/hpack.HpackErrorclass 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.
table: Table
whiplash/hpack.Tablemax_table_size: Int
Inttainted: Bool
Bool
fn(whiplash/hpack.Table, Int, Bool) -> whiplash/hpack.Decoderclass 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.
table: Table
whiplash/hpack.Tablemax_table_size: Int
Inthuffman: Bool
Boolpending: Int
Int
fn(whiplash/hpack.Table, Int, Bool, Int) -> whiplash/hpack.Encoderclass 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).
name: String
Stringvalue: String
Stringname_size: Int
Intvalue_size: Int
Int
fn(String, String, Int, Int) -> whiplash/hpack.Entryclass Table(entries: List(Entry), size: Int, capacity: Int)
entries: List(Entry)
List(whiplash/hpack.Entry)size: Int
Intcapacity: Int
Int
fn(List(whiplash/hpack.Entry), Int, Int) -> whiplash/hpack.Tabledecoder(max_table_size: Int) -> Decoder
fn(Int) -> whiplash/hpack.DecoderA decoder whose dynamic table may grow to max_table_size octets: 4096
unless SETTINGS_HEADER_TABLE_SIZE says otherwise.
encoder(max_table_size: Int) -> Encoder
fn(Int) -> whiplash/hpack.EncoderAn encoder that may use max_table_size octets of dynamic table, and
Huffman-codes strings when that makes them shorter.
without_huffman(encoder: Encoder) -> Encoder
fn(whiplash/hpack.Encoder) -> whiplash/hpack.EncoderThe same encoder, writing every string literally.
decode(decoder: Decoder, block: BitArray) -> Result(
#(List(Field), Decoder),
HpackError
)
fn(whiplash/hpack.Decoder, BitArray) -> Result(#(List(whiplash/hpack.Field), whiplash/hpack.Decoder), whiplash/hpack.HpackError)Decode one complete header block.
decode_checked(decoder: Decoder, block: BitArray) -> Result(
#(List(Field), Decoder, Bool),
HpackError
)
fn(whiplash/hpack.Decoder, BitArray) -> Result(#(List(whiplash/hpack.Field), whiplash/hpack.Decoder, Bool), whiplash/hpack.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.
encode(encoder: Encoder, fields: List(Field)) -> #(BitArray, Encoder)
fn(whiplash/hpack.Encoder, List(whiplash/hpack.Field)) -> #(BitArray, whiplash/hpack.Encoder)Encode fields as one header block.
with_max_table_size(decoder: Decoder, size: Int) -> Decoder
fn(whiplash/hpack.Decoder, Int) -> whiplash/hpack.DecoderChange the size the decoder accepts, after the local SETTINGS frame that announced it has been acknowledged. A smaller size evicts at once.
with_peer_table_size(encoder: Encoder, size: Int) -> Encoder
fn(whiplash/hpack.Encoder, Int) -> whiplash/hpack.EncoderThe 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.
static_entry(index: Int) -> Option(#(String, String))
fn(Int) -> Option(#(String, String))The indexth entry of the static table, from 1 to 61 (Appendix A).
encode_integer(value: Int, prefix: Int, flags: Int) -> BitArray
fn(Int, Int, Int) -> BitArrayAn integer with an prefix-bit prefix, the first octet's other bits
set from flags (RFC 7541 section 5.1).
decode_integer(bytes: BitArray, prefix: Int) -> Result(
#(Int, BitArray),
HpackError
)
fn(BitArray, Int) -> Result(#(Int, BitArray), whiplash/hpack.HpackError)An integer with a prefix-bit prefix, and what follows it.
huffman_encode(text: String) -> BitArray
fn(String) -> BitArrayText as the Huffman code of Appendix B.
huffman_decode(bytes: BitArray) -> Result(String, HpackError)
fn(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.
huffman_decode_bytes(bytes: BitArray) -> Result(BitArray, HpackError)
fn(BitArray) -> Result(BitArray, whiplash/hpack.HpackError)Decode to the original octets, for protocols that must account table size independently of text conversion.