Browse Source

extract https content

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

19
Reseed.cpp

@ -503,11 +503,18 @@ namespace data @@ -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 @@ -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 @@ -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

@ -53,7 +53,7 @@ namespace data @@ -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:

45
util.cpp

@ -242,19 +242,36 @@ namespace http @@ -242,19 +242,36 @@ 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
// read response and extract content
return GetHttpContent (site);
}
else
{
LogPrint ("Can't connect to ", address);
return "";
}
}
catch (std::exception& ex)
{
LogPrint ("Failed to download ", address, " : ", ex.what ());
return "";
}
}
std::string GetHttpContent (std::istream& response)
{
std::string version, statusMessage;
site >> version; // HTTP version
response >> version; // HTTP version
int status;
site >> status; // status
std::getline (site, statusMessage);
response >> status; // status
std::getline (response, statusMessage);
if (status == 200) // OK
{
bool isChunked = false;
std::string header;
while (!site.eof () && header != "\r")
while (!response.eof () && header != "\r")
{
std::getline(site, header);
std::getline(response, header);
auto colon = header.find (':');
if (colon != std::string::npos)
{
@ -266,9 +283,9 @@ namespace http @@ -266,9 +283,9 @@ namespace http
std::stringstream ss;
if (isChunked)
MergeChunkedResponse (site, ss);
MergeChunkedResponse (response, ss);
else
ss << site.rdbuf();
ss << response.rdbuf();
return ss.str();
}
else
@ -277,18 +294,6 @@ namespace http @@ -277,18 +294,6 @@ namespace http
return "";
}
}
else
{
LogPrint ("Can't connect to ", address);
return "";
}
}
catch (std::exception& ex)
{
LogPrint ("Failed to download ", address, " : ", ex.what ());
return "";
}
}
void MergeChunkedResponse (std::istream& response, std::ostream& merged)
{

1
util.h

@ -47,6 +47,7 @@ namespace util @@ -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…
Cancel
Save