33# Released under the MIT License.
44# Copyright, 2019-2026, by Samuel Williams.
55
6- require_relative "multiple"
76require_relative "../cookie"
87
98module Protocol
@@ -21,7 +20,7 @@ class Cookie < Array
2120 def self . parse ( value )
2221 self . new ( [ value ] )
2322 end
24-
23+
2524 # Coerces a value into a parsed header object.
2625 #
2726 # @parameter value [String | Array] the value to coerce.
@@ -34,65 +33,44 @@ def self.coerce(value)
3433 self . parse ( value . to_s )
3534 end
3635 end
37-
36+
3837 # Initializes the cookie header with the given values.
3938 #
4039 # @parameter value [Array | Nil] an array of cookie strings, or `nil` for an empty header.
4140 def initialize ( value = nil )
4241 super ( )
43-
42+
4443 if value
4544 self . concat ( value )
4645 end
4746 end
48-
47+
4948 # Parses the `cookie` header into a hash of cookie names and their corresponding cookie objects.
5049 #
5150 # @returns [Hash(String, HTTP::Cookie)] a hash where keys are cookie names and values are {HTTP::Cookie} objects.
5251 def to_h
5352 cookies = self . collect do |string |
5453 HTTP ::Cookie . parse ( string )
5554 end
56-
55+
5756 cookies . map { |cookie | [ cookie . name , cookie ] } . to_h
5857 end
59-
58+
6059 # Serializes the `cookie` header by joining individual cookie strings with `"; "` per RFC 6265.
6160 def to_s
6261 join ( "; " )
6362 end
64-
63+
6564 # Whether this header is acceptable in HTTP trailers.
6665 # Cookie headers should not appear in trailers as they contain state information needed early in processing.
6766 # @returns [Boolean] `false`, as cookie headers are needed during initial request processing.
6867 def self . trailer?
6968 false
7069 end
7170 end
72-
73- # The `set-cookie` header sends cookies from the server to the user agent.
74- #
75- # Each `Set-Cookie` header must be a separate header field — they cannot be combined.
76- # It is used to store cookies on the client side, which are then sent back to the server
77- # in subsequent requests using the `cookie` header.
78- class SetCookie < Multiple
79- # Parses the `set-cookie` headers into a hash of cookie names and their corresponding cookie objects.
80- #
81- # @returns [Hash(String, HTTP::Cookie)] a hash where keys are cookie names and values are {HTTP::Cookie} objects.
82- def to_h
83- cookies = self . collect do |string |
84- HTTP ::Cookie . parse ( string )
85- end
86-
87- cookies . map { |cookie | [ cookie . name , cookie ] } . to_h
88- end
89-
90- # Whether this header is acceptable in HTTP trailers.
91- # @returns [Boolean] `false`, as set-cookie headers are needed during initial response processing.
92- def self . trailer?
93- false
94- end
95- end
9671 end
9772 end
9873end
74+
75+ # Preserve the existing behavior where requiring this file also defines SetCookie.
76+ require_relative "set_cookie"
0 commit comments