Skip to content

Commit 283bce9

Browse files
More clean up.
1 parent 8b2afaf commit 283bce9

File tree

20 files changed

+335
-141
lines changed

20 files changed

+335
-141
lines changed

benchmark/array.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
require "sus/fixtures/benchmark"
7+
8+
describe "Array initialization" do
9+
include Sus::Fixtures::Benchmark
10+
11+
let(:source_array) {["value1", "value2", "value3", "value4", "value5"]}
12+
13+
measure "Array.new(array)" do |repeats|
14+
repeats.times do
15+
Array.new(source_array)
16+
end
17+
end
18+
19+
measure "Array.new.concat(array)" do |repeats|
20+
repeats.times do
21+
array = Array.new
22+
array.concat(source_array)
23+
end
24+
end
25+
end
26+

gems.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
gem "rubocop-socketry"
3030

3131
gem "sus-fixtures-async"
32+
gem "sus-fixtures-benchmark"
3233

3334
gem "bake-test"
3435
gem "bake-test-external"

lib/protocol/http/header/accept.rb

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,52 +68,26 @@ def quality_factor
6868
end
6969
end
7070

71-
# Parses a raw header value from the wire.
71+
# Parses a raw header value.
7272
#
73-
# @parameter value [String] the raw header value containing comma-separated media types.
73+
# @parameter value [String] a raw header value containing comma-separated media types.
7474
# @returns [Accept] a new instance containing the parsed media types.
7575
def self.parse(value)
7676
self.new(value.scan(SEPARATOR).map(&:strip))
7777
end
7878

79-
# Coerces a value into a parsed header object.
80-
#
81-
# @parameter value [String | Array] the value to coerce.
82-
# @returns [Accept] a parsed header object.
83-
def self.coerce(value)
84-
case value
85-
when Array
86-
self.new(value.map(&:to_s))
87-
else
88-
self.parse(value.to_s)
89-
end
90-
end
91-
92-
# Initializes an Accept header with already-parsed values.
93-
#
94-
# @parameter value [Array | Nil] an array of parsed media type strings, or `nil` for an empty header.
95-
def initialize(value = nil)
96-
if value.is_a?(Array)
97-
super(value)
98-
elsif value.is_a?(String)
99-
super(value)
100-
elsif value
101-
raise ArgumentError, "Invalid value: #{value.inspect}"
102-
end
103-
end
104-
105-
# Adds one or more comma-separated values to the header from a raw wire-format string.
79+
# Adds one or more comma-separated values to the header.
10680
#
10781
# The input string is split into distinct entries and appended to the array.
10882
#
109-
# @parameter value [String] a raw wire-format value containing one or more media types separated by commas.
83+
# @parameter value [String] a raw header value containing one or more media types separated by commas.
11084
def << value
11185
self.concat(value.scan(SEPARATOR).map(&:strip))
11286
end
11387

114-
# Converts the parsed header value into a raw wire-format string.
88+
# Converts the parsed header value into a raw header value.
11589
#
116-
# @returns [String] a raw wire-format value (comma-separated string) suitable for transmission.
90+
# @returns [String] a raw header value (comma-separated string).
11791
def to_s
11892
join(",")
11993
end

lib/protocol/http/header/authorization.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ module Header
1515
#
1616
# TODO Support other authorization mechanisms, e.g. bearer token.
1717
class Authorization < String
18-
# Parses a raw header value from the wire.
18+
# Parses a raw header value.
1919
#
20-
# @parameter value [String] the raw header value.
20+
# @parameter value [String] a raw header value.
2121
# @returns [Authorization] a new instance.
2222
def self.parse(value)
2323
self.new(value)
@@ -47,8 +47,8 @@ def self.basic(username, password)
4747
strict_base64_encoded = ["#{username}:#{password}"].pack("m0")
4848

4949
self.new(
50-
"Basic #{strict_base64_encoded}"
51-
)
50+
"Basic #{strict_base64_encoded}"
51+
)
5252
end
5353

5454
# Whether this header is acceptable in HTTP trailers.

lib/protocol/http/header/cache_control.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,44 @@ class CacheControl < Split
4444
# The `proxy-revalidate` directive is similar to `must-revalidate` but applies only to shared caches.
4545
PROXY_REVALIDATE = "proxy-revalidate"
4646

