Class WEBrick::Cookie
In: lib/webrick/cookie.rb
Parent: Object

Methods

Attributes

comment  [RW] 
domain  [RW] 
max_age  [RW] 
name  [R] 
path  [RW] 
secure  [RW] 
value  [RW] 
version  [RW] 

Public Class methods

attr_accessor :comment_url, :discard, :port

[Source]

    # File lib/webrick/cookie.rb, line 23
23:     def initialize(name, value)
24:       @name = name
25:       @value = value
26:       @version = 0     # Netscape Cookie
27: 
28:       @domain = @path = @secure = @comment = @max_age =
29:       @expires = @comment_url = @discard = @port = nil
30:     end

Cookie::parse()

  It parses Cookie field sent from the user agent.

[Source]

    # File lib/webrick/cookie.rb, line 55
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

[Source]

     # File lib/webrick/cookie.rb, line 79
 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

[Source]

     # File lib/webrick/cookie.rb, line 104
104:     def self.parse_set_cookies(str)
105:       return str.split(/,(?=[^;,]*=)|,$/).collect{|c|
106:         parse_set_cookie(c)
107:       }
108:     end

Public Instance methods

[Source]

    # File lib/webrick/cookie.rb, line 36
36:     def expires
37:       @expires && Time.parse(@expires)
38:     end

[Source]

    # File lib/webrick/cookie.rb, line 32
32:     def expires=(t)
33:       @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
34:     end

[Source]

    # File lib/webrick/cookie.rb, line 40
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

[Validate]