Removed inadvertently added files from mspec.
[rbx.git] / lib / webrick / cookie.rb
blob814e6645a37ff543f1a01365753d4bbc4f1f6c5b
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
7 # reserved.
9 # $IPR: cookie.rb,v 1.16 2002/09/21 12:23:35 gotoyuzo Exp $
11 require 'time'
12 require 'webrick/httputils'
14 module WEBrick
15   class Cookie
17     attr_reader :name
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)
24       @name = name
25       @value = value
26       @version = 0     # Netscape Cookie
28       @domain = @path = @secure = @comment = @max_age =
29       @expires = @comment_url = @discard = @port = nil
30     end
32     def expires=(t)
33       @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
34     end
36     def expires
37       @expires && Time.parse(@expires)
38     end
40     def to_s
41       ret = ""
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
50       ret
51     end
53     # Cookie::parse()
54     #   It parses Cookie field sent from the user agent.
55     def self.parse(str)
56       if str
57         ret = []
58         cookie = nil
59         ver = 0
60         str.split(/[;,]\s+/).each{|x|
61           key, val = x.split(/=/,2)
62           val = val ? HTTPUtils::dequote(val) : ""
63           case key
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
68           else
69             ret << cookie if cookie
70             cookie = self.new(key, val)
71             cookie.version = ver
72           end
73         }
74         ret << cookie if cookie
75         ret
76       end
77     end
79     def self.parse_set_cookie(str)
80       cookie_elem = str.split(/;/)
81       first_elem = cookie_elem.shift
82       first_elem.strip!
83       key, value = first_elem.split(/=/, 2)
84       cookie = new(key, HTTPUtils.dequote(value))
85       cookie_elem.each{|pair|
86         pair.strip!
87         key, value = pair.split(/=/, 2)
88         if value
89           value = HTTPUtils.dequote(value.strip)
90         end
91         case key.downcase
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
99         end
100       }
101       return cookie
102     end
104     def self.parse_set_cookies(str)
105       return str.split(/,(?=[^;,]*=)|,$/).collect{|c|
106         parse_set_cookie(c)
107       }
108     end
109   end