-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.cpp
More file actions
340 lines (324 loc) · 14.3 KB
/
proxy.cpp
File metadata and controls
340 lines (324 loc) · 14.3 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "proxy.h"
void *proxy_begin(void *ptr)
{
Client *client = (Client *)ptr;
std::unordered_map<std::string, Response> Cache;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/*
TODO: log file path
*/
std::ofstream logFile("/log/proxy.log");
char request_text[65536] = {0};
int request_size = recv(client->getFd(), request_text, sizeof(request_text), 0);
if (request_size <= 0)
{
pthread_mutex_lock(&mutex);
logFile << client->getId() << ": Error Invalid Request." << std::endl;
pthread_mutex_unlock(&mutex);
return NULL;
}
Request request;
HttpRequestParser parser;
HttpRequestParser::ParseResult res = parser.parse(request, request_text, request_text + sizeof(request_text));
if (res == HttpRequestParser::ParsingCompleted)
{
pthread_mutex_lock(&mutex);
logFile << client->getId() << ": \"" << request.getLine() << "\" from " << client->getIP() << " @ " << getTime() << std::endl;
pthread_mutex_unlock(&mutex);
// parse url
const char *url = request.uri.c_str();
UrlParser parser_url;
std::string host;
std::string port;
if (parser_url.parse(url))
{
host = parser_url.hostname();
port = parser_url.port();
// need test
// default 80
if (port == "")
{
port = "80";
}
}
else
{
pthread_mutex_lock(&mutex);
logFile << client->getId() << ": Error Invalid Request." << std::endl;
pthread_mutex_unlock(&mutex);
return NULL;
}
int server_fd = create_client(host.c_str(), port.c_str());
// TODO: should we add this?
// if (server_fd == -1)
// {
// std::cout << "Error in build client!\n";
// return NULL;
// }
}
else
{
pthread_mutex_lock(&mutex);
logFile << client->getId() << ": ERROR in parsing url" << std::endl;
pthread_mutex_unlock(&mutex);
return NULL;
}
}
if (request.method.compare("POST") == 0 || request.method.compare("CONNECT") == 0 || request.method.compare("GET") == 0)
{
// post
if (request.method.compare("POST") == 0)
{
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": "
<< "Requesting \"" << request.getLine() << "\" from " << host() << std::endl;
pthread_mutex_unlock(&mutex);
send(server_fd, myrequest, myrequest_len, MSG_NOSIGNAL);
char myresponse[10000] = {0};
int myresponse_len = recv(server_fd, myresponse, sizeof(myresponse), 0);
if (myresponse_len > 0)
{
Response response;
HttpResponseParser parser;
parser.parse(response, myresponse, myresponse + sizeof(myresponse));
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": Received \"" << response.getLine() << "\" from " << host() << std::endl;
pthread_mutex_unlock(&mutex);
send(myclient->getFd(), myresponse, myresponse_len, MSG_NOSIGNAL);
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": Responding \"" << response.getLine() << std::endl;
pthread_mutex_unlock(&mutex);
}
}
// connect
else if (request.method.compare("CONNECT") == 0)
{
pthread_mutex_lock(&mutex);
logFile << myclient_info->getID() << ": "
<< "Requesting \"" << request.line() << "\" from " << host() << std::endl;
pthread_mutex_unlock(&mutex);
send(myclient->getFd(), "HTTP/1.1 200 OK\r\n\r\n", 19, 0);
fd_set readfds;
int nfds = max_element(myclient->getFd(), server_fd) + 1;
while (true)
{
FD_ZERO(&readfds);
FD_SET(myclient->getFd(), &readfds);
FD_SET(server_fd, &readfds);
int select_result = select(nfds, &readfds, NULL, NULL, NULL);
if (select_result == -1)
{
std::cerr << "Selection failed" << std::endl;
return;
}
int recv_size = 0;
int connect_fds[2] = {myclient->getFd(), server_fd};
for (int i = 0; i < 2; i++)
{
char msg[65536] = {0};
if (FD_ISSET(connect_fds[i], &readfds))
{
recv_size = recv(connect_fds[i], msg, sizeof(msg), 0);
if (recv_size == 0)
{
return;
}
else if (recv_size == -1)
{
std::cerr << "Receive failed" << std::endl;
return;
}
else
{
if (send(connect_fds[(i + 1) % 2], msg, recv_size, 0) <= 0)
{
return;
}
}
}
}
pthread_mutex_lock(&mutex);
logFile << myclient_info->getID() << ": Tunnel closed" << std::endl;
pthread_mutex_unlock(&mutex);
}
// get
else
{
if (Cache.find(request.line) != Cache.end())
{
// in cache
/*revalidate:
Here, the response indicates the response you find in the cache
for(std::vector<Response::HeaderItem>::const_iterator it = response.headers.begin();it != response.headers.end(); ++it){
if (it->name.compare("Cache-Control") == 0){
if (it->value.compare("no-cache") == 0){
resend request to the server
}
else if (it->value.find("max-age") != std::string::npos){
if (it->value.compare("max-age=0") == 0){resend request to the server}
else{send the response in the cache}
}
}
if (it->name.compare("ETag") == 0){
add If-None-Match header in the request and send it
}
if (it->name.compare("Last-Modified") == 0){
add If-Modified-Since header in the request and send it
}
if (it->name.compare("Expires") == 0){
compare the it->value with the 'now' time, if < ,not fresh, send to server
else, fresh, send the response in the cache
}
}
*/
}
else
{
// not in cache
// or in cache but invalid
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": not in cache" << std::endl;
pthread_mutex_unlock(&mutex);
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": "
<< "Requesting \"" << request.line() << "\" from " << host << std::endl;
pthread_mutex_unlock(&mutex);
send(server_fd, myrequest, myrequest_len, MSG_NOSIGNAL);
char myresponse[10000] = {0};
int myresponse_len = recv(server_fd, myresponse, sizeof(myresponse), 0);
if (myresponse_len > 0)
{
Response response;
HttpResponseParser parser;
parser.parse(response, myresponse, myresponse + sizeof(myresponse));
string Line = response.line;
if (Line.find(502) != std::string::npos)
{
string bad_gateway = "HTTP/1.1 502 Bad Gateway";
send(client_fd, bad_gateway.c_str(), bad_gateway.size(), 0);
}
else
{
// send message
std::string str_myresponse = myresponse;
if (str_myresponse.find("chunked") != std::string::npos)
{
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": not cacheable because chunk" << std::endl;
pthread_mutex_unlock(&mutex);
send(myclient->getFD(), myresponse, myresponse_len, 0);
char chunked_msg[10000] = {0};
while (1)
{
int len = recv(server_fd, chunked_msg, sizeof(chunked_msg), 0);
if (len <= 0)
{
std::cout << "chunked break\n";
break;
}
send(myclient->getFD(), chunked_msg, len, 0);
}
}
else
{
send(myclient->getFd(), myresponse, myresponse_len, 0);
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": Received \"" << response.line() << "\" from " << host << std::endl;
pthread_mutex_unlock(&mutex);
int ifcache = 1;
for (std::vector<Response::HeaderItem>::const_iterator it = response.headers.begin(); it != response.headers.end(); ++it)
{
if (it->name.compare("Cache-Control") == 0)
{
if (it->value.compare("no-store"))
{
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": not cacheable because no-store" << std::endl;
pthread_mutex_unlock(&mutex);
ifcache = 0;
}
else if (it->value.compare("private") == 0)
{
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": not cacheable because private" << std::endl;
pthread_mutex_unlock(&mutex);
ifcache = 0;
}
else if (it->value.compare("no-cache") == 0)
{
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": NOTE Cache-Control: must-revalidate" << std::endl;
pthread_mutex_unlock(&mutex);
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": Cached, but requires re-validation" << std::endl;
pthread_mutex_unlock(&mutex);
}
else if (it->value.find("max-age") != std::string::npos)
{
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": NOTE Cache-Control: must-revalidate" << std::endl;
pthread_mutex_unlock(&mutex);
}
}
if (it->name.compare("ETag") == 0)
{
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": NOTE ETag: " << it->value << std::endl;
pthread_mutex_unlock(&mutex);
}
if (it->name.compare("Last-Modified") == 0)
{
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": NOTE Last-Modified: " << it->value << std::endl;
pthread_mutex_unlock(&mutex);
}
if (it->name.compare("Expires") == 0)
{
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": Cached, expires at" << it->value << std::endl;
pthread_mutex_unlock(&mutex);
}
}
if (iscache == 1)
{
Cache[request.line] = response;
}
}
}
}
}
}
}
else
{
// 400 code
string bad = "HTTP/1.1 400 Bad Request";
send(client_fd, bad.c_str(), bad.size(), MSG_NOSIGNAL);
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": Responding \"HTTP/1.1 400 Bad Request\"" << std::endl;
pthread_mutex_unlock(&mutex);
return NULL;
}
}
else
{
// 400 code
string bad = "HTTP/1.1 400 Bad Request";
send(client_fd, bad.c_str(), bad.size(), MSG_NOSIGNAL);
pthread_mutex_lock(&mutex);
logFile << myclient->getId() << ": Responding \"HTTP/1.1 400 Bad Request\"" << std::endl;
pthread_mutex_unlock(&mutex);
return NULL;
}
close(server_fd);
close(myclient->getFd());
}
}
std::string getTime()
{
time_t now = time(0);
char *dt = ctime(&now);
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
return std::string(dt);
}