class HTTP2::Header::EncodingContext

To decompress header blocks, a decoder only needs to maintain a dynamic table as a decoding context. No other state information is needed.

Constants

STATIC_TABLE

@private Static table

Attributes

options[R]

Current encoding options

:table_size  Integer  maximum dynamic table size in bytes
:huffman     Symbol   :always, :never, :shorter
:index       Symbol   :all, :static, :never
table[R]

Current table of header key-value pairs.

Public Class Methods

new(**options) click to toggle source

Initializes compression context with appropriate client/server defaults and maximum size of the dynamic table.

@param options [Hash] encoding options

:table_size  Integer  maximum dynamic table size in bytes
:huffman     Symbol   :always, :never, :shorter
:index       Symbol   :all, :static, :never
# File lib/http/2/compressor.rb, line 97
def initialize(**options)
  default_options = {
    huffman:    :shorter,
    index:      :all,
    table_size: 4096,
  }
  @table = []
  @options = default_options.merge(options)
  @limit = @options[:table_size]
end

Public Instance Methods

addcmd(*header) click to toggle source

Emits command for a header. Prefer static table over dynamic table. Prefer exact match over name-only match.

+@options [:index]+ controls whether to use the dynamic table, static table, or both.

:never   Do not use dynamic table or static table reference at all.
:static  Use static table only.
:all     Use all of them.

@param header [Array] +[name, value]+ @return [Hash] command

# File lib/http/2/compressor.rb, line 231
def addcmd(*header)
  exact = nil
  name_only = nil

  if [:all, :static].include?(@options[:index])
    STATIC_TABLE.each_index do |i|
      if STATIC_TABLE[i] == header
        exact ||= i
        break
      elsif STATIC_TABLE[i].first == header.first
        name_only ||= i
      end
    end
  end
  if [:all].include?(@options[:index]) && !exact
    @table.each_index do |i|
      if @table[i] == header
        exact ||= i + STATIC_TABLE.size
        break
      elsif @table[i].first == header.first
        name_only ||= i + STATIC_TABLE.size
      end
    end
  end

  if exact
    { name: exact, type: :indexed }
  elsif name_only
    { name: name_only, value: header.last, type: :incremental }
  else
    { name: header.first, value: header.last, type: :incremental }
  end
end
current_table_size() click to toggle source

Returns current table size in octets @return [Integer]

# File lib/http/2/compressor.rb, line 274
def current_table_size
  @table.inject(0) { |r, (k, v)| r + k.bytesize + v.bytesize + 32 }
end
dereference(index) click to toggle source

Finds an entry in current dynamic table by index. Note that index is zero-based in this module.

If the index is greater than the last index in the static table, an entry in the dynamic table is dereferenced.

If the index is greater than the last header index, an error is raised.

@param index [Integer] zero-based index in the dynamic table. @return [Array] +[key, value]+

# File lib/http/2/compressor.rb, line 131
def dereference(index)
  # NOTE: index is zero-based in this module.
  value = STATIC_TABLE[index] || @table[index - STATIC_TABLE.size]
  fail CompressionError, 'Index too large' unless value
  value
end
dup() click to toggle source

Duplicates current compression context @return [EncodingContext]

# File lib/http/2/compressor.rb, line 110
def dup
  other = EncodingContext.new(@options)
  t = @table
  l = @limit
  other.instance_eval do
    @table = t.dup              # shallow copy
    @limit = l
  end
  other
end
encode(headers) click to toggle source

Plan header compression according to +@options [:index]+

:never   Do not use dynamic table or static table reference at all.
:static  Use static table only.
:all     Use all of them.

@param headers [Array] +[[name, value], …]+ @return [Array] array of commands

# File lib/http/2/compressor.rb, line 202
def encode(headers)
  commands = []
  # Literals commands are marked with :noindex when index is not used
  noindex = [:static, :never].include?(@options[:index])
  headers.each do |field, value|
    # Literal header names MUST be translated to lowercase before
    # encoding and transmission.
    field = field.downcase
    value = '/' if field == ':path' && value.empty?
    cmd = addcmd(field, value)
    cmd[:type] = :noindex if noindex && cmd[:type] == :incremental
    commands << cmd
    process(cmd)
  end
  commands
end
process(cmd) click to toggle source

Header Block Processing

@param cmd [Hash] { type:, name:, value:, index: } @return [Array, nil] +[name, value]+ header field that is added to the decoded header list,

or nil if +cmd[:type]+ is +:changetablesize+
# File lib/http/2/compressor.rb, line 144
def process(cmd)
  emit = nil

  case cmd[:type]
  when :changetablesize
    if cmd[:value] > @limit
      fail CompressionError, 'dynamic table size update exceed limit'
    end
    self.table_size = cmd[:value]

  when :indexed
    # Indexed Representation
    # An _indexed representation_ entails the following actions:
    # o  The header field corresponding to the referenced entry in either
    # the static table or dynamic table is added to the decoded header
    # list.
    idx = cmd[:name]

    k, v = dereference(idx)
    emit = [k, v]

  when :incremental, :noindex, :neverindexed
    # A _literal representation_ that is _not added_ to the dynamic table
    # entails the following action:
    # o  The header field is added to the decoded header list.

    # A _literal representation_ that is _added_ to the dynamic table
    # entails the following actions:
    # o  The header field is added to the decoded header list.
    # o  The header field is inserted at the beginning of the dynamic table.

    if cmd[:name].is_a? Integer
      k, v = dereference(cmd[:name])

      cmd = cmd.dup
      cmd[:index] ||= cmd[:name]
      cmd[:value] ||= v
      cmd[:name] = k
    end

    emit = [cmd[:name], cmd[:value]]

    add_to_table(emit) if cmd[:type] == :incremental

  else
    fail CompressionError, "Invalid type: #{cmd[:type]}"
  end

  emit
end
table_size=(size) click to toggle source

Alter dynamic table size.

When the size is reduced, some headers might be evicted.
# File lib/http/2/compressor.rb, line 267
def table_size=(size)
  @limit = size
  size_check(nil)
end

Private Instance Methods

add_to_table(cmd) click to toggle source

Add a name-value pair to the dynamic table. Older entries might have been evicted so that the new entry fits in the dynamic table.

@param cmd [Array] +[name, value]+

# File lib/http/2/compressor.rb, line 285
def add_to_table(cmd)
  return unless size_check(cmd)
  @table.unshift(cmd)
end
size_check(cmd) click to toggle source

To keep the dynamic table size lower than or equal to @limit, remove one or more entries at the end of the dynamic table.

@param cmd [Hash] @return [Boolean] whether cmd fits in the dynamic table.

# File lib/http/2/compressor.rb, line 295
def size_check(cmd)
  cursize = current_table_size
  cmdsize = cmd.nil? ? 0 : cmd[0].bytesize + cmd[1].bytesize + 32

  while cursize + cmdsize > @limit
    break if @table.empty?

    e = @table.pop
    cursize -= e[0].bytesize + e[1].bytesize + 32
  end

  cmdsize <= @limit
end