Class: ActionDispatch::Cookies::CookieJar
- Includes:
- ChainedCookieJars, Enumerable
- Defined in:
- actionpack/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
/[^.]*\.([^.]*|..\...|...\...)$/
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(key_generator, host = nil, secure = false, options = {}) ⇒ CookieJar
constructor
A new instance of CookieJar.
- #key?(name) ⇒ Boolean (also: #has_key?)
-
#recycle! ⇒ Object
:nodoc:.
- #update(other_hash) ⇒ Object
- #write(headers) ⇒ Object
Methods included from Enumerable
#as_json, #exclude?, #index_by, #many?, #sum
Methods included from ChainedCookieJars
#encrypted, #permanent, #signed, #signed_or_encrypted
Constructor Details
#initialize(key_generator, host = nil, secure = false, options = {}) ⇒ CookieJar
Returns a new instance of CookieJar.
232 233 234 235 236 237 238 239 240 241 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 232 def initialize(key_generator, host = nil, secure = false, = {}) @key_generator = key_generator @set_cookies = {} @delete_cookies = {} @host = host @secure = secure @options = @cookies = {} @committed = false end |
Class Method Details
.build(request) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 219 def self.build(request) env = request.env key_generator = env[GENERATOR_KEY] = env host = request.host secure = request.ssl? new(key_generator, host, secure, ).tap do |hash| hash.update(request.) end end |
.options_for_env(env) ⇒ Object
:nodoc:
208 209 210 211 212 213 214 215 216 217 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 208 def self.(env) #:nodoc: { signed_cookie_salt: env[SIGNED_COOKIE_SALT] || '', encrypted_cookie_salt: env[ENCRYPTED_COOKIE_SALT] || '', encrypted_signed_cookie_salt: env[ENCRYPTED_SIGNED_COOKIE_SALT] || '', secret_token: env[SECRET_TOKEN], secret_key_base: env[SECRET_KEY_BASE], upgrade_legacy_signed_cookies: env[SECRET_TOKEN].present? && env[SECRET_KEY_BASE].present?, serializer: env[COOKIES_SERIALIZER] } end |
Instance Method Details
#[](name) ⇒ Object
Returns the value of the cookie by name
, or nil
if no such cookie exists.
256 257 258 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 256 def [](name) @cookies[name.to_s] end |
#[]=(name, options) ⇒ Object
Sets the cookie named name
. The second argument may be the very cookie value, or a hash of options as documented above.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 294 def []=(name, ) if .is_a?(Hash) .symbolize_keys! value = [:value] else value = = { :value => value } end () if @cookies[name.to_s] != value or [: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
338 339 340 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 338 def clear( = {}) @cookies.each_key{ |k| delete(k, ) } end |
#commit! ⇒ Object
245 246 247 248 249 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 245 def commit! @committed = true @set_cookies.freeze @delete_cookies.freeze end |
#committed? ⇒ Boolean
243 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 243 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
.
317 318 319 320 321 322 323 324 325 326 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 317 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.
331 332 333 334 335 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 331 def deleted?(name, = {}) .symbolize_keys! () @delete_cookies[name.to_s] == end |
#each(&block) ⇒ Object
251 252 253 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 251 def each(&block) @cookies.each(&block) end |
#fetch(name, *args, &block) ⇒ Object
260 261 262 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 260 def fetch(name, *args, &block) @cookies.fetch(name.to_s, *args, &block) end |
#handle_options(options) ⇒ Object
:nodoc:
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 274 def () #:nodoc: [:path] ||= "/" if [: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 (@host !~ /^[\d.]+$/) && (@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| @host.include? domain.sub(/^\./, '') } end end |
#key?(name) ⇒ Boolean Also known as: has_key?
264 265 266 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 264 def key?(name) @cookies.key?(name.to_s) end |
#recycle! ⇒ Object
:nodoc:
347 348 349 350 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 347 def recycle! #:nodoc: @set_cookies = {} @delete_cookies = {} end |
#update(other_hash) ⇒ Object
269 270 271 272 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 269 def update(other_hash) @cookies.update other_hash.stringify_keys self end |