Treat an empty path as '/'.

HTTP::CookieJar#cookies: Do not modify a given URI.  Remove a
redundant cleanup() call.
This commit is contained in:
Akinori MUSHA 2012-10-22 13:08:28 +09:00
parent 24e9b26004
commit fa45e028a7
3 changed files with 21 additions and 5 deletions

View file

@ -287,6 +287,22 @@ class HTTP::Cookie
@domain = @domain_name.hostname @domain = @domain_name.hostname
end end
def normalize_uri_path(uri)
# Currently does not replace // to /
uri.path.empty? ? uri + '/' : uri
end
private :normalize_uri_path
def normalize_path(path)
# Currently does not replace // to /
path.empty? ? '/' : path
end
private :normalize_path
def path=(path)
@path = normalize_path(path)
end
def origin=(origin) def origin=(origin)
@origin.nil? or @origin.nil? or
raise ArgumentError, "origin cannot be changed once it is set" raise ArgumentError, "origin cannot be changed once it is set"
@ -294,7 +310,7 @@ class HTTP::Cookie
acceptable_from_uri?(origin) or acceptable_from_uri?(origin) or
raise ArgumentError, "unacceptable cookie sent from URI #{origin}" raise ArgumentError, "unacceptable cookie sent from URI #{origin}"
self.domain ||= origin.host self.domain ||= origin.host
self.path ||= (origin + './').path self.path ||= (normalize_uri_path(origin) + './').path
@origin = origin @origin = origin
end end
@ -337,7 +353,7 @@ class HTTP::Cookie
raise "cannot tell if this cookie is valid because the domain is unknown" raise "cannot tell if this cookie is valid because the domain is unknown"
end end
return false if secure? && uri.scheme != 'https' return false if secure? && uri.scheme != 'https'
acceptable_from_uri?(uri) && uri.path.start_with?(@path) acceptable_from_uri?(uri) && normalize_path(uri.path).start_with?(@path)
end end
def to_s def to_s

View file

@ -33,10 +33,7 @@ class HTTP::CookieJar
# Fetch the cookies that should be used for the URI object passed in. # Fetch the cookies that should be used for the URI object passed in.
def cookies(url) def cookies(url)
cleanup
url.path = '/' if url.path.empty?
now = Time.now now = Time.now
select { |cookie| select { |cookie|
!cookie.expired? && cookie.valid_for_uri?(url) && (cookie.accessed_at = now) !cookie.expired? && cookie.valid_for_uri?(url) && (cookie.accessed_at = now)
}.sort }.sort

View file

@ -607,6 +607,9 @@ class TestHTTPCookie < Test::Unit::TestCase
assert_equal false, cookie.valid_for_uri?(URI('http://example.com/dir/test.html')) assert_equal false, cookie.valid_for_uri?(URI('http://example.com/dir/test.html'))
assert_equal false, cookie.valid_for_uri?(URI('https://example.com/dir2/test.html')) assert_equal false, cookie.valid_for_uri?(URI('https://example.com/dir2/test.html'))
assert_equal false, cookie.valid_for_uri?(URI('http://example.com/dir2/test.html')) assert_equal false, cookie.valid_for_uri?(URI('http://example.com/dir2/test.html'))
cookie = HTTP::Cookie.parse('a=b', :origin => URI('https://example.com/')).first
assert_equal true, cookie.valid_for_uri?(URI('https://example.com'))
end end
end end