mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-04-26 14:47:43 +00:00
HTTP::Cookie.parse now returns an compacted array.
This commit is contained in:
parent
6110545ff9
commit
905cd2bc26
2 changed files with 72 additions and 69 deletions
|
|
@ -82,82 +82,84 @@ class HTTP::Cookie
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
# Parses a Set-Cookie header line +str+ sent from +uri+ into an
|
# Parses a Set-Cookie header line +str+ sent from +uri+ into an
|
||||||
# array of Cookie objects. Note that this array may contain
|
# array of Cookie objects. Parts (separated by commas) that are
|
||||||
# nil's when some of the cookie-pairs are malformed.
|
# malformed are ignored.
|
||||||
def parse(uri, str, log = nil)
|
def parse(uri, str, log = nil)
|
||||||
return str.split(/,(?=[^;,]*=)|,$/).map { |c|
|
[].tap { |cookies|
|
||||||
cookie_elem = c.split(/;+/)
|
set_cookie.split(/,(?=[^;,]*=)|,$/).each { |c|
|
||||||
first_elem = cookie_elem.shift
|
cookie_elem = c.split(/;+/)
|
||||||
first_elem.strip!
|
first_elem = cookie_elem.shift
|
||||||
key, value = first_elem.split(/\=/, 2)
|
first_elem.strip!
|
||||||
|
key, value = first_elem.split(/\=/, 2)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
cookie = new(key, value.dup)
|
cookie = new(key, value.dup)
|
||||||
rescue
|
rescue
|
||||||
log.warn("Couldn't parse key/value: #{first_elem}") if log
|
logger.warn("Couldn't parse key/value: #{first_elem}") if logger
|
||||||
next
|
next
|
||||||
end
|
|
||||||
|
|
||||||
cookie_elem.each do |pair|
|
|
||||||
pair.strip!
|
|
||||||
key, value = pair.split(/=/, 2)
|
|
||||||
next unless key
|
|
||||||
value = WEBrick::HTTPUtils.dequote(value.strip) if value
|
|
||||||
|
|
||||||
case key.downcase
|
|
||||||
when 'domain'
|
|
||||||
next unless value && !value.empty?
|
|
||||||
begin
|
|
||||||
cookie.domain = value
|
|
||||||
cookie.for_domain = true
|
|
||||||
rescue
|
|
||||||
log.warn("Couldn't parse domain: #{value}") if log
|
|
||||||
end
|
|
||||||
when 'path'
|
|
||||||
next unless value && !value.empty?
|
|
||||||
cookie.path = value
|
|
||||||
when 'expires'
|
|
||||||
next unless value && !value.empty?
|
|
||||||
begin
|
|
||||||
cookie.expires = Time::parse(value)
|
|
||||||
rescue
|
|
||||||
log.warn("Couldn't parse expires: #{value}") if log
|
|
||||||
end
|
|
||||||
when 'max-age'
|
|
||||||
next unless value && !value.empty?
|
|
||||||
begin
|
|
||||||
cookie.max_age = Integer(value)
|
|
||||||
rescue
|
|
||||||
log.warn("Couldn't parse max age '#{value}'") if log
|
|
||||||
end
|
|
||||||
when 'comment'
|
|
||||||
next unless value
|
|
||||||
cookie.comment = value
|
|
||||||
when 'version'
|
|
||||||
next unless value
|
|
||||||
begin
|
|
||||||
cookie.version = Integer(value)
|
|
||||||
rescue
|
|
||||||
log.warn("Couldn't parse version '#{value}'") if log
|
|
||||||
cookie.version = nil
|
|
||||||
end
|
|
||||||
when 'secure'
|
|
||||||
cookie.secure = true
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
cookie.path ||= (uri + './').path
|
cookie_elem.each do |pair|
|
||||||
cookie.secure ||= false
|
pair.strip!
|
||||||
cookie.domain ||= uri.host
|
key, value = pair.split(/=/, 2) #/)
|
||||||
|
next unless key
|
||||||
|
value = WEBrick::HTTPUtils.dequote(value.strip) if value
|
||||||
|
|
||||||
# RFC 6265 4.1.2.2
|
case key.downcase
|
||||||
cookie.expires = Time.now + cookie.max_age if cookie.max_age
|
when 'domain'
|
||||||
cookie.session = !cookie.expires
|
next unless value && !value.empty?
|
||||||
|
begin
|
||||||
|
cookie.domain = value
|
||||||
|
cookie.for_domain = true
|
||||||
|
rescue
|
||||||
|
logger.warn("Couldn't parse domain: #{value}") if logger
|
||||||
|
end
|
||||||
|
when 'path'
|
||||||
|
next unless value && !value.empty?
|
||||||
|
cookie.path = value
|
||||||
|
when 'expires'
|
||||||
|
next unless value && !value.empty?
|
||||||
|
begin
|
||||||
|
cookie.expires = Time.parse(value)
|
||||||
|
rescue
|
||||||
|
logger.warn("Couldn't parse expires: #{value}") if logger
|
||||||
|
end
|
||||||
|
when 'max-age'
|
||||||
|
next unless value && !value.empty?
|
||||||
|
begin
|
||||||
|
cookie.max_age = Integer(value)
|
||||||
|
rescue
|
||||||
|
logger.warn("Couldn't parse max age '#{value}'") if logger
|
||||||
|
end
|
||||||
|
when 'comment'
|
||||||
|
next unless value
|
||||||
|
cookie.comment = value
|
||||||
|
when 'version'
|
||||||
|
next unless value
|
||||||
|
begin
|
||||||
|
cookie.version = Integer(value)
|
||||||
|
rescue
|
||||||
|
logger.warn("Couldn't parse version '#{value}'") if logger
|
||||||
|
cookie.version = nil
|
||||||
|
end
|
||||||
|
when 'secure'
|
||||||
|
cookie.secure = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Move this in to the cookie jar
|
cookie.path ||= (origin + './').path
|
||||||
yield cookie if block_given?
|
cookie.secure ||= false
|
||||||
|
cookie.domain ||= origin.host
|
||||||
|
|
||||||
cookie
|
# RFC 6265 4.1.2.2
|
||||||
|
cookie.expires = Time.now + cookie.max_age if cookie.max_age
|
||||||
|
cookie.session = !cookie.expires
|
||||||
|
|
||||||
|
# Move this in to the cookie jar
|
||||||
|
yield cookie if block_given?
|
||||||
|
|
||||||
|
cookies << cookie
|
||||||
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -207,6 +207,7 @@ class TestHTTPCookie < Test::Unit::TestCase
|
||||||
def test_parse_many
|
def test_parse_many
|
||||||
url = URI 'http://example/'
|
url = URI 'http://example/'
|
||||||
cookie_str =
|
cookie_str =
|
||||||
|
"abc, " \
|
||||||
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
|
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
|
||||||
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
|
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
|
||||||
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
|
"name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue