From 52ee861d3a97eb1027ed99eb690bfac6d942dd9c Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 22 Dec 2014 15:06:54 -0500 Subject: [PATCH] load subscriptions --- AddressBook.cpp | 32 ++++++++++++++++++++++++++++++-- AddressBook.h | 9 ++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/AddressBook.cpp b/AddressBook.cpp index a91ca7a3..6e61f4b1 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -177,6 +177,9 @@ namespace client delete m_Storage; } delete m_DefaultSubscription; + for (auto it: m_Subscriptions) + delete it; + } AddressBookStorage * AddressBook::CreateStorage () @@ -321,6 +324,26 @@ namespace client } } + void AddressBook::LoadSubscriptions () + { + if (!m_Subscriptions.size ()) + { + std::ifstream f (i2p::util::filesystem::GetFullPath ("subscriptions.txt").c_str (), std::ofstream::in); // in text mode + if (f.is_open ()) + { + std::string s; + while (!f.eof ()) + { + getline(f, s); + if (!s.length()) continue; // skip empty line + m_Subscriptions.push_back (new AddressBookSubscription (*this, s)); + } + } + } + else + LogPrint (eLogError, "Subscriptions already loaded"); + } + AddressBookSubscription::AddressBookSubscription (AddressBook& book, const std::string& link): m_Book (book), m_Link (link) { @@ -349,9 +372,14 @@ namespace client if (leaseSet) { std::stringstream request, response; + // standard header request << "GET " << u.path_ << " HTTP/1.0\r\nHost: " << u.host_ - << "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n" << "Connection: close\r\n\r\n"; - + << "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n" << "Connection: close\r\n"; + if (m_Etag.length () > 0) // etag + request << HTTP_FIELD_ETAG << ": " << m_Etag << "\r\n"; + if (m_LastModified.length () > 0) // if-modfief-since + request << HTTP_FIELD_IF_MODIFIED_SINCE << ": " << m_LastModified << "\r\n"; + request << "\r\n"; // end of header auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (*leaseSet, u.port_); stream->Send ((uint8_t *)request.str ().c_str (), request.str ().length ()); diff --git a/AddressBook.h b/AddressBook.h index f94e51c7..effcbaa4 100644 --- a/AddressBook.h +++ b/AddressBook.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include "base64.h" @@ -16,6 +17,10 @@ namespace i2p namespace client { const char DEFAULT_SUBSCRIPTION_ADDRESS[] = "http://udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p/hosts.txt"; + // TODO: move http fields to common http code + const char HTTP_FIELD_ETAG[] = "ETag"; + const char HTTP_FIELD_IF_MODIFIED_SINCE[] = "If-Modified-Since"; + const char HTTP_FIELD_LAST_MODIFIED[] = "Last-Modified"; class AddressBookStorage // interface for storage { @@ -50,6 +55,7 @@ namespace client AddressBookStorage * CreateStorage (); void LoadHosts (); + void LoadSubscriptions (); private: @@ -57,6 +63,7 @@ namespace client std::map m_Addresses; AddressBookStorage * m_Storage; volatile bool m_IsLoaded, m_IsDownloading; + std::list m_Subscriptions; AddressBookSubscription * m_DefaultSubscription; // in case if we don't know any addresses yet }; @@ -74,7 +81,7 @@ namespace client private: AddressBook& m_Book; - std::string m_Link; + std::string m_Link, m_Etag, m_LastModified; }; } }