Class: HTTP2::Header::Decompressor
- Inherits:
-
Object
- Object
- HTTP2::Header::Decompressor
- Defined in:
- lib/http/2/compressor.rb
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.
Instance Method Summary collapse
-
#decode(buf) ⇒ Array
Decodes and processes header commands within provided buffer.
-
#header(buf) ⇒ Hash
Decodes header command from provided buffer.
-
#initialize(**options) ⇒ Decompressor
constructor
A new instance of Decompressor.
-
#integer(buf, n) ⇒ Integer
Decodes integer value from provided buffer.
-
#string(buf) ⇒ String
Decodes string value from provided buffer.
-
#table_size=(size) ⇒ Object
Set dynamic table size in EncodingContext.
Constructor Details
#initialize(**options) ⇒ Decompressor
Returns a new instance of Decompressor.
469 470 471 |
# File 'lib/http/2/compressor.rb', line 469 def initialize(**) @cc = EncodingContext.new() end |
Instance Method Details
#decode(buf) ⇒ Array
Decodes and processes header commands within provided buffer.
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/http/2/compressor.rb', line 552 def decode(buf) list = [] decoding_pseudo_headers = true until buf.empty? next_header = @cc.process(header(buf)) next if next_header.nil? is_pseudo_header = next_header.first.start_with? ':' if !decoding_pseudo_headers && is_pseudo_header fail ProtocolError, 'one or more pseudo headers encountered after regular headers' end decoding_pseudo_headers = is_pseudo_header list << next_header end list end |
#header(buf) ⇒ Hash
Decodes header command from provided buffer.
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/http/2/compressor.rb', line 517 def header(buf) peek = buf.readbyte(0) header = {} header[:type], type = HEADREP.find do |_t, desc| mask = (peek >> desc[:prefix]) << desc[:prefix] mask == desc[:pattern] end fail CompressionError unless header[:type] header[:name] = integer(buf, type[:prefix]) case header[:type] when :indexed fail CompressionError if (header[:name]).zero? header[:name] -= 1 when :changetablesize header[:value] = header[:name] else if (header[:name]).zero? header[:name] = string(buf) else header[:name] -= 1 end header[:value] = string(buf) end header end |
#integer(buf, n) ⇒ Integer
Decodes integer value from provided buffer.
484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/http/2/compressor.rb', line 484 def integer(buf, n) limit = 2**n - 1 i = !n.zero? ? (buf.getbyte & limit) : 0 m = 0 while (byte = buf.getbyte) i += ((byte & 127) << m) m += 7 break if (byte & 128).zero? end if (i == limit) i end |
#string(buf) ⇒ String
Decodes string value from provided buffer.
504 505 506 507 508 509 510 511 |
# File 'lib/http/2/compressor.rb', line 504 def string(buf) huffman = (buf.readbyte(0) & 0x80) == 0x80 len = integer(buf, 7) str = buf.read(len) fail CompressionError, 'string too short' unless str.bytesize == len str = Huffman.new.decode(Buffer.new(str)) if huffman str.force_encoding(Encoding::UTF_8) end |
#table_size=(size) ⇒ Object
Set dynamic table size in EncodingContext
475 476 477 |
# File 'lib/http/2/compressor.rb', line 475 def table_size=(size) @cc.table_size = size end |