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