nahpackpy¶
nahpackpy is an HTTP/2 RFC 7541 HPACK library. It wraps the Rust nahpack library.
>>> from nahpackpy import Decoder
>>> decoder = Decoder()
>>> headers = decoder.decode_block(
b'\x82\x86\x84A\x8c\xf1\xe3\xc2\xe5'
b'\xf2:k\xa0\xab\x90\xf4\xff'
)
>>> for header in headers:
print(header)
Header(name=b':method', value=b'GET')
Header(name=b':scheme', value=b'http')
Header(name=b':path', value=b'/')
Header(name=b':authority', value=b'www.example.com')
>>> from nahpackpy import Encoder
>>> encoder = Encoder()
>>> encoder.encode_block((
(b':method', b'GET'),
(b':scheme', b'http'),
(b':path', b'/'),
(b':authority', b'www.example.com'),
))
b'\x82\x86\x84A\x8c\xf1\xe3\xc2\xe5\xf2:k\xa0\xab\x90\xf4\xff'
Decoding¶
-
class
nahpackpy.Header¶ Header is a namedtuple of name and value.
-
name bytes
-
value bytes
-
-
class
nahpackpy.Decoder¶ Decoder contains the dynamic table and capacity setting for decompressing inbound headers.
-
decode_block(block)¶ Decode a transmitted block and, if valid, return a
Headersobject containing the decoded headers.Parameters: headers (bytes) – Block to decode. If a block is transmitted over multiple frames, the entire, merged block must be given. Return type: HeadersRaises: InsufficientData,InvalidIndex,InvalidInteger,ExcessiveInteger,InvalidCompressedLiteral,IllegalEOS,SettingsSizeExceeded
-
set_capacity(capacity)¶ Set the maximum table capacity as set in a SETTINGS frame.
Parameters: capacity (int) – SETTINGS frame value.
-
-
class
nahpackpy.HpackDecodeError¶ nahpackpy captures all decoding exceptions to raise HpackDecodeError.
-
class
nahpackpy.InsufficientData¶ The header block ends prematurely.
-
class
nahpackpy.InvalidIndex¶ There was reference to an indexed header that is not in the table.
-
class
nahpackpy.InvalidInteger¶ An integer could not be decoded.
-
class
nahpackpy.ExcessiveInteger¶ This library’s limit of an integer being at most 4 bytes was exceeded.
-
class
nahpackpy.InvalidCompressedLiteral¶ The compressed literal could not be decoded.
-
class
nahpackpy.IllegalEOS¶ EOS symbol was unexpectedly found in a compressed literal.
-
class
nahpackpy.SettingsSizeExceeded¶ Header block gave a value for table resize that exceeds the value set by a settings frame.
Encoding¶
-
class
nahpackpy.Encoder¶ Encoder contains the dynamic table and size setting for compressing outbound headers.
-
encode_block(headers)¶ Encode an iterable of headers and return the bytes.
Parameters: headers (tuple) – Iterable of (name<bytes>, value<bytes>) iterables. Return type: bytes Raises: InvalidHeaders
-
set_capacity(capacity)¶ Set the maximum table capacity as set in a SETTINGS frame.
Parameters: capacity (int) – SETTINGS frame value.
-
-
class
nahpackpy.InvalidHeaders¶ The headers given for encoding were not of the type expected.