-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold.txt
More file actions
607 lines (532 loc) · 20.2 KB
/
old.txt
File metadata and controls
607 lines (532 loc) · 20.2 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#include <cstdlib> //exit, atoi
#include <cstdio> //perror
#include <netdb.h> //gethostbyname
#include <cstring> //memcpy
#include <sys/socket.h> //sockaddr_in, AF_INET, socket, bind, listen, connect
#include <poll.h> //poll
#include <unistd.h> //read, write, close
#include <arpa/inet.h> //htonl, htons
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <fstream>
#define LITTLE_STRING_SIZE 4096
#define DEFAULT_BUFFER_SIZE (4096)
#define DEFAULT_PORT 80
#define RESULT_CORRECT 1
#define RESULT_INCORRECT -1
std::ofstream out_to_file("output.txt");
struct Buffer {
char * buf;
size_t start;
size_t end;
size_t size;
bool is_correct;
Buffer(size_t size_buf) {
buf = (char*)malloc(size_buf);
if (NULL == buf) {
perror("new buf");
is_correct = false;
exit(EXIT_FAILURE);
}
start = 0;
end = 0;
size = size_buf;
is_correct = true;
}
int resize(size_t new_size_buf) {
char * result = (char *)realloc(buf, new_size_buf);
if (NULL == result) {
perror("realloc");
free(buf);
exit(EXIT_FAILURE);
}
buf = result;
size = new_size_buf;
}
// todo rename to 'push_back_data'
void add_data_to_end(const char * from, size_t size_data) {
while (end + size_data > size) {
resize(size * 2);
}
memcpy(buf + end, from, size_data);
end += size_data;
}
// todo rename to 'push_back_symbol'
void add_symbol_to_end(char c) {
while (end + 1 > size) {
resize(size * 2);
}
buf[end++] = c;
}
~Buffer() {
fprintf(stderr, "Destructor buffer!!!!\n");
fprintf(stderr, "Buf: %d\n", buf);
fprintf(stderr, "Size: %d\n", size);
if (is_correct){
free(buf);
perror("free");
}
else {
fprintf(stderr, "Destructor buffer not correct\n");
}
fprintf(stderr, "Done\n");
}
};
std::map<std::pair<std::string, std::string>, std::pair<char *, size_t>> m_cache;
struct Client {
int my_socket;
int http_socket;
bool is_correct_my_socket;
bool is_correct_http_socket;
Buffer * buffer_in;
Buffer * buffer_server_request;
Buffer * buffer_out;
bool is_received_get_request;
bool is_closed;
bool is_closed_correct;
bool is_data_cached;
bool is_connected;
struct sockaddr_in dest_addr;
int poll_id;
std::pair<std::string, std::string> first_line_and_host;
Client(int my_socket, size_t size_buf) {
this->my_socket = my_socket;
this->http_socket = -1;
is_correct_my_socket = true;
is_correct_http_socket = true;
buffer_in = new Buffer(size_buf);
buffer_out = new Buffer(size_buf);
buffer_server_request = new Buffer(size_buf);
is_received_get_request = false;
is_closed = false;
is_data_cached = false;
is_connected = false;
}
void add_result_to_cache() {
if (!m_cache.count(first_line_and_host)) {
size_t size_data = buffer_out->end;
char * data = (char*)malloc(size_data);
memcpy(data, buffer_out->buf, size_data);
m_cache[first_line_and_host] = std::make_pair(data, size_data);
}
}
void set_closed_correct() {
is_closed = true;
is_closed_correct = true;
}
void set_closed_incorrect() {
is_closed = true;
is_closed_correct = false;
}
~Client() {
fprintf(stderr, "Destructor client!!!!\n");
if (is_correct_my_socket) {
close(my_socket);
}
if (is_closed_correct) {
add_result_to_cache();
}
delete buffer_in;
delete buffer_server_request;
delete buffer_out;
}
};
int server_socket;
std::vector<Client*> clients;
void init_server_socket(unsigned short server_port) {
server_socket = socket(PF_INET, SOCK_STREAM, 0);
if (-1 == server_socket) {
perror("Error while creating serverSocket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_address;
server_address.sin_family = PF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(server_port);
if (-1 == (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)))) {
perror("Error while binding");
exit(EXIT_FAILURE);
}
if (-1 == listen(server_socket, 1024)) {
perror("Error while listen()");
exit(EXIT_FAILURE);
}
}
void accept_incoming_connection() {
struct sockaddr_in client_address;
int address_size = sizeof(sockaddr_in);
int client_socket = accept(server_socket, (struct sockaddr *)&client_address,
(socklen_t *)&address_size);
if (client_socket <= 0) {
perror("Error while accept()");
exit(EXIT_FAILURE);
}
Client * new_actor = new Client(client_socket, DEFAULT_BUFFER_SIZE);
clients.push_back(new_actor);
}
// two auxiliary vectors to delete closed on uncorrect clients
std::vector<bool> clients_to_delete;
std::vector<Client*> rest_clients;
std::pair<std::string, std::string> parse_hostname_and_path(char * uri) {
char host_name[LITTLE_STRING_SIZE];
char path[LITTLE_STRING_SIZE];
char * protocolEnd = strstr(uri, "://");
if (NULL == protocolEnd) {
perror("Wrong protocol");
return std::make_pair("", "");
}
char * host_end = strchr(protocolEnd + 3, '/');
size_t host_length = 0;
if (NULL == host_end) {
host_length = strlen(protocolEnd + 3);
path[0] = '/';
path[1] = '\0';
}
else {
host_length = host_end - (protocolEnd + 3);
size_t path_size = strlen(uri) - (host_end - uri);
strncpy(path, host_end, path_size);
path[path_size] = '\0';
}
strncpy(host_name, protocolEnd + 3, host_length);
host_name[host_length] = '\0';
return std::make_pair(std::string(host_name), std::string(path));
}
// return host name and first line to server GET request
// "" - when any error in parsing
std::pair<std::string, std::string> get_new_first_line_and_hostname(int i, char * p_new_line) {
Buffer * client_buffer_in = clients[i]->buffer_in;
if (client_buffer_in->size < 3 ||
client_buffer_in->buf[0] != 'G' ||
client_buffer_in->buf[1] != 'E' ||
client_buffer_in->buf[2] != 'T')
{
fprintf(stderr, "Not GET request\n");
return std::make_pair("", "");
}
char first_line[LITTLE_STRING_SIZE];
size_t first_line_length = p_new_line - client_buffer_in->buf;
strncpy(first_line, client_buffer_in->buf, first_line_length);
first_line[first_line_length] = '\0';
fprintf(stderr, "First line from client: %s\n", first_line);
char * method = strtok(first_line, " ");
char * uri = strtok(NULL, " ");
char * version = strtok(NULL, "\n\0");
fprintf(stderr, "method: %s\n", method);
fprintf(stderr, "uri: %s\n", uri);
fprintf(stderr, "version: %s\n", version);
std::pair<std::string, std::string> parsed = parse_hostname_and_path(uri);
std::string host_name = parsed.first;
std::string path = parsed.second;
if ("" == host_name || "" == path) {
fprintf(stderr, "Hostname or path haven't been parsed\n");
return std::make_pair("", "");
}
fprintf(stderr, "HostName: \'%s\'\n", host_name.c_str());
fprintf(stderr, "Path: %s\n", path.c_str());
std::string http10str = "HTTP/1.0";
std::string new_first_line = std::string(method) + " " + path + " " + http10str;
return std::make_pair(host_name, new_first_line);
}
void push_first_data_request(int i, std::string new_first_line, Buffer * client_buffer_in, size_t i_next_line) {
size_t size_without_first_line = client_buffer_in->end - i_next_line;
clients[i]->buffer_server_request->add_data_to_end(new_first_line.c_str(), new_first_line.size());
clients[i]->buffer_server_request->add_symbol_to_end('\n');
clients[i]->buffer_server_request->add_data_to_end(client_buffer_in->buf + i_next_line, size_without_first_line);
clients[i]->buffer_server_request->buf[clients[i]->buffer_server_request->end] = '\0';
client_buffer_in->start = client_buffer_in->end;
fprintf(stderr, "\n!!!!! New request: \n%s\n\n", clients[i]->buffer_server_request->buf);
}
void push_first_data_request_from_cache(int i, std::pair<char*, size_t> cache) {
clients[i]->is_data_cached = true;
Buffer * buffer = clients[i]->buffer_server_request;
buffer->add_data_to_end(cache.first, cache.second);
}
int create_tcp_connection_to_request(int i, std::string host_name) {
struct hostent * host_info = gethostbyname(host_name.c_str());
if (NULL == host_info) {
perror("Error while gethostbyname");
return RESULT_INCORRECT;
}
int http_socket = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == http_socket) {
perror("Error while socket()");
return RESULT_INCORRECT;
}
struct sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(DEFAULT_PORT);
memcpy(&dest_addr.sin_addr, host_info->h_addr, host_info->h_length);
clients[i]->dest_addr = dest_addr;
if (-1 == connect(http_socket, (struct sockaddr *)&dest_addr, sizeof(dest_addr))) {
perror("Error while connect()");
return RESULT_INCORRECT;
}
clients[i]->http_socket = http_socket;
}
int handle_first_line_proxy_request(int i, char * p_new_line, size_t i_next_line) {
Buffer * client_buffer_in = clients[i]->buffer_in;
std::pair<std::string, std::string> parsed = get_new_first_line_and_hostname(i, p_new_line);
std::string host_name = parsed.first;
std::string new_first_line = parsed.second;
fprintf(stderr, "Hostname: %s\n", host_name.c_str());
fprintf(stderr, "New first line: %s\n", new_first_line.c_str());
if ("" == host_name || "" == new_first_line) {
fprintf(stderr, "Can not correctly parse first line\n");
return RESULT_INCORRECT;
}
if (/*false && */m_cache.count(parsed)) {
push_first_data_request_from_cache(i, m_cache[parsed]);
return RESULT_CORRECT;
}
else {
push_first_data_request(i, new_first_line, client_buffer_in, i_next_line);
int result_connection = create_tcp_connection_to_request(i, host_name);
return result_connection;
}
}
int move_end_client_in_buffer(int i, ssize_t received) {
int result = RESULT_CORRECT;
Buffer * client_buffer_in = clients[i]->buffer_in;
client_buffer_in->end += received;
if (client_buffer_in->end == client_buffer_in->size) {
size_t new_size = client_buffer_in->size * 2;
result = client_buffer_in->resize(new_size);
}
client_buffer_in->buf[client_buffer_in->end] = '\0';
fprintf(stderr, "\nReceived from client: \n%s\n\n", client_buffer_in->buf);
return result;
}
void receive_request_from_client(int i) {
Buffer * client_buffer_in = clients[i]->buffer_in;
ssize_t received = recv(clients[i]->my_socket, client_buffer_in->buf + client_buffer_in->end,
client_buffer_in->size - client_buffer_in->end, 0);
switch (received) {
case -1:
perror("Error while read()");
clients[i]->is_correct_my_socket = false;
clients[i]->set_closed_incorrect();
clients_to_delete[i] = true;
break;
case 0:
fprintf(stderr, "Close client\n");
clients_to_delete[i] = true;
clients[i]->set_closed_correct();
break;
default:
int res = move_end_client_in_buffer(i, received);
if (RESULT_INCORRECT == res) {
clients_to_delete[i] = true;
clients[i]->set_closed_incorrect();
break;
}
if (!clients[i]->is_received_get_request) {
char * p_new_line = strchr(client_buffer_in->buf, '\n');
if (p_new_line != NULL) {
clients[i]->is_received_get_request = true;
size_t i_next_line = (p_new_line - client_buffer_in->buf) + 1;
int result = handle_first_line_proxy_request(i, p_new_line, i_next_line);
if (RESULT_INCORRECT == result) {
clients_to_delete[i] = true;
clients[i]->set_closed_incorrect();
break;
}
}
}
else {
char * from = client_buffer_in->buf + client_buffer_in->start;
size_t size_data = (client_buffer_in->end - client_buffer_in->start);
clients[i]->buffer_server_request->add_data_to_end(from, size_data);
client_buffer_in->start = client_buffer_in->end;
}
}
}
void send_answer_to_client(int i) {
bool flag_sent_answer_to_client = !clients_to_delete[i];
while (flag_sent_answer_to_client &&
clients[i]->is_received_get_request &&
clients[i]->buffer_out->end > clients[i]->buffer_out->start)
{
fprintf(stderr, "\nHave data to send to client %d\n", i);
Buffer * client_buffer_out = clients[i]->buffer_out;
ssize_t sent = send(clients[i]->my_socket, client_buffer_out->buf + client_buffer_out->start,
(size_t)(client_buffer_out->end - client_buffer_out->start), 0);
fprintf(stderr, "Sent: %ld\n", sent);
switch (sent) {
case -1:
perror("Error while send to client");
clients[i]->is_correct_my_socket = false;
clients_to_delete[i] = true;
clients[i]->set_closed_incorrect();
flag_sent_answer_to_client = false;
break;
case 0:
clients_to_delete[i] = true;
clients[i]->set_closed_correct();
flag_sent_answer_to_client = false;
break;
default:
client_buffer_out->start += sent;
}
}
}
void delete_finished_clients() {
fprintf(stderr, "\nClients size before clean: %ld\n", clients.size());
rest_clients.clear();
for (int i = 0; i < clients_to_delete.size(); ++i) {
if (clients[i]->is_closed) {
//fprintf(stderr, "%d - delete\n", i);
delete clients[i];
}
else {
//fprintf(stderr, "%d - rest, buf: %d, %d\n", i, clients[i]->buffer_in->buf, clients[i]->buffer_out->buf);
rest_clients.push_back(clients[i]);
}
}
clients = rest_clients;
fprintf(stderr, "Clients size after clean: %ld\n", clients.size());
}
void receive_server_response(int i) {
Buffer * client_buffer_out = clients[i]->buffer_out;
ssize_t received = recv(clients[i]->http_socket, client_buffer_out->buf + client_buffer_out->end,
(size_t)(client_buffer_out->size - client_buffer_out->end), 0);
switch (received) {
case -1:
perror("recv(http)");
clients[i]->is_correct_http_socket = false;
clients_to_delete[i] = true;
clients[i]->set_closed_incorrect();
break;
case 0:
fprintf(stderr, "Close http connection\n");
close(clients[i]->http_socket);
clients[i]->http_socket = -1;
break;
default:
client_buffer_out->end += received;
if (client_buffer_out->end == client_buffer_out->size) {
size_t new_size_buf = client_buffer_out->size * 2;
int res = client_buffer_out->resize(new_size_buf);
if (-1 == res) {
clients_to_delete[i] = true;
clients[i]->set_closed_incorrect();
break;
}
}
client_buffer_out->buf[client_buffer_out->end] ='\0';
fprintf(stderr, "\n\nReceived from http:\n%s\n\n", client_buffer_out->buf);
out_to_file << client_buffer_out->buf << std::endl << std::endl;
}
}
void send_request_to_server(int i) {
bool flag_send_request_to_server = !clients_to_delete[i];
while ( flag_send_request_to_server &&
clients[i]->is_received_get_request &&
!clients[i]->is_data_cached &&
clients[i]->buffer_server_request->end > clients[i]->buffer_server_request->start) {
fprintf(stderr, "\nHave data to send to http server (i):%d (fd):%d\n", i, clients[i]->http_socket);
Buffer * client_buffer_in = clients[i]->buffer_server_request;
ssize_t sent = send(clients[i]->http_socket, client_buffer_in->buf,
(size_t)(client_buffer_in->end - client_buffer_in->start), 0);
fprintf(stderr, "Sent to http: %ld, %d\n", sent, client_buffer_in->end - client_buffer_in->start);
switch (sent) {
case -1:
perror("Error while send(http)");
clients[i]->is_correct_http_socket = false;
clients_to_delete[i] = true;
clients[i]->set_closed_incorrect();
flag_send_request_to_server = false;
break;
case 0:
close(clients[i]->http_socket);
clients[i]->http_socket = -1;
flag_send_request_to_server = false;
break;
default:
client_buffer_in->start += sent;
}
}
}
int main(int argc, char *argv[]) {
// todo GET_OPT!!!
if (2 != argc) {
perror("Wrong number of arguments");
exit(EXIT_FAILURE);
}
unsigned short server_port = (unsigned short)atoi(argv[1]);
if (0 == server_port) {
perror("Wrong port for listening");
exit(EXIT_FAILURE);
}
init_server_socket(server_port);
bool flag_execute = true;
for ( ; flag_execute ; ) {
fd_set fds_read;
fd_set fds_write;
FD_ZERO(&fds_read);
FD_ZERO(&fds_write);
int max_fd = 0;
FD_SET(server_socket, &fds_read);
max_fd = server_socket;
for (auto client : clients) {
FD_SET(client->my_socket, &fds_read);
FD_SET(client->my_socket, &fds_write);
max_fd = std::max(max_fd, client->my_socket);
if (client->is_received_get_request) {
FD_SET(client->http_socket, &fds_read);
FD_SET(client->http_socket, &fds_write);
max_fd = std::max(max_fd, client->http_socket);
}
}
int activity = select(max_fd + 1, &fds_read, &fds_write, NULL, NULL);
fprintf(stderr, "Activity: %d\n", activity);
if (-1 == activity) {
perror("Error while select()");
continue;
}
else if (0 == activity) {
perror("poll() returned 0");
continue;
}
if (FD_ISSET(server_socket, &fds_read)) {
fprintf(stderr, "Have incoming client connection\n");
accept_incoming_connection();
}
clients_to_delete.assign(clients.size(), false);
for (int i = 0; i < clients.size(); ++i) {
if (FD_ISSET(clients[i]->my_socket, &fds_read)) {
fprintf(stderr, "Have data from client %d\n", i);
receive_request_from_client(i);
}
if (FD_ISSET(clients[i]->my_socket, &fds_write)) {
//send_request_to_server(i);
}
send_request_to_server(i);
}
delete_finished_clients();
clients_to_delete.assign(clients.size(), false);
for (int i = 0; i < clients.size(); ++i) {
if (!clients[i]->is_received_get_request) {
continue;
}
if (clients[i]->is_received_get_request && FD_ISSET(clients[i]->http_socket, &fds_read)) {
fprintf(stderr, "Have data (http response), (id):%d\n", i);
receive_server_response(i);
}
send_answer_to_client(i);
}
delete_finished_clients();
}
// free cache
for (auto record : m_cache) {
free(record.second.first);
}
// close clients
for (auto client : clients) {
delete client;
}
close(server_socket);
return 0;
}