Preserve quotes when parsing server cookie #11#15
Preserve quotes when parsing server cookie #11#15lacostej wants to merge 3 commits intosparklemotion:masterfrom
Conversation
We keep track of the original quoted value when it is already quoted, and we avoid consider values to be quoted if they start with a quote.
|
update I can solve it in both cases if I also expose the new @raw_value field |
…_value (and original quoting)
…t field initialization time, which could cause unexepected results on ruby 1.8.7
|
This may be a necessary change, as I observed that most popular browsers do not consider double quotes at all. |
|
I realize this PR is old, but in case it is still being considered: would it make more sense to modify the FWIW, the way I patched it in my project is: module HTTP
class Cookie
class Scanner
# https://github.com/sparklemotion/http-cookie/blob/master/lib/http/cookie/scanner.rb#L24
def self.quote(str)
return str unless str.match(RE_BAD_CHAR)
# Because double quotes are being added to the beginning and end of the return value,
# let's go ahead and remove them if the string already begins AND ends in double quotes
unquoted = str.sub(/^[\\"](.*)[\\"]$/, '\\1')
'"' + unquoted.gsub(/([\\"])/, '\\\\\\1') + '"'
end
end
end
end |
|
This is how it works for me, mixed with @tylerj 's snippet module RespectDoubleQuotedCookieValue
def self.prepended(klass)
klass.singleton_class.prepend(ClassMethods)
end
module ClassMethods
def quote(str)
return str unless str.match(HTTP::Cookie::Scanner::RE_BAD_CHAR)
# Because double quotes are being added to the beginning and end of the return value,
# let's go ahead and remove them if the string already begins AND ends in double quotes
unquoted = str.sub(/^[\\"](.*)[\\"]$/, '\\1')
"\"#{unquoted.gsub(/([\\"])/, '\\\\\\1')}\""
end
end
def scan_dquoted
unquoted =
[].tap do |s|
case # rubocop:disable Style/EmptyCaseCondition
when skip(/"/)
break
when skip(/\\/)
s << getch
when scan(/[^"\\]+/)
s << matched
end until eos?
end.join
"\"#{unquoted}\""
end
end
HTTP::Cookie::Scanner.prepend(RespectDoubleQuotedCookieValue) |
We keep track of the original quoted value when it is already quoted,
and we avoid consider values to be quoted if they start with a quote.