nginx/src/http/ngx_http_event.c

1159 lines
31 KiB
C
Raw Normal View History

#include <ngx_config.h>
2002-08-26 23:18:19 +08:00
#include <ngx_core.h>
#include <ngx_string.h>
2002-09-02 22:48:24 +08:00
#include <ngx_files.h>
#include <ngx_log.h>
#include <ngx_alloc.h>
#include <ngx_array.h>
#include <ngx_table.h>
#include <ngx_hunk.h>
#include <ngx_connection.h>
2003-01-24 02:47:54 +08:00
#include <ngx_event.h>
#include <ngx_event_timer.h>
2002-12-15 14:25:09 +08:00
#include <ngx_inet.h>
#include <ngx_http.h>
2002-09-11 23:18:33 +08:00
#include <ngx_http_config.h>
2003-01-09 13:36:00 +08:00
#include <ngx_http_core_module.h>
2003-02-07 01:21:13 +08:00
#include <ngx_http_output_filter.h>
2002-09-02 22:48:24 +08:00
static int ngx_http_init_request(ngx_event_t *ev);
2003-03-21 00:09:44 +08:00
static int ngx_http_process_request(ngx_event_t *ev);
2002-08-30 00:59:54 +08:00
static int ngx_http_process_request_line(ngx_http_request_t *r);
2002-12-11 02:05:12 +08:00
static int ngx_http_process_request_headers(ngx_http_request_t *r);
2002-09-02 22:48:24 +08:00
static int ngx_http_process_request_header_line(ngx_http_request_t *r);
2003-03-21 00:09:44 +08:00
static int ngx_http_request_handler(ngx_http_request_t *r, int error);
2002-09-02 22:48:24 +08:00
static int ngx_http_writer(ngx_event_t *ev);
2002-12-15 14:25:09 +08:00
static int ngx_http_block_read(ngx_event_t *ev);
static int ngx_http_read_discarded_body(ngx_event_t *ev);
2003-02-11 15:14:40 +08:00
static int ngx_http_set_keepalive(ngx_http_request_t *r);
2002-09-13 22:47:42 +08:00
static int ngx_http_keepalive_handler(ngx_event_t *ev);
2002-12-15 14:25:09 +08:00
static int ngx_http_set_lingering_close(ngx_http_request_t *r);
static int ngx_http_lingering_close_handler(ngx_event_t *ev);
2002-09-12 22:42:29 +08:00
2002-10-05 01:58:04 +08:00
static int ngx_http_close_connection(ngx_event_t *ev);
2003-03-21 00:09:44 +08:00
static void ngx_http_header_parse_error(ngx_http_request_t *r, int parse_err);
2002-08-26 23:18:19 +08:00
static size_t ngx_http_log_error(void *data, char *buf, size_t len);
2002-08-26 23:18:19 +08:00
2002-08-30 00:59:54 +08:00
static char *header_errors[] = {
"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-03-04 14:33:48 +08:00
"client %s sent HEAD method in HTTP/0.9 request",
"client %s sent invalid header, URL: %s",
"client %s sent too long header line, URL: %s",
"client %s sent HTTP/1.1 request without \"Host\" header, URL: %s"
2002-08-30 00:59:54 +08:00
};
2003-03-04 14:33:48 +08:00
static ngx_http_header_t headers_in[] = {
2003-03-21 00:09:44 +08:00
{ ngx_string("Host"), offsetof(ngx_http_headers_in_t, host) },
{ ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection) },
{ ngx_string("If-Modified-Since"),
2003-04-09 23:42:08 +08:00
offsetof(ngx_http_headers_in_t, if_modified_since) },
2003-03-21 00:09:44 +08:00
{ ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent) },
2003-03-21 00:09:44 +08:00
{ ngx_null_string, 0 }
};
int ngx_http_init_connection(ngx_connection_t *c)
{
2003-03-21 00:09:44 +08:00
ngx_event_t *rev;
2002-08-26 23:18:19 +08:00
ngx_http_log_ctx_t *ctx;
2003-03-21 00:09:44 +08:00
rev = c->read;
rev->event_handler = ngx_http_init_request;
2002-10-05 01:58:04 +08:00
2003-03-21 00:09:44 +08:00
rev->close_handler = ngx_http_close_connection;
2002-10-05 01:58:04 +08:00
c->write->close_handler = ngx_http_close_connection;
2003-01-09 13:36:00 +08:00
ngx_test_null(c->addr_text.data, ngx_palloc(c->pool, c->addr_text_max_len),
2002-08-26 23:18:19 +08:00
NGX_ERROR);
2002-12-11 02:05:12 +08:00
2003-01-30 15:28:09 +08:00
c->addr_text.len = ngx_sock_ntop(c->family, c->sockaddr,
2003-01-09 13:36:00 +08:00
c->addr_text.data, c->addr_text_max_len);
2003-01-30 15:28:09 +08:00
if (c->addr_text.len == 0) {
return NGX_ERROR;
}
2002-08-26 23:18:19 +08:00
ngx_test_null(ctx, ngx_pcalloc(c->pool, sizeof(ngx_http_log_ctx_t)),
NGX_ERROR);
2002-12-11 02:05:12 +08:00
ctx->client = c->addr_text.data;
2002-08-26 23:18:19 +08:00
ctx->action = "reading client request line";
c->log->data = ctx;
c->log->handler = ngx_http_log_error;
#if (HAVE_DEFERRED_ACCEPT)
2003-02-07 01:21:13 +08:00
2003-03-21 00:09:44 +08:00
if (rev->ready) {
return ngx_http_init_request(rev);
2003-01-27 05:08:14 +08:00
}
2003-02-07 01:21:13 +08:00
#endif
2003-01-27 05:08:14 +08:00
2003-03-21 00:09:44 +08:00
ngx_add_timer(rev, c->post_accept_timeout);
rev->timer_set = 1;
2003-01-27 05:08:14 +08:00
2002-08-26 23:18:19 +08:00
#if (USE_KQUEUE)
2003-01-27 05:08:14 +08:00
2003-03-21 00:09:44 +08:00
return ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT);
2003-01-27 05:08:14 +08:00
#else
2003-01-27 05:08:14 +08:00
2003-03-21 00:09:44 +08:00
/* kqueue */
2003-02-07 01:21:13 +08:00
2003-01-27 05:08:14 +08:00
if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
2003-03-21 00:09:44 +08:00
return ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT);
2003-01-27 05:08:14 +08:00
}
2003-02-07 01:21:13 +08:00
2003-03-21 00:09:44 +08:00
/* aio, iocp, epoll */
2003-02-07 01:21:13 +08:00
2003-03-21 00:09:44 +08:00
if (ngx_event_flags & (NGX_HAVE_AIO_EVENT|NGX_HAVE_EDGE_EVENT)) {
return ngx_http_init_request(rev);
2002-08-30 00:59:54 +08:00
}
2003-02-07 01:21:13 +08:00
/* select, poll, /dev/poll */
2003-01-27 05:08:14 +08:00
2003-03-21 00:09:44 +08:00
return ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT);
2003-01-27 05:08:14 +08:00
#endif /* USE_KQUEUE */
}
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
static int ngx_http_init_request(ngx_event_t *rev)
{
2003-01-30 15:28:09 +08:00
ngx_connection_t *c;
ngx_http_request_t *r;
ngx_http_conf_ctx_t *ctx;
2003-03-21 00:09:44 +08:00
c = (ngx_connection_t *) rev->data;
2003-01-15 15:02:27 +08:00
c->sent = 0;
2002-08-26 23:18:19 +08:00
ngx_test_null(r, ngx_pcalloc(c->pool, sizeof(ngx_http_request_t)),
NGX_ERROR);
c->data = r;
r->connection = c;
2002-12-15 14:25:09 +08:00
r->file.fd = NGX_INVALID_FILE;
2002-09-16 23:01:44 +08:00
if (c->buffer == NULL) {
ngx_test_null(c->buffer,
2003-01-09 13:36:00 +08:00
ngx_create_temp_hunk(c->pool,
ngx_http_client_header_buffer_size,
2002-09-16 23:01:44 +08:00
0, 0),
NGX_ERROR);
} else {
r->header_read = 1;
}
2003-03-05 14:37:42 +08:00
r->pipeline = c->pipeline;
2002-09-16 23:01:44 +08:00
r->header_in = c->buffer;
2003-03-21 00:09:44 +08:00
ngx_test_null(r->pool, ngx_create_pool(ngx_http_request_pool_size, c->log),
2003-03-13 01:32:22 +08:00
NGX_ERROR);
2002-12-27 00:26:23 +08:00
ngx_test_null(r->ctx,
ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module),
2003-03-21 00:09:44 +08:00
ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR));
2002-09-11 23:18:33 +08:00
2003-01-30 15:28:09 +08:00
ctx = (ngx_http_conf_ctx_t *) c->ctx;
r->srv_conf = ctx->srv_conf;
r->loc_conf = ctx->loc_conf;
2002-12-15 14:25:09 +08:00
r->headers_out.headers = ngx_create_table(r->pool, 10);
r->headers_out.content_length = -1;
r->headers_out.last_modified_time = -1;
2003-03-21 00:09:44 +08:00
rev->event_handler = ngx_http_process_request;
2002-08-30 00:59:54 +08:00
r->state_handler = ngx_http_process_request_line;
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
return ngx_http_process_request(rev);
}
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
static int ngx_http_process_request(ngx_event_t *rev)
{
2003-03-21 00:09:44 +08:00
int n, rc;
2003-01-27 05:08:14 +08:00
ngx_connection_t *c;
ngx_http_request_t *r;
2003-03-21 00:09:44 +08:00
ngx_http_log_ctx_t *lcx;
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
c = (ngx_connection_t *) rev->data;
2002-08-26 23:18:19 +08:00
r = (ngx_http_request_t *) c->data;
2003-03-21 00:09:44 +08:00
ngx_log_debug(c->log, "http process request");
if (rev->timedout) {
return ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
}
2003-02-07 01:21:13 +08:00
do {
if (r->header_read) {
r->header_read = 0;
2003-03-21 00:09:44 +08:00
ngx_log_debug(c->log, "http preread %d" _
2003-03-12 04:38:13 +08:00
r->header_in->last - r->header_in->pos);
2003-02-07 01:21:13 +08:00
} else {
2003-03-12 04:38:13 +08:00
n = ngx_event_recv(c, r->header_in->last,
r->header_in->end - r->header_in->last);
2002-09-16 23:01:44 +08:00
2003-02-07 01:21:13 +08:00
if (n == NGX_AGAIN) {
if (!r->header_timeout_set) {
2003-01-27 05:08:14 +08:00
2003-03-21 00:09:44 +08:00
if (rev->timer_set) {
ngx_del_timer(rev);
2003-02-07 01:21:13 +08:00
} else {
2003-03-21 00:09:44 +08:00
rev->timer_set = 1;
2003-02-07 01:21:13 +08:00
}
2003-01-27 05:08:14 +08:00
2003-03-21 00:09:44 +08:00
ngx_add_timer(rev, ngx_http_client_header_timeout);
2003-02-07 01:21:13 +08:00
r->header_timeout_set = 1;
}
2003-03-21 00:09:44 +08:00
2003-02-07 01:21:13 +08:00
return NGX_AGAIN;
2002-09-16 23:01:44 +08:00
}
2003-03-12 04:38:13 +08:00
if (n == NGX_ERROR) {
2003-03-21 00:09:44 +08:00
return ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
2003-03-12 04:38:13 +08:00
}
2002-08-30 00:59:54 +08:00
2003-03-21 00:09:44 +08:00
ngx_log_debug(c->log, "http read %d" _ n);
2003-02-07 01:21:13 +08:00
if (n == 0) {
ngx_log_error(NGX_LOG_INFO, c->log, 0,
2003-03-21 00:09:44 +08:00
"client closed prematurely connection");
return ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
2003-02-07 01:21:13 +08:00
}
2003-03-12 04:38:13 +08:00
r->header_in->last += n;
2002-09-16 23:01:44 +08:00
}
2003-04-08 23:40:10 +08:00
/* the state handlers are called in the following order:
2003-02-07 01:21:13 +08:00
ngx_http_process_request_line(r)
ngx_http_process_request_headers(r) */
2003-02-07 01:21:13 +08:00
do {
2003-04-08 23:40:10 +08:00
rc = r->state_handler(r);
2003-03-12 04:38:13 +08:00
} while (rc == NGX_AGAIN && r->header_in->pos < r->header_in->last);
2003-03-21 00:09:44 +08:00
} while (rc == NGX_AGAIN
&& (rev->ready || ngx_event_flags & NGX_HAVE_AIO_EVENT));
if (rc >= NGX_OK) {
2003-03-12 04:38:13 +08:00
2003-03-21 00:09:44 +08:00
/* HTTP header done */
rev->event_handler = ngx_http_block_read;
if (rc != NGX_OK) {
return ngx_http_finalize_request(r, rc);
2003-03-12 04:38:13 +08:00
}
2003-03-21 00:09:44 +08:00
lcx = r->connection->log->data;
lcx->action = "processing client request";
2002-09-02 22:48:24 +08:00
2003-03-21 00:09:44 +08:00
rc = ngx_http_handler(r);
2003-03-12 04:38:13 +08:00
2003-04-08 23:40:10 +08:00
/* a handler does its own processing */
if (rc == NGX_DONE) {
2003-03-21 00:09:44 +08:00
return rc;
}
2003-01-25 00:09:40 +08:00
2003-03-21 00:09:44 +08:00
if (rc == NGX_ERROR) {
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
2003-01-25 00:09:40 +08:00
}
2002-08-30 00:59:54 +08:00
2003-03-21 00:09:44 +08:00
return ngx_http_finalize_request(r, rc);
2003-01-25 00:09:40 +08:00
2003-03-21 00:09:44 +08:00
}
2003-01-27 05:08:14 +08:00
2003-03-21 00:09:44 +08:00
/* NGX_AGAIN */
2003-01-27 05:08:14 +08:00
2003-03-21 00:09:44 +08:00
if (!r->header_timeout_set) {
2003-01-25 00:09:40 +08:00
2003-03-21 00:09:44 +08:00
if (rev->timer_set) {
ngx_del_timer(rev);
} else {
rev->timer_set = 1;
2003-01-25 00:09:40 +08:00
}
2003-03-21 00:09:44 +08:00
ngx_add_timer(rev, ngx_http_client_header_timeout);
r->header_timeout_set = 1;
2003-01-25 00:09:40 +08:00
}
2003-03-21 00:09:44 +08:00
return rc;
}
2002-09-13 22:47:42 +08:00
2002-08-30 00:59:54 +08:00
static int ngx_http_process_request_line(ngx_http_request_t *r)
{
2003-03-05 14:37:42 +08:00
int rc, offset;
2002-10-05 01:58:04 +08:00
ngx_connection_t *c;
2003-03-21 00:09:44 +08:00
ngx_http_log_ctx_t *lcx;
2002-08-30 00:59:54 +08:00
rc = ngx_read_http_request_line(r);
2002-10-05 01:58:04 +08:00
c = r->connection;
2003-03-06 01:30:51 +08:00
/* a request line has been parsed successfully */
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-04-08 23:40:10 +08:00
if (r->http_version >= NGX_HTTP_VERSION_10
&& ngx_http_large_client_header == 0
&& r->header_in->pos == r->header_in->end)
{
ngx_http_header_parse_error(r, NGX_HTTP_PARSE_TOO_LONG_URI);
return NGX_HTTP_REQUEST_URI_TOO_LARGE;
}
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;
}
2002-12-11 02:05:12 +08:00
ngx_test_null(r->uri.data, ngx_palloc(r->pool, r->uri.len + 1),
2003-03-21 00:09:44 +08:00
NGX_HTTP_INTERNAL_SERVER_ERROR);
2002-12-11 02:05:12 +08:00
ngx_cpystrn(r->uri.data, r->uri_start, r->uri.len + 1);
2003-03-21 00:09:44 +08:00
r->request_line.len = r->request_end - r->request_start;
2003-03-12 04:38:13 +08:00
/* if the large client headers are enabled then
2003-03-06 01:30:51 +08:00
we need to copy a request line */
2003-03-04 14:33:48 +08:00
if (ngx_http_large_client_header) {
ngx_test_null(r->request_line.data,
ngx_palloc(r->pool, r->request_line.len + 1),
2003-03-21 00:09:44 +08:00
NGX_HTTP_INTERNAL_SERVER_ERROR);
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';
}
/* copy URI extention if it exists */
2003-03-21 00:09:44 +08:00
2002-09-16 23:01:44 +08:00
if (r->uri_ext) {
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;
}
2002-12-11 02:05:12 +08:00
ngx_test_null(r->exten.data,
ngx_palloc(r->pool, r->exten.len + 1),
2003-03-21 00:09:44 +08:00
NGX_HTTP_INTERNAL_SERVER_ERROR);
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
/* copy URI arguments if they exist */
if (r->args_start && r->uri_end > r->args_start) {
r->args.len = r->uri_end - r->args_start;
ngx_test_null(r->args.data,
ngx_palloc(r->pool, r->args.len + 1),
NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_cpystrn(r->args.data, r->args_start, r->args.len + 1);
}
2003-03-05 14:37:42 +08:00
#if 1
if (r->exten.data == NULL) {
r->exten.data = "";
}
2003-03-21 00:09:44 +08:00
if (r->args.data == NULL) {
r->args.data = "";
}
ngx_log_debug(r->connection->log, "HTTP: %d, %d, '%s', '%s', '%s'" _
2002-12-11 02:05:12 +08:00
r->method _ r->http_version _
2003-03-21 00:09:44 +08:00
r->uri.data _ r->exten.data _ r->args.data);
2003-03-05 14:37:42 +08:00
if (r->exten.data[0] == '\0') {
r->exten.data = NULL;
}
2003-03-21 00:09:44 +08:00
if (r->args.data[0] == '\0') {
r->args.data = NULL;
}
2002-12-24 02:22:18 +08:00
#endif
2002-08-30 00:59:54 +08:00
2003-03-21 00:09:44 +08:00
lcx = r->connection->log->data;
2003-03-04 14:33:48 +08:00
if (ngx_http_url_in_error_log) {
2003-03-21 00:09:44 +08:00
ngx_test_null(lcx->url,
2003-03-04 14:33:48 +08:00
ngx_palloc(r->pool, r->uri_end - r->uri_start + 1),
2003-03-21 00:09:44 +08:00
NGX_HTTP_INTERNAL_SERVER_ERROR);
ngx_cpystrn(lcx->url, r->uri_start, r->uri_end - r->uri_start + 1);
2003-03-04 14:33:48 +08:00
}
2002-08-30 00:59:54 +08:00
2003-03-06 01:30:51 +08:00
/* if we need to parse the headers then return NGX_AGAIN
becuase of HTTP/0.9 has no headers so return NGX_OK */
if (r->http_version == NGX_HTTP_VERSION_9) {
r->state_handler = NULL;
2003-03-04 14:33:48 +08:00
return NGX_OK;
2003-03-06 01:30:51 +08:00
}
2002-09-13 22:47:42 +08:00
r->headers_in.headers = ngx_create_table(r->pool, 10);
2002-12-11 02:05:12 +08:00
r->state_handler = ngx_http_process_request_headers;
2003-03-21 00:09:44 +08:00
lcx->action = "reading client request headers";
if (ngx_http_large_client_header
&& 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
2002-12-11 02:05:12 +08:00
return NGX_AGAIN;
2003-03-06 01:30:51 +08:00
/* there was error while a request line parsing */
2003-03-21 00:09:44 +08:00
2003-03-06 01:30:51 +08:00
} else if (rc != NGX_AGAIN) {
2003-03-21 00:09:44 +08:00
ngx_http_header_parse_error(r, rc);
return NGX_HTTP_BAD_REQUEST;
}
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-03-06 01:30:51 +08:00
/* If it's a pipelined request and a request line is not complete
then we need to copy it to the start of r->header_in hunk.
2003-03-12 04:38:13 +08:00
We need to copy it here only if the large client headers
are enabled otherwise a request line had been already copied
2003-03-06 01:30:51 +08:00
to the start of r->header_in hunk in ngx_http_set_keepalive() */
if (ngx_http_large_client_header) {
offset = r->request_start - r->header_in->start;
if (offset == 0) {
2003-03-21 00:09:44 +08:00
ngx_http_header_parse_error(r, NGX_HTTP_PARSE_TOO_LONG_URI);
return NGX_HTTP_REQUEST_URI_TOO_LARGE;
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-03-21 00:09:44 +08:00
ngx_http_header_parse_error(r, NGX_HTTP_PARSE_TOO_LONG_URI);
return NGX_HTTP_REQUEST_URI_TOO_LARGE;
2003-03-06 01:30:51 +08:00
}
2002-09-13 22:47:42 +08:00
}
2003-03-06 01:30:51 +08:00
return NGX_AGAIN;
}
2002-09-13 22:47:42 +08:00
2002-12-11 02:05:12 +08:00
static int ngx_http_process_request_headers(ngx_http_request_t *r)
{
2003-03-05 14:37:42 +08:00
int rc, offset;
2003-02-07 01:21:13 +08:00
size_t len;
2002-08-30 00:59:54 +08:00
ngx_http_log_ctx_t *ctx;
2002-08-30 00:59:54 +08:00
for ( ;; ) {
2002-12-11 02:05:12 +08:00
rc = ngx_read_http_header_line(r, r->header_in);
2002-08-30 00:59:54 +08:00
2003-03-06 01:30:51 +08:00
/* a header line has been parsed successfully */
2003-03-21 00:09:44 +08:00
2003-03-06 01:30:51 +08:00
if (rc == NGX_OK) {
2003-01-28 23:56:37 +08:00
if (ngx_http_process_request_header_line(r) == NGX_ERROR) {
2003-03-21 00:09:44 +08:00
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_http_large_client_header
&& 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
}
return NGX_AGAIN;
2002-09-02 22:48:24 +08:00
2003-03-12 04:38:13 +08:00
/* a whole header has been parsed successfully */
2003-03-21 00:09:44 +08:00
2002-09-13 22:47:42 +08:00
} else if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
2002-09-02 22:48:24 +08:00
ngx_log_debug(r->connection->log, "HTTP header done");
2002-12-15 14:25:09 +08:00
2003-01-28 23:56:37 +08:00
if (r->headers_in.host) {
2003-03-05 14:37:42 +08:00
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;
2003-01-28 23:56:37 +08:00
2002-12-15 14:25:09 +08:00
} else {
2003-03-05 14:37:42 +08:00
if (r->http_version > NGX_HTTP_VERSION_10) {
2003-03-21 00:09:44 +08:00
ngx_http_header_parse_error(r,
NGX_HTTP_PARSE_NO_HOST_HEADER);
return NGX_HTTP_BAD_REQUEST;
2003-03-05 14:37:42 +08:00
}
r->headers_in.host_name_len = 0;
2002-12-15 14:25:09 +08:00
}
2002-08-30 00:59:54 +08:00
2003-03-06 01:30:51 +08:00
r->state_handler = NULL;
2003-03-05 14:37:42 +08:00
return NGX_OK;
2003-03-04 14:33:48 +08:00
2003-03-06 01:30:51 +08:00
/* there was error while a header line parsing */
2003-03-21 00:09:44 +08:00
2003-03-06 01:30:51 +08:00
} else if (rc != NGX_AGAIN) {
2003-03-21 00:09:44 +08:00
ngx_http_header_parse_error(r, rc);
return NGX_HTTP_BAD_REQUEST;
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
2003-03-12 04:38:13 +08:00
/* if the large client headers are enabled then
2003-03-06 01:30:51 +08:00
we need to compact r->header_in hunk */
if (ngx_http_large_client_header) {
offset = r->header_name_start - r->header_in->start;
if (offset == 0) {
2003-03-21 00:09:44 +08:00
ngx_http_header_parse_error(r,
NGX_HTTP_PARSE_TOO_LONG_HEADER);
return NGX_HTTP_BAD_REQUEST;
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-03-21 00:09:44 +08:00
ngx_http_header_parse_error(r, NGX_HTTP_PARSE_TOO_LONG_HEADER);
return NGX_HTTP_BAD_REQUEST;
2003-03-06 01:30:51 +08:00
}
2003-01-28 23:56:37 +08:00
2003-03-04 14:33:48 +08:00
}
2003-03-06 01:30:51 +08:00
return NGX_AGAIN;
2002-08-30 00:59:54 +08:00
}
2002-09-02 22:48:24 +08:00
}
2002-09-13 22:47:42 +08:00
2002-09-02 22:48:24 +08:00
static int ngx_http_process_request_header_line(ngx_http_request_t *r)
{
2003-03-21 00:09:44 +08:00
int i;
ngx_table_elt_t *h;
2002-12-03 23:45:38 +08:00
ngx_test_null(h, ngx_push_table(r->headers_in.headers), NGX_ERROR);
2003-03-04 14:33:48 +08:00
h->key.len = r->header_name_end - r->header_name_start;
h->value.len = r->header_end - r->header_start;
2003-03-04 14:33:48 +08:00
2003-03-21 00:09:44 +08:00
/* if the large client headers are enabled then
we need to copy the header name and value */
2003-03-04 14:33:48 +08:00
if (ngx_http_large_client_header) {
ngx_test_null(h->key.data, ngx_palloc(r->pool, h->key.len + 1),
NGX_ERROR);
ngx_test_null(h->value.data, ngx_palloc(r->pool, h->value.len + 1),
NGX_ERROR);
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-03-21 00:09:44 +08:00
for (i = 0; headers_in[i].name.len != 0; i++) {
if (headers_in[i].name.len != h->key.len) {
2003-01-28 23:56:37 +08:00
continue;
}
2003-03-21 00:09:44 +08:00
if (ngx_strcasecmp(headers_in[i].name.data, h->key.data) == 0) {
2003-01-28 23:56:37 +08:00
*((ngx_table_elt_t **)
((char *) &r->headers_in + headers_in[i].offset)) = h;
}
}
2002-09-02 22:48:24 +08:00
ngx_log_debug(r->connection->log, "HTTP header: '%s: %s'" _
h->key.data _ h->value.data);
2002-09-02 22:48:24 +08:00
return NGX_OK;
}
2003-03-21 00:09:44 +08:00
int ngx_http_finalize_request(ngx_http_request_t *r, int error)
2002-12-11 02:05:12 +08:00
{
2003-03-21 00:09:44 +08:00
int rc, event;
ngx_msec_t timeout;
ngx_event_t *rev, *wev;
ngx_http_core_loc_conf_t *lcf;
2002-12-11 02:05:12 +08:00
2003-03-21 00:09:44 +08:00
rc = error;
2003-01-27 05:08:14 +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-03-21 00:09:44 +08:00
rev = r->connection->read;
2002-12-11 02:05:12 +08:00
2003-03-21 00:09:44 +08:00
if (rev->timer_set) {
ngx_del_timer(rev);
} else {
rev->timer_set = 1;
}
2003-03-04 14:33:48 +08:00
2003-03-21 00:09:44 +08:00
rc = ngx_http_special_response_handler(r, rc);
}
2002-12-11 02:05:12 +08:00
2003-03-21 00:09:44 +08:00
/* a handler has done its work completely */
2002-12-11 02:05:12 +08:00
2003-03-21 00:09:44 +08:00
if (rc == NGX_OK) {
2002-12-11 02:05:12 +08:00
2003-03-21 00:09:44 +08:00
if (r->keepalive != 0) {
return ngx_http_set_keepalive(r);
}
2002-12-11 02:05:12 +08:00
2003-03-21 00:09:44 +08:00
if (r->lingering_close) {
return ngx_http_set_lingering_close(r);
2002-12-11 02:05:12 +08:00
}
2003-03-21 00:09:44 +08:00
return ngx_http_close_request(r, 0);
}
/* NGX_AGAIN: a handler has done its work
but the transfer is still not completed */
lcf = (ngx_http_core_loc_conf_t *)
2003-04-08 23:40:10 +08:00
ngx_http_get_module_loc_conf(r->main ? r->main : r,
ngx_http_core_module_ctx);
2003-03-21 00:09:44 +08:00
wev = r->connection->write;
wev->event_handler = ngx_http_writer;
wev->timer_set = 1;
ngx_add_timer(wev, lcf->send_timeout);
2003-02-07 01:21:13 +08:00
#if (USE_KQUEUE)
2003-03-04 14:33:48 +08:00
#if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */
2003-03-21 00:09:44 +08:00
wev->lowat = lcf->send_lowat;
2003-03-04 14:33:48 +08:00
#endif
2003-03-21 00:09:44 +08:00
if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) == NGX_ERROR) {
return ngx_http_close_request(r, 0);
}
2003-02-07 01:21:13 +08:00
2003-03-21 00:09:44 +08:00
return rc;
2003-02-07 01:21:13 +08:00
#else
2003-03-21 00:09:44 +08:00
/* aio, iocp, epoll */
2003-02-07 01:21:13 +08:00
2003-03-21 00:09:44 +08:00
if (ngx_event_flags & (NGX_HAVE_AIO_EVENT|NGX_HAVE_EDGE_EVENT)) {
return rc;
}
2003-02-07 01:21:13 +08:00
2003-03-04 14:33:48 +08:00
#if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */
2003-03-21 00:09:44 +08:00
if (ngx_event_flags & NGX_HAVE_LOWAT_EVENT) {
wev->lowat = lcf->send_lowat;
}
2003-03-04 14:33:48 +08:00
#endif
2003-03-21 00:09:44 +08:00
/* kqueue */
2003-02-07 01:21:13 +08:00
2003-03-21 00:09:44 +08:00
if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
event = NGX_CLEAR_EVENT;
2003-02-07 01:21:13 +08:00
2003-03-21 00:09:44 +08:00
/* select, poll, /dev/poll */
2003-02-07 01:21:13 +08:00
2003-03-21 00:09:44 +08:00
} else {
2003-02-11 15:14:40 +08:00
event = NGX_LEVEL_EVENT;
2002-12-11 02:05:12 +08:00
}
2003-03-21 00:09:44 +08:00
if (ngx_add_event(wev, NGX_WRITE_EVENT, event) == NGX_ERROR) {
return ngx_http_close_request(r, 0);
2002-12-11 02:05:12 +08:00
}
2003-03-21 00:09:44 +08:00
return rc;
2002-12-11 02:05:12 +08:00
2003-03-21 00:09:44 +08:00
#endif /* USE_KQUEUE */
2002-12-11 02:05:12 +08:00
}
2003-03-21 00:09:44 +08:00
static int 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_msec_t timeout;
ngx_connection_t *c;
ngx_http_request_t *r;
2003-03-21 00:09:44 +08:00
ngx_http_core_loc_conf_t *lcf;
2002-12-15 14:25:09 +08:00
2003-03-21 00:09:44 +08:00
c = (ngx_connection_t *) wev->data;
2002-12-15 14:25:09 +08:00
r = (ngx_http_request_t *) c->data;
rc = ngx_http_output_filter(r, NULL);
2003-03-21 00:09:44 +08:00
ngx_log_debug(c->log, "output filter in writer: %d" _ rc);
2002-12-15 14:25:09 +08:00
if (rc == NGX_AGAIN) {
2003-03-21 00:09:44 +08:00
lcf = (ngx_http_core_loc_conf_t *)
2003-04-08 23:40:10 +08:00
ngx_http_get_module_loc_conf(r->main ? r->main : r,
ngx_http_core_module_ctx);
2003-03-21 00:09:44 +08:00
if (wev->timer_set) {
ngx_del_timer(wev);
} else {
wev->timer_set = 1;
2002-12-15 14:25:09 +08:00
}
2003-03-21 00:09:44 +08:00
ngx_add_timer(wev, lcf->send_timeout);
2002-12-15 14:25:09 +08:00
return rc;
}
if (rc == NGX_ERROR)
return rc;
/* rc == NGX_OK */
2003-03-21 00:09:44 +08:00
ngx_log_debug(c->log, "http writer done");
2002-12-15 14:25:09 +08:00
2003-03-21 00:09:44 +08:00
if (r->keepalive != 0) {
return ngx_http_set_keepalive(r);
2002-12-15 14:25:09 +08:00
}
2003-03-21 00:09:44 +08:00
if (r->lingering_close) {
return ngx_http_set_lingering_close(r);
}
2002-12-15 14:25:09 +08:00
2003-03-21 00:09:44 +08:00
return ngx_http_close_request(r, 0);
2002-12-15 14:25:09 +08:00
}
2002-12-11 02:05:12 +08:00
static int ngx_http_block_read(ngx_event_t *ev)
{
ngx_log_debug(ev->log, "http read blocked");
2003-02-07 01:21:13 +08:00
/* aio does not call this handler */
#if (USE_KQUEUE)
return NGX_OK;
#else
2003-03-12 04:38:13 +08:00
if (ngx_event_flags & NGX_USE_LEVEL_EVENT) { /* select, poll, /dev/poll */
2003-03-05 14:37:42 +08:00
ev->blocked = 1;
return ngx_del_event(ev, NGX_READ_EVENT, 0);
2003-02-07 01:21:13 +08:00
2003-03-12 04:38:13 +08:00
} else { /* kqueue, epoll */
2003-02-07 01:21:13 +08:00
return NGX_OK;
}
#endif /* USE_KQUEUE */
2002-12-11 02:05:12 +08:00
}
2003-02-11 15:14:40 +08:00
/* TODO */
2002-12-15 14:25:09 +08:00
int ngx_http_discard_body(ngx_http_request_t *r)
2002-09-02 22:48:24 +08:00
{
2003-01-27 05:08:14 +08:00
ngx_event_t *ev;
ev = r->connection->read;
2002-09-02 22:48:24 +08:00
ngx_log_debug(r->connection->log, "set discard body");
2003-01-27 05:08:14 +08:00
if (ev->timer_set) {
ngx_del_timer(ev);
ev->timer_set = 0;
}
2002-08-30 00:59:54 +08:00
2003-02-07 01:21:13 +08:00
if (r->client_content_length) {
2003-01-27 05:08:14 +08:00
ev->event_handler = ngx_http_read_discarded_body;
2003-02-07 01:21:13 +08:00
/* if blocked - read */
/* else add timer */
}
2002-12-15 14:25:09 +08:00
return NGX_OK;
}
2002-12-11 02:05:12 +08:00
2003-02-11 15:14:40 +08:00
/* TODO */
2002-09-02 22:48:24 +08:00
static int ngx_http_read_discarded_body(ngx_event_t *ev)
2002-08-26 23:18:19 +08:00
{
2002-09-02 22:48:24 +08:00
size_t size;
ssize_t n;
2003-01-09 13:36:00 +08:00
ngx_connection_t *c;
ngx_http_request_t *r;
ngx_http_core_loc_conf_t *lcf;
2002-09-02 22:48:24 +08:00
ngx_log_debug(ev->log, "http read discarded body");
2003-03-21 00:09:44 +08:00
if (ev->timedout) {
2002-09-02 22:48:24 +08:00
return NGX_ERROR;
2003-03-21 00:09:44 +08:00
}
2002-09-02 22:48:24 +08:00
2003-01-09 13:36:00 +08:00
c = (ngx_connection_t *) ev->data;
r = (ngx_http_request_t *) c->data;
lcf = (ngx_http_core_loc_conf_t *)
2003-04-08 23:40:10 +08:00
ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
2003-01-09 13:36:00 +08:00
2003-03-21 00:09:44 +08:00
if (r->discarded_buffer == NULL) {
2002-09-02 22:48:24 +08:00
ngx_test_null(r->discarded_buffer,
2003-01-09 13:36:00 +08:00
ngx_palloc(r->pool, lcf->discarded_buffer_size),
2002-09-02 22:48:24 +08:00
NGX_ERROR);
2003-03-21 00:09:44 +08:00
}
2002-09-02 22:48:24 +08:00
size = r->client_content_length;
2003-03-21 00:09:44 +08:00
if (size > lcf->discarded_buffer_size) {
2003-01-09 13:36:00 +08:00
size = lcf->discarded_buffer_size;
2003-03-21 00:09:44 +08:00
}
2002-09-02 22:48:24 +08:00
n = ngx_event_recv(c, r->discarded_buffer, size);
2003-03-21 00:09:44 +08:00
if (n == NGX_ERROR) {
2002-09-12 22:42:29 +08:00
return NGX_ERROR;
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) {
2002-09-12 22:42:29 +08:00
return NGX_OK;
2003-03-21 00:09:44 +08:00
}
2002-09-12 22:42:29 +08:00
r->client_content_length -= n;
/* XXX: what if r->client_content_length == 0 ? */
return NGX_OK;
2002-09-02 22:48:24 +08:00
}
2002-12-11 02:05:12 +08:00
2003-02-11 15:14:40 +08:00
static int ngx_http_set_keepalive(ngx_http_request_t *r)
{
2003-03-12 04:38:13 +08:00
int len, blocked;
2003-03-05 14:37:42 +08:00
ngx_hunk_t *h;
ngx_event_t *rev, *wev;
2003-02-11 15:14:40 +08:00
ngx_connection_t *c;
2003-03-04 14:33:48 +08:00
ngx_http_log_ctx_t *ctx;
2003-02-11 15:14:40 +08:00
c = (ngx_connection_t *) r->connection;
2003-03-05 14:37:42 +08:00
rev = c->read;
2003-02-11 15:14:40 +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
if (rev->blocked && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2003-03-05 14:37:42 +08:00
if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT) == NGX_ERROR) {
2003-02-11 15:14:40 +08:00
return NGX_ERROR;
}
2003-03-12 04:38:13 +08:00
blocked = 1;
2003-03-05 14:37:42 +08:00
rev->blocked = 0;
2003-02-11 15:14:40 +08:00
2003-03-12 04:38:13 +08:00
} else {
blocked = 0;
}
2003-02-11 15:14:40 +08:00
2003-03-05 14:37:42 +08:00
h = c->buffer;
/* pipelined request */
2003-03-12 04:38:13 +08:00
if (h->pos < h->last) {
2003-03-05 14:37:42 +08:00
2003-03-21 00:09:44 +08:00
/* We do not know here whether a pipelined request is complete
so if the large client headers are not enabled
2003-03-12 04:38:13 +08:00
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
if (!ngx_http_large_client_header) {
2003-03-12 04:38:13 +08:00
len = h->last - h->pos;
ngx_memcpy(h->start, h->pos, len);
h->pos = h->start;
h->last = h->start + len;
2003-03-05 14:37:42 +08:00
}
c->pipeline = 1;
ctx->action = "reading client pipelined request line";
return ngx_http_init_request(rev);
}
c->pipeline = 0;
2003-03-12 04:38:13 +08:00
h->pos = h->last = h->start;
2003-03-05 14:37:42 +08:00
rev->event_handler = ngx_http_keepalive_handler;
2003-03-21 00:09:44 +08:00
wev = c->write;
2003-03-05 14:37:42 +08:00
2003-03-12 04:38:13 +08:00
if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2003-03-05 14:37:42 +08:00
if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
return NGX_ERROR;
}
}
2003-03-04 14:33:48 +08:00
ctx->action = "keepalive";
2003-02-11 15:14:40 +08:00
#if (HAVE_AIO_EVENT) /* aio, iocp */
2003-03-05 14:37:42 +08:00
2003-03-12 04:38:13 +08:00
if ((ngx_event_flags & NGX_HAVE_AIO_EVENT) || blocked) {
return ngx_http_keepalive_handler(rev);
}
#else
if (blocked) {
2003-03-05 14:37:42 +08:00
return ngx_http_keepalive_handler(rev);
2003-02-11 15:14:40 +08:00
}
2003-03-05 14:37:42 +08:00
2003-02-11 15:14:40 +08:00
#endif
return NGX_OK;
}
2003-03-21 00:09:44 +08:00
static int ngx_http_keepalive_handler(ngx_event_t *rev)
2002-08-26 23:18:19 +08:00
{
2002-12-15 14:25:09 +08:00
ssize_t n;
ngx_connection_t *c;
2003-03-21 00:09:44 +08:00
ngx_http_log_ctx_t *lctx;
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
c = (ngx_connection_t *) rev->data;
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
ngx_log_debug(c->log, "http keepalive handler");
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
if (rev->timedout) {
2003-04-08 23:40:10 +08:00
return NGX_ERROR; /* to close connection */
2003-03-13 01:32:22 +08:00
}
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
/* MSIE closes a keepalive connection with RST flag
2003-03-12 04:38:13 +08:00
so we ignore ECONNRESET here */
2003-03-21 00:09:44 +08:00
rev->ignore_econnreset = 1;
2003-03-12 04:38:13 +08:00
ngx_set_socket_errno(0);
n = ngx_event_recv(c, c->buffer->last, c->buffer->end - c->buffer->last);
2003-03-21 00:09:44 +08:00
rev->ignore_econnreset = 0;
2002-08-26 23:18:19 +08:00
2003-03-13 01:32:22 +08:00
if (n == NGX_AGAIN || n == NGX_ERROR) {
2002-12-15 14:25:09 +08:00
return n;
2003-03-13 01:32:22 +08:00
}
2002-08-26 23:18:19 +08:00
2003-03-21 00:09:44 +08:00
lctx = (ngx_http_log_ctx_t *) rev->log->data;
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,
"client %s closed keepalive connection", lctx->client);
2003-04-08 23:40:10 +08:00
return NGX_ERROR; /* to close connection */
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;
lctx->action = "reading client request line";
2002-09-16 23:01:44 +08:00
2003-03-21 00:09:44 +08:00
return ngx_http_init_request(rev);
2002-09-16 23:01:44 +08:00
}
2002-12-11 02:05:12 +08:00
2002-09-16 23:01:44 +08:00
static int ngx_http_set_lingering_close(ngx_http_request_t *r)
{
2003-03-21 00:09:44 +08:00
ngx_event_t *rev;
2003-03-12 04:38:13 +08:00
ngx_connection_t *c;
2003-01-09 13:36:00 +08:00
ngx_http_core_loc_conf_t *lcf;
2003-02-11 15:14:40 +08:00
c = r->connection;
2003-03-12 04:38:13 +08:00
rev = c->read;
2003-01-27 05:08:14 +08:00
2003-01-09 13:36:00 +08:00
lcf = (ngx_http_core_loc_conf_t *)
2003-04-08 23:40:10 +08:00
ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
2003-01-09 13:36:00 +08:00
2003-03-12 04:38:13 +08:00
r->lingering_time = ngx_time() + lcf->lingering_time / 1000;
2002-12-15 14:25:09 +08:00
r->connection->read->event_handler = ngx_http_lingering_close_handler;
2002-09-16 23:01:44 +08:00
2003-03-12 04:38:13 +08:00
if (rev->timer_set) {
ngx_del_timer(rev);
2003-01-27 05:08:14 +08:00
} else {
2003-03-12 04:38:13 +08:00
rev->timer_set = 1;
2003-01-27 05:08:14 +08:00
}
2003-03-12 04:38:13 +08:00
ngx_add_timer(rev, lcf->lingering_timeout);
2002-09-16 23:01:44 +08:00
2003-03-12 04:38:13 +08:00
if (rev->blocked && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT) == NGX_ERROR) {
2003-03-21 00:09:44 +08:00
return ngx_http_close_request(r, 0);
2002-12-24 15:09:57 +08:00
}
2003-03-12 04:38:13 +08:00
rev->blocked = 0;
2002-09-16 23:01:44 +08:00
}
2003-03-21 00:09:44 +08:00
#if !(USE_KQUEUE)
if (c->write->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
if (ngx_del_event(c->write, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
return ngx_http_close_request(r, 0);
2003-03-12 04:38:13 +08:00
}
}
2003-03-21 00:09:44 +08:00
#endif
2003-03-12 04:38:13 +08:00
if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno,
2002-09-16 23:01:44 +08:00
ngx_shutdown_socket_n " failed");
2003-03-21 00:09:44 +08:00
return ngx_http_close_request(r, 0);
2002-09-16 23:01:44 +08:00
}
2003-03-21 00:09:44 +08:00
#if (USE_KQUEUE)
2003-03-12 04:38:13 +08:00
2003-03-21 00:09:44 +08:00
if (rev->ready) {
2003-03-12 04:38:13 +08:00
return ngx_http_lingering_close_handler(rev);
2003-02-11 15:14:40 +08:00
}
2003-03-12 04:38:13 +08:00
#else
2003-03-21 00:09:44 +08:00
if (rev->ready || (ngx_event_flags & NGX_HAVE_AIO_EVENT)) {
2003-03-12 04:38:13 +08:00
return ngx_http_lingering_close_handler(rev);
2003-02-11 15:14:40 +08:00
}
2003-03-12 04:38:13 +08:00
#endif
2003-02-11 15:14:40 +08:00
2003-03-12 04:38:13 +08:00
return NGX_OK;
2002-08-26 23:18:19 +08:00
}
2003-03-21 00:09:44 +08:00
static int 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-01-09 13:36:00 +08:00
ngx_http_core_loc_conf_t *lcf;
2002-09-13 22:47:42 +08:00
2003-03-21 00:09:44 +08:00
c = (ngx_connection_t *) rev->data;
2003-01-30 15:28:09 +08:00
r = (ngx_http_request_t *) c->data;
2003-03-21 00:09:44 +08:00
ngx_log_debug(c->log, "http lingering close handler");
if (rev->timedout) {
return ngx_http_close_request(r, 0);
2002-12-24 15:09:57 +08:00
}
2002-09-13 22:47:42 +08:00
timer = r->lingering_time - ngx_time();
2002-12-24 15:09:57 +08:00
if (timer <= 0) {
2003-03-21 00:09:44 +08:00
return ngx_http_close_request(r, 0);
2002-12-24 15:09:57 +08:00
}
2002-09-13 22:47:42 +08:00
2003-01-09 13:36:00 +08:00
lcf = (ngx_http_core_loc_conf_t *)
2003-04-08 23:40:10 +08:00
ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
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 */
if ((size_t)(r->header_in->end - r->header_in->last)
2003-01-09 13:36:00 +08:00
>= lcf->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 {
ngx_test_null(r->discarded_buffer,
2003-01-09 13:36:00 +08:00
ngx_palloc(c->pool, lcf->discarded_buffer_size),
2003-03-21 00:09:44 +08:00
ngx_http_close_request(r, 0));
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 {
n = ngx_event_recv(c, r->discarded_buffer, lcf->discarded_buffer_size);
2002-09-13 22:47:42 +08:00
2003-03-21 00:09:44 +08:00
ngx_log_debug(c->log, "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) {
return ngx_http_close_request(r, 0);
}
} while (rev->ready);
2002-09-13 22:47:42 +08:00
2002-09-16 23:01:44 +08:00
timer *= 1000;
2003-01-09 13:36:00 +08:00
if (timer > lcf->lingering_timeout) {
timer = lcf->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
if (rev->timer_set) {
ngx_del_timer(rev);
2003-01-27 05:08:14 +08:00
} else {
2003-03-21 00:09:44 +08:00
rev->timer_set = 1;
2003-01-27 05:08:14 +08:00
}
2003-03-21 00:09:44 +08:00
ngx_add_timer(rev, timer);
2002-09-13 22:47:42 +08:00
return NGX_OK;
}
2002-08-20 22:48:28 +08:00
2002-10-05 01:58:04 +08:00
static int ngx_http_close_connection(ngx_event_t *ev)
{
return ngx_event_close_connection(ev);
}
2003-03-21 00:09:44 +08:00
static void ngx_http_header_parse_error(ngx_http_request_t *r, int parse_err)
2003-03-05 14:37:42 +08:00
{
ngx_http_log_ctx_t *ctx;
ctx = r->connection->log->data;
r->connection->log->handler = NULL;
if (ctx->url) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
header_errors[parse_err - NGX_HTTP_PARSE_INVALID_METHOD],
ctx->client, ctx->url);
} else {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
header_errors[parse_err - NGX_HTTP_PARSE_INVALID_METHOD],
ctx->client);
}
r->connection->log->handler = ngx_http_log_error;
}
2002-08-26 23:18:19 +08:00
static size_t ngx_http_log_error(void *data, char *buf, size_t len)
{
ngx_http_log_ctx_t *ctx = (ngx_http_log_ctx_t *) data;
2002-09-02 22:48:24 +08:00
2003-03-21 00:09:44 +08:00
if (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-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
}
}