mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-21 11:29:56 +00:00
extract https content
This commit is contained in:
parent
71dae29077
commit
0d468a8f48
19
Reseed.cpp
19
Reseed.cpp
@ -503,11 +503,18 @@ namespace data
|
||||
{
|
||||
i2p::util::http::url u(address);
|
||||
TlsSession session (u.host_, 443);
|
||||
|
||||
// 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 ());
|
||||
return "";
|
||||
|
||||
// read response
|
||||
std::stringstream rs;
|
||||
while (session.Receive (rs))
|
||||
;
|
||||
return i2p::util::http::GetHttpContent (rs);
|
||||
}
|
||||
|
||||
TlsSession::TlsSession (const std::string& host, int port):
|
||||
@ -797,15 +804,15 @@ namespace data
|
||||
out[1] = 0x03; out[2] = 0x03; // version
|
||||
uint8_t mac[32];
|
||||
CalculateMAC (0x17, buf, len, mac);
|
||||
size_t encryptedLen = Encrypt (buf, len, mac, out);
|
||||
size_t encryptedLen = Encrypt (buf, len, mac, out + 5);
|
||||
htobe16buf (out + 3, encryptedLen);
|
||||
m_Site.write ((char *)out, encryptedLen + 5);
|
||||
delete[] out;
|
||||
}
|
||||
|
||||
std::string TlsSession::Receive ()
|
||||
bool TlsSession::Receive (std::ostream& rs)
|
||||
{
|
||||
if (m_Site.eof ()) return "";
|
||||
if (m_Site.eof ()) return false;
|
||||
uint8_t type; uint16_t version, length;
|
||||
m_Site.read ((char *)&type, 1);
|
||||
m_Site.read ((char *)&version, 2);
|
||||
@ -814,9 +821,9 @@ namespace data
|
||||
uint8_t * buf = new uint8_t[length];
|
||||
m_Site.read ((char *)buf, length);
|
||||
size_t decryptedLen = Decrypt (buf, length);
|
||||
std::string str ((char *)buf + 16, decryptedLen);
|
||||
rs.write ((char *)buf + 16, decryptedLen);
|
||||
delete[] buf;
|
||||
return str;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
2
Reseed.h
2
Reseed.h
@ -53,7 +53,7 @@ namespace data
|
||||
|
||||
TlsSession (const std::string& host, int port);
|
||||
void Send (const uint8_t * buf, size_t len);
|
||||
std::string Receive ();
|
||||
bool Receive (std::ostream& rs);
|
||||
|
||||
private:
|
||||
|
||||
|
73
util.cpp
73
util.cpp
@ -242,40 +242,8 @@ namespace http
|
||||
// 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";
|
||||
// read response
|
||||
std::string version, statusMessage;
|
||||
site >> version; // HTTP version
|
||||
int status;
|
||||
site >> status; // status
|
||||
std::getline (site, statusMessage);
|
||||
if (status == 200) // OK
|
||||
{
|
||||
bool isChunked = false;
|
||||
std::string header;
|
||||
while (!site.eof () && header != "\r")
|
||||
{
|
||||
std::getline(site, header);
|
||||
auto colon = header.find (':');
|
||||
if (colon != std::string::npos)
|
||||
{
|
||||
std::string field = header.substr (0, colon);
|
||||
if (field == i2p::util::http::TRANSFER_ENCODING)
|
||||
isChunked = (header.find ("chunked", colon + 1) != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
if (isChunked)
|
||||
MergeChunkedResponse (site, ss);
|
||||
else
|
||||
ss << site.rdbuf();
|
||||
return ss.str();
|
||||
}
|
||||
else
|
||||
{
|
||||
LogPrint ("HTTP response ", status);
|
||||
return "";
|
||||
}
|
||||
// read response and extract content
|
||||
return GetHttpContent (site);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -290,6 +258,43 @@ namespace http
|
||||
}
|
||||
}
|
||||
|
||||
std::string GetHttpContent (std::istream& response)
|
||||
{
|
||||
std::string version, statusMessage;
|
||||
response >> version; // HTTP version
|
||||
int status;
|
||||
response >> status; // status
|
||||
std::getline (response, statusMessage);
|
||||
if (status == 200) // OK
|
||||
{
|
||||
bool isChunked = false;
|
||||
std::string header;
|
||||
while (!response.eof () && header != "\r")
|
||||
{
|
||||
std::getline(response, header);
|
||||
auto colon = header.find (':');
|
||||
if (colon != std::string::npos)
|
||||
{
|
||||
std::string field = header.substr (0, colon);
|
||||
if (field == i2p::util::http::TRANSFER_ENCODING)
|
||||
isChunked = (header.find ("chunked", colon + 1) != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
if (isChunked)
|
||||
MergeChunkedResponse (response, ss);
|
||||
else
|
||||
ss << response.rdbuf();
|
||||
return ss.str();
|
||||
}
|
||||
else
|
||||
{
|
||||
LogPrint ("HTTP response ", status);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void MergeChunkedResponse (std::istream& response, std::ostream& merged)
|
||||
{
|
||||
while (!response.eof ())
|
||||
|
1
util.h
1
util.h
@ -47,6 +47,7 @@ namespace util
|
||||
const char TRANSFER_ENCODING[] = "Transfer-Encoding";
|
||||
|
||||
std::string httpRequest(const std::string& address);
|
||||
std::string GetHttpContent (std::istream& response);
|
||||
void MergeChunkedResponse (std::istream& response, std::ostream& merged);
|
||||
int httpRequestViaI2pProxy(const std::string& address, std::string &content); // return http code
|
||||
std::string urlDecode(const std::string& data);
|
||||
|
Loading…
x
Reference in New Issue
Block a user