forked from Yellow-Camper/libevhtp
/
evhtp.c
1765 lines (1376 loc) · 41.7 KB
/
evhtp.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "evhtp.h"
typedef struct evhtp_callback evhtp_callback_t;
typedef struct evhtp_callbacks evhtp_callbacks_t;
typedef void (*htp_conn_write_fini_cb)(evhtp_conn_t * conn, void * args);
struct evhtp {
evbase_t * evbase;
evserv_t * listener;
evhtp_callbacks_t * callbacks;
void * default_cbarg;
void * pre_accept_cbarg;
void * post_accept_cbarg;
char * server_name;
evhtp_callback_cb default_cb;
evhtp_pre_accept pre_accept_cb;
evhtp_post_accept post_accept_cb;
http_parser_settings psets;
#ifndef DISABLE_SSL
evhtp_ssl_ctx_t * ssl_ctx;
evhtp_ssl_cfg * ssl_cfg;
#endif
#ifndef DISABLE_EVTHR
evthr_pool_t * pool;
#else
void * pool;
#endif
};
struct evhtp_hooks {
evhtp_hook_hdr _hdr;
evhtp_hook_hdrs _hdrs;
evhtp_hook_path _path;
evhtp_hook_uri _uri;
evhtp_hook_read _read;
evhtp_hook_on_expect _on_expect;
void * _hdr_cbargs;
void * _hdrs_cbargs;
void * _path_cbargs;
void * _uri_cbargs;
void * _read_cbargs;
void * _on_expect_cbargs;
};
struct evhtp_callback {
char * uri;
void * cbarg;
unsigned int hash;
evhtp_callback_cb cb;
evhtp_callback_t * next;
};
struct evhtp_callbacks {
evhtp_callback_t ** callbacks;
unsigned int count;
unsigned int buckets;
};
struct evhtp_conn {
evhtp_t * htp;
evhtp_hooks_t * hooks;
evhtp_request_t * request;
http_parser * parser;
int sock;
evhtp_cflags flags;
evbase_t * evbase;
evbev_t * bev;
#ifndef DISABLE_SSL
evhtp_ssl_t * ssl;
#endif
#ifndef DISABLE_EVTHR
evthr_t * thr;
#endif
};
#define _HTP_CONN "Connection"
#define _HTP_CONTLEN "Content-Length"
#define _HTP_CONTYPE "Content-Type"
#define _HTP_EXPECT "Expect"
#define _HTP_SERVER "Server"
#define _HTP_TRANSENC "Transfer-Encoding"
#define _HTP_DEFCLOSE "close"
#define _HTP_DEFKALIVE "keep-alive"
#define _HTP_DEFCONTYPE "text/plain"
#define _HTP_DEFSERVER "libevht"
#define _HTP_DEFCHUNKED "chunked"
#ifdef DEBUG
#define __QUOTE(x) # x
#define _QUOTE(x) __QUOTE(x)
#define evhtp_log_debug(fmt, ...) do { \
fprintf(stdout, __FILE__ "[" _QUOTE(__LINE__) "] %s: " \
fmt "\n", __func__, ## __VA_ARGS__); \
fflush(stdout); \
} while (0)
#else
#define evhtp_log_debug(fmt, ...) do {} while (0)
#endif
#define _htp_conn_hook(c) (c)->hooks
#define _htp_conn_has_hook(c, n) (_htp_conn_hook(c) && _htp_conn_hook(c)->n)
#define _htp_conn_hook_cbarg(c, n) _htp_conn_hook(c)->n ## _cbargs
#define _htp_conn_hook_call(c, n, ...) _htp_conn_hook(c)->n(c->request, __VA_ARGS__, _htp_conn_hook_cbarg(c, n))
#define _htp_conn_hook_set(c, n, f, a) do { \
_htp_conn_hook(c)->n = f; \
_htp_conn_hook_cbarg(c, n) = a; \
} while (0)
#define CRLF "\r\n"
static evhtp_proto _htp_proto(char major, char minor);
static evhtp_callback_t * _htp_callbacks_find_callback(evhtp_callbacks_t *, const char *);
static void _htp_recv_cb(evbev_t * bev, void * arg);
static void _htp_err_cb(evbev_t * bev, short events, void * arg);
#ifndef DISABLE_OPENSSL
#ifndef DISABLE_EVTHR
static int ssl_num_locks;
static pthread_mutex_t ** ssl_locks;
#endif
#endif
static evhtp_status
_htp_run_on_expect_hook(evhtp_conn_t * conn, const char * expt_val) {
evhtp_status status = EVHTP_CODE_CONTINUE;
evhtp_log_debug("enter");
if (_htp_conn_has_hook(conn, _on_expect)) {
status = _htp_conn_hook_call(conn, _on_expect, expt_val);
}
return status;
}
static evhtp_res
_htp_run_hdr_hook(evhtp_conn_t * conn, evhtp_hdr_t * hdr) {
evhtp_res res = EVHTP_RES_OK;
evhtp_log_debug("enter");
if (_htp_conn_has_hook(conn, _hdr)) {
res = _htp_conn_hook_call(conn, _hdr, hdr);
}
return res;
}
static evhtp_res
_htp_run_hdrs_hook(evhtp_conn_t * conn, evhtp_hdrs_t * hdrs) {
evhtp_res res = EVHTP_RES_OK;
evhtp_log_debug("enter");
if (_htp_conn_has_hook(conn, _hdrs)) {
res = _htp_conn_hook_call(conn, _hdrs, hdrs);
}
return res;
}
static evhtp_res
_htp_run_path_hook(evhtp_conn_t * conn, const char * path) {
evhtp_res res = EVHTP_RES_OK;
evhtp_log_debug("enter");
if (_htp_conn_has_hook(conn, _path)) {
res = _htp_conn_hook_call(conn, _path, path);
}
return res;
}
static evhtp_res
_htp_run_uri_hook(evhtp_conn_t * conn, const char * uri) {
evhtp_res res = EVHTP_RES_OK;
evhtp_log_debug("enter");
if (_htp_conn_has_hook(conn, _uri)) {
res = _htp_conn_hook_call(conn, _uri, uri);
}
return res;
}
static evhtp_res
_htp_run_read_hook(evhtp_conn_t * conn, const char * data, size_t sz) {
evhtp_res res = EVHTP_RES_OK;
evhtp_log_debug("enter");
if (_htp_conn_has_hook(conn, _read)) {
res = _htp_conn_hook_call(conn, _read, data, sz);
}
return res;
}
static int
_htp_start_cb(http_parser * p) {
evhtp_conn_t * conn = p->data;
evhtp_log_debug("enter");
conn->request = evhtp_request_new(conn);
return 0;
}
static int
_htp_end_cb(http_parser * p) {
evhtp_conn_t * conn = NULL;
evhtp_request_t * request = NULL;
evhtp_log_debug("enter");
conn = p->data;
request = conn->request;
if (request->cb) {
request->cb(request, request->cbarg);
}
return 0;
}
static int
_htp_query_str_cb(http_parser * p, const char * buf, size_t len) {
/* evhtp_conn_t * conn = p->data; */
evhtp_log_debug("len = %" PRIoMAX " buf = '%.*s'", len, (int)len, buf);
return 0;
}
static int
_htp_uri_cb(http_parser * p, const char * buf, size_t len) {
evhtp_conn_t * conn;
evhtp_request_t * request;
evhtp_log_debug("enter");
conn = p->data;
request = conn->request;
request->uri = malloc(len + 1);
request->uri[len] = '\0';
memcpy(request->uri, buf, len);
if (_htp_run_uri_hook(conn, request->uri) != EVHTP_RES_OK) {
return -1;
}
return 0;
}
static int
_htp_fragment_cb(http_parser * p, const char * buf, size_t len) {
/* evhtp_conn_t * conn = p->data; */
evhtp_log_debug("len = %" PRIoMAX " buf = '%.*s", len, (int)len, buf);
return 0;
}
static int
_htp_header_key_cb(http_parser * p, const char * buf, size_t len) {
evhtp_hdr_t * hdr;
evhtp_conn_t * conn;
evhtp_log_debug("len = %" PRIdMAX, len);
conn = p->data;
hdr = malloc(sizeof(evhtp_hdr_t));
hdr->k_heaped = 1;
hdr->key = malloc(len + 1);
hdr->key[len] = '\0';
memcpy(hdr->key, buf, len);
TAILQ_INSERT_TAIL(&conn->request->headers_in, hdr, next);
return 0;
}
static int
_htp_header_val_cb(http_parser * p, const char * buf, size_t len) {
evhtp_hdr_t * hdr = NULL;
evhtp_conn_t * conn = NULL;
evhtp_request_t * req = NULL;
evhtp_log_debug("len = %" PRIdMAX, len);
conn = p->data;
req = conn->request;
hdr = TAILQ_LAST(&req->headers_in, evhtp_hdrs);
hdr->v_heaped = 1;
hdr->val = malloc(len + 1);
hdr->val[len] = '\0';
memcpy(hdr->val, buf, len);
if (_htp_run_hdr_hook(conn, hdr) != EVHTP_RES_OK) {
return -1;
}
return 0;
}
static int
_htp_headers_complete_cb(http_parser * p) {
evhtp_conn_t * conn;
evhtp_log_debug("enter");
conn = p->data;
conn->request->method = p->method;
conn->request->major = p->http_major;
conn->request->minor = p->http_minor;
conn->request->proto = _htp_proto(p->http_major, p->http_minor);
if (_htp_run_hdrs_hook(conn, &conn->request->headers_in) != EVHTP_RES_OK) {
return -1;
}
if (evhtp_hdr_find(&conn->request->headers_in, _HTP_CONTLEN)) {
const char * expt_val;
evbuf_t * buf;
evhtp_status status;
if (!(expt_val = evhtp_hdr_find(&conn->request->headers_in, _HTP_EXPECT))) {
return 0;
}
if ((status = _htp_run_on_expect_hook(conn, expt_val)) != EVHTP_CODE_CONTINUE) {
evhtp_send_reply(conn->request, status, "no", NULL);
return -1;
}
buf = evbuffer_new();
evbuffer_add_printf(buf, "HTTP/%d.%d 100 Continue\r\n\r\n", p->http_major, p->http_minor);
evbuffer_write(buf, conn->sock);
evbuffer_free(buf);
}
return 0;
}
static int
_htp_path_cb(http_parser * p, const char * buf, size_t len) {
evhtp_conn_t * conn = NULL;
evhtp_request_t * request = NULL;
evhtp_callback_t * cb = NULL;
/* printf("on_path size: %llu\n", len); */
evhtp_log_debug("enter");
conn = p->data;
request = conn->request;
request->path = malloc(len + 1);
request->path[len] = '\0';
memcpy(request->path, buf, len);
if (!(cb = _htp_callbacks_find_callback(conn->htp->callbacks, request->path))) {
if (conn->htp->default_cb == NULL) {
return -1;
}
request->cb = conn->htp->default_cb;
request->cbarg = conn->htp->default_cbarg;
} else {
request->cb = cb->cb;
request->cbarg = cb->cbarg;
}
if (_htp_run_path_hook(conn, conn->request->path) != EVHTP_RES_OK) {
return -1;
}
return 0;
}
static int
_htp_body_cb(http_parser * p, const char * buf, size_t len) {
evhtp_conn_t * conn = p->data;
evhtp_log_debug("enter");
evbuffer_add(conn->request->buffer_in, buf, len);
if (_htp_run_read_hook(conn, buf, len) != EVHTP_RES_OK) {
return -1;
}
return 0;
}
static inline unsigned int
_htp_thash(const char * key) {
unsigned int h = 0;
for (; *key; key++) {
h = 31 * h + *key;
}
return h;
}
static evhtp_callback_t *
_htp_callback_new(const char * uri, evhtp_callback_cb cb, void * cbarg) {
evhtp_callback_t * htp_cb;
evhtp_log_debug("enter");
if (!(htp_cb = calloc(sizeof(evhtp_callback_t), sizeof(char)))) {
return NULL;
}
htp_cb->hash = _htp_thash(uri);
htp_cb->cb = cb;
htp_cb->cbarg = cbarg;
htp_cb->uri = strdup(uri);
return htp_cb;
}
static evhtp_callbacks_t *
_htp_callbacks_new(unsigned int buckets) {
evhtp_callbacks_t * htp_cbs;
evhtp_log_debug("enter");
if (!(htp_cbs = calloc(sizeof(evhtp_callbacks_t), sizeof(char)))) {
return NULL;
}
if (!(htp_cbs->callbacks = calloc(sizeof(evhtp_callback_t *), buckets))) {
free(htp_cbs);
return NULL;
}
htp_cbs->count = 0;
htp_cbs->buckets = buckets;
return htp_cbs;
}
static evhtp_callback_t *
_htp_callbacks_find_callback(evhtp_callbacks_t * cbs, const char * uri) {
evhtp_callback_t * cb;
unsigned int hash;
evhtp_log_debug("enter");
if (cbs == NULL) {
return NULL;
}
hash = _htp_thash(uri);
cb = cbs->callbacks[hash & (cbs->buckets - 1)];
if (cb == NULL) {
return NULL;
}
while (cb != NULL) {
if (cb->hash == hash && !strcmp(cb->uri, uri)) {
return cb;
}
cb = cb->next;
}
return NULL;
}
static int
_htp_callbacks_add_callback(evhtp_callbacks_t * cbs, evhtp_callback_t * cb) {
unsigned int hkey;
evhtp_log_debug("enter");
hkey = cb->hash % cbs->buckets;
if (cbs->callbacks[hkey] == NULL) {
cbs->callbacks[hkey] = cb;
return 0;
}
cb->next = cbs->callbacks[hkey];
cbs->callbacks[hkey] = cb;
return 0;
}
void
_htp_conn_free(evhtp_conn_t * conn) {
if (conn == NULL) {
return;
}
evhtp_log_debug("enter");
if (conn->hooks) {
free(conn->hooks);
}
if (conn->parser) {
free(conn->parser);
}
if (conn->request) {
evhtp_request_free(conn->request);
}
#ifndef DISABLE_EVTHR
if (conn->thr) {
evthr_dec_backlog(conn->thr);
}
#endif
if (conn->bev) {
bufferevent_free(conn->bev);
}
free(conn);
} /* _htp_conn_free */
static evhtp_conn_t *
_htp_conn_new(evhtp_t * htp) {
evhtp_conn_t * conn;
evhtp_log_debug("enter");
if (!(conn = calloc(sizeof(evhtp_conn_t), sizeof(char)))) {
return NULL;
}
conn->htp = htp;
conn->flags = 0;
conn->parser = malloc(sizeof(http_parser));
conn->parser->data = conn;
http_parser_init(conn->parser, HTTP_REQUEST);
return conn;
}
static void
_htp_conn_reset(evhtp_conn_t * conn) {
http_parser_init(conn->parser, HTTP_REQUEST);
evhtp_log_debug("enter");
evhtp_request_free(conn->request);
conn->request = NULL;
bufferevent_disable(conn->bev, EV_WRITE);
bufferevent_enable(conn->bev, EV_READ);
bufferevent_setwatermark(conn->bev, EV_READ | EV_WRITE, 0, 0);
bufferevent_setcb(conn->bev, _htp_recv_cb, NULL, _htp_err_cb, conn);
}
static int
_htp_conn_get_sock(evhtp_conn_t * conn) {
if (conn == NULL) {
return -1;
}
evhtp_log_debug("enter");
return conn->sock;
}
static evserv_t *
_htp_conn_get_listener(evhtp_conn_t * conn) {
if (conn == NULL) {
return NULL;
}
evhtp_log_debug("enter");
return evhtp_get_listener(conn->htp);
}
static evbase_t *
_htp_conn_get_evbase(evhtp_conn_t * conn) {
if (conn == NULL) {
return NULL;
}
evhtp_log_debug("enter");
return conn->evbase;
}
static int
_htp_resp_can_have_content(evhtp_status code) {
evhtp_log_debug("enter");
if (code >= 100) {
if (code < 300) {
return 1;
}
return 0;
}
return 0;
}
static void
_htp_recv_cb(evbev_t * bev, void * arg) {
evbuf_t * ibuf;
evhtp_conn_t * conn;
char * read_buf;
size_t nread;
size_t avail;
evhtp_log_debug("enter");
conn = (evhtp_conn_t *)arg;
ibuf = bufferevent_get_input(bev);
avail = evbuffer_get_length(ibuf);
read_buf = (char *)evbuffer_pullup(ibuf, avail);
nread = http_parser_execute(conn->parser, &conn->htp->psets, read_buf, avail);
evbuffer_drain(ibuf, nread);
evhtp_log_debug("nread = %d", nread);
}
static void
_htp_err_cb(evbev_t * bev, short events, void * arg) {
evhtp_conn_t * conn;
evhtp_log_debug("events = %x", events);
if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) {
conn = (evhtp_conn_t *)arg;
evhtp_log_debug("leaving....");
return _htp_conn_free(conn);
}
}
static int
_htp_hdr_output(evhtp_hdr_t * hdr, void * arg) {
evbuf_t * buf = (evbuf_t *)arg;
evhtp_log_debug("enter");
evbuffer_add(buf, hdr->key, strlen(hdr->key));
/* evbuffer_add_reference(buf, ": ", 2, NULL, NULL); */
evbuffer_add(buf, ": ", 2);
evbuffer_add(buf, hdr->val, strlen(hdr->val));
evbuffer_add(buf, CRLF, 2);
/* evbuffer_add_reference(buf, CRLF, 2, NULL, NULL); */
return 0;
}
#ifndef DISABLE_EVTHR
static void
_htp_exec_in_thr(evthr_t * thr, void * arg, void * shared) {
evhtp_t * htp;
evhtp_conn_t * conn;
evhtp_log_debug("enter");
htp = (evhtp_t *)shared;
conn = (evhtp_conn_t *)arg;
conn->evbase = evthr_get_base(thr);
conn->thr = thr;
if (htp->ssl_ctx == NULL) {
conn->bev = bufferevent_socket_new(conn->evbase, conn->sock, BEV_OPT_CLOSE_ON_FREE);
} else {
#ifndef DISABLE_EVTHR
conn->ssl = SSL_new(htp->ssl_ctx);
conn->bev = bufferevent_openssl_socket_new(conn->evbase,
conn->sock, conn->ssl, BUFFEREVENT_SSL_ACCEPTING,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
SSL_set_app_data(conn->ssl, conn);
#else
fprintf(stderr, "SSL requested but not enabled\n");
abort();
#endif
}
bufferevent_setcb(conn->bev, _htp_recv_cb, NULL, _htp_err_cb, conn);
bufferevent_enable(conn->bev, EV_READ);
bufferevent_disable(conn->bev, EV_WRITE);
if (htp->post_accept_cb) {
htp->post_accept_cb(conn, htp->post_accept_cbarg);
}
evthr_inc_backlog(conn->thr);
}
#endif
static void
_htp_accept_cb(evserv_t * serv, int fd, struct sockaddr * s, int sl, void * arg) {
evhtp_t * htp;
evhtp_conn_t * conn;
evhtp_log_debug("enter");
htp = (evhtp_t *)arg;
evutil_make_socket_nonblocking(fd);
conn = _htp_conn_new(htp);
conn->evbase = htp->evbase;
conn->sock = fd;
if (htp->post_accept_cb) {
htp->post_accept_cb(conn, htp->post_accept_cbarg);
}
#ifndef DISABLE_EVTHR
if (htp->pool != NULL) {
evthr_pool_defer(htp->pool, _htp_exec_in_thr, conn);
return;
}
#endif
if (htp->ssl_ctx == NULL) {
conn->bev = bufferevent_socket_new(conn->evbase, conn->sock, BEV_OPT_CLOSE_ON_FREE);
} else {
#ifndef DISABLE_SSL
conn->ssl = SSL_new(htp->ssl_ctx);
conn->bev = bufferevent_openssl_socket_new(conn->evbase,
conn->sock, conn->ssl, BUFFEREVENT_SSL_ACCEPTING,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
SSL_set_app_data(conn->ssl, conn);
#else
fprintf(stderr, "SSL requested but not enabled\n");
abort();
#endif
}
bufferevent_disable(conn->bev, EV_WRITE);
bufferevent_enable(conn->bev, EV_READ);
bufferevent_setcb(conn->bev, _htp_recv_cb, NULL, _htp_err_cb, conn);
if (htp->post_accept_cb) {
htp->post_accept_cb(conn, htp->post_accept_cbarg);
}
} /* _htp_accept_cb */
static void
_htp_set_kalive_hdr(evhtp_hdrs_t * hdrs, evhtp_proto proto, int kalive) {
if (hdrs == NULL) {
return;
}
evhtp_log_debug("enter");
if (kalive && proto == EVHTP_PROTO_1_0) {
return evhtp_hdr_add(hdrs, evhtp_hdr_new(_HTP_CONN, _HTP_DEFKALIVE));
}
if (!kalive && proto == EVHTP_PROTO_1_1) {
return evhtp_hdr_add(hdrs, evhtp_hdr_new(_HTP_CONN, _HTP_DEFCLOSE));
}
}
static void
_htp_reply_set_content_hdrs(evhtp_request_t * req, size_t len) {
const char * content_len_hval;
const char * content_type_hval;
evhtp_log_debug("enter");
if (req == NULL) {
return;
}
if (len == 0) {
evhtp_hdr_add(&req->headers_out, evhtp_hdr_new(_HTP_CONTLEN, "0"));
return;
}
content_len_hval = evhtp_hdr_find(&req->headers_out, _HTP_CONTLEN);
content_type_hval = evhtp_hdr_find(&req->headers_out, _HTP_CONTYPE);
if (content_len_hval == NULL) {
evhtp_hdr_t * hdr;
#if __WORDSIZE == 64
char lstr[22];
#else
char lstr[12];
#endif
snprintf(lstr, sizeof(lstr), "%" PRIuMAX, len);
hdr = evhtp_hdr_new(_HTP_CONTLEN, strdup(lstr));
hdr->v_heaped = 1;
evhtp_hdr_add(&req->headers_out, hdr);
}
if (content_type_hval == NULL) {
evhtp_hdr_add(&req->headers_out, evhtp_hdr_new(_HTP_CONTYPE, _HTP_DEFCONTYPE));
}
} /* _htp_reply_set_content_hdrs */
static evhtp_status
_htp_code_parent(evhtp_status code) {
evhtp_log_debug("enter");
if (code > 599 || code < 100) {
return EVHTP_CODE_SCREWEDUP;
}
if (code >= 100 && code < 200) {
return EVHTP_CODE_100;
}
if (code >= 200 && code < 300) {
return EVHTP_CODE_200;
}
if (code >= 300 && code < 400) {
return EVHTP_CODE_300;
}
if (code >= 400 && code < 500) {
return EVHTP_CODE_400;
}
return EVHTP_CODE_500;
}
static int
_htp_should_close_based_on_cflags(evhtp_cflags flags, evhtp_status code) {
int res = 0;
evhtp_log_debug("enter");
switch (_htp_code_parent(code)) {
case EVHTP_CODE_100:
res = (flags & EVHTP_CLOSE_ON_100);
break;
case EVHTP_CODE_200:
res = (flags & EVHTP_CLOSE_ON_200);
break;
case EVHTP_CODE_300:
res = (flags & EVHTP_CLOSE_ON_300);
break;
case EVHTP_CODE_400:
if (code == EVHTP_CODE_EXPECTFAIL && flags & EVHTP_CLOSE_ON_EXPECT_ERR) {
res = 1;
} else {
res = (flags & EVHTP_CLOSE_ON_400);
}
break;
case EVHTP_CODE_500:
res = (flags & EVHTP_CLOSE_ON_500);
break;
case EVHTP_CODE_SCREWEDUP:
res = 1;
break;
} /* switch */
return res ? 1 : 0;
}
static int
_htp_should_keep_alive(evhtp_request_t * req, evhtp_status code) {
evhtp_conn_t * conn = req->conn;
evhtp_log_debug("enter");
if (http_should_keep_alive(conn->parser) == 0) {
/* parsed request doesn't even support keep-alive */
return 0;
}
if (_htp_should_close_based_on_cflags(conn->flags, code)) {
/* one of the user-set flags has informed us to close, thus
* do not keep alive */
return 0;
}
/* all above actions taken into account, the client is
* set to keep-alive */
return 1;
}
static inline int
_htp_is_http_1_1x(char major, char minor) {
evhtp_log_debug("enter");
if (major >= 1 && minor >= 1) {
return 1;
}
return 0;
}
static inline int
_htp_is_http_1_0x(char major, char minor) {
if (major >= 1 && minor <= 0) {
return 1;
}
return 0;
}
static evhtp_proto
_htp_proto(char major, char minor) {
if (_htp_is_http_1_0x(major, minor)) {
return EVHTP_PROTO_1_0;
}
if (_htp_is_http_1_1x(major, minor)) {
return EVHTP_PROTO_1_1;
}
return EVHTP_PROTO_INVALID;
}
#define _htp_set_status_buf(buf, major, minor, code) do { \
evbuffer_add_printf(buf, "HTTP/%d.%d %d DERP\r\n", major, minor, code); \
} while (0)
#define _htp_set_header_buf(buf, hdrs) do { \
evhtp_hdrs_for_each(hdrs, _htp_hdr_output, buf); \
} while (0)
#define _htp_set_server_hdr(hdrs, name) do { \
evhtp_hdr_add(hdrs, evhtp_hdr_new(_HTP_SERVER, name)); \
} while (0)
#define _htp_set_crlf_buf(buf) do { \
evbuffer_add_reference(buf, CRLF, 2, NULL, NULL); \
} while (0)
void
_htp_set_body_buf(evbuf_t * dst, evbuf_t * src) {
if (dst == NULL) {
return;
}
evhtp_log_debug("enter");
if (src && evbuffer_get_length(src)) {
evbuffer_add_buffer(dst, src);
}
}
static void
_htp_resp_fini_cb(evbev_t * bev, void * arg) {
evhtp_request_t * req;
evhtp_conn_t * conn;
int keepalive;
evhtp_log_debug("enter");
req = (evhtp_request_t *)arg;
keepalive = req->keepalive;
conn = req->conn;
if (keepalive) {
return _htp_conn_reset(conn);
} else {
return _htp_conn_free(conn);
}
}
static void
_htp_resp_err_cb(evbev_t * bev, short events, void * arg) {
evhtp_request_t * req;
evhtp_conn_t * conn;
evhtp_log_debug("events = %x", events);
req = (evhtp_request_t *)arg;
conn = req->conn;
return _htp_conn_free(conn);
}
static void
_htp_stream_fini_cb(evbev_t * bev, void * arg) {
evhtp_request_t * req;
evhtp_conn_t * conn;
evhtp_log_debug("enter");
req = (evhtp_request_t *)arg;
conn = req->conn;
switch (req->stream_cb(req, req->stream_cbarg)) {
case EVHTP_RES_OK:
bufferevent_write_buffer(conn->bev, req->buffer_out);
return;
case EVHTP_RES_DONE:
if (req->chunked) {
evbuffer_add_reference(req->buffer_out, "0\r\n\r\n", 5, NULL, NULL);
bufferevent_setcb(conn->bev, NULL, _htp_resp_fini_cb, _htp_resp_err_cb, req);
bufferevent_write_buffer(conn->bev, req->buffer_out);