47-
# Initializes the cache control header with already-parsed and normalized values.
47+
# Parses a raw header value.
4848
#
49-
# @parameter value [Array | Nil] an array of normalized (lowercase) directives, or `nil` for an empty header.
49+
# @parameter value [String] a raw header value containing comma-separated directives.
50+
# @returns [CacheControl] a new instance containing the parsed and normalized directives.
51+
def self.parse(value)
52+
self.new(value.downcase.split(COMMA))
53+
end
54+
55+
# Coerces a value into a parsed header object.
56+
#
57+
# @parameter value [String | Array] the value to coerce.
58+
# @returns [CacheControl] a parsed header object with normalized values.
59+
def self.coerce(value)
60+
case value
61+
when Array
62+
self.new(value.map(&:downcase))
63+
else
64+
self.parse(value.to_s)
65+
end
66+
end
67+
68+
# Initializes the cache control header with the given values.
69+
#
70+
# @parameter value [Array | String | Nil] an array of directives, a raw header value, or `nil` for an empty header.
5071
def initialize(value = nil)
5172
if value.is_a?(Array)
52-
super(value.map(&:downcase))
73+
super(value)
5374
elsif value.is_a?(String)
54-
# Compatibility with the old constructor, prefer to use `parse` instead:
5575
super()
5676
self << value
5777
elsif value
5878
raise ArgumentError, "Invalid value: #{value.inspect}"
5979
end
6080
end
6181

62-
# Adds a directive to the Cache-Control header from a raw wire-format string. The value will be normalized to lowercase before being added.
82+
# Adds a directive to the Cache-Control header. The value will be normalized to lowercase before being added.
6383
#
64-
# @parameter value [String] a raw wire-format directive to add.
84+
# @parameter value [String] a raw header value containing directives to add.
6585
def << value
6686
super(value.downcase)
6787
end

lib/protocol/http/header/connection.rb

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,44 @@ class Connection < Split
2222
# The `upgrade` directive indicates that the connection should be upgraded to a different protocol, as specified in the `Upgrade` header.
2323
UPGRADE = "upgrade"
2424

25-
# Initializes the connection header with already-parsed and normalized values.
25+
# Parses a raw header value.
2626
#
27-
# @parameter value [Array | Nil] an array of normalized (lowercase) directives, or `nil` for an empty header.
27+
# @parameter value [String] a raw header value containing comma-separated directives.
28+
# @returns [Connection] a new instance with normalized (lowercase) directives.
29+
def self.parse(value)
30+
self.new(value.downcase.split(COMMA))
31+
end
32+
33+
# Coerces a value into a parsed header object.
34+
#
35+
# @parameter value [String | Array] the value to coerce.
36+
# @returns [Connection] a parsed header object with normalized values.
37+
def self.coerce(value)
38+
case value
39+
when Array
40+
self.new(value.map(&:downcase))
41+
else
42+
self.parse(value.to_s)
43+
end
44+
end
45+
46+
# Initializes the connection header with the given values.
47+
#
48+
# @parameter value [Array | String | Nil] an array of directives, a raw header value, or `nil` for an empty header.
2849
def initialize(value = nil)
2950
if value.is_a?(Array)
30-
super(value.map(&:downcase))
31-
elsif value.is_a?(String)
32-
# Compatibility with the old constructor, prefer to use `parse` instead:
3351
super(value)
52+
elsif value.is_a?(String)
53+
super()
54+
self << value
3455
elsif value
3556
raise ArgumentError, "Invalid value: #{value.inspect}"
3657
end
3758
end
3859

39-
# Adds a directive to the `connection` header from a raw wire-format string. The value will be normalized to lowercase before being added.
60+
# Adds a directive to the `connection` header. The value will be normalized to lowercase before being added.
4061
#
41-
# @parameter value [String] a raw wire-format directive to add.
62+
# @parameter value [String] a raw header value containing directives to add.
4263
def << value
4364
super(value.downcase)
4465
end

lib/protocol/http/header/date.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ module Header
1212
#
1313
# This header is typically included in HTTP responses and follows the format defined in RFC 9110.
1414
class Date < String
15-
# Parses a raw header value from the wire.
15+
# Parses a raw header value.
1616
#
17-
# @parameter value [String] the raw header value.
17+
# @parameter value [String] a raw header value.
1818
# @returns [Date] a new instance.
1919
def self.parse(value)
2020
self.new(value)
@@ -28,9 +28,9 @@ def self.coerce(value)
2828
self.new(value.to_s)
2929
end
3030

31-
# Replaces the current value of the `date` header with a raw wire-format string.
31+
# Replaces the current value of the `date` header.
3232
#
33-
# @parameter value [String] a raw wire-format value for the `date` header.
33+
# @parameter value [String] a raw header value for the `date` header.
3434
def << value
3535
replace(value)
3636
end

