Class: HTTP2::Header::EncodingContext

Inherits:
Object
  • Object
show all
Includes:
Error
Defined in:
lib/http/2/header/encoding_context.rb,
sig/header/encoding_context.rbs

Constant Summary collapse

UPPER =

Returns:

  • (Regexp)
/[[:upper:]]/.freeze
STATIC_TABLE =

Returns:

  • (Array[header_pair])
[
  [":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",            ""]
].each(&:freeze).freeze
STATIC_TABLE_BY_FIELD =

Returns:

  • (Hash[String, Array[[Integer, String]]])
STATIC_TABLE
.each_with_object({})
.with_index { |((field, value), hs), idx| (hs[field] ||= []) << [idx, value].freeze }
.each_value(&:freeze)
.freeze
STATIC_TABLE_SIZE =

Returns:

  • (Integer)
STATIC_TABLE.size
STATIC_ALL =

Returns:

  • (Array[Symbol])
%i[all static].freeze
STATIC_NEVER =

Returns:

  • (Array[Symbol])
%i[never static].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_table_size ⇒ Integer (readonly)

Current table size in octets

Returns:

  • (Integer)


100
101
102
# File 'lib/http/2/header/encoding_context.rb', line 100

def current_table_size
  @current_table_size
end

#settings ⇒ Settings (readonly)

Current encoding settings

Returns:



97
98
99
# File 'lib/http/2/header/encoding_context.rb', line 97

def settings
  @settings
end

#table ⇒ Array[header_pair] (readonly)

Current table of header key-value pairs.

Returns:

  • (Array[header_pair])


94
95
96
# File 'lib/http/2/header/encoding_context.rb', line 94

def table
  @table
end

Instance Method Details

#addcmd(field, value) ⇒ header_command

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

Parameters:

  • name (String)
  • value (String)

Returns:

  • (header_command)


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/http/2/header/encoding_context.rb', line 262

def addcmd(field, value)
  # @type var exact: Integer?
  exact = nil
  # @type var name_only: Integer?
  name_only = nil

  index_type = @settings.index

  if STATIC_ALL.include?(index_type) &&
     STATIC_TABLE_BY_FIELD.key?(field)
    STATIC_TABLE_BY_FIELD[field].each do |i, svalue|
      name_only ||= i
      if value == svalue
        exact = i
        break
      end
    end
  end

  if index_type == :all && !exact
    field_entries = @table_by_field[field]

    field_entries&.each do |hvalue, unshift_id|
      abs_index = (@unshifts - unshift_id) + STATIC_TABLE_SIZE
      name_only ||= abs_index
      if value == hvalue
        exact = abs_index
        break
      end
    end
  end

  if exact
    { name: exact, type: :indexed }
  else
    { name: name_only || field, value: value, type: :incremental }
  end
end

#dereference(index) ⇒ header_pair

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.

Parameters:

  • (Integer)

Returns:

  • (header_pair)

Raises:



149
150
151
152
153
154
155
156
157
158
# File 'lib/http/2/header/encoding_context.rb', line 149

def dereference(index)
  # NOTE: index is zero-based in this module.
  return STATIC_TABLE[index] if index < STATIC_TABLE_SIZE

  idx = index - STATIC_TABLE_SIZE

  raise CompressionError, "Index too large" if idx >= @table.size

  @table[index - STATIC_TABLE_SIZE]
end

#dup ⇒ EncodingContext

Duplicates current compression context

Returns:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/http/2/header/encoding_context.rb', line 123

def dup
  other = EncodingContext.new(@settings)
  t = @table
  tbf = @table_by_field.transform_values(&:dup)
  unshifts = @unshifts
  l = @limit
  ml = @max_size_limit
  lml = @lowest_max_size_limit
  other.instance_eval do
    @table = t.dup # shallow copy
    @table_by_field = tbf
    @unshifts = unshifts
    @limit = l
    @max_size_limit = ml
    @lowest_max_size_limit = lml
  end
  other
end

#encode(headers) {|header_command| ... } ⇒ void

This method returns an undefined value.

Plan headers compression.

