From ea64aaa005a43ccd7631b1eb318eb99084608a3b Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 9 Jan 2025 11:56:13 +1300 Subject: [PATCH] Fix parsing of accept values. --- lib/protocol/http/header/accept.rb | 12 ++++++++---- test/protocol/http/header/accept.rb | 9 ++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/protocol/http/header/accept.rb b/lib/protocol/http/header/accept.rb index 414f87f..e6bb42d 100644 --- a/lib/protocol/http/header/accept.rb +++ b/lib/protocol/http/header/accept.rb @@ -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 @@ -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 @@ -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. @@ -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) diff --git a/test/protocol/http/header/accept.rb b/test/protocol/http/header/accept.rb index 9e3c000..f4ad946 100644 --- a/test/protocol/http/header/accept.rb +++ b/test/protocol/http/header/accept.rb @@ -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