-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-server.cpp
More file actions
1253 lines (1071 loc) · 38.2 KB
/
http-server.cpp
File metadata and controls
1253 lines (1071 loc) · 38.2 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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <netdb.h>
#include <dirent.h>
#include <errno.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <netdb.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <unordered_map>
#include <string>
#include "http-server.h"
// Hashtable linking file descriptors (either for a socket, pipe, or file)
// to connection structs.
static connections_t* connections = new connections_t(500);
static FILE* accessLog; // don't like this being global
static ext_x_mime_t* ext2mime = new ext_x_mime_t({
{"html", "text/html"},
{"htm", "text/html"},
{"jpeg", "image/jpeg"},
{"jpg", "image/jpeg"},
{"png", "image/png"},
{"gif", "image/gif"},
{"pdf", "application/pdf"},
{"json", "application/json"},
{"txt", "text/plain"}});
static const char* responseType2String[NUM_HTTP_RESPONSE_TYPES] = {
"200 OK",
"400 Bad Request",
"405 Method Not Allowed",
"403 Forbidden",
"404 Not Found",
"500 Internal Server Error"
};
static const char* requestType2String[NUM_HTTP_REQUEST_TYPES] = {
"GET",
"PUT",
"POST",
"DELETE"
};
int mySock;
int main(int argc, char* argv[]) {
struct sockaddr_in6 me;
int res;
char v6buf[INET6_ADDRSTRLEN];
socklen_t sockSize = sizeof(struct sockaddr_in6);
#ifdef DEBUG
set_debug_level(DEBUG_INFO);
#else
set_debug_level(DEBUG_NONE);
#endif
debug(DEBUG_INFO, "Starting on port %d...\n", SERVER_PORT);
add_handler(SIGINT, clean_exit);
signal(SIGCHLD, SIG_IGN);
if ((accessLog = fopen("access.log", "a")) == NULL) {
pexit("fopen");
}
if ((mySock = socket(AF_INET6, SOCK_STREAM, 0)) == -1) {
pexit("Opening socket failed");
}
// Make the socket non-blocking.
if (fcntl(mySock, F_SETFL, fcntl(mySock, F_GETFL) | O_NONBLOCK) == -1) {
pexit("fcntl");
}
memset(&me, 0, sizeof(me));
me.sin6_family = AF_INET6;
me.sin6_port = htons(SERVER_PORT);
if (argc > 1) {
if ((res = inet_pton(AF_INET6, argv[1], &me.sin6_addr)) == -1) {
pexit("inet_pton");
} else if (res == 0) {
debug(DEBUG_WARNING, "Converting v4 to v6 addr\n");
snprintf(v6buf, INET6_ADDRSTRLEN, "::ffff:%s", argv[1]);
if (inet_pton(AF_INET6, v6buf, &me.sin6_addr) == 0) {
debug(DEBUG_ERROR, "Invalid address.\n");
exit(EXIT_FAILURE);
}
}
} else {
me.sin6_addr = in6addr_any;
}
if (bind(mySock, (struct sockaddr *) &me, sockSize)) {
pexit("bind");
}
if (getsockname(mySock, (struct sockaddr *) &me, &sockSize)) {
pexit("getsockname");
}
if (listen(mySock, SOMAXCONN)) {
pexit("listen");
}
if (fdarr_cntl(FDARR_ADD, mySock, POLLIN) == POSIX_ERROR) {
debug(DEBUG_ERROR, "File descriptor array modification failed.\n");
exit(EXIT_FAILURE);
}
//the event loop
while (true) {
struct pollfd* fds;
nfds_t nfds;
unsigned int i;
int res, fd, revents;
fdarr_cntl(FDARR_GET, &fds, &nfds);
//debug(DEBUG_INFO, ">>> Calling poll.\n");
if ((res = poll(fds, nfds, -1)) == -1) {
switch (errno) {
case EINTR:
debug(DEBUG_INFO, "Poll interrupted by signal.\n");
continue;
default:
debug(DEBUG_ERROR, "poll: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
//debug(DEBUG_INFO, "<<< Returning from poll.\n");
for (i = 0; i < nfds; i++) {
fd = fds[i].fd;
revents = fds[i].revents;
if (fd != mySock && connections->count(fd) == 0) {
debug(DEBUG_ERROR, "Unrecognized fd(%d) in fdarr\n", fd);
exit(EXIT_FAILURE);
}
if (revents && fd == mySock) {
res = process_mysock_events(mySock, revents);
} else if (revents && fd == connections->at(fd)->socket) {
res = process_cxsock_events(connections->at(fd), revents);
} else if (revents && fd == connections->at(fd)->file) {
res = process_cxfile_events(connections->at(fd), revents);
} else if (revents) {
debug(DEBUG_WARNING, "Unknown event!\n");
}
// If the fdarr was modified, break from the loop
// and start over completely with the right nfds value.
if (res == FDARR_MODIFIED) {
debug(DEBUG_INFO, "Fdarr modified, breaking\n");
break;
} else if (res == BAD_SOCKET) {
debug(DEBUG_WARNING, "Error accepting socket connection\n");
} else if (res == POSIX_ERROR) {
debug(DEBUG_ERROR, "Allocating connections struct failed\n");
exit(EXIT_FAILURE);
}
}
}
return 0;
}
static int process_mysock_events(int socket, short revents) {
struct connection* cxn;
struct sockaddr_in6 me;
socklen_t sockSize = sizeof(struct sockaddr_in6);
char v6buf[INET6_ADDRSTRLEN];
int err = 0;
if (revents & POLLIN) {
debug(DEBUG_INFO, "mysock, revent: POLLIN, state: N/A\n");
cxn = (struct connection *) calloc(1, sizeof(struct connection));
if (cxn == NULL) {
return POSIX_ERROR;
}
cxn->socket = accept(socket, (struct sockaddr *) &me, &sockSize);
if (cxn->socket == -1) {
// We can't generate an internal error because we can't
// communicate with the client, so we just return and hope it
// doesn't happen again
debug(DEBUG_WARNING, "accept: %s\n", strerror(errno));
return BAD_SOCKET;
}
cxn->env.mySocket = cxn->socket;
cxn->env.serverSocket = socket;
debug(DEBUG_INFO, "Connected to %s\n",
inet_ntop(AF_INET6, &me.sin6_addr, v6buf, INET6_ADDRSTRLEN));
fdarr_cntl(FDARR_ADD, cxn->socket, POLLIN);
err = FDARR_MODIFIED;
connections->insert(std::make_pair(cxn->socket, cxn));
} else {
debug(DEBUG_INFO, "mysock, revent: unknown, %08X, state: N/A\n", revents);
}
return err;
}
static int process_cxsock_events(struct connection* cxn, short revents) {
static int (*frbActions[NUM_REQUEST_STATES])(struct connection *) = {
request_state_zero_read,
request_state_error,
request_state_incomplete,
request_state_finished
};
struct buf* buf;
enum CXN_STATE ps, ns;
int res;
char* ptr;
ps = cxn->state;
ns = ps;
if (revents & POLLIN && ps == ST_REQUEST) {
debug(DEBUG_INFO, "cxsock, revent: POLLIN, state: ST_REQUEST\n");
return frbActions[fill_request_buffer(cxn->socket,
cxn->request.buffer,
&cxn->request.bytesUsed)](cxn);
} else if (revents & POLLOUT && ps == ST_INTERNAL) {
debug(DEBUG_INFO, "cxsock, revent: POLLOUT, state: ST_INTERNAL\n");
buf = &cxn->response.buffer;
ptr = buf->data + (buf->bytesUsed - buf->bytesToWrite);
// In this state the request buffer (with HTTP headers) has been
// prepared by error_response, or cgi_response, or json_response.
// We send it, and we're done. The exception is for a normal CGI
// response where we then have to fork.
if ((res = write(cxn->socket, ptr, buf->bytesToWrite)) == -1) {
// If there is an error from write, close the connection.
// We can't even send a `500 Internal Error` since we can't
// write on the socket.
debug(DEBUG_WARNING, "write: %s\n", strerror(errno));
return finish_response(cxn, false);
} else if (buf->bytesToWrite - res == 0) {
if (!(cxn->opts & CXN_CGI)) {
return finish_response(cxn, false); // Fix this
} else {
ns = ST_CGI_FIL;
}
} else {
buf->bytesToWrite -= res;
ns = ps;
}
} else if (revents & POLLOUT && ps == ST_RESPONSE_WRIT) {
debug(DEBUG_INFO, "cxsock, revent: POLLOUT, state: ST_RESPONSE_WRIT\n");
buf = &cxn->response.buffer;
ptr = buf->data + (buf->bytesUsed - buf->bytesToWrite);
if ((res = write(cxn->socket, ptr, buf->bytesToWrite)) == -1) {
debug(DEBUG_WARNING, "write: %s\n", strerror(errno));
return finish_response(cxn, false);
} else if (buf->bytesToWrite - res == 0 && cxn->response.eofFound) {
return finish_response(cxn, false);
} else if (buf->bytesToWrite - res == 0) {
fdarr_cntl(FDARR_MODIFY, cxn->socket, 0);
fdarr_cntl(FDARR_MODIFY, cxn->file, POLLIN);
ns = ST_RESPONSE_READ;
} else {
buf->bytesToWrite -= res;
cxn->state = ps;
}
} else if (revents & POLLOUT && ps == ST_CGI_FIL) {
debug(DEBUG_INFO, "cxsock, revent: POLLOUT, state: ST_CGI_FIL\n");
if (do_cgi(cxn) == POSIX_ERROR) {
// If there is an error, send a 500 and "go back" in the
// state machine to ST_INTERNAL. We do this by calling
// this function again. Since we changed the state
// the error actions will occur. We mannually specify the revent
// that will cause the internal response code to run.
error_response(&cxn->response, RESPONSE_INTERNAL_ERROR);
cxn->opts &= ~CXN_CGI;
cxn->state = ST_INTERNAL;
return process_cxsock_events(cxn, POLLOUT);
} else {
return finish_response(cxn, false);
}
} else if (revents & POLLHUP || revents & POLLERR || revents & POLLNVAL) {
debug(DEBUG_INFO, "cxsock, revent: POLLHUP/POLLERR/POLLNVAL\n");
return finish_response(cxn, false);
} else {
debug(DEBUG_INFO, "cxsock, revent: unknown, %08X, state: N/A\n", revents);
}
cxn->state = ns;
return 0;
}
static int process_cxfile_events(struct connection* cxn, short revents) {
struct buf* buf;
int res, readAmt, err = 0;
if (revents & POLLIN && cxn->state == ST_RESPONSE_READ) {
debug(DEBUG_INFO, "cxfile, revent: POLLIN, state: ST_RESPONSE_READ\n");
buf = &cxn->response.buffer;
// Find a better way of doing this later
readAmt = cxn->opts & CXN_FORTUNE ? 0.9 * BUF_LEN : BUF_LEN;
if ((res = read(cxn->file, buf->data, readAmt)) == -1) {
debug(DEBUG_WARNING, "read: %s\n", strerror(errno));
error_response(&cxn->response, RESPONSE_INTERNAL_ERROR);
cxn->state = ST_INTERNAL;
return 0;
}
cxn->response.eofFound = res == 0 || revents & POLLHUP;
fdarr_cntl(FDARR_MODIFY, cxn->socket, POLLOUT);
buf->bytesUsed = buf->bytesToWrite = res;
cxn->state = ST_RESPONSE_WRIT;
if (cxn->opts & CXN_FORTUNE && fortune_decode(buf) == POSIX_ERROR) {
error_response(&cxn->response, RESPONSE_INTERNAL_ERROR);
cxn->state = ST_INTERNAL;
}
} else {
debug(DEBUG_WARNING, "cxfile, revent: unknown %08X, state: N/A\n",
revents);
}
return err;
}
static int request_state_zero_read(struct connection* cxn) {
debug(DEBUG_INFO, "Zero read, closing connection\n");
finish_response(cxn, false);
return FDARR_MODIFIED;
}
static int request_state_error(struct connection* cxn) {
cxn->state = ST_INTERNAL;
fdarr_cntl(FDARR_MODIFY, cxn->socket, POLLOUT);
error_response(&cxn->response, RESPONSE_INTERNAL_ERROR);
return 0;
}
static int request_state_incomplete(struct connection* cxn) {
// Do nothing!
return 0;
}
static int request_state_finished(struct connection* cxn) {
char* ptr;
fdarr_cntl(FDARR_MODIFY, cxn->socket, POLLOUT);
switch(process_request(cxn)) {
case HTTP_FORTUNE:
cxn->opts |= CXN_FORTUNE;
if (do_fortune(cxn) == POSIX_ERROR) {
error_response(&cxn->response, RESPONSE_INTERNAL_ERROR);
cxn->state = ST_INTERNAL;
return 0;
}
cxn->response.contentType = "application/json";
// Set the contentLength to -1 to indicate we're not
// providing this field for this response.
cxn->response.contentLength = -1;
make_response_header(&cxn->response);
//cxn->state = ST_RESPONSE_HEAD;
cxn->state = ST_RESPONSE_WRIT;
return FDARR_MODIFIED;
case HTTP_CGI:
cxn->opts |= CXN_CGI;
cxn->state = ST_INTERNAL;
return 0;
case INTERNAL_RESPONSE:
cxn->state = ST_INTERNAL;
return 0;
}
//cxn->state = ST_RESPONSE_HEAD;
cxn->state = ST_RESPONSE_WRIT;
fdarr_cntl(FDARR_ADD, cxn->file, POLLIN);
connections->insert(std::make_pair(cxn->file, cxn));
ptr = strrchr(cxn->request.filepath, '.');
if (ptr && ext2mime->count(std::string(ptr + 1))) {
cxn->response.contentType =
ext2mime->at(std::string(ptr +1)).c_str();
}
make_response_header(&cxn->response);
return FDARR_MODIFIED;
}
static int finish_response(struct connection* cxn, bool keepAlive) {
int socket;
struct env env;
log_access(cxn);
if (connections->erase(cxn->file)) {
fdarr_cntl(FDARR_REMOVE, cxn->file);
close(cxn->file);
}
if (keepAlive) {
socket = cxn->socket;
env = cxn->env;
fdarr_cntl(FDARR_MODIFY, cxn->socket, POLLIN);
memset(cxn, 0, sizeof(struct connection));
cxn->socket = socket;
cxn->env = env;
} else {
fdarr_cntl(FDARR_REMOVE, cxn->socket);
close(cxn->socket);
connections->erase(cxn->socket);
free(cxn);
}
return FDARR_MODIFIED;
}
static int cgi_request(struct connection* cxn) {
struct response* response = &cxn->response;
enum CGI_CMD cmd = CGI_STATUS; // Initialize to default.
enum RESPONSE_TYPE type;
debug(DEBUG_INFO, "cgi request: %s\n", cxn->env.query);
if ((type = parse_cgi_request(&cxn->env, &cmd)) != RESPONSE_OK) {
return error_response(response, type);
}
debug(DEBUG_INFO, "Doing CGI action.\n");
return cgi_response(&cxn->response, cmd);
}
static int json_request(struct connection* cxn) {
enum JSON_CMD cmd;
char* str = cxn->env.query + strlen("/json/");
debug(DEBUG_INFO, "json request: >%s<\n", cxn->env.query);
if (strcmp(cxn->env.query, "/json/") == 0) {
return error_response(&cxn->response, RESPONSE_NOT_FOUND);
} else if (strcmp(str, "about.json") == 0) {
cmd = JSON_ABOUT;
} else if (strcmp(str, "implemented.json") == 0) {
cmd = JSON_IMPLEMENTED;
} else if (strcmp(str, "fortune") == 0) {
cmd = JSON_FORTUNE;
} else {
debug(DEBUG_WARNING, "Unsupported JSON operation requested: %s\n", str);
return error_response(&cxn->response, RESPONSE_NOT_FOUND);
}
debug(DEBUG_INFO, "Doing some JSON action.\n");
return json_response(&cxn->response, cmd);
}
static int file_request(struct connection* cxn) {
struct stat stats;
struct request* request = &cxn->request;
struct response* response = &cxn->response;
int fd;
if (stat(request->filepath, &stats) == -1) {
switch (errno) {
case ENOENT: return error_response(response, RESPONSE_NOT_FOUND);
case EACCES: return error_response(response, RESPONSE_FORBIDDEN);
default:
debug(DEBUG_WARNING, "stat: %s\n", strerror(errno));
return error_response(response, RESPONSE_INTERNAL_ERROR);
}
}
if (S_ISDIR(stats.st_mode)) {
if (request->filepath[strlen(request->filepath) - 1] != '/') {
strcat(request->filepath, "/");
}
strcat(request->filepath, "index.html");
if (stat(request->filepath, &stats) == -1) {
switch (errno) {
case ENOENT:
debug(DEBUG_INFO, "Generating directory listing.\n");
return generate_listing(request->filepath, response);
default:
debug(DEBUG_WARNING, "stat: %s\n", strerror(errno));
return error_response(response, RESPONSE_INTERNAL_ERROR);
}
}
}
response->contentLength = stats.st_size;
if ((fd = open(request->filepath, O_RDONLY | O_NONBLOCK)) == -1) {
switch (errno) {
case EACCES: return error_response(response, RESPONSE_FORBIDDEN);
default:
debug(DEBUG_WARNING, "popen: %s\n", strerror(errno));
return error_response(response, RESPONSE_INTERNAL_ERROR);
}
}
cxn->file = fd;
return 0;
}
static int process_request(struct connection* cxn) {
char* tmp = cxn->request.filepath + strlen("docs");
enum RESPONSE_TYPE type;
type = make_request_header(&cxn->request);
print_request(&cxn->request);
// We're not supporing deflate right now.
cxn->response.usingDeflate = false;
cxn->response.noKeepAlive = cxn->request.noKeepAlive;
cxn->env.method = requestType2String[cxn->request.type];
cxn->env.query = tmp;
if (type != RESPONSE_OK) {
return error_response(&cxn->response, type);
}
if ((strstr(tmp, "/cgi-bin/"))) {
return cgi_request(cxn);
} else if (strstr(tmp, "/json/")) {
return json_request(cxn);
} else {
return file_request(cxn);
}
}
static enum REQUEST_STATE fill_request_buffer(int socket,
char* buffer,
unsigned int* bytesUsed) {
int bytesRead;
if ((bytesRead = read(socket,
buffer + *bytesUsed,
BUF_LEN - *bytesUsed)) == -1) {
debug(DEBUG_WARNING, "read: %s\n", strerror(errno));
return ST_ERROR;
} else if (bytesRead == 0) {
return ST_ZERO_READ;
}
*bytesUsed += bytesRead;
if (*bytesUsed >= BUF_LEN) {
debug(DEBUG_WARNING, "Request buffer full.\n");
return ST_ERROR;
}
if (memmem(buffer, *bytesUsed, "\r\n\r\n", strlen("\r\n\r\n")) == NULL) {
return ST_INCOMPLETE;
} else {
return ST_FINISHED;
}
}
static int fdarr_cntl(enum FDARR_CMD cmd, ...) {
static struct pollfd* fds = NULL;
static nfds_t nfds = 0;
static unsigned int fdarrMaxSize = FDARR_INIT_LEN / 2;
struct pollfd** fdsFill;
nfds_t* nfdsFill;
unsigned int i;
int fd, err;
va_list args;
va_start(args, cmd);
if (nfds >= fdarrMaxSize - 1 || fds == NULL) {
fdarrMaxSize *= 2;
debug(DEBUG_INFO, "Expanding pollfd array from "
"%d to %d bytes\n", nfds, fdarrMaxSize);
fds = (struct pollfd *) realloc(fds,
fdarrMaxSize * sizeof(struct pollfd));
if (fds == NULL) {
debug(DEBUG_ERROR, "Allocating pollfd array failed\n");
return POSIX_ERROR;
}
err = SIZE_CHANGE;
}
switch(cmd) {
default:
case FDARR_GET:
fdsFill = va_arg(args, struct pollfd **);
nfdsFill = va_arg(args, nfds_t *);
*fdsFill = fds;
*nfdsFill = nfds;
break;
case FDARR_ADD:
fds[nfds].fd = va_arg(args, int);
fds[nfds].events = (short) va_arg(args, int);
nfds++;
break;
case FDARR_MODIFY:
fd = va_arg(args, int);
for (i = 0; i < nfds; i++) {
if (fds[i].fd == fd) {
break;
}
}
fds[i].events = (short) va_arg(args, int);
break;
case FDARR_REMOVE:
fd = va_arg(args, int);
for (i = 0; i < nfds; i++) {
if (fds[i].fd == fd) {
break;
}
}
if (i == nfds) {
debug(DEBUG_WARNING, "Entry not found in pollfd array\n");
return STRUCT_NOT_FOUND;
}
if (i != nfds - 1 && nfds > 1) {
memcpy(fds + i, fds + nfds - 1, sizeof(struct pollfd));
}
nfds--;
break;
case FDARR_CLEAN_UP:
free(fds);
break;
}
va_end(args);
return err;
}
static int json_response(struct response* response, enum JSON_CMD cmd) {
int bytesWritten = 0;
char buf[BUF_LEN];
response->type = RESPONSE_OK;
response->contentType = "application/json";
switch(cmd) {
case JSON_FORTUNE:
debug(DEBUG_INFO, "JSON: fortune\n");
return HTTP_FORTUNE;
case JSON_IMPLEMENTED:
debug(DEBUG_INFO, "JSON: implemented.json\n");
bytesWritten = sprintf(buf, "%s", BODY_JSON_IMPLEMENTED);
break;
case JSON_ABOUT:
debug(DEBUG_INFO, "JSON: about.json\n");
bytesWritten = sprintf(buf, "%s", BODY_JSON_ABOUT);
break;
}
response->contentLength = bytesWritten;
make_response_header(response);
memcpy(response->buffer.data + response->headerLength, buf, bytesWritten);
response->buffer.bytesUsed += bytesWritten;
response->buffer.bytesToWrite = response->buffer.bytesUsed;
return INTERNAL_RESPONSE;
}
//an error response is a response that's not the normal HTTP 200 OK
static int error_response(struct response* response, enum RESPONSE_TYPE type) {
static const char* responseType2Desc[NUM_HTTP_RESPONSE_TYPES] = {
"",
"The request could not be understood by this server.",
"The method is not allowed for the requested url.",
"",
"Page not found.",
"The request could not be completed due to "
"encountering an internal error"
};
char buf[BUF_LEN];
int bytesWritten = 0;
const char* title = responseType2String[type];
debug(DEBUG_INFO, "Sending %s\n", title);
bytesWritten = snprintf(buf, BUF_LEN, ERROR_BODY, title, title,
responseType2Desc[type]);
response->contentLength = bytesWritten;
response->contentType = "text/html";
response->type = type;
make_response_header(response);
memcpy(response->buffer.data + response->headerLength, buf, bytesWritten);
response->buffer.bytesUsed += bytesWritten;
response->buffer.bytesToWrite = response->buffer.bytesUsed;
return INTERNAL_RESPONSE;
}
static int cgi_response(struct response* response, enum CGI_CMD cmd) {
char buf[BUF_LEN];
time_t t = time(NULL);
int bytesWritten = 0;
if (cmd == CGI_DO) {
debug(DEBUG_INFO, "Sending CGI response line\n");
bytesWritten = snprintf(response->buffer.data, BUF_LEN, "HTTP/1.1 200 OK\r\n");
response->buffer.bytesToWrite = response->buffer.bytesUsed =
response->headerLength = bytesWritten;
return HTTP_CGI;
}
debug(DEBUG_INFO, "Sending CGI status message\n");
bytesWritten = sprintf(
buf, "%sServer Process ID: %d<BR>\nCurrent Time: %s%s",
BODY_STATUS_BEGIN, getpid(), asctime(localtime(&t)), BODY_STATUS_END);
response->contentLength = bytesWritten;
response->contentType = "text/html";
response->type = RESPONSE_OK;
make_response_header(response);
memcpy(response->buffer.data + response->headerLength, buf, bytesWritten);
response->buffer.bytesUsed += bytesWritten;
response->buffer.bytesToWrite = response->buffer.bytesUsed;
return INTERNAL_RESPONSE;
}
static void make_response_header(struct response* response) {
struct buf* buf = &response->buffer;
int written;
written = snprintf(buf->data, BUF_LEN, "HTTP/1.1 %s\r\n",
responseType2String[response->type]);
if (response->contentType) {
written += snprintf(buf->data + written, BUF_LEN - written,
"Content-Type: %s\r\n", response->contentType);
}
if (response->noKeepAlive) {
written += snprintf(buf->data + written, BUF_LEN - written,
"Connection: Close\r\n");
}
if (response->contentLength >= 0) {
written += snprintf(buf->data + written, BUF_LEN - written,
"Content-Length: %d\r\n", response->contentLength);
}
written += snprintf(buf->data + written, BUF_LEN - written, "\r\n");
buf->bytesUsed = buf->bytesToWrite = response->headerLength = written;
}
static int generate_listing(char* filepath, struct response* response) {
char* buf;
int bytesWritten = 0, numDirEntries = 0;
DIR* parent;
char* str = (char *) calloc(1, strlen(filepath) - strlen("docs") + 1);
struct dirent* entry;
debug(DEBUG_INFO, "In generate_listing\n");
if (str == NULL) {
debug(DEBUG_WARNING, "Allocating filepath copy failed\n");
return error_response(response, RESPONSE_INTERNAL_ERROR);
}
memcpy(str, filepath + strlen("docs"),
strlen(filepath) - strlen("docs") + 1);
*(strstr(str, "/index.html")) = '\0';
*(strstr(filepath, "/index.html")) = '\0';
parent = opendir(filepath);
while ((entry = readdir(parent))) {
numDirEntries++;
}
closedir(parent);
buf = (char *) calloc(1, AVG_LISTING_LEN * numDirEntries);
if (buf == NULL) {
debug(DEBUG_WARNING, "Allocating generate listing buffer failed.\n");
free(str);
return error_response(response, RESPONSE_INTERNAL_ERROR);
}
//bufLen = AVG_LISTING_LEN * numDirEntries; //why is this commented out??
bytesWritten = sprintf(buf, "%s", BODY_LISTING_BEGIN);
parent = opendir(filepath);
while ((entry = readdir(parent))) {
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
bytesWritten += sprintf(buf + bytesWritten,
"<LI><A HREF=\"%s/%s\">%s</A></LI>\n",
str, entry->d_name, entry->d_name);
}
}
closedir(parent);
bytesWritten += sprintf(buf + bytesWritten, "%s", BODY_LISTING_END);
response->contentType = "text/html";
response->type = RESPONSE_OK;
response->contentLength = bytesWritten;
make_response_header(response);
memcpy(response->buffer.data + response->headerLength, buf, bytesWritten);
response->buffer.bytesUsed += bytesWritten;
response->buffer.bytesToWrite = response->buffer.bytesUsed;
free(str);
free(buf);
return INTERNAL_RESPONSE;
}
//parse the request declaration
//will modify the string in the request buffer via strtok!
static enum RESPONSE_TYPE parse_request_declaration(struct request* request,
char** filepath) {
int i;
char* tok;
char* toks[NUM_REQ_DECL_PARTS];
//separate out the request declaration into tokens
for (i = 0, tok = strtok(request->buffer, " \n");
tok;
i++, tok = strtok(NULL, " \n")) {
if (i > NUM_REQ_DECL_PARTS - 1) {
debug(DEBUG_INFO, "Invalid HTTP request declaration\n");
return RESPONSE_BAD_REQUEST;
}
toks[i] = tok;
}
if (strcasestr(toks[0], "GET")) {
request->type = REQUEST_GET;
} else {
debug(DEBUG_INFO, "Method not supported: %s\n", toks[0]);
return RESPONSE_METHOD_NOT_ALLOWED;
}
*filepath = toks[1];
//the second part of the declaration says what HTTP version we're using
if (strcasestr(toks[2], "HTTP/1.1")) {
request->httpVersion = 1;
} else if (strcasestr(toks[2], "HTTP/1.0")) {
request->httpVersion = 0;
} else if (strcasestr(toks[2], "HTTP/0.9")) {
request->httpVersion = -1;
} else {
debug(DEBUG_INFO, "Unsupported HTTP protocol version (!).\n");
return RESPONSE_BAD_REQUEST;
}
return RESPONSE_OK;
}
//the return value of the function indicates whether there were any errors
//all modification is done through pointers
static enum RESPONSE_TYPE make_request_header(struct request* request) {
char* lineEnd, * tok, * tmp;
char* lines[MAX_TOKS];
int i, numToks;
enum RESPONSE_TYPE res;
//find the end of the first line of the request
if ((lineEnd = (char *) memmem(request->buffer, request->bytesUsed,
"\r\n", 2)) == NULL) {
debug(DEBUG_INFO, "Can't find HTTP request declaration\n");
return RESPONSE_BAD_REQUEST;
}
*lineEnd = *(lineEnd + 1) = '\0';
if ((res = parse_request_declaration(request, &tmp)) != RESPONSE_OK) {
return res;
}
//split the remainder of the request up
for (i = 0, tok = strtok(lineEnd + 2, "\r\n");
tok;
i++, tok = strtok(NULL, "\r\n")) {
if (i >= MAX_TOKS) {
debug(DEBUG_WARNING, "Overflowed header lines buffer!\n");
return RESPONSE_INTERNAL_ERROR;
}
lines[i] = tok;
}
numToks = i;
request->referer = "";
request->userAgent = "";
for (i = 0; i < numToks; i++) {
if (strcasecmp(lines[i], "Connection: Close") == 0) {
request->noKeepAlive = true;
} else if (strcasestr(lines[i], "Accept-Encoding:")) {
if (strcasestr(lines[i] + strlen("Accept-Encoding:"), "deflate")) {
request->acceptDeflate = true;
}
} else if (strcasestr(lines[i], "Referer:")) {
request->referer = lines[i] + strlen("Referer: ");
} else if (strcasestr(lines[i], "User-Agent:")) {
request->userAgent = lines[i] + strlen("User Agent: ");
}
}
if (url_decode(tmp) == POSIX_ERROR) {
return RESPONSE_INTERNAL_ERROR;
}
snprintf(request->filepath, NAME_BUF_LEN, "docs%s", tmp);
return RESPONSE_OK;
}
static inline const char* bool_to_string(bool b) {
return b ? "true" : "false";
}
static char* request_declaration_to_string(const struct request* request) {
static char str[NAME_BUF_LEN];
snprintf(str, NAME_BUF_LEN, "%s %s HTTP/%.1lf",
requestType2String[request->type],
request->filepath + strlen("docs"),
request->httpVersion / 10.0 + 1.0);
return str;
}
//make this into a to_string method some time in the future
static void print_request(struct request* request) {
debug(DEBUG_INFO, "Request: %s\n", request_declaration_to_string(request));
debug(DEBUG_INFO, "|-HTTP-Option Referrer: %s\n", request->referer);
debug(DEBUG_INFO, "|-HTTP-Option User-Agent: %s\n", request->userAgent);
debug(DEBUG_INFO, "|-HTTP-Option `Connection: Close`?: %s\n",
bool_to_string(request->noKeepAlive));
debug(DEBUG_INFO, "|-HTTP-Option `Accept-Encoding: deflate`?: %s\n",
bool_to_string(request->acceptDeflate));
}
// Quick and dirty JSON string escaping function. Improve Later.
// As written may could easily create a buffer overflow error
static int fortune_decode(struct buf* buf) {
// extra counts the number of extra bytes we have from
// escaping the string characters
int extra = 0;
char* tmp = (char *) malloc(buf->bytesUsed);
char* tmpPtr = tmp, * bufPtr = buf->data;
if (tmp == NULL) {
return POSIX_ERROR;
}
memcpy(tmp, buf->data, buf->bytesUsed);
while (tmpPtr - tmp < buf->bytesUsed && extra < BUF_LEN - buf->bytesUsed) {
if (*tmpPtr == '"') {
extra++;
*bufPtr++ = '\\';
}
*bufPtr++ = *tmpPtr++;
}
buf->bytesUsed += extra;
buf->bytesToWrite = buf->bytesUsed;
free(tmp);
if (buf->bytesUsed >= BUF_LEN) {
debug(DEBUG_WARNING, "Buffer overflow in fortune_decode\n");
return POSIX_ERROR;
}
return 0;
}
// There will be no buffer overflow errors in this function because decoding
// the hex codes for the invalid URL chars, e.g. `%20` -> ' ' always results
// in fewer characters.
static int url_decode(char* url) {
int len = strlen(url);
char* tmp = (char *) malloc(len + 1);
char hexBuf[3];
int hexValue;
char* urlPtr = url, * tmpPtr = tmp;
if (tmp == NULL) {
return POSIX_ERROR;
}
hexBuf[2] = '\0';
while (urlPtr - url < len) {
*tmpPtr = *urlPtr;
if (*urlPtr == '%') {
hexBuf[0] = urlPtr[1];
hexBuf[1] = urlPtr[2];
hexValue = strtol(hexBuf, NULL, 16);
*tmpPtr = hexValue;
urlPtr += 2;
}