xref: /unit/src/nxt_http_parse.c (revision 481)
116Svbart@nginx.com 
216Svbart@nginx.com /*
316Svbart@nginx.com  * Copyright (C) NGINX, Inc.
416Svbart@nginx.com  * Copyright (C) Valentin V. Bartenev
516Svbart@nginx.com  */
616Svbart@nginx.com 
716Svbart@nginx.com #include <nxt_main.h>
816Svbart@nginx.com 
916Svbart@nginx.com 
1016Svbart@nginx.com static nxt_int_t nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp,
1116Svbart@nginx.com     u_char **pos, u_char *end);
1216Svbart@nginx.com static nxt_int_t nxt_http_parse_request_line(nxt_http_request_parse_t *rp,
1316Svbart@nginx.com     u_char **pos, u_char *end);
1416Svbart@nginx.com static nxt_int_t nxt_http_parse_field_name(nxt_http_request_parse_t *rp,
1516Svbart@nginx.com     u_char **pos, u_char *end);
1616Svbart@nginx.com static nxt_int_t nxt_http_parse_field_value(nxt_http_request_parse_t *rp,
1716Svbart@nginx.com     u_char **pos, u_char *end);
1816Svbart@nginx.com static u_char *nxt_http_lookup_field_end(u_char *p, u_char *end);
1916Svbart@nginx.com static nxt_int_t nxt_http_parse_field_end(nxt_http_request_parse_t *rp,
2016Svbart@nginx.com     u_char **pos, u_char *end);
2116Svbart@nginx.com 
22417Svbart@nginx.com static nxt_int_t nxt_http_parse_complex_target(nxt_http_request_parse_t *rp);
23417Svbart@nginx.com 
24417Svbart@nginx.com static nxt_int_t nxt_http_field_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
25417Svbart@nginx.com static void *nxt_http_field_hash_alloc(void *pool, size_t size);
26417Svbart@nginx.com static void nxt_http_field_hash_free(void *pool, void *p);
27417Svbart@nginx.com 
28417Svbart@nginx.com static nxt_int_t nxt_http_field_hash_collision(nxt_lvlhsh_query_t *lhq,
29417Svbart@nginx.com     void *data);
3016Svbart@nginx.com 
31417Svbart@nginx.com 
32417Svbart@nginx.com #define NXT_HTTP_MAX_FIELD_NAME         0xff
33417Svbart@nginx.com #define NXT_HTTP_MAX_FIELD_VALUE        NXT_INT32_T_MAX
34417Svbart@nginx.com 
35417Svbart@nginx.com #define NXT_HTTP_FIELD_LVLHSH_SHIFT     5
36417Svbart@nginx.com 
37417Svbart@nginx.com #define NXT_HTTP_FIELD_HASH_INIT        159406
38417Svbart@nginx.com #define nxt_http_field_hash_char(h, c)  (((h) << 4) + (h) + (c))
39417Svbart@nginx.com #define nxt_http_field_hash_end(h)      (((h) >> 16) ^ (h))
40112Smax.romanov@nginx.com 
4116Svbart@nginx.com 
4216Svbart@nginx.com typedef enum {
4316Svbart@nginx.com     NXT_HTTP_TARGET_SPACE = 1,   /* \s  */
4416Svbart@nginx.com     NXT_HTTP_TARGET_HASH,        /*  #  */
4516Svbart@nginx.com     NXT_HTTP_TARGET_AGAIN,
4616Svbart@nginx.com     NXT_HTTP_TARGET_BAD,         /* \0\r\n */
4716Svbart@nginx.com 
4816Svbart@nginx.com     /* traps below are used for extended check only */
4916Svbart@nginx.com 
5016Svbart@nginx.com     NXT_HTTP_TARGET_SLASH = 5,   /*  /  */
5116Svbart@nginx.com     NXT_HTTP_TARGET_DOT,         /*  .  */
5216Svbart@nginx.com     NXT_HTTP_TARGET_ARGS_MARK,   /*  ?  */
5316Svbart@nginx.com     NXT_HTTP_TARGET_QUOTE_MARK,  /*  %  */
5416Svbart@nginx.com     NXT_HTTP_TARGET_PLUS,        /*  +  */
5516Svbart@nginx.com } nxt_http_target_traps_e;
5616Svbart@nginx.com 
5716Svbart@nginx.com 
5816Svbart@nginx.com static const uint8_t  nxt_http_target_chars[256] nxt_aligned(64) = {
5916Svbart@nginx.com     /* \0                               \n        \r       */
6016Svbart@nginx.com         4, 0, 0, 0,  0, 0, 0, 0,   0, 0, 4, 0,  0, 4, 0, 0,
6116Svbart@nginx.com         0, 0, 0, 0,  0, 0, 0, 0,   0, 0, 0, 0,  0, 0, 0, 0,
6252Svbart@nginx.com 
6352Svbart@nginx.com     /* \s  !  "  #   $  %  &  '    (  )  *  +   ,  -  .  / */
6416Svbart@nginx.com         1, 0, 0, 2,  0, 8, 0, 0,   0, 0, 0, 9,  0, 0, 6, 5,
6552Svbart@nginx.com 
6652Svbart@nginx.com     /*  0  1  2  3   4  5  6  7    8  9  :  ;   <  =  >  ? */
6716Svbart@nginx.com         0, 0, 0, 0,  0, 0, 0, 0,   0, 0, 0, 0,  0, 0, 0, 7,
6816Svbart@nginx.com };
6916Svbart@nginx.com 
7016Svbart@nginx.com 
7116Svbart@nginx.com nxt_inline nxt_http_target_traps_e
7216Svbart@nginx.com nxt_http_parse_target(u_char **pos, u_char *end)
7316Svbart@nginx.com {
7416Svbart@nginx.com     u_char      *p;
7516Svbart@nginx.com     nxt_uint_t  trap;
7616Svbart@nginx.com 
7716Svbart@nginx.com     p = *pos;
7816Svbart@nginx.com 
79409Svbart@nginx.com     while (nxt_fast_path(end - p >= 10)) {
8016Svbart@nginx.com 
81409Svbart@nginx.com #define nxt_target_test_char(ch)                                              \
82409Svbart@nginx.com                                                                               \
83409Svbart@nginx.com         trap = nxt_http_target_chars[ch];                                     \
8416Svbart@nginx.com                                                                               \
85409Svbart@nginx.com         if (nxt_slow_path(trap != 0)) {                                       \
86409Svbart@nginx.com             *pos = &(ch);                                                     \
87409Svbart@nginx.com             return trap;                                                      \
8816Svbart@nginx.com         }
8916Svbart@nginx.com 
90409Svbart@nginx.com /* enddef */
91409Svbart@nginx.com 
92409Svbart@nginx.com         nxt_target_test_char(p[0]);
93409Svbart@nginx.com         nxt_target_test_char(p[1]);
94409Svbart@nginx.com         nxt_target_test_char(p[2]);
95409Svbart@nginx.com         nxt_target_test_char(p[3]);
9616Svbart@nginx.com 
97409Svbart@nginx.com         nxt_target_test_char(p[4]);
98409Svbart@nginx.com         nxt_target_test_char(p[5]);
99409Svbart@nginx.com         nxt_target_test_char(p[6]);
100409Svbart@nginx.com         nxt_target_test_char(p[7]);
10116Svbart@nginx.com 
102409Svbart@nginx.com         nxt_target_test_char(p[8]);
103409Svbart@nginx.com         nxt_target_test_char(p[9]);
10416Svbart@nginx.com 
105409Svbart@nginx.com         p += 10;
10616Svbart@nginx.com     }
10716Svbart@nginx.com 
108410Svbart@nginx.com     while (p != end) {
109410Svbart@nginx.com         nxt_target_test_char(*p); p++;
110410Svbart@nginx.com     }
111410Svbart@nginx.com 
112409Svbart@nginx.com     return NXT_HTTP_TARGET_AGAIN;
11316Svbart@nginx.com }
11416Svbart@nginx.com 
11516Svbart@nginx.com 
11616Svbart@nginx.com nxt_int_t
117417Svbart@nginx.com nxt_http_parse_request_init(nxt_http_request_parse_t *rp, nxt_mp_t *mp)
118417Svbart@nginx.com {
119417Svbart@nginx.com     rp->mem_pool = mp;
120417Svbart@nginx.com 
121417Svbart@nginx.com     rp->fields = nxt_list_create(mp, 8, sizeof(nxt_http_field_t));
122417Svbart@nginx.com     if (nxt_slow_path(rp->fields == NULL)){
123417Svbart@nginx.com         return NXT_ERROR;
124417Svbart@nginx.com     }
125417Svbart@nginx.com 
126417Svbart@nginx.com     rp->field_hash = NXT_HTTP_FIELD_HASH_INIT;
127417Svbart@nginx.com 
128417Svbart@nginx.com     return NXT_OK;
129417Svbart@nginx.com }
130417Svbart@nginx.com 
131417Svbart@nginx.com 
132417Svbart@nginx.com nxt_int_t
13316Svbart@nginx.com nxt_http_parse_request(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b)
13416Svbart@nginx.com {
13516Svbart@nginx.com     nxt_int_t  rc;
13616Svbart@nginx.com 
13716Svbart@nginx.com     if (rp->handler == NULL) {
13816Svbart@nginx.com         rp->handler = &nxt_http_parse_request_line;
13916Svbart@nginx.com     }
14016Svbart@nginx.com 
14116Svbart@nginx.com     do {
14216Svbart@nginx.com         rc = rp->handler(rp, &b->pos, b->free);
14316Svbart@nginx.com     } while (rc == NXT_OK);
14416Svbart@nginx.com 
14516Svbart@nginx.com     return rc;
14616Svbart@nginx.com }
14716Svbart@nginx.com 
14816Svbart@nginx.com 
149422Svbart@nginx.com nxt_int_t
150422Svbart@nginx.com nxt_http_parse_fields(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b)
151422Svbart@nginx.com {
152422Svbart@nginx.com     nxt_int_t  rc;
153422Svbart@nginx.com 
154422Svbart@nginx.com     if (rp->handler == NULL) {
155422Svbart@nginx.com         rp->handler = &nxt_http_parse_field_name;
156422Svbart@nginx.com     }
157422Svbart@nginx.com 
158422Svbart@nginx.com     do {
159422Svbart@nginx.com         rc = rp->handler(rp, &b->pos, b->free);
160422Svbart@nginx.com     } while (rc == NXT_OK);
161422Svbart@nginx.com 
162422Svbart@nginx.com     return rc;
163422Svbart@nginx.com }
164422Svbart@nginx.com 
165422Svbart@nginx.com 
16616Svbart@nginx.com static nxt_int_t
16716Svbart@nginx.com nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos,
16816Svbart@nginx.com     u_char *end)
16916Svbart@nginx.com {
17016Svbart@nginx.com     u_char                   *p, ch, *after_slash;
17116Svbart@nginx.com     nxt_int_t                rc;
172*481Svbart@nginx.com     nxt_http_ver_t           ver;
17316Svbart@nginx.com     nxt_http_target_traps_e  trap;
17416Svbart@nginx.com 
17516Svbart@nginx.com     static const nxt_http_ver_t  http11 = { "HTTP/1.1" };
17616Svbart@nginx.com     static const nxt_http_ver_t  http10 = { "HTTP/1.0" };
17716Svbart@nginx.com 
17816Svbart@nginx.com     p = *pos;
17916Svbart@nginx.com 
18016Svbart@nginx.com     rp->method.start = p;
18116Svbart@nginx.com 
182409Svbart@nginx.com     for ( ;; ) {
183409Svbart@nginx.com 
184409Svbart@nginx.com         while (nxt_fast_path(end - p >= 8)) {
18516Svbart@nginx.com 
186409Svbart@nginx.com #define nxt_method_test_char(ch)                                              \
187409Svbart@nginx.com                                                                               \
188409Svbart@nginx.com             if (nxt_slow_path((ch) < 'A' || (ch) > 'Z')) {                    \
189409Svbart@nginx.com                 p = &(ch);                                                    \
190409Svbart@nginx.com                 goto method_unusual_char;                                     \
19116Svbart@nginx.com             }
19216Svbart@nginx.com 
193409Svbart@nginx.com /* enddef */
194409Svbart@nginx.com 
195409Svbart@nginx.com             nxt_method_test_char(p[0]);
196409Svbart@nginx.com             nxt_method_test_char(p[1]);
197409Svbart@nginx.com             nxt_method_test_char(p[2]);
198409Svbart@nginx.com             nxt_method_test_char(p[3]);
19916Svbart@nginx.com 
200409Svbart@nginx.com             nxt_method_test_char(p[4]);
201409Svbart@nginx.com             nxt_method_test_char(p[5]);
202409Svbart@nginx.com             nxt_method_test_char(p[6]);
203409Svbart@nginx.com             nxt_method_test_char(p[7]);
20416Svbart@nginx.com 
205409Svbart@nginx.com             p += 8;
206409Svbart@nginx.com         }
20716Svbart@nginx.com 
208410Svbart@nginx.com         while (p != end) {
209410Svbart@nginx.com             nxt_method_test_char(*p); p++;
210410Svbart@nginx.com         }
211410Svbart@nginx.com 
212409Svbart@nginx.com         return NXT_AGAIN;
213409Svbart@nginx.com 
214409Svbart@nginx.com     method_unusual_char:
215409Svbart@nginx.com 
216409Svbart@nginx.com         ch = *p;
21716Svbart@nginx.com 
21816Svbart@nginx.com         if (nxt_fast_path(ch == ' ')) {
21916Svbart@nginx.com             rp->method.length = p - rp->method.start;
22016Svbart@nginx.com             break;
22116Svbart@nginx.com         }
22216Svbart@nginx.com 
22316Svbart@nginx.com         if (ch == '_' || ch == '-') {
224409Svbart@nginx.com             p++;
22516Svbart@nginx.com             continue;
22616Svbart@nginx.com         }
22716Svbart@nginx.com 
22816Svbart@nginx.com         if (rp->method.start == p && (ch == NXT_CR || ch == NXT_LF)) {
22916Svbart@nginx.com             rp->method.start++;
230409Svbart@nginx.com             p++;
23116Svbart@nginx.com             continue;
23216Svbart@nginx.com         }
23316Svbart@nginx.com 
234480Svbart@nginx.com         return NXT_HTTP_PARSE_INVALID;
23516Svbart@nginx.com     }
23616Svbart@nginx.com 
23716Svbart@nginx.com     p++;
23816Svbart@nginx.com 
23916Svbart@nginx.com     if (nxt_slow_path(p == end)) {
24016Svbart@nginx.com         return NXT_AGAIN;
24116Svbart@nginx.com     }
24216Svbart@nginx.com 
24316Svbart@nginx.com     /* target */
24416Svbart@nginx.com 
24516Svbart@nginx.com     ch = *p;
24616Svbart@nginx.com 
24716Svbart@nginx.com     if (nxt_slow_path(ch != '/')) {
24816Svbart@nginx.com         rc = nxt_http_parse_unusual_target(rp, &p, end);
24916Svbart@nginx.com 
25016Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
25116Svbart@nginx.com             return rc;
25216Svbart@nginx.com         }
25316Svbart@nginx.com     }
25416Svbart@nginx.com 
25516Svbart@nginx.com     rp->target_start = p;
25616Svbart@nginx.com 
25716Svbart@nginx.com     after_slash = p + 1;
25816Svbart@nginx.com 
25916Svbart@nginx.com     for ( ;; ) {
26016Svbart@nginx.com         p++;
26116Svbart@nginx.com 
26216Svbart@nginx.com         trap = nxt_http_parse_target(&p, end);
26316Svbart@nginx.com 
26416Svbart@nginx.com         switch (trap) {
26516Svbart@nginx.com         case NXT_HTTP_TARGET_SLASH:
26616Svbart@nginx.com             if (nxt_slow_path(after_slash == p)) {
26716Svbart@nginx.com                 rp->complex_target = 1;
26816Svbart@nginx.com                 goto rest_of_target;
26916Svbart@nginx.com             }
27016Svbart@nginx.com 
27116Svbart@nginx.com             after_slash = p + 1;
27216Svbart@nginx.com 
27316Svbart@nginx.com             rp->exten_start = NULL;
27416Svbart@nginx.com             continue;
27516Svbart@nginx.com 
27616Svbart@nginx.com         case NXT_HTTP_TARGET_DOT:
27716Svbart@nginx.com             if (nxt_slow_path(after_slash == p)) {
27816Svbart@nginx.com                 rp->complex_target = 1;
27916Svbart@nginx.com                 goto rest_of_target;
28016Svbart@nginx.com             }
28116Svbart@nginx.com 
28216Svbart@nginx.com             rp->exten_start = p + 1;
28316Svbart@nginx.com             continue;
28416Svbart@nginx.com 
28516Svbart@nginx.com         case NXT_HTTP_TARGET_ARGS_MARK:
28616Svbart@nginx.com             rp->args_start = p + 1;
28716Svbart@nginx.com             goto rest_of_target;
28816Svbart@nginx.com 
28916Svbart@nginx.com         case NXT_HTTP_TARGET_SPACE:
29016Svbart@nginx.com             rp->target_end = p;
29116Svbart@nginx.com             goto space_after_target;
29216Svbart@nginx.com 
29316Svbart@nginx.com         case NXT_HTTP_TARGET_QUOTE_MARK:
29416Svbart@nginx.com             rp->quoted_target = 1;
29516Svbart@nginx.com             goto rest_of_target;
29616Svbart@nginx.com 
29716Svbart@nginx.com         case NXT_HTTP_TARGET_PLUS:
29816Svbart@nginx.com             rp->plus_in_target = 1;
29916Svbart@nginx.com             continue;
30016Svbart@nginx.com 
30116Svbart@nginx.com         case NXT_HTTP_TARGET_HASH:
30216Svbart@nginx.com             rp->complex_target = 1;
30316Svbart@nginx.com             goto rest_of_target;
30416Svbart@nginx.com 
30516Svbart@nginx.com         case NXT_HTTP_TARGET_AGAIN:
30616Svbart@nginx.com             return NXT_AGAIN;
30716Svbart@nginx.com 
30816Svbart@nginx.com         case NXT_HTTP_TARGET_BAD:
309480Svbart@nginx.com             return NXT_HTTP_PARSE_INVALID;
31016Svbart@nginx.com         }
31116Svbart@nginx.com 
31216Svbart@nginx.com         nxt_unreachable();
31316Svbart@nginx.com     }
31416Svbart@nginx.com 
31516Svbart@nginx.com rest_of_target:
31616Svbart@nginx.com 
31716Svbart@nginx.com     for ( ;; ) {
31816Svbart@nginx.com         p++;
31916Svbart@nginx.com 
32019Svbart@nginx.com         trap = nxt_http_parse_target(&p, end);
32116Svbart@nginx.com 
32216Svbart@nginx.com         switch (trap) {
32316Svbart@nginx.com         case NXT_HTTP_TARGET_SPACE:
32416Svbart@nginx.com             rp->target_end = p;
32516Svbart@nginx.com             goto space_after_target;
32616Svbart@nginx.com 
32716Svbart@nginx.com         case NXT_HTTP_TARGET_HASH:
32816Svbart@nginx.com             rp->complex_target = 1;
32916Svbart@nginx.com             continue;
33016Svbart@nginx.com 
33116Svbart@nginx.com         case NXT_HTTP_TARGET_AGAIN:
33216Svbart@nginx.com             return NXT_AGAIN;
33316Svbart@nginx.com 
33416Svbart@nginx.com         case NXT_HTTP_TARGET_BAD:
335480Svbart@nginx.com             return NXT_HTTP_PARSE_INVALID;
33616Svbart@nginx.com 
33716Svbart@nginx.com         default:
33816Svbart@nginx.com             continue;
33916Svbart@nginx.com         }
34016Svbart@nginx.com 
34116Svbart@nginx.com         nxt_unreachable();
34216Svbart@nginx.com     }
34316Svbart@nginx.com 
34416Svbart@nginx.com space_after_target:
34516Svbart@nginx.com 
34616Svbart@nginx.com     if (nxt_slow_path(end - p < 10)) {
347410Svbart@nginx.com 
348410Svbart@nginx.com         do {
349410Svbart@nginx.com             p++;
350410Svbart@nginx.com 
351410Svbart@nginx.com             if (p == end) {
352410Svbart@nginx.com                 return NXT_AGAIN;
353410Svbart@nginx.com             }
354410Svbart@nginx.com 
355410Svbart@nginx.com         } while (*p == ' ');
356410Svbart@nginx.com 
357410Svbart@nginx.com         if (nxt_memcmp(p, "HTTP/", nxt_min(end - p, 5)) == 0) {
358410Svbart@nginx.com 
359410Svbart@nginx.com             switch (end - p) {
360410Svbart@nginx.com             case 8:
361410Svbart@nginx.com                 if (p[7] < '0' || p[7] > '9') {
362410Svbart@nginx.com                     break;
363410Svbart@nginx.com                 }
364410Svbart@nginx.com                 /* Fall through. */
365410Svbart@nginx.com             case 7:
366410Svbart@nginx.com                 if (p[6] != '.') {
367410Svbart@nginx.com                     break;
368410Svbart@nginx.com                 }
369410Svbart@nginx.com                 /* Fall through. */
370410Svbart@nginx.com             case 6:
371410Svbart@nginx.com                 if (p[5] < '0' || p[5] > '9') {
372410Svbart@nginx.com                     break;
373410Svbart@nginx.com                 }
374410Svbart@nginx.com                 /* Fall through. */
375410Svbart@nginx.com             default:
376410Svbart@nginx.com                 return NXT_AGAIN;
377410Svbart@nginx.com             }
378410Svbart@nginx.com         }
379410Svbart@nginx.com 
380410Svbart@nginx.com         rp->space_in_target = 1;
381410Svbart@nginx.com         goto rest_of_target;
38216Svbart@nginx.com     }
38316Svbart@nginx.com 
38416Svbart@nginx.com     /* " HTTP/1.1\r\n" or " HTTP/1.1\n" */
38516Svbart@nginx.com 
386*481Svbart@nginx.com     nxt_memcpy(ver.str, &p[1], 8);
38716Svbart@nginx.com 
388*481Svbart@nginx.com     if (nxt_fast_path((ver.ui64 == http11.ui64
389*481Svbart@nginx.com                        || ver.ui64 == http10.ui64
390*481Svbart@nginx.com                        || (nxt_memcmp(ver.s.prefix, "HTTP/", 5) == 0
391*481Svbart@nginx.com                            && ver.s.major >= '0' && ver.s.major <= '9'
392*481Svbart@nginx.com                            && ver.s.point == '.'
393*481Svbart@nginx.com                            && ver.s.minor >= '0' && ver.s.minor <= '9'))
39416Svbart@nginx.com                       && (p[9] == '\r' || p[9] == '\n')))
39516Svbart@nginx.com     {
396*481Svbart@nginx.com         rp->version.ui64 = ver.ui64;
39716Svbart@nginx.com 
39816Svbart@nginx.com         if (nxt_fast_path(p[9] == '\r')) {
39916Svbart@nginx.com             p += 10;
40016Svbart@nginx.com 
40116Svbart@nginx.com             if (nxt_slow_path(p == end)) {
40216Svbart@nginx.com                 return NXT_AGAIN;
40316Svbart@nginx.com             }
40416Svbart@nginx.com 
40516Svbart@nginx.com             if (nxt_slow_path(*p != '\n')) {
406480Svbart@nginx.com                 return NXT_HTTP_PARSE_INVALID;
40716Svbart@nginx.com             }
40816Svbart@nginx.com 
40916Svbart@nginx.com             *pos = p + 1;
410112Smax.romanov@nginx.com 
411112Smax.romanov@nginx.com         } else {
412112Smax.romanov@nginx.com             *pos = p + 10;
413112Smax.romanov@nginx.com         }
414112Smax.romanov@nginx.com 
415112Smax.romanov@nginx.com         if (rp->complex_target != 0 || rp->quoted_target != 0) {
416112Smax.romanov@nginx.com             rc = nxt_http_parse_complex_target(rp);
417112Smax.romanov@nginx.com 
418112Smax.romanov@nginx.com             if (nxt_slow_path(rc != NXT_OK)) {
419112Smax.romanov@nginx.com                 return rc;
420112Smax.romanov@nginx.com             }
421112Smax.romanov@nginx.com 
42216Svbart@nginx.com             return nxt_http_parse_field_name(rp, pos, end);
42316Svbart@nginx.com         }
42416Svbart@nginx.com 
425112Smax.romanov@nginx.com         rp->path.start = rp->target_start;
426112Smax.romanov@nginx.com 
427112Smax.romanov@nginx.com         if (rp->args_start != NULL) {
428112Smax.romanov@nginx.com             rp->path.length = rp->args_start - rp->target_start - 1;
429112Smax.romanov@nginx.com 
430112Smax.romanov@nginx.com             rp->args.start = rp->args_start;
431112Smax.romanov@nginx.com             rp->args.length = rp->target_end - rp->args_start;
432112Smax.romanov@nginx.com 
433112Smax.romanov@nginx.com         } else {
434112Smax.romanov@nginx.com             rp->path.length = rp->target_end - rp->target_start;
435112Smax.romanov@nginx.com         }
436112Smax.romanov@nginx.com 
437112Smax.romanov@nginx.com         if (rp->exten_start) {
438112Smax.romanov@nginx.com             rp->exten.length = rp->path.start + rp->path.length -
439112Smax.romanov@nginx.com                                rp->exten_start;
440112Smax.romanov@nginx.com             rp->exten.start = rp->exten_start;
441112Smax.romanov@nginx.com         }
442112Smax.romanov@nginx.com 
44316Svbart@nginx.com         return nxt_http_parse_field_name(rp, pos, end);
44416Svbart@nginx.com     }
44516Svbart@nginx.com 
44616Svbart@nginx.com     if (p[1] == ' ') {
44716Svbart@nginx.com         /* surplus space after tartet */
44816Svbart@nginx.com         p++;
44916Svbart@nginx.com         goto space_after_target;
45016Svbart@nginx.com     }
45116Svbart@nginx.com 
45216Svbart@nginx.com     rp->space_in_target = 1;
45316Svbart@nginx.com     goto rest_of_target;
45416Svbart@nginx.com }
45516Svbart@nginx.com 
45616Svbart@nginx.com 
45716Svbart@nginx.com static nxt_int_t
45816Svbart@nginx.com nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp, u_char **pos,
45916Svbart@nginx.com     u_char *end)
46016Svbart@nginx.com {
46116Svbart@nginx.com     u_char  *p, ch;
46216Svbart@nginx.com 
46316Svbart@nginx.com     p = *pos;
46416Svbart@nginx.com 
46516Svbart@nginx.com     ch = *p;
46616Svbart@nginx.com 
46716Svbart@nginx.com     if (ch == ' ') {
46816Svbart@nginx.com         /* skip surplus spaces before target */
46916Svbart@nginx.com 
47016Svbart@nginx.com         do {
47116Svbart@nginx.com             p++;
47216Svbart@nginx.com 
47316Svbart@nginx.com             if (nxt_slow_path(p == end)) {
47416Svbart@nginx.com                 return NXT_AGAIN;
47516Svbart@nginx.com             }
47616Svbart@nginx.com 
47716Svbart@nginx.com             ch = *p;
47816Svbart@nginx.com 
47916Svbart@nginx.com         } while (ch == ' ');
48016Svbart@nginx.com 
48116Svbart@nginx.com         if (ch == '/') {
48216Svbart@nginx.com             *pos = p;
48316Svbart@nginx.com             return NXT_OK;
48416Svbart@nginx.com         }
48516Svbart@nginx.com     }
48616Svbart@nginx.com 
48716Svbart@nginx.com     /* absolute path or '*' */
48816Svbart@nginx.com 
48916Svbart@nginx.com     /* TODO */
49016Svbart@nginx.com 
491480Svbart@nginx.com     return NXT_HTTP_PARSE_INVALID;
49216Svbart@nginx.com }
49316Svbart@nginx.com 
49416Svbart@nginx.com 
49516Svbart@nginx.com static nxt_int_t
49616Svbart@nginx.com nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
49716Svbart@nginx.com     u_char *end)
49816Svbart@nginx.com {
499417Svbart@nginx.com     u_char    *p, c;
500417Svbart@nginx.com     size_t    len;
501417Svbart@nginx.com     uint32_t  hash;
50216Svbart@nginx.com 
50316Svbart@nginx.com     static const u_char  normal[256]  nxt_aligned(64) =
50416Svbart@nginx.com         "\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"
50516Svbart@nginx.com         "\0\0\0\0\0\0\0\0\0\0\0\0\0-\0\0" "0123456789\0\0\0\0\0\0"
50616Svbart@nginx.com 
50716Svbart@nginx.com         /* These 64 bytes should reside in one cache line. */
508454Svbart@nginx.com         "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0_"
50916Svbart@nginx.com         "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0"
51016Svbart@nginx.com 
51116Svbart@nginx.com         "\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"
51216Svbart@nginx.com         "\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"
51316Svbart@nginx.com         "\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"
51416Svbart@nginx.com         "\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";
51516Svbart@nginx.com 
516417Svbart@nginx.com     p = *pos + rp->field_name.length;
517417Svbart@nginx.com     hash = rp->field_hash;
51816Svbart@nginx.com 
519417Svbart@nginx.com     while (nxt_fast_path(end - p >= 8)) {
520409Svbart@nginx.com 
521417Svbart@nginx.com #define nxt_field_name_test_char(ch)                                          \
522409Svbart@nginx.com                                                                               \
52319Svbart@nginx.com         c = normal[ch];                                                       \
52419Svbart@nginx.com                                                                               \
52519Svbart@nginx.com         if (nxt_slow_path(c == '\0')) {                                       \
526417Svbart@nginx.com             p = &(ch);                                                        \
52719Svbart@nginx.com             goto name_end;                                                    \
52819Svbart@nginx.com         }                                                                     \
52919Svbart@nginx.com                                                                               \
530417Svbart@nginx.com         hash = nxt_http_field_hash_char(hash, c);
531409Svbart@nginx.com 
532409Svbart@nginx.com /* enddef */
53316Svbart@nginx.com 
534417Svbart@nginx.com         nxt_field_name_test_char(p[0]);
535417Svbart@nginx.com         nxt_field_name_test_char(p[1]);
536417Svbart@nginx.com         nxt_field_name_test_char(p[2]);
537417Svbart@nginx.com         nxt_field_name_test_char(p[3]);
53816Svbart@nginx.com 
539417Svbart@nginx.com         nxt_field_name_test_char(p[4]);
540417Svbart@nginx.com         nxt_field_name_test_char(p[5]);
541417Svbart@nginx.com         nxt_field_name_test_char(p[6]);
542417Svbart@nginx.com         nxt_field_name_test_char(p[7]);
543417Svbart@nginx.com 
544417Svbart@nginx.com         p += 8;
54519Svbart@nginx.com     }
54616Svbart@nginx.com 
547417Svbart@nginx.com     while (nxt_fast_path(p != end)) {
548417Svbart@nginx.com         nxt_field_name_test_char(*p); p++;
54919Svbart@nginx.com     }
55016Svbart@nginx.com 
551417Svbart@nginx.com     len = p - *pos;
552417Svbart@nginx.com 
553417Svbart@nginx.com     if (nxt_slow_path(len > NXT_HTTP_MAX_FIELD_NAME)) {
554480Svbart@nginx.com         return NXT_HTTP_PARSE_TOO_LARGE_FIELD;
555417Svbart@nginx.com     }
556417Svbart@nginx.com 
557417Svbart@nginx.com     rp->field_hash = hash;
558417Svbart@nginx.com     rp->field_name.length = len;
559417Svbart@nginx.com 
56016Svbart@nginx.com     rp->handler = &nxt_http_parse_field_name;
56116Svbart@nginx.com 
56216Svbart@nginx.com     return NXT_AGAIN;
56319Svbart@nginx.com 
56419Svbart@nginx.com name_end:
56519Svbart@nginx.com 
566417Svbart@nginx.com     if (nxt_fast_path(*p == ':')) {
567417Svbart@nginx.com         if (nxt_slow_path(p == *pos)) {
568480Svbart@nginx.com             return NXT_HTTP_PARSE_INVALID;
56919Svbart@nginx.com         }
57019Svbart@nginx.com 
571417Svbart@nginx.com         len = p - *pos;
572417Svbart@nginx.com 
573417Svbart@nginx.com         if (nxt_slow_path(len > NXT_HTTP_MAX_FIELD_NAME)) {
574480Svbart@nginx.com             return NXT_HTTP_PARSE_TOO_LARGE_FIELD;
575417Svbart@nginx.com         }
57619Svbart@nginx.com 
577417Svbart@nginx.com         rp->field_hash = hash;
578417Svbart@nginx.com 
579417Svbart@nginx.com         rp->field_name.length = len;
580417Svbart@nginx.com         rp->field_name.start = *pos;
581417Svbart@nginx.com 
582417Svbart@nginx.com         *pos = p + 1;
58319Svbart@nginx.com 
58419Svbart@nginx.com         return nxt_http_parse_field_value(rp, pos, end);
58519Svbart@nginx.com     }
58619Svbart@nginx.com 
587417Svbart@nginx.com     if (nxt_slow_path(p != *pos)) {
588480Svbart@nginx.com         return NXT_HTTP_PARSE_INVALID;
58959Svbart@nginx.com     }
59019Svbart@nginx.com 
59119Svbart@nginx.com     return nxt_http_parse_field_end(rp, pos, end);
59216Svbart@nginx.com }
59316Svbart@nginx.com 
59416Svbart@nginx.com 
59516Svbart@nginx.com static nxt_int_t
59616Svbart@nginx.com nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos,
59716Svbart@nginx.com     u_char *end)
59816Svbart@nginx.com {
59916Svbart@nginx.com     u_char  *p, ch;
600417Svbart@nginx.com     size_t  len;
60116Svbart@nginx.com 
60216Svbart@nginx.com     p = *pos;
60316Svbart@nginx.com 
60416Svbart@nginx.com     for ( ;; ) {
60516Svbart@nginx.com         if (nxt_slow_path(p == end)) {
60616Svbart@nginx.com             *pos = p;
60716Svbart@nginx.com             rp->handler = &nxt_http_parse_field_value;
60816Svbart@nginx.com             return NXT_AGAIN;
60916Svbart@nginx.com         }
61016Svbart@nginx.com 
61116Svbart@nginx.com         if (*p != ' ') {
61216Svbart@nginx.com             break;
61316Svbart@nginx.com         }
61416Svbart@nginx.com 
61516Svbart@nginx.com         p++;
61616Svbart@nginx.com     }
61716Svbart@nginx.com 
61816Svbart@nginx.com     *pos = p;
61916Svbart@nginx.com 
62067Svbart@nginx.com     p += rp->field_value.length;
62116Svbart@nginx.com 
62216Svbart@nginx.com     for ( ;; ) {
62316Svbart@nginx.com         p = nxt_http_lookup_field_end(p, end);
62416Svbart@nginx.com 
62516Svbart@nginx.com         if (nxt_slow_path(p == end)) {
626417Svbart@nginx.com             len = p - *pos;
627417Svbart@nginx.com 
628417Svbart@nginx.com             if (nxt_slow_path(len > NXT_HTTP_MAX_FIELD_VALUE)) {
629480Svbart@nginx.com                 return NXT_HTTP_PARSE_TOO_LARGE_FIELD;
630417Svbart@nginx.com             }
631417Svbart@nginx.com 
632417Svbart@nginx.com             rp->field_value.length = len;
63316Svbart@nginx.com             rp->handler = &nxt_http_parse_field_value;
63416Svbart@nginx.com             return NXT_AGAIN;
63516Svbart@nginx.com         }
63616Svbart@nginx.com 
63716Svbart@nginx.com         ch = *p;
63816Svbart@nginx.com 
63916Svbart@nginx.com         if (nxt_fast_path(ch == '\r' || ch == '\n')) {
64016Svbart@nginx.com             break;
64116Svbart@nginx.com         }
64216Svbart@nginx.com 
64316Svbart@nginx.com         if (ch == '\0') {
644480Svbart@nginx.com             return NXT_HTTP_PARSE_INVALID;
64516Svbart@nginx.com         }
64616Svbart@nginx.com     }
64716Svbart@nginx.com 
64816Svbart@nginx.com     if (nxt_fast_path(p != *pos)) {
64916Svbart@nginx.com         while (p[-1] == ' ') {
65016Svbart@nginx.com             p--;
65116Svbart@nginx.com         }
65216Svbart@nginx.com     }
65316Svbart@nginx.com 
654417Svbart@nginx.com     len = p - *pos;
655417Svbart@nginx.com 
656417Svbart@nginx.com     if (nxt_slow_path(len > NXT_HTTP_MAX_FIELD_VALUE)) {
657480Svbart@nginx.com         return NXT_HTTP_PARSE_TOO_LARGE_FIELD;
658417Svbart@nginx.com     }
659417Svbart@nginx.com 
660417Svbart@nginx.com     rp->field_value.length = len;
66167Svbart@nginx.com     rp->field_value.start = *pos;
66216Svbart@nginx.com 
66316Svbart@nginx.com     *pos = p;
66416Svbart@nginx.com 
66516Svbart@nginx.com     return nxt_http_parse_field_end(rp, pos, end);
66616Svbart@nginx.com }
66716Svbart@nginx.com 
66816Svbart@nginx.com 
66916Svbart@nginx.com static u_char *
67016Svbart@nginx.com nxt_http_lookup_field_end(u_char *p, u_char *end)
67116Svbart@nginx.com {
672409Svbart@nginx.com     while (nxt_fast_path(end - p >= 16)) {
673409Svbart@nginx.com 
674409Svbart@nginx.com #define nxt_field_end_test_char(ch)                                           \
675409Svbart@nginx.com                                                                               \
676409Svbart@nginx.com         if (nxt_slow_path((ch) < 0x10)) {                                     \
677409Svbart@nginx.com             return &(ch);                                                     \
678409Svbart@nginx.com         }
679409Svbart@nginx.com 
680409Svbart@nginx.com /* enddef */
681409Svbart@nginx.com 
682409Svbart@nginx.com         nxt_field_end_test_char(p[0]);
683409Svbart@nginx.com         nxt_field_end_test_char(p[1]);
684409Svbart@nginx.com         nxt_field_end_test_char(p[2]);
685409Svbart@nginx.com         nxt_field_end_test_char(p[3]);
68616Svbart@nginx.com 
687409Svbart@nginx.com         nxt_field_end_test_char(p[4]);
688409Svbart@nginx.com         nxt_field_end_test_char(p[5]);
689409Svbart@nginx.com         nxt_field_end_test_char(p[6]);
690409Svbart@nginx.com         nxt_field_end_test_char(p[7]);
691409Svbart@nginx.com 
692409Svbart@nginx.com         nxt_field_end_test_char(p[8]);
693409Svbart@nginx.com         nxt_field_end_test_char(p[9]);
694409Svbart@nginx.com         nxt_field_end_test_char(p[10]);
695409Svbart@nginx.com         nxt_field_end_test_char(p[11]);
696409Svbart@nginx.com 
697409Svbart@nginx.com         nxt_field_end_test_char(p[12]);
698409Svbart@nginx.com         nxt_field_end_test_char(p[13]);
699409Svbart@nginx.com         nxt_field_end_test_char(p[14]);
700409Svbart@nginx.com         nxt_field_end_test_char(p[15]);
701409Svbart@nginx.com 
702409Svbart@nginx.com         p += 16;
70316Svbart@nginx.com     }
70416Svbart@nginx.com 
705409Svbart@nginx.com     while (nxt_fast_path(end - p >= 4)) {
70619Svbart@nginx.com 
707409Svbart@nginx.com         nxt_field_end_test_char(p[0]);
708409Svbart@nginx.com         nxt_field_end_test_char(p[1]);
709409Svbart@nginx.com         nxt_field_end_test_char(p[2]);
710409Svbart@nginx.com         nxt_field_end_test_char(p[3]);
71116Svbart@nginx.com 
712409Svbart@nginx.com         p += 4;
71319Svbart@nginx.com     }
71419Svbart@nginx.com 
71519Svbart@nginx.com     switch (end - p) {
71616Svbart@nginx.com     case 3:
717409Svbart@nginx.com         nxt_field_end_test_char(*p); p++;
71839Svbart@nginx.com         /* Fall through. */
71916Svbart@nginx.com     case 2:
720409Svbart@nginx.com         nxt_field_end_test_char(*p); p++;
72139Svbart@nginx.com         /* Fall through. */
72216Svbart@nginx.com     case 1:
723409Svbart@nginx.com         nxt_field_end_test_char(*p); p++;
72439Svbart@nginx.com         /* Fall through. */
72516Svbart@nginx.com     case 0:
72616Svbart@nginx.com         break;
72716Svbart@nginx.com     default:
72816Svbart@nginx.com         nxt_unreachable();
72916Svbart@nginx.com     }
73016Svbart@nginx.com 
73116Svbart@nginx.com     return p;
73216Svbart@nginx.com }
73316Svbart@nginx.com 
73416Svbart@nginx.com 
73516Svbart@nginx.com static nxt_int_t
73616Svbart@nginx.com nxt_http_parse_field_end(nxt_http_request_parse_t *rp, u_char **pos,
73716Svbart@nginx.com     u_char *end)
73816Svbart@nginx.com {
73960Svbart@nginx.com     u_char            *p;
74060Svbart@nginx.com     nxt_http_field_t  *field;
74116Svbart@nginx.com 
74216Svbart@nginx.com     p = *pos;
74316Svbart@nginx.com 
74416Svbart@nginx.com     if (nxt_fast_path(*p == '\r')) {
74516Svbart@nginx.com         p++;
74616Svbart@nginx.com 
74716Svbart@nginx.com         if (nxt_slow_path(p == end)) {
74816Svbart@nginx.com             rp->handler = &nxt_http_parse_field_end;
74916Svbart@nginx.com             return NXT_AGAIN;
75016Svbart@nginx.com         }
75116Svbart@nginx.com     }
75216Svbart@nginx.com 
75316Svbart@nginx.com     if (nxt_fast_path(*p == '\n')) {
75416Svbart@nginx.com         *pos = p + 1;
75516Svbart@nginx.com 
75667Svbart@nginx.com         if (rp->field_name.length != 0) {
75760Svbart@nginx.com             field = nxt_list_add(rp->fields);
75816Svbart@nginx.com 
75960Svbart@nginx.com             if (nxt_slow_path(field == NULL)) {
76060Svbart@nginx.com                 return NXT_ERROR;
76116Svbart@nginx.com             }
76216Svbart@nginx.com 
763417Svbart@nginx.com             field->hash = nxt_http_field_hash_end(rp->field_hash);
764417Svbart@nginx.com             field->skip = 0;
76560Svbart@nginx.com 
766417Svbart@nginx.com             field->name_length = rp->field_name.length;
767417Svbart@nginx.com             field->value_length = rp->field_value.length;
768417Svbart@nginx.com             field->name = rp->field_name.start;
769417Svbart@nginx.com             field->value = rp->field_value.start;
77067Svbart@nginx.com 
771417Svbart@nginx.com             rp->field_hash = NXT_HTTP_FIELD_HASH_INIT;
77267Svbart@nginx.com 
77367Svbart@nginx.com             rp->field_name.length = 0;
77467Svbart@nginx.com             rp->field_value.length = 0;
77516Svbart@nginx.com 
77616Svbart@nginx.com             rp->handler = &nxt_http_parse_field_name;
77716Svbart@nginx.com             return NXT_OK;
77816Svbart@nginx.com         }
77916Svbart@nginx.com 
78016Svbart@nginx.com         return NXT_DONE;
78116Svbart@nginx.com     }
78216Svbart@nginx.com 
783480Svbart@nginx.com     return NXT_HTTP_PARSE_INVALID;
78416Svbart@nginx.com }
78516Svbart@nginx.com 
78616Svbart@nginx.com 
787112Smax.romanov@nginx.com #define                                                                       \
788112Smax.romanov@nginx.com nxt_http_is_normal(c)                                                         \
789112Smax.romanov@nginx.com     (nxt_fast_path((nxt_http_normal[c / 8] & (1 << (c & 7))) != 0))
790112Smax.romanov@nginx.com 
791112Smax.romanov@nginx.com 
792112Smax.romanov@nginx.com static const uint8_t  nxt_http_normal[32]  nxt_aligned(32) = {
793112Smax.romanov@nginx.com 
794112Smax.romanov@nginx.com                              /*        \0   \r  \n                         */
795112Smax.romanov@nginx.com     0xfe, 0xdb, 0xff, 0xff,  /* 1111 1110  1101 1011  1111 1111  1111 1111 */
796112Smax.romanov@nginx.com 
797112Smax.romanov@nginx.com                              /* '&%$ #"!   /.-, |*)(  7654 3210  ?>=< ;:98 */
798112Smax.romanov@nginx.com     0xd6, 0x37, 0xff, 0x7f,  /* 1101 0110  0011 0111  1111 1111  0111 1111 */
799112Smax.romanov@nginx.com 
800112Smax.romanov@nginx.com                              /* GFED CBA@  ONML KJIH  WVUT SRQP  _^]\ [ZYX */
801112Smax.romanov@nginx.com     0xff, 0xff, 0xff, 0xff,  /* 1111 1111  1111 1111  1111 1111  1111 1111 */
802112Smax.romanov@nginx.com 
803112Smax.romanov@nginx.com                              /* gfed cba`  onml kjih  wvut srqp   ~}| {zyx */
804112Smax.romanov@nginx.com     0xff, 0xff, 0xff, 0xff,  /* 1111 1111  1111 1111  1111 1111  1111 1111 */
805112Smax.romanov@nginx.com 
806112Smax.romanov@nginx.com     0xff, 0xff, 0xff, 0xff,  /* 1111 1111  1111 1111  1111 1111  1111 1111 */
807112Smax.romanov@nginx.com     0xff, 0xff, 0xff, 0xff,  /* 1111 1111  1111 1111  1111 1111  1111 1111 */
808112Smax.romanov@nginx.com     0xff, 0xff, 0xff, 0xff,  /* 1111 1111  1111 1111  1111 1111  1111 1111 */
809112Smax.romanov@nginx.com     0xff, 0xff, 0xff, 0xff,  /* 1111 1111  1111 1111  1111 1111  1111 1111 */
810112Smax.romanov@nginx.com };
811112Smax.romanov@nginx.com 
812112Smax.romanov@nginx.com 
813112Smax.romanov@nginx.com static nxt_int_t
814112Smax.romanov@nginx.com nxt_http_parse_complex_target(nxt_http_request_parse_t *rp)
815112Smax.romanov@nginx.com {
816112Smax.romanov@nginx.com     u_char  *p, *u, c, ch, high;
817112Smax.romanov@nginx.com     enum {
818112Smax.romanov@nginx.com         sw_normal = 0,
819112Smax.romanov@nginx.com         sw_slash,
820112Smax.romanov@nginx.com         sw_dot,
821112Smax.romanov@nginx.com         sw_dot_dot,
822112Smax.romanov@nginx.com         sw_quoted,
823112Smax.romanov@nginx.com         sw_quoted_second,
824112Smax.romanov@nginx.com     } state, saved_state;
825112Smax.romanov@nginx.com 
826112Smax.romanov@nginx.com     nxt_prefetch(nxt_http_normal);
827112Smax.romanov@nginx.com 
828112Smax.romanov@nginx.com     state = sw_normal;
829112Smax.romanov@nginx.com     saved_state = sw_normal;
830112Smax.romanov@nginx.com     p = rp->target_start;
831112Smax.romanov@nginx.com 
832112Smax.romanov@nginx.com     u = nxt_mp_alloc(rp->mem_pool, rp->target_end - p + 1);
833112Smax.romanov@nginx.com 
834112Smax.romanov@nginx.com     if (nxt_slow_path(u == NULL)) {
835112Smax.romanov@nginx.com         return NXT_ERROR;
836112Smax.romanov@nginx.com     }
837112Smax.romanov@nginx.com 
838112Smax.romanov@nginx.com     rp->path.length = 0;
839112Smax.romanov@nginx.com     rp->path.start = u;
840112Smax.romanov@nginx.com 
841112Smax.romanov@nginx.com     high = '\0';
842112Smax.romanov@nginx.com     rp->exten_start = NULL;
843112Smax.romanov@nginx.com     rp->args_start = NULL;
844112Smax.romanov@nginx.com 
845112Smax.romanov@nginx.com     while (p < rp->target_end) {
846112Smax.romanov@nginx.com 
847112Smax.romanov@nginx.com         ch = *p++;
848112Smax.romanov@nginx.com 
849112Smax.romanov@nginx.com     again:
850112Smax.romanov@nginx.com 
851112Smax.romanov@nginx.com         switch (state) {
852112Smax.romanov@nginx.com 
853112Smax.romanov@nginx.com         case sw_normal:
854112Smax.romanov@nginx.com 
855112Smax.romanov@nginx.com             if (nxt_http_is_normal(ch)) {
856112Smax.romanov@nginx.com                 *u++ = ch;
857112Smax.romanov@nginx.com                 continue;
858112Smax.romanov@nginx.com             }
859112Smax.romanov@nginx.com 
860112Smax.romanov@nginx.com             switch (ch) {
861112Smax.romanov@nginx.com             case '/':
862112Smax.romanov@nginx.com                 rp->exten_start = NULL;
863112Smax.romanov@nginx.com                 state = sw_slash;
864112Smax.romanov@nginx.com                 *u++ = ch;
865112Smax.romanov@nginx.com                 continue;
866112Smax.romanov@nginx.com             case '%':
867112Smax.romanov@nginx.com                 saved_state = state;
868112Smax.romanov@nginx.com                 state = sw_quoted;
869112Smax.romanov@nginx.com                 continue;
870112Smax.romanov@nginx.com             case '?':
871112Smax.romanov@nginx.com                 rp->args_start = p;
872112Smax.romanov@nginx.com                 goto args;
873112Smax.romanov@nginx.com             case '#':
874112Smax.romanov@nginx.com                 goto done;
875112Smax.romanov@nginx.com             case '.':
876112Smax.romanov@nginx.com                 rp->exten_start = u + 1;
877112Smax.romanov@nginx.com                 *u++ = ch;
878112Smax.romanov@nginx.com                 continue;
879112Smax.romanov@nginx.com             case '+':
880112Smax.romanov@nginx.com                 rp->plus_in_target = 1;
881112Smax.romanov@nginx.com                 /* Fall through. */
882112Smax.romanov@nginx.com             default:
883112Smax.romanov@nginx.com                 *u++ = ch;
884112Smax.romanov@nginx.com                 continue;
885112Smax.romanov@nginx.com             }
886112Smax.romanov@nginx.com 
887112Smax.romanov@nginx.com             break;
888112Smax.romanov@nginx.com 
889112Smax.romanov@nginx.com         case sw_slash:
890112Smax.romanov@nginx.com 
891112Smax.romanov@nginx.com             if (nxt_http_is_normal(ch)) {
892112Smax.romanov@nginx.com                 state = sw_normal;
893112Smax.romanov@nginx.com                 *u++ = ch;
894112Smax.romanov@nginx.com                 continue;
895112Smax.romanov@nginx.com             }
896112Smax.romanov@nginx.com 
897112Smax.romanov@nginx.com             switch (ch) {
898112Smax.romanov@nginx.com             case '/':
899112Smax.romanov@nginx.com                 continue;
900112Smax.romanov@nginx.com             case '.':
901112Smax.romanov@nginx.com                 state = sw_dot;
902112Smax.romanov@nginx.com                 *u++ = ch;
903112Smax.romanov@nginx.com                 continue;
904112Smax.romanov@nginx.com             case '%':
905112Smax.romanov@nginx.com                 saved_state = state;
906112Smax.romanov@nginx.com                 state = sw_quoted;
907112Smax.romanov@nginx.com                 continue;
908112Smax.romanov@nginx.com             case '?':
909112Smax.romanov@nginx.com                 rp->args_start = p;
910112Smax.romanov@nginx.com                 goto args;
911112Smax.romanov@nginx.com             case '#':
912112Smax.romanov@nginx.com                 goto done;
913112Smax.romanov@nginx.com             case '+':
914112Smax.romanov@nginx.com                 rp->plus_in_target = 1;
915112Smax.romanov@nginx.com                 /* Fall through. */
916112Smax.romanov@nginx.com             default:
917112Smax.romanov@nginx.com                 state = sw_normal;
918112Smax.romanov@nginx.com                 *u++ = ch;
919112Smax.romanov@nginx.com                 continue;
920112Smax.romanov@nginx.com             }
921112Smax.romanov@nginx.com 
922112Smax.romanov@nginx.com             break;
923112Smax.romanov@nginx.com 
924112Smax.romanov@nginx.com         case sw_dot:
925112Smax.romanov@nginx.com 
926112Smax.romanov@nginx.com             if (nxt_http_is_normal(ch)) {
927112Smax.romanov@nginx.com                 state = sw_normal;
928112Smax.romanov@nginx.com                 *u++ = ch;
929112Smax.romanov@nginx.com                 continue;
930112Smax.romanov@nginx.com             }
931112Smax.romanov@nginx.com 
932112Smax.romanov@nginx.com             switch (ch) {
933112Smax.romanov@nginx.com             case '/':
934112Smax.romanov@nginx.com                 state = sw_slash;
935112Smax.romanov@nginx.com                 u--;
936112Smax.romanov@nginx.com                 continue;
937112Smax.romanov@nginx.com             case '.':
938112Smax.romanov@nginx.com                 state = sw_dot_dot;
939112Smax.romanov@nginx.com                 *u++ = ch;
940112Smax.romanov@nginx.com                 continue;
941112Smax.romanov@nginx.com             case '%':
942112Smax.romanov@nginx.com                 saved_state = state;
943112Smax.romanov@nginx.com                 state = sw_quoted;
944112Smax.romanov@nginx.com                 continue;
945112Smax.romanov@nginx.com             case '?':
946112Smax.romanov@nginx.com                 rp->args_start = p;
947112Smax.romanov@nginx.com                 goto args;
948112Smax.romanov@nginx.com             case '#':
949112Smax.romanov@nginx.com                 goto done;
950112Smax.romanov@nginx.com             case '+':
951112Smax.romanov@nginx.com                 rp->plus_in_target = 1;
952112Smax.romanov@nginx.com                 /* Fall through. */
953112Smax.romanov@nginx.com             default:
954112Smax.romanov@nginx.com                 state = sw_normal;
955112Smax.romanov@nginx.com                 *u++ = ch;
956112Smax.romanov@nginx.com                 continue;
957112Smax.romanov@nginx.com             }
958112Smax.romanov@nginx.com 
959112Smax.romanov@nginx.com             break;
960112Smax.romanov@nginx.com 
961112Smax.romanov@nginx.com         case sw_dot_dot:
962112Smax.romanov@nginx.com 
963112Smax.romanov@nginx.com             if (nxt_http_is_normal(ch)) {
964112Smax.romanov@nginx.com                 state = sw_normal;
965112Smax.romanov@nginx.com                 *u++ = ch;
966112Smax.romanov@nginx.com                 continue;
967112Smax.romanov@nginx.com             }
968112Smax.romanov@nginx.com 
969112Smax.romanov@nginx.com             switch (ch) {
970112Smax.romanov@nginx.com             case '/':
971112Smax.romanov@nginx.com                 state = sw_slash;
972112Smax.romanov@nginx.com                 u -= 5;
973112Smax.romanov@nginx.com                 for ( ;; ) {
974112Smax.romanov@nginx.com                     if (u < rp->path.start) {
975480Svbart@nginx.com                         return NXT_HTTP_PARSE_INVALID;
976112Smax.romanov@nginx.com                     }
977112Smax.romanov@nginx.com                     if (*u == '/') {
978112Smax.romanov@nginx.com                         u++;
979112Smax.romanov@nginx.com                         break;
980112Smax.romanov@nginx.com                     }
981112Smax.romanov@nginx.com                     u--;
982112Smax.romanov@nginx.com                 }
983112Smax.romanov@nginx.com                 break;
984112Smax.romanov@nginx.com 
985112Smax.romanov@nginx.com             case '%':
986112Smax.romanov@nginx.com                 saved_state = state;
987112Smax.romanov@nginx.com                 state = sw_quoted;
988112Smax.romanov@nginx.com                 continue;
989112Smax.romanov@nginx.com             case '?':
990112Smax.romanov@nginx.com                 rp->args_start = p;
991112Smax.romanov@nginx.com                 goto args;
992112Smax.romanov@nginx.com             case '#':
993112Smax.romanov@nginx.com                 goto done;
994112Smax.romanov@nginx.com             case '+':
995112Smax.romanov@nginx.com                 rp->plus_in_target = 1;
996112Smax.romanov@nginx.com                 /* Fall through. */
997112Smax.romanov@nginx.com             default:
998112Smax.romanov@nginx.com                 state = sw_normal;
999112Smax.romanov@nginx.com                 *u++ = ch;
1000112Smax.romanov@nginx.com                 continue;
1001112Smax.romanov@nginx.com             }
1002112Smax.romanov@nginx.com 
1003112Smax.romanov@nginx.com             break;
1004112Smax.romanov@nginx.com 
1005112Smax.romanov@nginx.com         case sw_quoted:
1006112Smax.romanov@nginx.com             rp->quoted_target = 1;
1007112Smax.romanov@nginx.com 
1008112Smax.romanov@nginx.com             if (ch >= '0' && ch <= '9') {
1009112Smax.romanov@nginx.com                 high = (u_char) (ch - '0');
1010112Smax.romanov@nginx.com                 state = sw_quoted_second;
1011112Smax.romanov@nginx.com                 continue;
1012112Smax.romanov@nginx.com             }
1013112Smax.romanov@nginx.com 
1014112Smax.romanov@nginx.com             c = (u_char) (ch | 0x20);
1015112Smax.romanov@nginx.com             if (c >= 'a' && c <= 'f') {
1016112Smax.romanov@nginx.com                 high = (u_char) (c - 'a' + 10);
1017112Smax.romanov@nginx.com                 state = sw_quoted_second;
1018112Smax.romanov@nginx.com                 continue;
1019112Smax.romanov@nginx.com             }
1020112Smax.romanov@nginx.com 
1021480Svbart@nginx.com             return NXT_HTTP_PARSE_INVALID;
1022112Smax.romanov@nginx.com 
1023112Smax.romanov@nginx.com         case sw_quoted_second:
1024112Smax.romanov@nginx.com             if (ch >= '0' && ch <= '9') {
1025112Smax.romanov@nginx.com                 ch = (u_char) ((high << 4) + ch - '0');
1026112Smax.romanov@nginx.com 
1027112Smax.romanov@nginx.com                 if (ch == '%' || ch == '#') {
1028112Smax.romanov@nginx.com                     state = sw_normal;
1029112Smax.romanov@nginx.com                     *u++ = ch;
1030112Smax.romanov@nginx.com                     continue;
1031112Smax.romanov@nginx.com 
1032112Smax.romanov@nginx.com                 } else if (ch == '\0') {
1033480Svbart@nginx.com                     return NXT_HTTP_PARSE_INVALID;
1034112Smax.romanov@nginx.com                 }
1035112Smax.romanov@nginx.com 
1036112Smax.romanov@nginx.com                 state = saved_state;
1037112Smax.romanov@nginx.com                 goto again;
1038112Smax.romanov@nginx.com             }
1039112Smax.romanov@nginx.com 
1040112Smax.romanov@nginx.com             c = (u_char) (ch | 0x20);
1041112Smax.romanov@nginx.com             if (c >= 'a' && c <= 'f') {
1042112Smax.romanov@nginx.com                 ch = (u_char) ((high << 4) + c - 'a' + 10);
1043112Smax.romanov@nginx.com 
1044112Smax.romanov@nginx.com                 if (ch == '?') {
1045112Smax.romanov@nginx.com                     state = sw_normal;
1046112Smax.romanov@nginx.com                     *u++ = ch;
1047112Smax.romanov@nginx.com                     continue;
1048112Smax.romanov@nginx.com 
1049112Smax.romanov@nginx.com                 } else if (ch == '+') {
1050112Smax.romanov@nginx.com                     rp->plus_in_target = 1;
1051112Smax.romanov@nginx.com                 }
1052112Smax.romanov@nginx.com 
1053112Smax.romanov@nginx.com                 state = saved_state;
1054112Smax.romanov@nginx.com                 goto again;
1055112Smax.romanov@nginx.com             }
1056112Smax.romanov@nginx.com 
1057480Svbart@nginx.com             return NXT_HTTP_PARSE_INVALID;
1058112Smax.romanov@nginx.com         }
1059112Smax.romanov@nginx.com     }
1060112Smax.romanov@nginx.com 
1061112Smax.romanov@nginx.com     if (state >= sw_quoted) {
1062480Svbart@nginx.com         return NXT_HTTP_PARSE_INVALID;
1063112Smax.romanov@nginx.com     }
1064112Smax.romanov@nginx.com 
1065112Smax.romanov@nginx.com args:
1066112Smax.romanov@nginx.com 
1067112Smax.romanov@nginx.com     for (/* void */; p < rp->target_end; p++) {
1068112Smax.romanov@nginx.com         if (*p == '#') {
1069112Smax.romanov@nginx.com             break;
1070112Smax.romanov@nginx.com         }
1071112Smax.romanov@nginx.com     }
1072112Smax.romanov@nginx.com 
1073112Smax.romanov@nginx.com     if (rp->args_start != NULL) {
1074112Smax.romanov@nginx.com         rp->args.length = p - rp->args_start;
1075112Smax.romanov@nginx.com         rp->args.start = rp->