Class: HTTP2::Header::Decompressor

Inherits:
Object
  • Object
show all
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.

Examples:

server_role = Decompressor.new(:request)
client_role = Decompressor.new(:response)

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Decompressor

Returns a new instance of Decompressor

Parameters:

  • options (Hash)

    decoding options. Only :table_size is effective.


467
468
469
# File 'lib/http/2/compressor.rb', line 467

def initialize(**options)
  @cc = EncodingContext.new(options)
end

Instance Method Details

#decode(buf) ⇒ Array

Decodes and processes header commands within provided buffer.

Parameters:

Returns:

  • (Array)

    [[name, value], …]


550
551
552
553
554
# File 'lib/http/2/compressor.rb', line 550

def decode(buf)
  list = []
  list << @cc.process(header(buf)) until buf.empty?
  list.compact
end

#header(buf) ⇒ Hash

Decodes header command from provided buffer.

Parameters:

Returns:

  • (Hash)

    command


515
516
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
# File 'lib/http/2/compressor.rb', line 515

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.

Parameters:

  • buf (String)
  • n (Integer)

    number of available bits

Returns:

  • (Integer)

482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/http/2/compressor.rb', line 482

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.

Parameters:

  • buf (String)

Returns:

  • (String)

    UTF-8 encoded string

Raises:

  • (CompressionError)

    when input is malformed


502
503
504
505
506
507
508
509
# File 'lib/http/2/compressor.rb', line 502

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

Parameters:

  • size (Integer)

    new dynamic table size


473
474
475
# File 'lib/http/2/compressor.rb', line 473

def table_size=(size)
  @cc.table_size = size
end