Bug #2124 ยป cgi_cookie_parse.patch
cgi.rb 2009-09-19 18:22:39.000000000 -0400 | ||
---|---|---|
raise "Please, use ruby 1.5.4 or later." if RUBY_VERSION < "1.5.4"
|
||
require 'English'
|
||
require 'time'
|
||
# CGI class. See documentation for the file cgi.rb for an overview
|
||
# of the CGI protocol.
|
||
... | ... | |
#
|
||
def Cookie::parse(raw_cookie)
|
||
cookies = Hash.new([])
|
||
return cookies unless raw_cookie
|
||
raw_cookie.split(/[;,]\s?/).each do |pairs|
|
||
name, values = pairs.split('=',2)
|
||
next unless name and values
|
||
name = CGI::unescape(name)
|
||
values ||= ""
|
||
values = values.split('&').collect{|v| CGI::unescape(v) }
|
||
if cookies.has_key?(name)
|
||
values = cookies[name].value + values
|
||
token = "[!#$\%&'*+.0-9A-Z^_a-z|-]+" # from RFC 2068
|
||
# split by comma with positive lookahead to the start of another
|
||
# cookie
|
||
raw_cookie.split(/, ?(?=#{token}=(?:#{token}|".*"))/).each do |cookie|
|
||
args = {}
|
||
fields = cookie.split(/; ?/).each do |field|
|
||
field_name, field_value =
|
||
field.split('=', 2).collect { |d| CGI::unescape(d) }
|
||
field_value ||= ''
|
||
field_value.gsub!(/^"|"$/, '')
|
||
if args.empty?
|
||
args['name'], args['value'] = field_name, [field_value, ]
|
||
else
|
||
case field_name
|
||
when 'path' then args['path'] = field_value
|
||
when 'domain' then args['domain'] = field_value
|
||
when 'expires' then args['expires'] = Time.parse(field_value)
|
||
when 'secure' then
|
||
args['secure'] = field_value[/true/i] ? true : false
|
||
end
|
||
end
|
||
end
|
||
cookies[name] = Cookie::new(name, *values)
|
||
cookies[args['name']] = CGI::Cookie::new(args)
|
||
end
|
||
cookies
|