hpack.cr v1.0.0
hpack
This shard provides a standalone, pure Crystal HPack implementation. HPack is a Huffman-based header compression format that is used with HTTP/2.
This implementation is based on @ysbaddaden's implementation that is bundled inside of his http2 server & client shard, with some modest refactoring and significant improvements, packaged and maintained for current Crystal versions.
Installation
-
Add the dependency to your
shard.yml
:dependencies: hpack: github: wyhaines/hpack.cr
-
Run
shards install
Usage
require "hpack"
There are two main classes provided by this shard, HPack::Encoder
and HPack::Decoder
. The default Encoder will be created with Indexing set to NONE
, huffman encoding false, and the max table size set to 4k (4096 bytes). These parameters can all be set in the constructor.
# To create a default Encoder:
encoder = HPack::Encoder.new
# To create an encoder with indexing set to Always and Huffamn encoding set to true:
encoder = HPack::Encoder.new(indexing: HPack::Indexing::ALWAYS, huffman: true)
encoder = HPack::Encoder.new(HPack::Indexing::ALWAYS, true)
# To create an encoder with the max table size set to 8k (8096 bytes):
encoder = HPack::Encoder.new(HPack::Indexing::ALWAYS, true, 8096)
# To encode headers:
encoder.encode(
HTTP::Headers {
":status" => "302",
"cache-control" => "private",
"date" => "Mon, 21 Oct 2013 20:13:21 GMT",
"location" => "https://www.example.com"
}
)
To decode headers, used a HPack::Decoder
instance. By default, a decoder is created with a 4k (4096 bytes) table size. That table size can be changed in the constructor.
# To create a default Decoder:
decoder = HPack::Decoder.new
# To create a decoder with a larger table size:
decoder = HPack::Decoder.new(8192)
# To decode headers:
headers = decoder.decode(bytes)
# To decode headers into an existing `HTTP::Headers` instance:
headers = decoder.decode(bytes, HTTP::Headers.new)
Contributing
- Fork it (https://github.com/wyhaines/hpack/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Kirk Haines - creator and maintainer
hpack.cr
- 3
- 0
- 0
- 1
- 1
- about 2 years ago
- December 24, 2021
Apache License 2.0
Tue, 07 May 2024 11:17:00 GMT