Class: Protocol::HPACK::Context
- Inherits:
-
Object
- Object
- Protocol::HPACK::Context
- Defined in:
- lib/protocol/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 =
Static header table. tools.ietf.org/html/rfc7541#appendix-A
[ [":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_EXACT_LOOKUP =
{}
- STATIC_NAME_LOOKUP =
{}
Instance Attribute Summary collapse
-
#huffman ⇒ Object
readonly
Returns the value of attribute huffman.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#table ⇒ Object
readonly
Current table of header key-value pairs.
-
#table_size ⇒ Object
Returns the value of attribute table_size.
Instance Method Summary collapse
-
#add_command(name, value) ⇒ Hash
Emits command for a header.
- #change_table_size(size) ⇒ Object
-
#compute_current_table_size ⇒ Integer
Returns current table size in octets.
-
#decode(command) ⇒ Array
Header Block Processing - tools.ietf.org/html/draft-ietf-httpbis-header-compression-10#section-4.1.
-
#dereference(index) ⇒ Array
Finds an entry in current dynamic table by index.
-
#encode(headers) ⇒ Array
Plan header compression according to @index :never Do not use dynamic table or static table reference at all.
-
#initialize(table = nil, huffman: :shorter, index: :all, table_size: 4096) ⇒ Context
constructor
Initializes compression context with appropriate client/server defaults and maximum size of the dynamic table.
- #initialize_copy(other) ⇒ Object
Constructor Details
#initialize(table = nil, huffman: :shorter, index: :all, table_size: 4096) ⇒ Context
Initializes compression context with appropriate client/server defaults and maximum size of the dynamic table.
119 120 121 122 123 124 125 126 |
# File 'lib/protocol/hpack/context.rb', line 119 def initialize(table = nil, huffman: :shorter, index: :all, table_size: 4096) @huffman = huffman @index = index @table_size = table_size @table = (table&.dup) || [] end |
Instance Attribute Details
#huffman ⇒ Object (readonly)
Returns the value of attribute huffman.
138 139 140 |
# File 'lib/protocol/hpack/context.rb', line 138 def huffman @huffman end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
139 140 141 |
# File 'lib/protocol/hpack/context.rb', line 139 def index @index end |
#table ⇒ Object (readonly)
Current table of header key-value pairs.
136 137 138 |
# File 'lib/protocol/hpack/context.rb', line 136 def table @table end |
#table_size ⇒ Object
Returns the value of attribute table_size.
141 142 143 |
# File 'lib/protocol/hpack/context.rb', line 141 def table_size @table_size end |
Instance Method Details
#add_command(name, value) ⇒ Hash
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.
254 255 256 257 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 291 292 293 294 295 296 297 298 |
# File 'lib/protocol/hpack/context.rb', line 254 def add_command(name, value) exact = nil name_only = nil if @index == :all || @index == :static if (values_and_indices = STATIC_EXACT_LOOKUP[name]) values_and_indices.each do |known_value, index| if value == known_value exact = index break end end needs_name_lookup = exact.nil? else needs_name_lookup = true end if needs_name_lookup && (static_value = STATIC_NAME_LOOKUP[name]) name_only = static_value end end if @index == :all && !exact @table.each_index do |i| entry = @table[i] if entry.first == name if entry.last == value exact ||= i + STATIC_TABLE.size break else name_only ||= i + STATIC_TABLE.size end end end end if exact {name: exact, type: :indexed} elsif name_only {name: name_only, value: value, type: :incremental} else {name: name, value: value, type: :incremental} end end |
#change_table_size(size) ⇒ Object
307 308 309 310 311 312 |
# File 'lib/protocol/hpack/context.rb', line 307 def change_table_size(size) self.table_size = size # The command to resize the table: return {type: :change_table_size, value: size} end |
#compute_current_table_size ⇒ Integer
Returns current table size in octets
316 317 318 |
# File 'lib/protocol/hpack/context.rb', line 316 def compute_current_table_size @table.sum { |k, v| k.bytesize + v.bytesize + 32 } end |
#decode(command) ⇒ Array
Header Block Processing
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 |
# File 'lib/protocol/hpack/context.rb', line 169 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) ⇒ 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.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/protocol/hpack/context.rb', line 153 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) ⇒ Array
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.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/protocol/hpack/context.rb', line 224 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) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/protocol/hpack/context.rb', line 128 def initialize_copy(other) super # This is the only mutable state: @table = @table.dup end |