-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtinyc.h
More file actions
210 lines (180 loc) · 6.01 KB
/
tinyc.h
File metadata and controls
210 lines (180 loc) · 6.01 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
#ifndef TINYC_H
#define TINYC_H
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
// Set multithread mode
#ifdef MULTITHREAD_ON
#include <pthread.h>
#endif
#ifdef __linux__
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <sys/stat.h>
#include <ctype.h>
typedef int32_t SocketType;
#define SIZE_T_FORMAT "%zu"
#define SEND_D_FLAG MSG_NOSIGNAL // avoid SIGPIPE signal
#else
#include <winsock2.h>
#include <windows.h>
#include <locale.h>
#include <wchar.h>
typedef SOCKET SocketType;
#define SIZE_T_FORMAT "%Illu"
#define SEND_D_FLAG 0
#endif
#define TRUE 1
#define FALSE 0
#define MAX_HEADER_SIZE 1024
#define BUFFER_SIZE 40000 // 40kb
#define MAX_PATH_LENGTH 400
#define MAX_THREADS 250
#define MAX_MIME_TYPES 30
#define DEFAULT_PORT 8081 // server default server
#define SERVER_BACKLOG 250 // server max listen connections
#define CLIENT_TIMEOUT 5
#define EXPLORER_MAX_FILES 2048 // max amount of files that explorer print
#define EXPLORER_MAX_FILENAME_LENGTH 500
#define HTML_EL_SIZE 1024
// log file
#define LOG_FILE_NAME "tinyc.log"
int8_t no_logs = FALSE;
FILE *log_file = NULL;
typedef struct {
const char *extension;
const char *mime_type;
} MimeType;
typedef struct {
SocketType socket;
char *default_route;
char *folder_to_serve;
int8_t show_explorer;
} connection_params;
typedef enum {
FS_NOT_FOUND = 0,
FS_FILE,
FS_DIR
} fs_type;
#ifdef MULTITHREAD_ON
void *handle_connection_thread(void *thread_args);
int active_threads = 0;
pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t thread_cond = PTHREAD_COND_INITIALIZER;
#endif
const char *get_filename_extension(const char* file_path);
const char *get_filename_mimetype(const char *path);
int get_file_size(FILE *file, uint64_t *size_out);
char **get_dir_content(const char* path, size_t *file_amount);
char *os_dir_to_html(const char *dir_path);
fs_type get_fs_type(const char *path);
void *safe_malloc(size_t size);
char *cstrdup(char *string);
int url_to_ospath(const char *url_path, char *os_path, size_t max_length);
void decode_url(char* url);
int extract_URI_from_header(char *header_content, char *output_buffer, size_t buffer_size) ;
void remove_slash_from_start(char* str);
void concat_str(char** str, const char* new_str);
int starts_with(const char *str, const char *word);
int is_directory_request(const char *url_path);
void init_log_file();
void write_log(const char* type, const char* msg, ...);
void close_log_file();
void set_shell_text_color(const char* color);
char* get_current_datetime();
char *get_arg_value(int argc, char **argv, char *target_arg);
// Response functions
void send_response(SocketType socket, const char *response_content);
void send_404_response(SocketType socket); // not found
void send_500_response(SocketType socket); // internal error
void send_302_response(SocketType socket, char *uri) ; // redirection
void send_content(SocketType socket, FILE *file, const char *content_type, size_t content_length);
void send_file_content(SocketType socket, FILE *file);
void send_partial_content(SocketType socket, FILE *file, const char *content_type, size_t file_size, size_t start, size_t end);
void close_socket(SocketType socket);
void socket_error_msg();
void handle_connection(connection_params *params);
// All supported mimetypes
MimeType mime_types[MAX_MIME_TYPES] = {
{ ".html", "text/html" },
{ ".htm", "text/html" },
{ ".srt", "application/x-subrip" },
{ ".vtt", "text/vtt" },
{ ".txt", "text/plain" },
{ ".css", "text/css" },
{ ".js", "application/javascript" },
{ ".json", "application/json" },
{ ".xml", "application/xml" },
{ ".gif", "image/gif" },
{ ".jpeg", "image/jpeg" },
{ ".mkv", "video/x-matroska"},
{ ".flac", "audio/flac"},
{ ".mp3", "audio/mp3"},
{ ".jpg", "image/jpeg" },
{ ".png", "image/png" },
{ ".svg", "image/svg+xml" },
{ ".mp4", "video/mp4" },
{ ".ico", "image/x-icon" },
{ ".pdf", "application/pdf" }
};
// File explorer view template
const char *FILE_EXPLORER_HEADER = "HTTP/1.1\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Connection: keep-alive\r\n"
"Keep-Alive: timeout=5\r\n\r\n"
"<!DOCTYPE html>"
"<html>"
"<head><title>TinyC</title><meta charset='UTF-8'></head>"
"<body>"
"<h1>Content into: %s</h1>"
"(%d elements found)"
"<hr>"
"<ul style='padding-left:3em'>";
const char *FILE_EXPLORER_LIST_ELEMENT = "<a href='%s'>%s</a><br>";
const char *FILE_EXPLORER_FOOTER = "</ul>"
"<hr><style>html{cursor:default;font-family:verdana;line-height:1.5;}</style>"
"</body>"
"</html>";
// HTTP common responses
const char *HTTP_404_NOT_FOUND =
"HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/html\r\n"
"Content-Length: %d\r\n\r\n<html>"
"<head><title> Oops! 404 Not Found</title></head>"
"<body><h1>404 Not Found! :(</h1>"
"<p>The requested resource was not found on this server.</p>"
"</body></html>";
const char *HTTP_500_INTERNAL_ERROR =
"HTTP/1.1 500 Internal Server Error\r\n"
"Content-Type: text/html\r\n"
"Content-Length: %d\r\n\r\n<html>"
"<head><title>500 Internal Error</title></head>"
"<body><h1>500</h1><p>Internal server error.</p>"
"</body></html>";
const char *HTTP_302_REDIRECTION =
"HTTP/1.1 302 Found\r\n"
"Location: %s\r\n"
"Connection: close\r\n"
"\r\n";
const char *HTTP_414_URL_TOO_LONG =
"HTTP/1.1 414 Found\r\n"
"Content-Type: text/html\r\n"
"Content-Length: %d\r\n\r\n<html>"
"<head><title>414 URL Too Long</title></head>"
"<body><h1>414</h1><p> URL too long!</p>"
"</body></html>";
const char *HTTP_200_OK =
"HTTP/1.1 200 Found\r\n"
"Content-Type: text/html\r\n"
"Content-Length: %d\r\n\r\n<html>"
"<head><title>OKi doki</title></head>"
"<body><h1>OK</h1>"
"</body></html>";
#endif