33# Released under the MIT License.
44# Copyright, 2019-2026, by Samuel Williams.
55
6- require_relative "multiple"
76require_relative "../cookie"
87
98module Protocol
@@ -12,7 +11,40 @@ module Header
1211 # The `cookie` header contains stored HTTP cookies previously sent by the server with the `set-cookie` header.
1312 #
1413 # It is used by clients to send key-value pairs representing stored cookies back to the server.
15- class Cookie < Multiple
14+ # Multiple cookies within a single `Cookie` header are joined with `"; "` per RFC 6265.
15+ class Cookie < Array
16+ # Parses a raw header value.
17+ #
18+ # @parameter value [String] a single raw header value.
19+ # @returns [Cookie] a new instance containing the parsed value.
20+ def self . parse ( value )
21+ self . new ( [ value ] )
22+ end
23+
24+ # Coerces a value into a parsed header object.
25+ #
26+ # @parameter value [String | Array] the value to coerce.
27+ # @returns [Cookie] a parsed header object.
28+ def self . coerce ( value )
29+ case value
30+ when Array
31+ self . new ( value . map ( &:to_s ) )
32+ else
33+ self . parse ( value . to_s )
34+ end
35+ end
36+
37+ # Initializes the cookie header with the given values.
38+ #
39+ # @parameter value [Array | Nil] an array of cookie strings, or `nil` for an empty header.
40+ def initialize ( value = nil )
41+ super ( )
42+
43+ if value
44+ self . concat ( value )
45+ end
46+ end
47+
1648 # Parses the `cookie` header into a hash of cookie names and their corresponding cookie objects.
1749 #
1850 # @returns [Hash(String, HTTP::Cookie)] a hash where keys are cookie names and values are {HTTP::Cookie} objects.
@@ -24,9 +56,9 @@ def to_h
2456 cookies . map { |cookie | [ cookie . name , cookie ] } . to_h
2557 end
2658
27- # Serializes the `cookie` header by joining individual cookie strings with semicolons .
59+ # Serializes the `cookie` header by joining individual cookie strings with `"; "` per RFC 6265 .
2860 def to_s
29- join ( ";" )
61+ join ( "; " )
3062 end
3163
3264 # Whether this header is acceptable in HTTP trailers.
@@ -36,12 +68,9 @@ def self.trailer?
3668 false
3769 end
3870 end
39-
40- # The `set-cookie` header sends cookies from the server to the user agent.
41- #
42- # It is used to store cookies on the client side, which are then sent back to the server in subsequent requests using the `cookie` header.
43- class SetCookie < Cookie
44- end
4571 end
4672 end
4773end
74+
75+ # Preserve the existing behavior where requiring this file also defines SetCookie.
76+ require_relative "set_cookie"
0 commit comments