Class: HTTP::HPACK::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/http/hpack/context.rb

Overview

To decompress header blocks, a decoder only needs to maintain a dynamic 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',            ''],
].each {|pair| pair.each(&:freeze).freeze}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Context

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

Parameters:

  • options (Hash)

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



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/http/hpack/context.rb', line 127

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 dynamic table size in bytes
:huffman     Symbol   :always, :never, :shorter
:index       Symbol   :all, :static, :never


118
119
120
# File 'lib/http/hpack/context.rb', line 118

def options
  @options
end

#tableObject (readonly)

Current table of header key-value pairs.



111
112
113
# File 'lib/http/hpack/context.rb', line 111

def table
  @table
end

Instance Method Details

#add_command(*header) ⇒ Hash

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.

Parameters:

  • header (Array)

    [name, value]

Returns:

  • (Hash)

    command



258
259
260
261
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
# File 'lib/http/hpack/context.rb', line 258

def add_command(*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)


301
302
303
# File 'lib/http/hpack/context.rb', line 301

def current_table_size
	@table.inject(0) {|r, (k, v)| r + k.bytesize + v.bytesize + 32}
end

#decode(command) ⇒ Array

Parameters:

  • command (Hash)

    name:, value:, index:

Returns:

  • (Array)

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



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
# File 'lib/http/hpack/context.rb', line 174

def decode(command)
	emit = nil

	case command[:type]
	when :changetablesize
		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, :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 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) ⇒ Array

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:

  • index (Integer)

    zero-based index in the dynamic table.

Returns:

  • (Array)

    [key, value]

Raises:



160
161
162
163
164
165
166
167
# File 'lib/http/hpack/context.rb', line 160

def dereference(index)
	# NOTE: index is zero-based in this module.
	value = STATIC_TABLE[index] || @table[index - STATIC_TABLE.size]
	
	raise CompressionError, "Index #{index} too large" unless value
	
	return value
end

#dupContext

Duplicates current compression context

Returns:



141
142
143
144
145
146
147
148
# File 'lib/http/hpack/context.rb', line 141

def dup
	other = Context.new(@options)
	
	other.instance_variable_set :@table, @table.dup
	other.instance_variable_set :@limit, @limit
	
	return other
end

#encode(headers) ⇒ Array

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.

Parameters:

  • headers (Array)

    [[name, value], …]

Returns:

  • (Array)

    array of commands



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/http/hpack/context.rb', line 229

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|
		command = add_command(field, value)
		command[:type] = :noindex if noindex && command[:type] == :incremental
		commands << command
		
		decode(command)
	end
	
	return commands
end

#table_size=(size) ⇒ Object

Alter dynamic table size.

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


294
295
296
297
# File 'lib/http/hpack/context.rb', line 294

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