class

HPack::Encoder

Inherits Reference < Object

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 Huffman 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 8 KiB (8192 bytes):
encoder = HPack::Encoder.new(HPack::Indexing::ALWAYS, true, 8192)

# 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",
  }
)

Constructors

new(indexing : HPack::Indexing = Indexing::NONE, huffman : Bool = false, max_table_size = 4096)
Source

Instance methods

default_huffman

Retain the established getter name for API compatibility.

Source
default_huffman=(default_huffman : Bool)

Retain the established getter name for API compatibility.

Source
default_indexing
Source
default_indexing=(default_indexing : Indexing)
Source
encode(fields : Enumerable(Tuple(String, String)), indexing = default_indexing, huffman = default_huffman) : Bytes

Encodes ordered name/value fields and returns an owned byte slice.

Fields are emitted in the supplied order and duplicate names are preserved. Callers must place every pseudo-header before regular fields. Use the HTTP::Headers overload when the encoder should perform that reordering.

Source
encode(fields : Enumerable(HeaderField), indexing : Indexing = default_indexing, huffman : Bool | HuffmanMode = default_huffman) : Bytes

Encodes ordered fields with optional per-field overrides.

Source
encode(headers : HTTP::Headers, indexing = default_indexing, huffman = default_huffman, _writer : IO::Memory | Nil = nil) : Bytes

Encodes headers and returns an owned byte slice.

Pseudo-headers are emitted before regular headers regardless of their insertion order. The returned bytes remain valid after later calls.

The _writer argument is retained for compatibility. New code that owns its output buffer should use #encode_into. When _writer is given, the returned slice is always a fresh copy: it never aliases _writer's own buffer, so mutating one afterward cannot affect the other.

Source
encode(fields : Enumerable(Tuple(String, String)), indexing : Indexing = default_indexing, huffman : Bool | HuffmanMode = default_huffman, & : String, String -> FieldOptions | Nil) : Bytes

Encodes ordered tuple fields using a per-field policy block.

Fields remain in supplied order. The block receives normalized names and may return FieldOptions overrides.

Source
encode(fields : Enumerable(HeaderField), indexing : Indexing = default_indexing, huffman : Bool | HuffmanMode = default_huffman, & : String, String -> FieldOptions | Nil) : Bytes

Encodes ordered fields with explicit options and a policy block.

Each HeaderField option takes precedence over the corresponding callback option, which takes precedence over the per-call value.

Source
encode(headers : HTTP::Headers, indexing : Indexing = default_indexing, huffman : Bool | HuffmanMode = default_huffman, & : String, String -> FieldOptions | Nil) : Bytes

Encodes headers using a policy evaluated once per field in wire order.

The block receives the normalized name and value and may return FieldOptions overrides. A nil return retains the per-call values.

The block must not call back into encode/encode_into on this same encoder (that raises Error), and must not mutate headers itself: pseudo-headers and regular headers are each walked in a separate pass over the same HTTP::Headers, so a mutation made from the block during the first pass would be observed by the second.

encoder.encode(headers, huffman: HPack::HuffmanMode::SMALLER) do |name, _|
  if name == "authorization"
    HPack::FieldOptions.new(indexing: HPack::Indexing::NEVER)
  end
end
Source
encode(fields : Enumerable(DecodedHeader), *, huffman : Bool | HuffmanMode = default_huffman) : Bytes

Encodes decoded fields while preserving each field's indexing marker.

Only Huffman behavior may be overridden; in particular, Indexing::NEVER cannot be downgraded while forwarding.

Source
encode_into(fields : Enumerable(Tuple(String, String)), output : IO, indexing = default_indexing, huffman = default_huffman) : Nil

Appends one encoded block for ordered name/value fields to output.

Existing output is not cleared, duplicate names are preserved, and fields are emitted in the supplied order. Callers must place every pseudo-header before regular fields.

The block is encoded into an internal buffer first, so a failure during encoding leaves output untouched. The completed block is then copied to output in a single write call; whether that call itself can partially deliver bytes before raising depends on output's own semantics, not on the encoder.

Source
encode_into(fields : Enumerable(HeaderField), output : IO, indexing : Indexing = default_indexing, huffman : Bool | HuffmanMode = default_huffman) : Nil

Appends ordered fields with optional per-field overrides to output.

The block is encoded into an internal buffer first, so a failure during encoding leaves output untouched. The completed block is then copied to output in a single write call; whether that call itself can partially deliver bytes before raising depends on output's own semantics, not on the encoder.

Source
encode_into(headers : HTTP::Headers, output : IO, indexing = default_indexing, huffman = default_huffman) : Nil

Appends one encoded block for headers to output.

Existing output is not cleared. Pseudo-headers are emitted before regular headers regardless of insertion order.

The block is encoded into an internal buffer first, so a failure during encoding leaves output untouched. The completed block is then copied to output in a single write call; whether that call itself can partially deliver bytes before raising depends on output's own semantics, not on the encoder.

Source
encode_into(fields : Enumerable(Tuple(String, String)), output : IO, indexing : Indexing = default_indexing, huffman : Bool | HuffmanMode = default_huffman, & : String, String -> FieldOptions | Nil) : Nil

Appends policy-driven ordered tuple fields to output.

The block is encoded into an internal buffer first, so a failure during encoding leaves output untouched. The completed block is then copied to output in a single write call; whether that call itself can partially deliver bytes before raising depends on output's own semantics, not on the encoder.

Source
encode_into(fields : Enumerable(HeaderField), output : IO, indexing : Indexing = default_indexing, huffman : Bool | HuffmanMode = default_huffman, & : String, String -> FieldOptions | Nil) : Nil

Appends ordered fields using explicit options and a policy block.

The block is encoded into an internal buffer first, so a failure during encoding leaves output untouched. The completed block is then copied to output in a single write call; whether that call itself can partially deliver bytes before raising depends on output's own semantics, not on the encoder.

Source
encode_into(headers : HTTP::Headers, output : IO, indexing : Indexing = default_indexing, huffman : Bool | HuffmanMode = default_huffman, & : String, String -> FieldOptions | Nil) : Nil

Appends a policy-driven encoded block for headers to output.

Existing output is not cleared. The policy is evaluated once per field after name normalization and pseudo-header ordering.

As with the policy-block #encode(headers) overload, the block must not call back into this same encoder and must not mutate headers (pseudo-headers and regular headers are walked in two separate passes over it).

The block is encoded into an internal buffer first, so a failure during encoding leaves output untouched. The completed block is then copied to output in a single write call; whether that call itself can partially deliver bytes before raising depends on output's own semantics, not on the encoder.

Source
encode_into(fields : Enumerable(DecodedHeader), output : IO, *, huffman : Bool | HuffmanMode = default_huffman) : Nil

Appends decoded fields while preserving every indexing marker.

The block is encoded into an internal buffer first, so a failure during encoding leaves output untouched. The completed block is then copied to output in a single write call; whether that call itself can partially deliver bytes before raising depends on output's own semantics, not on the encoder.

Source
resize_table(size : Int) : Nil

Changes the dynamic-table capacity and queues the corresponding HPACK size update for the beginning of the next encoded field block.

The local table is resized immediately. Multiple changes before a block are coalesced to the smallest requested size followed by the final size. Pending updates are consumed only after a field block is encoded successfully.

Use this operation for negotiated capacity changes. Calling #table.resize directly does not signal the peer decoder.

Source
table
Source