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] |
# File lib/www/mechanize/cookie_jar.rb, line 185 185: def self.strip_port(host) 186: host.gsub(/:[0-9]+$/,'') 187: end
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 =~ /#{CookieJar.strip_port(cookie.domain)}$/i 17: 18: normal_domain = cookie.domain.downcase 19: 20: unless @jar.has_key?(normal_domain) 21: @jar[normal_domain] = Hash.new { |h,k| h[k] = {} } 22: end 23: 24: @jar[normal_domain][cookie.path][cookie.name] = cookie 25: cleanup 26: cookie 27: end
Clear the cookie jar
# File lib/www/mechanize/cookie_jar.rb, line 102 102: def clear! 103: @jar = {} 104: end
Fetch the cookies that should be used for the URI object passed in.
# File lib/www/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| 47: !cookie.expires || Time.now < cookie.expires 48: } 49: end
Write cookies to Mozilla cookies.txt-style IO stream
# File lib/www/mechanize/cookie_jar.rb, line 144 144: def dump_cookiestxt(io) 145: to_a.each do |cookie| 146: fields = [] 147: fields[0] = cookie.domain 148: 149: if cookie.domain =~ /^\./ 150: fields[1] = "TRUE" 151: else 152: fields[1] = "FALSE" 153: end 154: 155: fields[2] = cookie.path 156: 157: if cookie.secure == true 158: fields[3] = "TRUE" 159: else 160: fields[3] = "FALSE" 161: end 162: 163: fields[4] = cookie.expires.to_i.to_s 164: 165: fields[5] = cookie.name 166: fields[6] = cookie.value 167: io.puts(fields.join("\t")) 168: end 169: end
# File lib/www/mechanize/cookie_jar.rb, line 51 51: def empty?(url) 52: cookies(url).length > 0 ? false : true 53: 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 88 88: def load(file, format = :yaml) 89: @jar = ::File.open(file) { |f| 90: case format 91: when :yaml then 92: YAML::load(f) 93: when :cookiestxt then 94: load_cookiestxt(f) 95: else 96: raise "Unknown cookie jar file format" 97: end 98: } 99: end
Read cookies from Mozilla cookies.txt-style IO stream
# File lib/www/mechanize/cookie_jar.rb, line 108 108: def load_cookiestxt(io) 109: now = Time.now 110: fakeuri = Struct.new(:host) # add_cookie wants something resembling a URI. 111: 112: io.each_line do |line| 113: line.chomp! 114: line.gsub!(/#.+/, '') 115: fields = line.split("\t") 116: 117: next if fields.length != 7 118: 119: expires_seconds = fields[4].to_i 120: begin 121: expires = Time.at(expires_seconds) 122: rescue 123: next 124: # Just in case we ever decide to support DateTime... 125: # expires = DateTime.new(1970,1,1) + ((expires_seconds + 1) / (60*60*24.0)) 126: end 127: next if expires < now 128: 129: c = WWW::Mechanize::Cookie.new(fields[5], fields[6]) 130: c.domain = fields[0] 131: # Field 1 indicates whether the cookie can be read by other machines at the same domain. 132: # This is computed by the cookie implementation, based on the domain value. 133: c.path = fields[2] # Path for which the cookie is relevant 134: c.secure = (fields[3] == "TRUE") # Requires a secure connection 135: c.expires = expires # Time the cookie expires. 136: c.version = 0 # Conforms to Netscape cookie spec. 137: 138: add(fakeuri.new(c.domain), c) 139: end 140: @jar 141: 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 70 70: def save_as(file, format = :yaml) 71: ::File.open(file, "w") { |f| 72: case format 73: when :yaml then 74: YAML::dump(@jar, f) 75: when :cookiestxt then 76: dump_cookiestxt(f) 77: else 78: raise "Unknown cookie jar file format" 79: end 80: } 81: end
# File lib/www/mechanize/cookie_jar.rb, line 55 55: def to_a 56: cookies = [] 57: @jar.each do |domain, paths| 58: paths.each do |path, names| 59: cookies << names.values 60: end 61: end 62: cookies.flatten 63: end
Remove expired cookies
# File lib/www/mechanize/cookie_jar.rb, line 173 173: def cleanup 174: @jar.each do |domain, paths| 175: paths.each do |path, names| 176: names.each do |cookie_name, cookie| 177: if cookie.expires && Time.now > cookie.expires 178: paths[path].delete(cookie_name) 179: end 180: end 181: end 182: end 183: end