From dc8dac51f7a878c926bac44edba3b1c84cf43132 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 22 Jan 2014 15:32:50 -0500 Subject: [PATCH] download RouterInfo through HTTP --- NetDb.cpp | 38 ++++++++++++++++++++++++++++++++++++++ NetDb.h | 1 + 2 files changed, 39 insertions(+) diff --git a/NetDb.cpp b/NetDb.cpp index 6cf9e251..a39d7ee1 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -1,6 +1,7 @@ #include "I2PEndian.h" #include #include +#include #include #include #include "base64.h" @@ -520,5 +521,42 @@ namespace data } return r; } + + void NetDb::DownloadRouterInfo (const std::string& address, const std::string& filename) + { + try + { + boost::asio::ip::tcp::iostream site(address, "http"); + if (!site) + { + site.expires_from_now (boost::posix_time::seconds (10)); // wait for 10 seconds + site << "GET " << filename << "HTTP/1.0\nHost: " << address << "\nAccept: */*\nConnection: close\n\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 (header != "\n") + std::getline (site, header); + // read content + std::stringstream ss; + ss << site.rdbuf(); + AddRouterInfo ((uint8_t *)ss.str ().c_str (), ss.str ().size ()); + } + else + LogPrint ("HTTP response ", status); + } + else + LogPrint ("Can't connect to ", address); + } + catch (std::exception& ex) + { + LogPrint ("Failed to download ", filename, " : ", ex.what ()); + } + } } } diff --git a/NetDb.h b/NetDb.h index 75c2c81b..03be5ed6 100644 --- a/NetDb.h +++ b/NetDb.h @@ -76,6 +76,7 @@ namespace data void Load (const char * directory); void SaveUpdated (const char * directory); + void DownloadRouterInfo (const std::string& address, const std::string& filename); // for reseed void Run (); // exploratory thread void Explore (); const RouterInfo * GetClosestFloodfill (const IdentHash& destination, const std::set& excluded) const;