Emits dynamic table size updates first when the table size has to change. See #max_table_size=.

Parameters:

  • (_Each[header_pair])

Yields:

Yield Parameters:

  • header_command (change_table_size_command)

Yield Returns:

  • (void)


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/http/2/header/encoding_context.rb', line 237

def encode(headers, &block)
  # Literals commands are marked with :noindex when index is not used
  noindex = STATIC_NEVER.include?(@settings.index)

  if @_max_table_size_updated
    encode_change_table_size(&block)
    @_max_table_size_updated = false
  end

  headers.each do |field, value|
    # Literal header names MUST be translated to lowercase before
    # encoding and transmission.
    field = field.downcase if UPPER.match?(field)
    value = "/" if field == ":path" && value.empty?
    cmd = addcmd(field, value)
    cmd[:type] = :noindex if noindex && cmd[:type] == :incremental
    process(cmd)
    yield cmd
  end
end

#listen_on_table { ... } ⇒ void

This method returns an undefined value.

Yields:

Yield Returns:

  • (void)


328
329
330
331
332
# File 'lib/http/2/header/encoding_context.rb', line 328

def listen_on_table
  yield
ensure
  @_table_updated = false
end

#max_table_size=(size) ⇒ void

This method returns an undefined value.

Set the maximum dynamic table size allowed by SETTINGS_HEADER_TABLE_SIZE.

The current table size does not change here. It changes only with a dynamic table size update at the beginning of a header block:

  • an encoder emits the update in the next #encode call. It uses settings.table_size, capped by this maximum.
  • a decoder requires the update in the next header block when the current table size exceeds this maximum.

When the maximum is reduced and then increased again before the next header block, the smallest maximum has to be signalled first.

Parameters:

  • (Integer)


321
322
323
324
325
326
# File 'lib/http/2/header/encoding_context.rb', line 321

def max_table_size=(size)
  @max_size_limit = size
  # alternative: clamp size based on @max_size_limit, instead of failing later on #process
  @lowest_max_size_limit = size if size < @lowest_max_size_limit
  @_max_table_size_updated = true
end

#process(cmd) ⇒ header_pair?

Parameters:

  • cmd (change_table_size_command, header_command)

Returns:

  • (header_pair, nil)


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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/http/2/header/encoding_context.rb', line 162

def process(cmd)
  type = cmd[:type]

  # The maximum size was reduced below the current table size and the
  # encoder has not signalled a size that fits into it.
  if type != :changetablesize && @lowest_max_size_limit < @limit
    raise CompressionError,
          "dynamic table size update required"
  end

  name = cmd[:name]
  value = cmd[:value]

  case type
  when :changetablesize
    raise CompressionError, "tried to change table size after adding elements to table" if @_table_updated

    # The new maximum size MUST be lower than or equal to the limit set
    # by SETTINGS_HEADER_TABLE_SIZE.
    # - https://www.rfc-editor.org/rfc/rfc7541#section-6.3
    raise CompressionError, "dynamic table size update exceed limit" if value > @max_size_limit

    # The smallest maximum size set since the last update is signalled.
    @lowest_max_size_limit = @max_size_limit if value <= @lowest_max_size_limit
    self.table_size = value

    nil
  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.
    dereference(name)
  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.

    case name
    when Integer
      name, v = dereference(name)

      value ||= v
    when UPPER
      raise ProtocolError, "Invalid uppercase key: #{name}"
    end

    emit = [name, value]

    # add to table
    cmdsize = name.bytesize + value.bytesize + 32
    if type == :incremental && size_check?(cmdsize)
      @table.unshift(emit)
      @unshifts += 1
      @table_by_field[name].unshift([value, @unshifts])
      @current_table_size += cmdsize
      @_table_updated = true
    end

    emit
  else
    raise CompressionError, "Invalid type: #{type}"
  end
end

#table_size=(size) ⇒ void

This method returns an undefined value.

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

Parameters:

  • (Integer)


303
304
305
306
# File 'lib/http/2/header/encoding_context.rb', line 303

def table_size=(size)
  @limit = size
  resize_table(0)
end