Skip to content

Commit

Permalink
Only cleanup cookies once when adding many cookies from a file. GH #38
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Apr 3, 2011
1 parent 5f67268 commit 208e3ed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
list. GH#56
* Improved handling of non ASCII-7bit compatible characters in links (only
an issue on ruby 1.8). GH #36
* Loading cookies.txt is faster. GH #38

=== 1.0.0

Expand Down
7 changes: 2 additions & 5 deletions lib/mechanize/cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ def self.parse(uri, str, log = Mechanize.log)
end

def expired?
if expires.nil?
false
else
Time.now > expires
end
return false unless expires
Time.now > expires
end

def to_s
Expand Down
24 changes: 12 additions & 12 deletions lib/mechanize/cookie_jar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def add(uri, cookie)

@jar[normal_domain][cookie.path] ||= {}
@jar[normal_domain][cookie.path][cookie.name] = cookie
cleanup

cookie
end

Expand Down Expand Up @@ -95,7 +95,7 @@ def save_as(file, format = :yaml)
# :yaml <- YAML structure.
# :cookiestxt <- Mozilla's cookies.txt format
def load(file, format = :yaml)
@jar = ::File.open(file) { |f|
@jar = open(file) { |f|
case format
when :yaml then
YAML::load(f)
Expand All @@ -105,6 +105,10 @@ def load(file, format = :yaml)
raise ArgumentError, "Unknown cookie jar file format"
end
}

cleanup

self
end

# Clear the cookie jar
Expand All @@ -124,26 +128,22 @@ def load_cookiestxt(io)
next if fields.length != 7

expires_seconds = fields[4].to_i
begin
expires = (expires_seconds == 0) ? nil : Time.at(expires_seconds)
rescue
next
# Just in case we ever decide to support DateTime...
# expires = DateTime.new(1970,1,1) + ((expires_seconds + 1) / (60*60*24.0))
end
next if (expires_seconds != 0) && (expires < now)
expires = (expires_seconds == 0) ? nil : Time.at(expires_seconds)
next if expires and (expires < now)

c = Mechanize::Cookie.new(fields[5], fields[6])
c.domain = fields[0]
# Field 1 indicates whether the cookie can be read by other machines at the same domain.
# This is computed by the cookie implementation, based on the domain value.
# Field 1 indicates whether the cookie can be read by other machines at
# the same domain. This is computed by the cookie implementation, based
# on the domain value.
c.path = fields[2] # Path for which the cookie is relevant
c.secure = (fields[3] == "TRUE") # Requires a secure connection
c.expires = expires # Time the cookie expires.
c.version = 0 # Conforms to Netscape cookie spec.

add(FakeURI.new(c.domain), c)
end

@jar
end

Expand Down

0 comments on commit 208e3ed

Please sign in to comment.