class Protocol::HPACK::Context

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

Static header table. tools.ietf.org/html/rfc7541#appendix-A

Attributes

huffman[R]
index[R]
table[R]

Current table of header key-value pairs.

table_size[R]

Public Class Methods

new(table = nil, huffman: :shorter, index: :all, table_size: 4096) click to toggle source

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

@param table [Array] Table of header key-value pairs. @option huffman [Symbol] One of `:always`, `:never`, `:shorter`. Controls use of compression. @option index [Symbol] One of `:all`, `:static`, `:never`. Controls use of static/dynamic tables. @option table_size [Integer] The current maximum dynamic table size.

# File lib/protocol/hpack/context.rb, line 117
def initialize(table = nil, huffman: :shorter, index: :all, table_size: 4096)
        @huffman = huffman
        @index = index
        
        @table_size = table_size
        
        @table = (table&.dup) || []
end

Public Instance Methods

add_command(*header) click to toggle source

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

+@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/protocol/hpack/context.rb, line 251
def add_command(*header)
        exact = nil
        name_only = nil

        if [:all, :static].include?(@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?(@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
change_table_size(size) click to toggle source
# File lib/protocol/hpack/context.rb, line 292
def change_table_size(size)
        self.table_size = size
        
        # The command to resize the table:
        return {type: :change_table_size, value: size}
end
current_table_size() click to toggle source

Returns current table size in octets @return [Integer]

# File lib/protocol/hpack/context.rb, line 301
def current_table_size
        @table.inject(0) {|r, (k, v)| r + k.bytesize + v.bytesize + 32}
end
decode(command) click to toggle source

Header Block Processing

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

# File lib/protocol/hpack/context.rb, line 167
def decode(command)
        emit = nil

        case command[:type]
        when :change_table_size
                self.table_size = command[: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 = command[:name]

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

        when :incremental, :no_index, :never_indexed
                # 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 command[:name].is_a? Integer
                        k, v = dereference(command[:name])

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

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

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

        else
                raise CompressionError, "Invalid type: #{command[:type]}"
        end

        return emit
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/protocol/hpack/context.rb, line 151
def dereference(index)
        # NOTE: index is zero-based in this module.
        value = STATIC_TABLE[index] || @table[index - STATIC_TABLE.size]
        
        if value.nil?
                raise CompressionError, "Index #{index} too large!"
        end
        
        return value
end
encode(headers) click to toggle source

Plan header compression according to +@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/protocol/hpack/context.rb, line 222
def encode(headers)
        commands = []
        
        # Literals commands are marked with :no_index when index is not used
        no_index = [:static, :never].include?(@index)
        
        headers.each do |field, value|
                command = add_command(field, value)
                command[:type] = :no_index if no_index && command[:type] == :incremental
                commands << command
                
                decode(command)
        end
        
        return commands
end
initialize_copy(other) click to toggle source
Calls superclass method
# File lib/protocol/hpack/context.rb, line 126
def initialize_copy(other)
        super
        
        # This is the only mutable state:
        @table = @table.dup
end
table_size=(size) click to toggle source

Alter dynamic table size.

When the size is reduced, some headers might be evicted.
# File lib/protocol/hpack/context.rb, line 287
def table_size= size
        @table_size = size
        size_check(nil)
end

Private Instance Methods

add_to_table(command) 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. The command and the component strings will be frozen.

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

# File lib/protocol/hpack/context.rb, line 310
def add_to_table(command)
        return unless size_check(command)
        
        command.each(&:freeze)
        command.freeze
        
        @table.unshift(command)
end
size_check(command) click to toggle source

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

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

# File lib/protocol/hpack/context.rb, line 324
def size_check(command)
        cursize = current_table_size
        cmdsize = command.nil? ? 0 : command[0].bytesize + command[1].bytesize + 32

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

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

        cmdsize <= @table_size
end