forked from Yellow-Camper/libevhtp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
evhtp.c
5664 lines (4522 loc) · 141 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
/**
* @file evhtp.c
*
* @brief implementation file for libevhtp.
*/
#ifdef HAVE_CONFIG_H
#include "libevhtp-config.h" /* generated by config & autoconf */
#endif
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <signal.h>
#include <strings.h>
#include <inttypes.h>
#include <stdbool.h>
#include <sys/param.h> /* MIN/MAX macro */
#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 <limits.h>
#include <event2/dns.h>
#include "internal.h"
#include "numtoa.h"
#include "evhtp/evhtp.h"
/**
* @brief structure containing a single callback and configuration
*
* The definition structure which is used within the evhtp_callbacks_t
* structure. This holds information about what should execute for either
* a single or regex path.
*
* For example, if you registered a callback to be executed on a request
* for "/herp/derp", your defined callback will be executed.
*
* Optionally you can set callback-specific hooks just like per-connection
* hooks using the same rules.
*
*/
struct evhtp_callback {
evhtp_callback_type type; /**< the type of callback (regex|path) */
evhtp_callback_cb cb; /**< the actual callback function */
void * cbarg; /**< user-defind arguments passed to the cb */
evhtp_hooks_t * hooks; /**< per-callback hooks */
size_t len;
union {
char * path;
char * glob;
#ifndef EVHTP_DISABLE_REGEX
regex_t * regex;
#endif
} val;
TAILQ_ENTRY(evhtp_callback) next;
};
TAILQ_HEAD(evhtp_callbacks, evhtp_callback);
#define SET_BIT(VAR, FLAG) VAR |= FLAG
#define UNSET_BIT(VAR, FLAG) VAR &= ~FLAG
#define HTP_FLAG_ON(PRE, FLAG) SET_BIT(PRE->flags, FLAG)
#define HTP_FLAG_OFF(PRE, FLAG) UNSET_BIT(PRE->flags, FLAG)
#define HTP_IS_READING(b) ((bufferevent_get_enabled(b) & \
EV_READ) ? true : false)
#define HTP_IS_WRITING(b) ((bufferevent_get_enabled(b) & \
EV_WRITE) ? true : false)
#define HTP_LEN_OUTPUT(b) (evbuffer_get_length(bufferevent_get_output(b)))
#define HTP_LEN_INPUT(b) (evbuffer_get_length(bufferevent_get_input(b)))
#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 (request->conn && HOOK_AVAIL(request->conn, 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 (__request->conn && 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
/**
* @brief Helper macro to lock htp structure
*
* @param h htp structure
*/
#define htp__lock_(h) do { \
if (h->lock) { \
pthread_mutex_lock(h->lock); \
} \
} while (0)
/**
* @brief Helper macro to unlock htp lock
*
* @param h htp structure
*/
#define htp__unlock_(h) do { \
if (h->lock) { \
pthread_mutex_unlock(h->lock); \
} \
} while (0)
#else
#define htp__lock_(h) do { \
} while (0)
#define htp__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
/* rc == request->conn. Just little things to make life easier */
#define rc_scratch conn->scratch_buf
#define rc_parser conn->parser
/* ch_ == conn->hooks->on_... */
#define ch_fini_arg hooks->on_connection_fini_arg
#define ch_fini hooks->on_connection_fini
/* cr_ == conn->request */
#define cr_status request->status
#define cr_flags request->flags
#define cr_proto request->proto
/* rh_ == request->hooks->on_ */
#define rh_err hooks->on_error
#define rh_err_arg hooks->on_error_arg
#ifndef EVHTP_DISABLE_MEMFUNCTIONS
static void * (*malloc_)(size_t sz) = malloc;
static void * (* realloc_)(void * d, size_t sz) = realloc;
static void (* free_)(void * d) = free;
/**
* @brief Wrapper for malloc so that a different malloc can be used
* if desired.
*
* @see evhtp_set_mem_functions
*
* @param size size_t of memory to be allocated
*
* @return void * to malloc'd memory or NULL if fail
*/
static void *
htp__malloc_(size_t size)
{
return malloc_(size);
}
#ifdef NOTUSED
/**
* @brief Wrapper for realloc so that a different realloc can be used
* if desired.
*
* @see evhtp_set_mem_functions
*
* @param ptr current memory ptr
* @param size size_t of memory to be allocated
*
* @return void * to newly realloc'd memory or NULL if fail
*/
static void *
htp__realloc_(void * ptr, size_t size)
{
return realloc_(ptr, size);
}
#endif
/**
* @brief Wrapper for free so that a different free can be used
* if desired.
*
* @see evhtp_set_mem_functions
*
* @param ptr pointer to memory to be freed.
*
*/
static void
htp__free_(void * ptr)
{
if (ptr == NULL) {
return;
}
evhtp_safe_free(ptr, free_);
}
/**
* @brief Wrapper for calloc so that a different calloc can be used
* if desired.
*
* @see evhtp_set_mem_functions
*
* @param nmemb number of members (as a size_t)
* @param size size of member blocks (as a size_t)
*
* @return void * to new memory block
*/
static void *
htp__calloc_(size_t nmemb, size_t size)
{
if (malloc_ != malloc) {
size_t len = nmemb * size;
void * p;
if ((p = malloc_(len)) == NULL) {
return NULL;
}
memset(p, 0, len);
return p;
}
return calloc(nmemb, size);
}
/**
* @brief implementation of strdup function.
*
* @param str - null terminated string.
*
* @return duplicate of string or NULL if fail
*
*/
static char *
htp__strdup_(const char * str)
{
if (malloc_ != malloc) {
size_t len;
void * p;
len = strlen(str);
if ((p = malloc_(len + 1)) == NULL) {
return NULL;
}
memcpy(p, str, len + 1);
return p;
}
return strdup(str);
}
/**
* @brief implementation of strndup function.
*
* @param str - null terminated string.
* @param len - size_t length off string
*
* @return duplicate of string or NULL if fail
*
*/
static char *
htp__strndup_(const char * str, size_t len)
{
if (malloc_ != malloc) {
char * p;
if ((p = malloc_(len + 1)) != NULL) {
memcpy(p, str, len + 1);
} else {
return NULL;
}
p[len] = '\0';
return p;
}
return strndup(str, len);
}
#else
#define htp__malloc_(sz) malloc(sz)
#define htp__calloc_(n, sz) calloc(n, sz)
#define htp__strdup_(s) strdup(s)
#define htp__strndup_(n, sz) strndup(n, sz)
#define htp__realloc_(p, sz) realloc(p, sz)
#define htp__free_(p) free(p)
#endif
void
evhtp_set_mem_functions(void *(*mallocfn_)(size_t len),
void *(*reallocfn_)(void * p, size_t sz),
void (*freefn_)(void * p))
{
#ifndef EVHTP_DISABLE_MEMFUNCTIONS
malloc_ = mallocfn_;
realloc_ = reallocfn_;
free_ = freefn_;
return event_set_mem_functions(malloc_, realloc_, free_);
#endif
}
/**
* @brief returns string status code from enum code
*
* @param code as evhtp_res enum
*
* @return string corresponding to code, else UNKNOWN
*/
static const char *
status_code_to_str(evhtp_res code)
{
switch (code) {
case EVHTP_RES_200:
return "OK";
case EVHTP_RES_300:
return "Redirect";
case EVHTP_RES_400:
return "Bad Request";
case EVHTP_RES_NOTFOUND:
return "Not Found";
case EVHTP_RES_SERVERR:
return "Internal Server Error";
case EVHTP_RES_CONTINUE:
return "Continue";
case EVHTP_RES_FORBIDDEN:
return "Forbidden";
case EVHTP_RES_SWITCH_PROTO:
return "Switching Protocols";
case EVHTP_RES_MOVEDPERM:
return "Moved Permanently";
case EVHTP_RES_PROCESSING:
return "Processing";
case EVHTP_RES_URI_TOOLONG:
return "URI Too Long";
case EVHTP_RES_CREATED:
return "Created";
case EVHTP_RES_ACCEPTED:
return "Accepted";
case EVHTP_RES_NAUTHINFO:
return "No Auth Info";
case EVHTP_RES_NOCONTENT:
return "No Content";
case EVHTP_RES_RSTCONTENT:
return "Reset Content";
case EVHTP_RES_PARTIAL:
return "Partial Content";
case EVHTP_RES_MSTATUS:
return "Multi-Status";
case EVHTP_RES_IMUSED:
return "IM Used";
case EVHTP_RES_FOUND:
return "Found";
case EVHTP_RES_SEEOTHER:
return "See Other";
case EVHTP_RES_NOTMOD:
return "Not Modified";
case EVHTP_RES_USEPROXY:
return "Use Proxy";
case EVHTP_RES_SWITCHPROXY:
return "Switch Proxy";
case EVHTP_RES_TMPREDIR:
return "Temporary Redirect";
case EVHTP_RES_UNAUTH:
return "Unauthorized";
case EVHTP_RES_PAYREQ:
return "Payment Required";
case EVHTP_RES_METHNALLOWED:
return "Not Allowed";
case EVHTP_RES_NACCEPTABLE:
return "Not Acceptable";
case EVHTP_RES_PROXYAUTHREQ:
return "Proxy Authentication Required";
case EVHTP_RES_TIMEOUT:
return "Request Timeout";
case EVHTP_RES_CONFLICT:
return "Conflict";
case EVHTP_RES_GONE:
return "Gone";
case EVHTP_RES_LENREQ:
return "Length Required";
case EVHTP_RES_PRECONDFAIL:
return "Precondition Failed";
case EVHTP_RES_ENTOOLARGE:
return "Entity Too Large";
case EVHTP_RES_URITOOLARGE:
return "Request-URI Too Long";
case EVHTP_RES_UNSUPPORTED:
return "Unsupported Media Type";
case EVHTP_RES_RANGENOTSC:
return "Requested Range Not Satisfiable";
case EVHTP_RES_EXPECTFAIL:
return "Expectation Failed";
case EVHTP_RES_IAMATEAPOT:
return "I'm a teapot";
case EVHTP_RES_NOTIMPL:
return "Not Implemented";
case EVHTP_RES_BADGATEWAY:
return "Bad Gateway";
case EVHTP_RES_SERVUNAVAIL:
return "Service Unavailable";
case EVHTP_RES_GWTIMEOUT:
return "Gateway Timeout";
case EVHTP_RES_VERNSUPPORT:
return "HTTP Version Not Supported";
case EVHTP_RES_BWEXEED:
return "Bandwidth Limit Exceeded";
} /* switch */
return "UNKNOWN";
} /* status_code_to_str */
#ifndef EVHTP_DISABLE_SSL
static int session_id_context = 1;
#ifndef EVHTP_DISABLE_EVTHR
static int ssl_num_locks;
static evhtp_mutex_t * ssl_locks;
static int ssl_locks_initialized = 0;
#endif
#endif
/*
* COMPAT FUNCTIONS
*/
#ifdef NO_STRNLEN
/**
* @brief Implementation of strnlen function if none exists.
*
* @param s - null terminated character string
* @param maxlen - maximum length of string
*
* @return length of string
*
*/
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
/**
* @brief Implementation of strndup if none exists.
*
* @param s - const char * to null terminated string
* @param n - size_t maximum legnth of string
*
* @return length limited string duplicate or NULL if fail
*
*/
static char *
strndup(const char * s, size_t n)
{
size_t len = strnlen(s, n);
char * ret;
if (len < n) {
return htp__strdup_(s);
}
if ((ret = htp__malloc_(n + 1)) == NULL) {
return NULL;
}
ret[n] = '\0';
memcpy(ret, s, n);
return ret;
}
#endif
/*
* PRIVATE FUNCTIONS
*/
/**
*
* @brief helper macro 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
*/
#define htp__is_http_11_(_major, _minor) \
(_major >= 1 && _minor >= 1)
/**
* @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
*/
#define htp__is_http_10_(_major, _minor) \
(_major >= 1 && _minor <= 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
htp__protocol_(const char major, const char minor)
{
if (htp__is_http_10_(major, minor)) {
return EVHTP_PROTO_10;
}
if (htp__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
htp__hook_path_(struct evhtp_request * request, struct evhtp_path * 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
htp__hook_header_(struct evhtp_request * 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
htp__hook_headers_(struct evhtp_request * 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
htp__hook_body_(struct evhtp_request * request, struct evbuffer * buf)
{
if (request == NULL) {
return EVHTP_RES_500;
}
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
htp__hook_request_fini_(struct evhtp_request * request)
{
if (request == NULL) {
return EVHTP_RES_500;
}
HOOK_REQUEST_RUN_NARGS(request, on_request_fini);
return EVHTP_RES_OK;
}
/**
* @brief Runs the user defined request hook
*
* @param request
* @param len
* @return
*/
static inline evhtp_res
htp__hook_chunk_new_(struct evhtp_request * request, uint64_t len)
{
HOOK_REQUEST_RUN(request, on_new_chunk, len);
return EVHTP_RES_OK;
}
/**
* @brief Runs the user defined on_chunk_fini hook
*
* @param request
* @return
*/
static inline evhtp_res
htp__hook_chunk_fini_(struct evhtp_request * request)
{
HOOK_REQUEST_RUN_NARGS(request, on_chunk_fini);
return EVHTP_RES_OK;
}
/**
* @brief Runs the user defined on chunk_finis hook
*
* @param request
* @return
*/
static inline evhtp_res
htp__hook_chunks_fini_(struct evhtp_request * request)
{
HOOK_REQUEST_RUN_NARGS(request, on_chunks_fini);
return EVHTP_RES_OK;
}
/**
* @brief Runs the user defined on_headers_start hook
*
* @param request
* @return
*/
static inline evhtp_res
htp__hook_headers_start_(struct evhtp_request * 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
htp__hook_connection_fini_(struct evhtp_connection * connection)
{
if (evhtp_unlikely(connection == NULL)) {
return 500;
}
if (connection->hooks != NULL && connection->ch_fini != NULL) {
return (connection->ch_fini)(connection, connection->ch_fini_arg);
}
return EVHTP_RES_OK;
}
/**
* @brief runs the user-defined hook when a connection error occurs
*
* @param request the request structure
* @param errtype the error that ocurred
*/
static inline void
htp__hook_error_(struct evhtp_request * request, evhtp_error_flags errtype)
{
if (request && request->hooks && request->rh_err) {
(*request->rh_err)(request, errtype, request->rh_err_arg);
}
}
/**
* @brief runs the user-defined hook when a connection error occurs
*
* @param connection the connection structure
* @param errtype the error that ocurred
*/
static inline evhtp_res
htp__hook_connection_error_(struct evhtp_connection * connection, evhtp_error_flags errtype)
{
if (connection == NULL) {
return EVHTP_RES_FATAL;
}
if (connection->request != NULL) {
htp__hook_error_(connection->request, errtype);
}
return EVHTP_RES_OK;
}
/**
* @brief Runs the user defined hostname processing hook
*
* @param r
* @param hostname
* @return
*/
static inline evhtp_res
htp__hook_hostname_(struct evhtp_request * r, const char * hostname)
{
HOOK_REQUEST_RUN(r, on_hostname, hostname);
return EVHTP_RES_OK;
}
/**
* @brief Runs the user defined on_write hook
*
* @param connection
* @return
*/
static inline evhtp_res
htp__hook_connection_write_(struct evhtp_connection * 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
htp__glob_match_(const char * pattern, size_t plen,
const char * string, size_t str_len)
{
while (plen) {
switch (pattern[0]) {
case '*':
while (pattern[1] == '*') {
pattern++;
plen--;
}
if (plen == 1) {
return 1; /* match */
}
while (str_len) {
if (htp__glob_match_(pattern + 1, plen - 1,
string, str_len)) {
return 1; /* match */
}
string++;
str_len--;
}
return 0; /* no match */
default:
if (pattern[0] != string[0]) {
return 0; /* no match */
}
string++;
str_len--;
break;
} /* switch */
pattern++;
plen--;
if (str_len == 0) {
while (*pattern == '*') {
pattern++;
plen--;
}
break;
}
}
if (plen == 0 && str_len == 0) {
return 1;
}
return 0;
} /* htp__glob_match_ */
/**
* @brief Locates a given callback offsets performs a regex pattern match
*
* @param [IN] cbs ptr to evhtp_callbacks_t structure
* @param [IN] path
* @param [OUT] start_offset
* @param [OUT] end_offset
* @return
*/
static evhtp_callback_t *
htp__callback_find_(evhtp_callbacks_t * cbs,
const char * path,
unsigned int * start_offset,
unsigned int * end_offset)
{
size_t path_len;
evhtp_callback_t * callback;
#ifndef EVHTP_DISABLE_REGEX
regmatch_t pmatch[28];
#endif
if (evhtp_unlikely(cbs == NULL)) {
return NULL;
}
path_len = strlen(path);
TAILQ_FOREACH(callback, cbs, next) {
switch (callback->type) {
case evhtp_callback_type_hash:
if (strncmp(path, callback->val.path, callback->len) == 0) {
*start_offset = 0;
*end_offset = path_len;
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:
{
size_t glob_len = strlen(callback->val.glob);
if (htp__glob_match_(callback->val.glob,
glob_len,
path,
path_len) == 1) {
*start_offset = 0;
*end_offset = path_len;
return callback;
}
}
default:
break;
} /* switch */
}
return NULL;
} /* htp__callback_find_ */
/**
* @brief Correctly frees the evhtp_path_t ptr that is passed in.
* @param path
*/
static void
htp__path_free_(struct evhtp_path * path)
{
if (evhtp_unlikely(path == NULL)) {
return;
}
evhtp_safe_free(path->full, htp__free_);
evhtp_safe_free(path->path, htp__free_);
evhtp_safe_free(path->file, htp__free_);
evhtp_safe_free(path->match_start, htp__free_);
evhtp_safe_free(path->match_end, htp__free_);
evhtp_safe_free(path, htp__free_);
}
/**
* @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 the unallocated destination buffer.
* @param data raw input data (assumes a /path/[file] structure)
* @param len length of the input data
*
* @return 0 on success, -1 on error.
*/
static int
htp__path_new_(evhtp_path_t ** out, const char * data, size_t len)
{
struct evhtp_path * req_path = NULL;
const char * data_end = (const char *)(data + len);
char * path = NULL;
char * file = NULL;
req_path = htp__calloc_(1, sizeof(*req_path));