-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
634 lines (576 loc) · 15.8 KB
/
server.c
File metadata and controls
634 lines (576 loc) · 15.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <signal.h>
#include <pthread.h>
/* Buffer sizes */
#define REQUEST_BUFFER_SIZE 2048
#define FILE_BUFFER_SIZE 1024
#define FILE_NAME_SIZE 64
/* Default server config */
#define LISTEN_BACKLOG 32
#define MAX_CONNECTIONS 64
#define DEFAULT_PORT 8000
#define DEFAULT_POOLSIZE 8
#define DEFAULT_STATIC "html"
/* Help string */
#define HELPSTRING "\nStatic HTTP server\n"\
"Usage: %s [-p port number] [-h html directory] [-t thread pool size]\n"\
"\n[Optional arguments]\n"\
"\t-p\tServer port (Default 8000)\n"\
"\t-t\tThread pool size. Number of threads for the server to use. (Default 8)\n"\
"\t-s\tHTML files in this directory are served. This directory must be in the same directory\n"\
"\t\tas the server binary and don't add './' to the directory name! (Default 'html')\n"\
"\t-h\tShows available arguments\n"
/* HTTP Response Headers */
#define HTTP_BASE_OK "HTTP/1.1 200 OK\r\n"\
"Server: Single File Server\r\n"\
"Connection: Closed\r\n"\
"Content-Type: "
#define HTTP_404 "HTTP/1.1 404 Not Found\r\n"\
"Server: Single File Server\r\n"\
"Content-Type: text/html; charset=utf-8\r\n"\
"Connection: Closed\r\n\r\nPAGE NOT FOUND"
#define HTTP_405 "HTTP/1.1 405 Method Not Allowed\r\n"\
"Server: Single File Server\r\n"\
"Content-Type: text/html; charset=utf-8\r\n"\
"Connection: Closed\r\n\r\nONLY GET IS ACCEPTED"
#define HTTP_BASE_OK_len strlen(HTTP_BASE_OK)
#define HTTP_404_len strlen(HTTP_404)
#define HTTP_405_len strlen(HTTP_405)
/* Supported filetypes */
enum filetype {
HTML,
CSS,
JPG,
PNG,
TXT,
SVG,
UNKNOWN
};
/* HTTP request status */
enum http_status {
OK,
INCOMPLETE,
NOTGET,
BADFILE,
NOTFOUND
};
struct qnode {
int clientfd;
struct qnode *next;
};
typedef struct qnode qnode;
struct queue {
int size;
int capacity;
qnode *head;
qnode *tail;
pthread_mutex_t lock;
pthread_cond_t cond_var;
};
typedef struct queue queue;
/* Global variables accessed by threads */
char *html_dir;
int html_dir_len = 0;
queue *connqueue;
unsigned int *bytes_read = NULL;
unsigned int *bytes_wrote = NULL;
/* Creates a queue of given capacity */
queue *create_queue(int capacity)
{
queue *q = malloc(sizeof(queue));
q->capacity = capacity;
q->size = 0;
q->head = NULL;
q->tail = NULL;
// Synchronization variables
if (pthread_mutex_init(&(q->lock), NULL) != 0) {
perror("Error Initialising mutex lock\n");
free(q);
return NULL;
}
if (pthread_cond_init(&(q->cond_var), NULL) != 0){
pthread_mutex_destroy(&(q->lock));
perror("Error Initialising conditional variable\n");
free(q);
return NULL;
}
return q;
}
/* Enqueues a new connection */
int enqueue(queue *q, int clientfd)
{
if(q->size == q->capacity) {
return -1;
}
qnode *node = malloc(sizeof(qnode));
node->clientfd = clientfd;
node->next = NULL;
if(q->size == 0) {
q->head = node;
q->tail = node;
} else {
q->tail->next = node;
q->tail = node;
}
q->size++;
return 1;
}
/* Dequeues a connection from the queue and returns pointer to the dequeued
* node. Frees the qnode and returns its contents. If q is empty -1 is returned.
*/
int dequeue(queue *q)
{
if(q->size == 0)
return -1;
int retval = q->head->clientfd;
if(q->size == 1) {
/* At this point, tail and head point to the same node, so don't free both. This was the cause of
* the occasional segfault */
free(q->head);
q->head = NULL;
q->tail = NULL;
} else {
qnode *oldhead = q->head;
q->head = oldhead->next;
free(oldhead);
}
q->size--;
return retval;
}
/* Deallocates the memory given to queue */
void freequeue(queue *q)
{
qnode *cursor = q->head, *temp;
while(cursor != NULL) {
temp = cursor;
cursor = cursor->next;
free(temp);
}
free(q);
/* Destroy lock and cond_var */
pthread_mutex_destroy(&(q->lock));
pthread_cond_destroy(&(q->cond_var));
return ;
}
/* Parses command line arguments and returns port, threads, html_dir via pointers
* (only if they were passed in the first place)
*/
int parse_args(int argc, char *argv[], int *port, int *threads, char **html_dir)
{
int option;
while ((option = getopt(argc, argv, "p:t:s:h")) != -1) {
switch(option) {
case 'p':
*port = atoi(optarg);
break;
case 't':
*threads = atoi(optarg);
break;
case 's':
*html_dir = optarg;
break;
case 'h':
printf(HELPSTRING, argv[0]);
return -1;
default:
printf("Invalid usage. Try -h for help\n");
return -1;
}
}
return 1;
}
/* Returns if ''html_dir' exists in the current directory */
int check_html_dir()
{
/* Make sure that root (/) or home (~) is not accessed. */
if(html_dir[0] == '/' || html_dir[0] == '~') {
printf("%s is invalid. Make sure that your static directory is present in the current directory\n", html_dir);
return 0;
}
struct stat s;
int res = stat(html_dir, &s);
if(res == -1) {
if(errno == ENOENT) {
printf("%s does not exist\n", html_dir);
} else {
perror("stat error\n");
}
} else {
if (S_ISDIR(s.st_mode)) {
return 1;
} else {
printf("%s is not a directory\n", html_dir);
}
}
return 0;
}
/* Creates a listen socket on supplied port */
int create_listen_socket(int port)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0) {
perror("Failed to create listen socket\n");
return -1;
}
/* Make socket reusable */
int enable = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
perror("setsockopt(SO_REUSEADDR) failed");
return -1;
}
struct sockaddr_in listen_addr;
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_port = htons(port);
listen_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(fd, (struct sockaddr*)&listen_addr, sizeof(listen_addr)) < 0) {
perror("Failed to bind listen socket\n");
return -1;
}
if(listen(fd, LISTEN_BACKLOG) < 0) {
perror("listen() error on socket\n");
return -1;
}
return fd;
}
/* Returns 1 is the user entered exit or quit */
int handle_stdin()
{
char input[8];
fgets(input, 7, stdin);
input[7] = '\0';
if(strcmp(input, "exit\n") == 0 || strcmp(input, "quit\n") == 0)
return 1;
printf("Enter 'exit' or 'quit' (without quotes) to stop the server\n");
return 0;
}
/* The file name extracted from HTTP GET request and returned via the filename pointer.
* 1 is returned on success and 0 otherwise
*/
int get_file_name(char *request, char *filename)
{
char method[8] = {'\0'}, version[8] = {'\0'};
sscanf(request, "%8s %s %8s\r\n", method, filename, version);
/* If method is GET, return NULL */
if (strcmp(method, "GET") != 0)
return 0;
return 1;
}
/* Returns filetype based on file extension */
enum filetype get_filetype(char *filename)
{
/* Find the last '.' in filename */
int i, len, found_dot;
len = strlen(filename);
for(i = len - 1; i > -1; i--) {
if (filename[i] == '.') {
found_dot = 1;
break;
}
}
if (!found_dot || i == 0 || i == len -1)
return UNKNOWN;
char *ext = filename + i + 1;
fflush(stdout);
if(strcmp(ext, "html") == 0)
return HTML;
else if(strcmp(ext, "css") == 0)
return CSS;
else if(strcmp(ext, "jpg") == 0 || strcmp(ext, "jpeg") == 0)
return JPG;
else if(strcmp(ext, "png") == 0)
return PNG;
else if(strcmp(ext, "svg") == 0)
return SVG;
else if (strcmp(ext, "txt") == 0)
return TXT;
else
return UNKNOWN;
}
/* Creates the HTTP response HEADER based on file extension */
char *get_response_header(char *filename)
{
enum filetype ft = get_filetype(filename);
char *response_header = calloc(1, HTTP_BASE_OK_len + 32);
switch(ft) {
case HTML:
sprintf(response_header, "%stext/html; charset=utf-8\r\n\r\n", HTTP_BASE_OK);
break;
case CSS:
sprintf(response_header, "%stext/css; charset=utf-8\r\n\r\n", HTTP_BASE_OK);
break;
case TXT:
sprintf(response_header, "%stext/plain; charset=utf-8\r\n\r\n", HTTP_BASE_OK);
break;
case JPG:
sprintf(response_header, "%simage/jpeg\r\n\r\n", HTTP_BASE_OK);
break;
case PNG:
sprintf(response_header, "%simage/png\r\n\r\n", HTTP_BASE_OK);
break;
case SVG:
sprintf(response_header, "%simage/svg+xml\r\n\r\n", HTTP_BASE_OK);
break;
default:
sprintf(response_header, "%sapplication/octet-stream\r\n\r\n", HTTP_BASE_OK);
break;
}
return response_header;
}
/* This function handles a http request and returns the enum indicating request status.
* If the request is valid, it returns the file descriptor of the html file via htmlfd pointer
* It also returns the response header via the response_header pointer.
*/
enum http_status handle_http_request(char *request, int *htmlfd, char **response_header)
{
char filename[FILE_NAME_SIZE] = {'\0'};
char fullpath[FILE_NAME_SIZE + html_dir_len + 4];
memset(fullpath, 0, FILE_NAME_SIZE + html_dir_len + 4);
int fd = -1;
/* Intial values for return pointers */
*htmlfd = -1;
*response_header = NULL;
/* Check that the entire request is read */
if (strstr(request, "\r\n\r\n") == NULL)
return INCOMPLETE;
/* Extract filename */
if(get_file_name(request, filename) == 0){
/* get_file_name() returns 0 if method is not GET */
return NOTGET;
}
if (filename[0] == '.' || filename[0] == '~' || strstr(filename, "..") != NULL) {
/* If the filename starts with any of the above chars, its a malicious request */
return BADFILE;
}
/* If the filename is "/" set it to "index.html" */
if(strcmp(filename, "/") == 0)
sprintf(filename, "/index.html");
sprintf(fullpath, "./%s%s", html_dir, filename);
if ((fd = open(fullpath, O_RDONLY)) < 0) {
return NOTFOUND;
} else {
*htmlfd = fd;
*response_header = get_response_header(filename);
return OK;
}
}
/* Thread function that handles a single client in a blocking fashion. This function takes no arguments.
* The connection queue pointer is global and mutex locks and condition variables are global.
*/
void* handle_connection(void *args)
{
/* thread receives its index as an argument */
int tindex = *((int*)args);
while(1) {
/* File descriptors */
int clientfd = -1; // holds the client socket descriptor
int htmlfd = -1; // holds the html file descriptor
/* indicates the status of the http request */
enum http_status req_status;
int br,bw; // stores bytes read and bytes wrote for each read() and write()
int total_wrote = 0; // bytes read and wrote for entire transfer per client
/* Char buffers */
char *response_header;
char filebuffer[FILE_BUFFER_SIZE] = {'\0'};
char req_buffer[REQUEST_BUFFER_SIZE] = {'\0'};
/* Dequeue a connection */
pthread_mutex_lock(&(connqueue->lock));
pthread_cond_wait(&(connqueue->cond_var), &(connqueue->lock));
clientfd = dequeue(connqueue);
pthread_mutex_unlock(&(connqueue->lock));
/* Verify that dequeue was successful */
if(clientfd == -1)
continue;
/* read request and check if it failed */
br = read(clientfd, req_buffer, REQUEST_BUFFER_SIZE-1);
if(br <= 0)
goto close_clientfd;
req_buffer[br] = '\0';
bytes_read[tindex] += br;
/* Handle http request */
req_status = handle_http_request(req_buffer, &htmlfd, &response_header);
switch(req_status){
case INCOMPLETE:
case BADFILE:
goto close_clientfd;
case NOTGET:
{
bw = write(clientfd, HTTP_405, HTTP_405_len);
if(bw > 0)
total_wrote += bw;
goto close_clientfd;
}
case NOTFOUND:
{
bw = write(clientfd, HTTP_404, HTTP_404_len);
if(bw > 0)
total_wrote += bw;
goto close_clientfd;
}
case OK:
{
bw = write(clientfd, response_header, strlen(response_header));
if(bw <= 0)
goto close_clientfd;
total_wrote += bw;
while((br = read(htmlfd, filebuffer, FILE_BUFFER_SIZE-1))) {
filebuffer[br] = '\0';
bw = write(clientfd, filebuffer, br);
if(bw <= 0)
goto close_clientfd;
total_wrote += bw;
memset(filebuffer, 0, bw);
}
close(htmlfd);
}
}
close_clientfd:
bytes_wrote[tindex] += total_wrote;
close(clientfd);
}
return NULL;
}
/* Creates 'poolsize' number of threads and returns a pointer to pthread_t array. If
* any error occurs, message is printed and NULL is returned
*/
pthread_t* create_threadpool(int poolsize, void *thread_func)
{
pthread_t *workers = calloc(poolsize, sizeof(pthread_t));
int i;
int *arg;
for(i = 0; i < poolsize; i++) {
arg = malloc(sizeof(int));
*arg = i;
if(pthread_create(&workers[i], NULL, thread_func, (void *)arg) < 0){
perror("Error in pthread_create()\n");
free(workers);
return NULL;
}
}
return workers;
}
int main(int argc, char *argv[])
{
int port = DEFAULT_PORT;
int threads = DEFAULT_POOLSIZE;
html_dir = DEFAULT_STATIC;
/* Parse arguments */
if(parse_args(argc, argv, &port, &threads, &html_dir) == -1)
return EXIT_SUCCESS;
/* check static directory */
if(!check_html_dir())
exit(EXIT_FAILURE);
html_dir_len = strlen(html_dir);
/* open listen socket */
int listenfd;
if((listenfd = create_listen_socket(port)) < 0)
exit(EXIT_FAILURE);
/* Epoll */
int epollfd = epoll_create1(0);
if(epollfd < 0) {
perror("error in epoll_create1()\n");
close(listenfd);
exit(EXIT_FAILURE);
}
struct epoll_event stdin_event, listen_event;
stdin_event.events = EPOLLIN;
stdin_event.data.fd = STDIN_FILENO;
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &stdin_event) < 0) {
perror("error in epoll_ctl() for stdin_event\n");
close(listenfd);
close(epollfd);
exit(EXIT_FAILURE);
}
listen_event.events = EPOLLIN;
listen_event.data.fd = listenfd;
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &listen_event) < 0) {
perror("error in epoll_ctl() for listen_event\n");
close(listenfd);
close(epollfd);
exit(EXIT_FAILURE);
}
/* Create connection queue */
if((connqueue = create_queue(MAX_CONNECTIONS)) == NULL){
close(listenfd);
exit(EXIT_FAILURE);
}
/* initiate thread pool */
pthread_t *workers;
if((workers = create_threadpool(threads, handle_connection)) == NULL) {
close(listenfd);
pthread_mutex_destroy(&(connqueue->lock));
pthread_cond_destroy(&(connqueue->cond_var));
exit(EXIT_FAILURE);
}
/* allocate counter arrays */
bytes_read = calloc(threads, sizeof(unsigned int));
bytes_wrote = calloc(threads, sizeof(unsigned int));
/* Print server configuration */
printf("Server running on port: %d\nthreads: %d\nstatic directory: %s\nType exit or quit to exit\n", port, threads, html_dir);
/* ignore broken pipe */
signal(SIGPIPE, SIG_IGN);
/* Start server loop */
int clientfd = -1;
struct sockaddr_in client_addr;
socklen_t len;
struct epoll_event events[2];
int i, event_count;
while(1) {
if((event_count = epoll_wait(epollfd, events, 2, -1)) < 0) {
perror("error in epoll_wait()\n");
break;
}
for(i = 0; i < event_count; i++) {
/* handle console input */
if (events[i].data.fd == STDIN_FILENO) {
if(handle_stdin()){
goto close_fds;
}
} else {
/* handle new connection */
memset(&client_addr, 0, sizeof(client_addr));
len = sizeof(client_addr);
clientfd = accept(listenfd, (struct sockaddr*)&client_addr, &len);
pthread_mutex_lock(&(connqueue->lock));
if(enqueue(connqueue, clientfd) < 0){
printf("Connection capacity reached. Dropped new connection!\n");
close(clientfd);
} else {
pthread_cond_signal(&(connqueue->cond_var));
}
pthread_mutex_unlock(&(connqueue->lock));
}
}
}
close_fds:
/* Close fds */
close(listenfd);
close(epollfd);
/* Clean up */
printf("stopping server\n");
for(i = 0; i < threads; i++) {
pthread_cancel(workers[i]);
}
freequeue(connqueue);
/* Prints stats */
unsigned int total_wrote = 0;
unsigned int total_read = 0;
for(i = 0; i < threads; i++) {
printf("thread %u\twrote: %u\tread: %u\n", i, bytes_wrote[i], bytes_read[i]);
total_wrote += bytes_wrote[i];
total_read += bytes_read[i];
}
printf("Total bytes received: %u Bytes\nTotal bytes sent: %u Bytes\n", total_read, total_wrote);
/* Exit */
return EXIT_SUCCESS;
}