2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-07-23 12:51:36 -04:00
|
|
|
#
|
|
|
|
# cookie.rb -- Cookie class
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
|
|
|
#
|
|
|
|
# $IPR: cookie.rb,v 1.16 2002/09/21 12:23:35 gotoyuzo Exp $
|
|
|
|
|
|
|
|
require 'time'
|
|
|
|
require 'webrick/httputils'
|
|
|
|
|
|
|
|
module WEBrick
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Processes HTTP cookies
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class Cookie
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# The cookie name
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
attr_reader :name
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# The cookie value
|
|
|
|
|
|
|
|
attr_accessor :value
|
|
|
|
|
|
|
|
##
|
|
|
|
# The cookie version
|
|
|
|
|
|
|
|
attr_accessor :version
|
|
|
|
|
|
|
|
##
|
|
|
|
# The cookie domain
|
|
|
|
attr_accessor :domain
|
|
|
|
|
|
|
|
##
|
|
|
|
# The cookie path
|
|
|
|
|
|
|
|
attr_accessor :path
|
|
|
|
|
|
|
|
##
|
|
|
|
# Is this a secure cookie?
|
2013-01-25 21:31:43 -05:00
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
attr_accessor :secure
|
|
|
|
|
|
|
|
##
|
|
|
|
# The cookie comment
|
|
|
|
|
|
|
|
attr_accessor :comment
|
|
|
|
|
|
|
|
##
|
|
|
|
# The maximum age of the cookie
|
|
|
|
|
|
|
|
attr_accessor :max_age
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
#attr_accessor :comment_url, :discard, :port
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Creates a new cookie with the given +name+ and +value+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def initialize(name, value)
|
|
|
|
@name = name
|
|
|
|
@value = value
|
|
|
|
@version = 0 # Netscape Cookie
|
|
|
|
|
|
|
|
@domain = @path = @secure = @comment = @max_age =
|
|
|
|
@expires = @comment_url = @discard = @port = nil
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Sets the cookie expiration to the time +t+. The expiration time may be
|
|
|
|
# a false value to disable expiration or a Time or HTTP format time string
|
|
|
|
# to set the expiration date.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def expires=(t)
|
|
|
|
@expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Retrieves the expiration time as a Time
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def expires
|
|
|
|
@expires && Time.parse(@expires)
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# The cookie string suitable for use in an HTTP header
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def to_s
|
|
|
|
ret = ""
|
|
|
|
ret << @name << "=" << @value
|
|
|
|
ret << "; " << "Version=" << @version.to_s if @version > 0
|
|
|
|
ret << "; " << "Domain=" << @domain if @domain
|
|
|
|
ret << "; " << "Expires=" << @expires if @expires
|
|
|
|
ret << "; " << "Max-Age=" << @max_age.to_s if @max_age
|
|
|
|
ret << "; " << "Comment=" << @comment if @comment
|
|
|
|
ret << "; " << "Path=" << @path if @path
|
|
|
|
ret << "; " << "Secure" if @secure
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Parses a Cookie field sent from the user-agent. Returns an array of
|
|
|
|
# cookies.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def self.parse(str)
|
|
|
|
if str
|
|
|
|
ret = []
|
|
|
|
cookie = nil
|
|
|
|
ver = 0
|
2011-06-20 02:48:40 -04:00
|
|
|
str.split(/[;,]\s+/).each{|x|
|
2003-07-23 12:51:36 -04:00
|
|
|
key, val = x.split(/=/,2)
|
|
|
|
val = val ? HTTPUtils::dequote(val) : ""
|
|
|
|
case key
|
|
|
|
when "$Version"; ver = val.to_i
|
|
|
|
when "$Path"; cookie.path = val
|
|
|
|
when "$Domain"; cookie.domain = val
|
|
|
|
when "$Port"; cookie.port = val
|
|
|
|
else
|
|
|
|
ret << cookie if cookie
|
|
|
|
cookie = self.new(key, val)
|
|
|
|
cookie.version = ver
|
|
|
|
end
|
|
|
|
}
|
|
|
|
ret << cookie if cookie
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Parses the cookie in +str+
|
|
|
|
|
2005-10-30 15:40:05 -05:00
|
|
|
def self.parse_set_cookie(str)
|
|
|
|
cookie_elem = str.split(/;/)
|
|
|
|
first_elem = cookie_elem.shift
|
|
|
|
first_elem.strip!
|
|
|
|
key, value = first_elem.split(/=/, 2)
|
|
|
|
cookie = new(key, HTTPUtils.dequote(value))
|
|
|
|
cookie_elem.each{|pair|
|
|
|
|
pair.strip!
|
|
|
|
key, value = pair.split(/=/, 2)
|
|
|
|
if value
|
|
|
|
value = HTTPUtils.dequote(value.strip)
|
|
|
|
end
|
|
|
|
case key.downcase
|
|
|
|
when "domain" then cookie.domain = value
|
|
|
|
when "path" then cookie.path = value
|
|
|
|
when "expires" then cookie.expires = value
|
|
|
|
when "max-age" then cookie.max_age = Integer(value)
|
|
|
|
when "comment" then cookie.comment = value
|
|
|
|
when "version" then cookie.version = Integer(value)
|
|
|
|
when "secure" then cookie.secure = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return cookie
|
|
|
|
end
|
2006-09-07 21:04:52 -04:00
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Parses the cookies in +str+
|
|
|
|
|
2006-09-07 21:04:52 -04:00
|
|
|
def self.parse_set_cookies(str)
|
|
|
|
return str.split(/,(?=[^;,]*=)|,$/).collect{|c|
|
|
|
|
parse_set_cookie(c)
|
|
|
|
}
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|