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
|
/*
* Copyright (C) NGINX, Inc.
* Copyright (C) Valentin V. Bartenev
*/
#include <nxt_main.h>
typedef struct {
nxt_http_field_handler_t handler;
uintptr_t data;
union {
uint8_t str[8];
uint64_t ui64;
} key[];
} nxt_http_fields_hash_entry_t;
#define nxt_http_fields_hash_next_entry(entry, n) \
((nxt_http_fields_hash_entry_t *) ((u_char *) (entry) \
+ sizeof(nxt_http_fields_hash_entry_t) \
+ n * 8))
struct nxt_http_fields_hash_s {
size_t min_length;
size_t max_length;
void *long_fields;
nxt_http_fields_hash_entry_t *entries[];
};
static nxt_int_t nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp,
u_char **pos, u_char *end);
static nxt_int_t nxt_http_parse_request_line(nxt_http_request_parse_t *rp,
u_char **pos, u_char *end);
static nxt_int_t nxt_http_parse_field_name(nxt_http_request_parse_t *rp,
u_char **pos, u_char *end);
static nxt_int_t nxt_http_parse_field_value(nxt_http_request_parse_t *rp,
u_char **pos, u_char *end);
static u_char *nxt_http_lookup_field_end(u_char *p, u_char *end);
static nxt_int_t nxt_http_parse_field_end(nxt_http_request_parse_t *rp,
u_char **pos, u_char *end);
static nxt_http_fields_hash_entry_t *nxt_http_fields_hash_lookup(
nxt_http_fields_hash_t *hash, uint64_t *key, nxt_str_t *value);
static nxt_http_fields_hash_entry_t *nxt_http_header_fields_hash_lookup_long(
nxt_http_fields_hash_t *hash, nxt_str_t *value);
typedef enum {
NXT_HTTP_TARGET_SPACE = 1, /* \s */
NXT_HTTP_TARGET_HASH, /* # */
NXT_HTTP_TARGET_AGAIN,
NXT_HTTP_TARGET_BAD, /* \0\r\n */
/* traps below are used for extended check only */
NXT_HTTP_TARGET_SLASH = 5, /* / */
NXT_HTTP_TARGET_DOT, /* . */
NXT_HTTP_TARGET_ARGS_MARK, /* ? */
NXT_HTTP_TARGET_QUOTE_MARK, /* % */
NXT_HTTP_TARGET_PLUS, /* + */
} nxt_http_target_traps_e;
static const uint8_t nxt_http_target_chars[256] nxt_aligned(64) = {
/* \0 \n \r */
4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 4, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* \s ! " # $ % & ' ( ) * + , - . / */
1, 0, 0, 2, 0, 8, 0, 0, 0, 0, 0, 9, 0, 0, 6, 5,
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
};
nxt_inline nxt_http_target_traps_e
nxt_http_parse_target(u_char **pos, u_char *end)
{
u_char *p;
nxt_uint_t trap;
p = *pos;
for ( ;; ) {
if (nxt_slow_path(end - p < 10)) {
return NXT_HTTP_TARGET_AGAIN;
}
#define nxt_http_parse_target_step \
{ \
trap = nxt_http_target_chars[*p]; \
\
if (nxt_slow_path(trap != 0)) { \
break; \
} \
\
p++; \
}
nxt_http_parse_target_step
nxt_http_parse_target_step
nxt_http_parse_target_step
nxt_http_parse_target_step
nxt_http_parse_target_step
nxt_http_parse_target_step
nxt_http_parse_target_step
nxt_http_parse_target_step
nxt_http_parse_target_step
nxt_http_parse_target_step
#undef nxt_http_parse_target_step
}
*pos = p;
return trap;
}
nxt_int_t
nxt_http_parse_request(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b)
{
nxt_int_t rc;
if (rp->handler == NULL) {
rp->handler = &nxt_http_parse_request_line;
}
do {
rc = rp->handler(rp, &b->pos, b->free);
} while (rc == NXT_OK);
return rc;
}
static nxt_int_t
nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos,
u_char *end)
{
u_char *p, ch, *after_slash;
nxt_int_t rc;
nxt_http_ver_t version;
nxt_http_target_traps_e trap;
static const nxt_http_ver_t http11 = { "HTTP/1.1" };
static const nxt_http_ver_t http10 = { "HTTP/1.0" };
p = *pos;
rp->method.start = p;
for ( ;; p++) {
for ( ;; ) {
if (nxt_slow_path(end - p < 12)) {
return NXT_AGAIN;
}
#define nxt_http_parse_request_line_step \
{ \
ch = *p; \
\
if (nxt_slow_path(ch < 'A' || ch > 'Z')) { \
break; \
} \
\
p++; \
}
nxt_http_parse_request_line_step
nxt_http_parse_request_line_step
nxt_http_parse_request_line_step
nxt_http_parse_request_line_step
nxt_http_parse_request_line_step
nxt_http_parse_request_line_step
nxt_http_parse_request_line_step
nxt_http_parse_request_line_step
#undef nxt_http_parse_request_line_step
}
if (nxt_fast_path(ch == ' ')) {
rp->method.length = p - rp->method.start;
break;
}
if (ch == '_' || ch == '-') {
continue;
}
if (rp->method.start == p && (ch == NXT_CR || ch == NXT_LF)) {
rp->method.start++;
continue;
}
return NXT_ERROR;
}
p++;
if (nxt_slow_path(p == end)) {
return NXT_AGAIN;
}
/* target */
ch = *p;
if (nxt_slow_path(ch != '/')) {
rc = nxt_http_parse_unusual_target(rp, &p, end);
if (nxt_slow_path(rc != NXT_OK)) {
return rc;
}
}
rp->target_start = p;
after_slash = p + 1;
for ( ;; ) {
p++;
trap = nxt_http_parse_target(&p, end);
switch (trap) {
case NXT_HTTP_TARGET_SLASH:
if (nxt_slow_path(after_slash == p)) {
rp->complex_target = 1;
goto rest_of_target;
}
after_slash = p + 1;
rp->exten_start = NULL;
continue;
case NXT_HTTP_TARGET_DOT:
if (nxt_slow_path(after_slash == p)) {
rp->complex_target = 1;
goto rest_of_target;
}
rp->exten_start = p + 1;
continue;
case NXT_HTTP_TARGET_ARGS_MARK:
rp->args_start = p + 1;
goto rest_of_target;
case NXT_HTTP_TARGET_SPACE:
rp->target_end = p;
goto space_after_target;
case NXT_HTTP_TARGET_QUOTE_MARK:
rp->quoted_target = 1;
goto rest_of_target;
case NXT_HTTP_TARGET_PLUS:
rp->plus_in_target = 1;
continue;
case NXT_HTTP_TARGET_HASH:
rp->complex_target = 1;
goto rest_of_target;
case NXT_HTTP_TARGET_AGAIN:
return NXT_AGAIN;
case NXT_HTTP_TARGET_BAD:
return NXT_ERROR;
}
nxt_unreachable();
}
rest_of_target:
for ( ;; ) {
p++;
trap = nxt_http_parse_target(&p, end);
switch (trap) {
case NXT_HTTP_TARGET_SPACE:
rp->target_end = p;
goto space_after_target;
case NXT_HTTP_TARGET_HASH:
rp->complex_target = 1;
continue;
case NXT_HTTP_TARGET_AGAIN:
return NXT_AGAIN;
case NXT_HTTP_TARGET_BAD:
return NXT_ERROR;
default:
continue;
}
nxt_unreachable();
}
space_after_target:
if (nxt_slow_path(end - p < 10)) {
return NXT_AGAIN;
}
/* " HTTP/1.1\r\n" or " HTTP/1.1\n" */
nxt_memcpy(version.str, &p[1], 8);
if (nxt_fast_path((version.ui64 == http11.ui64
|| version.ui64 == http10.ui64
|| (p[1] == 'H'
&& p[2] == 'T'
&& p[3] == 'T'
&& p[4] == 'P'
&& p[5] == '/'
&& p[6] >= '0' && p[6] <= '9'
&& p[7] == '.'
&& p[8] >= '0' && p[8] <= '9'))
&& (p[9] == '\r' || p[9] == '\n')))
{
rp->version.ui64 = version.ui64;
if (nxt_fast_path(p[9] == '\r')) {
p += 10;
if (nxt_slow_path(p == end)) {
return NXT_AGAIN;
}
if (nxt_slow_path(*p != '\n')) {
return NXT_ERROR;
}
*pos = p + 1;
return nxt_http_parse_field_name(rp, pos, end);
}
*pos = p + 10;
return nxt_http_parse_field_name(rp, pos, end);
}
if (p[1] == ' ') {
/* surplus space after tartet */
p++;
goto space_after_target;
}
rp->space_in_target = 1;
goto rest_of_target;
}
static nxt_int_t
nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp, u_char **pos,
u_char *end)
{
u_char *p, ch;
p = *pos;
ch = *p;
if (ch == ' ') {
/* skip surplus spaces before target */
do {
p++;
if (nxt_slow_path(p == end)) {
return NXT_AGAIN;
}
ch = *p;
} while (ch == ' ');
if (ch == '/') {
*pos = p;
return NXT_OK;
}
}
/* absolute path or '*' */
/* TODO */
return NXT_ERROR;
}
static nxt_int_t
nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
u_char *end)
{
u_char *p, ch, c;
size_t i, size;
static const u_char normal[256] nxt_aligned(64) =
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0-\0\0" "0123456789\0\0\0\0\0\0"
/* These 64 bytes should reside in one cache line. */
"\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0"
"\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
p = *pos;
size = end - p;
i = rp->offset;
#define nxt_http_parse_field_name_step \
{ \
ch = p[i]; \
c = normal[ch]; \
\
if (nxt_slow_path(c == '\0')) { \
goto name_end; \
} \
\
rp->field_name_key.str[i % 32] = c; \
i++; \
}
while (nxt_fast_path(size - i >= 8)) {
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
}
while (nxt_fast_path(i != size)) {
nxt_http_parse_field_name_step
}
#undef nxt_http_parse_field_name_step
rp->offset = i;
rp->handler = &nxt_http_parse_field_name;
return NXT_AGAIN;
name_end:
if (nxt_fast_path(ch == ':')) {
if (nxt_slow_path(i == 0)) {
return NXT_ERROR;
}
*pos = &p[i] + 1;
rp->field_name.start = p;
rp->field_name.length = i;
rp->offset = 0;
return nxt_http_parse_field_value(rp, pos, end);
}
*pos = &p[i];
rp->field_name.length = 0;
return nxt_http_parse_field_end(rp, pos, end);
}
static nxt_int_t
nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos,
u_char *end)
{
u_char *p, ch;
p = *pos;
for ( ;; ) {
if (nxt_slow_path(p == end)) {
*pos = p;
rp->handler = &nxt_http_parse_field_value;
return NXT_AGAIN;
}
if (*p != ' ') {
break;
}
p++;
}
*pos = p;
p += rp->offset;
for ( ;; ) {
p = nxt_http_lookup_field_end(p, end);
if (nxt_slow_path(p == end)) {
rp->offset = p - *pos;
rp->handler = &nxt_http_parse_field_value;
return NXT_AGAIN;
}
ch = *p;
if (nxt_fast_path(ch == '\r' || ch == '\n')) {
break;
}
if (ch == '\0') {
return NXT_ERROR;
}
}
if (nxt_fast_path(p != *pos)) {
while (p[-1] == ' ') {
p--;
}
}
rp->offset = 0;
rp->field_value.start = *pos;
rp->field_value.length = p - *pos;
*pos = p;
return nxt_http_parse_field_end(rp, pos, end);
}
static u_char *
nxt_http_lookup_field_end(u_char *p, u_char *end)
{
nxt_uint_t n;
#define nxt_http_lookup_field_end_step \
{ \
if (nxt_slow_path(*p < 0x10)) { \
return p; \
} \
\
p++; \
}
for (n = (end - p) / 16; nxt_fast_path(n != 0); n--) {
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
}
for (n = (end - p) / 4; nxt_fast_path(n != 0); n--) {
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
}
switch (end - p) {
case 3:
nxt_http_lookup_field_end_step
/* Fall through. */
case 2:
nxt_http_lookup_field_end_step
/* Fall through. */
case 1:
nxt_http_lookup_field_end_step
/* Fall through. */
case 0:
break;
default:
nxt_unreachable();
}
#undef nxt_http_lookup_field_end_step
return p;
}
static nxt_int_t
nxt_http_parse_field_end(nxt_http_request_parse_t *rp, u_char **pos,
u_char *end)
{
u_char *p;
nxt_int_t rc;
nxt_http_fields_hash_entry_t *entry;
p = *pos;
if (nxt_fast_path(*p == '\r')) {
p++;
if (nxt_slow_path(p == end)) {
rp->handler = &nxt_http_parse_field_end;
return NXT_AGAIN;
}
}
if (nxt_fast_path(*p == '\n')) {
*pos = p + 1;
if (rp->field_name.length != 0) {
entry = nxt_http_fields_hash_lookup(rp->hash,
rp->field_name_key.ui64,
&rp->field_name);
if (entry != NULL) {
rc = entry->handler(rp->ctx, &rp->field_name, &rp->field_value,
entry->data);
if (nxt_slow_path(rc != NXT_OK)) {
return NXT_ERROR;
}
}
nxt_memzero(rp->field_name_key.str, 32);
rp->handler = &nxt_http_parse_field_name;
return NXT_OK;
}
return NXT_DONE;
}
return NXT_ERROR;
}
static nxt_http_fields_hash_entry_t *
nxt_http_fields_hash_lookup(nxt_http_fields_hash_t *hash, uint64_t *key,
nxt_str_t *value)
{
nxt_http_fields_hash_entry_t *entry;
if (hash == NULL || value->length < hash->min_length) {
return NULL;
}
if (value->length > hash->max_length) {
if (value->length > 32 && hash->long_fields != NULL) {
return nxt_http_header_fields_hash_lookup_long(hash, value);
}
return NULL;
}
entry = hash->entries[value->length - hash->min_length];
if (entry == NULL) {
return NULL;
}
switch ((value->length + 7) / 8) {
case 1:
do {
if (entry->key[0].ui64 == key[0]) {
return entry;
}
entry = nxt_http_fields_hash_next_entry(entry, 1);
} while (entry->handler != NULL);
break;
case 2:
do {
if (entry->key[0].ui64 == key[0]
&& entry->key[1].ui64 == key[1])
{
return entry;
}
entry = nxt_http_fields_hash_next_entry(entry, 2);
} while (entry->handler != NULL);
break;
case 3:
do {
if (entry->key[0].ui64 == key[0]
&& entry->key[1].ui64 == key[1]
&& entry->key[2].ui64 == key[2])
{
return entry;
}
entry = nxt_http_fields_hash_next_entry(entry, 3);
} while (entry->handler != NULL);
break;
case 4:
do {
if (entry->key[0].ui64 == key[0]
&& entry->key[1].ui64 == key[1]
&& entry->key[2].ui64 == key[2]
&& entry->key[3].ui64 == key[3])
{
return entry;
}
entry = nxt_http_fields_hash_next_entry(entry, 4);
} while (entry->handler != NULL);
break;
default:
nxt_unreachable();
}
return NULL;
}
static nxt_http_fields_hash_entry_t *
nxt_http_header_fields_hash_lookup_long(nxt_http_fields_hash_t *hash,
nxt_str_t *value)
{
/* TODO */
return NULL;
}
nxt_http_fields_hash_t *
nxt_http_fields_hash(nxt_http_fields_t *fields, nxt_mem_pool_t *mp)
{
size_t min_length, max_length, length, size;
nxt_uint_t i, j, n;
nxt_http_fields_hash_t *hash;
nxt_http_fields_hash_entry_t *entry;
min_length = 32 + 1;
max_length = 0;
for (i = 0; fields[i].handler != NULL; i++) {
length = fields[i].name.length;
if (length > 32) {
/* TODO */
return NULL;
}
min_length = nxt_min(length, min_length);
max_length = nxt_max(length, max_length);
}
size = sizeof(nxt_http_fields_hash_t);
if (min_length <= 32) {
size += (max_length - min_length + 1)
* sizeof(nxt_http_fields_hash_entry_t *);
}
hash = nxt_mem_zalloc(mp, size);
if (nxt_slow_path(hash == NULL)) {
return NULL;
}
hash->min_length = min_length;
hash->max_length = max_length;
for (i = 0; fields[i].handler != NULL; i++) {
length = fields[i].name.length;
entry = hash->entries[length - min_length];
if (entry != NULL) {
continue;
}
n = 1;
for (j = i + 1; fields[j].handler != NULL; j++) {
if (length == fields[j].name.length) {
n++;
}
}
size = sizeof(nxt_http_fields_hash_entry_t) + nxt_align_size(length, 8);
entry = nxt_mem_zalloc(mp, n * size
+ sizeof(nxt_http_fields_hash_entry_t));
if (nxt_slow_path(entry == NULL)) {
return NULL;
}
hash->entries[length - min_length] = entry;
for (j = i; fields[j].handler != NULL; j++) {
if (length != fields[j].name.length) {
continue;
}
entry->handler = fields[j].handler;
entry->data = fields[j].data;
nxt_memcpy_lowcase(entry->key->str, fields[j].name.start, length);
n--;
if (n == 0) {
break;
}
entry = (nxt_http_fields_hash_entry_t *) ((u_char *) entry + size);
}
}
return hash;
}
|