Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 8 | #include <ngx_core.h> |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 9 | #include <ngx_http.h> |
| 10 | |
Igor Sysoev | 59cf56c | 2004-09-07 15:29:22 +0000 | [diff] [blame] | 11 | |
Igor Sysoev | bc808a7 | 2006-10-31 12:27:32 +0000 | [diff] [blame] | 12 | static uint32_t usual[] = { |
Igor Sysoev | b5c75dc | 2006-10-28 14:36:44 +0000 | [diff] [blame] | 13 | 0xffffdbfe, /* 1111 1111 1111 1111 1101 1011 1111 1110 */ |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 14 | |
Igor Sysoev | b5c75dc | 2006-10-28 14:36:44 +0000 | [diff] [blame] | 15 | /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ |
| 16 | 0x7fff37d6, /* 0111 1111 1111 1111 0011 0111 1101 0110 */ |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 17 | |
Igor Sysoev | b5c75dc | 2006-10-28 14:36:44 +0000 | [diff] [blame] | 18 | /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ |
Igor Sysoev | addd3c8 | 2006-10-30 20:36:54 +0000 | [diff] [blame] | 19 | #if (NGX_WIN32) |
Igor Sysoev | b5c75dc | 2006-10-28 14:36:44 +0000 | [diff] [blame] | 20 | 0xefffffff, /* 1110 1111 1111 1111 1111 1111 1111 1111 */ |
Igor Sysoev | addd3c8 | 2006-10-30 20:36:54 +0000 | [diff] [blame] | 21 | #else |
| 22 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ |
| 23 | #endif |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 24 | |
Igor Sysoev | b5c75dc | 2006-10-28 14:36:44 +0000 | [diff] [blame] | 25 | /* ~}| {zyx wvut srqp onml kjih gfed cba` */ |
| 26 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 27 | |
Igor Sysoev | b5c75dc | 2006-10-28 14:36:44 +0000 | [diff] [blame] | 28 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ |
| 29 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ |
| 30 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ |
| 31 | 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ |
| 32 | }; |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 33 | |
| 34 | |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 35 | #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED) |
| 36 | |
| 37 | #define ngx_str3_cmp(m, c0, c1, c2, c3) \ |
| 38 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) |
| 39 | |
| 40 | #define ngx_str3Ocmp(m, c0, c1, c2, c3) \ |
| 41 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) |
| 42 | |
| 43 | #define ngx_str4cmp(m, c0, c1, c2, c3) \ |
| 44 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) |
| 45 | |
| 46 | #define ngx_str5cmp(m, c0, c1, c2, c3, c4) \ |
| 47 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ |
| 48 | && m[4] == c4 |
| 49 | |
| 50 | #define ngx_str6cmp(m, c0, c1, c2, c3, c4, c5) \ |
| 51 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ |
| 52 | && (((uint32_t *) m)[1] & 0xffff) == ((c5 << 8) | c4) |
| 53 | |
| 54 | #define ngx_str7_cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \ |
| 55 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ |
| 56 | && ((uint32_t *) m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4) |
| 57 | |
| 58 | #define ngx_str8cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \ |
| 59 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ |
| 60 | && ((uint32_t *) m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4) |
| 61 | |
| 62 | #define ngx_str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \ |
| 63 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ |
| 64 | && ((uint32_t *) m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4) \ |
| 65 | && m[8] == c8 |
| 66 | |
| 67 | #else /* !(NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED) */ |
| 68 | |
| 69 | #define ngx_str3_cmp(m, c0, c1, c2, c3) \ |
| 70 | m[0] == c0 && m[1] == c1 && m[2] == c2 |
| 71 | |
| 72 | #define ngx_str3Ocmp(m, c0, c1, c2, c3) \ |
| 73 | m[0] == c0 && m[2] == c2 && m[3] == c3 |
| 74 | |
| 75 | #define ngx_str4cmp(m, c0, c1, c2, c3) \ |
| 76 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 |
| 77 | |
| 78 | #define ngx_str5cmp(m, c0, c1, c2, c3, c4) \ |
| 79 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 && m[4] == c4 |
| 80 | |
| 81 | #define ngx_str6cmp(m, c0, c1, c2, c3, c4, c5) \ |
| 82 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \ |
| 83 | && m[4] == c4 && m[5] == c5 |
| 84 | |
| 85 | #define ngx_str7_cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \ |
| 86 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \ |
| 87 | && m[4] == c4 && m[5] == c5 && m[6] == c6 |
| 88 | |
| 89 | #define ngx_str8cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \ |
| 90 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \ |
| 91 | && m[4] == c4 && m[5] == c5 && m[6] == c6 && m[7] == c7 |
| 92 | |
| 93 | #define ngx_str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \ |
| 94 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \ |
| 95 | && m[4] == c4 && m[5] == c5 && m[6] == c6 && m[7] == c7 && m[8] == c8 |
| 96 | |
| 97 | #endif |
| 98 | |
| 99 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 100 | /* gcc, icc, msvc and others compile these switches as an jump table */ |
| 101 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 102 | ngx_int_t |
| 103 | ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b) |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 104 | { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 105 | u_char c, ch, *p, *m; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 106 | enum { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 107 | sw_start = 0, |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 108 | sw_method, |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 109 | sw_spaces_before_uri, |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 110 | sw_schema, |
| 111 | sw_schema_slash, |
| 112 | sw_schema_slash_slash, |
| 113 | sw_host, |
| 114 | sw_port, |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 115 | sw_after_slash_in_uri, |
| 116 | sw_check_uri, |
| 117 | sw_uri, |
| 118 | sw_http_09, |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 119 | sw_http_H, |
| 120 | sw_http_HT, |
| 121 | sw_http_HTT, |
| 122 | sw_http_HTTP, |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 123 | sw_first_major_digit, |
| 124 | sw_major_digit, |
| 125 | sw_first_minor_digit, |
| 126 | sw_minor_digit, |
Igor Sysoev | 81924e8 | 2007-10-26 11:34:10 +0000 | [diff] [blame] | 127 | sw_spaces_after_digit, |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 128 | sw_almost_done |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 129 | } state; |
| 130 | |
| 131 | state = r->state; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 132 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 133 | for (p = b->pos; p < b->last; p++) { |
| 134 | ch = *p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 135 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 136 | switch (state) { |
| 137 | |
| 138 | /* HTTP methods: GET, HEAD, POST */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 139 | case sw_start: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 140 | r->request_start = p; |
Igor Sysoev | 3d062ad | 2003-03-05 06:37:42 +0000 | [diff] [blame] | 141 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 142 | if (ch == CR || ch == LF) { |
| 143 | break; |
| 144 | } |
| 145 | |
Igor Sysoev | df4b230 | 2009-07-13 09:33:34 +0000 | [diff] [blame] | 146 | if ((ch < 'A' || ch > 'Z') && ch != '_') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 147 | return NGX_HTTP_PARSE_INVALID_METHOD; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 148 | } |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 149 | |
| 150 | state = sw_method; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 151 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 152 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 153 | case sw_method: |
| 154 | if (ch == ' ') { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 155 | r->method_end = p - 1; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 156 | m = r->request_start; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 157 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 158 | switch (p - m) { |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 159 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 160 | case 3: |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 161 | if (ngx_str3_cmp(m, 'G', 'E', 'T', ' ')) { |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 162 | r->method = NGX_HTTP_GET; |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 163 | break; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 164 | } |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 165 | |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 166 | if (ngx_str3_cmp(m, 'P', 'U', 'T', ' ')) { |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 167 | r->method = NGX_HTTP_PUT; |
| 168 | break; |
| 169 | } |
| 170 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 171 | break; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 172 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 173 | case 4: |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 174 | if (m[1] == 'O') { |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 175 | |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 176 | if (ngx_str3Ocmp(m, 'P', 'O', 'S', 'T')) { |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 177 | r->method = NGX_HTTP_POST; |
| 178 | break; |
| 179 | } |
| 180 | |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 181 | if (ngx_str3Ocmp(m, 'C', 'O', 'P', 'Y')) { |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 182 | r->method = NGX_HTTP_COPY; |
| 183 | break; |
| 184 | } |
| 185 | |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 186 | if (ngx_str3Ocmp(m, 'M', 'O', 'V', 'E')) { |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 187 | r->method = NGX_HTTP_MOVE; |
| 188 | break; |
| 189 | } |
| 190 | |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 191 | if (ngx_str3Ocmp(m, 'L', 'O', 'C', 'K')) { |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 192 | r->method = NGX_HTTP_LOCK; |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | } else { |
| 197 | |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 198 | if (ngx_str4cmp(m, 'H', 'E', 'A', 'D')) { |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 199 | r->method = NGX_HTTP_HEAD; |
| 200 | break; |
| 201 | } |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 202 | } |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 203 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 204 | break; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 205 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 206 | case 5: |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 207 | if (ngx_str5cmp(m, 'M', 'K', 'C', 'O', 'L')) { |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 208 | r->method = NGX_HTTP_MKCOL; |
| 209 | } |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 210 | |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 211 | if (ngx_str5cmp(m, 'T', 'R', 'A', 'C', 'E')) { |
Igor Sysoev | 3e933d2 | 2007-03-30 18:59:26 +0000 | [diff] [blame] | 212 | r->method = NGX_HTTP_TRACE; |
| 213 | } |
| 214 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 215 | break; |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 216 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 217 | case 6: |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 218 | if (ngx_str6cmp(m, 'D', 'E', 'L', 'E', 'T', 'E')) { |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 219 | r->method = NGX_HTTP_DELETE; |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 220 | break; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 221 | } |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 222 | |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 223 | if (ngx_str6cmp(m, 'U', 'N', 'L', 'O', 'C', 'K')) { |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 224 | r->method = NGX_HTTP_UNLOCK; |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | break; |
| 229 | |
| 230 | case 7: |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 231 | if (ngx_str7_cmp(m, 'O', 'P', 'T', 'I', 'O', 'N', 'S', ' ')) |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 232 | { |
| 233 | r->method = NGX_HTTP_OPTIONS; |
| 234 | } |
| 235 | |
| 236 | break; |
| 237 | |
| 238 | case 8: |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 239 | if (ngx_str8cmp(m, 'P', 'R', 'O', 'P', 'F', 'I', 'N', 'D')) |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 240 | { |
| 241 | r->method = NGX_HTTP_PROPFIND; |
| 242 | } |
| 243 | |
| 244 | break; |
| 245 | |
| 246 | case 9: |
Igor Sysoev | 544e9f1 | 2007-05-07 06:27:14 +0000 | [diff] [blame] | 247 | if (ngx_str9cmp(m, |
| 248 | 'P', 'R', 'O', 'P', 'P', 'A', 'T', 'C', 'H')) |
Igor Sysoev | 8365f73 | 2006-11-14 12:43:48 +0000 | [diff] [blame] | 249 | { |
| 250 | r->method = NGX_HTTP_PROPPATCH; |
| 251 | } |
| 252 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 253 | break; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 254 | } |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 255 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 256 | state = sw_spaces_before_uri; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 257 | break; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 258 | } |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 259 | |
Igor Sysoev | df4b230 | 2009-07-13 09:33:34 +0000 | [diff] [blame] | 260 | if ((ch < 'A' || ch > 'Z') && ch != '_') { |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 261 | return NGX_HTTP_PARSE_INVALID_METHOD; |
| 262 | } |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 263 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 264 | break; |
| 265 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 266 | /* space* before URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 267 | case sw_spaces_before_uri: |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 268 | |
Igor Sysoev | e23b484 | 2006-10-28 10:47:11 +0000 | [diff] [blame] | 269 | if (ch == '/' ){ |
| 270 | r->uri_start = p; |
| 271 | state = sw_after_slash_in_uri; |
| 272 | break; |
| 273 | } |
| 274 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 275 | c = (u_char) (ch | 0x20); |
Igor Sysoev | 7b190b4 | 2005-06-07 15:56:31 +0000 | [diff] [blame] | 276 | if (c >= 'a' && c <= 'z') { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 277 | r->schema_start = p; |
| 278 | state = sw_schema; |
| 279 | break; |
| 280 | } |
| 281 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 282 | switch (ch) { |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 283 | case ' ': |
| 284 | break; |
| 285 | default: |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 286 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 287 | } |
| 288 | break; |
| 289 | |
| 290 | case sw_schema: |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 291 | |
| 292 | c = (u_char) (ch | 0x20); |
Igor Sysoev | 7b190b4 | 2005-06-07 15:56:31 +0000 | [diff] [blame] | 293 | if (c >= 'a' && c <= 'z') { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 297 | switch (ch) { |
| 298 | case ':': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 299 | r->schema_end = p; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 300 | state = sw_schema_slash; |
| 301 | break; |
| 302 | default: |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 303 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 304 | } |
| 305 | break; |
| 306 | |
| 307 | case sw_schema_slash: |
| 308 | switch (ch) { |
| 309 | case '/': |
| 310 | state = sw_schema_slash_slash; |
| 311 | break; |
| 312 | default: |
| 313 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 314 | } |
| 315 | break; |
| 316 | |
| 317 | case sw_schema_slash_slash: |
| 318 | switch (ch) { |
| 319 | case '/': |
Igor Sysoev | 95ead46 | 2006-11-23 20:20:23 +0000 | [diff] [blame] | 320 | r->host_start = p + 1; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 321 | state = sw_host; |
| 322 | break; |
| 323 | default: |
| 324 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 325 | } |
| 326 | break; |
| 327 | |
| 328 | case sw_host: |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 329 | |
| 330 | c = (u_char) (ch | 0x20); |
Igor Sysoev | 7b190b4 | 2005-06-07 15:56:31 +0000 | [diff] [blame] | 331 | if (c >= 'a' && c <= 'z') { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | |
Igor Sysoev | e9b7809 | 2006-10-31 12:30:24 +0000 | [diff] [blame] | 335 | if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-') { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | |
Igor Sysoev | 4c5207f | 2007-10-18 11:33:31 +0000 | [diff] [blame] | 339 | r->host_end = p; |
| 340 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 341 | switch (ch) { |
| 342 | case ':': |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 343 | state = sw_port; |
| 344 | break; |
| 345 | case '/': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 346 | r->uri_start = p; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 347 | state = sw_after_slash_in_uri; |
| 348 | break; |
Igor Sysoev | 4c5207f | 2007-10-18 11:33:31 +0000 | [diff] [blame] | 349 | case ' ': |
| 350 | /* |
| 351 | * use single "/" from request line to preserve pointers, |
| 352 | * if request line will be copied to large client buffer |
| 353 | */ |
| 354 | r->uri_start = r->schema_end + 1; |
| 355 | r->uri_end = r->schema_end + 2; |
| 356 | state = sw_http_09; |
| 357 | break; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 358 | default: |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 359 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 360 | } |
| 361 | break; |
| 362 | |
| 363 | case sw_port: |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 364 | if (ch >= '0' && ch <= '9') { |
| 365 | break; |
| 366 | } |
| 367 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 368 | switch (ch) { |
| 369 | case '/': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 370 | r->port_end = p; |
| 371 | r->uri_start = p; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 372 | state = sw_after_slash_in_uri; |
| 373 | break; |
Igor Sysoev | 4c5207f | 2007-10-18 11:33:31 +0000 | [diff] [blame] | 374 | case ' ': |
| 375 | r->port_end = p; |
| 376 | /* |
| 377 | * use single "/" from request line to preserve pointers, |
| 378 | * if request line will be copied to large client buffer |
| 379 | */ |
| 380 | r->uri_start = r->schema_end + 1; |
| 381 | r->uri_end = r->schema_end + 2; |
| 382 | state = sw_http_09; |
| 383 | break; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 384 | default: |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 385 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 386 | } |
| 387 | break; |
| 388 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 389 | /* check "/.", "//", "%", and "\" (Win32) in URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 390 | case sw_after_slash_in_uri: |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 391 | |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 392 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 393 | state = sw_check_uri; |
| 394 | break; |
| 395 | } |
| 396 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 397 | switch (ch) { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 398 | case ' ': |
| 399 | r->uri_end = p; |
| 400 | state = sw_http_09; |
| 401 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 402 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 403 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 404 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 405 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 406 | break; |
| 407 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 408 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 409 | r->http_minor = 9; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 410 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 411 | case '.': |
| 412 | r->complex_uri = 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 413 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 414 | break; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 415 | case '%': |
| 416 | r->quoted_uri = 1; |
| 417 | state = sw_uri; |
| 418 | break; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 419 | case '/': |
| 420 | r->complex_uri = 1; |
| 421 | state = sw_uri; |
| 422 | break; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 423 | #if (NGX_WIN32) |
| 424 | case '\\': |
| 425 | r->complex_uri = 1; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 426 | state = sw_uri; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 427 | break; |
| 428 | #endif |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 429 | case '?': |
| 430 | r->args_start = p + 1; |
| 431 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 432 | break; |
Igor Sysoev | b80a7f4 | 2006-10-28 10:15:31 +0000 | [diff] [blame] | 433 | case '#': |
| 434 | r->complex_uri = 1; |
| 435 | state = sw_uri; |
| 436 | break; |
Igor Sysoev | 805d9db | 2005-02-03 19:33:37 +0000 | [diff] [blame] | 437 | case '+': |
| 438 | r->plus_in_uri = 1; |
| 439 | break; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 440 | case '\0': |
| 441 | r->zero_in_uri = 1; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 442 | break; |
| 443 | default: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 444 | state = sw_check_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 445 | break; |
| 446 | } |
| 447 | break; |
| 448 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 449 | /* check "/", "%" and "\" (Win32) in URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 450 | case sw_check_uri: |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 451 | |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 452 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 453 | break; |
| 454 | } |
| 455 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 456 | switch (ch) { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 457 | case '/': |
| 458 | r->uri_ext = NULL; |
| 459 | state = sw_after_slash_in_uri; |
| 460 | break; |
| 461 | case '.': |
| 462 | r->uri_ext = p + 1; |
| 463 | break; |
| 464 | case ' ': |
| 465 | r->uri_end = p; |
| 466 | state = sw_http_09; |
| 467 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 468 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 469 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 470 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 471 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 472 | break; |
| 473 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 474 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 475 | r->http_minor = 9; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 476 | goto done; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 477 | #if (NGX_WIN32) |
| 478 | case '\\': |
| 479 | r->complex_uri = 1; |
| 480 | state = sw_after_slash_in_uri; |
| 481 | break; |
| 482 | #endif |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 483 | case '%': |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 484 | r->quoted_uri = 1; |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 485 | state = sw_uri; |
| 486 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 487 | case '?': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 488 | r->args_start = p + 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 489 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 490 | break; |
Igor Sysoev | b80a7f4 | 2006-10-28 10:15:31 +0000 | [diff] [blame] | 491 | case '#': |
| 492 | r->complex_uri = 1; |
| 493 | state = sw_uri; |
| 494 | break; |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 495 | case '+': |
| 496 | r->plus_in_uri = 1; |
| 497 | break; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 498 | case '\0': |
| 499 | r->zero_in_uri = 1; |
| 500 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 501 | } |
| 502 | break; |
| 503 | |
| 504 | /* URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 505 | case sw_uri: |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 506 | |
| 507 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { |
| 508 | break; |
| 509 | } |
| 510 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 511 | switch (ch) { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 512 | case ' ': |
| 513 | r->uri_end = p; |
| 514 | state = sw_http_09; |
| 515 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 516 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 517 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 518 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 519 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 520 | break; |
| 521 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 522 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 523 | r->http_minor = 9; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 524 | goto done; |
Igor Sysoev | b80a7f4 | 2006-10-28 10:15:31 +0000 | [diff] [blame] | 525 | case '#': |
| 526 | r->complex_uri = 1; |
| 527 | break; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 528 | case '\0': |
| 529 | r->zero_in_uri = 1; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 530 | break; |
| 531 | } |
| 532 | break; |
| 533 | |
| 534 | /* space+ after URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 535 | case sw_http_09: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 536 | switch (ch) { |
| 537 | case ' ': |
| 538 | break; |
| 539 | case CR: |
| 540 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 541 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 542 | break; |
| 543 | case LF: |
| 544 | r->http_minor = 9; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 545 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 546 | case 'H': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 547 | r->http_protocol.data = p; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 548 | state = sw_http_H; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 549 | break; |
| 550 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 551 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 552 | } |
| 553 | break; |
| 554 | |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 555 | case sw_http_H: |
| 556 | switch (ch) { |
| 557 | case 'T': |
| 558 | state = sw_http_HT; |
| 559 | break; |
| 560 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 561 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 562 | } |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 563 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 564 | |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 565 | case sw_http_HT: |
| 566 | switch (ch) { |
| 567 | case 'T': |
| 568 | state = sw_http_HTT; |
| 569 | break; |
| 570 | default: |
| 571 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 572 | } |
| 573 | break; |
| 574 | |
| 575 | case sw_http_HTT: |
| 576 | switch (ch) { |
| 577 | case 'P': |
| 578 | state = sw_http_HTTP; |
| 579 | break; |
| 580 | default: |
| 581 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 582 | } |
| 583 | break; |
| 584 | |
| 585 | case sw_http_HTTP: |
| 586 | switch (ch) { |
| 587 | case '/': |
| 588 | state = sw_first_major_digit; |
| 589 | break; |
| 590 | default: |
| 591 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 592 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 593 | break; |
| 594 | |
| 595 | /* first digit of major HTTP version */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 596 | case sw_first_major_digit: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 597 | if (ch < '1' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 598 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 599 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 600 | |
| 601 | r->http_major = ch - '0'; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 602 | state = sw_major_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 603 | break; |
| 604 | |
| 605 | /* major HTTP version or dot */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 606 | case sw_major_digit: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 607 | if (ch == '.') { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 608 | state = sw_first_minor_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 609 | break; |
| 610 | } |
| 611 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 612 | if (ch < '0' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 613 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 614 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 615 | |
| 616 | r->http_major = r->http_major * 10 + ch - '0'; |
| 617 | break; |
| 618 | |
| 619 | /* first digit of minor HTTP version */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 620 | case sw_first_minor_digit: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 621 | if (ch < '0' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 622 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 623 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 624 | |
| 625 | r->http_minor = ch - '0'; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 626 | state = sw_minor_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 627 | break; |
| 628 | |
| 629 | /* minor HTTP version or end of request line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 630 | case sw_minor_digit: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 631 | if (ch == CR) { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 632 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 633 | break; |
| 634 | } |
| 635 | |
| 636 | if (ch == LF) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 637 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Igor Sysoev | 81924e8 | 2007-10-26 11:34:10 +0000 | [diff] [blame] | 640 | if (ch == ' ') { |
| 641 | state = sw_spaces_after_digit; |
| 642 | break; |
| 643 | } |
| 644 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 645 | if (ch < '0' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 646 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 647 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 648 | |
| 649 | r->http_minor = r->http_minor * 10 + ch - '0'; |
| 650 | break; |
| 651 | |
Igor Sysoev | 81924e8 | 2007-10-26 11:34:10 +0000 | [diff] [blame] | 652 | case sw_spaces_after_digit: |
| 653 | switch (ch) { |
| 654 | case ' ': |
| 655 | break; |
| 656 | case CR: |
| 657 | state = sw_almost_done; |
| 658 | break; |
| 659 | case LF: |
| 660 | goto done; |
| 661 | default: |
| 662 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 663 | } |
| 664 | break; |
| 665 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 666 | /* end of request line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 667 | case sw_almost_done: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 668 | r->request_end = p - 1; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 669 | switch (ch) { |
| 670 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 671 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 672 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 673 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 674 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 678 | b->pos = p; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 679 | r->state = state; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 680 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 681 | return NGX_AGAIN; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 682 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 683 | done: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 684 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 685 | b->pos = p + 1; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 686 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 687 | if (r->request_end == NULL) { |
| 688 | r->request_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 689 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 690 | |
| 691 | r->http_version = r->http_major * 1000 + r->http_minor; |
| 692 | r->state = sw_start; |
| 693 | |
| 694 | if (r->http_version == 9 && r->method != NGX_HTTP_GET) { |
| 695 | return NGX_HTTP_PARSE_INVALID_09_METHOD; |
| 696 | } |
| 697 | |
| 698 | return NGX_OK; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 701 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 702 | ngx_int_t |
Igor Sysoev | 753792e | 2008-09-24 14:02:50 +0000 | [diff] [blame] | 703 | ngx_http_parse_header_line(ngx_http_request_t *r, ngx_buf_t *b, |
| 704 | ngx_uint_t allow_underscores) |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 705 | { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 706 | u_char c, ch, *p; |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 707 | ngx_uint_t hash, i; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 708 | enum { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 709 | sw_start = 0, |
| 710 | sw_name, |
| 711 | sw_space_before_value, |
| 712 | sw_value, |
| 713 | sw_space_after_value, |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 714 | sw_ignore_line, |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 715 | sw_almost_done, |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 716 | sw_header_almost_done |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 717 | } state; |
| 718 | |
Igor Sysoev | bb8bbb7 | 2006-10-17 12:47:14 +0000 | [diff] [blame] | 719 | /* the last '\0' is not needed because string is zero terminated */ |
| 720 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 721 | static u_char lowcase[] = |
| 722 | "\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" |
| 723 | "\0\0\0\0\0\0\0\0\0\0\0\0\0-\0\0" "0123456789\0\0\0\0\0\0" |
Igor Sysoev | 753792e | 2008-09-24 14:02:50 +0000 | [diff] [blame] | 724 | "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0" |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 725 | "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0" |
| 726 | "\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" |
| 727 | "\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" |
| 728 | "\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" |
Igor Sysoev | bb8bbb7 | 2006-10-17 12:47:14 +0000 | [diff] [blame] | 729 | "\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"; |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 730 | |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 731 | state = r->state; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 732 | hash = r->header_hash; |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 733 | i = r->lowcase_index; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 734 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 735 | for (p = b->pos; p < b->last; p++) { |
| 736 | ch = *p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 737 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 738 | switch (state) { |
| 739 | |
| 740 | /* first char */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 741 | case sw_start: |
Igor Sysoev | 63494b4 | 2009-09-02 07:02:49 +0000 | [diff] [blame] | 742 | r->header_name_start = p; |
Igor Sysoev | 3362b8d | 2005-05-14 18:42:03 +0000 | [diff] [blame] | 743 | r->invalid_header = 0; |
| 744 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 745 | switch (ch) { |
| 746 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 747 | r->header_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 748 | state = sw_header_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 749 | break; |
| 750 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 751 | r->header_end = p; |
| 752 | goto header_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 753 | default: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 754 | state = sw_name; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 755 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 756 | c = lowcase[ch]; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 757 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 758 | if (c) { |
| 759 | hash = ngx_hash(0, c); |
| 760 | r->lowcase_header[0] = c; |
| 761 | i = 1; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 762 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 763 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 764 | |
Igor Sysoev | 3362b8d | 2005-05-14 18:42:03 +0000 | [diff] [blame] | 765 | r->invalid_header = 1; |
| 766 | |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 767 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 768 | |
| 769 | } |
| 770 | break; |
| 771 | |
| 772 | /* header name */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 773 | case sw_name: |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 774 | c = lowcase[ch]; |
| 775 | |
| 776 | if (c) { |
| 777 | hash = ngx_hash(hash, c); |
| 778 | r->lowcase_header[i++] = c; |
Igor Sysoev | 8c8a6e5 | 2008-03-16 13:29:49 +0000 | [diff] [blame] | 779 | i &= (NGX_HTTP_LC_HEADER_LEN - 1); |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 780 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 781 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 782 | |
Igor Sysoev | 753792e | 2008-09-24 14:02:50 +0000 | [diff] [blame] | 783 | if (ch == '_') { |
| 784 | if (allow_underscores) { |
| 785 | hash = ngx_hash(hash, ch); |
| 786 | r->lowcase_header[i++] = ch; |
| 787 | i &= (NGX_HTTP_LC_HEADER_LEN - 1); |
| 788 | |
| 789 | } else { |
| 790 | r->invalid_header = 1; |
| 791 | } |
| 792 | |
| 793 | break; |
| 794 | } |
| 795 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 796 | if (ch == ':') { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 797 | r->header_name_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 798 | state = sw_space_before_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 799 | break; |
| 800 | } |
| 801 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 802 | if (ch == CR) { |
| 803 | r->header_name_end = p; |
| 804 | r->header_start = p; |
| 805 | r->header_end = p; |
| 806 | state = sw_almost_done; |
| 807 | break; |
| 808 | } |
| 809 | |
| 810 | if (ch == LF) { |
| 811 | r->header_name_end = p; |
| 812 | r->header_start = p; |
| 813 | r->header_end = p; |
| 814 | goto done; |
| 815 | } |
| 816 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 817 | /* IIS may send the duplicate "HTTP/1.1 ..." lines */ |
Igor Sysoev | 183f9a6 | 2003-04-09 15:42:08 +0000 | [diff] [blame] | 818 | if (ch == '/' |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 819 | && r->upstream |
| 820 | && p - r->header_name_start == 4 |
| 821 | && ngx_strncmp(r->header_name_start, "HTTP", 4) == 0) |
Igor Sysoev | 183f9a6 | 2003-04-09 15:42:08 +0000 | [diff] [blame] | 822 | { |
| 823 | state = sw_ignore_line; |
| 824 | break; |
Igor Sysoev | e2a3154 | 2003-04-08 15:40:10 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Igor Sysoev | 3362b8d | 2005-05-14 18:42:03 +0000 | [diff] [blame] | 827 | r->invalid_header = 1; |
| 828 | |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 829 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 830 | |
| 831 | /* space* before header value */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 832 | case sw_space_before_value: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 833 | switch (ch) { |
| 834 | case ' ': |
| 835 | break; |
| 836 | case CR: |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 837 | r->header_start = p; |
| 838 | r->header_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 839 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 840 | break; |
| 841 | case LF: |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 842 | r->header_start = p; |
| 843 | r->header_end = p; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 844 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 845 | default: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 846 | r->header_start = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 847 | state = sw_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 848 | break; |
| 849 | } |
| 850 | break; |
| 851 | |
| 852 | /* header value */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 853 | case sw_value: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 854 | switch (ch) { |
| 855 | case ' ': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 856 | r->header_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 857 | state = sw_space_after_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 858 | break; |
| 859 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 860 | r->header_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 861 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 862 | break; |
| 863 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 864 | r->header_end = p; |
| 865 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 866 | } |
| 867 | break; |
| 868 | |
| 869 | /* space* before end of header line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 870 | case sw_space_after_value: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 871 | switch (ch) { |
| 872 | case ' ': |
| 873 | break; |
| 874 | case CR: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 875 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 876 | break; |
| 877 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 878 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 879 | default: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 880 | state = sw_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 881 | break; |
| 882 | } |
| 883 | break; |
| 884 | |
Igor Sysoev | e2a3154 | 2003-04-08 15:40:10 +0000 | [diff] [blame] | 885 | /* ignore header line */ |
| 886 | case sw_ignore_line: |
| 887 | switch (ch) { |
| 888 | case LF: |
| 889 | state = sw_start; |
| 890 | break; |
| 891 | default: |
| 892 | break; |
| 893 | } |
| 894 | break; |
| 895 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 896 | /* end of header line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 897 | case sw_almost_done: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 898 | switch (ch) { |
| 899 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 900 | goto done; |
Igor Sysoev | 70d0961 | 2008-03-16 16:47:16 +0000 | [diff] [blame] | 901 | case CR: |
| 902 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 903 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 904 | return NGX_HTTP_PARSE_INVALID_HEADER; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 905 | } |
Igor Sysoev | 455a7fc | 2006-03-21 08:20:41 +0000 | [diff] [blame] | 906 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 907 | |
| 908 | /* end of header */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 909 | case sw_header_almost_done: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 910 | switch (ch) { |
| 911 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 912 | goto header_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 913 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 914 | return NGX_HTTP_PARSE_INVALID_HEADER; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 915 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 919 | b->pos = p; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 920 | r->state = state; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 921 | r->header_hash = hash; |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 922 | r->lowcase_index = i; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 923 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 924 | return NGX_AGAIN; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 925 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 926 | done: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 927 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 928 | b->pos = p + 1; |
| 929 | r->state = sw_start; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 930 | r->header_hash = hash; |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 931 | r->lowcase_index = i; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 932 | |
| 933 | return NGX_OK; |
| 934 | |
| 935 | header_done: |
| 936 | |
| 937 | b->pos = p + 1; |
| 938 | r->state = sw_start; |
| 939 | |
| 940 | return NGX_HTTP_PARSE_HEADER_DONE; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 941 | } |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 942 | |
| 943 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 944 | ngx_int_t |
Igor Sysoev | 8decab3 | 2007-10-18 11:36:58 +0000 | [diff] [blame] | 945 | ngx_http_parse_complex_uri(ngx_http_request_t *r, ngx_uint_t merge_slashes) |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 946 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 947 | u_char c, ch, decoded, *p, *u; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 948 | enum { |
| 949 | sw_usual = 0, |
| 950 | sw_slash, |
| 951 | sw_dot, |
| 952 | sw_dot_dot, |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 953 | sw_quoted, |
| 954 | sw_quoted_second |
| 955 | } state, quoted_state; |
| 956 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 957 | #if (NGX_SUPPRESS_WARN) |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 958 | decoded = '\0'; |
| 959 | quoted_state = sw_usual; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 960 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 961 | |
| 962 | state = sw_usual; |
| 963 | p = r->uri_start; |
| 964 | u = r->uri.data; |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 965 | r->uri_ext = NULL; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 966 | r->args_start = NULL; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 967 | |
| 968 | ch = *p++; |
| 969 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 970 | while (p <= r->uri_end) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 971 | |
| 972 | /* |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 973 | * we use "ch = *p++" inside the cycle, but this operation is safe, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 974 | * because after the URI there is always at least one charcter: |
| 975 | * the line feed |
| 976 | */ |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 977 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 978 | ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 979 | "s:%d in:'%Xd:%c', out:'%c'", state, ch, ch, *u); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 980 | |
| 981 | switch (state) { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 982 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 983 | case sw_usual: |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 984 | |
| 985 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { |
| 986 | *u++ = ch; |
| 987 | ch = *p++; |
| 988 | break; |
| 989 | } |
| 990 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 991 | switch(ch) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 992 | #if (NGX_WIN32) |
| 993 | case '\\': |
| 994 | r->uri_ext = NULL; |
| 995 | |
| 996 | if (p == r->uri_start + r->uri.len) { |
| 997 | |
| 998 | /* |
| 999 | * we omit the last "\" to cause redirect because |
| 1000 | * the browsers do not treat "\" as "/" in relative URL path |
| 1001 | */ |
| 1002 | |
| 1003 | break; |
| 1004 | } |
| 1005 | |
| 1006 | state = sw_slash; |
| 1007 | *u++ = '/'; |
| 1008 | break; |
| 1009 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1010 | case '/': |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 1011 | r->uri_ext = NULL; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1012 | state = sw_slash; |
| 1013 | *u++ = ch; |
| 1014 | break; |
| 1015 | case '%': |
| 1016 | quoted_state = state; |
| 1017 | state = sw_quoted; |
| 1018 | break; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1019 | case '?': |
| 1020 | r->args_start = p; |
Igor Sysoev | b80a7f4 | 2006-10-28 10:15:31 +0000 | [diff] [blame] | 1021 | goto args; |
| 1022 | case '#': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1023 | goto done; |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 1024 | case '.': |
| 1025 | r->uri_ext = u + 1; |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 1026 | *u++ = ch; |
| 1027 | break; |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 1028 | case '+': |
| 1029 | r->plus_in_uri = 1; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1030 | default: |
| 1031 | *u++ = ch; |
| 1032 | break; |
| 1033 | } |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 1034 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1035 | ch = *p++; |
| 1036 | break; |
| 1037 | |
| 1038 | case sw_slash: |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 1039 | |
| 1040 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { |
| 1041 | state = sw_usual; |
| 1042 | *u++ = ch; |
| 1043 | ch = *p++; |
| 1044 | break; |
| 1045 | } |
| 1046 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1047 | switch(ch) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1048 | #if (NGX_WIN32) |
| 1049 | case '\\': |
Igor Sysoev | 8decab3 | 2007-10-18 11:36:58 +0000 | [diff] [blame] | 1050 | break; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1051 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1052 | case '/': |
Igor Sysoev | b862cf4 | 2007-12-14 14:33:00 +0000 | [diff] [blame] | 1053 | if (!merge_slashes) { |
Igor Sysoev | 8decab3 | 2007-10-18 11:36:58 +0000 | [diff] [blame] | 1054 | *u++ = ch; |
| 1055 | } |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | case '.': |
| 1058 | state = sw_dot; |
| 1059 | *u++ = ch; |
| 1060 | break; |
| 1061 | case '%': |
| 1062 | quoted_state = state; |
| 1063 | state = sw_quoted; |
| 1064 | break; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1065 | case '?': |
| 1066 | r->args_start = p; |
Igor Sysoev | b80a7f4 | 2006-10-28 10:15:31 +0000 | [diff] [blame] | 1067 | goto args; |
| 1068 | case '#': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1069 | goto done; |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 1070 | case '+': |
| 1071 | r->plus_in_uri = 1; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1072 | default: |
| 1073 | state = sw_usual; |
| 1074 | *u++ = ch; |
| 1075 | break; |
| 1076 | } |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 1077 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1078 | ch = *p++; |
| 1079 | break; |
| 1080 | |
| 1081 | case sw_dot: |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 1082 | |
| 1083 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { |
| 1084 | state = sw_usual; |
| 1085 | *u++ = ch; |
| 1086 | ch = *p++; |
| 1087 | break; |
| 1088 | } |
| 1089 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1090 | switch(ch) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1091 | #if (NGX_WIN32) |
| 1092 | case '\\': |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1093 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1094 | case '/': |
| 1095 | state = sw_slash; |
| 1096 | u--; |
| 1097 | break; |
| 1098 | case '.': |
| 1099 | state = sw_dot_dot; |
| 1100 | *u++ = ch; |
| 1101 | break; |
| 1102 | case '%': |
| 1103 | quoted_state = state; |
| 1104 | state = sw_quoted; |
| 1105 | break; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1106 | case '?': |
| 1107 | r->args_start = p; |
Igor Sysoev | b80a7f4 | 2006-10-28 10:15:31 +0000 | [diff] [blame] | 1108 | goto args; |
| 1109 | case '#': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1110 | goto done; |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 1111 | case '+': |
| 1112 | r->plus_in_uri = 1; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1113 | default: |
| 1114 | state = sw_usual; |
| 1115 | *u++ = ch; |
| 1116 | break; |
| 1117 | } |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 1118 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1119 | ch = *p++; |
| 1120 | break; |
| 1121 | |
| 1122 | case sw_dot_dot: |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 1123 | |
| 1124 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { |
| 1125 | state = sw_usual; |
| 1126 | *u++ = ch; |
| 1127 | ch = *p++; |
| 1128 | break; |
| 1129 | } |
| 1130 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1131 | switch(ch) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1132 | #if (NGX_WIN32) |
| 1133 | case '\\': |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1134 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1135 | case '/': |
| 1136 | state = sw_slash; |
Igor Sysoev | 97aa4c8 | 2009-09-14 07:42:01 +0000 | [diff] [blame] | 1137 | u -= 5; |
| 1138 | for ( ;; ) { |
| 1139 | if (u < r->uri.data) { |
| 1140 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 1141 | } |
| 1142 | if (*u == '/') { |
| 1143 | u++; |
| 1144 | break; |
| 1145 | } |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1146 | u--; |
| 1147 | } |
| 1148 | break; |
| 1149 | case '%': |
| 1150 | quoted_state = state; |
| 1151 | state = sw_quoted; |
| 1152 | break; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1153 | case '?': |
| 1154 | r->args_start = p; |
Igor Sysoev | b80a7f4 | 2006-10-28 10:15:31 +0000 | [diff] [blame] | 1155 | goto args; |
| 1156 | case '#': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1157 | goto done; |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 1158 | case '+': |
| 1159 | r->plus_in_uri = 1; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1160 | default: |
| 1161 | state = sw_usual; |
| 1162 | *u++ = ch; |
| 1163 | break; |
| 1164 | } |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 1165 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1166 | ch = *p++; |
| 1167 | break; |
| 1168 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1169 | case sw_quoted: |
Igor Sysoev | 8f12558 | 2006-07-28 15:16:17 +0000 | [diff] [blame] | 1170 | r->quoted_uri = 1; |
| 1171 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1172 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 1173 | decoded = (u_char) (ch - '0'); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1174 | state = sw_quoted_second; |
| 1175 | ch = *p++; |
| 1176 | break; |
| 1177 | } |
| 1178 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 1179 | c = (u_char) (ch | 0x20); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1180 | if (c >= 'a' && c <= 'f') { |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 1181 | decoded = (u_char) (c - 'a' + 10); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1182 | state = sw_quoted_second; |
| 1183 | ch = *p++; |
| 1184 | break; |
| 1185 | } |
| 1186 | |
| 1187 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 1188 | |
| 1189 | case sw_quoted_second: |
| 1190 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 1191 | ch = (u_char) ((decoded << 4) + ch - '0'); |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 1192 | |
Igor Sysoev | 7adb8c0 | 2003-12-02 16:57:05 +0000 | [diff] [blame] | 1193 | if (ch == '%') { |
| 1194 | state = sw_usual; |
| 1195 | *u++ = ch; |
| 1196 | ch = *p++; |
| 1197 | break; |
| 1198 | } |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 1199 | |
Igor Sysoev | b80a7f4 | 2006-10-28 10:15:31 +0000 | [diff] [blame] | 1200 | if (ch == '#') { |
| 1201 | *u++ = ch; |
| 1202 | ch = *p++; |
| 1203 | |
| 1204 | } else if (ch == '\0') { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 1205 | r->zero_in_uri = 1; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1208 | state = quoted_state; |
| 1209 | break; |
| 1210 | } |
| 1211 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 1212 | c = (u_char) (ch | 0x20); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1213 | if (c >= 'a' && c <= 'f') { |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 1214 | ch = (u_char) ((decoded << 4) + c - 'a' + 10); |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 1215 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1216 | if (ch == '?') { |
Igor Sysoev | 7adb8c0 | 2003-12-02 16:57:05 +0000 | [diff] [blame] | 1217 | *u++ = ch; |
| 1218 | ch = *p++; |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 1219 | |
| 1220 | } else if (ch == '+') { |
| 1221 | r->plus_in_uri = 1; |
Igor Sysoev | 7adb8c0 | 2003-12-02 16:57:05 +0000 | [diff] [blame] | 1222 | } |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 1223 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1224 | state = quoted_state; |
| 1225 | break; |
| 1226 | } |
| 1227 | |
| 1228 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 1229 | } |
| 1230 | } |
| 1231 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1232 | done: |
| 1233 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1234 | r->uri.len = u - r->uri.data; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1235 | |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 1236 | if (r->uri_ext) { |
| 1237 | r->exten.len = u - r->uri_ext; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1238 | r->exten.data = r->uri_ext; |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | r->uri_ext = NULL; |
| 1242 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1243 | return NGX_OK; |
Igor Sysoev | b80a7f4 | 2006-10-28 10:15:31 +0000 | [diff] [blame] | 1244 | |
| 1245 | args: |
| 1246 | |
| 1247 | while (p < r->uri_end) { |
| 1248 | if (*p++ != '#') { |
| 1249 | continue; |
| 1250 | } |
| 1251 | |
| 1252 | r->args.len = p - 1 - r->args_start; |
| 1253 | r->args.data = r->args_start; |
| 1254 | r->args_start = NULL; |
| 1255 | |
| 1256 | break; |
| 1257 | } |
| 1258 | |
| 1259 | r->uri.len = u - r->uri.data; |
| 1260 | |
| 1261 | if (r->uri_ext) { |
| 1262 | r->exten.len = u - r->uri_ext; |
| 1263 | r->exten.data = r->uri_ext; |
| 1264 | } |
| 1265 | |
| 1266 | r->uri_ext = NULL; |
| 1267 | |
| 1268 | return NGX_OK; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1269 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1270 | |
| 1271 | |
| 1272 | ngx_int_t |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1273 | ngx_http_parse_unsafe_uri(ngx_http_request_t *r, ngx_str_t *uri, |
| 1274 | ngx_str_t *args, ngx_uint_t *flags) |
| 1275 | { |
| 1276 | u_char ch, *p; |
| 1277 | size_t len; |
| 1278 | |
| 1279 | len = uri->len; |
| 1280 | p = uri->data; |
| 1281 | |
| 1282 | if (len == 0 || p[0] == '?') { |
| 1283 | goto unsafe; |
| 1284 | } |
| 1285 | |
Igor Sysoev | bf14b00 | 2009-04-23 16:38:59 +0000 | [diff] [blame] | 1286 | if (p[0] == '.' && len == 3 && p[1] == '.' && (ngx_path_separator(p[2]))) { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1287 | goto unsafe; |
| 1288 | } |
| 1289 | |
| 1290 | for ( /* void */ ; len; len--) { |
| 1291 | |
| 1292 | ch = *p++; |
| 1293 | |
Igor Sysoev | 0359ba8 | 2006-10-28 12:04:43 +0000 | [diff] [blame] | 1294 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { |
| 1295 | continue; |
| 1296 | } |
| 1297 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1298 | if (ch == '?') { |
| 1299 | args->len = len - 1; |
| 1300 | args->data = p; |
| 1301 | uri->len -= len; |
| 1302 | |
| 1303 | return NGX_OK; |
| 1304 | } |
| 1305 | |
| 1306 | if (ch == '\0') { |
| 1307 | *flags |= NGX_HTTP_ZERO_IN_URI; |
| 1308 | continue; |
| 1309 | } |
| 1310 | |
Igor Sysoev | bf14b00 | 2009-04-23 16:38:59 +0000 | [diff] [blame] | 1311 | if (ngx_path_separator(ch) && len > 2) { |
| 1312 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1313 | /* detect "/../" */ |
| 1314 | |
Igor Sysoev | bf14b00 | 2009-04-23 16:38:59 +0000 | [diff] [blame] | 1315 | if (p[0] == '.' && p[1] == '.' && ngx_path_separator(p[2])) { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1316 | goto unsafe; |
| 1317 | } |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | return NGX_OK; |
| 1322 | |
| 1323 | unsafe: |
| 1324 | |
Igor Sysoev | a552ab4 | 2009-09-25 09:30:06 +0000 | [diff] [blame] | 1325 | if (*flags & NGX_HTTP_LOG_UNSAFE) { |
| 1326 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 1327 | "unsafe URI \"%V\" was detected", uri); |
| 1328 | } |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1329 | |
| 1330 | return NGX_ERROR; |
| 1331 | } |
| 1332 | |
| 1333 | |
| 1334 | ngx_int_t |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1335 | ngx_http_parse_multi_header_lines(ngx_array_t *headers, ngx_str_t *name, |
| 1336 | ngx_str_t *value) |
| 1337 | { |
| 1338 | ngx_uint_t i; |
| 1339 | u_char *start, *last, *end, ch; |
| 1340 | ngx_table_elt_t **h; |
| 1341 | |
| 1342 | h = headers->elts; |
| 1343 | |
| 1344 | for (i = 0; i < headers->nelts; i++) { |
| 1345 | |
| 1346 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, headers->pool->log, 0, |
| 1347 | "parse header: \"%V: %V\"", &h[i]->key, &h[i]->value); |
| 1348 | |
| 1349 | if (name->len > h[i]->value.len) { |
| 1350 | continue; |
| 1351 | } |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1352 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1353 | start = h[i]->value.data; |
| 1354 | end = h[i]->value.data + h[i]->value.len; |
| 1355 | |
| 1356 | while (start < end) { |
| 1357 | |
| 1358 | if (ngx_strncasecmp(start, name->data, name->len) != 0) { |
| 1359 | goto skip; |
| 1360 | } |
| 1361 | |
| 1362 | for (start += name->len; start < end && *start == ' '; start++) { |
| 1363 | /* void */ |
| 1364 | } |
| 1365 | |
| 1366 | if (value == NULL) { |
| 1367 | if (start == end || *start == ',') { |
| 1368 | return i; |
| 1369 | } |
| 1370 | |
| 1371 | goto skip; |
| 1372 | } |
| 1373 | |
| 1374 | if (start == end || *start++ != '=') { |
| 1375 | /* the invalid header value */ |
| 1376 | goto skip; |
| 1377 | } |
| 1378 | |
| 1379 | while (start < end && *start == ' ') { start++; } |
| 1380 | |
| 1381 | for (last = start; last < end && *last != ';'; last++) { |
| 1382 | /* void */ |
| 1383 | } |
| 1384 | |
| 1385 | value->len = last - start; |
| 1386 | value->data = start; |
| 1387 | |
| 1388 | return i; |
| 1389 | |
| 1390 | skip: |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1391 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1392 | while (start < end) { |
| 1393 | ch = *start++; |
| 1394 | if (ch == ';' || ch == ',') { |
| 1395 | break; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | while (start < end && *start == ' ') { start++; } |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | return NGX_DECLINED; |
| 1404 | } |
Igor Sysoev | 84d17bb | 2008-12-22 12:02:05 +0000 | [diff] [blame] | 1405 | |
| 1406 | |
| 1407 | ngx_int_t |
| 1408 | ngx_http_arg(ngx_http_request_t *r, u_char *name, size_t len, ngx_str_t *value) |
| 1409 | { |
Igor Sysoev | 1dcaa97 | 2009-04-04 17:51:38 +0000 | [diff] [blame] | 1410 | u_char *p, *last; |
Igor Sysoev | 84d17bb | 2008-12-22 12:02:05 +0000 | [diff] [blame] | 1411 | |
| 1412 | if (r->args.len == 0) { |
| 1413 | return NGX_DECLINED; |
| 1414 | } |
| 1415 | |
Igor Sysoev | 1dcaa97 | 2009-04-04 17:51:38 +0000 | [diff] [blame] | 1416 | p = r->args.data; |
| 1417 | last = p + r->args.len; |
Igor Sysoev | 84d17bb | 2008-12-22 12:02:05 +0000 | [diff] [blame] | 1418 | |
Igor Sysoev | 1dcaa97 | 2009-04-04 17:51:38 +0000 | [diff] [blame] | 1419 | for ( /* void */ ; p < last; p++) { |
Igor Sysoev | 84d17bb | 2008-12-22 12:02:05 +0000 | [diff] [blame] | 1420 | |
Igor Sysoev | 1dcaa97 | 2009-04-04 17:51:38 +0000 | [diff] [blame] | 1421 | /* we need '=' after name, so drop one char from last */ |
| 1422 | |
| 1423 | p = ngx_strlcasestrn(p, last - 1, name, len - 1); |
Igor Sysoev | 84d17bb | 2008-12-22 12:02:05 +0000 | [diff] [blame] | 1424 | |
| 1425 | if (p == NULL) { |
| 1426 | return NGX_DECLINED; |
| 1427 | } |
| 1428 | |
| 1429 | if ((p == r->args.data || *(p - 1) == '&') && *(p + len) == '=') { |
| 1430 | |
| 1431 | value->data = p + len + 1; |
| 1432 | |
Igor Sysoev | 1dcaa97 | 2009-04-04 17:51:38 +0000 | [diff] [blame] | 1433 | p = ngx_strlchr(p, last, '&'); |
Igor Sysoev | 84d17bb | 2008-12-22 12:02:05 +0000 | [diff] [blame] | 1434 | |
| 1435 | if (p == NULL) { |
| 1436 | p = r->args.data + r->args.len; |
| 1437 | } |
| 1438 | |
| 1439 | value->len = p - value->data; |
| 1440 | |
| 1441 | return NGX_OK; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | return NGX_DECLINED; |
| 1446 | } |
Igor Sysoev | 2c7cb55 | 2009-03-19 13:41:29 +0000 | [diff] [blame] | 1447 | |
| 1448 | |
| 1449 | void |
| 1450 | ngx_http_split_args(ngx_http_request_t *r, ngx_str_t *uri, ngx_str_t *args) |
| 1451 | { |
| 1452 | u_char ch, *p, *last; |
| 1453 | |
| 1454 | p = uri->data; |
| 1455 | |
| 1456 | last = p + uri->len; |
| 1457 | |
Igor Sysoev | f072a02 | 2009-03-30 14:15:47 +0000 | [diff] [blame] | 1458 | args->len = 0; |
| 1459 | |
Igor Sysoev | 2c7cb55 | 2009-03-19 13:41:29 +0000 | [diff] [blame] | 1460 | while (p < last) { |
| 1461 | |
Igor Sysoev | 9bc41a4 | 2009-03-22 15:50:07 +0000 | [diff] [blame] | 1462 | ch = *p++; |
Igor Sysoev | 2c7cb55 | 2009-03-19 13:41:29 +0000 | [diff] [blame] | 1463 | |
Igor Sysoev | 9bc41a4 | 2009-03-22 15:50:07 +0000 | [diff] [blame] | 1464 | if (ch == '?') { |
| 1465 | args->len = last - p; |
| 1466 | args->data = p; |
Igor Sysoev | 2c7cb55 | 2009-03-19 13:41:29 +0000 | [diff] [blame] | 1467 | |
Igor Sysoev | 9bc41a4 | 2009-03-22 15:50:07 +0000 | [diff] [blame] | 1468 | uri->len = p - 1 - uri->data; |
Igor Sysoev | 2c7cb55 | 2009-03-19 13:41:29 +0000 | [diff] [blame] | 1469 | |
Igor Sysoev | 9bc41a4 | 2009-03-22 15:50:07 +0000 | [diff] [blame] | 1470 | if (ngx_strlchr(p, last, '\0') != NULL) { |
| 1471 | r->zero_in_uri = 1; |
| 1472 | } |
Igor Sysoev | 2c7cb55 | 2009-03-19 13:41:29 +0000 | [diff] [blame] | 1473 | |
Igor Sysoev | 9bc41a4 | 2009-03-22 15:50:07 +0000 | [diff] [blame] | 1474 | return; |
| 1475 | } |
Igor Sysoev | 2c7cb55 | 2009-03-19 13:41:29 +0000 | [diff] [blame] | 1476 | |
Igor Sysoev | 9bc41a4 | 2009-03-22 15:50:07 +0000 | [diff] [blame] | 1477 | if (ch == '\0') { |
| 1478 | r->zero_in_uri = 1; |
| 1479 | continue; |
| 1480 | } |
Igor Sysoev | 2c7cb55 | 2009-03-19 13:41:29 +0000 | [diff] [blame] | 1481 | } |
| 1482 | } |