A relative path must be treated as the root path as per RFC 6265 5.1.4.

This commit is contained in:
Akinori MUSHA 2013-03-19 22:54:05 +09:00
parent 28eabedcd3
commit cc6780a5bc
2 changed files with 19 additions and 12 deletions

View file

@ -137,17 +137,15 @@ class HTTP::Cookie
alias for_domain? for_domain alias for_domain? for_domain
class << self class << self
# Normalizes a given path. If it is empty, the root path '/' is # Normalizes a given path. If it is empty or it is a relative
# returned. If a URI object is given, returns a new URI object # path, the root path '/' is returned.
# with the path part normalized. #
def normalize_path(uri) # If a URI object is given, returns a new URI object with the path
# Currently does not replace // to / # part normalized.
case uri def normalize_path(path)
when URI return path + normalize_path(path.path) if URI === path
uri.path.empty? ? uri + '/' : uri # RFC 6265 5.1.4
else path.start_with?('/') ? path : '/'
uri.empty? ? '/' : uri
end
end end
# Parses a Set-Cookie header value +set_cookie+ into an array of # Parses a Set-Cookie header value +set_cookie+ into an array of

View file

@ -248,12 +248,14 @@ class TestHTTPCookie < Test::Unit::TestCase
"no_path1=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT, no_expires=nope; Path=/, " \ "no_path1=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT, no_expires=nope; Path=/, " \
"no_path2=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path, " \ "no_path2=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path, " \
"no_path3=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path=, " \ "no_path3=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path=, " \
"rel_path1=rel_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path=foo/bar, " \
"rel_path1=rel_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path=foo, " \
"no_domain1=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope, " \ "no_domain1=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope, " \
"no_domain2=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope; Domain, " \ "no_domain2=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope; Domain, " \
"no_domain3=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope; Domain=" "no_domain3=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope; Domain="
cookies = HTTP::Cookie.parse cookie_str, :origin => url cookies = HTTP::Cookie.parse cookie_str, :origin => url
assert_equal 13, cookies.length assert_equal 15, cookies.length
name = cookies.find { |c| c.name == 'name' } name = cookies.find { |c| c.name == 'name' }
assert_equal "Aaron", name.value assert_equal "Aaron", name.value
@ -277,6 +279,13 @@ class TestHTTPCookie < Test::Unit::TestCase
assert_equal Time.at(1320539392), c.expires, c.name assert_equal Time.at(1320539392), c.expires, c.name
} }
rel_path_cookies = cookies.select { |c| c.value == 'rel_path' }
assert_equal 2, rel_path_cookies.size
rel_path_cookies.each { |c|
assert_equal "/", c.path, c.name
assert_equal Time.at(1320539392), c.expires, c.name
}
no_domain_cookies = cookies.select { |c| c.value == 'no_domain' } no_domain_cookies = cookies.select { |c| c.value == 'no_domain' }
assert_equal 3, no_domain_cookies.size assert_equal 3, no_domain_cookies.size
no_domain_cookies.each { |c| no_domain_cookies.each { |c|