From 9f5a0d65dabd4f0d32103bd72899eaef20c3d3a5 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Fri, 9 Dec 2016 21:12:32 +0900 Subject: [PATCH] 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 })