forked from PurpleI2P/i2pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPProxy.cpp
86 lines (78 loc) · 2.19 KB
/
HTTPProxy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include "NetDb.h"
#include "HTTPProxy.h"
namespace i2p
{
namespace proxy
{
void HTTPProxyConnection::parseHeaders(const std::string& h, std::vector<header>& hm) {
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++;
}
}
void HTTPProxyConnection::ExtractRequest(request& r)
{
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();
}
path=m[4].str();
}
LogPrint("server is: ",server, "\n path is: ",path);
r.uri = path;
r.method = method;
r.host = server;
}
void HTTPProxyConnection::RunRequest()
{
request r;
ExtractRequest(r);
parseHeaders(m_Buffer, r.headers);
size_t addressHelperPos = r.uri.find ("i2paddresshelper");
if (addressHelperPos != std::string::npos)
{
// jump service
size_t addressPos = r.uri.find ("=", addressHelperPos);
if (addressPos != std::string::npos)
{
LogPrint ("Jump service for ", r.host, " found. Inserting to address book");
auto base64 = r.uri.substr (addressPos + 1);
i2p::data::netdb.GetAddressBook ().InsertAddress (r.host, base64);
}
}
LogPrint("Requesting ", r.host, " with path ", r.uri, " and method ", r.method);
SendToAddress (r.host, m_Buffer, m_BufferLen);
}
}
}