Repository navigation
Expand file tree
/
Copy pathserver.c
More file actions
executable file
·409 lines (357 loc) · 11.8 KB
/
Copy pathserver.c
File metadata and controls
executable file
·409 lines (357 loc) · 11.8 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
#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <pthread.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define DEFAULT_PORT 8080
#define BUFFER_SIZE 104857600
const char DEFAULT_WEB_ROOT[] = "./web_root";
typedef struct {
int port;
char root_dir[1024];
} ServerOptions;
char *concat_string(const char* str1, const char* str2) {
size_t length = strlen(str1) + strlen(str2) + 1; // +1 for the null terminator
char *buffer = (char *)malloc(length);
strcpy(buffer, str1);
strcat(buffer, str2);
return buffer;
}
bool file_exists(const char *filename) {
FILE *file = fopen(filename, "r");
if (file != NULL) {
fclose(file);
return true;
}
return false;
}
int is_directory(const char *path) {
struct stat statbuf;
if (stat(path, &statbuf) != 0)
return 0;
return S_ISDIR(statbuf.st_mode);
}
const char *get_file_extension(const char *file_name) {
const char *dot = strrchr(file_name, '.');
if (!dot || dot == file_name) {
char *suffixed_file_name = concat_string(file_name, ".json");
if (file_exists(suffixed_file_name)) {
free(suffixed_file_name);
return "json";
}
free(suffixed_file_name);
return "";
}
return dot + 1;
}
const char *get_mime_type(const char *file_ext) {
if (strcasecmp(file_ext, "html") == 0 || strcasecmp(file_ext, "htm") == 0) {
return "text/html";
} else if (strcasecmp(file_ext, "txt") == 0) {
return "text/plain";
} else if (strcasecmp(file_ext, "jpg") == 0 || strcasecmp(file_ext, "jpeg") == 0) {
return "image/jpeg";
} else if (strcasecmp(file_ext, "png") == 0) {
return "image/png";
} else if (strcasecmp(file_ext, "json") == 0) {
return "application/json";
} else {
return "application/octet-stream";
}
}
bool case_insensitive_compare(const char *s1, const char *s2) {
while (*s1 && *s2) {
if (tolower((unsigned char)*s1) != tolower((unsigned char)*s2)) {
return false;
}
s1++;
s2++;
}
return *s1 == *s2;
}
char *get_file_case_insensitive(const char *file_name) {
DIR *dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return NULL;
}
struct dirent *entry;
char *found_file_name = NULL;
while ((entry = readdir(dir)) != NULL) {
if (case_insensitive_compare(entry->d_name, file_name)) {
found_file_name = entry->d_name;
break;
}
}
closedir(dir);
return found_file_name;
}
char *url_decode(const char *src) {
size_t src_len = strlen(src);
char *decoded = malloc(src_len + 1);
size_t decoded_len = 0;
// decode %2x to hex
for (size_t i = 0; i < src_len; i++) {
if (src[i] == '%' && i + 2 < src_len) {
int hex_val;
sscanf(src + i + 1, "%2x", &hex_val);
decoded[decoded_len++] = hex_val;
i += 2;
} else {
decoded[decoded_len++] = src[i];
}
}
// add null terminator
decoded[decoded_len] = '\0';
return decoded;
}
char *read_file(const char *filename, size_t *fileSize) {
FILE *file = fopen(filename, "rb"); // Open in binary mode to handle all file types correctly
if (file == NULL) {
perror("Error opening file"); // Print an error message to the console
return NULL; // Indicate an error
}
// Determine the file size
fseek(file, 0, SEEK_END); // Move the file pointer to the end of the file
long file_size_long = ftell(file); // Get the current position (which is the file size)
rewind(file); // Reset the file pointer to the beginning of the file
// Check for potential errors with file size (e.g., if it's too large)
if (file_size_long < 0) {
perror("Error getting file size");
fclose(file);
return NULL;
}
// Ensure file size doesn't exceed the maximum size of size_t
if (file_size_long > SIZE_MAX) {
fprintf(stderr, "File size is too large to handle.\n");
fclose(file);
return NULL;
}
*fileSize = (size_t)file_size_long; // Cast to size_t (safer)
// Allocate memory for the character array
char *buffer = (char *)malloc(*fileSize + 1); // +1 for the null terminator
if (buffer == NULL) {
perror("Error allocating memory");
fclose(file);
return NULL;
}
// Read the file into the buffer
size_t bytesRead = fread(buffer, 1, *fileSize, file);
if (bytesRead != *fileSize) {
if (ferror(file)) {
perror("Error reading file");
free(buffer);
fclose(file);
return NULL;
}
}
// 4. Null-terminate the buffer (important for strings!)
buffer[*fileSize] = '\0';
// 5. Close the file
fclose(file);
return buffer; // Return the pointer to the character array
}
void build_http_response(const char *file_name,
const char *file_ext,
char *response,
size_t *response_len) {
// build HTTP header
const char *mime_type = get_mime_type(file_ext);
int file_fd = open(file_name, O_RDONLY);
if (file_fd == -1) {
// If file doesn't exist, check if file with ".json" suffix exists
char *suffixed_file_name = concat_string(file_name, ".json");
file_fd = open(suffixed_file_name, O_RDONLY);
free(suffixed_file_name);
}
if (file_fd == -1) {
// If file still doesn't exist, return default_response
size_t default_response_file_size;
char *default_response = read_file("default_response", &default_response_file_size);
memcpy(response, default_response, default_response_file_size);
*response_len = default_response_file_size;
free(default_response);
return;
}
// get file size for Content-Length
struct stat file_stat;
fstat(file_fd, &file_stat);
off_t file_size = file_stat.st_size;
char *header = (char *)malloc(BUFFER_SIZE * sizeof(char));
snprintf(header, BUFFER_SIZE,
"HTTP/1.1 200 OK\r\n"
"Content-Type: %s\r\n"
"\r\n",
mime_type);
// copy header to response buffer
*response_len = 0;
memcpy(response, header, strlen(header));
*response_len += strlen(header);
free(header);
// copy file to response buffer
ssize_t bytes_read;
while ((bytes_read = read(file_fd,
response + *response_len,
BUFFER_SIZE - *response_len)) > 0) {
*response_len += bytes_read;
}
close(file_fd);
}
void *handle_client(void *arg) {
int client_fd = *((int *)arg);
char *buffer = (char *)malloc(BUFFER_SIZE * sizeof(char));
// receive request data from client and store into buffer
ssize_t bytes_received = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (bytes_received > 0) {
// parse the request
regex_t regex;
regcomp(®ex, "^([A-Z]+) /([^ ?]*)\\?{0,1}(.*) HTTP/1", REG_EXTENDED);
regmatch_t matches[3];
if (regexec(®ex, buffer, 3, matches, 0) == 0) {
// extract filename from request and decode URL
buffer[matches[2].rm_eo] = '\0';
const char *url_encoded_file_name = buffer + matches[2].rm_so;
char *file_name = url_decode(url_encoded_file_name);
// get file extension
char file_ext[32];
strcpy(file_ext, get_file_extension(file_name));
// build HTTP response
char *response = (char *)malloc(BUFFER_SIZE * 2 * sizeof(char));
size_t response_len;
build_http_response(file_name, file_ext, response, &response_len);
// send HTTP response to client
send(client_fd, response, response_len, 0);
free(response);
free(file_name);
}
regfree(®ex);
}
close(client_fd);
free(arg);
free(buffer);
return NULL;
}
int create_server(int port) {
int server_fd;
struct sockaddr_in server_addr;
printf("Starting server at port %i\n", port);
// create server socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// config socket
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);
// bind socket to port
if (bind(server_fd,
(struct sockaddr *)&server_addr,
sizeof(server_addr)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// listen for connections
if (listen(server_fd, 10) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
return server_fd;
}
void server_listen(int server_fd) {
while (1) {
// client info
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int *client_fd = malloc(sizeof(int));
// accept client connection
if ((*client_fd = accept(server_fd,
(struct sockaddr *)&client_addr,
&client_addr_len)) < 0) {
perror("accept failed");
continue;
}
// create a new thread to handle client request
pthread_t thread_id;
pthread_create(&thread_id, NULL, handle_client, (void *)client_fd);
pthread_detach(thread_id);
}
}
int parse_arguments(int argc, char*argv[], ServerOptions *options) {
int opt;
while((opt = getopt(argc, argv, ":p:d:h")) != -1)
{
switch(opt)
{
case 'p':
int port_int = atoi(optarg);
if (port_int > 0) {
printf("Using port: %s\n", optarg);
options->port = atoi(optarg);
} else {
printf("Invalid port: %s\n", optarg);
return 1;
}
break;
case 'd':
if (is_directory(optarg)) {
printf("Using web root directory: %s\n", optarg);
strcpy(options->root_dir, optarg);
} else {
printf("Invalid directory: %s\n", optarg);
return 1;
}
break;
case 'h':
printf("Usage:\n");
printf("-p <PORT> Web server's port number\n");
printf("-d <DIRECTORY> Root directory of web content\n");
return 1;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
// optind is for the extra arguments
// which are not parsed
for(; optind < argc; optind++){
printf("extra arguments: %s\n", argv[optind]);
}
if (options->port == 0) {
options->port = DEFAULT_PORT;
printf("Using default port: %d\n", DEFAULT_PORT);
}
if (strlen(options->root_dir) == 0) {
strcpy(options->root_dir, DEFAULT_WEB_ROOT);
printf("Using default web root directory: %s\n", DEFAULT_WEB_ROOT);
}
return 0;
}
int main(int argc, char *argv[]) {
ServerOptions options = { 0, ""};
if(parse_arguments(argc, argv, &options) != 0 ) {
return EXIT_FAILURE;
}
if (strlen(options.root_dir) != 0) {
chdir(options.root_dir);
}
int server_fd = create_server(options.port);
server_listen(server_fd);
close(server_fd);
return 0;
}