protocol-hpack 1.4.3 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3192190834a35550921cbff8e50482499716791371f521a77c3f7c4b5a0efcff
4
- data.tar.gz: 40d443b19eaac26aaaaae63da3baab5ae384246804643fdc7fb01f5455595dc3
3
+ metadata.gz: 3d664c70ae90134d28bf5491fe5f46612ce17292d8ebdc06daba9e423459501e
4
+ data.tar.gz: 0ede73ffcfb63a755022f3212309ce6ce4a74dcebd819f0287f8cf610a17fbee
5
5
  SHA512:
6
- metadata.gz: c19a7524c1a544016e0feb7d88099a67fbccf3fe200214f7bc21639512da55e72a9e3442c8917f69fec1568a3027487e701a2f1e99deb652942ebae54c4752a7
7
- data.tar.gz: 3f105d7bba7111585325db6bc8f414c34366bfb189f2263fa497e8ec53ed1502c3694c0dcd6749151fb10456c52a5a333daa9b7f5f8b362026d8be745c568375
6
+ metadata.gz: a194f5bbdfdbb5552f0ca6216b9cbb314bb5590a10316d51f99578961ebd3247bfa3aea6f3f8430f0e2f34ec89e9a18574c7080988f5cfde571b58ed8d6b143e
7
+ data.tar.gz: e11f0747b74859d27a2834350e46c49ea573e71f772687b387d6b4e4d1fd6b3033bc2c99537961fe2c59944e0acb2e0aeb78c0d7a9dbeb681cb04391d7e0ebb2
checksums.yaml.gz.sig CHANGED
Binary file
@@ -11,6 +11,7 @@
11
11
  # Copyright, 2018, by Kenichi Nakamura.
12
12
  # Copyright, 2019, by Jingyi Chen.
13
13
  # Copyright, 2020, by Justin Mazzocchi.
14
+ # Copyright, 2024, by Nathan Froyd.
14
15
 
15
16
  require_relative 'context'
16
17
  require_relative 'huffman'
@@ -118,7 +119,7 @@ module Protocol
118
119
  # @return [String] binary string
119
120
  def write_string(string, huffman = self.huffman)
120
121
  if huffman != :never
121
- encoded = Huffman.new.encode(string)
122
+ encoded = Huffman.encode(string)
122
123
 
123
124
  if huffman == :shorter and encoded.bytesize >= string.bytesize
124
125
  encoded = nil
@@ -2,6 +2,8 @@
2
2
 
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2018-2024, by Samuel Williams.
5
+ # Copyright, 2024, by Maruth Goyal.
6
+ # Copyright, 2024, by Nathan Froyd.
5
7
 
6
8
  require_relative 'huffman'
7
9
 
@@ -94,7 +96,20 @@ module Protocol
94
96
  ["via", ""],
95
97
  ["www-authenticate", ""],
96
98
  ].each(&:freeze).freeze
97
-
99
+
100
+ STATIC_EXACT_LOOKUP = {}
101
+ STATIC_NAME_LOOKUP = {}
102
+
103
+ STATIC_TABLE.each_with_index do |(name, value), i|
104
+ exact_header_values = (STATIC_EXACT_LOOKUP[name] ||= [])
105
+ exact_header_values << [value, i]
106
+ STATIC_NAME_LOOKUP[name] = i if STATIC_NAME_LOOKUP[name].nil?
107
+ end
108
+
109
+ STATIC_EXACT_LOOKUP.each {|k, v| v.freeze}
110
+ STATIC_EXACT_LOOKUP.freeze
111
+ STATIC_NAME_LOOKUP.freeze
112
+
98
113
  # Initializes compression context with appropriate client/server defaults and maximum size of the dynamic table.
99
114
  #
100
115
  # @param table [Array] Table of header key-value pairs.
@@ -233,29 +248,42 @@ module Protocol
233
248
  # :static Use static table only.
