lib/http/accept/quoted_string.rb in http-accept-1.0.1 vs lib/http/accept/quoted_string.rb in http-accept-1.1.0
- old
+ new
@@ -22,32 +22,23 @@
module Accept
# According to https://tools.ietf.org/html/rfc7231#appendix-C
TOKEN = /[!#$%&'*+\-.^_`|~0-9A-Z]+/i
QUOTED_STRING = /"(?:.(?!(?<!\\)"))*.?"/
- class QuotedString
- def initialize(value)
- @value = value
- end
-
- def unquote(normalize_whitespace = false)
- value = @value[1...-1]
+ module QuotedString
+ # Unquote a "quoted-string" value according to https://tools.ietf.org/html/rfc7230#section-3.2.6
+ # It should already match the QUOTED_STRING pattern above by the parser.
+ def self.unquote(value, normalize_whitespace = true)
+ value = value[1...-1]
value.gsub!(/\\(.)/, '\1')
if normalize_whitespace
- value.gsub!(/[\r\n]\s+/, ' ')
+ # LWS = [CRLF] 1*( SP | HT )
+ value.gsub!(/[\r\n]+\s+/, ' ')
end
return value
- end
-
- def to_str
- unquote(true)
- end
-
- def to_s
- to_str
end
end
end
end