Class: WEBrick::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/webrick/cookie.rb

Overview

Processes HTTP cookies

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value) ⇒ Cookie

Creates a new cookie with the given name and value



66
67
68
69
70
71
72
73
# File 'lib/webrick/cookie.rb', line 66

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

Instance Attribute Details

#commentObject

The cookie comment



54
55
56
# File 'lib/webrick/cookie.rb', line 54

def comment
  @comment
end

#domainObject

The cookie domain



39
40
41
# File 'lib/webrick/cookie.rb', line 39

def domain
  @domain
end

#max_ageObject

The maximum age of the cookie



59
60
61
# File 'lib/webrick/cookie.rb', line 59

def max_age
  @max_age
end

#nameObject (readonly)

The cookie name



25
26
27
# File 'lib/webrick/cookie.rb', line 25

def name
  @name
end

#pathObject

The cookie path



44
45
46
# File 'lib/webrick/cookie.rb', line 44

def path
  @path
end

#secureObject

Is this a secure cookie?



49
50
51
# File 'lib/webrick/cookie.rb', line 49

def secure
  @secure
end

#valueObject

The cookie value



30
31
32
# File 'lib/webrick/cookie.rb', line 30

def value
  @value
end

#versionObject

The cookie version



35
36
37
# File 'lib/webrick/cookie.rb', line 35

def version
  @version
end

Class Method Details

.parse(str) ⇒ Object

Parses a Cookie field sent from the user-agent. Returns an array of cookies.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/webrick/cookie.rb', line 111

def self.parse(str)
  if str
    ret = []
    cookie = nil
    ver = 0
    str.split(/;\s+/).each{|x|
      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

Parses the cookie in str



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/webrick/cookie.rb', line 138

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

.parse_set_cookies(str) ⇒ Object

Parses the cookies in str



166
167
168
169
170
# File 'lib/webrick/cookie.rb', line 166

def self.parse_set_cookies(str)
  return str.split(/,(?=[^;,]*=)|,$/).collect{|c|
    parse_set_cookie(c)
  }
end

Instance Method Details

#expiresObject

Retrieves the expiration time as a Time



87
88
89
# File 'lib/webrick/cookie.rb', line 87

def expires
  @expires && Time.parse(@expires)
end

#expires=(t) ⇒ Object

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.



80
81
82
# File 'lib/webrick/cookie.rb', line 80

def expires=(t)
  @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
end

#to_sObject

The cookie string suitable for use in an HTTP header



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/webrick/cookie.rb', line 94

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