Browse Source

Code cleanup

pull/46/head
Meeh 10 years ago
parent
commit
15f0f20e99
  1. 55
      HTTPProxy.cpp

55
HTTPProxy.cpp

@ -63,7 +63,7 @@ namespace proxy
std::pair<std::string,std::string> requestInfo = ExtractRequest (); std::pair<std::string,std::string> requestInfo = ExtractRequest ();
request m_Request; request m_Request;
parseHeaders(m_Buffer, m_Request.headers); parseHeaders (m_Buffer, m_Request.headers);
LogPrint("Requesting ", requestInfo.first, " with path ", requestInfo.second); LogPrint("Requesting ", requestInfo.first, " with path ", requestInfo.second);
HandleDestinationRequest (requestInfo.first, requestInfo.second); HandleDestinationRequest (requestInfo.first, requestInfo.second);
@ -77,31 +77,32 @@ namespace proxy
Terminate (); Terminate ();
} }
void HTTPConnection::parseHeaders(const std::string& h, std::vector<header>& hm) { void HTTPConnection::parseHeaders(const std::string& h, std::vector<header>& hm) {
std::string str(h); std::string str (h);
std::string::size_type idx; std::string::size_type idx;
std::string t; std::string t;
int i = 0; int i = 0;
while((idx=str.find("\r\n")) != std::string::npos) { while( (idx=str.find ("\r\n")) != std::string::npos) {
t=str.substr(0,idx); t=str.substr (0,idx);
str.erase(0,idx+2); str.erase (0,idx+2);
if(t == "") if (t == "")
break; break;
idx=t.find(": "); idx=t.find(": ");
if(idx == std::string::npos) { if (idx == std::string::npos)
{
std::cout << "Bad header line: " << t << std::endl; std::cout << "Bad header line: " << t << std::endl;
break; break;
} }
LogPrint ("Name: ", t.substr (0,idx), " Value: ", t.substr (idx+2)); LogPrint ("Name: ", t.substr (0,idx), " Value: ", t.substr (idx+2));
hm[i].name = t.substr(0,idx); hm[i].name = t.substr (0,idx);
hm[i].value = t.substr(idx+2); hm[i].value = t.substr (idx+2);
i++; i++;
} }
} }
// TODO: Support other requests than GET. // TODO: Support other requests than GET.
std::pair<std::string, std::string> HTTPConnection::ExtractRequest () std::pair<std::string, std::string> HTTPConnection::ExtractRequest ()
{ {
char * get = strstr (m_Buffer, "GET"); char * get = strstr (m_Buffer, "GET");
if (get) if (get)
{ {
@ -109,24 +110,24 @@ std::pair<std::string, std::string> HTTPConnection::ExtractRequest ()
if (http) if (http)
{ {
std::string url (get + 4, http - get - 5); std::string url (get + 4, http - get - 5);
size_t sp = url.find_first_of( '/', 7 /* skip http:// part */ ); size_t sp = url.find_first_of ('/', 7 /* skip http:// part */ );
if ( sp != std::string::npos ) if (sp != std::string::npos)
{ {
std::string base_url( url.begin()+7, url.begin()+sp ); std::string base_url (url.begin()+7, url.begin()+sp);
LogPrint("Base URL is: ", base_url); LogPrint ("Base URL is: ", base_url);
if ( sp != std::string::npos ) if ( sp != std::string::npos )
{ {
std::string query( url.begin()+sp+1, url.end() ); std::string query (url.begin ()+sp+1, url.end ());
LogPrint("Query is: ", "/" + query); LogPrint ("Query is: ", "/" + query);
return std::make_pair(base_url, "/" + query); return std::make_pair (base_url, "/" + query);
} }
return std::make_pair(base_url, "/"); return std::make_pair (base_url, "/");
} }
} }
} }
return std::make_pair("",""); return std::make_pair ("","");
} }
void HTTPConnection::HandleWrite (const boost::system::error_code& ecode) void HTTPConnection::HandleWrite (const boost::system::error_code& ecode)
{ {
@ -139,8 +140,8 @@ std::pair<std::string, std::string> HTTPConnection::ExtractRequest ()
std::string fullAddress; std::string fullAddress;
if (address.find (".b32.i2p") != std::string::npos) if (address.find (".b32.i2p") != std::string::npos)
{ {
int li = address.find_first_of("."); int li = address.find_first_of (".");
std::string newaddress = address.substr(0, li); std::string newaddress = address.substr (0, li);
if (i2p::data::Base32ToByteStream (newaddress.c_str (), newaddress.length (), (uint8_t *)destination, 32) != 32) if (i2p::data::Base32ToByteStream (newaddress.c_str (), newaddress.length (), (uint8_t *)destination, 32) != 32)
{ {
LogPrint ("Invalid Base32 address ", newaddress); LogPrint ("Invalid Base32 address ", newaddress);
@ -150,7 +151,7 @@ std::pair<std::string, std::string> HTTPConnection::ExtractRequest ()
} }
else else
{ {
auto addr = i2p::data::netdb.FindAddress(address); auto addr = i2p::data::netdb.FindAddress (address);
if (!addr) if (!addr)
{ {
LogPrint ("Unknown address ", address); LogPrint ("Unknown address ", address);

Loading…
Cancel
Save