mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-28 11:47:48 +02:00
Adding reseed support. v1, only http so far.
This commit is contained in:
parent
d107ef4007
commit
86355cfe09
7 changed files with 223 additions and 45 deletions
95
util.cpp
95
util.cpp
|
@ -1,4 +1,11 @@
|
|||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <functional>
|
||||
#include <fstream>
|
||||
#include <boost/asio.hpp>
|
||||
#include "util.h"
|
||||
#include "Log.h"
|
||||
|
||||
namespace i2p
|
||||
{
|
||||
|
@ -40,6 +47,94 @@ const char* GetCharArg(const std::string& strArg, const std::string& nDefault)
|
|||
return nDefault.c_str();
|
||||
}
|
||||
|
||||
namespace http
|
||||
{
|
||||
//const std::string& filename
|
||||
//AddRouterInfo ((uint8_t *)ss.str ().c_str (), ss.str ().size ());
|
||||
//TODO: Move to reseed.
|
||||
//TODO: Implement v1 & v2 reseeding. Lightweight zip library is needed for v2.
|
||||
// orignal: zip is part of crypto++, see implementation of DatabaseStoreMsg
|
||||
//TODO: Implement SU3, utils.
|
||||
std::string httpRequest(const std::string& address)
|
||||
{
|
||||
try
|
||||
{
|
||||
i2p::util::http::url u(address);
|
||||
boost::asio::ip::tcp::iostream site;
|
||||
site.expires_from_now (boost::posix_time::seconds(30));
|
||||
site.connect(u.host_, "http");
|
||||
if (site)
|
||||
{
|
||||
//site.expires_from_now (boost::posix_time::seconds (10)); // wait for 10 seconds
|
||||
// User-Agent is needed to get the server list routerInfo files.
|
||||
site << "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\r\n";
|
||||
// read response
|
||||
std::string version, statusMessage;
|
||||
site >> version; // HTTP version
|
||||
int status;
|
||||
site >> status; // status
|
||||
std::getline (site, statusMessage);
|
||||
if (status == 200) // OK
|
||||
{
|
||||
std::string header;
|
||||
while (std::getline(site, header) && header != "\r"){}
|
||||
std::stringstream ss;
|
||||
ss << site.rdbuf();
|
||||
return ss.str();
|
||||
}
|
||||
else
|
||||
{
|
||||
LogPrint ("HTTP response ", status);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogPrint ("Can't connect to ", address);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
catch (std::exception& ex)
|
||||
{
|
||||
LogPrint ("Failed to download ", address, " : ", ex.what ());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
url::url(const std::string& url_s)
|
||||
{
|
||||
parse(url_s);
|
||||
}
|
||||
|
||||
void url::parse(const std::string& url_s)
|
||||
{
|
||||
const std::string prot_end("://");
|
||||
std::string::const_iterator prot_i = search(url_s.begin(), url_s.end(),
|
||||
prot_end.begin(), prot_end.end());
|
||||
protocol_.reserve(distance(url_s.begin(), prot_i));
|
||||
transform(url_s.begin(), prot_i,
|
||||
back_inserter(protocol_),
|
||||
std::ptr_fun<int,int>(tolower)); // protocol is icase
|
||||
if( prot_i == url_s.end() )
|
||||
return;
|
||||
advance(prot_i, prot_end.length());
|
||||
std::string::const_iterator path_i = find(prot_i, url_s.end(), '/');
|
||||
host_.reserve(distance(prot_i, path_i));
|
||||
transform(prot_i, path_i,
|
||||
back_inserter(host_),
|
||||
std::ptr_fun<int,int>(tolower)); // host is icase
|
||||
std::string::const_iterator query_i = find(path_i, url_s.end(), '?');
|
||||
path_.assign(path_i, query_i);
|
||||
if( query_i != url_s.end() )
|
||||
++query_i;
|
||||
query_.assign(query_i, url_s.end());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // Namespace end
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue