mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-03-25 08:55:53 +00:00
Cookie#domain=: Fix handling of an empty string and let it accept nil.
This commit is contained in:
parent
ffabb614ad
commit
7dba33bd40
2 changed files with 37 additions and 4 deletions
|
|
@ -361,15 +361,28 @@ class HTTP::Cookie
|
||||||
|
|
||||||
# See #domain.
|
# See #domain.
|
||||||
def domain=(domain)
|
def domain=(domain)
|
||||||
if DomainName === domain
|
case domain
|
||||||
|
when nil
|
||||||
|
@for_domain = false
|
||||||
|
if @origin
|
||||||
|
@domain_name = DomainName.new(@origin.host)
|
||||||
|
@domain = @domain_name.hostname
|
||||||
|
else
|
||||||
|
@domain_name = @domain = nil
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
when DomainName
|
||||||
@domain_name = domain
|
@domain_name = domain
|
||||||
else
|
else
|
||||||
domain = check_string_type(domain) or
|
domain = check_string_type(domain) or
|
||||||
raise TypeError, "#{domain.class} is not a String"
|
raise TypeError, "#{domain.class} is not a String"
|
||||||
if domain.start_with?('.')
|
if domain.start_with?('.')
|
||||||
@for_domain = true
|
for_domain = true
|
||||||
domain = domain[1..-1]
|
domain = domain[1..-1]
|
||||||
end
|
end
|
||||||
|
if domain.empty?
|
||||||
|
return self.domain = nil
|
||||||
|
end
|
||||||
# Do we really need to support this?
|
# Do we really need to support this?
|
||||||
if domain.match(/\A([^:]+):[0-9]+\z/)
|
if domain.match(/\A([^:]+):[0-9]+\z/)
|
||||||
domain = $1
|
domain = $1
|
||||||
|
|
@ -377,8 +390,10 @@ class HTTP::Cookie
|
||||||
@domain_name = DomainName.new(domain)
|
@domain_name = DomainName.new(domain)
|
||||||
end
|
end
|
||||||
# RFC 6265 5.3 5.
|
# RFC 6265 5.3 5.
|
||||||
if @domain_name.domain.nil? # a public suffix or IP address
|
if domain_name.domain.nil? # a public suffix or IP address
|
||||||
@for_domain = false
|
@for_domain = false
|
||||||
|
else
|
||||||
|
@for_domain = for_domain unless for_domain.nil?
|
||||||
end
|
end
|
||||||
@domain = @domain_name.hostname
|
@domain = @domain_name.hostname
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -610,7 +610,7 @@ class TestHTTPCookie < Test::Unit::TestCase
|
||||||
HTTP::Cookie.new(cookie_values(:value => 'bar')))
|
HTTP::Cookie.new(cookie_values(:value => 'bar')))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_new_rejects_cookies_that_do_not_contain_an_embedded_dot
|
def test_new_tld_domain
|
||||||
url = URI 'http://rubyforge.org/'
|
url = URI 'http://rubyforge.org/'
|
||||||
|
|
||||||
tld_cookie1 = HTTP::Cookie.new(cookie_values(:domain => 'org', :origin => url))
|
tld_cookie1 = HTTP::Cookie.new(cookie_values(:domain => 'org', :origin => url))
|
||||||
|
|
@ -707,6 +707,24 @@ class TestHTTPCookie < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
assert 'example.com', cookie.domain
|
assert 'example.com', cookie.domain
|
||||||
|
|
||||||
|
url = URI 'http://rubyforge.org/'
|
||||||
|
|
||||||
|
[nil, '', '.'].each { |d|
|
||||||
|
cookie = HTTP::Cookie.new('Foo', 'Bar', :path => '/')
|
||||||
|
cookie.domain = d
|
||||||
|
assert_equal nil, cookie.domain, "domain=#{d.inspect}"
|
||||||
|
assert_equal nil, cookie.domain_name, "domain=#{d.inspect}"
|
||||||
|
assert_raises(ArgumentError) {
|
||||||
|
cookie.acceptable?
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie = HTTP::Cookie.new('Foo', 'Bar', :path => '/')
|
||||||
|
cookie.origin = url
|
||||||
|
cookie.domain = d
|
||||||
|
assert_equal url.host, cookie.domain, "domain=#{d.inspect}"
|
||||||
|
assert_equal true, cookie.acceptable?, "domain=#{d.inspect}"
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_origin=
|
def test_origin=
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue