3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static void ngx_http_init_request(ngx_event_t *ev);
13 static void ngx_http_process_request_line(ngx_event_t *rev);
14 static void ngx_http_process_request_headers(ngx_event_t *rev);
15 static ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
16 static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
17 ngx_uint_t request_line);
19 static ngx_int_t ngx_http_process_header_line(ngx_http_request_t *r,
20 ngx_table_elt_t *h, ngx_uint_t offset);
21 static ngx_int_t ngx_http_process_unique_header_line(ngx_http_request_t *r,
22 ngx_table_elt_t *h, ngx_uint_t offset);
23 static ngx_int_t ngx_http_process_host(ngx_http_request_t *r,
24 ngx_table_elt_t *h, ngx_uint_t offset);
25 static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r,
26 ngx_table_elt_t *h, ngx_uint_t offset);
27 static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r,
28 ngx_table_elt_t *h, ngx_uint_t offset);
29 static ngx_int_t ngx_http_process_cookie(ngx_http_request_t *r,
30 ngx_table_elt_t *h, ngx_uint_t offset);
32 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
33 static void ngx_http_process_request(ngx_http_request_t *r);
34 static ssize_t ngx_http_validate_host(u_char *host, size_t len);
35 static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r,
36 u_char *host, size_t len);
38 static void ngx_http_request_handler(ngx_event_t *ev);
39 static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
40 static void ngx_http_writer(ngx_http_request_t *r);
41 static void ngx_http_request_finalizer(ngx_http_request_t *r);
43 static void ngx_http_set_keepalive(ngx_http_request_t *r);
44 static void ngx_http_keepalive_handler(ngx_event_t *ev);
45 static void ngx_http_set_lingering_close(ngx_http_request_t *r);
46 static void ngx_http_lingering_close_handler(ngx_event_t *ev);
47 static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
48 static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
49 static void ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error);
50 static void ngx_http_log_request(ngx_http_request_t *r);
51 static void ngx_http_close_connection(ngx_connection_t *c);
53 static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
54 static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
55 ngx_http_request_t *sr, u_char *buf, size_t len);
58 static void ngx_http_ssl_handshake(ngx_event_t *rev);
59 static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
63 static char *ngx_http_client_errors[] = {
65 /* NGX_HTTP_PARSE_INVALID_METHOD */
66 "client sent invalid method",
68 /* NGX_HTTP_PARSE_INVALID_REQUEST */
69 "client sent invalid request",
71 /* NGX_HTTP_PARSE_INVALID_09_METHOD */
72 "client sent invalid method in HTTP/0.9 request"
76 ngx_http_header_t ngx_http_headers_in[] = {
77 { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host),
78 ngx_http_process_host },
80 { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
81 ngx_http_process_connection },
83 { ngx_string("If-Modified-Since"),
84 offsetof(ngx_http_headers_in_t, if_modified_since),
85 ngx_http_process_unique_header_line },
87 { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
88 ngx_http_process_user_agent },
90 { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
91 ngx_http_process_header_line },
93 { ngx_string("Content-Length"),
94 offsetof(ngx_http_headers_in_t, content_length),
95 ngx_http_process_unique_header_line },
97 { ngx_string("Content-Type"),
98 offsetof(ngx_http_headers_in_t, content_type),
99 ngx_http_process_header_line },
101 { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
102 ngx_http_process_header_line },
104 { ngx_string("If-Range"),
105 offsetof(ngx_http_headers_in_t, if_range),
106 ngx_http_process_unique_header_line },
108 { ngx_string("Transfer-Encoding"),
109 offsetof(ngx_http_headers_in_t, transfer_encoding),
110 ngx_http_process_header_line },
112 { ngx_string("Expect"),
113 offsetof(ngx_http_headers_in_t, expect),
114 ngx_http_process_unique_header_line },
117 { ngx_string("Accept-Encoding"),
118 offsetof(ngx_http_headers_in_t, accept_encoding),
119 ngx_http_process_header_line },
121 { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
122 ngx_http_process_header_line },
125 { ngx_string("Authorization"),
126 offsetof(ngx_http_headers_in_t, authorization),
127 ngx_http_process_unique_header_line },
129 { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
130 ngx_http_process_header_line },
132 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
133 { ngx_string("X-Forwarded-For"),
134 offsetof(ngx_http_headers_in_t, x_forwarded_for),
135 ngx_http_process_header_line },
138 #if (NGX_HTTP_REALIP)
139 { ngx_string("X-Real-IP"),
140 offsetof(ngx_http_headers_in_t, x_real_ip),
141 ngx_http_process_header_line },
144 #if (NGX_HTTP_HEADERS)
145 { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
146 ngx_http_process_header_line },
148 { ngx_string("Accept-Language"),
149 offsetof(ngx_http_headers_in_t, accept_language),
150 ngx_http_process_header_line },
154 { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
155 ngx_http_process_header_line },
157 { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
158 ngx_http_process_header_line },
160 { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite),
161 ngx_http_process_header_line },
163 { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date),
164 ngx_http_process_header_line },
167 { ngx_string("Cookie"), 0, ngx_http_process_cookie },
169 { ngx_null_string, 0, NULL }
174 ngx_http_init_connection(ngx_connection_t *c)
177 ngx_http_log_ctx_t *ctx;
179 ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
181 ngx_http_close_connection(c);
187 ctx->current_request = NULL;
189 c->log->connection = c->number;
190 c->log->handler = ngx_http_log_error;
192 c->log->action = "reading client request line";
194 c->log_error = NGX_ERROR_INFO;
197 rev->handler = ngx_http_init_request;
198 c->write->handler = ngx_http_empty_handler;
201 ngx_atomic_fetch_add(ngx_stat_reading, 1);
205 /* the deferred accept(), rtsig, aio, iocp */
207 if (ngx_use_accept_mutex) {
208 ngx_post_event(rev, &ngx_posted_events);
212 ngx_http_init_request(rev);
216 ngx_add_timer(rev, c->listening->post_accept_timeout);
218 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
220 ngx_atomic_fetch_add(ngx_stat_reading, -1);
222 ngx_http_close_connection(c);
229 ngx_http_init_request(ngx_event_t *rev)
234 ngx_http_request_t *r;
235 struct sockaddr_in *sin;
236 ngx_http_port_t *port;
237 ngx_http_in_addr_t *addr;
238 ngx_http_log_ctx_t *ctx;
239 ngx_http_addr_conf_t *addr_conf;
240 ngx_http_connection_t *hc;
241 ngx_http_core_srv_conf_t *cscf;
242 ngx_http_core_loc_conf_t *clcf;
243 ngx_http_core_main_conf_t *cmcf;
245 struct sockaddr_in6 *sin6;
246 ngx_http_in6_addr_t *addr6;
250 ngx_atomic_fetch_add(ngx_stat_reading, -1);
256 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
258 ngx_http_close_connection(c);
265 hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
267 ngx_http_close_connection(c);
275 ngx_memzero(r, sizeof(ngx_http_request_t));
277 r->pipeline = hc->pipeline;
280 r->header_in = hc->busy[0];
284 r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
286 ngx_http_close_connection(c);
294 r->http_connection = hc;
297 r->signature = NGX_HTTP_MODULE;
299 /* find the server configuration for the address:port */
301 port = c->listening->servers;
303 r->port = port->port;
304 r->port_text = &port->port_text;
308 if (port->naddrs > 1) {
311 * there are several addresses on this port and one of them
312 * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
313 * is required to determine a server address
316 c->local_sockaddr = NULL;
318 if (ngx_http_server_addr(r, NULL) != NGX_OK) {
319 ngx_http_close_connection(c);
323 switch (c->local_sockaddr->sa_family) {
327 sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
329 addr6 = (ngx_http_in6_addr_t *) port->addrs;
331 /* the last address is "*" */
333 for (i = 0; i < port->naddrs - 1; i++) {
334 if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
339 addr_conf = &addr6[i].conf;
344 default: /* AF_INET */
345 sin = (struct sockaddr_in *) c->local_sockaddr;
349 /* the last address is "*" */
351 for (i = 0; i < port->naddrs - 1; i++) {
352 if (addr[i].addr == sin->sin_addr.s_addr) {
357 addr_conf = &addr[i].conf;
364 switch (c->local_sockaddr->sa_family) {
368 addr6 = (ngx_http_in6_addr_t *) port->addrs;
369 addr_conf = &addr6[0].conf;
373 default: /* AF_INET */
375 addr_conf = &addr[0].conf;
376 r->in_addr = addr[0].addr;
381 r->virtual_names = addr_conf->virtual_names;
383 /* the default server configuration for the address:port */
384 cscf = addr_conf->core_srv_conf;
386 r->main_conf = cscf->ctx->main_conf;
387 r->srv_conf = cscf->ctx->srv_conf;
388 r->loc_conf = cscf->ctx->loc_conf;
390 rev->handler = ngx_http_process_request_line;
395 ngx_http_ssl_srv_conf_t *sscf;
397 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
398 if (sscf->enable || addr_conf->ssl) {
400 if (c->ssl == NULL) {
402 c->log->action = "SSL handshaking";
404 if (addr_conf->ssl && sscf->ssl.ctx == NULL) {
405 ngx_log_error(NGX_LOG_ERR, c->log, 0,
406 "no \"ssl_certificate\" is defined "
407 "in server listening on SSL port");
408 ngx_http_close_connection(c);
412 if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
415 ngx_http_close_connection(c);
419 rev->handler = ngx_http_ssl_handshake;
422 r->main_filter_need_in_memory = 1;
428 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
429 c->log->file = clcf->err_log->file;
430 if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
431 c->log->log_level = clcf->err_log->log_level;
434 if (c->buffer == NULL) {
435 c->buffer = ngx_create_temp_buf(c->pool,
436 cscf->client_header_buffer_size);
437 if (c->buffer == NULL) {
438 ngx_http_close_connection(c);
443 if (r->header_in == NULL) {
444 r->header_in = c->buffer;
447 r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
448 if (r->pool == NULL) {
449 ngx_http_close_connection(c);
454 if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
455 sizeof(ngx_table_elt_t))
458 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
462 r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
463 if (r->ctx == NULL) {
464 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
468 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
470 r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
471 * sizeof(ngx_http_variable_value_t));
472 if (r->variables == NULL) {
473 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
477 c->single_connection = 1;
482 tp = ngx_timeofday();
483 r->start_sec = tp->sec;
484 r->start_msec = tp->msec;
486 r->method = NGX_HTTP_UNKNOWN;
488 r->headers_in.content_length_n = -1;
489 r->headers_in.keep_alive_n = -1;
490 r->headers_out.content_length_n = -1;
491 r->headers_out.last_modified_time = -1;
493 r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
494 r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
496 r->http_state = NGX_HTTP_READING_REQUEST_STATE;
500 ctx->current_request = r;
501 r->log_handler = ngx_http_log_error_handler;
504 ngx_atomic_fetch_add(ngx_stat_reading, 1);
506 ngx_atomic_fetch_add(ngx_stat_requests, 1);
516 ngx_http_ssl_handshake(ngx_event_t *rev)
522 ngx_http_request_t *r;
527 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
528 "http check ssl handshake");
531 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
533 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
537 n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
539 if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
541 if (!rev->timer_set) {
542 ngx_add_timer(rev, c->listening->post_accept_timeout);
545 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
546 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
553 if (buf[0] == 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
554 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
555 "https ssl handshake: 0x%02Xd", buf[0]);
557 rc = ngx_ssl_handshake(c);
559 if (rc == NGX_AGAIN) {
561 if (!rev->timer_set) {
562 ngx_add_timer(rev, c->listening->post_accept_timeout);
565 c->ssl->handler = ngx_http_ssl_handshake_handler;
569 ngx_http_ssl_handshake_handler(c);
574 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
581 c->log->action = "reading client request line";
583 rev->handler = ngx_http_process_request_line;
584 ngx_http_process_request_line(rev);
589 ngx_http_ssl_handshake_handler(ngx_connection_t *c)
591 ngx_http_request_t *r;
593 if (c->ssl->handshaked) {
596 * The majority of browsers do not send the "close notify" alert.
597 * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
598 * and Links. And what is more, MSIE ignores the server's alert.
600 * Opera and recent Mozilla send the alert.
603 c->ssl->no_wait_shutdown = 1;
605 c->read->handler = ngx_http_process_request_line;
606 /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
608 ngx_http_process_request_line(c->read);
615 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
620 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
623 ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
626 const char *servername;
628 ngx_http_request_t *r;
629 ngx_http_ssl_srv_conf_t *sscf;
631 servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
633 if (servername == NULL) {
634 return SSL_TLSEXT_ERR_NOACK;
637 c = ngx_ssl_get_connection(ssl_conn);
639 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
640 "SSL server name: \"%s\"", servername);
642 len = ngx_strlen(servername);
645 return SSL_TLSEXT_ERR_NOACK;
650 if (ngx_http_find_virtual_server(r, (u_char *) servername, len) != NGX_OK) {
651 return SSL_TLSEXT_ERR_NOACK;
654 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
656 SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
658 return SSL_TLSEXT_ERR_OK;
667 ngx_http_process_request_line(ngx_event_t *rev)
672 ngx_http_request_t *r;
673 ngx_http_core_srv_conf_t *cscf;
678 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
679 "http process request line");
682 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
684 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
692 if (rc == NGX_AGAIN) {
693 n = ngx_http_read_request_header(r);
695 if (n == NGX_AGAIN || n == NGX_ERROR) {
700 rc = ngx_http_parse_request_line(r, r->header_in);
704 /* the request line has been parsed successfully */
706 r->request_line.len = r->request_end - r->request_start;
707 r->request_line.data = r->request_start;
708 *r->request_end = '\0';
712 r->uri.len = r->args_start - 1 - r->uri_start;
714 r->uri.len = r->uri_end - r->uri_start;
718 if (r->complex_uri || r->quoted_uri) {
720 r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
721 if (r->uri.data == NULL) {
722 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
726 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
728 rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
730 if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
731 ngx_log_error(NGX_LOG_INFO, c->log, 0,
732 "client sent invalid request");
733 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
738 r->uri.data = r->uri_start;
742 r->unparsed_uri.len = r->uri_end - r->uri_start;
743 r->unparsed_uri.data = r->uri_start;
746 r->method_name.len = r->method_end - r->request_start + 1;
747 r->method_name.data = r->request_line.data;
750 if (r->http_protocol.data) {
751 r->http_protocol.len = r->request_end - r->http_protocol.data;
757 r->exten.len = r->args_start - 1 - r->uri_ext;
759 r->exten.len = r->uri_end - r->uri_ext;
762 r->exten.data = r->uri_ext;
766 if (r->args_start && r->uri_end > r->args_start) {
767 r->args.len = r->uri_end - r->args_start;
768 r->args.data = r->args_start;
772 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
773 "http request line: \"%V\"", &r->request_line);
775 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
776 "http uri: \"%V\"", &r->uri);
778 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
779 "http args: \"%V\"", &r->args);
781 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
782 "http exten: \"%V\"", &r->exten);
784 if (r->host_start && r->host_end) {
785 n = ngx_http_validate_host(r->host_start,
786 r->host_end - r->host_start);
789 ngx_log_error(NGX_LOG_INFO, c->log, 0,
790 "client sent invalid host in request line");
791 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
795 r->headers_in.server.len = n;
796 r->headers_in.server.data = r->host_start;
799 if (r->http_version < NGX_HTTP_VERSION_10) {
801 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
802 r->headers_in.server.len)
805 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
809 ngx_http_process_request(r);
814 if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
815 sizeof(ngx_table_elt_t))
818 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
823 if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
824 sizeof(ngx_table_elt_t *))
827 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
831 c->log->action = "reading client request headers";
833 rev->handler = ngx_http_process_request_headers;
834 ngx_http_process_request_headers(rev);
839 if (rc != NGX_AGAIN) {
841 /* there was error while a request line parsing */
843 ngx_log_error(NGX_LOG_INFO, c->log, 0,
844 ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
845 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
849 /* NGX_AGAIN: a request line parsing is still incomplete */
851 if (r->header_in->pos == r->header_in->end) {
853 rv = ngx_http_alloc_large_header_buffer(r, 1);
855 if (rv == NGX_ERROR) {
856 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
860 if (rv == NGX_DECLINED) {
861 r->request_line.len = r->header_in->end - r->request_start;
862 r->request_line.data = r->request_start;
864 ngx_log_error(NGX_LOG_INFO, c->log, 0,
865 "client sent too long URI");
866 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
875 ngx_http_process_request_headers(ngx_event_t *rev)
882 ngx_http_header_t *hh;
883 ngx_http_request_t *r;
884 ngx_http_core_srv_conf_t *cscf;
885 ngx_http_core_main_conf_t *cmcf;
890 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
891 "http process request header line");
894 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
896 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
900 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
901 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
907 if (rc == NGX_AGAIN) {
909 if (r->header_in->pos == r->header_in->end) {
911 rv = ngx_http_alloc_large_header_buffer(r, 0);
913 if (rv == NGX_ERROR) {
914 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
918 if (rv == NGX_DECLINED) {
919 header.len = r->header_in->end - r->header_name_start;
920 header.data = r->header_name_start;
922 if (header.len > NGX_MAX_ERROR_STR - 300) {
923 header.len = NGX_MAX_ERROR_STR - 300;
924 header.data[header.len++] = '.';
925 header.data[header.len++] = '.';
926 header.data[header.len++] = '.';
929 ngx_log_error(NGX_LOG_INFO, c->log, 0,
930 "client sent too long header line: \"%V\"",
932 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
937 n = ngx_http_read_request_header(r);
939 if (n == NGX_AGAIN || n == NGX_ERROR) {
944 rc = ngx_http_parse_header_line(r, r->header_in,
945 cscf->underscores_in_headers);
949 if (r->invalid_header && cscf->ignore_invalid_headers) {
951 /* there was error while a header line parsing */
953 header.len = r->header_end - r->header_name_start;
954 header.data = r->header_name_start;
956 ngx_log_error(NGX_LOG_INFO, c->log, 0,
957 "client sent invalid header line: \"%V\"",
962 /* a header line has been parsed successfully */
964 h = ngx_list_push(&r->headers_in.headers);
966 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
970 h->hash = r->header_hash;
972 h->key.len = r->header_name_end - r->header_name_start;
973 h->key.data = r->header_name_start;
974 h->key.data[h->key.len] = '\0';
976 h->value.len = r->header_end - r->header_start;
977 h->value.data = r->header_start;
978 h->value.data[h->value.len] = '\0';
980 h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
981 if (h->lowcase_key == NULL) {
982 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
986 if (h->key.len == r->lowcase_index) {
987 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
990 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
993 hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
994 h->lowcase_key, h->key.len);
996 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
1000 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1001 "http header: \"%V: %V\"",
1002 &h->key, &h->value);
1007 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1009 /* a whole header has been parsed successfully */
1011 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1012 "http header done");
1014 r->request_length += r->header_in->pos - r->header_in->start;
1016 r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
1018 rc = ngx_http_process_request_header(r);
1024 ngx_http_process_request(r);
1029 if (rc == NGX_AGAIN) {
1031 /* a header line parsing is still not complete */
1036 /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
1038 header.len = r->header_end - r->header_name_start;
1039 header.data = r->header_name_start;
1040 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1041 "client sent invalid header line: \"%V\\r...\"",
1043 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1050 ngx_http_read_request_header(ngx_http_request_t *r)
1054 ngx_connection_t *c;
1055 ngx_http_core_srv_conf_t *cscf;
1060 n = r->header_in->last - r->header_in->pos;
1067 n = c->recv(c, r->header_in->last,
1068 r->header_in->end - r->header_in->last);
1073 if (n == NGX_AGAIN) {
1074 if (!rev->timer_set) {
1075 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1076 ngx_add_timer(rev, cscf->client_header_timeout);
1079 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
1080 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1088 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1089 "client closed prematurely connection");
1092 if (n == 0 || n == NGX_ERROR) {
1094 c->log->action = "reading client request headers";
1096 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1100 r->header_in->last += n;
1107 ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
1108 ngx_uint_t request_line)
1112 ngx_http_connection_t *hc;
1113 ngx_http_core_srv_conf_t *cscf;
1115 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1116 "http alloc large header buffer");
1118 if (request_line && r->state == 0) {
1120 /* the client fills up the buffer with "\r\n" */
1122 r->request_length += r->header_in->end - r->header_in->start;
1124 r->header_in->pos = r->header_in->start;
1125 r->header_in->last = r->header_in->start;
1130 old = request_line ? r->request_start : r->header_name_start;
1132 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1135 && (size_t) (r->header_in->pos - old)
1136 >= cscf->large_client_header_buffers.size)
1138 return NGX_DECLINED;
1141 hc = r->http_connection;
1144 b = hc->free[--hc->nfree];
1146 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1147 "http large header free: %p %uz",
1148 b->pos, b->end - b->last);
1150 } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
1152 if (hc->busy == NULL) {
1153 hc->busy = ngx_palloc(r->connection->pool,
1154 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
1155 if (hc->busy == NULL) {
1160 b = ngx_create_temp_buf(r->connection->pool,
1161 cscf->large_client_header_buffers.size);
1166 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1167 "http large header alloc: %p %uz",
1168 b->pos, b->end - b->last);
1171 return NGX_DECLINED;
1174 hc->busy[hc->nbusy++] = b;
1176 if (r->state == 0) {
1178 * r->state == 0 means that a header line was parsed successfully
1179 * and we do not need to copy incomplete header line and
1180 * to relocate the parser header pointers
1183 r->request_length += r->header_in->end - r->header_in->start;
1190 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1191 "http large header copy: %d", r->header_in->pos - old);
1193 r->request_length += old - r->header_in->start;
1197 ngx_memcpy(new, old, r->header_in->pos - old);
1199 b->pos = new + (r->header_in->pos - old);
1200 b->last = new + (r->header_in->pos - old);
1203 r->request_start = new;
1205 if (r->request_end) {
1206 r->request_end = new + (r->request_end - old);
1209 r->method_end = new + (r->method_end - old);
1211 r->uri_start = new + (r->uri_start - old);
1212 r->uri_end = new + (r->uri_end - old);
1214 if (r->schema_start) {
1215 r->schema_start = new + (r->schema_start - old);
1216 r->schema_end = new + (r->schema_end - old);
1219 if (r->host_start) {
1220 r->host_start = new + (r->host_start - old);
1222 r->host_end = new + (r->host_end - old);
1226 if (r->port_start) {
1227 r->port_start = new + (r->port_start - old);
1228 r->port_end = new + (r->port_end - old);
1232 r->uri_ext = new + (r->uri_ext - old);
1235 if (r->args_start) {
1236 r->args_start = new + (r->args_start - old);
1239 if (r->http_protocol.data) {
1240 r->http_protocol.data = new + (r->http_protocol.data - old);
1244 r->header_name_start = new;
1245 r->header_name_end = new + (r->header_name_end - old);
1246 r->header_start = new + (r->header_start - old);
1247 r->header_end = new + (r->header_end - old);
1257 ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1260 ngx_table_elt_t **ph;
1262 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1273 ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1276 ngx_table_elt_t **ph;
1278 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1285 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1286 "client sent duplicate header line: \"%V: %V\", "
1287 "previous value: \"%V: %V\"",
1288 &h->key, &h->value, &(*ph)->key, &(*ph)->value);
1290 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1297 ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
1302 if (r->headers_in.host == NULL) {
1303 r->headers_in.host = h;
1306 len = ngx_http_validate_host(h->value.data, h->value.len);
1309 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1310 "client sent invalid host header");
1311 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1315 if (r->headers_in.server.len) {
1319 r->headers_in.server.len = len;
1320 r->headers_in.server.data = h->value.data;
1327 ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
1330 if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
1331 r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
1333 } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
1334 r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
1342 ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
1345 u_char *user_agent, *msie;
1347 if (r->headers_in.user_agent) {
1351 r->headers_in.user_agent = h;
1353 /* check some widespread browsers while the header is in CPU cache */
1355 user_agent = h->value.data;
1357 msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
1359 if (msie && msie + 7 < user_agent + h->value.len) {
1361 r->headers_in.msie = 1;
1363 if (msie[6] == '.') {
1367 r->headers_in.msie4 = 1;
1371 r->headers_in.msie6 = 1;
1376 /* MSIE ignores the SSL "close notify" alert */
1378 c->ssl->no_send_shutdown = 1;
1383 if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
1384 r->headers_in.opera = 1;
1385 r->headers_in.msie = 0;
1386 r->headers_in.msie4 = 0;
1387 r->headers_in.msie6 = 0;
1390 if (!r->headers_in.msie && !r->headers_in.opera) {
1392 if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
1393 r->headers_in.gecko = 1;
1395 } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
1396 r->headers_in.konqueror = 1;
1405 ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
1408 ngx_table_elt_t **cookie;
1410 cookie = ngx_array_push(&r->headers_in.cookies);
1416 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1423 ngx_http_process_request_header(ngx_http_request_t *r)
1425 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
1426 r->headers_in.server.len)
1429 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1433 if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
1434 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1435 "client sent HTTP/1.1 request without \"Host\" header");
1436 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1440 if (r->headers_in.content_length) {
1441 r->headers_in.content_length_n =
1442 ngx_atoof(r->headers_in.content_length->value.data,
1443 r->headers_in.content_length->value.len);
1445 if (r->headers_in.content_length_n == NGX_ERROR) {
1446 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1447 "client sent invalid \"Content-Length\" header");
1448 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1453 if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
1454 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1455 "client sent %V method without \"Content-Length\" header",
1457 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1461 if (r->method & NGX_HTTP_TRACE) {
1462 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1463 "client sent TRACE method");
1464 ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
1468 if (r->headers_in.transfer_encoding
1469 && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
1472 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1473 "client sent \"Transfer-Encoding: chunked\" header");
1474 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1478 if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
1479 if (r->headers_in.keep_alive) {
1480 r->headers_in.keep_alive_n =
1481 ngx_atotm(r->headers_in.keep_alive->value.data,
1482 r->headers_in.keep_alive->value.len);
1491 ngx_http_process_request(ngx_http_request_t *r)
1493 ngx_connection_t *c;
1497 if (r->plain_http) {
1498 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1499 "client sent plain HTTP request to HTTPS port");
1500 ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
1509 ngx_http_ssl_srv_conf_t *sscf;
1511 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
1513 if (sscf->verify == 1) {
1514 rc = SSL_get_verify_result(c->ssl->connection);
1516 if (rc != X509_V_OK) {
1517 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1518 "client SSL certificate verify error: (%l:%s)",
1519 rc, X509_verify_cert_error_string(rc));
1521 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1522 (SSL_get0_session(c->ssl->connection)));
1524 ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
1528 cert = SSL_get_peer_certificate(c->ssl->connection);
1531 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1532 "client sent no required SSL certificate");
1534 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1535 (SSL_get0_session(c->ssl->connection)));
1537 ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
1547 if (c->read->timer_set) {
1548 ngx_del_timer(c->read);
1552 ngx_atomic_fetch_add(ngx_stat_reading, -1);
1553 r->stat_reading = 0;
1554 ngx_atomic_fetch_add(ngx_stat_writing, 1);
1555 r->stat_writing = 1;
1558 c->read->handler = ngx_http_request_handler;
1559 c->write->handler = ngx_http_request_handler;
1560 r->read_event_handler = ngx_http_block_reading;
1562 ngx_http_handler(r);
1564 ngx_http_run_posted_requests(c);
1569 ngx_http_validate_host(u_char *host, size_t len)
1578 for (i = 0; i < len; i++) {
1597 if (ch == '/' || ch == '\0') {
1617 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
1621 ngx_http_core_loc_conf_t *clcf;
1622 ngx_http_core_srv_conf_t *cscf;
1625 if (r->virtual_names == NULL) {
1626 return NGX_DECLINED;
1633 server = ngx_pnalloc(r->pool, len);
1634 if (server == NULL) {
1639 hash = ngx_hash_strlow(server, host, len);
1641 cscf = ngx_hash_find_combined(&r->virtual_names->names, hash, server, len);
1649 if (r->virtual_names->nregex) {
1653 ngx_http_server_name_t *sn;
1658 sn = r->virtual_names->regex;
1660 for (i = 0; i < r->virtual_names->nregex; i++) {
1662 n = ngx_regex_exec(sn[i].regex, &name, NULL, 0);
1664 if (n == NGX_REGEX_NO_MATCHED) {
1669 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1671 " failed: %d on \"%V\" using \"%V\"",
1672 n, &name, &sn[i].name);
1678 cscf = sn[i].core_srv_conf;
1690 r->srv_conf = cscf->ctx->srv_conf;
1691 r->loc_conf = cscf->ctx->loc_conf;
1693 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1694 r->connection->log->file = clcf->err_log->file;
1696 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
1697 r->connection->log->log_level = clcf->err_log->log_level;
1705 ngx_http_request_handler(ngx_event_t *ev)
1707 ngx_connection_t *c;
1708 ngx_http_request_t *r;
1709 ngx_http_log_ctx_t *ctx;
1715 ctx->current_request = r;
1717 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1718 "http run request: \"%V?%V\"", &r->uri, &r->args);
1721 r->write_event_handler(r);
1724 r->read_event_handler(r);
1727 ngx_http_run_posted_requests(c);
1732 ngx_http_run_posted_requests(ngx_connection_t *c)
1734 ngx_http_request_t *r;
1735 ngx_http_log_ctx_t *ctx;
1736 ngx_http_posted_request_t *pr;
1745 pr = r->main->posted_requests;
1751 r->main->posted_requests = pr->next;
1756 ctx->current_request = r;
1758 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1759 "http posted request: \"%V?%V\"", &r->uri, &r->args);
1761 r->write_event_handler(r);
1767 ngx_http_post_request(ngx_http_request_t *r)
1769 ngx_http_posted_request_t *pr, **p;
1771 pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
1779 for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
1788 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1790 ngx_connection_t *c;
1791 ngx_http_request_t *pr;
1792 ngx_http_core_loc_conf_t *clcf;
1794 if (rc == NGX_DONE) {
1795 /* the request pool may be already destroyed */
1801 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
1802 "http finalize request: %d, \"%V?%V\" %d",
1803 rc, &r->uri, &r->args, r == c->data);
1805 if (rc == NGX_DECLINED) {
1806 r->content_handler = NULL;
1807 r->write_event_handler = ngx_http_core_run_phases;
1808 ngx_http_core_run_phases(r);
1812 if (r != r->main && r->post_subrequest) {
1813 rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
1817 || rc == NGX_HTTP_REQUEST_TIME_OUT
1818 || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
1821 if (rc > 0 && r->headers_out.status == 0) {
1822 r->headers_out.status = rc;
1825 if (ngx_http_post_action(r) == NGX_OK) {
1829 ngx_http_close_request(r, 0);
1833 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
1834 || rc == NGX_HTTP_CREATED
1835 || rc == NGX_HTTP_NO_CONTENT)
1837 if (rc == NGX_HTTP_CLOSE) {
1838 ngx_http_close_request(r, rc);
1843 if (c->read->timer_set) {
1844 ngx_del_timer(c->read);
1847 if (c->write->timer_set) {
1848 ngx_del_timer(c->write);
1852 c->read->handler = ngx_http_request_handler;
1853 c->write->handler = ngx_http_request_handler;
1855 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
1861 if (r->buffered || r->postponed) {
1863 if (ngx_http_set_write_handler(r) != NGX_OK) {
1864 ngx_http_close_request(r->main, 0);
1872 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1873 "http finalize non-active request: \"%V?%V\"",
1884 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1886 if (clcf->log_subrequest) {
1887 ngx_http_log_request(r);
1893 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1894 "subrequest: \"%V?%V\" logged again",
1900 if (pr->postponed && pr->postponed->request == r) {
1901 pr->postponed = pr->postponed->next;
1908 r->write_event_handler = ngx_http_request_finalizer;
1915 if (ngx_http_post_request(pr) != NGX_OK) {
1916 ngx_http_close_request(r->main, 0);
1920 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1921 "http wake parent request: \"%V?%V\"",
1922 &pr->uri, &pr->args);
1927 if (r->buffered || c->buffered || r->postponed) {
1929 if (ngx_http_set_write_handler(r) != NGX_OK) {
1930 ngx_http_close_request(r, 0);
1937 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1938 "http finalize non-active request: \"%V?%V\"",
1945 if (!r->post_action) {
1946 r->request_complete = 1;
1949 if (ngx_http_post_action(r) == NGX_OK) {
1953 if (c->read->timer_set) {
1954 ngx_del_timer(c->read);
1957 if (c->write->timer_set) {
1958 c->write->delayed = 0;
1959 ngx_del_timer(c->write);
1967 ngx_http_close_request(r, 0);
1971 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1976 && clcf->keepalive_timeout > 0)
1978 ngx_http_set_keepalive(r);
1981 } else if (r->lingering_close && clcf->lingering_timeout > 0) {
1982 ngx_http_set_lingering_close(r);
1986 ngx_http_close_request(r, 0);
1991 ngx_http_set_write_handler(ngx_http_request_t *r)
1994 ngx_http_core_loc_conf_t *clcf;
1996 r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
1998 r->read_event_handler = ngx_http_test_reading;
1999 r->write_event_handler = ngx_http_writer;
2001 wev = r->connection->write;
2003 if (wev->ready && wev->delayed) {
2007 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2008 if (!wev->delayed) {
2009 ngx_add_timer(wev, clcf->send_timeout);
2012 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2013 ngx_http_close_request(r, 0);
2022 ngx_http_writer(ngx_http_request_t *r)
2026 ngx_connection_t *c;
2027 ngx_http_core_loc_conf_t *clcf;
2032 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2033 "http writer handler: \"%V?%V\"", &r->uri, &r->args);
2035 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
2037 if (wev->timedout) {
2038 if (!wev->delayed) {
2039 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
2040 "client timed out");
2043 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
2051 ngx_add_timer(wev, clcf->send_timeout);
2053 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2054 ngx_http_close_request(r, 0);
2062 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2063 "http writer delayed");
2065 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2066 ngx_http_close_request(r, 0);
2073 rc = ngx_http_output_filter(r, NULL);
2079 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
2080 "http writer output filter: %d, \"%V?%V\"",
2081 rc, &r->uri, &r->args);
2083 if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
2085 if (!wev->ready && !wev->delayed) {
2086 ngx_add_timer(wev, clcf->send_timeout);
2089 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2090 ngx_http_close_request(r, 0);
2096 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2097 "http writer done: \"%V?%V\"", &r->uri, &r->args);
2099 ngx_http_finalize_request(r, rc);
2104 ngx_http_request_finalizer(ngx_http_request_t *r)
2106 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2107 "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
2109 ngx_http_finalize_request(r, 0);
2114 ngx_http_block_reading(ngx_http_request_t *r)
2116 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2117 "http reading blocked");
2119 /* aio does not call this handler */
2121 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
2122 && r->connection->read->active)
2124 if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
2125 ngx_http_close_request(r, 0);
2132 ngx_http_test_reading(ngx_http_request_t *r)
2138 ngx_connection_t *c;
2143 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
2145 #if (NGX_HAVE_KQUEUE)
2147 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2149 if (!rev->pending_eof) {
2155 err = rev->kq_errno;
2162 n = recv(c->fd, buf, 1, MSG_PEEK);
2171 } else if (n == -1) {
2172 err = ngx_socket_errno;
2174 if (err != NGX_EAGAIN) {
2182 /* aio does not call this handler */
2184 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
2186 if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
2187 ngx_http_close_request(r, 0);
2199 ngx_log_error(NGX_LOG_INFO, c->log, err,
2200 "client closed prematurely connection");
2202 ngx_http_finalize_request(r, 0);
2207 ngx_http_set_keepalive(ngx_http_request_t *r)
2212 ngx_event_t *rev, *wev;
2213 ngx_connection_t *c;
2214 ngx_http_connection_t *hc;
2215 ngx_http_core_srv_conf_t *cscf;
2216 ngx_http_core_loc_conf_t *clcf;
2221 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2223 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
2225 if (r->discard_body) {
2226 r->write_event_handler = ngx_http_request_empty_handler;
2227 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2228 ngx_add_timer(rev, clcf->lingering_timeout);
2232 c->log->action = "closing request";
2234 hc = r->http_connection;
2237 if (b->pos < b->last) {
2239 /* the pipelined request */
2241 if (b != c->buffer) {
2244 * If the large header buffers were allocated while the previous
2245 * request processing then we do not use c->buffer for
2246 * the pipelined request (see ngx_http_init_request()).
2248 * Now we would move the large header buffers to the free list.
2251 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2253 if (hc->free == NULL) {
2254 hc->free = ngx_palloc(c->pool,
2255 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
2257 if (hc->free == NULL) {
2258 ngx_http_close_request(r, 0);
2263 for (i = 0; i < hc->nbusy - 1; i++) {
2265 hc->free[hc->nfree++] = f;
2275 ngx_http_request_done(r, 0);
2279 ngx_add_timer(rev, clcf->keepalive_timeout);
2281 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2282 ngx_http_close_connection(c);
2287 wev->handler = ngx_http_empty_handler;
2289 if (b->pos < b->last) {
2291 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
2294 ngx_atomic_fetch_add(ngx_stat_reading, 1);
2298 c->log->action = "reading client pipelined request line";
2300 rev->handler = ngx_http_init_request;
2301 ngx_post_event(rev, &ngx_posted_events);
2308 * To keep a memory footprint as small as possible for an idle
2309 * keepalive connection we try to free the ngx_http_request_t and
2310 * c->buffer's memory if they were allocated outside the c->pool.
2311 * The large header buffers are always allocated outside the c->pool and
2315 if (ngx_pfree(c->pool, r) == NGX_OK) {
2321 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
2324 * the special note for ngx_http_keepalive_handler() that
2325 * c->buffer's memory was freed
2335 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
2336 hc->free, hc->nfree);
2339 for (i = 0; i < hc->nfree; i++) {
2340 ngx_pfree(c->pool, hc->free[i]->start);
2347 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
2348 hc->busy, hc->nbusy);
2351 for (i = 0; i < hc->nbusy; i++) {
2352 ngx_pfree(c->pool, hc->busy[i]->start);
2361 ngx_ssl_free_buffer(c);
2365 rev->handler = ngx_http_keepalive_handler;
2367 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2368 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2369 ngx_http_close_connection(c);
2374 c->log->action = "keepalive";
2376 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
2377 if (ngx_tcp_push(c->fd) == -1) {
2378 ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
2379 ngx_http_close_connection(c);
2383 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
2384 tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
2391 && clcf->tcp_nodelay
2392 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
2394 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
2396 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
2397 (const void *) &tcp_nodelay, sizeof(int))
2400 ngx_connection_error(c, ngx_socket_errno,
2401 "setsockopt(TCP_NODELAY) failed");
2402 ngx_http_close_connection(c);
2406 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
2410 /* if ngx_http_request_t was freed then we need some other place */
2411 r->http_state = NGX_HTTP_KEEPALIVE_STATE;
2417 ngx_post_event(rev, &ngx_posted_events);
2423 ngx_http_keepalive_handler(ngx_event_t *rev)
2428 ngx_connection_t *c;
2432 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
2434 if (rev->timedout || c->close) {
2435 ngx_http_close_connection(c);
2439 #if (NGX_HAVE_KQUEUE)
2441 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2442 if (rev->pending_eof) {
2443 c->log->handler = NULL;
2444 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
2445 "kevent() reported that client %V closed "
2446 "keepalive connection", &c->addr_text);
2449 c->ssl->no_send_shutdown = 1;
2452 ngx_http_close_connection(c);
2460 size = b->end - b->start;
2462 if (b->pos == NULL) {
2465 * The c->buffer's memory was freed by ngx_http_set_keepalive().
2466 * However, the c->buffer->start and c->buffer->end were not changed
2467 * to keep the buffer size.
2470 b->pos = ngx_palloc(c->pool, size);
2471 if (b->pos == NULL) {
2472 ngx_http_close_connection(c);
2478 b->end = b->pos + size;
2482 * MSIE closes a keepalive connection with RST flag
2483 * so we ignore ECONNRESET here.
2486 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
2487 ngx_set_socket_errno(0);
2489 n = c->recv(c, b->last, size);
2490 c->log_error = NGX_ERROR_INFO;
2492 if (n == NGX_AGAIN) {
2493 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2494 ngx_http_close_connection(c);
2500 if (n == NGX_ERROR) {
2501 ngx_http_close_connection(c);
2505 c->log->handler = NULL;
2508 ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
2509 "client %V closed keepalive connection", &c->addr_text);
2510 ngx_http_close_connection(c);
2517 ngx_atomic_fetch_add(ngx_stat_reading, 1);
2520 c->log->handler = ngx_http_log_error;
2521 c->log->action = "reading client request line";
2525 ngx_http_init_request(rev);
2530 ngx_http_set_lingering_close(ngx_http_request_t *r)
2532 ngx_event_t *rev, *wev;
2533 ngx_connection_t *c;
2534 ngx_http_core_loc_conf_t *clcf;
2538 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2541 rev->handler = ngx_http_lingering_close_handler;
2543 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2544 ngx_add_timer(rev, clcf->lingering_timeout);
2546 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2547 ngx_http_close_request(r, 0);
2552 wev->handler = ngx_http_empty_handler;
2554 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2555 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2556 ngx_http_close_request(r, 0);
2561 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
2562 ngx_connection_error(c, ngx_socket_errno,
2563 ngx_shutdown_socket_n " failed");
2564 ngx_http_close_request(r, 0);
2569 ngx_http_lingering_close_handler(rev);
2575 ngx_http_lingering_close_handler(ngx_event_t *rev)
2579 ngx_connection_t *c;
2580 ngx_http_request_t *r;
2581 ngx_http_core_loc_conf_t *clcf;
2582 u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
2587 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2588 "http lingering close handler");
2590 if (rev->timedout) {
2592 ngx_http_close_request(r, 0);
2596 timer = (ngx_msec_t) (r->lingering_time - ngx_time());
2598 ngx_http_close_request(r, 0);
2603 n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
2605 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
2607 if (n == NGX_ERROR || n == 0) {
2608 ngx_http_close_request(r, 0);
2612 } while (rev->ready);
2614 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2615 ngx_http_close_request(r, 0);
2619 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2623 if (timer > clcf->lingering_timeout) {
2624 timer = clcf->lingering_timeout;
2627 ngx_add_timer(rev, timer);
2632 ngx_http_empty_handler(ngx_event_t *wev)
2634 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
2641 ngx_http_request_empty_handler(ngx_http_request_t *r)
2643 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2644 "http request empty handler");
2651 ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
2656 b = ngx_calloc_buf(r->pool);
2661 if (flags & NGX_HTTP_LAST) {
2665 if (flags & NGX_HTTP_FLUSH) {
2672 return ngx_http_output_filter(r, &out);
2677 ngx_http_post_action(ngx_http_request_t *r)
2679 ngx_http_core_loc_conf_t *clcf;
2681 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2683 if (clcf->post_action.data == NULL) {
2684 return NGX_DECLINED;
2687 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2688 "post action: \"%V\"", &clcf->post_action);
2690 r->http_version = NGX_HTTP_VERSION_9;
2694 r->read_event_handler = ngx_http_block_reading;
2696 if (clcf->post_action.data[0] == '/') {
2697 ngx_http_internal_redirect(r, &clcf->post_action, NULL);
2700 ngx_http_named_location(r, &clcf->post_action);
2708 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
2710 ngx_connection_t *c;
2714 ngx_http_request_done(r->main, error);
2715 ngx_http_close_connection(c);
2720 ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
2723 struct linger linger;
2724 ngx_http_cleanup_t *cln;
2725 ngx_http_log_ctx_t *ctx;
2726 ngx_http_core_loc_conf_t *clcf;
2728 log = r->connection->log;
2730 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
2732 if (r->pool == NULL) {
2733 ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
2737 for (cln = r->cleanup; cln; cln = cln->next) {
2739 cln->handler(cln->data);
2745 if (r->stat_reading) {
2746 ngx_atomic_fetch_add(ngx_stat_reading, -1);
2749 if (r->stat_writing) {
2750 ngx_atomic_fetch_add(ngx_stat_writing, -1);
2755 if (error && r->headers_out.status == 0) {
2756 r->headers_out.status = error;
2759 log->action = "logging request";
2761 ngx_http_log_request(r);
2763 log->action = "closing request";
2765 if (r->connection->timedout) {
2766 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2768 if (clcf->reset_timedout_connection) {
2770 linger.l_linger = 0;
2772 if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
2773 (const void *) &linger, sizeof(struct linger)) == -1)
2775 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
2776 "setsockopt(SO_LINGER) failed");
2781 /* the various request strings were allocated from r->pool */
2783 ctx->request = NULL;
2785 r->request_line.len = 0;
2787 r->connection->destroyed = 1;
2789 ngx_destroy_pool(r->pool);
2794 ngx_http_log_request(ngx_http_request_t *r)
2797 ngx_http_handler_pt *log_handler;
2798 ngx_http_core_main_conf_t *cmcf;
2800 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
2802 log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
2803 n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
2805 for (i = 0; i < n; i++) {
2812 ngx_http_close_connection(ngx_connection_t *c)
2816 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
2817 "close http connection: %d", c->fd);
2822 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
2823 c->ssl->handler = ngx_http_close_connection;
2831 ngx_atomic_fetch_add(ngx_stat_active, -1);
2838 ngx_close_connection(c);
2840 ngx_destroy_pool(pool);
2845 ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
2848 ngx_http_request_t *r;
2849 ngx_http_log_ctx_t *ctx;
2852 p = ngx_snprintf(buf, len, " while %s", log->action);
2859 p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
2865 return r->log_handler(r, ctx->current_request, p, len);
2868 p = ngx_snprintf(p, len, ", server: %V",
2869 &ctx->connection->listening->addr_text);
2877 ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
2878 u_char *buf, size_t len)
2880 char *uri_separator;
2882 ngx_http_upstream_t *u;
2883 ngx_http_core_srv_conf_t *cscf;
2885 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2887 p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
2891 if (r->request_line.data == NULL && r->request_start) {
2892 for (p = r->request_start; p < r->header_in->last; p++) {
2893 if (*p == CR || *p == LF) {
2898 r->request_line.len = p - r->request_start;
2899 r->request_line.data = r->request_start;
2902 if (r->request_line.len) {
2903 p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
2909 p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
2916 if (u && u->peer.name) {
2920 #if (NGX_HAVE_UNIX_DOMAIN)
2921 if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
2922 uri_separator = ":";
2926 p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
2927 &u->schema, u->peer.name,
2928 uri_separator, &u->uri);
2933 if (r->headers_in.host) {
2934 p = ngx_snprintf(buf, len, ", host: \"%V\"",
2935 &r->headers_in.host->value);
2940 if (r->headers_in.referer) {
2941 p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
2942 &r->headers_in.referer->value);