234
249
  # :all Use all of them.
235
250
  #
236
- # @param header [Array] +[name, value]+
251
+ # @param name [String]
252
+ # @param value [String]
237
253
  # @return [Hash] command
238
- def add_command(*header)
254
+ def add_command(name, value)
239
255
  exact = nil
240
256
  name_only = nil
241
257
 
242
- if [:all, :static].include?(@index)
243
- STATIC_TABLE.each_index do |i|
244
- if STATIC_TABLE[i] == header
245
- exact ||= i
246
- break
247
- elsif STATIC_TABLE[i].first == header.first
248
- name_only ||= i
258
+ if @index == :all || @index == :static
259
+ if (values_and_indices = STATIC_EXACT_LOOKUP[name])
260
+ values_and_indices.each do |known_value, index|
261
+ if value == known_value
262
+ exact = index
263
+ break
264
+ end
249
265
  end
266
+
267
+ needs_name_lookup = exact.nil?
268
+ else
269
+ needs_name_lookup = true
270
+ end
271
+
272
+ if needs_name_lookup && (static_value = STATIC_NAME_LOOKUP[name])
273
+ name_only = static_value
250
274
  end
251
275
  end
252
- if [:all].include?(@index) && !exact
276
+
277
+ if @index == :all && !exact
253
278
  @table.each_index do |i|
254
- if @table[i] == header
255
- exact ||= i + STATIC_TABLE.size
256
- break
257
- elsif @table[i].first == header.first
258
- name_only ||= i + STATIC_TABLE.size
279
+ entry = @table[i]
280
+ if entry.first == name
281
+ if entry.last == value
282
+ exact ||= i + STATIC_TABLE.size
283
+ break
284
+ else
285
+ name_only ||= i + STATIC_TABLE.size
286
+ end
259
287
  end
260
288
  end
261
289
  end
@@ -263,9 +291,9 @@ module Protocol
263
291
  if exact
264
292
  {name: exact, type: :indexed}
265
293
  elsif name_only
266
- {name: name_only, value: header.last, type: :incremental}
294
+ {name: name_only, value: value, type: :incremental}
267
295
  else
268
- {name: header.first, value: header.last, type: :incremental}
296
+ {name: name, value: value, type: :incremental}
269
297
  end
270
298
  end
271
299
 
@@ -2,6 +2,8 @@
2
2
 
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2018-2024, by Samuel Williams.
5
+ # Copyright, 2024, by Maruth Goyal.
6
+ # Copyright, 2024, by Nathan Froyd.
5
7
 
6
8
  require_relative 'context'
7
9
  require_relative 'huffman'
@@ -88,7 +90,7 @@ module Protocol
88
90
 
89
91
  raise CompressionError, "Invalid string length, got #{string.bytesize}, expecting #{length}!" unless string.bytesize == length
90
92
 
91
- string = Huffman.new.decode(string) if huffman
93
+ string = Huffman.decode(string) if huffman
92
94
 
93
95
  return string.force_encoding(Encoding::UTF_8)
94
96
  end
@@ -6,6 +6,7 @@
6
6
  # Copyright, 2015, by Tamir Duberstein.
7
7
  # Copyright, 2018-2024, by Samuel Williams.
8
8
  # Copyright, 2022, by Daniel Morrison.
9
+ # Copyright, 2024, by Nathan Froyd.
9
10
 
10
11
  require_relative 'huffman/machine'
11
12
  require_relative 'error'
@@ -22,7 +23,7 @@ module Protocol
22
23
  #
23
24
  # @param str [String]
24
25
  # @return [String] binary string
25
- def encode(str)
26
+ def self.encode(str)
26
27
  bitstring = str.each_byte.map {|chr| ENCODE_TABLE[chr]}.join
27
28
  bitstring << '1' * ((8 - bitstring.size) % 8)
28
29
  [bitstring].pack('B*')
