2014-03-18 02:31:29 +04:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-03-29 21:28:19 +04:00
|
|
|
#include <boost/regex.hpp>
|
2014-03-18 02:31:29 +04:00
|
|
|
|
|
|
|
#include "HTTPProxy.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace proxy
|
|
|
|
{
|
2014-03-30 02:16:23 +04:00
|
|
|
void HTTPProxyConnection::parseHeaders(const std::string& h, std::vector<header>& hm) {
|
2014-03-18 02:45:25 +04:00
|
|
|
std::string str (h);
|
|
|
|
std::string::size_type idx;
|
|
|
|
std::string t;
|
|
|
|
int i = 0;
|
|
|
|
while( (idx=str.find ("\r\n")) != std::string::npos) {
|
|
|
|
t=str.substr (0,idx);
|
|
|
|
str.erase (0,idx+2);
|
|
|
|
if (t == "")
|
|
|
|
break;
|
|
|
|
idx=t.find(": ");
|
|
|
|
if (idx == std::string::npos)
|
|
|
|
{
|
|
|
|
std::cout << "Bad header line: " << t << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LogPrint ("Name: ", t.substr (0,idx), " Value: ", t.substr (idx+2));
|
|
|
|
hm[i].name = t.substr (0,idx);
|
|
|
|
hm[i].value = t.substr (idx+2);
|
|
|
|
i++;
|
2014-03-18 02:31:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 23:02:00 +04:00
|
|
|
void HTTPProxyConnection::ExtractRequest(request& r)
|
2014-03-18 02:31:29 +04:00
|
|
|
{
|
2014-03-29 21:28:19 +04:00
|
|
|
std::string requestString = m_Buffer;
|
|
|
|
int idx=requestString.find(" ");
|
|
|
|
std::string method = requestString.substr(0,idx);
|
|
|
|
requestString = requestString.substr(idx+1);
|
|
|
|
idx=requestString.find(" ");
|
|
|
|
std::string requestUrl = requestString.substr(0,idx);
|
|
|
|
LogPrint("method is: ", method, "\nRequest is: ", requestUrl);
|
|
|
|
std::string server="";
|
|
|
|
std::string port="80";
|
|
|
|
boost::regex rHTTP("http://(.*?)(:(\\d+))?(/.*)");
|
|
|
|
boost::smatch m;
|
|
|
|
std::string path;
|
|
|
|
if(boost::regex_search(requestUrl, m, rHTTP, boost::match_extra)) {
|
|
|
|
server=m[1].str();
|
|
|
|
if(m[2].str() != "") {
|
|
|
|
port=m[3].str();
|
2014-03-18 02:31:29 +04:00
|
|
|
}
|
2014-03-29 21:28:19 +04:00
|
|
|
path=m[4].str();
|
2014-03-18 02:31:29 +04:00
|
|
|
}
|
2014-03-29 21:28:19 +04:00
|
|
|
LogPrint("server is: ",server, "\n path is: ",path);
|
2014-09-12 23:02:00 +04:00
|
|
|
r.uri = path;
|
|
|
|
r.method = method;
|
|
|
|
r.host = server;
|
2014-03-18 02:31:29 +04:00
|
|
|
}
|
|
|
|
|
2014-03-29 20:03:14 +04:00
|
|
|
|
2014-03-30 02:16:23 +04:00
|
|
|
void HTTPProxyConnection::RunRequest()
|
|
|
|
{
|
2014-09-12 23:02:00 +04:00
|
|
|
request r;
|
|
|
|
ExtractRequest(r);
|
|
|
|
parseHeaders(m_Buffer, r.headers);
|
2014-09-13 02:36:56 +04:00
|
|
|
const char * data = nullptr;
|
|
|
|
if (r.method == "POST")
|
|
|
|
{
|
|
|
|
data = strstr (m_Buffer, "\r\n\r\n");
|
|
|
|
if (data) data += 4;
|
|
|
|
}
|
2014-09-12 23:02:00 +04:00
|
|
|
LogPrint("Requesting ", r.host, " with path ", r.uri, " and method ", r.method);
|
2014-09-12 23:57:34 +04:00
|
|
|
HandleDestinationRequest(r.host, r.method, data ? std::string (data) : "" , r.uri);
|
2014-03-30 02:16:23 +04:00
|
|
|
}
|
|
|
|
|
2014-03-18 02:31:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|