mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-23 17:36:37 +02:00
Improve HTTP/S header handling, reorganize HTTPS.
This commit is contained in:
parent
45d27f8ddc
commit
03ecb957a5
5 changed files with 58 additions and 43 deletions
|
@ -487,11 +487,10 @@ namespace client
|
||||||
{
|
{
|
||||||
std::stringstream request, response;
|
std::stringstream request, response;
|
||||||
// standard header
|
// standard header
|
||||||
request << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_
|
request << i2p::util::http::httpHeader(u.path_, u.host_, "1.1");
|
||||||
<< "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n" << "Connection: close\r\n";
|
|
||||||
if (m_Etag.length () > 0) // etag
|
if (m_Etag.length () > 0) // etag
|
||||||
request << i2p::util::http::IF_NONE_MATCH << ": \"" << m_Etag << "\"\r\n";
|
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 << i2p::util::http::IF_MODIFIED_SINCE << ": " << m_LastModified << "\r\n";
|
||||||
request << "\r\n"; // end of header
|
request << "\r\n"; // end of header
|
||||||
auto stream = m_Book.getSharedLocalDestination()->CreateStream (leaseSet, u.port_);
|
auto stream = m_Book.getSharedLocalDestination()->CreateStream (leaseSet, u.port_);
|
||||||
|
|
|
@ -130,7 +130,7 @@ namespace data
|
||||||
{
|
{
|
||||||
std::string url = host + "i2pseeds.su3";
|
std::string url = host + "i2pseeds.su3";
|
||||||
LogPrint (eLogInfo, "Downloading SU3 from ", host);
|
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)
|
if (su3.length () > 0)
|
||||||
{
|
{
|
||||||
std::stringstream s(su3);
|
std::stringstream s(su3);
|
||||||
|
@ -491,30 +491,6 @@ namespace data
|
||||||
LogPrint (eLogInfo, numCertificates, " certificates loaded");
|
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>
|
template<class Hash>
|
||||||
|
|
|
@ -39,8 +39,6 @@ namespace data
|
||||||
int ProcessSU3Stream (std::istream& s);
|
int ProcessSU3Stream (std::istream& s);
|
||||||
|
|
||||||
bool FindZipDataDescriptor (std::istream& s);
|
bool FindZipDataDescriptor (std::istream& s);
|
||||||
|
|
||||||
std::string HttpsRequest (const std::string& address);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include <boost/program_options/parsers.hpp>
|
#include <boost/program_options/parsers.hpp>
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
#include "Reseed.h"
|
||||||
|
|
||||||
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
|
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
|
||||||
#include <sys/types.h>
|
#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)
|
std::string httpRequest(const std::string& address)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
i2p::util::http::url u(address);
|
url u(address);
|
||||||
boost::asio::ip::tcp::iostream site;
|
boost::asio::ip::tcp::iostream site;
|
||||||
// please don't uncomment following line because it's not compatible with boost 1.46
|
// 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
|
// 1.46 is default boost for Ubuntu 12.04 LTS
|
||||||
|
@ -323,9 +358,7 @@ namespace http
|
||||||
}
|
}
|
||||||
if(site) {
|
if(site) {
|
||||||
// User-Agent is needed to get the server list routerInfo files.
|
// User-Agent is needed to get the server list routerInfo files.
|
||||||
site << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_
|
site << httpHeader(u.path_, u.host_, "1.1");
|
||||||
<< "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n"
|
|
||||||
<< "Connection: close\r\n\r\n";
|
|
||||||
// read response and extract content
|
// read response and extract content
|
||||||
return GetHttpContent(site);
|
return GetHttpContent(site);
|
||||||
} else {
|
} else {
|
||||||
|
@ -353,7 +386,7 @@ namespace http
|
||||||
auto colon = header.find (':');
|
auto colon = header.find (':');
|
||||||
if(colon != std::string::npos) {
|
if(colon != std::string::npos) {
|
||||||
std::string field = header.substr (0, colon);
|
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);
|
isChunked = (header.find("chunked", colon + 1) != std::string::npos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -402,14 +435,11 @@ namespace http
|
||||||
site.connect("127.0.0.1", ss.str());
|
site.connect("127.0.0.1", ss.str());
|
||||||
}
|
}
|
||||||
if(site) {
|
if(site) {
|
||||||
i2p::util::http::url u(address);
|
url u(address);
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "GET " << address << " HTTP/1.0" << std::endl;
|
|
||||||
ss << "Host: " << u.host_ << std::endl;
|
// set header
|
||||||
ss << "Accept: */*" << std::endl;
|
ss << httpHeader(u.path_, u.host_, "1.0");
|
||||||
ss << "User - Agent: Wget / 1.11.4" << std::endl;
|
|
||||||
ss << "Connection: close" << std::endl;
|
|
||||||
ss << std::endl;
|
|
||||||
site << ss.str();
|
site << ss.str();
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
|
|
|
@ -124,6 +124,18 @@ namespace util
|
||||||
const char LAST_MODIFIED[] = "Last-Modified";
|
const char LAST_MODIFIED[] = "Last-Modified";
|
||||||
const char TRANSFER_ENCODING[] = "Transfer-Encoding";
|
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.
|
* Perform an HTTP request.
|
||||||
* @return the result of the request, or an empty string if it fails
|
* @return the result of the request, or an empty string if it fails
|
||||||
|
|
Loading…
Add table
Reference in a new issue