Class: HTTP::Cookie
- Inherits:
-
Object
- Object
- HTTP::Cookie
- Includes:
- Comparable, URIFix
- Defined in:
- lib/http/cookie.rb,
lib/http/cookie/version.rb
Overview
This class is used to represent an HTTP Cookie.
Defined Under Namespace
Modules: URIFix
Constant Summary collapse
- MAX_LENGTH =
Maximum number of bytes per cookie (RFC 6265 6.1 requires 4096 at least)
4096
- MAX_COOKIES_PER_DOMAIN =
Maximum number of cookies per domain (RFC 6265 6.1 requires 50 at least)
50
- MAX_COOKIES_TOTAL =
Maximum number of cookies total (RFC 6265 6.1 requires 3000 at least)
3000
- UNIX_EPOCH =
Time.at(0)
- PERSISTENT_PROPERTIES =
%w[ name value domain for_domain path secure httponly expires created_at accessed_at ]
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#accessed_at ⇒ Object
Returns the value of attribute accessed_at.
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#domain_name ⇒ Object
readonly
Returns the value of attribute domain_name.
-
#expires ⇒ Object
Returns the value of attribute expires.
-
#for_domain ⇒ Object
(also: #for_domain?)
If this flag is true, this cookie will be sent to any host in the
domain
. -
#httponly ⇒ Object
(also: #httponly?)
Returns the value of attribute httponly.
-
#max_age ⇒ Object
Returns the value of attribute max_age.
-
#name ⇒ Object
Returns the value of attribute name.
-
#origin ⇒ Object
Returns the value of attribute origin.
-
#path ⇒ Object
Returns the value of attribute path.
-
#secure ⇒ Object
(also: #secure?)
Returns the value of attribute secure.
-
#session ⇒ Object
(also: #session?)
Returns the value of attribute session.
-
#value ⇒ Object
Returns the value of attribute value.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.normalize_path(uri) ⇒ Object
Normalizes a given path.
-
.parse(set_cookie, options = nil, *_, &block) ⇒ Object
Parses a Set-Cookie header value
set_cookie
into an array of Cookie objects.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares the cookie with another.
- #acceptable_from_uri?(uri) ⇒ Boolean
-
#cookie_value ⇒ Object
(also: #to_s)
Returns a string for use in a Cookie header value, i.e.
-
#encode_with(coder) ⇒ Object
YAML serialization helper for Psych.
- #expire ⇒ Object
- #expired?(time = Time.now) ⇒ Boolean
-
#init_with(coder) ⇒ Object
YAML deserialization helper for Syck.
-
#initialize(*args) ⇒ Cookie
constructor
:call-seq: new(name, value) new(name, value, attr_hash) new(attr_hash).
-
#set_cookie_value(origin = nil) ⇒ Object
Returns a string for use in a Set-Cookie header value.
-
#set_domain(domain) ⇒ Object
Used to exist in Mechanize::CookieJar.
-
#to_yaml_properties ⇒ Object
YAML serialization helper for Syck.
- #valid_for_uri?(uri) ⇒ Boolean
-
#yaml_initialize(tag, map) ⇒ Object
YAML deserialization helper for Psych.
Constructor Details
#initialize(*args) ⇒ Cookie
:call-seq:
new(name, value)
new(name, value, attr_hash)
new(attr_hash)
Creates a cookie object. For each key of attr_hash
, the setter is called if defined. Each key can be either a symbol or a string, downcased or not.
e.g.
new("uid", "a12345")
new("uid", "a12345", :domain => 'example.org',
:for_domain => true, :expired => Time.now + 7*86400)
new("name" => "uid", "value" => "a12345", "Domain" => 'www.example.org')
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/http/cookie.rb', line 82 def initialize(*args) @version = 0 # Netscape Cookie @origin = @domain = @path = @secure = @httponly = @expires = @max_age = @comment = nil @created_at = @accessed_at = Time.now case args.size when 2 self.name, self.value = *args @for_domain = false return when 3 self.name, self.value, attr_hash = *args when 1 attr_hash = args.first else raise ArgumentError, "wrong number of arguments (#{args.size} for 1-3)" end for_domain = false origin = nil attr_hash.each_pair { |key, val| skey = key.to_s.downcase if skey.sub!(/\?\z/, '') val = val ? true : false end case skey when 'for_domain' for_domain = !!val when 'origin' origin = val else setter = :"#{skey}=" send(setter, val) if respond_to?(setter) end } if @name.nil? || @value.nil? raise ArgumentError, "at least name and value must be specified" end @for_domain = for_domain if origin self.origin = origin end end |
Instance Attribute Details
#accessed_at ⇒ Object
Returns the value of attribute accessed_at.
65 66 67 |
# File 'lib/http/cookie.rb', line 65 def accessed_at @accessed_at end |
#comment ⇒ Object
Returns the value of attribute comment.
60 61 62 |
# File 'lib/http/cookie.rb', line 60 def comment @comment end |
#created_at ⇒ Object
Returns the value of attribute created_at.
64 65 66 |
# File 'lib/http/cookie.rb', line 64 def created_at @created_at end |
#domain ⇒ Object
Returns the value of attribute domain.
57 58 59 |
# File 'lib/http/cookie.rb', line 57 def domain @domain end |
#domain_name ⇒ Object (readonly)
Returns the value of attribute domain_name.
59 60 61 |
# File 'lib/http/cookie.rb', line 59 def domain_name @domain_name end |
#expires ⇒ Object
Returns the value of attribute expires.
59 60 61 |
# File 'lib/http/cookie.rb', line 59 def expires @expires end |
#for_domain ⇒ Object Also known as: for_domain?
If this flag is true, this cookie will be sent to any host in the domain
. If it is false, this cookie will be sent only to the host indicated by the domain
.
132 133 134 |
# File 'lib/http/cookie.rb', line 132 def for_domain @for_domain end |
#httponly ⇒ Object Also known as: httponly?
Returns the value of attribute httponly.
58 59 60 |
# File 'lib/http/cookie.rb', line 58 def httponly @httponly end |
#max_age ⇒ Object
Returns the value of attribute max_age.
60 61 62 |
# File 'lib/http/cookie.rb', line 60 def max_age @max_age end |
#name ⇒ Object
Returns the value of attribute name.
57 58 59 |
# File 'lib/http/cookie.rb', line 57 def name @name end |
#origin ⇒ Object
Returns the value of attribute origin.
57 58 59 |
# File 'lib/http/cookie.rb', line 57 def origin @origin end |
#path ⇒ Object
Returns the value of attribute path.
57 58 59 |
# File 'lib/http/cookie.rb', line 57 def path @path end |
#secure ⇒ Object Also known as: secure?
Returns the value of attribute secure.
58 59 60 |
# File 'lib/http/cookie.rb', line 58 def secure @secure end |
#session ⇒ Object Also known as: session?
Returns the value of attribute session.
62 63 64 |
# File 'lib/http/cookie.rb', line 62 def session @session end |
#value ⇒ Object
Returns the value of attribute value.
58 59 60 |
# File 'lib/http/cookie.rb', line 58 def value @value end |
#version ⇒ Object
Returns the value of attribute version.
58 59 60 |
# File 'lib/http/cookie.rb', line 58 def version @version end |
Class Method Details
.normalize_path(uri) ⇒ Object
Normalizes a given path. If it is empty, the root path ‘/’ is returned. If a URI object is given, returns a new URI object with the path part normalized.
141 142 143 144 145 146 147 148 149 |
# File 'lib/http/cookie.rb', line 141 def normalize_path(uri) # Currently does not replace // to / case uri when URI uri.path.empty? ? uri + '/' : uri else uri.empty? ? '/' : uri end end |
.parse(set_cookie, options = nil, *_, &block) ⇒ Object
Parses a Set-Cookie header value set_cookie
into an array of Cookie objects. Parts (separated by commas) that are malformed are ignored.
If a block is given, each cookie object is passed to the block.
Available option keywords are below:
-
origin
The cookie’s origin URI/URL -
date
The base date used for interpreting Max-Age attribute values instead of the current time -
logger
Logger object useful for debugging
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/http/cookie.rb', line 166 def parse(, = nil, *_, &block) _.empty? && !.is_a?(String) or raise ArgumentError, 'HTTP::Cookie equivalent for Mechanize::Cookie.parse(uri, set_cookie[, log]) is HTTP::Cookie.parse(set_cookie, :origin => uri[, :logger => log]).' if logger = [:logger] origin = [:origin] and origin = URI(origin) date = [:date] end date ||= Time.now [].tap { || .split(/,(?=[^;,]*=)|,$/).each { |c| if c.bytesize > MAX_LENGTH logger.warn("Cookie definition too long: #{c}") if logger next end = c.split(/;+/) first_elem = .shift first_elem.strip! key, value = first_elem.split(/\=/, 2) begin = new(key, value.dup) rescue logger.warn("Couldn't parse key/value: #{first_elem}") if logger next end .each do |pair| pair.strip! key, value = pair.split(/=/, 2) #/) next unless key value = WEBrick::HTTPUtils.dequote(value.strip) if value case key.downcase when 'domain' next unless value && !value.empty? begin .domain = value .for_domain = true rescue logger.warn("Couldn't parse domain: #{value}") if logger end when 'path' next unless value && !value.empty? .path = value when 'expires' next unless value && !value.empty? begin .expires = Time.parse(value) rescue logger.warn("Couldn't parse expires: #{value}") if logger end when 'max-age' next unless value && !value.empty? begin .max_age = Integer(value) rescue logger.warn("Couldn't parse max age '#{value}'") if logger end when 'comment' next unless value .comment = value when 'version' next unless value begin .version = Integer(value) rescue logger.warn("Couldn't parse version '#{value}'") if logger .version = nil end when 'secure' .secure = true when 'httponly' .httponly = true end end .secure ||= false .httponly ||= false # RFC 6265 4.1.2.2 .expires = date + .max_age if .max_age .session = !.expires if origin begin .origin = origin rescue => e logger.warn("Invalid cookie for the origin: #{origin} (#{e})") if logger next end end yield if block_given? << } } end |
Instance Method Details
#<=>(other) ⇒ Object
Compares the cookie with another. When there are many cookies with the same name for a URL, the value of the smallest must be used.
411 412 413 414 415 416 417 418 |
# File 'lib/http/cookie.rb', line 411 def <=>(other) # RFC 6265 5.4 # Precedence: 1. longer path 2. older creation (@name <=> other.name).nonzero? || (other.path.length <=> @path.length).nonzero? || (@created_at <=> other.created_at).nonzero? || @value <=> other.value end |
#acceptable_from_uri?(uri) ⇒ Boolean
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/http/cookie.rb', line 343 def acceptable_from_uri?(uri) uri = URI(uri) host = DomainName.new(uri.host) # RFC 6265 5.3 # When the user agent "receives a cookie": return @domain.nil? || host.hostname == @domain unless @for_domain if host.(@domain_name) true elsif host.hostname == @domain @for_domain = false true else false end end |
#cookie_value ⇒ Object Also known as: to_s
Returns a string for use in a Cookie header value, i.e. “name=value”.
372 373 374 |
# File 'lib/http/cookie.rb', line 372 def "#{@name}=#{@value}" end |
#encode_with(coder) ⇒ Object
YAML serialization helper for Psych.
427 428 429 430 431 |
# File 'lib/http/cookie.rb', line 427 def encode_with(coder) PERSISTENT_PROPERTIES.each { |key| coder[key.to_s] = instance_variable_get(:"@#{key}") } end |
#expire ⇒ Object
334 335 336 337 |
# File 'lib/http/cookie.rb', line 334 def expire @expires = UNIX_EPOCH self end |
#expired?(time = Time.now) ⇒ Boolean
329 330 331 332 |
# File 'lib/http/cookie.rb', line 329 def expired?(time = Time.now) return false unless @expires time > @expires end |
#init_with(coder) ⇒ Object
YAML deserialization helper for Syck.
434 435 436 |
# File 'lib/http/cookie.rb', line 434 def init_with(coder) yaml_initialize(coder.tag, coder.map) end |
#set_cookie_value(origin = nil) ⇒ Object
Returns a string for use in a Set-Cookie header value. If the cookie does not have an origin set, one must be given from the argument.
This method does not check if this cookie will be accepted from the origin.
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/http/cookie.rb', line 383 def (origin = nil) origin = origin ? URI(origin) : @origin or raise "origin must be specified to produce a value for Set-Cookie" string = if @for_domain || @domain != DomainName.new(origin.host).hostname string << "; domain=#{@domain}" end if (HTTP::Cookie.normalize_path(origin) + './').path != @path string << "; path=#{@path}" end if expires = @expires string << "; expires=#{@expires.httpdate}" end if comment = @comment string << "; comment=#{@comment}" end if httponly? string << "; HttpOnly" end if secure? string << "; secure" end string end |
#set_domain(domain) ⇒ Object
Used to exist in Mechanize::CookieJar. Use #domain=().
301 302 303 |
# File 'lib/http/cookie.rb', line 301 def set_domain(domain) raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#set_domain() is #domain=().' end |
#to_yaml_properties ⇒ Object
YAML serialization helper for Syck.
422 423 424 |
# File 'lib/http/cookie.rb', line 422 def to_yaml_properties PERSISTENT_PROPERTIES.map { |name| "@#{name}" } end |
#valid_for_uri?(uri) ⇒ Boolean
361 362 363 364 365 366 367 368 |
# File 'lib/http/cookie.rb', line 361 def valid_for_uri?(uri) uri = URI(uri) if @domain.nil? raise "cannot tell if this cookie is valid because the domain is unknown" end return false if secure? && uri.scheme != 'https' acceptable_from_uri?(uri) && HTTP::Cookie.normalize_path(uri.path).start_with?(@path) end |
#yaml_initialize(tag, map) ⇒ Object
YAML deserialization helper for Psych.
439 440 441 442 443 444 445 446 |
# File 'lib/http/cookie.rb', line 439 def yaml_initialize(tag, map) map.each { |key, value| case key when *PERSISTENT_PROPERTIES send(:"#{key}=", value) end } end |