hpack.cr v1.0.0

This shard is a standalone implementation of HPack, the HTTP2 header compression algorithm.

hpack

Send.cr CI GitHub release GitHub commits since latest release (by SemVer)

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

  1. Add the dependency to your shard.yml:

    dependencies:
      hpack:
        github: wyhaines/hpack.cr
    
  2. 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

  1. Fork it (https://github.com/wyhaines/hpack/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

GitHub code size in bytes GitHub issues

Repository

hpack.cr

Owner
Statistic
  • 3
  • 0
  • 0
  • 1
  • 1
  • about 2 years ago
  • December 24, 2021
License

Apache License 2.0

Links
Synced at

Tue, 07 May 2024 11:17:00 GMT

Languages