Browse Source

load host.txt via HTTPProxy

pull/66/head
chertov 11 years ago
parent
commit
0c36925b87
  1. 15
      AddressBook.cpp
  2. 2
      HTTPServer.cpp
  3. 59
      util.cpp
  4. 2
      util.h

15
AddressBook.cpp

@ -33,18 +33,13 @@ const IdentHash * AddressBook::FindAddress (const std::string& address)
void AddressBook::LoadHostsFromI2P () void AddressBook::LoadHostsFromI2P ()
{ {
std::string content; std::string content;
std::stringstream url_ss;
// TODO: hosts link in config
// TODO: url download via HTTPProxy
url_ss << "http://127.0.0.1:" << i2p::util::config::GetArg("-httpport", 7070) << "/udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna/hosts.txt";
while (true) while (true)
{ {
content = i2p::util::http::httpRequest(url_ss.str()); // TODO: hosts link in config
int http_code = i2p::util::http::httpRequestViaI2pProxy("http://udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p/hosts.txt", content);
// TODO: check http errors if (http_code ==200)
if (! boost::starts_with(content, "<html>") && content.size() > 0) if (!boost::starts_with(content, "<html>") && !content.empty()) // TODO: test and remove
break; break;
std::this_thread::sleep_for(std::chrono::seconds(5)); std::this_thread::sleep_for(std::chrono::seconds(5));
} }

2
HTTPServer.cpp

@ -133,7 +133,7 @@ namespace util
case 500: buffers.push_back(boost::asio::buffer("HTTP/1.0 500 Internal Server Error\r\n")); break; case 500: buffers.push_back(boost::asio::buffer("HTTP/1.0 500 Internal Server Error\r\n")); break;
case 502: buffers.push_back(boost::asio::buffer("HTTP/1.0 502 Bad Gateway\r\n")); break; case 502: buffers.push_back(boost::asio::buffer("HTTP/1.0 502 Bad Gateway\r\n")); break;
case 503: buffers.push_back(boost::asio::buffer("HTTP/1.0 503 Not Implemented\r\n")); break; case 503: buffers.push_back(boost::asio::buffer("HTTP/1.0 503 Not Implemented\r\n")); break;
case 504: buffers.push_back(boost::asio::buffer("HTTP/1.0 504 Gateway Timeou\r\n")); break; case 504: buffers.push_back(boost::asio::buffer("HTTP/1.0 504 Gateway Timeout\r\n")); break;
default: default:
buffers.push_back(boost::asio::buffer("HTTP/1.0 200 OK\r\n")); buffers.push_back(boost::asio::buffer("HTTP/1.0 200 OK\r\n"));
} }

59
util.cpp

@ -249,6 +249,65 @@ namespace http
} }
} }
int httpRequestViaI2pProxy(const std::string& address, std::string &content)
{
content = "";
try
{
boost::asio::ip::tcp::iostream site;
// please don't uncomment following line because it's not compatible with boost 1.46
// 1.46 is default boost for Ubuntu 12.04 LTS
//site.expires_from_now (boost::posix_time::seconds(30));
{
std::stringstream ss; ss << i2p::util::config::GetArg("-httpproxyport", 4446);
site.connect("127.0.0.1", ss.str());
}
if (site)
{
i2p::util::http::url u(address);
std::stringstream ss;
ss << "GET " << address << " HTTP/1.0" << std::endl;
ss << "Host: " << u.host_ << std::endl;
ss << "Accept: */*" << std::endl;
ss << "User - Agent: Wget / 1.11.4" << std::endl;
ss << "Connection: close" << std::endl;
ss << std::endl;
site << ss.str();
// 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();
content = ss.str();
return status;
}
else
{
LogPrint("HTTP response ", status);
return status;
}
}
else
{
LogPrint("Can't connect to proxy");
return 408;
}
}
catch (std::exception& ex)
{
LogPrint("Failed to download ", address, " : ", ex.what());
return 408;
}
}
url::url(const std::string& url_s) url::url(const std::string& url_s)
{ {
portstr_ = "80"; portstr_ = "80";

2
util.h

@ -35,6 +35,8 @@ namespace util
namespace http namespace http
{ {
std::string httpRequest(const std::string& address); std::string httpRequest(const std::string& address);
int httpRequestViaI2pProxy(const std::string& address, std::string &content); // return http code
struct url { struct url {
url(const std::string& url_s); // omitted copy, ==, accessors, ... url(const std::string& url_s); // omitted copy, ==, accessors, ...
private: private:

Loading…
Cancel
Save