Class: HTTP2::Header::EncodingContext
- Inherits:
-
Object
- Object
- HTTP2::Header::EncodingContext
- Includes:
- Error
- Defined in:
- lib/http/2/header/encoding_context.rb
Constant Summary collapse
- UPPER =
/[[:upper:]]/.freeze
- 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(&:freeze).freeze
- STATIC_TABLE_BY_FIELD =
STATIC_TABLE .each_with_object({}) .with_index { |((field, value), hs), idx| (hs[field] ||= []) << [idx, value].freeze } .each_value(&:freeze) .freeze
- STATIC_TABLE_SIZE =
STATIC_TABLE.size
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Current encoding options.
-
#table ⇒ Object
readonly
Current table of header key-value pairs.
Instance Method Summary collapse
-
#addcmd(*header) ⇒ Hash
Emits command for a header.
-
#current_table_size ⇒ Integer
Returns current table size in octets.
-
#dereference(index) ⇒ Array
Finds an entry in current dynamic table by index.
-
#dup ⇒ EncodingContext
Duplicates current compression context.
-
#encode(headers) ⇒ Array
Plan header compression according to @options [:index] :never Do not use dynamic table or static table reference at all.
-
#initialize(options = {}) ⇒ EncodingContext
constructor
Initializes compression context with appropriate client/server defaults and maximum size of the dynamic table.
- #listen_on_table ⇒ Object
-
#process(cmd) ⇒ Array?
Header Block Processing - tools.ietf.org/html/draft-ietf-httpbis-header-compression-10#section-4.1.
-
#table_size=(size) ⇒ Object
Alter dynamic table size.
Constructor Details
#initialize(options = {}) ⇒ EncodingContext
Initializes compression context with appropriate client/server defaults and maximum size of the dynamic table.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/http/2/header/encoding_context.rb', line 106 def initialize( = {}) = { huffman: :shorter, index: :all, table_size: 4096 } @table = [] @options = .merge() @limit = @options[:table_size] @_table_updated = false end |
Instance Attribute Details
#options ⇒ Object (readonly)
Current encoding options
:table_size Integer maximum dynamic table size in bytes
:huffman Symbol :always, :never, :shorter
:index Symbol :all, :static, :never
97 98 99 |
# File 'lib/http/2/header/encoding_context.rb', line 97 def @options end |
#table ⇒ Object (readonly)
Current table of header key-value pairs.
90 91 92 |
# File 'lib/http/2/header/encoding_context.rb', line 90 def table @table end |
Instance Method Details
#addcmd(*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.
250 251 252 253 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 |
# File 'lib/http/2/header/encoding_context.rb', line 250 def addcmd(*header) exact = nil name_only = nil if %i[all static].include?(@options[:index]) field, value = header if (svalues = STATIC_TABLE_BY_FIELD[field]) svalues.each do |i, svalue| name_only ||= i if svalue == value exact = i break end 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_size ⇒ Integer
Returns current table size in octets
295 296 297 |
# File 'lib/http/2/header/encoding_context.rb', line 295 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 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.
141 142 143 144 145 146 147 |
# File 'lib/http/2/header/encoding_context.rb', line 141 def dereference(index) # NOTE: index is zero-based in this module. value = STATIC_TABLE[index] || @table[index - STATIC_TABLE_SIZE] raise CompressionError, "Index too large" unless value value end |
#dup ⇒ EncodingContext
Duplicates current compression context
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/http/2/header/encoding_context.rb', line 120 def dup other = EncodingContext.new(@options) t = @table l = @limit other.instance_eval do @table = t.dup # shallow copy @limit = l end 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.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/http/2/header/encoding_context.rb', line 221 def encode(headers) commands = [] # Literals commands are marked with :noindex when index is not used noindex = %i[static never].include?(@options[:index]) 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 commands << cmd process(cmd) end commands end |
#listen_on_table ⇒ Object
299 300 301 302 303 |
# File 'lib/http/2/header/encoding_context.rb', line 299 def listen_on_table yield ensure @_table_updated = false end |
#process(cmd) ⇒ Array?
Header Block Processing
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/http/2/header/encoding_context.rb', line 155 def process(cmd) emit = nil case cmd[:type] when :changetablesize raise CompressionError, "tried to change table size after adding elements to table" if @_table_updated # we can receive multiple table size change commands inside a header frame. However, # we should blow up if we receive another frame where the new table size is bigger. table_size_updated = @limit != @options[:table_size] raise CompressionError, "dynamic table size update exceed limit" if !table_size_updated && cmd[:value] > @limit self.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 dynamic 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 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 cmd[:name] when Integer k, v = dereference(cmd[:name]) cmd = cmd.dup cmd[:index] ||= cmd[:name] cmd[:value] ||= v cmd[:name] = k when UPPER raise ProtocolError, "Invalid uppercase key: #{cmd[:name]}" end emit = [cmd[:name], cmd[:value]] add_to_table(emit) if cmd[:type] == :incremental else raise CompressionError, "Invalid type: #{cmd[:type]}" end emit end |
#table_size=(size) ⇒ Object
Alter dynamic table size.
When the size is reduced, some headers might be evicted.
288 289 290 291 |
# File 'lib/http/2/header/encoding_context.rb', line 288 def table_size=(size) @limit = size size_check(nil) end |