@@ -33,14 +34,15 @@ module Protocol
33
34
  # @param buf [Buffer]
34
35
  # @return [String] binary string
35
36
  # @raise [CompressionError] when Huffman coded string is malformed
36
- def decode(buffer)
37
+ def self.decode(buffer)
37
38
  emit = String.new.b
38
39
  state = 0 # start state
39
40
 
40
41
  mask = (1 << BITS_AT_ONCE) - 1
41
42
  buffer.each_byte do |c|
42
- (8 / BITS_AT_ONCE - 1).downto(0) do |shift|
43
- branch = (c >> (shift * BITS_AT_ONCE)) & mask
43
+ shift = BITS_AT_ONCE
44
+ while shift >= 0
45
+ branch = (c >> shift) & mask
44
46
 
45
47
  # MACHINE[state] = [final, [transitions]]
46
48
  # [final] unfinished bits so far are prefix of the EOS code.
@@ -52,6 +54,7 @@ module Protocol
52
54
  raise CompressionError, 'Huffman decode error (EOS found)' if value == EOS
53
55
 
54
56
  emit << value.chr if value
57
+ shift -= BITS_AT_ONCE
55
58
  end
56
59
  end
57
60
  # Check whether partial input is correctly filled
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Protocol
7
7
  module HPACK
8
- VERSION = "1.4.3"
8
+ VERSION = "1.5.0"
9
9
  end
10
10
  end
data/license.md CHANGED
@@ -15,6 +15,8 @@ Copyright, 2020, by Olle Jonsson.
15
15
  Copyright, 2020, by Justin Mazzocchi.
16
16
  Copyright, 2022, by Daniel Morrison.
17
17
  Copyright, 2022, by Felix Yan.
18
+ Copyright, 2024, by Maruth Goyal.
19
+ Copyright, 2024, by Nathan Froyd.
18
20
 
19
21
  Permission is hereby granted, free of charge, to any person obtaining a copy
20
22
  of this software and associated documentation files (the "Software"), to deal
