Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge multiple cookie headers, preserving semantic correctness. #130

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/webrick/httprequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def content_type
def [](header_name)
if @header
value = @header[header_name.downcase]
value.empty? ? nil : value.join(", ")
value.empty? ? nil : value.join
end
end

Expand All @@ -329,7 +329,7 @@ def each
if @header
@header.each{|k, v|
value = @header[k]
yield(k, value.empty? ? nil : value.join(", "))
yield(k, value.empty? ? nil : value.join)
}
end
end
Expand Down
18 changes: 17 additions & 1 deletion lib/webrick/httputils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ def mime_type(filename, mime_tab)
# Parses an HTTP header +raw+ into a hash of header fields with an Array
# of values.

class SplitHeader < Array
def join(separator = ", ")
super
end
end

class CookieHeader < Array
def join(separator = "; ")
super
end
end

HEADER_CLASSES = Hash.new(SplitHeader).update({
"cookie" => CookieHeader,
})

def parse_header(raw)
header = Hash.new([].freeze)
field = nil
Expand All @@ -160,7 +176,7 @@ def parse_header(raw)
when /^([A-Za-z0-9!\#$%&'*+\-.^_`|~]+):(.*?)\z/om
field, value = $1, $2.strip
field.downcase!
header[field] = [] unless header.has_key?(field)
header[field] = HEADER_CLASSES[field].new unless header.has_key?(field)
header[field] << value
when /^\s+(.*?)/om
value = line.strip
Expand Down
7 changes: 7 additions & 0 deletions test/webrick/test_httprequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,11 @@ def test_eof_raised_when_line_is_nil
req.parse(StringIO.new(""))
}
end

def test_cookie_join
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new("GET / HTTP/1.1\r\ncookie: a=1\r\ncookie: b=2\r\n\r\n"))
assert_equal 2, req.cookies.length
assert_equal 'a=1; b=2', req['cookie']
end
end