From 84ccca0e985de516a14972ba6dde9479d95c1d6c Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 15 Mar 2016 14:37:07 -0400 Subject: [PATCH] read persistent ETags --- AddressBook.cpp | 29 ++++++++++++++++++++++++++++- AddressBook.h | 4 ++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/AddressBook.cpp b/AddressBook.cpp index 86d90a4c..2ffebe01 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -37,6 +37,8 @@ namespace client int Save (const std::map& addresses); void SaveEtag (const i2p::data::IdentHash& subsciption, const std::string& etag, const std::string& lastModified); + bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified); + }; bool AddressBookFilesystemStorage::Init() @@ -164,6 +166,17 @@ namespace client } } + bool AddressBookFilesystemStorage::GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified) + { + std::string fname = etagsPath + i2p::fs::dirSep + subscription.ToBase32 () + ".txt"; + std::ifstream f (fname, std::ofstream::in); + if (!f || f.eof ()) return false; + std::getline (f, etag); + if (f.eof ()) return false; + std::getline (f, lastModified); + return true; + } + //--------------------------------------------------------------------- AddressBook::AddressBook (): m_Storage(new AddressBookFilesystemStorage), m_IsLoaded (false), m_IsDownloading (false), m_DefaultSubscription (nullptr), m_SubscriptionsUpdateTimer (nullptr) @@ -355,6 +368,14 @@ namespace client LogPrint (eLogError, "Addressbook: subscriptions already loaded"); } + bool AddressBook::GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified) + { + if (m_Storage) + return m_Storage->GetEtag (subscription, etag, lastModified); + else + return false; + } + void AddressBook::DownloadComplete (bool success, const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified) { m_IsDownloading = false; @@ -457,6 +478,12 @@ namespace client i2p::data::IdentHash ident; if (m_Book.GetIdentHash (u.host_, ident)) { + if (!m_Etag.length ()) + { + // load ETag + m_Book.GetEtag (ident, m_Etag, m_LastModified); + LogPrint (eLogInfo, "Addressbook: set ", m_Link, " ETag: ", m_Etag, " Last-Modified: ", m_LastModified); + } std::condition_variable newDataReceived; std::mutex newDataReceivedMutex; auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (ident); @@ -548,7 +575,7 @@ namespace client !header.compare (colon + 1, std::string::npos, "x-i2p-gzip"); } } - LogPrint (eLogInfo, "Addressbook: ", m_Link, " ETag: ", m_Etag, " Last-Modified: ", m_LastModified); + LogPrint (eLogInfo, "Addressbook: received ", m_Link, " ETag: ", m_Etag, " Last-Modified: ", m_LastModified); if (!response.eof ()) { success = true; diff --git a/AddressBook.h b/AddressBook.h index a22a8b3b..2a1e0737 100644 --- a/AddressBook.h +++ b/AddressBook.h @@ -40,6 +40,7 @@ namespace client virtual int Save (const std::map& addresses) = 0; virtual void SaveEtag (const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified) = 0; + virtual bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified) = 0; }; class AddressBookSubscription; @@ -62,6 +63,9 @@ namespace client //This method returns the ".b32.i2p" address std::string ToAddress(const i2p::data::IdentHash& ident) { return GetB32Address(ident); } std::string ToAddress(std::shared_ptr ident) { return ToAddress(ident->GetIdentHash ()); } + + bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified); + private: void StartSubscriptions ();