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;
305 if (port->naddrs > 1) {
308 * there are several addresses on this port and one of them
309 * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
310 * is required to determine a server address
313 c->local_sockaddr = NULL;
315 if (ngx_http_server_addr(r, NULL) != NGX_OK) {
316 ngx_http_close_connection(c);
320 switch (c->local_sockaddr->sa_family) {
324 sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
326 addr6 = (ngx_http_in6_addr_t *) port->addrs;
328 /* the last address is "*" */
330 for (i = 0; i < port->naddrs - 1; i++) {
331 if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
336 addr_conf = &addr6[i].conf;
341 default: /* AF_INET */
342 sin = (struct sockaddr_in *) c->local_sockaddr;
346 /* the last address is "*" */
348 for (i = 0; i < port->naddrs - 1; i++) {
349 if (addr[i].addr == sin->sin_addr.s_addr) {
354 addr_conf = &addr[i].conf;
361 switch (c->local_sockaddr->sa_family) {
365 addr6 = (ngx_http_in6_addr_t *) port->addrs;
366 addr_conf = &addr6[0].conf;
370 default: /* AF_INET */
372 addr_conf = &addr[0].conf;
377 /* the default server configuration for the address:port */
378 cscf = addr_conf->core_srv_conf;
380 r->main_conf = cscf->ctx->main_conf;
381 r->srv_conf = cscf->ctx->srv_conf;
382 r->loc_conf = cscf->ctx->loc_conf;
384 rev->handler = ngx_http_process_request_line;
389 ngx_http_ssl_srv_conf_t *sscf;
391 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
392 if (sscf->enable || addr_conf->ssl) {
394 if (c->ssl == NULL) {
396 c->log->action = "SSL handshaking";
398 if (addr_conf->ssl && sscf->ssl.ctx == NULL) {
399 ngx_log_error(NGX_LOG_ERR, c->log, 0,
400 "no \"ssl_certificate\" is defined "
401 "in server listening on SSL port");
402 ngx_http_close_connection(c);
406 if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
409 ngx_http_close_connection(c);
413 rev->handler = ngx_http_ssl_handshake;
416 r->main_filter_need_in_memory = 1;
422 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
423 c->log->file = clcf->err_log->file;
424 if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
425 c->log->log_level = clcf->err_log->log_level;
428 if (c->buffer == NULL) {
429 c->buffer = ngx_create_temp_buf(c->pool,
430 cscf->client_header_buffer_size);
431 if (c->buffer == NULL) {
432 ngx_http_close_connection(c);
437 if (r->header_in == NULL) {
438 r->header_in = c->buffer;
441 r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
442 if (r->pool == NULL) {
443 ngx_http_close_connection(c);
448 if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
449 sizeof(ngx_table_elt_t))
452 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
456 r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
457 if (r->ctx == NULL) {
458 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
462 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
464 r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
465 * sizeof(ngx_http_variable_value_t));
466 if (r->variables == NULL) {
467 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
471 c->single_connection = 1;
476 tp = ngx_timeofday();
477 r->start_sec = tp->sec;
478 r->start_msec = tp->msec;
480 r->method = NGX_HTTP_UNKNOWN;
482 r->headers_in.content_length_n = -1;
483 r->headers_in.keep_alive_n = -1;
484 r->headers_out.content_length_n = -1;
485 r->headers_out.last_modified_time = -1;
487 r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
488 r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
490 r->http_state = NGX_HTTP_READING_REQUEST_STATE;
494 ctx->current_request = r;
495 r->log_handler = ngx_http_log_error_handler;
498 ngx_atomic_fetch_add(ngx_stat_reading, 1);
500 ngx_atomic_fetch_add(ngx_stat_requests, 1);
510 ngx_http_ssl_handshake(ngx_event_t *rev)
516 ngx_http_request_t *r;
521 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
522 "http check ssl handshake");
525 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
527 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
531 n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
533 if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
535 if (!rev->timer_set) {
536 ngx_add_timer(rev, c->listening->post_accept_timeout);
539 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
540 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
547 if (buf[0] == 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
548 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
549 "https ssl handshake: 0x%02Xd", buf[0]);
551 rc = ngx_ssl_handshake(c);
553 if (rc == NGX_AGAIN) {
555 if (!rev->timer_set) {
556 ngx_add_timer(rev, c->listening->post_accept_timeout);
559 c->ssl->handler = ngx_http_ssl_handshake_handler;
563 ngx_http_ssl_handshake_handler(c);
568 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
575 c->log->action = "reading client request line";
577 rev->handler = ngx_http_process_request_line;
578 ngx_http_process_request_line(rev);
583 ngx_http_ssl_handshake_handler(ngx_connection_t *c)
585 ngx_http_request_t *r;
587 if (c->ssl->handshaked) {
590 * The majority of browsers do not send the "close notify" alert.
591 * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
592 * and Links. And what is more, MSIE ignores the server's alert.
594 * Opera and recent Mozilla send the alert.
597 c->ssl->no_wait_shutdown = 1;
599 c->read->handler = ngx_http_process_request_line;
600 /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
602 ngx_http_process_request_line(c->read);
609 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
614 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
617 ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
620 const char *servername;
622 ngx_http_request_t *r;
623 ngx_http_ssl_srv_conf_t *sscf;
625 servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
627 if (servername == NULL) {
628 return SSL_TLSEXT_ERR_NOACK;
631 c = ngx_ssl_get_connection(ssl_conn);
633 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
634 "SSL server name: \"%s\"", servername);
636 len = ngx_strlen(servername);
639 return SSL_TLSEXT_ERR_NOACK;
644 if (ngx_http_find_virtual_server(r, (u_char *) servername, len) != NGX_OK) {
645 return SSL_TLSEXT_ERR_NOACK;
648 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
650 SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
652 return SSL_TLSEXT_ERR_OK;
661 ngx_http_process_request_line(ngx_event_t *rev)
666 ngx_http_request_t *r;
667 ngx_http_core_srv_conf_t *cscf;
672 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
673 "http process request line");
676 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
678 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
686 if (rc == NGX_AGAIN) {
687 n = ngx_http_read_request_header(r);
689 if (n == NGX_AGAIN || n == NGX_ERROR) {
694 rc = ngx_http_parse_request_line(r, r->header_in);
698 /* the request line has been parsed successfully */
700 r->request_line.len = r->request_end - r->request_start;
701 r->request_line.data = r->request_start;
702 *r->request_end = '\0';
706 r->uri.len = r->args_start - 1 - r->uri_start;
708 r->uri.len = r->uri_end - r->uri_start;
712 if (r->complex_uri || r->quoted_uri) {
714 r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
715 if (r->uri.data == NULL) {
716 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
720 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
722 rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
724 if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
725 ngx_log_error(NGX_LOG_INFO, c->log, 0,
726 "client sent invalid request");
727 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
732 r->uri.data = r->uri_start;
736 r->unparsed_uri.len = r->uri_end - r->uri_start;
737 r->unparsed_uri.data = r->uri_start;
740 r->method_name.len = r->method_end - r->request_start + 1;
741 r->method_name.data = r->request_line.data;
744 if (r->http_protocol.data) {
745 r->http_protocol.len = r->request_end - r->http_protocol.data;
751 r->exten.len = r->args_start - 1 - r->uri_ext;
753 r->exten.len = r->uri_end - r->uri_ext;
756 r->exten.data = r->uri_ext;
760 if (r->args_start && r->uri_end > r->args_start) {
761 r->args.len = r->uri_end - r->args_start;
762 r->args.data = r->args_start;
766 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
767 "http request line: \"%V\"", &r->request_line);
769 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
770 "http uri: \"%V\"", &r->uri);
772 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
773 "http args: \"%V\"", &r->args);
775 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
776 "http exten: \"%V\"", &r->exten);
778 if (r->host_start && r->host_end) {
779 n = ngx_http_validate_host(r->host_start,
780 r->host_end - r->host_start);
783 ngx_log_error(NGX_LOG_INFO, c->log, 0,
784 "client sent invalid host in request line");
785 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
789 r->headers_in.server.len = n;
790 r->headers_in.server.data = r->host_start;
793 if (r->http_version < NGX_HTTP_VERSION_10) {
795 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
796 r->headers_in.server.len)
799 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
803 ngx_http_process_request(r);
808 if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
809 sizeof(ngx_table_elt_t))
812 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
817 if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
818 sizeof(ngx_table_elt_t *))
821 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
825 c->log->action = "reading client request headers";
827 rev->handler = ngx_http_process_request_headers;
828 ngx_http_process_request_headers(rev);
833 if (rc != NGX_AGAIN) {
835 /* there was error while a request line parsing */
837 ngx_log_error(NGX_LOG_INFO, c->log, 0,
838 ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
839 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
843 /* NGX_AGAIN: a request line parsing is still incomplete */
845 if (r->header_in->pos == r->header_in->end) {
847 rv = ngx_http_alloc_large_header_buffer(r, 1);
849 if (rv == NGX_ERROR) {
850 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
854 if (rv == NGX_DECLINED) {
855 r->request_line.len = r->header_in->end - r->request_start;
856 r->request_line.data = r->request_start;
858 ngx_log_error(NGX_LOG_INFO, c->log, 0,
859 "client sent too long URI");
860 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
869 ngx_http_process_request_headers(ngx_event_t *rev)
876 ngx_http_header_t *hh;
877 ngx_http_request_t *r;
878 ngx_http_core_srv_conf_t *cscf;
879 ngx_http_core_main_conf_t *cmcf;
884 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
885 "http process request header line");
888 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
890 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
894 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
895 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
901 if (rc == NGX_AGAIN) {
903 if (r->header_in->pos == r->header_in->end) {
905 rv = ngx_http_alloc_large_header_buffer(r, 0);
907 if (rv == NGX_ERROR) {
908 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
912 if (rv == NGX_DECLINED) {
913 header.len = r->header_in->end - r->header_name_start;
914 header.data = r->header_name_start;
916 if (header.len > NGX_MAX_ERROR_STR - 300) {
917 header.len = NGX_MAX_ERROR_STR - 300;
918 header.data[header.len++] = '.';
919 header.data[header.len++] = '.';
920 header.data[header.len++] = '.';
923 ngx_log_error(NGX_LOG_INFO, c->log, 0,
924 "client sent too long header line: \"%V\"",
926 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
931 n = ngx_http_read_request_header(r);
933 if (n == NGX_AGAIN || n == NGX_ERROR) {
938 rc = ngx_http_parse_header_line(r, r->header_in,
939 cscf->underscores_in_headers);
943 if (r->invalid_header && cscf->ignore_invalid_headers) {
945 /* there was error while a header line parsing */
947 header.len = r->header_end - r->header_name_start;
948 header.data = r->header_name_start;
950 ngx_log_error(NGX_LOG_INFO, c->log, 0,
951 "client sent invalid header line: \"%V\"",
956 /* a header line has been parsed successfully */
958 h = ngx_list_push(&r->headers_in.headers);
960 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
964 h->hash = r->header_hash;
966 h->key.len = r->header_name_end - r->header_name_start;
967 h->key.data = r->header_name_start;
968 h->key.data[h->key.len] = '\0';
970 h->value.len = r->header_end - r->header_start;
971 h->value.data = r->header_start;
972 h->value.data[h->value.len] = '\0';
974 h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
975 if (h->lowcase_key == NULL) {
976 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
980 if (h->key.len == r->lowcase_index) {
981 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
984 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
987 hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
988 h->lowcase_key, h->key.len);
990 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
994 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
995 "http header: \"%V: %V\"",
1001 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1003 /* a whole header has been parsed successfully */
1005 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1006 "http header done");
1008 r->request_length += r->header_in->pos - r->header_in->start;
1010 r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
1012 rc = ngx_http_process_request_header(r);
1018 ngx_http_process_request(r);
1023 if (rc == NGX_AGAIN) {
1025 /* a header line parsing is still not complete */
1030 /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
1032 header.len = r->header_end - r->header_name_start;
1033 header.data = r->header_name_start;
1034 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1035 "client sent invalid header line: \"%V\\r...\"",
1037 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1044 ngx_http_read_request_header(ngx_http_request_t *r)
1048 ngx_connection_t *c;
1049 ngx_http_core_srv_conf_t *cscf;
1054 n = r->header_in->last - r->header_in->pos;
1061 n = c->recv(c, r->header_in->last,
1062 r->header_in->end - r->header_in->last);
1067 if (n == NGX_AGAIN) {
1068 if (!rev->timer_set) {
1069 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1070 ngx_add_timer(rev, cscf->client_header_timeout);
1073 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
1074 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1082 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1083 "client closed prematurely connection");
1086 if (n == 0 || n == NGX_ERROR) {
1088 c->log->action = "reading client request headers";
1090 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1094 r->header_in->last += n;
1101 ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
1102 ngx_uint_t request_line)
1106 ngx_http_connection_t *hc;
1107 ngx_http_core_srv_conf_t *cscf;
1109 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1110 "http alloc large header buffer");
1112 if (request_line && r->state == 0) {
1114 /* the client fills up the buffer with "\r\n" */
1116 r->request_length += r->header_in->end - r->header_in->start;
1118 r->header_in->pos = r->header_in->start;
1119 r->header_in->last = r->header_in->start;
1124 old = request_line ? r->request_start : r->header_name_start;
1126 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1129 && (size_t) (r->header_in->pos - old)
1130 >= cscf->large_client_header_buffers.size)
1132 return NGX_DECLINED;
1135 hc = r->http_connection;
1138 b = hc->free[--hc->nfree];
1140 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1141 "http large header free: %p %uz",
1142 b->pos, b->end - b->last);
1144 } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
1146 if (hc->busy == NULL) {
1147 hc->busy = ngx_palloc(r->connection->pool,
1148 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
1149 if (hc->busy == NULL) {
1154 b = ngx_create_temp_buf(r->connection->pool,
1155 cscf->large_client_header_buffers.size);
1160 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1161 "http large header alloc: %p %uz",
1162 b->pos, b->end - b->last);
1165 return NGX_DECLINED;
1168 hc->busy[hc->nbusy++] = b;
1170 if (r->state == 0) {
1172 * r->state == 0 means that a header line was parsed successfully
1173 * and we do not need to copy incomplete header line and
1174 * to relocate the parser header pointers
1177 r->request_length += r->header_in->end - r->header_in->start;
1184 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1185 "http large header copy: %d", r->header_in->pos - old);
1187 r->request_length += old - r->header_in->start;
1191 ngx_memcpy(new, old, r->header_in->pos - old);
1193 b->pos = new + (r->header_in->pos - old);
1194 b->last = new + (r->header_in->pos - old);
1197 r->request_start = new;
1199 if (r->request_end) {
1200 r->request_end = new + (r->request_end - old);
1203 r->method_end = new + (r->method_end - old);
1205 r->uri_start = new + (r->uri_start - old);
1206 r->uri_end = new + (r->uri_end - old);
1208 if (r->schema_start) {
1209 r->schema_start = new + (r->schema_start - old);
1210 r->schema_end = new + (r->schema_end - old);
1213 if (r->host_start) {
1214 r->host_start = new + (r->host_start - old);
1216 r->host_end = new + (r->host_end - old);
1220 if (r->port_start) {
1221 r->port_start = new + (r->port_start - old);
1222 r->port_end = new + (r->port_end - old);
1226 r->uri_ext = new + (r->uri_ext - old);
1229 if (r->args_start) {
1230 r->args_start = new + (r->args_start - old);
1233 if (r->http_protocol.data) {
1234 r->http_protocol.data = new + (r->http_protocol.data - old);
1238 r->header_name_start = new;
1239 r->header_name_end = new + (r->header_name_end - old);
1240 r->header_start = new + (r->header_start - old);
1241 r->header_end = new + (r->header_end - old);
1251 ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1254 ngx_table_elt_t **ph;
1256 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1267 ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1270 ngx_table_elt_t **ph;
1272 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1279 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1280 "client sent duplicate header line: \"%V: %V\", "
1281 "previous value: \"%V: %V\"",
1282 &h->key, &h->value, &(*ph)->key, &(*ph)->value);
1284 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1291 ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
1296 if (r->headers_in.host == NULL) {
1297 r->headers_in.host = h;
1300 len = ngx_http_validate_host(h->value.data, h->value.len);
1303 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1304 "client sent invalid host header");
1305 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1309 if (r->headers_in.server.len) {
1313 r->headers_in.server.len = len;
1314 r->headers_in.server.data = h->value.data;
1321 ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
1324 if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
1325 r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
1327 } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
1328 r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
1336 ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
1339 u_char *user_agent, *msie;
1341 if (r->headers_in.user_agent) {
1345 r->headers_in.user_agent = h;
1347 /* check some widespread browsers while the header is in CPU cache */
1349 user_agent = h->value.data;
1351 msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
1353 if (msie && msie + 7 < user_agent + h->value.len) {
1355 r->headers_in.msie = 1;
1357 if (msie[6] == '.') {
1361 r->headers_in.msie4 = 1;
1365 r->headers_in.msie6 = 1;
1370 /* MSIE ignores the SSL "close notify" alert */
1372 c->ssl->no_send_shutdown = 1;
1377 if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
1378 r->headers_in.opera = 1;
1379 r->headers_in.msie = 0;
1380 r->headers_in.msie4 = 0;
1381 r->headers_in.msie6 = 0;
1384 if (!r->headers_in.msie && !r->headers_in.opera) {
1386 if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
1387 r->headers_in.gecko = 1;
1389 } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
1390 r->headers_in.konqueror = 1;
1399 ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
1402 ngx_table_elt_t **cookie;
1404 cookie = ngx_array_push(&r->headers_in.cookies);
1410 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1417 ngx_http_process_request_header(ngx_http_request_t *r)
1419 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
1420 r->headers_in.server.len)
1423 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1427 if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
1428 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1429 "client sent HTTP/1.1 request without \"Host\" header");
1430 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1434 if (r->headers_in.content_length) {
1435 r->headers_in.content_length_n =
1436 ngx_atoof(r->headers_in.content_length->value.data,
1437 r->headers_in.content_length->value.len);
1439 if (r->headers_in.content_length_n == NGX_ERROR) {
1440 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1441 "client sent invalid \"Content-Length\" header");
1442 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1447 if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
1448 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1449 "client sent %V method without \"Content-Length\" header",
1451 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1455 if (r->method & NGX_HTTP_TRACE) {
1456 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1457 "client sent TRACE method");
1458 ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
1462 if (r->headers_in.transfer_encoding
1463 && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
1466 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1467 "client sent \"Transfer-Encoding: chunked\" header");
1468 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1472 if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
1473 if (r->headers_in.keep_alive) {
1474 r->headers_in.keep_alive_n =
1475 ngx_atotm(r->headers_in.keep_alive->value.data,
1476 r->headers_in.keep_alive->value.len);
1485 ngx_http_process_request(ngx_http_request_t *r)
1487 ngx_connection_t *c;
1491 if (r->plain_http) {
1492 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1493 "client sent plain HTTP request to HTTPS port");
1494 ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
1503 ngx_http_ssl_srv_conf_t *sscf;
1505 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
1507 if (sscf->verify == 1) {
1508 rc = SSL_get_verify_result(c->ssl->connection);
1510 if (rc != X509_V_OK) {
1511 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1512 "client SSL certificate verify error: (%l:%s)",
1513 rc, X509_verify_cert_error_string(rc));
1515 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1516 (SSL_get0_session(c->ssl->connection)));
1518 ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
1522 cert = SSL_get_peer_certificate(c->ssl->connection);
1525 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1526 "client sent no required SSL certificate");
1528 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1529 (SSL_get0_session(c->ssl->connection)));
1531 ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
1541 if (c->read->timer_set) {
1542 ngx_del_timer(c->read);
1546 ngx_atomic_fetch_add(ngx_stat_reading, -1);
1547 r->stat_reading = 0;
1548 ngx_atomic_fetch_add(ngx_stat_writing, 1);
1549 r->stat_writing = 1;
1552 c->read->handler = ngx_http_request_handler;
1553 c->write->handler = ngx_http_request_handler;
1554 r->read_event_handler = ngx_http_block_reading;
1556 ngx_http_handler(r);
1558 ngx_http_run_posted_requests(c);
1563 ngx_http_validate_host(u_char *host, size_t len)
1572 for (i = 0; i < len; i++) {
1591 if (ch == '/' || ch == '\0') {
1611 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
1615 ngx_http_virtual_names_t *vn;
1616 ngx_http_core_loc_conf_t *clcf;
1617 ngx_http_core_srv_conf_t *cscf;
1620 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1621 vn = cscf->virtual_names;
1624 return NGX_DECLINED;
1631 server = ngx_pnalloc(r->pool, len);
1632 if (server == NULL) {
1637 hash = ngx_hash_strlow(server, host, len);
1639 cscf = ngx_hash_find_combined(&vn->names, hash, server, len);
1651 ngx_http_server_name_t *sn;
1658 for (i = 0; i < vn->nregex; i++) {
1660 n = ngx_regex_exec(sn[i].regex, &name, NULL, 0);
1662 if (n == NGX_REGEX_NO_MATCHED) {
1667 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1669 " failed: %d on \"%V\" using \"%V\"",
1670 n, &name, &sn[i].name);
1676 cscf = sn[i].core_srv_conf;
1688 r->srv_conf = cscf->ctx->srv_conf;
1689 r->loc_conf = cscf->ctx->loc_conf;
1691 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1692 r->connection->log->file = clcf->err_log->file;
1694 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
1695 r->connection->log->log_level = clcf->err_log->log_level;
1703 ngx_http_request_handler(ngx_event_t *ev)
1705 ngx_connection_t *c;
1706 ngx_http_request_t *r;
1707 ngx_http_log_ctx_t *ctx;
1713 ctx->current_request = r;
1715 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1716 "http run request: \"%V?%V\"", &r->uri, &r->args);
1719 r->write_event_handler(r);
1722 r->read_event_handler(r);
1725 ngx_http_run_posted_requests(c);
1730 ngx_http_run_posted_requests(ngx_connection_t *c)
1732 ngx_http_request_t *r;
1733 ngx_http_log_ctx_t *ctx;
1734 ngx_http_posted_request_t *pr;
1743 pr = r->main->posted_requests;
1749 r->main->posted_requests = pr->next;
1754 ctx->current_request = r;
1756 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1757 "http posted request: \"%V?%V\"", &r->uri, &r->args);
1759 r->write_event_handler(r);
1765 ngx_http_post_request(ngx_http_request_t *r)
1767 ngx_http_posted_request_t *pr, **p;
1769 pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
1777 for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
1786 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1788 ngx_connection_t *c;
1789 ngx_http_request_t *pr;
1790 ngx_http_core_loc_conf_t *clcf;
1792 if (rc == NGX_DONE) {
1793 /* the request pool may be already destroyed */
1799 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
1800 "http finalize request: %d, \"%V?%V\" %d",
1801 rc, &r->uri, &r->args, r == c->data);
1803 if (rc == NGX_DECLINED) {
1804 r->content_handler = NULL;
1805 r->write_event_handler = ngx_http_core_run_phases;
1806 ngx_http_core_run_phases(r);
1810 if (r != r->main && r->post_subrequest) {
1811 rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
1815 || rc == NGX_HTTP_REQUEST_TIME_OUT
1816 || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
1819 if (rc > 0 && r->headers_out.status == 0) {
1820 r->headers_out.status = rc;
1823 if (ngx_http_post_action(r) == NGX_OK) {
1827 ngx_http_close_request(r, 0);
1831 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
1832 || rc == NGX_HTTP_CREATED
1833 || rc == NGX_HTTP_NO_CONTENT)
1835 if (rc == NGX_HTTP_CLOSE) {
1836 ngx_http_close_request(r, rc);
1841 if (c->read->timer_set) {
1842 ngx_del_timer(c->read);
1845 if (c->write->timer_set) {
1846 ngx_del_timer(c->write);
1850 c->read->handler = ngx_http_request_handler;
1851 c->write->handler = ngx_http_request_handler;
1853 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
1859 if (r->buffered || r->postponed) {
1861 if (ngx_http_set_write_handler(r) != NGX_OK) {
1862 ngx_http_close_request(r->main, 0);
1870 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1871 "http finalize non-active request: \"%V?%V\"",
1882 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1884 if (clcf->log_subrequest) {
1885 ngx_http_log_request(r);
1891 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1892 "subrequest: \"%V?%V\" logged again",
1898 if (pr->postponed && pr->postponed->request == r) {
1899 pr->postponed = pr->postponed->next;
1906 r->write_event_handler = ngx_http_request_finalizer;
1913 if (ngx_http_post_request(pr) != NGX_OK) {
1914 ngx_http_close_request(r->main, 0);
1918 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1919 "http wake parent request: \"%V?%V\"",
1920 &pr->uri, &pr->args);
1925 if (r->buffered || c->buffered || r->postponed) {
1927 if (ngx_http_set_write_handler(r) != NGX_OK) {
1928 ngx_http_close_request(r, 0);
1935 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1936 "http finalize non-active request: \"%V?%V\"",
1943 if (!r->post_action) {
1944 r->request_complete = 1;
1947 if (ngx_http_post_action(r) == NGX_OK) {
1951 if (c->read->timer_set) {
1952 ngx_del_timer(c->read);
1955 if (c->write->timer_set) {
1956 c->write->delayed = 0;
1957 ngx_del_timer(c->write);
1965 ngx_http_close_request(r, 0);
1969 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1974 && clcf->keepalive_timeout > 0)
1976 ngx_http_set_keepalive(r);
1979 } else if (r->lingering_close && clcf->lingering_timeout > 0) {
1980 ngx_http_set_lingering_close(r);
1984 ngx_http_close_request(r, 0);
1989 ngx_http_set_write_handler(ngx_http_request_t *r)
1992 ngx_http_core_loc_conf_t *clcf;
1994 r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
1996 r->read_event_handler = ngx_http_test_reading;
1997 r->write_event_handler = ngx_http_writer;
1999 wev = r->connection->write;
2001 if (wev->ready && wev->delayed) {
2005 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2006 if (!wev->delayed) {
2007 ngx_add_timer(wev, clcf->send_timeout);
2010 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2011 ngx_http_close_request(r, 0);
2020 ngx_http_writer(ngx_http_request_t *r)
2024 ngx_connection_t *c;
2025 ngx_http_core_loc_conf_t *clcf;
2030 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2031 "http writer handler: \"%V?%V\"", &r->uri, &r->args);
2033 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
2035 if (wev->timedout) {
2036 if (!wev->delayed) {
2037 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
2038 "client timed out");
2041 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
2049 ngx_add_timer(wev, clcf->send_timeout);
2051 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2052 ngx_http_close_request(r, 0);
2060 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2061 "http writer delayed");
2063 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2064 ngx_http_close_request(r, 0);
2071 rc = ngx_http_output_filter(r, NULL);
2077 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
2078 "http writer output filter: %d, \"%V?%V\"",
2079 rc, &r->uri, &r->args);
2081 if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
2083 if (!wev->ready && !wev->delayed) {
2084 ngx_add_timer(wev, clcf->send_timeout);
2087 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2088 ngx_http_close_request(r, 0);
2094 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2095 "http writer done: \"%V?%V\"", &r->uri, &r->args);
2097 ngx_http_finalize_request(r, rc);
2102 ngx_http_request_finalizer(ngx_http_request_t *r)
2104 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2105 "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
2107 ngx_http_finalize_request(r, 0);
2112 ngx_http_block_reading(ngx_http_request_t *r)
2114 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2115 "http reading blocked");
2117 /* aio does not call this handler */
2119 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
2120 && r->connection->read->active)
2122 if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
2123 ngx_http_close_request(r, 0);
2130 ngx_http_test_reading(ngx_http_request_t *r)
2136 ngx_connection_t *c;
2141 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
2143 #if (NGX_HAVE_KQUEUE)
2145 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2147 if (!rev->pending_eof) {
2153 err = rev->kq_errno;
2160 n = recv(c->fd, buf, 1, MSG_PEEK);
2169 } else if (n == -1) {
2170 err = ngx_socket_errno;
2172 if (err != NGX_EAGAIN) {
2180 /* aio does not call this handler */
2182 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
2184 if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
2185 ngx_http_close_request(r, 0);
2197 ngx_log_error(NGX_LOG_INFO, c->log, err,
2198 "client closed prematurely connection");
2200 ngx_http_finalize_request(r, 0);
2205 ngx_http_set_keepalive(ngx_http_request_t *r)
2210 ngx_event_t *rev, *wev;
2211 ngx_connection_t *c;
2212 ngx_http_connection_t *hc;
2213 ngx_http_core_srv_conf_t *cscf;
2214 ngx_http_core_loc_conf_t *clcf;
2219 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2221 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
2223 if (r->discard_body) {
2224 r->write_event_handler = ngx_http_request_empty_handler;
2225 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2226 ngx_add_timer(rev, clcf->lingering_timeout);
2230 c->log->action = "closing request";
2232 hc = r->http_connection;
2235 if (b->pos < b->last) {
2237 /* the pipelined request */
2239 if (b != c->buffer) {
2242 * If the large header buffers were allocated while the previous
2243 * request processing then we do not use c->buffer for
2244 * the pipelined request (see ngx_http_init_request()).
2246 * Now we would move the large header buffers to the free list.
2249 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2251 if (hc->free == NULL) {
2252 hc->free = ngx_palloc(c->pool,
2253 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
2255 if (hc->free == NULL) {
2256 ngx_http_close_request(r, 0);
2261 for (i = 0; i < hc->nbusy - 1; i++) {
2263 hc->free[hc->nfree++] = f;
2273 ngx_http_request_done(r, 0);
2277 ngx_add_timer(rev, clcf->keepalive_timeout);
2279 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2280 ngx_http_close_connection(c);
2285 wev->handler = ngx_http_empty_handler;
2287 if (b->pos < b->last) {
2289 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
2292 ngx_atomic_fetch_add(ngx_stat_reading, 1);
2296 c->log->action = "reading client pipelined request line";
2298 rev->handler = ngx_http_init_request;
2299 ngx_post_event(rev, &ngx_posted_events);
2306 * To keep a memory footprint as small as possible for an idle
2307 * keepalive connection we try to free the ngx_http_request_t and
2308 * c->buffer's memory if they were allocated outside the c->pool.
2309 * The large header buffers are always allocated outside the c->pool and
2313 if (ngx_pfree(c->pool, r) == NGX_OK) {
2319 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
2322 * the special note for ngx_http_keepalive_handler() that
2323 * c->buffer's memory was freed
2333 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
2334 hc->free, hc->nfree);
2337 for (i = 0; i < hc->nfree; i++) {
2338 ngx_pfree(c->pool, hc->free[i]->start);
2345 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
2346 hc->busy, hc->nbusy);
2349 for (i = 0; i < hc->nbusy; i++) {
2350 ngx_pfree(c->pool, hc->busy[i]->start);
2359 ngx_ssl_free_buffer(c);
2363 rev->handler = ngx_http_keepalive_handler;
2365 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2366 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2367 ngx_http_close_connection(c);
2372 c->log->action = "keepalive";
2374 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
2375 if (ngx_tcp_push(c->fd) == -1) {
2376 ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
2377 ngx_http_close_connection(c);
2381 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
2382 tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
2389 && clcf->tcp_nodelay
2390 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
2392 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
2394 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
2395 (const void *) &tcp_nodelay, sizeof(int))
2398 ngx_connection_error(c, ngx_socket_errno,
2399 "setsockopt(TCP_NODELAY) failed");
2400 ngx_http_close_connection(c);
2404 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
2408 /* if ngx_http_request_t was freed then we need some other place */
2409 r->http_state = NGX_HTTP_KEEPALIVE_STATE;
2415 ngx_post_event(rev, &ngx_posted_events);
2421 ngx_http_keepalive_handler(ngx_event_t *rev)
2426 ngx_connection_t *c;
2430 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
2432 if (rev->timedout || c->close) {
2433 ngx_http_close_connection(c);
2437 #if (NGX_HAVE_KQUEUE)
2439 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2440 if (rev->pending_eof) {
2441 c->log->handler = NULL;
2442 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
2443 "kevent() reported that client %V closed "
2444 "keepalive connection", &c->addr_text);
2447 c->ssl->no_send_shutdown = 1;
2450 ngx_http_close_connection(c);
2458 size = b->end - b->start;
2460 if (b->pos == NULL) {
2463 * The c->buffer's memory was freed by ngx_http_set_keepalive().
2464 * However, the c->buffer->start and c->buffer->end were not changed
2465 * to keep the buffer size.
2468 b->pos = ngx_palloc(c->pool, size);
2469 if (b->pos == NULL) {
2470 ngx_http_close_connection(c);
2476 b->end = b->pos + size;
2480 * MSIE closes a keepalive connection with RST flag
2481 * so we ignore ECONNRESET here.
2484 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
2485 ngx_set_socket_errno(0);
2487 n = c->recv(c, b->last, size);
2488 c->log_error = NGX_ERROR_INFO;
2490 if (n == NGX_AGAIN) {
2491 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2492 ngx_http_close_connection(c);
2498 if (n == NGX_ERROR) {
2499 ngx_http_close_connection(c);
2503 c->log->handler = NULL;
2506 ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
2507 "client %V closed keepalive connection", &c->addr_text);
2508 ngx_http_close_connection(c);
2515 ngx_atomic_fetch_add(ngx_stat_reading, 1);
2518 c->log->handler = ngx_http_log_error;
2519 c->log->action = "reading client request line";
2523 ngx_http_init_request(rev);
2528 ngx_http_set_lingering_close(ngx_http_request_t *r)
2530 ngx_event_t *rev, *wev;
2531 ngx_connection_t *c;
2532 ngx_http_core_loc_conf_t *clcf;
2536 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2539 rev->handler = ngx_http_lingering_close_handler;
2541 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2542 ngx_add_timer(rev, clcf->lingering_timeout);
2544 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2545 ngx_http_close_request(r, 0);
2550 wev->handler = ngx_http_empty_handler;
2552 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2553 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2554 ngx_http_close_request(r, 0);
2559 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
2560 ngx_connection_error(c, ngx_socket_errno,
2561 ngx_shutdown_socket_n " failed");
2562 ngx_http_close_request(r, 0);
2567 ngx_http_lingering_close_handler(rev);
2573 ngx_http_lingering_close_handler(ngx_event_t *rev)
2577 ngx_connection_t *c;
2578 ngx_http_request_t *r;
2579 ngx_http_core_loc_conf_t *clcf;
2580 u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
2585 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2586 "http lingering close handler");
2588 if (rev->timedout) {
2590 ngx_http_close_request(r, 0);
2594 timer = (ngx_msec_t) (r->lingering_time - ngx_time());
2596 ngx_http_close_request(r, 0);
2601 n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
2603 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
2605 if (n == NGX_ERROR || n == 0) {
2606 ngx_http_close_request(r, 0);
2610 } while (rev->ready);
2612 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2613 ngx_http_close_request(r, 0);
2617 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2621 if (timer > clcf->lingering_timeout) {
2622 timer = clcf->lingering_timeout;
2625 ngx_add_timer(rev, timer);
2630 ngx_http_empty_handler(ngx_event_t *wev)
2632 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
2639 ngx_http_request_empty_handler(ngx_http_request_t *r)
2641 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2642 "http request empty handler");
2649 ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
2654 b = ngx_calloc_buf(r->pool);
2659 if (flags & NGX_HTTP_LAST) {
2663 if (flags & NGX_HTTP_FLUSH) {
2670 return ngx_http_output_filter(r, &out);
2675 ngx_http_post_action(ngx_http_request_t *r)
2677 ngx_http_core_loc_conf_t *clcf;
2679 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2681 if (clcf->post_action.data == NULL) {
2682 return NGX_DECLINED;
2685 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2686 "post action: \"%V\"", &clcf->post_action);
2688 r->http_version = NGX_HTTP_VERSION_9;
2692 r->read_event_handler = ngx_http_block_reading;
2694 if (clcf->post_action.data[0] == '/') {
2695 ngx_http_internal_redirect(r, &clcf->post_action, NULL);
2698 ngx_http_named_location(r, &clcf->post_action);
2706 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
2708 ngx_connection_t *c;
2712 ngx_http_request_done(r->main, error);
2713 ngx_http_close_connection(c);
2718 ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
2721 struct linger linger;
2722 ngx_http_cleanup_t *cln;
2723 ngx_http_log_ctx_t *ctx;
2724 ngx_http_core_loc_conf_t *clcf;
2726 log = r->connection->log;
2728 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
2730 if (r->pool == NULL) {
2731 ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
2735 for (cln = r->cleanup; cln; cln = cln->next) {
2737 cln->handler(cln->data);
2743 if (r->stat_reading) {
2744 ngx_atomic_fetch_add(ngx_stat_reading, -1);
2747 if (r->stat_writing) {
2748 ngx_atomic_fetch_add(ngx_stat_writing, -1);
2753 if (error && r->headers_out.status == 0) {
2754 r->headers_out.status = error;
2757 log->action = "logging request";
2759 ngx_http_log_request(r);
2761 log->action = "closing request";
2763 if (r->connection->timedout) {
2764 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2766 if (clcf->reset_timedout_connection) {
2768 linger.l_linger = 0;
2770 if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
2771 (const void *) &linger, sizeof(struct linger)) == -1)
2773 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
2774 "setsockopt(SO_LINGER) failed");
2779 /* the various request strings were allocated from r->pool */
2781 ctx->request = NULL;
2783 r->request_line.len = 0;
2785 r->connection->destroyed = 1;
2787 ngx_destroy_pool(r->pool);
2792 ngx_http_log_request(ngx_http_request_t *r)
2795 ngx_http_handler_pt *log_handler;
2796 ngx_http_core_main_conf_t *cmcf;
2798 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
2800 log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
2801 n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
2803 for (i = 0; i < n; i++) {
2810 ngx_http_close_connection(ngx_connection_t *c)
2814 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
2815 "close http connection: %d", c->fd);
2820 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
2821 c->ssl->handler = ngx_http_close_connection;
2829 ngx_atomic_fetch_add(ngx_stat_active, -1);
2836 ngx_close_connection(c);
2838 ngx_destroy_pool(pool);
2843 ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
2846 ngx_http_request_t *r;
2847 ngx_http_log_ctx_t *ctx;
2850 p = ngx_snprintf(buf, len, " while %s", log->action);
2857 p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
2863 return r->log_handler(r, ctx->current_request, p, len);
2866 p = ngx_snprintf(p, len, ", server: %V",
2867 &ctx->connection->listening->addr_text);
2875 ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
2876 u_char *buf, size_t len)
2878 char *uri_separator;
2880 ngx_http_upstream_t *u;
2881 ngx_http_core_srv_conf_t *cscf;
2883 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2885 p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
2889 if (r->request_line.data == NULL && r->request_start) {
2890 for (p = r->request_start; p < r->header_in->last; p++) {
2891 if (*p == CR || *p == LF) {
2896 r->request_line.len = p - r->request_start;
2897 r->request_line.data = r->request_start;
2900 if (r->request_line.len) {
2901 p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
2907 p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
2914 if (u && u->peer.name) {
2918 #if (NGX_HAVE_UNIX_DOMAIN)
2919 if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
2920 uri_separator = ":";
2924 p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
2925 &u->schema, u->peer.name,
2926 uri_separator, &u->uri);
2931 if (r->headers_in.host) {
2932 p = ngx_snprintf(buf, len, ", host: \"%V\"",
2933 &r->headers_in.host->value);
2938 if (r->headers_in.referer) {
2939 p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
2940 &r->headers_in.referer->value);