-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
356 lines (317 loc) · 11.7 KB
/
main.cpp
File metadata and controls
356 lines (317 loc) · 11.7 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <iostream>
#include <string>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <thread>
#include "Request.h"
#include <unordered_map>
using namespace std;
#define BUFF_SIZE 65535
//unordered_map cache<string, string>;
void handle_connect(Request req, int ID, int client_connection_fd) {
int status_n;
int dest_fd;
struct addrinfo host_info_n;
struct addrinfo *host_info_list_n;
const char *hostname_n = req.host.c_str();
string portstr = to_string(req.port);
const char *port_n = portstr.c_str();
memset(&host_info_n, 0, sizeof(host_info_n));
host_info_n.ai_family = AF_UNSPEC;
host_info_n.ai_socktype = SOCK_STREAM;
status_n = getaddrinfo(hostname_n, port_n, &host_info_n, &host_info_list_n);
if (status_n != 0) {
cerr << "Error: cannot get address info for host" << endl;
return;
}
dest_fd = socket(host_info_list_n->ai_family,
host_info_list_n->ai_socktype,
host_info_list_n->ai_protocol);
if (dest_fd == -1) {
cerr << "Error: cannot create socket" << endl;
return;
}
status_n = connect(dest_fd, host_info_list_n->ai_addr, host_info_list_n->ai_addrlen);
if (status_n == -1) {
cerr << "Error: cannot connect to socket" << endl;
return;
} else
cout << "Successfully connected to " << req.host << " on port " << req.port << endl;
std::string ok("HTTP/1.1 200 Connection Established\r\n\r\n");
ssize_t ok_ack = send(client_connection_fd, ok.c_str(),strlen(ok.c_str()),0);
if(ok_ack < 0){
//print_error(ID,-200);
cout << "ID: " << ID << "cannot send 200 ok back to client" << endl;
close(dest_fd);
return;
}
int fdmax;
fd_set readfds;
char buff[BUFF_SIZE];
while(1){
FD_ZERO(&readfds);
FD_SET(dest_fd,&readfds);
FD_SET(client_connection_fd,&readfds);
fdmax = max(dest_fd, client_connection_fd);
if(select(fdmax + 1, &readfds, NULL, NULL, NULL) == -1){
//print_error(ID,-300);
cout << "error select" << endl;
break;
}
memset(buff, 0, BUFF_SIZE);
if(FD_ISSET(client_connection_fd, &readfds)){
ssize_t recv_len = recv(client_connection_fd, buff, BUFF_SIZE, MSG_WAITALL);
if(recv_len < 0){
//print_error(ID,-100);
break;
}
if(recv_len == 0){
close(dest_fd);
return;
}
cout << "recv length is " << recv_len<< " from client:" << endl;
cout << buff << endl;
ssize_t send_to_dest = send(dest_fd, buff, recv_len, 0);
if(send_to_dest < 0){
//print_error(ID,-200);
break;
}
}
else if(FD_ISSET(dest_fd, &readfds)){
ssize_t recv_len = recv(dest_fd, buff, BUFF_SIZE, MSG_WAITALL);
if(recv_len < 0){
//print_error(ID,-100);
break;
}
if(recv_len == 0){
close(dest_fd);
break;
}
cout << "recv length is " << recv_len << " from destination:" << endl;
cout << buff << endl;
ssize_t send_to_client = send(client_connection_fd, buff, recv_len, 0);
if(send_to_client < 0){
//print_error(ID,-200);
break;
}
}
}
close(dest_fd);
return;
}
void handle_get(Request req, int ID, int client_connection_fd) {
int status_n;
int dest_fd;
struct addrinfo host_info_n;
struct addrinfo *host_info_list_n;
const char *hostname_n = req.host.c_str();
const char *port_n = "80";
memset(&host_info_n, 0, sizeof(host_info_n));
host_info_n.ai_family = AF_UNSPEC;
host_info_n.ai_socktype = SOCK_STREAM;
status_n = getaddrinfo(hostname_n, port_n, &host_info_n, &host_info_list_n);
if (status_n != 0) {
cerr << "Error: cannot get address info for host" << endl;
return;
}
dest_fd = socket(host_info_list_n->ai_family,
host_info_list_n->ai_socktype,
host_info_list_n->ai_protocol);
if (dest_fd == -1) {
cerr << "Error: cannot create socket" << endl;
return;
}
status_n = connect(dest_fd, host_info_list_n->ai_addr, host_info_list_n->ai_addrlen);
if (status_n == -1) {
cerr << "Error: cannot connect to socket" << endl;
return;
} else
cout << "Successfully connected to " << req.host << " on port " << req.port << endl;
//1. is there any cache? if there is, judge the cache, or else send request to web server
//2. test the cache-control's max-age, expires
char reqbuff[BUFF_SIZE];
memset(reqbuff, 0, BUFF_SIZE);
size_t end_line = req.ori_req.find("\r\n\r\n");
string header = req.ori_req.substr(0, end_line + 4);
strcpy(reqbuff, header.c_str());
ssize_t send_to_dest = send(dest_fd, reqbuff, header.length(), 0); // proxy forward request to server before\r\n\r\n
if(send_to_dest < 0){
cout << "len < 0" << endl;
return;
}
char buff1[BUFF_SIZE];
ssize_t recv_len1 = recv(dest_fd, buff1, BUFF_SIZE, MSG_WAITALL); // proxy receive info from server
if (recv_len1 < 0) {
cout << "len < 0" << endl;
return;
}
if (recv_len1 == 0) {
close(dest_fd);
return;
}
cout << "recv length is " << recv_len1<< " from server:" << endl;
cout << buff1 << endl;
ssize_t send_to_client = send(client_connection_fd, buff1, BUFF_SIZE, 0); //proxy returns request to client
if (send_to_client < 0) {
cout << "len < 0" << endl;
}
close(dest_fd);
return;
}
void handle_post(Request req, int ID, int client_connection_fd) {
int status_n;
int dest_fd;
struct addrinfo host_info_n;
struct addrinfo *host_info_list_n;
const char *hostname_n = req.host.c_str();
const char *port_n = "80";
memset(&host_info_n, 0, sizeof(host_info_n));
host_info_n.ai_family = AF_UNSPEC;
host_info_n.ai_socktype = SOCK_STREAM;
status_n = getaddrinfo(hostname_n, port_n, &host_info_n, &host_info_list_n);
if (status_n != 0) {
cerr << "Error: cannot get address info for host" << endl;
return;
}
dest_fd = socket(host_info_list_n->ai_family,
host_info_list_n->ai_socktype,
host_info_list_n->ai_protocol);
if (dest_fd == -1) {
cerr << "Error: cannot create socket" << endl;
return;
}
status_n = connect(dest_fd, host_info_list_n->ai_addr, host_info_list_n->ai_addrlen);
if (status_n == -1) {
cerr << "Error: cannot connect to socket" << endl;
return;
} else
cout << "Successfully connected to " << req.host << " on port " << req.port << endl;
//parse header from req
char reqbuff1[BUFF_SIZE];
char reqbuff2[BUFF_SIZE];
memset(reqbuff1, 0, BUFF_SIZE);
memset(reqbuff2, 0, BUFF_SIZE);
strcpy(reqbuff1, req.ori_req.c_str());
ssize_t send_to_dest1 = send(dest_fd, reqbuff1, req.ori_req.length(), 0); // proxy forward request to server before\r\n\r\n(POST header)
if(send_to_dest1 < 0){
cout << "len < 0" << endl;
return;
}
char buff1[BUFF_SIZE];
ssize_t recv_len1 = recv(dest_fd, buff1, BUFF_SIZE, MSG_WAITALL); // proxy receive info from server
if (recv_len1 < 0) {
cout << "len < 0" << endl;
return;
}
if (recv_len1 == 0) {
close(dest_fd);
return;
}
cout << "recv length is " << recv_len1<< " from server:" << endl;
cout << buff1 << endl;
ssize_t send_to_client = send(client_connection_fd, buff1, BUFF_SIZE, 0); //proxy returns request to client
if (send_to_client < 0) {
cout << "len < 0" << endl;
}
close(dest_fd);
return;
}
void multi_thread(int ID, int client_connection_fd){
char reqbuff[BUFF_SIZE];
ssize_t recv_len = recv(client_connection_fd, reqbuff, BUFF_SIZE, 0); // header file
reqbuff[recv_len] = 0;
if(recv_len == -1){
cout << "cannot recv request from client" << endl;
return;
}
else if(recv_len == 0){
cout << "recv nothing from client" << endl;
return;
}
cout << "Resquest From Client: " << endl;
cout << reqbuff << endl;
Request req(reqbuff);
if (req.method == "CONNECT") {
cout << "req.method: " << req.method << endl;
cout << "req.host: " << req.host << endl;
cout << "req.port: " << req.port << endl;
cout << endl;
}
if (req.method == "GET") {
cout << "req.URI: " << req.URI << endl;
}
if(req.method == "CONNECT"){
handle_connect(req, ID, client_connection_fd);
}
else if(req.method == "GET"){
//handle_get
handle_get(req, ID, client_connection_fd);
}
else if(req.method == "POST"){
handle_post(req, ID, client_connection_fd);
}
}
int main(int argc, char *argv[]) {
//************ server part: accept connection from browser ***********//
int ID = 0;
int status;
int socket_fd;
struct addrinfo host_info;
struct addrinfo *host_info_list;
const char *hostname = NULL; //local
const char *port = "12345";
//initialize as server to browser waiting to accept
memset(&host_info, 0, sizeof(host_info));
host_info.ai_family = AF_UNSPEC;
host_info.ai_socktype = SOCK_STREAM;
host_info.ai_flags = AI_PASSIVE;
status = getaddrinfo(hostname, port, &host_info, &host_info_list);
if (status != 0) {
cerr << "Error: cannot get address info for host" << endl;
return -1;
}
socket_fd = socket(host_info_list->ai_family,
host_info_list->ai_socktype,
host_info_list->ai_protocol);
if (socket_fd == -1) {
cerr << "Error: cannot create socket" << endl;
return -1;
}
int yes = 1;
status = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
status = bind(socket_fd, host_info_list->ai_addr, host_info_list->ai_addrlen);
if (status == -1) {
cerr << "Error: cannot bind socket" << endl;
return -1;
}
status = listen(socket_fd, 100);
if (status == -1) {
cerr << "Error: cannot listen on socket" << endl;
return -1;
}
while (1) {
ID++;
//********** server part: wait to accept browser request ********//
cout << "================== Waiting for connection on port " << port << " ==========================" << endl;
cout << "ID = " << ID << endl;
struct sockaddr_storage socket_addr;
socklen_t socket_addr_len = sizeof(socket_addr);
int client_connection_fd;
client_connection_fd = accept(socket_fd, (struct sockaddr *) &socket_addr, &socket_addr_len); // waiting for connection from client
if (client_connection_fd == -1) {
cerr << "Error: cannot accept connection on socket" << endl;
return -1;
}
string ip(inet_ntoa(((struct sockaddr_in *) &socket_addr)->sin_addr));
cout << "Browser ip: " << ip << endl;
thread(multi_thread, ID, client_connection_fd).detach();
}
freeaddrinfo(host_info_list);
close(socket_fd);
return 0;
}