Browse Source

* Addressbook.cpp : use new URL class, unwrap some conditions

pull/576/head
hagen 9 years ago
parent
commit
83db868542
  1. 35
      AddressBook.cpp

35
AddressBook.cpp

@ -11,6 +11,7 @@
#include "Identity.h" #include "Identity.h"
#include "FS.h" #include "FS.h"
#include "Log.h" #include "Log.h"
#include "HTTP.h"
#include "NetDb.h" #include "NetDb.h"
#include "ClientContext.h" #include "ClientContext.h"
#include "AddressBook.h" #include "AddressBook.h"
@ -637,13 +638,21 @@ namespace client
void AddressBookSubscription::Request () void AddressBookSubscription::Request ()
{ {
i2p::data::IdentHash ident;
i2p::http::URL url;
// must be run in separate thread // must be run in separate thread
LogPrint (eLogInfo, "Addressbook: Downloading hosts database from ", m_Link, " ETag: ", m_Etag, " Last-Modified: ", m_LastModified); LogPrint (eLogInfo, "Addressbook: Downloading hosts database from ", m_Link);
if (!url.parse(m_Link)) {
LogPrint(eLogError, "Addressbook: failed to parse url: ", m_Link);
return;
}
if (!m_Book.GetIdentHash (url.host, ident)) {
LogPrint (eLogError, "Addressbook: Can't resolve ", url.host);
return;
}
/* all code below till end of function is subject of refacroring */
bool success = false; bool success = false;
i2p::util::http::url u (m_Link);
i2p::data::IdentHash ident;
if (m_Book.GetIdentHash (u.host_, ident))
{
if (!m_Etag.length ()) if (!m_Etag.length ())
{ {
// load ETag // load ETag
@ -671,9 +680,14 @@ namespace client
if (leaseSet) if (leaseSet)
{ {
std::stringstream request, response; std::stringstream request, response;
std::string host = url.host;
int port = url.port ? url.port : 80;
/* make relative url */
url.schema = "";
url.host = "";
// standard header // standard header
request << "GET " << u.path_ << " HTTP/1.1\r\n" request << "GET " << url.to_string() << " HTTP/1.1\r\n"
<< "Host: " << u.host_ << "\r\n" << "Host: " << url.host << "\r\n"
<< "Accept: */*\r\n" << "Accept: */*\r\n"
<< "User-Agent: Wget/1.11.4\r\n" << "User-Agent: Wget/1.11.4\r\n"
//<< "Accept-Encoding: gzip\r\n" //<< "Accept-Encoding: gzip\r\n"
@ -684,7 +698,7 @@ namespace client
if (m_LastModified.length () > 0) // if-modfief-since if (m_LastModified.length () > 0) // if-modfief-since
request << i2p::util::http::IF_MODIFIED_SINCE << ": " << m_LastModified << "\r\n"; request << i2p::util::http::IF_MODIFIED_SINCE << ": " << m_LastModified << "\r\n";
request << "\r\n"; // end of header request << "\r\n"; // end of header
auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (leaseSet, u.port_); auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (leaseSet, port);
stream->Send ((uint8_t *)request.str ().c_str (), request.str ().length ()); stream->Send ((uint8_t *)request.str ().c_str (), request.str ().length ());
uint8_t buf[4096]; uint8_t buf[4096];
@ -766,10 +780,7 @@ namespace client
LogPrint (eLogWarning, "Adressbook: HTTP response ", status); LogPrint (eLogWarning, "Adressbook: HTTP response ", status);
} }
else else
LogPrint (eLogError, "Addressbook: address ", u.host_, " not found"); LogPrint (eLogError, "Addressbook: address ", url.host, " not found");
}
else
LogPrint (eLogError, "Addressbook: Can't resolve ", u.host_);
if (!success) if (!success)
LogPrint (eLogError, "Addressbook: download hosts.txt from ", m_Link, " failed"); LogPrint (eLogError, "Addressbook: download hosts.txt from ", m_Link, " failed");

Loading…
Cancel
Save