Skip to content

Commit

Permalink
parse HTTP header fields
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Dec 22, 2014
1 parent b1b72d2 commit e1d445a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
23 changes: 19 additions & 4 deletions AddressBook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ namespace client

}

void AddressBook::LoadHostsFromStream (std::istream& f)
void AddressBook::LoadHostsFromStream (std::istream& f, bool isChunked)
{
std::unique_lock<std::mutex> l(m_AddressBookMutex);
int numAddresses = 0;
Expand Down Expand Up @@ -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 ());
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions AddressBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions Reseed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e1d445a

Please sign in to comment.