#huffman #hpack #canonical #huffman-coding #http #codec #string-literal


Canonical Huffman algorithm for handling HPACK format in HTTP/2

14 releases

0.3.4 Nov 11, 2020
0.3.3 Nov 10, 2020
0.2.3 Nov 5, 2020
0.2.0 Oct 20, 2020
0.1.4 Oct 16, 2020

#529 in Algorithms

Download history 443/week @ 2024-03-14 482/week @ 2024-03-21 806/week @ 2024-03-28 1468/week @ 2024-04-04 1078/week @ 2024-04-11 1010/week @ 2024-04-18 1854/week @ 2024-04-25 1416/week @ 2024-05-02 775/week @ 2024-05-09 854/week @ 2024-05-16 848/week @ 2024-05-23 760/week @ 2024-05-30 982/week @ 2024-06-06 912/week @ 2024-06-13 910/week @ 2024-06-20 594/week @ 2024-06-27

3,518 downloads per month
Used in 14 crates (2 directly)

MIT license



This crate implements canonical Huffman functionality for handling HPACK format in HTTP/2. It exposes a simple API for performing the encoding and decoding of HTTP/2 header string literals according to the HPACK spec.

Documentation Source

Header Compression format for HTTP/2, known as HPACK, foresees the use of the Huffman algorithm for encoding header literal values. This contributes to the additional decrease in the quantity of data, transferred with each web request and response.

A Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression. The process of finding or using such a code proceeds by means of Huffman coding, an algorithm developed by David A. Huffman. The output from Huffman's algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file). The algorithm derives this table from the estimated probability or frequency of occurrence (weight) for each possible value of the source symbol. As in other entropy encoding methods, more common symbols are generally represented using fewer bits than less common symbols. Huffman's method can be efficiently implemented, finding a code in time linear to the number of input weights if these weights are sorted.

HPACK compression entails a pre-created canonical Huffman code table for encoding ASCII characters to the Huffman sequence. A canonical Huffman code is a particular type of Huffman code with unique properties that allow it to be described in a very compact manner. In the aforementioned table are the Huffman codes for each ASCII character with a length up to 32 bits (4x by 8 fields with value 0 or 1), in the form of base-2 integer, aligned on the most significant bit (MSB is the bit farthest to the left).

Each module covers this topic in more details so check the rest of the code to learn more.


Encoding example:

use httlib_huffman::encode;

let mut dst = Vec::new();
let text = "Hello world!".as_bytes();
encode(&text, &mut dst).unwrap();

Decoding example:

use httlib_huffman::{DecoderSpeed, decode};

let speed = DecoderSpeed::ThreeBits;
let mut dst = Vec::new();
let src = vec![135];
decode(&src, &mut dst, speed).unwrap();


License: MIT

No runtime deps