Skip to content

Commit 50fedbf

Browse files
Separate SetCookie header policy
Assisted-By: devx/d2b0744c-2a13-4062-bbb7-b404acb1c0f5
1 parent 5e18de3 commit 50fedbf

5 files changed

Lines changed: 81 additions & 33 deletions

File tree

‎lib/protocol/http/header/cookie.rb‎

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Released under the MIT License.
44
# Copyright, 2019-2026, by Samuel Williams.
55

6-
require_relative "multiple"
76
require_relative "../cookie"
87

98
module 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
9873
end
74+
75+
# Preserve the existing behavior where requiring this file also defines SetCookie.
76+
require_relative "set_cookie"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2019-2026, by Samuel Williams.
5+
6+
require_relative "multiple"
7+
require_relative "../cookie"
8+
9+
module Protocol
10+
module HTTP
11+
module Header
12+
# The `set-cookie` header sends cookies from the server to the user agent.
13+
#
14+
# Each `Set-Cookie` header must be a separate header field — they cannot be combined.
15+
# It is used to store cookies on the client side, which are then sent back to the server
16+
# in subsequent requests using the `cookie` header.
17+
class SetCookie < Multiple
18+
# Parses the `set-cookie` headers into a hash of cookie names and their corresponding cookie objects.
19+
#
20+
# @returns [Hash(String, HTTP::Cookie)] a hash where keys are cookie names and values are {HTTP::Cookie} objects.
21+
def to_h
22+
cookies = self.collect do |string|
23+
HTTP::Cookie.parse(string)
24+
end
25+
26+
cookies.map{|cookie| [cookie.name, cookie]}.to_h
27+
end
28+
29+
# Whether this header is acceptable in HTTP trailers.
30+
# @returns [Boolean] `false`, as set-cookie headers are needed during initial response processing.
31+
def self.trailer?
32+
false
33+
end
34+
end
35+
end
36+
end
37+
end

‎lib/protocol/http/headers.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require_relative "header/multiple"
1010

1111
require_relative "header/cookie"
12+
require_relative "header/set_cookie"
1213
require_relative "header/connection"
1314
require_relative "header/cache_control"
1415
require_relative "header/etag"
@@ -299,7 +300,7 @@ def []=(key, value)
299300
if @indexed
300301
@indexed[key] = value
301302
end
302-
303+
303304
if value.is_a?(Multiple)
304305
value.each do |v|
305306
@fields << [key, v.to_s]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2026, by Samuel Williams.
5+
6+
require "protocol/http/header/set_cookie"
7+
8+
describe Protocol::HTTP::Header::SetCookie do
9+
let(:header) do
10+
subject.coerce([
11+
"session=abc123; Path=/",
12+
"theme=dark; HttpOnly",
13+
])
14+
end
15+
16+
it "represents values which must be emitted as separate fields" do
17+
expect(header).to be_a(Protocol::HTTP::Header::Multiple)
18+
expect(header).to be == ["session=abc123; Path=/", "theme=dark; HttpOnly"]
19+
end
20+
21+
it "can extract parsed cookies" do
22+
expect(header.to_h).to have_keys("session", "theme")
23+
end
24+
end

‎test/protocol/http/headers.rb‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@
212212
expect(headers["accept-encoding"]).to be(:include?, "deflate")
213213
end
214214

215+
it "joins multiple cookie values into one field" do
216+
headers["cookie"] = ["session=abc123", "theme=dark"]
217+
218+
expect(headers.fields.select{|key, value| key == "cookie"}).to be == [
219+
["cookie", "session=abc123; theme=dark"],
220+
]
221+
end
222+
215223
it "adds multiple set-cookie values as separate fields" do
216224
headers["set-cookie"] = ["session=abc123; Path=/", "theme=dark; HttpOnly"]
217225

0 commit comments

Comments
 (0)