nginx 1.1.11
[nginx.git] / src / http / ngx_http_request.c
blobfbb7915b3408d07e3d353905ede15035f8c460db
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.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(ngx_http_request_t *r, u_char **host,
35 size_t len, ngx_uint_t alloc);
36 static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r,
37 u_char *host, size_t len);
39 static void ngx_http_request_handler(ngx_event_t *ev);
40 static void ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc);
41 static void ngx_http_terminate_handler(ngx_http_request_t *r);
42 static void ngx_http_finalize_connection(ngx_http_request_t *r);
43 static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
44 static void ngx_http_writer(ngx_http_request_t *r);
45 static void ngx_http_request_finalizer(ngx_http_request_t *r);
47 static void ngx_http_set_keepalive(ngx_http_request_t *r);
48 static void ngx_http_keepalive_handler(ngx_event_t *ev);
49 static void ngx_http_set_lingering_close(ngx_http_request_t *r);
50 static void ngx_http_lingering_close_handler(ngx_event_t *ev);
51 static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
52 static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
53 static void ngx_http_free_request(ngx_http_request_t *r, ngx_int_t error);
54 static void ngx_http_log_request(ngx_http_request_t *r);
55 static void ngx_http_close_connection(ngx_connection_t *c);
57 static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
58 static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
59 ngx_http_request_t *sr, u_char *buf, size_t len);
61 #if (NGX_HTTP_SSL)
62 static void ngx_http_ssl_handshake(ngx_event_t *rev);
63 static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
64 #endif
67 static char *ngx_http_client_errors[] = {
69 /* NGX_HTTP_PARSE_INVALID_METHOD */
70 "client sent invalid method",
72 /* NGX_HTTP_PARSE_INVALID_REQUEST */
73 "client sent invalid request",
75 /* NGX_HTTP_PARSE_INVALID_09_METHOD */
76 "client sent invalid method in HTTP/0.9 request"
80 ngx_http_header_t ngx_http_headers_in[] = {
81 { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host),
82 ngx_http_process_host },
84 { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
85 ngx_http_process_connection },
87 { ngx_string("If-Modified-Since"),
88 offsetof(ngx_http_headers_in_t, if_modified_since),
89 ngx_http_process_unique_header_line },
91 { ngx_string("If-Unmodified-Since"),
92 offsetof(ngx_http_headers_in_t, if_unmodified_since),
93 ngx_http_process_unique_header_line },
95 { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
96 ngx_http_process_user_agent },
98 { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
99 ngx_http_process_header_line },
101 { ngx_string("Content-Length"),
102 offsetof(ngx_http_headers_in_t, content_length),
103 ngx_http_process_unique_header_line },
105 { ngx_string("Content-Type"),
106 offsetof(ngx_http_headers_in_t, content_type),
107 ngx_http_process_header_line },
109 { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
110 ngx_http_process_header_line },
112 { ngx_string("If-Range"),
113 offsetof(ngx_http_headers_in_t, if_range),
114 ngx_http_process_unique_header_line },
116 { ngx_string("Transfer-Encoding"),
117 offsetof(ngx_http_headers_in_t, transfer_encoding),
118 ngx_http_process_header_line },
120 { ngx_string("Expect"),
121 offsetof(ngx_http_headers_in_t, expect),
122 ngx_http_process_unique_header_line },
124 #if (NGX_HTTP_GZIP)
125 { ngx_string("Accept-Encoding"),
126 offsetof(ngx_http_headers_in_t, accept_encoding),
127 ngx_http_process_header_line },
129 { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
130 ngx_http_process_header_line },
131 #endif
133 { ngx_string("Authorization"),
134 offsetof(ngx_http_headers_in_t, authorization),
135 ngx_http_process_unique_header_line },
137 { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
138 ngx_http_process_header_line },
140 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP || NGX_HTTP_GEO)
141 { ngx_string("X-Forwarded-For"),
142 offsetof(ngx_http_headers_in_t, x_forwarded_for),
143 ngx_http_process_header_line },
144 #endif
146 #if (NGX_HTTP_REALIP)
147 { ngx_string("X-Real-IP"),
148 offsetof(ngx_http_headers_in_t, x_real_ip),
149 ngx_http_process_header_line },
150 #endif
152 #if (NGX_HTTP_HEADERS)
153 { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
154 ngx_http_process_header_line },
156 { ngx_string("Accept-Language"),
157 offsetof(ngx_http_headers_in_t, accept_language),
158 ngx_http_process_header_line },
159 #endif
161 #if (NGX_HTTP_DAV)
162 { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
163 ngx_http_process_header_line },
165 { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
166 ngx_http_process_header_line },
168 { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite),
169 ngx_http_process_header_line },
171 { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date),
172 ngx_http_process_header_line },
173 #endif
175 { ngx_string("Cookie"), 0, ngx_http_process_cookie },
177 { ngx_null_string, 0, NULL }
181 void
182 ngx_http_init_connection(ngx_connection_t *c)
184 ngx_event_t *rev;
185 ngx_http_log_ctx_t *ctx;
187 ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
188 if (ctx == NULL) {
189 ngx_http_close_connection(c);
190 return;
193 ctx->connection = c;
194 ctx->request = NULL;
195 ctx->current_request = NULL;
197 c->log->connection = c->number;
198 c->log->handler = ngx_http_log_error;
199 c->log->data = ctx;
200 c->log->action = "reading client request line";
202 c->log_error = NGX_ERROR_INFO;
204 rev = c->read;
205 rev->handler = ngx_http_init_request;
206 c->write->handler = ngx_http_empty_handler;
208 #if (NGX_STAT_STUB)
209 (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
210 #endif
212 if (rev->ready) {
213 /* the deferred accept(), rtsig, aio, iocp */
215 if (ngx_use_accept_mutex) {
216 ngx_post_event(rev, &ngx_posted_events);
217 return;
220 ngx_http_init_request(rev);
221 return;
224 ngx_add_timer(rev, c->listening->post_accept_timeout);
226 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
227 #if (NGX_STAT_STUB)
228 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
229 #endif
230 ngx_http_close_connection(c);
231 return;
236 static void
237 ngx_http_init_request(ngx_event_t *rev)
239 ngx_time_t *tp;
240 ngx_uint_t i;
241 ngx_connection_t *c;
242 ngx_http_request_t *r;
243 struct sockaddr_in *sin;
244 ngx_http_port_t *port;
245 ngx_http_in_addr_t *addr;
246 ngx_http_log_ctx_t *ctx;
247 ngx_http_addr_conf_t *addr_conf;
248 ngx_http_connection_t *hc;
249 ngx_http_core_srv_conf_t *cscf;
250 ngx_http_core_loc_conf_t *clcf;
251 ngx_http_core_main_conf_t *cmcf;
252 #if (NGX_HAVE_INET6)
253 struct sockaddr_in6 *sin6;
254 ngx_http_in6_addr_t *addr6;
255 #endif
257 #if (NGX_STAT_STUB)
258 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
259 #endif
261 c = rev->data;
263 if (rev->timedout) {
264 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
266 ngx_http_close_connection(c);
267 return;
270 c->requests++;
272 hc = c->data;
274 if (hc == NULL) {
275 hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
276 if (hc == NULL) {
277 ngx_http_close_connection(c);
278 return;
282 r = hc->request;
284 if (r) {
285 ngx_memzero(r, sizeof(ngx_http_request_t));
287 r->pipeline = hc->pipeline;
289 if (hc->nbusy) {
290 r->header_in = hc->busy[0];
293 } else {
294 r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
295 if (r == NULL) {
296 ngx_http_close_connection(c);
297 return;
300 hc->request = r;
303 c->data = r;
304 r->http_connection = hc;
306 c->sent = 0;
307 r->signature = NGX_HTTP_MODULE;
309 /* find the server configuration for the address:port */
311 port = c->listening->servers;
313 r->connection = c;
315 if (port->naddrs > 1) {
318 * there are several addresses on this port and one of them
319 * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
320 * is required to determine a server address
323 if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
324 ngx_http_close_connection(c);
325 return;
328 switch (c->local_sockaddr->sa_family) {
330 #if (NGX_HAVE_INET6)
331 case AF_INET6:
332 sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
334 addr6 = port->addrs;
336 /* the last address is "*" */
338 for (i = 0; i < port->naddrs - 1; i++) {
339 if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
340 break;
344 addr_conf = &addr6[i].conf;
346 break;
347 #endif
349 default: /* AF_INET */
350 sin = (struct sockaddr_in *) c->local_sockaddr;
352 addr = port->addrs;
354 /* the last address is "*" */
356 for (i = 0; i < port->naddrs - 1; i++) {
357 if (addr[i].addr == sin->sin_addr.s_addr) {
358 break;
362 addr_conf = &addr[i].conf;
364 break;
367 } else {
369 switch (c->local_sockaddr->sa_family) {
371 #if (NGX_HAVE_INET6)
372 case AF_INET6:
373 addr6 = port->addrs;
374 addr_conf = &addr6[0].conf;
375 break;
376 #endif
378 default: /* AF_INET */
379 addr = port->addrs;
380 addr_conf = &addr[0].conf;
381 break;
385 r->virtual_names = addr_conf->virtual_names;
387 /* the default server configuration for the address:port */
388 cscf = addr_conf->default_server;
390 r->main_conf = cscf->ctx->main_conf;
391 r->srv_conf = cscf->ctx->srv_conf;
392 r->loc_conf = cscf->ctx->loc_conf;
394 rev->handler = ngx_http_process_request_line;
395 r->read_event_handler = ngx_http_block_reading;
397 #if (NGX_HTTP_SSL)
400 ngx_http_ssl_srv_conf_t *sscf;
402 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
403 if (sscf->enable || addr_conf->ssl) {
405 if (c->ssl == NULL) {
407 c->log->action = "SSL handshaking";
409 if (addr_conf->ssl && sscf->ssl.ctx == NULL) {
410 ngx_log_error(NGX_LOG_ERR, c->log, 0,
411 "no \"ssl_certificate\" is defined "
412 "in server listening on SSL port");
413 ngx_http_close_connection(c);
414 return;
417 if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
418 != NGX_OK)
420 ngx_http_close_connection(c);
421 return;
424 rev->handler = ngx_http_ssl_handshake;
427 r->main_filter_need_in_memory = 1;
431 #endif
433 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
434 c->log->file = clcf->error_log->file;
435 if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
436 c->log->log_level = clcf->error_log->log_level;
439 if (c->buffer == NULL) {
440 c->buffer = ngx_create_temp_buf(c->pool,
441 cscf->client_header_buffer_size);
442 if (c->buffer == NULL) {
443 ngx_http_close_connection(c);
444 return;
448 if (r->header_in == NULL) {
449 r->header_in = c->buffer;
452 r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
453 if (r->pool == NULL) {
454 ngx_http_close_connection(c);
455 return;
459 if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
460 sizeof(ngx_table_elt_t))
461 != NGX_OK)
463 ngx_destroy_pool(r->pool);
464 ngx_http_close_connection(c);
465 return;
468 r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
469 if (r->ctx == NULL) {
470 ngx_destroy_pool(r->pool);
471 ngx_http_close_connection(c);
472 return;
475 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
477 r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
478 * sizeof(ngx_http_variable_value_t));
479 if (r->variables == NULL) {
480 ngx_destroy_pool(r->pool);
481 ngx_http_close_connection(c);
482 return;
485 c->single_connection = 1;
486 c->destroyed = 0;
488 r->main = r;
489 r->count = 1;
491 tp = ngx_timeofday();
492 r->start_sec = tp->sec;
493 r->start_msec = tp->msec;
495 r->method = NGX_HTTP_UNKNOWN;
497 r->headers_in.content_length_n = -1;
498 r->headers_in.keep_alive_n = -1;
499 r->headers_out.content_length_n = -1;
500 r->headers_out.last_modified_time = -1;
502 r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
503 r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
505 r->http_state = NGX_HTTP_READING_REQUEST_STATE;
507 ctx = c->log->data;
508 ctx->request = r;
509 ctx->current_request = r;
510 r->log_handler = ngx_http_log_error_handler;
512 #if (NGX_STAT_STUB)
513 (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
514 r->stat_reading = 1;
515 (void) ngx_atomic_fetch_add(ngx_stat_requests, 1);
516 #endif
518 rev->handler(rev);
522 #if (NGX_HTTP_SSL)
524 static void
525 ngx_http_ssl_handshake(ngx_event_t *rev)
527 u_char buf[1];
528 ssize_t n;
529 ngx_int_t rc;
530 ngx_connection_t *c;
531 ngx_http_request_t *r;
533 c = rev->data;
534 r = c->data;
536 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
537 "http check ssl handshake");
539 if (rev->timedout) {
540 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
541 c->timedout = 1;
542 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
543 return;
546 n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
548 if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
550 if (!rev->timer_set) {
551 ngx_add_timer(rev, c->listening->post_accept_timeout);
554 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
555 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
558 return;
561 if (n == 1) {
562 if (buf[0] & 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
563 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
564 "https ssl handshake: 0x%02Xd", buf[0]);
566 rc = ngx_ssl_handshake(c);
568 if (rc == NGX_AGAIN) {
570 if (!rev->timer_set) {
571 ngx_add_timer(rev, c->listening->post_accept_timeout);
574 c->ssl->handler = ngx_http_ssl_handshake_handler;
575 return;
578 ngx_http_ssl_handshake_handler(c);
580 return;
582 } else {
583 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
584 "plain http");
586 r->plain_http = 1;
590 c->log->action = "reading client request line";
592 rev->handler = ngx_http_process_request_line;
593 ngx_http_process_request_line(rev);
597 static void
598 ngx_http_ssl_handshake_handler(ngx_connection_t *c)
600 ngx_http_request_t *r;
602 if (c->ssl->handshaked) {
605 * The majority of browsers do not send the "close notify" alert.
606 * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
607 * and Links. And what is more, MSIE ignores the server's alert.
609 * Opera and recent Mozilla send the alert.
612 c->ssl->no_wait_shutdown = 1;
614 c->read->handler = ngx_http_process_request_line;
615 /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
617 ngx_http_process_request_line(c->read);
619 return;
622 r = c->data;
624 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
626 return;
629 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
632 ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
634 size_t len;
635 u_char *host;
636 const char *servername;
637 ngx_connection_t *c;
638 ngx_http_request_t *r;
639 ngx_http_ssl_srv_conf_t *sscf;
641 servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
643 if (servername == NULL) {
644 return SSL_TLSEXT_ERR_NOACK;
647 c = ngx_ssl_get_connection(ssl_conn);
649 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
650 "SSL server name: \"%s\"", servername);
652 len = ngx_strlen(servername);
654 if (len == 0) {
655 return SSL_TLSEXT_ERR_NOACK;
658 r = c->data;
660 host = (u_char *) servername;
662 len = ngx_http_validate_host(r, &host, len, 1);
664 if (len <= 0) {
665 return SSL_TLSEXT_ERR_NOACK;
668 if (ngx_http_find_virtual_server(r, host, len) != NGX_OK) {
669 return SSL_TLSEXT_ERR_NOACK;
672 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
674 if (sscf->ssl.ctx) {
675 SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
678 * SSL_set_SSL_CTX() only changes certs as of 1.0.0d
679 * adjust other things we care about
682 SSL_set_verify(ssl_conn, SSL_CTX_get_verify_mode(sscf->ssl.ctx),
683 SSL_CTX_get_verify_callback(sscf->ssl.ctx));
685 SSL_set_verify_depth(ssl_conn, SSL_CTX_get_verify_depth(sscf->ssl.ctx));
687 #ifdef SSL_CTRL_CLEAR_OPTIONS
688 /* only in 0.9.8m+ */
689 SSL_clear_options(ssl_conn, SSL_get_options(ssl_conn) &
690 ~SSL_CTX_get_options(sscf->ssl.ctx));
691 #endif
693 SSL_set_options(ssl_conn, SSL_CTX_get_options(sscf->ssl.ctx));
696 return SSL_TLSEXT_ERR_OK;
699 #endif
701 #endif
704 static void
705 ngx_http_process_request_line(ngx_event_t *rev)
707 u_char *host;
708 ssize_t n;
709 ngx_int_t rc, rv;
710 ngx_connection_t *c;
711 ngx_http_request_t *r;
712 ngx_http_core_srv_conf_t *cscf;
714 c = rev->data;
715 r = c->data;
717 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
718 "http process request line");
720 if (rev->timedout) {
721 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
722 c->timedout = 1;
723 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
724 return;
727 rc = NGX_AGAIN;
729 for ( ;; ) {
731 if (rc == NGX_AGAIN) {
732 n = ngx_http_read_request_header(r);
734 if (n == NGX_AGAIN || n == NGX_ERROR) {
735 return;
739 rc = ngx_http_parse_request_line(r, r->header_in);
741 if (rc == NGX_OK) {
743 /* the request line has been parsed successfully */
745 r->request_line.len = r->request_end - r->request_start;
746 r->request_line.data = r->request_start;
749 if (r->args_start) {
750 r->uri.len = r->args_start - 1 - r->uri_start;
751 } else {
752 r->uri.len = r->uri_end - r->uri_start;
756 if (r->complex_uri || r->quoted_uri) {
758 r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
759 if (r->uri.data == NULL) {
760 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
761 return;
764 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
766 rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
768 if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
769 ngx_log_error(NGX_LOG_INFO, c->log, 0,
770 "client sent invalid request");
771 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
772 return;
775 } else {
776 r->uri.data = r->uri_start;
780 r->unparsed_uri.len = r->uri_end - r->uri_start;
781 r->unparsed_uri.data = r->uri_start;
783 r->valid_unparsed_uri = r->space_in_uri ? 0 : 1;
785 r->method_name.len = r->method_end - r->request_start + 1;
786 r->method_name.data = r->request_line.data;
789 if (r->http_protocol.data) {
790 r->http_protocol.len = r->request_end - r->http_protocol.data;
794 if (r->uri_ext) {
795 if (r->args_start) {
796 r->exten.len = r->args_start - 1 - r->uri_ext;
797 } else {
798 r->exten.len = r->uri_end - r->uri_ext;
801 r->exten.data = r->uri_ext;
805 if (r->args_start && r->uri_end > r->args_start) {
806 r->args.len = r->uri_end - r->args_start;
807 r->args.data = r->args_start;
810 #if (NGX_WIN32)
812 u_char *p;
814 p = r->uri.data + r->uri.len - 1;
816 while (p > r->uri.data) {
818 if (*p == ' ') {
819 p--;
820 continue;
823 if (*p == '.') {
824 p--;
825 continue;
828 if (ngx_strncasecmp(p - 6, (u_char *) "::$data", 7) == 0) {
829 p -= 7;
830 continue;
833 break;
836 if (p != r->uri.data + r->uri.len - 1) {
837 r->uri.len = p + 1 - r->uri.data;
838 ngx_http_set_exten(r);
842 #endif
844 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
845 "http request line: \"%V\"", &r->request_line);
847 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
848 "http uri: \"%V\"", &r->uri);
850 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
851 "http args: \"%V\"", &r->args);
853 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
854 "http exten: \"%V\"", &r->exten);
856 if (r->host_start && r->host_end) {
858 host = r->host_start;
859 n = ngx_http_validate_host(r, &host,
860 r->host_end - r->host_start, 0);
862 if (n == 0) {
863 ngx_log_error(NGX_LOG_INFO, c->log, 0,
864 "client sent invalid host in request line");
865 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
866 return;
869 if (n < 0) {
870 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
871 return;
874 r->headers_in.server.len = n;
875 r->headers_in.server.data = host;
878 if (r->http_version < NGX_HTTP_VERSION_10) {
880 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
881 r->headers_in.server.len)
882 == NGX_ERROR)
884 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
885 return;
888 ngx_http_process_request(r);
889 return;
893 if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
894 sizeof(ngx_table_elt_t))
895 != NGX_OK)
897 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
898 return;
902 if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
903 sizeof(ngx_table_elt_t *))
904 != NGX_OK)
906 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
907 return;
910 c->log->action = "reading client request headers";
912 rev->handler = ngx_http_process_request_headers;
913 ngx_http_process_request_headers(rev);
915 return;
918 if (rc != NGX_AGAIN) {
920 /* there was error while a request line parsing */
922 ngx_log_error(NGX_LOG_INFO, c->log, 0,
923 ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
924 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
925 return;
928 /* NGX_AGAIN: a request line parsing is still incomplete */
930 if (r->header_in->pos == r->header_in->end) {
932 rv = ngx_http_alloc_large_header_buffer(r, 1);
934 if (rv == NGX_ERROR) {
935 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
936 return;
939 if (rv == NGX_DECLINED) {
940 r->request_line.len = r->header_in->end - r->request_start;
941 r->request_line.data = r->request_start;
943 ngx_log_error(NGX_LOG_INFO, c->log, 0,
944 "client sent too long URI");
945 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
946 return;
953 static void
954 ngx_http_process_request_headers(ngx_event_t *rev)
956 u_char *p;
957 size_t len;
958 ssize_t n;
959 ngx_int_t rc, rv;
960 ngx_table_elt_t *h;
961 ngx_connection_t *c;
962 ngx_http_header_t *hh;
963 ngx_http_request_t *r;
964 ngx_http_core_srv_conf_t *cscf;
965 ngx_http_core_main_conf_t *cmcf;
967 c = rev->data;
968 r = c->data;
970 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
971 "http process request header line");
973 if (rev->timedout) {
974 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
975 c->timedout = 1;
976 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
977 return;
980 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
981 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
983 rc = NGX_AGAIN;
985 for ( ;; ) {
987 if (rc == NGX_AGAIN) {
989 if (r->header_in->pos == r->header_in->end) {
991 rv = ngx_http_alloc_large_header_buffer(r, 0);
993 if (rv == NGX_ERROR) {
994 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
995 return;
998 if (rv == NGX_DECLINED) {
999 p = r->header_name_start;
1001 r->lingering_close = 1;
1003 if (p == NULL) {
1004 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1005 "client sent too large request");
1006 ngx_http_finalize_request(r,
1007 NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
1008 return;
1011 len = r->header_in->end - p;
1013 if (len > NGX_MAX_ERROR_STR - 300) {
1014 len = NGX_MAX_ERROR_STR - 300;
1015 p[len++] = '.'; p[len++] = '.'; p[len++] = '.';
1018 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1019 "client sent too long header line: \"%*s\"",
1020 len, r->header_name_start);
1022 ngx_http_finalize_request(r,
1023 NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
1024 return;
1028 n = ngx_http_read_request_header(r);
1030 if (n == NGX_AGAIN || n == NGX_ERROR) {
1031 return;
1035 rc = ngx_http_parse_header_line(r, r->header_in,
1036 cscf->underscores_in_headers);
1038 if (rc == NGX_OK) {
1040 if (r->invalid_header && cscf->ignore_invalid_headers) {
1042 /* there was error while a header line parsing */
1044 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1045 "client sent invalid header line: \"%*s\"",
1046 r->header_end - r->header_name_start,
1047 r->header_name_start);
1048 continue;
1051 /* a header line has been parsed successfully */
1053 h = ngx_list_push(&r->headers_in.headers);
1054 if (h == NULL) {
1055 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1056 return;
1059 h->hash = r->header_hash;
1061 h->key.len = r->header_name_end - r->header_name_start;
1062 h->key.data = r->header_name_start;
1063 h->key.data[h->key.len] = '\0';
1065 h->value.len = r->header_end - r->header_start;
1066 h->value.data = r->header_start;
1067 h->value.data[h->value.len] = '\0';
1069 h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
1070 if (h->lowcase_key == NULL) {
1071 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1072 return;
1075 if (h->key.len == r->lowcase_index) {
1076 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
1078 } else {
1079 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
1082 hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
1083 h->lowcase_key, h->key.len);
1085 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
1086 return;
1089 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1090 "http header: \"%V: %V\"",
1091 &h->key, &h->value);
1093 continue;
1096 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1098 /* a whole header has been parsed successfully */
1100 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1101 "http header done");
1103 r->request_length += r->header_in->pos - r->header_in->start;
1105 r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
1107 rc = ngx_http_process_request_header(r);
1109 if (rc != NGX_OK) {
1110 return;
1113 ngx_http_process_request(r);
1115 return;
1118 if (rc == NGX_AGAIN) {
1120 /* a header line parsing is still not complete */
1122 continue;
1125 /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
1127 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1128 "client sent invalid header line: \"%*s\\r...\"",
1129 r->header_end - r->header_name_start,
1130 r->header_name_start);
1131 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1132 return;
1137 static ssize_t
1138 ngx_http_read_request_header(ngx_http_request_t *r)
1140 ssize_t n;
1141 ngx_event_t *rev;
1142 ngx_connection_t *c;
1143 ngx_http_core_srv_conf_t *cscf;
1145 c = r->connection;
1146 rev = c->read;
1148 n = r->header_in->last - r->header_in->pos;
1150 if (n > 0) {
1151 return n;
1154 if (rev->ready) {
1155 n = c->recv(c, r->header_in->last,
1156 r->header_in->end - r->header_in->last);
1157 } else {
1158 n = NGX_AGAIN;
1161 if (n == NGX_AGAIN) {
1162 if (!rev->timer_set) {
1163 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1164 ngx_add_timer(rev, cscf->client_header_timeout);
1167 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
1168 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1169 return NGX_ERROR;
1172 return NGX_AGAIN;
1175 if (n == 0) {
1176 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1177 "client closed prematurely connection");
1180 if (n == 0 || n == NGX_ERROR) {
1181 c->error = 1;
1182 c->log->action = "reading client request headers";
1184 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1185 return NGX_ERROR;
1188 r->header_in->last += n;
1190 return n;
1194 static ngx_int_t
1195 ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
1196 ngx_uint_t request_line)
1198 u_char *old, *new;
1199 ngx_buf_t *b;
1200 ngx_http_connection_t *hc;
1201 ngx_http_core_srv_conf_t *cscf;
1203 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1204 "http alloc large header buffer");
1206 if (request_line && r->state == 0) {
1208 /* the client fills up the buffer with "\r\n" */
1210 r->request_length += r->header_in->end - r->header_in->start;
1212 r->header_in->pos = r->header_in->start;
1213 r->header_in->last = r->header_in->start;
1215 return NGX_OK;
1218 old = request_line ? r->request_start : r->header_name_start;
1220 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1222 if (r->state != 0
1223 && (size_t) (r->header_in->pos - old)
1224 >= cscf->large_client_header_buffers.size)
1226 return NGX_DECLINED;
1229 hc = r->http_connection;
1231 if (hc->nfree) {
1232 b = hc->free[--hc->nfree];
1234 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1235 "http large header free: %p %uz",
1236 b->pos, b->end - b->last);
1238 } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
1240 if (hc->busy == NULL) {
1241 hc->busy = ngx_palloc(r->connection->pool,
1242 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
1243 if (hc->busy == NULL) {
1244 return NGX_ERROR;
1248 b = ngx_create_temp_buf(r->connection->pool,
1249 cscf->large_client_header_buffers.size);
1250 if (b == NULL) {
1251 return NGX_ERROR;
1254 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1255 "http large header alloc: %p %uz",
1256 b->pos, b->end - b->last);
1258 } else {
1259 return NGX_DECLINED;
1262 hc->busy[hc->nbusy++] = b;
1264 if (r->state == 0) {
1266 * r->state == 0 means that a header line was parsed successfully
1267 * and we do not need to copy incomplete header line and
1268 * to relocate the parser header pointers
1271 r->request_length += r->header_in->end - r->header_in->start;
1273 r->header_in = b;
1275 return NGX_OK;
1278 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1279 "http large header copy: %d", r->header_in->pos - old);
1281 r->request_length += old - r->header_in->start;
1283 new = b->start;
1285 ngx_memcpy(new, old, r->header_in->pos - old);
1287 b->pos = new + (r->header_in->pos - old);
1288 b->last = new + (r->header_in->pos - old);
1290 if (request_line) {
1291 r->request_start = new;
1293 if (r->request_end) {
1294 r->request_end = new + (r->request_end - old);
1297 r->method_end = new + (r->method_end - old);
1299 r->uri_start = new + (r->uri_start - old);
1300 r->uri_end = new + (r->uri_end - old);
1302 if (r->schema_start) {
1303 r->schema_start = new + (r->schema_start - old);
1304 r->schema_end = new + (r->schema_end - old);
1307 if (r->host_start) {
1308 r->host_start = new + (r->host_start - old);
1309 if (r->host_end) {
1310 r->host_end = new + (r->host_end - old);
1314 if (r->port_start) {
1315 r->port_start = new + (r->port_start - old);
1316 r->port_end = new + (r->port_end - old);
1319 if (r->uri_ext) {
1320 r->uri_ext = new + (r->uri_ext - old);
1323 if (r->args_start) {
1324 r->args_start = new + (r->args_start - old);
1327 if (r->http_protocol.data) {
1328 r->http_protocol.data = new + (r->http_protocol.data - old);
1331 } else {
1332 r->header_name_start = new;
1333 r->header_name_end = new + (r->header_name_end - old);
1334 r->header_start = new + (r->header_start - old);
1335 r->header_end = new + (r->header_end - old);
1338 r->header_in = b;
1340 return NGX_OK;
1344 static ngx_int_t
1345 ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1346 ngx_uint_t offset)
1348 ngx_table_elt_t **ph;
1350 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1352 if (*ph == NULL) {
1353 *ph = h;
1356 return NGX_OK;
1360 static ngx_int_t
1361 ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1362 ngx_uint_t offset)
1364 ngx_table_elt_t **ph;
1366 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1368 if (*ph == NULL) {
1369 *ph = h;
1370 return NGX_OK;
1373 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1374 "client sent duplicate header line: \"%V: %V\", "
1375 "previous value: \"%V: %V\"",
1376 &h->key, &h->value, &(*ph)->key, &(*ph)->value);
1378 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1380 return NGX_ERROR;
1384 static ngx_int_t
1385 ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
1386 ngx_uint_t offset)
1388 u_char *host;
1389 ssize_t len;
1391 if (r->headers_in.host == NULL) {
1392 r->headers_in.host = h;
1395 host = h->value.data;
1396 len = ngx_http_validate_host(r, &host, h->value.len, 0);
1398 if (len == 0) {
1399 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1400 "client sent invalid host header");
1401 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1402 return NGX_ERROR;
1405 if (len < 0) {
1406 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1407 return NGX_ERROR;
1410 if (r->headers_in.server.len) {
1411 return NGX_OK;
1414 r->headers_in.server.len = len;
1415 r->headers_in.server.data = host;
1417 return NGX_OK;
1421 static ngx_int_t
1422 ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
1423 ngx_uint_t offset)
1425 if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
1426 r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
1428 } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
1429 r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
1432 return NGX_OK;
1436 static ngx_int_t
1437 ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
1438 ngx_uint_t offset)
1440 u_char *user_agent, *msie;
1442 if (r->headers_in.user_agent) {
1443 return NGX_OK;
1446 r->headers_in.user_agent = h;
1448 /* check some widespread browsers while the header is in CPU cache */
1450 user_agent = h->value.data;
1452 msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
1454 if (msie && msie + 7 < user_agent + h->value.len) {
1456 r->headers_in.msie = 1;
1458 if (msie[6] == '.') {
1460 switch (msie[5]) {
1461 case '4':
1462 case '5':
1463 r->headers_in.msie6 = 1;
1464 break;
1465 case '6':
1466 if (ngx_strstrn(msie + 8, "SV1", 3 - 1) == NULL) {
1467 r->headers_in.msie6 = 1;
1469 break;
1473 #if 0
1474 /* MSIE ignores the SSL "close notify" alert */
1475 if (c->ssl) {
1476 c->ssl->no_send_shutdown = 1;
1478 #endif
1481 if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
1482 r->headers_in.opera = 1;
1483 r->headers_in.msie = 0;
1484 r->headers_in.msie6 = 0;
1487 if (!r->headers_in.msie && !r->headers_in.opera) {
1489 if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
1490 r->headers_in.gecko = 1;
1492 } else if (ngx_strstrn(user_agent, "Chrome/", 7 - 1)) {
1493 r->headers_in.chrome = 1;
1495 } else if (ngx_strstrn(user_agent, "Safari/", 7 - 1)) {
1496 r->headers_in.safari = 1;
1498 } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
1499 r->headers_in.konqueror = 1;
1503 return NGX_OK;
1507 static ngx_int_t
1508 ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
1509 ngx_uint_t offset)
1511 ngx_table_elt_t **cookie;
1513 cookie = ngx_array_push(&r->headers_in.cookies);
1514 if (cookie) {
1515 *cookie = h;
1516 return NGX_OK;
1519 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1521 return NGX_ERROR;
1525 static ngx_int_t
1526 ngx_http_process_request_header(ngx_http_request_t *r)
1528 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
1529 r->headers_in.server.len)
1530 == NGX_ERROR)
1532 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1533 return NGX_ERROR;
1536 if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
1537 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1538 "client sent HTTP/1.1 request without \"Host\" header");
1539 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1540 return NGX_ERROR;
1543 if (r->headers_in.content_length) {
1544 r->headers_in.content_length_n =
1545 ngx_atoof(r->headers_in.content_length->value.data,
1546 r->headers_in.content_length->value.len);
1548 if (r->headers_in.content_length_n == NGX_ERROR) {
1549 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1550 "client sent invalid \"Content-Length\" header");
1551 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1552 return NGX_ERROR;
1556 if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
1557 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1558 "client sent %V method without \"Content-Length\" header",
1559 &r->method_name);
1560 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1561 return NGX_ERROR;
1564 if (r->method & NGX_HTTP_TRACE) {
1565 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1566 "client sent TRACE method");
1567 ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
1568 return NGX_ERROR;
1571 if (r->headers_in.transfer_encoding
1572 && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
1573 "chunked", 7 - 1))
1575 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1576 "client sent \"Transfer-Encoding: chunked\" header");
1577 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1578 return NGX_ERROR;
1581 if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
1582 if (r->headers_in.keep_alive) {
1583 r->headers_in.keep_alive_n =
1584 ngx_atotm(r->headers_in.keep_alive->value.data,
1585 r->headers_in.keep_alive->value.len);
1589 return NGX_OK;
1593 static void
1594 ngx_http_process_request(ngx_http_request_t *r)
1596 ngx_connection_t *c;
1598 c = r->connection;
1600 if (r->plain_http) {
1601 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1602 "client sent plain HTTP request to HTTPS port");
1603 ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
1604 return;
1607 #if (NGX_HTTP_SSL)
1609 if (c->ssl) {
1610 long rc;
1611 X509 *cert;
1612 ngx_http_ssl_srv_conf_t *sscf;
1614 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
1616 if (sscf->verify) {
1617 rc = SSL_get_verify_result(c->ssl->connection);
1619 if (rc != X509_V_OK) {
1620 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1621 "client SSL certificate verify error: (%l:%s)",
1622 rc, X509_verify_cert_error_string(rc));
1624 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1625 (SSL_get0_session(c->ssl->connection)));
1627 ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
1628 return;
1631 if (sscf->verify == 1) {
1632 cert = SSL_get_peer_certificate(c->ssl->connection);
1634 if (cert == NULL) {
1635 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1636 "client sent no required SSL certificate");
1638 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1639 (SSL_get0_session(c->ssl->connection)));
1641 ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
1642 return;
1645 X509_free(cert);
1650 #endif
1652 if (c->read->timer_set) {
1653 ngx_del_timer(c->read);
1656 #if (NGX_STAT_STUB)
1657 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
1658 r->stat_reading = 0;
1659 (void) ngx_atomic_fetch_add(ngx_stat_writing, 1);
1660 r->stat_writing = 1;
1661 #endif
1663 c->read->handler = ngx_http_request_handler;
1664 c->write->handler = ngx_http_request_handler;
1665 r->read_event_handler = ngx_http_block_reading;
1667 ngx_http_handler(r);
1669 ngx_http_run_posted_requests(c);
1673 static ssize_t
1674 ngx_http_validate_host(ngx_http_request_t *r, u_char **host, size_t len,
1675 ngx_uint_t alloc)
1677 u_char *h, ch;
1678 size_t i, dot_pos, host_len;
1680 enum {
1681 sw_usual = 0,
1682 sw_literal,
1683 sw_rest
1684 } state;
1686 dot_pos = len;
1687 host_len = len;
1689 h = *host;
1691 state = sw_usual;
1693 for (i = 0; i < len; i++) {
1694 ch = h[i];
1696 switch (ch) {
1698 case '.':
1699 if (dot_pos == i - 1) {
1700 return 0;
1702 dot_pos = i;
1703 break;
1705 case ':':
1706 if (state == sw_usual) {
1707 host_len = i;
1708 state = sw_rest;
1710 break;
1712 case '[':
1713 if (i == 0) {
1714 state = sw_literal;
1716 break;
1718 case ']':
1719 if (state == sw_literal) {
1720 host_len = i + 1;
1721 state = sw_rest;
1723 break;
1725 case '\0':
1726 return 0;
1728 default:
1730 if (ngx_path_separator(ch)) {
1731 return 0;
1734 if (ch >= 'A' && ch <= 'Z') {
1735 alloc = 1;
1738 break;
1742 if (dot_pos == host_len - 1) {
1743 host_len--;
1746 if (alloc) {
1747 *host = ngx_pnalloc(r->pool, host_len);
1748 if (*host == NULL) {
1749 return -1;
1752 ngx_strlow(*host, h, host_len);
1755 return host_len;
1759 static ngx_int_t
1760 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
1762 ngx_http_core_loc_conf_t *clcf;
1763 ngx_http_core_srv_conf_t *cscf;
1765 if (r->virtual_names == NULL) {
1766 return NGX_DECLINED;
1769 cscf = ngx_hash_find_combined(&r->virtual_names->names,
1770 ngx_hash_key(host, len), host, len);
1772 if (cscf) {
1773 goto found;
1776 #if (NGX_PCRE)
1778 if (len && r->virtual_names->nregex) {
1779 ngx_int_t n;
1780 ngx_uint_t i;
1781 ngx_str_t name;
1782 ngx_http_server_name_t *sn;
1784 name.len = len;
1785 name.data = host;
1787 sn = r->virtual_names->regex;
1789 for (i = 0; i < r->virtual_names->nregex; i++) {
1791 n = ngx_http_regex_exec(r, sn[i].regex, &name);
1793 if (n == NGX_OK) {
1794 cscf = sn[i].server;
1795 goto found;
1798 if (n == NGX_DECLINED) {
1799 continue;
1802 return NGX_ERROR;
1806 #endif
1808 return NGX_OK;
1810 found:
1812 r->srv_conf = cscf->ctx->srv_conf;
1813 r->loc_conf = cscf->ctx->loc_conf;
1815 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1816 r->connection->log->file = clcf->error_log->file;
1818 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
1819 r->connection->log->log_level = clcf->error_log->log_level;
1822 return NGX_OK;
1826 static void
1827 ngx_http_request_handler(ngx_event_t *ev)
1829 ngx_connection_t *c;
1830 ngx_http_request_t *r;
1831 ngx_http_log_ctx_t *ctx;
1833 c = ev->data;
1834 r = c->data;
1836 ctx = c->log->data;
1837 ctx->current_request = r;
1839 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1840 "http run request: \"%V?%V\"", &r->uri, &r->args);
1842 if (ev->write) {
1843 r->write_event_handler(r);
1845 } else {
1846 r->read_event_handler(r);
1849 ngx_http_run_posted_requests(c);
1853 void
1854 ngx_http_run_posted_requests(ngx_connection_t *c)
1856 ngx_http_request_t *r;
1857 ngx_http_log_ctx_t *ctx;
1858 ngx_http_posted_request_t *pr;
1860 for ( ;; ) {
1862 if (c->destroyed) {
1863 return;
1866 r = c->data;
1867 pr = r->main->posted_requests;
1869 if (pr == NULL) {
1870 return;
1873 r->main->posted_requests = pr->next;
1875 r = pr->request;
1877 ctx = c->log->data;
1878 ctx->current_request = r;
1880 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1881 "http posted request: \"%V?%V\"", &r->uri, &r->args);
1883 r->write_event_handler(r);
1888 ngx_int_t
1889 ngx_http_post_request(ngx_http_request_t *r, ngx_http_posted_request_t *pr)
1891 ngx_http_posted_request_t **p;
1893 if (pr == NULL) {
1894 pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
1895 if (pr == NULL) {
1896 return NGX_ERROR;
1900 pr->request = r;
1901 pr->next = NULL;
1903 for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
1905 *p = pr;
1907 return NGX_OK;
1911 void
1912 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1914 ngx_connection_t *c;
1915 ngx_http_request_t *pr;
1916 ngx_http_core_loc_conf_t *clcf;
1918 c = r->connection;
1920 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,
1921 "http finalize request: %d, \"%V?%V\" a:%d, c:%d",
1922 rc, &r->uri, &r->args, r == c->data, r->main->count);
1924 if (rc == NGX_DONE) {
1925 ngx_http_finalize_connection(r);
1926 return;
1929 if (rc == NGX_OK && r->filter_finalize) {
1930 c->error = 1;
1931 return;
1934 if (rc == NGX_DECLINED) {
1935 r->content_handler = NULL;
1936 r->write_event_handler = ngx_http_core_run_phases;
1937 ngx_http_core_run_phases(r);
1938 return;
1941 if (r != r->main && r->post_subrequest) {
1942 rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
1945 if (rc == NGX_ERROR
1946 || rc == NGX_HTTP_REQUEST_TIME_OUT
1947 || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
1948 || c->error)
1950 if (ngx_http_post_action(r) == NGX_OK) {
1951 return;
1954 if (r->main->blocked) {
1955 r->write_event_handler = ngx_http_request_finalizer;
1958 ngx_http_terminate_request(r, rc);
1959 return;
1962 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
1963 || rc == NGX_HTTP_CREATED
1964 || rc == NGX_HTTP_NO_CONTENT)
1966 if (rc == NGX_HTTP_CLOSE) {
1967 ngx_http_terminate_request(r, rc);
1968 return;
1971 if (r == r->main) {
1972 if (c->read->timer_set) {
1973 ngx_del_timer(c->read);
1976 if (c->write->timer_set) {
1977 ngx_del_timer(c->write);
1981 c->read->handler = ngx_http_request_handler;
1982 c->write->handler = ngx_http_request_handler;
1984 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
1985 return;
1988 if (r != r->main) {
1990 if (r->buffered || r->postponed) {
1992 if (ngx_http_set_write_handler(r) != NGX_OK) {
1993 ngx_http_terminate_request(r, 0);
1996 return;
1999 #if (NGX_DEBUG)
2000 if (r != c->data) {
2001 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
2002 "http finalize non-active request: \"%V?%V\"",
2003 &r->uri, &r->args);
2005 #endif
2007 pr = r->parent;
2009 if (r == c->data) {
2011 r->main->count--;
2013 if (!r->logged) {
2015 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2017 if (clcf->log_subrequest) {
2018 ngx_http_log_request(r);
2021 r->logged = 1;
2023 } else {
2024 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
2025 "subrequest: \"%V?%V\" logged again",
2026 &r->uri, &r->args);
2029 r->done = 1;
2031 if (pr->postponed && pr->postponed->request == r) {
2032 pr->postponed = pr->postponed->next;
2035 c->data = pr;
2037 } else {
2039 r->write_event_handler = ngx_http_request_finalizer;
2041 if (r->waited) {
2042 r->done = 1;
2046 if (ngx_http_post_request(pr, NULL) != NGX_OK) {
2047 r->main->count++;
2048 ngx_http_terminate_request(r, 0);
2049 return;
2052 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
2053 "http wake parent request: \"%V?%V\"",
2054 &pr->uri, &pr->args);
2056 return;
2059 if (r->buffered || c->buffered || r->postponed || r->blocked) {
2061 if (ngx_http_set_write_handler(r) != NGX_OK) {
2062 ngx_http_terminate_request(r, 0);
2065 return;
2068 if (r != c->data) {
2069 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
2070 "http finalize non-active request: \"%V?%V\"",
2071 &r->uri, &r->args);
2072 return;
2075 r->done = 1;
2076 r->write_event_handler = ngx_http_request_empty_handler;
2078 if (!r->post_action) {
2079 r->request_complete = 1;
2082 if (ngx_http_post_action(r) == NGX_OK) {
2083 return;
2086 if (c->read->timer_set) {
2087 ngx_del_timer(c->read);
2090 if (c->write->timer_set) {
2091 c->write->delayed = 0;
2092 ngx_del_timer(c->write);
2095 if (c->read->eof) {
2096 ngx_http_close_request(r, 0);
2097 return;
2100 ngx_http_finalize_connection(r);
2104 static void
2105 ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc)
2107 ngx_http_cleanup_t *cln;
2108 ngx_http_request_t *mr;
2109 ngx_http_ephemeral_t *e;
2111 mr = r->main;
2113 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2114 "http terminate request count:%d", mr->count);
2116 if (rc > 0 && (mr->headers_out.status == 0 || mr->connection->sent == 0)) {
2117 mr->headers_out.status = rc;
2120 cln = mr->cleanup;
2121 mr->cleanup = NULL;
2123 while (cln) {
2124 if (cln->handler) {
2125 cln->handler(cln->data);
2128 cln = cln->next;
2131 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2132 "http terminate cleanup count:%d blk:%d",
2133 mr->count, mr->blocked);
2135 if (mr->write_event_handler) {
2137 if (mr->blocked) {
2138 return;
2141 e = ngx_http_ephemeral(mr);
2142 mr->posted_requests = NULL;
2143 mr->write_event_handler = ngx_http_terminate_handler;
2144 (void) ngx_http_post_request(mr, &e->terminal_posted_request);
2145 return;
2148 ngx_http_close_request(mr, rc);
2152 static void
2153 ngx_http_terminate_handler(ngx_http_request_t *r)
2155 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2156 "http terminate handler count:%d", r->count);
2158 r->count = 1;
2160 ngx_http_close_request(r, 0);
2164 static void
2165 ngx_http_finalize_connection(ngx_http_request_t *r)
2167 ngx_http_core_loc_conf_t *clcf;
2169 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2171 if (r->main->count != 1) {
2173 if (r->discard_body) {
2174 r->read_event_handler = ngx_http_discarded_request_body_handler;
2175 ngx_add_timer(r->connection->read, clcf->lingering_timeout);
2177 if (r->lingering_time == 0) {
2178 r->lingering_time = ngx_time()
2179 + (time_t) (clcf->lingering_time / 1000);
2183 ngx_http_close_request(r, 0);
2184 return;
2187 if (!ngx_terminate
2188 && !ngx_exiting
2189 && r->keepalive
2190 && clcf->keepalive_timeout > 0)
2192 ngx_http_set_keepalive(r);
2193 return;
2196 if (clcf->lingering_close == NGX_HTTP_LINGERING_ALWAYS
2197 || (clcf->lingering_close == NGX_HTTP_LINGERING_ON
2198 && (r->lingering_close
2199 || r->header_in->pos < r->header_in->last
2200 || r->connection->read->ready)))
2202 ngx_http_set_lingering_close(r);
2203 return;
2206 ngx_http_close_request(r, 0);
2210 static ngx_int_t
2211 ngx_http_set_write_handler(ngx_http_request_t *r)
2213 ngx_event_t *wev;
2214 ngx_http_core_loc_conf_t *clcf;
2216 r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
2218 r->read_event_handler = r->discard_body ?
2219 ngx_http_discarded_request_body_handler:
2220 ngx_http_test_reading;
2221 r->write_event_handler = ngx_http_writer;
2223 wev = r->connection->write;
2225 if (wev->ready && wev->delayed) {
2226 return NGX_OK;
2229 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2230 if (!wev->delayed) {
2231 ngx_add_timer(wev, clcf->send_timeout);
2234 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2235 ngx_http_close_request(r, 0);
2236 return NGX_ERROR;
2239 return NGX_OK;
2243 static void
2244 ngx_http_writer(ngx_http_request_t *r)
2246 int rc;
2247 ngx_event_t *wev;
2248 ngx_connection_t *c;
2249 ngx_http_core_loc_conf_t *clcf;
2251 c = r->connection;
2252 wev = c->write;
2254 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2255 "http writer handler: \"%V?%V\"", &r->uri, &r->args);
2257 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
2259 if (wev->timedout) {
2260 if (!wev->delayed) {
2261 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
2262 "client timed out");
2263 c->timedout = 1;
2265 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
2266 return;
2269 wev->timedout = 0;
2270 wev->delayed = 0;
2272 if (!wev->ready) {
2273 ngx_add_timer(wev, clcf->send_timeout);
2275 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2276 ngx_http_close_request(r, 0);
2279 return;
2284 if (wev->delayed || r->aio) {
2285 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2286 "http writer delayed");
2288 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2289 ngx_http_close_request(r, 0);
2292 return;
2295 rc = ngx_http_output_filter(r, NULL);
2297 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
2298 "http writer output filter: %d, \"%V?%V\"",
2299 rc, &r->uri, &r->args);
2301 if (rc == NGX_ERROR) {
2302 ngx_http_finalize_request(r, rc);
2303 return;
2306 if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
2308 if (!wev->delayed) {
2309 ngx_add_timer(wev, clcf->send_timeout);
2312 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2313 ngx_http_close_request(r, 0);
2316 return;
2319 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2320 "http writer done: \"%V?%V\"", &r->uri, &r->args);
2322 r->write_event_handler = ngx_http_request_empty_handler;
2324 ngx_http_finalize_request(r, rc);
2328 static void
2329 ngx_http_request_finalizer(ngx_http_request_t *r)
2331 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2332 "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
2334 ngx_http_finalize_request(r, 0);
2338 void
2339 ngx_http_block_reading(ngx_http_request_t *r)
2341 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2342 "http reading blocked");
2344 /* aio does not call this handler */
2346 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
2347 && r->connection->read->active)
2349 if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
2350 ngx_http_close_request(r, 0);
2356 void
2357 ngx_http_test_reading(ngx_http_request_t *r)
2359 int n;
2360 char buf[1];
2361 ngx_err_t err;
2362 ngx_event_t *rev;
2363 ngx_connection_t *c;
2365 c = r->connection;
2366 rev = c->read;
2368 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
2370 #if (NGX_HAVE_KQUEUE)
2372 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2374 if (!rev->pending_eof) {
2375 return;
2378 rev->eof = 1;
2379 c->error = 1;
2380 err = rev->kq_errno;
2382 goto closed;
2385 #endif
2387 n = recv(c->fd, buf, 1, MSG_PEEK);
2389 if (n == 0) {
2390 rev->eof = 1;
2391 c->error = 1;
2392 err = 0;
2394 goto closed;
2396 } else if (n == -1) {
2397 err = ngx_socket_errno;
2399 if (err != NGX_EAGAIN) {
2400 rev->eof = 1;
2401 c->error = 1;
2403 goto closed;
2407 /* aio does not call this handler */
2409 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
2411 if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
2412 ngx_http_close_request(r, 0);
2416 return;
2418 closed:
2420 if (err) {
2421 rev->error = 1;
2424 ngx_log_error(NGX_LOG_INFO, c->log, err,
2425 "client closed prematurely connection");
2427 ngx_http_finalize_request(r, 0);
2431 static void
2432 ngx_http_set_keepalive(ngx_http_request_t *r)
2434 int tcp_nodelay;
2435 ngx_int_t i;
2436 ngx_buf_t *b, *f;
2437 ngx_event_t *rev, *wev;
2438 ngx_connection_t *c;
2439 ngx_http_connection_t *hc;
2440 ngx_http_core_srv_conf_t *cscf;
2441 ngx_http_core_loc_conf_t *clcf;
2443 c = r->connection;
2444 rev = c->read;
2446 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2448 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
2450 if (r->discard_body) {
2451 r->write_event_handler = ngx_http_request_empty_handler;
2452 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2453 ngx_add_timer(rev, clcf->lingering_timeout);
2454 return;
2457 c->log->action = "closing request";
2459 hc = r->http_connection;
2460 b = r->header_in;
2462 if (b->pos < b->last) {
2464 /* the pipelined request */
2466 if (b != c->buffer) {
2469 * If the large header buffers were allocated while the previous
2470 * request processing then we do not use c->buffer for
2471 * the pipelined request (see ngx_http_init_request()).
2473 * Now we would move the large header buffers to the free list.
2476 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2478 if (hc->free == NULL) {
2479 hc->free = ngx_palloc(c->pool,
2480 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
2482 if (hc->free == NULL) {
2483 ngx_http_close_request(r, 0);
2484 return;
2488 for (i = 0; i < hc->nbusy - 1; i++) {
2489 f = hc->busy[i];
2490 hc->free[hc->nfree++] = f;
2491 f->pos = f->start;
2492 f->last = f->start;
2495 hc->busy[0] = b;
2496 hc->nbusy = 1;
2500 r->keepalive = 0;
2502 ngx_http_free_request(r, 0);
2504 c->data = hc;
2506 ngx_add_timer(rev, clcf->keepalive_timeout);
2508 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2509 ngx_http_close_connection(c);
2510 return;
2513 wev = c->write;
2514 wev->handler = ngx_http_empty_handler;
2516 if (b->pos < b->last) {
2518 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
2520 #if (NGX_STAT_STUB)
2521 (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
2522 #endif
2524 hc->pipeline = 1;
2525 c->log->action = "reading client pipelined request line";
2527 rev->handler = ngx_http_init_request;
2528 ngx_post_event(rev, &ngx_posted_events);
2529 return;
2532 hc->pipeline = 0;
2535 * To keep a memory footprint as small as possible for an idle
2536 * keepalive connection we try to free the ngx_http_request_t and
2537 * c->buffer's memory if they were allocated outside the c->pool.
2538 * The large header buffers are always allocated outside the c->pool and
2539 * are freed too.
2542 if (ngx_pfree(c->pool, r) == NGX_OK) {
2543 hc->request = NULL;
2546 b = c->buffer;
2548 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
2551 * the special note for ngx_http_keepalive_handler() that
2552 * c->buffer's memory was freed
2555 b->pos = NULL;
2557 } else {
2558 b->pos = b->start;
2559 b->last = b->start;
2562 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
2563 hc->free, hc->nfree);
2565 if (hc->free) {
2566 for (i = 0; i < hc->nfree; i++) {
2567 ngx_pfree(c->pool, hc->free[i]->start);
2568 hc->free[i] = NULL;
2571 hc->nfree = 0;
2574 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
2575 hc->busy, hc->nbusy);
2577 if (hc->busy) {
2578 for (i = 0; i < hc->nbusy; i++) {
2579 ngx_pfree(c->pool, hc->busy[i]->start);
2580 hc->busy[i] = NULL;
2583 hc->nbusy = 0;
2586 #if (NGX_HTTP_SSL)
2587 if (c->ssl) {
2588 ngx_ssl_free_buffer(c);
2590 #endif
2592 rev->handler = ngx_http_keepalive_handler;
2594 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2595 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2596 ngx_http_close_connection(c);
2597 return;
2601 c->log->action = "keepalive";
2603 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
2604 if (ngx_tcp_push(c->fd) == -1) {
2605 ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
2606 ngx_http_close_connection(c);
2607 return;
2610 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
2611 tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
2613 } else {
2614 tcp_nodelay = 1;
2617 if (tcp_nodelay
2618 && clcf->tcp_nodelay
2619 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
2621 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
2623 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
2624 (const void *) &tcp_nodelay, sizeof(int))
2625 == -1)
2627 #if (NGX_SOLARIS)
2628 /* Solaris returns EINVAL if a socket has been shut down */
2629 c->log_error = NGX_ERROR_IGNORE_EINVAL;
2630 #endif
2632 ngx_connection_error(c, ngx_socket_errno,
2633 "setsockopt(TCP_NODELAY) failed");
2635 c->log_error = NGX_ERROR_INFO;
2636 ngx_http_close_connection(c);
2637 return;
2640 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
2643 #if 0
2644 /* if ngx_http_request_t was freed then we need some other place */
2645 r->http_state = NGX_HTTP_KEEPALIVE_STATE;
2646 #endif
2648 c->idle = 1;
2649 ngx_reusable_connection(c, 1);
2651 if (rev->ready) {
2652 ngx_post_event(rev, &ngx_posted_events);
2657 static void
2658 ngx_http_keepalive_handler(ngx_event_t *rev)
2660 size_t size;
2661 ssize_t n;
2662 ngx_buf_t *b;
2663 ngx_connection_t *c;
2665 c = rev->data;
2667 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
2669 if (rev->timedout || c->close) {
2670 ngx_http_close_connection(c);
2671 return;
2674 #if (NGX_HAVE_KQUEUE)
2676 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2677 if (rev->pending_eof) {
2678 c->log->handler = NULL;
2679 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
2680 "kevent() reported that client %V closed "
2681 "keepalive connection", &c->addr_text);
2682 #if (NGX_HTTP_SSL)
2683 if (c->ssl) {
2684 c->ssl->no_send_shutdown = 1;
2686 #endif
2687 ngx_http_close_connection(c);
2688 return;
2692 #endif
2694 b = c->buffer;
2695 size = b->end - b->start;
2697 if (b->pos == NULL) {
2700 * The c->buffer's memory was freed by ngx_http_set_keepalive().
2701 * However, the c->buffer->start and c->buffer->end were not changed
2702 * to keep the buffer size.
2705 b->pos = ngx_palloc(c->pool, size);
2706 if (b->pos == NULL) {
2707 ngx_http_close_connection(c);
2708 return;
2711 b->start = b->pos;
2712 b->last = b->pos;
2713 b->end = b->pos + size;
2717 * MSIE closes a keepalive connection with RST flag
2718 * so we ignore ECONNRESET here.
2721 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
2722 ngx_set_socket_errno(0);
2724 n = c->recv(c, b->last, size);
2725 c->log_error = NGX_ERROR_INFO;
2727 if (n == NGX_AGAIN) {
2728 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2729 ngx_http_close_connection(c);
2732 return;
2735 if (n == NGX_ERROR) {
2736 ngx_http_close_connection(c);
2737 return;
2740 c->log->handler = NULL;
2742 if (n == 0) {
2743 ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
2744 "client %V closed keepalive connection", &c->addr_text);
2745 ngx_http_close_connection(c);
2746 return;
2749 b->last += n;
2751 #if (NGX_STAT_STUB)
2752 (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
2753 #endif
2755 c->log->handler = ngx_http_log_error;
2756 c->log->action = "reading client request line";
2758 c->idle = 0;
2759 ngx_reusable_connection(c, 0);
2761 ngx_http_init_request(rev);
2765 static void
2766 ngx_http_set_lingering_close(ngx_http_request_t *r)
2768 ngx_event_t *rev, *wev;
2769 ngx_connection_t *c;
2770 ngx_http_core_loc_conf_t *clcf;
2772 c = r->connection;
2774 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2776 rev = c->read;
2777 rev->handler = ngx_http_lingering_close_handler;
2779 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2780 ngx_add_timer(rev, clcf->lingering_timeout);
2782 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2783 ngx_http_close_request(r, 0);
2784 return;
2787 wev = c->write;
2788 wev->handler = ngx_http_empty_handler;
2790 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2791 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2792 ngx_http_close_request(r, 0);
2793 return;
2797 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
2798 ngx_connection_error(c, ngx_socket_errno,
2799 ngx_shutdown_socket_n " failed");
2800 ngx_http_close_request(r, 0);
2801 return;
2804 if (rev->ready) {
2805 ngx_http_lingering_close_handler(rev);
2810 static void
2811 ngx_http_lingering_close_handler(ngx_event_t *rev)
2813 ssize_t n;
2814 ngx_msec_t timer;
2815 ngx_connection_t *c;
2816 ngx_http_request_t *r;
2817 ngx_http_core_loc_conf_t *clcf;
2818 u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
2820 c = rev->data;
2821 r = c->data;
2823 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2824 "http lingering close handler");
2826 if (rev->timedout) {
2827 ngx_http_close_request(r, 0);
2828 return;
2831 timer = (ngx_msec_t) (r->lingering_time - ngx_time());
2832 if (timer <= 0) {
2833 ngx_http_close_request(r, 0);
2834 return;
2837 do {
2838 n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
2840 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
2842 if (n == NGX_ERROR || n == 0) {
2843 ngx_http_close_request(r, 0);
2844 return;
2847 } while (rev->ready);
2849 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2850 ngx_http_close_request(r, 0);
2851 return;
2854 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2856 timer *= 1000;
2858 if (timer > clcf->lingering_timeout) {
2859 timer = clcf->lingering_timeout;
2862 ngx_add_timer(rev, timer);
2866 void
2867 ngx_http_empty_handler(ngx_event_t *wev)
2869 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
2871 return;
2875 void
2876 ngx_http_request_empty_handler(ngx_http_request_t *r)
2878 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2879 "http request empty handler");
2881 return;
2885 ngx_int_t
2886 ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
2888 ngx_buf_t *b;
2889 ngx_chain_t out;
2891 b = ngx_calloc_buf(r->pool);
2892 if (b == NULL) {
2893 return NGX_ERROR;
2896 if (flags & NGX_HTTP_LAST) {
2898 if (r == r->main && !r->post_action) {
2899 b->last_buf = 1;
2901 } else {
2902 b->sync = 1;
2903 b->last_in_chain = 1;
2907 if (flags & NGX_HTTP_FLUSH) {
2908 b->flush = 1;
2911 out.buf = b;
2912 out.next = NULL;
2914 return ngx_http_output_filter(r, &out);
2918 static ngx_int_t
2919 ngx_http_post_action(ngx_http_request_t *r)
2921 ngx_http_core_loc_conf_t *clcf;
2923 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2925 if (clcf->post_action.data == NULL) {
2926 return NGX_DECLINED;
2929 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2930 "post action: \"%V\"", &clcf->post_action);
2932 r->main->count--;
2934 r->http_version = NGX_HTTP_VERSION_9;
2935 r->header_only = 1;
2936 r->post_action = 1;
2938 r->read_event_handler = ngx_http_block_reading;
2940 if (clcf->post_action.data[0] == '/') {
2941 ngx_http_internal_redirect(r, &clcf->post_action, NULL);
2943 } else {
2944 ngx_http_named_location(r, &clcf->post_action);
2947 return NGX_OK;
2951 static void
2952 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t rc)
2954 ngx_connection_t *c;
2956 r = r->main;
2957 c = r->connection;
2959 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
2960 "http request count:%d blk:%d", r->count, r->blocked);
2962 if (r->count == 0) {
2963 ngx_log_error(NGX_LOG_ALERT, c->log, 0, "http request count is zero");
2966 r->count--;
2968 if (r->count || r->blocked) {
2969 return;
2972 ngx_http_free_request(r, rc);
2973 ngx_http_close_connection(c);
2977 static void
2978 ngx_http_free_request(ngx_http_request_t *r, ngx_int_t rc)
2980 ngx_log_t *log;
2981 struct linger linger;
2982 ngx_http_cleanup_t *cln;
2983 ngx_http_log_ctx_t *ctx;
2984 ngx_http_core_loc_conf_t *clcf;
2986 log = r->connection->log;
2988 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
2990 if (r->pool == NULL) {
2991 ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
2992 return;
2995 for (cln = r->cleanup; cln; cln = cln->next) {
2996 if (cln->handler) {
2997 cln->handler(cln->data);
3001 #if (NGX_STAT_STUB)
3003 if (r->stat_reading) {
3004 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
3007 if (r->stat_writing) {
3008 (void) ngx_atomic_fetch_add(ngx_stat_writing, -1);
3011 #endif
3013 if (rc > 0 && (r->headers_out.status == 0 || r->connection->sent == 0)) {
3014 r->headers_out.status = rc;
3017 log->action = "logging request";
3019 ngx_http_log_request(r);
3021 log->action = "closing request";
3023 if (r->connection->timedout) {
3024 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
3026 if (clcf->reset_timedout_connection) {
3027 linger.l_onoff = 1;
3028 linger.l_linger = 0;
3030 if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
3031 (const void *) &linger, sizeof(struct linger)) == -1)
3033 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
3034 "setsockopt(SO_LINGER) failed");
3039 /* the various request strings were allocated from r->pool */
3040 ctx = log->data;
3041 ctx->request = NULL;
3043 r->request_line.len = 0;
3045 r->connection->destroyed = 1;
3047 ngx_destroy_pool(r->pool);
3051 static void
3052 ngx_http_log_request(ngx_http_request_t *r)
3054 ngx_uint_t i, n;
3055 ngx_http_handler_pt *log_handler;
3056 ngx_http_core_main_conf_t *cmcf;
3058 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
3060 log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
3061 n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
3063 for (i = 0; i < n; i++) {
3064 log_handler[i](r);
3069 static void
3070 ngx_http_close_connection(ngx_connection_t *c)
3072 ngx_pool_t *pool;
3074 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
3075 "close http connection: %d", c->fd);
3077 #if (NGX_HTTP_SSL)
3079 if (c->ssl) {
3080 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
3081 c->ssl->handler = ngx_http_close_connection;
3082 return;
3086 #endif
3088 #if (NGX_STAT_STUB)
3089 (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
3090 #endif
3092 c->destroyed = 1;
3094 pool = c->pool;
3096 ngx_close_connection(c);
3098 ngx_destroy_pool(pool);
3102 static u_char *
3103 ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
3105 u_char *p;
3106 ngx_http_request_t *r;
3107 ngx_http_log_ctx_t *ctx;
3109 if (log->action) {
3110 p = ngx_snprintf(buf, len, " while %s", log->action);
3111 len -= p - buf;
3112 buf = p;
3115 ctx = log->data;
3117 p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
3118 len -= p - buf;
3120 r = ctx->request;
3122 if (r) {
3123 return r->log_handler(r, ctx->current_request, p, len);
3125 } else {
3126 p = ngx_snprintf(p, len, ", server: %V",
3127 &ctx->connection->listening->addr_text);
3130 return p;
3134 static u_char *
3135 ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
3136 u_char *buf, size_t len)
3138 char *uri_separator;
3139 u_char *p;
3140 ngx_http_upstream_t *u;
3141 ngx_http_core_srv_conf_t *cscf;
3143 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
3145 p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
3146 len -= p - buf;
3147 buf = p;
3149 if (r->request_line.data == NULL && r->request_start) {
3150 for (p = r->request_start; p < r->header_in->last; p++) {
3151 if (*p == CR || *p == LF) {
3152 break;
3156 r->request_line.len = p - r->request_start;
3157 r->request_line.data = r->request_start;
3160 if (r->request_line.len) {
3161 p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
3162 len -= p - buf;
3163 buf = p;
3166 if (r != sr) {
3167 p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
3168 len -= p - buf;
3169 buf = p;
3172 u = sr->upstream;
3174 if (u && u->peer.name) {
3176 uri_separator = "";
3178 #if (NGX_HAVE_UNIX_DOMAIN)
3179 if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
3180 uri_separator = ":";
3182 #endif
3184 p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
3185 &u->schema, u->peer.name,
3186 uri_separator, &u->uri);
3187 len -= p - buf;
3188 buf = p;
3191 if (r->headers_in.host) {
3192 p = ngx_snprintf(buf, len, ", host: \"%V\"",
3193 &r->headers_in.host->value);
3194 len -= p - buf;
3195 buf = p;
3198 if (r->headers_in.referer) {
3199 p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
3200 &r->headers_in.referer->value);
3201 buf = p;
3204 return buf;