-
Notifications
You must be signed in to change notification settings - Fork 47
/
helloworld.cpp
478 lines (412 loc) · 15.6 KB
/
helloworld.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
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
// Server side C program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <errno.h>
//#include <fstream>
//#include <sstream>
//#include <arpa/inet.h>
//#include <string>
//#include <netdb.h>
//#include <unistd.h>
//using namespace std;
#define PORT 8081
char* parse(char line[], const char symbol[]);
char* parse_method(char line[], const char symbol[]);
char* find_token(char line[], const char symbol[], const char match[]);
int send_message(int fd, char image_path[], char head[]);
//void setHttpHeader_other(char httpHeader[], char *path);
//void setHttpHeader(char httpHeader[]);
//void report(struct sockaddr_in *serverAddress);
/*std::string read_image(const std::string& image_path);
int send_image(int & fd, std::string& image);*/
//https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
//https://stackoverflow.com/questions/45670369/c-web-server-image-not-showing-up-on-browser
//https://www.linuxhowtos.org/C_C++/socket.htm
char http_header[25] = "HTTP/1.1 200 Ok\r\n";
int main(int argc, char const *argv[])
{
int server_fd, new_socket, pid;
long valread;
struct sockaddr_in address;
int addrlen = sizeof(address);
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("In sockets");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
memset(address.sin_zero, '\0', sizeof address.sin_zero);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0)
{
perror("In bind");
close(server_fd);
exit(EXIT_FAILURE);
}
if (listen(server_fd, 10) < 0)
{
perror("In listen");
exit(EXIT_FAILURE);
}
//report(&address);
//setHttpHeader(httpHeader);
while(1)
{
printf("\n+++++++ Waiting for new connection ++++++++\n\n");
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
{
perror("In accept");
exit(EXIT_FAILURE);
}
//Create child process to handle request from different client
pid = fork();
if(pid < 0){
perror("Error on fork");
exit(EXIT_FAILURE);
}
if(pid == 0){
char buffer[30000] = {0};
valread = read( new_socket , buffer, 30000);
printf("\n buffer message: %s \n ", buffer);
char *parse_string_method = parse_method(buffer, " "); //Try to get the path which the client ask for
printf("Client method: %s\n", parse_string_method);
//char httpHeader1[800021] = "HTTP/1.1 200 OK\r\n\n";
char *parse_string = parse(buffer, " "); //Try to get the path which the client ask for
printf("Client ask for path: %s\n", parse_string);
//prevent strtok from changing the string
//https://wiki.sei.cmu.edu/confluence/display/c/STR06-C.+Do+not+assume+that+strtok%28%29+leaves+the+parse+string+unchanged
//https://stackoverflow.com/questions/5099669/invalid-conversion-from-void-to-char-when-using-malloc/5099675
char *copy = (char *)malloc(strlen(parse_string) + 1);
strcpy(copy, parse_string);
char *parse_ext = parse(copy, "."); // get the file extension such as JPG, jpg
char *copy_head = (char *)malloc(strlen(http_header) +200);
strcpy(copy_head, http_header);
if(parse_string_method[0] == 'G' && parse_string_method[1] == 'E' && parse_string_method[2] == 'T'){
//https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
if(strlen(parse_string) <= 1){
//case that the parse_string = "/" --> Send index.html file
//write(new_socket , httpHeader , strlen(httpHeader));
char path_head[500] = ".";
strcat(path_head, "/index.html");
strcat(copy_head, "Content-Type: text/html\r\n\r\n");
send_message(new_socket, path_head, copy_head);
}
else if ((parse_ext[0] == 'j' && parse_ext[1] == 'p' && parse_ext[2] == 'g') || (parse_ext[0] == 'J' && parse_ext[1] == 'P' && parse_ext[2] == 'G'))
{
//send image to client
char path_head[500] = ".";
strcat(path_head, parse_string);
strcat(copy_head, "Content-Type: image/jpeg\r\n\r\n");
send_message(new_socket, path_head, copy_head);
}
else if (parse_ext[0] == 'i' && parse_ext[1] == 'c' && parse_ext[2] == 'o')
{
//https://www.cisco.com/c/en/us/support/docs/security/web-security-appliance/117995-qna-wsa-00.html
char path_head[500] = ".";
strcat(path_head, "/img/favicon.png");
strcat(copy_head, "Content-Type: image/vnd.microsoft.icon\r\n\r\n");
send_message(new_socket, path_head, copy_head);
}
else if (parse_ext[0] == 't' && parse_ext[1] == 't' && parse_ext[2] == 'f')
{
//font type, to display icon from FontAwesome
char path_head[500] = ".";
strcat(path_head, parse_string);
strcat(copy_head, "Content-Type: font/ttf\r\n\r\n");
send_message(new_socket, path_head, copy_head);
}
else if (parse_ext[strlen(parse_ext)-2] == 'j' && parse_ext[strlen(parse_ext)-1] == 's')
{
//javascript
char path_head[500] = ".";
strcat(path_head, parse_string);
strcat(copy_head, "Content-Type: text/javascript\r\n\r\n");
send_message(new_socket, path_head, copy_head);
}
else if (parse_ext[strlen(parse_ext)-3] == 'c' && parse_ext[strlen(parse_ext)-2] == 's' && parse_ext[strlen(parse_ext)-1] == 's')
{
//css
char path_head[500] = ".";
strcat(path_head, parse_string);
strcat(copy_head, "Content-Type: text/css\r\n\r\n");
send_message(new_socket, path_head, copy_head);
}
else if (parse_ext[0] == 'w' && parse_ext[1] == 'o' && parse_ext[2] == 'f')
{
//Web Open Font Format woff and woff2
char path_head[500] = ".";
strcat(path_head, parse_string);
strcat(copy_head, "Content-Type: font/woff\r\n\r\n");
send_message(new_socket, path_head, copy_head);
}
else if (parse_ext[0] == 'm' && parse_ext[1] == '3' && parse_ext[2] == 'u' && parse_ext[3] == '8')
{
//Web Open m3u8
char path_head[500] = ".";
strcat(path_head, parse_string);
strcat(copy_head, "Content-Type: application/vnd.apple.mpegurl\r\n\r\n");
send_message(new_socket, path_head, copy_head);
}
else if (parse_ext[0] == 't' && parse_ext[1] == 's')
{
//Web Open ts
char path_head[500] = ".";
strcat(path_head, parse_string);
strcat(copy_head, "Content-Type: video/mp2t\r\n\r\n");
send_message(new_socket, path_head, copy_head);
}
else{
//send other file
char path_head[500] = ".";
strcat(path_head, parse_string);
strcat(copy_head, "Content-Type: text/plain\r\n\r\n");
send_message(new_socket, path_head, copy_head);
printf("Else: %s \n", parse_string);
}
printf("\n------------------Server sent----------------------------------------------------\n");
}
else if (parse_string_method[0] == 'P' && parse_string_method[1] == 'O' && parse_string_method[2] == 'S' && parse_string_method[3] == 'T'){
char *find_string = malloc(200);
find_string = find_token(buffer, "\r\n", "action");
strcat(copy_head, "Content-Type: text/plain \r\n\r\n"); //\r\n\r\n
//strcat(copy_head, "Content-Length: 12 \n");
strcat(copy_head, "User Action: ");
printf("find string: %s \n", find_string);
strcat(copy_head, find_string);
write(new_socket, copy_head, strlen(copy_head));
}
close(new_socket);
free(copy);
free(copy_head);
}
else{
printf(">>>>>>>>>>Parent create child with pid: %d <<<<<<<<<", pid);
close(new_socket);
}
}
close(server_fd);
return 0;
}
char* parse(char line[], const char symbol[])
{
char *copy = (char *)malloc(strlen(line) + 1);
strcpy(copy, line);
char *message;
char * token = strtok(copy, symbol);
int current = 0;
while( token != NULL ) {
token = strtok(NULL, " ");
if(current == 0){
message = token;
if(message == NULL){
message = "";
}
return message;
}
current = current + 1;
}
free(token);
free(copy);
return message;
}
char* parse_method(char line[], const char symbol[])
{
char *copy = (char *)malloc(strlen(line) + 1);
strcpy(copy, line);
char *message;
char * token = strtok(copy, symbol);
int current = 0;
while( token != NULL ) {
//token = strtok(NULL, " ");
if(current == 0){
message = token;
if(message == NULL){
message = "";
}
return message;
}
current = current + 1;
}
free(copy);
free(token);
return message;
}
char* find_token(char line[], const char symbol[], const char match[])
{
char *copy = (char *)malloc(strlen(line) + 1);
strcpy(copy, line);
char *message;
char * token = strtok(copy, symbol);
while( token != NULL ) {
//printf("--Token: %s \n", token);
if(strlen(match) <= strlen(token))
{
int match_char = 0;
for(int i = 0; i < strlen(match); i++)
{
if(token[i] == match[i])
{
match_char++;
}
}
if(match_char == strlen(match)){
message = token;
return message;
}
}
token = strtok(NULL, symbol);
}
free(copy);
free(token);
message = "";
return message;
}
//https://stackoverflow.com/questions/45670369/c-web-server-image-not-showing-up-on-browser
//http://www.tldp.org/LDP/LG/issue91/tranter.html
//https://linux.die.net/man/2/fstat
//http://man7.org/linux/man-pages/man2/stat.2.html
//http://man7.org/linux/man-pages/man2/sendfile.2.html
int send_message(int fd, char image_path[], char head[]){
/*
char imageheader[] =
"HTTP/1.1 200 Ok\r\n"
"Content-Type: image/jpeg\r\n\r\n";
*/
struct stat stat_buf; /* hold information about input file */
write(fd, head, strlen(head));
int fdimg = open(image_path, O_RDONLY);
if(fdimg < 0){
printf("Cannot Open file path : %s with error %d\n", image_path, fdimg);
}
fstat(fdimg, &stat_buf);
int img_total_size = stat_buf.st_size;
int block_size = stat_buf.st_blksize;
//printf("image block size: %d\n", stat_buf.st_blksize);
//printf("image total byte st_size: %d\n", stat_buf.st_size);
if(fdimg >= 0){
ssize_t sent_size;
while(img_total_size > 0){
//if(img_total_size < block_size){
// sent_size = sendfile(fd, fdimg, NULL, img_total_size);
//}
//else{
// sent_size = sendfile(fd, fdimg, NULL, block_size);
//}
//img_total_size = img_total_size - sent_size;
//if(sent_size < 0){
// printf("send file error --> file: %d, send size: %d , error: %s\n", fdimg, sent_size, strerror(errno));
// img_total_size = -1;
int send_bytes = ((img_total_size < block_size) ? img_total_size : block_size);
int done_bytes = sendfile(fd, fdimg, NULL, block_size);
img_total_size = img_total_size - done_bytes;
//}
}
if(sent_size >= 0){
printf("send file: %s \n" , image_path);
}
close(fdimg);
}
}
/*
void setHttpHeader(char httpHeader[])
{
// File object to return
FILE *htmlData = fopen("index.html", "r");
char line[100];
char responseData[1000000];
if(htmlData){
while (fgets(line, 100, htmlData) != 0 ) {
strcat(responseData, line);
}
// char httpHeader[8000] = "HTTP/1.1 200 OK\r\n\n";
strcat(httpHeader, responseData);
fclose(htmlData);
}
else
{
printf("\n Read index.html file problem");
}
}
*/
/*
void setHttpHeader_other(char httpHeader[], char *path)
{
// File object to return
char path_head[500] = ".";
strcat(path_head, path);
printf("\n path head : %s", path_head);
//printf("\n Length of httpheader: %d", strlen(httpHeader));
FILE *htmlData1 = fopen(path_head, "r");
////char httpHeader1[8000] = "HTTP/1.1 200 OK\r\n\n";
int size_data = 800000;
char line[100];
char *responseData;
responseData = (char*)malloc(size_data * sizeof(char));
//https://stackoverflow.com/questions/5099669/invalid-conversion-from-void-to-char-when-using-malloc/5099675
if(htmlData1){
while (fgets(line, 100, htmlData1) != 0 ) {
strcat(responseData, line);
}
strcat(httpHeader, responseData);
fclose(htmlData1);
//free(responseData);
}
else
{
printf("\n Read other file problem");
}
//delete[] responseData;
}
*/
/*void report(struct sockaddr_in *serverAddress)
{
char hostBuffer[INET6_ADDRSTRLEN];
char serviceBuffer[NI_MAXSERV]; // defined in `<netdb.h>`
socklen_t addr_len = sizeof(*serverAddress);
int err = getnameinfo(
(struct sockaddr *) serverAddress,
addr_len,
hostBuffer,
sizeof(hostBuffer),
serviceBuffer,
sizeof(serviceBuffer),
NI_NUMERICHOST
);
if (err != 0) {
printf("It's not working!!\n");
}
printf("\n\n\tServer listening on http://%s:%s\n", hostBuffer, serviceBuffer);
}
*/
/*
std::string read_image(const std::string& image_path){
std::ifstream is(image_path.c_str(), std::ifstream::in);
is.seekg(0, is.end);
int flength = is.tellg();
is.seekg(0, is.beg);
char * buffer = new char[flength];
is.read(buffer, flength);
std::string image(buffer, flength);
return image;
}
int send_image(int & fd, std::string& image){
int body_length = image.size();
const char * body = image.data();
int response_length = body_length;
char * buffer = new char[response_length];
memcpy(buffer, body, body_length);
int ret = write(fd, buffer, response_length);
delete [] buffer;
return ret;
}
*/