Mercurial > hg > nginx-quic
annotate src/http/ngx_http_upstream.c @ 755:63b9dc652c3d
finalize nonbuffered request on timeout
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 10 Oct 2006 13:11:06 +0000 |
parents | bae59a740c40 |
children | d904b5cb3bba |
rev | line source |
---|---|
479 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
509 | 12 static void ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r); |
13 static void ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r); | |
14 static void ngx_http_upstream_check_broken_connection(ngx_http_request_t *r, | |
15 ngx_event_t *ev); | |
479 | 16 static void ngx_http_upstream_connect(ngx_http_request_t *r, |
487 | 17 ngx_http_upstream_t *u); |
509 | 18 static ngx_int_t ngx_http_upstream_reinit(ngx_http_request_t *r, |
487 | 19 ngx_http_upstream_t *u); |
479 | 20 static void ngx_http_upstream_send_request(ngx_http_request_t *r, |
487 | 21 ngx_http_upstream_t *u); |
479 | 22 static void ngx_http_upstream_send_request_handler(ngx_event_t *wev); |
23 static void ngx_http_upstream_process_header(ngx_event_t *rev); | |
24 static void ngx_http_upstream_send_response(ngx_http_request_t *r, | |
487 | 25 ngx_http_upstream_t *u); |
581 | 26 static void |
27 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r); | |
28 static void ngx_http_upstream_process_non_buffered_body(ngx_event_t *ev); | |
29 static ngx_int_t ngx_http_upstream_non_buffered_filter_init(void *data); | |
30 static ngx_int_t ngx_http_upstream_non_buffered_filter(void *data, | |
31 ssize_t bytes); | |
509 | 32 static void ngx_http_upstream_process_downstream(ngx_http_request_t *r); |
479 | 33 static void ngx_http_upstream_process_body(ngx_event_t *ev); |
34 static void ngx_http_upstream_dummy_handler(ngx_event_t *wev); | |
35 static void ngx_http_upstream_next(ngx_http_request_t *r, | |
487 | 36 ngx_http_upstream_t *u, ngx_uint_t ft_type); |
569 | 37 static void ngx_http_upstream_cleanup(void *data); |
479 | 38 static void ngx_http_upstream_finalize_request(ngx_http_request_t *r, |
487 | 39 ngx_http_upstream_t *u, ngx_int_t rc); |
479 | 40 |
509 | 41 static ngx_int_t ngx_http_upstream_process_header_line(ngx_http_request_t *r, |
42 ngx_table_elt_t *h, ngx_uint_t offset); | |
43 static ngx_int_t | |
44 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r, | |
45 ngx_table_elt_t *h, ngx_uint_t offset); | |
46 static ngx_int_t ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, | |
47 ngx_table_elt_t *h, ngx_uint_t offset); | |
527 | 48 static ngx_int_t ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, |
49 ngx_table_elt_t *h, ngx_uint_t offset); | |
649 | 50 static ngx_int_t ngx_http_upstream_process_buffering(ngx_http_request_t *r, |
509 | 51 ngx_table_elt_t *h, ngx_uint_t offset); |
657 | 52 static ngx_int_t ngx_http_upstream_process_charset(ngx_http_request_t *r, |
53 ngx_table_elt_t *h, ngx_uint_t offset); | |
649 | 54 static ngx_int_t ngx_http_upstream_copy_header_line(ngx_http_request_t *r, |
509 | 55 ngx_table_elt_t *h, ngx_uint_t offset); |
56 static ngx_int_t | |
57 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r, | |
58 ngx_table_elt_t *h, ngx_uint_t offset); | |
59 static ngx_int_t ngx_http_upstream_copy_content_type(ngx_http_request_t *r, | |
60 ngx_table_elt_t *h, ngx_uint_t offset); | |
61 static ngx_int_t ngx_http_upstream_copy_content_length(ngx_http_request_t *r, | |
62 ngx_table_elt_t *h, ngx_uint_t offset); | |
63 static ngx_int_t ngx_http_upstream_rewrite_location(ngx_http_request_t *r, | |
64 ngx_table_elt_t *h, ngx_uint_t offset); | |
65 static ngx_int_t ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, | |
66 ngx_table_elt_t *h, ngx_uint_t offset); | |
67 #if (NGX_HTTP_GZIP) | |
68 static ngx_int_t ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r, | |
69 ngx_table_elt_t *h, ngx_uint_t offset); | |
70 #endif | |
71 | |
479 | 72 static size_t ngx_http_upstream_log_status_getlen(ngx_http_request_t *r, |
487 | 73 uintptr_t data); |
74 static u_char *ngx_http_upstream_log_status(ngx_http_request_t *r, | |
75 u_char *buf, ngx_http_log_op_t *op); | |
573 | 76 static size_t ngx_http_upstream_log_response_time_getlen(ngx_http_request_t *r, |
77 uintptr_t data); | |
563 | 78 static u_char *ngx_http_upstream_log_response_time(ngx_http_request_t *r, |
79 u_char *buf, ngx_http_log_op_t *op); | |
479 | 80 |
573 | 81 static ngx_int_t ngx_http_upstream_add_variables(ngx_conf_t *cf); |
82 static ngx_int_t ngx_http_upstream_status_variable(ngx_http_request_t *r, | |
83 ngx_http_variable_value_t *v, uintptr_t data); | |
84 static ngx_int_t ngx_http_upstream_response_time_variable(ngx_http_request_t *r, | |
85 ngx_http_variable_value_t *v, uintptr_t data); | |
86 | |
651 | 87 static char *ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy); |
88 static char *ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, | |
89 void *conf); | |
90 | |
509 | 91 static void *ngx_http_upstream_create_main_conf(ngx_conf_t *cf); |
651 | 92 static char *ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf); |
509 | 93 |
577 | 94 #if (NGX_HTTP_SSL) |
591 | 95 static void ngx_http_upstream_ssl_init_connection(ngx_http_request_t *, |
96 ngx_http_upstream_t *u, ngx_connection_t *c); | |
577 | 97 static void ngx_http_upstream_ssl_handshake(ngx_connection_t *c); |
98 static void ngx_http_upstream_ssl_shutdown(ngx_connection_t *c, | |
99 ngx_peer_t *peer); | |
100 #endif | |
101 | |
509 | 102 |
103 ngx_http_upstream_header_t ngx_http_upstream_headers_in[] = { | |
104 | |
105 { ngx_string("Status"), | |
106 ngx_http_upstream_process_header_line, | |
107 offsetof(ngx_http_upstream_headers_in_t, status), | |
649 | 108 ngx_http_upstream_copy_header_line, 0, 0 }, |
509 | 109 |
110 { ngx_string("Content-Type"), | |
111 ngx_http_upstream_process_header_line, | |
112 offsetof(ngx_http_upstream_headers_in_t, content_type), | |
673 | 113 ngx_http_upstream_copy_content_type, 0, 1 }, |
509 | 114 |
115 { ngx_string("Content-Length"), | |
116 ngx_http_upstream_process_header_line, | |
117 offsetof(ngx_http_upstream_headers_in_t, content_length), | |
515 | 118 ngx_http_upstream_copy_content_length, 0, 0 }, |
509 | 119 |
120 { ngx_string("Date"), | |
121 ngx_http_upstream_process_header_line, | |
122 offsetof(ngx_http_upstream_headers_in_t, date), | |
649 | 123 ngx_http_upstream_copy_header_line, |
124 offsetof(ngx_http_headers_out_t, date), 0 }, | |
509 | 125 |
126 { ngx_string("Server"), | |
127 ngx_http_upstream_process_header_line, | |
128 offsetof(ngx_http_upstream_headers_in_t, server), | |
649 | 129 ngx_http_upstream_copy_header_line, |
130 offsetof(ngx_http_headers_out_t, server), 0 }, | |
509 | 131 |
529 | 132 { ngx_string("WWW-Authenticate"), |
133 ngx_http_upstream_process_header_line, | |
134 offsetof(ngx_http_upstream_headers_in_t, www_authenticate), | |
135 ngx_http_upstream_copy_header_line, 0, 0 }, | |
136 | |
509 | 137 { ngx_string("Location"), |
138 ngx_http_upstream_ignore_header_line, 0, | |
515 | 139 ngx_http_upstream_rewrite_location, 0, 0 }, |
509 | 140 |
141 { ngx_string("Refresh"), | |
142 ngx_http_upstream_ignore_header_line, 0, | |
515 | 143 ngx_http_upstream_rewrite_refresh, 0, 0 }, |
144 | |
145 { ngx_string("Set-Cookie"), | |
146 ngx_http_upstream_ignore_header_line, 0, | |
531 | 147 ngx_http_upstream_copy_header_line, 0, 1 }, |
148 | |
149 { ngx_string("Content-Disposition"), | |
150 ngx_http_upstream_ignore_header_line, 0, | |
151 ngx_http_upstream_copy_header_line, 0, 1 }, | |
509 | 152 |
153 { ngx_string("Cache-Control"), | |
154 ngx_http_upstream_process_multi_header_lines, | |
155 offsetof(ngx_http_upstream_headers_in_t, cache_control), | |
156 ngx_http_upstream_copy_multi_header_lines, | |
515 | 157 offsetof(ngx_http_headers_out_t, cache_control), 1 }, |
509 | 158 |
573 | 159 { ngx_string("Expires"), |
160 ngx_http_upstream_process_header_line, | |
161 offsetof(ngx_http_upstream_headers_in_t, expires), | |
162 ngx_http_upstream_copy_header_line, | |
163 offsetof(ngx_http_headers_out_t, expires), 1 }, | |
164 | |
577 | 165 { ngx_string("Accept-Ranges"), |
166 ngx_http_upstream_process_header_line, | |
167 offsetof(ngx_http_upstream_headers_in_t, accept_ranges), | |
168 ngx_http_upstream_copy_header_line, | |
169 offsetof(ngx_http_headers_out_t, accept_ranges), 1 }, | |
170 | |
509 | 171 { ngx_string("Connection"), |
172 ngx_http_upstream_ignore_header_line, 0, | |
515 | 173 ngx_http_upstream_ignore_header_line, 0, 0 }, |
509 | 174 |
641 | 175 { ngx_string("Keep-Alive"), |
176 ngx_http_upstream_ignore_header_line, 0, | |
177 ngx_http_upstream_ignore_header_line, 0, 0 }, | |
178 | |
509 | 179 { ngx_string("X-Powered-By"), |
180 ngx_http_upstream_ignore_header_line, 0, | |
649 | 181 ngx_http_upstream_copy_header_line, 0, 0 }, |
509 | 182 |
183 { ngx_string("X-Accel-Expires"), | |
184 ngx_http_upstream_process_header_line, | |
185 offsetof(ngx_http_upstream_headers_in_t, x_accel_expires), | |
649 | 186 ngx_http_upstream_copy_header_line, 0, 0 }, |
515 | 187 |
188 { ngx_string("X-Accel-Redirect"), | |
189 ngx_http_upstream_process_header_line, | |
190 offsetof(ngx_http_upstream_headers_in_t, x_accel_redirect), | |
191 ngx_http_upstream_ignore_header_line, 0, 0 }, | |
509 | 192 |
527 | 193 { ngx_string("X-Accel-Limit-Rate"), |
194 ngx_http_upstream_process_limit_rate, 0, | |
195 ngx_http_upstream_ignore_header_line, 0, 0 }, | |
196 | |
649 | 197 { ngx_string("X-Accel-Buffering"), |
198 ngx_http_upstream_process_buffering, 0, | |
199 ngx_http_upstream_ignore_header_line, 0, 0 }, | |
200 | |
657 | 201 { ngx_string("X-Accel-Charset"), |
202 ngx_http_upstream_process_charset, 0, | |
203 ngx_http_upstream_ignore_header_line, 0, 0 }, | |
204 | |
509 | 205 #if (NGX_HTTP_GZIP) |
206 { ngx_string("Content-Encoding"), | |
207 ngx_http_upstream_process_header_line, | |
208 offsetof(ngx_http_upstream_headers_in_t, content_encoding), | |
515 | 209 ngx_http_upstream_copy_content_encoding, 0, 0 }, |
509 | 210 #endif |
211 | |
515 | 212 { ngx_null_string, NULL, 0, NULL, 0, 0 } |
509 | 213 }; |
479 | 214 |
215 | |
651 | 216 static ngx_command_t ngx_http_upstream_commands[] = { |
217 | |
218 { ngx_string("upstream"), | |
219 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1, | |
220 ngx_http_upstream, | |
221 0, | |
222 0, | |
223 NULL }, | |
224 | |
225 { ngx_string("server"), | |
663 | 226 NGX_HTTP_UPS_CONF|NGX_CONF_TAKE12, |
651 | 227 ngx_http_upstream_server, |
228 NGX_HTTP_SRV_CONF_OFFSET, | |
229 0, | |
230 NULL }, | |
231 | |
232 ngx_null_command | |
233 }; | |
234 | |
235 | |
236 static ngx_http_module_t ngx_http_upstream_module_ctx = { | |
573 | 237 ngx_http_upstream_add_variables, /* preconfiguration */ |
509 | 238 NULL, /* postconfiguration */ |
239 | |
240 ngx_http_upstream_create_main_conf, /* create main configuration */ | |
651 | 241 ngx_http_upstream_init_main_conf, /* init main configuration */ |
479 | 242 |
243 NULL, /* create server configuration */ | |
244 NULL, /* merge server configuration */ | |
245 | |
246 NULL, /* create location configuration */ | |
247 NULL /* merge location configuration */ | |
248 }; | |
577 | 249 |
479 | 250 |
251 ngx_module_t ngx_http_upstream_module = { | |
509 | 252 NGX_MODULE_V1, |
479 | 253 &ngx_http_upstream_module_ctx, /* module context */ |
651 | 254 ngx_http_upstream_commands, /* module directives */ |
479 | 255 NGX_HTTP_MODULE, /* module type */ |
541 | 256 NULL, /* init master */ |
479 | 257 NULL, /* init module */ |
541 | 258 NULL, /* init process */ |
259 NULL, /* init thread */ | |
260 NULL, /* exit thread */ | |
261 NULL, /* exit process */ | |
262 NULL, /* exit master */ | |
263 NGX_MODULE_V1_PADDING | |
479 | 264 }; |
265 | |
266 | |
583 | 267 static ngx_http_log_op_name_t ngx_http_upstream_log_fmt_ops[] = { |
479 | 268 { ngx_string("upstream_status"), 0, NULL, |
573 | 269 ngx_http_upstream_log_status_getlen, |
270 ngx_http_upstream_log_status }, | |
271 { ngx_string("upstream_response_time"), 0, NULL, | |
272 ngx_http_upstream_log_response_time_getlen, | |
273 ngx_http_upstream_log_response_time }, | |
479 | 274 { ngx_null_string, 0, NULL, NULL, NULL } |
275 }; | |
276 | |
277 | |
573 | 278 static ngx_http_variable_t ngx_http_upstream_vars[] = { |
279 | |
637 | 280 { ngx_string("upstream_status"), NULL, |
583 | 281 ngx_http_upstream_status_variable, 0, NGX_HTTP_VAR_NOHASH, 0 }, |
573 | 282 |
637 | 283 { ngx_string("upstream_response_time"), NULL, |
583 | 284 ngx_http_upstream_response_time_variable, 0, NGX_HTTP_VAR_NOHASH, 0 }, |
573 | 285 |
637 | 286 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
573 | 287 }; |
288 | |
289 | |
487 | 290 void |
291 ngx_http_upstream_init(ngx_http_request_t *r) | |
479 | 292 { |
509 | 293 ngx_connection_t *c; |
569 | 294 ngx_http_cleanup_t *cln; |
509 | 295 ngx_http_upstream_t *u; |
296 ngx_http_core_loc_conf_t *clcf; | |
479 | 297 |
298 c = r->connection; | |
299 | |
300 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
301 "http init upstream, client timer: %d", c->read->timer_set); | |
302 | |
303 if (c->read->timer_set) { | |
304 ngx_del_timer(c->read); | |
305 } | |
306 | |
629 | 307 u = r->upstream; |
308 | |
309 if (!r->post_action && !u->conf->ignore_client_abort) { | |
615 | 310 r->read_event_handler = ngx_http_upstream_rd_check_broken_connection; |
311 r->write_event_handler = ngx_http_upstream_wr_check_broken_connection; | |
312 } | |
577 | 313 |
479 | 314 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) { |
315 | |
316 if (!c->write->active) { | |
531 | 317 if (ngx_add_event(c->write, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) |
318 == NGX_ERROR) | |
479 | 319 { |
320 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
321 return; | |
322 } | |
323 } | |
324 } | |
325 | |
537 | 326 if (r->request_body) { |
327 u->request_bufs = r->request_body->bufs; | |
328 } | |
509 | 329 |
537 | 330 if (u->create_request(r) != NGX_OK) { |
479 | 331 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
332 return; | |
333 } | |
334 | |
335 u->peer.log = r->connection->log; | |
509 | 336 |
337 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
479 | 338 |
339 u->output.sendfile = r->connection->sendfile; | |
340 u->output.pool = r->pool; | |
341 u->output.bufs.num = 1; | |
509 | 342 u->output.bufs.size = clcf->client_body_buffer_size; |
479 | 343 u->output.output_filter = ngx_chain_writer; |
344 u->output.filter_ctx = &u->writer; | |
345 | |
346 u->writer.pool = r->pool; | |
347 | |
348 if (ngx_array_init(&u->states, r->pool, u->peer.peers->number, | |
569 | 349 sizeof(ngx_http_upstream_state_t)) |
350 != NGX_OK) | |
479 | 351 { |
352 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
353 return; | |
354 } | |
355 | |
593 | 356 cln = ngx_http_cleanup_add(r, 0); |
569 | 357 if (cln == NULL) { |
358 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
359 return; | |
360 } | |
361 | |
362 cln->handler = ngx_http_upstream_cleanup; | |
363 cln->data = r; | |
364 u->cleanup = &cln->handler; | |
365 | |
479 | 366 ngx_http_upstream_connect(r, u); |
367 } | |
368 | |
369 | |
487 | 370 static void |
509 | 371 ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r) |
372 { | |
373 ngx_http_upstream_check_broken_connection(r, r->connection->read); | |
374 } | |
375 | |
376 | |
377 static void | |
378 ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r) | |
379 { | |
380 ngx_http_upstream_check_broken_connection(r, r->connection->write); | |
381 } | |
382 | |
383 | |
384 static void | |
385 ngx_http_upstream_check_broken_connection(ngx_http_request_t *r, | |
386 ngx_event_t *ev) | |
479 | 387 { |
388 int n; | |
389 char buf[1]; | |
577 | 390 ngx_err_t err; |
479 | 391 ngx_connection_t *c; |
392 ngx_http_upstream_t *u; | |
393 | |
527 | 394 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ev->log, 0, |
395 "http upstream check client, write event:%d, \"%V\"", | |
396 ev->write, &r->uri); | |
479 | 397 |
509 | 398 c = r->connection; |
483 | 399 u = r->upstream; |
400 | |
583 | 401 if (c->error) { |
527 | 402 ngx_http_upstream_finalize_request(r, u, |
403 NGX_HTTP_CLIENT_CLOSED_REQUEST); | |
404 return; | |
405 } | |
406 | |
483 | 407 if (u->peer.connection == NULL) { |
408 return; | |
409 } | |
410 | |
479 | 411 #if (NGX_HAVE_KQUEUE) |
412 | |
413 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { | |
414 | |
415 if (!ev->pending_eof) { | |
416 return; | |
417 } | |
418 | |
419 ev->eof = 1; | |
583 | 420 c->error = 1; |
479 | 421 |
422 if (ev->kq_errno) { | |
423 ev->error = 1; | |
424 } | |
425 | |
426 if (!u->cachable && u->peer.connection) { | |
427 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno, | |
527 | 428 "kevent() reported that client closed prematurely " |
429 "connection, so upstream connection is closed too"); | |
479 | 430 ngx_http_upstream_finalize_request(r, u, |
431 NGX_HTTP_CLIENT_CLOSED_REQUEST); | |
432 return; | |
433 } | |
434 | |
435 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno, | |
436 "kevent() reported that client closed " | |
437 "prematurely connection"); | |
438 | |
439 if (u->peer.connection == NULL) { | |
440 ngx_http_upstream_finalize_request(r, u, | |
441 NGX_HTTP_CLIENT_CLOSED_REQUEST); | |
442 return; | |
443 } | |
444 | |
445 return; | |
446 } | |
447 | |
448 #endif | |
449 | |
450 n = recv(c->fd, buf, 1, MSG_PEEK); | |
451 | |
452 err = ngx_socket_errno; | |
453 | |
454 /* | |
455 * we do not need to disable the write event because | |
456 * that event has NGX_USE_CLEAR_EVENT type | |
457 */ | |
458 | |
459 if (ev->write && (n >= 0 || err == NGX_EAGAIN)) { | |
460 return; | |
461 } | |
462 | |
463 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) { | |
464 if (ngx_del_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) { | |
465 ngx_http_upstream_finalize_request(r, u, | |
466 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
467 return; | |
468 } | |
469 } | |
470 | |
471 if (n > 0) { | |
472 return; | |
473 } | |
474 | |
475 if (n == -1) { | |
476 if (err == NGX_EAGAIN) { | |
477 return; | |
478 } | |
479 | |
480 ev->error = 1; | |
481 | |
483 | 482 } else { /* n == 0 */ |
479 | 483 err = 0; |
484 } | |
485 | |
529 | 486 ev->eof = 1; |
583 | 487 c->error = 1; |
529 | 488 |
479 | 489 if (!u->cachable && u->peer.connection) { |
490 ngx_log_error(NGX_LOG_INFO, ev->log, err, | |
491 "client closed prematurely connection, " | |
492 "so upstream connection is closed too"); | |
493 ngx_http_upstream_finalize_request(r, u, | |
494 NGX_HTTP_CLIENT_CLOSED_REQUEST); | |
495 return; | |
496 } | |
497 | |
498 ngx_log_error(NGX_LOG_INFO, ev->log, err, | |
499 "client closed prematurely connection"); | |
500 | |
501 if (u->peer.connection == NULL) { | |
502 ngx_http_upstream_finalize_request(r, u, | |
503 NGX_HTTP_CLIENT_CLOSED_REQUEST); | |
504 return; | |
505 } | |
506 } | |
507 | |
508 | |
487 | 509 static void |
510 ngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u) | |
479 | 511 { |
591 | 512 ngx_int_t rc; |
665 | 513 ngx_time_t *tp; |
591 | 514 ngx_connection_t *c; |
479 | 515 |
483 | 516 r->connection->log->action = "connecting to upstream"; |
479 | 517 |
518 r->connection->single_connection = 0; | |
519 | |
665 | 520 if (u->state && u->state->response_time) { |
521 tp = ngx_timeofday(); | |
522 u->state->response_time = tp->sec * 1000 + tp->msec | |
523 - u->state->response_time; | |
524 } | |
525 | |
526 u->state = ngx_array_push(&u->states); | |
527 if (u->state == NULL) { | |
528 ngx_http_upstream_finalize_request(r, u, | |
529 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
530 return; | |
531 } | |
532 | |
533 ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t)); | |
534 | |
535 tp = ngx_timeofday(); | |
536 u->state->response_time = tp->sec * 1000 + tp->msec; | |
537 | |
479 | 538 rc = ngx_event_connect_peer(&u->peer); |
539 | |
540 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
541 "http upstream connect: %i", rc); | |
542 | |
543 if (rc == NGX_ERROR) { | |
544 ngx_http_upstream_finalize_request(r, u, | |
545 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
546 return; | |
547 } | |
548 | |
591 | 549 u->state->peer = &u->peer.peers->peer[u->peer.cur_peer].name; |
479 | 550 |
543 | 551 if (rc == NGX_BUSY) { |
552 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams"); | |
553 } | |
554 | |
555 if (rc == NGX_BUSY || rc == NGX_DECLINED) { | |
479 | 556 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); |
557 return; | |
558 } | |
559 | |
577 | 560 /* rc == NGX_OK || rc == NGX_AGAIN */ |
561 | |
479 | 562 c = u->peer.connection; |
563 | |
564 c->data = r; | |
591 | 565 |
509 | 566 c->write->handler = ngx_http_upstream_send_request_handler; |
567 c->read->handler = ngx_http_upstream_process_header; | |
479 | 568 |
569 c->sendfile = r->connection->sendfile; | |
570 | |
571 c->pool = r->pool; | |
572 c->read->log = c->write->log = c->log = r->connection->log; | |
573 | |
574 /* init or reinit the ngx_output_chain() and ngx_chain_writer() contexts */ | |
575 | |
576 u->writer.out = NULL; | |
577 u->writer.last = &u->writer.out; | |
578 u->writer.connection = c; | |
579 u->writer.limit = 0; | |
580 | |
581 if (u->request_sent) { | |
509 | 582 if (ngx_http_upstream_reinit(r, u) != NGX_OK) { |
583 ngx_http_upstream_finalize_request(r, u, | |
584 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
585 return; | |
586 } | |
479 | 587 } |
588 | |
597 | 589 if (r->request_body && r->request_body->temp_file && r == r->main) { |
573 | 590 |
591 /* | |
592 * the r->request_body->buf can be reused for one request only, | |
593 * the subrequests should allocate their own temporay bufs | |
594 */ | |
595 | |
596 u->output.free = ngx_alloc_chain_link(r->pool); | |
597 if (u->output.free == NULL) { | |
598 ngx_http_upstream_finalize_request(r, u, | |
599 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
600 return; | |
479 | 601 } |
573 | 602 |
603 u->output.free->buf = r->request_body->buf; | |
604 u->output.free->next = NULL; | |
605 u->output.allocated = 1; | |
606 | |
607 r->request_body->buf->pos = r->request_body->buf->start; | |
608 r->request_body->buf->last = r->request_body->buf->start; | |
609 r->request_body->buf->tag = u->output.tag; | |
479 | 610 } |
611 | |
612 u->request_sent = 0; | |
613 | |
614 if (rc == NGX_AGAIN) { | |
615 ngx_add_timer(c->write, u->conf->connect_timeout); | |
616 return; | |
617 } | |
618 | |
577 | 619 #if (NGX_HTTP_SSL) |
620 | |
591 | 621 if (u->conf->ssl && c->ssl == NULL) { |
622 ngx_http_upstream_ssl_init_connection(r, u, c); | |
577 | 623 return; |
624 } | |
625 | |
626 #endif | |
627 | |
479 | 628 ngx_http_upstream_send_request(r, u); |
629 } | |
630 | |
631 | |
577 | 632 #if (NGX_HTTP_SSL) |
633 | |
634 static void | |
591 | 635 ngx_http_upstream_ssl_init_connection(ngx_http_request_t *r, |
636 ngx_http_upstream_t *u, ngx_connection_t *c) | |
637 { | |
638 ngx_int_t rc; | |
639 ngx_peer_t *peer; | |
640 | |
641 if (ngx_ssl_create_connection(u->conf->ssl, c, | |
642 NGX_SSL_BUFFER|NGX_SSL_CLIENT) | |
643 == NGX_ERROR) | |
644 { | |
645 ngx_http_upstream_finalize_request(r, u, | |
646 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
647 return; | |
648 } | |
649 | |
650 c->sendfile = 0; | |
619 | 651 u->output.sendfile = 0; |
591 | 652 |
653 peer = &u->peer.peers->peer[u->peer.cur_peer]; | |
654 | |
655 if (ngx_ssl_set_session(c, peer->ssl_session) != NGX_OK) { | |
656 ngx_http_upstream_finalize_request(r, u, | |
657 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
658 return; | |
659 } | |
660 | |
641 | 661 r->connection->log->action = "SSL handshaking to upstream"; |
662 | |
591 | 663 rc = ngx_ssl_handshake(c); |
664 | |
665 if (rc == NGX_AGAIN) { | |
666 c->ssl->handler = ngx_http_upstream_ssl_handshake; | |
667 return; | |
668 } | |
669 | |
670 ngx_http_upstream_ssl_handshake(c); | |
671 } | |
672 | |
673 | |
674 static void | |
577 | 675 ngx_http_upstream_ssl_handshake(ngx_connection_t *c) |
676 { | |
677 ngx_http_request_t *r; | |
678 ngx_http_upstream_t *u; | |
679 | |
680 r = c->data; | |
681 u = r->upstream; | |
682 | |
683 if (c->ssl->handshaked) { | |
684 | |
685 c->write->handler = ngx_http_upstream_send_request_handler; | |
686 c->read->handler = ngx_http_upstream_process_header; | |
687 | |
688 ngx_http_upstream_send_request(r, u); | |
689 | |
690 return; | |
691 } | |
692 | |
693 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); | |
694 | |
695 } | |
696 | |
697 #endif | |
698 | |
699 | |
509 | 700 static ngx_int_t |
487 | 701 ngx_http_upstream_reinit(ngx_http_request_t *r, ngx_http_upstream_t *u) |
479 | 702 { |
703 ngx_chain_t *cl; | |
704 | |
509 | 705 if (u->reinit_request(r) != NGX_OK) { |
706 return NGX_ERROR; | |
707 } | |
708 | |
709 ngx_memzero(&r->upstream->headers_in, | |
710 sizeof(ngx_http_upstream_headers_in_t)); | |
711 | |
712 if (ngx_list_init(&r->upstream->headers_in.headers, r->pool, 8, | |
591 | 713 sizeof(ngx_table_elt_t)) |
714 != NGX_OK) | |
509 | 715 { |
716 return NGX_ERROR; | |
479 | 717 } |
718 | |
719 /* reinit the request chain */ | |
577 | 720 |
509 | 721 for (cl = u->request_bufs; cl; cl = cl->next) { |
479 | 722 cl->buf->pos = cl->buf->start; |
723 cl->buf->file_pos = 0; | |
724 } | |
725 | |
509 | 726 /* reinit the subrequest's ngx_output_chain() context */ |
727 | |
573 | 728 if (r->request_body && r->request_body->temp_file |
597 | 729 && r != r->main && u->output.buf) |
573 | 730 { |
731 u->output.free = ngx_alloc_chain_link(r->pool); | |
732 if (u->output.free == NULL) { | |
733 return NGX_ERROR; | |
509 | 734 } |
573 | 735 |
736 u->output.free->buf = u->output.buf; | |
737 u->output.free->next = NULL; | |
738 | |
739 u->output.buf->pos = u->output.buf->start; | |
740 u->output.buf->last = u->output.buf->start; | |
509 | 741 } |
479 | 742 |
743 u->output.buf = NULL; | |
744 u->output.in = NULL; | |
745 u->output.busy = NULL; | |
577 | 746 |
581 | 747 /* reinit u->buffer */ |
577 | 748 |
479 | 749 #if 0 |
750 if (u->cache) { | |
581 | 751 u->buffer.pos = u->buffer.start + u->cache->ctx.header_size; |
752 u->buffer.last = u->buffer.pos; | |
479 | 753 |
754 } else { | |
581 | 755 u->buffer.pos = u->buffer.start; |
756 u->buffer.last = u->buffer.start; | |
479 | 757 } |
758 #else | |
509 | 759 |
581 | 760 u->buffer.pos = u->buffer.start; |
761 u->buffer.last = u->buffer.start; | |
509 | 762 |
479 | 763 #endif |
764 | |
509 | 765 return NGX_OK; |
479 | 766 } |
767 | |
768 | |
487 | 769 static void |
770 ngx_http_upstream_send_request(ngx_http_request_t *r, ngx_http_upstream_t *u) | |
479 | 771 { |
671 | 772 int rc, err; |
773 socklen_t len; | |
483 | 774 ngx_connection_t *c; |
577 | 775 |
479 | 776 c = u->peer.connection; |
777 | |
778 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
779 "http upstream send request"); | |
780 | |
671 | 781 if (!u->request_sent) { |
782 | |
479 | 783 #if (NGX_HAVE_KQUEUE) |
784 | |
671 | 785 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { |
786 if (c->write->pending_eof) { | |
787 (void) ngx_connection_error(c, c->write->kq_errno, | |
537 | 788 "kevent() reported that connect() failed"); |
671 | 789 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); |
790 return; | |
791 } | |
792 | |
793 } else | |
794 #endif | |
795 { | |
796 err = 0; | |
797 len = sizeof(int); | |
798 | |
799 /* | |
800 * BSDs and Linux return 0 and set a pending error in err | |
801 * Solaris returns -1 and sets errno | |
802 */ | |
803 | |
804 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len) | |
805 == -1) | |
806 { | |
807 err = ngx_errno; | |
808 } | |
809 | |
810 if (err) { | |
811 (void) ngx_connection_error(c, err, "connect() failed"); | |
812 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); | |
813 return; | |
814 } | |
815 } | |
479 | 816 } |
817 | |
483 | 818 c->log->action = "sending request to upstream"; |
479 | 819 |
509 | 820 rc = ngx_output_chain(&u->output, u->request_sent ? NULL : u->request_bufs); |
479 | 821 |
822 u->request_sent = 1; | |
823 | |
824 if (rc == NGX_ERROR) { | |
825 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); | |
826 return; | |
827 } | |
828 | |
829 if (c->write->timer_set) { | |
830 ngx_del_timer(c->write); | |
831 } | |
832 | |
833 if (rc == NGX_AGAIN) { | |
834 ngx_add_timer(c->write, u->conf->send_timeout); | |
835 | |
836 if (ngx_handle_write_event(c->write, u->conf->send_lowat) == NGX_ERROR) | |
837 { | |
838 ngx_http_upstream_finalize_request(r, u, | |
839 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
840 return; | |
841 } | |
842 | |
843 return; | |
844 } | |
845 | |
846 /* rc == NGX_OK */ | |
847 | |
848 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) { | |
849 if (ngx_tcp_push(c->fd) == NGX_ERROR) { | |
850 ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno, | |
851 ngx_tcp_push_n " failed"); | |
852 ngx_http_upstream_finalize_request(r, u, | |
853 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
577 | 854 return; |
479 | 855 } |
856 | |
857 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET; | |
858 } | |
577 | 859 |
479 | 860 ngx_add_timer(c->read, u->conf->read_timeout); |
861 | |
862 #if 1 | |
863 if (c->read->ready) { | |
577 | 864 |
479 | 865 /* post aio operation */ |
866 | |
867 /* | |
868 * TODO comment | |
869 * although we can post aio operation just in the end | |
870 * of ngx_http_upstream_connect() CHECK IT !!! | |
871 * it's better to do here because we postpone header buffer allocation | |
872 */ | |
873 | |
874 ngx_http_upstream_process_header(c->read); | |
875 return; | |
876 } | |
877 #endif | |
878 | |
509 | 879 c->write->handler = ngx_http_upstream_dummy_handler; |
479 | 880 |
509 | 881 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) { |
479 | 882 ngx_http_upstream_finalize_request(r, u, |
883 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
884 return; | |
885 } | |
886 } | |
887 | |
888 | |
487 | 889 static void |
890 ngx_http_upstream_send_request_handler(ngx_event_t *wev) | |
479 | 891 { |
892 ngx_connection_t *c; | |
893 ngx_http_request_t *r; | |
894 ngx_http_upstream_t *u; | |
895 | |
896 c = wev->data; | |
897 r = c->data; | |
898 u = r->upstream; | |
899 | |
900 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, | |
901 "http upstream send request handler"); | |
902 | |
903 if (wev->timedout) { | |
904 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT); | |
905 return; | |
906 } | |
907 | |
591 | 908 #if (NGX_HTTP_SSL) |
909 | |
910 if (u->conf->ssl && c->ssl == NULL) { | |
911 ngx_http_upstream_ssl_init_connection(r, u, c); | |
912 return; | |
913 } | |
914 | |
915 #endif | |
916 | |
651 | 917 if (u->header_sent) { |
918 wev->handler = ngx_http_upstream_dummy_handler; | |
919 | |
920 (void) ngx_handle_write_event(wev, 0); | |
921 | |
922 return; | |
923 } | |
924 | |
479 | 925 ngx_http_upstream_send_request(r, u); |
926 } | |
927 | |
928 | |
487 | 929 static void |
930 ngx_http_upstream_process_header(ngx_event_t *rev) | |
479 | 931 { |
515 | 932 ssize_t n; |
933 ngx_int_t rc; | |
573 | 934 ngx_str_t *uri, args; |
649 | 935 ngx_uint_t i, flags; |
515 | 936 ngx_list_part_t *part; |
937 ngx_table_elt_t *h; | |
938 ngx_connection_t *c; | |
939 ngx_http_request_t *r; | |
940 ngx_http_upstream_t *u; | |
941 ngx_http_err_page_t *err_page; | |
942 ngx_http_core_loc_conf_t *clcf; | |
943 ngx_http_upstream_header_t *hh; | |
944 ngx_http_upstream_main_conf_t *umcf; | |
479 | 945 |
946 c = rev->data; | |
947 r = c->data; | |
948 u = r->upstream; | |
949 | |
950 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, | |
483 | 951 "http upstream process header"); |
479 | 952 |
483 | 953 c->log->action = "reading response header from upstream"; |
479 | 954 |
955 if (rev->timedout) { | |
956 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT); | |
957 return; | |
958 } | |
577 | 959 |
581 | 960 if (u->buffer.start == NULL) { |
961 u->buffer.start = ngx_palloc(r->pool, u->conf->buffer_size); | |
962 if (u->buffer.start == NULL) { | |
479 | 963 ngx_http_upstream_finalize_request(r, u, |
964 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
965 return; | |
966 } | |
967 | |
581 | 968 u->buffer.pos = u->buffer.start; |
969 u->buffer.last = u->buffer.start; | |
970 u->buffer.end = u->buffer.start + u->conf->buffer_size; | |
971 u->buffer.temporary = 1; | |
972 | |
973 u->buffer.tag = u->output.tag; | |
479 | 974 |
509 | 975 if (ngx_list_init(&r->upstream->headers_in.headers, r->pool, 8, |
581 | 976 sizeof(ngx_table_elt_t)) |
977 != NGX_OK) | |
509 | 978 { |
979 ngx_http_upstream_finalize_request(r, u, | |
980 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
981 return; | |
982 } | |
983 | |
479 | 984 #if 0 |
985 if (u->cache) { | |
581 | 986 u->buffer.pos += u->cache->ctx.header_size; |
987 u->buffer.last = u->buffer.pos; | |
479 | 988 } |
989 #endif | |
990 } | |
991 | |
581 | 992 n = u->peer.connection->recv(u->peer.connection, u->buffer.last, |
993 u->buffer.end - u->buffer.last); | |
479 | 994 |
995 if (n == NGX_AGAIN) { | |
996 #if 0 | |
997 ngx_add_timer(rev, u->read_timeout); | |
998 #endif | |
999 | |
1000 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { | |
1001 ngx_http_upstream_finalize_request(r, u, | |
1002 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1003 return; | |
1004 } | |
1005 | |
1006 return; | |
1007 } | |
1008 | |
1009 if (n == 0) { | |
1010 ngx_log_error(NGX_LOG_ERR, rev->log, 0, | |
1011 "upstream prematurely closed connection"); | |
1012 } | |
1013 | |
1014 if (n == NGX_ERROR || n == 0) { | |
1015 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); | |
1016 return; | |
1017 } | |
1018 | |
581 | 1019 u->buffer.last += n; |
479 | 1020 |
1021 #if 0 | |
1022 u->valid_header_in = 0; | |
1023 | |
1024 u->peer.cached = 0; | |
1025 #endif | |
1026 | |
1027 rc = u->process_header(r); | |
1028 | |
1029 if (rc == NGX_AGAIN) { | |
1030 #if 0 | |
1031 ngx_add_timer(rev, u->read_timeout); | |
1032 #endif | |
1033 | |
581 | 1034 if (u->buffer.pos == u->buffer.end) { |
479 | 1035 ngx_log_error(NGX_LOG_ERR, rev->log, 0, |
1036 "upstream sent too big header"); | |
1037 | |
1038 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER); | |
1039 return; | |
1040 } | |
1041 | |
1042 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { | |
1043 ngx_http_upstream_finalize_request(r, u, | |
1044 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1045 return; | |
1046 } | |
1047 | |
1048 return; | |
1049 } | |
1050 | |
1051 if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) { | |
1052 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER); | |
1053 return; | |
1054 } | |
1055 | |
1056 if (rc == NGX_ERROR || rc == NGX_HTTP_INTERNAL_SERVER_ERROR) { | |
1057 ngx_http_upstream_finalize_request(r, u, | |
1058 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1059 return; | |
1060 } | |
1061 | |
1062 /* rc == NGX_OK */ | |
1063 | |
529 | 1064 if (u->headers_in.status_n == NGX_HTTP_INTERNAL_SERVER_ERROR) { |
509 | 1065 |
1066 if (u->peer.tries > 1 | |
1067 && (u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_500)) | |
1068 { | |
1069 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_500); | |
1070 return; | |
1071 } | |
1072 | |
1073 #if (NGX_HTTP_CACHE) | |
1074 | |
1075 if (u->peer.tries == 0 | |
1076 && u->stale | |
1077 && (u->conf->use_stale & NGX_HTTP_UPSTREAM_FT_HTTP_500)) | |
1078 { | |
1079 ngx_http_upstream_finalize_request(r, u, | |
529 | 1080 ngx_http_send_cached_response(r)); |
509 | 1081 return; |
1082 } | |
1083 | |
1084 #endif | |
1085 } | |
1086 | |
597 | 1087 if (u->headers_in.status_n == NGX_HTTP_NOT_FOUND) { |
1088 | |
1089 if (u->peer.tries > 1 | |
1090 && u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_404) | |
1091 { | |
1092 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_404); | |
1093 return; | |
1094 } | |
1095 | |
675 | 1096 if (u->conf->intercept_404) { |
1097 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_NOT_FOUND); | |
597 | 1098 return; |
1099 } | |
509 | 1100 } |
1101 | |
1102 | |
529 | 1103 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST |
675 | 1104 && u->conf->intercept_errors) |
487 | 1105 { |
1106 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1107 | |
1108 if (clcf->error_pages) { | |
1109 | |
1110 err_page = clcf->error_pages->elts; | |
1111 for (i = 0; i < clcf->error_pages->nelts; i++) { | |
529 | 1112 if (err_page[i].status == (ngx_int_t) u->headers_in.status_n) { |
1113 | |
1114 if (u->headers_in.status_n == NGX_HTTP_UNAUTHORIZED) { | |
1115 | |
1116 r->headers_out.www_authenticate = | |
1117 ngx_list_push(&r->headers_out.headers); | |
1118 | |
1119 if (r->headers_out.www_authenticate == NULL) { | |
1120 ngx_http_upstream_finalize_request(r, u, | |
1121 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1122 return; | |
1123 } | |
1124 | |
577 | 1125 *r->headers_out.www_authenticate = |
529 | 1126 *u->headers_in.www_authenticate; |
1127 } | |
1128 | |
487 | 1129 ngx_http_upstream_finalize_request(r, u, |
529 | 1130 u->headers_in.status_n); |
487 | 1131 return; |
1132 } | |
1133 } | |
1134 } | |
1135 } | |
1136 | |
681 | 1137 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); |
1138 | |
515 | 1139 if (r->upstream->headers_in.x_accel_redirect) { |
1140 ngx_http_upstream_finalize_request(r, u, NGX_DECLINED); | |
1141 | |
1142 part = &r->upstream->headers_in.headers.part; | |
1143 h = part->elts; | |
1144 | |
1145 for (i = 0; /* void */; i++) { | |
1146 | |
1147 if (i >= part->nelts) { | |
1148 if (part->next == NULL) { | |
1149 break; | |
1150 } | |
577 | 1151 |
515 | 1152 part = part->next; |
1153 h = part->elts; | |
1154 i = 0; | |
1155 } | |
1156 | |
649 | 1157 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash, |
1158 h[i].lowcase_key, h[i].key.len); | |
1159 | |
1160 if (hh && hh->redirect) { | |
1161 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) { | |
515 | 1162 ngx_http_upstream_finalize_request(r, u, |
1163 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1164 return; | |
1165 } | |
1166 } | |
1167 } | |
1168 | |
573 | 1169 uri = &r->upstream->headers_in.x_accel_redirect->value; |
1170 args.len = 0; | |
1171 args.data = NULL; | |
1172 flags = 0; | |
1173 | |
1174 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) { | |
1175 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_NOT_FOUND); | |
1176 return; | |
1177 } | |
1178 | |
1179 if (flags & NGX_HTTP_ZERO_IN_URI) { | |
1180 r->zero_in_uri = 1; | |
1181 } | |
1182 | |
653 | 1183 if (r->method != NGX_HTTP_HEAD) { |
1184 r->method = NGX_HTTP_GET; | |
1185 } | |
1186 | |
573 | 1187 ngx_http_internal_redirect(r, uri, &args); |
515 | 1188 return; |
1189 } | |
1190 | |
509 | 1191 part = &r->upstream->headers_in.headers.part; |
1192 h = part->elts; | |
1193 | |
1194 for (i = 0; /* void */; i++) { | |
479 | 1195 |
509 | 1196 if (i >= part->nelts) { |
1197 if (part->next == NULL) { | |
1198 break; | |
1199 } | |
577 | 1200 |
509 | 1201 part = part->next; |
1202 h = part->elts; | |
1203 i = 0; | |
1204 } | |
1205 | |
649 | 1206 if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash, |
1207 h[i].lowcase_key, h[i].key.len)) | |
509 | 1208 { |
649 | 1209 continue; |
1210 } | |
1211 | |
1212 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash, | |
1213 h[i].lowcase_key, h[i].key.len); | |
1214 | |
1215 if (hh) { | |
1216 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) { | |
509 | 1217 ngx_http_upstream_finalize_request(r, u, |
1218 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1219 return; | |
1220 } | |
1221 | |
1222 continue; | |
1223 } | |
1224 | |
1225 if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) { | |
1226 ngx_http_upstream_finalize_request(r, u, | |
1227 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1228 return; | |
1229 } | |
1230 } | |
1231 | |
649 | 1232 if (r->headers_out.server && r->headers_out.server->value.data == NULL) { |
1233 r->headers_out.server->hash = 0; | |
1234 } | |
1235 | |
1236 if (r->headers_out.date && r->headers_out.date->value.data == NULL) { | |
1237 r->headers_out.date->hash = 0; | |
1238 } | |
1239 | |
529 | 1240 r->headers_out.status = u->headers_in.status_n; |
1241 r->headers_out.status_line = u->headers_in.status_line; | |
1242 | |
581 | 1243 if (r->headers_out.content_length_n != -1) { |
1244 u->length = (size_t) r->headers_out.content_length_n; | |
1245 | |
1246 } else { | |
1247 u->length = NGX_MAX_SIZE_T_VALUE; | |
1248 } | |
1249 | |
681 | 1250 ngx_http_upstream_send_response(r, u); |
1251 } | |
1252 | |
1253 | |
1254 static void | |
1255 ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u) | |
1256 { | |
1257 int tcp_nodelay; | |
1258 ssize_t size; | |
1259 ngx_int_t rc; | |
1260 ngx_event_pipe_t *p; | |
1261 ngx_connection_t *c; | |
1262 ngx_pool_cleanup_t *cl; | |
1263 ngx_pool_cleanup_file_t *clf; | |
1264 ngx_http_core_loc_conf_t *clcf; | |
1265 | |
509 | 1266 rc = ngx_http_send_header(r); |
479 | 1267 |
629 | 1268 if (rc == NGX_ERROR || rc > NGX_OK || r->post_action) { |
485 | 1269 ngx_http_upstream_finalize_request(r, u, rc); |
479 | 1270 return; |
1271 } | |
1272 | |
1273 u->header_sent = 1; | |
1274 | |
573 | 1275 if (r->request_body && r->request_body->temp_file) { |
563 | 1276 for (cl = r->pool->cleanup; cl; cl = cl->next) { |
1277 if (cl->handler == ngx_pool_cleanup_file) { | |
1278 clf = cl->data; | |
1279 | |
1280 if (clf->fd == r->request_body->temp_file->file.fd) { | |
1281 cl->handler(clf); | |
1282 cl->handler = NULL; | |
659 | 1283 r->request_body->temp_file->file.fd = NGX_INVALID_FILE; |
563 | 1284 break; |
1285 } | |
1286 } | |
1287 } | |
1288 } | |
1289 | |
583 | 1290 c = r->connection; |
1291 | |
581 | 1292 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
1293 | |
649 | 1294 if (!u->buffering) { |
581 | 1295 |
1296 if (u->input_filter == NULL) { | |
1297 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init; | |
1298 u->input_filter = ngx_http_upstream_non_buffered_filter; | |
1299 u->input_filter_ctx = r; | |
1300 } | |
1301 | |
1302 u->peer.connection->read->handler = | |
1303 ngx_http_upstream_process_non_buffered_body; | |
1304 r->write_event_handler = | |
1305 ngx_http_upstream_process_non_buffered_downstream; | |
1306 | |
1307 r->limit_rate = 0; | |
1308 | |
1309 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) { | |
1310 ngx_http_upstream_finalize_request(r, u, 0); | |
1311 return; | |
1312 } | |
1313 | |
583 | 1314 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { |
1315 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay"); | |
1316 | |
1317 tcp_nodelay = 1; | |
1318 | |
1319 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, | |
1320 (const void *) &tcp_nodelay, sizeof(int)) == -1) | |
1321 { | |
1322 ngx_connection_error(c, ngx_socket_errno, | |
1323 "setsockopt(TCP_NODELAY) failed"); | |
1324 ngx_http_upstream_finalize_request(r, u, 0); | |
1325 return; | |
1326 } | |
1327 | |
1328 c->tcp_nodelay = NGX_TCP_NODELAY_SET; | |
1329 } | |
1330 | |
581 | 1331 size = u->buffer.last - u->buffer.pos; |
1332 | |
1333 if (size) { | |
1334 u->buffer.last = u->buffer.pos; | |
1335 | |
1336 if (u->input_filter(u->input_filter_ctx, size) == NGX_ERROR) { | |
1337 ngx_http_upstream_finalize_request(r, u, 0); | |
1338 return; | |
1339 } | |
1340 | |
583 | 1341 ngx_http_upstream_process_non_buffered_body(c->write); |
581 | 1342 |
1343 } else { | |
1344 u->buffer.pos = u->buffer.start; | |
1345 u->buffer.last = u->buffer.start; | |
1346 | |
1347 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) { | |
1348 ngx_http_upstream_finalize_request(r, u, 0); | |
1349 return; | |
1350 } | |
1351 } | |
1352 | |
1353 return; | |
1354 } | |
1355 | |
479 | 1356 /* TODO: preallocate event_pipe bufs, look "Content-Length" */ |
1357 | |
1358 #if 0 | |
1359 | |
1360 if (u->cache && u->cache->ctx.file.fd != NGX_INVALID_FILE) { | |
1361 if (ngx_close_file(u->cache->ctx.file.fd) == NGX_FILE_ERROR) { | |
583 | 1362 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
479 | 1363 ngx_close_file_n " \"%s\" failed", |
1364 u->cache->ctx.file.name.data); | |
1365 } | |
1366 } | |
1367 | |
1368 if (u->cachable) { | |
581 | 1369 header = (ngx_http_cache_header_t *) u->buffer->start; |
479 | 1370 |
1371 header->expires = u->cache->ctx.expires; | |
1372 header->last_modified = u->cache->ctx.last_modified; | |
1373 header->date = u->cache->ctx.date; | |
1374 header->length = r->headers_out.content_length_n; | |
1375 u->cache->ctx.length = r->headers_out.content_length_n; | |
1376 | |
1377 header->key_len = u->cache->ctx.key0.len; | |
1378 ngx_memcpy(&header->key, u->cache->ctx.key0.data, header->key_len); | |
1379 header->key[header->key_len] = LF; | |
1380 } | |
1381 | |
1382 #endif | |
1383 | |
581 | 1384 p = u->pipe; |
479 | 1385 |
1386 p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter; | |
1387 p->output_ctx = r; | |
1388 p->tag = u->output.tag; | |
577 | 1389 p->bufs = u->conf->bufs; |
479 | 1390 p->busy_size = u->conf->busy_buffers_size; |
1391 p->upstream = u->peer.connection; | |
583 | 1392 p->downstream = c; |
479 | 1393 p->pool = r->pool; |
583 | 1394 p->log = c->log; |
577 | 1395 |
479 | 1396 p->cachable = u->cachable; |
1397 | |
501 | 1398 p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)); |
1399 if (p->temp_file == NULL) { | |
479 | 1400 ngx_http_upstream_finalize_request(r, u, 0); |
1401 return; | |
1402 } | |
1403 | |
1404 p->temp_file->file.fd = NGX_INVALID_FILE; | |
583 | 1405 p->temp_file->file.log = c->log; |
479 | 1406 p->temp_file->path = u->conf->temp_path; |
1407 p->temp_file->pool = r->pool; | |
577 | 1408 |
479 | 1409 if (u->cachable) { |
1410 p->temp_file->persistent = 1; | |
1411 } else { | |
667 | 1412 p->temp_file->log_level = NGX_LOG_WARN; |
479 | 1413 p->temp_file->warn = "an upstream response is buffered " |
1414 "to a temporary file"; | |
1415 } | |
1416 | |
1417 p->max_temp_file_size = u->conf->max_temp_file_size; | |
1418 p->temp_file_write_size = u->conf->temp_file_write_size; | |
1419 | |
501 | 1420 p->preread_bufs = ngx_alloc_chain_link(r->pool); |
1421 if (p->preread_bufs == NULL) { | |
479 | 1422 ngx_http_upstream_finalize_request(r, u, 0); |
1423 return; | |
1424 } | |
581 | 1425 p->preread_bufs->buf = &u->buffer; |
479 | 1426 p->preread_bufs->next = NULL; |
581 | 1427 u->buffer.recycled = 1; |
1428 | |
1429 p->preread_size = u->buffer.last - u->buffer.pos; | |
479 | 1430 |
1431 if (u->cachable) { | |
1432 p->buf_to_file = ngx_calloc_buf(r->pool); | |
1433 if (p->buf_to_file == NULL) { | |
1434 ngx_http_upstream_finalize_request(r, u, 0); | |
1435 return; | |
1436 } | |
581 | 1437 p->buf_to_file->pos = u->buffer.start; |
1438 p->buf_to_file->last = u->buffer.pos; | |
479 | 1439 p->buf_to_file->temporary = 1; |
1440 } | |
1441 | |
1442 if (ngx_event_flags & NGX_USE_AIO_EVENT) { | |
1443 /* the posted aio operation may currupt a shadow buffer */ | |
1444 p->single_buf = 1; | |
1445 } | |
1446 | |
1447 /* TODO: p->free_bufs = 0 if use ngx_create_chain_of_bufs() */ | |
1448 p->free_bufs = 1; | |
1449 | |
1450 /* | |
581 | 1451 * event_pipe would do u->buffer.last += p->preread_size |
479 | 1452 * as though these bytes were read |
1453 */ | |
581 | 1454 u->buffer.last = u->buffer.pos; |
479 | 1455 |
1456 if (u->conf->cyclic_temp_file) { | |
1457 | |
1458 /* | |
1459 * we need to disable the use of sendfile() if we use cyclic temp file | |
1460 * because the writing a new data may interfere with sendfile() | |
1461 * that uses the same kernel file pages (at least on FreeBSD) | |
1462 */ | |
1463 | |
1464 p->cyclic_temp_file = 1; | |
583 | 1465 c->sendfile = 0; |
479 | 1466 |
1467 } else { | |
1468 p->cyclic_temp_file = 0; | |
1469 } | |
1470 | |
1471 p->read_timeout = u->conf->read_timeout; | |
1472 p->send_timeout = clcf->send_timeout; | |
1473 p->send_lowat = clcf->send_lowat; | |
1474 | |
509 | 1475 u->peer.connection->read->handler = ngx_http_upstream_process_body; |
1476 r->write_event_handler = ngx_http_upstream_process_downstream; | |
479 | 1477 |
1478 ngx_http_upstream_process_body(u->peer.connection->read); | |
1479 } | |
1480 | |
1481 | |
487 | 1482 static void |
581 | 1483 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r) |
1484 { | |
1485 ngx_http_upstream_process_non_buffered_body(r->connection->write); | |
1486 } | |
1487 | |
1488 | |
1489 static void | |
1490 ngx_http_upstream_process_non_buffered_body(ngx_event_t *ev) | |
1491 { | |
1492 size_t size; | |
1493 ssize_t n; | |
1494 ngx_buf_t *b; | |
583 | 1495 ngx_int_t rc; |
581 | 1496 ngx_uint_t do_write; |
583 | 1497 ngx_connection_t *c, *client; |
581 | 1498 ngx_http_request_t *r; |
1499 ngx_http_upstream_t *u; | |
1500 ngx_http_core_loc_conf_t *clcf; | |
1501 | |
1502 c = ev->data; | |
1503 | |
1504 if (ev->write) { | |
1505 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1506 "http upstream process non buffered downstream"); | |
1507 c->log->action = "sending to client"; | |
1508 | |
1509 } else { | |
1510 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1511 "http upstream process non buffered upstream"); | |
1512 c->log->action = "reading upstream"; | |
1513 } | |
1514 | |
1515 if (ev->timedout) { | |
1516 if (ev->write) { | |
1517 c->timedout = 1; | |
611 | 1518 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out"); |
1519 | |
581 | 1520 } else { |
611 | 1521 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
581 | 1522 } |
755
63b9dc652c3d
finalize nonbuffered request on timeout
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
1523 |
63b9dc652c3d
finalize nonbuffered request on timeout
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
1524 ngx_http_upstream_finalize_request(r, u, 0); |
63b9dc652c3d
finalize nonbuffered request on timeout
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
1525 return; |
581 | 1526 } |
1527 | |
1528 r = c->data; | |
1529 u = r->upstream; | |
583 | 1530 client = r->connection; |
581 | 1531 |
1532 b = &u->buffer; | |
1533 | |
1534 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1535 | |
657 | 1536 do_write = ev->write || u->length == 0; |
581 | 1537 |
1538 for ( ;; ) { | |
1539 | |
1540 if (do_write) { | |
1541 | |
1542 if (u->out_bufs || u->busy_bufs) { | |
583 | 1543 rc = ngx_http_output_filter(r, u->out_bufs); |
1544 | |
1545 if (client->destroyed) { | |
1546 return; | |
1547 } | |
1548 | |
1549 if (rc == NGX_ERROR) { | |
581 | 1550 ngx_http_upstream_finalize_request(r, u, 0); |
1551 return; | |
1552 } | |
1553 | |
1554 ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs, | |
1555 &u->out_bufs, u->output.tag); | |
1556 } | |
1557 | |
1558 if (u->busy_bufs == NULL) { | |
1559 | |
1560 if (u->length == 0 | |
1561 || u->peer.connection->read->eof | |
1562 || u->peer.connection->read->error) | |
1563 { | |
1564 ngx_http_upstream_finalize_request(r, u, 0); | |
1565 return; | |
1566 } | |
1567 | |
1568 b->pos = b->start; | |
1569 b->last = b->start; | |
1570 } | |
1571 } | |
1572 | |
1573 size = b->end - b->last; | |
1574 | |
1575 if (size > u->length) { | |
1576 size = u->length; | |
1577 } | |
1578 | |
1579 if (size && u->peer.connection->read->ready) { | |
657 | 1580 |
581 | 1581 n = u->peer.connection->recv(u->peer.connection, b->last, size); |
1582 | |
1583 if (n == NGX_AGAIN) { | |
1584 break; | |
1585 } | |
1586 | |
1587 if (n > 0) { | |
1588 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { | |
1589 ngx_http_upstream_finalize_request(r, u, 0); | |
1590 return; | |
1591 } | |
1592 } | |
1593 | |
1594 do_write = 1; | |
1595 | |
1596 continue; | |
1597 } | |
1598 | |
1599 break; | |
1600 } | |
1601 | |
583 | 1602 if (client->data == r) { |
1603 if (ngx_handle_write_event(client->write, clcf->send_lowat) | |
1604 == NGX_ERROR) | |
1605 { | |
1606 ngx_http_upstream_finalize_request(r, u, 0); | |
1607 return; | |
1608 } | |
581 | 1609 } |
1610 | |
583 | 1611 if (client->write->active) { |
1612 ngx_add_timer(client->write, clcf->send_timeout); | |
1613 | |
1614 } else if (client->write->timer_set) { | |
1615 ngx_del_timer(client->write); | |
581 | 1616 } |
1617 | |
1618 if (ngx_handle_read_event(u->peer.connection->read, 0) == NGX_ERROR) { | |
1619 ngx_http_upstream_finalize_request(r, u, 0); | |
1620 return; | |
1621 } | |
1622 | |
1623 if (u->peer.connection->read->active) { | |
1624 ngx_add_timer(u->peer.connection->read, u->conf->read_timeout); | |
1625 | |
583 | 1626 } else if (u->peer.connection->read->timer_set) { |
1627 ngx_del_timer(u->peer.connection->read); | |
581 | 1628 } |
1629 } | |
1630 | |
1631 | |
1632 static ngx_int_t | |
1633 ngx_http_upstream_non_buffered_filter_init(void *data) | |
1634 { | |
1635 return NGX_OK; | |
1636 } | |
1637 | |
1638 | |
1639 static ngx_int_t | |
1640 ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes) | |
1641 { | |
1642 ngx_http_request_t *r = data; | |
1643 | |
1644 ngx_buf_t *b; | |
1645 ngx_chain_t *cl, **ll; | |
1646 ngx_http_upstream_t *u; | |
1647 | |
1648 u = r->upstream; | |
1649 | |
1650 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { | |
1651 ll = &cl->next; | |
1652 } | |
1653 | |
1654 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); | |
1655 if (cl == NULL) { | |
1656 return NGX_ERROR; | |
1657 } | |
1658 | |
1659 *ll = cl; | |
1660 | |
1661 cl->buf->flush = 1; | |
1662 cl->buf->memory = 1; | |
1663 | |
1664 b = &u->buffer; | |
1665 | |
1666 cl->buf->pos = b->last; | |
1667 b->last += bytes; | |
1668 cl->buf->last = b->last; | |
1669 | |
1670 if (u->length == NGX_MAX_SIZE_T_VALUE) { | |
1671 return NGX_OK; | |
1672 } | |
1673 | |
1674 u->length -= bytes; | |
1675 | |
1676 return NGX_OK; | |
1677 } | |
1678 | |
1679 | |
1680 static void | |
509 | 1681 ngx_http_upstream_process_downstream(ngx_http_request_t *r) |
1682 { | |
1683 ngx_http_upstream_process_body(r->connection->write); | |
1684 } | |
1685 | |
1686 | |
1687 static void | |
487 | 1688 ngx_http_upstream_process_body(ngx_event_t *ev) |
479 | 1689 { |
509 | 1690 ngx_event_pipe_t *p; |
583 | 1691 ngx_connection_t *c, *downstream; |
479 | 1692 ngx_http_request_t *r; |
1693 ngx_http_upstream_t *u; | |
1694 | |
1695 c = ev->data; | |
1696 r = c->data; | |
1697 u = r->upstream; | |
583 | 1698 downstream = r->connection; |
479 | 1699 |
1700 if (ev->write) { | |
483 | 1701 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1702 "http upstream process downstream"); | |
1703 c->log->action = "sending to client"; | |
577 | 1704 |
479 | 1705 } else { |
483 | 1706 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1707 "http upstream process upstream"); | |
1708 c->log->action = "reading upstream"; | |
479 | 1709 } |
577 | 1710 |
581 | 1711 p = u->pipe; |
479 | 1712 |
1713 if (ev->timedout) { | |
1714 if (ev->write) { | |
527 | 1715 if (ev->delayed) { |
1716 | |
1717 ev->timedout = 0; | |
1718 ev->delayed = 0; | |
1719 | |
1720 if (!ev->ready) { | |
1721 ngx_add_timer(ev, p->send_timeout); | |
1722 | |
1723 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) | |
1724 { | |
1725 ngx_http_upstream_finalize_request(r, u, 0); | |
1726 return; | |
1727 } | |
1728 | |
1729 return; | |
1730 } | |
1731 | |
1732 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) { | |
583 | 1733 |
1734 if (downstream->destroyed) { | |
1735 return; | |
1736 } | |
1737 | |
527 | 1738 ngx_http_upstream_finalize_request(r, u, 0); |
1739 return; | |
1740 } | |
1741 | |
1742 } else { | |
1743 p->downstream_error = 1; | |
577 | 1744 c->timedout = 1; |
611 | 1745 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out"); |
527 | 1746 } |
479 | 1747 |
1748 } else { | |
577 | 1749 p->upstream_error = 1; |
611 | 1750 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
479 | 1751 } |
1752 | |
1753 } else { | |
527 | 1754 if (ev->write && ev->delayed) { |
1755 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1756 "http downstream delayed"); | |
1757 | |
1758 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) { | |
1759 return; | |
1760 } | |
1761 | |
1762 return; | |
1763 } | |
1764 | |
479 | 1765 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) { |
583 | 1766 |
1767 if (downstream->destroyed) { | |
1768 return; | |
1769 } | |
1770 | |
479 | 1771 ngx_http_upstream_finalize_request(r, u, 0); |
1772 return; | |
1773 } | |
1774 } | |
577 | 1775 |
479 | 1776 if (u->peer.connection) { |
1777 | |
1778 #if (NGX_HTTP_FILE_CACHE) | |
1779 | |
1780 if (p->upstream_done && u->cachable) { | |
1781 if (ngx_http_cache_update(r) == NGX_ERROR) { | |
1782 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1783 ngx_http_upstream_finalize_request(r, u, 0); | |
1784 return; | |
1785 } | |
1786 | |
1787 } else if (p->upstream_eof && u->cachable) { | |
1788 | |
1789 /* TODO: check length & update cache */ | |
1790 | |
1791 if (ngx_http_cache_update(r) == NGX_ERROR) { | |
1792 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1793 ngx_http_upstream_finalize_request(r, u, 0); | |
1794 return; | |
1795 } | |
1796 } | |
1797 | |
1798 #endif | |
1799 | |
1800 if (p->upstream_done || p->upstream_eof || p->upstream_error) { | |
483 | 1801 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1802 "http upstream exit: %p", p->out); | |
479 | 1803 #if 0 |
1804 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1805 #endif | |
1806 ngx_http_upstream_finalize_request(r, u, 0); | |
1807 return; | |
1808 } | |
1809 } | |
1810 | |
1811 if (p->downstream_error) { | |
483 | 1812 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1813 "http upstream downstream error"); | |
479 | 1814 |
1815 if (!u->cachable && u->peer.connection) { | |
1816 ngx_http_upstream_finalize_request(r, u, 0); | |
1817 } | |
1818 } | |
1819 } | |
1820 | |
1821 | |
487 | 1822 static void |
1823 ngx_http_upstream_dummy_handler(ngx_event_t *wev) | |
479 | 1824 { |
1825 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, | |
1826 "http upstream dummy handler"); | |
1827 } | |
1828 | |
1829 | |
487 | 1830 static void |
1831 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u, | |
1832 ngx_uint_t ft_type) | |
479 | 1833 { |
525 | 1834 ngx_uint_t status, down; |
479 | 1835 |
1836 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1837 "http next upstream, %xD", ft_type); | |
1838 | |
1839 #if 0 | |
1840 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1841 #endif | |
1842 | |
525 | 1843 if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) { |
1844 down = 0; | |
1845 } else { | |
1846 down = 1; | |
479 | 1847 } |
525 | 1848 |
1849 ngx_event_connect_peer_failed(&u->peer, down); | |
1850 | |
479 | 1851 if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) { |
1852 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT, | |
1853 "upstream timed out"); | |
1854 } | |
577 | 1855 |
479 | 1856 if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) { |
1857 status = 0; | |
1858 | |
1859 } else { | |
1860 switch(ft_type) { | |
1861 | |
1862 case NGX_HTTP_UPSTREAM_FT_TIMEOUT: | |
1863 status = NGX_HTTP_GATEWAY_TIME_OUT; | |
1864 break; | |
1865 | |
1866 case NGX_HTTP_UPSTREAM_FT_HTTP_500: | |
1867 status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1868 break; | |
1869 | |
1870 case NGX_HTTP_UPSTREAM_FT_HTTP_404: | |
1871 status = NGX_HTTP_NOT_FOUND; | |
1872 break; | |
1873 | |
1874 /* | |
1875 * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING | |
1876 * never reach here | |
1877 */ | |
1878 | |
1879 default: | |
1880 status = NGX_HTTP_BAD_GATEWAY; | |
1881 } | |
1882 } | |
1883 | |
583 | 1884 if (r->connection->error) { |
479 | 1885 ngx_http_upstream_finalize_request(r, u, |
1886 NGX_HTTP_CLIENT_CLOSED_REQUEST); | |
1887 return; | |
1888 } | |
1889 | |
1890 if (status) { | |
1891 u->state->status = status; | |
1892 | |
525 | 1893 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) { |
479 | 1894 |
1895 #if (NGX_HTTP_CACHE) | |
1896 | |
1897 if (u->stale && (u->conf->use_stale & ft_type)) { | |
1898 ngx_http_upstream_finalize_request(r, u, | |
525 | 1899 ngx_http_send_cached_response(r)); |
479 | 1900 return; |
1901 } | |
1902 | |
1903 #endif | |
1904 | |
1905 ngx_http_upstream_finalize_request(r, u, status); | |
1906 return; | |
1907 } | |
1908 } | |
1909 | |
1910 if (u->peer.connection) { | |
1911 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1912 "close http upstream connection: %d", | |
1913 u->peer.connection->fd); | |
577 | 1914 #if (NGX_HTTP_SSL) |
1915 if (u->peer.connection->ssl) { | |
1916 ngx_http_upstream_ssl_shutdown(u->peer.connection, | |
1917 &u->peer.peers->peer[u->peer.cur_peer]); | |
1918 } | |
1919 #endif | |
479 | 1920 ngx_close_connection(u->peer.connection); |
1921 } | |
1922 | |
1923 #if 0 | |
1924 if (u->conf->busy_lock && !u->busy_locked) { | |
1925 ngx_http_upstream_busy_lock(p); | |
1926 return; | |
1927 } | |
1928 #endif | |
1929 | |
1930 ngx_http_upstream_connect(r, u); | |
1931 } | |
1932 | |
1933 | |
487 | 1934 static void |
569 | 1935 ngx_http_upstream_cleanup(void *data) |
1936 { | |
1937 ngx_http_request_t *r = data; | |
1938 | |
1939 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1940 "cleanup http upstream request: \"%V\"", &r->uri); | |
1941 | |
1942 ngx_http_upstream_finalize_request(r, r->upstream, NGX_DONE); | |
1943 } | |
1944 | |
1945 | |
1946 static void | |
487 | 1947 ngx_http_upstream_finalize_request(ngx_http_request_t *r, |
1948 ngx_http_upstream_t *u, ngx_int_t rc) | |
479 | 1949 { |
563 | 1950 ngx_time_t *tp; |
1951 | |
483 | 1952 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1953 "finalize http upstream request: %i", rc); | |
479 | 1954 |
569 | 1955 *u->cleanup = NULL; |
1956 | |
563 | 1957 if (u->state->response_time) { |
1958 tp = ngx_timeofday(); | |
1959 u->state->response_time = tp->sec * 1000 + tp->msec | |
1960 - u->state->response_time; | |
1961 } | |
1962 | |
479 | 1963 u->finalize_request(r, rc); |
1964 | |
1965 if (u->peer.connection) { | |
1966 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1967 "close http upstream connection: %d", | |
1968 u->peer.connection->fd); | |
577 | 1969 #if (NGX_HTTP_SSL) |
1970 | |
1971 /* TODO: do not shutdown persistent connection */ | |
1972 | |
1973 if (u->peer.connection->ssl) { | |
1974 ngx_http_upstream_ssl_shutdown(u->peer.connection, | |
1975 &u->peer.peers->peer[u->peer.cur_peer]); | |
1976 } | |
1977 #endif | |
479 | 1978 ngx_close_connection(u->peer.connection); |
1979 } | |
1980 | |
483 | 1981 u->peer.connection = NULL; |
1982 | |
563 | 1983 if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE)) |
479 | 1984 { |
1985 rc = 0; | |
1986 } | |
1987 | |
581 | 1988 if (u->pipe && u->pipe->temp_file) { |
479 | 1989 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1990 "http upstream temp fd: %d", | |
581 | 1991 u->pipe->temp_file->file.fd); |
479 | 1992 } |
1993 | |
1994 #if 0 | |
1995 if (u->cache) { | |
1996 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
483 | 1997 "http upstream cache fd: %d", |
479 | 1998 u->cache->ctx.file.fd); |
1999 } | |
2000 #endif | |
2001 | |
515 | 2002 if (rc == NGX_DECLINED) { |
2003 return; | |
2004 } | |
2005 | |
483 | 2006 r->connection->log->action = "sending to client"; |
2007 | |
629 | 2008 if (rc == 0 && r == r->main && !r->post_action) { |
581 | 2009 rc = ngx_http_send_special(r, NGX_HTTP_LAST); |
479 | 2010 } |
2011 | |
2012 ngx_http_finalize_request(r, rc); | |
2013 } | |
2014 | |
2015 | |
577 | 2016 #if (NGX_HTTP_SSL) |
2017 | |
2018 static void | |
2019 ngx_http_upstream_ssl_shutdown(ngx_connection_t *c, ngx_peer_t *peer) | |
2020 { | |
2021 /* lock peer mutex */ | |
2022 | |
2023 if (peer->ssl_session) { | |
2024 ngx_ssl_free_session(peer->ssl_session); | |
2025 } | |
2026 | |
2027 peer->ssl_session = ngx_ssl_get_session(c); | |
2028 | |
2029 /* unlock peer mutex */ | |
2030 | |
2031 /* | |
2032 * We send the "close notify" shutdown alert to the upstream only | |
2033 * and do not wait its "close notify" shutdown alert. | |
2034 * It is acceptable according to the TLS standard. | |
2035 */ | |
2036 | |
2037 c->ssl->no_wait_shutdown = 1; | |
2038 | |
2039 (void) ngx_ssl_shutdown(c); | |
2040 } | |
2041 | |
2042 #endif | |
2043 | |
2044 | |
509 | 2045 static ngx_int_t |
2046 ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2047 ngx_uint_t offset) | |
577 | 2048 { |
509 | 2049 ngx_table_elt_t **ph; |
577 | 2050 |
509 | 2051 ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset); |
2052 | |
2053 if (*ph == NULL) { | |
2054 *ph = h; | |
2055 } | |
2056 | |
2057 return NGX_OK; | |
2058 } | |
2059 | |
2060 | |
2061 static ngx_int_t | |
2062 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r, | |
2063 ngx_table_elt_t *h, ngx_uint_t offset) | |
2064 { | |
2065 ngx_array_t *pa; | |
2066 ngx_table_elt_t **ph; | |
2067 | |
2068 pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset); | |
2069 | |
2070 if (pa->elts == NULL) { | |
2071 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) | |
2072 { | |
2073 return NGX_ERROR; | |
2074 } | |
2075 } | |
2076 | |
2077 ph = ngx_array_push(pa); | |
2078 if (ph == NULL) { | |
2079 return NGX_ERROR; | |
2080 } | |
2081 | |
2082 *ph = h; | |
2083 | |
2084 return NGX_OK; | |
2085 } | |
2086 | |
2087 | |
2088 static ngx_int_t | |
2089 ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2090 ngx_uint_t offset) | |
2091 { | |
2092 return NGX_OK; | |
2093 } | |
2094 | |
2095 | |
2096 static ngx_int_t | |
527 | 2097 ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h, |
2098 ngx_uint_t offset) | |
577 | 2099 { |
527 | 2100 ngx_int_t n; |
2101 | |
2102 r->upstream->headers_in.x_accel_limit_rate = h; | |
2103 | |
2104 n = ngx_atoi(h->value.data, h->value.len); | |
2105 | |
2106 if (n != NGX_ERROR) { | |
2107 r->limit_rate = (size_t) n; | |
2108 } | |
2109 | |
2110 return NGX_OK; | |
2111 } | |
2112 | |
2113 | |
2114 static ngx_int_t | |
649 | 2115 ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h, |
2116 ngx_uint_t offset) | |
2117 { | |
2118 u_char c0, c1, c2; | |
2119 | |
2120 if (r->upstream->conf->change_buffering) { | |
2121 | |
2122 if (h->value.len == 2) { | |
2123 c0 = ngx_tolower(h->value.data[0]); | |
2124 c1 = ngx_tolower(h->value.data[1]); | |
2125 | |
2126 if (c0 == 'n' && c1 == 'o') { | |
2127 r->upstream->buffering = 0; | |
2128 } | |
2129 | |
2130 } else if (h->value.len == 3) { | |
2131 c0 = ngx_tolower(h->value.data[0]); | |
2132 c1 = ngx_tolower(h->value.data[1]); | |
2133 c2 = ngx_tolower(h->value.data[2]); | |
2134 | |
2135 if (c0 == 'y' && c1 == 'e' && c2 == 's') { | |
2136 r->upstream->buffering = 1; | |
2137 } | |
2138 } | |
2139 } | |
2140 | |
2141 return NGX_OK; | |
2142 } | |
2143 | |
2144 | |
2145 static ngx_int_t | |
657 | 2146 ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h, |
2147 ngx_uint_t offset) | |
2148 { | |
2149 r->headers_out.override_charset = &h->value; | |
2150 | |
2151 return NGX_OK; | |
2152 } | |
2153 | |
2154 | |
2155 static ngx_int_t | |
509 | 2156 ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, |
2157 ngx_uint_t offset) | |
2158 { | |
573 | 2159 ngx_table_elt_t *ho, **ph; |
509 | 2160 |
2161 ho = ngx_list_push(&r->headers_out.headers); | |
2162 if (ho == NULL) { | |
2163 return NGX_ERROR; | |
2164 } | |
2165 | |
2166 *ho = *h; | |
2167 | |
573 | 2168 if (offset) { |
2169 ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset); | |
2170 *ph = ho; | |
2171 } | |
2172 | |
509 | 2173 return NGX_OK; |
2174 } | |
2175 | |
2176 | |
2177 static ngx_int_t | |
2178 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r, | |
2179 ngx_table_elt_t *h, ngx_uint_t offset) | |
2180 { | |
2181 ngx_array_t *pa; | |
2182 ngx_table_elt_t *ho, **ph; | |
2183 | |
2184 pa = (ngx_array_t *) ((char *) &r->headers_out + offset); | |
2185 | |
2186 if (pa->elts == NULL) { | |
2187 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) | |
2188 { | |
2189 return NGX_ERROR; | |
2190 } | |
2191 } | |
2192 | |
2193 ph = ngx_array_push(pa); | |
2194 if (ph == NULL) { | |
2195 return NGX_ERROR; | |
2196 } | |
2197 | |
2198 ho = ngx_list_push(&r->headers_out.headers); | |
2199 if (ho == NULL) { | |
2200 return NGX_ERROR; | |
2201 } | |
2202 | |
2203 *ho = *h; | |
2204 *ph = ho; | |
2205 | |
2206 return NGX_OK; | |
2207 } | |
2208 | |
2209 | |
2210 static ngx_int_t | |
2211 ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2212 ngx_uint_t offset) | |
2213 { | |
657 | 2214 u_char *p, *last; |
2215 | |
2216 r->headers_out.content_type_len = h->value.len; | |
509 | 2217 r->headers_out.content_type = h->value; |
2218 | |
657 | 2219 for (p = h->value.data; *p; p++) { |
2220 | |
2221 if (*p != ';') { | |
2222 continue; | |
2223 } | |
2224 | |
2225 last = p; | |
2226 | |
2227 while (*++p == ' ') { /* void */ } | |
2228 | |
2229 if (ngx_strncasecmp(p, "charset=", 8) != 0) { | |
2230 continue; | |
2231 } | |
2232 | |
2233 p += 8; | |
2234 | |
2235 r->headers_out.content_type_len = last - h->value.data; | |
2236 | |
2237 r->headers_out.charset.len = h->value.data + h->value.len - p; | |
2238 r->headers_out.charset.data = p; | |
2239 } | |
2240 | |
509 | 2241 return NGX_OK; |
2242 } | |
2243 | |
2244 | |
2245 static ngx_int_t | |
2246 ngx_http_upstream_copy_content_length(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2247 ngx_uint_t offset) | |
2248 { | |
2249 ngx_table_elt_t *ho; | |
2250 | |
2251 ho = ngx_list_push(&r->headers_out.headers); | |
2252 if (ho == NULL) { | |
2253 return NGX_ERROR; | |
2254 } | |
2255 | |
2256 *ho = *h; | |
2257 | |
2258 r->headers_out.content_length = ho; | |
2259 r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len); | |
2260 | |
2261 return NGX_OK; | |
2262 } | |
2263 | |
2264 | |
2265 static ngx_int_t | |
2266 ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2267 ngx_uint_t offset) | |
2268 { | |
2269 ngx_int_t rc; | |
2270 ngx_table_elt_t *ho; | |
2271 | |
2272 ho = ngx_list_push(&r->headers_out.headers); | |
2273 if (ho == NULL) { | |
2274 return NGX_ERROR; | |
2275 } | |
2276 | |
2277 *ho = *h; | |
2278 | |
2279 if (r->upstream->rewrite_redirect) { | |
2280 rc = r->upstream->rewrite_redirect(r, ho, 0); | |
2281 | |
529 | 2282 if (rc == NGX_DECLINED) { |
2283 return NGX_OK; | |
2284 } | |
2285 | |
509 | 2286 if (rc == NGX_OK) { |
2287 r->headers_out.location = ho; | |
2288 | |
2289 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2290 "rewritten location: \"%V\"", &ho->value); | |
2291 } | |
2292 | |
2293 return rc; | |
2294 } | |
2295 | |
2296 /* | |
2297 * we do not set r->headers_out.location here to avoid the handling | |
2298 * the local redirects without a host name by ngx_http_header_filter() | |
2299 */ | |
2300 | |
2301 return NGX_OK; | |
2302 } | |
2303 | |
2304 | |
2305 static ngx_int_t | |
2306 ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2307 ngx_uint_t offset) | |
2308 { | |
2309 u_char *p; | |
2310 ngx_int_t rc; | |
2311 ngx_table_elt_t *ho; | |
2312 | |
2313 ho = ngx_list_push(&r->headers_out.headers); | |
2314 if (ho == NULL) { | |
2315 return NGX_ERROR; | |
2316 } | |
2317 | |
2318 *ho = *h; | |
2319 | |
2320 if (r->upstream->rewrite_redirect) { | |
2321 | |
2322 p = (u_char *) ngx_strstr(ho->value.data, "url="); | |
2323 | |
2324 if (p) { | |
2325 rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data); | |
2326 | |
2327 } else { | |
2328 return NGX_OK; | |
2329 } | |
2330 | |
529 | 2331 if (rc == NGX_DECLINED) { |
2332 return NGX_OK; | |
2333 } | |
2334 | |
509 | 2335 #if (NGX_DEBUG) |
2336 if (rc == NGX_OK) { | |
2337 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2338 "rewritten refresh: \"%V\"", &ho->value); | |
2339 } | |
2340 #endif | |
2341 | |
2342 return rc; | |
2343 } | |
2344 | |
2345 return NGX_OK; | |
2346 } | |
2347 | |
2348 | |
2349 #if (NGX_HTTP_GZIP) | |
2350 | |
2351 static ngx_int_t | |
2352 ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r, | |
2353 ngx_table_elt_t *h, ngx_uint_t offset) | |
2354 { | |
2355 ngx_table_elt_t *ho; | |
2356 | |
2357 ho = ngx_list_push(&r->headers_out.headers); | |
2358 if (ho == NULL) { | |
2359 return NGX_ERROR; | |
2360 } | |
2361 | |
2362 *ho = *h; | |
2363 | |
2364 r->headers_out.content_encoding = ho; | |
2365 | |
2366 return NGX_OK; | |
2367 } | |
2368 | |
2369 #endif | |
2370 | |
2371 | |
487 | 2372 static size_t |
2373 ngx_http_upstream_log_status_getlen(ngx_http_request_t *r, uintptr_t data) | |
479 | 2374 { |
2375 if (r->upstream) { | |
2376 return r->upstream->states.nelts * (3 + 2); | |
2377 } | |
2378 | |
2379 return 1; | |
2380 } | |
2381 | |
2382 | |
487 | 2383 static u_char * |
2384 ngx_http_upstream_log_status(ngx_http_request_t *r, u_char *buf, | |
2385 ngx_http_log_op_t *op) | |
479 | 2386 { |
2387 ngx_uint_t i; | |
2388 ngx_http_upstream_t *u; | |
2389 ngx_http_upstream_state_t *state; | |
2390 | |
2391 u = r->upstream; | |
2392 | |
573 | 2393 if (u == NULL || u->states.nelts == 0) { |
479 | 2394 *buf = '-'; |
2395 return buf + 1; | |
2396 } | |
2397 | |
2398 i = 0; | |
2399 state = u->states.elts; | |
2400 | |
2401 for ( ;; ) { | |
2402 if (state[i].status == 0) { | |
2403 *buf++ = '-'; | |
2404 | |
2405 } else { | |
2406 buf = ngx_sprintf(buf, "%ui", state[i].status); | |
2407 } | |
2408 | |
2409 if (++i == u->states.nelts) { | |
2410 return buf; | |
2411 } | |
2412 | |
2413 *buf++ = ','; | |
2414 *buf++ = ' '; | |
2415 } | |
2416 } | |
2417 | |
2418 | |
573 | 2419 static size_t |
2420 ngx_http_upstream_log_response_time_getlen(ngx_http_request_t *r, | |
2421 uintptr_t data) | |
2422 { | |
2423 if (r->upstream) { | |
2424 return r->upstream->states.nelts * (NGX_TIME_T_LEN + 4 + 2); | |
2425 } | |
2426 | |
2427 return 1; | |
2428 } | |
2429 | |
2430 | |
509 | 2431 static u_char * |
563 | 2432 ngx_http_upstream_log_response_time(ngx_http_request_t *r, u_char *buf, |
2433 ngx_http_log_op_t *op) | |
2434 { | |
2435 ngx_uint_t i; | |
2436 ngx_http_upstream_t *u; | |
2437 ngx_http_upstream_state_t *state; | |
2438 | |
2439 u = r->upstream; | |
2440 | |
573 | 2441 if (u == NULL || u->states.nelts == 0) { |
563 | 2442 *buf = '-'; |
2443 return buf + 1; | |
2444 } | |
2445 | |
2446 i = 0; | |
2447 state = u->states.elts; | |
2448 | |
2449 for ( ;; ) { | |
2450 if (state[i].status == 0) { | |
2451 *buf++ = '-'; | |
2452 | |
2453 } else { | |
2454 buf = ngx_sprintf(buf, "%d.%03d", | |
2455 state[i].response_time / 1000, | |
2456 state[i].response_time % 1000); | |
2457 } | |
2458 | |
2459 if (++i == u->states.nelts) { | |
2460 return buf; | |
2461 } | |
2462 | |
2463 *buf++ = ','; | |
2464 *buf++ = ' '; | |
2465 } | |
2466 } | |
2467 | |
2468 | |
487 | 2469 static ngx_int_t |
573 | 2470 ngx_http_upstream_add_variables(ngx_conf_t *cf) |
479 | 2471 { |
573 | 2472 ngx_http_variable_t *var, *v; |
479 | 2473 ngx_http_log_op_name_t *op; |
2474 | |
573 | 2475 for (v = ngx_http_upstream_vars; v->name.len; v++) { |
2476 var = ngx_http_add_variable(cf, &v->name, v->flags); | |
2477 if (var == NULL) { | |
2478 return NGX_ERROR; | |
2479 } | |
2480 | |
637 | 2481 var->get_handler = v->get_handler; |
573 | 2482 var->data = v->data; |
2483 } | |
2484 | |
479 | 2485 for (op = ngx_http_upstream_log_fmt_ops; op->name.len; op++) { /* void */ } |
2486 op->run = NULL; | |
2487 | |
2488 for (op = ngx_http_log_fmt_ops; op->run; op++) { | |
2489 if (op->name.len == 0) { | |
2490 op = (ngx_http_log_op_name_t *) op->run; | |
2491 } | |
2492 } | |
2493 | |
2494 op->run = (ngx_http_log_op_run_pt) ngx_http_upstream_log_fmt_ops; | |
2495 | |
2496 return NGX_OK; | |
2497 } | |
509 | 2498 |
2499 | |
573 | 2500 static ngx_int_t |
2501 ngx_http_upstream_status_variable(ngx_http_request_t *r, | |
2502 ngx_http_variable_value_t *v, uintptr_t data) | |
2503 { | |
2504 u_char *p; | |
2505 size_t len; | |
2506 ngx_uint_t i; | |
2507 ngx_http_upstream_t *u; | |
2508 ngx_http_upstream_state_t *state; | |
2509 | |
2510 v->valid = 1; | |
2511 v->no_cachable = 0; | |
2512 v->not_found = 0; | |
2513 | |
2514 u = r->upstream; | |
2515 | |
2516 if (u == NULL || u->states.nelts == 0) { | |
2517 v->not_found = 1; | |
2518 return NGX_OK; | |
2519 } | |
2520 | |
2521 len = u->states.nelts * (3 + 2); | |
2522 | |
2523 p = ngx_palloc(r->pool, len); | |
2524 if (p == NULL) { | |
2525 return NGX_ERROR; | |
2526 } | |
2527 | |
2528 v->data = p; | |
2529 | |
2530 i = 0; | |
2531 state = u->states.elts; | |
2532 | |
2533 for ( ;; ) { | |
2534 if (state[i].status == 0) { | |
2535 *p++ = '-'; | |
2536 | |
2537 } else { | |
2538 p = ngx_sprintf(p, "%ui", state[i].status); | |
2539 } | |
2540 | |
2541 if (++i == u->states.nelts) { | |
2542 break; | |
2543 } | |
2544 | |
2545 *p++ = ','; | |
2546 *p++ = ' '; | |
2547 } | |
2548 | |
2549 v->len = p - v->data; | |
2550 | |
2551 return NGX_OK; | |
2552 } | |
2553 | |
2554 | |
2555 static ngx_int_t | |
2556 ngx_http_upstream_response_time_variable(ngx_http_request_t *r, | |
2557 ngx_http_variable_value_t *v, uintptr_t data) | |
2558 { | |
2559 u_char *p; | |
2560 size_t len; | |
2561 ngx_uint_t i; | |
2562 ngx_http_upstream_t *u; | |
2563 ngx_http_upstream_state_t *state; | |
2564 | |
2565 v->valid = 1; | |
2566 v->no_cachable = 0; | |
2567 v->not_found = 0; | |
2568 | |
2569 u = r->upstream; | |
2570 | |
2571 if (u == NULL || u->states.nelts == 0) { | |
2572 v->not_found = 1; | |
2573 return NGX_OK; | |
2574 } | |
2575 | |
2576 len = u->states.nelts * (NGX_TIME_T_LEN + 4 + 2); | |
2577 | |
2578 p = ngx_palloc(r->pool, len); | |
2579 if (p == NULL) { | |
2580 return NGX_ERROR; | |
2581 } | |
2582 | |
2583 v->data = p; | |
2584 | |
2585 i = 0; | |
2586 state = u->states.elts; | |
2587 | |
2588 for ( ;; ) { | |
2589 if (state[i].status == 0) { | |
2590 *p++ = '-'; | |
2591 | |
2592 } else { | |
2593 p = ngx_sprintf(p, "%d.%03d", | |
2594 state[i].response_time / 1000, | |
2595 state[i].response_time % 1000); | |
2596 } | |
2597 | |
2598 if (++i == u->states.nelts) { | |
2599 break; | |
2600 } | |
2601 | |
2602 *p++ = ','; | |
2603 *p++ = ' '; | |
2604 } | |
2605 | |
2606 v->len = p - v->data; | |
2607 | |
2608 return NGX_OK; | |
2609 } | |
2610 | |
2611 | |
651 | 2612 static char * |
2613 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) | |
2614 { | |
2615 char *rv; | |
2616 void *mconf; | |
2617 ngx_str_t *value; | |
2618 ngx_url_t u; | |
2619 ngx_uint_t i, j, m, n; | |
2620 ngx_conf_t pcf; | |
2621 ngx_peers_t **peers; | |
2622 ngx_http_module_t *module; | |
2623 ngx_http_conf_ctx_t *ctx; | |
2624 ngx_http_upstream_srv_conf_t *uscf; | |
2625 | |
2626 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); | |
2627 if (ctx == NULL) { | |
2628 return NGX_CONF_ERROR; | |
2629 } | |
2630 | |
2631 ngx_memzero(&u, sizeof(ngx_url_t)); | |
2632 | |
2633 value = cf->args->elts; | |
2634 u.host = value[1]; | |
2635 | |
2636 uscf = ngx_http_upstream_add(cf, &u); | |
2637 if (uscf == NULL) { | |
2638 return NGX_CONF_ERROR; | |
2639 } | |
2640 | |
2641 /* the upstream{}'s srv_conf */ | |
2642 | |
2643 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
2644 if (ctx->srv_conf == NULL) { | |
2645 return NGX_CONF_ERROR; | |
2646 } | |
2647 | |
2648 ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf; | |
2649 | |
2650 | |
2651 /* the upstream{}'s loc_conf */ | |
2652 | |
2653 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
2654 if (ctx->loc_conf == NULL) { | |
2655 return NGX_CONF_ERROR; | |
2656 } | |
2657 | |
2658 for (m = 0; ngx_modules[m]; m++) { | |
2659 if (ngx_modules[m]->type != NGX_HTTP_MODULE) { | |
2660 continue; | |
2661 } | |
2662 | |
2663 module = ngx_modules[m]->ctx; | |
2664 | |
2665 if (module->create_loc_conf) { | |
2666 mconf = module->create_loc_conf(cf); | |
2667 if (mconf == NULL) { | |
2668 return NGX_CONF_ERROR; | |
2669 } | |
2670 | |
2671 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf; | |
2672 } | |
2673 } | |
2674 | |
2675 | |
2676 /* parse inside upstream{} */ | |
2677 | |
2678 pcf = *cf; | |
2679 cf->ctx = ctx; | |
2680 cf->cmd_type = NGX_HTTP_UPS_CONF; | |
2681 | |
2682 rv = ngx_conf_parse(cf, NULL); | |
2683 | |
2684 *cf = pcf; | |
2685 | |
2686 if (rv != NGX_CONF_OK) { | |
2687 return rv; | |
2688 } | |
2689 | |
2690 if (uscf->servers == NULL) { | |
2691 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2692 "no servers are inside upstream"); | |
2693 return NGX_CONF_ERROR; | |
2694 } | |
2695 | |
2696 peers = uscf->servers->elts; | |
2697 | |
2698 if (uscf->servers->nelts == 1) { | |
2699 uscf->peers = peers[0]; | |
2700 } | |
2701 | |
2702 n = 0; | |
2703 | |
2704 for (i = 0; i < uscf->servers->nelts; i++) { | |
2705 n += peers[i]->number; | |
2706 } | |
2707 | |
2708 uscf->peers = ngx_pcalloc(cf->pool, | |
2709 sizeof(ngx_peers_t) + sizeof(ngx_peer_t) * (n - 1)); | |
2710 if (uscf->peers == NULL) { | |
2711 return NGX_CONF_ERROR; | |
2712 } | |
2713 | |
2714 uscf->peers->number = n; | |
2715 | |
2716 n = 0; | |
2717 | |
2718 for (i = 0; i < uscf->servers->nelts; i++) { | |
2719 for (j = 0; j < peers[i]->number; j++) { | |
2720 uscf->peers->peer[n++] = peers[i]->peer[j]; | |
2721 } | |
2722 } | |
2723 | |
2724 return rv; | |
2725 } | |
2726 | |
2727 | |
2728 static char * | |
2729 ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
2730 { | |
2731 ngx_http_upstream_srv_conf_t *uscf = conf; | |
2732 | |
2733 ngx_str_t *value; | |
2734 ngx_url_t u; | |
663 | 2735 ngx_int_t weight; |
2736 ngx_uint_t i; | |
651 | 2737 ngx_peers_t **peers; |
2738 | |
2739 if (uscf->servers == NULL) { | |
2740 uscf->servers = ngx_array_create(cf->pool, 4, sizeof(ngx_peers_t *)); | |
2741 if (uscf->servers == NULL) { | |
2742 return NGX_CONF_ERROR; | |
2743 } | |
2744 } | |
2745 | |
2746 peers = ngx_array_push(uscf->servers); | |
2747 if (peers == NULL) { | |
2748 return NGX_CONF_ERROR; | |
2749 } | |
2750 | |
2751 value = cf->args->elts; | |
2752 | |
2753 ngx_memzero(&u, sizeof(ngx_url_t)); | |
2754 | |
2755 u.url = value[1]; | |
2756 u.default_portn = 80; | |
2757 | |
2758 if (ngx_parse_url(cf, &u) != NGX_OK) { | |
2759 if (u.err) { | |
2760 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2761 "%s in upstream \"%V\"", u.err, &u.url); | |
2762 } | |
2763 | |
2764 return NGX_CONF_ERROR; | |
2765 } | |
2766 | |
663 | 2767 weight = 1; |
2768 | |
2769 if (cf->args->nelts == 3) { | |
2770 | |
2771 value = &value[2]; | |
2772 | |
2773 if (ngx_strncmp(value->data, "weight=", 7) == 0) { | |
2774 | |
2775 weight = ngx_atoi(&value->data[7], value->len - 7); | |
2776 | |
2777 if (weight == NGX_ERROR || weight == 0) { | |
2778 goto invalid; | |
2779 } | |
2780 | |
2781 } else { | |
2782 goto invalid; | |
2783 } | |
2784 } | |
2785 | |
2786 for (i = 0; i < u.peers->number; i++) { | |
2787 u.peers->peer[i].weight = weight; | |
2788 u.peers->peer[i].current_weight = weight; | |
2789 u.peers->peer[i].max_fails = NGX_CONF_UNSET_UINT; | |
2790 u.peers->peer[i].fail_timeout = NGX_CONF_UNSET; | |
2791 } | |
2792 | |
651 | 2793 *peers = u.peers; |
2794 | |
2795 return NGX_CONF_OK; | |
663 | 2796 |
2797 invalid: | |
2798 | |
2799 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", value); | |
2800 | |
2801 return NGX_CONF_ERROR; | |
651 | 2802 } |
2803 | |
2804 | |
2805 ngx_http_upstream_srv_conf_t * | |
2806 ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u) | |
2807 { | |
2808 ngx_uint_t i; | |
2809 ngx_http_upstream_srv_conf_t *uscf, **uscfp; | |
2810 ngx_http_upstream_main_conf_t *umcf; | |
2811 | |
2812 if (u->upstream) { | |
2813 if (ngx_parse_url(cf, u) != NGX_OK) { | |
2814 if (u->err) { | |
2815 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2816 "%s in upstream \"%V\"", u->err, &u->url); | |
2817 } | |
2818 | |
2819 return NULL; | |
2820 } | |
2821 | |
2822 if (u->peers) { | |
2823 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t)); | |
2824 if (uscf == NULL) { | |
2825 return NULL; | |
2826 } | |
2827 | |
2828 uscf->peers = u->peers; | |
2829 | |
2830 return uscf; | |
2831 } | |
2832 } | |
2833 | |
2834 umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module); | |
2835 | |
2836 uscfp = umcf->upstreams.elts; | |
2837 | |
2838 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
2839 if (uscfp[i]->host.len != u->host.len) { | |
2840 continue; | |
2841 } | |
2842 | |
2843 if (ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len) | |
2844 == 0) | |
2845 { | |
2846 return uscfp[i]; | |
2847 } | |
2848 } | |
2849 | |
2850 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t)); | |
2851 if (uscf == NULL) { | |
2852 return NULL; | |
2853 } | |
2854 | |
2855 uscf->host = u->host; | |
2856 uscf->file_name = cf->conf_file->file.name; | |
2857 uscf->line = cf->conf_file->line; | |
2858 uscf->port = u->default_portn; | |
2859 | |
2860 uscfp = ngx_array_push(&umcf->upstreams); | |
2861 if (uscfp == NULL) { | |
2862 return NULL; | |
2863 } | |
2864 | |
2865 *uscfp = uscf; | |
2866 | |
2867 return uscf; | |
2868 } | |
2869 | |
2870 | |
509 | 2871 static void * |
2872 ngx_http_upstream_create_main_conf(ngx_conf_t *cf) | |
2873 { | |
2874 ngx_http_upstream_main_conf_t *umcf; | |
2875 | |
2876 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t)); | |
2877 if (umcf == NULL) { | |
2878 return NULL; | |
2879 } | |
2880 | |
651 | 2881 if (ngx_array_init(&umcf->upstreams, cf->pool, 4, |
2882 sizeof(ngx_http_upstream_srv_conf_t *)) | |
2883 != NGX_OK) | |
2884 { | |
2885 return NGX_CONF_ERROR; | |
2886 } | |
2887 | |
509 | 2888 return umcf; |
2889 } | |
2890 | |
2891 | |
2892 static char * | |
651 | 2893 ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf) |
509 | 2894 { |
2895 ngx_http_upstream_main_conf_t *umcf = conf; | |
2896 | |
651 | 2897 ngx_uint_t i; |
2898 ngx_array_t headers_in; | |
2899 ngx_hash_key_t *hk; | |
2900 ngx_hash_init_t hash; | |
2901 ngx_http_upstream_header_t *header; | |
2902 ngx_http_upstream_srv_conf_t **uscfp; | |
2903 | |
2904 uscfp = umcf->upstreams.elts; | |
2905 | |
2906 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
2907 if (uscfp[i]->peers) { | |
2908 continue; | |
2909 } | |
2910 | |
2911 uscfp[i]->peers = ngx_inet_resolve_peer(cf, &uscfp[i]->host, | |
663 | 2912 uscfp[i]->port); |
651 | 2913 if (uscfp[i]->peers == NULL) { |
2914 return NGX_CONF_ERROR; | |
2915 } | |
2916 | |
2917 if (uscfp[i]->peers == NGX_CONF_ERROR) { | |
2918 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
2919 "upstream host \"%V\" is not found in %s:%ui", | |
2920 &uscfp[i]->host, uscfp[i]->file_name.data, | |
2921 uscfp[i]->line); | |
2922 return NGX_CONF_ERROR; | |
2923 } | |
2924 } | |
649 | 2925 |
663 | 2926 |
649 | 2927 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t)) |
2928 != NGX_OK) | |
509 | 2929 { |
2930 return NGX_CONF_ERROR; | |
2931 } | |
2932 | |
649 | 2933 for (header = ngx_http_upstream_headers_in; header->name.len; header++) { |
2934 hk = ngx_array_push(&headers_in); | |
2935 if (hk == NULL) { | |
2936 return NGX_CONF_ERROR; | |
2937 } | |
2938 | |
2939 hk->key = header->name; | |
2940 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len); | |
2941 hk->value = header; | |
2942 } | |
2943 | |
2944 hash.hash = &umcf->headers_in_hash; | |
2945 hash.key = ngx_hash_key_lc; | |
2946 hash.max_size = 512; | |
751
bae59a740c40
align hash bucket size to cache line
Igor Sysoev <igor@sysoev.ru>
parents:
750
diff
changeset
|
2947 hash.bucket_size = ngx_align(64, ngx_cacheline_size); |
649 | 2948 hash.name = "upstream_headers_in_hash"; |
2949 hash.pool = cf->pool; | |
2950 hash.temp_pool = NULL; | |
2951 | |
2952 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) { | |
2953 return NGX_CONF_ERROR; | |
2954 } | |
509 | 2955 |
2956 return NGX_CONF_OK; | |
2957 } |