Hold session cookies in an internal HashStore.

This commit is contained in:
Akinori MUSHA 2013-03-28 02:47:46 +09:00
parent 11a9df8559
commit 91b5184d96

View file

@ -5,6 +5,9 @@ require 'sqlite3'
class HTTP::CookieJar class HTTP::CookieJar
# A store class that uses Mozilla compatible SQLite3 database as # A store class that uses Mozilla compatible SQLite3 database as
# backing store. # backing store.
#
# Session cookies are stored separately on memory and will not be
# stored persistently in the SQLite3 database.
class MozillaStore < AbstractStore class MozillaStore < AbstractStore
SCHEMA_VERSION = 5 SCHEMA_VERSION = 5
@ -55,6 +58,7 @@ class HTTP::CookieJar
@filename = options[:filename] or raise ArgumentError, ':filename option is missing' @filename = options[:filename] or raise ArgumentError, ':filename option is missing'
@sjar = HashStore.new
@db = SQLite3::Database.new(@filename) @db = SQLite3::Database.new(@filename)
@db.results_as_hash = true @db.results_as_hash = true
@ -188,9 +192,7 @@ class HTTP::CookieJar
end end
end end
public def db_add(cookie)
def add(cookie)
@st_add ||= @st_add ||=
@db.prepare('INSERT OR REPLACE INTO moz_cookies (%s) VALUES (%s)' % [ @db.prepare('INSERT OR REPLACE INTO moz_cookies (%s) VALUES (%s)' % [
ALL_COLUMNS.join(', '), ALL_COLUMNS.join(', '),
@ -215,7 +217,7 @@ class HTTP::CookieJar
self self
end end
def delete(cookie) def db_delete(cookie)
@st_delete ||= @st_delete ||=
@db.prepare(<<-'SQL') @db.prepare(<<-'SQL')
DELETE FROM moz_cookies DELETE FROM moz_cookies
@ -236,6 +238,23 @@ class HTTP::CookieJar
self self
end 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) def each(uri = nil)
now = Time.now now = Time.now
if uri if uri
@ -285,6 +304,7 @@ class HTTP::CookieJar
yield cookie yield cookie
end end
} }
@sjar.each(uri, &proc)
else else
@st_all_cookies ||= @st_all_cookies ||=
@db.prepare(<<-'SQL') @db.prepare(<<-'SQL')
@ -313,12 +333,14 @@ class HTTP::CookieJar
yield cookie yield cookie
} }
@sjar.each(&proc)
end end
self self
end end
def clear def clear
@db.execute("DELETE FROM moz_cookies") @db.execute("DELETE FROM moz_cookies")
@sjar.clear
self self
end end
@ -331,7 +353,7 @@ class HTTP::CookieJar
protected :count protected :count
def empty? def empty?
count == 0 @sjar.empty? && count == 0
end end
def cleanup(session = false) def cleanup(session = false)