Class: WEBrick::Cookie
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/webrick/cookie.rb |
Overview
Processes HTTP cookies
Class Method Summary
- .new(name, value) ⇒ Cookie constructor
-
.parse(str)
Parses a
Cookie
field sent from the user-agent. -
.parse_set_cookie(str)
Parses the cookie in
str
-
.parse_set_cookies(str)
Parses the cookies in
str
Instance Attribute Summary
-
#comment
rw
The cookie comment.
-
#domain
rw
The cookie domain.
-
#expires
rw
Retrieves the expiration time as a Time.
-
#expires=(t)
rw
Sets the cookie expiration to the time
t
. -
#max_age
rw
The maximum age of the cookie.
-
#path
rw
The cookie path.
-
#secure
rw
Is this a secure cookie?
-
#value
rw
The cookie value.
-
#version
rw
The cookie version.
-
#name
readonly
The cookie name.
Instance Method Summary
-
#to_s
The cookie string suitable for use in an HTTP header.
Constructor Details
.new(name, value) ⇒ Cookie
[ GitHub ]
Class Method Details
.parse(str)
Parses a Cookie
field sent from the user-agent. Returns an array of cookies.
# File 'lib/webrick/cookie.rb', line 110
def self.parse(str) if str ret = [] = 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"; .path = val when "$Domain"; .domain = val when "$Port"; .port = val else ret << if = self.new(key, val) .version = ver end } ret << if ret end end
.parse_set_cookie(str)
Parses the cookie in str
# File 'lib/webrick/cookie.rb', line 137
def self. (str) = str.split(/;/) first_elem = .shift first_elem.strip! key, value = first_elem.split(/=/, 2) = new(key, HTTPUtils.dequote(value)) .each{|pair| pair.strip! key, value = pair.split(/=/, 2) if value value = HTTPUtils.dequote(value.strip) end case key.downcase when "domain" then .domain = value when "path" then .path = value when "expires" then .expires = value when "max-age" then .max_age = Integer(value) when "comment" then .comment = value when "version" then .version = Integer(value) when "secure" then .secure = true end } return end
.parse_set_cookies(str)
Parses the cookies in str
# File 'lib/webrick/cookie.rb', line 165
def self. (str) return str.split(/,(?=[^;,]*=)|,$/).collect{|c| (c) } end
Instance Attribute Details
#comment (rw)
The cookie comment
# File 'lib/webrick/cookie.rb', line 53
attr_accessor :comment
#domain (rw)
The cookie domain
# File 'lib/webrick/cookie.rb', line 38
attr_accessor :domain
#expires (rw)
Retrieves the expiration time as a Time
# File 'lib/webrick/cookie.rb', line 86
def expires @expires && Time.parse(@expires) end
#expires=(t) (rw)
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.
# File 'lib/webrick/cookie.rb', line 79
def expires=(t) @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s) end
#max_age (rw)
The maximum age of the cookie
# File 'lib/webrick/cookie.rb', line 58
attr_accessor :max_age
#name (readonly)
The cookie name
# File 'lib/webrick/cookie.rb', line 24
attr_reader :name
#path (rw)
The cookie path
# File 'lib/webrick/cookie.rb', line 43
attr_accessor :path
#secure (rw)
Is this a secure cookie?
# File 'lib/webrick/cookie.rb', line 48
attr_accessor :secure
#value (rw)
The cookie value
# File 'lib/webrick/cookie.rb', line 29
attr_accessor :value
#version (rw)
The cookie version
# File 'lib/webrick/cookie.rb', line 34
attr_accessor :version
Instance Method Details
#to_s
The cookie string suitable for use in an HTTP header
# File 'lib/webrick/cookie.rb', line 93
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