Permalink
Browse files

Sort cookies by path length and creation time as requested by RFC 6265.

Creation time and last-access time are introduced while at it.
  • Loading branch information...
knu committed Nov 8, 2011
1 parent 363b1d3 commit cad4a59992ce3ae89bd374f0d1b9cd22c69d3158
Showing with 10 additions and 1 deletion.
  1. +4 −0 lib/mechanize/cookie.rb
  2. +6 −1 lib/mechanize/cookie_jar.rb
View
@@ -11,6 +11,9 @@ class Mechanize::Cookie
attr_accessor :session
+ attr_accessor :created_at
+ attr_accessor :accessed_at
+
# :call-seq:
# new(name, value)
# new(name, value, attr_hash)
@@ -32,6 +35,7 @@ def initialize(*args)
@domain = @path = @secure = @comment = @max_age =
@expires = @comment_url = @discard = @port = nil
+ @created_at = @accessed_at = Time.now
case args.size
when 2
@name, @value = *args
@@ -43,9 +43,14 @@ def add!(cookie)
def cookies(url)
cleanup
url.path = '/' if url.path.empty?
+ now = Time.now
select { |cookie|
- !cookie.expired? && cookie.valid_for_uri?(url)
+ !cookie.expired? && cookie.valid_for_uri?(url) && (cookie.accessed_at = now)
+ }.sort_by { |cookie|
+ # RFC 6265 5.4
+ # Precedence: 1. longer path 2. older creation
+ [-cookie.path.length, cookie.created_at]
}
end

0 comments on commit cad4a59

Please sign in to comment.