#hpack #http2 #codec #encoder #decoder #rfc #table

hpack_codec

Encoder and Decoder for HPACK (RFC 7541)

2 releases

0.1.1 Nov 12, 2019
0.1.0 Sep 18, 2017

#392 in Compression

Download history 1/week @ 2024-06-30

60 downloads per month
Used in httimple

MIT license

78KB
2K SLoC

hpack_codec

Documentation Build Status Code Coverage License: MIT

Encoder and Decoder for HPACK (RFC 7541).

Documentation

Examples

use hpack_codec::{Encoder, Decoder};
use hpack_codec::field::{HeaderField, LiteralHeaderField as Field};
use hpack_codec::table::{StaticEntry, Index};

// Encoding
let mut encoder = Encoder::new(4096);
let mut header = encoder.enter_header_block(Vec::new()).unwrap();
header.encode_field(StaticEntry::MethodGet).unwrap();
header.encode_field(Field::with_indexed_name(StaticEntry::Path, b"/hello")).unwrap();
header.encode_field(Field::new(b"foo", b"bar").with_indexing()).unwrap();
header.encode_field(Index::dynamic_table_offset() + 0).unwrap();
let encoded_data = header.finish();

// Decoding
let mut decoder = Decoder::new(4096);
let mut header = decoder.enter_header_block(&encoded_data[..]).unwrap();
assert_eq!(header.decode_field().unwrap(), HeaderField::new(b":method", b"GET").ok());
assert_eq!(header.decode_field().unwrap(), HeaderField::new(b":path", b"/hello").ok());
assert_eq!(header.decode_field().unwrap(), HeaderField::new(b"foo", b"bar").ok());
assert_eq!(header.decode_field().unwrap(), HeaderField::new(b"foo", b"bar").ok());

Dependencies

~1.5MB
~37K SLoC