mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-03-25 08:55:53 +00:00
Cookie#cookie_value too should quote values if necessary.
This commit is contained in:
parent
2dc17ab7dc
commit
ef7bdeefba
3 changed files with 13 additions and 3 deletions
|
|
@ -545,7 +545,7 @@ class HTTP::Cookie
|
||||||
# Returns a string for use in a Cookie header value,
|
# Returns a string for use in a Cookie header value,
|
||||||
# i.e. "name=value".
|
# i.e. "name=value".
|
||||||
def cookie_value
|
def cookie_value
|
||||||
"#{@name}=#{@value}"
|
"#{@name}=#{Scanner.quote(@value)}"
|
||||||
end
|
end
|
||||||
alias to_s cookie_value
|
alias to_s cookie_value
|
||||||
|
|
||||||
|
|
@ -559,7 +559,7 @@ class HTTP::Cookie
|
||||||
origin = origin ? URI(origin) : @origin or
|
origin = origin ? URI(origin) : @origin or
|
||||||
raise "origin must be specified to produce a value for Set-Cookie"
|
raise "origin must be specified to produce a value for Set-Cookie"
|
||||||
|
|
||||||
string = "#{@name}=#{Scanner.quote(@value)}"
|
string = cookie_value
|
||||||
if @for_domain
|
if @for_domain
|
||||||
string << "; Domain=#{@domain}"
|
string << "; Domain=#{@domain}"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ class HTTP::Cookie::Scanner < StringScanner
|
||||||
class << self
|
class << self
|
||||||
def quote(s)
|
def quote(s)
|
||||||
return s unless s.match(RE_BAD_CHAR)
|
return s unless s.match(RE_BAD_CHAR)
|
||||||
'"' << s.gsub(RE_BAD_CHAR, "\\\\\\1") << '"'
|
'"' << s.gsub(/([\\"])/, "\\\\\\1") << '"'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -390,6 +390,16 @@ class TestHTTPCookie < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_cookie_value
|
||||||
|
[
|
||||||
|
['foo="bar baz"', 'bar baz'],
|
||||||
|
['foo="bar\"; \"baz"', 'bar"; "baz'],
|
||||||
|
].each { |cookie_value, value|
|
||||||
|
cookie = HTTP::Cookie.new('foo', value)
|
||||||
|
assert_equal(cookie_value, cookie.cookie_value)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def test_set_cookie_value
|
def test_set_cookie_value
|
||||||
url = URI.parse('http://rubyforge.org/')
|
url = URI.parse('http://rubyforge.org/')
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue