Skip to content

Commit

Permalink
Fix parsing of accept values.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jan 8, 2025
1 parent 3b14d08 commit 3d4c9b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/protocol/http/header/accept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Header
# The `accept-content-type` header represents a list of content-types that the client can accept.
class Accept < Array
# Regular expression used to split values on commas, with optional surrounding whitespace, taking into account quoted strings.
SPLIT = /
SEPARATOR = /
(?: # Start non-capturing group
"[^"\\]*" # Match quoted strings (no escaping of quotes within)
| # OR
Expand Down Expand Up @@ -76,7 +76,7 @@ def split(*args)

def initialize(value = nil)
if value
super(value.scan(SPLIT).map(&:strip))
super(value.scan(SEPARATOR).map(&:strip))
else
end
end
Expand All @@ -87,7 +87,7 @@ def initialize(value = nil)
#
# @parameter value [String] the value or values to add, separated by commas.
def << (value)
self.concat(value.scan(SPLIT).map(&:strip))
self.concat(value.scan(SEPARATOR).map(&:strip))
end

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

match[:parameters].scan(PARAMETER) do |key, value, quoted_value|
parameters[key] = quoted_value || value
if quoted_value
value = QuotedString.unquote(quoted_value)
end

parameters[key] = value
end

return MediaRange.new(type, subtype, parameters)
Expand Down
9 changes: 8 additions & 1 deletion test/protocol/http/header/accept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,12 @@
end
end


with "text/html;schema=\"example.org\";q=0.5" do
it "should parse parameters" do
expect(media_ranges[0].parameters).to have_keys(
"schema" => be == "example.org",
"q" => be == "0.5",
)
end
end
end

0 comments on commit 3d4c9b7

Please sign in to comment.