upstream nginx-0.7.39
[nginx.git] / nginx / src / http / ngx_http_request.c
1
2 /*
3  * Copyright (C) Igor Sysoev
4  */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10
11
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);
18
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);
31
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);
37
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);
42
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);
52
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);
56
57 #if (NGX_HTTP_SSL)
58 static void ngx_http_ssl_handshake(ngx_event_t *rev);
59 static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
60 #endif
61
62
63 static char *ngx_http_client_errors[] = {
64
65     /* NGX_HTTP_PARSE_INVALID_METHOD */
66     "client sent invalid method",
67
68     /* NGX_HTTP_PARSE_INVALID_REQUEST */
69     "client sent invalid request",
70
71     /* NGX_HTTP_PARSE_INVALID_09_METHOD */
72     "client sent invalid method in HTTP/0.9 request"
73 };
74
75
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 },
79
80     { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
81                  ngx_http_process_connection },
82
83     { ngx_string("If-Modified-Since"),
84                  offsetof(ngx_http_headers_in_t, if_modified_since),
85                  ngx_http_process_unique_header_line },
86
87     { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
88                  ngx_http_process_user_agent },
89
90     { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
91                  ngx_http_process_header_line },
92
93     { ngx_string("Content-Length"),
94                  offsetof(ngx_http_headers_in_t, content_length),
95                  ngx_http_process_unique_header_line },
96
97     { ngx_string("Content-Type"),
98                  offsetof(ngx_http_headers_in_t, content_type),
99                  ngx_http_process_header_line },
100
101     { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
102                  ngx_http_process_header_line },
103
104     { ngx_string("If-Range"),
105                  offsetof(ngx_http_headers_in_t, if_range),
106                  ngx_http_process_unique_header_line },
107
108     { ngx_string("Transfer-Encoding"),
109                  offsetof(ngx_http_headers_in_t, transfer_encoding),
110                  ngx_http_process_header_line },
111
112     { ngx_string("Expect"),
113                  offsetof(ngx_http_headers_in_t, expect),
114                  ngx_http_process_unique_header_line },
115
116 #if (NGX_HTTP_GZIP)
117     { ngx_string("Accept-Encoding"),
118                  offsetof(ngx_http_headers_in_t, accept_encoding),
119                  ngx_http_process_header_line },
120
121     { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
122                  ngx_http_process_header_line },
123 #endif
124
125     { ngx_string("Authorization"),
126                  offsetof(ngx_http_headers_in_t, authorization),
127                  ngx_http_process_unique_header_line },
128
129     { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
130                  ngx_http_process_header_line },
131
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 },
136 #endif
137
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 },
142 #endif
143
144 #if (NGX_HTTP_HEADERS)
145     { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
146                  ngx_http_process_header_line },
147
148     { ngx_string("Accept-Language"),
149                  offsetof(ngx_http_headers_in_t, accept_language),
150                  ngx_http_process_header_line },
151 #endif
152
153 #if (NGX_HTTP_DAV)
154     { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
155                  ngx_http_process_header_line },
156
157     { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
158                  ngx_http_process_header_line },
159
160     { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite),
161                  ngx_http_process_header_line },
162
163     { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date),
164                  ngx_http_process_header_line },
165 #endif
166
167     { ngx_string("Cookie"), 0, ngx_http_process_cookie },
168
169     { ngx_null_string, 0, NULL }
170 };
171
172
173 void
174 ngx_http_init_connection(ngx_connection_t *c)
175 {
176     ngx_event_t         *rev;
177     ngx_http_log_ctx_t  *ctx;
178
179     ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
180     if (ctx == NULL) {
181         ngx_http_close_connection(c);
182         return;
183     }
184
185     ctx->connection = c;
186     ctx->request = NULL;
187     ctx->current_request = NULL;
188
189     c->log->connection = c->number;
190     c->log->handler = ngx_http_log_error;
191     c->log->data = ctx;
192     c->log->action = "reading client request line";
193
194     c->log_error = NGX_ERROR_INFO;
195
196     rev = c->read;
197     rev->handler = ngx_http_init_request;
198     c->write->handler = ngx_http_empty_handler;
199
200 #if (NGX_STAT_STUB)
201     ngx_atomic_fetch_add(ngx_stat_reading, 1);
202 #endif
203
204     if (rev->ready) {
205         /* the deferred accept(), rtsig, aio, iocp */
206
207         if (ngx_use_accept_mutex) {
208             ngx_post_event(rev, &ngx_posted_events);
209             return;
210         }
211
212         ngx_http_init_request(rev);
213         return;
214     }
215
216     ngx_add_timer(rev, c->listening->post_accept_timeout);
217
218     if (ngx_handle_read_event(rev, 0) != NGX_OK) {
219 #if (NGX_STAT_STUB)
220         ngx_atomic_fetch_add(ngx_stat_reading, -1);
221 #endif
222         ngx_http_close_connection(c);
223         return;
224     }
225 }
226
227
228 static void
229 ngx_http_init_request(ngx_event_t *rev)
230 {
231     ngx_time_t                 *tp;
232     ngx_uint_t                  i;
233     ngx_connection_t           *c;
234     ngx_http_request_t         *r;
235     struct sockaddr_in         *sin;
236     ngx_http_port_t            *port;
237     ngx_http_in_addr_t         *addr;
238     ngx_http_log_ctx_t         *ctx;
239     ngx_http_addr_conf_t       *addr_conf;
240     ngx_http_connection_t      *hc;
241     ngx_http_core_srv_conf_t   *cscf;
242     ngx_http_core_loc_conf_t   *clcf;
243     ngx_http_core_main_conf_t  *cmcf;
244 #if (NGX_HAVE_INET6)
245     struct sockaddr_in6        *sin6;
246     ngx_http_in6_addr_t        *addr6;
247 #endif
248
249 #if (NGX_STAT_STUB)
250     ngx_atomic_fetch_add(ngx_stat_reading, -1);
251 #endif
252
253     c = rev->data;
254
255     if (rev->timedout) {
256         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
257
258         ngx_http_close_connection(c);
259         return;
260     }
261
262     hc = c->data;
263
264     if (hc == NULL) {
265         hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
266         if (hc == NULL) {
267             ngx_http_close_connection(c);
268             return;
269         }
270     }
271
272     r = hc->request;
273
274     if (r) {
275         ngx_memzero(r, sizeof(ngx_http_request_t));
276
277         r->pipeline = hc->pipeline;
278
279         if (hc->nbusy) {
280             r->header_in = hc->busy[0];
281         }
282
283     } else {
284         r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
285         if (r == NULL) {
286             ngx_http_close_connection(c);
287             return;
288         }
289
290         hc->request = r;
291     }
292
293     c->data = r;
294     r->http_connection = hc;
295
296     c->sent = 0;
297     r->signature = NGX_HTTP_MODULE;
298
299     /* find the server configuration for the address:port */
300
301     port = c->listening->servers;
302
303     r->connection = c;
304
305     if (port->naddrs > 1) {
306
307         /*
308          * there are several addresses on this port and one of them
309          * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
310          * is required to determine a server address
311          */
312
313         c->local_sockaddr = NULL;
314
315         if (ngx_http_server_addr(r, NULL) != NGX_OK) {
316             ngx_http_close_connection(c);
317             return;
318         }
319
320         switch (c->local_sockaddr->sa_family) {
321
322 #if (NGX_HAVE_INET6)
323         case AF_INET6:
324             sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
325
326             addr6 = (ngx_http_in6_addr_t *) port->addrs;
327
328             /* the last address is "*" */
329
330             for (i = 0; i < port->naddrs - 1; i++) {
331                 if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
332                     break;
333                 }
334             }
335
336             addr_conf = &addr6[i].conf;
337
338             break;
339 #endif
340
341         default: /* AF_INET */
342             sin = (struct sockaddr_in *) c->local_sockaddr;
343
344             addr = port->addrs;
345
346             /* the last address is "*" */
347
348             for (i = 0; i < port->naddrs - 1; i++) {
349                 if (addr[i].addr == sin->sin_addr.s_addr) {
350                     break;
351                 }
352             }
353
354             addr_conf = &addr[i].conf;
355
356             break;
357         }
358
359     } else {
360
361         switch (c->local_sockaddr->sa_family) {
362
363 #if (NGX_HAVE_INET6)
364         case AF_INET6:
365             addr6 = (ngx_http_in6_addr_t *) port->addrs;
366             addr_conf = &addr6[0].conf;
367             break;
368 #endif
369
370         default: /* AF_INET */
371             addr = port->addrs;
372             addr_conf = &addr[0].conf;
373             break;
374         }
375     }
376
377     /* the default server configuration for the address:port */
378     cscf = addr_conf->core_srv_conf;
379
380     r->main_conf = cscf->ctx->main_conf;
381     r->srv_conf = cscf->ctx->srv_conf;
382     r->loc_conf = cscf->ctx->loc_conf;
383
384     rev->handler = ngx_http_process_request_line;
385
386 #if (NGX_HTTP_SSL)
387
388     {
389     ngx_http_ssl_srv_conf_t  *sscf;
390
391     sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
392     if (sscf->enable || addr_conf->ssl) {
393
394         if (c->ssl == NULL) {
395
396             c->log->action = "SSL handshaking";
397
398             if (addr_conf->ssl && sscf->ssl.ctx == NULL) {
399                 ngx_log_error(NGX_LOG_ERR, c->log, 0,
400                               "no \"ssl_certificate\" is defined "
401                               "in server listening on SSL port");
402                 ngx_http_close_connection(c);
403                 return;
404             }
405
406             if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
407                 != NGX_OK)
408             {
409                 ngx_http_close_connection(c);
410                 return;
411             }
412
413             rev->handler = ngx_http_ssl_handshake;
414         }
415
416         r->main_filter_need_in_memory = 1;
417     }
418     }
419
420 #endif
421
422     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
423     c->log->file = clcf->err_log->file;
424     if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
425         c->log->log_level = clcf->err_log->log_level;
426     }
427
428     if (c->buffer == NULL) {
429         c->buffer = ngx_create_temp_buf(c->pool,
430                                         cscf->client_header_buffer_size);
431         if (c->buffer == NULL) {
432             ngx_http_close_connection(c);
433             return;
434         }
435     }
436
437     if (r->header_in == NULL) {
438         r->header_in = c->buffer;
439     }
440
441     r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
442     if (r->pool == NULL) {
443         ngx_http_close_connection(c);
444         return;
445     }
446
447
448     if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
449                       sizeof(ngx_table_elt_t))
450         != NGX_OK)
451     {
452         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
453         return;
454     }
455
456     r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
457     if (r->ctx == NULL) {
458         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
459         return;
460     }
461
462     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
463
464     r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
465                                         * sizeof(ngx_http_variable_value_t));
466     if (r->variables == NULL) {
467         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
468         return;
469     }
470
471     c->single_connection = 1;
472     c->destroyed = 0;
473
474     r->main = r;
475
476     tp = ngx_timeofday();
477     r->start_sec = tp->sec;
478     r->start_msec = tp->msec;
479
480     r->method = NGX_HTTP_UNKNOWN;
481
482     r->headers_in.content_length_n = -1;
483     r->headers_in.keep_alive_n = -1;
484     r->headers_out.content_length_n = -1;
485     r->headers_out.last_modified_time = -1;
486
487     r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
488     r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
489
490     r->http_state = NGX_HTTP_READING_REQUEST_STATE;
491
492     ctx = c->log->data;
493     ctx->request = r;
494     ctx->current_request = r;
495     r->log_handler = ngx_http_log_error_handler;
496
497 #if (NGX_STAT_STUB)
498     ngx_atomic_fetch_add(ngx_stat_reading, 1);
499     r->stat_reading = 1;
500     ngx_atomic_fetch_add(ngx_stat_requests, 1);
501 #endif
502
503     rev->handler(rev);
504 }
505
506
507 #if (NGX_HTTP_SSL)
508
509 static void
510 ngx_http_ssl_handshake(ngx_event_t *rev)
511 {
512     u_char               buf[1];
513     ssize_t              n;
514     ngx_int_t            rc;
515     ngx_connection_t    *c;
516     ngx_http_request_t  *r;
517
518     c = rev->data;
519     r = c->data;
520
521     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
522                    "http check ssl handshake");
523
524     if (rev->timedout) {
525         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
526         c->timedout = 1;
527         ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
528         return;
529     }
530
531     n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
532
533     if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
534
535         if (!rev->timer_set) {
536             ngx_add_timer(rev, c->listening->post_accept_timeout);
537         }
538
539         if (ngx_handle_read_event(rev, 0) != NGX_OK) {
540             ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
541         }
542
543         return;
544     }
545
546     if (n == 1) {
547         if (buf[0] == 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
548             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
549                            "https ssl handshake: 0x%02Xd", buf[0]);
550
551             rc = ngx_ssl_handshake(c);
552
553             if (rc == NGX_AGAIN) {
554
555                 if (!rev->timer_set) {
556                     ngx_add_timer(rev, c->listening->post_accept_timeout);
557                 }
558
559                 c->ssl->handler = ngx_http_ssl_handshake_handler;
560                 return;
561             }
562
563             ngx_http_ssl_handshake_handler(c);
564
565             return;
566
567         } else {
568             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
569                            "plain http");
570
571             r->plain_http = 1;
572         }
573     }
574
575     c->log->action = "reading client request line";
576
577     rev->handler = ngx_http_process_request_line;
578     ngx_http_process_request_line(rev);
579 }
580
581
582 static void
583 ngx_http_ssl_handshake_handler(ngx_connection_t *c)
584 {
585     ngx_http_request_t  *r;
586
587     if (c->ssl->handshaked) {
588
589         /*
590          * The majority of browsers do not send the "close notify" alert.
591          * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
592          * and Links.  And what is more, MSIE ignores the server's alert.
593          *
594          * Opera and recent Mozilla send the alert.
595          */
596
597         c->ssl->no_wait_shutdown = 1;
598
599         c->read->handler = ngx_http_process_request_line;
600         /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
601
602         ngx_http_process_request_line(c->read);
603
604         return;
605     }
606
607     r = c->data;
608
609     ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
610
611     return;
612 }
613
614 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
615
616 int
617 ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
618 {
619     size_t                    len;
620     const char               *servername;
621     ngx_connection_t         *c;
622     ngx_http_request_t       *r;
623     ngx_http_ssl_srv_conf_t  *sscf;
624
625     servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
626
627     if (servername == NULL) {
628         return SSL_TLSEXT_ERR_NOACK;
629     }
630
631     c = ngx_ssl_get_connection(ssl_conn);
632
633     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
634                    "SSL server name: \"%s\"", servername);
635
636     len = ngx_strlen(servername);
637
638     if (len == 0) {
639         return SSL_TLSEXT_ERR_NOACK;
640     }
641
642     r = c->data;
643
644     if (ngx_http_find_virtual_server(r, (u_char *) servername, len) != NGX_OK) {
645         return SSL_TLSEXT_ERR_NOACK;
646     }
647
648     sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
649
650     SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
651
652     return SSL_TLSEXT_ERR_OK;
653 }
654
655 #endif
656
657 #endif
658
659
660 static void
661 ngx_http_process_request_line(ngx_event_t *rev)
662 {
663     ssize_t                    n;
664     ngx_int_t                  rc, rv;
665     ngx_connection_t          *c;
666     ngx_http_request_t        *r;
667     ngx_http_core_srv_conf_t  *cscf;
668
669     c = rev->data;
670     r = c->data;
671
672     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
673                    "http process request line");
674
675     if (rev->timedout) {
676         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
677         c->timedout = 1;
678         ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
679         return;
680     }
681
682     rc = NGX_AGAIN;
683
684     for ( ;; ) {
685
686         if (rc == NGX_AGAIN) {
687             n = ngx_http_read_request_header(r);
688
689             if (n == NGX_AGAIN || n == NGX_ERROR) {
690                 return;
691             }
692         }
693
694         rc = ngx_http_parse_request_line(r, r->header_in);
695
696         if (rc == NGX_OK) {
697
698             /* the request line has been parsed successfully */
699
700             r->request_line.len = r->request_end - r->request_start;
701             r->request_line.data = r->request_start;
702             *r->request_end = '\0';
703
704
705             if (r->args_start) {
706                 r->uri.len = r->args_start - 1 - r->uri_start;
707             } else {
708                 r->uri.len = r->uri_end - r->uri_start;
709             }
710
711
712             if (r->complex_uri || r->quoted_uri) {
713
714                 r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
715                 if (r->uri.data == NULL) {
716                     ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
717                     return;
718                 }
719
720                 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
721
722                 rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
723
724                 if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
725                     ngx_log_error(NGX_LOG_INFO, c->log, 0,
726                                   "client sent invalid request");
727                     ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
728                     return;
729                 }
730
731             } else {
732                 r->uri.data = r->uri_start;
733             }
734
735
736             r->unparsed_uri.len = r->uri_end - r->uri_start;
737             r->unparsed_uri.data = r->uri_start;
738
739
740             r->method_name.len = r->method_end - r->request_start + 1;
741             r->method_name.data = r->request_line.data;
742
743
744             if (r->http_protocol.data) {
745                 r->http_protocol.len = r->request_end - r->http_protocol.data;
746             }
747
748
749             if (r->uri_ext) {
750                 if (r->args_start) {
751                     r->exten.len = r->args_start - 1 - r->uri_ext;
752                 } else {
753                     r->exten.len = r->uri_end - r->uri_ext;
754                 }
755
756                 r->exten.data = r->uri_ext;
757             }
758
759
760             if (r->args_start && r->uri_end > r->args_start) {
761                 r->args.len = r->uri_end - r->args_start;
762                 r->args.data = r->args_start;
763             }
764
765
766             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
767                            "http request line: \"%V\"", &r->request_line);
768
769             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
770                            "http uri: \"%V\"", &r->uri);
771
772             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
773                            "http args: \"%V\"", &r->args);
774
775             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
776                            "http exten: \"%V\"", &r->exten);
777
778             if (r->host_start && r->host_end) {
779                 n = ngx_http_validate_host(r->host_start,
780                                            r->host_end - r->host_start);
781
782                 if (n <= 0) {
783                     ngx_log_error(NGX_LOG_INFO, c->log, 0,
784                                   "client sent invalid host in request line");
785                     ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
786                     return;
787                 }
788
789                 r->headers_in.server.len = n;
790                 r->headers_in.server.data = r->host_start;
791             }
792
793             if (r->http_version < NGX_HTTP_VERSION_10) {
794
795                 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
796                                                  r->headers_in.server.len)
797                     == NGX_ERROR)
798                 {
799                     ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
800                     return;
801                 }
802
803                 ngx_http_process_request(r);
804                 return;
805             }
806
807
808             if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
809                               sizeof(ngx_table_elt_t))
810                 != NGX_OK)
811             {
812                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
813                 return;
814             }
815
816
817             if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
818                                sizeof(ngx_table_elt_t *))
819                 != NGX_OK)
820             {
821                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
822                 return;
823             }
824
825             c->log->action = "reading client request headers";
826
827             rev->handler = ngx_http_process_request_headers;
828             ngx_http_process_request_headers(rev);
829
830             return;
831         }
832
833         if (rc != NGX_AGAIN) {
834
835             /* there was error while a request line parsing */
836
837             ngx_log_error(NGX_LOG_INFO, c->log, 0,
838                           ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
839             ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
840             return;
841         }
842
843         /* NGX_AGAIN: a request line parsing is still incomplete */
844
845         if (r->header_in->pos == r->header_in->end) {
846
847             rv = ngx_http_alloc_large_header_buffer(r, 1);
848
849             if (rv == NGX_ERROR) {
850                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
851                 return;
852             }
853
854             if (rv == NGX_DECLINED) {
855                 r->request_line.len = r->header_in->end - r->request_start;
856                 r->request_line.data = r->request_start;
857
858                 ngx_log_error(NGX_LOG_INFO, c->log, 0,
859                               "client sent too long URI");
860                 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
861                 return;
862             }
863         }
864     }
865 }
866
867
868 static void
869 ngx_http_process_request_headers(ngx_event_t *rev)
870 {
871     ssize_t                     n;
872     ngx_int_t                   rc, rv;
873     ngx_str_t                   header;
874     ngx_table_elt_t            *h;
875     ngx_connection_t           *c;
876     ngx_http_header_t          *hh;
877     ngx_http_request_t         *r;
878     ngx_http_core_srv_conf_t   *cscf;
879     ngx_http_core_main_conf_t  *cmcf;
880
881     c = rev->data;
882     r = c->data;
883
884     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
885                    "http process request header line");
886
887     if (rev->timedout) {
888         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
889         c->timedout = 1;
890         ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
891         return;
892     }
893
894     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
895     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
896
897     rc = NGX_AGAIN;
898
899     for ( ;; ) {
900
901         if (rc == NGX_AGAIN) {
902
903             if (r->header_in->pos == r->header_in->end) {
904
905                 rv = ngx_http_alloc_large_header_buffer(r, 0);
906
907                 if (rv == NGX_ERROR) {
908                     ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
909                     return;
910                 }
911
912                 if (rv == NGX_DECLINED) {
913                     header.len = r->header_in->end - r->header_name_start;
914                     header.data = r->header_name_start;
915
916                     if (header.len > NGX_MAX_ERROR_STR - 300) {
917                         header.len = NGX_MAX_ERROR_STR - 300;
918                         header.data[header.len++] = '.';
919                         header.data[header.len++] = '.';
920                         header.data[header.len++] = '.';
921                     }
922
923                     ngx_log_error(NGX_LOG_INFO, c->log, 0,
924                                   "client sent too long header line: \"%V\"",
925                                   &header);
926                     ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
927                     return;
928                 }
929             }
930
931             n = ngx_http_read_request_header(r);
932
933             if (n == NGX_AGAIN || n == NGX_ERROR) {
934                 return;
935             }
936         }
937
938         rc = ngx_http_parse_header_line(r, r->header_in,
939                                         cscf->underscores_in_headers);
940
941         if (rc == NGX_OK) {
942
943             if (r->invalid_header && cscf->ignore_invalid_headers) {
944
945                 /* there was error while a header line parsing */
946
947                 header.len = r->header_end - r->header_name_start;
948                 header.data = r->header_name_start;
949
950                 ngx_log_error(NGX_LOG_INFO, c->log, 0,
951                               "client sent invalid header line: \"%V\"",
952                               &header);
953                 continue;
954             }
955
956             /* a header line has been parsed successfully */
957
958             h = ngx_list_push(&r->headers_in.headers);
959             if (h == NULL) {
960                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
961                 return;
962             }
963
964             h->hash = r->header_hash;
965
966             h->key.len = r->header_name_end - r->header_name_start;
967             h->key.data = r->header_name_start;
968             h->key.data[h->key.len] = '\0';
969
970             h->value.len = r->header_end - r->header_start;
971             h->value.data = r->header_start;
972             h->value.data[h->value.len] = '\0';
973
974             h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
975             if (h->lowcase_key == NULL) {
976                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
977                 return;
978             }
979
980             if (h->key.len == r->lowcase_index) {
981                 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
982
983             } else {
984                 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
985             }
986
987             hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
988                                h->lowcase_key, h->key.len);
989
990             if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
991                 return;
992             }
993
994             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
995                            "http header: \"%V: %V\"",
996                            &h->key, &h->value);
997
998             continue;
999         }
1000
1001         if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1002
1003             /* a whole header has been parsed successfully */
1004
1005             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1006                            "http header done");
1007
1008             r->request_length += r->header_in->pos - r->header_in->start;
1009
1010             r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
1011
1012             rc = ngx_http_process_request_header(r);
1013
1014             if (rc != NGX_OK) {
1015                 return;
1016             }
1017
1018             ngx_http_process_request(r);
1019
1020             return;
1021         }
1022
1023         if (rc == NGX_AGAIN) {
1024
1025             /* a header line parsing is still not complete */
1026
1027             continue;
1028         }
1029
1030         /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
1031
1032         header.len = r->header_end - r->header_name_start;
1033         header.data = r->header_name_start;
1034         ngx_log_error(NGX_LOG_INFO, c->log, 0,
1035                       "client sent invalid header line: \"%V\\r...\"",
1036                       &header);
1037         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1038         return;
1039     }
1040 }
1041
1042
1043 static ssize_t
1044 ngx_http_read_request_header(ngx_http_request_t *r)
1045 {
1046     ssize_t                    n;
1047     ngx_event_t               *rev;
1048     ngx_connection_t          *c;
1049     ngx_http_core_srv_conf_t  *cscf;
1050
1051     c = r->connection;
1052     rev = c->read;
1053
1054     n = r->header_in->last - r->header_in->pos;
1055
1056     if (n > 0) {
1057         return n;
1058     }
1059
1060     if (rev->ready) {
1061         n = c->recv(c, r->header_in->last,
1062                     r->header_in->end - r->header_in->last);
1063     } else {
1064         n = NGX_AGAIN;
1065     }
1066
1067     if (n == NGX_AGAIN) {
1068         if (!rev->timer_set) {
1069             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1070             ngx_add_timer(rev, cscf->client_header_timeout);
1071         }
1072
1073         if (ngx_handle_read_event(rev, 0) != NGX_OK) {
1074             ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1075             return NGX_ERROR;
1076         }
1077
1078         return NGX_AGAIN;
1079     }
1080
1081     if (n == 0) {
1082         ngx_log_error(NGX_LOG_INFO, c->log, 0,
1083                       "client closed prematurely connection");
1084     }
1085
1086     if (n == 0 || n == NGX_ERROR) {
1087         c->error = 1;
1088         c->log->action = "reading client request headers";
1089
1090         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1091         return NGX_ERROR;
1092     }
1093
1094     r->header_in->last += n;
1095
1096     return n;
1097 }
1098
1099
1100 static ngx_int_t
1101 ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
1102     ngx_uint_t request_line)
1103 {
1104     u_char                    *old, *new;
1105     ngx_buf_t                 *b;
1106     ngx_http_connection_t     *hc;
1107     ngx_http_core_srv_conf_t  *cscf;
1108
1109     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1110                    "http alloc large header buffer");
1111
1112     if (request_line && r->state == 0) {
1113
1114         /* the client fills up the buffer with "\r\n" */
1115
1116         r->request_length += r->header_in->end - r->header_in->start;
1117
1118         r->header_in->pos = r->header_in->start;
1119         r->header_in->last = r->header_in->start;
1120
1121         return NGX_OK;
1122     }
1123
1124     old = request_line ? r->request_start : r->header_name_start;
1125
1126     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1127
1128     if (r->state != 0
1129         && (size_t) (r->header_in->pos - old)
1130                                      >= cscf->large_client_header_buffers.size)
1131     {
1132         return NGX_DECLINED;
1133     }
1134
1135     hc = r->http_connection;
1136
1137     if (hc->nfree) {
1138         b = hc->free[--hc->nfree];
1139
1140         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1141                        "http large header free: %p %uz",
1142                        b->pos, b->end - b->last);
1143
1144     } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
1145
1146         if (hc->busy == NULL) {
1147             hc->busy = ngx_palloc(r->connection->pool,
1148                   cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
1149             if (hc->busy == NULL) {
1150                 return NGX_ERROR;
1151             }
1152         }
1153
1154         b = ngx_create_temp_buf(r->connection->pool,
1155                                 cscf->large_client_header_buffers.size);
1156         if (b == NULL) {
1157             return NGX_ERROR;
1158         }
1159
1160         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1161                        "http large header alloc: %p %uz",
1162                        b->pos, b->end - b->last);
1163
1164     } else {
1165         return NGX_DECLINED;
1166     }
1167
1168     hc->busy[hc->nbusy++] = b;
1169
1170     if (r->state == 0) {
1171         /*
1172          * r->state == 0 means that a header line was parsed successfully
1173          * and we do not need to copy incomplete header line and
1174          * to relocate the parser header pointers
1175          */
1176
1177         r->request_length += r->header_in->end - r->header_in->start;
1178
1179         r->header_in = b;
1180
1181         return NGX_OK;
1182     }
1183
1184     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1185                    "http large header copy: %d", r->header_in->pos - old);
1186
1187     r->request_length += old - r->header_in->start;
1188
1189     new = b->start;
1190
1191     ngx_memcpy(new, old, r->header_in->pos - old);
1192
1193     b->pos = new + (r->header_in->pos - old);
1194     b->last = new + (r->header_in->pos - old);
1195
1196     if (request_line) {
1197         r->request_start = new;
1198
1199         if (r->request_end) {
1200             r->request_end = new + (r->request_end - old);
1201         }
1202
1203         r->method_end = new + (r->method_end - old);
1204
1205         r->uri_start = new + (r->uri_start - old);
1206         r->uri_end = new + (r->uri_end - old);
1207
1208         if (r->schema_start) {
1209             r->schema_start = new + (r->schema_start - old);
1210             r->schema_end = new + (r->schema_end - old);
1211         }
1212
1213         if (r->host_start) {
1214             r->host_start = new + (r->host_start - old);
1215             if (r->host_end) {
1216                 r->host_end = new + (r->host_end - old);
1217             }
1218         }
1219
1220         if (r->port_start) {
1221             r->port_start = new + (r->port_start - old);
1222             r->port_end = new + (r->port_end - old);
1223         }
1224
1225         if (r->uri_ext) {
1226             r->uri_ext = new + (r->uri_ext - old);
1227         }
1228
1229         if (r->args_start) {
1230             r->args_start = new + (r->args_start - old);
1231         }
1232
1233         if (r->http_protocol.data) {
1234             r->http_protocol.data = new + (r->http_protocol.data - old);
1235         }
1236
1237     } else {
1238         r->header_name_start = new;
1239         r->header_name_end = new + (r->header_name_end - old);
1240         r->header_start = new + (r->header_start - old);
1241         r->header_end = new + (r->header_end - old);
1242     }
1243
1244     r->header_in = b;
1245
1246     return NGX_OK;
1247 }
1248
1249
1250 static ngx_int_t
1251 ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1252     ngx_uint_t offset)
1253 {
1254     ngx_table_elt_t  **ph;
1255
1256     ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1257
1258     if (*ph == NULL) {
1259         *ph = h;
1260     }
1261
1262     return NGX_OK;
1263 }
1264
1265
1266 static ngx_int_t
1267 ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1268     ngx_uint_t offset)
1269 {
1270     ngx_table_elt_t  **ph;
1271
1272     ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1273
1274     if (*ph == NULL) {
1275         *ph = h;
1276         return NGX_OK;
1277     }
1278
1279     ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1280                   "client sent duplicate header line: \"%V: %V\", "
1281                   "previous value: \"%V: %V\"",
1282                   &h->key, &h->value, &(*ph)->key, &(*ph)->value);
1283
1284     ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1285
1286     return NGX_ERROR;
1287 }
1288
1289
1290 static ngx_int_t
1291 ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
1292     ngx_uint_t offset)
1293 {
1294     ssize_t  len;
1295
1296     if (r->headers_in.host == NULL) {
1297         r->headers_in.host = h;
1298     }
1299
1300     len = ngx_http_validate_host(h->value.data, h->value.len);
1301
1302     if (len <= 0) {
1303         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1304                       "client sent invalid host header");
1305         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1306         return NGX_ERROR;
1307     }
1308
1309     if (r->headers_in.server.len) {
1310         return NGX_OK;
1311     }
1312
1313     r->headers_in.server.len = len;
1314     r->headers_in.server.data = h->value.data;
1315
1316     return NGX_OK;
1317 }
1318
1319
1320 static ngx_int_t
1321 ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
1322     ngx_uint_t offset)
1323 {
1324     if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
1325         r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
1326
1327     } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
1328         r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
1329     }
1330
1331     return NGX_OK;
1332 }
1333
1334
1335 static ngx_int_t
1336 ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
1337     ngx_uint_t offset)
1338 {
1339     u_char  *user_agent, *msie;
1340
1341     if (r->headers_in.user_agent) {
1342         return NGX_OK;
1343     }
1344
1345     r->headers_in.user_agent = h;
1346
1347     /* check some widespread browsers while the header is in CPU cache */
1348
1349     user_agent = h->value.data;
1350
1351     msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
1352
1353     if (msie && msie + 7 < user_agent + h->value.len) {
1354
1355         r->headers_in.msie = 1;
1356
1357         if (msie[6] == '.') {
1358
1359             switch (msie[5]) {
1360             case '4':
1361                 r->headers_in.msie4 = 1;
1362                 /* fall through */
1363             case '5':
1364             case '6':
1365                 r->headers_in.msie6 = 1;
1366             }
1367         }
1368
1369 #if 0
1370         /* MSIE ignores the SSL "close notify" alert */
1371         if (c->ssl) {
1372             c->ssl->no_send_shutdown = 1;
1373         }
1374 #endif
1375     }
1376
1377     if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
1378         r->headers_in.opera = 1;
1379         r->headers_in.msie = 0;
1380         r->headers_in.msie4 = 0;
1381         r->headers_in.msie6 = 0;
1382     }
1383
1384     if (!r->headers_in.msie && !r->headers_in.opera) {
1385
1386         if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
1387             r->headers_in.gecko = 1;
1388
1389         } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
1390             r->headers_in.konqueror = 1;
1391         }
1392     }
1393
1394     return NGX_OK;
1395 }
1396
1397
1398 static ngx_int_t
1399 ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
1400     ngx_uint_t offset)
1401 {
1402     ngx_table_elt_t  **cookie;
1403
1404     cookie = ngx_array_push(&r->headers_in.cookies);
1405     if (cookie) {
1406         *cookie = h;
1407         return NGX_OK;
1408     }
1409
1410     ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1411
1412     return NGX_ERROR;
1413 }
1414
1415
1416 static ngx_int_t
1417 ngx_http_process_request_header(ngx_http_request_t *r)
1418 {
1419     if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
1420                                      r->headers_in.server.len)
1421         == NGX_ERROR)
1422     {
1423         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1424         return NGX_ERROR;
1425     }
1426
1427     if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
1428         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1429                    "client sent HTTP/1.1 request without \"Host\" header");
1430         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1431         return NGX_ERROR;
1432     }
1433
1434     if (r->headers_in.content_length) {
1435         r->headers_in.content_length_n =
1436                             ngx_atoof(r->headers_in.content_length->value.data,
1437                                       r->headers_in.content_length->value.len);
1438
1439         if (r->headers_in.content_length_n == NGX_ERROR) {
1440             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1441                           "client sent invalid \"Content-Length\" header");
1442             ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1443             return NGX_ERROR;
1444         }
1445     }
1446
1447     if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
1448         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1449                   "client sent %V method without \"Content-Length\" header",
1450                   &r->method_name);
1451         ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1452         return NGX_ERROR;
1453     }
1454
1455     if (r->method & NGX_HTTP_TRACE) {
1456         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1457                       "client sent TRACE method");
1458         ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
1459         return NGX_ERROR;
1460     }
1461
1462     if (r->headers_in.transfer_encoding
1463         && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
1464                            "chunked", 7 - 1))
1465     {
1466         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1467                       "client sent \"Transfer-Encoding: chunked\" header");
1468         ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1469         return NGX_ERROR;
1470     }
1471
1472     if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
1473         if (r->headers_in.keep_alive) {
1474             r->headers_in.keep_alive_n =
1475                             ngx_atotm(r->headers_in.keep_alive->value.data,
1476                                       r->headers_in.keep_alive->value.len);
1477         }
1478     }
1479
1480     return NGX_OK;
1481 }
1482
1483
1484 static void
1485 ngx_http_process_request(ngx_http_request_t *r)
1486 {
1487     ngx_connection_t  *c;
1488
1489     c = r->connection;
1490
1491     if (r->plain_http) {
1492         ngx_log_error(NGX_LOG_INFO, c->log, 0,
1493                       "client sent plain HTTP request to HTTPS port");
1494         ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
1495         return;
1496     }
1497
1498 #if (NGX_HTTP_SSL)
1499
1500     if (c->ssl) {
1501         long                      rc;
1502         X509                     *cert;
1503         ngx_http_ssl_srv_conf_t  *sscf;
1504
1505         sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
1506
1507         if (sscf->verify == 1) {
1508             rc = SSL_get_verify_result(c->ssl->connection);
1509
1510             if (rc != X509_V_OK) {
1511                 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1512                               "client SSL certificate verify error: (%l:%s)",
1513                               rc, X509_verify_cert_error_string(rc));
1514
1515                 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1516                                        (SSL_get0_session(c->ssl->connection)));
1517
1518                 ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
1519                 return;
1520             }
1521
1522             cert = SSL_get_peer_certificate(c->ssl->connection);
1523
1524             if (cert == NULL) {
1525                 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1526                               "client sent no required SSL certificate");
1527
1528                 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
1529                                        (SSL_get0_session(c->ssl->connection)));
1530
1531                 ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
1532                 return;
1533             }
1534
1535             X509_free(cert);
1536         }
1537     }
1538
1539 #endif
1540
1541     if (c->read->timer_set) {
1542         ngx_del_timer(c->read);
1543     }
1544
1545 #if (NGX_STAT_STUB)
1546     ngx_atomic_fetch_add(ngx_stat_reading, -1);
1547     r->stat_reading = 0;
1548     ngx_atomic_fetch_add(ngx_stat_writing, 1);
1549     r->stat_writing = 1;
1550 #endif
1551
1552     c->read->handler = ngx_http_request_handler;
1553     c->write->handler = ngx_http_request_handler;
1554     r->read_event_handler = ngx_http_block_reading;
1555
1556     ngx_http_handler(r);
1557
1558     ngx_http_run_posted_requests(c);
1559 }
1560
1561
1562 static ssize_t
1563 ngx_http_validate_host(u_char *host, size_t len)
1564 {
1565     u_char      ch;
1566     size_t      i, last;
1567     ngx_uint_t  dot;
1568
1569     last = len;
1570     dot = 0;
1571
1572     for (i = 0; i < len; i++) {
1573         ch = host[i];
1574
1575         if (ch == '.') {
1576             if (dot) {
1577                 return -1;
1578             }
1579
1580             dot = 1;
1581             continue;
1582         }
1583
1584         dot = 0;
1585
1586         if (ch == ':') {
1587             last = i;
1588             continue;
1589         }
1590
1591         if (ch == '/' || ch == '\0') {
1592             return -1;
1593         }
1594
1595 #if (NGX_WIN32)
1596         if (ch == '\\') {
1597             return -1;
1598         }
1599 #endif
1600     }
1601
1602     if (dot) {
1603         last--;
1604     }
1605
1606     return last;
1607 }
1608
1609
1610 static ngx_int_t
1611 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
1612 {
1613     u_char                    *server;
1614     ngx_uint_t                 hash;
1615     ngx_http_virtual_names_t  *vn;
1616     ngx_http_core_loc_conf_t  *clcf;
1617     ngx_http_core_srv_conf_t  *cscf;
1618     u_char                     buf[32];
1619
1620     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1621     vn = cscf->virtual_names;
1622
1623     if (vn == NULL) {
1624         return NGX_DECLINED;
1625     }
1626
1627     if (len <= 32) {
1628         server = buf;
1629
1630     } else {
1631         server = ngx_pnalloc(r->pool, len);
1632         if (server == NULL) {
1633             return NGX_ERROR;
1634         }
1635     }
1636
1637     hash = ngx_hash_strlow(server, host, len);
1638
1639     cscf = ngx_hash_find_combined(&vn->names, hash, server, len);
1640
1641     if (cscf) {
1642         goto found;
1643     }
1644
1645 #if (NGX_PCRE)
1646
1647     if (vn->nregex) {
1648         ngx_int_t                n;
1649         ngx_uint_t               i;
1650         ngx_str_t                name;
1651         ngx_http_server_name_t  *sn;
1652
1653         name.len = len;
1654         name.data = server;
1655
1656         sn = vn->regex;
1657
1658         for (i = 0; i < vn->nregex; i++) {
1659
1660             n = ngx_regex_exec(sn[i].regex, &name, NULL, 0);
1661
1662             if (n == NGX_REGEX_NO_MATCHED) {
1663                 continue;
1664             }
1665
1666             if (n < 0) {
1667                 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1668                               ngx_regex_exec_n
1669                               " failed: %d on \"%V\" using \"%V\"",
1670                               n, &name, &sn[i].name);
1671                 return NGX_ERROR;
1672             }
1673
1674             /* match */
1675
1676             cscf = sn[i].core_srv_conf;
1677
1678             goto found;
1679         }
1680     }
1681
1682 #endif
1683
1684     return NGX_OK;
1685
1686 found:
1687
1688     r->srv_conf = cscf->ctx->srv_conf;
1689     r->loc_conf = cscf->ctx->loc_conf;
1690
1691     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1692     r->connection->log->file = clcf->err_log->file;
1693
1694     if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
1695         r->connection->log->log_level = clcf->err_log->log_level;
1696     }
1697
1698     return NGX_OK;
1699 }
1700
1701
1702 static void
1703 ngx_http_request_handler(ngx_event_t *ev)
1704 {
1705     ngx_connection_t    *c;
1706     ngx_http_request_t  *r;
1707     ngx_http_log_ctx_t  *ctx;
1708
1709     c = ev->data;
1710     r = c->data;
1711
1712     ctx = c->log->data;
1713     ctx->current_request = r;
1714
1715     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1716                    "http run request: \"%V?%V\"", &r->uri, &r->args);
1717
1718     if (ev->write) {
1719         r->write_event_handler(r);
1720
1721     } else {
1722         r->read_event_handler(r);
1723     }
1724
1725     ngx_http_run_posted_requests(c);
1726 }
1727
1728
1729 void
1730 ngx_http_run_posted_requests(ngx_connection_t *c)
1731 {
1732     ngx_http_request_t         *r;
1733     ngx_http_log_ctx_t         *ctx;
1734     ngx_http_posted_request_t  *pr;
1735
1736     for ( ;; ) {
1737
1738         if (c->destroyed) {
1739             return;
1740         }
1741
1742         r = c->data;
1743         pr = r->main->posted_requests;
1744
1745         if (pr == NULL) {
1746             return;
1747         }
1748
1749         r->main->posted_requests = pr->next;
1750
1751         r = pr->request;
1752
1753         ctx = c->log->data;
1754         ctx->current_request = r;
1755
1756         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1757                        "http posted request: \"%V?%V\"", &r->uri, &r->args);
1758
1759         r->write_event_handler(r);
1760     }
1761 }
1762
1763
1764 ngx_int_t
1765 ngx_http_post_request(ngx_http_request_t *r)
1766 {
1767     ngx_http_posted_request_t  *pr, **p;
1768
1769     pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
1770     if (pr == NULL) {
1771         return NGX_ERROR;
1772     }
1773
1774     pr->request = r;
1775     pr->next = NULL;
1776
1777     for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
1778
1779     *p = pr;
1780
1781     return NGX_OK;
1782 }
1783
1784
1785 void
1786 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1787 {
1788     ngx_connection_t          *c;
1789     ngx_http_request_t        *pr;
1790     ngx_http_core_loc_conf_t  *clcf;
1791
1792     if (rc == NGX_DONE) {
1793         /* the request pool may be already destroyed */
1794         return;
1795     }
1796
1797     c = r->connection;
1798
1799     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
1800                    "http finalize request: %d, \"%V?%V\" %d",
1801                    rc, &r->uri, &r->args, r == c->data);
1802
1803     if (rc == NGX_DECLINED) {
1804         r->content_handler = NULL;
1805         r->write_event_handler = ngx_http_core_run_phases;
1806         ngx_http_core_run_phases(r);
1807         return;
1808     }
1809
1810     if (r != r->main && r->post_subrequest) {
1811         rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
1812     }
1813
1814     if (rc == NGX_ERROR
1815         || rc == NGX_HTTP_REQUEST_TIME_OUT
1816         || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
1817         || c->error)
1818     {
1819         if (rc > 0 && r->headers_out.status == 0) {
1820             r->headers_out.status = rc;
1821         }
1822
1823         if (ngx_http_post_action(r) == NGX_OK) {
1824             return;
1825         }
1826
1827         ngx_http_close_request(r, 0);
1828         return;
1829     }
1830
1831     if (rc >= NGX_HTTP_SPECIAL_RESPONSE
1832         || rc == NGX_HTTP_CREATED
1833         || rc == NGX_HTTP_NO_CONTENT)
1834     {
1835         if (rc == NGX_HTTP_CLOSE) {
1836             ngx_http_close_request(r, rc);
1837             return;
1838         }
1839
1840         if (r == r->main) {
1841             if (c->read->timer_set) {
1842                 ngx_del_timer(c->read);
1843             }
1844
1845             if (c->write->timer_set) {
1846                 ngx_del_timer(c->write);
1847             }
1848         }
1849
1850         c->read->handler = ngx_http_request_handler;
1851         c->write->handler = ngx_http_request_handler;
1852
1853         ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
1854         return;
1855     }
1856
1857     if (r != r->main) {
1858
1859         if (r->buffered || r->postponed) {
1860
1861             if (ngx_http_set_write_handler(r) != NGX_OK) {
1862                 ngx_http_close_request(r->main, 0);
1863             }
1864
1865             return;
1866         }
1867
1868 #if (NGX_DEBUG)
1869         if (r != c->data) {
1870             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1871                            "http finalize non-active request: \"%V?%V\"",
1872                            &r->uri, &r->args);
1873         }
1874 #endif
1875
1876         pr = r->parent;
1877
1878         if (r == c->data) {
1879
1880             if (!r->logged) {
1881
1882                 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1883
1884                 if (clcf->log_subrequest) {
1885                     ngx_http_log_request(r);
1886                 }
1887
1888                 r->logged = 1;
1889
1890             } else {
1891                 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1892                               "subrequest: \"%V?%V\" logged again",
1893                               &r->uri, &r->args);
1894             }
1895
1896             r->done = 1;
1897
1898             if (pr->postponed && pr->postponed->request == r) {
1899                 pr->postponed = pr->postponed->next;
1900             }
1901
1902             c->data = pr;
1903
1904         } else {
1905
1906             r->write_event_handler = ngx_http_request_finalizer;
1907
1908             if (r->waited) {
1909                 r->done = 1;
1910             }
1911         }
1912
1913         if (ngx_http_post_request(pr) != NGX_OK) {
1914             ngx_http_close_request(r->main, 0);
1915             return;
1916         }
1917
1918         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1919                        "http wake parent request: \"%V?%V\"",
1920                        &pr->uri, &pr->args);
1921
1922         return;
1923     }
1924
1925     if (r->buffered || c->buffered || r->postponed) {
1926
1927         if (ngx_http_set_write_handler(r) != NGX_OK) {
1928             ngx_http_close_request(r, 0);
1929         }
1930
1931         return;
1932     }
1933
1934     if (r != c->data) {
1935         ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1936                       "http finalize non-active request: \"%V?%V\"",
1937                       &r->uri, &r->args);
1938         return;
1939     }
1940
1941     r->done = 1;
1942
1943     if (!r->post_action) {
1944         r->request_complete = 1;
1945     }
1946
1947     if (ngx_http_post_action(r) == NGX_OK) {
1948         return;
1949     }
1950
1951     if (c->read->timer_set) {
1952         ngx_del_timer(c->read);
1953     }
1954
1955     if (c->write->timer_set) {
1956         c->write->delayed = 0;
1957         ngx_del_timer(c->write);
1958     }
1959
1960     if (c->destroyed) {
1961         return;
1962     }
1963
1964     if (c->read->eof) {
1965         ngx_http_close_request(r, 0);
1966         return;
1967     }
1968
1969     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1970
1971     if (!ngx_terminate
1972          && !ngx_exiting
1973          && r->keepalive
1974          && clcf->keepalive_timeout > 0)
1975     {
1976         ngx_http_set_keepalive(r);
1977         return;
1978
1979     } else if (r->lingering_close && clcf->lingering_timeout > 0) {
1980         ngx_http_set_lingering_close(r);
1981         return;
1982     }
1983
1984     ngx_http_close_request(r, 0);
1985 }
1986
1987
1988 static ngx_int_t
1989 ngx_http_set_write_handler(ngx_http_request_t *r)
1990 {
1991     ngx_event_t               *wev;
1992     ngx_http_core_loc_conf_t  *clcf;
1993
1994     r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
1995
1996     r->read_event_handler = ngx_http_test_reading;
1997     r->write_event_handler = ngx_http_writer;
1998
1999     wev = r->connection->write;
2000
2001     if (wev->ready && wev->delayed) {
2002         return NGX_OK;
2003     }
2004
2005     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2006     if (!wev->delayed) {
2007         ngx_add_timer(wev, clcf->send_timeout);
2008     }
2009
2010     if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2011         ngx_http_close_request(r, 0);
2012         return NGX_ERROR;
2013     }
2014
2015     return NGX_OK;
2016 }
2017
2018
2019 static void
2020 ngx_http_writer(ngx_http_request_t *r)
2021 {
2022     int                        rc;
2023     ngx_event_t               *wev;
2024     ngx_connection_t          *c;
2025     ngx_http_core_loc_conf_t  *clcf;
2026
2027     c = r->connection;
2028     wev = c->write;
2029
2030     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2031                    "http writer handler: \"%V?%V\"", &r->uri, &r->args);
2032
2033     clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
2034
2035     if (wev->timedout) {
2036         if (!wev->delayed) {
2037             ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
2038                           "client timed out");
2039             c->timedout = 1;
2040
2041             ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
2042             return;
2043         }
2044
2045         wev->timedout = 0;
2046         wev->delayed = 0;
2047
2048         if (!wev->ready) {
2049             ngx_add_timer(wev, clcf->send_timeout);
2050
2051             if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2052                 ngx_http_close_request(r, 0);
2053             }
2054
2055             return;
2056         }
2057
2058     } else {
2059         if (wev->delayed) {
2060             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2061                            "http writer delayed");
2062
2063             if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2064                 ngx_http_close_request(r, 0);
2065             }
2066
2067             return;
2068         }
2069     }
2070
2071     rc = ngx_http_output_filter(r, NULL);
2072
2073     if (c->destroyed) {
2074         return;
2075     }
2076
2077     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
2078                    "http writer output filter: %d, \"%V?%V\"",
2079                    rc, &r->uri, &r->args);
2080
2081     if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
2082
2083         if (!wev->ready && !wev->delayed) {
2084             ngx_add_timer(wev, clcf->send_timeout);
2085         }
2086
2087         if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2088             ngx_http_close_request(r, 0);
2089         }
2090
2091         return;
2092     }
2093
2094     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2095                    "http writer done: \"%V?%V\"", &r->uri, &r->args);
2096
2097     ngx_http_finalize_request(r, rc);
2098 }
2099
2100
2101 static void
2102 ngx_http_request_finalizer(ngx_http_request_t *r)
2103 {
2104     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2105                    "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
2106
2107     ngx_http_finalize_request(r, 0);
2108 }
2109
2110
2111 void
2112 ngx_http_block_reading(ngx_http_request_t *r)
2113 {
2114     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2115                    "http reading blocked");
2116
2117     /* aio does not call this handler */
2118
2119     if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
2120         && r->connection->read->active)
2121     {
2122         if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
2123             ngx_http_close_request(r, 0);
2124         }
2125     }
2126 }
2127
2128
2129 void
2130 ngx_http_test_reading(ngx_http_request_t *r)
2131 {
2132     int                n;
2133     char               buf[1];
2134     ngx_err_t          err;
2135     ngx_event_t       *rev;
2136     ngx_connection_t  *c;
2137
2138     c = r->connection;
2139     rev = c->read;
2140
2141     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
2142
2143 #if (NGX_HAVE_KQUEUE)
2144
2145     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2146
2147         if (!rev->pending_eof) {
2148             return;
2149         }
2150
2151         rev->eof = 1;
2152         c->error = 1;
2153         err = rev->kq_errno;
2154
2155         goto closed;
2156     }
2157
2158 #endif
2159
2160     n = recv(c->fd, buf, 1, MSG_PEEK);
2161
2162     if (n == 0) {
2163         rev->eof = 1;
2164         c->error = 1;
2165         err = 0;
2166
2167         goto closed;
2168
2169     } else if (n == -1) {
2170         err = ngx_socket_errno;
2171
2172         if (err != NGX_EAGAIN) {
2173             rev->eof = 1;
2174             c->error = 1;
2175
2176             goto closed;
2177         }
2178     }
2179
2180     /* aio does not call this handler */
2181
2182     if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
2183
2184         if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
2185             ngx_http_close_request(r, 0);
2186         }
2187     }
2188
2189     return;
2190
2191 closed:
2192
2193     if (err) {
2194         rev->error = 1;
2195     }
2196
2197     ngx_log_error(NGX_LOG_INFO, c->log, err,
2198                   "client closed prematurely connection");
2199
2200     ngx_http_finalize_request(r, 0);
2201 }
2202
2203
2204 static void
2205 ngx_http_set_keepalive(ngx_http_request_t *r)
2206 {
2207     int                        tcp_nodelay;
2208     ngx_int_t                  i;
2209     ngx_buf_t                 *b, *f;
2210     ngx_event_t               *rev, *wev;
2211     ngx_connection_t          *c;
2212     ngx_http_connection_t     *hc;
2213     ngx_http_core_srv_conf_t  *cscf;
2214     ngx_http_core_loc_conf_t  *clcf;
2215
2216     c = r->connection;
2217     rev = c->read;
2218
2219     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2220
2221     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
2222
2223     if (r->discard_body) {
2224         r->write_event_handler = ngx_http_request_empty_handler;
2225         r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2226         ngx_add_timer(rev, clcf->lingering_timeout);
2227         return;
2228     }
2229
2230     c->log->action = "closing request";
2231
2232     hc = r->http_connection;
2233     b = r->header_in;
2234
2235     if (b->pos < b->last) {
2236
2237         /* the pipelined request */
2238
2239         if (b != c->buffer) {
2240
2241             /*
2242              * If the large header buffers were allocated while the previous
2243              * request processing then we do not use c->buffer for
2244              * the pipelined request (see ngx_http_init_request()).
2245              *
2246              * Now we would move the large header buffers to the free list.
2247              */
2248
2249             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2250
2251             if (hc->free == NULL) {
2252                 hc->free = ngx_palloc(c->pool,
2253                   cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
2254
2255                 if (hc->free == NULL) {
2256                     ngx_http_close_request(r, 0);
2257                     return;
2258                 }
2259             }
2260
2261             for (i = 0; i < hc->nbusy - 1; i++) {
2262                 f = hc->busy[i];
2263                 hc->free[hc->nfree++] = f;
2264                 f->pos = f->start;
2265                 f->last = f->start;
2266             }
2267
2268             hc->busy[0] = b;
2269             hc->nbusy = 1;
2270         }
2271     }
2272
2273     ngx_http_request_done(r, 0);
2274
2275     c->data = hc;
2276
2277     ngx_add_timer(rev, clcf->keepalive_timeout);
2278
2279     if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2280         ngx_http_close_connection(c);
2281         return;
2282     }
2283
2284     wev = c->write;
2285     wev->handler = ngx_http_empty_handler;
2286
2287     if (b->pos < b->last) {
2288
2289         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
2290
2291 #if (NGX_STAT_STUB)
2292         ngx_atomic_fetch_add(ngx_stat_reading, 1);
2293 #endif
2294
2295         hc->pipeline = 1;
2296         c->log->action = "reading client pipelined request line";
2297
2298         rev->handler = ngx_http_init_request;
2299         ngx_post_event(rev, &ngx_posted_events);
2300         return;
2301     }
2302
2303     hc->pipeline = 0;
2304
2305     /*
2306      * To keep a memory footprint as small as possible for an idle
2307      * keepalive connection we try to free the ngx_http_request_t and
2308      * c->buffer's memory if they were allocated outside the c->pool.
2309      * The large header buffers are always allocated outside the c->pool and
2310      * are freed too.
2311      */
2312
2313     if (ngx_pfree(c->pool, r) == NGX_OK) {
2314         hc->request = NULL;
2315     }
2316
2317     b = c->buffer;
2318
2319     if (ngx_pfree(c->pool, b->start) == NGX_OK) {
2320
2321         /*
2322          * the special note for ngx_http_keepalive_handler() that
2323          * c->buffer's memory was freed
2324          */
2325
2326         b->pos = NULL;
2327
2328     } else {
2329         b->pos = b->start;
2330         b->last = b->start;
2331     }
2332
2333     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
2334                    hc->free, hc->nfree);
2335
2336     if (hc->free) {
2337         for (i = 0; i < hc->nfree; i++) {
2338             ngx_pfree(c->pool, hc->free[i]->start);
2339             hc->free[i] = NULL;
2340         }
2341
2342         hc->nfree = 0;
2343     }
2344
2345     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
2346                    hc->busy, hc->nbusy);
2347
2348     if (hc->busy) {
2349         for (i = 0; i < hc->nbusy; i++) {
2350             ngx_pfree(c->pool, hc->busy[i]->start);
2351             hc->busy[i] = NULL;
2352         }
2353
2354         hc->nbusy = 0;
2355     }
2356
2357 #if (NGX_HTTP_SSL)
2358     if (c->ssl) {
2359         ngx_ssl_free_buffer(c);
2360     }
2361 #endif
2362
2363     rev->handler = ngx_http_keepalive_handler;
2364
2365     if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2366         if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2367             ngx_http_close_connection(c);
2368             return;
2369         }
2370     }
2371
2372     c->log->action = "keepalive";
2373
2374     if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
2375         if (ngx_tcp_push(c->fd) == -1) {
2376             ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
2377             ngx_http_close_connection(c);
2378             return;
2379         }
2380
2381         c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
2382         tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
2383
2384     } else {
2385         tcp_nodelay = 1;
2386     }
2387
2388     if (tcp_nodelay
2389         && clcf->tcp_nodelay
2390         && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
2391     {
2392         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
2393
2394         if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
2395                        (const void *) &tcp_nodelay, sizeof(int))
2396             == -1)
2397         {
2398             ngx_connection_error(c, ngx_socket_errno,
2399                                  "setsockopt(TCP_NODELAY) failed");
2400             ngx_http_close_connection(c);
2401             return;
2402         }
2403
2404         c->tcp_nodelay = NGX_TCP_NODELAY_SET;
2405     }
2406
2407 #if 0
2408     /* if ngx_http_request_t was freed then we need some other place */
2409     r->http_state = NGX_HTTP_KEEPALIVE_STATE;
2410 #endif
2411
2412     c->idle = 1;
2413
2414     if (rev->ready) {
2415         ngx_post_event(rev, &ngx_posted_events);
2416     }
2417 }
2418
2419
2420 static void
2421 ngx_http_keepalive_handler(ngx_event_t *rev)
2422 {
2423     size_t             size;
2424     ssize_t            n;
2425     ngx_buf_t         *b;
2426     ngx_connection_t  *c;
2427
2428     c = rev->data;
2429
2430     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
2431
2432     if (rev->timedout || c->close) {
2433         ngx_http_close_connection(c);
2434         return;
2435     }
2436
2437 #if (NGX_HAVE_KQUEUE)
2438
2439     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
2440         if (rev->pending_eof) {
2441             c->log->handler = NULL;
2442             ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
2443                           "kevent() reported that client %V closed "
2444                           "keepalive connection", &c->addr_text);
2445 #if (NGX_HTTP_SSL)
2446             if (c->ssl) {
2447                 c->ssl->no_send_shutdown = 1;
2448             }
2449 #endif
2450             ngx_http_close_connection(c);
2451             return;
2452         }
2453     }
2454
2455 #endif
2456
2457     b = c->buffer;
2458     size = b->end - b->start;
2459
2460     if (b->pos == NULL) {
2461
2462         /*
2463          * The c->buffer's memory was freed by ngx_http_set_keepalive().
2464          * However, the c->buffer->start and c->buffer->end were not changed
2465          * to keep the buffer size.
2466          */
2467
2468         b->pos = ngx_palloc(c->pool, size);
2469         if (b->pos == NULL) {
2470             ngx_http_close_connection(c);
2471             return;
2472         }
2473
2474         b->start = b->pos;
2475         b->last = b->pos;
2476         b->end = b->pos + size;
2477     }
2478
2479     /*
2480      * MSIE closes a keepalive connection with RST flag
2481      * so we ignore ECONNRESET here.
2482      */
2483
2484     c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
2485     ngx_set_socket_errno(0);
2486
2487     n = c->recv(c, b->last, size);
2488     c->log_error = NGX_ERROR_INFO;
2489
2490     if (n == NGX_AGAIN) {
2491         if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2492             ngx_http_close_connection(c);
2493         }
2494
2495         return;
2496     }
2497
2498     if (n == NGX_ERROR) {
2499         ngx_http_close_connection(c);
2500         return;
2501     }
2502
2503     c->log->handler = NULL;
2504
2505     if (n == 0) {
2506         ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
2507                       "client %V closed keepalive connection", &c->addr_text);
2508         ngx_http_close_connection(c);
2509         return;
2510     }
2511
2512     b->last += n;
2513
2514 #if (NGX_STAT_STUB)
2515     ngx_atomic_fetch_add(ngx_stat_reading, 1);
2516 #endif
2517
2518     c->log->handler = ngx_http_log_error;
2519     c->log->action = "reading client request line";
2520
2521     c->idle = 0;
2522
2523     ngx_http_init_request(rev);
2524 }
2525
2526
2527 static void
2528 ngx_http_set_lingering_close(ngx_http_request_t *r)
2529 {
2530     ngx_event_t               *rev, *wev;
2531     ngx_connection_t          *c;
2532     ngx_http_core_loc_conf_t  *clcf;
2533
2534     c = r->connection;
2535
2536     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2537
2538     rev = c->read;
2539     rev->handler = ngx_http_lingering_close_handler;
2540
2541     r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2542     ngx_add_timer(rev, clcf->lingering_timeout);
2543
2544     if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2545         ngx_http_close_request(r, 0);
2546         return;
2547     }
2548
2549     wev = c->write;
2550     wev->handler = ngx_http_empty_handler;
2551
2552     if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2553         if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2554             ngx_http_close_request(r, 0);
2555             return;
2556         }
2557     }
2558
2559     if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
2560         ngx_connection_error(c, ngx_socket_errno,
2561                              ngx_shutdown_socket_n " failed");
2562         ngx_http_close_request(r, 0);
2563         return;
2564     }
2565
2566     if (rev->ready) {
2567         ngx_http_lingering_close_handler(rev);
2568     }
2569 }
2570
2571
2572 static void
2573 ngx_http_lingering_close_handler(ngx_event_t *rev)
2574 {
2575     ssize_t                    n;
2576     ngx_msec_t                 timer;
2577     ngx_connection_t          *c;
2578     ngx_http_request_t        *r;
2579     ngx_http_core_loc_conf_t  *clcf;
2580     u_char                     buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
2581
2582     c = rev->data;
2583     r = c->data;
2584
2585     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2586                    "http lingering close handler");
2587
2588     if (rev->timedout) {
2589         c->timedout = 1;
2590         ngx_http_close_request(r, 0);
2591         return;
2592     }
2593
2594     timer = (ngx_msec_t) (r->lingering_time - ngx_time());
2595     if (timer <= 0) {
2596         ngx_http_close_request(r, 0);
2597         return;
2598     }
2599
2600     do {
2601         n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
2602
2603         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
2604
2605         if (n == NGX_ERROR || n == 0) {
2606             ngx_http_close_request(r, 0);
2607             return;
2608         }
2609
2610     } while (rev->ready);
2611
2612     if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2613         ngx_http_close_request(r, 0);
2614         return;
2615     }
2616
2617     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2618
2619     timer *= 1000;
2620
2621     if (timer > clcf->lingering_timeout) {
2622         timer = clcf->lingering_timeout;
2623     }
2624
2625     ngx_add_timer(rev, timer);
2626 }
2627
2628
2629 void
2630 ngx_http_empty_handler(ngx_event_t *wev)
2631 {
2632     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
2633
2634     return;
2635 }
2636
2637
2638 void
2639 ngx_http_request_empty_handler(ngx_http_request_t *r)
2640 {
2641     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2642                    "http request empty handler");
2643
2644     return;
2645 }
2646
2647
2648 ngx_int_t
2649 ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
2650 {
2651     ngx_buf_t    *b;
2652     ngx_chain_t   out;
2653
2654     b = ngx_calloc_buf(r->pool);
2655     if (b == NULL) {
2656         return NGX_ERROR;
2657     }
2658
2659     if (flags & NGX_HTTP_LAST) {
2660         b->last_buf = 1;
2661     }
2662
2663     if (flags & NGX_HTTP_FLUSH) {
2664         b->flush = 1;
2665     }
2666
2667     out.buf = b;
2668     out.next = NULL;
2669
2670     return ngx_http_output_filter(r, &out);
2671 }
2672
2673
2674 static ngx_int_t
2675 ngx_http_post_action(ngx_http_request_t *r)
2676 {
2677     ngx_http_core_loc_conf_t  *clcf;
2678
2679     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2680
2681     if (clcf->post_action.data == NULL) {
2682         return NGX_DECLINED;
2683     }
2684
2685     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2686                    "post action: \"%V\"", &clcf->post_action);
2687
2688     r->http_version = NGX_HTTP_VERSION_9;
2689     r->header_only = 1;
2690     r->post_action = 1;
2691
2692     r->read_event_handler = ngx_http_block_reading;
2693
2694     if (clcf->post_action.data[0] == '/') {
2695         ngx_http_internal_redirect(r, &clcf->post_action, NULL);
2696
2697     } else {
2698         ngx_http_named_location(r, &clcf->post_action);
2699     }
2700
2701     return NGX_OK;
2702 }
2703
2704
2705 static void
2706 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
2707 {
2708     ngx_connection_t  *c;
2709
2710     c = r->connection;
2711
2712     ngx_http_request_done(r->main, error);
2713     ngx_http_close_connection(c);
2714 }
2715
2716
2717 static void
2718 ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
2719 {
2720     ngx_log_t                 *log;
2721     struct linger              linger;
2722     ngx_http_cleanup_t        *cln;
2723     ngx_http_log_ctx_t        *ctx;
2724     ngx_http_core_loc_conf_t  *clcf;
2725
2726     log = r->connection->log;
2727
2728     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
2729
2730     if (r->pool == NULL) {
2731         ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
2732         return;
2733     }
2734
2735     for (cln = r->cleanup; cln; cln = cln->next) {
2736         if (cln->handler) {
2737             cln->handler(cln->data);
2738         }
2739     }
2740
2741 #if (NGX_STAT_STUB)
2742
2743     if (r->stat_reading) {
2744         ngx_atomic_fetch_add(ngx_stat_reading, -1);
2745     }
2746
2747     if (r->stat_writing) {
2748         ngx_atomic_fetch_add(ngx_stat_writing, -1);
2749     }
2750
2751 #endif
2752
2753     if (error && r->headers_out.status == 0) {
2754         r->headers_out.status = error;
2755     }
2756
2757     log->action = "logging request";
2758
2759     ngx_http_log_request(r);
2760
2761     log->action = "closing request";
2762
2763     if (r->connection->timedout) {
2764         clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2765
2766         if (clcf->reset_timedout_connection) {
2767             linger.l_onoff = 1;
2768             linger.l_linger = 0;
2769
2770             if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
2771                            (const void *) &linger, sizeof(struct linger)) == -1)
2772             {
2773                 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
2774                               "setsockopt(SO_LINGER) failed");
2775             }
2776         }
2777     }
2778
2779     /* the various request strings were allocated from r->pool */
2780     ctx = log->data;
2781     ctx->request = NULL;
2782
2783     r->request_line.len = 0;
2784
2785     r->connection->destroyed = 1;
2786
2787     ngx_destroy_pool(r->pool);
2788 }
2789
2790
2791 static void
2792 ngx_http_log_request(ngx_http_request_t *r)
2793 {
2794     ngx_uint_t                  i, n;
2795     ngx_http_handler_pt        *log_handler;
2796     ngx_http_core_main_conf_t  *cmcf;
2797
2798     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
2799
2800     log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
2801     n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
2802
2803     for (i = 0; i < n; i++) {
2804         log_handler[i](r);
2805     }
2806 }
2807
2808
2809 static void
2810 ngx_http_close_connection(ngx_connection_t *c)
2811 {
2812     ngx_pool_t  *pool;
2813
2814     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
2815                    "close http connection: %d", c->fd);
2816
2817 #if (NGX_HTTP_SSL)
2818
2819     if (c->ssl) {
2820         if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
2821             c->ssl->handler = ngx_http_close_connection;
2822             return;
2823         }
2824     }
2825
2826 #endif
2827
2828 #if (NGX_STAT_STUB)
2829     ngx_atomic_fetch_add(ngx_stat_active, -1);
2830 #endif
2831
2832     c->destroyed = 1;
2833
2834     pool = c->pool;
2835
2836     ngx_close_connection(c);
2837
2838     ngx_destroy_pool(pool);
2839 }
2840
2841
2842 static u_char *
2843 ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
2844 {
2845     u_char              *p;
2846     ngx_http_request_t  *r;
2847     ngx_http_log_ctx_t  *ctx;
2848
2849     if (log->action) {
2850         p = ngx_snprintf(buf, len, " while %s", log->action);
2851         len -= p - buf;
2852         buf = p;
2853     }
2854
2855     ctx = log->data;
2856
2857     p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
2858     len -= p - buf;
2859
2860     r = ctx->request;
2861
2862     if (r) {
2863         return r->log_handler(r, ctx->current_request, p, len);
2864
2865     } else {
2866         p = ngx_snprintf(p, len, ", server: %V",
2867                          &ctx->connection->listening->addr_text);
2868     }
2869
2870     return p;
2871 }
2872
2873
2874 static u_char *
2875 ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
2876     u_char *buf, size_t len)
2877 {
2878     char                      *uri_separator;
2879     u_char                    *p;
2880     ngx_http_upstream_t       *u;
2881     ngx_http_core_srv_conf_t  *cscf;
2882
2883     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2884
2885     p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
2886     len -= p - buf;
2887     buf = p;
2888
2889     if (r->request_line.data == NULL && r->request_start) {
2890         for (p = r->request_start; p < r->header_in->last; p++) {
2891             if (*p == CR || *p == LF) {
2892                 break;
2893             }
2894         }
2895
2896         r->request_line.len = p - r->request_start;
2897         r->request_line.data = r->request_start;
2898     }
2899
2900     if (r->request_line.len) {
2901         p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
2902         len -= p - buf;
2903         buf = p;
2904     }
2905
2906     if (r != sr) {
2907         p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
2908         len -= p - buf;
2909         buf = p;
2910     }
2911
2912     u = sr->upstream;
2913
2914     if (u && u->peer.name) {
2915
2916         uri_separator = "";
2917
2918 #if (NGX_HAVE_UNIX_DOMAIN)
2919         if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
2920             uri_separator = ":";
2921         }
2922 #endif
2923
2924         p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
2925                          &u->schema, u->peer.name,
2926                          uri_separator, &u->uri);
2927         len -= p - buf;
2928         buf = p;
2929     }
2930
2931     if (r->headers_in.host) {
2932         p = ngx_snprintf(buf, len, ", host: \"%V\"",
2933                          &r->headers_in.host->value);
2934         len -= p - buf;
2935         buf = p;
2936     }
2937
2938     if (r->headers_in.referer) {
2939         p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
2940                          &r->headers_in.referer->value);
2941         buf = p;
2942     }
2943
2944     return buf;
2945 }