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 ngx_http_in_port_t *hip;
236 ngx_http_in_addr_t *hia;
237 ngx_http_log_ctx_t *ctx;
238 ngx_http_connection_t *hc;
239 ngx_http_core_srv_conf_t *cscf;
240 ngx_http_core_loc_conf_t *clcf;
241 ngx_http_core_main_conf_t *cmcf;
244 ngx_atomic_fetch_add(ngx_stat_reading, -1);
250 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
252 ngx_http_close_connection(c);
259 hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
261 ngx_http_close_connection(c);
269 ngx_memzero(r, sizeof(ngx_http_request_t));
271 r->pipeline = hc->pipeline;
274 r->header_in = hc->busy[0];
278 r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
280 ngx_http_close_connection(c);
288 r->http_connection = hc;
291 r->signature = NGX_HTTP_MODULE;
293 /* find the server configuration for the address:port */
297 hip = c->listening->servers;
301 r->port_text = &hip->port_text;
307 if (hip->naddrs > 1) {
310 * There are several addresses on this port and one of them
311 * is the "*:port" wildcard so getsockname() is needed to determine
312 * the server address.
314 * AcceptEx() already has given this address.
318 if (c->local_sockaddr) {
320 ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
325 if (ngx_http_server_addr(r, NULL) != NGX_OK) {
326 ngx_http_close_connection(c);
331 /* the last address is "*" */
333 for ( /* void */ ; i < hip->naddrs - 1; i++) {
334 if (hia[i].addr == r->in_addr) {
340 r->in_addr = hia[0].addr;
343 r->virtual_names = hia[i].virtual_names;
345 /* the default server configuration for the address:port */
346 cscf = hia[i].core_srv_conf;
348 r->main_conf = cscf->ctx->main_conf;
349 r->srv_conf = cscf->ctx->srv_conf;
350 r->loc_conf = cscf->ctx->loc_conf;
352 rev->handler = ngx_http_process_request_line;
357 ngx_http_ssl_srv_conf_t *sscf;
359 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
360 if (sscf->enable || hia[i].ssl) {
362 if (c->ssl == NULL) {
364 c->log->action = "SSL handshaking";
366 if (hia[i].ssl && sscf->ssl.ctx == NULL) {
367 ngx_log_error(NGX_LOG_ERR, c->log, 0,
368 "no \"ssl_certificate\" is defined "
369 "in server listening on SSL port");
370 ngx_http_close_connection(c);
374 if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
377 ngx_http_close_connection(c);
381 rev->handler = ngx_http_ssl_handshake;
384 r->main_filter_need_in_memory = 1;
390 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
391 c->log->file = clcf->err_log->file;
392 if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
393 c->log->log_level = clcf->err_log->log_level;
396 if (c->buffer == NULL) {
397 c->buffer = ngx_create_temp_buf(c->pool,
398 cscf->client_header_buffer_size);
399 if (c->buffer == NULL) {
400 ngx_http_close_connection(c);
405 if (r->header_in == NULL) {
406 r->header_in = c->buffer;
409 r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
410 if (r->pool == NULL) {
411 ngx_http_close_connection(c);
416 if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
417 sizeof(ngx_table_elt_t))
420 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
424 r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
425 if (r->ctx == NULL) {
426 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
430 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
432 r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
433 * sizeof(ngx_http_variable_value_t));
434 if (r->variables == NULL) {
435 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
439 c->single_connection = 1;
444 tp = ngx_timeofday();
445 r->start_sec = tp->sec;
446 r->start_msec = tp->msec;
448 r->method = NGX_HTTP_UNKNOWN;
450 r->headers_in.content_length_n = -1;
451 r->headers_in.keep_alive_n = -1;
452 r->headers_out.content_length_n = -1;
453 r->headers_out.last_modified_time = -1;
455 r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
456 r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
458 r->http_state = NGX_HTTP_READING_REQUEST_STATE;
462 ctx->current_request = r;
463 r->log_handler = ngx_http_log_error_handler;
466 ngx_atomic_fetch_add(ngx_stat_reading, 1);
468 ngx_atomic_fetch_add(ngx_stat_requests, 1);
478 ngx_http_ssl_handshake(ngx_event_t *rev)
484 ngx_http_request_t *r;
489 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
490 "http check ssl handshake");
493 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
495 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
499 n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
501 if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
503 if (!rev->timer_set) {
504 ngx_add_timer(rev, c->listening->post_accept_timeout);
507 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
508 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
515 if (buf[0] == 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
516 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
517 "https ssl handshake: 0x%02Xd", buf[0]);
519 rc = ngx_ssl_handshake(c);
521 if (rc == NGX_AGAIN) {
523 if (!rev->timer_set) {
524 ngx_add_timer(rev, c->listening->post_accept_timeout);
527 c->ssl->handler = ngx_http_ssl_handshake_handler;
531 ngx_http_ssl_handshake_handler(c);
536 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
543 c->log->action = "reading client request line";
545 rev->handler = ngx_http_process_request_line;
546 ngx_http_process_request_line(rev);
551 ngx_http_ssl_handshake_handler(ngx_connection_t *c)
553 ngx_http_request_t *r;
555 if (c->ssl->handshaked) {
558 * The majority of browsers do not send the "close notify" alert.
559 * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
560 * and Links. And what is more, MSIE ignores the server's alert.
562 * Opera and recent Mozilla send the alert.
565 c->ssl->no_wait_shutdown = 1;
567 c->read->handler = ngx_http_process_request_line;
568 /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
570 ngx_http_process_request_line(c->read);
577 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
582 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
585 ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
588 const char *servername;
590 ngx_http_request_t *r;
591 ngx_http_ssl_srv_conf_t *sscf;
593 servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
595 if (servername == NULL) {
596 return SSL_TLSEXT_ERR_NOACK;
599 c = ngx_ssl_get_connection(ssl_conn);
601 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
602 "SSL server name: \"%s\"", servername);
604 len = ngx_strlen(servername);
607 return SSL_TLSEXT_ERR_NOACK;
612 if (ngx_http_find_virtual_server(r, (u_char *) servername, len) != NGX_OK) {
613 return SSL_TLSEXT_ERR_NOACK;
616 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
618 SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
620 return SSL_TLSEXT_ERR_OK;
629 ngx_http_process_request_line(ngx_event_t *rev)
634 ngx_http_request_t *r;
635 ngx_http_core_srv_conf_t *cscf;
640 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
641 "http process request line");
644 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
646 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
654 if (rc == NGX_AGAIN) {
655 n = ngx_http_read_request_header(r);
657 if (n == NGX_AGAIN || n == NGX_ERROR) {
662 rc = ngx_http_parse_request_line(r, r->header_in);
666 /* the request line has been parsed successfully */
668 r->request_line.len = r->request_end - r->request_start;
669 r->request_line.data = r->request_start;
670 *r->request_end = '\0';
674 r->uri.len = r->args_start - 1 - r->uri_start;
676 r->uri.len = r->uri_end - r->uri_start;
680 if (r->complex_uri || r->quoted_uri) {
682 r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
683 if (r->uri.data == NULL) {
684 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
688 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
690 rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
692 if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
693 ngx_log_error(NGX_LOG_INFO, c->log, 0,
694 "client sent invalid request");
695 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
700 r->uri.data = r->uri_start;
704 r->unparsed_uri.len = r->uri_end - r->uri_start;
705 r->unparsed_uri.data = r->uri_start;
708 r->method_name.len = r->method_end - r->request_start + 1;
709 r->method_name.data = r->request_line.data;
712 if (r->http_protocol.data) {
713 r->http_protocol.len = r->request_end - r->http_protocol.data;
719 r->exten.len = r->args_start - 1 - r->uri_ext;
721 r->exten.len = r->uri_end - r->uri_ext;
724 r->exten.data = r->uri_ext;
728 if (r->args_start && r->uri_end > r->args_start) {
729 r->args.len = r->uri_end - r->args_start;
730 r->args.data = r->args_start;
734 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
735 "http request line: \"%V\"", &r->request_line);
737 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
738 "http uri: \"%V\"", &r->uri);
740 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
741 "http args: \"%V\"", &r->args);
743 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
744 "http exten: \"%V\"", &r->exten);
746 if (r->host_start && r->host_end) {
747 n = ngx_http_validate_host(r->host_start,
748 r->host_end - r->host_start);
751 ngx_log_error(NGX_LOG_INFO, c->log, 0,
752 "client sent invalid host in request line");
753 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
757 r->headers_in.server.len = n;
758 r->headers_in.server.data = r->host_start;
761 if (r->http_version < NGX_HTTP_VERSION_10) {
763 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
764 r->headers_in.server.len)
767 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
771 ngx_http_process_request(r);
776 if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
777 sizeof(ngx_table_elt_t))
780 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
785 if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
786 sizeof(ngx_table_elt_t *))
789 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
793 c->log->action = "reading client request headers";
795 rev->handler = ngx_http_process_request_headers;
796 ngx_http_process_request_headers(rev);
801 if (rc != NGX_AGAIN) {
803 /* there was error while a request line parsing */
805 ngx_log_error(NGX_LOG_INFO, c->log, 0,
806 ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
807 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
811 /* NGX_AGAIN: a request line parsing is still incomplete */
813 if (r->header_in->pos == r->header_in->end) {
815 rv = ngx_http_alloc_large_header_buffer(r, 1);
817 if (rv == NGX_ERROR) {
818 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
822 if (rv == NGX_DECLINED) {
823 r->request_line.len = r->header_in->end - r->request_start;
824 r->request_line.data = r->request_start;
826 ngx_log_error(NGX_LOG_INFO, c->log, 0,
827 "client sent too long URI");
828 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
837 ngx_http_process_request_headers(ngx_event_t *rev)
844 ngx_http_header_t *hh;
845 ngx_http_request_t *r;
846 ngx_http_core_srv_conf_t *cscf;
847 ngx_http_core_main_conf_t *cmcf;
852 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
853 "http process request header line");
856 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
858 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
862 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
863 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
869 if (rc == NGX_AGAIN) {
871 if (r->header_in->pos == r->header_in->end) {
873 rv = ngx_http_alloc_large_header_buffer(r, 0);
875 if (rv == NGX_ERROR) {
876 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
880 if (rv == NGX_DECLINED) {
881 header.len = r->header_in->end - r->header_name_start;
882 header.data = r->header_name_start;
884 if (header.len > NGX_MAX_ERROR_STR - 300) {
885 header.len = NGX_MAX_ERROR_STR - 300;
886 header.data[header.len++] = '.';
887 header.data[header.len++] = '.';
888 header.data[header.len++] = '.';
891 ngx_log_error(NGX_LOG_INFO, c->log, 0,
892 "client sent too long header line: \"%V\"",
894 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
899 n = ngx_http_read_request_header(r);
901 if (n == NGX_AGAIN || n == NGX_ERROR) {
906 rc = ngx_http_parse_header_line(r, r->header_in,
907 cscf->underscores_in_headers);
911 if (r->invalid_header && cscf->ignore_invalid_headers) {
913 /* there was error while a header line parsing */
915 header.len = r->header_end - r->header_name_start;
916 header.data = r->header_name_start;
918 ngx_log_error(NGX_LOG_INFO, c->log, 0,
919 "client sent invalid header line: \"%V\"",
924 /* a header line has been parsed successfully */
926 h = ngx_list_push(&r->headers_in.headers);
928 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
932 h->hash = r->header_hash;
934 h->key.len = r->header_name_end - r->header_name_start;
935 h->key.data = r->header_name_start;
936 h->key.data[h->key.len] = '\0';
938 h->value.len = r->header_end - r->header_start;
939 h->value.data = r->header_start;
940 h->value.data[h->value.len] = '\0';
942 h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
943 if (h->lowcase_key == NULL) {
944 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
948 if (h->key.len == r->lowcase_index) {
949 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
952 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
955 hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
956 h->lowcase_key, h->key.len);
958 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
962 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
963 "http header: \"%V: %V\"",
969 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
971 /* a whole header has been parsed successfully */
973 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
976 r->request_length += r->header_in->pos - r->header_in->start;
978 r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
980 rc = ngx_http_process_request_header(r);
986 ngx_http_process_request(r);
991 if (rc == NGX_AGAIN) {
993 /* a header line parsing is still not complete */
998 /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
1000 header.len = r->header_end - r->header_name_start;
1001 header.data = r->header_name_start;
1002 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1003 "client sent invalid header line: \"%V\\r...\"",
1005 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1012 ngx_http_read_request_header(ngx_http_request_t *r)
1016 ngx_connection_t *c;
1017 ngx_http_core_srv_conf_t *cscf;
1022 n = r->header_in->last - r->header_in->pos;
1029 n = c->recv(c, r->header_in->last,
1030 r->header_in->end - r->header_in->last);
1035 if (n == NGX_AGAIN) {
1036 if (!rev->timer_set) {
1037 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1038 ngx_add_timer(rev, cscf->client_header_timeout);
1041 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
1042 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1050 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1051 "client closed prematurely connection");
1054 if (n == 0 || n == NGX_ERROR) {
1056 c->log->action = "reading client request headers";
1058 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1062 r->header_in->last += n;
1069 ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
1070 ngx_uint_t request_line)
1074 ngx_http_connection_t *hc;
1075 ngx_http_core_srv_conf_t *cscf;
1077 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1078 "http alloc large header buffer");
1080 if (request_line && r->state == 0) {
1082 /* the client fills up the buffer with "\r\n" */
1084 r->request_length += r->header_in->end - r->header_in->start;
1086 r->header_in->pos = r->header_in->start;
1087 r->header_in->last = r->header_in->start;
1092 old = request_line ? r->request_start : r->header_name_start;
1094 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1097 && (size_t) (r->header_in->pos - old)
1098 >= cscf->large_client_header_buffers.size)
1100 return NGX_DECLINED;
1103 hc = r->http_connection;
1106 b = hc->free[--hc->nfree];
1108 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1109 "http large header free: %p %uz",
1110 b->pos, b->end - b->last);
1112 } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
1114 if (hc->busy == NULL) {
1115 hc->busy = ngx_palloc(r->connection->pool,
1116 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
1117 if (hc->busy == NULL) {
1122 b = ngx_create_temp_buf(r->connection->pool,
1123 cscf->large_client_header_buffers.size);
1128 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1129 "http large header alloc: %p %uz",
1130 b->pos, b->end - b->last);
1133 return NGX_DECLINED;
1136 hc->busy[hc->nbusy++] = b;
1138 if (r->state == 0) {
1140 * r->state == 0 means that a header line was parsed successfully
1141 * and we do not need to copy incomplete header line and
1142 * to relocate the parser header pointers
1145 r->request_length += r->header_in->end - r->header_in->start;
1152 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1153 "http large header copy: %d", r->header_in->pos - old);
1155 r->request_length += old - r->header_in->start;
1159 ngx_memcpy(new, old, r->header_in->pos - old);
1161 b->pos = new + (r->header_in->pos - old);
1162 b->last = new + (r->header_in->pos - old);
1165 r->request_start = new;
1167 if (r->request_end) {
1168 r->request_end = new + (r->request_end - old);
1171 r->method_end = new + (r->method_end - old);
1173 r->uri_start = new + (r->uri_start - old);
1174 r->uri_end = new + (r->uri_end - old);
1176 if (r->schema_start) {
1177 r->schema_start = new + (r->schema_start - old);
1178 r->schema_end = new + (r->schema_end - old);
1181 if (r->host_start) {
1182 r->host_start = new + (r->host_start - old);
1184 r->host_end = new + (r->host_end - old);
1188 if (r->port_start) {
1189 r->port_start = new + (r->port_start - old);
1190 r->port_end = new + (r->port_end - old);
1194 r->uri_ext = new + (r->uri_ext - old);
1197 if (r->args_start) {
1198 r->args_start = new + (r->args_start - old);
1201 if (r->http_protocol.data) {
1202 r->http_protocol.data = new + (r->http_protocol.data - old);
1206 r->header_name_start = new;
1207 r->header_name_end = new + (r->header_name_end - old);
1208 r->header_start = new + (r->header_start - old);
1209 r->header_end = new + (r->header_end - old);
1219 ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1222 ngx_table_elt_t **ph;
1224 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1235 ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1238 ngx_table_elt_t **ph;
1240 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1247 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1248 "client sent duplicate header line: \"%V: %V\", "
1249 "previous value: \"%V: %V\"",
1250 &h->key, &h->value, &(*ph)->key, &(*ph)->value);
1252 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1259 ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
1264 if (r->headers_in.host == NULL) {
1265 r->headers_in.host = h;
1268 len = ngx_http_validate_host(h->value.data, h->value.len);
1271 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1272 "client sent invalid host header");
1273 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1277 if (r->headers_in.server.len) {
1281 r->headers_in.server.len = len;
1282 r->headers_in.server.data = h->value.data;
1289 ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
1292 if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
1293 r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
1295 } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
1296 r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
1304 ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
1307 u_char *user_agent, *msie;
1309 if (r->headers_in.user_agent) {
1313 r->headers_in.user_agent = h;
1315 /* check some widespread browsers while the header is in CPU cache */
1317 user_agent = h->value.data;
1319 msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
1321 if (msie && msie + 7 < user_agent + h->value.len) {
1323 r->headers_in.msie = 1;
1325 if (msie[6] == '.') {
1329 r->headers_in.msie4 = 1;
1333 r->headers_in.msie6 = 1;
1338 /* MSIE ignores the SSL "close notify" alert */
1340 c->ssl->no_send_shutdown = 1;
1345 if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
1346 r->headers_in.opera = 1;
1347 r->headers_in.msie = 0;
1348 r->headers_in.msie4 = 0;
1349 r->headers_in.msie6 = 0;
1352 if (!r->headers_in.msie && !r->headers_in.opera) {
1354 if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
1355 r->headers_in.gecko = 1;
1357 } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
1358 r->headers_in.konqueror = 1;
1367 ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
1370 ngx_table_elt_t **cookie;
1372 cookie = ngx_array_push(&r->headers_in.cookies);
1378 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1385 ngx_http_process_request_header(ngx_http_request_t *r)
1387 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
1388 r->headers_in.server.len)
1391 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1395 if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
1396 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1397 "client sent HTTP/1.1 request without \"Host\" header");
1398 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1402 if (r->headers_in.content_length) {
1403 r->headers_in.content_length_n =
1404 ngx_atoof(r->headers_in.content_length->value.data,
1405 r->headers_in.content_length->value.len);
1407 if (r->headers_in.content_length_n == NGX_ERROR) {
1408 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1409 "client sent invalid \"Content-Length\" header");
1410 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1415 if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
1416 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1417 "client sent %V method without \"Content-Length\" header",
1419 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1423 if (r->method & NGX_HTTP_TRACE) {
1424 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1425 "client sent TRACE method");
1426 ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
1430 if (r->headers_in.transfer_encoding
1431 && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
1434 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1435 "client sent \"Transfer-Encoding: chunked\" header");
1436 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1440 if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
1441 if (r->headers_in.keep_alive) {
1442 r->headers_in.keep_alive_n =
1443 ngx_atotm(r->headers_in.keep_alive->value.data,
1444 r->headers_in.keep_alive->value.len);
1453 ngx_http_process_request(ngx_http_request_t *r)
1455 ngx_connection_t *c;
1459 if (r->plain_http) {
1460 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1461 "client sent plain HTTP request to HTTPS port");
1462 ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
1471 ngx_http_ssl_srv_conf_t *sscf;
1473 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
1475 if (sscf->verify == 1) {
1476 rc = SSL_get_verify_result(c->ssl->connection);
1478 if (rc != X509_V_OK) {
1479 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1480 "client SSL certificate verify error: (%l:%s)",
1481 rc, X509_verify_cert_error_string(rc));
1483 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1484 (SSL_get0_session(c->ssl->connection)));
1486 ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
1490 cert = SSL_get_peer_certificate(c->ssl->connection);
1493 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1494 "client sent no required SSL certificate");
1496 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1497 (SSL_get0_session(c->ssl->connection)));
1499 ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
1509 if (c->read->timer_set) {
1510 ngx_del_timer(c->read);
1514 ngx_atomic_fetch_add(ngx_stat_reading, -1);
1515 r->stat_reading = 0;
1516 ngx_atomic_fetch_add(ngx_stat_writing, 1);
1517 r->stat_writing = 1;
1520 c->read->handler = ngx_http_request_handler;
1521 c->write->handler = ngx_http_request_handler;
1522 r->read_event_handler = ngx_http_block_reading;
1524 ngx_http_handler(r);
1526 ngx_http_run_posted_requests(c);
1531 ngx_http_validate_host(u_char *host, size_t len)
1540 for (i = 0; i < len; i++) {
1559 if (ch == '/' || ch == '\0') {
1579 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
1583 ngx_http_core_loc_conf_t *clcf;
1584 ngx_http_core_srv_conf_t *cscf;
1587 if (r->virtual_names == NULL) {
1588 return NGX_DECLINED;
1595 server = ngx_pnalloc(r->pool, len);
1596 if (server == NULL) {
1601 hash = ngx_hash_strlow(server, host, len);
1603 cscf = ngx_hash_find_combined(&r->virtual_names->names, hash, server, len);
1611 if (r->virtual_names->nregex) {
1615 ngx_http_server_name_t *sn;
1620 sn = r->virtual_names->regex;
1622 for (i = 0; i < r->virtual_names->nregex; i++) {
1624 n = ngx_regex_exec(sn[i].regex, &name, NULL, 0);
1626 if (n == NGX_REGEX_NO_MATCHED) {
1631 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1633 " failed: %d on \"%V\" using \"%V\"",
1634 n, &name, &sn[i].name);
1640 cscf = sn[i].core_srv_conf;
1652 r->srv_conf = cscf->ctx->srv_conf;
1653 r->loc_conf = cscf->ctx->loc_conf;
1655 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1656 r->connection->log->file = clcf->err_log->file;
1658 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
1659 r->connection->log->log_level = clcf->err_log->log_level;
1667 ngx_http_request_handler(ngx_event_t *ev)
1669 ngx_connection_t *c;
1670 ngx_http_request_t *r;
1671 ngx_http_log_ctx_t *ctx;
1677 ctx->current_request = r;
1679 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1680 "http run request: \"%V?%V\"", &r->uri, &r->args);
1683 r->write_event_handler(r);
1686 r->read_event_handler(r);
1689 ngx_http_run_posted_requests(c);
1694 ngx_http_run_posted_requests(ngx_connection_t *c)
1696 ngx_http_request_t *r;
1697 ngx_http_log_ctx_t *ctx;
1698 ngx_http_posted_request_t *pr;
1707 pr = r->main->posted_requests;
1713 r->main->posted_requests = pr->next;
1718 ctx->current_request = r;
1720 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1721 "http posted request: \"%V?%V\"", &r->uri, &r->args);
1723 r->write_event_handler(r);
1729 ngx_http_post_request(ngx_http_request_t *r)
1731 ngx_http_posted_request_t *pr, **p;
1733 pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
1741 for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
1750 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1752 ngx_connection_t *c;
1753 ngx_http_request_t *pr;
1754 ngx_http_core_loc_conf_t *clcf;
1756 if (rc == NGX_DONE) {
1757 /* the request pool may be already destroyed */
1763 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
1764 "http finalize request: %d, \"%V?%V\" %d",
1765 rc, &r->uri, &r->args, r == c->data);
1767 if (rc == NGX_DECLINED) {
1768 r->content_handler = NULL;
1769 r->write_event_handler = ngx_http_core_run_phases;
1770 ngx_http_core_run_phases(r);
1774 if (r != r->main && r->post_subrequest) {
1775 rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
1779 || rc == NGX_HTTP_REQUEST_TIME_OUT
1780 || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
1783 if (rc > 0 && r->headers_out.status == 0) {
1784 r->headers_out.status = rc;
1787 if (ngx_http_post_action(r) == NGX_OK) {
1791 ngx_http_close_request(r, 0);
1795 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
1796 || rc == NGX_HTTP_CREATED
1797 || rc == NGX_HTTP_NO_CONTENT)
1799 if (rc == NGX_HTTP_CLOSE) {
1800 ngx_http_close_request(r, rc);
1805 if (c->read->timer_set) {
1806 ngx_del_timer(c->read);
1809 if (c->write->timer_set) {
1810 ngx_del_timer(c->write);
1814 c->read->handler = ngx_http_request_handler;
1815 c->write->handler = ngx_http_request_handler;
1817 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
1823 if (r->buffered || r->postponed) {
1825 if (ngx_http_set_write_handler(r) != NGX_OK) {
1826 ngx_http_close_request(r->main, 0);
1834 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1835 "http finalize non-active request: \"%V?%V\"",
1846 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1848 if (clcf->log_subrequest) {
1849 ngx_http_log_request(r);
1855 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1856 "subrequest: \"%V?%V\" logged again",
1862 if (pr->postponed && pr->postponed->request == r) {
1863 pr->postponed = pr->postponed->next;
1870 r->write_event_handler = ngx_http_request_finalizer;
1877 if (ngx_http_post_request(pr) != NGX_OK) {
1878 ngx_http_close_request(r->main, 0);
1882 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1883 "http wake parent request: \"%V?%V\"",
1884 &pr->uri, &pr->args);
1889 if (r->buffered || c->buffered || r->postponed) {
1891 if (ngx_http_set_write_handler(r) != NGX_OK) {
1892 ngx_http_close_request(r, 0);
1899 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1900 "http finalize non-active request: \"%V?%V\"",
1907 if (!r->post_action) {
1908 r->request_complete = 1;
1911 if (ngx_http_post_action(r) == NGX_OK) {
1915 if (c->read->timer_set) {
1916 ngx_del_timer(c->read);
1919 if (c->write->timer_set) {
1920 c->write->delayed = 0;
1921 ngx_del_timer(c->write);
1929 ngx_http_close_request(r, 0);
1933 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1938 && clcf->keepalive_timeout > 0)
1940 ngx_http_set_keepalive(r);
1943 } else if (r->lingering_close && clcf->lingering_timeout > 0) {
1944 ngx_http_set_lingering_close(r);
1948 ngx_http_close_request(r, 0);
1953 ngx_http_set_write_handler(ngx_http_request_t *r)
1956 ngx_http_core_loc_conf_t *clcf;
1958 r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
1960 r->read_event_handler = ngx_http_test_reading;
1961 r->write_event_handler = ngx_http_writer;
1963 wev = r->connection->write;
1965 if (wev->ready && wev->delayed) {
1969 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1970 if (!wev->delayed) {
1971 ngx_add_timer(wev, clcf->send_timeout);
1974 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
1975 ngx_http_close_request(r, 0);
1984 ngx_http_writer(ngx_http_request_t *r)
1988 ngx_connection_t *c;
1989 ngx_http_core_loc_conf_t *clcf;
1994 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
1995 "http writer handler: \"%V?%V\"", &r->uri, &r->args);
1997 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
1999 if (wev->timedout) {
2000 if (!wev->delayed) {
2001 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
2002 "client timed out");
2005 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
2013 ngx_add_timer(wev, clcf->send_timeout);
2015 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2016 ngx_http_close_request(r, 0);
2024 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2025 "http writer delayed");
2027 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2028 ngx_http_close_request(r, 0);
2035 rc = ngx_http_output_filter(r, NULL);
2041 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
2042 "http writer output filter: %d, \"%V?%V\"",
2043 rc, &r->uri, &r->args);
2045 if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
2047 if (!wev->ready && !wev->delayed) {
2048 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);
2058 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2059 "http writer done: \"%V?%V\"", &r->uri, &r->args);
2061 ngx_http_finalize_request(r, rc);
2066 ngx_http_request_finalizer(ngx_http_request_t *r)
2068 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2069 "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
2071 ngx_http_finalize_request(r, 0);
2076 ngx_http_block_reading(ngx_http_request_t *r)
2078 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2079 "http reading blocked");
2081 /* aio does not call this handler */
2083 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
2084 && r->connection->read->active)
2086 if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
2087 ngx_http_close_request(r, 0);
2094 ngx_http_test_reading(ngx_http_request_t *r)
2100 ngx_connection_t *c;
2105 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
2107 #if (NGX_HAVE_KQUEUE)
2109 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2111 if (!rev->pending_eof) {
2117 err = rev->kq_errno;
2124 n = recv(c->fd, buf, 1, MSG_PEEK);
2133 } else if (n == -1) {
2134 err = ngx_socket_errno;
2136 if (err != NGX_EAGAIN) {
2144 /* aio does not call this handler */
2146 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
2148 if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
2149 ngx_http_close_request(r, 0);
2161 ngx_log_error(NGX_LOG_INFO, c->log, err,
2162 "client closed prematurely connection");
2164 ngx_http_finalize_request(r, 0);
2169 ngx_http_set_keepalive(ngx_http_request_t *r)
2174 ngx_event_t *rev, *wev;
2175 ngx_connection_t *c;
2176 ngx_http_connection_t *hc;
2177 ngx_http_core_srv_conf_t *cscf;
2178 ngx_http_core_loc_conf_t *clcf;
2183 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2185 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
2187 if (r->discard_body) {
2188 r->write_event_handler = ngx_http_request_empty_handler;
2189 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2190 ngx_add_timer(rev, clcf->lingering_timeout);
2194 c->log->action = "closing request";
2196 hc = r->http_connection;
2199 if (b->pos < b->last) {
2201 /* the pipelined request */
2203 if (b != c->buffer) {
2206 * If the large header buffers were allocated while the previous
2207 * request processing then we do not use c->buffer for
2208 * the pipelined request (see ngx_http_init_request()).
2210 * Now we would move the large header buffers to the free list.
2213 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2215 if (hc->free == NULL) {
2216 hc->free = ngx_palloc(c->pool,
2217 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
2219 if (hc->free == NULL) {
2220 ngx_http_close_request(r, 0);
2225 for (i = 0; i < hc->nbusy - 1; i++) {
2227 hc->free[hc->nfree++] = f;
2237 ngx_http_request_done(r, 0);
2241 ngx_add_timer(rev, clcf->keepalive_timeout);
2243 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2244 ngx_http_close_connection(c);
2249 wev->handler = ngx_http_empty_handler;
2251 if (b->pos < b->last) {
2253 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
2256 ngx_atomic_fetch_add(ngx_stat_reading, 1);
2260 c->log->action = "reading client pipelined request line";
2262 rev->handler = ngx_http_init_request;
2263 ngx_post_event(rev, &ngx_posted_events);
2270 * To keep a memory footprint as small as possible for an idle
2271 * keepalive connection we try to free the ngx_http_request_t and
2272 * c->buffer's memory if they were allocated outside the c->pool.
2273 * The large header buffers are always allocated outside the c->pool and
2277 if (ngx_pfree(c->pool, r) == NGX_OK) {
2283 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
2286 * the special note for ngx_http_keepalive_handler() that
2287 * c->buffer's memory was freed
2297 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
2298 hc->free, hc->nfree);
2301 for (i = 0; i < hc->nfree; i++) {
2302 ngx_pfree(c->pool, hc->free[i]->start);
2309 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
2310 hc->busy, hc->nbusy);
2313 for (i = 0; i < hc->nbusy; i++) {
2314 ngx_pfree(c->pool, hc->busy[i]->start);
2323 ngx_ssl_free_buffer(c);
2327 rev->handler = ngx_http_keepalive_handler;
2329 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2330 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2331 ngx_http_close_connection(c);
2336 c->log->action = "keepalive";
2338 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
2339 if (ngx_tcp_push(c->fd) == -1) {
2340 ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
2341 ngx_http_close_connection(c);
2345 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
2346 tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
2353 && clcf->tcp_nodelay
2354 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
2356 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
2358 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
2359 (const void *) &tcp_nodelay, sizeof(int))
2362 ngx_connection_error(c, ngx_socket_errno,
2363 "setsockopt(TCP_NODELAY) failed");
2364 ngx_http_close_connection(c);
2368 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
2372 /* if ngx_http_request_t was freed then we need some other place */
2373 r->http_state = NGX_HTTP_KEEPALIVE_STATE;
2379 ngx_post_event(rev, &ngx_posted_events);
2385 ngx_http_keepalive_handler(ngx_event_t *rev)
2390 ngx_connection_t *c;
2394 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
2396 if (rev->timedout || c->close) {
2397 ngx_http_close_connection(c);
2401 #if (NGX_HAVE_KQUEUE)
2403 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2404 if (rev->pending_eof) {
2405 c->log->handler = NULL;
2406 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
2407 "kevent() reported that client %V closed "
2408 "keepalive connection", &c->addr_text);
2411 c->ssl->no_send_shutdown = 1;
2414 ngx_http_close_connection(c);
2422 size = b->end - b->start;
2424 if (b->pos == NULL) {
2427 * The c->buffer's memory was freed by ngx_http_set_keepalive().
2428 * However, the c->buffer->start and c->buffer->end were not changed
2429 * to keep the buffer size.
2432 b->pos = ngx_palloc(c->pool, size);
2433 if (b->pos == NULL) {
2434 ngx_http_close_connection(c);
2440 b->end = b->pos + size;
2444 * MSIE closes a keepalive connection with RST flag
2445 * so we ignore ECONNRESET here.
2448 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
2449 ngx_set_socket_errno(0);
2451 n = c->recv(c, b->last, size);
2452 c->log_error = NGX_ERROR_INFO;
2454 if (n == NGX_AGAIN) {
2455 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2456 ngx_http_close_connection(c);
2462 if (n == NGX_ERROR) {
2463 ngx_http_close_connection(c);
2467 c->log->handler = NULL;
2470 ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
2471 "client %V closed keepalive connection", &c->addr_text);
2472 ngx_http_close_connection(c);
2479 ngx_atomic_fetch_add(ngx_stat_reading, 1);
2482 c->log->handler = ngx_http_log_error;
2483 c->log->action = "reading client request line";
2487 ngx_http_init_request(rev);
2492 ngx_http_set_lingering_close(ngx_http_request_t *r)
2494 ngx_event_t *rev, *wev;
2495 ngx_connection_t *c;
2496 ngx_http_core_loc_conf_t *clcf;
2500 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2503 rev->handler = ngx_http_lingering_close_handler;
2505 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2506 ngx_add_timer(rev, clcf->lingering_timeout);
2508 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2509 ngx_http_close_request(r, 0);
2514 wev->handler = ngx_http_empty_handler;
2516 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2517 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2518 ngx_http_close_request(r, 0);
2523 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
2524 ngx_connection_error(c, ngx_socket_errno,
2525 ngx_shutdown_socket_n " failed");
2526 ngx_http_close_request(r, 0);
2531 ngx_http_lingering_close_handler(rev);
2537 ngx_http_lingering_close_handler(ngx_event_t *rev)
2541 ngx_connection_t *c;
2542 ngx_http_request_t *r;
2543 ngx_http_core_loc_conf_t *clcf;
2544 u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
2549 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2550 "http lingering close handler");
2552 if (rev->timedout) {
2554 ngx_http_close_request(r, 0);
2558 timer = (ngx_msec_t) (r->lingering_time - ngx_time());
2560 ngx_http_close_request(r, 0);
2565 n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
2567 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
2569 if (n == NGX_ERROR || n == 0) {
2570 ngx_http_close_request(r, 0);
2574 } while (rev->ready);
2576 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2577 ngx_http_close_request(r, 0);
2581 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2585 if (timer > clcf->lingering_timeout) {
2586 timer = clcf->lingering_timeout;
2589 ngx_add_timer(rev, timer);
2594 ngx_http_empty_handler(ngx_event_t *wev)
2596 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
2603 ngx_http_request_empty_handler(ngx_http_request_t *r)
2605 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2606 "http request empty handler");
2613 ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
2618 b = ngx_calloc_buf(r->pool);
2623 if (flags & NGX_HTTP_LAST) {
2627 if (flags & NGX_HTTP_FLUSH) {
2634 return ngx_http_output_filter(r, &out);
2639 ngx_http_post_action(ngx_http_request_t *r)
2641 ngx_http_core_loc_conf_t *clcf;
2643 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2645 if (clcf->post_action.data == NULL) {
2646 return NGX_DECLINED;
2649 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2650 "post action: \"%V\"", &clcf->post_action);
2652 r->http_version = NGX_HTTP_VERSION_9;
2656 r->read_event_handler = ngx_http_block_reading;
2658 if (clcf->post_action.data[0] == '/') {
2659 ngx_http_internal_redirect(r, &clcf->post_action, NULL);
2662 ngx_http_named_location(r, &clcf->post_action);
2670 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
2672 ngx_connection_t *c;
2676 ngx_http_request_done(r->main, error);
2677 ngx_http_close_connection(c);
2682 ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
2685 struct linger linger;
2686 ngx_http_cleanup_t *cln;
2687 ngx_http_log_ctx_t *ctx;
2688 ngx_http_core_loc_conf_t *clcf;
2690 log = r->connection->log;
2692 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
2694 if (r->pool == NULL) {
2695 ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
2699 for (cln = r->cleanup; cln; cln = cln->next) {
2701 cln->handler(cln->data);
2707 if (r->stat_reading) {
2708 ngx_atomic_fetch_add(ngx_stat_reading, -1);
2711 if (r->stat_writing) {
2712 ngx_atomic_fetch_add(ngx_stat_writing, -1);
2717 if (error && r->headers_out.status == 0) {
2718 r->headers_out.status = error;
2721 log->action = "logging request";
2723 ngx_http_log_request(r);
2725 log->action = "closing request";
2727 if (r->connection->timedout) {
2728 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2730 if (clcf->reset_timedout_connection) {
2732 linger.l_linger = 0;
2734 if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
2735 (const void *) &linger, sizeof(struct linger)) == -1)
2737 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
2738 "setsockopt(SO_LINGER) failed");
2743 /* the various request strings were allocated from r->pool */
2745 ctx->request = NULL;
2747 r->request_line.len = 0;
2749 r->connection->destroyed = 1;
2751 ngx_destroy_pool(r->pool);
2756 ngx_http_log_request(ngx_http_request_t *r)
2759 ngx_http_handler_pt *log_handler;
2760 ngx_http_core_main_conf_t *cmcf;
2762 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
2764 log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
2765 n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
2767 for (i = 0; i < n; i++) {
2774 ngx_http_close_connection(ngx_connection_t *c)
2778 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
2779 "close http connection: %d", c->fd);
2784 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
2785 c->ssl->handler = ngx_http_close_connection;
2793 ngx_atomic_fetch_add(ngx_stat_active, -1);
2800 ngx_close_connection(c);
2802 ngx_destroy_pool(pool);
2807 ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
2810 ngx_http_request_t *r;
2811 ngx_http_log_ctx_t *ctx;
2814 p = ngx_snprintf(buf, len, " while %s", log->action);
2821 p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
2827 return r->log_handler(r, ctx->current_request, p, len);
2830 p = ngx_snprintf(p, len, ", server: %V",
2831 &ctx->connection->listening->addr_text);
2839 ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
2840 u_char *buf, size_t len)
2842 char *uri_separator;
2844 ngx_http_upstream_t *u;
2845 ngx_http_core_srv_conf_t *cscf;
2847 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2849 p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
2853 if (r->request_line.data == NULL && r->request_start) {
2854 for (p = r->request_start; p < r->header_in->last; p++) {
2855 if (*p == CR || *p == LF) {
2860 r->request_line.len = p - r->request_start;
2861 r->request_line.data = r->request_start;
2864 if (r->request_line.len) {
2865 p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
2871 p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
2878 if (u && u->peer.name) {
2882 #if (NGX_HAVE_UNIX_DOMAIN)
2883 if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
2884 uri_separator = ":";
2888 p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
2889 &u->schema, u->peer.name,
2890 uri_separator, &u->uri);
2895 if (r->headers_in.host) {
2896 p = ngx_snprintf(buf, len, ", host: \"%V\"",
2897 &r->headers_in.host->value);
2902 if (r->headers_in.referer) {
2903 p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
2904 &r->headers_in.referer->value);