nginx/src/http/ngx_http_parse.c

863 lines
21 KiB
C
Raw Normal View History

2002-08-16 01:20:26 +08:00
#include <ngx_config.h>
2002-08-30 00:59:54 +08:00
#include <ngx_core.h>
2002-08-16 01:20:26 +08:00
#include <ngx_http.h>
2003-12-15 04:10:27 +08:00
ngx_int_t ngx_http_parse_request_line(ngx_http_request_t *r)
2002-08-16 01:20:26 +08:00
{
2004-04-14 13:57:36 +08:00
u_char ch, *p, *m;
2002-08-16 01:20:26 +08:00
enum {
2002-08-30 00:59:54 +08:00
sw_start = 0,
2004-04-14 13:57:36 +08:00
sw_method,
2002-08-30 00:59:54 +08:00
sw_space_after_method,
sw_spaces_before_uri,
2004-04-14 13:57:36 +08:00
sw_schema,
sw_schema_slash,
sw_schema_slash_slash,
sw_host,
sw_port,
2002-08-30 00:59:54 +08:00
sw_after_slash_in_uri,
sw_check_uri,
sw_uri,
sw_http_09,
2003-05-20 00:39:14 +08:00
sw_http_H,
sw_http_HT,
sw_http_HTT,
sw_http_HTTP,
2002-08-30 00:59:54 +08:00
sw_first_major_digit,
sw_major_digit,
sw_first_minor_digit,
sw_minor_digit,
sw_almost_done,
sw_done
2002-12-05 00:29:40 +08:00
} state;
state = r->state;
2003-03-12 04:38:13 +08:00
p = r->header_in->pos;
2002-08-16 01:20:26 +08:00
2003-03-12 04:38:13 +08:00
while (p < r->header_in->last && state < sw_done) {
2002-08-16 01:20:26 +08:00
ch = *p++;
2003-05-20 00:39:14 +08:00
/* gcc 2.95.2 and vc 6.0 compile this switch as an jump table */
2002-08-16 01:20:26 +08:00
switch (state) {
/* HTTP methods: GET, HEAD, POST */
2002-08-30 00:59:54 +08:00
case sw_start:
2003-03-05 14:37:42 +08:00
r->request_start = p - 1;
2004-04-22 02:54:33 +08:00
if (ch == CR || ch == LF) {
break;
}
2004-04-14 13:57:36 +08:00
if (ch < 'A' || ch > 'Z') {
2003-05-20 00:39:14 +08:00
return NGX_HTTP_PARSE_INVALID_METHOD;
}
2002-08-16 01:20:26 +08:00
2004-04-14 13:57:36 +08:00
state = sw_method;
2003-05-20 00:39:14 +08:00
break;
2002-08-16 01:20:26 +08:00
2004-04-14 13:57:36 +08:00
case sw_method:
if (ch == ' ') {
r->method_end = p - 1;
m = r->request_start;
if (r->method_end - m == 3) {
if (*m == 'G' && *(m + 1) == 'E' && *(m + 2) == 'T') {
r->method = NGX_HTTP_GET;
}
} else if (r->method_end - m == 4) {
if (*m == 'P' && *(m + 1) == 'O'
&& *(m + 2) == 'T' && *(m + 3) == 'T')
{
r->method = NGX_HTTP_POST;
} else if (*m == 'H' && *(m + 1) == 'E'
&& *(m + 2) == 'A' && *(m + 3) == 'D')
{
r->method = NGX_HTTP_HEAD;
}
}
state = sw_spaces_before_uri;
2002-08-16 01:20:26 +08:00
break;
2003-05-20 00:39:14 +08:00
}
2002-08-16 01:20:26 +08:00
2004-04-14 13:57:36 +08:00
if (ch < 'A' || ch > 'Z') {
2003-05-20 00:39:14 +08:00
return NGX_HTTP_PARSE_INVALID_METHOD;
}
2004-04-14 13:57:36 +08:00
2003-05-20 00:39:14 +08:00
break;
2002-08-16 01:20:26 +08:00
2004-04-14 13:57:36 +08:00
/* single space after method */
case sw_space_after_method:
2003-05-20 00:39:14 +08:00
switch (ch) {
2004-04-14 13:57:36 +08:00
case ' ':
state = sw_spaces_before_uri;
2003-05-20 00:39:14 +08:00
break;
default:
return NGX_HTTP_PARSE_INVALID_METHOD;
}
break;
2002-08-16 01:20:26 +08:00
2004-04-14 13:57:36 +08:00
/* space* before URI */
case sw_spaces_before_uri:
2003-05-20 00:39:14 +08:00
switch (ch) {
2004-04-14 13:57:36 +08:00
case '/':
r->uri_start = p - 1;
state = sw_after_slash_in_uri;
break;
case ' ':
2002-08-16 01:20:26 +08:00
break;
2003-05-20 00:39:14 +08:00
default:
2004-04-14 13:57:36 +08:00
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
r->schema_start = p - 1;
state = sw_schema;
break;
}
return NGX_HTTP_PARSE_INVALID_REQUEST;
2003-05-20 00:39:14 +08:00
}
break;
2002-08-16 01:20:26 +08:00
2004-04-14 13:57:36 +08:00
case sw_schema:
2003-05-20 00:39:14 +08:00
switch (ch) {
2004-04-14 13:57:36 +08:00
case ':':
r->schema_end = p - 1;
state = sw_schema_slash;
2003-05-20 00:39:14 +08:00
break;
default:
2004-04-14 13:57:36 +08:00
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
break;
}
return NGX_HTTP_PARSE_INVALID_REQUEST;
2003-05-20 00:39:14 +08:00
}
break;
2002-08-16 01:20:26 +08:00
2004-04-14 13:57:36 +08:00
case sw_schema_slash:
2003-05-20 00:39:14 +08:00
switch (ch) {
2004-04-14 13:57:36 +08:00
case '/':
state = sw_schema_slash_slash;
2003-05-20 00:39:14 +08:00
break;
default:
2004-04-14 13:57:36 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2003-05-20 00:39:14 +08:00
}
break;
2002-08-16 01:20:26 +08:00
2004-04-14 13:57:36 +08:00
case sw_schema_slash_slash:
2003-05-20 00:39:14 +08:00
switch (ch) {
2004-04-14 13:57:36 +08:00
case '/':
r->host_start = p - 1;
state = sw_host;
2002-08-16 01:20:26 +08:00
break;
default:
2004-04-14 13:57:36 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2002-08-16 01:20:26 +08:00
}
break;
2004-04-14 13:57:36 +08:00
case sw_host:
2002-08-16 01:20:26 +08:00
switch (ch) {
2004-04-14 13:57:36 +08:00
case ':':
r->host_end = p - 1;
state = sw_port;
break;
case '/':
r->host_end = p - 1;
r->uri_start = p - 1;
state = sw_after_slash_in_uri;
2002-08-16 01:20:26 +08:00
break;
default:
2004-04-14 13:57:36 +08:00
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
|| (ch >= '0' && ch <= '9') || ch == '.' || ch == '-')
{
break;
}
return NGX_HTTP_PARSE_INVALID_REQUEST;
2002-08-16 01:20:26 +08:00
}
break;
2004-04-14 13:57:36 +08:00
case sw_port:
2002-08-16 01:20:26 +08:00
switch (ch) {
case '/':
2004-04-14 13:57:36 +08:00
r->port_end = p - 1;
2002-08-16 01:20:26 +08:00
r->uri_start = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_after_slash_in_uri;
2002-08-16 01:20:26 +08:00
break;
default:
2004-04-14 13:57:36 +08:00
if (ch < '0' && ch > '9') {
return NGX_HTTP_PARSE_INVALID_REQUEST;
}
2002-08-16 01:20:26 +08:00
break;
}
break;
2003-06-02 23:24:30 +08:00
/* check "/.", "//", and "%" in URI */
2002-08-30 00:59:54 +08:00
case sw_after_slash_in_uri:
2002-08-16 01:20:26 +08:00
switch (ch) {
case CR:
r->uri_end = p - 1;
r->http_minor = 9;
2002-08-30 00:59:54 +08:00
state = sw_almost_done;
2002-08-16 01:20:26 +08:00
break;
case LF:
r->uri_end = p - 1;
r->http_minor = 9;
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
case ' ':
r->uri_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_http_09;
2002-08-16 01:20:26 +08:00
break;
case '.':
2003-06-02 23:24:30 +08:00
case '%':
2002-08-16 01:20:26 +08:00
r->complex_uri = 1;
2002-08-30 00:59:54 +08:00
state = sw_uri;
2002-08-16 01:20:26 +08:00
break;
case '/':
r->complex_uri = 1;
break;
case '?':
r->args_start = p;
2002-08-30 00:59:54 +08:00
state = sw_uri;
2002-08-16 01:20:26 +08:00
break;
default:
2002-08-30 00:59:54 +08:00
state = sw_check_uri;
2002-08-16 01:20:26 +08:00
break;
}
break;
2003-06-02 23:24:30 +08:00
/* check "/" and "%" in URI */
2002-08-30 00:59:54 +08:00
case sw_check_uri:
2002-08-16 01:20:26 +08:00
switch (ch) {
case CR:
r->uri_end = p - 1;
r->http_minor = 9;
2002-08-30 00:59:54 +08:00
state = sw_almost_done;
2002-08-16 01:20:26 +08:00
break;
case LF:
r->uri_end = p - 1;
r->http_minor = 9;
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
case ' ':
r->uri_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_http_09;
2002-08-16 01:20:26 +08:00
break;
case '.':
r->uri_ext = p;
break;
case '/':
r->uri_ext = NULL;
2002-08-30 00:59:54 +08:00
state = sw_after_slash_in_uri;
2002-08-16 01:20:26 +08:00
break;
2003-06-02 23:24:30 +08:00
case '%':
r->complex_uri = 1;
state = sw_uri;
break;
2002-08-16 01:20:26 +08:00
case '?':
r->args_start = p;
2002-08-30 00:59:54 +08:00
state = sw_uri;
2002-08-16 01:20:26 +08:00
break;
}
break;
/* URI */
2002-08-30 00:59:54 +08:00
case sw_uri:
2002-08-16 01:20:26 +08:00
switch (ch) {
case CR:
r->uri_end = p - 1;
r->http_minor = 9;
2002-08-30 00:59:54 +08:00
state = sw_almost_done;
2002-08-16 01:20:26 +08:00
break;
case LF:
r->uri_end = p - 1;
r->http_minor = 9;
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
case ' ':
r->uri_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_http_09;
2002-08-16 01:20:26 +08:00
break;
}
break;
/* space+ after URI */
2002-08-30 00:59:54 +08:00
case sw_http_09:
2002-08-16 01:20:26 +08:00
switch (ch) {
case ' ':
break;
case CR:
r->http_minor = 9;
2002-08-30 00:59:54 +08:00
state = sw_almost_done;
2002-08-16 01:20:26 +08:00
break;
case LF:
r->http_minor = 9;
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
case 'H':
2003-05-20 00:39:14 +08:00
state = sw_http_H;
break;
default:
return NGX_HTTP_PARSE_INVALID_REQUEST;
}
break;
case sw_http_H:
switch (ch) {
case 'T':
state = sw_http_HT;
2002-08-16 01:20:26 +08:00
break;
default:
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2002-08-16 01:20:26 +08:00
}
break;
2003-05-20 00:39:14 +08:00
case sw_http_HT:
switch (ch) {
case 'T':
state = sw_http_HTT;
break;
default:
return NGX_HTTP_PARSE_INVALID_REQUEST;
2002-08-16 01:20:26 +08:00
}
2003-05-20 00:39:14 +08:00
break;
2002-08-16 01:20:26 +08:00
2003-05-20 00:39:14 +08:00
case sw_http_HTT:
switch (ch) {
case 'P':
state = sw_http_HTTP;
break;
default:
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2003-01-09 13:36:00 +08:00
}
2003-05-20 00:39:14 +08:00
break;
2002-08-16 01:20:26 +08:00
2003-05-20 00:39:14 +08:00
case sw_http_HTTP:
switch (ch) {
case '/':
state = sw_first_major_digit;
break;
default:
return NGX_HTTP_PARSE_INVALID_REQUEST;
}
2002-08-16 01:20:26 +08:00
break;
/* first digit of major HTTP version */
2002-08-30 00:59:54 +08:00
case sw_first_major_digit:
2003-01-09 13:36:00 +08:00
if (ch < '1' || ch > '9') {
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
r->http_major = ch - '0';
2002-08-30 00:59:54 +08:00
state = sw_major_digit;
2002-08-16 01:20:26 +08:00
break;
/* major HTTP version or dot */
2002-08-30 00:59:54 +08:00
case sw_major_digit:
2002-08-16 01:20:26 +08:00
if (ch == '.') {
2002-08-30 00:59:54 +08:00
state = sw_first_minor_digit;
2002-08-16 01:20:26 +08:00
break;
}
2003-01-09 13:36:00 +08:00
if (ch < '0' || ch > '9') {
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
r->http_major = r->http_major * 10 + ch - '0';
break;
/* first digit of minor HTTP version */
2002-08-30 00:59:54 +08:00
case sw_first_minor_digit:
2003-01-09 13:36:00 +08:00
if (ch < '0' || ch > '9') {
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
r->http_minor = ch - '0';
2002-08-30 00:59:54 +08:00
state = sw_minor_digit;
2002-08-16 01:20:26 +08:00
break;
/* minor HTTP version or end of request line */
2002-08-30 00:59:54 +08:00
case sw_minor_digit:
2002-08-16 01:20:26 +08:00
if (ch == CR) {
2002-08-30 00:59:54 +08:00
state = sw_almost_done;
2002-08-16 01:20:26 +08:00
break;
}
if (ch == LF) {
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
}
2003-01-09 13:36:00 +08:00
if (ch < '0' || ch > '9') {
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
r->http_minor = r->http_minor * 10 + ch - '0';
break;
/* end of request line */
2002-08-30 00:59:54 +08:00
case sw_almost_done:
2002-10-05 01:58:04 +08:00
r->request_end = p - 2;
2002-08-16 01:20:26 +08:00
switch (ch) {
case LF:
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
default:
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2002-08-16 01:20:26 +08:00
}
break;
2003-11-21 14:30:49 +08:00
/* suppress warning */
case sw_done:
break;
2002-08-16 01:20:26 +08:00
}
}
2003-03-12 04:38:13 +08:00
r->header_in->pos = p;
2002-08-16 01:20:26 +08:00
2002-08-30 00:59:54 +08:00
if (state == sw_done) {
2003-01-09 13:36:00 +08:00
if (r->request_end == NULL) {
2002-10-05 01:58:04 +08:00
r->request_end = p - 1;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
r->http_version = r->http_major * 1000 + r->http_minor;
2002-08-30 00:59:54 +08:00
r->state = sw_start;
2003-01-09 13:36:00 +08:00
2003-05-15 23:42:53 +08:00
if (r->http_version == 9 && r->method != NGX_HTTP_GET) {
return NGX_HTTP_PARSE_INVALID_09_METHOD;
2003-01-09 13:36:00 +08:00
}
2003-05-15 23:42:53 +08:00
return NGX_OK;
2002-08-16 01:20:26 +08:00
} else {
r->state = state;
2002-08-30 00:59:54 +08:00
return NGX_AGAIN;
2002-08-16 01:20:26 +08:00
}
}
2003-11-29 01:41:47 +08:00
ngx_int_t ngx_http_parse_header_line(ngx_http_request_t *r, ngx_buf_t *b)
2002-08-16 01:20:26 +08:00
{
2004-03-16 15:10:12 +08:00
u_char c, ch, *p;
2003-11-29 01:41:47 +08:00
enum {
2002-08-30 00:59:54 +08:00
sw_start = 0,
sw_name,
sw_space_before_value,
sw_value,
sw_space_after_value,
sw_almost_done,
sw_header_almost_done,
2003-04-08 23:40:10 +08:00
sw_ignore_line,
2002-08-30 00:59:54 +08:00
sw_done,
sw_header_done
2002-12-05 00:29:40 +08:00
} state;
state = r->state;
p = b->pos;
2002-08-16 01:20:26 +08:00
while (p < b->last && state < sw_done) {
2002-08-16 01:20:26 +08:00
ch = *p++;
switch (state) {
/* first char */
2002-08-30 00:59:54 +08:00
case sw_start:
2002-08-16 01:20:26 +08:00
switch (ch) {
case CR:
r->header_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_header_almost_done;
2002-08-16 01:20:26 +08:00
break;
case LF:
r->header_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_header_done;
2002-08-16 01:20:26 +08:00
break;
default:
2002-08-30 00:59:54 +08:00
state = sw_name;
2002-08-16 01:20:26 +08:00
r->header_name_start = p - 1;
2004-03-16 21:35:20 +08:00
c = (u_char) (ch | 0x20);
2003-01-09 13:36:00 +08:00
if (c >= 'a' && c <= 'z') {
2002-08-16 01:20:26 +08:00
break;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
2004-01-26 16:52:49 +08:00
if (ch == '-' || ch == '_' || ch == '~' || ch == '.') {
2002-08-16 01:20:26 +08:00
break;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
2003-01-09 13:36:00 +08:00
if (ch >= '0' && ch <= '9') {
2002-08-16 01:20:26 +08:00
break;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_HEADER;
2002-08-16 01:20:26 +08:00
}
break;
/* header name */
2002-08-30 00:59:54 +08:00
case sw_name:
2004-03-16 15:10:12 +08:00
c = (u_char) (ch | 0x20);
2003-01-09 13:36:00 +08:00
if (c >= 'a' && c <= 'z') {
2002-08-16 01:20:26 +08:00
break;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
if (ch == ':') {
r->header_name_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_space_before_value;
2002-08-16 01:20:26 +08:00
break;
}
2004-01-26 16:52:49 +08:00
if (ch == '-' || ch == '_' || ch == '~' || ch == '.') {
2002-08-16 01:20:26 +08:00
break;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
2003-01-09 13:36:00 +08:00
if (ch >= '0' && ch <= '9') {
2002-08-16 01:20:26 +08:00
break;
2003-01-09 13:36:00 +08:00
}
2002-08-16 01:20:26 +08:00
2003-04-09 23:42:08 +08:00
/* IIS can send duplicate "HTTP/1.1 ..." lines */
if (ch == '/'
&& r->proxy
&& p - r->header_start == 5
&& ngx_strncmp(r->header_start, "HTTP", 4) == 0)
{
state = sw_ignore_line;
break;
2003-04-08 23:40:10 +08:00
}
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_HEADER;
2002-08-16 01:20:26 +08:00
/* space* before header value */
2002-08-30 00:59:54 +08:00
case sw_space_before_value:
2002-08-16 01:20:26 +08:00
switch (ch) {
case ' ':
break;
case CR:
r->header_start = r->header_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_almost_done;
2002-08-16 01:20:26 +08:00
break;
case LF:
r->header_start = r->header_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
default:
r->header_start = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_value;
2002-08-16 01:20:26 +08:00
break;
}
break;
/* header value */
2002-08-30 00:59:54 +08:00
case sw_value:
2002-08-16 01:20:26 +08:00
switch (ch) {
case ' ':
r->header_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_space_after_value;
2002-08-16 01:20:26 +08:00
break;
case CR:
r->header_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_almost_done;
2002-08-16 01:20:26 +08:00
break;
case LF:
r->header_end = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
}
break;
/* space* before end of header line */
2002-08-30 00:59:54 +08:00
case sw_space_after_value:
2002-08-16 01:20:26 +08:00
switch (ch) {
case ' ':
break;
case CR:
2002-08-30 00:59:54 +08:00
state = sw_almost_done;
2002-08-16 01:20:26 +08:00
break;
case LF:
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
default:
2002-08-30 00:59:54 +08:00
state = sw_value;
2002-08-16 01:20:26 +08:00
break;
}
break;
2003-04-08 23:40:10 +08:00
/* ignore header line */
case sw_ignore_line:
switch (ch) {
case LF:
state = sw_start;
break;
default:
break;
}
break;
2002-08-16 01:20:26 +08:00
/* end of header line */
2002-08-30 00:59:54 +08:00
case sw_almost_done:
2002-08-16 01:20:26 +08:00
switch (ch) {
case LF:
2002-08-30 00:59:54 +08:00
state = sw_done;
2002-08-16 01:20:26 +08:00
break;
default:
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_HEADER;
2002-08-16 01:20:26 +08:00
}
break;
/* end of header */
2002-08-30 00:59:54 +08:00
case sw_header_almost_done:
2002-08-16 01:20:26 +08:00
switch (ch) {
case LF:
2002-08-30 00:59:54 +08:00
state = sw_header_done;
2002-08-16 01:20:26 +08:00
break;
default:
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_HEADER;
2002-08-16 01:20:26 +08:00
}
break;
2003-11-21 14:30:49 +08:00
/* suppress warning */
case sw_done:
case sw_header_done:
break;
2002-08-16 01:20:26 +08:00
}
}
b->pos = p;
2002-08-16 01:20:26 +08:00
2002-08-30 00:59:54 +08:00
if (state == sw_done) {
r->state = sw_start;
return NGX_OK;
2003-01-09 13:36:00 +08:00
2002-08-30 00:59:54 +08:00
} else if (state == sw_header_done) {
r->state = sw_start;
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_HEADER_DONE;
2003-01-09 13:36:00 +08:00
2002-08-16 01:20:26 +08:00
} else {
r->state = state;
2002-08-30 00:59:54 +08:00
return NGX_AGAIN;
2002-08-16 01:20:26 +08:00
}
}
2003-11-29 01:41:47 +08:00
2003-12-15 04:10:27 +08:00
ngx_int_t ngx_http_parse_complex_uri(ngx_http_request_t *r)
2003-11-29 01:41:47 +08:00
{
2004-03-16 15:10:12 +08:00
u_char c, ch, decoded, *p, *u;
2003-11-29 01:41:47 +08:00
enum {
sw_usual = 0,
sw_slash,
sw_dot,
sw_dot_dot,
#if (WIN32)
sw_dot_dot_dot,
#endif
sw_quoted,
sw_quoted_second
} state, quoted_state;
decoded = '\0';
quoted_state = sw_usual;
state = sw_usual;
p = r->uri_start;
u = r->uri.data;
2003-12-01 04:03:18 +08:00
r->uri_ext = NULL;
2003-11-29 01:41:47 +08:00
ch = *p++;
while (p < r->uri_start + r->uri.len + 1) {
2004-02-12 01:08:49 +08:00
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"s:%d in:'%x:%c', out:'%c'", state, ch, ch, *u);
2003-11-29 01:41:47 +08:00
switch (state) {
case sw_usual:
switch(ch) {
case '/':
2003-12-01 04:03:18 +08:00
r->uri_ext = NULL;
2003-11-29 01:41:47 +08:00
state = sw_slash;
*u++ = ch;
break;
case '%':
quoted_state = state;
state = sw_quoted;
break;
2003-12-01 04:03:18 +08:00
case '.':
r->uri_ext = u + 1;
2003-11-29 01:41:47 +08:00
default:
*u++ = ch;
break;
}
ch = *p++;
break;
case sw_slash:
switch(ch) {
case '/':
break;
case '.':
state = sw_dot;
*u++ = ch;
break;
case '%':
quoted_state = state;
state = sw_quoted;
break;
default:
state = sw_usual;
*u++ = ch;
break;
}
ch = *p++;
break;
case sw_dot:
switch(ch) {
case '/':
state = sw_slash;
u--;
break;
case '.':
state = sw_dot_dot;
*u++ = ch;
break;
case '%':
quoted_state = state;
state = sw_quoted;
break;
default:
state = sw_usual;
*u++ = ch;
break;
}
ch = *p++;
break;
case sw_dot_dot:
switch(ch) {
case '/':
state = sw_slash;
u -= 4;
if (u < r->uri.data) {
return NGX_HTTP_PARSE_INVALID_REQUEST;
}
while (*(u - 1) != '/') {
u--;
}
break;
case '%':
quoted_state = state;
state = sw_quoted;
break;
#if (WIN32)
case '.':
state = sw_dot_dot_dot;
*u++ = ch;
break;
#endif
default:
state = sw_usual;
*u++ = ch;
break;
}
ch = *p++;
break;
#if (WIN32)
case sw_dot_dot_dot:
switch(ch) {
case '/':
state = sw_slash;
u -= 5;
if (u < r->uri.data) {
return NGX_HTTP_PARSE_INVALID_REQUEST;
}
while (*u != '/') {
u--;
}
if (u < r->uri.data) {
return NGX_HTTP_PARSE_INVALID_REQUEST;
}
while (*(u - 1) != '/') {
u--;
}
break;
case '%':
quoted_state = state;
state = sw_quoted;
break;
default:
state = sw_usual;
*u++ = ch;
break;
}
ch = *p++;
break;
#endif
case sw_quoted:
if (ch >= '0' && ch <= '9') {
2004-03-16 21:35:20 +08:00
decoded = (u_char) (ch - '0');
2003-11-29 01:41:47 +08:00
state = sw_quoted_second;
ch = *p++;
break;
}
2004-03-16 21:35:20 +08:00
c = (u_char) (ch | 0x20);
2003-11-29 01:41:47 +08:00
if (c >= 'a' && c <= 'f') {
2004-03-16 21:35:20 +08:00
decoded = (u_char) (c - 'a' + 10);
2003-11-29 01:41:47 +08:00
state = sw_quoted_second;
ch = *p++;
break;
}
return NGX_HTTP_PARSE_INVALID_REQUEST;
case sw_quoted_second:
if (ch >= '0' && ch <= '9') {
2004-03-16 21:35:20 +08:00
ch = (u_char) ((decoded << 4) + ch - '0');
2003-12-03 00:57:05 +08:00
if (ch == '%') {
state = sw_usual;
*u++ = ch;
ch = *p++;
break;
}
2003-11-29 01:41:47 +08:00
state = quoted_state;
break;
}
2004-03-16 21:35:20 +08:00
c = (u_char) (ch | 0x20);
2003-11-29 01:41:47 +08:00
if (c >= 'a' && c <= 'f') {
2004-03-16 21:35:20 +08:00
ch = (u_char) ((decoded << 4) + c - 'a' + 10);
2003-12-03 00:57:05 +08:00
if (ch == '%') {
state = sw_usual;
*u++ = ch;
ch = *p++;
break;
}
2003-11-29 01:41:47 +08:00
state = quoted_state;
break;
}
return NGX_HTTP_PARSE_INVALID_REQUEST;
}
}
r->uri.len = u - r->uri.data;
r->uri.data[r->uri.len] = '\0';
2003-12-01 04:03:18 +08:00
if (r->uri_ext) {
r->exten.len = u - r->uri_ext;
if (!(r->exten.data = ngx_palloc(r->pool, r->exten.len + 1))) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_cpystrn(r->exten.data, r->uri_ext, r->exten.len + 1);
}
r->uri_ext = NULL;
2003-11-29 01:41:47 +08:00
return NGX_OK;
}