From e1d445ab50b5d132bf30be4f43440dd7a4372505 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 22 Dec 2014 16:45:50 -0500 Subject: [PATCH] parse HTTP header fields --- AddressBook.cpp | 23 +++++++++++++++++++---- AddressBook.h | 6 +----- Reseed.cpp | 1 + util.h | 5 +++++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/AddressBook.cpp b/AddressBook.cpp index 6e61f4b1..1b453b7d 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -290,7 +290,7 @@ namespace client } - void AddressBook::LoadHostsFromStream (std::istream& f) + void AddressBook::LoadHostsFromStream (std::istream& f, bool isChunked) { std::unique_lock l(m_AddressBookMutex); int numAddresses = 0; @@ -376,9 +376,9 @@ namespace client 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"; if (m_Etag.length () > 0) // etag - request << HTTP_FIELD_ETAG << ": " << m_Etag << "\r\n"; + request << i2p::util::http::ETAG << ": " << m_Etag << "\r\n"; if (m_LastModified.length () > 0) // if-modfief-since - request << HTTP_FIELD_IF_MODIFIED_SINCE << ": " << m_LastModified << "\r\n"; + request << i2p::util::http::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 ()); @@ -411,13 +411,28 @@ namespace client response >> status; // status if (status == 200) // OK { + bool isChunked = false; std::string header, statusMessage; std::getline (response, statusMessage); // read until new line meaning end of header while (!response.eof () && header != "\r") + { std::getline (response, header); + auto colon = header.find (':'); + if (colon != std::string::npos) + { + std::string field = header.substr (0, colon); + header.resize (header.length () - 1); // delete \r + if (field == i2p::util::http::ETAG) + m_Etag = header.substr (colon + 1); + else if (field == i2p::util::http::LAST_MODIFIED) + m_LastModified = header.substr (colon + 1); + else if (field == i2p::util::http::TRANSFER_ENCODING) + isChunked = !header.compare (colon + 1, std::string::npos, "chunked"); + } + } if (!response.eof ()) - m_Book.LoadHostsFromStream (response); + m_Book.LoadHostsFromStream (response, isChunked); } else LogPrint (eLogWarning, "Adressbook HTTP response ", status); diff --git a/AddressBook.h b/AddressBook.h index effcbaa4..20234144 100644 --- a/AddressBook.h +++ b/AddressBook.h @@ -17,10 +17,6 @@ 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 { @@ -48,7 +44,7 @@ namespace client void InsertAddress (const std::string& address, const std::string& base64); // for jump service void InsertAddress (const i2p::data::IdentityEx& address); - void LoadHostsFromStream (std::istream& f); + void LoadHostsFromStream (std::istream& f, bool isChunked = false); void SetIsDownloading (bool isDownloading) { m_IsDownloading = isDownloading; }; private: diff --git a/Reseed.cpp b/Reseed.cpp index 9c72dc84..47141f7b 100644 --- a/Reseed.cpp +++ b/Reseed.cpp @@ -133,6 +133,7 @@ namespace data { CryptoPP::AutoSeededRandomPool rnd; auto ind = rnd.GenerateWord32 (0, httpReseedHostList.size() - 1); +ind =5; std::string reseedHost = httpReseedHostList[ind]; return ReseedFromSU3 (reseedHost); } diff --git a/util.h b/util.h index 862885a9..29012137 100644 --- a/util.h +++ b/util.h @@ -39,6 +39,11 @@ namespace util namespace http { + const char ETAG[] = "ETag"; + const char IF_MODIFIED_SINCE[] = "If-Modified-Since"; + const char LAST_MODIFIED[] = "Last-Modified"; + const char TRANSFER_ENCODING[] = "Transfer-Encoding"; + std::string httpRequest(const std::string& address); int httpRequestViaI2pProxy(const std::string& address, std::string &content); // return http code