Add support for the HttpOnly attribute.

New methods are added to HTTP::Cookie: httponly?, httponly=
This commit is contained in:
Akinori MUSHA 2013-03-12 00:22:42 +09:00
parent 2af7ffa907
commit 6d8fb94f83
2 changed files with 18 additions and 7 deletions

View file

@ -12,7 +12,7 @@ class HTTP::Cookie
PERSISTENT_PROPERTIES = %w[ PERSISTENT_PROPERTIES = %w[
name value name value
domain for_domain path domain for_domain path
secure secure httponly
expires created_at accessed_at expires created_at accessed_at
] ]
True = "TRUE" True = "TRUE"
@ -48,7 +48,7 @@ class HTTP::Cookie
include URIFix if defined?(URIFix) include URIFix if defined?(URIFix)
attr_reader :name, :domain, :path, :origin attr_reader :name, :domain, :path, :origin
attr_accessor :secure, :value, :version attr_accessor :secure, :httponly, :value, :version
attr_reader :domain_name attr_reader :domain_name
attr_accessor :comment, :max_age attr_accessor :comment, :max_age
@ -75,8 +75,10 @@ class HTTP::Cookie
def initialize(*args) def initialize(*args)
@version = 0 # Netscape Cookie @version = 0 # Netscape Cookie
@origin = @domain = @path = @secure = @comment = @max_age = @origin = @domain = @path =
@expires = nil @secure = @httponly =
@expires = @max_age =
@comment = nil
@created_at = @accessed_at = Time.now @created_at = @accessed_at = Time.now
case args.size case args.size
@ -200,10 +202,13 @@ class HTTP::Cookie
end end
when 'secure' when 'secure'
cookie.secure = true cookie.secure = true
when 'httponly'
cookie.httponly = true
end end
end end
cookie.secure ||= false cookie.secure ||= false
cookie.httponly ||= false
# RFC 6265 4.1.2.2 # RFC 6265 4.1.2.2
cookie.expires = Time.now + cookie.max_age if cookie.max_age cookie.expires = Time.now + cookie.max_age if cookie.max_age
@ -326,6 +331,7 @@ class HTTP::Cookie
end end
alias secure? secure alias secure? secure
alias httponly? httponly
def acceptable_from_uri?(uri) def acceptable_from_uri?(uri)
uri = URI(uri) uri = URI(uri)

View file

@ -284,6 +284,7 @@ class TestHTTPCookie < Test::Unit::TestCase
assert_equal('/', cookie.path) assert_equal('/', cookie.path)
assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires) assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
assert_equal(keys.include?('httponly'), cookie.httponly?)
end end
end end
@ -302,6 +303,7 @@ class TestHTTPCookie < Test::Unit::TestCase
assert_equal('/', cookie.path) assert_equal('/', cookie.path)
assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires) assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
assert_equal(keys.include?('httponly'), cookie.httponly?)
end end
end end
@ -321,6 +323,7 @@ class TestHTTPCookie < Test::Unit::TestCase
assert_equal('/', cookie.path) assert_equal('/', cookie.path)
assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires) assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
assert_equal(keys.include?('httponly'), cookie.httponly?)
end end
end end
@ -341,6 +344,7 @@ class TestHTTPCookie < Test::Unit::TestCase
assert_equal(true, cookie.secure) assert_equal(true, cookie.secure)
assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires) assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
assert_equal(keys.include?('httponly'), cookie.httponly?)
end end
end end
@ -358,6 +362,7 @@ class TestHTTPCookie < Test::Unit::TestCase
assert_equal('/', cookie.path) assert_equal('/', cookie.path)
assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires) assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
assert_equal(keys.include?('httponly'), cookie.httponly?)
end end
end end