forked from zerotao/libevhtp
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathevhtp.c
3692 lines (2976 loc) · 97.6 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
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <signal.h>
#include <strings.h>
#include <inttypes.h>
#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#else
#define WINVER 0x0501
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#ifndef NO_SYS_UN
#include <sys/un.h>
#endif
#include <sys/tree.h>
#include "evhtp.h"
static int _evhtp_request_parser_start(htparser * p);
static int _evhtp_request_parser_path(htparser * p, const char * data, size_t len);
static int _evhtp_request_parser_args(htparser * p, const char * data, size_t len);
static int _evhtp_request_parser_header_key(htparser * p, const char * data, size_t len);
static int _evhtp_request_parser_header_val(htparser * p, const char * data, size_t len);
static int _evhtp_request_parser_hostname(htparser * p, const char * data, size_t len);
static int _evhtp_request_parser_headers(htparser * p);
static int _evhtp_request_parser_body(htparser * p, const char * data, size_t len);
static int _evhtp_request_parser_fini(htparser * p);
static int _evhtp_request_parser_chunk_new(htparser * p);
static int _evhtp_request_parser_chunk_fini(htparser * p);
static int _evhtp_request_parser_chunks_fini(htparser * p);
static int _evhtp_request_parser_headers_start(htparser * p);
static void _evhtp_connection_readcb(evbev_t * bev, void * arg);
static evhtp_connection_t * _evhtp_connection_new(evhtp_t * htp, evutil_socket_t sock, evhtp_type type);
static evhtp_uri_t * _evhtp_uri_new(void);
static void _evhtp_uri_free(evhtp_uri_t * uri);
static evhtp_path_t * _evhtp_path_new(const char * data, size_t len);
static void _evhtp_path_free(evhtp_path_t * path);
#define HOOK_AVAIL(var, hook_name) (var->hooks && var->hooks->hook_name)
#define HOOK_FUNC(var, hook_name) (var->hooks->hook_name)
#define HOOK_ARGS(var, hook_name) var->hooks->hook_name ## _arg
#define HOOK_REQUEST_RUN(request, hook_name, ...) do { \
if (HOOK_AVAIL(request, hook_name)) { \
return HOOK_FUNC(request, hook_name) (request, __VA_ARGS__, \
HOOK_ARGS(request, hook_name)); \
} \
\
if (HOOK_AVAIL(evhtp_request_get_connection(request), hook_name)) { \
return HOOK_FUNC(request->conn, hook_name) (request, __VA_ARGS__, \
HOOK_ARGS(request->conn, hook_name)); \
} \
} while (0)
#define HOOK_REQUEST_RUN_NARGS(request, hook_name) do { \
if (HOOK_AVAIL(request, hook_name)) { \
return HOOK_FUNC(request, hook_name) (request, \
HOOK_ARGS(request, hook_name)); \
} \
\
if (HOOK_AVAIL(request->conn, hook_name)) { \
return HOOK_FUNC(request->conn, hook_name) (request, \
HOOK_ARGS(request->conn, hook_name)); \
} \
} while (0);
#ifndef EVHTP_DISABLE_EVTHR
#define _evhtp_lock(h) do { \
if (h->lock) { \
pthread_mutex_lock(h->lock); \
} \
} while (0)
#define _evhtp_unlock(h) do { \
if (h->lock) { \
pthread_mutex_unlock(h->lock); \
} \
} while (0)
#else
#define _evhtp_lock(h) do {} while (0)
#define _evhtp_unlock(h) do {} while (0)
#endif
#ifndef TAILQ_FOREACH_SAFE
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = TAILQ_FIRST((head)); \
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
(var) = (tvar))
#endif
static int scode_tree_initialized = 0;
/**
* @brief An RBTREE entry for the status code -> str matcher
*/
struct status_code {
evhtp_res code;
const char * str;
RB_ENTRY(status_code) entry;
};
static int
status_code_cmp(void * _a, void * _b) {
struct status_code * a = _a;
struct status_code * b = _b;
return b->code - a->code;
}
RB_HEAD(status_code_tree, status_code) status_code_head = RB_INITIALIZER(&status_code_head);
RB_GENERATE(status_code_tree, status_code, entry, status_code_cmp)
#define scode_add(scode, cstr) do { \
struct status_code * c = malloc(sizeof(struct status_code)); \
\
c->code = scode; \
c->str = cstr; \
\
RB_INSERT(status_code_tree, &status_code_head, c); \
} while (0)
static void
status_code_init(void) {
if (scode_tree_initialized) {
/* Already initialized. */
return;
}
/* 100 codes */
scode_add(EVHTP_RES_CONTINUE, "Continue");
scode_add(EVHTP_RES_SWITCH_PROTO, "Switching Protocols");
scode_add(EVHTP_RES_PROCESSING, "Processing");
scode_add(EVHTP_RES_URI_TOOLONG, "URI Too Long");
/* 200 codes */
scode_add(EVHTP_RES_200, "OK");
scode_add(EVHTP_RES_CREATED, "Created");
scode_add(EVHTP_RES_ACCEPTED, "Accepted");
scode_add(EVHTP_RES_NAUTHINFO, "No Auth Info");
scode_add(EVHTP_RES_NOCONTENT, "No Content");
scode_add(EVHTP_RES_RSTCONTENT, "Reset Content");
scode_add(EVHTP_RES_PARTIAL, "Partial Content");
scode_add(EVHTP_RES_MSTATUS, "Multi-Status");
scode_add(EVHTP_RES_IMUSED, "IM Used");
/* 300 codes */
scode_add(EVHTP_RES_300, "Redirect");
scode_add(EVHTP_RES_MOVEDPERM, "Moved Permanently");
scode_add(EVHTP_RES_FOUND, "Found");
scode_add(EVHTP_RES_SEEOTHER, "See Other");
scode_add(EVHTP_RES_NOTMOD, "Not Modified");
scode_add(EVHTP_RES_USEPROXY, "Use Proxy");
scode_add(EVHTP_RES_SWITCHPROXY, "Switch Proxy");
scode_add(EVHTP_RES_TMPREDIR, "Temporary Redirect");
/* 400 codes */
scode_add(EVHTP_RES_400, "Bad Request");
scode_add(EVHTP_RES_UNAUTH, "Unauthorized");
scode_add(EVHTP_RES_PAYREQ, "Payment Required");
scode_add(EVHTP_RES_FORBIDDEN, "Forbidden");
scode_add(EVHTP_RES_NOTFOUND, "Not Found");
scode_add(EVHTP_RES_METHNALLOWED, "Not Allowed");
scode_add(EVHTP_RES_NACCEPTABLE, "Not Acceptable");
scode_add(EVHTP_RES_PROXYAUTHREQ, "Proxy Authentication Required");
scode_add(EVHTP_RES_TIMEOUT, "Request Timeout");
scode_add(EVHTP_RES_CONFLICT, "Conflict");
scode_add(EVHTP_RES_GONE, "Gone");
scode_add(EVHTP_RES_LENREQ, "Length Required");
scode_add(EVHTP_RES_PRECONDFAIL, "Precondition Failed");
scode_add(EVHTP_RES_ENTOOLARGE, "Entity Too Large");
scode_add(EVHTP_RES_URITOOLARGE, "Request-URI Too Long");
scode_add(EVHTP_RES_UNSUPPORTED, "Unsupported Media Type");
scode_add(EVHTP_RES_RANGENOTSC, "Requested Range Not Satisfiable");
scode_add(EVHTP_RES_EXPECTFAIL, "Expectation Failed");
scode_add(EVHTP_RES_IAMATEAPOT, "I'm a teapot");
/* 500 codes */
scode_add(EVHTP_RES_SERVERR, "Internal Server Error");
scode_add(EVHTP_RES_NOTIMPL, "Not Implemented");
scode_add(EVHTP_RES_BADGATEWAY, "Bad Gateway");
scode_add(EVHTP_RES_SERVUNAVAIL, "Service Unavailable");
scode_add(EVHTP_RES_GWTIMEOUT, "Gateway Timeout");
scode_add(EVHTP_RES_VERNSUPPORT, "HTTP Version Not Supported");
scode_add(EVHTP_RES_BWEXEED, "Bandwidth Limit Exceeded");
scode_tree_initialized = 1;
} /* status_code_init */
const char *
status_code_to_str(evhtp_res code) {
struct status_code c;
struct status_code * found;
c.code = code;
if (!(found = RB_FIND(status_code_tree, &status_code_head, &c))) {
return "DERP";
}
return found->str;
}
/**
* @brief callback definitions for request processing from libhtparse
*/
static htparse_hooks request_psets = {
.on_msg_begin = _evhtp_request_parser_start,
.method = NULL,
.scheme = NULL,
.host = NULL,
.port = NULL,
.path = _evhtp_request_parser_path,
.args = _evhtp_request_parser_args,
.uri = NULL,
.on_hdrs_begin = _evhtp_request_parser_headers_start,
.hdr_key = _evhtp_request_parser_header_key,
.hdr_val = _evhtp_request_parser_header_val,
.hostname = _evhtp_request_parser_hostname,
.on_hdrs_complete = _evhtp_request_parser_headers,
.on_new_chunk = _evhtp_request_parser_chunk_new,
.on_chunk_complete = _evhtp_request_parser_chunk_fini,
.on_chunks_complete = _evhtp_request_parser_chunks_fini,
.body = _evhtp_request_parser_body,
.on_msg_complete = _evhtp_request_parser_fini
};
#ifndef EVHTP_DISABLE_SSL
static int session_id_context = 1;
static int ssl_num_locks;
static evhtp_mutex_t * ssl_locks;
static int ssl_locks_initialized = 0;
#endif
/*
* COMPAT FUNCTIONS
*/
#ifdef NO_STRNLEN
static size_t
strnlen(const char * s, size_t maxlen) {
const char * e;
size_t n;
for (e = s, n = 0; *e && n < maxlen; e++, n++) {
;
}
return n;
}
#endif
#ifdef NO_STRNDUP
static char *
strndup(const char * s, size_t n) {
size_t len = strnlen(s, n);
char * ret;
if (len < n) {
return strdup(s);
}
ret = malloc(n + 1);
ret[n] = '\0';
strncpy(ret, s, n);
return ret;
}
#endif
/*
* PRIVATE FUNCTIONS
*/
/**
* @brief a weak hash function
*
* @param str a null terminated string
*
* @return an unsigned integer hash of str
*/
static inline unsigned int
_evhtp_quick_hash(const char * str) {
unsigned int h = 0;
for (; *str; str++) {
h = 31 * h + *str;
}
return h;
}
/**
* @brief helper function to determine if http version is HTTP/1.0
*
* @param major the major version number
* @param minor the minor version number
*
* @return 1 if HTTP/1.0, else 0
*/
static inline int
_evhtp_is_http_10(const char major, const char minor) {
if (major >= 1 && minor <= 0) {
return 1;
}
return 0;
}
/**
* @brief helper function to determine if http version is HTTP/1.1
*
* @param major the major version number
* @param minor the minor version number
*
* @return 1 if HTTP/1.1, else 0
*/
static inline int
_evhtp_is_http_11(const char major, const char minor) {
if (major >= 1 && minor >= 1) {
return 1;
}
return 0;
}
/**
* @brief returns the HTTP protocol version
*
* @param major the major version number
* @param minor the minor version number
*
* @return EVHTP_PROTO_10 if HTTP/1.0, EVHTP_PROTO_11 if HTTP/1.1, otherwise
* EVHTP_PROTO_INVALID
*/
static inline evhtp_proto
_evhtp_protocol(const char major, const char minor) {
if (_evhtp_is_http_10(major, minor)) {
return EVHTP_PROTO_10;
}
if (_evhtp_is_http_11(major, minor)) {
return EVHTP_PROTO_11;
}
return EVHTP_PROTO_INVALID;
}
/**
* @brief runs the user-defined on_path hook for a request
*
* @param request the request structure
* @param path the path structure
*
* @return EVHTP_RES_OK on success, otherwise something else.
*/
static inline evhtp_res
_evhtp_path_hook(evhtp_request_t * request, evhtp_path_t * path) {
HOOK_REQUEST_RUN(request, on_path, path);
return EVHTP_RES_OK;
}
/**
* @brief runs the user-defined on_header hook for a request
*
* once a full key: value header has been parsed, this will call the hook
*
* @param request the request strucutre
* @param header the header structure
*
* @return EVHTP_RES_OK on success, otherwise something else.
*/
static inline evhtp_res
_evhtp_header_hook(evhtp_request_t * request, evhtp_header_t * header) {
HOOK_REQUEST_RUN(request, on_header, header);
return EVHTP_RES_OK;
}
/**
* @brief runs the user-defined on_Headers hook for a request after all headers
* have been parsed.
*
* @param request the request structure
* @param headers the headers tailq structure
*
* @return EVHTP_RES_OK on success, otherwise something else.
*/
static inline evhtp_res
_evhtp_headers_hook(evhtp_request_t * request, evhtp_headers_t * headers) {
HOOK_REQUEST_RUN(request, on_headers, headers);
return EVHTP_RES_OK;
}
/**
* @brief runs the user-defined on_body hook for requests containing a body.
* the data is stored in the request->buffer_in so the user may either
* leave it, or drain upon being called.
*
* @param request the request strucutre
* @param buf a evbuffer containing body data
*
* @return EVHTP_RES_OK on success, otherwise something else.
*/
static inline evhtp_res
_evhtp_body_hook(evhtp_request_t * request, evbuf_t * buf) {
HOOK_REQUEST_RUN(request, on_read, buf);
return EVHTP_RES_OK;
}
/**
* @brief runs the user-defined hook called just prior to a request been
* free()'d
*
* @param request therequest structure
*
* @return EVHTP_RES_OK on success, otherwise treated as an error
*/
static inline evhtp_res
_evhtp_request_fini_hook(evhtp_request_t * request) {
HOOK_REQUEST_RUN_NARGS(request, on_request_fini);
return EVHTP_RES_OK;
}
static inline evhtp_res
_evhtp_chunk_new_hook(evhtp_request_t * request, uint64_t len) {
HOOK_REQUEST_RUN(request, on_new_chunk, len);
return EVHTP_RES_OK;
}
static inline evhtp_res
_evhtp_chunk_fini_hook(evhtp_request_t * request) {
HOOK_REQUEST_RUN_NARGS(request, on_chunk_fini);
return EVHTP_RES_OK;
}
static inline evhtp_res
_evhtp_chunks_fini_hook(evhtp_request_t * request) {
HOOK_REQUEST_RUN_NARGS(request, on_chunks_fini);
return EVHTP_RES_OK;
}
static inline evhtp_res
_evhtp_headers_start_hook(evhtp_request_t * request) {
HOOK_REQUEST_RUN_NARGS(request, on_headers_start);
return EVHTP_RES_OK;
}
/**
* @brief runs the user-definedhook called just prior to a connection being
* closed
*
* @param connection the connection structure
*
* @return EVHTP_RES_OK on success, but pretty much ignored in any case.
*/
static inline evhtp_res
_evhtp_connection_fini_hook(evhtp_connection_t * connection) {
if (connection->hooks && connection->hooks->on_connection_fini) {
return (connection->hooks->on_connection_fini)(connection,
connection->hooks->on_connection_fini_arg);
}
return EVHTP_RES_OK;
}
static inline evhtp_res
_evhtp_hostname_hook(evhtp_request_t * r, const char * hostname) {
HOOK_REQUEST_RUN(r, on_hostname, hostname);
return EVHTP_RES_OK;
}
static inline evhtp_res
_evhtp_connection_write_hook(evhtp_connection_t * connection) {
if (connection->hooks && connection->hooks->on_write) {
return (connection->hooks->on_write)(connection,
connection->hooks->on_write_arg);
}
return EVHTP_RES_OK;
}
/**
* @brief glob/wildcard type pattern matching.
*
* Note: This code was derived from redis's (v2.6) stringmatchlen() function.
*
* @param pattern
* @param string
*
* @return
*/
static int
_evhtp_glob_match(const char * pattern, const char * string) {
size_t pat_len;
size_t str_len;
if (!pattern || !string) {
return 0;
}
pat_len = strlen(pattern);
str_len = strlen(string);
while (pat_len) {
if (pattern[0] == '*') {
while (pattern[1] == '*') {
pattern++;
pat_len--;
}
if (pat_len == 1) {
return 1;
}
while (str_len) {
if (_evhtp_glob_match(pattern + 1, string)) {
return 1;
}
string++;
str_len--;
}
return 0;
} else {
if (pattern[0] != string[0]) {
return 0;
}
string++;
str_len--;
}
pattern++;
pat_len--;
if (str_len == 0) {
while (*pattern == '*') {
pattern++;
pat_len--;
}
break;
}
}
if (pat_len == 0 && str_len == 0) {
return 1;
}
return 0;
} /* _evhtp_glob_match */
static evhtp_callback_t *
_evhtp_callback_find(evhtp_callbacks_t * cbs,
const char * path,
unsigned int * start_offset,
unsigned int * end_offset) {
#ifndef EVHTP_DISABLE_REGEX
regmatch_t pmatch[28];
#endif
evhtp_callback_t * callback;
if (cbs == NULL) {
return NULL;
}
TAILQ_FOREACH(callback, cbs, next) {
switch (callback->type) {
case evhtp_callback_type_hash:
if (strcmp(callback->val.path, path) == 0) {
*start_offset = 0;
*end_offset = (unsigned int)strlen(path);
return callback;
}
break;
#ifndef EVHTP_DISABLE_REGEX
case evhtp_callback_type_regex:
if (regexec(callback->val.regex, path, callback->val.regex->re_nsub + 1, pmatch, 0) == 0) {
*start_offset = pmatch[callback->val.regex->re_nsub].rm_so;
*end_offset = pmatch[callback->val.regex->re_nsub].rm_eo;
return callback;
}
break;
#endif
case evhtp_callback_type_glob:
if (_evhtp_glob_match(callback->val.glob, path) == 1) {
*start_offset = 0;
*end_offset = (unsigned int)strlen(path);
return callback;
}
default:
break;
} /* switch */
}
return NULL;
} /* _evhtp_callback_find */
/**
* @brief Creates a new evhtp_request_t
*
* @param c
*
* @return evhtp_request_t structure on success, otherwise NULL
*/
static evhtp_request_t *
_evhtp_request_new(evhtp_connection_t * c) {
evhtp_request_t * req;
if (!(req = calloc(sizeof(evhtp_request_t), 1))) {
return NULL;
}
req->conn = c;
req->htp = c ? c->htp : NULL;
req->status = EVHTP_RES_OK;
req->buffer_in = evbuffer_new();
req->buffer_out = evbuffer_new();
req->headers_in = malloc(sizeof(evhtp_headers_t));
req->headers_out = malloc(sizeof(evhtp_headers_t));
TAILQ_INIT(req->headers_in);
TAILQ_INIT(req->headers_out);
return req;
}
/**
* @brief frees all data in an evhtp_request_t along with calling finished hooks
*
* @param request the request structure
*/
static void
_evhtp_request_free(evhtp_request_t * request) {
if (request == NULL) {
return;
}
_evhtp_request_fini_hook(request);
_evhtp_uri_free(request->uri);
evhtp_headers_free(request->headers_in);
evhtp_headers_free(request->headers_out);
if (request->buffer_in) {
evbuffer_free(request->buffer_in);
}
if (request->buffer_out) {
evbuffer_free(request->buffer_out);
}
free(request->hooks);
free(request);
}
/**
* @brief create an overlay URI structure
*
* @return evhtp_uri_t
*/
static evhtp_uri_t *
_evhtp_uri_new(void) {
evhtp_uri_t * uri;
if (!(uri = calloc(sizeof(evhtp_uri_t), 1))) {
return NULL;
}
return uri;
}
/**
* @brief frees an overlay URI structure
*
* @param uri evhtp_uri_t
*/
static void
_evhtp_uri_free(evhtp_uri_t * uri) {
if (uri == NULL) {
return;
}
evhtp_query_free(uri->query);
_evhtp_path_free(uri->path);
free(uri->fragment);
free(uri->query_raw);
free(uri);
}
/**
* @brief parses the path and file from an input buffer
*
* @details in order to properly create a structure that can match
* both a path and a file, this will parse a string into
* what it considers a path, and a file.
*
* @details if for example the input was "/a/b/c", the parser will
* consider "/a/b/" as the path, and "c" as the file.
*
* @param data raw input data (assumes a /path/[file] structure)
* @param len length of the input data
*
* @return evhtp_request_t * on success, NULL on error.
*/
static evhtp_path_t *
_evhtp_path_new(const char * data, size_t len) {
evhtp_path_t * req_path;
const char * data_end = (const char *)(data + len);
char * path = NULL;
char * file = NULL;
if (!(req_path = calloc(sizeof(evhtp_path_t), 1))) {
return NULL;
}
if (len == 0) {
/*
* odd situation here, no preceding "/", so just assume the path is "/"
*/
path = strdup("/");
} else if (*data != '/') {
/* request like GET stupid HTTP/1.0, treat stupid as the file, and
* assume the path is "/"
*/
path = strdup("/");
file = strndup(data, len);
} else {
if (data[len - 1] != '/') {
/*
* the last character in data is assumed to be a file, not the end of path
* loop through the input data backwards until we find a "/"
*/
size_t i;
for (i = (len - 1); i != 0; i--) {
if (data[i] == '/') {
/*
* we have found a "/" representing the start of the file,
* and the end of the path
*/
size_t path_len;
size_t file_len;
path_len = (size_t)(&data[i] - data) + 1;
file_len = (size_t)(data_end - &data[i + 1]);
/* check for overflow */
if ((const char *)(data + path_len) > data_end) {
fprintf(stderr, "PATH Corrupted.. (path_len > len)\n");
free(req_path);
return NULL;
}
/* check for overflow */
if ((const char *)(&data[i + 1] + file_len) > data_end) {
fprintf(stderr, "FILE Corrupted.. (file_len > len)\n");
free(req_path);
return NULL;
}
path = strndup(data, path_len);
file = strndup(&data[i + 1], file_len);
break;
}
}
if (i == 0 && data[i] == '/' && !file && !path) {
/* drops here if the request is something like GET /foo */
path = strdup("/");
if (len > 1) {
file = strndup((const char *)(data + 1), len);
}
}
} else {
/* the last character is a "/", thus the request is just a path */
path = strndup(data, len);
}
}
if (len != 0) {
req_path->full = strndup(data, len);
}
req_path->path = path;
req_path->file = file;
return req_path;
} /* _evhtp_path_new */
static void
_evhtp_path_free(evhtp_path_t * path) {
if (path == NULL) {
return;
}
free(path->full);
free(path->path);
free(path->file);
free(path->match_start);
free(path->match_end);
free(path);
}
static int
_evhtp_request_parser_start(htparser * p) {
evhtp_connection_t * c = htparser_get_userdata(p);
if (c->type == evhtp_type_client) {
return 0;
}
if (c->paused == 1) {
return -1;
}
if (c->request) {
if (c->request->finished == 1) {
_evhtp_request_free(c->request);
} else {
return -1;
}
}
if (!(c->request = _evhtp_request_new(c))) {
return -1;
}
return 0;
}
static int
_evhtp_request_parser_args(htparser * p, const char * data, size_t len) {
evhtp_connection_t * c = htparser_get_userdata(p);
evhtp_uri_t * uri = c->request->uri;
if (c->type == evhtp_type_client) {
/* as a client, technically we should never get here, but just in case
* we return a 0 to the parser to continue.
*/
return 0;
}
if (!(uri->query = evhtp_parse_query(data, len))) {
c->request->status = EVHTP_RES_ERROR;
return -1;
}
uri->query_raw = calloc(len + 1, 1);
memcpy(uri->query_raw, data, len);
return 0;
}
static int
_evhtp_request_parser_headers_start(htparser * p) {
evhtp_connection_t * c = htparser_get_userdata(p);
if ((c->request->status = _evhtp_headers_start_hook(c->request)) != EVHTP_RES_OK) {
return -1;
}
return 0;
}
static int
_evhtp_request_parser_header_key(htparser * p, const char * data, size_t len) {
evhtp_connection_t * c = htparser_get_userdata(p);
char * key_s; /* = strndup(data, len); */
evhtp_header_t * hdr;
key_s = malloc(len + 1);
key_s[len] = '\0';
memcpy(key_s, data, len);
if ((hdr = evhtp_header_key_add(c->request->headers_in, key_s, 0)) == NULL) {
c->request->status = EVHTP_RES_FATAL;
return -1;
}
hdr->k_heaped = 1;
return 0;
}
static int
_evhtp_request_parser_header_val(htparser * p, const char * data, size_t len) {
evhtp_connection_t * c = htparser_get_userdata(p);
char * val_s;
evhtp_header_t * header;
val_s = malloc(len + 1);
val_s[len] = '\0';
memcpy(val_s, data, len);
if ((header = evhtp_header_val_add(c->request->headers_in, val_s, 0)) == NULL) {
free(val_s);
c->request->status = EVHTP_RES_FATAL;
return -1;
}
header->v_heaped = 1;
if ((c->request->status = _evhtp_header_hook(c->request, header)) != EVHTP_RES_OK) {
return -1;
}
return 0;
}
static inline evhtp_t *
_evhtp_request_find_vhost(evhtp_t * evhtp, const char * name) {
evhtp_t * evhtp_vhost;
evhtp_alias_t * evhtp_alias;
TAILQ_FOREACH(evhtp_vhost, &evhtp->vhosts, next_vhost) {
if (evhtp_vhost->server_name == NULL) {
continue;
}
if (_evhtp_glob_match(evhtp_vhost->server_name, name) == 1) {
return evhtp_vhost;
}
TAILQ_FOREACH(evhtp_alias, &evhtp_vhost->aliases, next) {
if (evhtp_alias->alias == NULL) {
continue;
}
if (_evhtp_glob_match(evhtp_alias->alias, name) == 1) {
return evhtp_vhost;
}
}
}
return NULL;
}
static inline int
_evhtp_request_set_callbacks(evhtp_request_t * request) {
evhtp_t * evhtp;
evhtp_connection_t * conn;
evhtp_uri_t * uri;
evhtp_path_t * path;
evhtp_hooks_t * hooks;
evhtp_callback_t * callback;
evhtp_callback_cb cb;
void * cbarg;
if (request == NULL) {
return -1;
}
if ((evhtp = request->htp) == NULL) {
return -1;
}
if ((conn = request->conn) == NULL) {
return -1;
}
if ((uri = request->uri) == NULL) {
return -1;
}
if ((path = uri->path) == NULL) {
return -1;
}