-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyc.c
668 lines (576 loc) · 23.1 KB
/
tinyc.c
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#include "tinyc.h"
int main(int argc, char *argv[]) {
char *input_arg = NULL;
char *folder_to_serve = NULL, *default_route = NULL;
char server_ip[] = "0.0.0.0";
char client_ip[8] = ":";
int16_t port = DEFAULT_PORT;
int16_t backlog = SERVER_BACKLOG;
int16_t max_threads = MAX_THREADS;
int8_t show_explorer = TRUE;
#ifndef __linux__
setlocale(LC_ALL, "");
#endif
// Socket vars declaration
SocketType server_socket, client_socket;
struct sockaddr_in address;
int32_t addrlen = sizeof(address);
#ifdef _WIN32
WSADATA wsaData;
#endif
/* ===================================== */
/* ======= Arg parse =================== */
/* ===================================== */
if(get_arg_value(argc, argv, "--help") != NULL){
printf(
"::: TinyC lightweight http server (by hwpoison) :::\n"
"\nBasic usage: %s --port 8081 --folder /my_web\n"
" example: %s --port 3543 --folder simple_web/index.html\n"
"\nOptions:\n"
"\t--folder <folder_path>: Folder to serve. By default serve all executable location dir content.\n"
"\t--ip: Set server IP. Default: ANY (Local/Network).\n"
"\t--port <port_number>: Port number. Default is %d\n"
"\t--backlog <number>: Max server listener.\n"
"\t--max-threads <number>: Max server threads.\n"
"\t--default-redirect <file_path>/: redirect / to default file route. ex: simple_web/index.html\n"
"\t--no-print : No print log (less mem consumption).\n"
"\t--no-file-explorer: Disable file explorer.\n"
,argv[0], argv[0], DEFAULT_PORT);
return 0;
}
// Get args
if((input_arg = get_arg_value(argc, argv, "--port")) != NULL)
port = atoi(input_arg);
if((input_arg = get_arg_value(argc, argv, "--backlog")) != NULL)
backlog = atoi(input_arg);
if((input_arg = get_arg_value(argc, argv, "--max-threads")) != NULL)
max_threads = atoi(input_arg);
if((input_arg = get_arg_value(argc, argv, "--ip")) != NULL)
strcpy(server_ip, input_arg);
if((get_arg_value(argc, argv, "--no-print")) != NULL)
print_output = 0;
if(get_arg_value(argc, argv, "--no-file-explorer") != NULL)
show_explorer = FALSE;
if((input_arg = get_arg_value(argc, argv, "--folder")) != NULL)
folder_to_serve = input_arg;
default_route = get_arg_value(argc, argv, "--default-redirect");
print_tinyc_welcome_logo();
set_shell_text_color("36"); // lightblue
print(NULL, "Max threads: %d", max_threads);
print(NULL, "Backlog: %d", backlog);
#ifdef MULTITHREAD_ON
pthread_t *active_threads = safe_malloc(sizeof(active_threads)*max_threads);
int16_t thread_count = 0;
print(NULL, "Multithreading enabled.");
#endif
/* ============================================================= */
/* ======= Server scket initialization and configuration ======= */
/* ============================================================= */
print(NULL, "Initializing server socket.");
// Winsock init
#ifdef _WIN32
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
perror("Error with winsock");
exit(EXIT_FAILURE);
}
#endif
// Create server socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Error to create server socket.");
exit(EXIT_FAILURE);
}
// Set up the socket
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(server_ip);
address.sin_port = htons(port);
#ifdef __linux__
struct timeval timeout = { .tv_sec = CLIENT_TIMEOUT, .tv_usec = 0};
#else
int timeout = 1000*CLIENT_TIMEOUT; // ms to sec for win
#endif
// Bind addr and port
if (bind(server_socket, (struct sockaddr*)&address, sizeof(address)) < 0) {
print(NULL, "[x] Error binding the socket to address and port %d.", port);
perror("Caused by ");
exit(EXIT_FAILURE);
}
// Start to listen incoming connections
if (listen(server_socket, backlog) < 0) {
perror("Error to listen connections.");
exit(EXIT_FAILURE);
}
print(NULL, "> Running server at: %s:%d", server_ip, port);
set_shell_text_color("0");
/* ===================================== */
/* ======= Accept connections loop ===== */
/* ===================================== */
for(;;) {
#ifdef MULTITHREAD_ON
// Check threads limits
if(thread_count>=max_threads){
print("error", "Server too busy...\n");
for(int i = 0; i < max_threads; i++){
pthread_join(active_threads[i], NULL);
}
thread_count = 0;
print("info", "All threads free up.\n");
}
#endif
// Accept client new connection
#ifdef __linux__
if ((client_socket = accept(server_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
#else
if ((client_socket = accept(server_socket, (struct sockaddr *)&address, &addrlen)) == INVALID_SOCKET) {
#endif
print("error", "Error accepting the connection");
continue;
}
// Get client ip address
#ifdef __linux__
inet_ntop(AF_INET, &(address.sin_addr), client_ip, INET_ADDRSTRLEN);
#else
strcpy(client_ip, inet_ntoa(address.sin_addr));
#endif
// Set timeout in send and receive data from client_socket
if (setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)) == -1 ||
setsockopt(client_socket, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof(timeout)) == -1) {
perror("Error to setup socket timeout.");
close(client_socket);
exit(EXIT_FAILURE);
}
// Create new thread for handle connection
print("info", "[%d] Incoming connection from %s",client_socket, client_ip);
connection_params *params = safe_malloc(sizeof(connection_params));
params->socket = client_socket;
params->default_route = default_route;
params->folder_to_serve = folder_to_serve;
params->show_explorer = show_explorer;
#ifdef MULTITHREAD_ON
pthread_create(&active_threads[thread_count], NULL, handle_connection_thread, (void*)params);
pthread_detach(active_threads[thread_count]);
thread_count++;
#else
handle_connection(params);
#endif
}
// Close server socket and release memory
close_socket(server_socket);
#ifdef _WIN32
WSACleanup();
#endif
return 0;
}
char *get_arg_value(int argc, char **argv, char *target_arg){
for(int arg_idx = 0; arg_idx < argc; arg_idx++){
if(!strcmp(argv[arg_idx], target_arg)) // <arg> <value>
return argv[arg_idx+1]==NULL?"":argv[arg_idx+1];
}
return NULL;
}
inline void print_tinyc_welcome_logo(){
set_shell_text_color("32");
print(NULL, "#### Welcome to tinyC! #### (%s)", __TIMESTAMP__);
set_shell_text_color("0");
}
void set_shell_text_color(const char* color) {
printf("\033[%sm", color);
}
void print(const char* type, const char* msg, ...) {
if (print_output == TRUE) {
va_list args;
char* full_date = get_current_datetime();
va_start(args, msg);
if (type != NULL) {
if(!strcmp(type, "error")){
set_shell_text_color("31");
printf("[ERROR][%s] ", full_date);
}
if(!strcmp(type, "info")){
set_shell_text_color("35"); // purple
printf("[INFO][%s] ", full_date);
}
set_shell_text_color("0");
} else {
printf("[%s] ", full_date);
}
vprintf(msg, args);
printf("\n");
va_end(args);
}
}
void send_206_http_response(SocketType socket, FILE *file, const char *content_type, size_t file_size, size_t start, size_t end) {
// Seek the file to the specified rangue before send
fseek(file, start, SEEK_SET);
// Check if the requested range is within the file size
if (start > file_size || end > file_size) {
print("error", "[!] Error: requested range is out of bounds.");
send_500_response(socket);
return;
}
// Send header with range and content length (for video html stream content)
char header[MAX_HEADER_SIZE];
snprintf(header, MAX_HEADER_SIZE, "HTTP/1.1 206 Partial Content\r\n"
"Connection: keep-alive\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Type: %s; charset=utf-8\r\n"
"Content-Range: bytes " SIZE_T_FORMAT "-" SIZE_T_FORMAT "/" SIZE_T_FORMAT "\r\n"
"Content-Length: " SIZE_T_FORMAT "\r\n\r\n", content_type, start, end, file_size, end - start + 1);
send_response(socket, header);
send_file_in_chuncks(socket, file);
print("info", "Response 206 done.");
}
void send_200_http_response(SocketType socket, FILE *file, const char *content_type, size_t content_length) {
char header[MAX_HEADER_SIZE];
snprintf(header, MAX_HEADER_SIZE, "HTTP/1.1 200 OK\r\n"
"Connection: keep-alive\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Type: %s; charset=utf-8\r\n"
"Content-Length: " SIZE_T_FORMAT "\r\n\r\n", content_type, content_length);
send_response(socket, header);
send_file_in_chuncks(socket, file);
print("info", "Response 200 Done.");
}
void send_302_response(SocketType socket, char *uri) {
char buffer[BUFFER_SIZE];
snprintf(buffer, MAX_HEADER_SIZE, HTTP_302_REDIRECTION, uri);
send_response(socket, buffer);
print("info", "302 redirection to %s", uri);
}
void send_404_response(SocketType socket) {
send_response(socket, HTTP_404_NOT_FOUND);
print("info", "404 not found.");
}
void send_500_response(SocketType socket) {
send_response(socket, HTTP_500_INTERNAL_ERROR);
print("error", "500 server side error.");
}
int starts_with(const char *str, const char *word) {
size_t word_len = strlen(word);
return strncmp(str, word, word_len) == 0?TRUE:FALSE;
}
#ifdef __linux__
size_t get_file_length(const char* filename){
FILE *file = fopen(filename, "rb");
if (file == NULL) {
print("error", "Error to open the file for get file size : %s", filename);
return 0;
}
fseek(file, 0, SEEK_END);
size_t fileLength = ftell(file);
fclose(file);
return fileLength;
}
#else
uint64_t get_file_length(const char* filename) {
HANDLE hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
print("error", "Error to open the file for get file size : %s", filename);
return 0;
}
LARGE_INTEGER fileSize;
if (!GetFileSizeEx(hFile, &fileSize)) {
CloseHandle(hFile);
print("error", "Error to get file size : %s", filename);
return 0;
}
CloseHandle(hFile);
return (uint64_t)fileSize.QuadPart;
}
#endif
void send_response(SocketType to_socket, const char *response_content) {
print(NULL, "Sending %d bytes.", strlen(response_content));
if(send(to_socket, response_content, strlen(response_content), 0) < 0){
print(NULL, "Error to sending.\n");
}
}
void send_file_in_chuncks(SocketType to_socket, FILE *file){
char buffer[BUFFER_SIZE] = {0};
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, file)) > 0) {
if(send(to_socket, buffer, bytesRead, SEND_D_FLAG ) < 0)
break;
}
}
void remove_slash_from_start(char* str) {
size_t length = strlen(str);
if (length > 0 && str[0] == '/') {
memmove(str, str + 1, length);
str[length - 1] = '\0';
}
}
const char *get_filename_extension(const char *path) {
const char *extension = strrchr(path, '.');
return extension!=NULL && extension!=path?extension:"";
}
const char *get_filename_mimetype(const char *path) {
const char *extension = get_filename_extension(path);
for (int i = 0; mime_types[i].extension != NULL; i++) {
if (strcmp(mime_types[i].extension, extension) == 0)
return mime_types[i].mime_type;
}
return "application/octet-stream"; // default mimetype
}
char* get_current_datetime() {
time_t now;
struct tm current_date;
static char currenttime[30];
time(&now);
#ifdef __linux__
localtime_r(&now, ¤t_date);
#else
localtime_s(¤t_date, &now);
#endif
strftime(currenttime, sizeof(currenttime), "%Y-%m-%d %H:%M:%S", ¤t_date);
return currenttime;
}
void close_socket(SocketType socket) {
#ifdef __linux__
close(socket);
#else
closesocket(socket);
#endif
print("info", "[%d] Socket closed.", socket);
}
char *extract_URI_from_header(char *header_content){
char *extracted_uri;
char *start, *end;
int length;
if((start = strchr(header_content, ' ')) != NULL &&
(end = strchr(start+1, ' '))!=NULL){
length = (end-header_content)-(start-header_content)-1;
extracted_uri = (char*)malloc(MAX_PATH_LENGTH);
strncpy(extracted_uri, start+1, length);
extracted_uri[length] = '\0';
return extracted_uri;
}
return "/";
}
void decode_url(char* url) {
char *url_p = url;
int decoded_char;
while (*url_p) {
if (*url_p == '%' && isxdigit(*(url_p + 1)) && isxdigit(*(url_p + 2))) {
char hex[3] = {url_p[1], url_p[2], '\0'};
sscanf(hex, "%x", &decoded_char);
memmove(url_p + 1, url_p + 3, strlen(url_p + 2) + 1);
*url_p = decoded_char;
}
url_p++;
}
}
void *safe_malloc(size_t size) {
void* ptr = malloc(size);
if (ptr == NULL) {
print("error", "Error allocating memory.");
exit(-1);
}
return ptr;
}
char **get_dir_content(const char* path, size_t *file_amount) {
char **dir_content = (char**)safe_malloc(EXPLORER_MAX_FILES * sizeof(char*) + 1);
char entry_name[EXPLORER_MAX_FILENAME_LENGTH];
int file_n = 0;
#ifdef __linux__
DIR *dir;
struct dirent *entry;
dir = opendir(path);
struct stat file_stat;
if(dir == NULL){
print("error", "linux: not possible get directory content %s", path);
return NULL;
}
while ((entry= readdir(dir)) != NULL){
if(strlen(entry->d_name) > EXPLORER_MAX_FILENAME_LENGTH){
print("error", "File name too long.");
continue;
}
if(file_n >= EXPLORER_MAX_FILES){
print("info", "Maximum of showed files exceded.");
break;
}
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
snprintf(entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name);
if (stat(entry_name, &file_stat) == 0) {
if (S_ISDIR(file_stat.st_mode))
sprintf(entry_name, "%s/", entry->d_name);
else
sprintf(entry_name, "%s", entry->d_name);
}
dir_content[file_n] = (char*)safe_malloc(strlen(entry_name) + 1);
strcpy(dir_content[file_n], entry_name);
file_n++;
}
closedir(dir);
#else
WIN32_FIND_DATA find_data;
HANDLE h_find;
h_find = FindFirstFile(path, &find_data);
if (h_find == INVALID_HANDLE_VALUE){
print("error", "windows: not possible get directory content %s", path);
return NULL;
}
while(FindNextFile(h_find, &find_data)){
if(file_n >= EXPLORER_MAX_FILES){
print("info", "Maximum of files exceded.");
break;
}
if(strlen(find_data.cFileName) > EXPLORER_MAX_FILENAME_LENGTH){
print("error", "File name too long.");
continue;
}
if (!strcmp(find_data.cFileName, ".") || !strcmp(find_data.cFileName, ".."))
continue;
if(find_data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
sprintf(entry_name, "%s/", find_data.cFileName);
else
sprintf(entry_name, "%s", find_data.cFileName);
dir_content[file_n] = (char*)safe_malloc(strlen(entry_name) + 1);
strcpy(dir_content[file_n], entry_name);
file_n++;
}
FindClose(h_find);
#endif
*file_amount = file_n;
dir_content[file_n+1] = NULL;
return dir_content;
}
void concatenate_string(char** str, const char* new_str) {
if (*str == NULL) {
*str = (char*)safe_malloc(strlen(new_str) + 1);
strcpy(*str, new_str);
} else {
size_t currentLength = strlen(*str);
size_t newLength = strlen(new_str);
char* temp = (char*)realloc(*str, currentLength + newLength + 1);
if (temp == NULL) {
print("error", "Error reallocating memory");
exit(EXIT_FAILURE);
}
*str = temp;
strcat(*str, new_str);
}
}
void handle_connection(connection_params *conn){
char *file_path = NULL;
char buffer[BUFFER_SIZE] = {0};
int8_t in_folder = FALSE; // Only serve files into specific folder
size_t read_bytes, file_size, start_offset, end_offset;
/* ====================================== */
/* =Read-Send loop between client-server= */
/* ====================================== */
for(;;){
print(NULL, "[%d] Reading from client...", conn->socket);
#ifdef __linux__
read_bytes = read(conn->socket, buffer, BUFFER_SIZE);
#else
read_bytes = recv(conn->socket, buffer, BUFFER_SIZE, 0);
#endif
if (read_bytes == 0) {
print(NULL, "[%d] Connection closed by client.", conn->socket);
break;
} else if (read_bytes == -1) {
print("error", "[%d] Error reading content from client socket.", conn->socket);
break;
}
// Extract URI from client recv header
file_path = extract_URI_from_header(buffer);
decode_url(file_path);
print(NULL, "Handling route: %s", file_path);
// Check if uri path == '/' and redirect to default route
if(strcmp(file_path, "/") == 0 && conn->default_route != NULL){
print("info", "Redirecting to %s", conn->default_route);
send_302_response(conn->socket, conn->default_route);
}
remove_slash_from_start(file_path);
// Check if the path match with default folder
if(conn->folder_to_serve != NULL){
remove_slash_from_start(conn->folder_to_serve);
in_folder = starts_with(file_path, conn->folder_to_serve);
}
/* ===================================== */
/* ======= File explorer ===== */
/* ===================================== */
if(conn->show_explorer == TRUE &&
(file_path[strlen(file_path)-1] == '/' || strcmp(file_path, "") == 0)) {
// If folder to serve is specified and path did not match, send a 404
if(conn->folder_to_serve!=NULL && !in_folder){
send_404_response(conn->socket);
break;
}
#ifdef __linux__
sprintf(buffer, "./%s", file_path);
#else
sprintf(buffer, "./%s/*", file_path);
#endif
size_t file_amount;
print(NULL, "[%d] Explorer opened for '%s'", conn->socket, buffer);
char **dir_content = get_dir_content(buffer, &file_amount);
char *file_html_list = NULL;
sprintf(buffer, FILE_EXPLORER_HEADER, file_path, file_amount);
concatenate_string(&file_html_list, buffer);
sprintf(buffer, "<a href='%s'>%s</a><br>", "..", "..");
concatenate_string(&file_html_list, buffer);
// List all files
char list_element[EXPLORER_MAX_FILENAME_LENGTH];
for(int x=0; x < file_amount; x++){
sprintf(list_element, FILE_EXPLORER_LIST_ELEMENT, dir_content[x], dir_content[x]);
list_element[strlen(list_element)] = '\0';
concatenate_string(&file_html_list, list_element);
}
concatenate_string(&file_html_list, FILE_EXPLORER_FOOTER);
send_response(conn->socket, file_html_list);
free(file_html_list);
for (int i = 0; i < file_amount; i++)
free(dir_content[i]);
free(dir_content);
break;
}
// Open the file
print(NULL, "Finding for %s file..", file_path);
FILE *file = fopen(file_path, "rb");
// If file is not found send a 404
if (file == NULL) {
print("error", "The file '%s' could not be opened/found.", file_path);
send_404_response(conn->socket);
break;
// Serve the file
} else {
file_size = get_file_length(file_path);
start_offset = 0, end_offset = file_size -1;
print(NULL, "File size: "SIZE_T_FORMAT, file_size);
// Check if the request is has a "Range" header and extract range to stream
char* range_header = strstr(buffer, "Range: bytes=");
if (range_header != NULL) {
sscanf(range_header, "Range: bytes="SIZE_T_FORMAT"-"SIZE_T_FORMAT"", &start_offset, &end_offset);
print(NULL, "Range detected: from "SIZE_T_FORMAT" to " SIZE_T_FORMAT, start_offset, end_offset);
send_206_http_response(
conn->socket,
file,
get_filename_mimetype(file_path),
file_size,
start_offset,
end_offset);
}else{
send_200_http_response(
conn->socket,
file,
get_filename_mimetype(file_path),
file_size);
}
fclose(file);
}
}
free(file_path);
close_socket(conn->socket);
}
#ifdef MULTITHREAD_ON
void *handle_connection_thread(void *thread_args) {
connection_params *connection = (connection_params*)thread_args;
handle_connection(connection);
free(connection);
pthread_exit(NULL);
return NULL;
}
#endif