Class: HTTP2::Header::EncodingContext

Inherits:
Object
  • Object
show all
Includes:
Error
Defined in:
lib/http/2/compressor.rb

Overview

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

Constant Summary collapse

STATIC_TABLE =
[
  [':authority',                  ''            ],
  [':method',                     'GET'         ],
  [':method',                     'POST'        ],
  [':path',                       '/'           ],
  [':path',                       '/index.html' ],
  [':scheme',                     'http'        ],
  [':scheme',                     'https'       ],
  [':status',                     '200'         ],
  [':status',                     '204'         ],
  [':status',                     '206'         ],
  [':status',                     '304'         ],
  [':status',                     '400'         ],
  [':status',                     '404'         ],
  [':status',                     '500'         ],
  ['accept-charset',              ''            ],
  ['accept-encoding',             'gzip, deflate' ],
  ['accept-language',             ''            ],
  ['accept-ranges',               ''            ],
  ['accept',                      ''            ],
  ['access-control-allow-origin', ''            ],
  ['age',                         ''            ],
  ['allow',                       ''            ],
  ['authorization',               ''            ],
  ['cache-control',               ''            ],
  ['content-disposition',         ''            ],
  ['content-encoding',            ''            ],
  ['content-language',            ''            ],
  ['content-length',              ''            ],
  ['content-location',            ''            ],
  ['content-range',               ''            ],
  ['content-type',                ''            ],
  ['cookie',                      ''            ],
  ['date',                        ''            ],
  ['etag',                        ''            ],
  ['expect',                      ''            ],
  ['expires',                     ''            ],
  ['from',                        ''            ],
  ['host',                        ''            ],
  ['if-match',                    ''            ],
  ['if-modified-since',           ''            ],
  ['if-none-match',               ''            ],
  ['if-range',                    ''            ],
  ['if-unmodified-since',         ''            ],
  ['last-modified',               ''            ],
  ['link',                        ''            ],
  ['location',                    ''            ],
  ['max-forwards',                ''            ],
  ['proxy-authenticate',          ''            ],
  ['proxy-authorization',         ''            ],
  ['range',                       ''            ],
  ['referer',                     ''            ],
  ['refresh',                     ''            ],
  ['retry-after',                 ''            ],
  ['server',                      ''            ],
  ['set-cookie',                  ''            ],
  ['strict-transport-security',   ''            ],
  ['transfer-encoding',           ''            ],
  ['user-agent',                  ''            ],
  ['vary',                        ''            ],
  ['via',                         ''            ],
  ['www-authenticate',            ''            ],
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ EncodingContext

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

Parameters:

  • options (Hash)

    encoding options :table_size Integer maximum header table size in bytes :huffman Symbol :always, :never, :shorter :index Symbol :all, :static, :never



101
102
103
104
105
106
107
108
109
110
# File 'lib/http/2/compressor.rb', line 101

def initialize(**options)
  default_options = {
    huffman:    :shorter,
    index:      :all,
    table_size: 4096,
  }
  @table = []
  @options = default_options.merge(options)
  @limit = @options[:table_size]
end

Instance Attribute Details

#optionsObject (readonly)

Current encoding options

:table_size  Integer  maximum header table size in bytes
:huffman     Symbol   :always, :never, :shorter
:index       Symbol   :all, :static, :never


92
93
94
# File 'lib/http/2/compressor.rb', line 92

def options
  @options
end

#tableObject (readonly)

Current table of header key-value pairs.



85
86
87
# File 'lib/http/2/compressor.rb', line 85

def table
  @table
end

Instance Method Details

#addcmd(header) ⇒ Hash

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

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

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

Parameters:

  • header (Array)

    [name, value]

Returns:

  • (Hash)

    command



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# 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_sizeInteger

Returns current table size in octets

Returns:

  • (Integer)


274
275
276
# 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) ⇒ Array

Finds an entry in current header 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 header table is dereferenced.

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

Parameters:

  • index (Integer)

    zero-based index in the header table.

Returns:

  • (Array)

    [key, value]



135
136
137
138
139
140
# File 'lib/http/2/compressor.rb', line 135

def dereference(index)
  # NOTE: index is zero-based in this module.
  STATIC_TABLE[index] or
    @table[index - STATIC_TABLE.size] or
    raise CompressionError.new("Index too large")
end

#dupEncodingContext

Duplicates current compression context

Returns:



114
115
116
117
118
119
120
121
122
123
# File 'lib/http/2/compressor.rb', line 114

def dup
  other = EncodingContext.new(@options)
  t = @table
  l = @limit
  other.instance_eval {
    @table = t.dup              # shallow copy
    @limit = l
  }
  other
end

#encode(headers) ⇒ Array

Plan header compression according to @options [:index]

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

Parameters:

  • headers (Array)

    [[name, value], …]

Returns:

  • (Array)

    array of commands



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/http/2/compressor.rb', line 204

def encode(headers)
  commands = []
  # Literals commands are marked with :noindex when index is not used
  noindex = [:static, :never].include?(@options[:index])
  headers.each do |h|
    cmd = addcmd(h)
    if noindex && cmd[:type] == :incremental
      cmd[:type] = :noindex
    end
    commands << cmd
    process(cmd)
  end
  commands
end

#process(cmd) ⇒ Array

Parameters:

  • cmd (Hash)

    { type:, name:, value:, index: }

Returns:

  • (Array)

    [name, value] header field that is added to the decoded header list



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/http/2/compressor.rb', line 147

def process(cmd)
  emit = nil

  case cmd[:type]
  when :changetablesize
    set_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 header 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 header table
    # entails the following action:
    # o  The header field is added to the decoded header list.

    # A _literal representation_ that is _added_ to the header 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 header 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]]

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

  else
    raise CompressionError.new("Invalid type: #{cmd[:type]}")
  end

  emit
end

#set_table_size(size) ⇒ Object

Alter header table size.

When the size is reduced, some headers might be evicted.


267
268
269
270
# File 'lib/http/2/compressor.rb', line 267

def set_table_size(size)
  @limit = size
  size_check(nil)
end