Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

http.h2.huffman

import http

http exposes this as http.h2.huffman, so import http is enough and the names are called as http.h2.huffman.*. import http.h2.huffman reaches the same definitions directly.

The static Huffman code HPACK uses to compress header field names and values, from RFC 7541 Appendix B.

Only the code lengths are written out below. The code itself is canonical - codes are assigned in increasing symbol order within each length - so every one of the 257 codes can be derived from its length alone, which is both far less to state and far less to get wrong. _build() does the derivation at load time and also checks the Kraft sum, which comes to exactly one for a complete prefix code and to something else the moment a single length is wrong.

Constants

EOS

http.h2.huffman.EOS: int = 256

The number of the symbol HPACK uses to pad the final byte of a Huffman-encoded string, and which must never appear as a decoded value.

Functions

encoded_length()

http.h2.huffman.encoded_length(data) -> number

The number of bytes data would occupy once Huffman-encoded.

The encoder uses this to decide whether encoding is worth it at all: HPACK lets a string go out as-is, and for a string of mostly high-entropy bytes the encoded form is the longer one.

Parameters

  • data (bytes)

Returns number

encode()

http.h2.huffman.encode(data) -> bytes

Huffman-encodes data, padding the final byte with the leading bits of the EOS code (which are all ones) as RFC 7541 §5.2 requires.

Parameters

  • data (bytes)

Returns bytes

decode()

http.h2.huffman.decode(data, start, end) -> bytes

Decodes a Huffman-encoded string.

The padding at the end must be the leading bits of EOS and no longer than seven bits; anything else is a connection-level error per RFC 7541 §5.2, since it is the shape a smuggled second header takes.

Parameters

  • data (bytes)
  • start (?number)
  • end (?number)

Returns bytes

Raises ProtocolError on invalid padding or an encoded EOS