From 91b5184d96b754906979fdaa8015b0e248bb7ed5 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Thu, 28 Mar 2013 02:47:46 +0900 Subject: [PATCH] Hold session cookies in an internal HashStore. --- lib/http/cookie_jar/mozilla_store.rb | 32 +++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/http/cookie_jar/mozilla_store.rb b/lib/http/cookie_jar/mozilla_store.rb index 67e950d..5e1aa79 100644 --- a/lib/http/cookie_jar/mozilla_store.rb +++ b/lib/http/cookie_jar/mozilla_store.rb @@ -5,6 +5,9 @@ require 'sqlite3' class HTTP::CookieJar # A store class that uses Mozilla compatible SQLite3 database as # backing store. + # + # Session cookies are stored separately on memory and will not be + # stored persistently in the SQLite3 database. class MozillaStore < AbstractStore SCHEMA_VERSION = 5 @@ -55,6 +58,7 @@ class HTTP::CookieJar @filename = options[:filename] or raise ArgumentError, ':filename option is missing' + @sjar = HashStore.new @db = SQLite3::Database.new(@filename) @db.results_as_hash = true @@ -188,9 +192,7 @@ class HTTP::CookieJar end end - public - - def add(cookie) + def db_add(cookie) @st_add ||= @db.prepare('INSERT OR REPLACE INTO moz_cookies (%s) VALUES (%s)' % [ ALL_COLUMNS.join(', '), @@ -215,7 +217,7 @@ class HTTP::CookieJar self end - def delete(cookie) + def db_delete(cookie) @st_delete ||= @db.prepare(<<-'SQL') DELETE FROM moz_cookies @@ -236,6 +238,23 @@ class HTTP::CookieJar self end + public + + def add(cookie) + if cookie.session? + @sjar.add(cookie) + db_delete(cookie) + else + @sjar.delete(cookie) + db_add(cookie) + end + end + + def delete(cookie) + @sjar.delete(cookie) + db_delete(cookie) + end + def each(uri = nil) now = Time.now if uri @@ -285,6 +304,7 @@ class HTTP::CookieJar yield cookie end } + @sjar.each(uri, &proc) else @st_all_cookies ||= @db.prepare(<<-'SQL') @@ -313,12 +333,14 @@ class HTTP::CookieJar yield cookie } + @sjar.each(&proc) end self end def clear @db.execute("DELETE FROM moz_cookies") + @sjar.clear self end @@ -331,7 +353,7 @@ class HTTP::CookieJar protected :count def empty? - count == 0 + @sjar.empty? && count == 0 end def cleanup(session = false)