-
Notifications
You must be signed in to change notification settings - Fork 14
/
http.c
1227 lines (1033 loc) · 33.8 KB
/
http.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
/* darkstat 3
* copyright (c) 2001-2016 Emil Mikulic.
*
* http.c: embedded webserver.
* This borrows a lot of code from darkhttpd.
*
* You may use, modify and redistribute this file under the terms of the
* GNU General Public License version 2. (see COPYING.GPL)
*/
#include "cdefs.h"
#include "config.h"
#include "conv.h"
#include "err.h"
#include "graph_db.h"
#include "hosts_db.h"
#include "http.h"
#include "now.h"
#include "queue.h"
#include "str.h"
#include <sys/uio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <zlib.h>
static char *http_base_url = NULL;
static int http_base_len = 0;
static const char mime_type_xml[] = "text/xml";
static const char mime_type_html[] = "text/html; charset=us-ascii";
static const char mime_type_text_prometheus[] = "text/plain; version=0.0.4";
static const char mime_type_css[] = "text/css";
static const char mime_type_js[] = "text/javascript";
static const char mime_type_png[] = "image/png";
static const char encoding_identity[] = "identity";
static const char encoding_gzip[] = "gzip";
static const char server[] = PACKAGE_NAME "/" PACKAGE_VERSION;
static int idletime = 60;
#define MAX_REQUEST_LENGTH 4000
static int *insocks = NULL;
static unsigned int insock_num = 0;
struct connection {
LIST_ENTRY(connection) entries;
int socket;
struct sockaddr_storage client;
time_t last_active_mono;
enum {
RECV_REQUEST, /* receiving request */
SEND_HEADER_AND_REPLY, /* try to send header+reply together */
SEND_HEADER, /* sending generated header */
SEND_REPLY, /* sending reply */
DONE /* conn closed, need to remove from queue */
} state;
/* char request[request_length+1] is null-terminated */
char *request;
size_t request_length;
int accept_gzip;
/* request fields */
char *method, *uri, *query; /* query can be NULL */
char *header;
const char *mime_type, *encoding, *header_extra;
size_t header_length, header_sent;
int header_dont_free, header_only, http_code;
char *reply;
int reply_dont_free;
size_t reply_length, reply_sent;
unsigned int total_sent; /* header + body = total, for logging */
};
static LIST_HEAD(conn_list_head, connection) connlist =
LIST_HEAD_INITIALIZER(conn_list_head);
struct bindaddr_entry {
STAILQ_ENTRY(bindaddr_entry) entries;
const char *s;
};
static STAILQ_HEAD(bindaddrs_head, bindaddr_entry) bindaddrs =
STAILQ_HEAD_INITIALIZER(bindaddrs);
/* ---------------------------------------------------------------------------
* Decode URL by converting %XX (where XX are hexadecimal digits) to the
* character it represents. Don't forget to free the return value.
*/
static char *urldecode(const char *url)
{
size_t i, len = strlen(url);
char *out = xmalloc(len+1);
int pos;
for (i=0, pos=0; i<len; i++)
{
if (url[i] == '%' && i+2 < len &&
isxdigit(url[i+1]) && isxdigit(url[i+2]))
{
/* decode %XX */
#define HEX_TO_DIGIT(hex) ( \
((hex) >= 'A' && (hex) <= 'F') ? ((hex)-'A'+10): \
((hex) >= 'a' && (hex) <= 'f') ? ((hex)-'a'+10): \
((hex)-'0') )
out[pos++] = HEX_TO_DIGIT(url[i+1]) * 16 +
HEX_TO_DIGIT(url[i+2]);
i += 2;
#undef HEX_TO_DIGIT
}
else
{
/* straight copy */
out[pos++] = url[i];
}
}
out[pos] = 0;
#if 0
/* don't really need to realloc here - it's probably a performance hit */
out = xrealloc(out, strlen(out)+1); /* dealloc what we don't need */
#endif
return (out);
}
/* ---------------------------------------------------------------------------
* Consolidate slashes in-place by shifting parts of the string over repeated
* slashes.
*/
static void consolidate_slashes(char *s)
{
size_t left = 0, right = 0;
int saw_slash = 0;
assert(s != NULL);
while (s[right] != '\0')
{
if (saw_slash)
{
if (s[right] == '/') right++;
else
{
saw_slash = 0;
s[left++] = s[right++];
}
}
else
{
if (s[right] == '/') saw_slash++;
s[left++] = s[right++];
}
}
s[left] = '\0';
}
/* ---------------------------------------------------------------------------
* Resolve /./ and /../ in a URI, returing a new, safe URI, or NULL if the URI
* is invalid/unsafe. Returned buffer needs to be deallocated.
*/
static char *make_safe_uri(char *uri)
{
char **elem, *out;
unsigned int slashes = 0, elements = 0;
size_t urilen, i, j, pos;
assert(uri != NULL);
if (uri[0] != '/')
return (NULL);
consolidate_slashes(uri);
urilen = strlen(uri);
/* count the slashes */
for (i=0, slashes=0; i<urilen; i++)
if (uri[i] == '/') slashes++;
/* make an array for the URI elements */
elem = xmalloc(sizeof(*elem) * slashes);
for (i=0; i<slashes; i++)
elem[i] = (NULL);
/* split by slashes and build elem[] array */
for (i=1; i<urilen;)
{
/* look for the next slash */
for (j=i; j<urilen && uri[j] != '/'; j++)
;
/* process uri[i,j) */
if ((j == i+1) && (uri[i] == '.'))
/* "." */;
else if ((j == i+2) && (uri[i] == '.') && (uri[i+1] == '.'))
{
/* ".." */
if (elements == 0)
{
/*
* Unsafe string so free elem[]. All its elements are free
* at this point.
*/
free(elem);
return (NULL);
}
else
{
elements--;
free(elem[elements]);
}
}
else elem[elements++] = split_string(uri, i, j);
i = j + 1; /* uri[j] is a slash - move along one */
}
/* reassemble */
out = xmalloc(urilen+1); /* it won't expand */
pos = 0;
for (i=0; i<elements; i++)
{
size_t delta = strlen(elem[i]);
assert(pos <= urilen);
out[pos++] = '/';
assert(pos+delta <= urilen);
memcpy(out+pos, elem[i], delta);
free(elem[i]);
pos += delta;
}
free(elem);
if ((elements == 0) || (uri[urilen-1] == '/')) out[pos++] = '/';
assert(pos <= urilen);
out[pos] = '\0';
#if 0
/* don't really need to do this and it's probably a performance hit: */
/* shorten buffer if necessary */
if (pos != urilen) out = xrealloc(out, strlen(out)+1);
#endif
return (out);
}
/* ---------------------------------------------------------------------------
* Allocate and initialize an empty connection.
*/
static struct connection *new_connection(void)
{
struct connection *conn = xmalloc(sizeof(*conn));
conn->socket = -1;
memset(&conn->client, 0, sizeof(conn->client));
conn->last_active_mono = now_mono();
conn->request = NULL;
conn->request_length = 0;
conn->accept_gzip = 0;
conn->method = NULL;
conn->uri = NULL;
conn->query = NULL;
conn->header = NULL;
conn->mime_type = NULL;
conn->encoding = NULL;
conn->header_extra = "";
conn->header_length = 0;
conn->header_sent = 0;
conn->header_dont_free = 0;
conn->header_only = 0;
conn->http_code = 0;
conn->reply = NULL;
conn->reply_dont_free = 0;
conn->reply_length = 0;
conn->reply_sent = 0;
conn->total_sent = 0;
/* Make it harmless so it gets garbage-collected if it should, for some
* reason, fail to be correctly filled out.
*/
conn->state = DONE;
return (conn);
}
/* ---------------------------------------------------------------------------
* Accept a connection from sockin and add it to the connection queue.
*/
static void accept_connection(const int sockin)
{
struct sockaddr_storage addrin;
socklen_t sin_size;
struct connection *conn;
char ipaddr[INET6_ADDRSTRLEN], portstr[12];
int sock;
sin_size = (socklen_t)sizeof(addrin);
sock = accept(sockin, (struct sockaddr *)&addrin, &sin_size);
if (sock == -1)
{
if (errno == ECONNABORTED || errno == EINTR)
{
verbosef("accept() failed: %s", strerror(errno));
return;
}
/* else */ err(1, "accept()");
}
fd_set_nonblock(sock);
/* allocate and initialise struct connection */
conn = new_connection();
conn->socket = sock;
conn->state = RECV_REQUEST;
memcpy(&conn->client, &addrin, sizeof(conn->client));
LIST_INSERT_HEAD(&connlist, conn, entries);
getnameinfo((struct sockaddr *) &addrin, sin_size,
ipaddr, sizeof(ipaddr), portstr, sizeof(portstr),
NI_NUMERICHOST | NI_NUMERICSERV);
verbosef("accepted connection from %s:%s", ipaddr, portstr);
}
/* ---------------------------------------------------------------------------
* Log a connection, then cleanly deallocate its internals.
*/
static void free_connection(struct connection *conn)
{
dverbosef("free_connection(%d)", conn->socket);
if (conn->socket != -1)
close(conn->socket);
free(conn->request);
free(conn->method);
free(conn->uri);
free(conn->query);
if (!conn->header_dont_free)
free(conn->header);
if (!conn->reply_dont_free)
free(conn->reply);
}
/* ---------------------------------------------------------------------------
* Format [when] as an RFC1123 date, stored in the specified buffer. The same
* buffer is returned for convenience.
*/
#define DATE_LEN 30 /* strlen("Fri, 28 Feb 2003 00:02:08 GMT")+1 */
static char *rfc1123_date(char *dest, time_t when) {
if (strftime(dest, DATE_LEN,
"%a, %d %b %Y %H:%M:%S %Z", gmtime(&when) ) == 0)
errx(1, "strftime() failed [%s]", dest);
return dest;
}
static void generate_header(struct connection *conn,
const int code, const char *text)
{
char date[DATE_LEN];
assert(conn->header == NULL);
assert(conn->mime_type != NULL);
if (conn->encoding == NULL)
conn->encoding = encoding_identity;
verbosef("http: %d %s (%s: %zu bytes)",
code,
text,
conn->encoding,
conn->reply_length);
conn->header_length = xasprintf(&(conn->header),
"HTTP/1.1 %d %s\r\n"
"Date: %s\r\n"
"Server: %s\r\n"
"Vary: Accept-Encoding\r\n"
"Content-Type: %s\r\n"
"Content-Length: %qu\r\n"
"Content-Encoding: %s\r\n"
"X-Robots-Tag: noindex, noarchive\r\n"
"%s"
"\r\n",
code, text,
rfc1123_date(date, now_real()),
server,
conn->mime_type,
(qu)conn->reply_length,
conn->encoding,
conn->header_extra);
conn->http_code = code;
}
/* ---------------------------------------------------------------------------
* A default reply for any (erroneous) occasion.
*/
static void default_reply(struct connection *conn,
const int errcode, const char *errname, const char *format, ...)
_printflike_(4, 5);
static void default_reply(struct connection *conn,
const int errcode, const char *errname, const char *format, ...)
{
char *reason;
va_list va;
va_start(va, format);
xvasprintf(&reason, format, va);
va_end(va);
conn->reply_length = xasprintf(&(conn->reply),
"<html><head><title>%d %s</title></head><body>\n"
"<h1>%s</h1>\n" /* errname */
"%s\n" /* reason */
"<hr>\n"
"Generated by %s"
"</body></html>\n",
errcode, errname, errname, reason, server);
free(reason);
/* forget any dangling metadata */
conn->mime_type = mime_type_html;
conn->encoding = encoding_identity;
generate_header(conn, errcode, errname);
}
/* ---------------------------------------------------------------------------
* Parses a single HTTP request field. Returns string from end of [field] to
* first \r, \n or end of request string. Returns NULL if [field] can't be
* matched.
*
* You need to remember to deallocate the result.
* example: parse_field(conn, "Referer: ");
*/
static char *parse_field(const struct connection *conn, const char *field)
{
size_t bound1, bound2;
char *pos;
/* find start */
pos = strstr(conn->request, field);
if (pos == NULL)
return (NULL);
bound1 = pos - conn->request + strlen(field);
/* find end */
for (bound2 = bound1;
bound2 < conn->request_length &&
conn->request[bound2] != '\r'; bound2++)
;
/* copy to buffer */
return (split_string(conn->request, bound1, bound2));
}
/* ---------------------------------------------------------------------------
* Parse an HTTP request like "GET /hosts/?sort=in HTTP/1.1" to get the method
* (GET), the uri (/hosts/), the query (sort=in) and whether the UA will
* accept gzip encoding. Remember to deallocate all these buffers. Query
* can be NULL. The method will be returned in uppercase.
*/
static int parse_request(struct connection *conn)
{
size_t bound1, bound2, mid;
char *accept_enc;
/* parse method */
for (bound1 = 0; bound1 < conn->request_length &&
conn->request[bound1] != ' '; bound1++)
;
conn->method = split_string(conn->request, 0, bound1);
strntoupper(conn->method, bound1);
/* parse uri */
for (; bound1 < conn->request_length &&
conn->request[bound1] == ' '; bound1++)
;
if (bound1 == conn->request_length)
return (0); /* fail */
for (bound2=bound1+1; bound2 < conn->request_length &&
conn->request[bound2] != ' ' &&
conn->request[bound2] != '\r'; bound2++)
;
/* find query string */
for (mid=bound1; mid<bound2 && conn->request[mid] != '?'; mid++)
;
if (conn->request[mid] == '?') {
conn->query = split_string(conn->request, mid+1, bound2);
bound2 = mid;
}
conn->uri = split_string(conn->request, bound1, bound2);
/* parse important fields */
accept_enc = parse_field(conn, "Accept-Encoding: ");
if (accept_enc != NULL) {
if (strstr(accept_enc, "gzip") != NULL)
conn->accept_gzip = 1;
free(accept_enc);
}
return (1);
}
/* FIXME: maybe we need a smarter way of doing static pages: */
/* ---------------------------------------------------------------------------
* Web interface: static stylesheet.
*/
static void
static_style_css(struct connection *conn)
{
#include "stylecss.h"
conn->reply = (char*)style_css;
conn->reply_length = style_css_len;
conn->reply_dont_free = 1;
conn->mime_type = mime_type_css;
}
/* ---------------------------------------------------------------------------
* Web interface: static JavaScript.
*/
static void
static_graph_js(struct connection *conn)
{
#include "graphjs.h"
conn->reply = (char*)graph_js;
conn->reply_length = graph_js_len;
conn->reply_dont_free = 1;
conn->mime_type = mime_type_js;
}
/* ---------------------------------------------------------------------------
* Web interface: favicon.
*/
static void
static_favicon(struct connection *conn)
{
#include "favicon.h"
conn->reply = (char*)favicon_png;
conn->reply_length = sizeof(favicon_png);
conn->reply_dont_free = 1;
conn->mime_type = mime_type_png;
}
/* ---------------------------------------------------------------------------
* gzip a reply, if requested and possible. Don't bother with a minimum
* length requirement, I've never seen a page fail to compress.
*/
static void
process_gzip(struct connection *conn)
{
char *buf;
size_t len;
z_stream zs;
if (!conn->accept_gzip)
return;
buf = xmalloc(conn->reply_length);
len = conn->reply_length;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
if (deflateInit2(&zs,
Z_BEST_COMPRESSION,
Z_DEFLATED,
15+16, /* 15 = biggest window,
16 = add gzip header+trailer */
8 /* default */,
Z_DEFAULT_STRATEGY) != Z_OK) {
free(buf);
return;
}
zs.avail_in = conn->reply_length;
zs.next_in = (unsigned char *)conn->reply;
zs.avail_out = conn->reply_length;
zs.next_out = (unsigned char *)buf;
if (deflate(&zs, Z_FINISH) != Z_STREAM_END) {
deflateEnd(&zs);
free(buf);
verbosef("failed to compress %zu bytes", len);
return;
}
if (conn->reply_dont_free)
conn->reply_dont_free = 0;
else
free(conn->reply);
conn->reply = buf;
conn->reply_length -= zs.avail_out;
conn->encoding = encoding_gzip;
deflateEnd(&zs);
}
/* ---------------------------------------------------------------------------
* Process a GET/HEAD request
*/
static void process_get(struct connection *conn)
{
char *safe_url;
verbosef("http: %s \"%s\" %s", conn->method, conn->uri,
(conn->query == NULL)?"":conn->query);
{
/* Decode the URL being requested. */
char *decoded_url;
char *decoded_url_offset;
decoded_url = urldecode(conn->uri);
/* Optionally strip the base. */
decoded_url_offset = decoded_url;
if (str_starts_with(decoded_url, http_base_url)) {
decoded_url_offset += http_base_len - 1;
}
/* Make sure it's safe. */
safe_url = make_safe_uri(decoded_url_offset);
free(decoded_url);
if (safe_url == NULL) {
default_reply(conn, 400, "Bad Request",
"You requested an invalid URI: %s", conn->uri);
return;
}
}
if (strcmp(safe_url, "/") == 0) {
struct str *buf = html_front_page();
str_extract(buf, &(conn->reply_length), &(conn->reply));
conn->mime_type = mime_type_html;
}
else if (str_starts_with(safe_url, "/hosts/")) {
/* FIXME here - make this saner */
struct str *buf = html_hosts(safe_url, conn->query);
if (buf == NULL) {
default_reply(conn, 404, "Not Found",
"The page you requested could not be found.");
free(safe_url);
return;
}
str_extract(buf, &(conn->reply_length), &(conn->reply));
conn->mime_type = mime_type_html;
}
else if (str_starts_with(safe_url, "/graphs.xml")) {
struct str *buf = xml_graphs();
str_extract(buf, &(conn->reply_length), &(conn->reply));
conn->mime_type = mime_type_xml;
/* hack around Opera caching the XML */
conn->header_extra = "Pragma: no-cache\r\n";
}
else if (str_starts_with(safe_url, "/metrics")) {
struct str *buf = text_metrics();
str_extract(buf, &(conn->reply_length), &(conn->reply));
conn->mime_type = mime_type_text_prometheus;
}
else if (strcmp(safe_url, "/style.css") == 0)
static_style_css(conn);
else if (strcmp(safe_url, "/graph.js") == 0)
static_graph_js(conn);
else if (strcmp(safe_url, "/favicon.ico") == 0) {
/* serves a PNG instead of an ICO, might cause problems for IE6 */
static_favicon(conn);
} else {
default_reply(conn, 404, "Not Found",
"The page you requested could not be found.");
free(safe_url);
return;
}
free(safe_url);
process_gzip(conn);
assert(conn->mime_type != NULL);
generate_header(conn, 200, "OK");
}
/* ---------------------------------------------------------------------------
* Process a request: build the header and reply, advance state.
*/
static void process_request(struct connection *conn)
{
if (!parse_request(conn))
{
default_reply(conn, 400, "Bad Request",
"You sent a request that the server couldn't understand.");
}
else if (strcmp(conn->method, "GET") == 0)
{
process_get(conn);
}
else if (strcmp(conn->method, "HEAD") == 0)
{
process_get(conn);
conn->header_only = 1;
}
else
{
default_reply(conn, 501, "Not Implemented",
"The method you specified (%s) is not implemented.",
conn->method);
}
/* advance state */
if (conn->header_only)
conn->state = SEND_HEADER;
else
conn->state = SEND_HEADER_AND_REPLY;
}
/* ---------------------------------------------------------------------------
* Receiving request.
*/
static void poll_recv_request(struct connection *conn)
{
char buf[65536];
ssize_t recvd;
recvd = recv(conn->socket, buf, sizeof(buf), 0);
dverbosef("poll_recv_request(%d) got %d bytes", conn->socket, (int)recvd);
if (recvd <= 0)
{
if (recvd == -1)
verbosef("recv(%d) error: %s", conn->socket, strerror(errno));
conn->state = DONE;
return;
}
conn->last_active_mono = now_mono();
/* append to conn->request */
conn->request = xrealloc(conn->request, conn->request_length+recvd+1);
memcpy(conn->request+conn->request_length, buf, (size_t)recvd);
conn->request_length += recvd;
conn->request[conn->request_length] = 0;
/* die if it's too long */
if (conn->request_length > MAX_REQUEST_LENGTH)
{
default_reply(conn, 413, "Request Entity Too Large",
"Your request was dropped because it was too long.");
conn->state = SEND_HEADER;
return;
}
/* process request if we have all of it */
if (conn->request_length > 4 &&
memcmp(conn->request+conn->request_length-4, "\r\n\r\n", 4) == 0)
{
process_request(conn);
/* request not needed anymore */
free(conn->request);
conn->request = NULL; /* important: don't free it again later */
}
}
/* ---------------------------------------------------------------------------
* Try to send header and [a part of the] reply in one packet.
*/
static void poll_send_header_and_reply(struct connection *conn)
{
ssize_t sent;
struct iovec iov[2];
assert(!conn->header_only);
assert(conn->reply_length > 0);
assert(conn->header_sent == 0);
assert(conn->reply_sent == 0);
/* Fill out iovec */
iov[0].iov_base = conn->header;
iov[0].iov_len = conn->header_length;
iov[1].iov_base = conn->reply;
iov[1].iov_len = conn->reply_length;
sent = writev(conn->socket, iov, 2);
conn->last_active_mono = now_mono();
/* handle any errors (-1) or closure (0) in send() */
if (sent < 1) {
if (sent == -1)
verbosef("writev(%d) error: %s", conn->socket, strerror(errno));
conn->state = DONE;
return;
}
/* Figure out what we've sent. */
conn->total_sent += (unsigned int)sent;
if (sent < (ssize_t)conn->header_length) {
verbosef("partially sent header");
conn->header_sent = sent;
conn->state = SEND_HEADER;
return;
}
/* else */
conn->header_sent = conn->header_length;
sent -= conn->header_length;
if (sent < (ssize_t)conn->reply_length) {
verbosef("partially sent reply");
conn->reply_sent += sent;
conn->state = SEND_REPLY;
return;
}
/* else */
conn->reply_sent = conn->reply_length;
conn->state = DONE;
}
/* ---------------------------------------------------------------------------
* Sending header. Assumes conn->header is not NULL.
*/
static void poll_send_header(struct connection *conn)
{
ssize_t sent;
sent = send(conn->socket, conn->header + conn->header_sent,
conn->header_length - conn->header_sent, 0);
conn->last_active_mono = now_mono();
dverbosef("poll_send_header(%d) sent %d bytes", conn->socket, (int)sent);
/* handle any errors (-1) or closure (0) in send() */
if (sent < 1)
{
if (sent == -1)
verbosef("send(%d) error: %s", conn->socket, strerror(errno));
conn->state = DONE;
return;
}
conn->header_sent += (unsigned int)sent;
conn->total_sent += (unsigned int)sent;
/* check if we're done sending */
if (conn->header_sent == conn->header_length)
{
if (conn->header_only)
conn->state = DONE;
else
conn->state = SEND_REPLY;
}
}
/* ---------------------------------------------------------------------------
* Sending reply.
*/
static void poll_send_reply(struct connection *conn)
{
ssize_t sent;
sent = send(conn->socket,
conn->reply + conn->reply_sent,
conn->reply_length - conn->reply_sent, 0);
conn->last_active_mono = now_mono();
dverbosef("poll_send_reply(%d) sent %d: [%d-%d] of %d",
conn->socket, (int)sent,
(int)conn->reply_sent,
(int)(conn->reply_sent + sent - 1),
(int)conn->reply_length);
/* handle any errors (-1) or closure (0) in send() */
if (sent < 1)
{
if (sent == -1)
verbosef("send(%d) error: %s", conn->socket, strerror(errno));
else if (sent == 0)
verbosef("send(%d) closure", conn->socket);
conn->state = DONE;
return;
}
conn->reply_sent += (unsigned int)sent;
conn->total_sent += (unsigned int)sent;
/* check if we're done sending */
if (conn->reply_sent == conn->reply_length) conn->state = DONE;
}
/* --------------------------------------------------------------------------
* Initialize the base url.
*/
void http_init_base(const char *url) {
char *slashed_url, *safe_url;
size_t urllen;
if (url == NULL) {
http_base_url = strdup("/");
} else {
/* Make sure that the url has leading and trailing slashes. */
urllen = strlen(url);
slashed_url = xmalloc(urllen+3);
slashed_url[0] = '/';
memcpy(slashed_url+1, url, urllen); /* don't copy NUL */
slashed_url[urllen+1] = '/';
slashed_url[urllen+2] = '\0';
/* Clean the url. */
safe_url = make_safe_uri(slashed_url);
free(slashed_url);
if (safe_url == NULL) {
verbosef("invalid base \"%s\", ignored", url);
http_base_url = strdup("/"); /* set to default */
} else {
http_base_url = safe_url;
}
}
http_base_len = strlen(http_base_url);
verbosef("set base url to \"%s\"", http_base_url);
}
/* Use getaddrinfo to figure out what type of socket to create and
* what to bind it to. "bindaddr" can be NULL. Remember to freeaddrinfo()
* the result.
*/
static struct addrinfo *get_bind_addr(
const char *bindaddr, const unsigned short bindport)
{
struct addrinfo hints, *ai;
char portstr[6];
int ret;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
snprintf(portstr, sizeof(portstr), "%u", bindport);
if ((ret = getaddrinfo(bindaddr, portstr, &hints, &ai)))
err(1, "getaddrinfo(%s, %s) failed: %s",
bindaddr ? bindaddr : "NULL", portstr, gai_strerror(ret));
if (ai == NULL)
err(1, "getaddrinfo() returned NULL pointer");
return ai;
}
void http_add_bindaddr(const char *bindaddr)
{
struct bindaddr_entry *ent;
ent = xmalloc(sizeof(*ent));
ent->s = bindaddr;
STAILQ_INSERT_TAIL(&bindaddrs, ent, entries);
}
static void http_listen_one(struct addrinfo *ai,
const unsigned short bindport)
{
char ipaddr[INET6_ADDRSTRLEN];
int sockin, sockopt, ret;
/* format address into ipaddr string */