-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinn.c
More file actions
678 lines (562 loc) · 16.9 KB
/
Copy pathtinn.c
File metadata and controls
678 lines (562 loc) · 16.9 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
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
669
670
671
672
673
674
675
676
677
678
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdarg.h> // for varidic functions
#include <unistd.h> // for system calls, like read and close etc
#include <fts.h> // for file system access stuff
#include <netdb.h> // for getaddrinfo
#include <arpa/inet.h> // for inet stuff
#include <poll.h>
#define IMF_DATE_LEN 30 // length of a date in Internet Messaging Format with null terminator
// ================ date/time stuff ================
// formated printing with the time
void tprintf(const char* format, ...) {
// output the time
time_t seconds = time(NULL);
struct tm* gmt = gmtime(&seconds);
printf("%02d:%02d:%02d ", gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
// output the rest
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
// generate a date stamp in Internet Messaging Format
char* imf_date(char* buf, size_t max_len) {
time_t seconds = time(NULL);
struct tm* gmt = gmtime(&seconds);
strftime(buf, max_len, "%a, %d %b %Y %H:%M:%S GMT", gmt);
return buf;
}
// ================ Static files ================
struct static_file {
char* local_path;
char* server_path;
struct static_file* next;
};
struct static_file* files; // a global?????
struct static_file* find_static_files(char* path) {
// a simple linked list of files
struct static_file temp_head;
temp_head.next = NULL;
struct static_file* tail = &temp_head;
// To work out the server path, we are going to remove the traversal to
// the root directory, but we want to keep the forward slash so all
// server paths start with one.
int pathlen = strlen(path);
if (pathlen>0) {
if (path[pathlen-1] == '/') {
pathlen--;
}
}
// read the file system
FTS* file_system = NULL;
FTSENT* node = NULL;
file_system = fts_open((char* const[]){path, NULL}, FTS_LOGICAL, NULL);
if (file_system == NULL) {
perror("read_static_files");
return NULL;
}
while ((node = fts_read(file_system)) != NULL) {
// ignore dot (hidden) files unless it's the first directory and it's . or ..
if(node->fts_name[0]=='.' && strcmp(node->fts_path, ".")!=0 && strcmp(node->fts_path, "..")!=0) {
fts_set(file_system, node, FTS_SKIP);
} else if (node->fts_info == FTS_F) {
if (node->fts_pathlen > pathlen) {
// a valid file, add it to the list
struct static_file* new_file = malloc(sizeof(*new_file));
new_file->next = NULL;
tail->next = new_file;
tail = new_file;
// file paths
new_file->local_path = malloc(node->fts_pathlen+1);
strcpy(new_file->local_path, node->fts_path);
new_file->server_path = new_file->local_path+pathlen;
}
}
}
fts_close(file_system);
// return the first file in the list
return temp_head.next;
}
void free_static_files(struct static_file* file) {
while (file != NULL) {
struct static_file* next = file->next;
free(file->local_path);
free(file);
file = next;
}
}
char* get_file_path(struct static_file* file, char* server_path) {
while (file != NULL) {
if (strcmp(file->server_path, server_path)==0) {
return file->local_path;
}
file = file->next;
}
return NULL;
}
// ================ basic network stuff ================
int get_server_socket(char* port) {
int status;
struct addrinfo hints;
struct addrinfo* addresses;
struct addrinfo* address;
int sock;
// get local address info
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((status = getaddrinfo(NULL, port, &hints, &addresses)) != 0) {
fprintf(stderr, "gettaddrinfo error: %s\n", gai_strerror(status));
return -1;
}
// get a socket and bind to it
for (address = addresses; address != NULL; address = address->ai_next) {
if ((sock = socket(address->ai_family, address->ai_socktype, address->ai_protocol)) < 0) {
continue;
}
// re-use socket if in use
int yes=1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(sock, address->ai_addr, address->ai_addrlen) != 0) {
close(sock);
continue;
}
break;
}
// if address is null nothing bound
if (address == NULL) {
freeaddrinfo(addresses);
fprintf(stderr, "bind error.\n");
return -1;
}
freeaddrinfo(addresses);
// listen to socket
if (listen(sock, 10) != 0) {
perror("listen");
return -1;
}
return sock;
}
// ================ socket list ================
struct sockets_list;
typedef void (*socket_listener)(struct sockets_list* sockets, int index);
struct sockets_list {
size_t size;
size_t count;
struct pollfd* pollfds;
socket_listener* listeners;
void** states;
};
struct sockets_list* sockets_list_new() {
struct sockets_list* list;
if ((list = malloc(sizeof(*list))) == NULL) {
fprintf(stderr, "PANIC: Unable to allocate memory for sockets list");
exit(EXIT_FAILURE);
}
list->size = 5;
list->count = 0;
list->pollfds = malloc(sizeof(*list->pollfds) * list->size);
list->listeners = malloc(sizeof(*list->listeners) * list->size);
list->states = malloc(sizeof(*list->states) * list->size);
if (list->pollfds == NULL || list->listeners == NULL || list->states == NULL) {
fprintf(stderr, "PANIC: Unable to allocate memory for sockets list");
exit(EXIT_FAILURE);
}
return list;
}
int sockets_list_add(struct sockets_list* list, int new_socket, socket_listener new_listener) {
// do we need to expand the arrays
if (list->count == list->size) {
list->size *= 2;
list->pollfds = realloc(list->pollfds, sizeof(*list->pollfds) * list->size);
list->listeners = realloc(list->listeners, sizeof(*list->listeners) * list->size);
list->states = realloc(list->states, sizeof(*list->states) * list->size);
if (list->pollfds == NULL || list->listeners == NULL || list->states == NULL) {
fprintf(stderr, "PANIC: Unable to allocate memory for sockets list");
exit(EXIT_FAILURE);
}
}
// add new socket
list->pollfds[list->count].fd = new_socket;
list->pollfds[list->count].events = POLLIN;
list->pollfds[list->count].revents = 0;
list->listeners[list->count] = new_listener;
list->states[list->count] = NULL;
// update count
list->count++;
return list->count - 1;
}
void sockets_list_rm(struct sockets_list* list, size_t index) {
if (index < list->count) {
list->pollfds[index] = list->pollfds[list->count-1];
list->listeners[index] = list->listeners[list->count-1];
list->states[index] = list->states[list->count-1];
list->count--;
}
}
// ================ buffer ================
struct buffer {
size_t size;
size_t length;
char* data;
};
struct buffer* buf_new(size_t size) {
struct buffer* buf;
if ((buf = malloc(sizeof(*buf))) == NULL) {
fprintf(stderr, "Unable to allocate memory for buffer");
return NULL;
}
buf->size = size;
buf->length = 0;
if ((buf->data = malloc(buf->size)) == NULL) {
fprintf(stderr, "Unable to allocate memory for buffer");
free(buf);
return NULL;
}
return buf;
}
void buf_free(struct buffer* buf) {
free(buf->data);
free(buf);
}
int buf_extend(struct buffer* buf, size_t new_size) {
if (new_size > buf->size) {
char* new_data = realloc(buf->data, new_size);
if (new_data == NULL) {
return -1;
}
buf->size = new_size;
buf->data = new_data;
}
return 0;
}
int buf_ensure(struct buffer* buf, size_t n) {
if (buf->length + n > buf->size) {
int new_size = buf->size * 2;
while (new_size < buf->length + n) {
new_size *= 2;
}
if (buf_extend(buf, new_size) < 0) {
return -1;
}
}
return 0;
}
int buf_append(struct buffer* buf, char* data, size_t n) {
if (buf_ensure(buf, n) < 0) {
return -1;
}
memcpy(buf->data + buf->length, data, n);
buf->length += n;
return buf->length;
}
int buf_append_str(struct buffer* buf, char* str) {
return buf_append(buf, str, strlen(str));
}
int buf_append_format(struct buffer* buf, size_t max, char* format, ...) {
if (buf_ensure(buf, max+1) < 0) {
return -1;
}
va_list args;
va_start(args, format);
int added = vsnprintf(buf->data+buf->length, max+1, format, args);
va_end(args);
if (added < 0) {
return -1;
}
buf->length += added;
return buf->length;
}
char* buf_reserve(struct buffer* buf, size_t n) {
if (buf_ensure(buf, n) < 0) {
return NULL;
}
char* rv = buf->data + buf->length;
buf->length += n;
return rv;
}
void buf_seek(struct buffer* buf, int offset) {
if (buf->length + offset < 0) {
buf->length = 0;
} else if (buf->length + offset > buf->size) {
buf->length = buf->size;
} else {
buf-> length += offset;
}
}
void buf_reset(struct buffer* buf) {
buf->length = 0;
}
char* buf_as_str(struct buffer* buf) {
if (buf->length == buf->size) {
if (buf_extend(buf, buf->size + 1) < 0) {
return NULL;
}
}
buf->data[buf->length] = '\0';
return buf->data;
}
// ================ client code ================
struct client_state {
char address[INET6_ADDRSTRLEN];
struct buffer* in;
};
struct client_state* client_state_new() {
struct client_state* state;
if ((state = malloc(sizeof(*state))) == NULL) {
fprintf(stderr, "PANIC: Unable to allocate memory for client state");
exit(EXIT_FAILURE);
}
if ((state->in = buf_new(256)) == NULL) {
fprintf(stderr, "PANIC: Unable to allocate memory for client state");
exit(EXIT_FAILURE);
}
return state;
}
void client_state_free(struct client_state* state) {
buf_free(state->in);
free(state);
}
void send_all(int sock, char *buf, size_t len) {
size_t total = 0;
int sent;
while (total < len) {
sent = send(sock, buf+total, len-total, 0);
if (sent == -1) {
fprintf(stderr, "send error: %s", strerror(errno));
return;
}
total += sent;
}
}
void send_header(int sock, struct client_state* state, char *status_code, char *status_text, char* ext, size_t length) {
struct buffer* header = buf_new(128);
// status line
buf_append_format(header, 12 + strlen(status_code) + strlen(status_text), "HTTP/1.1 %s %s\r\n", status_code, status_text);
// date
buf_append_str(header, "Date: ");
imf_date(buf_reserve(header, IMF_DATE_LEN), IMF_DATE_LEN);
buf_seek(header, -1);
buf_append_str(header, "\r\n");
// server
buf_append_str(header, "Server: Stub\r\n");
// content type
buf_append_str(header, "Content-Type: ");
if (ext == NULL) {
buf_append_str(header, "text/plain; charset=utf-8");
} else {
if (strcmp(ext, "html")==0 || strcmp(ext, "htm")==0) {
buf_append_str(header, "text/html; charset=utf-8");
} else if (strcmp(ext, "css")==0) {
buf_append_str(header, "text/css; charset=utf-8");
} else if (strcmp(ext, "js")==0) {
buf_append_str(header, "text/javascript; charset=utf-8");
} else if (strcmp(ext, "jpeg")==0 || strcmp(ext, "jpg")==0) {
buf_append_str(header, "image/jpeg");
} else if (strcmp(ext, "png")==0) {
buf_append_str(header, "image/png");
} else if (strcmp(ext, "gif")==0) {
buf_append_str(header, "image/gif");
} else if (strcmp(ext, "bmp")==0) {
buf_append_str(header, "image/bmp");
} else if (strcmp(ext, "svg")==0) {
buf_append_str(header, "image/svg+xml");
} else if (strcmp(ext, "ico")==0) {
buf_append_str(header, "image/vnd.microsoft.icon");
} else {
buf_append_str(header, "text/plain; charset=utf-8");
}
}
buf_append_str(header, "\r\n");
// content length
buf_append_format(header, 30, "Content-Length: %ld\r\n", length);
// end header
buf_append_str(header, "\r\n");
// send it
send_all(sock, header->data, header->length);
buf_free(header);
}
void send_simple_status(int sock, struct client_state* state, char *status_code, char *status_text, char *description) {
struct buffer* body = buf_new(256);
buf_append_str(body, "<html><body><h1>");
buf_append_str(body, status_code);
buf_append_str(body, " - ");
buf_append_str(body, status_text);
buf_append_str(body, "</h1><p>");
buf_append_str(body, description);
buf_append_str(body, "</p></body></html>");
send_header(sock, state, status_code, status_text, "html", body->length);
send_all(sock, body->data, body->length);
buf_free(body);
}
int send_file(int sock, struct client_state* state, char* path) {
size_t length;
FILE *file = fopen(path, "rb");
if (file == NULL) {
return -1;
}
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
struct buffer* body = buf_new(length);
if (body == NULL) {
fclose(file);
return -1;
}
fread(buf_reserve(body, length), 1, length, file);
fclose(file);
char *ext;
ext = strrchr(path, '.');
if (strlen(ext) <= 1) {
ext = ".txt"; // hack!
}
send_header(sock, state, "200", "OK", ext+1, length);
send_all(sock, body->data, length);
buf_free(body);
}
void client_listener(struct sockets_list* sockets, int index) {
int sock = sockets->pollfds[index].fd;
struct client_state* state = sockets->states[index];
int recvied = recv(sock, state->in->data + state->in->length, state->in->size - state->in->length, 0);
if (recvied <= 0) {
if (recvied < 0) {
fprintf(stderr, "recv error from %s (%d): %s\n", state->address, sock, strerror(errno));
} else {
tprintf("connection from %s (%d) closed\n", state->address, sock);
}
close(sock);
client_state_free(state);
sockets_list_rm(sockets, index);
} else {
// update buffer
buf_seek(state->in, recvied);
// parse request
int space_1 = -1;
int space_2 = -1;
int end = -1;
for (int i=0; i<state->in->length; i++) {
if (space_1<0) {
if (state->in->data[i]==' ') {
space_1 = i;
}
} else if (space_2<0) {
if (state->in->data[i]==' ') {
space_2 = i;
}
}
// look for a blank line
if (i>2 && state->in->data[i-3]=='\r' && state->in->data[i-2]=='\n' && state->in->data[i-1]=='\r' && state->in->data[i]=='\n') {
end = i;
}
}
if (end<0) {
// make buffer bigger
buf_extend(state->in, state->in->size * 2);
} else {
if (space_1>0 && space_2>space_1+1) {
char method[space_1+1];
memcpy(method, state->in->data, space_1);
method[space_1] = '\0';
char path[space_2-space_1+11]; // extra 11 is for /index.html
memcpy(path, state->in->data+space_1+1, space_2-space_1-1);
path[space_2-space_1-1] = '\0';
tprintf("\"%s\" \"%s\" from %s (%d)\n", method, path, state->address, sock);
if (strcmp(method, "GET")==0) {
char* file_path = get_file_path(files, path);
if (file_path != NULL) {
send_file(sock, state, file_path);
} else {
// try appending /index.html
if (path[space_2-space_1-2] == '/') {
strcat(path, "index.html");
} else {
strcat(path, "/index.html");
}
file_path = get_file_path(files, path);
if (file_path != NULL) {
send_file(sock, state, file_path);
} else {
send_simple_status(sock, state, "404", "Not Found", "Opps, that resource can not be found.");
}
}
} else {
send_simple_status(sock, state, "501", "Not Implemented", "Opps, that functionality has not been implemented.");
}
} else {
send_simple_status(sock, state, "400", "Bad Request", "Opps, that request made no sense.");
}
// done with this request to reset
buf_reset(state->in);
}
}
}
// ================ server code ================
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa) {
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
void server_listener(struct sockets_list* sockets, int index) {
struct sockaddr_storage address;
socklen_t address_size;
int client_socket;
struct client_state* client_state;
int client_index;
address_size = sizeof(address);
if ((client_socket = accept(sockets->pollfds[index].fd, (struct sockaddr *)&address, &address_size)) < 0) {
perror("accept");
} else {
client_state = client_state_new();
client_index = sockets_list_add(sockets, client_socket, client_listener);
sockets->states[client_index] = client_state;
inet_ntop(address.ss_family, get_in_addr((struct sockaddr *)&address), client_state->address, INET6_ADDRSTRLEN);
tprintf("connection from %s (%d) opened\n", client_state->address, client_socket);
}
}
// ================ Main loop etc ================
int main(int argc, char* argv[]) {
// validate arguments
if (argc != 3) {
fprintf(stderr, "Usage: %s port root_dir\n", argv[0]);
return EXIT_FAILURE;
}
// get static files
files = find_static_files(argv[2]);
tprintf("available files\n");
for (struct static_file* file = files; file != NULL; file = file->next) {
printf(" %s\n", file->server_path);
}
// create list of sockets
struct sockets_list* sockets = sockets_list_new();
// open server socket
int server_socket = get_server_socket(argv[1]);
if (server_socket < 0) {
fprintf(stderr, "error getting server socket\n");
return EXIT_FAILURE;
}
sockets_list_add(sockets, server_socket, server_listener);
tprintf("waiting for connections\n");
// loop forever directing network traffic
for (;;) {
if (poll(sockets->pollfds, sockets->count, -1) < 0 ) {
perror("poll");
return EXIT_FAILURE;
}
for (int i = 0; i < sockets->count; i++) {
if (sockets->pollfds[i].revents & POLLIN) {
sockets->listeners[i](sockets, i);
}
}
}
// tidy up, but we should never get here?
close(server_socket);
free_static_files(files);
return EXIT_SUCCESS;
}