blob: 4d21a002a82648c515a15a277007053a7fdbdef9 [file] [log] [blame]
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00007#include <ngx_config.h>
Igor Sysoev0ad17c02002-08-26 15:18:19 +00008#include <ngx_core.h>
Igor Sysoevfcce8d52003-01-23 18:47:54 +00009#include <ngx_event.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000010#include <ngx_http.h>
11
Igor Sysoeva58e3ca2002-09-02 14:48:24 +000012
Igor Sysoev6b863e32003-05-12 15:52:24 +000013static void ngx_http_init_request(ngx_event_t *ev);
14static void ngx_http_process_request_line(ngx_event_t *rev);
15static void ngx_http_process_request_headers(ngx_event_t *rev);
Igor Sysoev6b863e32003-05-12 15:52:24 +000016static ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
Igor Sysoevf7abd722004-09-23 06:32:00 +000017static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
Igor Sysoevc1571722005-03-19 12:38:37 +000018 ngx_uint_t request_line);
Igor Sysoev899b44e2005-05-12 14:58:06 +000019
20static ngx_int_t ngx_http_process_header_line(ngx_http_request_t *r,
21 ngx_table_elt_t *h, ngx_uint_t offset);
Igor Sysoevb145b062005-06-15 18:33:41 +000022static ngx_int_t ngx_http_process_unique_header_line(ngx_http_request_t *r,
23 ngx_table_elt_t *h, ngx_uint_t offset);
Igor Sysoevec67b192007-09-09 18:28:49 +000024static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r,
25 ngx_table_elt_t *h, ngx_uint_t offset);
Igor Sysoev899b44e2005-05-12 14:58:06 +000026static ngx_int_t ngx_http_process_cookie(ngx_http_request_t *r,
27 ngx_table_elt_t *h, ngx_uint_t offset);
28
Igor Sysoev2f657222004-06-16 15:32:11 +000029static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
Igor Sysoev62864d12007-06-04 20:40:03 +000030static void ngx_http_process_request(ngx_http_request_t *r);
Igor Sysoeve60303c2007-05-29 15:21:09 +000031static void ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host,
32 size_t len, ngx_uint_t hash);
Igor Sysoev6b863e32003-05-12 15:52:24 +000033
Igor Sysoev899b44e2005-05-12 14:58:06 +000034static void ngx_http_request_handler(ngx_event_t *ev);
Igor Sysoev85ef94b2005-06-23 13:41:06 +000035static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
Igor Sysoev899b44e2005-05-12 14:58:06 +000036static void ngx_http_writer(ngx_http_request_t *r);
Igor Sysoev6b863e32003-05-12 15:52:24 +000037
Igor Sysoev7ab4d9c2007-08-06 15:22:40 +000038static void ngx_http_test_reading(ngx_http_request_t *r);
Igor Sysoevd581fd52003-05-13 16:02:32 +000039static void ngx_http_set_keepalive(ngx_http_request_t *r);
40static void ngx_http_keepalive_handler(ngx_event_t *ev);
41static void ngx_http_set_lingering_close(ngx_http_request_t *r);
42static void ngx_http_lingering_close_handler(ngx_event_t *ev);
Igor Sysoevc2807ec2006-02-16 15:26:46 +000043static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
Igor Sysoev90c08142005-07-25 09:41:38 +000044static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
Igor Sysoev9ac946b2005-10-24 15:09:41 +000045static void ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error);
Igor Sysoev90c08142005-07-25 09:41:38 +000046static void ngx_http_close_connection(ngx_connection_t *c);
Igor Sysoevd581fd52003-05-13 16:02:32 +000047
Igor Sysoeve5a222c2005-01-25 12:27:35 +000048static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
Igor Sysoevbb28b6d2006-07-11 13:20:19 +000049static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
50 ngx_http_request_t *sr, u_char *buf, size_t len);
Igor Sysoev0ad17c02002-08-26 15:18:19 +000051
Igor Sysoevceb99292005-09-06 16:09:32 +000052#if (NGX_HTTP_SSL)
53static void ngx_http_ssl_handshake(ngx_event_t *rev);
Igor Sysoev9fa5a822005-09-30 14:41:25 +000054static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
Igor Sysoevceb99292005-09-06 16:09:32 +000055#endif
56
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000057
Igor Sysoevc04deca2005-03-28 14:43:02 +000058static char *ngx_http_client_errors[] = {
Igor Sysoev0ad17c02002-08-26 15:18:19 +000059
Igor Sysoevc04deca2005-03-28 14:43:02 +000060 /* NGX_HTTP_PARSE_INVALID_METHOD */
61 "client sent invalid method",
Igor Sysoev6a644c62003-03-04 06:33:48 +000062
Igor Sysoevc04deca2005-03-28 14:43:02 +000063 /* NGX_HTTP_PARSE_INVALID_REQUEST */
64 "client sent invalid request",
65
66 /* NGX_HTTP_PARSE_INVALID_09_METHOD */
67 "client sent invalid method in HTTP/0.9 request"
Igor Sysoev016b8522002-08-29 16:59:54 +000068};
69
70
Igor Sysoevaab4d8c2004-09-06 18:45:00 +000071ngx_http_header_t ngx_http_headers_in[] = {
Igor Sysoev899b44e2005-05-12 14:58:06 +000072 { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host),
Igor Sysoevb145b062005-06-15 18:33:41 +000073 ngx_http_process_unique_header_line },
Igor Sysoevaab4d8c2004-09-06 18:45:00 +000074
Igor Sysoev899b44e2005-05-12 14:58:06 +000075 { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
Igor Sysoevec67b192007-09-09 18:28:49 +000076 ngx_http_process_connection },
Igor Sysoev899b44e2005-05-12 14:58:06 +000077
78 { ngx_string("If-Modified-Since"),
79 offsetof(ngx_http_headers_in_t, if_modified_since),
Igor Sysoev4f62b732007-08-29 15:32:49 +000080 ngx_http_process_unique_header_line },
Igor Sysoev899b44e2005-05-12 14:58:06 +000081
82 { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
83 ngx_http_process_header_line },
84
85 { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
86 ngx_http_process_header_line },
87
88 { ngx_string("Content-Length"),
89 offsetof(ngx_http_headers_in_t, content_length),
Igor Sysoevb145b062005-06-15 18:33:41 +000090 ngx_http_process_unique_header_line },
Igor Sysoev899b44e2005-05-12 14:58:06 +000091
92 { ngx_string("Content-Type"),
93 offsetof(ngx_http_headers_in_t, content_type),
94 ngx_http_process_header_line },
95
96 { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
97 ngx_http_process_header_line },
98
Igor Sysoev6c2f0522007-11-09 15:41:35 +000099 { ngx_string("If-Range"),
100 offsetof(ngx_http_headers_in_t, if_range),
101 ngx_http_process_unique_header_line },
102
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000103 { ngx_string("Transfer-Encoding"),
104 offsetof(ngx_http_headers_in_t, transfer_encoding),
105 ngx_http_process_header_line },
106
Igor Sysoevaab4d8c2004-09-06 18:45:00 +0000107#if (NGX_HTTP_GZIP)
108 { ngx_string("Accept-Encoding"),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000109 offsetof(ngx_http_headers_in_t, accept_encoding),
110 ngx_http_process_header_line },
111
112 { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
113 ngx_http_process_header_line },
Igor Sysoevaab4d8c2004-09-06 18:45:00 +0000114#endif
115
116 { ngx_string("Authorization"),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000117 offsetof(ngx_http_headers_in_t, authorization),
Igor Sysoevb145b062005-06-15 18:33:41 +0000118 ngx_http_process_unique_header_line },
Igor Sysoevaab4d8c2004-09-06 18:45:00 +0000119
Igor Sysoev899b44e2005-05-12 14:58:06 +0000120 { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
121 ngx_http_process_header_line },
Igor Sysoevaab4d8c2004-09-06 18:45:00 +0000122
Igor Sysoev09c684b2005-11-09 17:25:55 +0000123#if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
Igor Sysoevaab4d8c2004-09-06 18:45:00 +0000124 { ngx_string("X-Forwarded-For"),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000125 offsetof(ngx_http_headers_in_t, x_forwarded_for),
126 ngx_http_process_header_line },
Igor Sysoevaab4d8c2004-09-06 18:45:00 +0000127#endif
Igor Sysoev02025fd2005-01-18 13:03:58 +0000128
Igor Sysoev09c684b2005-11-09 17:25:55 +0000129#if (NGX_HTTP_REALIP)
130 { ngx_string("X-Real-IP"),
131 offsetof(ngx_http_headers_in_t, x_real_ip),
132 ngx_http_process_header_line },
133#endif
134
Igor Sysoev1ebfead2005-02-16 13:40:36 +0000135#if (NGX_HTTP_HEADERS)
Igor Sysoev899b44e2005-05-12 14:58:06 +0000136 { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
137 ngx_http_process_header_line },
138
Igor Sysoev1ebfead2005-02-16 13:40:36 +0000139 { ngx_string("Accept-Language"),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000140 offsetof(ngx_http_headers_in_t, accept_language),
141 ngx_http_process_header_line },
Igor Sysoev1ebfead2005-02-16 13:40:36 +0000142#endif
143
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000144#if (NGX_HTTP_DAV)
145 { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
146 ngx_http_process_header_line },
147
148 { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
149 ngx_http_process_header_line },
Igor Sysoevda173ab2006-08-30 10:39:17 +0000150
Igor Sysoev56331ba2007-01-18 21:11:23 +0000151 { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite),
152 ngx_http_process_header_line },
153
Igor Sysoevda173ab2006-08-30 10:39:17 +0000154 { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date),
155 ngx_http_process_header_line },
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000156#endif
157
Igor Sysoev899b44e2005-05-12 14:58:06 +0000158 { ngx_string("Cookie"), 0, ngx_http_process_cookie },
159
160 { ngx_null_string, 0, NULL }
Igor Sysoevaab4d8c2004-09-06 18:45:00 +0000161};
162
163
Igor Sysoevc1571722005-03-19 12:38:37 +0000164void
165ngx_http_init_connection(ngx_connection_t *c)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000166{
Igor Sysoev6253ca12003-05-27 12:18:54 +0000167 ngx_event_t *rev;
Igor Sysoev160d7742003-11-19 16:26:41 +0000168 ngx_http_log_ctx_t *ctx;
Igor Sysoev6b863e32003-05-12 15:52:24 +0000169
Igor Sysoevc1571722005-03-19 12:38:37 +0000170 ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
171 if (ctx == NULL) {
Igor Sysoev6b863e32003-05-12 15:52:24 +0000172 ngx_http_close_connection(c);
173 return;
174 }
175
Igor Sysoevcc595672007-12-30 08:01:50 +0000176 ctx->connection = c;
Igor Sysoev1b735832004-11-11 14:07:14 +0000177 ctx->request = NULL;
Igor Sysoevbb28b6d2006-07-11 13:20:19 +0000178 ctx->current_request = NULL;
Igor Sysoeve5a222c2005-01-25 12:27:35 +0000179
180 c->log->connection = c->number;
Igor Sysoev6b863e32003-05-12 15:52:24 +0000181 c->log->handler = ngx_http_log_error;
Igor Sysoeve5a222c2005-01-25 12:27:35 +0000182 c->log->data = ctx;
183 c->log->action = "reading client request line";
184
Igor Sysoevc7a2f682004-02-10 16:23:38 +0000185 c->log_error = NGX_ERROR_INFO;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000186
Igor Sysoevdc479b42003-03-20 16:09:44 +0000187 rev = c->read;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000188 rev->handler = ngx_http_init_request;
Igor Sysoev9ac946b2005-10-24 15:09:41 +0000189 c->write->handler = ngx_http_empty_handler;
Igor Sysoev7af6b162004-02-09 07:46:43 +0000190
Igor Sysoev055951d2005-10-21 19:12:18 +0000191#if (NGX_STAT_STUB)
192 ngx_atomic_fetch_add(ngx_stat_reading, 1);
193#endif
194
Igor Sysoevdc479b42003-03-20 16:09:44 +0000195 if (rev->ready) {
Igor Sysoev0ab91b92004-06-06 19:49:18 +0000196 /* the deferred accept(), rtsig, aio, iocp */
Igor Sysoev709405b2004-03-31 15:26:46 +0000197
Igor Sysoevffe71442006-02-08 15:33:12 +0000198 if (ngx_use_accept_mutex) {
Igor Sysoevc2068d02005-10-19 12:33:58 +0000199 ngx_post_event(rev, &ngx_posted_events);
Igor Sysoev709405b2004-03-31 15:26:46 +0000200 return;
201 }
202
Igor Sysoev6b863e32003-05-12 15:52:24 +0000203 ngx_http_init_request(rev);
204 return;
Igor Sysoev016b8522002-08-29 16:59:54 +0000205 }
Igor Sysoev73009772003-02-06 17:21:13 +0000206
Igor Sysoev239baac2003-06-11 15:28:34 +0000207 ngx_add_timer(rev, c->listening->post_accept_timeout);
Igor Sysoev9b25d692003-01-26 21:08:14 +0000208
Igor Sysoevb5faed22003-10-29 08:30:44 +0000209 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
Igor Sysoev055951d2005-10-21 19:12:18 +0000210#if (NGX_STAT_STUB)
211 ngx_atomic_fetch_add(ngx_stat_reading, -1);
212#endif
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000213 ngx_http_close_connection(c);
214 return;
215 }
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000216}
217
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000218
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000219static void
220ngx_http_init_request(ngx_event_t *rev)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000221{
Igor Sysoevb4ccb9f2007-04-21 07:50:19 +0000222 ngx_time_t *tp;
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000223 ngx_uint_t i;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000224 ngx_connection_t *c;
225 ngx_http_request_t *r;
Igor Sysoev305a9d82005-12-26 17:07:48 +0000226 ngx_http_in_port_t *hip;
227 ngx_http_in_addr_t *hia;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000228 ngx_http_log_ctx_t *ctx;
229 ngx_http_connection_t *hc;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000230 ngx_http_core_srv_conf_t *cscf;
231 ngx_http_core_loc_conf_t *clcf;
232 ngx_http_core_main_conf_t *cmcf;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000233
Igor Sysoev055951d2005-10-21 19:12:18 +0000234#if (NGX_STAT_STUB)
235 ngx_atomic_fetch_add(ngx_stat_reading, -1);
236#endif
237
Igor Sysoeva9830112003-05-19 16:39:14 +0000238 c = rev->data;
Igor Sysoeva9830112003-05-19 16:39:14 +0000239
Igor Sysoev10fc9ef2003-10-27 08:53:49 +0000240 if (rev->timedout) {
241 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000242
Igor Sysoev10fc9ef2003-10-27 08:53:49 +0000243 ngx_http_close_connection(c);
244 return;
245 }
246
Igor Sysoevf7abd722004-09-23 06:32:00 +0000247 hc = c->data;
Igor Sysoevdd888c42004-09-21 05:38:28 +0000248
Igor Sysoev055951d2005-10-21 19:12:18 +0000249 if (hc == NULL) {
Igor Sysoevc1571722005-03-19 12:38:37 +0000250 hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
251 if (hc == NULL) {
Igor Sysoevdd888c42004-09-21 05:38:28 +0000252 ngx_http_close_connection(c);
253 return;
254 }
Igor Sysoevf7abd722004-09-23 06:32:00 +0000255 }
Igor Sysoevdd888c42004-09-21 05:38:28 +0000256
Igor Sysoevf7abd722004-09-23 06:32:00 +0000257 r = hc->request;
258
259 if (r) {
260 ngx_memzero(r, sizeof(ngx_http_request_t));
261
262 r->pipeline = hc->pipeline;
263
264 if (hc->nbusy) {
265 r->header_in = hc->busy[0];
266 }
267
Igor Sysoevf7abd722004-09-23 06:32:00 +0000268 } else {
Igor Sysoevc1571722005-03-19 12:38:37 +0000269 r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
270 if (r == NULL) {
Igor Sysoev0a280a32003-10-12 16:49:16 +0000271 ngx_http_close_connection(c);
272 return;
273 }
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000274
Igor Sysoevdd888c42004-09-21 05:38:28 +0000275 hc->request = r;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000276 }
277
Igor Sysoevdd888c42004-09-21 05:38:28 +0000278 c->data = r;
279 r->http_connection = hc;
280
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000281 c->sent = 0;
Igor Sysoevc83f6832004-06-24 07:53:37 +0000282 r->signature = NGX_HTTP_MODULE;
Igor Sysoev3b30a902003-12-25 20:26:58 +0000283
Igor Sysoev6253ca12003-05-27 12:18:54 +0000284 /* find the server configuration for the address:port */
285
286 /* AF_INET only */
287
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000288 hip = c->listening->servers;
Igor Sysoev305a9d82005-12-26 17:07:48 +0000289 hia = hip->addrs;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000290
Igor Sysoev305a9d82005-12-26 17:07:48 +0000291 r->port = hip->port;
292 r->port_text = &hip->port_text;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000293
294 i = 0;
295
Igor Sysoevc7a9b7a2007-12-29 16:00:34 +0000296 r->connection = c;
297
Igor Sysoev305a9d82005-12-26 17:07:48 +0000298 if (hip->naddrs > 1) {
Igor Sysoev6253ca12003-05-27 12:18:54 +0000299
Igor Sysoev239baac2003-06-11 15:28:34 +0000300 /*
Igor Sysoevd00b3652004-08-23 19:09:29 +0000301 * There are several addresses on this port and one of them
302 * is the "*:port" wildcard so getsockname() is needed to determine
Igor Sysoev239baac2003-06-11 15:28:34 +0000303 * the server address.
Igor Sysoevd00b3652004-08-23 19:09:29 +0000304 *
Igor Sysoevc7a9b7a2007-12-29 16:00:34 +0000305 * AcceptEx() already has given this address.
Igor Sysoev239baac2003-06-11 15:28:34 +0000306 */
Igor Sysoev6253ca12003-05-27 12:18:54 +0000307
Igor Sysoev1b735832004-11-11 14:07:14 +0000308#if (NGX_WIN32)
Igor Sysoev239baac2003-06-11 15:28:34 +0000309 if (c->local_sockaddr) {
310 r->in_addr =
311 ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000312
Igor Sysoev02025fd2005-01-18 13:03:58 +0000313 } else
Igor Sysoev239baac2003-06-11 15:28:34 +0000314#endif
Igor Sysoev02025fd2005-01-18 13:03:58 +0000315 {
Igor Sysoevc7a9b7a2007-12-29 16:00:34 +0000316 if (ngx_http_server_addr(r, NULL) != NGX_OK) {
Igor Sysoev239baac2003-06-11 15:28:34 +0000317 ngx_http_close_connection(c);
318 return;
319 }
Igor Sysoev6253ca12003-05-27 12:18:54 +0000320 }
321
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000322 /* the last address is "*" */
Igor Sysoev6253ca12003-05-27 12:18:54 +0000323
Igor Sysoev305a9d82005-12-26 17:07:48 +0000324 for ( /* void */ ; i < hip->naddrs - 1; i++) {
325 if (hia[i].addr == r->in_addr) {
Igor Sysoev6253ca12003-05-27 12:18:54 +0000326 break;
327 }
328 }
329
330 } else {
Igor Sysoev305a9d82005-12-26 17:07:48 +0000331 r->in_addr = hia[0].addr;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000332 }
333
Igor Sysoev305a9d82005-12-26 17:07:48 +0000334 r->virtual_names = hia[i].virtual_names;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000335
336 /* the default server configuration for the address:port */
Igor Sysoev305a9d82005-12-26 17:07:48 +0000337 cscf = hia[i].core_srv_conf;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000338
339 r->main_conf = cscf->ctx->main_conf;
340 r->srv_conf = cscf->ctx->srv_conf;
341 r->loc_conf = cscf->ctx->loc_conf;
342
Igor Sysoev899b44e2005-05-12 14:58:06 +0000343 rev->handler = ngx_http_process_request_line;
Igor Sysoevf38e0462004-07-16 17:11:43 +0000344
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000345#if (NGX_HTTP_SSL)
346
Igor Sysoev86ef6aa2007-12-10 12:09:51 +0000347 {
348 ngx_http_ssl_srv_conf_t *sscf;
349
Igor Sysoevf38e0462004-07-16 17:11:43 +0000350 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000351 if (sscf->enable) {
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000352
353 if (c->ssl == NULL) {
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000354 if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
Igor Sysoev02f742b2005-04-08 15:18:55 +0000355 == NGX_ERROR)
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000356 {
357 ngx_http_close_connection(c);
358 return;
359 }
360
Igor Sysoev899b44e2005-05-12 14:58:06 +0000361 rev->handler = ngx_http_ssl_handshake;
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000362 }
363
Igor Sysoevd52477f2005-05-16 13:53:20 +0000364 r->main_filter_need_in_memory = 1;
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000365 }
Igor Sysoev86ef6aa2007-12-10 12:09:51 +0000366 }
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000367
Igor Sysoev4aa88882004-07-14 20:07:58 +0000368#endif
369
Igor Sysoev890fc962003-07-20 21:15:59 +0000370 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
371 c->log->file = clcf->err_log->file;
Igor Sysoevfff32322004-04-08 15:58:25 +0000372 if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
373 c->log->log_level = clcf->err_log->log_level;
374 }
Igor Sysoev890fc962003-07-20 21:15:59 +0000375
Igor Sysoev682bf8e2002-09-16 15:01:44 +0000376 if (c->buffer == NULL) {
Igor Sysoev369145c2004-05-28 15:49:23 +0000377 c->buffer = ngx_create_temp_buf(c->pool,
378 cscf->client_header_buffer_size);
Igor Sysoev6b863e32003-05-12 15:52:24 +0000379 if (c->buffer == NULL) {
380 ngx_http_close_connection(c);
381 return;
382 }
Igor Sysoev682bf8e2002-09-16 15:01:44 +0000383 }
384
Igor Sysoevf7abd722004-09-23 06:32:00 +0000385 if (r->header_in == NULL) {
386 r->header_in = c->buffer;
387 }
388
Igor Sysoevc1571722005-03-19 12:38:37 +0000389 r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
390 if (r->pool == NULL) {
Igor Sysoev6b863e32003-05-12 15:52:24 +0000391 ngx_http_close_connection(c);
392 return;
393 }
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000394
Igor Sysoev89690bf2004-03-23 06:01:52 +0000395
Igor Sysoevaab4d8c2004-09-06 18:45:00 +0000396 if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000397 sizeof(ngx_table_elt_t))
398 == NGX_ERROR)
Igor Sysoev980a9242004-09-05 19:54:02 +0000399 {
400 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoev980a9242004-09-05 19:54:02 +0000401 return;
402 }
403
Igor Sysoev6b863e32003-05-12 15:52:24 +0000404 r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
405 if (r->ctx == NULL) {
406 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoev6b863e32003-05-12 15:52:24 +0000407 return;
408 }
Igor Sysoeve0268b92002-09-11 15:18:33 +0000409
Igor Sysoev09c684b2005-11-09 17:25:55 +0000410 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
411
412 r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
413 * sizeof(ngx_http_variable_value_t));
414 if (r->variables == NULL) {
415 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
416 return;
417 }
418
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000419 c->single_connection = 1;
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000420 c->destroyed = 0;
421
Igor Sysoevf6e1fe32005-10-04 10:38:53 +0000422 r->main = r;
423
Igor Sysoevb4ccb9f2007-04-21 07:50:19 +0000424 tp = ngx_timeofday();
425 r->start_sec = tp->sec;
426 r->start_msec = tp->msec;
Igor Sysoev11d75322005-03-01 15:20:36 +0000427
Igor Sysoev94e32ce2006-04-07 14:08:04 +0000428 r->method = NGX_HTTP_UNKNOWN;
429
Igor Sysoeva09f08d2003-04-25 14:43:13 +0000430 r->headers_in.content_length_n = -1;
Igor Sysoev419f9ac2003-10-21 16:49:56 +0000431 r->headers_in.keep_alive_n = -1;
432 r->headers_out.content_length_n = -1;
Igor Sysoev42feecb2002-12-15 06:25:09 +0000433 r->headers_out.last_modified_time = -1;
434
Igor Sysoevef316432006-08-16 13:09:33 +0000435 r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
436 r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
437
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000438 r->http_state = NGX_HTTP_READING_REQUEST_STATE;
439
Igor Sysoevc04deca2005-03-28 14:43:02 +0000440 ctx = c->log->data;
441 ctx->request = r;
Igor Sysoevbb28b6d2006-07-11 13:20:19 +0000442 ctx->current_request = r;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000443 r->log_handler = ngx_http_log_error_handler;
Igor Sysoevc04deca2005-03-28 14:43:02 +0000444
Igor Sysoevdc3b2a72004-09-14 19:39:54 +0000445#if (NGX_STAT_STUB)
Igor Sysoev78452232005-10-12 13:50:36 +0000446 ngx_atomic_fetch_add(ngx_stat_reading, 1);
Igor Sysoevb1dfe472004-12-21 12:30:30 +0000447 r->stat_reading = 1;
Igor Sysoev78452232005-10-12 13:50:36 +0000448 ngx_atomic_fetch_add(ngx_stat_requests, 1);
Igor Sysoevdc3b2a72004-09-14 19:39:54 +0000449#endif
450
Igor Sysoev899b44e2005-05-12 14:58:06 +0000451 rev->handler(rev);
Igor Sysoevf38e0462004-07-16 17:11:43 +0000452}
453
454
455#if (NGX_HTTP_SSL)
456
Igor Sysoevc1571722005-03-19 12:38:37 +0000457static void
458ngx_http_ssl_handshake(ngx_event_t *rev)
Igor Sysoevf38e0462004-07-16 17:11:43 +0000459{
Igor Sysoevf38e0462004-07-16 17:11:43 +0000460 u_char buf[1];
Igor Sysoev1b735832004-11-11 14:07:14 +0000461 ssize_t n;
462 ngx_int_t rc;
Igor Sysoevf38e0462004-07-16 17:11:43 +0000463 ngx_connection_t *c;
464 ngx_http_request_t *r;
465
466 c = rev->data;
467 r = c->data;
468
469 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
470 "http check ssl handshake");
471
472 if (rev->timedout) {
Igor Sysoevc04deca2005-03-28 14:43:02 +0000473 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
474 c->timedout = 1;
475 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
Igor Sysoevf38e0462004-07-16 17:11:43 +0000476 return;
477 }
478
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000479 n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
Igor Sysoevf38e0462004-07-16 17:11:43 +0000480
481 if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
Igor Sysoevbd68d512008-01-08 20:51:06 +0000482
483 if (!rev->timer_set) {
484 ngx_add_timer(rev, c->listening->post_accept_timeout);
485 }
486
Igor Sysoev57b088d2008-01-09 08:21:57 +0000487 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
488 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
489 }
490
Igor Sysoevf38e0462004-07-16 17:11:43 +0000491 return;
492 }
493
494 if (n == 1) {
495 if (buf[0] == 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
496 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000497 "https ssl handshake: 0x%02Xd", buf[0]);
Igor Sysoevf38e0462004-07-16 17:11:43 +0000498
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000499 rc = ngx_ssl_handshake(c);
500
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000501 if (rc == NGX_AGAIN) {
Igor Sysoev7b8ed422008-01-04 09:32:12 +0000502
503 if (!rev->timer_set) {
504 ngx_add_timer(rev, c->listening->post_accept_timeout);
505 }
506
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000507 c->ssl->handler = ngx_http_ssl_handshake_handler;
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000508 return;
509 }
510
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000511 ngx_http_ssl_handshake_handler(c);
512
513 return;
Igor Sysoevf38e0462004-07-16 17:11:43 +0000514
515 } else {
516 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
517 "plain http");
518
519 r->plain_http = 1;
520 }
521 }
522
Igor Sysoev899b44e2005-05-12 14:58:06 +0000523 rev->handler = ngx_http_process_request_line;
Igor Sysoev6b863e32003-05-12 15:52:24 +0000524 ngx_http_process_request_line(rev);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000525}
526
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000527
528static void
529ngx_http_ssl_handshake_handler(ngx_connection_t *c)
530{
531 ngx_http_request_t *r;
532
533 if (c->ssl->handshaked) {
534
535 /*
536 * The majority of browsers do not send the "close notify" alert.
537 * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
538 * and Links. And what is more, MSIE ignores the server's alert.
539 *
540 * Opera and recent Mozilla send the alert.
541 */
542
543 c->ssl->no_wait_shutdown = 1;
544
545 c->read->handler = ngx_http_process_request_line;
546 /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
547
548 ngx_http_process_request_line(c->read);
549
550 return;
551 }
552
553 r = c->data;
554
555 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000556
557 return;
558}
559
Igor Sysoeve60303c2007-05-29 15:21:09 +0000560#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
561
562int
563ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
564{
565 u_char *p;
566 ngx_uint_t hash;
567 const char *servername;
568 ngx_connection_t *c;
569 ngx_http_request_t *r;
570 ngx_http_ssl_srv_conf_t *sscf;
571
572 servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
573
574 if (servername == NULL) {
575 return SSL_TLSEXT_ERR_NOACK;
576 }
577
578 c = ngx_ssl_get_connection(ssl_conn);
579
580 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
581 "SSL server name: \"%s\"", servername);
582
583 r = c->data;
584
585 if (r->virtual_names == NULL) {
586 return SSL_TLSEXT_ERR_NOACK;
587 }
588
589 /* it seems browsers send low case server name */
590
591 hash = 0;
592
593 for (p = (u_char *) servername; *p; p++) {
594 hash = ngx_hash(hash, *p);
595 }
596
597 ngx_http_find_virtual_server(r, (u_char *) servername,
598 p - (u_char *) servername, hash);
599
600 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
601
602 SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
603
604 return SSL_TLSEXT_ERR_OK;
605}
606
607#endif
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000608
Igor Sysoevf38e0462004-07-16 17:11:43 +0000609#endif
610
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000611
Igor Sysoevc1571722005-03-19 12:38:37 +0000612static void
613ngx_http_process_request_line(ngx_event_t *rev)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000614{
Igor Sysoev8decab32007-10-18 11:36:58 +0000615 ssize_t n;
616 ngx_int_t rc, rv;
617 ngx_connection_t *c;
618 ngx_http_request_t *r;
619 ngx_http_core_srv_conf_t *cscf;
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000620
Igor Sysoeva9830112003-05-19 16:39:14 +0000621 c = rev->data;
622 r = c->data;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000623
Igor Sysoev2b58fbf2003-12-09 15:08:11 +0000624 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
625 "http process request line");
Igor Sysoevdc479b42003-03-20 16:09:44 +0000626
627 if (rev->timedout) {
Igor Sysoevc04deca2005-03-28 14:43:02 +0000628 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
629 c->timedout = 1;
630 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
Igor Sysoev6b863e32003-05-12 15:52:24 +0000631 return;
Igor Sysoevdc479b42003-03-20 16:09:44 +0000632 }
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000633
Igor Sysoevf7abd722004-09-23 06:32:00 +0000634 rc = NGX_AGAIN;
Igor Sysoev016b8522002-08-29 16:59:54 +0000635
Igor Sysoevf7abd722004-09-23 06:32:00 +0000636 for ( ;; ) {
Igor Sysoevdc479b42003-03-20 16:09:44 +0000637
Igor Sysoevf7abd722004-09-23 06:32:00 +0000638 if (rc == NGX_AGAIN) {
639 n = ngx_http_read_request_header(r);
Igor Sysoevdc479b42003-03-20 16:09:44 +0000640
Igor Sysoevf7abd722004-09-23 06:32:00 +0000641 if (n == NGX_AGAIN || n == NGX_ERROR) {
Igor Sysoev865c1502003-11-30 20:03:18 +0000642 return;
643 }
Igor Sysoeva9830112003-05-19 16:39:14 +0000644 }
645
Igor Sysoevf7abd722004-09-23 06:32:00 +0000646 rc = ngx_http_parse_request_line(r, r->header_in);
Igor Sysoeva9830112003-05-19 16:39:14 +0000647
Igor Sysoevf7abd722004-09-23 06:32:00 +0000648 if (rc == NGX_OK) {
Igor Sysoevdc479b42003-03-20 16:09:44 +0000649
Igor Sysoevf7abd722004-09-23 06:32:00 +0000650 /* the request line has been parsed successfully */
Igor Sysoev6b863e32003-05-12 15:52:24 +0000651
Igor Sysoev1b735832004-11-11 14:07:14 +0000652 r->request_line.len = r->request_end - r->request_start;
653 r->request_line.data = r->request_start;
Igor Sysoevbb6ec8c2003-11-20 07:05:50 +0000654
Igor Sysoevbb6ec8c2003-11-20 07:05:50 +0000655
Igor Sysoevdc479b42003-03-20 16:09:44 +0000656 if (r->args_start) {
Igor Sysoevf7abd722004-09-23 06:32:00 +0000657 r->uri.len = r->args_start - 1 - r->uri_start;
Igor Sysoevdc479b42003-03-20 16:09:44 +0000658 } else {
Igor Sysoevf7abd722004-09-23 06:32:00 +0000659 r->uri.len = r->uri_end - r->uri_start;
Igor Sysoevdc479b42003-03-20 16:09:44 +0000660 }
661
Igor Sysoevf6906042004-11-25 16:17:31 +0000662
Igor Sysoev924bd792004-10-11 15:07:03 +0000663 if (r->complex_uri || r->quoted_uri) {
Igor Sysoev1b735832004-11-11 14:07:14 +0000664
Igor Sysoevc1571722005-03-19 12:38:37 +0000665 r->uri.data = ngx_palloc(r->pool, r->uri.len + 1);
666 if (r->uri.data == NULL) {
Igor Sysoev1b735832004-11-11 14:07:14 +0000667 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoev1b735832004-11-11 14:07:14 +0000668 return;
669 }
670
Igor Sysoev8decab32007-10-18 11:36:58 +0000671 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
672
673 rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
Igor Sysoev682bf8e2002-09-16 15:01:44 +0000674
Igor Sysoevc04deca2005-03-28 14:43:02 +0000675 if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
676 ngx_log_error(NGX_LOG_INFO, c->log, 0,
677 "client sent invalid request");
678 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
Igor Sysoevf7abd722004-09-23 06:32:00 +0000679 return;
680 }
Igor Sysoevdc479b42003-03-20 16:09:44 +0000681
Igor Sysoevf7abd722004-09-23 06:32:00 +0000682 } else {
Igor Sysoev1b735832004-11-11 14:07:14 +0000683 r->uri.data = r->uri_start;
Igor Sysoevf7abd722004-09-23 06:32:00 +0000684 }
685
Igor Sysoevf6906042004-11-25 16:17:31 +0000686
Igor Sysoev1b735832004-11-11 14:07:14 +0000687 r->unparsed_uri.len = r->uri_end - r->uri_start;
688 r->unparsed_uri.data = r->uri_start;
Igor Sysoevf7abd722004-09-23 06:32:00 +0000689
Igor Sysoevf7abd722004-09-23 06:32:00 +0000690
Igor Sysoev899b44e2005-05-12 14:58:06 +0000691 r->method_name.len = r->method_end - r->request_start + 1;
692 r->method_name.data = r->request_line.data;
Igor Sysoevf7abd722004-09-23 06:32:00 +0000693
Igor Sysoev1b735832004-11-11 14:07:14 +0000694
Igor Sysoev02025fd2005-01-18 13:03:58 +0000695 if (r->http_protocol.data) {
696 r->http_protocol.len = r->request_end - r->http_protocol.data;
697 }
698
699
Igor Sysoevf7abd722004-09-23 06:32:00 +0000700 if (r->uri_ext) {
Igor Sysoevf7abd722004-09-23 06:32:00 +0000701 if (r->args_start) {
702 r->exten.len = r->args_start - 1 - r->uri_ext;
703 } else {
704 r->exten.len = r->uri_end - r->uri_ext;
705 }
706
Igor Sysoev1b735832004-11-11 14:07:14 +0000707 r->exten.data = r->uri_ext;
Igor Sysoevf7abd722004-09-23 06:32:00 +0000708 }
709
Igor Sysoev1b735832004-11-11 14:07:14 +0000710
Igor Sysoevf7abd722004-09-23 06:32:00 +0000711 if (r->args_start && r->uri_end > r->args_start) {
Igor Sysoevf7abd722004-09-23 06:32:00 +0000712 r->args.len = r->uri_end - r->args_start;
Igor Sysoev1b735832004-11-11 14:07:14 +0000713 r->args.data = r->args_start;
Igor Sysoevf7abd722004-09-23 06:32:00 +0000714 }
715
Igor Sysoevf7abd722004-09-23 06:32:00 +0000716
717 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000718 "http request line: \"%V\"", &r->request_line);
Igor Sysoevf7abd722004-09-23 06:32:00 +0000719
720 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000721 "http uri: \"%V\"", &r->uri);
Igor Sysoevf7abd722004-09-23 06:32:00 +0000722
723 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000724 "http args: \"%V\"", &r->args);
725
726 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
727 "http exten: \"%V\"", &r->exten);
728
Igor Sysoevf7abd722004-09-23 06:32:00 +0000729 if (r->http_version < NGX_HTTP_VERSION_10) {
Igor Sysoev62864d12007-06-04 20:40:03 +0000730 ngx_http_process_request(r);
Igor Sysoev6b863e32003-05-12 15:52:24 +0000731 return;
732 }
Igor Sysoevdc479b42003-03-20 16:09:44 +0000733
Igor Sysoevdc479b42003-03-20 16:09:44 +0000734
Igor Sysoevf7abd722004-09-23 06:32:00 +0000735 if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000736 sizeof(ngx_table_elt_t))
737 == NGX_ERROR)
Igor Sysoevf7abd722004-09-23 06:32:00 +0000738 {
739 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoevf7abd722004-09-23 06:32:00 +0000740 return;
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000741 }
Igor Sysoevf7abd722004-09-23 06:32:00 +0000742
743
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000744 if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000745 sizeof(ngx_table_elt_t *))
746 == NGX_ERROR)
Igor Sysoevf7abd722004-09-23 06:32:00 +0000747 {
748 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoevf7abd722004-09-23 06:32:00 +0000749 return;
750 }
751
Igor Sysoeve5a222c2005-01-25 12:27:35 +0000752 c->log->action = "reading client request headers";
Igor Sysoevf7abd722004-09-23 06:32:00 +0000753
Igor Sysoev899b44e2005-05-12 14:58:06 +0000754 rev->handler = ngx_http_process_request_headers;
Igor Sysoevf7abd722004-09-23 06:32:00 +0000755 ngx_http_process_request_headers(rev);
756
757 return;
Igor Sysoevc04deca2005-03-28 14:43:02 +0000758 }
Igor Sysoevf7abd722004-09-23 06:32:00 +0000759
Igor Sysoevc04deca2005-03-28 14:43:02 +0000760 if (rc != NGX_AGAIN) {
Igor Sysoevf7abd722004-09-23 06:32:00 +0000761
762 /* there was error while a request line parsing */
763
Igor Sysoevc04deca2005-03-28 14:43:02 +0000764 ngx_log_error(NGX_LOG_INFO, c->log, 0,
765 ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
766 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
Igor Sysoevf7abd722004-09-23 06:32:00 +0000767 return;
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000768 }
769
Igor Sysoevf7abd722004-09-23 06:32:00 +0000770 /* NGX_AGAIN: a request line parsing is still incomplete */
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000771
Igor Sysoev236e0452004-09-23 16:39:34 +0000772 if (r->header_in->pos == r->header_in->end) {
Igor Sysoeva1c8a922004-01-19 18:09:14 +0000773
Igor Sysoevf7abd722004-09-23 06:32:00 +0000774 rv = ngx_http_alloc_large_header_buffer(r, 1);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000775
Igor Sysoevf7abd722004-09-23 06:32:00 +0000776 if (rv == NGX_ERROR) {
777 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoevf7abd722004-09-23 06:32:00 +0000778 return;
779 }
Igor Sysoev0dad6292003-03-05 17:30:51 +0000780
Igor Sysoevf7abd722004-09-23 06:32:00 +0000781 if (rv == NGX_DECLINED) {
Igor Sysoevc04deca2005-03-28 14:43:02 +0000782 r->request_line.len = r->header_in->end - r->request_start;
783 r->request_line.data = r->request_start;
784
785 ngx_log_error(NGX_LOG_INFO, c->log, 0,
786 "client sent too long URI");
787 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
Igor Sysoev6b863e32003-05-12 15:52:24 +0000788 return;
Igor Sysoev0dad6292003-03-05 17:30:51 +0000789 }
Igor Sysoev0dad6292003-03-05 17:30:51 +0000790 }
Igor Sysoev1af7c822002-09-13 14:47:42 +0000791 }
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000792}
793
Igor Sysoev1af7c822002-09-13 14:47:42 +0000794
Igor Sysoevc1571722005-03-19 12:38:37 +0000795static void
796ngx_http_process_request_headers(ngx_event_t *rev)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000797{
Igor Sysoev02f742b2005-04-08 15:18:55 +0000798 ssize_t n;
799 ngx_int_t rc, rv;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000800 ngx_str_t header;
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000801 ngx_uint_t i;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000802 ngx_table_elt_t *h;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000803 ngx_connection_t *c;
804 ngx_http_header_t *hh;
805 ngx_http_request_t *r;
Igor Sysoev3362b8d2005-05-14 18:42:03 +0000806 ngx_http_core_srv_conf_t *cscf;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000807 ngx_http_core_main_conf_t *cmcf;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000808
Igor Sysoeva9830112003-05-19 16:39:14 +0000809 c = rev->data;
810 r = c->data;
Igor Sysoevd581fd52003-05-13 16:02:32 +0000811
Igor Sysoev865c1502003-11-30 20:03:18 +0000812 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
813 "http process request header line");
Igor Sysoevd581fd52003-05-13 16:02:32 +0000814
Igor Sysoev6b863e32003-05-12 15:52:24 +0000815 if (rev->timedout) {
Igor Sysoevc04deca2005-03-28 14:43:02 +0000816 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
817 c->timedout = 1;
818 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
Igor Sysoev6b863e32003-05-12 15:52:24 +0000819 return;
820 }
821
Igor Sysoev02f742b2005-04-08 15:18:55 +0000822 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
Igor Sysoev3362b8d2005-05-14 18:42:03 +0000823 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000824
Igor Sysoevd581fd52003-05-13 16:02:32 +0000825 rc = NGX_AGAIN;
Igor Sysoev6b863e32003-05-12 15:52:24 +0000826
Igor Sysoev016b8522002-08-29 16:59:54 +0000827 for ( ;; ) {
Igor Sysoevf7abd722004-09-23 06:32:00 +0000828
Igor Sysoevd581fd52003-05-13 16:02:32 +0000829 if (rc == NGX_AGAIN) {
Igor Sysoevf7abd722004-09-23 06:32:00 +0000830
Igor Sysoev236e0452004-09-23 16:39:34 +0000831 if (r->header_in->pos == r->header_in->end) {
Igor Sysoevf7abd722004-09-23 06:32:00 +0000832
833 rv = ngx_http_alloc_large_header_buffer(r, 0);
834
835 if (rv == NGX_ERROR) {
836 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoevf7abd722004-09-23 06:32:00 +0000837 return;
838 }
839
840 if (rv == NGX_DECLINED) {
Igor Sysoevc04deca2005-03-28 14:43:02 +0000841 header.len = r->header_in->end - r->header_name_start;
842 header.data = r->header_name_start;
Igor Sysoev90c08142005-07-25 09:41:38 +0000843
844 if (header.len > NGX_MAX_ERROR_STR - 300) {
845 header.len = NGX_MAX_ERROR_STR - 300;
846 header.data[header.len++] = '.';
847 header.data[header.len++] = '.';
848 header.data[header.len++] = '.';
849 }
850
Igor Sysoevc04deca2005-03-28 14:43:02 +0000851 ngx_log_error(NGX_LOG_INFO, c->log, 0,
852 "client sent too long header line: \"%V\"",
853 &header);
Igor Sysoev543a6ae2007-08-27 20:52:40 +0000854 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
Igor Sysoevf7abd722004-09-23 06:32:00 +0000855 return;
856 }
857 }
858
Igor Sysoevd581fd52003-05-13 16:02:32 +0000859 n = ngx_http_read_request_header(r);
860
861 if (n == NGX_AGAIN || n == NGX_ERROR) {
862 return;
863 }
864 }
865
Igor Sysoeve6779222003-10-03 15:50:53 +0000866 rc = ngx_http_parse_header_line(r, r->header_in);
Igor Sysoev016b8522002-08-29 16:59:54 +0000867
Igor Sysoev0dad6292003-03-05 17:30:51 +0000868 if (rc == NGX_OK) {
Igor Sysoevd581fd52003-05-13 16:02:32 +0000869
Igor Sysoev3362b8d2005-05-14 18:42:03 +0000870 if (r->invalid_header && cscf->ignore_invalid_headers) {
Igor Sysoev4d656dc2005-03-22 16:02:46 +0000871
872 /* there was error while a header line parsing */
873
874 header.len = r->header_end - r->header_name_start;
875 header.data = r->header_name_start;
876
Igor Sysoevc04deca2005-03-28 14:43:02 +0000877 ngx_log_error(NGX_LOG_INFO, c->log, 0,
878 "client sent invalid header line: \"%V\"",
Igor Sysoev4d656dc2005-03-22 16:02:46 +0000879 &header);
880 continue;
881 }
882
Igor Sysoevd581fd52003-05-13 16:02:32 +0000883 /* a header line has been parsed successfully */
884
Igor Sysoevc1571722005-03-19 12:38:37 +0000885 h = ngx_list_push(&r->headers_in.headers);
886 if (h == NULL) {
Igor Sysoevd581fd52003-05-13 16:02:32 +0000887 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoevd581fd52003-05-13 16:02:32 +0000888 return;
Igor Sysoevdc479b42003-03-20 16:09:44 +0000889 }
890
Igor Sysoev02f742b2005-04-08 15:18:55 +0000891 h->hash = r->header_hash;
892
Igor Sysoevd581fd52003-05-13 16:02:32 +0000893 h->key.len = r->header_name_end - r->header_name_start;
Igor Sysoevf7abd722004-09-23 06:32:00 +0000894 h->key.data = r->header_name_start;
895 h->key.data[h->key.len] = '\0';
896
Igor Sysoevd581fd52003-05-13 16:02:32 +0000897 h->value.len = r->header_end - r->header_start;
Igor Sysoevf7abd722004-09-23 06:32:00 +0000898 h->value.data = r->header_start;
899 h->value.data[h->value.len] = '\0';
Igor Sysoevd581fd52003-05-13 16:02:32 +0000900
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000901 h->lowcase_key = ngx_palloc(r->pool, h->key.len);
902 if (h->lowcase_key == NULL) {
903 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
904 return;
905 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000906
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000907 if (h->key.len == r->lowcase_index) {
908 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
909
910 } else {
911 for (i = 0; i < h->key.len; i++) {
Igor Sysoev8f125582006-07-28 15:16:17 +0000912 h->lowcase_key[i] = ngx_tolower(h->key.data[i]);
Igor Sysoevd581fd52003-05-13 16:02:32 +0000913 }
Igor Sysoevd581fd52003-05-13 16:02:32 +0000914 }
915
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000916 hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
917 h->lowcase_key, h->key.len);
918
919 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
920 return;
921 }
922
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000923 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000924 "http header: \"%V: %V\"",
925 &h->key, &h->value);
Igor Sysoevd581fd52003-05-13 16:02:32 +0000926
Igor Sysoev6253ca12003-05-27 12:18:54 +0000927 continue;
Igor Sysoevc04deca2005-03-28 14:43:02 +0000928 }
Igor Sysoev6253ca12003-05-27 12:18:54 +0000929
Igor Sysoevc04deca2005-03-28 14:43:02 +0000930 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
Igor Sysoevd581fd52003-05-13 16:02:32 +0000931
932 /* a whole header has been parsed successfully */
933
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000934 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
935 "http header done");
Igor Sysoev42feecb2002-12-15 06:25:09 +0000936
Igor Sysoev6a12fc92004-12-06 14:45:08 +0000937 r->request_length += r->header_in->pos - r->header_in->start;
938
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000939 r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
940
Igor Sysoev419f9ac2003-10-21 16:49:56 +0000941 rc = ngx_http_process_request_header(r);
Igor Sysoeva19a85e2003-01-28 15:56:37 +0000942
Igor Sysoev419f9ac2003-10-21 16:49:56 +0000943 if (rc != NGX_OK) {
Igor Sysoev419f9ac2003-10-21 16:49:56 +0000944 return;
Igor Sysoeva09f08d2003-04-25 14:43:13 +0000945 }
946
Igor Sysoev62864d12007-06-04 20:40:03 +0000947 ngx_http_process_request(r);
Igor Sysoeve5035392005-08-30 10:55:07 +0000948
Igor Sysoevd581fd52003-05-13 16:02:32 +0000949 return;
Igor Sysoev0dad6292003-03-05 17:30:51 +0000950 }
951
Igor Sysoevc04deca2005-03-28 14:43:02 +0000952 if (rc == NGX_AGAIN) {
Igor Sysoev0dad6292003-03-05 17:30:51 +0000953
Igor Sysoevc04deca2005-03-28 14:43:02 +0000954 /* a header line parsing is still not complete */
955
956 continue;
957 }
958
959 /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
960
961 header.len = r->header_end - r->header_name_start;
962 header.data = r->header_name_start;
963 ngx_log_error(NGX_LOG_INFO, c->log, 0,
964 "client sent invalid header line: \"%V\\r...\"",
965 &header);
Igor Sysoev543a6ae2007-08-27 20:52:40 +0000966 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
Igor Sysoevc04deca2005-03-28 14:43:02 +0000967 return;
Igor Sysoeva0bb31f2002-12-02 16:09:40 +0000968 }
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000969}
970
Igor Sysoevb0869052002-12-10 18:05:12 +0000971
Igor Sysoevc1571722005-03-19 12:38:37 +0000972static ssize_t
973ngx_http_read_request_header(ngx_http_request_t *r)
Igor Sysoev6b863e32003-05-12 15:52:24 +0000974{
Igor Sysoev187fcd82003-05-23 11:53:01 +0000975 ssize_t n;
976 ngx_event_t *rev;
Igor Sysoevda173ab2006-08-30 10:39:17 +0000977 ngx_connection_t *c;
Igor Sysoev187fcd82003-05-23 11:53:01 +0000978 ngx_http_core_srv_conf_t *cscf;
Igor Sysoev6b863e32003-05-12 15:52:24 +0000979
Igor Sysoevda173ab2006-08-30 10:39:17 +0000980 c = r->connection;
981 rev = c->read;
Igor Sysoev239baac2003-06-11 15:28:34 +0000982
Igor Sysoev6b863e32003-05-12 15:52:24 +0000983 n = r->header_in->last - r->header_in->pos;
984
985 if (n > 0) {
986 return n;
987 }
988
Igor Sysoevb145b062005-06-15 18:33:41 +0000989 if (rev->ready) {
Igor Sysoevda173ab2006-08-30 10:39:17 +0000990 n = c->recv(c, r->header_in->last,
991 r->header_in->end - r->header_in->last);
Igor Sysoevb145b062005-06-15 18:33:41 +0000992 } else {
993 n = NGX_AGAIN;
Igor Sysoev222a2ad2003-11-18 16:49:00 +0000994 }
995
Igor Sysoev6b863e32003-05-12 15:52:24 +0000996 if (n == NGX_AGAIN) {
Igor Sysoev1bc3a932008-01-08 20:55:27 +0000997 if (!rev->timer_set) {
Igor Sysoevf38e0462004-07-16 17:11:43 +0000998 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
Igor Sysoev187fcd82003-05-23 11:53:01 +0000999 ngx_add_timer(rev, cscf->client_header_timeout);
Igor Sysoev6b863e32003-05-12 15:52:24 +00001000 }
1001
Igor Sysoevb5faed22003-10-29 08:30:44 +00001002 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
Igor Sysoevb3e73d82003-10-10 15:10:50 +00001003 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoevb3e73d82003-10-10 15:10:50 +00001004 return NGX_ERROR;
1005 }
1006
Igor Sysoev6b863e32003-05-12 15:52:24 +00001007 return NGX_AGAIN;
1008 }
1009
1010 if (n == 0) {
Igor Sysoevda173ab2006-08-30 10:39:17 +00001011 ngx_log_error(NGX_LOG_INFO, c->log, 0,
Igor Sysoev6b863e32003-05-12 15:52:24 +00001012 "client closed prematurely connection");
Igor Sysoevd581fd52003-05-13 16:02:32 +00001013 }
Igor Sysoev6b863e32003-05-12 15:52:24 +00001014
1015 if (n == 0 || n == NGX_ERROR) {
Igor Sysoev23169362007-01-08 18:22:06 +00001016 c->error = 1;
Igor Sysoev1691aaf2007-01-08 18:23:38 +00001017 c->log->action = "reading client request headers";
Igor Sysoevda173ab2006-08-30 10:39:17 +00001018
Igor Sysoev23169362007-01-08 18:22:06 +00001019 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
Igor Sysoev6b863e32003-05-12 15:52:24 +00001020 return NGX_ERROR;
1021 }
1022
1023 r->header_in->last += n;
1024
1025 return n;
1026}
1027
1028
Igor Sysoevc1571722005-03-19 12:38:37 +00001029static ngx_int_t
1030ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
1031 ngx_uint_t request_line)
Igor Sysoev85080d02004-09-22 16:18:21 +00001032{
1033 u_char *old, *new;
Igor Sysoev85080d02004-09-22 16:18:21 +00001034 ngx_buf_t *b;
1035 ngx_http_connection_t *hc;
1036 ngx_http_core_srv_conf_t *cscf;
1037
1038 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1039 "http alloc large header buffer");
1040
Igor Sysoevf7abd722004-09-23 06:32:00 +00001041 if (request_line && r->state == 0) {
1042
1043 /* the client fills up the buffer with "\r\n" */
1044
Igor Sysoev6a12fc92004-12-06 14:45:08 +00001045 r->request_length += r->header_in->end - r->header_in->start;
1046
Igor Sysoevf7abd722004-09-23 06:32:00 +00001047 r->header_in->pos = r->header_in->start;
1048 r->header_in->last = r->header_in->start;
1049
1050 return NGX_OK;
1051 }
1052
Igor Sysoev85080d02004-09-22 16:18:21 +00001053 old = request_line ? r->request_start : r->header_name_start;
1054
1055 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1056
Igor Sysoevf7abd722004-09-23 06:32:00 +00001057 if (r->state != 0
1058 && (size_t) (r->header_in->pos - old)
1059 >= cscf->large_client_header_buffers.size)
1060 {
Igor Sysoev85080d02004-09-22 16:18:21 +00001061 return NGX_DECLINED;
1062 }
1063
1064 hc = r->http_connection;
1065
1066 if (hc->nfree) {
1067 b = hc->free[--hc->nfree];
1068
Igor Sysoev236e0452004-09-23 16:39:34 +00001069 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00001070 "http large header free: %p %uz",
Igor Sysoev236e0452004-09-23 16:39:34 +00001071 b->pos, b->end - b->last);
1072
Igor Sysoevf7abd722004-09-23 06:32:00 +00001073 } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
Igor Sysoev85080d02004-09-22 16:18:21 +00001074
1075 if (hc->busy == NULL) {
1076 hc->busy = ngx_palloc(r->connection->pool,
Igor Sysoevf7abd722004-09-23 06:32:00 +00001077 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
Igor Sysoev85080d02004-09-22 16:18:21 +00001078 if (hc->busy == NULL) {
1079 return NGX_ERROR;
1080 }
1081 }
1082
1083 b = ngx_create_temp_buf(r->connection->pool,
Igor Sysoevf7abd722004-09-23 06:32:00 +00001084 cscf->large_client_header_buffers.size);
Igor Sysoev85080d02004-09-22 16:18:21 +00001085 if (b == NULL) {
1086 return NGX_ERROR;
1087 }
1088
Igor Sysoev236e0452004-09-23 16:39:34 +00001089 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00001090 "http large header alloc: %p %uz",
Igor Sysoev236e0452004-09-23 16:39:34 +00001091 b->pos, b->end - b->last);
1092
Igor Sysoev85080d02004-09-22 16:18:21 +00001093 } else {
1094 return NGX_DECLINED;
1095 }
1096
1097 hc->busy[hc->nbusy++] = b;
1098
Igor Sysoevf7abd722004-09-23 06:32:00 +00001099 if (r->state == 0) {
1100 /*
1101 * r->state == 0 means that a header line was parsed successfully
1102 * and we do not need to copy incomplete header line and
1103 * to relocate the parser header pointers
1104 */
1105
Igor Sysoev6a12fc92004-12-06 14:45:08 +00001106 r->request_length += r->header_in->end - r->header_in->start;
1107
Igor Sysoevf7abd722004-09-23 06:32:00 +00001108 r->header_in = b;
1109
1110 return NGX_OK;
1111 }
1112
Igor Sysoev236e0452004-09-23 16:39:34 +00001113 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1114 "http large header copy: %d", r->header_in->pos - old);
1115
Igor Sysoev6a12fc92004-12-06 14:45:08 +00001116 r->request_length += old - r->header_in->start;
1117
Igor Sysoev85080d02004-09-22 16:18:21 +00001118 new = b->start;
1119
Igor Sysoev236e0452004-09-23 16:39:34 +00001120 ngx_memcpy(new, old, r->header_in->pos - old);
Igor Sysoev85080d02004-09-22 16:18:21 +00001121
1122 b->pos = new + (r->header_in->pos - old);
Igor Sysoev236e0452004-09-23 16:39:34 +00001123 b->last = new + (r->header_in->pos - old);
Igor Sysoev85080d02004-09-22 16:18:21 +00001124
1125 if (request_line) {
1126 r->request_start = new;
Igor Sysoevf7abd722004-09-23 06:32:00 +00001127
1128 if (r->request_end) {
1129 r->request_end = new + (r->request_end - old);
1130 }
1131
1132 r->method_end = new + (r->method_end - old);
Igor Sysoev85080d02004-09-22 16:18:21 +00001133
1134 r->uri_start = new + (r->uri_start - old);
1135 r->uri_end = new + (r->uri_end - old);
1136
Igor Sysoevf7abd722004-09-23 06:32:00 +00001137 if (r->schema_start) {
1138 r->schema_start = new + (r->schema_start - old);
1139 r->schema_end = new + (r->schema_end - old);
1140 }
1141
1142 if (r->host_start) {
1143 r->host_start = new + (r->host_start - old);
Igor Sysoev03011fa2006-12-15 10:24:57 +00001144 if (r->host_end) {
1145 r->host_end = new + (r->host_end - old);
1146 }
Igor Sysoevf7abd722004-09-23 06:32:00 +00001147 }
1148
1149 if (r->port_start) {
1150 r->port_start = new + (r->port_start - old);
1151 r->port_end = new + (r->port_end - old);
1152 }
1153
Igor Sysoev85080d02004-09-22 16:18:21 +00001154 if (r->uri_ext) {
1155 r->uri_ext = new + (r->uri_ext - old);
1156 }
1157
1158 if (r->args_start) {
1159 r->args_start = new + (r->args_start - old);
1160 }
1161
Igor Sysoev58ea0c12007-12-01 12:22:48 +00001162 if (r->http_protocol.data) {
1163 r->http_protocol.data = new + (r->http_protocol.data - old);
1164 }
1165
Igor Sysoev85080d02004-09-22 16:18:21 +00001166 } else {
1167 r->header_name_start = new;
1168 r->header_name_end = new + (r->header_name_end - old);
1169 r->header_start = new + (r->header_start - old);
1170 r->header_end = new + (r->header_end - old);
1171 }
1172
1173 r->header_in = b;
1174
1175 return NGX_OK;
1176}
1177
Igor Sysoev85080d02004-09-22 16:18:21 +00001178
Igor Sysoevc1571722005-03-19 12:38:37 +00001179static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +00001180ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1181 ngx_uint_t offset)
1182{
1183 ngx_table_elt_t **ph;
1184
1185 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1186
1187 if (*ph == NULL) {
1188 *ph = h;
1189 }
1190
1191 return NGX_OK;
1192}
1193
1194
1195static ngx_int_t
Igor Sysoevb145b062005-06-15 18:33:41 +00001196ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
1197 ngx_uint_t offset)
1198{
1199 ngx_table_elt_t **ph;
1200
1201 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
1202
1203 if (*ph == NULL) {
1204 *ph = h;
1205 return NGX_OK;
1206 }
1207
1208 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
Igor Sysoevda173ab2006-08-30 10:39:17 +00001209 "client sent duplicate header line: \"%V: %V\", "
1210 "previous value: \"%V: %V\"",
1211 &h->key, &h->value, &(*ph)->key, &(*ph)->value);
Igor Sysoevb145b062005-06-15 18:33:41 +00001212
1213 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1214
1215 return NGX_ERROR;
1216}
1217
1218
1219static ngx_int_t
Igor Sysoevec67b192007-09-09 18:28:49 +00001220ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
1221 ngx_uint_t offset)
1222{
Igor Sysoevf5329412007-10-01 12:57:47 +00001223 if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
Igor Sysoevec67b192007-09-09 18:28:49 +00001224 r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
1225
Igor Sysoevf5329412007-10-01 12:57:47 +00001226 } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
Igor Sysoevec67b192007-09-09 18:28:49 +00001227 r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
1228 }
1229
1230 return NGX_OK;
1231}
1232
1233
1234static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +00001235ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
1236 ngx_uint_t offset)
1237{
1238 ngx_table_elt_t **cookie;
1239
1240 cookie = ngx_array_push(&r->headers_in.cookies);
Igor Sysoevb145b062005-06-15 18:33:41 +00001241 if (cookie) {
1242 *cookie = h;
1243 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001244 }
1245
Igor Sysoevb145b062005-06-15 18:33:41 +00001246 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001247
Igor Sysoevb145b062005-06-15 18:33:41 +00001248 return NGX_ERROR;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001249}
1250
1251
1252static ngx_int_t
Igor Sysoevc1571722005-03-19 12:38:37 +00001253ngx_http_process_request_header(ngx_http_request_t *r)
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001254{
Igor Sysoev62864d12007-06-04 20:40:03 +00001255 size_t len;
1256 u_char *host, *ua, *user_agent, ch;
1257 ngx_uint_t hash;
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001258
1259 if (r->headers_in.host) {
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001260
1261 hash = 0;
1262
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001263 for (len = 0; len < r->headers_in.host->value.len; len++) {
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001264 ch = r->headers_in.host->value.data[len];
1265
1266 if (ch == ':') {
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001267 break;
1268 }
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001269
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001270 ch = ngx_tolower(ch);
1271 r->headers_in.host->value.data[len] = ch;
1272 hash = ngx_hash(hash, ch);
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001273 }
Igor Sysoevc04deca2005-03-28 14:43:02 +00001274
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001275 if (len && r->headers_in.host->value.data[len - 1] == '.') {
Igor Sysoev899b44e2005-05-12 14:58:06 +00001276 len--;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001277 hash = ngx_hash_key(r->headers_in.host->value.data, len);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001278 }
1279
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001280 r->headers_in.host_name_len = len;
1281
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001282 if (r->virtual_names) {
Igor Sysoeve60303c2007-05-29 15:21:09 +00001283
1284 host = r->host_start;
1285
1286 if (host == NULL) {
1287 host = r->headers_in.host->value.data;
1288 len = r->headers_in.host_name_len;
1289
1290 } else {
1291 len = r->host_end - host;
1292 }
1293
1294 ngx_http_find_virtual_server(r, host, len, hash);
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001295 }
Igor Sysoev3043bfc2004-05-14 16:51:47 +00001296
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001297 } else {
1298 if (r->http_version > NGX_HTTP_VERSION_10) {
Igor Sysoevc04deca2005-03-28 14:43:02 +00001299 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1300 "client sent HTTP/1.1 request without \"Host\" header");
1301 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
1302 return NGX_ERROR;
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001303 }
Igor Sysoevc04deca2005-03-28 14:43:02 +00001304
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001305 r->headers_in.host_name_len = 0;
1306 }
1307
1308 if (r->headers_in.content_length) {
1309 r->headers_in.content_length_n =
Igor Sysoev1765f472006-07-07 16:33:19 +00001310 ngx_atoof(r->headers_in.content_length->value.data,
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001311 r->headers_in.content_length->value.len);
1312
1313 if (r->headers_in.content_length_n == NGX_ERROR) {
Igor Sysoevc04deca2005-03-28 14:43:02 +00001314 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1315 "client sent invalid \"Content-Length\" header");
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001316 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
Igor Sysoevc04deca2005-03-28 14:43:02 +00001317 return NGX_ERROR;
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001318 }
1319 }
1320
Igor Sysoev4ecb4d72006-04-21 12:06:44 +00001321 if (r->method & (NGX_HTTP_POST|NGX_HTTP_PUT)
1322 && r->headers_in.content_length_n == -1)
1323 {
Igor Sysoevc04deca2005-03-28 14:43:02 +00001324 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
Igor Sysoev0d68f272007-12-30 09:11:19 +00001325 "client sent %V method without \"Content-Length\" header",
1326 &r->method_name);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001327 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
1328 return NGX_ERROR;
1329 }
1330
Igor Sysoev589fc742007-09-07 09:29:08 +00001331 if (r->method & NGX_HTTP_TRACE) {
Igor Sysoev3e933d22007-03-30 18:59:26 +00001332 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1333 "client sent TRACE method");
1334 ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
1335 return NGX_ERROR;
1336 }
1337
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001338 if (r->headers_in.transfer_encoding
Igor Sysoev06597202007-09-26 19:26:14 +00001339 && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
Igor Sysoevf5329412007-10-01 12:57:47 +00001340 "chunked", 7 - 1))
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001341 {
1342 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1343 "client sent \"Transfer-Encoding: chunked\" header");
1344 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
Igor Sysoevc04deca2005-03-28 14:43:02 +00001345 return NGX_ERROR;
Igor Sysoev6881bfb2004-03-30 06:27:36 +00001346 }
1347
Igor Sysoevec67b192007-09-09 18:28:49 +00001348 if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
1349 if (r->headers_in.keep_alive) {
1350 r->headers_in.keep_alive_n =
1351 ngx_atotm(r->headers_in.keep_alive->value.data,
1352 r->headers_in.keep_alive->value.len);
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001353 }
1354 }
1355
Igor Sysoev2f657222004-06-16 15:32:11 +00001356 if (r->headers_in.user_agent) {
1357
1358 /*
1359 * check some widespread browsers while the headers are still
1360 * in CPU cache
1361 */
1362
Igor Sysoev307c3ad2004-09-17 16:07:35 +00001363 user_agent = r->headers_in.user_agent->value.data;
1364
Igor Sysoevf5329412007-10-01 12:57:47 +00001365 ua = ngx_strstrn(user_agent, "MSIE", 4 - 1);
Igor Sysoev307c3ad2004-09-17 16:07:35 +00001366
1367 if (ua && ua + 8 < user_agent + r->headers_in.user_agent->value.len) {
1368
Igor Sysoev2f657222004-06-16 15:32:11 +00001369 r->headers_in.msie = 1;
Igor Sysoev307c3ad2004-09-17 16:07:35 +00001370
Igor Sysoev2f657222004-06-16 15:32:11 +00001371 if (ua[4] == ' ' && ua[5] == '4' && ua[6] == '.') {
1372 r->headers_in.msie4 = 1;
1373 }
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001374
1375#if 0
1376 /* MSIE ignores the SSL "close notify" alert */
Igor Sysoev42b12b32004-12-02 18:40:46 +00001377 if (c->ssl) {
Igor Sysoev9fa5a822005-09-30 14:41:25 +00001378 c->ssl->no_send_shutdown = 1;
Igor Sysoev42b12b32004-12-02 18:40:46 +00001379 }
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001380#endif
Igor Sysoev2f657222004-06-16 15:32:11 +00001381 }
Igor Sysoev85cccfb2004-09-15 16:00:43 +00001382
Igor Sysoevf5329412007-10-01 12:57:47 +00001383 if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
Igor Sysoev85cccfb2004-09-15 16:00:43 +00001384 r->headers_in.opera = 1;
1385 r->headers_in.msie = 0;
1386 r->headers_in.msie4 = 0;
1387 }
Igor Sysoev307c3ad2004-09-17 16:07:35 +00001388
1389 if (!r->headers_in.msie && !r->headers_in.opera) {
1390
Igor Sysoevf5329412007-10-01 12:57:47 +00001391 if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
Igor Sysoev307c3ad2004-09-17 16:07:35 +00001392 r->headers_in.gecko = 1;
1393
Igor Sysoevf5329412007-10-01 12:57:47 +00001394 } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
Igor Sysoev307c3ad2004-09-17 16:07:35 +00001395 r->headers_in.konqueror = 1;
1396 }
1397 }
Igor Sysoev2f657222004-06-16 15:32:11 +00001398 }
1399
Igor Sysoev62864d12007-06-04 20:40:03 +00001400 return NGX_OK;
1401}
1402
1403
1404static void
1405ngx_http_process_request(ngx_http_request_t *r)
1406{
Igor Sysoev86ef6aa2007-12-10 12:09:51 +00001407 ngx_connection_t *c;
Igor Sysoev62864d12007-06-04 20:40:03 +00001408
1409 c = r->connection;
1410
Igor Sysoev1d4e9b32007-12-01 11:46:04 +00001411 if (r->plain_http) {
1412 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1413 "client sent plain HTTP request to HTTPS port");
1414 ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
1415 return;
1416 }
1417
Igor Sysoevb71c6902006-08-04 16:04:04 +00001418#if (NGX_HTTP_SSL)
1419
Igor Sysoev62864d12007-06-04 20:40:03 +00001420 if (c->ssl) {
Igor Sysoev86ef6aa2007-12-10 12:09:51 +00001421 long rc;
1422 ngx_http_ssl_srv_conf_t *sscf;
1423
Igor Sysoevb71c6902006-08-04 16:04:04 +00001424 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
1425
1426 if (sscf->verify) {
Igor Sysoev62864d12007-06-04 20:40:03 +00001427 rc = SSL_get_verify_result(c->ssl->connection);
Igor Sysoevb71c6902006-08-04 16:04:04 +00001428
1429 if (rc != X509_V_OK) {
Igor Sysoev62864d12007-06-04 20:40:03 +00001430 ngx_log_error(NGX_LOG_INFO, c->log, 0,
Igor Sysoevc55a1042006-08-09 19:59:45 +00001431 "client SSL certificate verify error: (%l:%s)",
Igor Sysoevb71c6902006-08-04 16:04:04 +00001432 rc, X509_verify_cert_error_string(rc));
1433 ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
Igor Sysoev62864d12007-06-04 20:40:03 +00001434 return;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001435 }
1436
Igor Sysoev62864d12007-06-04 20:40:03 +00001437 if (SSL_get_peer_certificate(c->ssl->connection)
Igor Sysoevb71c6902006-08-04 16:04:04 +00001438 == NULL)
1439 {
Igor Sysoev62864d12007-06-04 20:40:03 +00001440 ngx_log_error(NGX_LOG_INFO, c->log, 0,
Igor Sysoevb71c6902006-08-04 16:04:04 +00001441 "client sent no required SSL certificate");
1442 ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
Igor Sysoev62864d12007-06-04 20:40:03 +00001443 return;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001444 }
1445 }
1446 }
1447
1448#endif
1449
Igor Sysoev62864d12007-06-04 20:40:03 +00001450 if (c->read->timer_set) {
1451 ngx_del_timer(c->read);
1452 }
1453
1454#if (NGX_STAT_STUB)
1455 ngx_atomic_fetch_add(ngx_stat_reading, -1);
1456 r->stat_reading = 0;
1457 ngx_atomic_fetch_add(ngx_stat_writing, 1);
1458 r->stat_writing = 1;
1459#endif
1460
1461 c->read->handler = ngx_http_request_handler;
1462 c->write->handler = ngx_http_request_handler;
Igor Sysoev7ab4d9c2007-08-06 15:22:40 +00001463 r->read_event_handler = ngx_http_block_reading;
Igor Sysoev62864d12007-06-04 20:40:03 +00001464
1465 ngx_http_handler(r);
1466
1467 return;
Igor Sysoev419f9ac2003-10-21 16:49:56 +00001468}
1469
1470
Igor Sysoevc2807ec2006-02-16 15:26:46 +00001471static void
Igor Sysoeve60303c2007-05-29 15:21:09 +00001472ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len,
1473 ngx_uint_t hash)
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001474{
Igor Sysoev305a9d82005-12-26 17:07:48 +00001475 ngx_http_core_loc_conf_t *clcf;
1476 ngx_http_core_srv_conf_t *cscf;
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001477
Igor Sysoev43c3bf42007-08-12 19:48:12 +00001478 cscf = ngx_hash_find_combined(&r->virtual_names->names, hash, host, len);
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001479
Igor Sysoev9d8a75c2007-06-11 19:49:22 +00001480 if (cscf) {
1481 goto found;
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001482 }
1483
Igor Sysoev43c3bf42007-08-12 19:48:12 +00001484#if (NGX_PCRE)
1485
1486 if (r->virtual_names->nregex) {
Igor Sysoev86ef6aa2007-12-10 12:09:51 +00001487 ngx_int_t n;
1488 ngx_uint_t i;
1489 ngx_str_t name;
1490 ngx_http_server_name_t *sn;
Igor Sysoev43c3bf42007-08-12 19:48:12 +00001491
1492 name.len = len;
1493 name.data = host;
1494
1495 sn = r->virtual_names->regex;
1496
1497 for (i = 0; i < r->virtual_names->nregex; i++) {
1498
1499 n = ngx_regex_exec(sn[i].regex, &name, NULL, 0);
1500
1501 if (n == NGX_REGEX_NO_MATCHED) {
1502 continue;
1503 }
1504
1505 if (n < 0) {
1506 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1507 ngx_regex_exec_n
1508 " failed: %d on \"%V\" using \"%V\"",
1509 n, &name, &sn[i].name);
1510 return;
1511 }
1512
1513 /* match */
1514
1515 cscf = sn[i].core_srv_conf;
1516
1517 goto found;
1518 }
1519 }
1520
1521#endif
1522
Igor Sysoevc2807ec2006-02-16 15:26:46 +00001523 return;
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001524
Igor Sysoev899b44e2005-05-12 14:58:06 +00001525found:
1526
Igor Sysoev305a9d82005-12-26 17:07:48 +00001527 r->srv_conf = cscf->ctx->srv_conf;
1528 r->loc_conf = cscf->ctx->loc_conf;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001529
1530 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1531 r->connection->log->file = clcf->err_log->file;
1532
1533 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
1534 r->connection->log->log_level = clcf->err_log->log_level;
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001535 }
1536
Igor Sysoevc2807ec2006-02-16 15:26:46 +00001537 return;
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001538}
1539
1540
Igor Sysoev899b44e2005-05-12 14:58:06 +00001541static void
1542ngx_http_request_handler(ngx_event_t *ev)
Igor Sysoevd581fd52003-05-13 16:02:32 +00001543{
Igor Sysoev899b44e2005-05-12 14:58:06 +00001544 ngx_connection_t *c;
1545 ngx_http_request_t *r;
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00001546 ngx_http_log_ctx_t *ctx;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001547
1548 c = ev->data;
1549 r = c->data;
1550
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00001551 ctx = c->log->data;
1552 ctx->current_request = r;
1553
Igor Sysoev899b44e2005-05-12 14:58:06 +00001554 if (ev->write) {
1555 r->write_event_handler(r);
1556
1557 } else {
1558 r->read_event_handler(r);
1559 }
1560}
1561
1562
1563void
1564ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1565{
Igor Sysoevae0347a2007-08-27 15:53:00 +00001566 ngx_connection_t *c;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001567 ngx_http_request_t *pr;
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00001568 ngx_http_log_ctx_t *ctx;
Igor Sysoevdc867cd2003-12-14 20:10:27 +00001569 ngx_http_core_loc_conf_t *clcf;
1570
Igor Sysoev899b44e2005-05-12 14:58:06 +00001571 if (rc == NGX_DONE) {
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001572 /* the request pool may be already destroyed */
Igor Sysoev1342d9c2003-10-09 07:00:45 +00001573 return;
1574 }
1575
Igor Sysoevae0347a2007-08-27 15:53:00 +00001576 c = r->connection;
1577
1578 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001579 "http finalize request: %d, \"%V?%V\"",
1580 rc, &r->uri, &r->args);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001581
Igor Sysoevda173ab2006-08-30 10:39:17 +00001582 if (rc == NGX_DECLINED) {
1583 r->content_handler = NULL;
1584 r->write_event_handler = ngx_http_core_run_phases;
1585 ngx_http_core_run_phases(r);
1586 return;
1587 }
1588
Igor Sysoev960100e2006-10-13 15:20:10 +00001589 if (r != r->main && r->post_subrequest) {
1590 rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
Igor Sysoev8f125582006-07-28 15:16:17 +00001591 }
1592
Igor Sysoeve87afba2007-12-27 13:13:34 +00001593 if (rc == NGX_ERROR
1594 || rc == NGX_HTTP_REQUEST_TIME_OUT
1595 || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
1596 || c->error)
1597 {
Igor Sysoev8b57df92007-01-29 11:53:23 +00001598 if (rc > 0 && r->headers_out.status == 0) {
1599 r->headers_out.status = rc;
Igor Sysoev7f7846d2006-04-26 09:52:47 +00001600 }
1601
Igor Sysoevc2807ec2006-02-16 15:26:46 +00001602 if (ngx_http_post_action(r) == NGX_OK) {
1603 return;
1604 }
1605
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001606 ngx_http_close_request(r, 0);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001607 return;
1608 }
1609
Igor Sysoev7bdb7202006-04-19 15:30:56 +00001610 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
1611 || rc == NGX_HTTP_CREATED
1612 || rc == NGX_HTTP_NO_CONTENT)
1613 {
Igor Sysoevcdc46302005-12-07 14:51:31 +00001614 if (rc == NGX_HTTP_CLOSE) {
1615 ngx_http_close_request(r, rc);
1616 return;
1617 }
1618
Igor Sysoevdf3254a2006-01-11 15:26:57 +00001619 if (r == r->main) {
Igor Sysoevae0347a2007-08-27 15:53:00 +00001620 if (c->read->timer_set) {
1621 ngx_del_timer(c->read);
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001622 }
1623
Igor Sysoevae0347a2007-08-27 15:53:00 +00001624 if (c->write->timer_set) {
1625 ngx_del_timer(c->write);
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001626 }
1627 }
1628
1629 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001630 return;
1631 }
1632
Igor Sysoevdf3254a2006-01-11 15:26:57 +00001633 if (r != r->main || rc == NGX_AGAIN) {
Igor Sysoev85ef94b2005-06-23 13:41:06 +00001634 if (ngx_http_set_write_handler(r) != NGX_OK) {
1635 return;
1636 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00001637 }
1638
Igor Sysoeva936b272007-12-27 14:39:05 +00001639 r->done = 1;
1640
Igor Sysoevae0347a2007-08-27 15:53:00 +00001641 if (r != c->data) {
1642 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001643 "http finalize non-active request: \"%V?%V\"",
1644 &r->uri, &r->args);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001645 return;
1646 }
1647
Igor Sysoevdf3254a2006-01-11 15:26:57 +00001648 if (r != r->main) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00001649
1650 pr = r->parent;
1651
Igor Sysoevae0347a2007-08-27 15:53:00 +00001652 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001653 "http parent request: \"%V?%V\"", &pr->uri, &pr->args);
Igor Sysoev5192b362005-07-08 14:34:20 +00001654
Igor Sysoev899b44e2005-05-12 14:58:06 +00001655 if (rc != NGX_AGAIN) {
Igor Sysoevae0347a2007-08-27 15:53:00 +00001656 c->data = pr;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001657 }
1658
Igor Sysoevae0347a2007-08-27 15:53:00 +00001659 ctx = c->log->data;
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00001660 ctx->current_request = pr;
1661
Igor Sysoev899b44e2005-05-12 14:58:06 +00001662 if (pr->postponed) {
1663
Igor Sysoevae0347a2007-08-27 15:53:00 +00001664 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001665 "http request: \"%V?%V\" has postponed",
1666 &pr->uri, &pr->args);
Igor Sysoev5192b362005-07-08 14:34:20 +00001667
Igor Sysoev899b44e2005-05-12 14:58:06 +00001668 if (rc != NGX_AGAIN && pr->postponed->request == r) {
1669 pr->postponed = pr->postponed->next;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001670 }
1671
Igor Sysoevdf3254a2006-01-11 15:26:57 +00001672 if (r->fast_subrequest) {
Igor Sysoevac72bd12006-05-04 15:32:46 +00001673
1674 if (rc == NGX_AGAIN) {
1675 r->fast_subrequest = 0;
1676 }
1677
Igor Sysoevae0347a2007-08-27 15:53:00 +00001678 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoev12dcb352007-12-25 15:31:18 +00001679 "http fast subrequest: \"%V?%V\" done",
1680 &r->uri, &r->args);
Igor Sysoevdf3254a2006-01-11 15:26:57 +00001681 return;
1682 }
1683
Igor Sysoevac72bd12006-05-04 15:32:46 +00001684 if (rc != NGX_AGAIN) {
Igor Sysoevae0347a2007-08-27 15:53:00 +00001685 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoevac72bd12006-05-04 15:32:46 +00001686 "http wake parent request: \"%V?%V\"",
1687 &pr->uri, &pr->args);
Igor Sysoevdf3254a2006-01-11 15:26:57 +00001688
Igor Sysoevac72bd12006-05-04 15:32:46 +00001689 pr->write_event_handler(pr);
1690 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00001691 }
1692
1693 return;
1694 }
Igor Sysoev10fc9ef2003-10-27 08:53:49 +00001695
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001696 if (rc == NGX_AGAIN) {
Igor Sysoev1342d9c2003-10-09 07:00:45 +00001697 return;
Igor Sysoev5192b362005-07-08 14:34:20 +00001698 }
Igor Sysoev1342d9c2003-10-09 07:00:45 +00001699
Igor Sysoevae0347a2007-08-27 15:53:00 +00001700 if (c->buffered) {
Igor Sysoev85ef94b2005-06-23 13:41:06 +00001701 (void) ngx_http_set_write_handler(r);
Igor Sysoevb3e73d82003-10-10 15:10:50 +00001702 return;
Igor Sysoevd581fd52003-05-13 16:02:32 +00001703 }
1704
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00001705 if (!r->post_action) {
1706 r->request_complete = 1;
1707 }
1708
Igor Sysoevc2807ec2006-02-16 15:26:46 +00001709 if (ngx_http_post_action(r) == NGX_OK) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001710 return;
1711 }
1712
Igor Sysoevae0347a2007-08-27 15:53:00 +00001713 if (c->read->timer_set) {
1714 ngx_del_timer(c->read);
Igor Sysoevd581fd52003-05-13 16:02:32 +00001715 }
1716
Igor Sysoevae0347a2007-08-27 15:53:00 +00001717 if (c->write->timer_set) {
1718 c->write->delayed = 0;
1719 ngx_del_timer(c->write);
Igor Sysoevd581fd52003-05-13 16:02:32 +00001720 }
1721
Igor Sysoevae0347a2007-08-27 15:53:00 +00001722 if (c->destroyed) {
Igor Sysoev24025022005-12-16 15:07:08 +00001723 return;
1724 }
1725
Igor Sysoevae0347a2007-08-27 15:53:00 +00001726 if (c->read->eof) {
Igor Sysoevdb7b1262007-08-27 15:40:19 +00001727 ngx_http_close_request(r, 0);
1728 return;
1729 }
1730
Igor Sysoevc2807ec2006-02-16 15:26:46 +00001731 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
Igor Sysoev3c3ca172004-01-05 20:55:48 +00001732
Igor Sysoevc5991982004-01-16 06:15:48 +00001733 if (!ngx_terminate
Igor Sysoev3f4685f2004-04-25 20:13:21 +00001734 && !ngx_exiting
Igor Sysoevc5991982004-01-16 06:15:48 +00001735 && r->keepalive != 0
1736 && clcf->keepalive_timeout > 0)
1737 {
Igor Sysoevd581fd52003-05-13 16:02:32 +00001738 ngx_http_set_keepalive(r);
Igor Sysoevc5991982004-01-16 06:15:48 +00001739 return;
Igor Sysoevd581fd52003-05-13 16:02:32 +00001740
Igor Sysoevdc867cd2003-12-14 20:10:27 +00001741 } else if (r->lingering_close && clcf->lingering_timeout > 0) {
Igor Sysoevd581fd52003-05-13 16:02:32 +00001742 ngx_http_set_lingering_close(r);
Igor Sysoevc5991982004-01-16 06:15:48 +00001743 return;
Igor Sysoevd581fd52003-05-13 16:02:32 +00001744 }
Igor Sysoev1342d9c2003-10-09 07:00:45 +00001745
Igor Sysoevc5991982004-01-16 06:15:48 +00001746 ngx_http_close_request(r, 0);
Igor Sysoevd581fd52003-05-13 16:02:32 +00001747}
1748
1749
Igor Sysoev85ef94b2005-06-23 13:41:06 +00001750static ngx_int_t
Igor Sysoevc1571722005-03-19 12:38:37 +00001751ngx_http_set_write_handler(ngx_http_request_t *r)
Igor Sysoevd581fd52003-05-13 16:02:32 +00001752{
Igor Sysoevd581fd52003-05-13 16:02:32 +00001753 ngx_event_t *wev;
Igor Sysoeva9830112003-05-19 16:39:14 +00001754 ngx_http_core_loc_conf_t *clcf;
Igor Sysoevd581fd52003-05-13 16:02:32 +00001755
Igor Sysoevb1af9bb2004-06-25 14:42:03 +00001756 r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
1757
Igor Sysoev7ab4d9c2007-08-06 15:22:40 +00001758 r->read_event_handler = ngx_http_test_reading;
Igor Sysoev85ef94b2005-06-23 13:41:06 +00001759 r->write_event_handler = ngx_http_writer;
1760
Igor Sysoev899b44e2005-05-12 14:58:06 +00001761 wev = r->connection->write;
1762
Igor Sysoevef066482004-06-21 15:59:32 +00001763 if (wev->ready && wev->delayed) {
Igor Sysoev85ef94b2005-06-23 13:41:06 +00001764 return NGX_OK;
Igor Sysoevd581fd52003-05-13 16:02:32 +00001765 }
1766
Igor Sysoev899b44e2005-05-12 14:58:06 +00001767 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
Igor Sysoevef066482004-06-21 15:59:32 +00001768 if (!wev->delayed) {
Igor Sysoev73a73b52004-06-20 19:54:15 +00001769 ngx_add_timer(wev, clcf->send_timeout);
1770 }
Igor Sysoevd581fd52003-05-13 16:02:32 +00001771
Igor Sysoev924bd792004-10-11 15:07:03 +00001772 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
Igor Sysoevb3e73d82003-10-10 15:10:50 +00001773 ngx_http_close_request(r, 0);
Igor Sysoev85ef94b2005-06-23 13:41:06 +00001774 return NGX_ERROR;
Igor Sysoevb3e73d82003-10-10 15:10:50 +00001775 }
Igor Sysoev85ef94b2005-06-23 13:41:06 +00001776
1777 return NGX_OK;
Igor Sysoevd581fd52003-05-13 16:02:32 +00001778}
1779
1780
Igor Sysoev899b44e2005-05-12 14:58:06 +00001781static void
1782ngx_http_writer(ngx_http_request_t *r)
Igor Sysoevd581fd52003-05-13 16:02:32 +00001783{
1784 int rc;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001785 ngx_event_t *wev;
Igor Sysoevd581fd52003-05-13 16:02:32 +00001786 ngx_connection_t *c;
Igor Sysoev6253ca12003-05-27 12:18:54 +00001787 ngx_http_core_loc_conf_t *clcf;
Igor Sysoevd581fd52003-05-13 16:02:32 +00001788
Igor Sysoev899b44e2005-05-12 14:58:06 +00001789 c = r->connection;
1790 wev = c->write;
Igor Sysoev160d7742003-11-19 16:26:41 +00001791
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001792 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
1793 "http writer handler: \"%V?%V\"", &r->uri, &r->args);
Igor Sysoevd581fd52003-05-13 16:02:32 +00001794
Igor Sysoev10fc9ef2003-10-27 08:53:49 +00001795 if (wev->timedout) {
Igor Sysoevef066482004-06-21 15:59:32 +00001796 if (!wev->delayed) {
Igor Sysoevc04deca2005-03-28 14:43:02 +00001797 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
1798 "client timed out");
1799 c->timedout = 1;
Igor Sysoev055951d2005-10-21 19:12:18 +00001800
Igor Sysoevc2807ec2006-02-16 15:26:46 +00001801 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
Igor Sysoev73a73b52004-06-20 19:54:15 +00001802 return;
1803 }
1804
1805 wev->timedout = 0;
Igor Sysoevef066482004-06-21 15:59:32 +00001806 wev->delayed = 0;
Igor Sysoev73a73b52004-06-20 19:54:15 +00001807
1808 if (!wev->ready) {
Igor Sysoevf6e1fe32005-10-04 10:38:53 +00001809 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
Igor Sysoev73a73b52004-06-20 19:54:15 +00001810 ngx_add_timer(wev, clcf->send_timeout);
1811
Igor Sysoev924bd792004-10-11 15:07:03 +00001812 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001813 ngx_http_close_request(r, 0);
Igor Sysoev73a73b52004-06-20 19:54:15 +00001814 }
1815
1816 return;
1817 }
1818
1819 } else {
Igor Sysoevef066482004-06-21 15:59:32 +00001820 if (wev->delayed) {
Igor Sysoev73a73b52004-06-20 19:54:15 +00001821 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
1822 "http writer delayed");
Igor Sysoevef066482004-06-21 15:59:32 +00001823
Igor Sysoevf6e1fe32005-10-04 10:38:53 +00001824 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
Igor Sysoevef066482004-06-21 15:59:32 +00001825
Igor Sysoev924bd792004-10-11 15:07:03 +00001826 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001827 ngx_http_close_request(r, 0);
Igor Sysoevef066482004-06-21 15:59:32 +00001828 }
1829
Igor Sysoev73a73b52004-06-20 19:54:15 +00001830 return;
1831 }
Igor Sysoev10fc9ef2003-10-27 08:53:49 +00001832 }
1833
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001834 rc = ngx_http_output_filter(r, NULL);
Igor Sysoevd581fd52003-05-13 16:02:32 +00001835
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001836 if (c->destroyed) {
1837 return;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001838 }
1839
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001840 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
1841 "http writer output filter: %d, \"%V?%V\"",
1842 rc, &r->uri, &r->args);
Igor Sysoevd581fd52003-05-13 16:02:32 +00001843
1844 if (rc == NGX_AGAIN) {
Igor Sysoevf6e1fe32005-10-04 10:38:53 +00001845 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
Igor Sysoevef066482004-06-21 15:59:32 +00001846 if (!wev->ready && !wev->delayed) {
Igor Sysoev0a280a32003-10-12 16:49:16 +00001847 ngx_add_timer(wev, clcf->send_timeout);
Igor Sysoevd581fd52003-05-13 16:02:32 +00001848 }
1849
Igor Sysoev924bd792004-10-11 15:07:03 +00001850 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001851 ngx_http_close_request(r, 0);
Igor Sysoev0a280a32003-10-12 16:49:16 +00001852 }
Igor Sysoevd581fd52003-05-13 16:02:32 +00001853
Igor Sysoev020ffea2006-10-30 20:25:22 +00001854 if (r == r->main || r->buffered) {
Igor Sysoevdf3254a2006-01-11 15:26:57 +00001855 return;
1856 }
1857
1858 rc = NGX_OK;
Igor Sysoevd581fd52003-05-13 16:02:32 +00001859 }
1860
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001861 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
1862 "http writer done: \"%V?%V\"", &r->uri, &r->args);
Igor Sysoevd581fd52003-05-13 16:02:32 +00001863
Igor Sysoev0a280a32003-10-12 16:49:16 +00001864 ngx_http_finalize_request(r, rc);
Igor Sysoevd581fd52003-05-13 16:02:32 +00001865}
1866
1867
Igor Sysoev7ab4d9c2007-08-06 15:22:40 +00001868void
1869ngx_http_block_reading(ngx_http_request_t *r)
Igor Sysoev899b44e2005-05-12 14:58:06 +00001870{
Igor Sysoevf53d0f32007-07-10 20:53:45 +00001871 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev7ab4d9c2007-08-06 15:22:40 +00001872 "http reading blocked");
Igor Sysoevf53d0f32007-07-10 20:53:45 +00001873
1874 /* aio does not call this handler */
1875
1876 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
1877 && r->connection->read->active)
1878 {
1879 if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0)
1880 == NGX_ERROR)
1881 {
1882 ngx_http_close_request(r, 0);
1883 }
1884 }
1885}
1886
1887
1888static void
Igor Sysoev7ab4d9c2007-08-06 15:22:40 +00001889ngx_http_test_reading(ngx_http_request_t *r)
Igor Sysoevf53d0f32007-07-10 20:53:45 +00001890{
Igor Sysoev8d6de942007-01-19 08:10:06 +00001891 int n;
1892 char buf[1];
1893 ngx_err_t err;
1894 ngx_event_t *rev;
1895 ngx_connection_t *c;
1896
1897 c = r->connection;
1898 rev = c->read;
1899
Igor Sysoev7ab4d9c2007-08-06 15:22:40 +00001900 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
Igor Sysoev8d6de942007-01-19 08:10:06 +00001901
1902#if (NGX_HAVE_KQUEUE)
1903
1904 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
1905
1906 if (!rev->pending_eof) {
1907 return;
1908 }
1909
1910 rev->eof = 1;
1911 c->error = 1;
1912 err = rev->kq_errno;
1913
1914 goto closed;
1915 }
1916
1917#endif
1918
1919 n = recv(c->fd, buf, 1, MSG_PEEK);
1920
1921 if (n == 0) {
1922 rev->eof = 1;
1923 c->error = 1;
1924 err = 0;
1925
1926 goto closed;
1927
1928 } else if (n == -1) {
1929 err = ngx_socket_errno;
1930
1931 if (err != NGX_EAGAIN) {
1932 rev->eof = 1;
1933 c->error = 1;
1934
1935 goto closed;
1936 }
1937 }
Igor Sysoev42feecb2002-12-15 06:25:09 +00001938
Igor Sysoev73009772003-02-06 17:21:13 +00001939 /* aio does not call this handler */
1940
Igor Sysoev8d6de942007-01-19 08:10:06 +00001941 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
1942
1943 if (ngx_del_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
Igor Sysoev6ddfbf02003-05-15 15:42:53 +00001944 ngx_http_close_request(r, 0);
Igor Sysoev6ddfbf02003-05-15 15:42:53 +00001945 }
Igor Sysoev73009772003-02-06 17:21:13 +00001946 }
Igor Sysoev8d6de942007-01-19 08:10:06 +00001947
1948 return;
1949
1950closed:
1951
1952 if (err) {
1953 rev->error = 1;
1954 }
1955
1956 ngx_log_error(NGX_LOG_INFO, c->log, err,
1957 "client closed prematurely connection");
1958
Igor Sysoevd74b66c2007-02-15 18:53:48 +00001959 ngx_http_finalize_request(r, 0);
Igor Sysoev42feecb2002-12-15 06:25:09 +00001960}
1961
1962
Igor Sysoevc1571722005-03-19 12:38:37 +00001963static void
1964ngx_http_set_keepalive(ngx_http_request_t *r)
Igor Sysoev1e7ec9d2003-02-11 07:14:40 +00001965{
Igor Sysoev924bd792004-10-11 15:07:03 +00001966 int tcp_nodelay;
Igor Sysoevf7abd722004-09-23 06:32:00 +00001967 ngx_int_t i;
Igor Sysoevf7abd722004-09-23 06:32:00 +00001968 ngx_buf_t *b, *f;
Igor Sysoev187fcd82003-05-23 11:53:01 +00001969 ngx_event_t *rev, *wev;
1970 ngx_connection_t *c;
Igor Sysoevdd888c42004-09-21 05:38:28 +00001971 ngx_http_connection_t *hc;
Igor Sysoev187fcd82003-05-23 11:53:01 +00001972 ngx_http_core_srv_conf_t *cscf;
1973 ngx_http_core_loc_conf_t *clcf;
Igor Sysoev1e7ec9d2003-02-11 07:14:40 +00001974
Igor Sysoev6253ca12003-05-27 12:18:54 +00001975 c = r->connection;
Igor Sysoev3d062ad2003-03-05 06:37:42 +00001976 rev = c->read;
Igor Sysoev1e7ec9d2003-02-11 07:14:40 +00001977
Igor Sysoev4fbd8682007-08-07 10:53:27 +00001978 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1979
Igor Sysoeve04084c2004-01-26 08:52:49 +00001980 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
Igor Sysoev6a1cc902003-05-22 15:23:47 +00001981
Igor Sysoev4fbd8682007-08-07 10:53:27 +00001982 if (r->discard_body) {
1983 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
1984 ngx_add_timer(rev, clcf->lingering_timeout);
1985 return;
1986 }
1987
Igor Sysoeve5a222c2005-01-25 12:27:35 +00001988 c->log->action = "closing request";
Igor Sysoevdd888c42004-09-21 05:38:28 +00001989
1990 hc = r->http_connection;
Igor Sysoevf7abd722004-09-23 06:32:00 +00001991 b = r->header_in;
1992
Igor Sysoev236e0452004-09-23 16:39:34 +00001993 if (b->pos < b->last) {
Igor Sysoevf7abd722004-09-23 06:32:00 +00001994
Igor Sysoev236e0452004-09-23 16:39:34 +00001995 /* the pipelined request */
Igor Sysoevf7abd722004-09-23 06:32:00 +00001996
1997 if (b != c->buffer) {
1998
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00001999 /*
2000 * If the large header buffers were allocated while the previous
2001 * request processing then we do not use c->buffer for
2002 * the pipelined request (see ngx_http_init_request()).
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002003 *
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00002004 * Now we would move the large header buffers to the free list.
2005 */
Igor Sysoevf7abd722004-09-23 06:32:00 +00002006
2007 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2008
2009 if (hc->free == NULL) {
2010 hc->free = ngx_palloc(c->pool,
2011 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
Igor Sysoev236e0452004-09-23 16:39:34 +00002012
Igor Sysoevf7abd722004-09-23 06:32:00 +00002013 if (hc->free == NULL) {
Igor Sysoevb1dfe472004-12-21 12:30:30 +00002014 ngx_http_close_request(r, 0);
Igor Sysoevf7abd722004-09-23 06:32:00 +00002015 return;
2016 }
2017 }
2018
2019 for (i = 0; i < hc->nbusy - 1; i++) {
2020 f = hc->busy[i];
2021 hc->free[hc->nfree++] = f;
2022 f->pos = f->start;
2023 f->last = f->start;
2024 }
2025
2026 hc->busy[0] = b;
2027 hc->nbusy = 1;
2028 }
2029 }
2030
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002031 ngx_http_request_done(r, 0);
2032
Igor Sysoevdd888c42004-09-21 05:38:28 +00002033 c->data = hc;
Igor Sysoev1e7ec9d2003-02-11 07:14:40 +00002034
Igor Sysoevfa73aac2003-05-21 13:28:21 +00002035 ngx_add_timer(rev, clcf->keepalive_timeout);
2036
Igor Sysoev899b44e2005-05-12 14:58:06 +00002037 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
Igor Sysoev0a280a32003-10-12 16:49:16 +00002038 ngx_http_close_connection(c);
2039 return;
Igor Sysoevb7387572003-03-11 20:38:13 +00002040 }
2041
Igor Sysoev7b6062a2004-02-12 20:57:10 +00002042 wev = c->write;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002043 wev->handler = ngx_http_empty_handler;
Igor Sysoev3d062ad2003-03-05 06:37:42 +00002044
Igor Sysoev369145c2004-05-28 15:49:23 +00002045 if (b->pos < b->last) {
Igor Sysoev3d062ad2003-03-05 06:37:42 +00002046
Igor Sysoeve04084c2004-01-26 08:52:49 +00002047 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
Igor Sysoev6253ca12003-05-27 12:18:54 +00002048
Igor Sysoev055951d2005-10-21 19:12:18 +00002049#if (NGX_STAT_STUB)
2050 ngx_atomic_fetch_add(ngx_stat_reading, 1);
2051#endif
2052
Igor Sysoevdd888c42004-09-21 05:38:28 +00002053 hc->pipeline = 1;
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002054 c->log->action = "reading client pipelined request line";
Igor Sysoev055951d2005-10-21 19:12:18 +00002055
Igor Sysoevd581fd52003-05-13 16:02:32 +00002056 ngx_http_init_request(rev);
2057 return;
Igor Sysoev3d062ad2003-03-05 06:37:42 +00002058 }
2059
Igor Sysoevdd888c42004-09-21 05:38:28 +00002060 hc->pipeline = 0;
Igor Sysoev3d062ad2003-03-05 06:37:42 +00002061
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00002062 /*
2063 * To keep a memory footprint as small as possible for an idle
2064 * keepalive connection we try to free the ngx_http_request_t and
2065 * c->buffer's memory if they were allocated outside the c->pool.
2066 * The large header buffers are always allocated outside the c->pool and
2067 * are freed too.
2068 */
2069
Igor Sysoev236e0452004-09-23 16:39:34 +00002070 if (ngx_pfree(c->pool, r) == NGX_OK) {
2071 hc->request = NULL;
2072 }
Igor Sysoevf7abd722004-09-23 06:32:00 +00002073
Igor Sysoev236e0452004-09-23 16:39:34 +00002074 b = c->buffer;
Igor Sysoevf7abd722004-09-23 06:32:00 +00002075
Igor Sysoev236e0452004-09-23 16:39:34 +00002076 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00002077
2078 /*
2079 * the special note for ngx_http_keepalive_handler() that
2080 * c->buffer's memory was freed
2081 */
2082
Igor Sysoev236e0452004-09-23 16:39:34 +00002083 b->pos = NULL;
2084
2085 } else {
2086 b->pos = b->start;
2087 b->last = b->start;
2088 }
2089
Igor Sysoev1b735832004-11-11 14:07:14 +00002090 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
Igor Sysoev236e0452004-09-23 16:39:34 +00002091 hc->free, hc->nfree);
2092
2093 if (hc->free) {
2094 for (i = 0; i < hc->nfree; i++) {
Igor Sysoevacb2f5d2007-12-12 15:42:39 +00002095 ngx_pfree(c->pool, hc->free[i]->start);
Igor Sysoev236e0452004-09-23 16:39:34 +00002096 hc->free[i] = NULL;
Igor Sysoevf7abd722004-09-23 06:32:00 +00002097 }
2098
Igor Sysoev236e0452004-09-23 16:39:34 +00002099 hc->nfree = 0;
2100 }
2101
Igor Sysoev1b735832004-11-11 14:07:14 +00002102 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
Igor Sysoev236e0452004-09-23 16:39:34 +00002103 hc->busy, hc->nbusy);
2104
2105 if (hc->busy) {
2106 for (i = 0; i < hc->nbusy; i++) {
Igor Sysoevacb2f5d2007-12-12 15:42:39 +00002107 ngx_pfree(c->pool, hc->busy[i]->start);
Igor Sysoev236e0452004-09-23 16:39:34 +00002108 hc->busy[i] = NULL;
Igor Sysoevf7abd722004-09-23 06:32:00 +00002109 }
2110
Igor Sysoev236e0452004-09-23 16:39:34 +00002111 hc->nbusy = 0;
Igor Sysoevf7abd722004-09-23 06:32:00 +00002112 }
2113
Igor Sysoevcd2aa8e2007-12-26 21:07:30 +00002114#if (NGX_HTTP_SSL)
2115 if (c->ssl) {
2116 ngx_ssl_free_buffer(c);
2117 }
2118#endif
2119
Igor Sysoev899b44e2005-05-12 14:58:06 +00002120 rev->handler = ngx_http_keepalive_handler;
Igor Sysoev3d062ad2003-03-05 06:37:42 +00002121
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002122 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2123 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
2124 ngx_http_close_connection(c);
2125 return;
Igor Sysoev3d062ad2003-03-05 06:37:42 +00002126 }
2127 }
2128
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002129 c->log->action = "keepalive";
Igor Sysoev6a644c62003-03-04 06:33:48 +00002130
Igor Sysoev67f450d2004-06-01 06:04:46 +00002131 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
Igor Sysoevc1571722005-03-19 12:38:37 +00002132 if (ngx_tcp_push(c->fd) == -1) {
Igor Sysoev4cec79f2004-04-28 06:14:50 +00002133 ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
2134 ngx_http_close_connection(c);
2135 return;
Igor Sysoev9cf78302003-06-04 17:28:33 +00002136 }
Igor Sysoev924bd792004-10-11 15:07:03 +00002137
Igor Sysoev67f450d2004-06-01 06:04:46 +00002138 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
Igor Sysoev42b12b32004-12-02 18:40:46 +00002139 tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
Igor Sysoev924bd792004-10-11 15:07:03 +00002140
2141 } else {
Igor Sysoev42b12b32004-12-02 18:40:46 +00002142 tcp_nodelay = 1;
2143 }
Igor Sysoev924bd792004-10-11 15:07:03 +00002144
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002145 if (tcp_nodelay
2146 && clcf->tcp_nodelay
2147 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
2148 {
Igor Sysoev42b12b32004-12-02 18:40:46 +00002149 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
Igor Sysoev924bd792004-10-11 15:07:03 +00002150
Igor Sysoev42b12b32004-12-02 18:40:46 +00002151 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
Igor Sysoevcdf609a2006-04-17 19:55:41 +00002152 (const void *) &tcp_nodelay, sizeof(int))
2153 == -1)
Igor Sysoev42b12b32004-12-02 18:40:46 +00002154 {
2155 ngx_connection_error(c, ngx_socket_errno,
2156 "setsockopt(TCP_NODELAY) failed");
2157 ngx_http_close_connection(c);
2158 return;
Igor Sysoev924bd792004-10-11 15:07:03 +00002159 }
Igor Sysoev42b12b32004-12-02 18:40:46 +00002160
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002161 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
Igor Sysoev9cf78302003-06-04 17:28:33 +00002162 }
2163
Igor Sysoevf7abd722004-09-23 06:32:00 +00002164#if 0
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00002165 /* if ngx_http_request_t was freed then we need some other place */
Igor Sysoevc83f6832004-06-24 07:53:37 +00002166 r->http_state = NGX_HTTP_KEEPALIVE_STATE;
Igor Sysoevf7abd722004-09-23 06:32:00 +00002167#endif
Igor Sysoevc83f6832004-06-24 07:53:37 +00002168
Igor Sysoev4ddeff42007-03-19 13:20:15 +00002169 c->idle = 1;
2170
Igor Sysoevb5faed22003-10-29 08:30:44 +00002171 if (rev->ready) {
Igor Sysoevd581fd52003-05-13 16:02:32 +00002172 ngx_http_keepalive_handler(rev);
Igor Sysoevb7387572003-03-11 20:38:13 +00002173 }
Igor Sysoev1e7ec9d2003-02-11 07:14:40 +00002174}
2175
2176
Igor Sysoevc1571722005-03-19 12:38:37 +00002177static void
2178ngx_http_keepalive_handler(ngx_event_t *rev)
Igor Sysoev0ad17c02002-08-26 15:18:19 +00002179{
Igor Sysoevc1571722005-03-19 12:38:37 +00002180 size_t size;
2181 ssize_t n;
2182 ngx_buf_t *b;
2183 ngx_connection_t *c;
Igor Sysoev0ad17c02002-08-26 15:18:19 +00002184
Igor Sysoev0ad25372004-07-16 06:33:35 +00002185 c = rev->data;
Igor Sysoev682bf8e2002-09-16 15:01:44 +00002186
Igor Sysoeve04084c2004-01-26 08:52:49 +00002187 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
Igor Sysoev1af7c822002-09-13 14:47:42 +00002188
Igor Sysoev4ddeff42007-03-19 13:20:15 +00002189 if (rev->timedout || c->close) {
Igor Sysoevd581fd52003-05-13 16:02:32 +00002190 ngx_http_close_connection(c);
2191 return;
Igor Sysoev90ace682003-03-12 17:32:22 +00002192 }
Igor Sysoev1af7c822002-09-13 14:47:42 +00002193
Igor Sysoevf6906042004-11-25 16:17:31 +00002194#if (NGX_HAVE_KQUEUE)
Igor Sysoev236e0452004-09-23 16:39:34 +00002195
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00002196 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
Igor Sysoevcdc46302005-12-07 14:51:31 +00002197 if (rev->pending_eof) {
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002198 c->log->handler = NULL;
Igor Sysoev236e0452004-09-23 16:39:34 +00002199 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
Igor Sysoev1b735832004-11-11 14:07:14 +00002200 "kevent() reported that client %V closed "
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002201 "keepalive connection", &c->addr_text);
Igor Sysoev42b12b32004-12-02 18:40:46 +00002202#if (NGX_HTTP_SSL)
2203 if (c->ssl) {
Igor Sysoev9fa5a822005-09-30 14:41:25 +00002204 c->ssl->no_send_shutdown = 1;
Igor Sysoev42b12b32004-12-02 18:40:46 +00002205 }
2206#endif
Igor Sysoev236e0452004-09-23 16:39:34 +00002207 ngx_http_close_connection(c);
2208 return;
2209 }
2210 }
2211
2212#endif
2213
Igor Sysoev236e0452004-09-23 16:39:34 +00002214 b = c->buffer;
2215 size = b->end - b->start;
2216
2217 if (b->pos == NULL) {
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00002218
2219 /*
2220 * The c->buffer's memory was freed by ngx_http_set_keepalive().
2221 * However, the c->buffer->start and c->buffer->end were not changed
2222 * to keep the buffer size.
2223 */
2224
Igor Sysoevc1571722005-03-19 12:38:37 +00002225 b->pos = ngx_palloc(c->pool, size);
2226 if (b->pos == NULL) {
Igor Sysoev236e0452004-09-23 16:39:34 +00002227 ngx_http_close_connection(c);
2228 return;
2229 }
2230
2231 b->start = b->pos;
2232 b->last = b->pos;
2233 b->end = b->pos + size;
2234 }
Igor Sysoevf7abd722004-09-23 06:32:00 +00002235
Igor Sysoev0a280a32003-10-12 16:49:16 +00002236 /*
2237 * MSIE closes a keepalive connection with RST flag
2238 * so we ignore ECONNRESET here.
2239 */
Igor Sysoevb7387572003-03-11 20:38:13 +00002240
Igor Sysoevc7a2f682004-02-10 16:23:38 +00002241 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
Igor Sysoevb7387572003-03-11 20:38:13 +00002242 ngx_set_socket_errno(0);
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00002243
Igor Sysoev236e0452004-09-23 16:39:34 +00002244 n = c->recv(c, b->last, size);
Igor Sysoevc7a2f682004-02-10 16:23:38 +00002245 c->log_error = NGX_ERROR_INFO;
Igor Sysoev0ad17c02002-08-26 15:18:19 +00002246
Igor Sysoevd581fd52003-05-13 16:02:32 +00002247 if (n == NGX_AGAIN) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00002248 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
Igor Sysoev6a12fc92004-12-06 14:45:08 +00002249 ngx_http_close_connection(c);
2250 }
2251
Igor Sysoevd581fd52003-05-13 16:02:32 +00002252 return;
2253 }
2254
2255 if (n == NGX_ERROR) {
2256 ngx_http_close_connection(c);
2257 return;
Igor Sysoev90ace682003-03-12 17:32:22 +00002258 }
Igor Sysoev0ad17c02002-08-26 15:18:19 +00002259
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002260 c->log->handler = NULL;
Igor Sysoev682bf8e2002-09-16 15:01:44 +00002261
2262 if (n == 0) {
Igor Sysoevdc479b42003-03-20 16:09:44 +00002263 ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002264 "client %V closed keepalive connection", &c->addr_text);
Igor Sysoevd581fd52003-05-13 16:02:32 +00002265 ngx_http_close_connection(c);
2266 return;
Igor Sysoev682bf8e2002-09-16 15:01:44 +00002267 }
2268
Igor Sysoevf7abd722004-09-23 06:32:00 +00002269 b->last += n;
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002270
Igor Sysoev055951d2005-10-21 19:12:18 +00002271#if (NGX_STAT_STUB)
2272 ngx_atomic_fetch_add(ngx_stat_reading, 1);
2273#endif
2274
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002275 c->log->handler = ngx_http_log_error;
2276 c->log->action = "reading client request line";
Igor Sysoev0ad17c02002-08-26 15:18:19 +00002277
Igor Sysoev4ddeff42007-03-19 13:20:15 +00002278 c->idle = 0;
2279
Igor Sysoevd581fd52003-05-13 16:02:32 +00002280 ngx_http_init_request(rev);
Igor Sysoev0ad17c02002-08-26 15:18:19 +00002281}
2282
Igor Sysoev0ad17c02002-08-26 15:18:19 +00002283
Igor Sysoevc1571722005-03-19 12:38:37 +00002284static void
2285ngx_http_set_lingering_close(ngx_http_request_t *r)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002286{
Igor Sysoev0a280a32003-10-12 16:49:16 +00002287 ngx_event_t *rev, *wev;
Igor Sysoevb7387572003-03-11 20:38:13 +00002288 ngx_connection_t *c;
Igor Sysoev6253ca12003-05-27 12:18:54 +00002289 ngx_http_core_loc_conf_t *clcf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00002290
Igor Sysoev1e7ec9d2003-02-11 07:14:40 +00002291 c = r->connection;
Igor Sysoev9b25d692003-01-26 21:08:14 +00002292
Igor Sysoev6253ca12003-05-27 12:18:54 +00002293 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
Igor Sysoev4e9393a2003-01-09 05:36:00 +00002294
Igor Sysoev0a280a32003-10-12 16:49:16 +00002295 rev = c->read;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002296 rev->handler = ngx_http_lingering_close_handler;
Igor Sysoev42feecb2002-12-15 06:25:09 +00002297
Igor Sysoev208eed22005-10-07 13:30:52 +00002298 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
Igor Sysoev6253ca12003-05-27 12:18:54 +00002299 ngx_add_timer(rev, clcf->lingering_timeout);
Igor Sysoev9b25d692003-01-26 21:08:14 +00002300
Igor Sysoev899b44e2005-05-12 14:58:06 +00002301 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
Igor Sysoev0a280a32003-10-12 16:49:16 +00002302 ngx_http_close_request(r, 0);
Igor Sysoev0a280a32003-10-12 16:49:16 +00002303 return;
2304 }
2305
2306 wev = c->write;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002307 wev->handler = ngx_http_empty_handler;
Igor Sysoev0a280a32003-10-12 16:49:16 +00002308
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002309 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2310 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
2311 ngx_http_close_request(r, 0);
2312 return;
Igor Sysoevb7387572003-03-11 20:38:13 +00002313 }
Igor Sysoev42feecb2002-12-15 06:25:09 +00002314 }
2315
Igor Sysoevb7387572003-03-11 20:38:13 +00002316 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
Igor Sysoev4cec79f2004-04-28 06:14:50 +00002317 ngx_connection_error(c, ngx_socket_errno,
2318 ngx_shutdown_socket_n " failed");
Igor Sysoevd581fd52003-05-13 16:02:32 +00002319 ngx_http_close_request(r, 0);
Igor Sysoevd581fd52003-05-13 16:02:32 +00002320 return;
Igor Sysoev42feecb2002-12-15 06:25:09 +00002321 }
2322
Igor Sysoevb5faed22003-10-29 08:30:44 +00002323 if (rev->ready) {
Igor Sysoevd581fd52003-05-13 16:02:32 +00002324 ngx_http_lingering_close_handler(rev);
Igor Sysoevb7387572003-03-11 20:38:13 +00002325 }
Igor Sysoev42feecb2002-12-15 06:25:09 +00002326}
2327
2328
Igor Sysoevc1571722005-03-19 12:38:37 +00002329static void
2330ngx_http_lingering_close_handler(ngx_event_t *rev)
Igor Sysoev42feecb2002-12-15 06:25:09 +00002331{
Igor Sysoevdc479b42003-03-20 16:09:44 +00002332 ssize_t n;
2333 ngx_msec_t timer;
2334 ngx_connection_t *c;
2335 ngx_http_request_t *r;
Igor Sysoev6253ca12003-05-27 12:18:54 +00002336 ngx_http_core_loc_conf_t *clcf;
Igor Sysoevf7abd722004-09-23 06:32:00 +00002337 u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
Igor Sysoev1af7c822002-09-13 14:47:42 +00002338
Igor Sysoev6253ca12003-05-27 12:18:54 +00002339 c = rev->data;
2340 r = c->data;
Igor Sysoev86de4cb2003-01-30 07:28:09 +00002341
Igor Sysoeve04084c2004-01-26 08:52:49 +00002342 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2343 "http lingering close handler");
Igor Sysoevdc479b42003-03-20 16:09:44 +00002344
2345 if (rev->timedout) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002346 c->timedout = 1;
Igor Sysoevd581fd52003-05-13 16:02:32 +00002347 ngx_http_close_request(r, 0);
Igor Sysoevd581fd52003-05-13 16:02:32 +00002348 return;
Igor Sysoev0d2bda52002-12-24 07:09:57 +00002349 }
Igor Sysoev1af7c822002-09-13 14:47:42 +00002350
Igor Sysoev1d04b142007-11-15 14:26:36 +00002351 timer = (ngx_msec_t) (r->lingering_time - ngx_time());
Igor Sysoev0d2bda52002-12-24 07:09:57 +00002352 if (timer <= 0) {
Igor Sysoevd581fd52003-05-13 16:02:32 +00002353 ngx_http_close_request(r, 0);
Igor Sysoevd581fd52003-05-13 16:02:32 +00002354 return;
Igor Sysoev0d2bda52002-12-24 07:09:57 +00002355 }
Igor Sysoev1af7c822002-09-13 14:47:42 +00002356
Igor Sysoevdc479b42003-03-20 16:09:44 +00002357 do {
Igor Sysoevf7abd722004-09-23 06:32:00 +00002358 n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
Igor Sysoev1af7c822002-09-13 14:47:42 +00002359
Igor Sysoeve04084c2004-01-26 08:52:49 +00002360 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
Igor Sysoev1af7c822002-09-13 14:47:42 +00002361
Igor Sysoevdc479b42003-03-20 16:09:44 +00002362 if (n == NGX_ERROR || n == 0) {
Igor Sysoevd581fd52003-05-13 16:02:32 +00002363 ngx_http_close_request(r, 0);
Igor Sysoevd581fd52003-05-13 16:02:32 +00002364 return;
Igor Sysoevdc479b42003-03-20 16:09:44 +00002365 }
2366
2367 } while (rev->ready);
Igor Sysoev1af7c822002-09-13 14:47:42 +00002368
Igor Sysoev899b44e2005-05-12 14:58:06 +00002369 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
Igor Sysoevb1dfe472004-12-21 12:30:30 +00002370 ngx_http_close_request(r, 0);
Igor Sysoev6a12fc92004-12-06 14:45:08 +00002371 return;
2372 }
2373
Igor Sysoevf7abd722004-09-23 06:32:00 +00002374 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2375
Igor Sysoev682bf8e2002-09-16 15:01:44 +00002376 timer *= 1000;
Igor Sysoevf7abd722004-09-23 06:32:00 +00002377
Igor Sysoev6253ca12003-05-27 12:18:54 +00002378 if (timer > clcf->lingering_timeout) {
2379 timer = clcf->lingering_timeout;
Igor Sysoev0d2bda52002-12-24 07:09:57 +00002380 }
Igor Sysoev1af7c822002-09-13 14:47:42 +00002381
Igor Sysoevdc479b42003-03-20 16:09:44 +00002382 ngx_add_timer(rev, timer);
Igor Sysoev1af7c822002-09-13 14:47:42 +00002383}
2384
Igor Sysoev2b542382002-08-20 14:48:28 +00002385
Igor Sysoevc1571722005-03-19 12:38:37 +00002386void
2387ngx_http_empty_handler(ngx_event_t *wev)
Igor Sysoev6a1cc902003-05-22 15:23:47 +00002388{
Igor Sysoeve04084c2004-01-26 08:52:49 +00002389 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
Igor Sysoev6a1cc902003-05-22 15:23:47 +00002390
2391 return;
2392}
2393
2394
Igor Sysoev899b44e2005-05-12 14:58:06 +00002395void
2396ngx_http_request_empty_handler(ngx_http_request_t *r)
2397{
2398 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002399 "http request empty handler");
Igor Sysoev899b44e2005-05-12 14:58:06 +00002400
2401 return;
2402}
2403
2404
Igor Sysoevc1571722005-03-19 12:38:37 +00002405ngx_int_t
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002406ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
Igor Sysoev1342d9c2003-10-09 07:00:45 +00002407{
Igor Sysoev369145c2004-05-28 15:49:23 +00002408 ngx_buf_t *b;
Igor Sysoev419f9ac2003-10-21 16:49:56 +00002409 ngx_chain_t out;
Igor Sysoev1342d9c2003-10-09 07:00:45 +00002410
Igor Sysoevc1571722005-03-19 12:38:37 +00002411 b = ngx_calloc_buf(r->pool);
2412 if (b == NULL) {
Igor Sysoev369145c2004-05-28 15:49:23 +00002413 return NGX_ERROR;
2414 }
2415
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002416 if (flags & NGX_HTTP_LAST) {
2417 b->last_buf = 1;
2418 }
2419
2420 if (flags & NGX_HTTP_FLUSH) {
2421 b->flush = 1;
2422 }
2423
Igor Sysoev369145c2004-05-28 15:49:23 +00002424 out.buf = b;
Igor Sysoev419f9ac2003-10-21 16:49:56 +00002425 out.next = NULL;
Igor Sysoev1342d9c2003-10-09 07:00:45 +00002426
Igor Sysoev419f9ac2003-10-21 16:49:56 +00002427 return ngx_http_output_filter(r, &out);
Igor Sysoev1342d9c2003-10-09 07:00:45 +00002428}
2429
2430
Igor Sysoevc2807ec2006-02-16 15:26:46 +00002431static ngx_int_t
2432ngx_http_post_action(ngx_http_request_t *r)
2433{
2434 ngx_http_core_loc_conf_t *clcf;
2435
2436 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2437
2438 if (clcf->post_action.data == NULL) {
2439 return NGX_DECLINED;
2440 }
2441
Igor Sysoev8fea8852006-03-15 09:53:04 +00002442 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2443 "post action: \"%V\"", &clcf->post_action);
2444
Igor Sysoevc2807ec2006-02-16 15:26:46 +00002445 r->http_version = NGX_HTTP_VERSION_9;
2446 r->header_only = 1;
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00002447 r->post_action = 1;
Igor Sysoevc2807ec2006-02-16 15:26:46 +00002448
Igor Sysoev7ab4d9c2007-08-06 15:22:40 +00002449 r->read_event_handler = ngx_http_block_reading;
Igor Sysoevcf100a72006-12-11 08:31:54 +00002450
Igor Sysoevc2807ec2006-02-16 15:26:46 +00002451 ngx_http_internal_redirect(r, &clcf->post_action, NULL);
2452
2453 return NGX_OK;
2454}
2455
2456
2457static void
Igor Sysoev899b44e2005-05-12 14:58:06 +00002458ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
Igor Sysoevd581fd52003-05-13 16:02:32 +00002459{
Igor Sysoev4ecb4d72006-04-21 12:06:44 +00002460 ngx_connection_t *c;
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002461
2462 c = r->connection;
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002463
Igor Sysoev4ecb4d72006-04-21 12:06:44 +00002464 ngx_http_request_done(r->main, error);
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002465 ngx_http_close_connection(c);
2466}
2467
2468
Igor Sysoevc2807ec2006-02-16 15:26:46 +00002469static void
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002470ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
2471{
2472 ngx_log_t *log;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002473 ngx_uint_t i, n;
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002474 struct linger linger;
Igor Sysoev4ecb4d72006-04-21 12:06:44 +00002475 ngx_http_cleanup_t *cln;
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002476 ngx_http_log_ctx_t *ctx;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002477 ngx_http_handler_pt *log_handler;
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002478 ngx_http_core_loc_conf_t *clcf;
2479 ngx_http_core_main_conf_t *cmcf;
Igor Sysoevd581fd52003-05-13 16:02:32 +00002480
Igor Sysoev865c1502003-11-30 20:03:18 +00002481 log = r->connection->log;
2482
2483 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
Igor Sysoevd581fd52003-05-13 16:02:32 +00002484
2485 if (r->pool == NULL) {
Igor Sysoev90c08142005-07-25 09:41:38 +00002486 ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
Igor Sysoevd581fd52003-05-13 16:02:32 +00002487 return;
2488 }
2489
Igor Sysoev4ecb4d72006-04-21 12:06:44 +00002490 for (cln = r->cleanup; cln; cln = cln->next) {
2491 if (cln->handler) {
2492 cln->handler(cln->data);
2493 }
2494 }
2495
Igor Sysoev85cccfb2004-09-15 16:00:43 +00002496#if (NGX_STAT_STUB)
Igor Sysoev055951d2005-10-21 19:12:18 +00002497
Igor Sysoev85cccfb2004-09-15 16:00:43 +00002498 if (r->stat_reading) {
Igor Sysoev78452232005-10-12 13:50:36 +00002499 ngx_atomic_fetch_add(ngx_stat_reading, -1);
Igor Sysoev85cccfb2004-09-15 16:00:43 +00002500 }
2501
2502 if (r->stat_writing) {
Igor Sysoev78452232005-10-12 13:50:36 +00002503 ngx_atomic_fetch_add(ngx_stat_writing, -1);
Igor Sysoev85cccfb2004-09-15 16:00:43 +00002504 }
Igor Sysoev055951d2005-10-21 19:12:18 +00002505
Igor Sysoev85cccfb2004-09-15 16:00:43 +00002506#endif
2507
Igor Sysoev669e3312003-12-22 09:40:48 +00002508 if (error && r->headers_out.status == 0) {
Igor Sysoevd581fd52003-05-13 16:02:32 +00002509 r->headers_out.status = error;
2510 }
2511
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002512 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
2513
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002514 log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
2515 n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
2516 for (i = 0; i < n; i++) {
2517 log_handler[i](r);
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002518 }
Igor Sysoevd581fd52003-05-13 16:02:32 +00002519
Igor Sysoev0ab91b92004-06-06 19:49:18 +00002520 if (r->connection->timedout) {
2521 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2522
2523 if (clcf->reset_timedout_connection) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00002524 linger.l_onoff = 1;
2525 linger.l_linger = 0;
Igor Sysoev0ab91b92004-06-06 19:49:18 +00002526
2527 if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
Igor Sysoev899b44e2005-05-12 14:58:06 +00002528 (const void *) &linger, sizeof(struct linger)) == -1)
Igor Sysoev0ab91b92004-06-06 19:49:18 +00002529 {
2530 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
2531 "setsockopt(SO_LINGER) failed");
2532 }
2533 }
2534 }
2535
Igor Sysoev899b44e2005-05-12 14:58:06 +00002536 /* the various request strings were allocated from r->pool */
Igor Sysoev865c1502003-11-30 20:03:18 +00002537 ctx = log->data;
Igor Sysoev1b735832004-11-11 14:07:14 +00002538 ctx->request = NULL;
Igor Sysoevd581fd52003-05-13 16:02:32 +00002539
Igor Sysoevc83f6832004-06-24 07:53:37 +00002540 r->request_line.len = 0;
2541
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002542 r->connection->destroyed = 1;
2543
Igor Sysoevd581fd52003-05-13 16:02:32 +00002544 ngx_destroy_pool(r->pool);
2545}
2546
2547
Igor Sysoev90c08142005-07-25 09:41:38 +00002548static void
Igor Sysoevc1571722005-03-19 12:38:37 +00002549ngx_http_close_connection(ngx_connection_t *c)
Igor Sysoev6b863e32003-05-12 15:52:24 +00002550{
Igor Sysoev02025fd2005-01-18 13:03:58 +00002551 ngx_pool_t *pool;
2552
Igor Sysoeve04084c2004-01-26 08:52:49 +00002553 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoev54498db2004-02-11 17:08:49 +00002554 "close http connection: %d", c->fd);
Igor Sysoev6b863e32003-05-12 15:52:24 +00002555
Igor Sysoevceb99292005-09-06 16:09:32 +00002556#if (NGX_HTTP_SSL)
Igor Sysoev924bd792004-10-11 15:07:03 +00002557
2558 if (c->ssl) {
2559 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002560 c->ssl->handler = ngx_http_close_connection;
Igor Sysoev924bd792004-10-11 15:07:03 +00002561 return;
2562 }
2563 }
2564
2565#endif
2566
Igor Sysoevdc3b2a72004-09-14 19:39:54 +00002567#if (NGX_STAT_STUB)
Igor Sysoev78452232005-10-12 13:50:36 +00002568 ngx_atomic_fetch_add(ngx_stat_active, -1);
Igor Sysoevdc3b2a72004-09-14 19:39:54 +00002569#endif
2570
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002571 c->destroyed = 1;
2572
Igor Sysoev02025fd2005-01-18 13:03:58 +00002573 pool = c->pool;
2574
Igor Sysoev59cf56c2004-09-07 15:29:22 +00002575 ngx_close_connection(c);
Igor Sysoev02025fd2005-01-18 13:03:58 +00002576
Igor Sysoevc1571722005-03-19 12:38:37 +00002577 ngx_destroy_pool(pool);
Igor Sysoev2ba1ee02002-10-04 17:58:04 +00002578}
2579
2580
Igor Sysoevc1571722005-03-19 12:38:37 +00002581static u_char *
2582ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
Igor Sysoev0ad17c02002-08-26 15:18:19 +00002583{
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002584 u_char *p;
Igor Sysoevc04deca2005-03-28 14:43:02 +00002585 ngx_http_request_t *r;
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002586 ngx_http_log_ctx_t *ctx;
Igor Sysoev7578ec92003-06-02 15:24:30 +00002587
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002588 if (log->action) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00002589 p = ngx_snprintf(buf, len, " while %s", log->action);
Igor Sysoev1b735832004-11-11 14:07:14 +00002590 len -= p - buf;
Igor Sysoevc04deca2005-03-28 14:43:02 +00002591 buf = p;
Igor Sysoevdc479b42003-03-20 16:09:44 +00002592 }
Igor Sysoev1b735832004-11-11 14:07:14 +00002593
Igor Sysoev899b44e2005-05-12 14:58:06 +00002594 ctx = log->data;
2595
Igor Sysoevcc595672007-12-30 08:01:50 +00002596 p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
Igor Sysoev899b44e2005-05-12 14:58:06 +00002597 len -= p - buf;
Igor Sysoev1b735832004-11-11 14:07:14 +00002598
Igor Sysoevc04deca2005-03-28 14:43:02 +00002599 r = ctx->request;
2600
Igor Sysoev899b44e2005-05-12 14:58:06 +00002601 if (r) {
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00002602 return r->log_handler(r, ctx->current_request, p, len);
Igor Sysoevcc595672007-12-30 08:01:50 +00002603
2604 } else {
2605 p = ngx_snprintf(p, len, ", server: %V",
2606 &ctx->connection->listening->addr_text);
Igor Sysoev1b735832004-11-11 14:07:14 +00002607 }
2608
Igor Sysoev899b44e2005-05-12 14:58:06 +00002609 return p;
2610}
2611
2612
2613static u_char *
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00002614ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
2615 u_char *buf, size_t len)
Igor Sysoev899b44e2005-05-12 14:58:06 +00002616{
Igor Sysoevfeee7262007-12-30 08:15:27 +00002617 char *uri_separator;
2618 u_char *p;
2619 ngx_http_upstream_t *u;
2620 ngx_http_core_srv_conf_t *cscf;
Igor Sysoev1b735832004-11-11 14:07:14 +00002621
Igor Sysoevfeee7262007-12-30 08:15:27 +00002622 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2623
2624 p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
2625 len -= p - buf;
2626 buf = p;
Igor Sysoev4d656dc2005-03-22 16:02:46 +00002627
Igor Sysoeva6b59572007-11-27 12:22:01 +00002628 if (r->request_line.data == NULL && r->request_start) {
2629 for (p = r->request_start; p < r->header_in->last; p++) {
2630 if (*p == CR || *p == LF) {
2631 break;
2632 }
2633 }
2634
2635 r->request_line.len = p - r->request_start;
2636 r->request_line.data = r->request_start;
2637 }
2638
2639 if (r->request_line.len) {
2640 p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
Igor Sysoevc04deca2005-03-28 14:43:02 +00002641 len -= p - buf;
2642 buf = p;
Igor Sysoev1b735832004-11-11 14:07:14 +00002643 }
2644
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00002645 if (r != sr) {
2646 p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
2647 len -= p - buf;
2648 buf = p;
2649 }
2650
2651 u = sr->upstream;
2652
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002653 if (u && u->peer.name) {
2654
2655 uri_separator = "";
2656
2657#if (NGX_HAVE_UNIX_DOMAIN)
2658 if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
2659 uri_separator = ":";
2660 }
2661#endif
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00002662
2663 p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
Igor Sysoev96dd8af2007-11-27 13:34:13 +00002664 &u->schema, u->peer.name,
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002665 uri_separator, &u->uri);
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00002666 len -= p - buf;
2667 buf = p;
2668 }
2669
Igor Sysoevc04deca2005-03-28 14:43:02 +00002670 if (r->headers_in.host) {
2671 p = ngx_snprintf(buf, len, ", host: \"%V\"",
2672 &r->headers_in.host->value);
2673 len -= p - buf;
2674 buf = p;
2675 }
2676
2677 if (r->headers_in.referer) {
2678 p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
2679 &r->headers_in.referer->value);
2680 buf = p;
2681 }
2682
2683 return buf;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00002684}