data/readme.md CHANGED
@@ -10,6 +10,11 @@ Please see the [project documentation](https://socketry.github.io/protocol-hpack
10
10
 
11
11
  - [Getting Started](https://socketry.github.io/protocol-hpack/guides/getting-started/index) - This guide explains how to use the `protocol-hpack` gem to compress and decompress HTTP/2 headers using HPACK.
12
12
 
13
+ ## See Also
14
+
15
+ - [protocol-http2](https://github.com/socketry/protocol-http2) - Provides an HTTP/2 client and server implementation.
16
+ - [async-http](https://github.com/socketry/async-http) - Provides a complete HTTP client and server implementation on top of Async.
17
+
13
18
  ## Contributing
14
19
 
15
20
  We welcome contributions to this project.
@@ -22,13 +27,8 @@ We welcome contributions to this project.
22
27
 
23
28
  ### Developer Certificate of Origin
24
29
 
25
- This project uses the [Developer Certificate of Origin](https://developercertificate.org/). All contributors to this project must agree to this document to have their contributions accepted.
30
+ In order to protect users of this project, we require all contributors to comply with the [Developer Certificate of Origin](https://developercertificate.org/). This ensures that all contributions are properly licensed and attributed.
26
31
 
27
- ### Contributor Covenant
28
-
29
- This project is governed by the [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms.
30
-
31
- ## See Also
32
+ ### Community Guidelines
32
33
 
33
- - [protocol-http2](https://github.com/socketry/protocol-http2) - Provides an HTTP/2 client and server implementation.
34
- - [async-http](https://github.com/socketry/async-http) - Provides a complete HTTP client and server implementation on top of Async.
34
+ This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
data/tasks/huffman.rb CHANGED
@@ -6,13 +6,14 @@
6
6
  # Copyright, 2015, by Ilya Grigorik.
7
7
  # Copyright, 2016, by George Ulmer.
8
8
  # Copyright, 2018-2024, by Samuel Williams.
9
+ # Copyright, 2024, by Nathan Froyd.
9
10
 
10
- require_relative '../lib/http/hpack/huffman'
11
+ require_relative '../lib/protocol/hpack/huffman'
11
12
 
12
13
  require 'set'
13
14
 
14
15
  module Huffman
15
- BITS_AT_ONCE = HTTP::HPACK::Huffman::BITS_AT_ONCE
16
+ BITS_AT_ONCE = Protocol::HPACK::Huffman::BITS_AT_ONCE
16
17
  EOS = 256
17
18
 
18
19
  class Node
@@ -49,7 +50,7 @@ module Huffman
49
50
 
50
51
  def self.generate_tree
51
52
  @root = new(0)
52
- HTTP::HPACK::Huffman::CODES.each_with_index do |c, chr|
53
+ Protocol::HPACK::Huffman::CODES.each_with_index do |c, chr|
53
54
  code, len = c
54
55
  @root.add(code, len, chr)
55
56
  end
@@ -71,7 +72,7 @@ module Huffman
71
72
 
72
73
  (1 << BITS_AT_ONCE).times do |input|
73
74
  n = node
74
- emit = ''
75
+ emit = +''
75
76
  (BITS_AT_ONCE - 1).downto(0) do |i|
76
77
  bit = (input & (1 << i)).zero? ? 0 : 1
77
78
  n = n.next[bit]
@@ -107,7 +108,7 @@ module Huffman
107
108
  id += 1
108
109
  end
109
110
 
110
- File.open(File.expand_path('../lib/http/hpack/huffman/machine.rb', File.dirname(__FILE__)), 'w') do |f|
111
+ File.open(File.expand_path('../lib/protocol/hpack/huffman/machine.rb', File.dirname(__FILE__)), 'w') do |f|
111
112
  f.print <<HEADER
112
113
  # frozen_string_literal: true
113
114
 
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-hpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.3
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  - Ilya Grigorik
9
9
  - Tamir Duberstein
10
+ - Nathan Froyd
10
11
  - Kaoru Maeda
11
12
  - Tiago Cardoso
12
13
  - Byron Formwalt
@@ -18,6 +19,7 @@ authors:
18
19
  - Justin Mazzocchi
19
20
  - Kenichi Nakamura
20
21
  - Kien Nguyen Trung
22
+ - Maruth Goyal
21
23
  - Olle Jonsson
22
24
  autorequire:
23
25
  bindir: bin
@@ -51,7 +53,7 @@ cert_chain:
51
53
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
52
54
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
53
55
  -----END CERTIFICATE-----
54
- date: 2024-03-12 00:00:00.000000000 Z
56
+ date: 2024-07-28 00:00:00.000000000 Z
55
57
  dependencies: []
56
58
  description:
57
59
  email:
@@ -76,6 +78,7 @@ licenses:
76
78
  - MIT
77
79
  metadata:
78
80
  documentation_uri: https://socketry.github.io/protocol-hpack/
81
+ source_code_uri: https://github.com/socketry/http-hpack.git
79
82
  post_install_message:
80
83
  rdoc_options: []
81
84
  require_paths:
@@ -84,14 +87,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
87
  requirements:
85
88
  - - ">="
86
89
  - !ruby/object:Gem::Version
87
- version: '3.0'
90
+ version: '3.1'
88
91
  required_rubygems_version: !ruby/object:Gem::Requirement
89
92
  requirements:
90
93
  - - ">="
91
94
  - !ruby/object:Gem::Version
92
95
  version: '0'
93
96
  requirements: []
94
- rubygems_version: 3.5.3
97
+ rubygems_version: 3.5.11
95
98
  signing_key:
96
99
  specification_version: 4
97
100
  summary: A compresssor and decompressor for HTTP/2's HPACK format.
metadata.gz.sig CHANGED
Binary file