-
Notifications
You must be signed in to change notification settings - Fork 0
/
mimino.c
1150 lines (1009 loc) · 32.9 KB
/
mimino.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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Mimino - small http server
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
#include <dirent.h>
#include <time.h>
#include <ifaddrs.h>
#include "mimino.h"
#include "fdwatch.h"
#include "xmalloc.h"
#include "defer.h"
#include "http.h"
#include "arg.h"
#include "connection.h"
int sockbind(struct addrinfo *ai);
int send_buf(int sock, char *buf, size_t nbytes);
void *get_in_addr(struct sockaddr *sa);
unsigned short get_in_port(struct sockaddr *sa);
void hex_dump_line(FILE *stream, char *buf, size_t buf_size, size_t width);
void dump_data(FILE *stream, char *buf, size_t bufsize, size_t nbytes_recvd);
void ascii_dump_buf(FILE *stream, char *buf, size_t buf_size);
void
print_server_config(Server_Config *conf)
{
if (!conf) {
printf("(Server_Config) (NULL)\n");
return;
}
printf("(Server_Config) = {\n");
printf(" .verbose = %d,\n", conf->verbose);
printf(" .quiet = %d,\n", conf->quiet);
printf(" .unsafe = %d,\n", conf->unsafe);
printf(" .chroot = %d,\n", conf->chroot);
printf(" .serve_error_files = %d,\n", conf->serve_error_files);
printf(" .serve_path = \"%s\",\n", conf->serve_path);
printf(" .port = \"%s\",\n", conf->port);
printf(" .index = \"%s\",\n", conf->index);
printf(" .suffix = \"%s\",\n", conf->suffix);
printf(" .chroot_dir = \"%s\",\n", conf->chroot_dir);
printf(" .max_fds = %d,\n", conf->max_fds);
printf(" .timeout_secs = %d,\n", conf->timeout_secs);
printf(" .poll_interval_ms = %d,\n", conf->poll_interval_ms);
printf("}\n\n");
}
void
print_server_state(Server *serv)
{
printf("time_now: %ld\n", serv->time_now);
printf("n_conns_alloc: %zu\n", serv->queue.n_conns_alloc);
printf("n_conns: %zu\n", serv->queue.n_conns);
for (nfds_t i = 1; i < serv->queue.n_conns; i++) {
printf("Connection %zu:\n", i);
print_connection(serv->queue.pollfds + i,
serv->queue.conns + i);
printf("------------\n");
}
}
void
print_server_lan_addr(char *port)
{
struct ifaddrs *ifa;
if (getifaddrs(&ifa) != 0) {
perror("getifaddrs() inside print_server_lan_addr()");
return;
}
/* TODO: don't print the ip that's disabled (depending on
serv.conf.ipv4/serv.conf.ipv6) */
printf("Available on:\n");
char addr_str[INET6_ADDRSTRLEN];
for (struct ifaddrs *i = ifa; i; i = i->ifa_next) {
if (!i->ifa_addr) continue;
if (i->ifa_addr->sa_family != AF_INET &&
i->ifa_addr->sa_family != AF_INET6)
continue;
const char *succ = inet_ntop(
i->ifa_addr->sa_family,
get_in_addr(i->ifa_addr),
addr_str,
(socklen_t) INET6_ADDRSTRLEN);
if (!succ) {
perror("inet_ntop() on ifa_addr");
continue;
}
if (i->ifa_addr->sa_family == AF_INET6)
printf(" http://[%s]:%s\n", addr_str, port);
else
printf(" http://%s:%s\n", addr_str, port);
}
printf("\n");
}
int
init_server(char *port, struct addrinfo *server_addrinfo)
{
int sockfd;
/* char ip_str[INET6_ADDRSTRLEN]; */
struct addrinfo *getaddrinfo_res;
struct addrinfo **ipv4_addrinfos = NULL;
int ipv4_addrinfos_i = 0;
struct addrinfo **ipv6_addrinfos = NULL;
int ipv6_addrinfos_i = 0;
// Init hints for getaddrinfo
struct addrinfo hints = {
.ai_flags = AI_PASSIVE,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
};
// Get server info
int err = getaddrinfo(NULL, port, &hints, &getaddrinfo_res);
if (err) {
fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(err));
return 1;
}
// Populate ipv4_addrinfos and ipv6_addrinfos arrays
for (struct addrinfo *rp = getaddrinfo_res; rp != NULL; rp = rp->ai_next) {
// Log each getaddrinfo response
/* if (inet_ntop(rp->ai_family, */
/* get_in_addr(rp->ai_addr), */
/* ip_str, sizeof(ip_str))) { */
/* printf("getaddrinfo response: %s:%s\n", ip_str, port); */
/* } */
switch (rp->ai_family) {
case AF_INET:
ipv4_addrinfos = xrealloc(ipv4_addrinfos,
(ipv4_addrinfos_i + 1) * sizeof(ipv4_addrinfos));
ipv4_addrinfos[ipv4_addrinfos_i] = rp;
ipv4_addrinfos_i++;
break;
case AF_INET6:
ipv6_addrinfos = xrealloc(ipv6_addrinfos,
(ipv6_addrinfos_i + 1) * sizeof(ipv6_addrinfos));
ipv6_addrinfos[ipv6_addrinfos_i] = rp;
ipv6_addrinfos_i++;
break;
default:
fprintf(stderr, "Unknown AF (addrinfo family): %d\n", rp->ai_family);
continue;
}
}
int bound = 0;
// Try binding to ipv4 (prefer ipv4 over ipv6)
for (int i = 0; i < ipv4_addrinfos_i; i++) {
sockfd = sockbind(ipv4_addrinfos[i]);
if (sockfd != -1) {
memcpy(server_addrinfo, ipv4_addrinfos[i], sizeof(*server_addrinfo));
bound = 1;
break;
}
}
// Try binding to ipv6
if (!bound) {
for (int i = 0; i < ipv6_addrinfos_i; i++) {
sockfd = sockbind(ipv6_addrinfos[i]);
if (sockfd != -1) {
memcpy(server_addrinfo, ipv6_addrinfos[i], sizeof(*server_addrinfo));
bound = 1;
break;
}
}
}
free(ipv4_addrinfos);
free(ipv6_addrinfos);
freeaddrinfo(getaddrinfo_res);
return bound ? sockfd : -1;
}
void
init_conn_pool(Server *serv)
{
serv->queue.n_conns_alloc = MIN(serv->conf.max_fds, 1024);
serv->queue.n_conns = 0;
serv->queue.pollfds = xmalloc(
sizeof(struct pollfd) * serv->queue.n_conns_alloc);
serv->queue.conns = xmalloc(
sizeof(Connection) * serv->queue.n_conns_alloc);
}
// Returns the socket fd of the new connection
int
accept_new_conn(int listen_sock)
{
int newsock;
int saved_errno;
char their_ip_str[INET6_ADDRSTRLEN];
struct sockaddr_storage their_addr;
socklen_t their_addr_size = sizeof(their_addr);
// Accept new connection
// TODO: set O_NONBLOCK and check for EWOULDBLOCK. Right now this will block
newsock = accept(listen_sock, (struct sockaddr *)&their_addr, &their_addr_size);
saved_errno = errno;
if (newsock == -1) {
if (saved_errno != EAGAIN) {
fprintf(stderr, "%d", saved_errno);
perror("accept()");
}
}
// Don't block on newsock
if (fcntl(newsock, F_SETFL, O_NONBLOCK) != 0) {
perror("fcntl()");
}
// Print their_addr
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
their_ip_str, sizeof(their_ip_str));
/* printf("Got connection from %s:%d\n", their_ip_str, */
/* ntohs(get_in_port((struct sockaddr *)&their_addr))); */
return newsock;
}
#define R_CLIENT_CLOSED 2 // client closed connection.
#define R_COMPLETE_READ 1 // done reading completely.
#define R_PARTIAL_READ 0 // an incomplete read happened.
#define R_FATAL_ERROR -1 // fatal error or max retry reached.
#define R_REQ_TOO_BIG -2 // when request is too big to handle.
int
read_request(Connection *conn)
{
int n = recv(conn->fd,
conn->req->buf->data + conn->req->buf->n_items,
conn->req->buf->n_alloc - conn->req->buf->n_items,
0);
if (n < 0) {
int saved_errno = errno;
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) {
if (conn->read_tries_left == 0) {
fprintf(stderr, "Reached max read tries for conn\n");
return R_FATAL_ERROR;
}
conn->read_tries_left--;
}
errno = saved_errno;
perror("recv()");
return R_PARTIAL_READ;
}
//dump_data(stdout,
// conn->req->buf->data + conn->req->buf->n_items,
// n,
// DUMP_WIDTH);
conn->req->buf->n_items += n;
// Finished reading
if (is_http_end(conn->req->buf->data, conn->req->buf->n_items))
return R_COMPLETE_READ;
if (n == 0) {
if (conn->req->buf->n_items == 0) return R_CLIENT_CLOSED;
else return R_COMPLETE_READ;
}
// Not finished yet, but req buffer full
if (conn->req->buf->n_items == conn->req->buf->n_alloc)
return R_REQ_TOO_BIG;
// TODO: maybe decrease read_tries_left here? imagine that a
// client sends 10 bytes with 1 second delays (can happen
// with slow networks), should we block them if they fail to
// send their request in more than 5 tries? Otherwise, a
// person with a larger memory than the server can simply
// create 1000s of very slow requests and clog up the server.
// Solution to this would be to have a
// conn->reading_abs_timeout_ms which is set to 30000, which
// means the client has 30 seconds to make the request in
// total.
// TODO: Could be smart to have conn->writing_abs_timeout_ms
// as well.
return R_PARTIAL_READ;
}
#define W_TIMED_OUT -3
#define W_MAX_TRIES -2
#define W_FATAL_ERROR -1
#define W_PARTIAL_WRITE 0
#define W_COMPLETE_WRITE 1
int
write_headers(Connection *conn)
{
int sent = send_buf(
conn->fd,
conn->res->head.data + conn->res->head_nbytes_sent,
conn->res->head.n_items - conn->res->head_nbytes_sent);
// Fatal error, no use retrying
if (sent == -1) {
conn->res->error = "write_headers(): Other error";
return W_FATAL_ERROR;
}
conn->res->head_nbytes_sent += sent;
// Retry later if not sent fully
if (conn->res->head_nbytes_sent < conn->res->head.n_items) {
if (conn->write_tries_left == 0) {
conn->res->error = "write_headers(): Max write tries reached";
return W_MAX_TRIES;
}
conn->write_tries_left--;
return W_PARTIAL_WRITE;
}
return W_COMPLETE_WRITE;
}
int
write_body(Connection *conn)
{
if (conn->res->body.data == NULL) {
printf("Eror: Response for conn with fd %d has empty body\n",
conn->fd);
return W_FATAL_ERROR;
}
int sent = send_buf(
conn->fd,
conn->res->body.data +
(conn->res->range_start + conn->res->body_nbytes_sent),
conn->res->range_end + 1 - conn->res->body_nbytes_sent);
// Fatal error, no use retrying
if (sent == -1) {
conn->res->error = "write_body(): Other error";
return W_FATAL_ERROR;
}
conn->res->body_nbytes_sent += sent;
// Retry later if not sent fully
if (conn->res->range_start + (off_t) conn->res->body_nbytes_sent <
conn->res->range_end + 1) {
if (conn->write_tries_left == 0) {
conn->res->error = "write_body(): Max write tries reached";
return W_MAX_TRIES;
}
conn->write_tries_left--;
return W_PARTIAL_WRITE;
}
return W_COMPLETE_WRITE;
}
int
write_file(Connection *conn)
{
Defer_Queue dq = NULL_DEFER_QUEUE;
if (conn->res->file.is_null) {
printf("Eror: File is null for conn with fd %d\n",
conn->fd);
return W_FATAL_ERROR;
}
char file_buf[4096];
size_t file_buf_len = sizeof(file_buf) * sizeof(char);
size_t nbytes_left = conn->res->range_end - conn->res->file_offset;
size_t nbytes_to_read = file_buf_len < nbytes_left ?
file_buf_len : nbytes_left;
// TODO: reuse file handle if partial file writes happen
// Open file
FILE *file_handle = fopen(conn->res->file_path, "r");
if (file_handle == NULL) {
perror("write_file(): Error on fopen()");
// TODO: handle various fopen() errors by switching errno here
return W_FATAL_ERROR;
}
defer(&dq, fclose, file_handle);
// Seek
if (conn->res->file_offset != 0) {
int err = fseeko(file_handle, conn->res->file_offset, SEEK_SET);
if (err) {
perror("write_file(): Error on fseeko()");
// TODO: handle various fseeko() errors by switching errno here
return fulfill(&dq, W_FATAL_ERROR);
}
}
// Start reading the file and sending it.
// This works without loading the entire file into memory.
while (1) {
size_t bytes_read = fread(
file_buf,
1,
nbytes_to_read,
file_handle);
if (bytes_read == 0) {
if (ferror(file_handle)) {
conn->res->error = "write_file(): Error when fread()ing file";
// TODO: think of an appropriate error to return here
return fulfill(&dq, W_FATAL_ERROR);
}
return W_COMPLETE_WRITE;
}
int sent = send_buf(conn->fd, file_buf, bytes_read);
if (sent == -1) {
conn->res->error = "write_file(): send_buf() returned -1";
return fulfill(&dq, W_FATAL_ERROR);
}
conn->res->file_offset += sent;
conn->res->file_nbytes_sent += sent; // TODO: useless?
// Retry later if not sent at all
// TODO: a better mechanic for dropping unresponsive
// clients is to time the activity: if the client doesn't
// receive any data for x seconds, drop the connection
if ((size_t) sent == 0) {
// This only happens when the client has a sudden
// disconnection. Retrying later gives the client
// some time to regain the connection.
if (conn->write_tries_left == 0) {
conn->res->error = "write_file(): Max write tries reached";
return fulfill(&dq, W_MAX_TRIES);
}
conn->write_tries_left--;
return fulfill(&dq, W_PARTIAL_WRITE);
} else {
conn->write_tries_left = 5;
}
};
// Unreachable
return fulfill(&dq, W_FATAL_ERROR);
}
void
close_connection(Server *s, nfds_t i)
{
/*
No need to check for errno here as most sane OSes close the
file descriptor early in their close syscall. So, retrying
with the same fd might close some other file. For more info
read close(2) manual.
*/
if (close(s->queue.pollfds[i].fd) == -1) {
perror("close()");
}
s->queue.conns[i].fd = -1;
s->queue.pollfds[i].fd = -1;
s->queue.pollfds[i].events = 0;
s->queue.pollfds[i].revents = 0;
// Copy last pollfd and conn onto current pollfd and conn
if (i != s->queue.n_conns - 1) {
s->queue.pollfds[i] = s->queue.pollfds[s->queue.n_conns - 1];
s->queue.conns[i] = s->queue.conns[s->queue.n_conns - 1];
}
s->queue.n_conns--;
// Downsize if necessary
if (s->queue.n_conns_alloc > 20 &&
s->queue.n_conns <= s->queue.n_conns_alloc / 4) {
s->queue.n_conns_alloc = MAX(20, s->queue.n_conns_alloc / 2);
s->queue.pollfds =
xrealloc(s->queue.pollfds,
sizeof(s->queue.pollfds[0]) * s->queue.n_conns_alloc);
s->queue.conns =
xrealloc(s->queue.conns,
sizeof(s->queue.conns[0]) * s->queue.n_conns_alloc);
}
}
void
recycle_connection(Server *s, nfds_t i)
{
free_http_request(s->queue.conns[i].req);
free_http_response(s->queue.conns[i].res);
s->queue.conns[i].req = NULL;
s->queue.conns[i].res = NULL;
s->queue.conns[i].read_tries_left = 5;
s->queue.conns[i].write_tries_left = 5;
s->queue.conns[i].keep_alive = 1;
s->queue.conns[i].last_active = s->time_now;
s->queue.pollfds[i].events = POLLIN;
s->queue.pollfds[i].revents = 0;
}
// TODO: don't take pollfd pointer, reference it from conn->
void
set_conn_state(struct pollfd *pfd, Connection *conn, int state)
{
conn->state = state;
switch (state) {
case CONN_STATE_READING:
pfd->events = POLLIN;
break;
case CONN_STATE_WRITING_HEADERS:
case CONN_STATE_WRITING_BODY:
pfd->events = POLLOUT;
break;
case CONN_STATE_WRITING_FINISHED:
case CONN_STATE_CLOSING:
pfd->events = 0;
break;
}
}
// State machine for handling connections
int
do_conn_state(Server *serv, nfds_t idx)
{
Connection *conn = &(serv->queue.conns[idx]);
struct pollfd *pfd = &(serv->queue.pollfds[idx]);
switch (conn->state) {
case CONN_STATE_READING: {
if (!(pfd->revents & POLLIN))
return 0;
// Allocate memory for request struct
if (conn->req == NULL) {
conn->req = xmalloc(sizeof(*(conn->req)));
memset(conn->req, 0, sizeof(*(conn->req)));
// We won't grow this buffer
conn->req->buf = new_buf(MAX_REQUEST_SIZE);
}
int status = read_request(conn);
switch (status) {
case R_PARTIAL_READ:
conn->last_active = serv->time_now;
break;
case R_FATAL_ERROR:
case R_CLIENT_CLOSED:
set_conn_state(pfd, conn, CONN_STATE_CLOSING);
do_conn_state(serv, idx);
break;
case R_REQ_TOO_BIG:
// TODO: send 413 error instead
set_conn_state(pfd, conn, CONN_STATE_CLOSING);
do_conn_state(serv, idx);
break;
case R_COMPLETE_READ:
conn->last_active = serv->time_now;
// Reading done, parse request
// TODO: turn this into a separate state
parse_http_request(conn->req);
print_http_request(stdout, conn->req);
// Parse error
if (conn->req->error) {
fprintf(stdout,
"Parse error: %s\n",
conn->req->error);
fprintf(stderr,
"Request buffer dump:\n");
print_buf_ascii(stderr, conn->req->buf);
// Close the connection if we didn't manage
// to parse the essential headers
if (!conn->req->method ||
!conn->req->path ||
!conn->req->host) {
set_conn_state(pfd, conn, CONN_STATE_CLOSING);
do_conn_state(serv, idx);
return 1;
}
}
// Set keep-alive
if (conn->req->connection &&
!strcasecmp(conn->req->connection, "close")) {
conn->keep_alive = 0;
}
// Start writing
set_conn_state(pfd, conn, CONN_STATE_WRITING_HEADERS);
break;
}
break;
}
case CONN_STATE_WRITING_HEADERS: {
if (!(pfd->revents & POLLOUT))
return 0;
// Generate response
if (!conn->res) {
conn->last_active = serv->time_now;
conn->res = make_http_response(serv, conn->req);
if (serv->conf.verbose) {
printf("-----------------\n");
print_http_request(stdout, conn->req);
print_http_response(stdout, conn->res);
printf("-----------------\n");
}
}
int status = write_headers(conn);
switch (status) {
case W_PARTIAL_WRITE:
conn->last_active = serv->time_now;
return 0;
case W_MAX_TRIES:
if (serv->conf.verbose) {
printf("DEB: write_headers() on connection %lud "
"returned W_MAX_TRIES with error \"%s\"\n",
idx, conn->res->error);
}
set_conn_state(pfd, conn, CONN_STATE_CLOSING);
do_conn_state(serv, idx);
break;
case W_FATAL_ERROR:
if (serv->conf.verbose) {
printf("DEB: write_headers() on connection %lud "
"returned W_FATAL_ERROR with error \"%s\"\n",
idx, conn->res->error);
}
set_conn_state(pfd, conn, CONN_STATE_CLOSING);
do_conn_state(serv, idx);
break;
case W_COMPLETE_WRITE:
if (!strcmp(conn->req->method, "HEAD")) {
set_conn_state(pfd, conn, CONN_STATE_WRITING_FINISHED);
do_conn_state(serv, idx);
} else {
set_conn_state(pfd, conn, CONN_STATE_WRITING_BODY);
do_conn_state(serv, idx);
}
break;
}
break;
}
case CONN_STATE_WRITING_BODY: {
if (!(pfd->revents & POLLOUT))
return 0;
int write_fn; // If write_body was called, holds 0, otherwise holds 1
int status;
if (conn->res->body.data) {
status = write_body(conn);
write_fn = 0;
} else {
status = write_file(conn);
write_fn = 1;
}
switch (status) {
case W_PARTIAL_WRITE:
return 0;
case W_MAX_TRIES:
// TODO: print error stuff
printf("W_MAX_TRIES\n");
set_conn_state(pfd, conn, CONN_STATE_CLOSING);
do_conn_state(serv, idx);
break;
case W_FATAL_ERROR:
// TODO: print error stuff
if (serv->conf.verbose) {
if (write_fn == 0) {
printf("ERR: write_file() on connection %lud "
"returned W_FATAL_ERROR with error \"%s\"\n",
idx, conn->res->error);
} else {
printf("ERR: write_body() on connection %lud "
"returned W_FATAL_ERROR with error \"%s\"\n",
idx, conn->res->error);
}
}
set_conn_state(pfd, conn, CONN_STATE_CLOSING);
do_conn_state(serv, idx);
break;
case W_COMPLETE_WRITE:
set_conn_state(pfd, conn, CONN_STATE_WRITING_FINISHED);
do_conn_state(serv, idx);
break;
}
break;
}
case CONN_STATE_WRITING_FINISHED: {
if (conn->keep_alive) {
recycle_connection(serv, idx);
set_conn_state(pfd, conn, CONN_STATE_READING);
} else {
set_conn_state(pfd, conn, CONN_STATE_CLOSING);
do_conn_state(serv, idx);
}
break;
}
case CONN_STATE_CLOSING:
free_connection_parts(conn);
close_connection(serv, idx);
break;
}
return -1;
}
void
serv_add_connection(Server *s, int newsock)
{
int idx = s->queue.n_conns;
s->queue.n_conns++;
// Resize pollfds and conns arrays
if (s->queue.n_conns >= s->queue.n_conns_alloc) {
s->queue.n_conns_alloc = MIN(
s->queue.n_conns_alloc * 2,
(nfds_t) s->conf.max_fds);
s->queue.pollfds =
xrealloc(s->queue.pollfds,
sizeof(s->queue.pollfds[0]) * s->queue.n_conns_alloc);
s->queue.conns =
xrealloc(s->queue.conns,
sizeof(s->queue.conns[0]) * s->queue.n_conns_alloc);
}
// Add pollfd
s->queue.pollfds[idx] = (struct pollfd) {
.fd = newsock,
.events = POLLIN,
.revents = 0,
};
// Add connection
s->queue.conns[idx] = make_connection(newsock, s, idx);
}
int
main(int argc, char **argv)
{
Server serv = {0};
Argdef argdefs[9];
memset(argdefs, 0, sizeof(argdefs));
argdefs[0] = (Argdef) {
.short_arg = 'v',
.long_arg = "verbose",
.type = ARGDEF_TYPE_BOOL,
};
argdefs[1] = (Argdef) {
.short_arg = 'q',
.long_arg = "quiet",
.type = ARGDEF_TYPE_BOOL,
};
argdefs[2] = (Argdef) {
.short_arg = 'u',
.long_arg = "unsafe",
.type = ARGDEF_TYPE_BOOL,
};
argdefs[3] = (Argdef) {
.short_arg = 'r',
.long_arg = "chroot",
.type = ARGDEF_TYPE_STRING,
};
argdefs[4] = (Argdef) {
.short_arg = 'e',
.long_arg = "error-files",
.type = ARGDEF_TYPE_BOOL,
};
argdefs[5] = (Argdef) {
.short_arg = 's',
.long_arg = "suffix",
.type = ARGDEF_TYPE_STRING,
};
argdefs[6] = (Argdef) {
.short_arg = 'p',
.long_arg = "port",
.type = ARGDEF_TYPE_STRING,
};
argdefs[7] = (Argdef) {
.short_arg = 'i',
.long_arg = "index",
.type = ARGDEF_TYPE_STRING,
};
argdefs[8] = (Argdef) {
// file/directory to serve
.type = ARGDEF_TYPE_RAW,
};
int parse_ok = parse_args(argc, argv, 9, argdefs);
if (!parse_ok) {
printf("Error parsing arguments\n");
return 1;
}
// Set server configs
serv.conf = (Server_Config) {
.verbose = argdefs[0].bvalue,
.quiet = argdefs[1].bvalue,
.unsafe = argdefs[2].bvalue && !argdefs[3].bvalue,
.chroot = argdefs[3].bvalue,
.chroot_dir = argdefs[3].value,
.serve_error_files = argdefs[4].bvalue,
.port = argdefs[6].value ? argdefs[6].value : "8080",
.suffix = argdefs[5].bvalue ?
(argdefs[5].value ? argdefs[5].value : ".html")
: NULL,
.index = argdefs[7].bvalue ?
(argdefs[7].value ? argdefs[7].value : "index.html")
: NULL,
.serve_path = argdefs[8].value ? argdefs[8].value : "./",
.timeout_secs = 20,
.poll_interval_ms = 1000,
.max_fds = fdwatch_get_max_poll_nfds(),
};
// Set chroot directory
if (serv.conf.chroot && serv.conf.chroot_dir == NULL) {
serv.conf.chroot_dir = serv.conf.serve_path;
}
// Print server configuration
if (!serv.conf.quiet) print_server_config(&serv.conf);
// Init server
serv.time_now = time(NULL);
struct addrinfo server_addrinfo = {0};
int listen_sock = init_server(serv.conf.port, &server_addrinfo);
if (listen_sock == -1) {
fprintf(stderr, "init_server() failed.\n");
return 1;
}
// Print server LAN addresses
print_server_lan_addr(serv.conf.port);
// Start listening
int backlog = 10;
if (listen(listen_sock, backlog) == -1) {
perror("listen()");
return 1;
}
// Preallocate space for connections & pollfds
init_conn_pool(&serv);
// Add listen_sock to poll queue
serv.queue.pollfds[0] = (struct pollfd) {
.fd = listen_sock,
.events = POLLIN,
.revents = 0,
};
serv.queue.n_conns = 1;
// NOTE: This connection won't be used; it's the listen socket
serv.queue.conns[0] = make_connection(listen_sock, &serv, 0);
// Main loop
while (1) {
serv.time_now = time(NULL);
int nfds = poll(
serv.queue.pollfds,
serv.queue.n_conns,
serv.conf.poll_interval_ms);
if (nfds == -1) {
perror("poll() returned -1");
return 1;
}
if (serv.conf.verbose) {
printf("\n\nSERVER STATE BEFORE ITERATION:\n");
print_server_state(&serv);
}
// Accept new connection
// pollfds[0].fd is the listen_sock
if (serv.queue.pollfds[0].revents & POLLIN) {
if (serv.queue.n_conns >= (nfds_t) serv.conf.max_fds) {
fprintf(
stderr,
"poll queue reached maximum capacity of %d\n",
serv.conf.max_fds);
} else {
int newsock = accept_new_conn(listen_sock);
if (newsock != -1) {
// Update conn queue
serv_add_connection(&serv, newsock);
// Restart loop to prioritize new connections
continue;
}
}
}
// Iterate conn/pollfd queue (starts from 1, skipping listen_sock)
for (nfds_t idx = 1; idx < serv.queue.n_conns; idx++) {
Connection *conn = &(serv.queue.conns[idx]);
// Drop connection if it timed out
if ((conn->state != CONN_STATE_CLOSING) &&
(conn->last_active + serv.conf.timeout_secs
<= serv.time_now)) {
fprintf(stdout, "Connection %ld timed out\n", idx);
conn->state = CONN_STATE_CLOSING;
}
do_conn_state(&serv, idx);
}
if (serv.conf.verbose) {
printf("\n\nSERVER STATE AFTER ITERATION:\n");
print_server_state(&serv);
}
}
return 0;
}
// Get internet address (sin_addr or sin6_addr) from sockaddr
inline void*
get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return (void*) &((struct sockaddr_in*)sa)->sin_addr;
} else {
return (void*) &((struct sockaddr_in6*)sa)->sin6_addr;
}
}
inline unsigned short
get_in_port(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return ((struct sockaddr_in*)sa)->sin_port;
} else {
return ((struct sockaddr_in6*)sa)->sin6_port;
}
}
// Calls socket(), setsockopt() and then bind().
// Returns socket file descriptor on success, -1 on error.
int
sockbind(struct addrinfo *ai)
{
int s = socket(ai->ai_family, // AF_INET or AF_INET6
ai->ai_socktype, // SOCK_STREAM
ai->ai_protocol); // 0
if (s == -1) {
perror("socket() inside sockbind()");
return -1;
}
int status;
// Reuse socket address
int reuse = 1;
status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));