From c0157913117acaef9a8f31efa55f9c634c6db6bc Mon Sep 17 00:00:00 2001 From: ore Date: Sat, 25 Jul 2015 10:49:35 +0900 Subject: [PATCH 1/2] fix lastAccessed and creationTime https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsICookie2 creationTime PRInt64 The creation time of the cookie, in microseconds since midnight (00:00:00), January 1, 1970 UTC. lastAccessed PRInt64 The last time the cookie was accessed, in microseconds since midnight (00:00:00) on January 1, 1970 UTC. --- lib/http/cookie_jar/mozilla_store.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/http/cookie_jar/mozilla_store.rb b/lib/http/cookie_jar/mozilla_store.rb index 090eeae..491337b 100644 --- a/lib/http/cookie_jar/mozilla_store.rb +++ b/lib/http/cookie_jar/mozilla_store.rb @@ -265,8 +265,8 @@ class HTTP::CookieJar :host => cookie.dot_domain, :path => cookie.path, :expiry => cookie.expires_at.to_i, - :creationTime => cookie.created_at.to_i, - :lastAccessed => cookie.accessed_at.to_i, + :creationTime => ("%10.6f" % cookie.created_at.to_f), + :lastAccessed => ("%10.6f" % cookie.accessed_at.to_f), :isSecure => cookie.secure? ? 1 : 0, :isHttpOnly => cookie.httponly? ? 1 : 0, }) @@ -333,6 +333,16 @@ class HTTP::CookieJar expiry >= :expiry SQL + def microseconds_to_time(microseconds) + seconds = if microseconds.to_s.size <= 10 + microseconds + else + microseconds / 1_000_000 + end + + Time.at seconds + end + def each(uri = nil, &block) # :yield: cookie now = Time.now if uri @@ -355,8 +365,8 @@ class HTTP::CookieJar attrs[:domain] = row['host'] attrs[:path] = row['path'] attrs[:expires_at] = Time.at(row['expiry']) - attrs[:accessed_at] = Time.at(row['lastAccessed'] || 0) - attrs[:created_at] = Time.at(row['creationTime'] || 0) + attrs[:accessed_at] = microseconds_to_time(row['lastAccessed'] || 0) + attrs[:created_at] = microseconds_to_time(row['creationTime'] || 0) attrs[:secure] = secure attrs[:httponly] = row['isHttpOnly'] != 0 }) @@ -383,8 +393,8 @@ class HTTP::CookieJar attrs[:domain] = row['host'] attrs[:path] = row['path'] attrs[:expires_at] = Time.at(row['expiry']) - attrs[:accessed_at] = Time.at(row['lastAccessed'] || 0) - attrs[:created_at] = Time.at(row['creationTime'] || 0) + attrs[:accessed_at] = microseconds_to_time(row['lastAccessed'] || 0) + attrs[:created_at] = microseconds_to_time(row['creationTime'] || 0) attrs[:secure] = row['isSecure'] != 0 attrs[:httponly] = row['isHttpOnly'] != 0 }) From 9f5a0d65dabd4f0d32103bd72899eaef20c3d3a5 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Fri, 9 Dec 2016 21:12:32 +0900 Subject: [PATCH 2/2] MozillaStore: creationTime and lastAccessed are in usec, not seconds This was pointed out by #8. I decided not to add extra code for backwards compatibility of their values. --- lib/http/cookie_jar/mozilla_store.rb | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/http/cookie_jar/mozilla_store.rb b/lib/http/cookie_jar/mozilla_store.rb index 409cf45..597c440 100644 --- a/lib/http/cookie_jar/mozilla_store.rb +++ b/lib/http/cookie_jar/mozilla_store.rb @@ -367,8 +367,8 @@ class HTTP::CookieJar :host => cookie.dot_domain, :path => cookie.path, :expiry => cookie.expires_at.to_i, - :creationTime => cookie.created_at.to_i, - :lastAccessed => cookie.accessed_at.to_i, + :creationTime => serialize_usectime(cookie.created_at), + :lastAccessed => serialize_usectime(cookie.accessed_at), :isSecure => cookie.secure? ? 1 : 0, :isHttpOnly => cookie.httponly? ? 1 : 0, :appId => @app_id, @@ -399,6 +399,14 @@ class HTTP::CookieJar self end + def serialize_usectime(time) + time ? (time.to_f * 1e6).floor : 0 + end + + def deserialize_usectime(value) + Time.at(value ? value / 1e6 : 0) + end + public def add(cookie) @@ -458,8 +466,8 @@ class HTTP::CookieJar attrs[:domain] = row['host'] attrs[:path] = row['path'] attrs[:expires_at] = Time.at(row['expiry']) - attrs[:accessed_at] = Time.at(row['lastAccessed'] || 0) - attrs[:created_at] = Time.at(row['creationTime'] || 0) + attrs[:accessed_at] = deserialize_usectime(row['lastAccessed']) + attrs[:created_at] = deserialize_usectime(row['creationTime']) attrs[:secure] = secure attrs[:httponly] = row['isHttpOnly'] != 0 }) @@ -467,7 +475,7 @@ class HTTP::CookieJar if cookie.valid_for_uri?(uri) cookie.accessed_at = now @stmt[:update_lastaccessed].execute({ - 'lastAccessed' => now.to_i, + 'lastAccessed' => serialize_usectime(now), 'id' => row['id'], }) yield cookie @@ -486,8 +494,8 @@ class HTTP::CookieJar attrs[:domain] = row['host'] attrs[:path] = row['path'] attrs[:expires_at] = Time.at(row['expiry']) - attrs[:accessed_at] = Time.at(row['lastAccessed'] || 0) - attrs[:created_at] = Time.at(row['creationTime'] || 0) + attrs[:accessed_at] = deserialize_usectime(row['lastAccessed']) + attrs[:created_at] = deserialize_usectime(row['creationTime']) attrs[:secure] = row['isSecure'] != 0 attrs[:httponly] = row['isHttpOnly'] != 0 })