Method: HTTP2::Header::Compressor#string

Defined in:
lib/http/2/compressor.rb

#string(str) ⇒ String

Encodes provided value via string literal representation.

  • tools.ietf.org/html/draft-ietf-httpbis-header-compression-10#section-5.2

  • The string length, defined as the number of bytes needed to store its UTF-8 representation, is represented as an integer with a seven bits prefix. If the string length is strictly less than 127, it is represented as one byte.

  • If the bit 7 of the first byte is 1, the string value is represented as a list of Huffman encoded octets (padded with bit 1’s until next octet boundary).

  • If the bit 7 of the first byte is 0, the string value is represented as a list of UTF-8 encoded octets.

@options [:huffman] controls whether to use Huffman encoding:

:never   Do not use Huffman encoding
:always  Always use Huffman encoding
:shorter Use Huffman when the result is strictly shorter

Parameters:

  • str (String)

Returns:

  • (String)

    binary string



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/http/2/compressor.rb', line 395

def string(str)
  plain, huffman = nil, nil
  unless @cc.options[:huffman] == :always
    plain = integer(str.bytesize, 7) << str.dup.force_encoding(Encoding::BINARY)
  end
  unless @cc.options[:huffman] == :never
    huffman = Huffman.new.encode(str)
    huffman = integer(huffman.bytesize, 7) << huffman
    huffman.setbyte(0, huffman.ord | 0x80)
  end
  case @cc.options[:huffman]
  when :always
    huffman
  when :never
    plain
  else
    huffman.bytesize < plain.bytesize ? huffman : plain
  end
end