2 # cookie.rb -- Cookie class
4 # Author: IPR -- Internet Programming with Ruby -- writers
5 # Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
6 # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
9 # $IPR: cookie.rb,v 1.16 2002/09/21 12:23:35 gotoyuzo Exp $
12 require 'webrick/httputils'
18 attr_accessor :value, :version
19 attr_accessor :domain, :path, :secure
20 attr_accessor :comment, :max_age
21 #attr_accessor :comment_url, :discard, :port
23 def initialize(name, value)
26 @version = 0 # Netscape Cookie
28 @domain = @path = @secure = @comment = @max_age =
29 @expires = @comment_url = @discard = @port = nil
33 @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
37 @expires && Time.parse(@expires)
42 ret << @name << "=" << @value
43 ret << "; " << "Version=" << @version.to_s if @version > 0
44 ret << "; " << "Domain=" << @domain if @domain
45 ret << "; " << "Expires=" << @expires if @expires
46 ret << "; " << "Max-Age=" << @max_age.to_s if @max_age
47 ret << "; " << "Comment=" << @comment if @comment
48 ret << "; " << "Path=" << @path if @path
49 ret << "; " << "Secure" if @secure
54 # It parses Cookie field sent from the user agent.
60 str.split(/[;,]\s+/).each{|x|
61 key, val = x.split(/=/,2)
62 val = val ? HTTPUtils::dequote(val) : ""
64 when "$Version"; ver = val.to_i
65 when "$Path"; cookie.path = val
66 when "$Domain"; cookie.domain = val
67 when "$Port"; cookie.port = val
69 ret << cookie if cookie
70 cookie = self.new(key, val)
74 ret << cookie if cookie
79 def self.parse_set_cookie(str)
80 cookie_elem = str.split(/;/)
81 first_elem = cookie_elem.shift
83 key, value = first_elem.split(/=/, 2)
84 cookie = new(key, HTTPUtils.dequote(value))
85 cookie_elem.each{|pair|
87 key, value = pair.split(/=/, 2)
89 value = HTTPUtils.dequote(value.strip)
92 when "domain" then cookie.domain = value
93 when "path" then cookie.path = value
94 when "expires" then cookie.expires = value
95 when "max-age" then cookie.max_age = Integer(value)
96 when "comment" then cookie.comment = value
97 when "version" then cookie.version = Integer(value)
98 when "secure" then cookie.secure = true
104 def self.parse_set_cookies(str)
105 return str.split(/,(?=[^;,]*=)|,$/).collect{|c|