lib/protocol/http/header/etag.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ module Header
1010
#
1111
# The `etag` header provides a unique identifier for a specific version of a resource, typically used for cache validation or conditional requests. It can be either a strong or weak validator as defined in RFC 9110.
1212
class ETag < String
13-
# Parses a raw header value from the wire.
13+
# Parses a raw header value.
1414
#
15-
# @parameter value [String] the raw header value.
15+
# @parameter value [String] a raw header value.
1616
# @returns [ETag] a new instance.
1717
def self.parse(value)
1818
self.new(value)
@@ -26,9 +26,9 @@ def self.coerce(value)
2626
self.new(value.to_s)
2727
end
2828

29-
# Replaces the current value of the `etag` header with a raw wire-format string.
29+
# Replaces the current value of the `etag` header.
3030
#
31-
# @parameter value [String] a raw wire-format value for the `etag` header.
31+
# @parameter value [String] a raw header value for the `etag` header.
3232
def << value
3333
replace(value)
3434
end

lib/protocol/http/header/multiple.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ module Header
1010
#
1111
# This isn't a specific header but is used as a base for headers that store multiple values, such as cookies. The values are split and stored as an array internally, and serialized back to a newline-separated string when needed.
1212
class Multiple < Array
13-
# Parses a raw header value from the wire.
13+
# Parses a raw header value.
1414
#
15-
# Multiple headers receive each value as a separate header entry on the wire, so this method takes a single string value and creates a new instance containing it.
15+
# Multiple headers receive each value as a separate header entry, so this method takes a single string value and creates a new instance containing it.
1616
#
17-
# @parameter value [String] a single raw header value from the wire.
17+
# @parameter value [String] a single raw header value.
1818
# @returns [Multiple] a new instance containing the parsed value.
1919
def self.parse(value)
2020
self.new([value])
@@ -46,11 +46,11 @@ def initialize(value = nil)
4646
end
4747
end
4848

49-
# Converts the parsed header value into a raw wire-format string.
49+
# Converts the parsed header value into a raw header value.
5050
#
51-
# Multiple headers are transmitted as separate header entries on the wire, so this serializes to a newline-separated string for storage.
51+
# Multiple headers are transmitted as separate header entries, so this serializes to a newline-separated string for storage.
5252
#
53-
# @returns [String] a raw wire-format value (newline-separated string).
53+
# @returns [String] a raw header value (newline-separated string).
5454
def to_s
5555
join("\n")
5656
end

lib/protocol/http/header/priority.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,44 @@ module Header
1212
#
1313
# The `priority` header allows clients to express their preference for how resources should be prioritized by the server. It supports directives like `u=` to specify the urgency level of a request, and `i` to indicate whether a response can be delivered incrementally. The urgency levels range from 0 (highest priority) to 7 (lowest priority), while the `i` directive is a boolean flag.
1414
class Priority < Split
15-
# Initializes the priority header with already-parsed and normalized values.
15+
# Parses a raw header value.
1616
#
17-
# @parameter value [Array | Nil] an array of normalized (lowercase) directives, or `nil` for an empty header.
17+
# @parameter value [String] a raw header value containing comma-separated directives.
18+
# @returns [Priority] a new instance with normalized (lowercase) directives.
19+
def self.parse(value)
20+
self.new(value.downcase.split(COMMA))
21+
end
22+
23+
# Coerces a value into a parsed header object.
24+
#
25+
# @parameter value [String | Array] the value to coerce.
26+
# @returns [Priority] a parsed header object with normalized values.
27+
def self.coerce(value)
28+
case value
29+
when Array
30+
self.new(value.map(&:downcase))
31+
else
32+
self.parse(value.to_s)
33+
end
34+
end
35+
36+
# Initializes the priority header with the given values.
37+
#
38+
# @parameter value [Array | String | Nil] an array of directives, a raw header value, or `nil` for an empty header.
1839
def initialize(value = nil)
1940
if value.is_a?(Array)
20-
super(value.map(&:downcase))
41+
super(value)
2142
elsif value.is_a?(String)
22-
# Compatibility with the old constructor, prefer to use `parse` instead:
2343
super()
2444
self << value
2545
elsif value
2646
raise ArgumentError, "Invalid value: #{value.inspect}"
2747
end
2848
end
2949

30-
# Add a value to the priority header from a raw wire-format string.
50+
# Add a value to the priority header.
3151
#
32-
# @parameter value [String] a raw wire-format directive to add to the header.
52+
# @parameter value [String] a raw header value containing directives to add to the header.
3353
def << value
3454
super(value.downcase)
3555
end

0 commit comments

Comments
 (0)