Class: HTTP2::Header::Decompressor
- Inherits:
-
Object
- Object
- HTTP2::Header::Decompressor
- Includes:
- BufferUtils, Error
- Defined in:
- lib/http/2/header/decompressor.rb,
sig/header/decompressor.rbs
Overview
Responsible for decoding received headers and maintaining compression context of the opposing peer. Decompressor must be initialized with appropriate starting context based on local role: client or server.
Constant Summary collapse
- FORBIDDEN_HEADERS =
%w[connection te].freeze
Instance Attribute Summary collapse
-
#max_header_list_size ⇒ Object
writeonly
Sets the attribute max_header_list_size.
Instance Method Summary collapse
- #accept_invalid_headers! ⇒ void
-
#decode(buf, frame = nil) ⇒ Object
Decodes and processes header commands within provided
buf. -
#header(buf) ⇒ change_table_size_command, header_command
Decodes header command from provided
buf. -
#integer(buf, n) ⇒ Integer
Decodes integer value from provided
buffer. -
#max_table_size=(size) ⇒ void
Set the maximum dynamic table
sizeallowed by our acknowledged SETTINGS_HEADER_TABLE_SIZE. -
#string(buf) ⇒ String
Decodes string value from provided
buf.
Methods included from BufferUtils
#append_str, #read_str, #read_uint32, #shift_byte
Instance Attribute Details
#max_header_list_size=(value) ⇒ Object (writeonly)
Sets the attribute max_header_list_size
14 15 16 |
# File 'lib/http/2/header/decompressor.rb', line 14 def max_header_list_size=(value) @max_header_list_size = value end |
Instance Method Details
#accept_invalid_headers! ⇒ void
This method returns an undefined value.
22 23 24 |
# File 'lib/http/2/header/decompressor.rb', line 22 def accept_invalid_headers! @accept_invalid_headers = true end |
#decode(arg0, arg1) ⇒ Array[header_pair] #decode(arg0) ⇒ Array[header_pair]
Decodes and processes header commands within provided buf.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/http/2/header/decompressor.rb', line 108 def decode(buf, frame = nil) list = [] #: Array[header_pair] header_list_size = 0 decoding_pseudo_headers = true @cc.listen_on_table do until buf.empty? field, value = @cc.process(header(buf)) next if field.nil? is_pseudo_header = field.start_with?(":") if !decoding_pseudo_headers && is_pseudo_header && !@accept_invalid_headers raise ProtocolError, "one or more pseudo headers encountered after regular headers" end decoding_pseudo_headers = is_pseudo_header if !@accept_invalid_headers && FORBIDDEN_HEADERS.include?(field) raise ProtocolError, "invalid header received: #{field}" end header_list_size += field.bytesize + value.bytesize + 32 raise ProtocolError, "header list exceeds configured maximum" if header_list_size > @max_header_list_size if frame case field when ":status" frame[:status] = Integer(value) when ":method" frame[:method] = value when "content-length" frame[:content_length] = Integer(value) when "trailer" (frame[:trailer] ||= []) << value end end list << [field, value] end end list end |
#header(buf) ⇒ change_table_size_command, header_command
Decodes header command from provided buf.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/http/2/header/decompressor.rb', line 74 def header(buf) peek = buf.getbyte(0) header_type, type = HEADREP.find do |_, desc| mask = (peek >> desc[:prefix]) << desc[:prefix] mask == desc[:pattern] end raise CompressionError unless header_type && type header_name = integer(buf, type[:prefix]) case header_type when :indexed raise CompressionError if header_name.zero? header_name -= 1 { type: header_type, name: header_name } when :changetablesize { type: header_type, name: header_name, value: header_name } else if header_name.zero? header_name = string(buf) else header_name -= 1 end header_value = string(buf) { type: header_type, name: header_name, value: header_value } end end |
#integer(buf, n) ⇒ Integer
Decodes integer value from provided buffer.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/http/2/header/decompressor.rb', line 34 def integer(buf, n) limit = (1 << n) - 1 if n.zero? i = 0 consumed = 0 else i = buf.getbyte(0) & limit consumed = 1 end if i == limit m = 0 while (byte = buf.getbyte(consumed)) i += ((byte & 127) << m) m += 7 consumed += 1 break if byte.nobits?(128) end end read_str(buf, consumed) i end |
#max_table_size=(size) ⇒ void
This method returns an undefined value.
Set the maximum dynamic table size allowed by our acknowledged
SETTINGS_HEADER_TABLE_SIZE. When the current table is larger, the
next header block has to start with a dynamic table size update.
29 30 31 |
# File 'lib/http/2/header/decompressor.rb', line 29 def max_table_size=(size) @cc.max_table_size = size end |
#string(buf) ⇒ String
Decodes string value from provided buf.
raises CompressionError when input is malformed
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/http/2/header/decompressor.rb', line 61 def string(buf) raise CompressionError, "invalid header block fragment" if buf.empty? huffman = buf.getbyte(0).allbits?(0x80) len = integer(buf, 7) str = read_str(buf, len) raise CompressionError, "string too short" unless str.bytesize == len str = Huffman.decode(str) if huffman str.force_encoding(Encoding::UTF_8) end |