This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
/
parser.c
2254 lines (1897 loc) · 68 KB
/
parser.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 <stddef.h>
#include <ctype.h>
#include <string.h>
#ifdef EVHTP_HAS_SYS_TYPES
#include <sys/types.h>
#endif
#include "internal.h"
#include "evhtp/parser.h"
#include "evhtp/config.h"
#if '\n' != '\x0a' || 'A' != 65
#error "You have somehow found a non-ASCII host. We can't build here."
#endif
#define PARSER_STACK_MAX 8192
#define LF (unsigned char)10
#define CR (unsigned char)13
#define CRLF "\x0d\x0a"
enum eval_hdr_val {
eval_hdr_val_none = 0,
eval_hdr_val_connection,
eval_hdr_val_proxy_connection,
eval_hdr_val_content_length,
eval_hdr_val_transfer_encoding,
eval_hdr_val_hostname,
eval_hdr_val_content_type
};
enum parser_flags {
parser_flag_chunked = (1 << 0),
parser_flag_connection_keep_alive = (1 << 1),
parser_flag_connection_close = (1 << 2),
parser_flag_trailing = (1 << 3),
};
enum parser_state {
s_start = 0,
s_method,
s_spaces_before_uri,
s_schema,
s_schema_slash,
s_schema_slash_slash,
s_host,
s_host_ipv6,
s_host_done,
s_port,
s_after_slash_in_uri,
s_check_uri,
s_uri,
s_http_09,
s_http_H,
s_http_HT,
s_http_HTT,
s_http_HTTP,
s_first_major_digit,
s_major_digit,
s_first_minor_digit,
s_minor_digit,
s_spaces_after_digit,
s_almost_done,
s_done,
s_hdrline_start,
s_hdrline_hdr_almost_done,
s_hdrline_hdr_done,
s_hdrline_hdr_key,
s_hdrline_hdr_space_before_val,
s_hdrline_hdr_val,
s_hdrline_almost_done,
s_hdrline_done,
s_body_read,
s_chunk_size_start,
s_chunk_size,
s_chunk_size_almost_done,
s_chunk_data,
s_chunk_data_almost_done,
s_chunk_data_done,
s_status,
s_space_after_status,
s_status_text
};
typedef enum eval_hdr_val eval_hdr_val;
typedef enum parser_flags parser_flags;
typedef enum parser_state parser_state;
struct htparser {
htpparse_error error;
parser_state state;
parser_flags flags;
eval_hdr_val heval;
htp_type type;
htp_scheme scheme;
htp_method method;
unsigned char multipart;
unsigned char major;
unsigned char minor;
uint64_t content_len; /* this gets decremented as data passes through */
uint64_t orig_content_len; /* this contains the original length of the body */
uint64_t bytes_read;
uint64_t total_bytes_read;
unsigned int status; /* only for responses */
unsigned int status_count; /* only for responses */
char * scheme_offset;
char * host_offset;
char * port_offset;
char * path_offset;
char * args_offset;
void * userdata;
size_t buf_idx;
/* Must be last since htparser_init memsets up to the offset of this buffer */
char buf[PARSER_STACK_MAX];
};
#ifdef EVHTP_DEBUG
static void
log_htparser__s_(struct htparser * p)
{
log_debug(
"struct htparser {\n"
" htpparse_error = %d\n"
" parser_state = %d\n"
" parser_flags = %d\n"
" eval_hdr_val = %d\n"
" htp_type = %d\n"
" htp_scheme = %d\n"
" htp_method = %d\n"
" multipart = %c\n"
" major = %c\n"
" minor = %c\n"
" content_len = %zu\n"
" orig_clen = %zu\n"
" bytes_read = %zu\n"
" total_read = %zu\n"
" status = %d\n"
" status_count = %d\n"
" scheme_offset = %s\n"
" host_offset = %s\n"
" port_offset = %s\n"
" path_offset = %s\n"
" args_offset = %s\n"
" userdata = %p\n"
" buf_idx = %zu\n"
" buf = %s\n"
"};",
p->error,
p->state,
p->flags,
p->heval,
p->type,
p->scheme,
p->method,
p->multipart,
p->major,
p->minor,
p->content_len,
p->orig_content_len,
p->bytes_read,
p->total_bytes_read,
p->status,
p->status_count,
p->scheme_offset,
p->host_offset,
p->port_offset,
p->path_offset,
p->args_offset,
p->userdata,
p->buf_idx,
p->buf);
} /* log_htparser__s_ */
#else
#define log_htparser__s_(p)
#endif
static uint32_t usual[] = {
0xffffdbfe,
0x7fff37d6,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff
};
static int8_t unhex[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
static const char * errstr_map[] = {
"htparse_error_none",
"htparse_error_too_big",
"htparse_error_invalid_method",
"htparse_error_invalid_requestline",
"htparse_error_invalid_schema",
"htparse_error_invalid_protocol",
"htparse_error_invalid_version",
"htparse_error_invalid_header",
"htparse_error_invalid_chunk_size",
"htparse_error_invalid_chunk",
"htparse_error_invalid_state",
"htparse_error_user",
"htparse_error_status",
"htparse_error_unknown"
};
static const char * method_strmap[] = {
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"MKCOL",
"COPY",
"MOVE",
"OPTIONS",
"PROPFIND",
"PROPATCH",
"LOCK",
"UNLOCK",
"TRACE",
"CONNECT",
"PATCH",
};
#define _MIN_READ(a, b) ((a) < (b) ? (a) : (b))
#ifndef HOST_BIG_ENDIAN
/* Little-endian cmp macros */
#define _str3_cmp(m, c0, c1, c2, c3) \
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)
#define _str3Ocmp(m, c0, c1, c2, c3) \
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)
#define _str4cmp(m, c0, c1, c2, c3) \
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)
#define _str5cmp(m, c0, c1, c2, c3, c4) \
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& m[4] == c4
#define _str6cmp(m, c0, c1, c2, c3, c4, c5) \
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& (((uint32_t *)m)[1] & 0xffff) == ((c5 << 8) | c4)
#define _str7_cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& ((uint32_t *)m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4)
#define _str8cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& ((uint32_t *)m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4)
#define _str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \
*(uint32_t *)m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& ((uint32_t *)m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4) \
&& m[8] == c8
#else
/* Big endian cmp macros */
#define _str3_cmp(m, c0, c1, c2, c3) \
m[0] == c0 && m[1] == c1 && m[2] == c2
#define _str3Ocmp(m, c0, c1, c2, c3) \
m[0] == c0 && m[2] == c2 && m[3] == c3
#define _str4cmp(m, c0, c1, c2, c3) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3
#define _str5cmp(m, c0, c1, c2, c3, c4) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 && m[4] == c4
#define _str6cmp(m, c0, c1, c2, c3, c4, c5) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \
&& m[4] == c4 && m[5] == c5
#define _str7_cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \
&& m[4] == c4 && m[5] == c5 && m[6] == c6
#define _str8cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \
&& m[4] == c4 && m[5] == c5 && m[6] == c6 && m[7] == c7
#define _str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \
&& m[4] == c4 && m[5] == c5 && m[6] == c6 && m[7] == c7 && m[8] == c8
#endif
#define __HTPARSE_GENHOOK(__n) \
static inline int hook_ ## __n ## _run(htparser * p, htparse_hooks * hooks) { \
log_debug("enter"); \
if (hooks && (hooks)->__n) \
{ \
return (hooks)->__n(p); \
} \
\
return 0; \
}
#define __HTPARSE_GENDHOOK(__n) \
static inline int hook_ ## __n ## _run(htparser * p, \
htparse_hooks * hooks, \
const char * s, size_t l) { \
log_debug("enter"); \
if (hooks && (hooks)->__n) \
{ \
return (hooks)->__n(p, s, l); \
} \
\
return 0; \
}
__HTPARSE_GENHOOK(on_msg_begin)
__HTPARSE_GENHOOK(on_hdrs_begin)
__HTPARSE_GENHOOK(on_hdrs_complete)
__HTPARSE_GENHOOK(on_new_chunk)
__HTPARSE_GENHOOK(on_chunk_complete)
__HTPARSE_GENHOOK(on_chunks_complete)
__HTPARSE_GENHOOK(on_msg_complete)
__HTPARSE_GENDHOOK(method)
__HTPARSE_GENDHOOK(scheme)
__HTPARSE_GENDHOOK(host)
__HTPARSE_GENDHOOK(port)
__HTPARSE_GENDHOOK(path)
__HTPARSE_GENDHOOK(args)
__HTPARSE_GENDHOOK(uri)
__HTPARSE_GENDHOOK(hdr_key)
__HTPARSE_GENDHOOK(hdr_val)
__HTPARSE_GENDHOOK(body)
__HTPARSE_GENDHOOK(hostname)
static inline uint64_t
str_to_uint64(char * str, size_t n, int * err)
{
uint64_t value;
/* Trim whitespace after value. */
while (n && isblank(str[n - 1]))
{
n--;
}
if (n > 20)
{
/* 18446744073709551615 is 20 bytes */
*err = 1;
return 0;
}
for (value = 0; n--; str++)
{
uint64_t check;
if (*str < '0' || *str > '9')
{
*err = 1;
return 0;
}
check = value * 10 + (*str - '0');
if ((value && check <= value))
{
*err = 1;
return 0;
}
value = check;
}
return value;
}
static inline ssize_t
_str_to_ssize_t(char * str, size_t n)
{
ssize_t value;
if (n == 0)
{
return -1;
}
for (value = 0; n--; str++)
{
if (*str < '0' || *str > '9')
{
return -1;
}
value = value * 10 + (*str - '0');
#if 0
if (value > INTMAX_MAX)
{
return -1;
}
#endif
}
return value;
}
htpparse_error
htparser_get_error(htparser * p)
{
return p->error;
}
const char *
htparser_get_strerror(htparser * p)
{
htpparse_error e = htparser_get_error(p);
if (e > htparse_error_generic)
{
return "htparse_no_such_error";
}
return errstr_map[e];
}
unsigned int
htparser_get_status(htparser * p)
{
return p->status;
}
int
htparser_should_keep_alive(htparser * p)
{
if (p->major > 0 && p->minor > 0)
{
if (p->flags & parser_flag_connection_close)
{
return 0;
} else {
return 1;
}
} else {
if (p->flags & parser_flag_connection_keep_alive)
{
return 1;
} else {
return 0;
}
}
return 0;
}
htp_scheme
htparser_get_scheme(htparser * p)
{
return p->scheme;
}
htp_method
htparser_get_method(htparser * p)
{
return p->method;
}
const char *
htparser_get_methodstr_m(htp_method meth)
{
if (meth >= htp_method_UNKNOWN)
{
return NULL;
}
return method_strmap[meth];
}
const char *
htparser_get_methodstr(htparser * p)
{
return htparser_get_methodstr_m(p->method);
}
void
htparser_set_major(htparser * p, unsigned char major)
{
p->major = major;
}
void
htparser_set_minor(htparser * p, unsigned char minor)
{
p->minor = minor;
}
unsigned char
htparser_get_major(htparser * p)
{
return p->major;
}
unsigned char
htparser_get_minor(htparser * p)
{
return p->minor;
}
unsigned char
htparser_get_multipart(htparser * p)
{
return p->multipart;
}
void *
htparser_get_userdata(htparser * p)
{
return p->userdata;
}
void
htparser_set_userdata(htparser * p, void * ud)
{
p->userdata = ud;
}
uint64_t
htparser_get_content_pending(htparser * p)
{
return p->content_len;
}
uint64_t
htparser_get_content_length(htparser * p)
{
return p->orig_content_len;
}
uint64_t
htparser_get_bytes_read(htparser * p)
{
return p->bytes_read;
}
uint64_t
htparser_get_total_bytes_read(htparser * p)
{
return p->total_bytes_read;
}
void
htparser_init(htparser * p, htp_type type)
{
/* Do not memset entire string buffer. */
//memset(p, 0, offsetof(htparser, buf));
p->buf[0] = '\0';
p->state = s_start;
p->error = htparse_error_none;
p->method = htp_method_UNKNOWN;
p->type = type;
}
htparser *
htparser_new(void)
{
return malloc(sizeof(htparser));
}
static int
is_host_char(unsigned char ch)
{
char c = (unsigned char)(ch | 0x20);
if (c >= 'a' && c <= 'z')
{
return 1;
}
if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-')
{
return 1;
}
return 0;
}
static htp_method
get_method(const char * m, const size_t sz)
{
switch (sz) {
case 3:
if (_str3_cmp(m, 'G', 'E', 'T', '\0'))
{
return htp_method_GET;
}
if (_str3_cmp(m, 'P', 'U', 'T', '\0'))
{
return htp_method_PUT;
}
break;
case 4:
if (m[1] == 'O')
{
if (_str3Ocmp(m, 'P', 'O', 'S', 'T'))
{
return htp_method_POST;
}
if (_str3Ocmp(m, 'C', 'O', 'P', 'Y'))
{
return htp_method_COPY;
}
if (_str3Ocmp(m, 'M', 'O', 'V', 'E'))
{
return htp_method_MOVE;
}
if (_str3Ocmp(m, 'L', 'O', 'C', 'K'))
{
return htp_method_LOCK;
}
} else {
if (_str4cmp(m, 'H', 'E', 'A', 'D'))
{
return htp_method_HEAD;
}
}
break;
case 5:
if (_str5cmp(m, 'M', 'K', 'C', 'O', 'L'))
{
return htp_method_MKCOL;
}
if (_str5cmp(m, 'T', 'R', 'A', 'C', 'E'))
{
return htp_method_TRACE;
}
if (_str5cmp(m, 'P', 'A', 'T', 'C', 'H'))
{
return htp_method_PATCH;
}
break;
case 6:
if (_str6cmp(m, 'D', 'E', 'L', 'E', 'T', 'E'))
{
return htp_method_DELETE;
}
if (_str6cmp(m, 'U', 'N', 'L', 'O', 'C', 'K'))
{
return htp_method_UNLOCK;
}
break;
case 7:
if (_str7_cmp(m, 'O', 'P', 'T', 'I', 'O', 'N', 'S', '\0'))
{
return htp_method_OPTIONS;
}
if (_str7_cmp(m, 'C', 'O', 'N', 'N', 'E', 'C', 'T', '\0'))
{
return htp_method_CONNECT;
}
break;
case 8:
if (_str8cmp(m, 'P', 'R', 'O', 'P', 'F', 'I', 'N', 'D'))
{
return htp_method_PROPFIND;
}
break;
case 9:
if (_str9cmp(m, 'P', 'R', 'O', 'P', 'P', 'A', 'T', 'C', 'H'))
{
return htp_method_PROPPATCH;
}
break;
} /* switch */
return htp_method_UNKNOWN;
} /* get_method */
#define HTP_SET_BUF(CH) do { \
if (evhtp_likely((p->buf_idx + 1) < PARSER_STACK_MAX)) { \
p->buf[p->buf_idx++] = CH; \
p->buf[p->buf_idx] = '\0'; \
} else { \
p->error = htparse_error_too_big; \
return i + 1; \
} \
} while (0)
size_t
htparser_run(htparser * p, htparse_hooks * hooks, const char * data, size_t len)
{
unsigned char ch;
char c;
size_t i;
log_debug("enter");
log_debug("p == %p", p);
p->error = htparse_error_none;
p->bytes_read = 0;
for (i = 0; i < len; i++)
{
int res;
int err;
ch = data[i];
log_debug("[%p] data[%zu] = %c (%x)", p, i, isprint(ch) ? ch : ' ', ch);
p->total_bytes_read += 1;
p->bytes_read += 1;
switch (p->state) {
case s_start:
log_debug("[%p] s_start", p);
if (ch == CR || ch == LF)
{
break;
}
if ((ch < 'A' || ch > 'Z') && ch != '_')
{
p->error = htparse_error_inval_reqline;
log_debug("s_start invalid fist char '%c'", ch);
log_htparser__s_(p);
return i + 1;
}
p->flags = 0;
p->error = htparse_error_none;
p->method = htp_method_UNKNOWN;
p->multipart = 0;
p->major = 0;
p->minor = 0;
p->content_len = 0;
p->orig_content_len = 0;
p->status = 0;
p->status_count = 0;
p->scheme_offset = NULL;
p->host_offset = NULL;
p->port_offset = NULL;
p->path_offset = NULL;
p->args_offset = NULL;
p->buf_idx = 0;
res = hook_on_msg_begin_run(p, hooks);
HTP_SET_BUF(ch);
if (evhtp_likely(p->type == htp_type_request)) {
p->state = s_method;
} else if (p->type == htp_type_response && ch == 'H') {
p->state = s_http_H;
} else {
log_debug("not type of request or response?");
log_htparser__s_(p);
p->error = htparse_error_inval_reqline;
return i + 1;
}
if (res)
{
p->error = htparse_error_user;
return i + 1;
}
break;
case s_method:
log_debug("[%p] s_method", p);
do {
if (ch == ' ')
{
p->method = get_method(p->buf, p->buf_idx);
res = hook_method_run(p, hooks, p->buf, p->buf_idx);
p->buf_idx = 0;
p->state = s_spaces_before_uri;
if (res)
{
p->error = htparse_error_user;
return i + 1;
}
break;
} else {
if ((ch < 'A' || ch > 'Z') && ch != '_')
{
p->error = htparse_error_inval_method;
return i + 1;
}
HTP_SET_BUF(ch);
}
if (evhtp_unlikely(i + 1 >= len)) {
break;
}
ch = data[++i];
} while (i < len);
break;
case s_spaces_before_uri:
log_debug("[%p] s_spaces_before_uri", p);
/* CONNECT is special - RFC 2817 section 5.2:
* The Request-URI portion of the Request-Line is
* always an 'authority' as defined by URI Generic
* Syntax [2], which is to say the host name and port
* number destination of the requested connection
* separated by a colon
*/
if (p->method == htp_method_CONNECT)
{
switch (ch) {
case ' ':
break;
case '[':
/* Literal IPv6 address start. */
HTP_SET_BUF(ch);
p->host_offset = &p->buf[p->buf_idx];
p->state = s_host_ipv6;
break;
default:
if (!is_host_char(ch))
{
p->error = htparse_error_inval_reqline;
log_htparser__s_(p);
return i + 1;
}
p->host_offset = &p->buf[p->buf_idx];
HTP_SET_BUF(ch);
p->state = s_host;
break;
} /* switch */
break;
}
switch (ch) {
case ' ':
break;
case '/':
p->path_offset = &p->buf[p->buf_idx];
HTP_SET_BUF(ch);
p->state = s_after_slash_in_uri;
break;
default:
c = (unsigned char)(ch | 0x20);
if (c >= 'a' && c <= 'z') {
p->scheme_offset = &p->buf[p->buf_idx];
HTP_SET_BUF(ch);
p->state = s_schema;
break;
}
p->error = htparse_error_inval_reqline;
log_htparser__s_(p);
return i + 1;
} /* switch */
break;
case s_schema:
log_debug("[%p] s_schema", p);
c = (unsigned char)(ch | 0x20);
if (c >= 'a' && c <= 'z') {
HTP_SET_BUF(ch);
break;
}
switch (ch) {
case ':':
p->scheme = htp_scheme_unknown;
switch (p->buf_idx) {
case 3:
if (_str3_cmp(p->scheme_offset, 'f', 't', 'p', '\0'))
{
p->scheme = htp_scheme_ftp;
break;
}
if (_str3_cmp(p->scheme_offset, 'n', 'f', 's', '\0'))
{
p->scheme = htp_scheme_nfs;
break;
}
break;
case 4:
if (_str4cmp(p->scheme_offset, 'h', 't', 't', 'p'))
{
p->scheme = htp_scheme_http;
break;
}
break;
case 5:
if (_str5cmp(p->scheme_offset, 'h', 't', 't', 'p', 's'))
{
p->scheme = htp_scheme_https;
break;
}
break;
} /* switch */
res = hook_scheme_run(p, hooks,
p->scheme_offset,
(&p->buf[p->buf_idx] - p->scheme_offset));
HTP_SET_BUF(ch);
p->state = s_schema_slash;
if (res) {
p->error = htparse_error_user;
return i + 1;
}
break;
default:
p->error = htparse_error_inval_schema;
return i + 1;
} /* switch */
break;
case s_schema_slash:
log_debug("[%p] s_schema_slash", p);
switch (ch) {
case '/':
HTP_SET_BUF(ch);
p->state = s_schema_slash_slash;
break;
default:
p->error = htparse_error_inval_schema;
return i + 1;
}
break;
case s_schema_slash_slash:
log_debug("[%p] s_schema_slash_slash", p);
switch (ch) {
case '/':