Skip to content

Commit 3d4c9b7

Browse files
committed
Fix parsing of accept values.
1 parent 3b14d08 commit 3d4c9b7

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/protocol/http/header/accept.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Header
1414
# The `accept-content-type` header represents a list of content-types that the client can accept.
1515
class Accept < Array
1616
# Regular expression used to split values on commas, with optional surrounding whitespace, taking into account quoted strings.
17-
SPLIT = /
17+
SEPARATOR = /
1818
(?: # Start non-capturing group
1919
"[^"\\]*" # Match quoted strings (no escaping of quotes within)
2020
| # OR
@@ -76,7 +76,7 @@ def split(*args)
7676

7777
def initialize(value = nil)
7878
if value
79-
super(value.scan(SPLIT).map(&:strip))
79+
super(value.scan(SEPARATOR).map(&:strip))
8080
else
8181
end
8282
end
@@ -87,7 +87,7 @@ def initialize(value = nil)
8787
#
8888
# @parameter value [String] the value or values to add, separated by commas.
8989
def << (value)
90-
self.concat(value.scan(SPLIT).map(&:strip))
90+
self.concat(value.scan(SEPARATOR).map(&:strip))
9191
end
9292

9393
# Serializes the stored values into a comma-separated string.
@@ -115,7 +115,11 @@ def parse_media_range(value)
115115
parameters = {}
116116

117117
match[:parameters].scan(PARAMETER) do |key, value, quoted_value|
118-
parameters[key] = quoted_value || value
118+
if quoted_value
119+
value = QuotedString.unquote(quoted_value)
120+
end
121+
122+
parameters[key] = value
119123
end
120124

121125
return MediaRange.new(type, subtype, parameters)

test/protocol/http/header/accept.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,12 @@
5151
end
5252
end
5353

54-
54+
with "text/html;schema=\"example.org\";q=0.5" do
55+
it "should parse parameters" do
56+
expect(media_ranges[0].parameters).to have_keys(
57+
"schema" => be == "example.org",
58+
"q" => be == "0.5",
59+
)
60+
end
61+
end
5562
end

0 commit comments

Comments
 (0)