| Class | WWW::Mechanize::CookieJar |
| In: |
lib/www/mechanize/cookie_jar.rb
|
| Parent: | Object |
This class is used to manage the Cookies that have been returned from any particular website.
| jar | [R] |
Add a cookie to the Jar.
# File lib/www/mechanize/cookie_jar.rb, line 15
15: def add(uri, cookie)
16: return unless uri.host =~ /#{cookie.domain}$/i
17: normal_domain = cookie.domain.downcase
18: unless @jar.has_key?(normal_domain)
19: @jar[normal_domain] = Hash.new
20: end
21:
22: @jar[normal_domain][cookie.name] = cookie
23: cleanup()
24: cookie
25: end
Clear the cookie jar
# File lib/www/mechanize/cookie_jar.rb, line 100
100: def clear!
101: @jar = {}
102: end
Fetch the cookies that should be used for the URI object passed in.
# File lib/www/mechanize/cookie_jar.rb, line 28
28: def cookies(url)
29: cleanup
30: cookies = []
31: url.path = '/' if url.path.empty?
32: @jar.each_key do |domain|
33: if url.host =~ /#{domain}$/i
34: @jar[domain].each_key do |name|
35: if url.path =~ /^#{@jar[domain][name].path}/
36: if @jar[domain][name].expires.nil?
37: cookies << @jar[domain][name]
38: elsif Time.now < @jar[domain][name].expires
39: cookies << @jar[domain][name]
40: end
41: end
42: end
43: end
44: end
45:
46: cookies
47: end
Write cookies to Mozilla cookies.txt-style IO stream
# File lib/www/mechanize/cookie_jar.rb, line 142
142: def dump_cookiestxt(io)
143: @jar.each_pair do |domain, cookies|
144: cookies.each_pair do |name, cookie|
145: fields = []
146: fields[0] = cookie.domain
147:
148: if cookie.domain =~ /^\./
149: fields[1] = "TRUE"
150: else
151: fields[1] = "FALSE"
152: end
153:
154: fields[2] = cookie.path
155:
156: if cookie.secure == true
157: fields[3] = "TRUE"
158: else
159: fields[3] = "FALSE"
160: end
161:
162: fields[4] = cookie.expires.to_i.to_s
163:
164: fields[5] = cookie.name
165: fields[6] = cookie.value
166: io.puts(fields.join("\t"))
167: end
168: end
169: end
# File lib/www/mechanize/cookie_jar.rb, line 49
49: def empty?(url)
50: cookies(url).length > 0 ? false : true
51: end
Load cookie jar from a file in the format specified.
Available formats: :yaml <- YAML structure. :cookiestxt <- Mozilla‘s cookies.txt format
# File lib/www/mechanize/cookie_jar.rb, line 86
86: def load(file, format = :yaml)
87: @jar = ::File.open(file) { |f|
88: case format
89: when :yaml:
90: YAML::load(f)
91: when :cookiestxt:
92: load_cookiestxt(f)
93: else
94: raise "Unknown cookie jar file format"
95: end
96: }
97: end
Read cookies from Mozilla cookies.txt-style IO stream
# File lib/www/mechanize/cookie_jar.rb, line 106
106: def load_cookiestxt(io)
107: now = Time.now
108: fakeuri = Struct.new(:host) # add_cookie wants something resembling a URI.
109:
110: io.each_line do |line|
111: line.chomp!
112: line.gsub!(/#.+/, '')
113: fields = line.split("\t")
114:
115: next if fields.length != 7
116:
117: expires_seconds = fields[4].to_i
118: begin
119: expires = Time.at(expires_seconds)
120: rescue
121: next
122: # Just in case we ever decide to support DateTime...
123: # expires = DateTime.new(1970,1,1) + ((expires_seconds + 1) / (60*60*24.0))
124: end
125: next if expires < now
126:
127: c = WWW::Mechanize::Cookie.new(fields[5], fields[6])
128: c.domain = fields[0]
129: # Field 1 indicates whether the cookie can be read by other machines at the same domain.
130: # This is computed by the cookie implementation, based on the domain value.
131: c.path = fields[2] # Path for which the cookie is relevant
132: c.secure = (fields[3] == "TRUE") # Requires a secure connection
133: c.expires = expires # Time the cookie expires.
134: c.version = 0 # Conforms to Netscape cookie spec.
135:
136: add(fakeuri.new(c.domain), c)
137: end
138: @jar
139: end
Save the cookie jar to a file in the format specified.
Available formats: :yaml <- YAML structure :cookiestxt <- Mozilla‘s cookies.txt format
# File lib/www/mechanize/cookie_jar.rb, line 68
68: def save_as(file, format = :yaml)
69: ::File.open(file, "w") { |f|
70: case format
71: when :yaml:
72: YAML::dump(@jar, f)
73: when :cookiestxt:
74: dump_cookiestxt(f)
75: else
76: raise "Unknown cookie jar file format"
77: end
78: }
79: end
# File lib/www/mechanize/cookie_jar.rb, line 53
53: def to_a
54: cookies = []
55: @jar.each_key do |domain|
56: @jar[domain].each_key do |name|
57: cookies << @jar[domain][name]
58: end
59: end
60: cookies
61: end
Remove expired cookies
# File lib/www/mechanize/cookie_jar.rb, line 173
173: def cleanup
174: @jar.each_key do |domain|
175: @jar[domain].each_key do |name|
176: unless @jar[domain][name].expires.nil?
177: if Time.now > @jar[domain][name].expires
178: @jar[domain].delete(name)
179: end
180: end
181: end
182: end
183: end