| 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 = nil)
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: cookie = new(key, WEBrick::HTTPUtils.dequote(value))
15: cookie_elem.each{|pair|
16: pair.strip!
17: key, value = pair.split(/=/, 2)
18: if value
19: value = WEBrick::HTTPUtils.dequote(value.strip)
20: end
21: case key.downcase
22: when "domain" then cookie.domain = value.sub(/^\./, '')
23: when "path" then cookie.path = value
24: when 'expires'
25: begin
26: cookie.expires = Time::parse(value)
27: rescue
28: if log
29: log.warn("Couldn't parse expires: #{value}")
30: end
31: end
32: when "max-age" then
33: begin
34: cookie.max_age = Integer(value)
35: rescue
36: log.warn("Couldn't parse max age '#{value}'") if log
37: cookie.max_age = nil
38: end
39: when "comment" then cookie.comment = value
40: when "version" then
41: begin
42: cookie.version = Integer(value)
43: rescue
44: log.warn("Couldn't parse version '#{value}'") if log
45: cookie.version = nil
46: end
47: when "secure" then cookie.secure = true
48: end
49: }
50:
51: cookie.path ||= uri.path.to_s.sub(/[^\/]*$/, '')
52: cookie.secure ||= false
53: cookie.domain ||= uri.host
54: # Move this in to the cookie jar
55: yield cookie if block_given?
56: }
57: end