|
|
|
@ -833,40 +833,22 @@ namespace client
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
m_Ident = addr->identHash;
|
|
|
|
|
/* this code block still needs some love */
|
|
|
|
|
std::condition_variable newDataReceived;
|
|
|
|
|
std::mutex newDataReceivedMutex;
|
|
|
|
|
auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (m_Ident);
|
|
|
|
|
if (!leaseSet)
|
|
|
|
|
// save url parts for later use
|
|
|
|
|
std::string dest_host = url.host;
|
|
|
|
|
int dest_port = url.port ? url.port : 80;
|
|
|
|
|
// try to create stream to addressbook site
|
|
|
|
|
auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (m_Ident, dest_port);
|
|
|
|
|
if (!stream)
|
|
|
|
|
{
|
|
|
|
|
std::unique_lock<std::mutex> l(newDataReceivedMutex);
|
|
|
|
|
i2p::client::context.GetSharedLocalDestination ()->RequestDestination (m_Ident,
|
|
|
|
|
[&newDataReceived, &leaseSet, &newDataReceivedMutex](std::shared_ptr<i2p::data::LeaseSet> ls)
|
|
|
|
|
{
|
|
|
|
|
leaseSet = ls;
|
|
|
|
|
std::unique_lock<std::mutex> l1(newDataReceivedMutex);
|
|
|
|
|
newDataReceived.notify_all ();
|
|
|
|
|
});
|
|
|
|
|
if (newDataReceived.wait_for (l, std::chrono::seconds (SUBSCRIPTION_REQUEST_TIMEOUT)) == std::cv_status::timeout)
|
|
|
|
|
{
|
|
|
|
|
LogPrint (eLogError, "Addressbook: Subscription LeaseSet request timeout expired");
|
|
|
|
|
i2p::client::context.GetSharedLocalDestination ()->CancelDestinationRequest (m_Ident, false); // don't notify, because we know it already
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!leaseSet) {
|
|
|
|
|
/* still no leaseset found */
|
|
|
|
|
LogPrint (eLogError, "Addressbook: LeaseSet for address ", url.host, " not found");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (m_Etag.empty() && m_LastModified.empty()) {
|
|
|
|
|
}
|
|
|
|
|
if (m_Etag.empty() && m_LastModified.empty())
|
|
|
|
|
{
|
|
|
|
|
m_Book.GetEtag (m_Ident, m_Etag, m_LastModified);
|
|
|
|
|
LogPrint (eLogDebug, "Addressbook: Loaded for ", url.host, ": ETag: ", m_Etag, ", Last-Modified: ", m_LastModified);
|
|
|
|
|
}
|
|
|
|
|
/* save url parts for later use */
|
|
|
|
|
std::string dest_host = url.host;
|
|
|
|
|
int dest_port = url.port ? url.port : 80;
|
|
|
|
|
/* create http request & send it */
|
|
|
|
|
// create http request & send it
|
|
|
|
|
i2p::http::HTTPReq req;
|
|
|
|
|
req.AddHeader("Host", dest_host);
|
|
|
|
|
req.AddHeader("User-Agent", "Wget/1.11.4");
|
|
|
|
@ -877,15 +859,14 @@ namespace client
|
|
|
|
|
req.AddHeader("If-None-Match", m_Etag);
|
|
|
|
|
if (!m_LastModified.empty())
|
|
|
|
|
req.AddHeader("If-Modified-Since", m_LastModified);
|
|
|
|
|
/* convert url to relative */
|
|
|
|
|
// convert url to relative
|
|
|
|
|
url.schema = "";
|
|
|
|
|
url.host = "";
|
|
|
|
|
req.uri = url.to_string();
|
|
|
|
|
req.version = "HTTP/1.1";
|
|
|
|
|
auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (leaseSet, dest_port);
|
|
|
|
|
std::string request = req.to_string();
|
|
|
|
|
stream->Send ((const uint8_t *) request.data(), request.length());
|
|
|
|
|
/* read response */
|
|
|
|
|
// read response
|
|
|
|
|
std::string response;
|
|
|
|
|
uint8_t recv_buf[4096];
|
|
|
|
|
bool end = false;
|
|
|
|
@ -910,7 +891,7 @@ namespace client
|
|
|
|
|
// process remaining buffer
|
|
|
|
|
while (size_t len = stream->ReadSome (recv_buf, sizeof(recv_buf)))
|
|
|
|
|
response.append ((char *)recv_buf, len);
|
|
|
|
|
/* parse response */
|
|
|
|
|
// parse response
|
|
|
|
|
i2p::http::HTTPRes res;
|
|
|
|
|
int res_head_len = res.parse(response);
|
|
|
|
|
if (res_head_len < 0)
|
|
|
|
@ -923,7 +904,7 @@ namespace client
|
|
|
|
|
LogPrint(eLogError, "Addressbook: Incomplete http response from ", dest_host, ", interrupted by timeout");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
/* assert: res_head_len > 0 */
|
|
|
|
|
// assert: res_head_len > 0
|
|
|
|
|
response.erase(0, res_head_len);
|
|
|
|
|
if (res.code == 304)
|
|
|
|
|
{
|
|
|
|
@ -946,7 +927,7 @@ namespace client
|
|
|
|
|
LogPrint(eLogError, "Addressbook: Response size mismatch, expected: ", len, ", got: ", response.length(), "bytes");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
/* assert: res.code == 200 */
|
|
|
|
|
// assert: res.code == 200
|
|
|
|
|
auto it = res.headers.find("ETag");
|
|
|
|
|
if (it != res.headers.end()) m_Etag = it->second;
|
|
|
|
|
it = res.headers.find("Last-Modified");
|
|
|
|
|