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.

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.

See Also:



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

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], …]



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/http/2/compressor.rb', line 561

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
      raise 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.

Parameters:

Returns:

  • (Hash)

    command

Raises:

  • (CompressionError)


525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/http/2/compressor.rb', line 525

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

  raise CompressionError unless header[:type]

  header[:name] = integer(buf, type[:prefix])

  case header[:type]
  when :indexed
    raise 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)


489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/http/2/compressor.rb', line 489

def integer(buf, n)
  limit = (2**n) - 1
  i = n.zero? ? 0 : (buf.getbyte & limit)

  m = 0
  if i == limit
    while (byte = buf.getbyte)
      i += ((byte & 127) << m)
      m += 7

      break if (byte & 128).zero?
    end
  end

  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



511
512
513
514
515
516
517
518
519
# File 'lib/http/2/compressor.rb', line 511

def string(buf)
  huffman = (buf.readbyte(0) & 0x80) == 0x80
  len = integer(buf, 7)
  str = buf.read(len)
  raise 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



480
481
482
# File 'lib/http/2/compressor.rb', line 480

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