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 | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 12 | ngx_int_t |
| 13 | 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] | 14 | { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 15 | u_char c, ch, *p, *m; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 16 | enum { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 17 | sw_start = 0, |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 18 | sw_method, |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 19 | sw_space_after_method, |
| 20 | sw_spaces_before_uri, |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 21 | sw_schema, |
| 22 | sw_schema_slash, |
| 23 | sw_schema_slash_slash, |
| 24 | sw_host, |
| 25 | sw_port, |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 26 | sw_after_slash_in_uri, |
| 27 | sw_check_uri, |
| 28 | sw_uri, |
| 29 | sw_http_09, |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 30 | sw_http_H, |
| 31 | sw_http_HT, |
| 32 | sw_http_HTT, |
| 33 | sw_http_HTTP, |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 34 | sw_first_major_digit, |
| 35 | sw_major_digit, |
| 36 | sw_first_minor_digit, |
| 37 | sw_minor_digit, |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 38 | sw_almost_done |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 39 | } state; |
| 40 | |
| 41 | state = r->state; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 42 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 43 | for (p = b->pos; p < b->last; p++) { |
| 44 | ch = *p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 45 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 46 | /* gcc 2.95.2 and msvc 6.0 compile this switch as an jump table */ |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 47 | |
| 48 | switch (state) { |
| 49 | |
| 50 | /* HTTP methods: GET, HEAD, POST */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 51 | case sw_start: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 52 | r->request_start = p; |
Igor Sysoev | 3d062ad | 2003-03-05 06:37:42 +0000 | [diff] [blame] | 53 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 54 | if (ch == CR || ch == LF) { |
| 55 | break; |
| 56 | } |
| 57 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 58 | if (ch < 'A' || ch > 'Z') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 59 | return NGX_HTTP_PARSE_INVALID_METHOD; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 60 | } |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 61 | |
| 62 | state = sw_method; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 63 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 64 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 65 | case sw_method: |
| 66 | if (ch == ' ') { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 67 | r->method_end = p - 1; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 68 | m = r->request_start; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 69 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 70 | if (p - m == 3) { |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 71 | |
Igor Sysoev | 2e6ba93 | 2004-09-09 15:40:48 +0000 | [diff] [blame] | 72 | if (m[0] == 'G' && m[1] == 'E' && m[2] == 'T') { |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 73 | r->method = NGX_HTTP_GET; |
| 74 | } |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 75 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 76 | } else if (p - m == 4) { |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 77 | |
Igor Sysoev | 2e6ba93 | 2004-09-09 15:40:48 +0000 | [diff] [blame] | 78 | if (m[0] == 'P' && m[1] == 'O' |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 79 | && m[2] == 'S' && m[3] == 'T') |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 80 | { |
| 81 | r->method = NGX_HTTP_POST; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 82 | |
Igor Sysoev | 2e6ba93 | 2004-09-09 15:40:48 +0000 | [diff] [blame] | 83 | } else if (m[0] == 'H' && m[1] == 'E' |
| 84 | && m[2] == 'A' && m[3] == 'D') |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 85 | { |
| 86 | r->method = NGX_HTTP_HEAD; |
| 87 | } |
| 88 | } |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 89 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 90 | state = sw_spaces_before_uri; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 91 | break; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 92 | } |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 93 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 94 | if (ch < 'A' || ch > 'Z') { |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 95 | return NGX_HTTP_PARSE_INVALID_METHOD; |
| 96 | } |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 97 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 98 | break; |
| 99 | |
| 100 | /* single space after method */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 101 | case sw_space_after_method: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 102 | switch (ch) { |
| 103 | case ' ': |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 104 | state = sw_spaces_before_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 105 | break; |
| 106 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 107 | return NGX_HTTP_PARSE_INVALID_METHOD; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 108 | } |
| 109 | break; |
| 110 | |
| 111 | /* space* before URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 112 | case sw_spaces_before_uri: |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 113 | |
| 114 | c = (u_char) (ch | 0x20); |
| 115 | if (c >= 'a' && c <= 'f') { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 116 | r->schema_start = p; |
| 117 | state = sw_schema; |
| 118 | break; |
| 119 | } |
| 120 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 121 | switch (ch) { |
| 122 | case '/': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 123 | r->uri_start = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 124 | state = sw_after_slash_in_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 125 | break; |
| 126 | case ' ': |
| 127 | break; |
| 128 | default: |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 129 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 130 | } |
| 131 | break; |
| 132 | |
| 133 | case sw_schema: |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 134 | |
| 135 | c = (u_char) (ch | 0x20); |
| 136 | if (c >= 'a' && c <= 'f') { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 140 | switch (ch) { |
| 141 | case ':': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 142 | r->schema_end = p; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 143 | state = sw_schema_slash; |
| 144 | break; |
| 145 | default: |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 146 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 147 | } |
| 148 | break; |
| 149 | |
| 150 | case sw_schema_slash: |
| 151 | switch (ch) { |
| 152 | case '/': |
| 153 | state = sw_schema_slash_slash; |
| 154 | break; |
| 155 | default: |
| 156 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 157 | } |
| 158 | break; |
| 159 | |
| 160 | case sw_schema_slash_slash: |
| 161 | switch (ch) { |
| 162 | case '/': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 163 | r->host_start = p; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 164 | state = sw_host; |
| 165 | break; |
| 166 | default: |
| 167 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 168 | } |
| 169 | break; |
| 170 | |
| 171 | case sw_host: |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 172 | |
| 173 | c = (u_char) (ch | 0x20); |
| 174 | if (c >= 'a' && c <= 'f') { |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-') |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 179 | { |
| 180 | break; |
| 181 | } |
| 182 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 183 | switch (ch) { |
| 184 | case ':': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 185 | r->host_end = p; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 186 | state = sw_port; |
| 187 | break; |
| 188 | case '/': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 189 | r->host_end = p; |
| 190 | r->uri_start = p; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 191 | state = sw_after_slash_in_uri; |
| 192 | break; |
| 193 | default: |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 194 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 195 | } |
| 196 | break; |
| 197 | |
| 198 | case sw_port: |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 199 | if (ch >= '0' && ch <= '9') { |
| 200 | break; |
| 201 | } |
| 202 | |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 203 | switch (ch) { |
| 204 | case '/': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 205 | r->port_end = p; |
| 206 | r->uri_start = p; |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 207 | state = sw_after_slash_in_uri; |
| 208 | break; |
| 209 | default: |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 210 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 211 | } |
| 212 | break; |
| 213 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 214 | /* check "/.", "//", "%", and "\" (Win32) in URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 215 | case sw_after_slash_in_uri: |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 216 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 217 | c = (u_char) (ch | 0x20); |
| 218 | if (c >= 'a' && c <= 'f') { |
| 219 | state = sw_check_uri; |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 224 | state = sw_check_uri; |
| 225 | break; |
| 226 | } |
| 227 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 228 | switch (ch) { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 229 | case ' ': |
| 230 | r->uri_end = p; |
| 231 | state = sw_http_09; |
| 232 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 233 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 234 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 235 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 236 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 237 | break; |
| 238 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 239 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 240 | r->http_minor = 9; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 241 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 242 | case '.': |
| 243 | r->complex_uri = 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 244 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 245 | break; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 246 | case '%': |
| 247 | r->quoted_uri = 1; |
| 248 | state = sw_uri; |
| 249 | break; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 250 | case '/': |
| 251 | r->complex_uri = 1; |
| 252 | state = sw_uri; |
| 253 | break; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 254 | #if (NGX_WIN32) |
| 255 | case '\\': |
| 256 | r->complex_uri = 1; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 257 | state = sw_uri; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 258 | break; |
| 259 | #endif |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 260 | case '?': |
| 261 | r->args_start = p + 1; |
| 262 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 263 | break; |
Igor Sysoev | 805d9db | 2005-02-03 19:33:37 +0000 | [diff] [blame] | 264 | case '+': |
| 265 | r->plus_in_uri = 1; |
| 266 | break; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 267 | case '\0': |
| 268 | r->zero_in_uri = 1; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 269 | break; |
| 270 | default: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 271 | state = sw_check_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | break; |
| 275 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 276 | /* check "/", "%" and "\" (Win32) in URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 277 | case sw_check_uri: |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 278 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 279 | c = (u_char) (ch | 0x20); |
| 280 | if (c >= 'a' && c <= 'f') { |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 288 | switch (ch) { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 289 | case '/': |
| 290 | r->uri_ext = NULL; |
| 291 | state = sw_after_slash_in_uri; |
| 292 | break; |
| 293 | case '.': |
| 294 | r->uri_ext = p + 1; |
| 295 | break; |
| 296 | case ' ': |
| 297 | r->uri_end = p; |
| 298 | state = sw_http_09; |
| 299 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 300 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 301 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 302 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 303 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 304 | break; |
| 305 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 306 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 307 | r->http_minor = 9; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 308 | goto done; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 309 | #if (NGX_WIN32) |
| 310 | case '\\': |
| 311 | r->complex_uri = 1; |
| 312 | state = sw_after_slash_in_uri; |
| 313 | break; |
| 314 | #endif |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 315 | case '%': |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 316 | r->quoted_uri = 1; |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 317 | state = sw_uri; |
| 318 | break; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 319 | case '+': |
| 320 | r->plus_in_uri = 1; |
| 321 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 322 | case '?': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 323 | r->args_start = p + 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 324 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 325 | break; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 326 | case '\0': |
| 327 | r->zero_in_uri = 1; |
| 328 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 329 | } |
| 330 | break; |
| 331 | |
| 332 | /* URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 333 | case sw_uri: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 334 | switch (ch) { |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 335 | case ' ': |
| 336 | r->uri_end = p; |
| 337 | state = sw_http_09; |
| 338 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 339 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 340 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 341 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 342 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 343 | break; |
| 344 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 345 | r->uri_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 346 | r->http_minor = 9; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 347 | goto done; |
Igor Sysoev | 805d9db | 2005-02-03 19:33:37 +0000 | [diff] [blame] | 348 | case '+': |
| 349 | r->plus_in_uri = 1; |
| 350 | break; |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 351 | case '\0': |
| 352 | r->zero_in_uri = 1; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | break; |
| 356 | |
| 357 | /* space+ after URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 358 | case sw_http_09: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 359 | switch (ch) { |
| 360 | case ' ': |
| 361 | break; |
| 362 | case CR: |
| 363 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 364 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 365 | break; |
| 366 | case LF: |
| 367 | r->http_minor = 9; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 368 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 369 | case 'H': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 370 | r->http_protocol.data = p; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 371 | state = sw_http_H; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 372 | break; |
| 373 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 374 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 375 | } |
| 376 | break; |
| 377 | |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 378 | case sw_http_H: |
| 379 | switch (ch) { |
| 380 | case 'T': |
| 381 | state = sw_http_HT; |
| 382 | break; |
| 383 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 384 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 385 | } |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 386 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 387 | |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 388 | case sw_http_HT: |
| 389 | switch (ch) { |
| 390 | case 'T': |
| 391 | state = sw_http_HTT; |
| 392 | break; |
| 393 | default: |
| 394 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 395 | } |
| 396 | break; |
| 397 | |
| 398 | case sw_http_HTT: |
| 399 | switch (ch) { |
| 400 | case 'P': |
| 401 | state = sw_http_HTTP; |
| 402 | break; |
| 403 | default: |
| 404 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 405 | } |
| 406 | break; |
| 407 | |
| 408 | case sw_http_HTTP: |
| 409 | switch (ch) { |
| 410 | case '/': |
| 411 | state = sw_first_major_digit; |
| 412 | break; |
| 413 | default: |
| 414 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 415 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 416 | break; |
| 417 | |
| 418 | /* first digit of major HTTP version */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 419 | case sw_first_major_digit: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 420 | if (ch < '1' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 421 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 422 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 423 | |
| 424 | r->http_major = ch - '0'; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 425 | state = sw_major_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 426 | break; |
| 427 | |
| 428 | /* major HTTP version or dot */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 429 | case sw_major_digit: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 430 | if (ch == '.') { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 431 | state = sw_first_minor_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 435 | if (ch < '0' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 436 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 437 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 438 | |
| 439 | r->http_major = r->http_major * 10 + ch - '0'; |
| 440 | break; |
| 441 | |
| 442 | /* first digit of minor HTTP version */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 443 | case sw_first_minor_digit: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 444 | if (ch < '0' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 445 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 446 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 447 | |
| 448 | r->http_minor = ch - '0'; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 449 | state = sw_minor_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 450 | break; |
| 451 | |
| 452 | /* minor HTTP version or end of request line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 453 | case sw_minor_digit: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 454 | if (ch == CR) { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 455 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 456 | break; |
| 457 | } |
| 458 | |
| 459 | if (ch == LF) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 460 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 463 | if (ch < '0' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 464 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 465 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 466 | |
| 467 | r->http_minor = r->http_minor * 10 + ch - '0'; |
| 468 | break; |
| 469 | |
| 470 | /* end of request line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 471 | case sw_almost_done: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 472 | r->request_end = p - 1; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 473 | switch (ch) { |
| 474 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 475 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 476 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 477 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 478 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 482 | b->pos = p; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 483 | r->state = state; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 484 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 485 | return NGX_AGAIN; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 486 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 487 | done: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 488 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 489 | b->pos = p + 1; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 490 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 491 | if (r->request_end == NULL) { |
| 492 | r->request_end = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 493 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 494 | |
| 495 | r->http_version = r->http_major * 1000 + r->http_minor; |
| 496 | r->state = sw_start; |
| 497 | |
| 498 | if (r->http_version == 9 && r->method != NGX_HTTP_GET) { |
| 499 | return NGX_HTTP_PARSE_INVALID_09_METHOD; |
| 500 | } |
| 501 | |
| 502 | return NGX_OK; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 505 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 506 | ngx_int_t |
| 507 | ngx_http_parse_header_line(ngx_http_request_t *r, ngx_buf_t *b) |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 508 | { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 509 | u_char c, ch, *p; |
| 510 | ngx_uint_t hash; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 511 | enum { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 512 | sw_start = 0, |
| 513 | sw_name, |
| 514 | sw_space_before_value, |
| 515 | sw_value, |
| 516 | sw_space_after_value, |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 517 | sw_ignore_line, |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 518 | sw_almost_done, |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 519 | sw_header_almost_done |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 520 | } state; |
| 521 | |
| 522 | state = r->state; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 523 | hash = r->header_hash; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 524 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 525 | for (p = b->pos; p < b->last; p++) { |
| 526 | ch = *p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 527 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 528 | switch (state) { |
| 529 | |
| 530 | /* first char */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 531 | case sw_start: |
Igor Sysoev | 3362b8d | 2005-05-14 18:42:03 +0000 | [diff] [blame^] | 532 | r->invalid_header = 0; |
| 533 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 534 | switch (ch) { |
| 535 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 536 | r->header_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 537 | state = sw_header_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 538 | break; |
| 539 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 540 | r->header_end = p; |
| 541 | goto header_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 542 | default: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 543 | state = sw_name; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 544 | r->header_name_start = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 545 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 546 | c = (u_char) (ch | 0x20); |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 547 | if (c >= 'a' && c <= 'z') { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 548 | hash = c; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 549 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 550 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 551 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 552 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 553 | hash = ch; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 554 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 555 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 556 | |
Igor Sysoev | 3362b8d | 2005-05-14 18:42:03 +0000 | [diff] [blame^] | 557 | r->invalid_header = 1; |
| 558 | |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 559 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 560 | |
| 561 | } |
| 562 | break; |
| 563 | |
| 564 | /* header name */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 565 | case sw_name: |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 566 | c = (u_char) (ch | 0x20); |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 567 | if (c >= 'a' && c <= 'z') { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 568 | hash += c; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 569 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 570 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 571 | |
| 572 | if (ch == ':') { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 573 | r->header_name_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 574 | state = sw_space_before_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 575 | break; |
| 576 | } |
| 577 | |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 578 | if (ch == '-') { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 579 | hash += ch; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 580 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 581 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 582 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 583 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 584 | hash += ch; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 585 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 586 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 587 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 588 | if (ch == CR) { |
| 589 | r->header_name_end = p; |
| 590 | r->header_start = p; |
| 591 | r->header_end = p; |
| 592 | state = sw_almost_done; |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | if (ch == LF) { |
| 597 | r->header_name_end = p; |
| 598 | r->header_start = p; |
| 599 | r->header_end = p; |
| 600 | goto done; |
| 601 | } |
| 602 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 603 | /* IIS may send the duplicate "HTTP/1.1 ..." lines */ |
Igor Sysoev | 183f9a6 | 2003-04-09 15:42:08 +0000 | [diff] [blame] | 604 | if (ch == '/' |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 605 | && r->upstream |
| 606 | && p - r->header_name_start == 4 |
| 607 | && ngx_strncmp(r->header_name_start, "HTTP", 4) == 0) |
Igor Sysoev | 183f9a6 | 2003-04-09 15:42:08 +0000 | [diff] [blame] | 608 | { |
| 609 | state = sw_ignore_line; |
| 610 | break; |
Igor Sysoev | e2a3154 | 2003-04-08 15:40:10 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Igor Sysoev | 3362b8d | 2005-05-14 18:42:03 +0000 | [diff] [blame^] | 613 | r->invalid_header = 1; |
| 614 | |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 615 | break; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 616 | |
| 617 | /* space* before header value */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 618 | case sw_space_before_value: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 619 | switch (ch) { |
| 620 | case ' ': |
| 621 | break; |
| 622 | case CR: |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 623 | r->header_start = p; |
| 624 | r->header_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 625 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 626 | break; |
| 627 | case LF: |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 628 | r->header_start = p; |
| 629 | r->header_end = p; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 630 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 631 | default: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 632 | r->header_start = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 633 | state = sw_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | break; |
| 637 | |
| 638 | /* header value */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 639 | case sw_value: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 640 | switch (ch) { |
| 641 | case ' ': |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 642 | r->header_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 643 | state = sw_space_after_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 644 | break; |
| 645 | case CR: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 646 | r->header_end = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 647 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 648 | break; |
| 649 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 650 | r->header_end = p; |
| 651 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 652 | } |
| 653 | break; |
| 654 | |
| 655 | /* space* before end of header line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 656 | case sw_space_after_value: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 657 | switch (ch) { |
| 658 | case ' ': |
| 659 | break; |
| 660 | case CR: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 661 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 662 | break; |
| 663 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 664 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 665 | default: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 666 | state = sw_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 667 | break; |
| 668 | } |
| 669 | break; |
| 670 | |
Igor Sysoev | e2a3154 | 2003-04-08 15:40:10 +0000 | [diff] [blame] | 671 | /* ignore header line */ |
| 672 | case sw_ignore_line: |
| 673 | switch (ch) { |
| 674 | case LF: |
| 675 | state = sw_start; |
| 676 | break; |
| 677 | default: |
| 678 | break; |
| 679 | } |
| 680 | break; |
| 681 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 682 | /* end of header line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 683 | case sw_almost_done: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 684 | switch (ch) { |
| 685 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 686 | goto done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 687 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 688 | return NGX_HTTP_PARSE_INVALID_HEADER; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 689 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 690 | |
| 691 | /* end of header */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 692 | case sw_header_almost_done: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 693 | switch (ch) { |
| 694 | case LF: |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 695 | goto header_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 696 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 697 | return NGX_HTTP_PARSE_INVALID_HEADER; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 698 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 702 | b->pos = p; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 703 | r->state = state; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 704 | r->header_hash = hash; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 705 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 706 | return NGX_AGAIN; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 707 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 708 | done: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 709 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 710 | b->pos = p + 1; |
| 711 | r->state = sw_start; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 712 | r->header_hash = hash; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 713 | |
| 714 | return NGX_OK; |
| 715 | |
| 716 | header_done: |
| 717 | |
| 718 | b->pos = p + 1; |
| 719 | r->state = sw_start; |
| 720 | |
| 721 | return NGX_HTTP_PARSE_HEADER_DONE; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 722 | } |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 723 | |
| 724 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 725 | ngx_int_t |
| 726 | ngx_http_parse_complex_uri(ngx_http_request_t *r) |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 727 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 728 | u_char c, ch, decoded, *p, *u; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 729 | enum { |
| 730 | sw_usual = 0, |
| 731 | sw_slash, |
| 732 | sw_dot, |
| 733 | sw_dot_dot, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 734 | #if (NGX_WIN32) |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 735 | sw_dot_dot_dot, |
| 736 | #endif |
| 737 | sw_quoted, |
| 738 | sw_quoted_second |
| 739 | } state, quoted_state; |
| 740 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 741 | #if (NGX_SUPPRESS_WARN) |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 742 | decoded = '\0'; |
| 743 | quoted_state = sw_usual; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 744 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 745 | |
| 746 | state = sw_usual; |
| 747 | p = r->uri_start; |
| 748 | u = r->uri.data; |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 749 | r->uri_ext = NULL; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 750 | r->args_start = NULL; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 751 | |
| 752 | ch = *p++; |
| 753 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 754 | while (p <= r->uri_end) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 755 | |
| 756 | /* |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 757 | * we use "ch = *p++" inside the cycle, but this operation is safe, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 758 | * because after the URI there is always at least one charcter: |
| 759 | * the line feed |
| 760 | */ |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 761 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 762 | ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 763 | "s:%d in:'%Xd:%c', out:'%c'", state, ch, ch, *u); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 764 | |
| 765 | switch (state) { |
| 766 | case sw_usual: |
| 767 | switch(ch) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 768 | #if (NGX_WIN32) |
| 769 | case '\\': |
| 770 | r->uri_ext = NULL; |
| 771 | |
| 772 | if (p == r->uri_start + r->uri.len) { |
| 773 | |
| 774 | /* |
| 775 | * we omit the last "\" to cause redirect because |
| 776 | * the browsers do not treat "\" as "/" in relative URL path |
| 777 | */ |
| 778 | |
| 779 | break; |
| 780 | } |
| 781 | |
| 782 | state = sw_slash; |
| 783 | *u++ = '/'; |
| 784 | break; |
| 785 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 786 | case '/': |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 787 | r->uri_ext = NULL; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 788 | state = sw_slash; |
| 789 | *u++ = ch; |
| 790 | break; |
| 791 | case '%': |
| 792 | quoted_state = state; |
| 793 | state = sw_quoted; |
| 794 | break; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 795 | case '?': |
| 796 | r->args_start = p; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 797 | goto done; |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 798 | case '.': |
| 799 | r->uri_ext = u + 1; |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 800 | *u++ = ch; |
| 801 | break; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 802 | default: |
| 803 | *u++ = ch; |
| 804 | break; |
| 805 | } |
| 806 | ch = *p++; |
| 807 | break; |
| 808 | |
| 809 | case sw_slash: |
| 810 | switch(ch) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 811 | #if (NGX_WIN32) |
| 812 | case '\\': |
| 813 | break; |
| 814 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 815 | case '/': |
| 816 | break; |
| 817 | case '.': |
| 818 | state = sw_dot; |
| 819 | *u++ = ch; |
| 820 | break; |
| 821 | case '%': |
| 822 | quoted_state = state; |
| 823 | state = sw_quoted; |
| 824 | break; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 825 | case '?': |
| 826 | r->args_start = p; |
| 827 | goto done; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 828 | default: |
| 829 | state = sw_usual; |
| 830 | *u++ = ch; |
| 831 | break; |
| 832 | } |
| 833 | ch = *p++; |
| 834 | break; |
| 835 | |
| 836 | case sw_dot: |
| 837 | switch(ch) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 838 | #if (NGX_WIN32) |
| 839 | case '\\': |
| 840 | /* fall through */ |
| 841 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 842 | case '/': |
| 843 | state = sw_slash; |
| 844 | u--; |
| 845 | break; |
| 846 | case '.': |
| 847 | state = sw_dot_dot; |
| 848 | *u++ = ch; |
| 849 | break; |
| 850 | case '%': |
| 851 | quoted_state = state; |
| 852 | state = sw_quoted; |
| 853 | break; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 854 | case '?': |
| 855 | r->args_start = p; |
| 856 | goto done; |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 857 | default: |
| 858 | state = sw_usual; |
| 859 | *u++ = ch; |
| 860 | break; |
| 861 | } |
| 862 | ch = *p++; |
| 863 | break; |
| 864 | |
| 865 | case sw_dot_dot: |
| 866 | switch(ch) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 867 | #if (NGX_WIN32) |
| 868 | case '\\': |
| 869 | /* fall through */ |
| 870 | #endif |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 871 | case '/': |
| 872 | state = sw_slash; |
| 873 | u -= 4; |
| 874 | if (u < r->uri.data) { |
| 875 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 876 | } |
| 877 | while (*(u - 1) != '/') { |
| 878 | u--; |
| 879 | } |
| 880 | break; |
| 881 | case '%': |
| 882 | quoted_state = state; |
| 883 | state = sw_quoted; |
| 884 | break; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 885 | case '?': |
| 886 | r->args_start = p; |
| 887 | goto done; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 888 | #if (NGX_WIN32) |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 889 | case '.': |
| 890 | state = sw_dot_dot_dot; |
| 891 | *u++ = ch; |
| 892 | break; |
| 893 | #endif |
| 894 | default: |
| 895 | state = sw_usual; |
| 896 | *u++ = ch; |
| 897 | break; |
| 898 | } |
| 899 | ch = *p++; |
| 900 | break; |
| 901 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 902 | #if (NGX_WIN32) |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 903 | case sw_dot_dot_dot: |
| 904 | switch(ch) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 905 | case '\\': |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 906 | case '/': |
| 907 | state = sw_slash; |
| 908 | u -= 5; |
| 909 | if (u < r->uri.data) { |
| 910 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 911 | } |
| 912 | while (*u != '/') { |
| 913 | u--; |
| 914 | } |
| 915 | if (u < r->uri.data) { |
| 916 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 917 | } |
| 918 | while (*(u - 1) != '/') { |
| 919 | u--; |
| 920 | } |
| 921 | break; |
| 922 | case '%': |
| 923 | quoted_state = state; |
| 924 | state = sw_quoted; |
| 925 | break; |
| 926 | default: |
| 927 | state = sw_usual; |
| 928 | *u++ = ch; |
| 929 | break; |
| 930 | } |
| 931 | ch = *p++; |
| 932 | break; |
| 933 | #endif |
| 934 | |
| 935 | case sw_quoted: |
| 936 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 937 | decoded = (u_char) (ch - '0'); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 938 | state = sw_quoted_second; |
| 939 | ch = *p++; |
| 940 | break; |
| 941 | } |
| 942 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 943 | c = (u_char) (ch | 0x20); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 944 | if (c >= 'a' && c <= 'f') { |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 945 | decoded = (u_char) (c - 'a' + 10); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 946 | state = sw_quoted_second; |
| 947 | ch = *p++; |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 952 | |
| 953 | case sw_quoted_second: |
| 954 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 955 | ch = (u_char) ((decoded << 4) + ch - '0'); |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 956 | |
Igor Sysoev | 7adb8c0 | 2003-12-02 16:57:05 +0000 | [diff] [blame] | 957 | if (ch == '%') { |
| 958 | state = sw_usual; |
| 959 | *u++ = ch; |
| 960 | ch = *p++; |
| 961 | break; |
| 962 | } |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 963 | |
| 964 | if (ch == '\0') { |
| 965 | r->zero_in_uri = 1; |
| 966 | *u++ = ch; |
| 967 | ch = *p++; |
| 968 | } |
| 969 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 970 | state = quoted_state; |
| 971 | break; |
| 972 | } |
| 973 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 974 | c = (u_char) (ch | 0x20); |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 975 | if (c >= 'a' && c <= 'f') { |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 976 | ch = (u_char) ((decoded << 4) + c - 'a' + 10); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 977 | if (ch == '?') { |
Igor Sysoev | 7adb8c0 | 2003-12-02 16:57:05 +0000 | [diff] [blame] | 978 | *u++ = ch; |
| 979 | ch = *p++; |
Igor Sysoev | 7adb8c0 | 2003-12-02 16:57:05 +0000 | [diff] [blame] | 980 | } |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 981 | state = quoted_state; |
| 982 | break; |
| 983 | } |
| 984 | |
| 985 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
| 986 | } |
| 987 | } |
| 988 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 989 | done: |
| 990 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 991 | r->uri.len = u - r->uri.data; |
| 992 | r->uri.data[r->uri.len] = '\0'; |
| 993 | |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 994 | if (r->uri_ext) { |
| 995 | r->exten.len = u - r->uri_ext; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 996 | r->exten.data = r->uri_ext; |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | r->uri_ext = NULL; |
| 1000 | |
Igor Sysoev | c2bba09 | 2003-11-28 17:41:47 +0000 | [diff] [blame] | 1001 | return NGX_OK; |
| 1002 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1003 | |
| 1004 | |
| 1005 | ngx_int_t |
| 1006 | ngx_http_parse_multi_header_lines(ngx_array_t *headers, ngx_str_t *name, |
| 1007 | ngx_str_t *value) |
| 1008 | { |
| 1009 | ngx_uint_t i; |
| 1010 | u_char *start, *last, *end, ch; |
| 1011 | ngx_table_elt_t **h; |
| 1012 | |
| 1013 | h = headers->elts; |
| 1014 | |
| 1015 | for (i = 0; i < headers->nelts; i++) { |
| 1016 | |
| 1017 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, headers->pool->log, 0, |
| 1018 | "parse header: \"%V: %V\"", &h[i]->key, &h[i]->value); |
| 1019 | |
| 1020 | if (name->len > h[i]->value.len) { |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | start = h[i]->value.data; |
| 1025 | end = h[i]->value.data + h[i]->value.len; |
| 1026 | |
| 1027 | while (start < end) { |
| 1028 | |
| 1029 | if (ngx_strncasecmp(start, name->data, name->len) != 0) { |
| 1030 | goto skip; |
| 1031 | } |
| 1032 | |
| 1033 | for (start += name->len; start < end && *start == ' '; start++) { |
| 1034 | /* void */ |
| 1035 | } |
| 1036 | |
| 1037 | if (value == NULL) { |
| 1038 | if (start == end || *start == ',') { |
| 1039 | return i; |
| 1040 | } |
| 1041 | |
| 1042 | goto skip; |
| 1043 | } |
| 1044 | |
| 1045 | if (start == end || *start++ != '=') { |
| 1046 | /* the invalid header value */ |
| 1047 | goto skip; |
| 1048 | } |
| 1049 | |
| 1050 | while (start < end && *start == ' ') { start++; } |
| 1051 | |
| 1052 | for (last = start; last < end && *last != ';'; last++) { |
| 1053 | /* void */ |
| 1054 | } |
| 1055 | |
| 1056 | value->len = last - start; |
| 1057 | value->data = start; |
| 1058 | |
| 1059 | return i; |
| 1060 | |
| 1061 | skip: |
| 1062 | while (start < end) { |
| 1063 | ch = *start++; |
| 1064 | if (ch == ';' || ch == ',') { |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | while (start < end && *start == ' ') { start++; } |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | return NGX_DECLINED; |
| 1074 | } |