From 03ecb957a554aa005138805aa088e55409a28815 Mon Sep 17 00:00:00 2001 From: anonimal Date: Thu, 15 Oct 2015 05:19:19 +0000 Subject: [PATCH] Improve HTTP/S header handling, reorganize HTTPS. --- core/AddressBook.cpp | 5 ++-- core/Reseed.cpp | 26 +------------------- core/Reseed.h | 2 -- core/util/util.cpp | 56 ++++++++++++++++++++++++++++++++++---------- core/util/util.h | 12 ++++++++++ 5 files changed, 58 insertions(+), 43 deletions(-) diff --git a/core/AddressBook.cpp b/core/AddressBook.cpp index d1faaeec..2963b85e 100644 --- a/core/AddressBook.cpp +++ b/core/AddressBook.cpp @@ -487,11 +487,10 @@ namespace client { std::stringstream request, response; // standard header - request << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_ - << "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n" << "Connection: close\r\n"; + request << i2p::util::http::httpHeader(u.path_, u.host_, "1.1"); if (m_Etag.length () > 0) // etag request << i2p::util::http::IF_NONE_MATCH << ": \"" << m_Etag << "\"\r\n"; - if (m_LastModified.length () > 0) // if-modfief-since + if (m_LastModified.length () > 0) // if modified since request << i2p::util::http::IF_MODIFIED_SINCE << ": " << m_LastModified << "\r\n"; request << "\r\n"; // end of header auto stream = m_Book.getSharedLocalDestination()->CreateStream (leaseSet, u.port_); diff --git a/core/Reseed.cpp b/core/Reseed.cpp index 6b9c90c2..c1f52d16 100644 --- a/core/Reseed.cpp +++ b/core/Reseed.cpp @@ -130,7 +130,7 @@ namespace data { std::string url = host + "i2pseeds.su3"; LogPrint (eLogInfo, "Downloading SU3 from ", host); - std::string su3 = https ? HttpsRequest (url) : i2p::util::http::httpRequest (url); + std::string su3 = https ? i2p::util::http::httpsRequest (url) : i2p::util::http::httpRequest (url); if (su3.length () > 0) { std::stringstream s(su3); @@ -491,30 +491,6 @@ namespace data LogPrint (eLogInfo, numCertificates, " certificates loaded"); } - std::string Reseeder::HttpsRequest (const std::string& address) - { - i2p::util::http::url u(address); - if (u.port_ == 80) u.port_ = 443; - TlsSession session (u.host_, u.port_); - - if (session.IsEstablished ()) - { - // send request - std::stringstream ss; - ss << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_ - << "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n" << "Connection: close\r\n\r\n"; - session.Send ((uint8_t *)ss.str ().c_str (), ss.str ().length ()); - - // read response - std::stringstream rs; - while (session.Receive (rs)) - ; - return i2p::util::http::GetHttpContent (rs); - } - else - return ""; - } - //------------------------------------------------------------- template diff --git a/core/Reseed.h b/core/Reseed.h index 274698e0..f1af9de3 100644 --- a/core/Reseed.h +++ b/core/Reseed.h @@ -39,8 +39,6 @@ namespace data int ProcessSU3Stream (std::istream& s); bool FindZipDataDescriptor (std::istream& s); - - std::string HttpsRequest (const std::string& address); private: diff --git a/core/util/util.cpp b/core/util/util.cpp index eef5d1f0..398be49b 100644 --- a/core/util/util.cpp +++ b/core/util/util.cpp @@ -14,6 +14,7 @@ #include #include #include "Log.h" +#include "Reseed.h" #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__) #include @@ -305,12 +306,46 @@ namespace filesystem } } -namespace http +namespace http // also provides https { + std::string httpHeader (const std::string& path, const std::string& host, const std::string& version) + { + std::string header = + "GET " + path + " HTTP/" + version + "\r\n" + + "Host: " + host + "\r\n" + + "Accept: */*\r\n" + + "User-Agent: Wget/1.11.4\r\n" + + "Connection: close\r\n\r\n"; + return header; + } + + std::string httpsRequest (const std::string& address) + { + url u(address); + if (u.port_ == 80) u.port_ = 443; + i2p::data::TlsSession session (u.host_, u.port_); + + if (session.IsEstablished ()) + { + // send request + std::stringstream ss; + ss << httpHeader(u.path_, u.host_, "1.1"); + session.Send ((uint8_t *)ss.str ().c_str (), ss.str ().length ()); + + // read response + std::stringstream rs; + while (session.Receive (rs)) + ; + return GetHttpContent (rs); + } + else + return ""; + } + std::string httpRequest(const std::string& address) { try { - i2p::util::http::url u(address); + url u(address); 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 @@ -323,9 +358,7 @@ namespace http } if(site) { // User-Agent is needed to get the server list routerInfo files. - site << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_ - << "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n" - << "Connection: close\r\n\r\n"; + site << httpHeader(u.path_, u.host_, "1.1"); // read response and extract content return GetHttpContent(site); } else { @@ -353,7 +386,7 @@ namespace http auto colon = header.find (':'); if(colon != std::string::npos) { std::string field = header.substr (0, colon); - if(field == i2p::util::http::TRANSFER_ENCODING) + if(field == TRANSFER_ENCODING) isChunked = (header.find("chunked", colon + 1) != std::string::npos); } } @@ -402,14 +435,11 @@ namespace http site.connect("127.0.0.1", ss.str()); } if(site) { - i2p::util::http::url u(address); + 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; + + // set header + ss << httpHeader(u.path_, u.host_, "1.0"); site << ss.str(); // read response diff --git a/core/util/util.h b/core/util/util.h index fe47cbf6..da7f1971 100644 --- a/core/util/util.h +++ b/core/util/util.h @@ -124,6 +124,18 @@ namespace util const char LAST_MODIFIED[] = "Last-Modified"; const char TRANSFER_ENCODING[] = "Transfer-Encoding"; + /** + * Header for HTTP/S requests. + * @return a string of the complete header + */ + std::string httpHeader(const std::string& path, const std::string& host, const std::string& version); + + /** + * Perform an HTTPS request. + * @return the result of the request, or an empty string if it fails + */ + std::string httpsRequest(const std::string& address); + /** * Perform an HTTP request. * @return the result of the request, or an empty string if it fails