httlib-hpack
This crate implements HPACK, a compression format for efficiently representing HTTP header fields in HTTP/2. It exposes a simple API for performing the encoding and decoding of HTTP headers.
About
HPACK is a compression format that eliminates redundant header fields in requests and responses. This is one of the features based on which the HTTP/2 protocol significantly reduces the amount of transferred data from one entity to another.
A significant shift in thinking is required from the implementer of the HTTP/2 protocol. A connection in HTTP/2 does not represent a single request/response session. We can start multiple simultaneous streams in one connection, representing multiple request/response sessions, which was not possible in the previous versions of the HTTP protocol. The HPACK compressor uses this characteristic of HTTP/2 by indexing headers considering the whole connection and not per stream.
The implementation of HPACK contains three main parts of the process:
-
Indexing table
is a list, to which the HPACK saves the commonly used headers. Each entity indexes headers per connection, separately for incoming (decoding) and for outgoing (encoding) data. -
Encoder
performs the task of data compression. It converts the data from its original readable form into an optimized byte sequence by applying the rules defined in the HPACK specification. -
Decoder
takes over the task of the decompressor. It executes the commands inversely to the encoder. It converts the data back into its readable form.
Usage
Encoding example:
use Encoder;
let mut encoder = default;
let name = b":method".to_vec;
let value = b"PATCH".to_vec;
let flags = HUFFMAN_VALUE | WITH_INDEXING | BEST_FORMAT;
let mut dst = Vec new;
encoder.encode.unwrap;
Decoding example:
use Decoder;
let mut decoder = default;
let mut buf = vec!;
let mut dst = Vec new;
decoder.decode.unwrap;
for in dst
Articles
License: MIT