-
Notifications
You must be signed in to change notification settings - Fork 6
/
httpconn.c
1352 lines (1144 loc) · 30.3 KB
/
httpconn.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
#include "netheaders.h"
#include <sys/queue.h>
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <event2/util.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include "httpconn.h"
#include "conn.h"
#include "headers.h"
#include "util.h"
#include "log.h"
#define EVENT0(conn, slot) \
(conn)->cbs->slot((conn), (conn)->cbarg)
#define EVENT1(conn, slot, a) \
(conn)->cbs->slot((conn), (a), (conn)->cbarg)
#define EVENT2(conn, slot, a, b) \
(conn)->cbs->slot((conn), (a), (b), (conn)->cbarg)
#define EVENT3(conn, slot, a, b, c) \
(conn)->cbs->slot((conn), (a), (b), (c), (conn)->cbarg)
#define EVENT4(conn, slot, a, b, c, d) \
(conn)->cbs->slot((conn), (a), (b), (c), (d), (conn)->cbarg)
/* max amount of data we can have backlogged on outbuf before choaking */
static size_t max_write_backlog = 50 * 1024;
/* the number of seconds to keep an idle connections hanging around */
static struct timeval idle_client_timeout = {120, 0};
static struct timeval idle_server_timeout = {120, 0};
struct http_conn {
enum http_state state;
enum http_version vers;
enum http_te te;
enum http_type type;
enum http_te output_te;
int choked;
int has_body;
int read_paused;
int tunnel_read_paused;
int msg_complete_on_eof;
int persistent;
int expect_continue;
int will_flush;
int will_free;
const struct http_cbs *cbs;
void *cbarg;
ev_int64_t body_length;
ev_int64_t data_remaining;
char *firstline;
struct header_list *headers;
struct event_base *base;
struct bufferevent *bev;
struct bufferevent *tunnel_bev;
struct evbuffer *inbuf_processed;
};
static int
method_from_string(enum http_method *m, const char *method)
{
if (!evutil_ascii_strcasecmp(method, "GET"))
*m = METH_GET;
else if (!evutil_ascii_strcasecmp(method, "HEAD"))
*m = METH_HEAD;
else if (!evutil_ascii_strcasecmp(method, "POST"))
*m = METH_POST;
else if (!evutil_ascii_strcasecmp(method, "PUT"))
*m = METH_PUT;
else if (!evutil_ascii_strcasecmp(method, "CONNECT"))
*m = METH_CONNECT;
else {
log_warn("method_from_string: unknown method, '%s'", method);
return -1;
}
return 0;
}
const char *
http_method_to_string(enum http_method m)
{
switch (m) {
case METH_GET:
return "GET";
case METH_HEAD:
return "HEAD";
case METH_POST:
return "POST";
case METH_PUT:
return "PUT";
case METH_CONNECT:
return "CONNECT";
}
log_fatal("http_method_to_string: unknown method %d", m);
return "???";
}
static int
version_from_string(enum http_version *v, const char *vers)
{
if (evutil_ascii_strncasecmp(vers, "HTTP/", 5)) {
log_warn("version_from_string: bad http-version, '%s'", vers);
return -1;
}
vers += 5;
/* XXX this only understands 1.0 and 1.1 */
if (!strcmp(vers, "1.0"))
*v = HTTP_10;
else if (!strcmp(vers, "1.1"))
*v = HTTP_11;
else {
log_warn("version_from_string: unknown http-version, '%s'",
vers);
return -1;
}
return 0;
}
const char *
http_version_to_string(enum http_version v)
{
switch (v) {
case HTTP_UNKNOWN:
return "HTTP/??";
case HTTP_10:
return "HTTP/1.0";
case HTTP_11:
return "HTTP/1.1";
}
log_fatal("http_version_to_string: unknown version %d", v);
return "???";
}
const char *
http_conn_error_to_string(enum http_conn_error err)
{
switch (err) {
case ERROR_NONE:
return "No error";
case ERROR_CONNECT_FAILED:
return "Connection failed";
case ERROR_IDLE_CONN_TIMEDOUT:
return "Idle connection timed out";
case ERROR_CLIENT_EXPECTATION_FAILED:
return "Can't statisfy client's expectation";
case ERROR_CLIENT_POST_WITHOUT_LENGTH:
return "Client post with unknown length";
case ERROR_INCOMPLETE_HEADERS:
return "Connection terminated while reading headers";
case ERROR_INCOMPLETE_BODY:
return "Connection terminated prematurely while reading body";
case ERROR_HEADER_PARSE_FAILED:
return "Invalid client request";
case ERROR_CHUNK_PARSE_FAILED:
return "Invalid chunked data";
case ERROR_WRITE_FAILED:
return "Write failed";
case ERROR_TUNNEL_CONNECT_FAILED:
return "Tunnel connection failed";
case ERROR_TUNNEL_CLOSED:
return "Tunnel closed";
}
return "???";
}
static void
begin_message(struct http_conn *conn)
{
assert(conn->headers == NULL && conn->firstline == NULL);
conn->headers = mem_calloc(1, sizeof(*conn->headers));
TAILQ_INIT(conn->headers);
conn->state = HTTP_STATE_IDLE;
if (!conn->read_paused)
bufferevent_enable(conn->bev, EV_READ);
// XXX we should have a separate function to tell that server is idle.
if (conn->type == HTTP_SERVER)
bufferevent_set_timeouts(conn->bev, &idle_server_timeout, NULL);
else
bufferevent_set_timeouts(conn->bev, &idle_client_timeout, NULL);
}
static void
end_message(struct http_conn *conn, enum http_conn_error err)
{
if (conn->firstline)
mem_free(conn->firstline);
if (conn->headers) {
headers_clear(conn->headers);
mem_free(conn->headers);
}
conn->firstline = NULL;
conn->headers = NULL;
if (err != ERROR_NONE || !conn->persistent) {
conn->state = HTTP_STATE_MANGLED;
http_conn_stop_reading(conn);
} else
begin_message(conn);
if (err != ERROR_NONE)
EVENT1(conn, on_error, err);
else
EVENT0(conn, on_msg_complete);
}
static struct http_request *
build_request(struct http_conn *conn)
{
struct http_request *req;
struct token_list tokens;
struct token *method, *url, *vers;
enum http_method m;
enum http_version v;
struct url *u = NULL;
size_t ntokens;
assert(conn->type == HTTP_CLIENT);
TAILQ_INIT(&tokens);
req = NULL;
ntokens = tokenize(conn->firstline, " ", 4, &tokens);
if (ntokens != 3)
goto out;
method = TAILQ_FIRST(&tokens);
url = TAILQ_NEXT(method, next);
vers = TAILQ_NEXT(url, next);
if (method_from_string(&m, method->token) < 0 ||
version_from_string(&v, vers->token) < 0)
goto out;
if (m == METH_CONNECT)
u = url_connect_tokenize(url->token);
else
u = url_tokenize(url->token);
if (!u)
goto out;
req = mem_calloc(1, sizeof(*req));
req->meth = m;
req->vers = v;
req->url = u;
u = NULL;
req->headers = conn->headers;
out:
url_free(u);
token_list_clear(&tokens);
return req;
}
static struct http_response *
build_response(struct http_conn *conn)
{
struct http_response *resp;
struct token_list tokens;
struct token *vers, *code, *reason;
enum http_version v;
int c;
size_t ntokens;
assert(conn->type == HTTP_SERVER);
TAILQ_INIT(&tokens);
resp = NULL;
ntokens = tokenize(conn->firstline, " ", 2, &tokens);
if (ntokens != 3)
goto out;
vers = TAILQ_FIRST(&tokens);
code = TAILQ_NEXT(vers, next);
reason = TAILQ_NEXT(code, next);
c = atoi(code->token);
if (version_from_string(&v, vers->token) < 0 || c < 100 || c > 999)
goto out;
resp = mem_calloc(1, sizeof(*resp));
resp->vers = v;
resp->code = c;
resp->reason = reason->token;
reason->token = NULL; /* so token_list_clear will skip this */
resp->headers = conn->headers;
out:
token_list_clear(&tokens);
return resp;
}
/* return -1 failure, 0 incomplete, 1 ok */
static int
parse_chunk_len(struct http_conn *conn)
{
struct evbuffer *inbuf = bufferevent_get_input(conn->bev);
char *line;
ev_int64_t len;
while ((line = evbuffer_readln(inbuf, NULL, EVBUFFER_EOL_CRLF))) {
if (*line == '\0') {
mem_free(line);
continue;
}
len = get_int(line, 16);
mem_free(line);
if (len < 0) {
log_warn("parse_chunk_len: invalid chunk len");
return -1;
}
conn->data_remaining = len;
return 1;
}
return 0;
}
static void
read_chunk(struct http_conn *conn)
{
struct evbuffer *inbuf = bufferevent_get_input(conn->bev);
size_t len;
char *line;
int ret;
while ((len = evbuffer_get_length(inbuf)) > 0) {
if (conn->data_remaining < 0) {
ret = parse_chunk_len(conn);
if (ret <= 0) {
if (ret < 0)
end_message(conn,
ERROR_CHUNK_PARSE_FAILED);
return;
}
} else if (conn->data_remaining == 0) {
line = evbuffer_readln(inbuf, NULL, EVBUFFER_EOL_CRLF);
if (line) {
/* XXX doesn't handle trailers */
mem_free(line);
end_message(conn, ERROR_NONE);
}
return;
} else {
/* XXX should mind potential overflow */
if (len >= (size_t)conn->data_remaining)
len = (size_t)conn->data_remaining;
evbuffer_remove_buffer(inbuf, conn->inbuf_processed,
len);
EVENT1(conn, on_read_body, conn->inbuf_processed);
conn->data_remaining -= len;
if (conn->data_remaining == 0)
conn->data_remaining = -1;
}
}
}
static void
read_body(struct http_conn *conn)
{
struct evbuffer *inbuf = bufferevent_get_input(conn->bev);
size_t len;
assert(conn->has_body);
if (conn->te == TE_CHUNKED) {
read_chunk(conn);
return;
}
len = evbuffer_get_length(inbuf);
if (len) {
/* XXX should mind potential overflow */
if (conn->data_remaining >= 0 &&
len > (size_t)conn->data_remaining) {
len = (size_t)conn->data_remaining;
evbuffer_remove_buffer(inbuf, conn->inbuf_processed,
len);
EVENT1(conn, on_read_body, conn->inbuf_processed);
} else {
evbuffer_add_buffer(conn->inbuf_processed, inbuf);
EVENT1(conn, on_read_body, conn->inbuf_processed);
}
conn->data_remaining -= len;
if (conn->data_remaining == 0)
end_message(conn, ERROR_NONE);
}
}
static enum http_conn_error
check_headers(struct http_conn *conn, struct http_request *req,
struct http_response *resp)
{
enum http_version vers;
int persistent;
int tunnel;
char *val;
conn->te = TE_IDENTITY;
conn->has_body = 1;
conn->msg_complete_on_eof = 0;
conn->data_remaining = -1;
conn->body_length = -1;
conn->expect_continue = 0;
tunnel = 0;
if (conn->type == HTTP_CLIENT) {
vers = req->vers;
conn->has_body = 0;
if (req->meth == METH_POST ||
req->meth == METH_PUT)
conn->has_body = 1;
else if (req->meth == METH_CONNECT)
tunnel = 1;
val = headers_find(conn->headers, "Expect");
if (val) {
int cont;
cont = !evutil_ascii_strcasecmp(val, "100-continue");
mem_free(val);
if (cont == 0 || !conn->has_body)
return ERROR_CLIENT_EXPECTATION_FAILED;
if (cont && req->vers != HTTP_11) {
cont = 0;
log_info("http: ignoring expect continue from "
"old client");
headers_remove(conn->headers, "Expect");
}
conn->expect_continue = cont;
}
} else { /* server */
vers = resp->vers;
if ((resp->code >= 100 && resp->code < 200) ||
resp->code == 204 || resp->code == 205 ||
resp->code == 304)
conn->has_body = 0;
}
/* check headers */
if (conn->has_body) {
val = headers_find(conn->headers, "transfer-encoding");
if (val) {
if (!evutil_ascii_strcasecmp(val, "chunked"))
conn->te = TE_CHUNKED;
mem_free(val);
}
if (conn->te != TE_CHUNKED) {
val = headers_find(conn->headers, "content-length");
if (val) {
ev_int64_t iv;
iv = get_int(val, 10);
if (iv < 0) {
log_warn("http: mangled "
"Content-Length");
headers_remove(conn->headers,
"content-length");
} else
conn->body_length = iv;
mem_free(val);
if (conn->body_length == 0)
conn->has_body = 0;
} else {
conn->msg_complete_on_eof = 1;
}
}
if (conn->type == HTTP_CLIENT && conn->body_length < 0 &&
conn->te != TE_CHUNKED)
return ERROR_CLIENT_POST_WITHOUT_LENGTH;
}
conn->data_remaining = conn->body_length;
assert(vers != HTTP_UNKNOWN);
persistent = 0;
if (!tunnel && !conn->msg_complete_on_eof && vers == HTTP_11)
persistent = 1;
if (conn->vers != HTTP_UNKNOWN && conn->vers != vers) {
log_warn("http_conn: http version changed!");
persistent = 0;
}
conn->vers = vers;
if (persistent) {
val = headers_find(conn->headers, "connection");
if (val) {
if (!evutil_ascii_strcasecmp(val, "close"))
persistent = 0;
mem_free(val);
}
}
conn->persistent = persistent;
return ERROR_NONE;
}
static void
read_headers(struct http_conn *conn)
{
int failed = 0;
struct evbuffer *inbuf = bufferevent_get_input(conn->bev);
struct http_request *req = NULL;
struct http_response *resp = NULL;
enum http_conn_error err;
assert(conn->state == HTTP_STATE_READ_HEADERS);
switch (headers_load(conn->headers, inbuf)) {
case -1:
end_message(conn, ERROR_HEADER_PARSE_FAILED);
return;
case 0:
return;
/* case 1: finished, fall thru */
}
assert(conn->firstline);
if (conn->type == HTTP_CLIENT) {
req = build_request(conn);
if (!req)
failed = 1;
} else {
resp = build_response(conn);
if (!resp)
failed = 1;
}
mem_free(conn->firstline);
conn->firstline = NULL;
if (failed) {
assert(!req && !resp);
end_message(conn, ERROR_HEADER_PARSE_FAILED);
return;
}
err = check_headers(conn, req, resp);
conn->headers = NULL;
if (err == ERROR_NONE) {
int server_continuation = 0;
/* ownership of req or resp is now passed on */
if (req)
EVENT1(conn, on_client_request, req);
if (resp) {
if (resp->code == 100) {
http_response_free(resp);
EVENT0(conn, on_server_continuation);
begin_message(conn);
server_continuation = 1;
} else
EVENT1(conn, on_server_response, resp);
}
if (!server_continuation &&
conn->state != HTTP_STATE_TUNNEL_CONNECTING) {
if (!conn->has_body)
end_message(conn, ERROR_NONE);
else
conn->state = HTTP_STATE_READ_BODY;
}
} else {
http_request_free(req);
http_response_free(resp);
end_message(conn, err);
}
}
static void
tunnel_transfer_data(struct http_conn *conn, struct bufferevent *to,
struct bufferevent *from)
{
struct evbuffer *frombuf = bufferevent_get_input(from);
struct evbuffer *tobuf = bufferevent_get_output(to);
if (evbuffer_get_length(frombuf) == 0)
return;
evbuffer_add_buffer(tobuf, frombuf);
if (evbuffer_get_length(tobuf) > max_write_backlog) {
bufferevent_setwatermark(to, EV_WRITE,
max_write_backlog / 2, 0);
bufferevent_disable(from, EV_READ);
if (from == conn->bev) {
log_debug("tunnel: throttling client read");
conn->read_paused = 1;
} else {
log_debug("tunnel: throttling server read");
conn->tunnel_read_paused = 1;
}
}
}
static void
tunnel_writecb(struct bufferevent *bev, void *_conn)
{
struct http_conn *conn = _conn;
if (conn->state == HTTP_STATE_TUNNEL_OPEN) {
if (conn->tunnel_read_paused && bev == conn->bev) {
log_debug("tunnel: unthrottling server read");
conn->tunnel_read_paused = 0;
bufferevent_enable(conn->tunnel_bev, EV_READ);
bufferevent_setwatermark(bev, EV_WRITE, 0, 0);
} else if (conn->read_paused && bev == conn->tunnel_bev) {
log_debug("tunnel: unthrottling client read");
conn->read_paused = 0;
bufferevent_enable(conn->bev, EV_READ);
bufferevent_setwatermark(bev, EV_WRITE, 0, 0);
}
} else {
log_debug("tunnel: flushed!");
bufferevent_setcb(conn->bev, NULL, NULL, NULL, NULL);
bufferevent_setcb(conn->tunnel_bev, NULL, NULL, NULL, NULL);
EVENT1(conn, on_error, ERROR_TUNNEL_CLOSED);
}
}
static void
tunnel_readcb(struct bufferevent *bev, void *_conn)
{
struct http_conn *conn = _conn;
if (bev == conn->bev)
tunnel_transfer_data(conn, conn->tunnel_bev, bev);
else
tunnel_transfer_data(conn, conn->bev, bev);
}
static void
tunnel_errorcb(struct bufferevent *bev, short what, void *_conn)
{
struct http_conn *conn = _conn;
struct evbuffer *buf;
switch (conn->state) {
case HTTP_STATE_TUNNEL_OPEN:
if (bev == conn->bev) {
log_debug("tunnel: client closed conn...");
bev = conn->tunnel_bev;
} else {
log_debug("tunnel: server closed conn...");
bev = conn->bev;
}
buf = bufferevent_get_output(bev);
if (evbuffer_get_length(buf)) {
conn->state = HTTP_STATE_TUNNEL_FLUSHING;
log_debug("tunnel: flushing %lu bytes...",
(unsigned long)evbuffer_get_length(buf));
bufferevent_disable(bev, EV_READ);
bufferevent_setcb(bev, NULL, tunnel_writecb,
tunnel_errorcb, conn);
break;
}
/* nothing left to write.. lets just fall thru... */
case HTTP_STATE_TUNNEL_FLUSHING:
/* an error happend while flushing, lets just give up. */
bufferevent_setcb(conn->bev, NULL, NULL, NULL, NULL);
bufferevent_setcb(conn->tunnel_bev, NULL, NULL, NULL, NULL);
EVENT1(conn, on_error, ERROR_TUNNEL_CLOSED);
break;
default:
log_fatal("tunnel: errorcb called in invalid state!");
}
}
static void
tunnel_connectcb(struct bufferevent *bev, int ok, void *_conn)
{
struct http_conn *conn = _conn;
assert(conn->state == HTTP_STATE_TUNNEL_CONNECTING);
if (ok) {
conn->state = HTTP_STATE_TUNNEL_OPEN;
bufferevent_setcb(conn->tunnel_bev, tunnel_readcb,
tunnel_writecb, tunnel_errorcb, conn);
bufferevent_enable(conn->bev, EV_READ);
bufferevent_enable(conn->tunnel_bev, EV_READ);
conn->read_paused = 0;
conn->tunnel_read_paused = 0;
tunnel_transfer_data(conn, conn->tunnel_bev, conn->bev);
evbuffer_add_printf(bufferevent_get_output(conn->bev),
"%s 200 Connection established\r\n\r\n",
http_version_to_string(conn->vers));
} else {
bufferevent_setcb(conn->tunnel_bev, NULL, NULL,
NULL, NULL);
EVENT1(conn, on_error, ERROR_TUNNEL_CONNECT_FAILED);
}
}
static void
http_errorcb(struct bufferevent *bev, short what, void *_conn)
{
enum http_state state;
struct http_conn *conn = _conn;
assert(!(what & BEV_EVENT_CONNECTED));
state = conn->state;
conn->state = HTTP_STATE_MANGLED;
if (what & BEV_EVENT_WRITING) {
end_message(conn, ERROR_WRITE_FAILED);
return;
}
switch (state) {
case HTTP_STATE_IDLE:
end_message(conn, ERROR_IDLE_CONN_TIMEDOUT);
break;
case HTTP_STATE_READ_FIRSTLINE:
case HTTP_STATE_READ_HEADERS:
end_message(conn, ERROR_INCOMPLETE_HEADERS);
break;
case HTTP_STATE_READ_BODY:
if ((what & BEV_EVENT_EOF) && conn->msg_complete_on_eof)
end_message(conn, ERROR_NONE);
else
end_message(conn, ERROR_INCOMPLETE_BODY);
break;
default:
log_fatal("http_conn: errorcb called in invalid state");
}
}
static void
process_one_step(struct http_conn *conn)
{
struct evbuffer *inbuf = bufferevent_get_input(conn->bev);
switch (conn->state) {
case HTTP_STATE_IDLE:
conn->state = HTTP_STATE_READ_FIRSTLINE;
bufferevent_set_timeouts(conn->bev, NULL, NULL);
/* fallthru... */
case HTTP_STATE_READ_FIRSTLINE:
assert(conn->firstline == NULL);
conn->firstline = evbuffer_readln(inbuf, NULL,
EVBUFFER_EOL_CRLF);
if (conn->firstline)
conn->state = HTTP_STATE_READ_HEADERS;
break;
case HTTP_STATE_READ_HEADERS:
read_headers(conn);
break;
case HTTP_STATE_READ_BODY:
read_body(conn);
break;
default:
log_fatal("http_conn: read cb called in invalid state");
}
}
static void
process_inbuf(struct http_conn *conn)
{
struct evbuffer *inbuf = bufferevent_get_input(conn->bev);
enum http_state state_before;
if (evbuffer_get_length(inbuf) == 0)
return;
do {
state_before = conn->state;
process_one_step(conn);
} while (!conn->read_paused &&
evbuffer_get_length(inbuf) > 0 &&
state_before != conn->state);
}
static void
http_readcb(struct bufferevent *bev, void *_conn)
{
process_inbuf(_conn);
}
static void
http_writecb(struct bufferevent *bev, void *_conn)
{
struct http_conn *conn = _conn;
struct evbuffer *outbuf = bufferevent_get_output(bev);
if (conn->choked) {
bufferevent_setwatermark(bev, EV_WRITE, 0, 0);
conn->choked = 0;
EVENT0(conn, on_write_more);
} else if (evbuffer_get_length(outbuf) == 0) {
if (!conn->will_flush)
EVENT0(conn, on_flush);
}
}
static void
http_connectcb(struct bufferevent *bev, int ok, void *_conn)
{
struct http_conn *conn = _conn;
assert(conn->state == HTTP_STATE_CONNECTING);
bufferevent_setcb(conn->bev, http_readcb, http_writecb,
http_errorcb, conn);
if (ok) {
begin_message(conn);
EVENT0(conn, on_connect);
} else {
conn->state = HTTP_STATE_MANGLED;
EVENT1(conn, on_error, ERROR_CONNECT_FAILED);
}
}
struct http_conn *
http_conn_new(struct event_base *base, evutil_socket_t sock,
enum http_type type, const struct http_cbs *cbs, void *cbarg)
{
struct http_conn *conn;
conn = mem_calloc(1, sizeof(*conn));
conn->base = base;
conn->type = type;
conn->cbs = cbs;
conn->cbarg = cbarg;
conn->bev = bufferevent_socket_new(base, sock,
BEV_OPT_CLOSE_ON_FREE);
if (!conn->bev)
log_fatal("http_conn: failed to create bufferevent");
conn->inbuf_processed = evbuffer_new();
if (!conn->inbuf_processed)
log_fatal("http_conn: failed to create evbuffer");
if (type != HTTP_SERVER)
bufferevent_setcb(conn->bev, http_readcb, http_writecb,
http_errorcb, conn);
if (sock >= 0)
begin_message(conn);
return conn;
}
int
http_conn_connect(struct http_conn *conn, struct evdns_base *dns,
int family, const char *host, int port)
{
assert(conn->type == HTTP_SERVER);
conn->state = HTTP_STATE_CONNECTING;
return conn_connect_bufferevent(conn->bev, dns, family, host, port,
http_connectcb, conn);
}
static void
deferred_free(evutil_socket_t s, short what, void *arg)
{
struct http_conn *conn = arg;
bufferevent_free(conn->bev);
if (conn->tunnel_bev)
bufferevent_free(conn->tunnel_bev);
evbuffer_free(conn->inbuf_processed);
mem_free(conn);
}
void
http_conn_free(struct http_conn *conn)
{
if (conn->will_free)
return;
conn->will_free = 1;
http_conn_stop_reading(conn);
bufferevent_disable(conn->bev, EV_WRITE);
bufferevent_setcb(conn->bev, NULL, NULL, NULL, NULL);
conn->cbs = NULL;
event_base_once(conn->base, -1, EV_TIMEOUT, deferred_free, conn, NULL);
}
void
http_conn_write_request(struct http_conn *conn, struct http_request *req)
{
struct evbuffer *outbuf;
assert(conn->type == HTTP_SERVER);
headers_remove(req->headers, "connection");
req->vers = HTTP_11;
outbuf = bufferevent_get_output(conn->bev);
evbuffer_add_printf(outbuf, "%s %s %s\r\n",
http_method_to_string(req->meth),
req->url->path,
http_version_to_string(req->vers));
headers_dump(req->headers, outbuf);
}
int
http_conn_expect_continue(struct http_conn *conn)
{
return conn->expect_continue;
}
void
http_conn_write_continue(struct http_conn *conn)
{
struct evbuffer *outbuf;
if (conn->expect_continue) {
log_debug("httpconn: writing continue status");
outbuf = bufferevent_get_output(conn->bev);
conn->expect_continue = 0;
assert(conn->vers == HTTP_11);
evbuffer_add_printf(outbuf, "HTTP/1.1 100 Continue\r\n\r\n");
}
}
void
http_conn_write_response(struct http_conn *conn, struct http_response *resp)
{
struct evbuffer *outbuf;
assert(conn->type == HTTP_CLIENT);
assert(conn->vers != HTTP_UNKNOWN);
headers_remove(resp->headers, "connection");
headers_remove(resp->headers, "transfer-encoding");
resp->vers = conn->vers;
if (conn->vers == HTTP_10 || !conn->persistent) {
if (conn->vers == HTTP_10)
conn->output_te = TE_IDENTITY;
headers_add_key_val(resp->headers, "Connection", "close");
}
if (conn->output_te == TE_CHUNKED) {
headers_add_key_val(resp->headers,
"Transfer-Encoding", "chunked");
}
outbuf = bufferevent_get_output(conn->bev);
evbuffer_add_printf(outbuf, "%s %d %s\r\n",
http_version_to_string(conn->vers),
resp->code,
resp->reason);
headers_dump(resp->headers, outbuf);
}
int
http_conn_write_buf(struct http_conn *conn, struct evbuffer *buf)
{
struct evbuffer *outbuf;
outbuf = bufferevent_get_output(conn->bev);
if (conn->output_te == TE_CHUNKED)
evbuffer_add_printf(outbuf, "%x\r\n",
(unsigned)evbuffer_get_length(buf));
evbuffer_add_buffer(outbuf, buf);
if (conn->output_te == TE_CHUNKED)
evbuffer_add(outbuf, "\r\n", 2);
/* have we choked? */
if (evbuffer_get_length(outbuf) > max_write_backlog) {
bufferevent_setwatermark(conn->bev, EV_WRITE,
max_write_backlog / 2, 0);
conn->choked = 1;
return 0;
}