protocol-http 0.55.0 → 0.56.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +2 -3
- data/lib/protocol/http/body/stream.rb +1 -1
- data/lib/protocol/http/header/accept.rb +9 -10
- data/lib/protocol/http/header/authorization.rb +18 -2
- data/lib/protocol/http/header/cache_control.rb +33 -4
- data/lib/protocol/http/header/connection.rb +33 -4
- data/lib/protocol/http/header/date.rb +18 -2
- data/lib/protocol/http/header/etag.rb +18 -2
- data/lib/protocol/http/header/etags.rb +1 -1
- data/lib/protocol/http/header/multiple.rb +35 -6
- data/lib/protocol/http/header/priority.rb +33 -4
- data/lib/protocol/http/header/split.rb +38 -9
- data/lib/protocol/http/header/te.rb +33 -4
- data/lib/protocol/http/header/transfer_encoding.rb +33 -4
- data/lib/protocol/http/header/vary.rb +34 -5
- data/lib/protocol/http/headers.rb +48 -15
- data/lib/protocol/http/version.rb +1 -1
- data/readme.md +8 -4
- data/releases.md +8 -0
- data.tar.gz.sig +0 -0
- metadata +2 -2
- metadata.gz.sig +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5409468cd5dfe153d3ab2ecc8fae839ff0a688e0d54a81aa402395e6b3de0f1e
|
|
4
|
+
data.tar.gz: 11d700efbe0ecc425438a605f09643bba5bdde1e258bb0508e34428285b209f3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 90bf1b95afe9e830249676ab82cba8306c2184dfac7b105dff9c068eb67484f5002b73ec5141a1f42177042a4016c83a118a875df36f30ab65c8aa4b9b1ed2fb
|
|
7
|
+
data.tar.gz: 43ce8ee9a1ace9ff2ae0be4d9ae90625415a504fad5162ebf7e8b1f487bab25bae0aa452260b7cd3fe00193c53835c80f730a1ca16ac142c90f827c852119497
|
checksums.yaml.gz.sig
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
H
|
|
2
|
-
|
|
3
|
-
y�������^Je8�6O�����O��iH6�6쐐���Hʸ��6P���_.��z�qX���O�v�T��އ��ɲ>���xv쓼�~���n�\XD�(3
|
|
1
|
+
'�5uS�e�T���̯�?r̳���n�n��&��@�T��H�$�)ʣb�o�[�Y�����]br*=�ҵ[�O{R]
|
|
2
|
+
<Ǧ9�y���@�.A5}�F}�贿Xryc%��=�p�@�>��T�V���@��N�i~���¶��šϘD�lH��ў����L���T��]wv>�O;Jo���
|
|
@@ -10,7 +10,7 @@ require_relative "buffered"
|
|
|
10
10
|
module Protocol
|
|
11
11
|
module HTTP
|
|
12
12
|
module Body
|
|
13
|
-
# The input stream is an IO-like object which contains the raw HTTP POST data. When applicable, its external encoding must be
|
|
13
|
+
# The input stream is an IO-like object which contains the raw HTTP POST data. When applicable, its external encoding must be "ASCII-8BIT" and it must be opened in binary mode, for Ruby 1.9 compatibility. The input stream must respond to gets, each, read and rewind.
|
|
14
14
|
class Stream
|
|
15
15
|
# The default line separator, used by {gets}.
|
|
16
16
|
NEWLINE = "\n"
|
|
@@ -12,7 +12,7 @@ module Protocol
|
|
|
12
12
|
module HTTP
|
|
13
13
|
module Header
|
|
14
14
|
# The `accept-content-type` header represents a list of content-types that the client can accept.
|
|
15
|
-
class Accept <
|
|
15
|
+
class Accept < Split
|
|
16
16
|
# Regular expression used to split values on commas, with optional surrounding whitespace, taking into account quoted strings.
|
|
17
17
|
SEPARATOR = /
|
|
18
18
|
(?: # Start non-capturing group
|
|
@@ -68,27 +68,26 @@ module Protocol
|
|
|
68
68
|
end
|
|
69
69
|
end
|
|
70
70
|
|
|
71
|
-
#
|
|
71
|
+
# Parses a raw header value.
|
|
72
72
|
#
|
|
73
|
-
# @parameter value [String]
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
end
|
|
73
|
+
# @parameter value [String] a raw header value containing comma-separated media types.
|
|
74
|
+
# @returns [Accept] a new instance containing the parsed media types.
|
|
75
|
+
def self.parse(value)
|
|
76
|
+
self.new(value.scan(SEPARATOR).map(&:strip))
|
|
78
77
|
end
|
|
79
78
|
|
|
80
79
|
# Adds one or more comma-separated values to the header.
|
|
81
80
|
#
|
|
82
81
|
# The input string is split into distinct entries and appended to the array.
|
|
83
82
|
#
|
|
84
|
-
# @parameter value [String]
|
|
83
|
+
# @parameter value [String] a raw header value containing one or more media types separated by commas.
|
|
85
84
|
def << value
|
|
86
85
|
self.concat(value.scan(SEPARATOR).map(&:strip))
|
|
87
86
|
end
|
|
88
87
|
|
|
89
|
-
#
|
|
88
|
+
# Converts the parsed header value into a raw header value.
|
|
90
89
|
#
|
|
91
|
-
# @returns [String]
|
|
90
|
+
# @returns [String] a raw header value (comma-separated string).
|
|
92
91
|
def to_s
|
|
93
92
|
join(",")
|
|
94
93
|
end
|
|
@@ -15,6 +15,22 @@ module Protocol
|
|
|
15
15
|
#
|
|
16
16
|
# TODO Support other authorization mechanisms, e.g. bearer token.
|
|
17
17
|
class Authorization < String
|
|
18
|
+
# Parses a raw header value.
|
|
19
|
+
#
|
|
20
|
+
# @parameter value [String] a raw header value.
|
|
21
|
+
# @returns [Authorization] a new instance.
|
|
22
|
+
def self.parse(value)
|
|
23
|
+
self.new(value)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Coerces a value into a parsed header object.
|
|
27
|
+
#
|
|
28
|
+
# @parameter value [String] the value to coerce.
|
|
29
|
+
# @returns [Authorization] a parsed header object.
|
|
30
|
+
def self.coerce(value)
|
|
31
|
+
self.new(value.to_s)
|
|
32
|
+
end
|
|
33
|
+
|
|
18
34
|
# Splits the header into the credentials.
|
|
19
35
|
#
|
|
20
36
|
# @returns [Tuple(String, String)] The username and password.
|
|
@@ -31,8 +47,8 @@ module Protocol
|
|
|
31
47
|
strict_base64_encoded = ["#{username}:#{password}"].pack("m0")
|
|
32
48
|
|
|
33
49
|
self.new(
|
|
34
|
-
|
|
35
|
-
|
|
50
|
+
"Basic #{strict_base64_encoded}"
|
|
51
|
+
)
|
|
36
52
|
end
|
|
37
53
|
|
|
38
54
|
# Whether this header is acceptable in HTTP trailers.
|
|
@@ -44,16 +44,44 @@ module Protocol
|
|
|
44
44
|
# The `proxy-revalidate` directive is similar to `must-revalidate` but applies only to shared caches.
|
|
45
45
|
PROXY_REVALIDATE = "proxy-revalidate"
|
|
46
46
|
|
|
47
|
-
#
|
|
47
|
+
# Parses a raw header value.
|
|
48
48
|
#
|
|
49
|
-
# @parameter value [String
|
|
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.
|
|
50
71
|
def initialize(value = nil)
|
|
51
|
-
|
|
72
|
+
if value.is_a?(Array)
|
|
73
|
+
super(value)
|
|
74
|
+
elsif value.is_a?(String)
|
|
75
|
+
super()
|
|
76
|
+
self << value
|
|
77
|
+
elsif value
|
|
78
|
+
raise ArgumentError, "Invalid value: #{value.inspect}"
|
|
79
|
+
end
|
|
52
80
|
end
|
|
53
81
|
|
|
54
82
|
# Adds a directive to the Cache-Control header. The value will be normalized to lowercase before being added.
|
|
55
83
|
#
|
|
56
|
-
# @parameter value [String]
|
|
84
|
+
# @parameter value [String] a raw header value containing directives to add.
|
|
57
85
|
def << value
|
|
58
86
|
super(value.downcase)
|
|
59
87
|
end
|
|
@@ -132,3 +160,4 @@ module Protocol
|
|
|
132
160
|
end
|
|
133
161
|
end
|
|
134
162
|
end
|
|
163
|
+
|
|
@@ -22,16 +22,44 @@ module Protocol
|
|
|
22
22
|
# The `upgrade` directive indicates that the connection should be upgraded to a different protocol, as specified in the `Upgrade` header.
|
|
23
23
|
UPGRADE = "upgrade"
|
|
24
24
|
|
|
25
|
-
#
|
|
25
|
+
# Parses a raw header value.
|
|
26
26
|
#
|
|
27
|
-
# @parameter value [String
|
|
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.
|
|
28
49
|
def initialize(value = nil)
|
|
29
|
-
|
|
50
|
+
if value.is_a?(Array)
|
|
51
|
+
super(value)
|
|
52
|
+
elsif value.is_a?(String)
|
|
53
|
+
super()
|
|
54
|
+
self << value
|
|
55
|
+
elsif value
|
|
56
|
+
raise ArgumentError, "Invalid value: #{value.inspect}"
|
|
57
|
+
end
|
|
30
58
|
end
|
|
31
59
|
|
|
32
60
|
# Adds a directive to the `connection` header. The value will be normalized to lowercase before being added.
|
|
33
61
|
#
|
|
34
|
-
# @parameter value [String]
|
|
62
|
+
# @parameter value [String] a raw header value containing directives to add.
|
|
35
63
|
def << value
|
|
36
64
|
super(value.downcase)
|
|
37
65
|
end
|
|
@@ -61,3 +89,4 @@ module Protocol
|
|
|
61
89
|
end
|
|
62
90
|
end
|
|
63
91
|
end
|
|
92
|
+
|
|
@@ -12,9 +12,25 @@ module Protocol
|
|
|
12
12
|
#
|
|
13
13
|
# This header is typically included in HTTP responses and follows the format defined in RFC 9110.
|
|
14
14
|
class Date < String
|
|
15
|
-
#
|
|
15
|
+
# Parses a raw header value.
|
|
16
16
|
#
|
|
17
|
-
# @parameter value [String]
|
|
17
|
+
# @parameter value [String] a raw header value.
|
|
18
|
+
# @returns [Date] a new instance.
|
|
19
|
+
def self.parse(value)
|
|
20
|
+
self.new(value)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Coerces a value into a parsed header object.
|
|
24
|
+
#
|
|
25
|
+
# @parameter value [String] the value to coerce.
|
|
26
|
+
# @returns [Date] a parsed header object.
|
|
27
|
+
def self.coerce(value)
|
|
28
|
+
self.new(value.to_s)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Replaces the current value of the `date` header.
|
|
32
|
+
#
|
|
33
|
+
# @parameter value [String] a raw header value for the `date` header.
|
|
18
34
|
def << value
|
|
19
35
|
replace(value)
|
|
20
36
|
end
|
|
@@ -10,9 +10,25 @@ module Protocol
|
|
|
10
10
|
#
|
|
11
11
|
# 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.
|
|
12
12
|
class ETag < String
|
|
13
|
-
#
|
|
13
|
+
# Parses a raw header value.
|
|
14
14
|
#
|
|
15
|
-
# @parameter value [String]
|
|
15
|
+
# @parameter value [String] a raw header value.
|
|
16
|
+
# @returns [ETag] a new instance.
|
|
17
|
+
def self.parse(value)
|
|
18
|
+
self.new(value)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Coerces a value into a parsed header object.
|
|
22
|
+
#
|
|
23
|
+
# @parameter value [String] the value to coerce.
|
|
24
|
+
# @returns [ETag] a parsed header object.
|
|
25
|
+
def self.coerce(value)
|
|
26
|
+
self.new(value.to_s)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Replaces the current value of the `etag` header.
|
|
30
|
+
#
|
|
31
|
+
# @parameter value [String] a raw header value for the `etag` header.
|
|
16
32
|
def << value
|
|
17
33
|
replace(value)
|
|
18
34
|
end
|
|
@@ -10,18 +10,47 @@ module Protocol
|
|
|
10
10
|
#
|
|
11
11
|
# 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.
|
|
12
12
|
class Multiple < Array
|
|
13
|
-
#
|
|
13
|
+
# Parses a raw header value.
|
|
14
14
|
#
|
|
15
|
-
#
|
|
16
|
-
|
|
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.
|
|
16
|
+
#
|
|
17
|
+
# @parameter value [String] a single raw header value.
|
|
18
|
+
# @returns [Multiple] a new instance containing the parsed value.
|
|
19
|
+
def self.parse(value)
|
|
20
|
+
self.new([value])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Coerces a value into a parsed header object.
|
|
24
|
+
#
|
|
25
|
+
# This method is used by the Headers class when setting values via `[]=` to convert application values into the appropriate policy type.
|
|
26
|
+
#
|
|
27
|
+
# @parameter value [String | Array] the value to coerce.
|
|
28
|
+
# @returns [Multiple] a parsed header object.
|
|
29
|
+
def self.coerce(value)
|
|
30
|
+
case value
|
|
31
|
+
when Array
|
|
32
|
+
self.new(value.map(&:to_s))
|
|
33
|
+
else
|
|
34
|
+
self.parse(value.to_s)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Initializes the multiple header with the given values.
|
|
39
|
+
#
|
|
40
|
+
# @parameter value [Array | Nil] an array of header values, or `nil` for an empty header.
|
|
41
|
+
def initialize(value = nil)
|
|
17
42
|
super()
|
|
18
43
|
|
|
19
|
-
|
|
44
|
+
if value
|
|
45
|
+
self.concat(value)
|
|
46
|
+
end
|
|
20
47
|
end
|
|
21
48
|
|
|
22
|
-
#
|
|
49
|
+
# Converts the parsed header value into a raw header value.
|
|
50
|
+
#
|
|
51
|
+
# Multiple headers are transmitted as separate header entries, so this serializes to a newline-separated string for storage.
|
|
23
52
|
#
|
|
24
|
-
# @returns [String]
|
|
53
|
+
# @returns [String] a raw header value (newline-separated string).
|
|
25
54
|
def to_s
|
|
26
55
|
join("\n")
|
|
27
56
|
end
|
|
@@ -12,16 +12,44 @@ module Protocol
|
|
|
12
12
|
#
|
|
13
13
|
# 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.
|
|
14
14
|
class Priority < Split
|
|
15
|
-
#
|
|
15
|
+
# Parses a raw header value.
|
|
16
16
|
#
|
|
17
|
-
# @parameter value [String
|
|
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.
|
|
18
39
|
def initialize(value = nil)
|
|
19
|
-
|
|
40
|
+
if value.is_a?(Array)
|
|
41
|
+
super(value)
|
|
42
|
+
elsif value.is_a?(String)
|
|
43
|
+
super()
|
|
44
|
+
self << value
|
|
45
|
+
elsif value
|
|
46
|
+
raise ArgumentError, "Invalid value: #{value.inspect}"
|
|
47
|
+
end
|
|
20
48
|
end
|
|
21
49
|
|
|
22
50
|
# Add a value to the priority header.
|
|
23
51
|
#
|
|
24
|
-
# @parameter value [String]
|
|
52
|
+
# @parameter value [String] a raw header value containing directives to add to the header.
|
|
25
53
|
def << value
|
|
26
54
|
super(value.downcase)
|
|
27
55
|
end
|
|
@@ -55,3 +83,4 @@ module Protocol
|
|
|
55
83
|
end
|
|
56
84
|
end
|
|
57
85
|
end
|
|
86
|
+
|
|
@@ -13,14 +13,43 @@ module Protocol
|
|
|
13
13
|
# Regular expression used to split values on commas, with optional surrounding whitespace.
|
|
14
14
|
COMMA = /\s*,\s*/
|
|
15
15
|
|
|
16
|
-
#
|
|
16
|
+
# Parses a raw header value.
|
|
17
17
|
#
|
|
18
|
-
#
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
# Split headers receive comma-separated values in a single header entry. This method splits the raw value into individual entries.
|
|
19
|
+
#
|
|
20
|
+
# @parameter value [String] a raw header value containing multiple entries separated by commas.
|
|
21
|
+
# @returns [Split] a new instance containing the parsed values.
|
|
22
|
+
def self.parse(value)
|
|
23
|
+
self.new(value.split(COMMA))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Coerces a value into a parsed header object.
|
|
27
|
+
#
|
|
28
|
+
# This method is used by the Headers class when setting values via `[]=` to convert application values into the appropriate policy type.
|
|
29
|
+
#
|
|
30
|
+
# @parameter value [String | Array] the value to coerce.
|
|
31
|
+
# @returns [Split] a parsed header object.
|
|
32
|
+
def self.coerce(value)
|
|
33
|
+
case value
|
|
34
|
+
when Array
|
|
35
|
+
self.new(value.map(&:to_s))
|
|
22
36
|
else
|
|
37
|
+
self.parse(value.to_s)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Initializes a `Split` header with the given values.
|
|
42
|
+
#
|
|
43
|
+
# @parameter value [Array | String | Nil] an array of values, a raw header value, or `nil` for an empty header.
|
|
44
|
+
def initialize(value = nil)
|
|
45
|
+
if value.is_a?(Array)
|
|
46
|
+
super(value)
|
|
47
|
+
elsif value.is_a?(String)
|
|
48
|
+
# Compatibility with the old constructor, prefer to use `parse` instead:
|
|
23
49
|
super()
|
|
50
|
+
self << value
|
|
51
|
+
elsif value
|
|
52
|
+
raise ArgumentError, "Invalid value: #{value.inspect}"
|
|
24
53
|
end
|
|
25
54
|
end
|
|
26
55
|
|
|
@@ -28,14 +57,14 @@ module Protocol
|
|
|
28
57
|
#
|
|
29
58
|
# The input string is split into distinct entries and appended to the array.
|
|
30
59
|
#
|
|
31
|
-
# @parameter value [String]
|
|
60
|
+
# @parameter value [String] a raw header value containing one or more values separated by commas.
|
|
32
61
|
def << value
|
|
33
62
|
self.concat(value.split(COMMA))
|
|
34
63
|
end
|
|
35
64
|
|
|
36
|
-
#
|
|
65
|
+
# Converts the parsed header value into a raw header value.
|
|
37
66
|
#
|
|
38
|
-
# @returns [String]
|
|
67
|
+
# @returns [String] a raw header value (comma-separated string).
|
|
39
68
|
def to_s
|
|
40
69
|
join(",")
|
|
41
70
|
end
|
|
@@ -47,7 +76,7 @@ module Protocol
|
|
|
47
76
|
false
|
|
48
77
|
end
|
|
49
78
|
|
|
50
|
-
|
|
79
|
+
protected
|
|
51
80
|
|
|
52
81
|
def reverse_find(&block)
|
|
53
82
|
reverse_each do |value|
|
|
@@ -62,16 +62,44 @@ module Protocol
|
|
|
62
62
|
end
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
#
|
|
65
|
+
# Parses a raw header value.
|
|
66
66
|
#
|
|
67
|
-
# @parameter value [String
|
|
67
|
+
# @parameter value [String] a raw header value containing comma-separated encodings.
|
|
68
|
+
# @returns [TE] a new instance with normalized (lowercase) encodings.
|
|
69
|
+
def self.parse(value)
|
|
70
|
+
self.new(value.downcase.split(COMMA))
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Coerces a value into a parsed header object.
|
|
74
|
+
#
|
|
75
|
+
# @parameter value [String | Array] the value to coerce.
|
|
76
|
+
# @returns [TE] a parsed header object with normalized values.
|
|
77
|
+
def self.coerce(value)
|
|
78
|
+
case value
|
|
79
|
+
when Array
|
|
80
|
+
self.new(value.map(&:downcase))
|
|
81
|
+
else
|
|
82
|
+
self.parse(value.to_s)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Initializes the TE header with the given values.
|
|
87
|
+
#
|
|
88
|
+
# @parameter value [Array | String | Nil] an array of encodings, a raw header value, or `nil` for an empty header.
|
|
68
89
|
def initialize(value = nil)
|
|
69
|
-
|
|
90
|
+
if value.is_a?(Array)
|
|
91
|
+
super(value)
|
|
92
|
+
elsif value.is_a?(String)
|
|
93
|
+
super()
|
|
94
|
+
self << value
|
|
95
|
+
elsif value
|
|
96
|
+
raise ArgumentError, "Invalid value: #{value.inspect}"
|
|
97
|
+
end
|
|
70
98
|
end
|
|
71
99
|
|
|
72
100
|
# Adds one or more comma-separated values to the TE header. The values are converted to lowercase for normalization.
|
|
73
101
|
#
|
|
74
|
-
# @parameter value [String]
|
|
102
|
+
# @parameter value [String] a raw header value containing one or more values separated by commas.
|
|
75
103
|
def << value
|
|
76
104
|
super(value.downcase)
|
|
77
105
|
end
|
|
@@ -129,3 +157,4 @@ module Protocol
|
|
|
129
157
|
end
|
|
130
158
|
end
|
|
131
159
|
end
|
|
160
|
+
|
|
@@ -27,16 +27,44 @@ module Protocol
|
|
|
27
27
|
# The `identity` transfer encoding indicates no transformation has been applied.
|
|
28
28
|
IDENTITY = "identity"
|
|
29
29
|
|
|
30
|
-
#
|
|
30
|
+
# Parses a raw header value.
|
|
31
31
|
#
|
|
32
|
-
# @parameter value [String
|
|
32
|
+
# @parameter value [String] a raw header value containing comma-separated encodings.
|
|
33
|
+
# @returns [TransferEncoding] a new instance with normalized (lowercase) encodings.
|
|
34
|
+
def self.parse(value)
|
|
35
|
+
self.new(value.downcase.split(COMMA))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Coerces a value into a parsed header object.
|
|
39
|
+
#
|
|
40
|
+
# @parameter value [String | Array] the value to coerce.
|
|
41
|
+
# @returns [TransferEncoding] a parsed header object with normalized values.
|
|
42
|
+
def self.coerce(value)
|
|
43
|
+
case value
|
|
44
|
+
when Array
|
|
45
|
+
self.new(value.map(&:downcase))
|
|
46
|
+
else
|
|
47
|
+
self.parse(value.to_s)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Initializes the transfer encoding header with the given values.
|
|
52
|
+
#
|
|
53
|
+
# @parameter value [Array | String | Nil] an array of encodings, a raw header value, or `nil` for an empty header.
|
|
33
54
|
def initialize(value = nil)
|
|
34
|
-
|
|
55
|
+
if value.is_a?(Array)
|
|
56
|
+
super(value)
|
|
57
|
+
elsif value.is_a?(String)
|
|
58
|
+
super()
|
|
59
|
+
self << value
|
|
60
|
+
elsif value
|
|
61
|
+
raise ArgumentError, "Invalid value: #{value.inspect}"
|
|
62
|
+
end
|
|
35
63
|
end
|
|
36
64
|
|
|
37
65
|
# Adds one or more comma-separated values to the transfer encoding header. The values are converted to lowercase for normalization.
|
|
38
66
|
#
|
|
39
|
-
# @parameter value [String]
|
|
67
|
+
# @parameter value [String] a raw header value containing one or more values separated by commas.
|
|
40
68
|
def << value
|
|
41
69
|
super(value.downcase)
|
|
42
70
|
end
|
|
@@ -76,3 +104,4 @@ module Protocol
|
|
|
76
104
|
end
|
|
77
105
|
end
|
|
78
106
|
end
|
|
107
|
+
|
|
@@ -12,16 +12,44 @@ module Protocol
|
|
|
12
12
|
#
|
|
13
13
|
# The `vary` header is used in HTTP responses to indicate which request headers affect the selected response. It allows caches to differentiate stored responses based on specific request headers.
|
|
14
14
|
class Vary < Split
|
|
15
|
-
#
|
|
15
|
+
# Parses a raw header value.
|
|
16
16
|
#
|
|
17
|
-
# @parameter value [String]
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
# @parameter value [String] a raw header value containing comma-separated header names.
|
|
18
|
+
# @returns [Vary] a new instance with normalized (lowercase) header names.
|
|
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 [Vary] 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 a `Vary` header with the given values.
|
|
37
|
+
#
|
|
38
|
+
# @parameter value [Array | String | Nil] an array of header names, a raw header value, or `nil` for an empty header.
|
|
39
|
+
def initialize(value = nil)
|
|
40
|
+
if value.is_a?(Array)
|
|
41
|
+
super(value)
|
|
42
|
+
elsif value.is_a?(String)
|
|
43
|
+
super()
|
|
44
|
+
self << value
|
|
45
|
+
elsif value
|
|
46
|
+
raise ArgumentError, "Invalid value: #{value.inspect}"
|
|
47
|
+
end
|
|
20
48
|
end
|
|
21
49
|
|
|
22
50
|
# Adds one or more comma-separated values to the `vary` header. The values are converted to lowercase for normalization.
|
|
23
51
|
#
|
|
24
|
-
# @parameter value [String]
|
|
52
|
+
# @parameter value [String] a raw header value containing one or more values separated by commas.
|
|
25
53
|
def << value
|
|
26
54
|
super(value.downcase)
|
|
27
55
|
end
|
|
@@ -29,3 +57,4 @@ module Protocol
|
|
|
29
57
|
end
|
|
30
58
|
end
|
|
31
59
|
end
|
|
60
|
+
|
|
@@ -189,7 +189,7 @@ module Protocol
|
|
|
189
189
|
#
|
|
190
190
|
# @yields {|key, value| ...}
|
|
191
191
|
# @parameter key [String] The header key.
|
|
192
|
-
# @parameter value [String] The header value.
|
|
192
|
+
# @parameter value [String] The raw header value.
|
|
193
193
|
def each(&block)
|
|
194
194
|
@fields.each(&block)
|
|
195
195
|
end
|
|
@@ -228,7 +228,6 @@ module Protocol
|
|
|
228
228
|
# @parameter key [String] the header key.
|
|
229
229
|
# @parameter value [String] the header value to assign.
|
|
230
230
|
def add(key, value)
|
|
231
|
-
# The value MUST be a string, so we convert it to a string to prevent errors later on.
|
|
232
231
|
value = value.to_s
|
|
233
232
|
|
|
234
233
|
if @indexed
|
|
@@ -238,18 +237,51 @@ module Protocol
|
|
|
238
237
|
@fields << [key, value]
|
|
239
238
|
end
|
|
240
239
|
|
|
241
|
-
alias []= add
|
|
242
|
-
|
|
243
240
|
# Set the specified header key to the specified value, replacing any existing header keys with the same name.
|
|
244
241
|
#
|
|
245
242
|
# @parameter key [String] the header key to replace.
|
|
246
243
|
# @parameter value [String] the header value to assign.
|
|
247
244
|
def set(key, value)
|
|
248
|
-
# TODO This could be a bit more efficient:
|
|
249
245
|
self.delete(key)
|
|
250
246
|
self.add(key, value)
|
|
251
247
|
end
|
|
252
248
|
|
|
249
|
+
# Set the specified header key to the specified value, replacing any existing values.
|
|
250
|
+
#
|
|
251
|
+
# The value can be a String or a coercable value.
|
|
252
|
+
#
|
|
253
|
+
# @parameter key [String] the header key.
|
|
254
|
+
# @parameter value [String | Array] the header value to assign.
|
|
255
|
+
def []=(key, value)
|
|
256
|
+
key = key.downcase
|
|
257
|
+
|
|
258
|
+
# Delete existing value if any:
|
|
259
|
+
self.delete(key)
|
|
260
|
+
|
|
261
|
+
if policy = @policy[key]
|
|
262
|
+
unless value.is_a?(policy)
|
|
263
|
+
value = policy.coerce(value)
|
|
264
|
+
end
|
|
265
|
+
else
|
|
266
|
+
value = value.to_s
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# Clear the indexed cache so it will be rebuilt with parsed values when accessed:
|
|
270
|
+
if @indexed
|
|
271
|
+
@indexed[key] = value
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
@fields << [key, value.to_s]
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# Get the value of the specified header key.
|
|
278
|
+
#
|
|
279
|
+
# @parameter key [String] The header key.
|
|
280
|
+
# @returns [String | Array | Object] The header value.
|
|
281
|
+
def [] key
|
|
282
|
+
to_h[key]
|
|
283
|
+
end
|
|
284
|
+
|
|
253
285
|
# Merge the headers into this instance.
|
|
254
286
|
def merge!(headers)
|
|
255
287
|
headers.each do |key, value|
|
|
@@ -338,6 +370,11 @@ module Protocol
|
|
|
338
370
|
# @parameter key [String] The header key.
|
|
339
371
|
# @returns [String | Array | Object] The merged header value.
|
|
340
372
|
def delete(key)
|
|
373
|
+
# If we've indexed the headers, we can bail out early if the key is not present:
|
|
374
|
+
if @indexed && !@indexed.key?(key.downcase)
|
|
375
|
+
return nil
|
|
376
|
+
end
|
|
377
|
+
|
|
341
378
|
deleted, @fields = @fields.partition do |field|
|
|
342
379
|
field.first.downcase == key
|
|
343
380
|
end
|
|
@@ -350,7 +387,7 @@ module Protocol
|
|
|
350
387
|
return @indexed.delete(key)
|
|
351
388
|
elsif policy = @policy[key]
|
|
352
389
|
(key, value), *tail = deleted
|
|
353
|
-
merged = policy.
|
|
390
|
+
merged = policy.parse(value)
|
|
354
391
|
|
|
355
392
|
tail.each{|k,v| merged << v}
|
|
356
393
|
|
|
@@ -376,7 +413,11 @@ module Protocol
|
|
|
376
413
|
if current_value = hash[key]
|
|
377
414
|
current_value << value
|
|
378
415
|
else
|
|
379
|
-
|
|
416
|
+
if policy.respond_to?(:parse)
|
|
417
|
+
hash[key] = policy.parse(value)
|
|
418
|
+
else
|
|
419
|
+
hash[key] = policy.new(value)
|
|
420
|
+
end
|
|
380
421
|
end
|
|
381
422
|
else
|
|
382
423
|
# By default, headers are not allowed in trailers:
|
|
@@ -392,14 +433,6 @@ module Protocol
|
|
|
392
433
|
end
|
|
393
434
|
end
|
|
394
435
|
|
|
395
|
-
# Get the value of the specified header key.
|
|
396
|
-
#
|
|
397
|
-
# @parameter key [String] The header key.
|
|
398
|
-
# @returns [String | Array | Object] The header value.
|
|
399
|
-
def [] key
|
|
400
|
-
to_h[key]
|
|
401
|
-
end
|
|
402
|
-
|
|
403
436
|
# Compute a hash table of headers, where the keys are normalized to lower case and the values are normalized according to the policy for that header.
|
|
404
437
|
#
|
|
405
438
|
# @returns [Hash] A hash table of `{key, value}` pairs.
|
data/readme.md
CHANGED
|
@@ -30,6 +30,14 @@ Please see the [project documentation](https://socketry.github.io/protocol-http/
|
|
|
30
30
|
|
|
31
31
|
Please see the [project releases](https://socketry.github.io/protocol-http/releases/index) for all releases.
|
|
32
32
|
|
|
33
|
+
### v0.56.0
|
|
34
|
+
|
|
35
|
+
- Introduce `Header::*.parse(value)` which parses a raw header value string into a header instance.
|
|
36
|
+
- Introduce `Header::*.coerce(value)` which coerces any value (`String`, `Array`, etc.) into a header instance with normalization.
|
|
37
|
+
- `Header::*#initialize` now accepts arrays without normalization for efficiency, or strings for backward compatibility.
|
|
38
|
+
- Update `Headers#[]=` to use `coerce(value)` for smart conversion of user input.
|
|
39
|
+
- Normalization (e.g., lowercasing) is applied by `parse`, `coerce`, and `<<` methods, but not by `new` when given arrays.
|
|
40
|
+
|
|
33
41
|
### v0.55.0
|
|
34
42
|
|
|
35
43
|
- **Breaking**: Move `Protocol::HTTP::Header::QuotedString` to `Protocol::HTTP::QuotedString` for better reusability.
|
|
@@ -78,10 +86,6 @@ Please see the [project releases](https://socketry.github.io/protocol-http/relea
|
|
|
78
86
|
- Clarify behaviour of streaming bodies and copy `Protocol::Rack::Body::Streaming` to `Protocol::HTTP::Body::Streamable`.
|
|
79
87
|
- Copy `Async::HTTP::Body::Writable` to `Protocol::HTTP::Body::Writable`.
|
|
80
88
|
|
|
81
|
-
### v0.31.0
|
|
82
|
-
|
|
83
|
-
- Ensure chunks are flushed if required, when streaming.
|
|
84
|
-
|
|
85
89
|
## See Also
|
|
86
90
|
|
|
87
91
|
- [protocol-http1](https://github.com/socketry/protocol-http1) — HTTP/1 client/server implementation using this
|
data/releases.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Releases
|
|
2
2
|
|
|
3
|
+
## v0.56.0
|
|
4
|
+
|
|
5
|
+
- Introduce `Header::*.parse(value)` which parses a raw header value string into a header instance.
|
|
6
|
+
- Introduce `Header::*.coerce(value)` which coerces any value (`String`, `Array`, etc.) into a header instance with normalization.
|
|
7
|
+
- `Header::*#initialize` now accepts arrays without normalization for efficiency, or strings for backward compatibility.
|
|
8
|
+
- Update `Headers#[]=` to use `coerce(value)` for smart conversion of user input.
|
|
9
|
+
- Normalization (e.g., lowercasing) is applied by `parse`, `coerce`, and `<<` methods, but not by `new` when given arrays.
|
|
10
|
+
|
|
3
11
|
## v0.55.0
|
|
4
12
|
|
|
5
13
|
- **Breaking**: Move `Protocol::HTTP::Header::QuotedString` to `Protocol::HTTP::QuotedString` for better reusability.
|
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: protocol-http
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.56.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Samuel Williams
|
|
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
134
134
|
- !ruby/object:Gem::Version
|
|
135
135
|
version: '0'
|
|
136
136
|
requirements: []
|
|
137
|
-
rubygems_version: 3.
|
|
137
|
+
rubygems_version: 3.6.9
|
|
138
138
|
specification_version: 4
|
|
139
139
|
summary: Provides abstractions to handle HTTP protocols.
|
|
140
140
|
test_files: []
|
metadata.gz.sig
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
�R�}��k�Q�y*���lk�J�C�ߏ�A�]vQ=/-w��ݷ%��L9��C���#��5w��g���d^�x�xt����(=� s�ιrX�����gڳ
|
|
2
|
+
�T�N9�o
|
|
3
|
+
�!t�Dϗ�Wv��-���z
|
|
4
|
+
��zQY����*}���|��U�:��5v� U��C�z���,Aċ�s�t���=����F���A��t�v>&
|