Browse Source

extract https content

pull/163/head
orignal 10 years ago
parent
commit
0d468a8f48
  1. 19
      Reseed.cpp
  2. 2
      Reseed.h
  3. 73
      util.cpp
  4. 1
      util.h

19
Reseed.cpp

@ -503,11 +503,18 @@ namespace data
{ {
i2p::util::http::url u(address); i2p::util::http::url u(address);
TlsSession session (u.host_, 443); TlsSession session (u.host_, 443);
// send request
std::stringstream ss; std::stringstream ss;
ss << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_ 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"; << "\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 ()); 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): TlsSession::TlsSession (const std::string& host, int port):
@ -797,15 +804,15 @@ namespace data
out[1] = 0x03; out[2] = 0x03; // version out[1] = 0x03; out[2] = 0x03; // version
uint8_t mac[32]; uint8_t mac[32];
CalculateMAC (0x17, buf, len, mac); 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); htobe16buf (out + 3, encryptedLen);
m_Site.write ((char *)out, encryptedLen + 5); m_Site.write ((char *)out, encryptedLen + 5);
delete[] out; 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; uint8_t type; uint16_t version, length;
m_Site.read ((char *)&type, 1); m_Site.read ((char *)&type, 1);
m_Site.read ((char *)&version, 2); m_Site.read ((char *)&version, 2);
@ -814,9 +821,9 @@ namespace data
uint8_t * buf = new uint8_t[length]; uint8_t * buf = new uint8_t[length];
m_Site.read ((char *)buf, length); m_Site.read ((char *)buf, length);
size_t decryptedLen = Decrypt (buf, length); size_t decryptedLen = Decrypt (buf, length);
std::string str ((char *)buf + 16, decryptedLen); rs.write ((char *)buf + 16, decryptedLen);
delete[] buf; delete[] buf;
return str; return true;
} }
} }
} }

2
Reseed.h

@ -53,7 +53,7 @@ namespace data
TlsSession (const std::string& host, int port); TlsSession (const std::string& host, int port);
void Send (const uint8_t * buf, size_t len); void Send (const uint8_t * buf, size_t len);
std::string Receive (); bool Receive (std::ostream& rs);
private: private:

73
util.cpp

@ -242,40 +242,8 @@ namespace http
// 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 << "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"; << "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n" << "Connection: close\r\n\r\n";
// read response // read response and extract content
std::string version, statusMessage; return GetHttpContent (site);
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 "";
}
} }
else 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) void MergeChunkedResponse (std::istream& response, std::ostream& merged)
{ {
while (!response.eof ()) while (!response.eof ())

1
util.h

@ -47,6 +47,7 @@ namespace util
const char TRANSFER_ENCODING[] = "Transfer-Encoding"; const char TRANSFER_ENCODING[] = "Transfer-Encoding";
std::string httpRequest(const std::string& address); std::string httpRequest(const std::string& address);
std::string GetHttpContent (std::istream& response);
void MergeChunkedResponse (std::istream& response, std::ostream& merged); void MergeChunkedResponse (std::istream& response, std::ostream& merged);
int httpRequestViaI2pProxy(const std::string& address, std::string &content); // return http code int httpRequestViaI2pProxy(const std::string& address, std::string &content); // return http code
std::string urlDecode(const std::string& data); std::string urlDecode(const std::string& data);

Loading…
Cancel
Save