mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 08:14:15 +00:00
* Addressbook.cpp : use HTTPRes class for response parsing
This commit is contained in:
parent
403e34506e
commit
b6c336bf72
167
AddressBook.cpp
167
AddressBook.cpp
@ -698,108 +698,77 @@ namespace client
|
|||||||
auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (leaseSet, dest_port);
|
auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (leaseSet, dest_port);
|
||||||
std::string request = req.to_string();
|
std::string request = req.to_string();
|
||||||
stream->Send ((const uint8_t *) request.data(), request.length());
|
stream->Send ((const uint8_t *) request.data(), request.length());
|
||||||
/* all code below till end of function is subject of refacroring */
|
/* read response */
|
||||||
bool success = false;
|
std::string response;
|
||||||
std::stringstream response;
|
uint8_t recv_buf[4096];
|
||||||
|
bool end = false;
|
||||||
uint8_t buf[4096];
|
while (!end) {
|
||||||
bool end = false;
|
stream->AsyncReceive (boost::asio::buffer (recv_buf, 4096),
|
||||||
while (!end)
|
[&](const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||||||
{
|
{
|
||||||
stream->AsyncReceive (boost::asio::buffer (buf, 4096),
|
if (bytes_transferred)
|
||||||
[&](const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
response.append ((char *)recv_buf, bytes_transferred);
|
||||||
{
|
if (ecode == boost::asio::error::timed_out || !stream->IsOpen ())
|
||||||
if (bytes_transferred)
|
end = true;
|
||||||
response.write ((char *)buf, bytes_transferred);
|
newDataReceived.notify_all ();
|
||||||
if (ecode == boost::asio::error::timed_out || !stream->IsOpen ())
|
},
|
||||||
end = true;
|
30); // wait for 30 seconds
|
||||||
newDataReceived.notify_all ();
|
std::unique_lock<std::mutex> l(newDataReceivedMutex);
|
||||||
},
|
if (newDataReceived.wait_for (l, std::chrono::seconds (SUBSCRIPTION_REQUEST_TIMEOUT)) == std::cv_status::timeout)
|
||||||
30); // wait for 30 seconds
|
LogPrint (eLogError, "Addressbook: subscriptions request timeout expired");
|
||||||
std::unique_lock<std::mutex> l(newDataReceivedMutex);
|
}
|
||||||
if (newDataReceived.wait_for (l, std::chrono::seconds (SUBSCRIPTION_REQUEST_TIMEOUT)) == std::cv_status::timeout)
|
// process remaining buffer
|
||||||
LogPrint (eLogError, "Addressbook: subscriptions request timeout expired");
|
while (size_t len = stream->ReadSome (recv_buf, sizeof(recv_buf))) {
|
||||||
}
|
response.append ((char *)recv_buf, len);
|
||||||
// process remaining buffer
|
}
|
||||||
while (size_t len = stream->ReadSome (buf, 4096))
|
/* parse response */
|
||||||
response.write ((char *)buf, len);
|
i2p::http::HTTPRes res;
|
||||||
|
int res_head_len = res.parse(response);
|
||||||
// parse response
|
if (res_head_len < 0) {
|
||||||
std::string version;
|
LogPrint(eLogError, "Addressbook: can't parse http response from ", dest_host);
|
||||||
response >> version; // HTTP version
|
return;
|
||||||
int status = 0;
|
}
|
||||||
response >> status; // status
|
if (res_head_len == 0) {
|
||||||
if (status == 200) // OK
|
LogPrint(eLogError, "Addressbook: incomplete http response from ", dest_host, ", interrupted by timeout");
|
||||||
{
|
return;
|
||||||
bool isChunked = false, isGzip = false;
|
}
|
||||||
m_Etag = ""; m_LastModified = "";
|
/* assert: res_head_len > 0 */
|
||||||
std::string header, statusMessage;
|
response.erase(0, res_head_len);
|
||||||
std::getline (response, statusMessage);
|
if (res.code == 304) {
|
||||||
// read until new line meaning end of header
|
LogPrint (eLogInfo, "Addressbook: no updates from ", dest_host, ", code 304");
|
||||||
while (!response.eof () && header != "\r")
|
return;
|
||||||
{
|
}
|
||||||
std::getline (response, header);
|
if (res.code != 200) {
|
||||||
if (response.fail ()) break;
|
LogPrint (eLogWarning, "Adressbook: can't get updates from ", dest_host, ", response code ", res.code);
|
||||||
auto colon = header.find (':');
|
return;
|
||||||
if (colon != std::string::npos)
|
}
|
||||||
{
|
/* assert: res.code == 200 */
|
||||||
std::string field = header.substr (0, colon);
|
auto it = res.headers.find("ETag");
|
||||||
boost::to_lower (field); // field are not case-sensitive
|
if (it != res.headers.end()) {
|
||||||
colon++;
|
m_Etag = it->second;
|
||||||
header.resize (header.length () - 1); // delete \r
|
}
|
||||||
if (field == i2p::util::http::ETAG)
|
it = res.headers.find("If-Modified-Since");
|
||||||
m_Etag = header.substr (colon + 1);
|
if (it != res.headers.end()) {
|
||||||
else if (field == i2p::util::http::LAST_MODIFIED)
|
m_LastModified = it->second;
|
||||||
m_LastModified = header.substr (colon + 1);
|
}
|
||||||
else if (field == i2p::util::http::TRANSFER_ENCODING)
|
if (res.is_chunked()) {
|
||||||
isChunked = !header.compare (colon + 1, std::string::npos, "chunked");
|
std::stringstream in(response), out;
|
||||||
else if (field == i2p::util::http::CONTENT_ENCODING)
|
i2p::http::MergeChunkedResponse (in, out);
|
||||||
isGzip = !header.compare (colon + 1, std::string::npos, "gzip") ||
|
response = out.str();
|
||||||
!header.compare (colon + 1, std::string::npos, "x-i2p-gzip");
|
} else if (res.is_gzipped()) {
|
||||||
}
|
std::stringstream out;
|
||||||
}
|
|
||||||
LogPrint (eLogInfo, "Addressbook: received ", m_Link, " ETag: ", m_Etag, " Last-Modified: ", m_LastModified);
|
|
||||||
if (!response.eof () && !response.fail ())
|
|
||||||
{
|
|
||||||
if (!isChunked)
|
|
||||||
success = ProcessResponse (response, isGzip);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// merge chunks
|
|
||||||
std::stringstream merged;
|
|
||||||
i2p::util::http::MergeChunkedResponse (response, merged);
|
|
||||||
success = ProcessResponse (merged, isGzip);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (status == 304)
|
|
||||||
{
|
|
||||||
success = true;
|
|
||||||
LogPrint (eLogInfo, "Addressbook: no updates from ", m_Link);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
LogPrint (eLogWarning, "Adressbook: HTTP response ", status);
|
|
||||||
|
|
||||||
if (!success)
|
|
||||||
LogPrint (eLogError, "Addressbook: download hosts.txt from ", m_Link, " failed");
|
|
||||||
|
|
||||||
m_Book.DownloadComplete (success, ident, m_Etag, m_LastModified);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AddressBookSubscription::ProcessResponse (std::stringstream& s, bool isGzip)
|
|
||||||
{
|
|
||||||
if (isGzip)
|
|
||||||
{
|
|
||||||
std::stringstream uncompressed;
|
|
||||||
i2p::data::GzipInflator inflator;
|
i2p::data::GzipInflator inflator;
|
||||||
inflator.Inflate (s, uncompressed);
|
inflator.Inflate ((const uint8_t *) response.data(), response.length(), out);
|
||||||
if (!uncompressed.fail ())
|
if (out.fail()) {
|
||||||
return m_Book.LoadHostsFromStream (uncompressed);
|
LogPrint(eLogError, "Addressbook: can't gunzip http response");
|
||||||
else
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
response = out.str();
|
||||||
return m_Book.LoadHostsFromStream (s);
|
}
|
||||||
|
std::stringstream ss(response);
|
||||||
|
LogPrint (eLogInfo, "Addressbook: got update from ", dest_host);
|
||||||
|
m_Book.LoadHostsFromStream (ss);
|
||||||
|
m_Book.DownloadComplete (true, ident, m_Etag, m_LastModified);
|
||||||
}
|
}
|
||||||
|
|
||||||
AddressResolver::AddressResolver (std::shared_ptr<ClientDestination> destination):
|
AddressResolver::AddressResolver (std::shared_ptr<ClientDestination> destination):
|
||||||
|
@ -117,7 +117,6 @@ namespace client
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
void Request ();
|
void Request ();
|
||||||
bool ProcessResponse (std::stringstream& s, bool isGzip = false);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user