/src/http/ngx_http_request.c
C | 3226 lines | 2280 code | 864 blank | 82 comment | 581 complexity | c3ef7e394991347b813e175ec6f5d15c MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /*
- * Copyright (C) Igor Sysoev
- * Copyright (C) Nginx, Inc.
- */
- #include <ngx_config.h>
- #include <ngx_core.h>
- #include <ngx_http.h>
- static void ngx_http_init_request(ngx_event_t *ev);
- 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);
- static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
- ngx_uint_t request_line);
- static ngx_int_t ngx_http_process_header_line(ngx_http_request_t *r,
- ngx_table_elt_t *h, ngx_uint_t offset);
- static ngx_int_t ngx_http_process_unique_header_line(ngx_http_request_t *r,
- ngx_table_elt_t *h, ngx_uint_t offset);
- static ngx_int_t ngx_http_process_host(ngx_http_request_t *r,
- ngx_table_elt_t *h, ngx_uint_t offset);
- static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r,
- ngx_table_elt_t *h, ngx_uint_t offset);
- static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r,
- ngx_table_elt_t *h, ngx_uint_t offset);
- static ngx_int_t ngx_http_process_cookie(ngx_http_request_t *r,
- ngx_table_elt_t *h, ngx_uint_t offset);
- static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
- static void ngx_http_process_request(ngx_http_request_t *r);
- static ssize_t ngx_http_validate_host(ngx_http_request_t *r, u_char **host,
- size_t len, ngx_uint_t alloc);
- static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r,
- u_char *host, size_t len);
- static void ngx_http_request_handler(ngx_event_t *ev);
- static void ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc);
- static void ngx_http_terminate_handler(ngx_http_request_t *r);
- static void ngx_http_finalize_connection(ngx_http_request_t *r);
- static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
- static void ngx_http_writer(ngx_http_request_t *r);
- static void ngx_http_request_finalizer(ngx_http_request_t *r);
- 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);
- static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
- static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
- static void ngx_http_free_request(ngx_http_request_t *r, ngx_int_t error);
- static void ngx_http_log_request(ngx_http_request_t *r);
- static void ngx_http_close_connection(ngx_connection_t *c);
- static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
- static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
- ngx_http_request_t *sr, u_char *buf, size_t len);
- #if (NGX_HTTP_SSL)
- static void ngx_http_ssl_handshake(ngx_event_t *rev);
- static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
- #endif
- static char *ngx_http_client_errors[] = {
- /* NGX_HTTP_PARSE_INVALID_METHOD */
- "client sent invalid method",
- /* NGX_HTTP_PARSE_INVALID_REQUEST */
- "client sent invalid request",
- /* NGX_HTTP_PARSE_INVALID_09_METHOD */
- "client sent invalid method in HTTP/0.9 request"
- };
- ngx_http_header_t ngx_http_headers_in[] = {
- { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host),
- ngx_http_process_host },
- { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
- ngx_http_process_connection },
- { ngx_string("If-Modified-Since"),
- offsetof(ngx_http_headers_in_t, if_modified_since),
- ngx_http_process_unique_header_line },
- { ngx_string("If-Unmodified-Since"),
- offsetof(ngx_http_headers_in_t, if_unmodified_since),
- ngx_http_process_unique_header_line },
- { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
- ngx_http_process_user_agent },
- { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
- ngx_http_process_header_line },
- { ngx_string("Content-Length"),
- offsetof(ngx_http_headers_in_t, content_length),
- ngx_http_process_unique_header_line },
- { ngx_string("Content-Type"),
- offsetof(ngx_http_headers_in_t, content_type),
- ngx_http_process_header_line },
- { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
- ngx_http_process_header_line },
- { ngx_string("If-Range"),
- offsetof(ngx_http_headers_in_t, if_range),
- ngx_http_process_unique_header_line },
- { ngx_string("Transfer-Encoding"),
- offsetof(ngx_http_headers_in_t, transfer_encoding),
- ngx_http_process_header_line },
- { ngx_string("Expect"),
- offsetof(ngx_http_headers_in_t, expect),
- ngx_http_process_unique_header_line },
- #if (NGX_HTTP_GZIP)
- { ngx_string("Accept-Encoding"),
- offsetof(ngx_http_headers_in_t, accept_encoding),
- ngx_http_process_header_line },
- { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
- ngx_http_process_header_line },
- #endif
- { ngx_string("Authorization"),
- offsetof(ngx_http_headers_in_t, authorization),
- ngx_http_process_unique_header_line },
- { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
- ngx_http_process_header_line },
- #if (NGX_HTTP_X_FORWARDED_FOR)
- { ngx_string("X-Forwarded-For"),
- offsetof(ngx_http_headers_in_t, x_forwarded_for),
- ngx_http_process_header_line },
- #endif
- #if (NGX_HTTP_REALIP)
- { ngx_string("X-Real-IP"),
- offsetof(ngx_http_headers_in_t, x_real_ip),
- ngx_http_process_header_line },
- #endif
- #if (NGX_HTTP_HEADERS)
- { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
- ngx_http_process_header_line },
- { ngx_string("Accept-Language"),
- offsetof(ngx_http_headers_in_t, accept_language),
- ngx_http_process_header_line },
- #endif
- #if (NGX_HTTP_DAV)
- { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
- ngx_http_process_header_line },
- { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
- ngx_http_process_header_line },
- { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite),
- ngx_http_process_header_line },
- { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date),
- ngx_http_process_header_line },
- #endif
- { ngx_string("Cookie"), 0, ngx_http_process_cookie },
- { ngx_null_string, 0, NULL }
- };
- void
- ngx_http_init_connection(ngx_connection_t *c)
- {
- ngx_event_t *rev;
- ngx_http_log_ctx_t *ctx;
- ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
- if (ctx == NULL) {
- ngx_http_close_connection(c);
- return;
- }
- ctx->connection = c;
- ctx->request = NULL;
- ctx->current_request = NULL;
- c->log->connection = c->number;
- c->log->handler = ngx_http_log_error;
- c->log->data = ctx;
- c->log->action = "reading client request line";
- c->log_error = NGX_ERROR_INFO;
- rev = c->read;
- rev->handler = ngx_http_init_request;
- c->write->handler = ngx_http_empty_handler;
- #if (NGX_STAT_STUB)
- (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
- #endif
- if (rev->ready) {
- /* the deferred accept(), rtsig, aio, iocp */
- if (ngx_use_accept_mutex) {
- ngx_post_event(rev, &ngx_posted_events);
- return;
- }
- ngx_http_init_request(rev);
- return;
- }
- ngx_add_timer(rev, c->listening->post_accept_timeout);
- if (ngx_handle_read_event(rev, 0) != NGX_OK) {
- #if (NGX_STAT_STUB)
- (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
- #endif
- ngx_http_close_connection(c);
- return;
- }
- }
- static void
- ngx_http_init_request(ngx_event_t *rev)
- {
- ngx_time_t *tp;
- ngx_uint_t i;
- ngx_connection_t *c;
- ngx_http_request_t *r;
- struct sockaddr_in *sin;
- ngx_http_port_t *port;
- ngx_http_in_addr_t *addr;
- ngx_http_log_ctx_t *ctx;
- ngx_http_addr_conf_t *addr_conf;
- ngx_http_connection_t *hc;
- ngx_http_core_srv_conf_t *cscf;
- ngx_http_core_loc_conf_t *clcf;
- ngx_http_core_main_conf_t *cmcf;
- #if (NGX_HAVE_INET6)
- struct sockaddr_in6 *sin6;
- ngx_http_in6_addr_t *addr6;
- #endif
- #if (NGX_STAT_STUB)
- (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
- #endif
- c = rev->data;
- if (rev->timedout) {
- ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
- ngx_http_close_connection(c);
- return;
- }
- c->requests++;
- hc = c->data;
- if (hc == NULL) {
- hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
- if (hc == NULL) {
- ngx_http_close_connection(c);
- return;
- }
- }
- r = hc->request;
- if (r) {
- ngx_memzero(r, sizeof(ngx_http_request_t));
- r->pipeline = hc->pipeline;
- if (hc->nbusy) {
- r->header_in = hc->busy[0];
- }
- } else {
- r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
- if (r == NULL) {
- ngx_http_close_connection(c);
- return;
- }
- hc->request = r;
- }
- c->data = r;
- r->http_connection = hc;
- c->sent = 0;
- r->signature = NGX_HTTP_MODULE;
- /* find the server configuration for the address:port */
- port = c->listening->servers;
- r->connection = c;
- if (port->naddrs > 1) {
- /*
- * there are several addresses on this port and one of them
- * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
- * is required to determine a server address
- */
- if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
- ngx_http_close_connection(c);
- return;
- }
- switch (c->local_sockaddr->sa_family) {
- #if (NGX_HAVE_INET6)
- case AF_INET6:
- sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
- addr6 = port->addrs;
- /* the last address is "*" */
- for (i = 0; i < port->naddrs - 1; i++) {
- if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
- break;
- }
- }
- addr_conf = &addr6[i].conf;
- break;
- #endif
- default: /* AF_INET */
- sin = (struct sockaddr_in *) c->local_sockaddr;
- addr = port->addrs;
- /* the last address is "*" */
- for (i = 0; i < port->naddrs - 1; i++) {
- if (addr[i].addr == sin->sin_addr.s_addr) {
- break;
- }
- }
- addr_conf = &addr[i].conf;
- break;
- }
- } else {
- switch (c->local_sockaddr->sa_family) {
- #if (NGX_HAVE_INET6)
- case AF_INET6:
- addr6 = port->addrs;
- addr_conf = &addr6[0].conf;
- break;
- #endif
- default: /* AF_INET */
- addr = port->addrs;
- addr_conf = &addr[0].conf;
- break;
- }
- }
- r->virtual_names = addr_conf->virtual_names;
- /* the default server configuration for the address:port */
- cscf = addr_conf->default_server;
- r->main_conf = cscf->ctx->main_conf;
- r->srv_conf = cscf->ctx->srv_conf;
- r->loc_conf = cscf->ctx->loc_conf;
- rev->handler = ngx_http_process_request_line;
- r->read_event_handler = ngx_http_block_reading;
- #if (NGX_HTTP_SSL)
- {
- ngx_http_ssl_srv_conf_t *sscf;
- sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
- if (sscf->enable || addr_conf->ssl) {
- if (c->ssl == NULL) {
- c->log->action = "SSL handshaking";
- if (addr_conf->ssl && sscf->ssl.ctx == NULL) {
- ngx_log_error(NGX_LOG_ERR, c->log, 0,
- "no \"ssl_certificate\" is defined "
- "in server listening on SSL port");
- ngx_http_close_connection(c);
- return;
- }
- if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
- != NGX_OK)
- {
- ngx_http_close_connection(c);
- return;
- }
- rev->handler = ngx_http_ssl_handshake;
- }
- r->main_filter_need_in_memory = 1;
- }
- }
- #endif
- clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
- c->log->file = clcf->error_log->file;
- if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
- c->log->log_level = clcf->error_log->log_level;
- }
- if (c->buffer == NULL) {
- c->buffer = ngx_create_temp_buf(c->pool,
- cscf->client_header_buffer_size);
- if (c->buffer == NULL) {
- ngx_http_close_connection(c);
- return;
- }
- }
- if (r->header_in == NULL) {
- r->header_in = c->buffer;
- }
- r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
- if (r->pool == NULL) {
- ngx_http_close_connection(c);
- return;
- }
- if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
- sizeof(ngx_table_elt_t))
- != NGX_OK)
- {
- ngx_destroy_pool(r->pool);
- ngx_http_close_connection(c);
- return;
- }
- r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
- if (r->ctx == NULL) {
- ngx_destroy_pool(r->pool);
- ngx_http_close_connection(c);
- return;
- }
- cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
- r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
- * sizeof(ngx_http_variable_value_t));
- if (r->variables == NULL) {
- ngx_destroy_pool(r->pool);
- ngx_http_close_connection(c);
- return;
- }
- c->single_connection = 1;
- c->destroyed = 0;
- r->main = r;
- r->count = 1;
- tp = ngx_timeofday();
- r->start_sec = tp->sec;
- r->start_msec = tp->msec;
- r->method = NGX_HTTP_UNKNOWN;
- r->headers_in.content_length_n = -1;
- r->headers_in.keep_alive_n = -1;
- r->headers_out.content_length_n = -1;
- r->headers_out.last_modified_time = -1;
- r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
- r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
- r->http_state = NGX_HTTP_READING_REQUEST_STATE;
- ctx = c->log->data;
- ctx->request = r;
- ctx->current_request = r;
- r->log_handler = ngx_http_log_error_handler;
- #if (NGX_STAT_STUB)
- (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
- r->stat_reading = 1;
- (void) ngx_atomic_fetch_add(ngx_stat_requests, 1);
- #endif
- rev->handler(rev);
- }
- #if (NGX_HTTP_SSL)
- static void
- ngx_http_ssl_handshake(ngx_event_t *rev)
- {
- u_char buf[1];
- ssize_t n;
- ngx_int_t rc;
- 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_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
- c->timedout = 1;
- ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
- return;
- }
- n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
- if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
- if (!rev->timer_set) {
- ngx_add_timer(rev, c->listening->post_accept_timeout);
- }
- if (ngx_handle_read_event(rev, 0) != NGX_OK) {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- }
- 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%02Xd", buf[0]);
- rc = ngx_ssl_handshake(c);
- if (rc == NGX_AGAIN) {
- if (!rev->timer_set) {
- ngx_add_timer(rev, c->listening->post_accept_timeout);
- }
- c->ssl->handler = ngx_http_ssl_handshake_handler;
- return;
- }
- ngx_http_ssl_handshake_handler(c);
- return;
- } else {
- ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
- "plain http");
- r->plain_http = 1;
- }
- }
- c->log->action = "reading client request line";
- rev->handler = ngx_http_process_request_line;
- ngx_http_process_request_line(rev);
- }
- static void
- ngx_http_ssl_handshake_handler(ngx_connection_t *c)
- {
- ngx_http_request_t *r;
- if (c->ssl->handshaked) {
- /*
- * The majority of browsers do not send the "close notify" alert.
- * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
- * and Links. And what is more, MSIE ignores the server's alert.
- *
- * Opera and recent Mozilla send the alert.
- */
- c->ssl->no_wait_shutdown = 1;
- c->log->action = "reading client request line";
- c->read->handler = ngx_http_process_request_line;
- /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
- ngx_http_process_request_line(c->read);
- return;
- }
- r = c->data;
- ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
- return;
- }
- #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
- int
- ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
- {
- size_t len;
- u_char *host;
- const char *servername;
- ngx_connection_t *c;
- ngx_http_request_t *r;
- ngx_http_ssl_srv_conf_t *sscf;
- servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
- if (servername == NULL) {
- return SSL_TLSEXT_ERR_NOACK;
- }
- c = ngx_ssl_get_connection(ssl_conn);
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
- "SSL server name: \"%s\"", servername);
- len = ngx_strlen(servername);
- if (len == 0) {
- return SSL_TLSEXT_ERR_NOACK;
- }
- r = c->data;
- host = (u_char *) servername;
- len = ngx_http_validate_host(r, &host, len, 1);
- if (len <= 0) {
- return SSL_TLSEXT_ERR_NOACK;
- }
- if (ngx_http_find_virtual_server(r, host, len) != NGX_OK) {
- return SSL_TLSEXT_ERR_NOACK;
- }
- sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
- if (sscf->ssl.ctx) {
- SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
- /*
- * SSL_set_SSL_CTX() only changes certs as of 1.0.0d
- * adjust other things we care about
- */
- SSL_set_verify(ssl_conn, SSL_CTX_get_verify_mode(sscf->ssl.ctx),
- SSL_CTX_get_verify_callback(sscf->ssl.ctx));
- SSL_set_verify_depth(ssl_conn, SSL_CTX_get_verify_depth(sscf->ssl.ctx));
- #ifdef SSL_CTRL_CLEAR_OPTIONS
- /* only in 0.9.8m+ */
- SSL_clear_options(ssl_conn, SSL_get_options(ssl_conn) &
- ~SSL_CTX_get_options(sscf->ssl.ctx));
- #endif
- SSL_set_options(ssl_conn, SSL_CTX_get_options(sscf->ssl.ctx));
- }
- return SSL_TLSEXT_ERR_OK;
- }
- #endif
- #endif
- static void
- ngx_http_process_request_line(ngx_event_t *rev)
- {
- u_char *host;
- ssize_t n;
- ngx_int_t rc, rv;
- ngx_connection_t *c;
- ngx_http_request_t *r;
- ngx_http_core_srv_conf_t *cscf;
- c = rev->data;
- r = c->data;
- ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
- "http process request line");
- if (rev->timedout) {
- ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
- c->timedout = 1;
- ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
- return;
- }
- rc = NGX_AGAIN;
- for ( ;; ) {
- if (rc == NGX_AGAIN) {
- n = ngx_http_read_request_header(r);
- if (n == NGX_AGAIN || n == NGX_ERROR) {
- return;
- }
- }
- rc = ngx_http_parse_request_line(r, r->header_in);
- if (rc == NGX_OK) {
- /* the request line has been parsed successfully */
- r->request_line.len = r->request_end - r->request_start;
- r->request_line.data = r->request_start;
- if (r->args_start) {
- r->uri.len = r->args_start - 1 - r->uri_start;
- } else {
- r->uri.len = r->uri_end - r->uri_start;
- }
- if (r->complex_uri || r->quoted_uri) {
- r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
- if (r->uri.data == NULL) {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
- rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
- if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent invalid request");
- ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
- return;
- }
- } else {
- r->uri.data = r->uri_start;
- }
- r->unparsed_uri.len = r->uri_end - r->uri_start;
- r->unparsed_uri.data = r->uri_start;
- r->valid_unparsed_uri = r->space_in_uri ? 0 : 1;
- r->method_name.len = r->method_end - r->request_start + 1;
- r->method_name.data = r->request_line.data;
- if (r->http_protocol.data) {
- r->http_protocol.len = r->request_end - r->http_protocol.data;
- }
- if (r->uri_ext) {
- if (r->args_start) {
- r->exten.len = r->args_start - 1 - r->uri_ext;
- } else {
- r->exten.len = r->uri_end - r->uri_ext;
- }
- r->exten.data = r->uri_ext;
- }
- if (r->args_start && r->uri_end > r->args_start) {
- r->args.len = r->uri_end - r->args_start;
- r->args.data = r->args_start;
- }
- #if (NGX_WIN32)
- {
- u_char *p, *last;
- p = r->uri.data;
- last = r->uri.data + r->uri.len;
- while (p < last) {
- if (*p++ == ':') {
- /*
- * this check covers "::$data", "::$index_allocation" and
- * ":$i30:$index_allocation"
- */
- if (p < last && *p == '$') {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent unsafe win32 URI");
- ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
- return;
- }
- }
- }
- p = r->uri.data + r->uri.len - 1;
- while (p > r->uri.data) {
- if (*p == ' ') {
- p--;
- continue;
- }
- if (*p == '.') {
- p--;
- continue;
- }
- break;
- }
- if (p != r->uri.data + r->uri.len - 1) {
- r->uri.len = p + 1 - r->uri.data;
- ngx_http_set_exten(r);
- }
- }
- #endif
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
- "http request line: \"%V\"", &r->request_line);
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
- "http uri: \"%V\"", &r->uri);
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
- "http args: \"%V\"", &r->args);
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
- "http exten: \"%V\"", &r->exten);
- if (r->host_start && r->host_end) {
- host = r->host_start;
- n = ngx_http_validate_host(r, &host,
- r->host_end - r->host_start, 0);
- if (n == 0) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent invalid host in request line");
- ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
- return;
- }
- if (n < 0) {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- r->headers_in.server.len = n;
- r->headers_in.server.data = host;
- }
- if (r->http_version < NGX_HTTP_VERSION_10) {
- if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
- r->headers_in.server.len)
- == NGX_ERROR)
- {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- ngx_http_process_request(r);
- return;
- }
- if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
- sizeof(ngx_table_elt_t))
- != NGX_OK)
- {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
- sizeof(ngx_table_elt_t *))
- != NGX_OK)
- {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- c->log->action = "reading client request headers";
- rev->handler = ngx_http_process_request_headers;
- ngx_http_process_request_headers(rev);
- return;
- }
- if (rc != NGX_AGAIN) {
- /* there was error while a request line parsing */
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
- ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
- return;
- }
- /* NGX_AGAIN: a request line parsing is still incomplete */
- if (r->header_in->pos == r->header_in->end) {
- rv = ngx_http_alloc_large_header_buffer(r, 1);
- if (rv == NGX_ERROR) {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- if (rv == NGX_DECLINED) {
- r->request_line.len = r->header_in->end - r->request_start;
- r->request_line.data = r->request_start;
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent too long URI");
- ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
- return;
- }
- }
- }
- }
- static void
- ngx_http_process_request_headers(ngx_event_t *rev)
- {
- u_char *p;
- size_t len;
- ssize_t n;
- ngx_int_t rc, rv;
- ngx_table_elt_t *h;
- ngx_connection_t *c;
- ngx_http_header_t *hh;
- ngx_http_request_t *r;
- ngx_http_core_srv_conf_t *cscf;
- ngx_http_core_main_conf_t *cmcf;
- c = rev->data;
- r = c->data;
- ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
- "http process request header line");
- if (rev->timedout) {
- ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
- c->timedout = 1;
- ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
- return;
- }
- cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
- cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
- rc = NGX_AGAIN;
- for ( ;; ) {
- if (rc == NGX_AGAIN) {
- if (r->header_in->pos == r->header_in->end) {
- rv = ngx_http_alloc_large_header_buffer(r, 0);
- if (rv == NGX_ERROR) {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- if (rv == NGX_DECLINED) {
- p = r->header_name_start;
- r->lingering_close = 1;
- if (p == NULL) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent too large request");
- ngx_http_finalize_request(r,
- NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
- return;
- }
- len = r->header_in->end - p;
- if (len > NGX_MAX_ERROR_STR - 300) {
- len = NGX_MAX_ERROR_STR - 300;
- p[len++] = '.'; p[len++] = '.'; p[len++] = '.';
- }
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent too long header line: \"%*s\"",
- len, r->header_name_start);
- ngx_http_finalize_request(r,
- NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
- return;
- }
- }
- n = ngx_http_read_request_header(r);
- if (n == NGX_AGAIN || n == NGX_ERROR) {
- return;
- }
- }
- rc = ngx_http_parse_header_line(r, r->header_in,
- cscf->underscores_in_headers);
- if (rc == NGX_OK) {
- if (r->invalid_header && cscf->ignore_invalid_headers) {
- /* there was error while a header line parsing */
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent invalid header line: \"%*s\"",
- r->header_end - r->header_name_start,
- r->header_name_start);
- continue;
- }
- /* a header line has been parsed successfully */
- h = ngx_list_push(&r->headers_in.headers);
- if (h == NULL) {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- h->hash = r->header_hash;
- h->key.len = r->header_name_end - r->header_name_start;
- h->key.data = r->header_name_start;
- h->key.data[h->key.len] = '\0';
- h->value.len = r->header_end - r->header_start;
- h->value.data = r->header_start;
- h->value.data[h->value.len] = '\0';
- h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
- if (h->lowcase_key == NULL) {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- if (h->key.len == r->lowcase_index) {
- ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
- } else {
- ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
- }
- hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
- h->lowcase_key, h->key.len);
- if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
- return;
- }
- ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
- "http header: \"%V: %V\"",
- &h->key, &h->value);
- continue;
- }
- if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
- /* a whole header has been parsed successfully */
- ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
- "http header done");
- r->request_length += r->header_in->pos - r->header_in->start;
- r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
- rc = ngx_http_process_request_header(r);
- if (rc != NGX_OK) {
- return;
- }
- ngx_http_process_request(r);
- return;
- }
- if (rc == NGX_AGAIN) {
- /* a header line parsing is still not complete */
- continue;
- }
- /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent invalid header line: \"%*s\\r...\"",
- r->header_end - r->header_name_start,
- r->header_name_start);
- ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
- return;
- }
- }
- static ssize_t
- ngx_http_read_request_header(ngx_http_request_t *r)
- {
- ssize_t n;
- ngx_event_t *rev;
- ngx_connection_t *c;
- ngx_http_core_srv_conf_t *cscf;
- c = r->connection;
- rev = c->read;
- n = r->header_in->last - r->header_in->pos;
- if (n > 0) {
- return n;
- }
- if (rev->ready) {
- n = c->recv(c, r->header_in->last,
- r->header_in->end - r->header_in->last);
- } else {
- n = NGX_AGAIN;
- }
- if (n == NGX_AGAIN) {
- if (!rev->timer_set) {
- cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
- ngx_add_timer(rev, cscf->client_header_timeout);
- }
- if (ngx_handle_read_event(rev, 0) != NGX_OK) {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return NGX_ERROR;
- }
- return NGX_AGAIN;
- }
- if (n == 0) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client prematurely closed connection");
- }
- if (n == 0 || n == NGX_ERROR) {
- c->error = 1;
- c->log->action = "reading client request headers";
- ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
- return NGX_ERROR;
- }
- r->header_in->last += n;
- return n;
- }
- static ngx_int_t
- ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
- ngx_uint_t request_line)
- {
- u_char *old, *new;
- ngx_buf_t *b;
- ngx_http_connection_t *hc;
- ngx_http_core_srv_conf_t *cscf;
- ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
- "http alloc large header buffer");
- if (request_line && r->state == 0) {
- /* the client fills up the buffer with "\r\n" */
- r->request_length += r->header_in->end - r->header_in->start;
- r->header_in->pos = r->header_in->start;
- r->header_in->last = r->header_in->start;
- return NGX_OK;
- }
- old = request_line ? r->request_start : r->header_name_start;
- cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
- if (r->state != 0
- && (size_t) (r->header_in->pos - old)
- >= cscf->large_client_header_buffers.size)
- {
- return NGX_DECLINED;
- }
- hc = r->http_connection;
- if (hc->nfree) {
- b = hc->free[--hc->nfree];
- ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
- "http large header free: %p %uz",
- b->pos, b->end - b->last);
- } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
- if (hc->busy == NULL) {
- hc->busy = ngx_palloc(r->connection->pool,
- cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
- if (hc->busy == NULL) {
- return NGX_ERROR;
- }
- }
- b = ngx_create_temp_buf(r->connection->pool,
- cscf->large_client_header_buffers.size);
- if (b == NULL) {
- return NGX_ERROR;
- }
- ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
- "http large header alloc: %p %uz",
- b->pos, b->end - b->last);
- } else {
- return NGX_DECLINED;
- }
- hc->busy[hc->nbusy++] = b;
- if (r->state == 0) {
- /*
- * r->state == 0 means that a header line was parsed successfully
- * and we do not need to copy incomplete header line and
- * to relocate the parser header pointers
- */
- r->request_length += r->header_in->end - r->header_in->start;
- r->header_in = b;
- return NGX_OK;
- }
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
- "http large header copy: %d", r->header_in->pos - old);
- r->request_length += old - r->header_in->start;
- new = b->start;
- ngx_memcpy(new, old, r->header_in->pos - old);
- b->pos = new + (r->header_in->pos - old);
- b->last = new + (r->header_in->pos - old);
- if (request_line) {
- r->request_start = new;
- if (r->request_end) {
- r->request_end = new + (r->request_end - old);
- }
- r->method_end = new + (r->method_end - old);
- r->uri_start = new + (r->uri_start - old);
- r->uri_end = new + (r->uri_end - old);
- if (r->schema_start) {
- r->schema_start = new + (r->schema_start - old);
- r->schema_end = new + (r->schema_end - old);
- }
- if (r->host_start) {
- r->host_start = new + (r->host_start - old);
- if (r->host_end) {
- r->host_end = new + (r->host_end - old);
- }
- }
- if (r->port_start) {
- r->port_start = new + (r->port_start - old);
- r->port_end = new + (r->port_end - old);
- }
- if (r->uri_ext) {
- r->uri_ext = new + (r->uri_ext - old);
- }
- if (r->args_start) {
- r->args_start = new + (r->args_start - old);
- }
- if (r->http_protocol.data) {
- r->http_protocol.data = new + (r->http_protocol.data - old);
- }
- } else {
- r->header_name_start = new;
- r->header_name_end = new + (r->header_name_end - old);
- r->header_start = new + (r->header_start - old);
- r->header_end = new + (r->header_end - old);
- }
- r->header_in = b;
- return NGX_OK;
- }
- static ngx_int_t
- ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
- ngx_uint_t offset)
- {
- ngx_table_elt_t **ph;
- ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
- if (*ph == NULL) {
- *ph = h;
- }
- return NGX_OK;
- }
- static ngx_int_t
- ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
- ngx_uint_t offset)
- {
- ngx_table_elt_t **ph;
- ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
- if (*ph == NULL) {
- *ph = h;
- return NGX_OK;
- }
- ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
- "client sent duplicate header line: \"%V: %V\", "
- "previous value: \"%V: %V\"",
- &h->key, &h->value, &(*ph)->key, &(*ph)->value);
- ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
- return NGX_ERROR;
- }
- static ngx_int_t
- ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
- ngx_uint_t offset)
- {
- u_char *host;
- ssize_t len;
- if (r->headers_in.host == NULL) {
- r->headers_in.host = h;
- }
- host = h->value.data;
- len = ngx_http_validate_host(r, &host, h->value.len, 0);
- if (len == 0) {
- ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
- "client sent invalid host header");
- ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
- return NGX_ERROR;
- }
- if (len < 0) {
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return NGX_ERROR;
- }
- if (r->headers_in.server.len) {
- return NGX_OK;
- }
- r->headers_in.server.len = len;
- r->headers_in.server.data = host;
- return NGX_OK;
- }
- static ngx_int_t
- ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
- ngx_uint_t offset)
- {
- if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
- r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
- } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
- r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
- }
- return NGX_OK;
- }
- static ngx_int_t
- ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
- ngx_uint_t offset)
- {
- u_char *user_agent, *msie;
- if (r->headers_in.user_agent) {
- return NGX_OK;
- }
- r->headers_in.user_agent = h;
- /* check some widespread browsers while the header is in CPU cache */
- user_agent = h->value.data;
- msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
- if (msie && msie + 7 < user_agent + h->value.len) {
- r->headers_in.msie = 1;
- if (msie[6] == '.') {
- switch (msie[5]) {
- case '4':
- case '5':
- r->headers_in.msie6 = 1;
- break;
- case '6':
- if (ngx_strstrn(msie + 8, "SV1", 3 - 1) == NULL) {
- r->headers_in.msie6 = 1;
- }
- break;
- }
- }
- #if 0
- /* MSIE ignores the SSL "close notify" alert */
- if (c->ssl) {
- c->ssl->no_send_shutdown = 1;
- }
- #endif
- }
- if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
- r->headers_in.opera = 1;
- r->headers_in.msie = 0;
- r->headers_in.msie6 = 0;
- }
- if (!r->headers_in.msie && !r->headers_in.opera) {
- if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
- r->headers_in.gecko = 1;
- } else if (ngx_strstrn(user_agent, "Chrome/", 7 - 1)) {
- r->headers_in.chrome = 1;
- } else if (ngx_strstrn(user_agent, "Safari/", 7 - 1)
- && ngx_strstrn(user_agent, "Mac OS X", 8 - 1))
- {
- r->headers_in.safari = 1;
- } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
- r->headers_in.konqueror = 1;
- }
- }
- return NGX_OK;
- }
- static ngx_int_t
- ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
- ngx_uint_t offset)
- {
- ngx_table_elt_t **cookie;
- cookie = ngx_array_push(&r->headers_in.cookies);
- if (cookie) {
- *cookie = h;
- return NGX_OK;
- }
- ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return NGX_ERROR;
- }
- static ngx_int_t
- ngx_http_process_request_header(ngx_http_request_t *r)
- {
- if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
- r->headers_in.server.len)
- == NGX_ERROR)
- {
- ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return NGX_ERROR;
- }
- if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
- ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
- "client sent HTTP/1.1 request without \"Host\" header");
- ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
- return NGX_ERROR;
- }
- if (r->headers_in.content_length) {
- r->headers_in.content_length_n =
- ngx_atoof(r->headers_in.content_length->value.data,
- r->headers_in.content_length->value.len);
- if (r->headers_in.content_length_n == NGX_ERROR) {
- ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
- "client sent invalid \"Content-Length\" header");
- ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
- return NGX_ERROR;
- }
- }
- if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
- ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
- "client sent %V method without \"Content-Length\" header",
- &r->method_name);
- ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
- return NGX_ERROR;
- }
- if (r->method & NGX_HTTP_TRACE) {
- ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
- "client sent TRACE method");
- ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
- return NGX_ERROR;
- }
- if (r->headers_in.transfer_encoding
- && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
- "chunked", 7 - 1))
- {
- ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
- "client sent \"Transfer-Encoding: chunked\" header");
- ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
- return NGX_ERROR;
- }
- if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
- if (r->headers_in.keep_alive) {
- r->headers_in.keep_alive_n =
- ngx_atotm(r->headers_in.keep_alive->value.data,
- r->headers_in.keep_alive->value.len);
- }
- }
- return NGX_OK;
- }
- static void
- ngx_http_process_request(ngx_http_request_t *r)
- {
- ngx_connection_t *c;
- c = r->connection;
- if (r->plain_http) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent plain HTTP request to HTTPS port");
- ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
- return;
- }
- #if (NGX_HTTP_SSL)
- if (c->ssl) {
- long rc;
- X509 *cert;
- ngx_http_ssl_srv_conf_t *sscf;
- sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
- if (sscf->verify) {
- rc = SSL_get_verify_result(c->ssl->connection);
- if (rc != X509_V_OK) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client SSL certificate verify error: (%l:%s)",
- rc, X509_verify_cert_error_string(rc));
- ngx_ssl_remove_cached_session(sscf->ssl.ctx,
- (SSL_get0_session(c->ssl->connection)));
- ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
- return;
- }
- if (sscf->verify == 1) {
- cert = SSL_get_peer_certificate(c->ssl->connection);
- if (cert == NULL) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent no required SSL certificate");
- ngx_ssl_remove_cached_session(sscf->ssl.ctx,
- (SSL_get0_session(c->ssl->connection)));
- ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
- return;
- }
- X509_free(cert);
- }
- }
- }
- #endif
- if (c->read->timer_set) {
- ngx_del_timer(c->read);
- }
- #if (NGX_STAT_STUB)
- (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
- r->stat_reading = 0;
- (void) ngx_atomic_fetch_add(ngx_stat_writing, 1);
- r->stat_writing = 1;
- #endif
- c->read->handler = ngx_http_request_handler;
- c->write->handler = ngx_http_request_handler;
- r->read_event_handler = ngx_http_block_reading;
- ngx_http_handler(r);
- ngx_http_run_posted_requests(c);
- }
- static ssize_t
- ngx_http_validate_host(ngx_http_request_t *r, u_char **host, size_t len,
- ngx_uint_t alloc)
- {
- u_char *h, ch;
- size_t i, dot_pos, host_len;
- enum {
- sw_usual = 0,
- sw_literal,
- sw_rest
- } state;
- dot_pos = len;
- host_len = len;
- h = *host;
- state = sw_usual;
- for (i = 0; i < len; i++) {
- ch = h[i];
- switch (ch) {
- case '.':
- if (dot_pos == i - 1) {
- return 0;
- }
- dot_pos = i;
- break;
- case ':':
- if (state == sw_usual) {
- host_len = i;
- state = sw_rest;
- }
- break;
- case '[':
- if (i == 0) {
- state = sw_literal;
- }
- break;
- case ']':
- if (state == sw_literal) {
- host_len = i + 1;
- state = sw_rest;
- }
- break;
- case '\0':
- return 0;
- default:
- if (ngx_path_separator(ch)) {
- return 0;
- }
- if (ch >= 'A' && ch <= 'Z') {
- alloc = 1;
- }
- break;
- }
- }
- if (dot_pos == host_len - 1) {
- host_len--;
- }
- if (alloc) {
- *host = ngx_pnalloc(r->pool, host_len);
- if (*host == NULL) {
- return -1;
- }
- ngx_strlow(*host, h, host_len);
- }
- return host_len;
- }
- static ngx_int_t
- ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
- {
- ngx_http_core_loc_conf_t *clcf;
- ngx_http_core_srv_conf_t *cscf;
- if (r->virtual_names == NULL) {
- return NGX_DECLINED;
- }
- cscf = ngx_hash_find_combined(&r->virtual_names->names,
- ngx_hash_key(host, len), host, len);
- if (cscf) {
- goto found;
- }
- #if (NGX_PCRE)
- if (len && r->virtual_names->nregex) {
- ngx_int_t n;
- ngx_uint_t i;
- ngx_str_t name;
- ngx_http_server_name_t *sn;
- name.len = len;
- name.data = host;
- sn = r->virtual_names->regex;
- for (i = 0; i < r->virtual_names->nregex; i++) {
- n = ngx_http_regex_exec(r, sn[i].regex, &name);
- if (n == NGX_OK) {
- cs…
Large files files are truncated, but you can click here to view the full file