diff options
Diffstat (limited to '')
| -rw-r--r-- | src/http/ngx_http.c | 7 | ||||
| -rw-r--r-- | src/http/ngx_http.h | 6 | ||||
| -rw-r--r-- | src/http/ngx_http_core_module.c | 45 | ||||
| -rw-r--r-- | src/http/ngx_http_event.c | 204 | ||||
| -rw-r--r-- | src/http/ngx_http_header_filter.c | 16 |
5 files changed, 206 insertions, 72 deletions
diff --git a/src/http/ngx_http.c b/src/http/ngx_http.c index 4675325ad..83c9e1dd9 100644 --- a/src/http/ngx_http.c +++ b/src/http/ngx_http.c @@ -18,11 +18,14 @@ int ngx_http_max_module; ngx_array_t ngx_http_servers; /* array of ngx_http_core_srv_conf_t */ -int ngx_http_post_accept_timeout = 10000; +int ngx_http_post_accept_timeout = 30000; int ngx_http_connection_pool_size = 16384; int ngx_http_request_pool_size = 16384; -int ngx_http_client_header_timeout = 20000; +int ngx_http_client_header_timeout = 60000; int ngx_http_client_header_buffer_size = 1024; +int ngx_http_large_client_header = 1; + +int ngx_http_url_in_error_log = 1; /* STUB: per location */ int ngx_http_lingering_timeout = 5000; diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h index 41bd932eb..cb62966a3 100644 --- a/src/http/ngx_http.h +++ b/src/http/ngx_http.h @@ -15,7 +15,9 @@ /* STUB */ #include <ngx_event_timer.h> +#define NGX_HTTP_VERSION_9 9 #define NGX_HTTP_VERSION_10 1000 +#define NGX_HTTP_VERSION_11 1001 #define NGX_HTTP_GET 1 #define NGX_HTTP_HEAD 2 @@ -31,6 +33,8 @@ #define NGX_HTTP_PARSE_TOO_LONG_URI 12 #define NGX_HTTP_PARSE_INVALID_HEAD 13 #define NGX_HTTP_PARSE_INVALID_HEADER 14 +#define NGX_HTTP_PARSE_TOO_LONG_HEADER 15 +#define NGX_HTTP_PARSE_NO_HOST_HEADER 16 #define NGX_HTTP_OK 200 @@ -270,11 +274,13 @@ extern int ngx_http_connection_pool_size; extern int ngx_http_request_pool_size; extern int ngx_http_client_header_timeout; extern int ngx_http_client_header_buffer_size; +extern int ngx_http_large_client_header; extern int ngx_http_discarded_buffer_size; extern int ngx_http_lingering_timeout; extern int ngx_http_lingering_time; +extern int ngx_http_url_in_error_log; extern ngx_array_t ngx_http_index_handlers; diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c index 072fe3666..5ae83b3b2 100644 --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -36,6 +36,42 @@ static ngx_command_t ngx_http_core_commands[] = { 0, 0}, + {ngx_string("post_accept_timeout"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_conf_set_time_slot, + 0, + addressof(ngx_http_post_accept_timeout)}, + + {ngx_string("connection_pool_size"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + 0, + addressof(ngx_http_connection_pool_size)}, + + {ngx_string("request_pool_size"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + 0, + addressof(ngx_http_request_pool_size)}, + + {ngx_string("client_header_timeout"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_conf_set_time_slot, + 0, + addressof(ngx_http_client_header_timeout)}, + + {ngx_string("client_header_buffer_size"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + 0, + addressof(ngx_http_client_header_buffer_size)}, + + {ngx_string("large_client_header"), + NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG, + ngx_conf_set_flag_slot, + 0, + addressof(ngx_http_large_client_header)}, + {ngx_string("location"), NGX_HTTP_SRV_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1, ngx_location_block, @@ -468,6 +504,8 @@ int ngx_http_error(ngx_http_request_t *r, int error) int ngx_http_close_request(ngx_http_request_t *r) { + ngx_http_log_ctx_t *ctx; + ngx_log_debug(r->connection->log, "CLOSE#: %d" _ r->file.fd); ngx_http_log_handler(r); @@ -487,9 +525,14 @@ int ngx_http_close_request(ngx_http_request_t *r) ngx_http_log_request(r); */ + ctx = (ngx_http_log_ctx_t *) r->connection->log->data; + + /* ctx->url was allocated from r->pool */ + ctx->url = NULL; + ngx_destroy_pool(r->pool); - ngx_log_debug(r->connection->log, "http close"); + ngx_log_debug(r->connection->log, "http closed"); if (r->connection->read->timer_set) { ngx_del_timer(r->connection->read); diff --git a/src/http/ngx_http_event.c b/src/http/ngx_http_event.c index 0b69209ba..a968be65f 100644 --- a/src/http/ngx_http_event.c +++ b/src/http/ngx_http_event.c @@ -44,10 +44,15 @@ static char *header_errors[] = { "client %s sent invalid method", "client %s sent invalid request", "client %s sent too long URI", - "client %s sent HEAD method in HTTP/0.9 request" + "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" }; + static ngx_http_header_t headers_in[] = { { 4, "Host", offsetof(ngx_http_headers_in_t, host) }, { 10, "Connection", offsetof(ngx_http_headers_in_t, connection) }, @@ -113,21 +118,9 @@ int ngx_http_init_connection(ngx_connection_t *c) #endif -/* THINK: should ngx_edge_add_event() be moved to accept part ? */ -#if (HAVE_EDGE_EVENT) /* epoll */ - - if (ngx_event_flags & NGX_HAVE_EDGE_EVENT) { - if (ngx_edge_add_event(ev) == NGX_ERROR) { - return NGX_ERROR; - } - return ngx_http_init_request(ev); - } - -#endif - -#if (HAVE_AIO_EVENT) /* aio, iocp */ +#if (HAVE_EDGE_EVENT) /* epoll */ || (HAVE_AIO_EVENT) /* aio, iocp */ - if (ngx_event_flags & NGX_HAVE_AIO_EVENT) { + if (ngx_event_flags & (NGX_HAVE_EDGE_EVENT|NGX_HAVE_AIO_EVENT)) { return ngx_http_init_request(ev); } @@ -193,9 +186,10 @@ static int ngx_http_init_request(ngx_event_t *ev) static int ngx_http_process_request_header(ngx_event_t *ev) { - int n, rc; + int n, rc, offset; ngx_connection_t *c; ngx_http_request_t *r; + ngx_http_log_ctx_t *ctx; c = (ngx_connection_t *) ev->data; r = (ngx_http_request_t *) c->data; @@ -249,6 +243,7 @@ static int ngx_http_process_request_header(ngx_event_t *ev) ngx_http_process_request_headers(r) */ do { + /* state_handlers return NGX_OK when whole header done */ rc = (r->state_handler)(r); if (rc == NGX_ERROR) @@ -257,6 +252,38 @@ static int ngx_http_process_request_header(ngx_event_t *ev) } while (rc == NGX_AGAIN && r->header_in->pos.mem < r->header_in->last.mem); + /* if large client header is supported then + we need to compact r->header_in hunk */ + + if (ngx_http_large_client_header + && rc == NGX_AGAIN + && r->header_in->last.mem == r->header_in->end + && r->header_in->pos.mem == r->header_in->last.mem) + { + offset = r->header_name_start - r->header_in->start; + + if (offset == 0) { + ctx = r->connection->log->data; + r->connection->log->handler = NULL; + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "client %s sent too long header line, URL: %s", + ctx->client, ctx->url); + r->connection->log->handler = ngx_http_log_error; + + return ngx_http_error(r, NGX_HTTP_BAD_REQUEST); + } + + ngx_memcpy(r->header_in->start, r->header_name_start, + r->header_in->last.mem - r->header_name_start); + + r->header_name_start = r->header_in->start; + r->header_in->last.mem -= offset; + r->header_in->pos.mem -= offset; + r->header_name_end -= offset; + r->header_start -= offset; + r->header_end -= offset; + } + #if (HAVE_AIO_EVENT) /* aio, iocp */ } while (rc == NGX_AGAIN && ngx_event_flags & NGX_HAVE_AIO_EVENT); #endif @@ -301,19 +328,30 @@ static int ngx_http_process_request_line(ngx_http_request_t *r) c = r->connection; if (rc == NGX_OK) { + /* copy URI */ r->uri.len = (r->args_start ? r->args_start - 1 : r->uri_end) - r->uri_start; ngx_test_null(r->uri.data, ngx_palloc(r->pool, r->uri.len + 1), ngx_http_close_request(r)); ngx_cpystrn(r->uri.data, r->uri_start, r->uri.len + 1); + /* if large client headers is supported then + we need to copy request line */ + r->request_line.len = r->request_end - r->header_in->start; - ngx_test_null(r->request_line.data, - ngx_palloc(r->pool, r->request_line.len + 1), - ngx_http_close_request(r)); - ngx_cpystrn(r->request_line.data, r->header_in->start, - r->request_line.len + 1); + if (ngx_http_large_client_header) { + ngx_test_null(r->request_line.data, + ngx_palloc(r->pool, r->request_line.len + 1), + ngx_http_close_request(r)); + ngx_cpystrn(r->request_line.data, r->header_in->start, + r->request_line.len + 1); + } else { + r->request_line.data = r->header_in->start; + r->request_line.data[r->request_line.len] = '\0'; + } + + /* copy URI extention if it exists */ if (r->uri_ext) { r->exten.len = (r->args_start ? r->args_start - 1 : r->uri_end) - r->uri_ext; @@ -329,21 +367,26 @@ static int ngx_http_process_request_line(ngx_http_request_t *r) r->uri.data _ r->exten.data); #endif - if (r->http_version == 9) - return NGX_OK; + ctx = r->connection->log->data; + if (ngx_http_url_in_error_log) { + ngx_test_null(ctx->url, + ngx_palloc(r->pool, r->uri_end - r->uri_start + 1), + ngx_http_close_request(r)); + ngx_cpystrn(ctx->url, r->uri_start, r->uri_end - r->uri_start + 1); + } - /* TODO: check too long URI - no space for header, compact buffer */ + if (r->http_version == NGX_HTTP_VERSION_9) + return NGX_OK; r->headers_in.headers = ngx_create_table(r->pool, 10); r->state_handler = ngx_http_process_request_headers; - ctx = r->connection->log->data; ctx->action = "reading client request headers"; return NGX_AGAIN; } - if (r->header_in->last.mem >= r->header_in->end) { + if (r->header_in->last.mem == r->header_in->end) { rc = NGX_HTTP_PARSE_TOO_LONG_URI; } else if (rc == NGX_AGAIN) { @@ -372,11 +415,9 @@ static int ngx_http_process_request_headers(ngx_http_request_t *r) for ( ;; ) { rc = ngx_read_http_header_line(r, r->header_in); - /* TODO: check too long header, compact buffer */ - if (rc == NGX_OK) { /* header line is ready */ if (ngx_http_process_request_header_line(r) == NGX_ERROR) { - return ngx_http_error(r, NGX_HTTP_BAD_REQUEST); + return ngx_http_error(r, NGX_HTTP_INTERNAL_SERVER_ERROR); } return NGX_AGAIN; @@ -391,28 +432,33 @@ static int ngx_http_process_request_headers(ngx_http_request_t *r) } } r->headers_in.host_name_len = len; + return NGX_OK; } else { - if (r->http_version > NGX_HTTP_VERSION_10) { - return ngx_http_error(r, NGX_HTTP_BAD_REQUEST); + if (r->http_version < NGX_HTTP_VERSION_11) { + r->headers_in.host_name_len = 0; + return NGX_OK; } - r->headers_in.host_name_len = 0; } - return NGX_OK; + rc = NGX_HTTP_PARSE_NO_HOST_HEADER; + + } else if (!ngx_http_large_client_header + && r->header_in->last.mem == r->header_in->end) { + rc = NGX_HTTP_PARSE_TOO_LONG_HEADER; } else if (rc == NGX_AGAIN) { return NGX_AGAIN; + } - } else if (rc == NGX_HTTP_PARSE_INVALID_HEADER) { - ctx = r->connection->log->data; - r->connection->log->handler = NULL; - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, - "client %s sent invalid header", ctx->client); - r->connection->log->handler = ngx_http_log_error; + ctx = r->connection->log->data; + r->connection->log->handler = NULL; + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + header_errors[rc - NGX_HTTP_PARSE_INVALID_METHOD], + ctx->client, ctx->url); + r->connection->log->handler = ngx_http_log_error; - return ngx_http_error(r, NGX_HTTP_BAD_REQUEST); - } + return ngx_http_error(r, NGX_HTTP_BAD_REQUEST); } } @@ -424,14 +470,26 @@ static int ngx_http_process_request_header_line(ngx_http_request_t *r) ngx_test_null(h, ngx_push_table(r->headers_in.headers), NGX_ERROR); - h->key.len = r->header_name_end - r->header_name_start; - ngx_test_null(h->key.data, ngx_palloc(r->pool, h->key.len + 1), NGX_ERROR); - ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1); + /* if large client headers is supported then + we need to copy header name and value */ + h->key.len = r->header_name_end - r->header_name_start; h->value.len = r->header_end - r->header_start; - ngx_test_null(h->value.data, ngx_palloc(r->pool, h->value.len + 1), - NGX_ERROR); - ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1); + + 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'; + } for (i = 0; headers_in[i].len != 0; i++) { if (headers_in[i].len != h->key.len) { @@ -453,9 +511,10 @@ static int ngx_http_process_request_header_line(ngx_http_request_t *r) static int ngx_http_event_request_handler(ngx_http_request_t *r) { - int rc, event; - ngx_msec_t timeout; - ngx_event_t *rev, *wev; + int rc, event; + ngx_msec_t timeout; + ngx_event_t *rev, *wev; + ngx_http_log_ctx_t *ctx; rev = r->connection->read; wev = r->connection->write; @@ -468,6 +527,9 @@ static int ngx_http_event_request_handler(ngx_http_request_t *r) r->state_handler = NULL; rev->event_handler = ngx_http_block_read; + ctx = r->connection->log->data; + ctx->action = "processing client request"; + rc = ngx_http_handler(r); /* handler is still busy */ @@ -477,6 +539,7 @@ static int ngx_http_event_request_handler(ngx_http_request_t *r) /* handler has done its work but transfer is still not completed */ if (rc == NGX_AGAIN) { + /* STUB: timeouts should be reworked */ if (r->connection->sent > 0) { ngx_log_debug(r->connection->log, "sent: " OFF_FMT _ r->connection->sent); @@ -492,6 +555,10 @@ static int ngx_http_event_request_handler(ngx_http_request_t *r) #if (USE_KQUEUE) +#if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */ + wev->lowat = /* STUB */ NGX_LOWAT; +#endif + if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) == NGX_ERROR) { return ngx_http_close_request(r); } @@ -508,6 +575,14 @@ static int ngx_http_event_request_handler(ngx_http_request_t *r) #endif +#if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */ + + if (ngx_event_flags & NGX_HAVE_LOWAT_EVENT) { + wev->lowat = /* STUB */ NGX_LOWAT; + } + +#endif + #if (HAVE_CLEAR_EVENT) /* kqueue */ if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) { @@ -546,7 +621,7 @@ static int ngx_http_event_request_handler(ngx_http_request_t *r) /* rc == NGX_OK */ - if (!r->keepalive) { + if (r->keepalive == 0) { if (r->lingering_close) { return ngx_http_set_lingering_close(r); @@ -578,6 +653,7 @@ static int ngx_http_writer(ngx_event_t *ev) if (rc == NGX_AGAIN) { + /* STUB: timeouts should be reworked */ if (c->sent > 0) { conf = (ngx_http_core_loc_conf_t *) ngx_http_get_module_loc_conf(r->main ? r->main : r, @@ -607,7 +683,7 @@ static int ngx_http_writer(ngx_event_t *ev) ngx_log_debug(ev->log, "http writer done"); - if (!r->keepalive) { + if (r->keepalive == 0) { if (r->lingering_close) { return ngx_http_set_lingering_close(r); @@ -731,6 +807,7 @@ static int ngx_http_read_discarded_body(ngx_event_t *ev) static int ngx_http_set_keepalive(ngx_http_request_t *r) { ngx_connection_t *c; + ngx_http_log_ctx_t *ctx; c = (ngx_connection_t *) r->connection; @@ -743,8 +820,13 @@ static int ngx_http_set_keepalive(ngx_http_request_t *r) } } + ctx = (ngx_http_log_ctx_t *) c->log->data; + ctx->action = "closing request"; + ngx_http_close_request(r); + ctx->action = "keepalive"; + #if (HAVE_AIO_EVENT) /* aio, iocp */ if (ngx_event_flags & NGX_HAVE_AIO_EVENT) { return ngx_http_keepalive_handler(c->read); @@ -768,6 +850,9 @@ static int ngx_http_keepalive_handler(ngx_event_t *ev) if (ev->timedout) return NGX_DONE; + /* TODO: MSIE closes keepalive connection with ECONNRESET + so we need to handle here this error + 1) in INFO (not ERR) level, 2) with time elapsed */ n = ngx_event_recv(c, c->buffer->last.mem, c->buffer->end - c->buffer->last.mem); @@ -815,14 +900,11 @@ static int ngx_http_set_lingering_close(ngx_http_request_t *r) ngx_add_timer(ev, lcf->lingering_timeout); if (ev->blocked) { - if (ngx_add_event(ev, NGX_READ_EVENT, -#if (HAVE_CLEAR_EVENT) - NGX_CLEAR_EVENT) == NGX_ERROR) -#else - NGX_ONESHOT_EVENT) == NGX_ERROR) -#endif - { - return ngx_http_close_request(r); + if (ngx_event_flags & NGX_HAVE_LEVEL_EVENT) { + if (ngx_add_event(ev, NGX_READ_EVENT, NGX_LEVEL_EVENT) + == NGX_ERROR) { + return ngx_http_close_request(r); + } } } @@ -838,7 +920,7 @@ static int ngx_http_set_lingering_close(ngx_http_request_t *r) } #endif -#if (HAVE_CLEAR_EVENT) || (HAVE_EDGE_EVENT) /* kqueue, epoll */ +#if (HAVE_CLEAR_EVENT) /* kqueue */ || (HAVE_EDGE_EVENT) /* epoll */ if (ngx_event_flags & (NGX_HAVE_CLEAR_EVENT|NGX_HAVE_EDGE_EVENT)) { return NGX_OK; } diff --git a/src/http/ngx_http_header_filter.c b/src/http/ngx_http_header_filter.c index 6af5cc0db..ab2c832c6 100644 --- a/src/http/ngx_http_header_filter.c +++ b/src/http/ngx_http_header_filter.c @@ -163,10 +163,10 @@ static int ngx_http_header_filter(ngx_http_request_t *r) len += 46; } - if (r->keepalive) { - len += 24; - } else { + if (r->keepalive == 0) { len += 19; + } else { + len += 24; } header = (ngx_table_elt_t *) r->headers_out.headers->elts; @@ -238,13 +238,13 @@ static int ngx_http_header_filter(ngx_http_request_t *r) *(h->last.mem++) = CR; *(h->last.mem++) = LF; } - if (r->keepalive) { - ngx_memcpy(h->last.mem, "Connection: keep-alive" CRLF, 24); - h->last.mem += 24; - - } else { + if (r->keepalive == 0) { ngx_memcpy(h->last.mem, "Connection: close" CRLF, 19); h->last.mem += 19; + + } else { + ngx_memcpy(h->last.mem, "Connection: keep-alive" CRLF, 24); + h->last.mem += 24; } for (i = 0; i < r->headers_out.headers->nelts; i++) { |
