hpack API

This document provides the HPACK API.

class hpack.Encoder[source]

An HPACK encoder object. This object takes HTTP headers and emits encoded HTTP/2 header blocks.

encode(headers, huffman=True)[source]

Takes a set of headers and encodes them into a HPACK-encoded header block.

  • headers

    The headers to encode. Must be either an iterable of tuples, an iterable of HeaderTuple, or a dict.

    If an iterable of tuples, the tuples may be either two-tuples or three-tuples. If they are two-tuples, the tuples must be of the format (name, value). If they are three-tuples, they must be of the format (name, value, sensitive), where sensitive is a boolean value indicating whether the header should be added to header tables anywhere. If not present, sensitive defaults to False.

    If an iterable of HeaderTuple, the tuples must always be two-tuples. Instead of using sensitive as a third tuple entry, use NeverIndexedHeaderTuple to request that the field never be indexed.


    HTTP/2 requires that all special headers (headers whose names begin with : characters) appear at the start of the header block. While this method will ensure that happens for dict subclasses, callers using any other iterable of tuples must ensure they place their special headers at the start of the iterable.

    For efficiency reasons users should prefer to use iterables of two-tuples: fixing the ordering of dictionary headers is an expensive operation that should be avoided if possible.

  • huffman – (optional) Whether to Huffman-encode any header sent as a literal value. Except for use when debugging, it is recommended that this be left enabled.

A bytestring containing the HPACK-encoded header block.


Controls the size of the HPACK header table.

class hpack.Decoder(max_header_list_size=65536)[source]

An HPACK decoder object.

Changed in version 2.3.0: Added max_header_list_size argument.

Parameters:max_header_list_size (int) –

The maximum decompressed size we will allow for any single header block. This is a protection against DoS attacks that attempt to force the application to expand a relatively small amount of data into a really large header list, allowing enormous amounts of memory to be allocated.

If this amount of data is exceeded, a OversizedHeaderListError <hpack.OversizedHeaderListError> exception will be raised. At this point the connection should be shut down, as the HPACK state will no longer be usable.

Defaults to 64kB.

decode(data, raw=False)[source]

Takes an HPACK-encoded header block and decodes it into a header set.

  • data – A bytestring representing a complete HPACK-encoded header block.
  • raw – (optional) Whether to return the headers as tuples of raw byte strings or to decode them as UTF-8 before returning them. The default value is False, which returns tuples of Unicode strings

A list of two-tuples of (name, value) representing the HPACK-encoded headers, in the order they were decoded.


HPACKDecodingError – If an error is encountered while decoding the header block.


Controls the size of the HPACK header table.

class hpack.HeaderTuple[source]

A data structure that stores a single header field.

HTTP headers can be thought of as tuples of (field name, field value). A single header block is a sequence of such tuples.

In HTTP/2, however, certain bits of additional information are required for compressing these headers: in particular, whether the header field can be safely added to the HPACK compression context.

This class stores a header that can be added to the compression context. In all other ways it behaves exactly like a tuple.

class hpack.NeverIndexedHeaderTuple[source]

A data structure that stores a single header field that cannot be added to a HTTP/2 header compression context.

class hpack.HPACKError[source]

The base class for all hpack exceptions.

class hpack.HPACKDecodingError[source]

An error has been encountered while performing HPACK decoding.

class hpack.InvalidTableIndex[source]

An invalid table index was received.

class hpack.OversizedHeaderListError[source]

A header list that was larger than we allow has been received. This may be a DoS attack.

New in version 2.3.0.

class hpack.InvalidTableSizeError[source]

An attempt was made to change the decoder table size to a value larger than allowed, or the list was shrunk and the remote peer didn’t shrink their table size.

New in version 3.0.0.