Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 1 | |
| 2 | #include <ngx_config.h> |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 3 | #include <ngx_core.h> |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 4 | #include <ngx_http.h> |
| 5 | |
| 6 | int ngx_read_http_request_line(ngx_http_request_t *r) |
| 7 | { |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 8 | char ch; |
| 9 | char *p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 10 | enum { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 11 | sw_start = 0, |
| 12 | sw_space_after_method, |
| 13 | sw_spaces_before_uri, |
| 14 | sw_after_slash_in_uri, |
| 15 | sw_check_uri, |
| 16 | sw_uri, |
| 17 | sw_http_09, |
| 18 | sw_http_version, |
| 19 | sw_first_major_digit, |
| 20 | sw_major_digit, |
| 21 | sw_first_minor_digit, |
| 22 | sw_minor_digit, |
| 23 | sw_almost_done, |
| 24 | sw_done |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 25 | } state; |
| 26 | |
| 27 | state = r->state; |
| 28 | p = r->header_in->pos.mem; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 29 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 30 | while (p < r->header_in->last.mem && state < sw_done) { |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 31 | ch = *p++; |
| 32 | |
| 33 | /* |
| 34 | printf("\nstate: %d, pos: %x, end: %x, char: '%c' buf: %s", |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 35 | state, p, r->header_in->last, ch, p); |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 38 | /* GCC 2.95.2 and VC 6.0 compile this switch as jump table */ |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 39 | |
| 40 | switch (state) { |
| 41 | |
| 42 | /* HTTP methods: GET, HEAD, POST */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 43 | case sw_start: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 44 | switch (ch) { |
| 45 | case 'G': |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 46 | if (p + 1 >= r->header_in->last.mem) { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 47 | return NGX_AGAIN; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 48 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 49 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 50 | if (*p != 'E' || *(p + 1) != 'T') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 51 | return NGX_HTTP_PARSE_INVALID_METHOD; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 52 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 53 | |
| 54 | r->method = NGX_HTTP_GET; |
| 55 | p += 2; |
| 56 | break; |
| 57 | |
| 58 | case 'H': |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 59 | if (p + 2 >= r->header_in->last.mem) { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 60 | return NGX_AGAIN; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 61 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 62 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 63 | if (*p != 'E' || *(p + 1) != 'A' || *(p + 2) != 'D') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 64 | return NGX_HTTP_PARSE_INVALID_METHOD; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 65 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 66 | |
| 67 | r->method = NGX_HTTP_HEAD; |
| 68 | p += 3; |
| 69 | break; |
| 70 | |
| 71 | case 'P': |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 72 | if (p + 2 >= r->header_in->last.mem) { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 73 | return NGX_AGAIN; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 74 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 75 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 76 | if (*p != 'O' || *(p + 1) != 'S' || *(p + 2) != 'T') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 77 | return NGX_HTTP_PARSE_INVALID_METHOD; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 78 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 79 | |
| 80 | r->method = NGX_HTTP_POST; |
| 81 | p += 3; |
| 82 | break; |
| 83 | |
| 84 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 85 | return NGX_HTTP_PARSE_INVALID_METHOD; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 88 | state = sw_space_after_method; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 89 | break; |
| 90 | |
| 91 | /* single space after method */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 92 | case sw_space_after_method: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 93 | switch (ch) { |
| 94 | case ' ': |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 95 | state = sw_spaces_before_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 96 | break; |
| 97 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 98 | return NGX_HTTP_PARSE_INVALID_METHOD; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 99 | } |
| 100 | break; |
| 101 | |
| 102 | /* space* before URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 103 | case sw_spaces_before_uri: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 104 | switch (ch) { |
| 105 | case '/': |
| 106 | r->uri_start = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 107 | state = sw_after_slash_in_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 108 | break; |
| 109 | case ' ': |
| 110 | break; |
| 111 | default: |
| 112 | r->unusual_uri = 1; |
| 113 | r->uri_start = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 114 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | break; |
| 118 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 119 | /* check "/." or "//" */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 120 | case sw_after_slash_in_uri: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 121 | switch (ch) { |
| 122 | case CR: |
| 123 | r->uri_end = p - 1; |
| 124 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 125 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 126 | break; |
| 127 | case LF: |
| 128 | r->uri_end = p - 1; |
| 129 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 130 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 131 | break; |
| 132 | case ' ': |
| 133 | r->uri_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 134 | state = sw_http_09; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 135 | break; |
| 136 | case '.': |
| 137 | r->complex_uri = 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 138 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 139 | break; |
| 140 | case '/': |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 141 | #if (WIN32) |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 142 | r->complex_uri = 1; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 143 | #endif |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 144 | break; |
| 145 | case '?': |
| 146 | r->args_start = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 147 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 148 | break; |
| 149 | default: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 150 | state = sw_check_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | break; |
| 154 | |
| 155 | /* check slash in URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 156 | case sw_check_uri: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 157 | switch (ch) { |
| 158 | case CR: |
| 159 | r->uri_end = p - 1; |
| 160 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 161 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 162 | break; |
| 163 | case LF: |
| 164 | r->uri_end = p - 1; |
| 165 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 166 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 167 | break; |
| 168 | case ' ': |
| 169 | r->uri_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 170 | state = sw_http_09; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 171 | break; |
| 172 | case '.': |
| 173 | r->uri_ext = p; |
| 174 | break; |
| 175 | case '/': |
| 176 | r->uri_ext = NULL; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 177 | state = sw_after_slash_in_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 178 | break; |
| 179 | case '?': |
| 180 | r->args_start = p; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 181 | state = sw_uri; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | break; |
| 185 | |
| 186 | /* URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 187 | case sw_uri: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 188 | switch (ch) { |
| 189 | case CR: |
| 190 | r->uri_end = p - 1; |
| 191 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 192 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 193 | break; |
| 194 | case LF: |
| 195 | r->uri_end = p - 1; |
| 196 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 197 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 198 | break; |
| 199 | case ' ': |
| 200 | r->uri_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 201 | state = sw_http_09; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | break; |
| 205 | |
| 206 | /* space+ after URI */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 207 | case sw_http_09: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 208 | switch (ch) { |
| 209 | case ' ': |
| 210 | break; |
| 211 | case CR: |
| 212 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 213 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 214 | break; |
| 215 | case LF: |
| 216 | r->http_minor = 9; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 217 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 218 | break; |
| 219 | case 'H': |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 220 | state = sw_http_version; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 221 | break; |
| 222 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 223 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 224 | } |
| 225 | break; |
| 226 | |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 227 | /* "TTP/" */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 228 | case sw_http_version: |
| 229 | if (p + 2 >= r->header_in->last.mem) { |
| 230 | r->state = sw_http_version; |
| 231 | r->header_in->pos.mem = p - 1; |
| 232 | return NGX_AGAIN; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 235 | if (ch != 'T' || *p != 'T' || *(p + 1) != 'P' || *(p + 2) != '/') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 236 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 237 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 238 | |
| 239 | p += 3; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 240 | state = sw_first_major_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 241 | break; |
| 242 | |
| 243 | /* first digit of major HTTP version */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 244 | case sw_first_major_digit: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 245 | if (ch < '1' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 246 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 247 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 248 | |
| 249 | r->http_major = ch - '0'; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 250 | state = sw_major_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 251 | break; |
| 252 | |
| 253 | /* major HTTP version or dot */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 254 | case sw_major_digit: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 255 | if (ch == '.') { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 256 | state = sw_first_minor_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 260 | if (ch < '0' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 261 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 262 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 263 | |
| 264 | r->http_major = r->http_major * 10 + ch - '0'; |
| 265 | break; |
| 266 | |
| 267 | /* first digit of minor HTTP version */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 268 | case sw_first_minor_digit: |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 269 | if (ch < '0' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 270 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 271 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 272 | |
| 273 | r->http_minor = ch - '0'; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 274 | state = sw_minor_digit; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 275 | break; |
| 276 | |
| 277 | /* minor HTTP version or end of request line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 278 | case sw_minor_digit: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 279 | if (ch == CR) { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 280 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | |
| 284 | if (ch == LF) { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 285 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 286 | break; |
| 287 | } |
| 288 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 289 | if (ch < '0' || ch > '9') { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 290 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 291 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 292 | |
| 293 | r->http_minor = r->http_minor * 10 + ch - '0'; |
| 294 | break; |
| 295 | |
| 296 | /* end of request line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 297 | case sw_almost_done: |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 298 | r->request_end = p - 2; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 299 | switch (ch) { |
| 300 | case LF: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 301 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 302 | break; |
| 303 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 304 | return NGX_HTTP_PARSE_INVALID_REQUEST; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 305 | } |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 310 | r->header_in->pos.mem = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 311 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 312 | if (state == sw_done) { |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 313 | if (r->request_end == NULL) { |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 314 | r->request_end = p - 1; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 317 | r->http_version = r->http_major * 1000 + r->http_minor; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 318 | r->state = sw_start; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 319 | |
| 320 | if (r->http_version == 9 && r->method == NGX_HTTP_HEAD) { |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 321 | return NGX_HTTP_PARSE_INVALID_HEAD; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 322 | } else { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 323 | return NGX_OK; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 326 | } else { |
| 327 | r->state = state; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 328 | return NGX_AGAIN; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 332 | int ngx_read_http_header_line(ngx_http_request_t *r, ngx_hunk_t *h) |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 333 | { |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 334 | char c, ch; |
| 335 | char *p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 336 | enum { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 337 | sw_start = 0, |
| 338 | sw_name, |
| 339 | sw_space_before_value, |
| 340 | sw_value, |
| 341 | sw_space_after_value, |
| 342 | sw_almost_done, |
| 343 | sw_header_almost_done, |
| 344 | sw_done, |
| 345 | sw_header_done |
Igor Sysoev | 481b643 | 2002-12-04 16:29:40 +0000 | [diff] [blame] | 346 | } state; |
| 347 | |
| 348 | state = r->state; |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 349 | p = h->pos.mem; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 350 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 351 | while (p < h->last.mem && state < sw_done) { |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 352 | ch = *p++; |
| 353 | |
| 354 | /* |
| 355 | printf("\nstate: %d, pos: %x, end: %x, char: '%c' buf: %s", |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 356 | state, p, h->last.mem, ch, p); |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 357 | */ |
| 358 | |
| 359 | switch (state) { |
| 360 | |
| 361 | /* first char */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 362 | case sw_start: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 363 | switch (ch) { |
| 364 | case CR: |
| 365 | r->header_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 366 | state = sw_header_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 367 | break; |
| 368 | case LF: |
| 369 | r->header_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 370 | state = sw_header_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 371 | break; |
| 372 | default: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 373 | state = sw_name; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 374 | r->header_name_start = p - 1; |
| 375 | |
| 376 | c = ch | 0x20; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 377 | if (c >= 'a' && c <= 'z') { |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 378 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 379 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 380 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 381 | if (ch == '-') { |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 382 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 383 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 384 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 385 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 386 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 387 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 388 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 389 | return NGX_HTTP_PARSE_INVALID_HEADER; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 390 | |
| 391 | } |
| 392 | break; |
| 393 | |
| 394 | /* header name */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 395 | case sw_name: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 396 | c = ch | 0x20; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 397 | if (c >= 'a' && c <= 'z') { |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 398 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 399 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 400 | |
| 401 | if (ch == ':') { |
| 402 | r->header_name_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 403 | state = sw_space_before_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 404 | break; |
| 405 | } |
| 406 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 407 | if (ch == '-') { |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 408 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 409 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 410 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 411 | if (ch >= '0' && ch <= '9') { |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 412 | break; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 413 | } |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 414 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 415 | return NGX_HTTP_PARSE_INVALID_HEADER; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 416 | |
| 417 | /* space* before header value */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 418 | case sw_space_before_value: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 419 | switch (ch) { |
| 420 | case ' ': |
| 421 | break; |
| 422 | case CR: |
| 423 | r->header_start = r->header_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 424 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 425 | break; |
| 426 | case LF: |
| 427 | r->header_start = r->header_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 428 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 429 | break; |
| 430 | default: |
| 431 | r->header_start = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 432 | state = sw_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | break; |
| 436 | |
| 437 | /* header value */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 438 | case sw_value: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 439 | switch (ch) { |
| 440 | case ' ': |
| 441 | r->header_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 442 | state = sw_space_after_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 443 | break; |
| 444 | case CR: |
| 445 | r->header_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 446 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 447 | break; |
| 448 | case LF: |
| 449 | r->header_end = p - 1; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 450 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | break; |
| 454 | |
| 455 | /* space* before end of header line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 456 | case sw_space_after_value: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 457 | switch (ch) { |
| 458 | case ' ': |
| 459 | break; |
| 460 | case CR: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 461 | state = sw_almost_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 462 | break; |
| 463 | case LF: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 464 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 465 | break; |
| 466 | default: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 467 | state = sw_value; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | break; |
| 471 | |
| 472 | /* end of header line */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 473 | case sw_almost_done: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 474 | switch (ch) { |
| 475 | case LF: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 476 | state = sw_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 477 | break; |
| 478 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 479 | return NGX_HTTP_PARSE_INVALID_HEADER; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 480 | } |
| 481 | break; |
| 482 | |
| 483 | /* end of header */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 484 | case sw_header_almost_done: |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 485 | switch (ch) { |
| 486 | case LF: |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 487 | state = sw_header_done; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 488 | break; |
| 489 | default: |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 490 | return NGX_HTTP_PARSE_INVALID_HEADER; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 491 | } |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 496 | h->pos.mem = p; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 497 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 498 | if (state == sw_done) { |
| 499 | r->state = sw_start; |
| 500 | return NGX_OK; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 501 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 502 | } else if (state == sw_header_done) { |
| 503 | r->state = sw_start; |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 504 | return NGX_HTTP_PARSE_HEADER_DONE; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 505 | |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 506 | } else { |
| 507 | r->state = state; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 508 | return NGX_AGAIN; |
Igor Sysoev | 0c331d9 | 2002-08-15 17:20:26 +0000 | [diff] [blame] | 509 | } |
| 510 | } |