nginx/src/http/ngx_http_parse.c

486 lines
12 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>
int ngx_read_http_request_line(ngx_http_request_t *r)
{
2002-12-05 00:29:40 +08:00
char ch;
char *p;
2002-08-16 01:20:26 +08:00
enum {
2002-08-30 00:59:54 +08:00
sw_start = 0,
sw_space_after_method,
sw_spaces_before_uri,
sw_after_slash_in_uri,
sw_check_uri,
sw_uri,
sw_http_09,
sw_http_version,
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;
p = r->header_in->pos.mem;
2002-08-16 01:20:26 +08:00
2002-08-30 00:59:54 +08:00
while (p < r->header_in->last.mem && state < sw_done) {
2002-08-16 01:20:26 +08:00
ch = *p++;
/*
printf("\nstate: %d, pos: %x, end: %x, char: '%c' buf: %s",
2002-08-30 00:59:54 +08:00
state, p, r->header_in->last, ch, p);
2002-08-16 01:20:26 +08:00
*/
2002-10-05 01:58:04 +08:00
/* GCC 2.95.2 and VC 6.0 compile this switch as 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:
2002-08-16 01:20:26 +08:00
switch (ch) {
case 'G':
2002-08-30 00:59:54 +08:00
if (p + 1 >= r->header_in->last.mem)
return NGX_AGAIN;
2002-08-16 01:20:26 +08:00
if (*p != 'E' || *(p + 1) != 'T')
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_METHOD;
2002-08-16 01:20:26 +08:00
r->method = NGX_HTTP_GET;
p += 2;
break;
case 'H':
2002-08-30 00:59:54 +08:00
if (p + 2 >= r->header_in->last.mem)
return NGX_AGAIN;
2002-08-16 01:20:26 +08:00
if (*p != 'E' || *(p + 1) != 'A' || *(p + 2) != 'D')
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_METHOD;
2002-08-16 01:20:26 +08:00
r->method = NGX_HTTP_HEAD;
p += 3;
break;
case 'P':
2002-08-30 00:59:54 +08:00
if (p + 2 >= r->header_in->last.mem)
return NGX_AGAIN;
2002-08-16 01:20:26 +08:00
if (*p != 'O' || *(p + 1) != 'S' || *(p + 2) != 'T')
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_METHOD;
2002-08-16 01:20:26 +08:00
r->method = NGX_HTTP_POST;
p += 3;
break;
default:
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_METHOD;
2002-08-16 01:20:26 +08:00
}
2002-08-30 00:59:54 +08:00
state = sw_space_after_method;
2002-08-16 01:20:26 +08:00
break;
/* single space after method */
2002-08-30 00:59:54 +08:00
case sw_space_after_method:
2002-08-16 01:20:26 +08:00
switch (ch) {
case ' ':
2002-08-30 00:59:54 +08:00
state = sw_spaces_before_uri;
2002-08-16 01:20:26 +08:00
break;
default:
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_METHOD;
2002-08-16 01:20:26 +08:00
}
break;
/* space* before URI */
2002-08-30 00:59:54 +08:00
case sw_spaces_before_uri:
2002-08-16 01:20:26 +08:00
switch (ch) {
case '/':
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;
case ' ':
break;
default:
r->unusual_uri = 1;
r->uri_start = p - 1;
2002-08-30 00:59:54 +08:00
state = sw_uri;
2002-08-16 01:20:26 +08:00
break;
}
break;
/* check dot after slash */
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 '.':
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;
2002-08-30 00:59:54 +08:00
state = sw_uri;
2002-08-16 01:20:26 +08:00
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;
/* check slash 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;
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':
2002-08-30 00:59:54 +08:00
state = sw_http_version;
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;
2002-12-05 00:29:40 +08:00
/* "TTP/" */
2002-08-30 00:59:54 +08:00
case sw_http_version:
if (p + 2 >= r->header_in->last.mem) {
r->state = sw_http_version;
r->header_in->pos.mem = p - 1;
return NGX_AGAIN;
2002-08-16 01:20:26 +08:00
}
if (ch != 'T' || *p != 'T' || *(p + 1) != 'P' || *(p + 2) != '/')
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
2002-08-16 01:20:26 +08:00
p += 3;
2002-08-30 00:59:54 +08:00
state = sw_first_major_digit;
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:
2002-08-16 01:20:26 +08:00
if (ch < '1' || ch > '9')
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
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;
}
if (ch < '0' || ch > '9')
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
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:
2002-08-16 01:20:26 +08:00
if (ch < '0' || ch > '9')
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
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;
}
if (ch < '0' || ch > '9')
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_REQUEST;
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;
}
}
2002-08-30 00:59:54 +08:00
r->header_in->pos.mem = p;
2002-08-16 01:20:26 +08:00
2002-08-30 00:59:54 +08:00
if (state == sw_done) {
2002-10-05 01:58:04 +08:00
if (r->request_end == NULL)
r->request_end = p - 1;
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;
if (r->http_version == 9 && r->method == NGX_HTTP_HEAD)
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_INVALID_HEAD;
2002-08-30 00:59:54 +08:00
else
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
}
}
2002-12-11 02:05:12 +08:00
int ngx_read_http_header_line(ngx_http_request_t *r, ngx_hunk_t *h)
2002-08-16 01:20:26 +08:00
{
2002-12-05 00:29:40 +08:00
char c, ch;
char *p;
2002-08-16 01:20:26 +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,
sw_done,
sw_header_done
2002-12-05 00:29:40 +08:00
} state;
state = r->state;
2002-12-11 02:05:12 +08:00
p = h->pos.mem;
2002-08-16 01:20:26 +08:00
2002-12-11 02:05:12 +08:00
while (p < h->last.mem && state < sw_done) {
2002-08-16 01:20:26 +08:00
ch = *p++;
/*
printf("\nstate: %d, pos: %x, end: %x, char: '%c' buf: %s",
2002-12-11 02:05:12 +08:00
state, p, h->last.mem, ch, p);
2002-08-16 01:20:26 +08:00
*/
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;
c = ch | 0x20;
if (c >= 'a' && c <= 'z')
break;
if (ch == '-')
break;
if (ch >= '0' && ch <= '9')
break;
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:
2002-08-16 01:20:26 +08:00
c = ch | 0x20;
if (c >= 'a' && c <= 'z')
break;
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;
}
if (ch == '-')
break;
if (ch >= '0' && ch <= '9')
break;
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;
/* 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;
}
}
2002-12-11 02:05:12 +08:00
h->pos.mem = 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;
} else if (state == sw_header_done) {
r->state = sw_start;
2002-09-13 22:47:42 +08:00
return NGX_HTTP_PARSE_HEADER_DONE;
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
}
}