Class | WWW::Mechanize::Cookie |
In: |
lib/www/mechanize/cookie.rb
|
Parent: | WEBrick::Cookie |
# File lib/www/mechanize/cookie.rb, line 8 8: def self.parse(uri, str, log = Mechanize.log) 9: return str.split(/,(?=[^;,]*=)|,$/).collect { |c| 10: cookie_elem = c.split(/;+/) 11: first_elem = cookie_elem.shift 12: first_elem.strip! 13: key, value = first_elem.split(/=/, 2) 14: 15: cookie = nil 16: begin 17: cookie = new(key, WEBrick::HTTPUtils.dequote(value)) 18: rescue 19: log.warn("Couldn't parse key/value: #{first_elem}") if log 20: end 21: next unless cookie 22: 23: cookie_elem.each{|pair| 24: pair.strip! 25: key, value = pair.split(/=/, 2) 26: if value 27: value = WEBrick::HTTPUtils.dequote(value.strip) 28: end 29: case key.downcase 30: when "domain" then cookie.domain = value.sub(/^\./, '') 31: when "path" then cookie.path = value 32: when 'expires' 33: begin 34: cookie.expires = Time::parse(value) 35: rescue 36: if log 37: log.warn("Couldn't parse expires: #{value}") 38: end 39: end 40: when "max-age" then 41: begin 42: cookie.max_age = Integer(value) 43: rescue 44: log.warn("Couldn't parse max age '#{value}'") if log 45: cookie.max_age = nil 46: end 47: when "comment" then cookie.comment = value 48: when "version" then 49: begin 50: cookie.version = Integer(value) 51: rescue 52: log.warn("Couldn't parse version '#{value}'") if log 53: cookie.version = nil 54: end 55: when "secure" then cookie.secure = true 56: end 57: } 58: 59: cookie.path ||= uri.path.to_s.sub(/[^\/]*$/, '') 60: cookie.secure ||= false 61: cookie.domain ||= uri.host 62: # Move this in to the cookie jar 63: yield cookie if block_given? 64: } 65: end