Improve HTTP/S header handling, reorganize HTTPS.

This commit is contained in:
anonimal 2015-10-15 05:19:19 +00:00
parent 45d27f8ddc
commit 03ecb957a5
5 changed files with 58 additions and 43 deletions

View file

@ -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_);

View file

@ -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<class Hash>

View file

@ -39,8 +39,6 @@ namespace data
int ProcessSU3Stream (std::istream& s);
bool FindZipDataDescriptor (std::istream& s);
std::string HttpsRequest (const std::string& address);
private:

View file

@ -14,6 +14,7 @@
#include <boost/program_options/parsers.hpp>
#include <boost/algorithm/string.hpp>
#include "Log.h"
#include "Reseed.h"
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
#include <sys/types.h>
@ -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

View file

@ -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