diff options
Diffstat (limited to 'lib/webrick/cookie.rb')
-rw-r--r-- | lib/webrick/cookie.rb | 71 |
1 files changed, 66 insertions, 5 deletions
diff --git a/lib/webrick/cookie.rb b/lib/webrick/cookie.rb index 814e6645a3..3cf8928bb0 100644 --- a/lib/webrick/cookie.rb +++ b/lib/webrick/cookie.rb @@ -12,14 +12,56 @@ require 'time' require 'webrick/httputils' module WEBrick + + ## + # Processes HTTP cookies + class Cookie + ## + # The cookie name + attr_reader :name - attr_accessor :value, :version - attr_accessor :domain, :path, :secure - attr_accessor :comment, :max_age + + ## + # 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? + + attr_accessor :secure + + ## + # The cookie comment + + attr_accessor :comment + + ## + # The maximum age of the cookie + + attr_accessor :max_age + #attr_accessor :comment_url, :discard, :port + ## + # Creates a new cookie with the given +name+ and +value+ + def initialize(name, value) @name = name @value = value @@ -29,14 +71,25 @@ module WEBrick @expires = @comment_url = @discard = @port = nil end + ## + # 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. + def expires=(t) @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s) end + ## + # Retrieves the expiration time as a Time + def expires @expires && Time.parse(@expires) end + ## + # The cookie string suitable for use in an HTTP header + def to_s ret = "" ret << @name << "=" << @value @@ -50,8 +103,10 @@ module WEBrick ret end - # Cookie::parse() - # It parses Cookie field sent from the user agent. + ## + # Parses a Cookie field sent from the user-agent. Returns an array of + # cookies. + def self.parse(str) if str ret = [] @@ -76,6 +131,9 @@ module WEBrick end end + ## + # Parses the cookie in +str+ + def self.parse_set_cookie(str) cookie_elem = str.split(/;/) first_elem = cookie_elem.shift @@ -101,6 +159,9 @@ module WEBrick return cookie end + ## + # Parses the cookies in +str+ + def self.parse_set_cookies(str) return str.split(/,(?=[^;,]*=)|,$/).collect{|c| parse_set_cookie(c) |