Class: WEBrick::Cookie
- Inherits:
-
Object
- Object
- WEBrick::Cookie
- Defined in:
- lib/webrick/cookie.rb
Overview
Processes HTTP cookies
Instance Attribute Summary collapse
-
#comment ⇒ Object
The cookie comment.
-
#domain ⇒ Object
The cookie domain.
-
#max_age ⇒ Object
The maximum age of the cookie.
-
#name ⇒ Object
readonly
The cookie name.
-
#path ⇒ Object
The cookie path.
-
#secure ⇒ Object
Is this a secure cookie?.
-
#value ⇒ Object
The cookie value.
-
#version ⇒ Object
The cookie version.
Class Method Summary collapse
-
.parse(str) ⇒ Object
Parses a Cookie field sent from the user-agent.
-
.parse_set_cookie(str) ⇒ Object
Parses the cookie in
str
. -
.parse_set_cookies(str) ⇒ Object
Parses the cookies in
str
.
Instance Method Summary collapse
-
#expires ⇒ Object
Retrieves the expiration time as a Time.
-
#expires=(t) ⇒ Object
Sets the cookie expiration to the time
t
. -
#initialize(name, value) ⇒ Cookie
constructor
Creates a new cookie with the given
name
andvalue
. -
#to_s ⇒ Object
The cookie string suitable for use in an HTTP header.
Constructor Details
#initialize(name, value) ⇒ Cookie
Creates a new cookie with the given name
and value
65 66 67 68 69 70 71 72 |
# File 'lib/webrick/cookie.rb', line 65 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
#comment ⇒ Object
The cookie comment
53 54 55 |
# File 'lib/webrick/cookie.rb', line 53 def comment @comment end |
#domain ⇒ Object
The cookie domain
38 39 40 |
# File 'lib/webrick/cookie.rb', line 38 def domain @domain end |
#max_age ⇒ Object
The maximum age of the cookie
58 59 60 |
# File 'lib/webrick/cookie.rb', line 58 def max_age @max_age end |
#name ⇒ Object (readonly)
The cookie name
24 25 26 |
# File 'lib/webrick/cookie.rb', line 24 def name @name end |
#path ⇒ Object
The cookie path
43 44 45 |
# File 'lib/webrick/cookie.rb', line 43 def path @path end |
#secure ⇒ Object
Is this a secure cookie?
48 49 50 |
# File 'lib/webrick/cookie.rb', line 48 def secure @secure end |
#value ⇒ Object
The cookie value
29 30 31 |
# File 'lib/webrick/cookie.rb', line 29 def value @value end |
#version ⇒ Object
The cookie version
34 35 36 |
# File 'lib/webrick/cookie.rb', line 34 def version @version end |
Class Method Details
.parse(str) ⇒ Object
Parses a Cookie field sent from the user-agent. Returns an array of cookies.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# 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) ⇒ Object
Parses the cookie in str
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# 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) ⇒ Object
Parses the cookies in str
165 166 167 168 169 |
# File 'lib/webrick/cookie.rb', line 165 def self.(str) return str.split(/,(?=[^;,]*=)|,$/).collect{|c| (c) } end |
Instance Method Details
#expires ⇒ Object
Retrieves the expiration time as a Time
86 87 88 |
# File 'lib/webrick/cookie.rb', line 86 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.
79 80 81 |
# File 'lib/webrick/cookie.rb', line 79 def expires=(t) @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s) end |
#to_s ⇒ Object
The cookie string suitable for use in an HTTP header
93 94 95 96 97 98 99 100 101 102 103 104 |
# 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 |