Class: HTTP::CookieJar::HashStore
- Inherits:
-
AbstractStore
- Object
- AbstractStore
- HTTP::CookieJar::HashStore
- Defined in:
- lib/http/cookie_jar/hash_store.rb
Overview
A store class that uses a hash-based cookie store.
In this store, cookies that share the same name, domain and path will overwrite each other regardless of the ‘for_domain` flag value. This store is built after the storage model described in RFC 6265 5.3 where there is no mention of how the host-only-flag affects in storing cookies. On the other hand, in MozillaStore two cookies with the same name, domain and path coexist as long as they differ in the `for_domain` flag value, which means they need to be expired individually.
Instance Method Summary collapse
- #add(cookie) ⇒ Object
- #cleanup(session = false) ⇒ Object
- #clear ⇒ Object
- #default_options ⇒ Object
- #delete(cookie) ⇒ Object
-
#each(uri = nil) ⇒ Object
:yield: cookie.
-
#initialize(options = nil) ⇒ HashStore
constructor
:call-seq: new(**options).
-
#initialize_copy(other) ⇒ Object
The copy constructor.
Methods inherited from AbstractStore
class_to_symbol, #empty?, implementation, inherited
Constructor Details
#initialize(options = nil) ⇒ HashStore
:call-seq:
new(**)
Generates a hash based cookie store.
Available option keywords are as below:
:gc_threshold : GC threshold; A GC happens when this many times cookies have been stored (default: ‘HTTP::Cookie::MAX_COOKIES_TOTAL / 20`)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/http/cookie_jar/hash_store.rb', line 32 def initialize( = nil) super @jar = { # hostname => { # path => { # name => cookie, # ... # }, # ... # }, # ... } @gc_index = 0 end |
Instance Method Details
#add(cookie) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/http/cookie_jar/hash_store.rb', line 54 def add() = ((@jar[.domain] ||= {})[.path] ||= {}) [.name] = cleanup if (@gc_index += 1) >= @gc_threshold self end |
#cleanup(session = false) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/http/cookie_jar/hash_store.rb', line 113 def cleanup(session = false) now = Time.now = [] synchronize { break if @gc_index == 0 @jar.each { |domain, paths| = [] paths.each { |path, hash| hash.delete_if { |name, | if .expired?(now) || (session && .session?) true else << false end } } if (debt = .size - HTTP::Cookie::MAX_COOKIES_PER_DOMAIN) > 0 .sort_by!(&:created_at) .slice!(0, debt).each { || delete() } end .concat() } if (debt = .size - HTTP::Cookie::MAX_COOKIES_TOTAL) > 0 .sort_by!(&:created_at) .slice!(0, debt).each { || delete() } end @jar.delete_if { |domain, paths| paths.delete_if { |path, hash| hash.empty? } paths.empty? } @gc_index = 0 } self end |
#clear ⇒ Object
108 109 110 111 |
# File 'lib/http/cookie_jar/hash_store.rb', line 108 def clear @jar.clear self end |
#default_options ⇒ Object
16 17 18 19 20 |
# File 'lib/http/cookie_jar/hash_store.rb', line 16 def { :gc_threshold => HTTP::Cookie::MAX_COOKIES_TOTAL / 20 } end |
#delete(cookie) ⇒ Object
61 62 63 64 65 |
# File 'lib/http/cookie_jar/hash_store.rb', line 61 def delete() = ((@jar[.domain] ||= {})[.path] ||= {}) .delete(.name) self end |
#each(uri = nil) ⇒ Object
:yield: cookie
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 |
# File 'lib/http/cookie_jar/hash_store.rb', line 67 def each(uri = nil) # :yield: cookie now = Time.now if uri thost = DomainName.new(uri.host) tpath = uri.path @jar.each { |domain, paths| next unless thost.(domain) paths.each { |path, hash| next unless HTTP::Cookie.path_match?(path, tpath) hash.delete_if { |name, | if .expired?(now) true else if .valid_for_uri?(uri) .accessed_at = now yield end false end } } } else synchronize { @jar.each { |domain, paths| paths.each { |path, hash| hash.delete_if { |name, | if .expired?(now) true else yield false end } } } } end self end |
#initialize_copy(other) ⇒ Object
The copy constructor. This store class supports cloning.
50 51 52 |
# File 'lib/http/cookie_jar/hash_store.rb', line 50 def initialize_copy(other) @jar = Marshal.load(Marshal.dump(other.instance_variable_get(:@jar))) end |