Class: Hpack::LookupTable

Inherits:
Object
  • Object
show all
Defined in:
lib/hpack/lookup_table.rb

Defined Under Namespace

Classes: Entry, IndexOutOfBounds

Constant Summary collapse

SETTINGS_HEADER_TABLE_SIZE =
4096
STATIC_TABLE_SIZE =
61
STATIC_ENTRIES =
[
  [":authority"],
  [":method", "GET"],
  [":method", "POST"],
  [":path", "/"],
  [":path", "/index.html"],
  [":scheme", "http"],
  [":scheme", "https"],
  [":status", "200"],
  [":status", "204"],
  [":status", "206"],
  [":status", "304"],
  [":status", "400"],
  [":status", "404"],
  [":status", "500"],
  ["accept-charset"],
  ["accept-encoding", "gzip, deflate"],
  ["accept-language"],
  ["accept-ranges"],
  ["accept"],
  ["access-control-allow-origin"],
  ["age"],
  ["allow"],
  ["authorization"],
  ["cache-control"],
  ["content-disposition"],
  ["content-encoding"],
  ["content-language"],
  ["content-length"],
  ["content-location"],
  ["content-range"],
  ["content-type"],
  ["cookie"],
  ["date"],
  ["etag"],
  ["expect"],
  ["expires"],
  ["from"],
  ["host"],
  ["if-match"],
  ["if-modified-since"],
  ["if-none-match"],
  ["if-range"],
  ["if-unmodified-since"],
  ["last-modified"],
  ["link"],
  ["location"],
  ["max-forwards"],
  ["proxy-authenticate"],
  ["proxy-authorization"],
  ["range"],
  ["referer"],
  ["refresh"],
  ["retry-after"],
  ["server"],
  ["set-cookie"],
  ["strict-transport-security"],
  ["transfer-encoding"],
  ["user-agent"],
  ["vary"],
  ["via"],
  ["www-authenticate"],
]

Instance Method Summary collapse

Constructor Details

#initialize(max_size: SETTINGS_HEADER_TABLE_SIZE) ⇒ LookupTable

Returns a new instance of LookupTable.



93
94
95
96
# File 'lib/hpack/lookup_table.rb', line 93

def initialize max_size: SETTINGS_HEADER_TABLE_SIZE
  @max_size = max_size
  @dynamic_entries = []
end

Instance Method Details

#<<(entry) ⇒ Object



146
147
148
149
# File 'lib/hpack/lookup_table.rb', line 146

def << entry
  @dynamic_entries.unshift entry
  evict
end

#[](index) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hpack/lookup_table.rb', line 98

def [] index
  if index <= STATIC_TABLE_SIZE
    Entry.new(*STATIC_ENTRIES[index - 1])
  else
    dynamic_index = index - STATIC_TABLE_SIZE - 1
    size = @dynamic_entries.length

    if dynamic_index >= size
      raise IndexOutOfBounds, "#{dynamic_index} is greater than dynamic table size #{size}"
    end

    @dynamic_entries[dynamic_index]
  end
end

#max_sizeObject



137
138
139
# File 'lib/hpack/lookup_table.rb', line 137

def max_size
  @max_size
end

#max_size=(value) ⇒ Object



141
142
143
144
# File 'lib/hpack/lookup_table.rb', line 141

def max_size= value
  @max_size = value
  evict
end

#sizeObject

4.1 Calculating Table Size

The size of the dynamic table is the sum of the size of its entries.

The size of an entry is the sum of its name’s length in octets (as defined in Section 5.2), its value’s length in octets (see Section 5.2), plus 32.

The size of an entry is calculated using the length of the name and value without any Huffman encoding applied.

NOTE: The additional 32 octets account for the overhead associated with an entry. For example, an entry structure using two 64-bit pointers to reference the name and the value of the entry, and two 64-bit integers for counting the number of references to the name and value would have 32 octets of overhead.



131
132
133
134
135
# File 'lib/hpack/lookup_table.rb', line 131

def size
  @dynamic_entries
    .map(&:size)
    .reduce(0, :+)
end

#to_sObject



151
152
153
154
155
156
157
158
159
160
# File 'lib/hpack/lookup_table.rb', line 151

def to_s
  table = @dynamic_entries
    .each_with_index
    .map { |e, i| "[%4s] %s" % [i + 1, e.to_s] }
    .join "\n"

  summary = "       Table size: #{size}"

  table + "\n" + summary
end