nginx/src/http/ngx_http_request.c

2003 lines
55 KiB
C
Raw Normal View History

#include <ngx_config.h>
2002-08-26 23:18:19 +08:00
#include <ngx_core.h>
2003-01-24 02:47:54 +08:00
#include <ngx_event.h>
#include <ngx_http.h>
2002-09-02 22:48:24 +08:00
2003-05-12 23:52:24 +08:00
static void ngx_http_init_request(ngx_event_t *ev);
2004-07-17 01:11:43 +08:00
#if (NGX_HTTP_SSL)
static void ngx_http_check_ssl_handshake(ngx_event_t *rev);
#endif
2003-05-12 23:52:24 +08:00
static void ngx_http_process_request_line(ngx_event_t *rev);
static void ngx_http_process_request_headers(ngx_event_t *rev);
static ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
2004-06-16 23:32:11 +08:00
static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
2003-05-12 23:52:24 +08:00
2003-10-10 23:10:50 +08:00
static void ngx_http_set_write_handler(ngx_http_request_t *r);
2003-05-12 23:52:24 +08:00
2003-05-15 23:42:53 +08:00
static void ngx_http_block_read(ngx_event_t *ev);
static void ngx_http_read_discarded_body_event(ngx_event_t *rev);
2004-06-16 23:32:11 +08:00
static ngx_int_t ngx_http_read_discarded_body(ngx_http_request_t *r);
2002-09-12 22:42:29 +08:00
2003-05-14 00:02:32 +08:00
static void ngx_http_set_keepalive(ngx_http_request_t *r);
static void ngx_http_keepalive_handler(ngx_event_t *ev);
static void ngx_http_set_lingering_close(ngx_http_request_t *r);
static void ngx_http_lingering_close_handler(ngx_event_t *ev);
2003-10-27 16:53:49 +08:00
static void ngx_http_client_error(ngx_http_request_t *r,
int client_error, int error);
2002-08-26 23:18:19 +08:00
static size_t ngx_http_log_error(void *data, char *buf, size_t len);
2003-10-27 16:53:49 +08:00
/* NGX_HTTP_PARSE_... errors */
2002-08-26 23:18:19 +08:00
2003-10-27 16:53:49 +08:00
static char *client_header_errors[] = {
2002-08-30 00:59:54 +08:00
"client %s sent invalid method",
"client %s sent invalid request",
2002-09-13 22:47:42 +08:00
"client %s sent too long URI",
2003-05-15 23:42:53 +08:00
"client %s sent invalid method in HTTP/0.9 request",
2003-03-04 14:33:48 +08:00
"client %s sent invalid header, URL: %s",
"client %s sent too long header line, URL: %s",
2003-04-25 22:43:13 +08:00
"client %s sent HTTP/1.1 request without \"Host\" header, URL: %s",
2004-03-30 23:59:50 +08:00
"client %s sent invalid \"Content-Length\" header, URL: %s",
2004-05-15 00:51:47 +08:00
"client %s sent POST method without \"Content-Length\" header, URL: %s",
2004-07-17 01:11:43 +08:00
"client %s sent plain HTTP request to HTTPS port, URL: %s",
2004-05-15 00:51:47 +08:00
"client %s sent invalid \"Host\" header \"%s\", URL: %s"
2002-08-30 00:59:54 +08:00
};
2003-12-15 04:10:27 +08:00
#if 0
2003-10-27 16:53:49 +08:00
static void ngx_http_dummy(ngx_event_t *wev)
{
return;
}
2003-12-15 04:10:27 +08:00
#endif
2003-10-27 16:53:49 +08:00
2003-05-12 23:52:24 +08:00
void ngx_http_init_connection(ngx_connection_t *c)
{
2003-05-27 20:18:54 +08:00
ngx_event_t *rev;
2003-11-20 00:26:41 +08:00
ngx_http_log_ctx_t *ctx;
2002-10-05 01:58:04 +08:00
2003-06-11 23:28:34 +08:00
c->addr_text.data = ngx_palloc(c->pool, c->listening->addr_text_max_len);
2003-05-12 23:52:24 +08:00
if (c->addr_text.data == NULL) {
ngx_http_close_connection(c);
return;
}
2002-12-11 02:05:12 +08:00
2003-06-11 23:28:34 +08:00
c->addr_text.len = ngx_sock_ntop(c->listening->family, c->sockaddr,
c->addr_text.data,
c->listening->addr_text_max_len);
2004-03-16 21:35:20 +08:00
2003-01-30 15:28:09 +08:00
if (c->addr_text.len == 0) {
2003-05-12 23:52:24 +08:00
ngx_http_close_connection(c);
return;
}
2003-11-20 00:26:41 +08:00
if (!(ctx = ngx_pcalloc(c->pool, sizeof(ngx_http_log_ctx_t)))) {
2003-05-12 23:52:24 +08:00
ngx_http_close_connection(c);
return;
2003-01-30 15:28:09 +08:00
}
2002-08-26 23:18:19 +08:00
2003-11-20 00:26:41 +08:00
ctx->connection = c->number;
ctx->client = c->addr_text.data;
ctx->action = "reading client request line";
c->log->data = ctx;
2002-08-26 23:18:19 +08:00
c->log->handler = ngx_http_log_error;
2004-02-11 00:23:38 +08:00
c->log_error = NGX_ERROR_INFO;
2002-08-26 23:18:19 +08:00
2003-05-12 23:52:24 +08:00
rev = c->read;
rev->event_handler = ngx_http_init_request;
2003-02-07 01:21:13 +08:00
2004-02-13 04:57:10 +08:00
/* STUB: epoll edge */ c->write->event_handler = ngx_http_empty_handler;
2004-02-09 15:46:43 +08:00
2003-03-21 00:09:44 +08:00
if (rev->ready) {
2004-06-07 03:49:18 +08:00
/* the deferred accept(), rtsig, aio, iocp */
2004-03-31 23:26:46 +08:00
2004-06-01 14:04:46 +08:00
if (ngx_accept_mutex) {
2004-03-31 23:26:46 +08:00
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
2004-04-13 23:08:48 +08:00
ngx_post_event(rev);
2004-03-31 23:26:46 +08:00
ngx_mutex_unlock(ngx_posted_events_mutex);
return;
}
2003-05-12 23:52:24 +08:00
ngx_http_init_request(rev);
return;
2003-01-27 05:08:14 +08:00
}
2003-02-07 01:21:13 +08:00
2003-06-11 23:28:34 +08:00
ngx_add_timer(rev, c->listening->post_accept_timeout);
2003-01-27 05:08:14 +08:00
2003-10-29 16:30:44 +08:00
if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
2003-10-10 23:10:50 +08:00
ngx_http_close_connection(c);
return;
}
2003-10-27 16:53:49 +08:00
#if 0
2003-11-01 00:05:33 +08:00
/* TODO: learn SO_SNDBUF (to use in zerocopy) via kqueue's EV_CLEAR event */
2003-10-27 16:53:49 +08:00
c->write->ready = 0;
c->write->event_handler = ngx_http_dummy;
if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
#endif
}
2002-08-26 23:18:19 +08:00
2003-05-12 23:52:24 +08:00
static void ngx_http_init_request(ngx_event_t *rev)
{
2004-03-16 15:10:12 +08:00
ngx_uint_t i;
2003-05-27 20:18:54 +08:00
socklen_t len;
struct sockaddr_in addr_in;
2003-05-23 19:53:01 +08:00
ngx_connection_t *c;
ngx_http_request_t *r;
2003-05-27 20:18:54 +08:00
ngx_http_in_port_t *in_port;
ngx_http_in_addr_t *in_addr;
ngx_http_server_name_t *server_name;
2003-05-23 19:53:01 +08:00
ngx_http_core_srv_conf_t *cscf;
2003-07-21 05:15:59 +08:00
ngx_http_core_loc_conf_t *clcf;
2004-07-16 00:35:51 +08:00
#if (NGX_HTTP_SSL)
ngx_http_ssl_srv_conf_t *sscf;
#endif
2003-05-20 00:39:14 +08:00
c = rev->data;
2003-10-27 16:53:49 +08:00
if (rev->timedout) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
ngx_http_close_connection(c);
return;
}
2003-10-13 00:49:16 +08:00
if (c->data) {
r = c->data;
ngx_memzero(r, sizeof(ngx_http_request_t));
} else {
2003-10-30 16:51:06 +08:00
if (!(r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t)))) {
2003-10-13 00:49:16 +08:00
ngx_http_close_connection(c);
return;
}
2004-06-25 00:07:04 +08:00
c->data = r;
2003-05-27 20:18:54 +08:00
}
2004-06-25 00:07:04 +08:00
c->sent = 0;
2004-06-24 15:53:37 +08:00
r->signature = NGX_HTTP_MODULE;
2003-12-26 04:26:58 +08:00
2003-05-27 20:18:54 +08:00
/* find the server configuration for the address:port */
/* AF_INET only */
in_port = c->servers;
in_addr = in_port->addrs.elts;
r->port = in_port->port;
r->port_name = &in_port->port_name;
i = 0;
if (in_port->addrs.nelts > 1) {
2003-06-11 23:28:34 +08:00
/*
2004-02-09 15:46:43 +08:00
* There are the several addresses on this port and one of them
2003-06-11 23:28:34 +08:00
* is "*:port" so getsockname() is needed to determine
* the server address.
* AcceptEx() already gave this address.
*/
2003-05-27 20:18:54 +08:00
2003-06-11 23:28:34 +08:00
#if (WIN32)
if (c->local_sockaddr) {
r->in_addr =
((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
2003-05-27 20:18:54 +08:00
2003-06-11 23:28:34 +08:00
} else {
#endif
len = sizeof(struct sockaddr_in);
if (getsockname(c->fd, (struct sockaddr *) &addr_in, &len) == -1) {
2004-04-28 14:14:50 +08:00
ngx_connection_error(c, ngx_socket_errno,
"getsockname() failed");
2003-06-11 23:28:34 +08:00
ngx_http_close_connection(c);
return;
}
2004-03-16 15:10:12 +08:00
r->in_addr = addr_in.sin_addr.s_addr;
2003-06-11 23:28:34 +08:00
#if (WIN32)
2003-05-27 20:18:54 +08:00
}
2003-06-11 23:28:34 +08:00
#endif
2003-05-27 20:18:54 +08:00
/* the last in_port->addrs address is "*" */
for ( /* void */ ; i < in_port->addrs.nelts - 1; i++) {
if (in_addr[i].addr == r->in_addr) {
break;
}
}
} else {
r->in_addr = in_addr[0].addr;
}
r->virtual_names = &in_addr[i].names;
/* the default server configuration for the address:port */
cscf = in_addr[i].core_srv_conf;
r->main_conf = cscf->ctx->main_conf;
r->srv_conf = cscf->ctx->srv_conf;
r->loc_conf = cscf->ctx->loc_conf;
2004-07-17 01:11:43 +08:00
rev->event_handler = ngx_http_process_request_line;
r->recv = ngx_recv;
r->send_chain = ngx_send_chain;
2004-07-16 00:35:51 +08:00
#if (NGX_HTTP_SSL)
2004-07-17 01:11:43 +08:00
sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
2004-07-16 00:35:51 +08:00
if (sscf->enable) {
2004-07-17 01:11:43 +08:00
if (ngx_ssl_create_session(sscf->ssl_ctx, c, NGX_SSL_BUFFER)
== NGX_ERROR)
{
2004-07-16 00:35:51 +08:00
ngx_http_close_connection(c);
return;
}
r->filter_need_in_memory = 1;
2004-07-17 01:11:43 +08:00
rev->event_handler = ngx_http_check_ssl_handshake;
2004-07-16 00:35:51 +08:00
}
2004-07-15 04:07:58 +08:00
#endif
2003-05-27 20:18:54 +08:00
server_name = cscf->server_names.elts;
r->server_name = &server_name->name;
2003-07-21 05:15:59 +08:00
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
c->log->file = clcf->err_log->file;
2004-04-08 23:58:25 +08:00
if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
c->log->log_level = clcf->err_log->log_level;
}
2003-07-21 05:15:59 +08:00
2002-09-16 23:01:44 +08:00
if (c->buffer == NULL) {
c->buffer = ngx_create_temp_buf(c->pool,
cscf->client_header_buffer_size);
2003-05-12 23:52:24 +08:00
if (c->buffer == NULL) {
ngx_http_close_connection(c);
return;
}
2002-09-16 23:01:44 +08:00
}
2003-10-30 16:51:06 +08:00
if (!(r->pool = ngx_create_pool(cscf->request_pool_size, c->log))) {
2003-05-12 23:52:24 +08:00
ngx_http_close_connection(c);
return;
}
2003-11-29 01:41:47 +08:00
r->cleanup.elts = ngx_palloc(r->pool, 5 * sizeof(ngx_http_cleanup_t));
if (r->cleanup.elts == NULL) {
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
/*
* set by ngx_pcalloc():
*
* r->cleanup.nelts = 0;
*/
r->cleanup.nalloc = 5;
r->cleanup.size = sizeof(ngx_http_cleanup_t);
r->cleanup.pool = r->pool;
2004-03-23 14:01:52 +08:00
/* init the r->headers_out.headers table */
r->headers_out.headers.elts = ngx_pcalloc(r->pool,
20 * sizeof(ngx_table_elt_t));
if (r->headers_out.headers.elts == NULL) {
2003-05-12 23:52:24 +08:00
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
2004-03-23 14:01:52 +08:00
/* r->headers_out.headers.nelts = 0; */
r->headers_out.headers.nalloc = 20;
r->headers_out.headers.size = sizeof(ngx_table_elt_t);
r->headers_out.headers.pool = r->pool;
2003-05-12 23:52:24 +08:00
r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
if (r->ctx == NULL) {
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
2002-09-11 23:18:33 +08:00
2004-07-07 14:15:04 +08:00
c->single_connection = 1;
2003-05-12 23:52:24 +08:00
r->connection = c;
r->pipeline = c->pipeline;
r->header_in = c->buffer;
r->file.fd = NGX_INVALID_FILE;
2003-04-25 22:43:13 +08:00
r->headers_in.content_length_n = -1;
2003-10-22 00:49:56 +08:00
r->headers_in.keep_alive_n = -1;
r->headers_out.content_length_n = -1;
2002-12-15 14:25:09 +08:00
r->headers_out.last_modified_time = -1;
2004-06-25 00:07:04 +08:00
r->http_state = NGX_HTTP_READING_REQUEST_STATE;
2004-07-17 01:11:43 +08:00
rev->event_handler(rev);
}
#if (NGX_HTTP_SSL)
static void ngx_http_check_ssl_handshake(ngx_event_t *rev)
{
int n;
u_char buf[1];
ngx_connection_t *c;
ngx_http_request_t *r;
c = rev->data;
r = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
"http check ssl handshake");
if (rev->timedout) {
ngx_http_client_error(r, 0, NGX_HTTP_REQUEST_TIME_OUT);
return;
}
n = recv(c->fd, buf, 1, MSG_PEEK);
if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
return;
}
if (n == 1) {
if (buf[0] == 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
"https ssl handshake: 0x%X", buf[0]);
r->recv = ngx_ssl_recv;
r->send_chain = ngx_ssl_send_chain;
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
"plain http");
r->plain_http = 1;
}
}
2003-05-12 23:52:24 +08:00
rev->event_handler = ngx_http_process_request_line;
ngx_http_process_request_line(rev);
}
2004-07-17 01:11:43 +08:00
#endif
2002-08-26 23:18:19 +08:00
2003-05-12 23:52:24 +08:00
static void ngx_http_process_request_line(ngx_event_t *rev)
{
2004-03-16 15:10:12 +08:00
u_char *p;
2003-05-23 19:53:01 +08:00
ssize_t n;
2003-12-15 04:10:27 +08:00
ngx_int_t rc, offset;
2003-05-23 19:53:01 +08:00
ngx_connection_t *c;
ngx_http_request_t *r;
2003-11-20 00:26:41 +08:00
ngx_http_log_ctx_t *ctx;
2003-05-23 19:53:01 +08:00
ngx_http_core_srv_conf_t *cscf;
2002-08-26 23:18:19 +08:00
2003-05-20 00:39:14 +08:00
c = rev->data;
r = c->data;
2003-12-09 23:08:11 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
"http process request line");
2003-03-21 00:09:44 +08:00
if (rev->timedout) {
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, 0, NGX_HTTP_REQUEST_TIME_OUT);
2003-05-12 23:52:24 +08:00
return;
2003-03-21 00:09:44 +08:00
}
2003-05-12 23:52:24 +08:00
n = ngx_http_read_request_header(r);
2003-01-25 00:09:40 +08:00
2003-05-12 23:52:24 +08:00
if (n == NGX_AGAIN || n == NGX_ERROR) {
return;
2003-01-25 00:09:40 +08:00
}
2003-03-21 00:09:44 +08:00
2003-10-03 23:50:53 +08:00
rc = ngx_http_parse_request_line(r);
2003-03-21 00:09:44 +08:00
2002-08-30 00:59:54 +08:00
if (rc == NGX_OK) {
2003-03-21 00:09:44 +08:00
2003-05-12 23:52:24 +08:00
/* the request line has been parsed successfully */
2004-04-14 13:57:36 +08:00
#if 0
2004-01-08 16:47:17 +08:00
/* TODO: we need to handle proxy URIs */
2003-11-29 01:41:47 +08:00
if (r->unusual_uri) {
2003-06-03 23:42:58 +08:00
r->request_line.len = r->request_end - r->request_start;
r->request_line.data = r->request_start;
2004-01-08 16:47:17 +08:00
#if 0
2003-06-03 23:42:58 +08:00
r->request_line.data[r->request_line.len] = '\0';
2004-01-08 16:47:17 +08:00
#endif
2003-06-03 23:42:58 +08:00
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, NGX_HTTP_PARSE_INVALID_REQUEST,
NGX_HTTP_BAD_REQUEST);
2003-06-02 23:24:30 +08:00
return;
}
2004-04-14 13:57:36 +08:00
#endif
2003-06-02 23:24:30 +08:00
2003-05-27 20:18:54 +08:00
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2003-05-20 00:39:14 +08:00
2003-04-08 23:40:10 +08:00
if (r->http_version >= NGX_HTTP_VERSION_10
2003-05-23 19:53:01 +08:00
&& cscf->large_client_header == 0
2003-04-08 23:40:10 +08:00
&& r->header_in->pos == r->header_in->end)
{
2003-05-12 23:52:24 +08:00
/* no space for "\r\n" at the end of the header */
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, NGX_HTTP_PARSE_TOO_LONG_URI,
NGX_HTTP_REQUEST_URI_TOO_LARGE);
2003-05-12 23:52:24 +08:00
return;
2003-04-08 23:40:10 +08:00
}
2003-10-25 00:10:38 +08:00
2003-11-29 01:41:47 +08:00
/* copy unparsed URI */
r->unparsed_uri.len = r->uri_end - r->uri_start;
r->unparsed_uri.data = ngx_palloc(r->pool, r->unparsed_uri.len + 1);
if (r->unparsed_uri.data == NULL) {
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
ngx_cpystrn(r->unparsed_uri.data, r->uri_start,
r->unparsed_uri.len + 1);
2003-03-04 14:33:48 +08:00
/* copy URI */
2003-03-21 00:09:44 +08:00
if (r->args_start) {
r->uri.len = r->args_start - 1 - r->uri_start;
} else {
r->uri.len = r->uri_end - r->uri_start;
}
2003-10-30 16:51:06 +08:00
if (!(r->uri.data = ngx_palloc(r->pool, r->uri.len + 1))) {
2003-05-12 23:52:24 +08:00
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
2003-03-21 00:09:44 +08:00
2003-11-29 01:41:47 +08:00
if (r->complex_uri) {
rc = ngx_http_parse_complex_uri(r);
2003-05-20 00:39:14 +08:00
2003-12-01 04:03:18 +08:00
if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
ngx_http_close_request(r, rc);
ngx_http_close_connection(c);
return;
}
if (rc != NGX_OK) {
r->request_line.len = r->request_end - r->request_start;
r->request_line.data = r->request_start;
ngx_http_client_error(r, rc, NGX_HTTP_BAD_REQUEST);
return;
}
2003-11-29 01:41:47 +08:00
} else {
ngx_cpystrn(r->uri.data, r->uri_start, r->uri.len + 1);
2003-05-20 00:39:14 +08:00
}
2003-03-21 00:09:44 +08:00
r->request_line.len = r->request_end - r->request_start;
2003-05-23 19:53:01 +08:00
if (cscf->large_client_header) {
2003-05-12 23:52:24 +08:00
/*
* if the large client headers are enabled then
2004-01-19 05:09:21 +08:00
* we need to copy the request line
*/
2003-05-12 23:52:24 +08:00
r->request_line.data = ngx_palloc(r->pool, r->request_line.len + 1);
if (r->request_line.data == NULL) {
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
2003-03-21 00:09:44 +08:00
2003-03-05 14:37:42 +08:00
ngx_cpystrn(r->request_line.data, r->request_start,
2003-03-04 14:33:48 +08:00
r->request_line.len + 1);
2002-12-03 23:45:38 +08:00
2003-03-04 14:33:48 +08:00
} else {
2003-03-05 14:37:42 +08:00
r->request_line.data = r->request_start;
2003-03-04 14:33:48 +08:00
r->request_line.data[r->request_line.len] = '\0';
}
2004-04-14 13:57:36 +08:00
if (r->method == 0) {
r->method_name.len = r->method_end - r->request_start + 1;
r->method_name.data = r->request_line.data;
}
2003-03-21 00:09:44 +08:00
2002-09-16 23:01:44 +08:00
if (r->uri_ext) {
/* copy URI extention */
2003-03-21 00:09:44 +08:00
if (r->args_start) {
r->exten.len = r->args_start - 1 - r->uri_ext;
} else {
r->exten.len = r->uri_end - r->uri_ext;
}
2003-10-30 16:51:06 +08:00
if (!(r->exten.data = ngx_palloc(r->pool, r->exten.len + 1))) {
2003-05-12 23:52:24 +08:00
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
2003-03-21 00:09:44 +08:00
2002-12-11 02:05:12 +08:00
ngx_cpystrn(r->exten.data, r->uri_ext, r->exten.len + 1);
2002-09-16 23:01:44 +08:00
}
2003-03-21 00:09:44 +08:00
if (r->args_start && r->uri_end > r->args_start) {
/* copy URI arguments */
2003-03-21 00:09:44 +08:00
r->args.len = r->uri_end - r->args_start;
2003-10-30 16:51:06 +08:00
if (!(r->args.data = ngx_palloc(r->pool, r->args.len + 1))) {
2003-05-12 23:52:24 +08:00
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
2003-03-21 00:09:44 +08:00
ngx_cpystrn(r->args.data, r->args_start, r->args.len + 1);
}
2003-12-15 04:10:27 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http request line: \"%s\"", r->request_line.data);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http uri: \"%s\"", r->uri.data);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
2004-03-16 15:10:12 +08:00
"http args: \"%s\"",
r->args.data ? r->args.data : (u_char *) "");
2003-12-15 04:10:27 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http exten: \"%s\"",
2004-03-16 15:10:12 +08:00
r->exten.data ? r->exten.data : (u_char *) "");
2002-08-30 00:59:54 +08:00
2003-06-02 23:24:30 +08:00
if (r->http_version < NGX_HTTP_VERSION_10) {
2003-05-15 23:42:53 +08:00
rev->event_handler = ngx_http_block_read;
2003-05-14 00:02:32 +08:00
ngx_http_handler(r);
return;
2003-03-06 01:30:51 +08:00
}
2002-09-13 22:47:42 +08:00
2004-03-23 14:01:52 +08:00
/* init the r->headers_in.headers table */
r->headers_in.headers.elts = ngx_pcalloc(r->pool,
20 * sizeof(ngx_table_elt_t));
if (r->headers_in.headers.elts == NULL) {
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
/* r->headers_in.headers.elts = NULL; */
/* r->headers_in.headers.nelts = 0; */
r->headers_in.headers.size = sizeof(ngx_table_elt_t);
r->headers_in.headers.nalloc = 20;
r->headers_in.headers.pool = r->pool;
2003-11-20 00:26:41 +08:00
ctx = c->log->data;
ctx->action = "reading client request headers";
ctx->url = r->unparsed_uri.data;
2003-03-21 00:09:44 +08:00
2003-05-23 19:53:01 +08:00
if (cscf->large_client_header
2003-03-21 00:09:44 +08:00
&& r->header_in->pos == r->header_in->last)
{
r->header_in->pos = r->header_in->last = r->header_in->start;
}
2002-08-30 00:59:54 +08:00
2003-05-14 00:02:32 +08:00
rev->event_handler = ngx_http_process_request_headers;
ngx_http_process_request_headers(rev);
return;
2003-05-12 23:52:24 +08:00
} else if (rc != NGX_AGAIN) {
2003-03-06 01:30:51 +08:00
2003-05-12 23:52:24 +08:00
/* there was error while a request line parsing */
2003-03-21 00:09:44 +08:00
2004-01-08 16:47:17 +08:00
for (p = r->request_start; p < r->header_in->last; p++) {
if (*p == CR || *p == LF) {
break;
}
}
r->request_line.len = p - r->request_start;
r->request_line.data = r->request_start;
2004-01-20 02:09:14 +08:00
if (rc == NGX_HTTP_PARSE_INVALID_METHOD) {
r->http_version = NGX_HTTP_VERSION_10;
}
2004-01-19 05:09:21 +08:00
ngx_http_client_error(r, rc,
(rc == NGX_HTTP_PARSE_INVALID_METHOD) ?
NGX_HTTP_NOT_IMPLEMENTED:
NGX_HTTP_BAD_REQUEST);
2003-05-12 23:52:24 +08:00
return;
}
2003-03-06 01:30:51 +08:00
/* NGX_AGAIN: a request line parsing is still not complete */
2003-03-12 04:38:13 +08:00
if (r->header_in->last == r->header_in->end) {
2002-09-13 22:47:42 +08:00
2003-10-13 00:49:16 +08:00
/*
* If it's a pipelined request and a request line is not complete
2004-06-01 14:04:46 +08:00
* then we have to copy it to the start of the r->header_in buf.
* We have to copy it here only if the large client headers
2003-10-13 00:49:16 +08:00
* are enabled otherwise a request line had been already copied
2004-06-01 14:04:46 +08:00
* to the start of the r->header_in buf in ngx_http_set_keepalive().
2003-10-13 00:49:16 +08:00
*/
2003-03-06 01:30:51 +08:00
2003-05-27 20:18:54 +08:00
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2003-05-20 00:39:14 +08:00
2003-05-23 19:53:01 +08:00
if (cscf->large_client_header) {
2003-03-06 01:30:51 +08:00
offset = r->request_start - r->header_in->start;
if (offset == 0) {
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, NGX_HTTP_PARSE_TOO_LONG_URI,
NGX_HTTP_REQUEST_URI_TOO_LARGE);
2003-05-12 23:52:24 +08:00
return;
2003-03-06 01:30:51 +08:00
}
ngx_memcpy(r->header_in->start, r->request_start,
2003-03-12 04:38:13 +08:00
r->header_in->last - r->request_start);
2003-03-06 01:30:51 +08:00
2003-03-12 04:38:13 +08:00
r->header_in->pos -= offset;
r->header_in->last -= offset;
2003-03-06 01:30:51 +08:00
r->request_start = r->header_in->start;
r->request_end -= offset;
r->uri_start -= offset;
r->uri_end -= offset;
if (r->uri_ext) {
r->uri_ext -= offset;
}
if (r->args_start) {
r->args_start -= offset;
}
} else {
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, NGX_HTTP_PARSE_TOO_LONG_URI,
NGX_HTTP_REQUEST_URI_TOO_LARGE);
2003-03-06 01:30:51 +08:00
}
2002-09-13 22:47:42 +08:00
}
2003-05-12 23:52:24 +08:00
return;
}
2002-09-13 22:47:42 +08:00
2003-05-12 23:52:24 +08:00
static void ngx_http_process_request_headers(ngx_event_t *rev)
{
2003-05-23 19:53:01 +08:00
ssize_t n;
2003-12-15 04:10:27 +08:00
ngx_int_t rc, i, offset;
2003-05-23 19:53:01 +08:00
ngx_table_elt_t *h;
ngx_connection_t *c;
ngx_http_request_t *r;
ngx_http_core_srv_conf_t *cscf;
2003-05-20 00:39:14 +08:00
c = rev->data;
r = c->data;
2003-05-14 00:02:32 +08:00
2003-12-01 04:03:18 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
"http process request header line");
2003-05-14 00:02:32 +08:00
2003-05-12 23:52:24 +08:00
if (rev->timedout) {
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, 0, NGX_HTTP_REQUEST_TIME_OUT);
2003-05-12 23:52:24 +08:00
return;
}
2003-05-27 20:18:54 +08:00
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2003-05-14 00:02:32 +08:00
rc = NGX_AGAIN;
2003-05-12 23:52:24 +08:00
2002-08-30 00:59:54 +08:00
for ( ;; ) {
2003-05-14 00:02:32 +08:00
if (rc == NGX_AGAIN) {
n = ngx_http_read_request_header(r);
2002-08-30 00:59:54 +08:00
2003-05-14 00:02:32 +08:00
if (n == NGX_AGAIN || n == NGX_ERROR) {
return;
}
}
2003-10-03 23:50:53 +08:00
rc = ngx_http_parse_header_line(r, r->header_in);
2003-03-21 00:09:44 +08:00
2003-03-06 01:30:51 +08:00
if (rc == NGX_OK) {
2003-05-14 00:02:32 +08:00
/* a header line has been parsed successfully */
2003-10-30 16:51:06 +08:00
h = ngx_http_add_header(&r->headers_in, ngx_http_headers_in);
if (h == NULL) {
2003-05-14 00:02:32 +08:00
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
2003-03-21 00:09:44 +08:00
}
2003-05-14 00:02:32 +08:00
h->key.len = r->header_name_end - r->header_name_start;
h->value.len = r->header_end - r->header_start;
/* if the large client headers are enabled then
we need to copy the header name and value */
2003-05-23 19:53:01 +08:00
if (cscf->large_client_header) {
2003-05-14 00:02:32 +08:00
h->key.data = ngx_palloc(r->pool,
h->key.len + 1 + h->value.len + 1);
if (h->key.data == NULL) {
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(c);
return;
}
h->value.data = h->key.data + h->key.len + 1;
ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
} else {
h->key.data = r->header_name_start;
h->key.data[h->key.len] = '\0';
h->value.data = r->header_start;
h->value.data[h->value.len] = '\0';
}
2003-10-29 16:30:44 +08:00
for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) {
if (ngx_http_headers_in[i].name.len != h->key.len) {
2003-05-14 00:02:32 +08:00
continue;
}
2003-10-29 16:30:44 +08:00
if (ngx_strcasecmp(ngx_http_headers_in[i].name.data,
h->key.data) == 0)
{
*((ngx_table_elt_t **) ((char *) &r->headers_in
+ ngx_http_headers_in[i].offset)) = h;
2003-10-21 15:47:21 +08:00
break;
2003-05-14 00:02:32 +08:00
}
}
2003-12-15 04:10:27 +08:00
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2003-12-23 01:27:24 +08:00
"http header: \"%s: %s\"",
2003-12-15 04:10:27 +08:00
h->key.data, h->value.data);
2003-05-14 00:02:32 +08:00
2003-05-23 19:53:01 +08:00
if (cscf->large_client_header
2003-03-21 00:09:44 +08:00
&& r->header_in->pos == r->header_in->last)
{
r->header_in->pos = r->header_in->last = r->header_in->start;
2003-01-28 23:56:37 +08:00
}
2003-05-27 20:18:54 +08:00
continue;
2003-05-14 00:02:32 +08:00
} else if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
2002-09-02 22:48:24 +08:00
2003-05-14 00:02:32 +08:00
/* a whole header has been parsed successfully */
2003-03-21 00:09:44 +08:00
2003-12-15 04:10:27 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http header done");
2002-12-15 14:25:09 +08:00
2004-06-25 00:07:04 +08:00
r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
2003-10-22 00:49:56 +08:00
rc = ngx_http_process_request_header(r);
2003-10-13 00:49:16 +08:00
2003-10-22 00:49:56 +08:00
if (rc != NGX_OK) {
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, rc, NGX_HTTP_BAD_REQUEST);
2003-10-22 00:49:56 +08:00
return;
2003-04-25 22:43:13 +08:00
}
2003-10-28 00:16:17 +08:00
if (rev->timer_set) {
2003-10-13 00:49:16 +08:00
ngx_del_timer(rev);
}
2003-05-15 23:42:53 +08:00
rev->event_handler = ngx_http_block_read;
2003-05-14 00:02:32 +08:00
ngx_http_handler(r);
return;
2003-03-21 00:09:44 +08:00
2003-03-06 01:30:51 +08:00
} else if (rc != NGX_AGAIN) {
2003-05-14 00:02:32 +08:00
/* there was error while a header line parsing */
2004-04-22 02:54:33 +08:00
#if (NGX_DEBUG)
if (rc == NGX_HTTP_PARSE_INVALID_HEADER
&& (rev->log->log_level & NGX_LOG_DEBUG_HTTP))
{
2004-03-16 15:10:12 +08:00
u_char *p;
2003-12-22 17:40:48 +08:00
for (p = r->header_name_start;
p < r->header_in->last - 1;
p++)
{
2004-01-30 01:06:07 +08:00
if (*p == LF) {
2003-12-22 17:40:48 +08:00
break;
}
}
*p = '\0';
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
"http invalid header: \"%s\"",
r->header_name_start);
}
#endif
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, rc, NGX_HTTP_BAD_REQUEST);
2003-05-14 00:02:32 +08:00
return;
2003-03-06 01:30:51 +08:00
}
/* NGX_AGAIN: a header line parsing is still not complete */
2003-03-12 04:38:13 +08:00
if (r->header_in->last == r->header_in->end) {
2003-03-06 01:30:51 +08:00
2004-06-01 14:04:46 +08:00
/*
* if the large client headers are enabled then
* we need to compact r->header_in buf
*/
2003-03-06 01:30:51 +08:00
2003-05-23 19:53:01 +08:00
if (cscf->large_client_header) {
2003-03-06 01:30:51 +08:00
offset = r->header_name_start - r->header_in->start;
if (offset == 0) {
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, NGX_HTTP_PARSE_TOO_LONG_HEADER,
NGX_HTTP_BAD_REQUEST);
2003-05-14 00:02:32 +08:00
return;
2003-03-06 01:30:51 +08:00
}
ngx_memcpy(r->header_in->start, r->header_name_start,
2003-03-12 04:38:13 +08:00
r->header_in->last - r->header_name_start);
2003-03-06 01:30:51 +08:00
2003-03-12 04:38:13 +08:00
r->header_in->last -= offset;
r->header_in->pos -= offset;
2003-03-06 01:30:51 +08:00
r->header_name_start = r->header_in->start;
r->header_name_end -= offset;
r->header_start -= offset;
r->header_end -= offset;
} else {
2003-10-27 16:53:49 +08:00
ngx_http_client_error(r, NGX_HTTP_PARSE_TOO_LONG_HEADER,
NGX_HTTP_BAD_REQUEST);
2003-05-14 00:02:32 +08:00
return;
2003-03-06 01:30:51 +08:00
}
2003-03-04 14:33:48 +08:00
}
2002-08-30 00:59:54 +08:00
}
2002-09-02 22:48:24 +08:00
}
2002-09-13 22:47:42 +08:00
2003-05-14 00:02:32 +08:00
static ssize_t ngx_http_read_request_header(ngx_http_request_t *r)
2002-09-02 22:48:24 +08:00
{
2003-05-23 19:53:01 +08:00
ssize_t n;
ngx_event_t *rev;
ngx_http_core_srv_conf_t *cscf;
2003-06-11 23:28:34 +08:00
rev = r->connection->read;
2003-05-14 00:02:32 +08:00
n = r->header_in->last - r->header_in->pos;
2003-05-14 00:02:32 +08:00
if (n > 0) {
return n;
}
2003-03-04 14:33:48 +08:00
2003-11-19 00:49:00 +08:00
if (!rev->ready) {
return NGX_AGAIN;
}
2004-07-17 01:11:43 +08:00
n = r->recv(r->connection, r->header_in->last,
r->header_in->end - r->header_in->last);
2003-03-21 00:09:44 +08:00
2003-05-14 00:02:32 +08:00
if (n == NGX_AGAIN) {
2003-05-15 23:42:53 +08:00
if (!r->header_timeout_set) {
2004-07-17 01:11:43 +08:00
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2003-05-23 19:53:01 +08:00
ngx_add_timer(rev, cscf->client_header_timeout);
2003-05-14 00:02:32 +08:00
r->header_timeout_set = 1;
2003-01-28 23:56:37 +08:00
}
2003-10-29 16:30:44 +08:00
if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
2003-10-10 23:10:50 +08:00
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_http_close_connection(r->connection);
return NGX_ERROR;
}
2003-05-14 00:02:32 +08:00
return NGX_AGAIN;
}
2003-05-14 00:02:32 +08:00
if (n == 0) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client closed prematurely connection");
}
2002-09-02 22:48:24 +08:00
2003-05-14 00:02:32 +08:00
if (n == 0 || n == NGX_ERROR) {
ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
ngx_http_close_connection(r->connection);
return NGX_ERROR;
}
r->header_in->last += n;
return n;
2002-09-02 22:48:24 +08:00
}
2003-12-15 04:10:27 +08:00
static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r)
2003-10-22 00:49:56 +08:00
{
2004-06-16 23:32:11 +08:00
u_char *ua;
2003-10-22 00:49:56 +08:00
size_t len;
2004-03-16 15:10:12 +08:00
ngx_uint_t i;
2003-10-22 00:49:56 +08:00
ngx_http_server_name_t *name;
2004-05-15 00:51:47 +08:00
ngx_http_core_srv_conf_t *cscf;
2003-10-22 00:49:56 +08:00
ngx_http_core_loc_conf_t *clcf;
if (r->headers_in.host) {
for (len = 0; len < r->headers_in.host->value.len; len++) {
if (r->headers_in.host->value.data[len] == ':') {
break;
}
}
r->headers_in.host_name_len = len;
/* find the name based server configuration */
name = r->virtual_names->elts;
for (i = 0; i < r->virtual_names->nelts; i++) {
if (r->headers_in.host_name_len != name[i].name.len) {
continue;
}
if (ngx_strncasecmp(r->headers_in.host->value.data,
name[i].name.data,
r->headers_in.host_name_len) == 0)
{
r->srv_conf = name[i].core_srv_conf->ctx->srv_conf;
r->loc_conf = name[i].core_srv_conf->ctx->loc_conf;
2004-05-05 01:56:58 +08:00
r->server_name = &name[i].name;
2003-10-22 00:49:56 +08:00
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
r->connection->log->file = clcf->err_log->file;
r->connection->log->log_level = clcf->err_log->log_level;
break;
}
}
2004-05-15 00:51:47 +08:00
if (i == r->virtual_names->nelts) {
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
if (cscf->restrict_host_names != NGX_HTTP_RESTRICT_HOST_OFF) {
return NGX_HTTP_PARSE_INVALID_HOST;
}
}
2003-10-22 00:49:56 +08:00
} else {
if (r->http_version > NGX_HTTP_VERSION_10) {
return NGX_HTTP_PARSE_NO_HOST_HEADER;
}
r->headers_in.host_name_len = 0;
}
if (r->headers_in.content_length) {
r->headers_in.content_length_n =
ngx_atoi(r->headers_in.content_length->value.data,
r->headers_in.content_length->value.len);
if (r->headers_in.content_length_n == NGX_ERROR) {
return NGX_HTTP_PARSE_INVALID_CL_HEADER;
}
}
2004-03-30 14:27:36 +08:00
if (r->method == NGX_HTTP_POST && r->headers_in.content_length_n <= 0) {
return NGX_HTTP_PARSE_POST_WO_CL_HEADER;
}
2004-07-17 01:11:43 +08:00
if (r->plain_http) {
return NGX_HTTP_PARSE_HTTP_TO_HTTPS;
}
2003-10-22 00:49:56 +08:00
if (r->headers_in.connection) {
if (r->headers_in.connection->value.len == 5
&& ngx_strcasecmp(r->headers_in.connection->value.data, "close")
== 0)
{
r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
} else if (r->headers_in.connection->value.len == 10
&& ngx_strcasecmp(r->headers_in.connection->value.data,
"keep-alive") == 0)
{
r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
if (r->headers_in.keep_alive) {
r->headers_in.keep_alive_n =
ngx_atoi(r->headers_in.keep_alive->value.data,
r->headers_in.keep_alive->value.len);
}
}
}
2004-06-16 23:32:11 +08:00
if (r->headers_in.user_agent) {
/*
* check some widespread browsers while the headers are still
* in CPU cache
*/
2004-06-18 01:18:53 +08:00
ua = (u_char *) ngx_strstr(r->headers_in.user_agent->value.data,
"MSIE");
2004-06-16 23:32:11 +08:00
if (ua
&& ua + 8 < r->headers_in.user_agent->value.data
+ r->headers_in.user_agent->value.len)
{
r->headers_in.msie = 1;
if (ua[4] == ' ' && ua[5] == '4' && ua[6] == '.') {
r->headers_in.msie4 = 1;
}
}
}
2003-10-22 00:49:56 +08:00
return NGX_OK;
}
2003-10-10 23:10:50 +08:00
void ngx_http_finalize_request(ngx_http_request_t *r, int rc)
2002-12-11 02:05:12 +08:00
{
2003-12-15 04:10:27 +08:00
ngx_http_core_loc_conf_t *clcf;
2003-10-27 16:53:49 +08:00
/* r can be already destroyed when rc == NGX_DONE */
2002-12-11 02:05:12 +08:00
2003-10-27 16:53:49 +08:00
if (rc == NGX_DONE || r->main) {
2003-10-09 15:00:45 +08:00
return;
}
2004-03-19 13:25:53 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http finalize request: %d", rc);
2003-10-27 16:53:49 +08:00
2003-03-21 00:09:44 +08:00
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
2002-12-11 02:05:12 +08:00
2003-10-13 00:49:16 +08:00
if (r->connection->read->timer_set) {
ngx_del_timer(r->connection->read);
2003-05-15 23:42:53 +08:00
}
2003-10-13 00:49:16 +08:00
if (r->connection->write->timer_set) {
ngx_del_timer(r->connection->write);
2003-03-21 00:09:44 +08:00
}
2003-03-04 14:33:48 +08:00
2004-03-23 14:01:52 +08:00
if (rc == NGX_HTTP_CLIENT_CLOSED_REQUEST || r->closed) {
ngx_http_close_request(r, 0);
ngx_http_close_connection(r->connection);
return;
}
2003-10-10 23:10:50 +08:00
ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
2003-06-11 23:28:34 +08:00
2003-10-09 15:00:45 +08:00
return;
2003-06-11 23:28:34 +08:00
} else if (rc == NGX_ERROR) {
2003-10-10 23:10:50 +08:00
ngx_http_close_request(r, 0);
ngx_http_close_connection(r->connection);
return;
2003-10-09 15:00:45 +08:00
2003-10-10 23:10:50 +08:00
} else if (rc == NGX_AGAIN) {
ngx_http_set_write_handler(r);
return;
2003-03-21 00:09:44 +08:00
}
2002-12-11 02:05:12 +08:00
2003-10-13 00:49:16 +08:00
if (r->connection->read->timer_set) {
ngx_del_timer(r->connection->read);
2003-05-14 00:02:32 +08:00
}
2002-12-11 02:05:12 +08:00
2003-10-13 00:49:16 +08:00
if (r->connection->write->timer_set) {
2004-06-25 22:42:03 +08:00
r->connection->write->delayed = 0;
2003-10-13 00:49:16 +08:00
ngx_del_timer(r->connection->write);
2003-05-14 00:02:32 +08:00
}
2002-12-11 02:05:12 +08:00
2004-07-02 23:54:34 +08:00
if (r->connection->read->pending_eof) {
2004-01-06 04:55:48 +08:00
#if (NGX_KQUEUE)
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log,
r->connection->read->kq_errno,
2004-02-09 15:46:43 +08:00
"kevent() reported about an closed connection");
2004-01-06 04:55:48 +08:00
#endif
ngx_http_close_request(r, 0);
ngx_http_close_connection(r->connection);
return;
}
2003-12-15 04:10:27 +08:00
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2004-01-16 14:15:48 +08:00
if (!ngx_terminate
2004-04-26 04:13:21 +08:00
&& !ngx_exiting
2004-01-16 14:15:48 +08:00
&& r->keepalive != 0
&& clcf->keepalive_timeout > 0)
{
2003-05-14 00:02:32 +08:00
ngx_http_set_keepalive(r);
2004-01-16 14:15:48 +08:00
return;
2002-12-11 02:05:12 +08:00
2003-12-15 04:10:27 +08:00
} else if (r->lingering_close && clcf->lingering_timeout > 0) {
2003-05-14 00:02:32 +08:00
ngx_http_set_lingering_close(r);
2004-01-16 14:15:48 +08:00
return;
2003-03-21 00:09:44 +08:00
}
2003-10-09 15:00:45 +08:00
2004-01-16 14:15:48 +08:00
ngx_http_close_request(r, 0);
ngx_http_close_connection(r->connection);
2003-05-14 00:02:32 +08:00
}
2003-03-21 00:09:44 +08:00
2003-05-14 00:02:32 +08:00
2003-10-10 23:10:50 +08:00
static void ngx_http_set_write_handler(ngx_http_request_t *r)
2003-05-14 00:02:32 +08:00
{
ngx_event_t *wev;
2003-05-20 00:39:14 +08:00
ngx_http_core_loc_conf_t *clcf;
2003-03-21 00:09:44 +08:00
2003-04-18 01:59:35 +08:00
wev = r->connection->write;
wev->event_handler = ngx_http_writer;
2004-06-25 22:42:03 +08:00
r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
2004-06-21 23:59:32 +08:00
if (wev->ready && wev->delayed) {
2003-05-14 00:02:32 +08:00
return;
2003-04-18 01:59:35 +08:00
}
2003-05-27 20:18:54 +08:00
clcf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
ngx_http_core_module);
2004-06-21 23:59:32 +08:00
if (!wev->delayed) {
2004-06-21 03:54:15 +08:00
ngx_add_timer(wev, clcf->send_timeout);
}
2003-02-07 01:21:13 +08:00
2003-11-19 00:49:00 +08:00
wev->available = clcf->send_lowat;
if (ngx_handle_write_event(wev, NGX_LOWAT_EVENT) == NGX_ERROR) {
2003-10-10 23:10:50 +08:00
ngx_http_close_request(r, 0);
ngx_http_close_connection(r->connection);
}
2003-10-09 15:00:45 +08:00
return;
2002-12-11 02:05:12 +08:00
}
2003-10-10 23:10:50 +08:00
void ngx_http_writer(ngx_event_t *wev)
2002-12-15 14:25:09 +08:00
{
2003-03-21 00:09:44 +08:00
int rc;
2002-12-15 14:25:09 +08:00
ngx_connection_t *c;
ngx_http_request_t *r;
2003-05-27 20:18:54 +08:00
ngx_http_core_loc_conf_t *clcf;
2002-12-15 14:25:09 +08:00
2003-12-15 04:10:27 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http writer handler");
2003-11-20 00:26:41 +08:00
2003-05-27 20:18:54 +08:00
c = wev->data;
r = c->data;
2002-12-15 14:25:09 +08:00
2003-10-27 16:53:49 +08:00
if (wev->timedout) {
2004-06-21 23:59:32 +08:00
if (!wev->delayed) {
2004-06-21 03:54:15 +08:00
ngx_http_client_error(r, 0, NGX_HTTP_REQUEST_TIME_OUT);
return;
}
wev->timedout = 0;
2004-06-21 23:59:32 +08:00
wev->delayed = 0;
2004-06-21 03:54:15 +08:00
if (!wev->ready) {
clcf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
ngx_http_core_module);
ngx_add_timer(wev, clcf->send_timeout);
wev->available = clcf->send_lowat;
if (ngx_handle_write_event(wev, NGX_LOWAT_EVENT) == NGX_ERROR) {
ngx_http_close_request(r, 0);
ngx_http_close_connection(r->connection);
}
return;
}
} else {
2004-06-21 23:59:32 +08:00
if (wev->delayed) {
2004-06-21 03:54:15 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
"http writer delayed");
2004-06-21 23:59:32 +08:00
clcf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
ngx_http_core_module);
wev->available = clcf->send_lowat;
if (ngx_handle_write_event(wev, NGX_LOWAT_EVENT) == NGX_ERROR) {
ngx_http_close_request(r, 0);
ngx_http_close_connection(r->connection);
}
2004-06-21 03:54:15 +08:00
return;
}
2003-10-27 16:53:49 +08:00
}
2002-12-15 14:25:09 +08:00
rc = ngx_http_output_filter(r, NULL);
2003-12-15 04:10:27 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http writer output filter: %d", rc);
2002-12-15 14:25:09 +08:00
if (rc == NGX_AGAIN) {
2004-06-21 03:54:15 +08:00
clcf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
ngx_http_core_module);
2004-06-21 23:59:32 +08:00
if (!wev->ready && !wev->delayed) {
2003-10-13 00:49:16 +08:00
ngx_add_timer(wev, clcf->send_timeout);
2002-12-15 14:25:09 +08:00
}
2004-06-21 03:54:15 +08:00
wev->available = clcf->send_lowat;
if (ngx_handle_write_event(wev, NGX_LOWAT_EVENT) == NGX_ERROR) {
2003-10-13 00:49:16 +08:00
ngx_http_close_request(r, 0);
ngx_http_close_connection(r->connection);
}
2002-12-15 14:25:09 +08:00
2003-05-14 00:02:32 +08:00
return;
}
2002-12-15 14:25:09 +08:00
2003-12-15 04:10:27 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http writer done");
2002-12-15 14:25:09 +08:00
2003-10-13 00:49:16 +08:00
ngx_http_finalize_request(r, rc);
2003-05-12 23:52:24 +08:00
}
2003-05-15 23:42:53 +08:00
static void ngx_http_block_read(ngx_event_t *rev)
2002-12-11 02:05:12 +08:00
{
2003-05-15 23:42:53 +08:00
ngx_connection_t *c;
ngx_http_request_t *r;
2003-12-15 04:10:27 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http read blocked");
2002-12-11 02:05:12 +08:00
2003-02-07 01:21:13 +08:00
/* aio does not call this handler */
2003-10-13 00:49:16 +08:00
if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
2003-05-15 23:42:53 +08:00
if (ngx_del_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
2003-10-13 00:49:16 +08:00
c = rev->data;
r = c->data;
2003-05-15 23:42:53 +08:00
ngx_http_close_request(r, 0);
ngx_http_close_connection(c);
}
2003-02-07 01:21:13 +08:00
}
2003-05-14 00:02:32 +08:00
}
2003-05-15 23:42:53 +08:00
2004-06-16 23:32:11 +08:00
ngx_int_t ngx_http_discard_body(ngx_http_request_t *r)
2002-09-02 22:48:24 +08:00
{
2003-05-15 23:42:53 +08:00
ssize_t size;
ngx_event_t *rev;
2003-01-27 05:08:14 +08:00
2003-05-15 23:42:53 +08:00
rev = r->connection->read;
2003-01-27 05:08:14 +08:00
2003-12-15 04:10:27 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http set discard body");
2003-05-15 23:42:53 +08:00
if (rev->timer_set) {
ngx_del_timer(rev);
2003-01-27 05:08:14 +08:00
}
2002-08-30 00:59:54 +08:00
2003-10-13 00:49:16 +08:00
if (r->headers_in.content_length_n <= 0) {
return NGX_OK;
}
2003-05-15 23:42:53 +08:00
2003-10-13 00:49:16 +08:00
size = r->header_in->last - r->header_in->pos;
2003-05-15 23:42:53 +08:00
2003-10-13 00:49:16 +08:00
if (size) {
if (r->headers_in.content_length_n > size) {
r->headers_in.content_length_n -= size;
2003-05-15 23:42:53 +08:00
2003-10-13 00:49:16 +08:00
} else {
r->header_in->pos += r->headers_in.content_length_n;
r->headers_in.content_length_n = 0;
return NGX_OK;
2003-05-15 23:42:53 +08:00
}
2003-10-13 00:49:16 +08:00
}
2003-05-23 19:53:01 +08:00
2003-10-13 00:49:16 +08:00
rev->event_handler = ngx_http_read_discarded_body_event;
2003-05-15 23:42:53 +08:00
2003-10-13 00:49:16 +08:00
if (ngx_handle_level_read_event(rev) == NGX_ERROR) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
2003-02-07 01:21:13 +08:00
}
2002-12-15 14:25:09 +08:00
2003-10-13 00:49:16 +08:00
return ngx_http_read_discarded_body(r);
}
2002-12-11 02:05:12 +08:00
2003-05-15 23:42:53 +08:00
static void ngx_http_read_discarded_body_event(ngx_event_t *rev)
2002-08-26 23:18:19 +08:00
{
2003-05-15 23:42:53 +08:00
int rc;
ngx_connection_t *c;
ngx_http_request_t *r;
2002-09-02 22:48:24 +08:00
2003-05-27 20:18:54 +08:00
c = rev->data;
r = c->data;
2002-09-02 22:48:24 +08:00
2003-05-15 23:42:53 +08:00
rc = ngx_http_read_discarded_body(r);
2003-10-13 00:49:16 +08:00
if (rc == NGX_AGAIN) {
if (ngx_handle_level_read_event(rev) == NGX_ERROR) {
ngx_http_close_request(r, rc);
ngx_http_close_connection(c);
return;
}
}
2003-05-15 23:42:53 +08:00
if (rc != NGX_OK) {
ngx_http_close_request(r, rc);
ngx_http_close_connection(c);
2003-03-21 00:09:44 +08:00
}
2003-05-15 23:42:53 +08:00
}
2002-09-02 22:48:24 +08:00
2003-05-15 23:42:53 +08:00
2004-06-16 23:32:11 +08:00
static ngx_int_t ngx_http_read_discarded_body(ngx_http_request_t *r)
2003-05-15 23:42:53 +08:00
{
2003-06-12 13:54:39 +08:00
ssize_t size, n;
2003-05-27 20:18:54 +08:00
ngx_http_core_loc_conf_t *clcf;
2003-05-15 23:42:53 +08:00
2004-01-26 16:52:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http read discarded body");
2003-01-09 13:36:00 +08:00
2003-10-13 00:49:16 +08:00
if (r->headers_in.content_length_n == 0) {
return NGX_OK;
}
2003-05-27 20:18:54 +08:00
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2003-01-09 13:36:00 +08:00
2003-03-21 00:09:44 +08:00
if (r->discarded_buffer == NULL) {
2003-05-27 20:18:54 +08:00
r->discarded_buffer = ngx_palloc(r->pool, clcf->discarded_buffer_size);
2003-05-15 23:42:53 +08:00
if (r->discarded_buffer == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
2003-03-21 00:09:44 +08:00
}
2002-09-02 22:48:24 +08:00
2003-04-25 22:43:13 +08:00
size = r->headers_in.content_length_n;
2003-10-13 00:49:16 +08:00
2004-03-16 15:10:12 +08:00
if (size > (ssize_t) clcf->discarded_buffer_size) {
size = (ssize_t) clcf->discarded_buffer_size;
2003-03-21 00:09:44 +08:00
}
2002-09-02 22:48:24 +08:00
2003-06-11 23:28:34 +08:00
n = ngx_recv(r->connection, r->discarded_buffer, size);
2003-03-21 00:09:44 +08:00
if (n == NGX_ERROR) {
2004-03-23 14:01:52 +08:00
r->closed = 1;
/*
* when a client request body is discarded then we already set
* some HTTP response code for client and we can ignore the error
*/
return NGX_OK;
2003-03-21 00:09:44 +08:00
}
2002-09-02 22:48:24 +08:00
2003-03-21 00:09:44 +08:00
if (n == NGX_AGAIN) {
2003-10-13 00:49:16 +08:00
return NGX_AGAIN;
2003-03-21 00:09:44 +08:00
}
2002-09-12 22:42:29 +08:00
2003-04-25 22:43:13 +08:00
r->headers_in.content_length_n -= n;
2003-05-15 23:42:53 +08:00
2002-09-12 22:42:29 +08:00
return NGX_OK;
2002-09-02 22:48:24 +08:00
}
2002-12-11 02:05:12 +08:00
2003-05-14 00:02:32 +08:00
static void ngx_http_set_keepalive(ngx_http_request_t *r)
2003-02-11 15:14:40 +08:00
{
2003-10-13 00:49:16 +08:00
int len;
ngx_buf_t *b;
2003-05-23 19:53:01 +08:00
ngx_event_t *rev, *wev;
ngx_connection_t *c;
ngx_http_log_ctx_t *ctx;
ngx_http_core_srv_conf_t *cscf;
ngx_http_core_loc_conf_t *clcf;
2003-02-11 15:14:40 +08:00
2003-05-27 20:18:54 +08:00
c = r->connection;
2003-03-05 14:37:42 +08:00
rev = c->read;
2003-02-11 15:14:40 +08:00
2004-01-26 16:52:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
2003-05-22 23:23:47 +08:00
2003-03-12 04:38:13 +08:00
ctx = (ngx_http_log_ctx_t *) c->log->data;
ctx->action = "closing request";
2003-03-21 00:09:44 +08:00
ngx_http_close_request(r, 0);
2003-03-12 04:38:13 +08:00
2003-05-27 20:18:54 +08:00
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2003-05-21 21:28:21 +08:00
ngx_add_timer(rev, clcf->keepalive_timeout);
2003-10-13 00:49:16 +08:00
if (ngx_handle_level_read_event(rev) == NGX_ERROR) {
ngx_http_close_connection(c);
return;
2003-03-12 04:38:13 +08:00
}
2003-02-11 15:14:40 +08:00
b = c->buffer;
2004-02-13 04:57:10 +08:00
wev = c->write;
wev->event_handler = ngx_http_empty_handler;
2003-03-05 14:37:42 +08:00
2004-03-30 14:27:36 +08:00
if (b->pos < b->last) {
2003-03-05 14:37:42 +08:00
/*
2004-03-30 14:27:36 +08:00
* The pipelined request.
2003-10-13 00:49:16 +08:00
*
2004-01-19 05:09:21 +08:00
* We do not know here whether the pipelined request is complete
2003-10-13 00:49:16 +08:00
* so if the large client headers are not enabled
* we need to copy the data to the start of c->buffer.
* This copy should be rare because clients that support
* pipelined requests (Mozilla 1.x, Opera 6.x+) are still rare.
*/
2003-03-05 14:37:42 +08:00
2003-05-27 20:18:54 +08:00
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2003-05-20 00:39:14 +08:00
2003-05-23 19:53:01 +08:00
if (!cscf->large_client_header) {
len = b->last - b->pos;
ngx_memcpy(b->start, b->pos, len);
b->pos = b->start;
b->last = b->start + len;
2003-03-05 14:37:42 +08:00
}
2004-01-26 16:52:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
2003-05-27 20:18:54 +08:00
2003-03-05 14:37:42 +08:00
c->pipeline = 1;
ctx->action = "reading client pipelined request line";
2003-05-14 00:02:32 +08:00
ngx_http_init_request(rev);
return;
2003-03-05 14:37:42 +08:00
}
c->pipeline = 0;
b->pos = b->last = b->start;
2003-03-05 14:37:42 +08:00
rev->event_handler = ngx_http_keepalive_handler;
2003-11-21 14:30:49 +08:00
if (wev->active) {
if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_DISABLE_EVENT)
== NGX_ERROR)
{
ngx_http_close_connection(c);
return;
}
} else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
2003-03-05 14:37:42 +08:00
}
}
2003-03-04 14:33:48 +08:00
ctx->action = "keepalive";
2003-06-05 01:28:33 +08:00
2004-06-01 14:04:46 +08:00
if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
2003-06-05 01:28:33 +08:00
if (ngx_tcp_push(c->fd) == NGX_ERROR) {
2004-04-28 14:14:50 +08:00
ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
ngx_http_close_connection(c);
return;
2003-06-05 01:28:33 +08:00
}
2004-06-01 14:04:46 +08:00
c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
2003-06-05 01:28:33 +08:00
}
2003-03-04 14:33:48 +08:00
2004-06-24 15:53:37 +08:00
r->http_state = NGX_HTTP_KEEPALIVE_STATE;
2003-10-29 16:30:44 +08:00
if (rev->ready) {
2003-05-14 00:02:32 +08:00
ngx_http_keepalive_handler(rev);
2003-03-12 04:38:13 +08:00
}
2003-02-11 15:14:40 +08:00
}
2003-05-14 00:02:32 +08:00
static void ngx_http_keepalive_handler(ngx_event_t *rev)
2002-08-26 23:18:19 +08:00
{
2003-10-13 00:49:16 +08:00
ssize_t n;
2002-12-15 14:25:09 +08:00
ngx_connection_t *c;
2003-11-20 00:26:41 +08:00
ngx_http_log_ctx_t *ctx;
2002-08-26 23:18:19 +08:00
2004-07-16 14:33:35 +08:00
c = rev->data;
2002-08-26 23:18:19 +08:00
2004-01-26 16:52:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
if (rev->timedout) {
2003-05-14 00:02:32 +08:00
ngx_http_close_connection(c);
return;
2003-03-13 01:32:22 +08:00
}
2002-08-26 23:18:19 +08:00
2003-10-13 00:49:16 +08:00
/*
* MSIE closes a keepalive connection with RST flag
* so we ignore ECONNRESET here.
*/
2003-03-12 04:38:13 +08:00
2004-02-11 00:23:38 +08:00
c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
2003-03-12 04:38:13 +08:00
ngx_set_socket_errno(0);
2003-06-11 23:28:34 +08:00
n = ngx_recv(c, c->buffer->last, c->buffer->end - c->buffer->last);
2004-02-11 00:23:38 +08:00
c->log_error = NGX_ERROR_INFO;
2002-08-26 23:18:19 +08:00
2003-05-14 00:02:32 +08:00
if (n == NGX_AGAIN) {
return;
}
if (n == NGX_ERROR) {
ngx_http_close_connection(c);
return;
2003-03-13 01:32:22 +08:00
}
2002-08-26 23:18:19 +08:00
2003-11-20 00:26:41 +08:00
ctx = (ngx_http_log_ctx_t *) rev->log->data;
2003-03-21 00:09:44 +08:00
rev->log->handler = NULL;
2002-09-16 23:01:44 +08:00
2002-12-15 14:25:09 +08:00
if (n == 0) {
2003-03-21 00:09:44 +08:00
ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
2003-11-20 00:26:41 +08:00
"client %s closed keepalive connection", ctx->client);
2003-05-14 00:02:32 +08:00
ngx_http_close_connection(c);
return;
2002-09-12 22:42:29 +08:00
}
2002-08-26 23:18:19 +08:00
2003-03-12 04:38:13 +08:00
c->buffer->last += n;
2003-03-21 00:09:44 +08:00
rev->log->handler = ngx_http_log_error;
2003-11-20 00:26:41 +08:00
ctx->action = "reading client request line";
2002-09-16 23:01:44 +08:00
2003-05-14 00:02:32 +08:00
ngx_http_init_request(rev);
2002-09-16 23:01:44 +08:00
}
2002-12-11 02:05:12 +08:00
2003-05-14 00:02:32 +08:00
static void ngx_http_set_lingering_close(ngx_http_request_t *r)
2002-09-16 23:01:44 +08:00
{
2003-10-13 00:49:16 +08:00
ngx_event_t *rev, *wev;
2003-03-12 04:38:13 +08:00
ngx_connection_t *c;
2003-05-27 20:18:54 +08:00
ngx_http_core_loc_conf_t *clcf;
2003-01-09 13:36:00 +08:00
2003-02-11 15:14:40 +08:00
c = r->connection;
2003-01-27 05:08:14 +08:00
2003-05-27 20:18:54 +08:00
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2003-01-09 13:36:00 +08:00
2003-10-13 00:49:16 +08:00
rev = c->read;
2003-05-14 00:02:32 +08:00
rev->event_handler = ngx_http_lingering_close_handler;
2002-09-16 23:01:44 +08:00
2004-06-28 02:01:57 +08:00
r->lingering_time = ngx_time() + clcf->lingering_time / 1000;
2003-10-13 00:49:16 +08:00
ngx_add_timer(rev, clcf->lingering_timeout);
if (ngx_handle_level_read_event(rev) == NGX_ERROR) {
ngx_http_close_request(r, 0);
ngx_http_close_connection(c);
return;
2003-01-27 05:08:14 +08:00
}
2003-10-13 00:49:16 +08:00
wev = c->write;
wev->event_handler = ngx_http_empty_handler;
2002-09-16 23:01:44 +08:00
2003-11-21 14:30:49 +08:00
if (wev->active) {
if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_DISABLE_EVENT)
== NGX_ERROR)
{
ngx_http_close_connection(c);
return;
}
} else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
2002-12-24 15:09:57 +08:00
}
2003-03-12 04:38:13 +08:00
}
if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
2004-04-28 14:14:50 +08:00
ngx_connection_error(c, ngx_socket_errno,
ngx_shutdown_socket_n " failed");
2003-05-14 00:02:32 +08:00
ngx_http_close_request(r, 0);
ngx_http_close_connection(c);
return;
2003-02-11 15:14:40 +08:00
}
2003-10-29 16:30:44 +08:00
if (rev->ready) {
2003-05-14 00:02:32 +08:00
ngx_http_lingering_close_handler(rev);
2003-02-11 15:14:40 +08:00
}
2002-08-26 23:18:19 +08:00
}
2003-05-14 00:02:32 +08:00
static void ngx_http_lingering_close_handler(ngx_event_t *rev)
2002-08-26 23:18:19 +08:00
{
2003-03-21 00:09:44 +08:00
ssize_t n;
ngx_msec_t timer;
ngx_connection_t *c;
ngx_http_request_t *r;
2003-05-27 20:18:54 +08:00
ngx_http_core_loc_conf_t *clcf;
2002-09-13 22:47:42 +08:00
2003-05-27 20:18:54 +08:00
c = rev->data;
r = c->data;
2003-01-30 15:28:09 +08:00
2004-01-26 16:52:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http lingering close handler");
2003-03-21 00:09:44 +08:00
if (rev->timedout) {
2003-05-14 00:02:32 +08:00
ngx_http_close_request(r, 0);
ngx_http_close_connection(c);
return;
2002-12-24 15:09:57 +08:00
}
2002-09-13 22:47:42 +08:00
2004-06-28 02:01:57 +08:00
timer = r->lingering_time - ngx_time();
2002-12-24 15:09:57 +08:00
if (timer <= 0) {
2003-05-14 00:02:32 +08:00
ngx_http_close_request(r, 0);
ngx_http_close_connection(c);
return;
2002-12-24 15:09:57 +08:00
}
2002-09-13 22:47:42 +08:00
2003-05-27 20:18:54 +08:00
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2003-01-09 13:36:00 +08:00
2002-09-16 23:01:44 +08:00
if (r->discarded_buffer == NULL) {
2003-03-12 04:38:13 +08:00
/* TODO: r->header_in->start (if large headers are enabled)
or the end of parsed header (otherwise)
instead of r->header_in->last */
2003-06-12 13:54:39 +08:00
if (r->header_in->end - r->header_in->last
2004-03-16 15:10:12 +08:00
>= (ssize_t) clcf->discarded_buffer_size)
{
2003-03-12 04:38:13 +08:00
r->discarded_buffer = r->header_in->last;
2002-09-16 23:01:44 +08:00
} else {
2003-05-14 00:02:32 +08:00
r->discarded_buffer = ngx_palloc(c->pool,
2003-05-27 20:18:54 +08:00
clcf->discarded_buffer_size);
2003-05-14 00:02:32 +08:00
if (r->discarded_buffer) {
ngx_http_close_request(r, 0);
ngx_http_close_connection(c);
return;
}
2002-09-16 23:01:44 +08:00
}
}
2002-09-13 22:47:42 +08:00
2003-03-21 00:09:44 +08:00
do {
2003-06-11 23:28:34 +08:00
n = ngx_recv(c, r->discarded_buffer, clcf->discarded_buffer_size);
2002-09-13 22:47:42 +08:00
2004-01-26 16:52:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
2002-09-13 22:47:42 +08:00
2003-03-21 00:09:44 +08:00
if (n == NGX_ERROR || n == 0) {
2003-05-14 00:02:32 +08:00
ngx_http_close_request(r, 0);
ngx_http_close_connection(c);
return;
2003-03-21 00:09:44 +08:00
}
} while (rev->ready);
2002-09-13 22:47:42 +08:00
2002-09-16 23:01:44 +08:00
timer *= 1000;
2003-05-27 20:18:54 +08:00
if (timer > clcf->lingering_timeout) {
timer = clcf->lingering_timeout;
2002-12-24 15:09:57 +08:00
}
2002-09-13 22:47:42 +08:00
2003-03-21 00:09:44 +08:00
ngx_add_timer(rev, timer);
2002-09-13 22:47:42 +08:00
2003-05-14 00:02:32 +08:00
return;
2002-09-13 22:47:42 +08:00
}
2002-08-20 22:48:28 +08:00
2003-11-20 00:26:41 +08:00
void ngx_http_empty_handler(ngx_event_t *wev)
2003-05-22 23:23:47 +08:00
{
2004-01-26 16:52:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
2003-05-22 23:23:47 +08:00
return;
}
2004-06-16 23:32:11 +08:00
ngx_int_t ngx_http_send_last(ngx_http_request_t *r)
2003-10-09 15:00:45 +08:00
{
ngx_buf_t *b;
2003-10-22 00:49:56 +08:00
ngx_chain_t out;
2003-10-09 15:00:45 +08:00
if (!(b = ngx_calloc_buf(r->pool))) {
return NGX_ERROR;
}
b->last_buf = 1;
out.buf = b;
2003-10-22 00:49:56 +08:00
out.next = NULL;
2003-10-09 15:00:45 +08:00
2003-10-22 00:49:56 +08:00
return ngx_http_output_filter(r, &out);
2003-10-09 15:00:45 +08:00
}
2003-05-14 00:02:32 +08:00
void ngx_http_close_request(ngx_http_request_t *r, int error)
{
2004-06-07 03:49:18 +08:00
ngx_uint_t i;
ngx_log_t *log;
ngx_http_log_ctx_t *ctx;
ngx_http_cleanup_t *cleanup;
ngx_http_core_loc_conf_t *clcf;
struct linger l;
2003-05-14 00:02:32 +08:00
2003-12-01 04:03:18 +08:00
log = r->connection->log;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
2003-05-14 00:02:32 +08:00
if (r->pool == NULL) {
2003-12-01 04:03:18 +08:00
ngx_log_error(NGX_LOG_ALERT, log, 0,
2003-05-15 23:42:53 +08:00
"http request already closed");
2003-05-14 00:02:32 +08:00
return;
}
2003-12-22 17:40:48 +08:00
if (error && r->headers_out.status == 0) {
2003-05-14 00:02:32 +08:00
r->headers_out.status = error;
}
ngx_http_log_handler(r);
2003-11-29 01:41:47 +08:00
cleanup = r->cleanup.elts;
for (i = 0; i < r->cleanup.nelts; i++) {
2003-12-01 04:03:18 +08:00
if (!cleanup[i].valid) {
continue;
}
2004-03-13 00:57:08 +08:00
#if (NGX_HTTP_CACHE)
2003-11-29 01:41:47 +08:00
if (cleanup[i].cache) {
ngx_http_cache_unlock(cleanup[i].data.cache.hash,
2003-12-01 04:03:18 +08:00
cleanup[i].data.cache.cache, log);
2003-11-29 01:41:47 +08:00
continue;
}
2004-03-13 00:57:08 +08:00
#endif
2003-12-01 04:03:18 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http cleanup fd: %d",
cleanup[i].data.file.fd);
2003-11-29 01:41:47 +08:00
if (ngx_close_file(cleanup[i].data.file.fd) == NGX_FILE_ERROR) {
2003-12-01 04:03:18 +08:00
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
2003-11-29 01:41:47 +08:00
ngx_close_file_n " \"%s\" failed",
cleanup[i].data.file.name);
}
}
2003-12-01 04:03:18 +08:00
/* STUB */
2003-05-14 00:02:32 +08:00
if (r->file.fd != NGX_INVALID_FILE) {
if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
2003-12-01 04:03:18 +08:00
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
2003-05-14 00:02:32 +08:00
ngx_close_file_n " \"%s\" failed", r->file.name.data);
}
}
2004-04-26 04:13:21 +08:00
if (r->request_body
&& r->request_body->temp_file
&& r->request_body->temp_file->file.fd != NGX_INVALID_FILE)
{
if (ngx_close_file(r->request_body->temp_file->file.fd)
== NGX_FILE_ERROR)
{
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
ngx_close_file_n " deleted file \"%s\" failed",
r->request_body->temp_file->file.name.data);
}
}
2004-06-07 03:49:18 +08:00
if (r->connection->timedout) {
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (clcf->reset_timedout_connection) {
l.l_onoff = 1;
l.l_linger = 0;
if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
(const void *) &l, sizeof(struct linger)) == -1)
{
ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
"setsockopt(SO_LINGER) failed");
}
}
}
2003-05-14 00:02:32 +08:00
/* ctx->url was allocated from r->pool */
2003-12-01 04:03:18 +08:00
ctx = log->data;
2003-05-14 00:02:32 +08:00
ctx->url = NULL;
2004-06-24 15:53:37 +08:00
r->request_line.len = 0;
2003-05-14 00:02:32 +08:00
ngx_destroy_pool(r->pool);
2003-10-10 23:10:50 +08:00
return;
2003-05-14 00:02:32 +08:00
}
2004-07-16 14:33:35 +08:00
#if (NGX_HTTP_SSL)
void ngx_ssl_close_handler(ngx_event_t *ev)
{
ngx_connection_t *c;
c = ev->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0, "http ssl close handler");
if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
return;
}
ngx_http_close_connection(c);
}
#endif
2003-05-14 00:02:32 +08:00
void ngx_http_close_connection(ngx_connection_t *c)
2003-05-12 23:52:24 +08:00
{
2004-06-29 05:03:14 +08:00
ngx_socket_t fd;
2004-01-26 16:52:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
2004-02-12 01:08:49 +08:00
"close http connection: %d", c->fd);
2003-05-12 23:52:24 +08:00
2003-05-14 00:02:32 +08:00
if (c->pool == NULL) {
2003-05-12 23:52:24 +08:00
ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed");
return;
}
2004-07-16 14:33:35 +08:00
#if (NGX_HTTP_SSL)
if (c->ssl) {
if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
c->read->event_handler = ngx_ssl_close_handler;
c->write->event_handler = ngx_ssl_close_handler;
return;
}
}
#endif
2003-05-12 23:52:24 +08:00
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
2003-05-27 20:18:54 +08:00
if (ngx_del_conn) {
2004-02-18 05:11:27 +08:00
ngx_del_conn(c, NGX_CLOSE_EVENT);
2003-05-22 23:23:47 +08:00
} else {
2004-07-02 23:54:34 +08:00
if (c->read->active || c->read->disabled) {
2003-05-22 23:23:47 +08:00
ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
}
2004-07-02 23:54:34 +08:00
if (c->write->active || c->write->disabled) {
2003-05-22 23:23:47 +08:00
ngx_del_event(c->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
}
2003-05-12 23:52:24 +08:00
}
2004-07-02 13:47:00 +08:00
/*
* we have to clean the connection information before the closing
* because another thread may reopen the same file descriptor
* before we clean the connection
*/
2003-05-12 23:52:24 +08:00
2004-07-02 13:47:00 +08:00
#if (NGX_THREADS)
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_OK) {
2004-07-02 23:54:34 +08:00
if (c->read->prev) {
ngx_delete_posted_event(c->read);
}
if (c->write->prev) {
ngx_delete_posted_event(c->write);
}
2004-07-05 23:08:23 +08:00
c->read->closed = 1;
c->write->closed = 1;
2004-07-07 14:15:04 +08:00
if (c->single_connection) {
ngx_unlock(&c->lock);
c->read->locked = 0;
c->write->locked = 0;
}
2004-07-05 23:08:23 +08:00
2004-07-02 13:47:00 +08:00
ngx_mutex_unlock(ngx_posted_events_mutex);
}
2004-07-02 23:54:34 +08:00
#else
if (c->read->prev) {
ngx_delete_posted_event(c->read);
}
if (c->write->prev) {
ngx_delete_posted_event(c->write);
}
2004-07-02 13:47:00 +08:00
2004-07-05 23:08:23 +08:00
c->read->closed = 1;
c->write->closed = 1;
2004-07-02 13:47:00 +08:00
#endif
2004-06-29 05:03:14 +08:00
2004-07-02 23:54:34 +08:00
fd = c->fd;
c->fd = (ngx_socket_t) -1;
c->data = NULL;
ngx_destroy_pool(c->pool);
2004-06-29 05:03:14 +08:00
if (ngx_close_socket(fd) == -1) {
/* we use ngx_cycle->log because c->log was in c->pool */
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
2003-10-10 23:10:50 +08:00
return;
2002-10-05 01:58:04 +08:00
}
2003-10-27 16:53:49 +08:00
static void ngx_http_client_error(ngx_http_request_t *r,
int client_error, int error)
2003-03-05 14:37:42 +08:00
{
2004-05-15 00:51:47 +08:00
ngx_http_log_ctx_t *ctx;
ngx_http_core_srv_conf_t *cscf;
2003-03-05 14:37:42 +08:00
ctx = r->connection->log->data;
2003-10-27 16:53:49 +08:00
if (error == NGX_HTTP_REQUEST_TIME_OUT) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, NGX_ETIMEDOUT,
"client timed out");
2004-06-07 03:49:18 +08:00
r->connection->timedout = 1;
2003-10-27 16:53:49 +08:00
ngx_http_close_request(r, error);
ngx_http_close_connection(r->connection);
return;
}
2003-03-05 14:37:42 +08:00
r->connection->log->handler = NULL;
if (ctx->url) {
2004-07-17 01:11:43 +08:00
switch (client_error) {
case NGX_HTTP_PARSE_INVALID_HOST:
2004-05-15 00:51:47 +08:00
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
client_header_errors[client_error - NGX_HTTP_CLIENT_ERROR],
ctx->client, r->headers_in.host->value.data, ctx->url);
error = NGX_HTTP_INVALID_HOST;
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
if (cscf->restrict_host_names == NGX_HTTP_RESTRICT_HOST_CLOSE) {
ngx_http_close_request(r, error);
ngx_http_close_connection(r->connection);
return;
}
2004-07-17 01:11:43 +08:00
break;
case NGX_HTTP_PARSE_HTTP_TO_HTTPS:
error = NGX_HTTP_TO_HTTPS;
/* fall through */
default:
2004-05-15 00:51:47 +08:00
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2003-12-26 04:26:58 +08:00
client_header_errors[client_error - NGX_HTTP_CLIENT_ERROR],
ctx->client, ctx->url);
2004-05-15 00:51:47 +08:00
}
2003-03-05 14:37:42 +08:00
} else {
2004-01-08 16:47:17 +08:00
if (error == NGX_HTTP_REQUEST_URI_TOO_LARGE) {
r->request_line.len = r->header_in->end - r->request_start;
r->request_line.data = r->request_start;
}
2003-03-05 14:37:42 +08:00
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2003-12-26 04:26:58 +08:00
client_header_errors[client_error - NGX_HTTP_CLIENT_ERROR],
ctx->client);
2003-03-05 14:37:42 +08:00
}
r->connection->log->handler = ngx_http_log_error;
2003-10-13 00:49:16 +08:00
ngx_http_finalize_request(r, error);
2003-03-05 14:37:42 +08:00
}
2002-08-26 23:18:19 +08:00
static size_t ngx_http_log_error(void *data, char *buf, size_t len)
{
2003-11-20 00:26:41 +08:00
ngx_http_log_ctx_t *ctx = data;
2002-09-02 22:48:24 +08:00
2003-06-02 23:24:30 +08:00
if (ctx->action && ctx->url) {
2002-08-26 23:18:19 +08:00
return ngx_snprintf(buf, len, " while %s, client: %s, URL: %s",
ctx->action, ctx->client, ctx->url);
2003-06-02 23:24:30 +08:00
} else if (ctx->action == NULL && ctx->url) {
return ngx_snprintf(buf, len, ", client: %s, URL: %s",
ctx->client, ctx->url);
2003-03-21 00:09:44 +08:00
} else {
2002-08-26 23:18:19 +08:00
return ngx_snprintf(buf, len, " while %s, client: %s",
ctx->action, ctx->client);
2003-03-21 00:09:44 +08:00
}
}