Class: ActionDispatch::Cookies::CookieJar
- Inherits:
-
Object
- Object
- ActionDispatch::Cookies::CookieJar
- Includes:
- ChainedCookieJars, Enumerable
- Defined in:
- lib/action_dispatch/middleware/cookies.rb
Overview
:nodoc:
Direct Known Subclasses
ActionController::RequestForgeryProtection::ProtectionMethods::NullSession::NullCookieJar
Constant Summary collapse
- DOMAIN_REGEXP =
This regular expression is used to split the levels of a domain. The top level domain can be any string without a period or ., *. style TLDs like co.uk or com.au
www.example.co.uk gives: $& => example.co.uk
example.com gives: $& => example.com
lots.of.subdomains.example.local gives: $& => example.local
/[^.]*\.([^.]*|..\...|...\...)$/
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns the value of the cookie by
name
, ornil
if no such cookie exists. -
#[]=(name, options) ⇒ Object
Sets the cookie named
name
. -
#clear(options = {}) ⇒ Object
Removes all cookies on the client machine by calling
delete
for each cookie. - #commit! ⇒ Object
- #committed? ⇒ Boolean
-
#delete(name, options = {}) ⇒ Object
Removes the cookie on the client machine by setting the value to an empty string and the expiration date in the past.
-
#deleted?(name, options = {}) ⇒ Boolean
Whether the given cookie is to be deleted by this CookieJar.
- #each(&block) ⇒ Object
- #fetch(name, *args, &block) ⇒ Object
-
#handle_options(options) ⇒ Object
:nodoc:.
-
#initialize(request) ⇒ CookieJar
constructor
A new instance of CookieJar.
- #key?(name) ⇒ Boolean (also: #has_key?)
- #to_header ⇒ Object
- #update(other_hash) ⇒ Object
- #update_cookies_from_jar ⇒ Object
- #write(headers) ⇒ Object
Methods included from ChainedCookieJars
#encrypted, #permanent, #signed, #signed_or_encrypted
Constructor Details
#initialize(request) ⇒ CookieJar
Returns a new instance of CookieJar.
297 298 299 300 301 302 303 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 297 def initialize(request) @set_cookies = {} @delete_cookies = {} @request = request @cookies = {} @committed = false end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
295 296 297 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 295 def request @request end |
Class Method Details
.build(req, cookies) ⇒ Object
289 290 291 292 293 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 289 def self.build(req, ) new(req).tap do |hash| hash.update() end end |
Instance Method Details
#[](name) ⇒ Object
Returns the value of the cookie by name
, or nil
if no such cookie exists.
318 319 320 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 318 def [](name) @cookies[name.to_s] end |
#[]=(name, options) ⇒ Object
Sets the cookie named name
. The second argument may be the cookie’s value or a hash of options as documented above.
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 374 def []=(name, ) if .is_a?(Hash) .symbolize_keys! value = [:value] else value = = { value: value } end () if @cookies[name.to_s] != value || [:expires] @cookies[name.to_s] = value @set_cookies[name.to_s] = @delete_cookies.delete(name.to_s) end value end |
#clear(options = {}) ⇒ Object
Removes all cookies on the client machine by calling delete
for each cookie.
418 419 420 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 418 def clear( = {}) @cookies.each_key { |k| delete(k, ) } end |
#commit! ⇒ Object
307 308 309 310 311 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 307 def commit! @committed = true @set_cookies.freeze @delete_cookies.freeze end |
#committed? ⇒ Boolean
305 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 305 def committed?; @committed; end |
#delete(name, options = {}) ⇒ Object
Removes the cookie on the client machine by setting the value to an empty string and the expiration date in the past. Like []=
, you can pass in an options hash to delete cookies with extra data such as a :path
.
397 398 399 400 401 402 403 404 405 406 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 397 def delete(name, = {}) return unless @cookies.has_key? name.to_s .symbolize_keys! () value = @cookies.delete(name.to_s) @delete_cookies[name.to_s] = value end |
#deleted?(name, options = {}) ⇒ Boolean
Whether the given cookie is to be deleted by this CookieJar. Like []=
, you can pass in an options hash to test if a deletion applies to a specific :path
, :domain
etc.
411 412 413 414 415 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 411 def deleted?(name, = {}) .symbolize_keys! () @delete_cookies[name.to_s] == end |
#each(&block) ⇒ Object
313 314 315 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 313 def each(&block) @cookies.each(&block) end |
#fetch(name, *args, &block) ⇒ Object
322 323 324 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 322 def fetch(name, *args, &block) @cookies.fetch(name.to_s, *args, &block) end |
#handle_options(options) ⇒ Object
:nodoc:
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 350 def () # :nodoc: if [:expires].respond_to?(:from_now) [:expires] = [:expires].from_now end [:path] ||= "/" if [:domain] == :all || [:domain] == "all" # If there is a provided tld length then we use it otherwise default domain regexp. domain_regexp = [:tld_length] ? /([^.]+\.?){#{[:tld_length]}}$/ : DOMAIN_REGEXP # If host is not ip and matches domain regexp. # (ip confirms to domain regexp so we explicitly check for ip) [:domain] = if (request.host !~ /^[\d.]+$/) && (request.host =~ domain_regexp) ".#{$&}" end elsif [:domain].is_a? Array # If host matches one of the supplied domains without a dot in front of it. [:domain] = [:domain].find { |domain| request.host.include? domain.sub(/^\./, "") } end end |
#key?(name) ⇒ Boolean Also known as: has_key?
326 327 328 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 326 def key?(name) @cookies.key?(name.to_s) end |
#to_header ⇒ Object
346 347 348 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 346 def to_header @cookies.map { |k, v| "#{escape(k)}=#{escape(v)}" }.join "; " end |
#update(other_hash) ⇒ Object
334 335 336 337 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 334 def update(other_hash) @cookies.update other_hash.stringify_keys self end |
#update_cookies_from_jar ⇒ Object
339 340 341 342 343 344 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 339 def request_jar = @request..instance_variable_get(:@cookies) = request_jar.reject { |k, _| @delete_cookies.key?(k) || @set_cookies.key?(k) } @cookies.update if end |
#write(headers) ⇒ Object
422 423 424 425 426 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 422 def write(headers) if header = (headers[HTTP_HEADER]) headers[HTTP_HEADER] = header end end |