lib/protocol/hpack/context.rb in protocol-hpack-1.4.2 vs lib/protocol/hpack/context.rb in protocol-hpack-1.4.3

- old
+ new

@@ -1,43 +1,30 @@ # frozen_string_literal: true -# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com> -# Copyrigh, 2013, by Ilya Grigorik. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. +# Released under the MIT License. +# Copyright, 2018-2024, by Samuel Williams. require_relative 'huffman' module Protocol # Implementation of header compression for HTTP 2.0 (HPACK) format adapted # to efficiently represent HTTP headers in the context of HTTP 2.0. # # - http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-10 module HPACK # Header representation as defined by the spec. + NO_INDEX_TYPE = {prefix: 4, pattern: 0x00}.freeze + NEVER_INDEXED_TYPE = {prefix: 4, pattern: 0x10}.freeze + CHANGE_TABLE_SIZE_TYPE = {prefix: 5, pattern: 0x20}.freeze + INCREMENTAL_TYPE = {prefix: 6, pattern: 0x40}.freeze + INDEXED_TYPE = {prefix: 7, pattern: 0x80}.freeze HEADER_REPRESENTATION = { - indexed: {prefix: 7, pattern: 0x80}, - incremental: {prefix: 6, pattern: 0x40}, - no_index: {prefix: 4, pattern: 0x00}, - never_indexed: {prefix: 4, pattern: 0x10}, - change_table_size: {prefix: 5, pattern: 0x20}, + indexed: INDEXED_TYPE, + incremental: INCREMENTAL_TYPE, + no_index: NO_INDEX_TYPE, + never_indexed: NEVER_INDEXED_TYPE, + change_table_size: CHANGE_TABLE_SIZE_TYPE } # To decompress header blocks, a decoder only needs to maintain a # dynamic table as a decoding context. # No other state information is needed. @@ -296,12 +283,12 @@ return {type: :change_table_size, value: size} end # Returns current table size in octets # @return [Integer] - def current_table_size - @table.inject(0) {|r, (k, v)| r + k.bytesize + v.bytesize + 32} + def compute_current_table_size + @table.sum { |k, v| k.bytesize + v.bytesize + 32 } end private # Add a name-value pair to the dynamic table. Older entries might have been evicted so that the new entry fits in the dynamic table. The command and the component strings will be frozen. @@ -312,25 +299,32 @@ command.each(&:freeze) command.freeze @table.unshift(command) + @current_table_size += entry_size(command) end + def entry_size(e) + e[0].bytesize + e[1].bytesize + 32 + end + # To keep the dynamic table size lower than or equal to @table_size, # remove one or more entries at the end of the dynamic table. # # @param command [Hash] # @return [Boolean] whether +command+ fits in the dynamic table. def size_check(command) - cursize = current_table_size + + @current_table_size ||= compute_current_table_size + cmdsize = command.nil? ? 0 : command[0].bytesize + command[1].bytesize + 32 - while cursize + cmdsize > @table_size + while @current_table_size + cmdsize > @table_size break if @table.empty? e = @table.pop - cursize -= e[0].bytesize + e[1].bytesize + 32 + @current_table_size -= e[0].bytesize + e[1].bytesize + 32 end cmdsize <= @table_size end end