Mercurial > hg > nginx-quic
annotate src/http/ngx_http_upstream.c @ 752:907361d07f25
ngx_http_charset_module supports the variables
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 09 Oct 2006 14:16:36 +0000 |
parents | bae59a740c40 |
children | 63b9dc652c3d |
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 } |
1523 } | |
1524 | |
1525 r = c->data; | |
1526 u = r->upstream; | |
583 | 1527 client = r->connection; |
581 | 1528 |
1529 b = &u->buffer; | |
1530 | |
1531 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1532 | |
657 | 1533 do_write = ev->write || u->length == 0; |
581 | 1534 |
1535 for ( ;; ) { | |
1536 | |
1537 if (do_write) { | |
1538 | |
1539 if (u->out_bufs || u->busy_bufs) { | |
583 | 1540 rc = ngx_http_output_filter(r, u->out_bufs); |
1541 | |
1542 if (client->destroyed) { | |
1543 return; | |
1544 } | |
1545 | |
1546 if (rc == NGX_ERROR) { | |
581 | 1547 ngx_http_upstream_finalize_request(r, u, 0); |
1548 return; | |
1549 } | |
1550 | |
1551 ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs, | |
1552 &u->out_bufs, u->output.tag); | |
1553 } | |
1554 | |
1555 if (u->busy_bufs == NULL) { | |
1556 | |
1557 if (u->length == 0 | |
1558 || u->peer.connection->read->eof | |
1559 || u->peer.connection->read->error) | |
1560 { | |
1561 ngx_http_upstream_finalize_request(r, u, 0); | |
1562 return; | |
1563 } | |
1564 | |
1565 b->pos = b->start; | |
1566 b->last = b->start; | |
1567 } | |
1568 } | |
1569 | |
1570 size = b->end - b->last; | |
1571 | |
1572 if (size > u->length) { | |
1573 size = u->length; | |
1574 } | |
1575 | |
1576 if (size && u->peer.connection->read->ready) { | |
657 | 1577 |
581 | 1578 n = u->peer.connection->recv(u->peer.connection, b->last, size); |
1579 | |
1580 if (n == NGX_AGAIN) { | |
1581 break; | |
1582 } | |
1583 | |
1584 if (n > 0) { | |
1585 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { | |
1586 ngx_http_upstream_finalize_request(r, u, 0); | |
1587 return; | |
1588 } | |
1589 } | |
1590 | |
1591 do_write = 1; | |
1592 | |
1593 continue; | |
1594 } | |
1595 | |
1596 break; | |
1597 } | |
1598 | |
583 | 1599 if (client->data == r) { |
1600 if (ngx_handle_write_event(client->write, clcf->send_lowat) | |
1601 == NGX_ERROR) | |
1602 { | |
1603 ngx_http_upstream_finalize_request(r, u, 0); | |
1604 return; | |
1605 } | |
581 | 1606 } |
1607 | |
583 | 1608 if (client->write->active) { |
1609 ngx_add_timer(client->write, clcf->send_timeout); | |
1610 | |
1611 } else if (client->write->timer_set) { | |
1612 ngx_del_timer(client->write); | |
581 | 1613 } |
1614 | |
1615 if (ngx_handle_read_event(u->peer.connection->read, 0) == NGX_ERROR) { | |
1616 ngx_http_upstream_finalize_request(r, u, 0); | |
1617 return; | |
1618 } | |
1619 | |
1620 if (u->peer.connection->read->active) { | |
1621 ngx_add_timer(u->peer.connection->read, u->conf->read_timeout); | |
1622 | |
583 | 1623 } else if (u->peer.connection->read->timer_set) { |
1624 ngx_del_timer(u->peer.connection->read); | |
581 | 1625 } |
1626 } | |
1627 | |
1628 | |
1629 static ngx_int_t | |
1630 ngx_http_upstream_non_buffered_filter_init(void *data) | |
1631 { | |
1632 return NGX_OK; | |
1633 } | |
1634 | |
1635 | |
1636 static ngx_int_t | |
1637 ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes) | |
1638 { | |
1639 ngx_http_request_t *r = data; | |
1640 | |
1641 ngx_buf_t *b; | |
1642 ngx_chain_t *cl, **ll; | |
1643 ngx_http_upstream_t *u; | |
1644 | |
1645 u = r->upstream; | |
1646 | |
1647 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { | |
1648 ll = &cl->next; | |
1649 } | |
1650 | |
1651 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); | |
1652 if (cl == NULL) { | |
1653 return NGX_ERROR; | |
1654 } | |
1655 | |
1656 *ll = cl; | |
1657 | |
1658 cl->buf->flush = 1; | |
1659 cl->buf->memory = 1; | |
1660 | |
1661 b = &u->buffer; | |
1662 | |
1663 cl->buf->pos = b->last; | |
1664 b->last += bytes; | |
1665 cl->buf->last = b->last; | |
1666 | |
1667 if (u->length == NGX_MAX_SIZE_T_VALUE) { | |
1668 return NGX_OK; | |
1669 } | |
1670 | |
1671 u->length -= bytes; | |
1672 | |
1673 return NGX_OK; | |
1674 } | |
1675 | |
1676 | |
1677 static void | |
509 | 1678 ngx_http_upstream_process_downstream(ngx_http_request_t *r) |
1679 { | |
1680 ngx_http_upstream_process_body(r->connection->write); | |
1681 } | |
1682 | |
1683 | |
1684 static void | |
487 | 1685 ngx_http_upstream_process_body(ngx_event_t *ev) |
479 | 1686 { |
509 | 1687 ngx_event_pipe_t *p; |
583 | 1688 ngx_connection_t *c, *downstream; |
479 | 1689 ngx_http_request_t *r; |
1690 ngx_http_upstream_t *u; | |
1691 | |
1692 c = ev->data; | |
1693 r = c->data; | |
1694 u = r->upstream; | |
583 | 1695 downstream = r->connection; |
479 | 1696 |
1697 if (ev->write) { | |
483 | 1698 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1699 "http upstream process downstream"); | |
1700 c->log->action = "sending to client"; | |
577 | 1701 |
479 | 1702 } else { |
483 | 1703 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1704 "http upstream process upstream"); | |
1705 c->log->action = "reading upstream"; | |
479 | 1706 } |
577 | 1707 |
581 | 1708 p = u->pipe; |
479 | 1709 |
1710 if (ev->timedout) { | |
1711 if (ev->write) { | |
527 | 1712 if (ev->delayed) { |
1713 | |
1714 ev->timedout = 0; | |
1715 ev->delayed = 0; | |
1716 | |
1717 if (!ev->ready) { | |
1718 ngx_add_timer(ev, p->send_timeout); | |
1719 | |
1720 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) | |
1721 { | |
1722 ngx_http_upstream_finalize_request(r, u, 0); | |
1723 return; | |
1724 } | |
1725 | |
1726 return; | |
1727 } | |
1728 | |
1729 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) { | |
583 | 1730 |
1731 if (downstream->destroyed) { | |
1732 return; | |
1733 } | |
1734 | |
527 | 1735 ngx_http_upstream_finalize_request(r, u, 0); |
1736 return; | |
1737 } | |
1738 | |
1739 } else { | |
1740 p->downstream_error = 1; | |
577 | 1741 c->timedout = 1; |
611 | 1742 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out"); |
527 | 1743 } |
479 | 1744 |
1745 } else { | |
577 | 1746 p->upstream_error = 1; |
611 | 1747 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
479 | 1748 } |
1749 | |
1750 } else { | |
527 | 1751 if (ev->write && ev->delayed) { |
1752 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1753 "http downstream delayed"); | |
1754 | |
1755 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) { | |
1756 return; | |
1757 } | |
1758 | |
1759 return; | |
1760 } | |
1761 | |
479 | 1762 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) { |
583 | 1763 |
1764 if (downstream->destroyed) { | |
1765 return; | |
1766 } | |
1767 | |
479 | 1768 ngx_http_upstream_finalize_request(r, u, 0); |
1769 return; | |
1770 } | |
1771 } | |
577 | 1772 |
479 | 1773 if (u->peer.connection) { |
1774 | |
1775 #if (NGX_HTTP_FILE_CACHE) | |
1776 | |
1777 if (p->upstream_done && u->cachable) { | |
1778 if (ngx_http_cache_update(r) == NGX_ERROR) { | |
1779 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1780 ngx_http_upstream_finalize_request(r, u, 0); | |
1781 return; | |
1782 } | |
1783 | |
1784 } else if (p->upstream_eof && u->cachable) { | |
1785 | |
1786 /* TODO: check length & update cache */ | |
1787 | |
1788 if (ngx_http_cache_update(r) == NGX_ERROR) { | |
1789 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1790 ngx_http_upstream_finalize_request(r, u, 0); | |
1791 return; | |
1792 } | |
1793 } | |
1794 | |
1795 #endif | |
1796 | |
1797 if (p->upstream_done || p->upstream_eof || p->upstream_error) { | |
483 | 1798 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1799 "http upstream exit: %p", p->out); | |
479 | 1800 #if 0 |
1801 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1802 #endif | |
1803 ngx_http_upstream_finalize_request(r, u, 0); | |
1804 return; | |
1805 } | |
1806 } | |
1807 | |
1808 if (p->downstream_error) { | |
483 | 1809 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1810 "http upstream downstream error"); | |
479 | 1811 |
1812 if (!u->cachable && u->peer.connection) { | |
1813 ngx_http_upstream_finalize_request(r, u, 0); | |
1814 } | |
1815 } | |
1816 } | |
1817 | |
1818 | |
487 | 1819 static void |
1820 ngx_http_upstream_dummy_handler(ngx_event_t *wev) | |
479 | 1821 { |
1822 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, | |
1823 "http upstream dummy handler"); | |
1824 } | |
1825 | |
1826 | |
487 | 1827 static void |
1828 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u, | |
1829 ngx_uint_t ft_type) | |
479 | 1830 { |
525 | 1831 ngx_uint_t status, down; |
479 | 1832 |
1833 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1834 "http next upstream, %xD", ft_type); | |
1835 | |
1836 #if 0 | |
1837 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1838 #endif | |
1839 | |
525 | 1840 if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) { |
1841 down = 0; | |
1842 } else { | |
1843 down = 1; | |
479 | 1844 } |
525 | 1845 |
1846 ngx_event_connect_peer_failed(&u->peer, down); | |
1847 | |
479 | 1848 if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) { |
1849 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT, | |
1850 "upstream timed out"); | |
1851 } | |
577 | 1852 |
479 | 1853 if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) { |
1854 status = 0; | |
1855 | |
1856 } else { | |
1857 switch(ft_type) { | |
1858 | |
1859 case NGX_HTTP_UPSTREAM_FT_TIMEOUT: | |
1860 status = NGX_HTTP_GATEWAY_TIME_OUT; | |
1861 break; | |
1862 | |
1863 case NGX_HTTP_UPSTREAM_FT_HTTP_500: | |
1864 status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1865 break; | |
1866 | |
1867 case NGX_HTTP_UPSTREAM_FT_HTTP_404: | |
1868 status = NGX_HTTP_NOT_FOUND; | |
1869 break; | |
1870 | |
1871 /* | |
1872 * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING | |
1873 * never reach here | |
1874 */ | |
1875 | |
1876 default: | |
1877 status = NGX_HTTP_BAD_GATEWAY; | |
1878 } | |
1879 } | |
1880 | |
583 | 1881 if (r->connection->error) { |
479 | 1882 ngx_http_upstream_finalize_request(r, u, |
1883 NGX_HTTP_CLIENT_CLOSED_REQUEST); | |
1884 return; | |
1885 } | |
1886 | |
1887 if (status) { | |
1888 u->state->status = status; | |
1889 | |
525 | 1890 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) { |
479 | 1891 |
1892 #if (NGX_HTTP_CACHE) | |
1893 | |
1894 if (u->stale && (u->conf->use_stale & ft_type)) { | |
1895 ngx_http_upstream_finalize_request(r, u, | |
525 | 1896 ngx_http_send_cached_response(r)); |
479 | 1897 return; |
1898 } | |
1899 | |
1900 #endif | |
1901 | |
1902 ngx_http_upstream_finalize_request(r, u, status); | |
1903 return; | |
1904 } | |
1905 } | |
1906 | |
1907 if (u->peer.connection) { | |
1908 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1909 "close http upstream connection: %d", | |
1910 u->peer.connection->fd); | |
577 | 1911 #if (NGX_HTTP_SSL) |
1912 if (u->peer.connection->ssl) { | |
1913 ngx_http_upstream_ssl_shutdown(u->peer.connection, | |
1914 &u->peer.peers->peer[u->peer.cur_peer]); | |
1915 } | |
1916 #endif | |
479 | 1917 ngx_close_connection(u->peer.connection); |
1918 } | |
1919 | |
1920 #if 0 | |
1921 if (u->conf->busy_lock && !u->busy_locked) { | |
1922 ngx_http_upstream_busy_lock(p); | |
1923 return; | |
1924 } | |
1925 #endif | |
1926 | |
1927 ngx_http_upstream_connect(r, u); | |
1928 } | |
1929 | |
1930 | |
487 | 1931 static void |
569 | 1932 ngx_http_upstream_cleanup(void *data) |
1933 { | |
1934 ngx_http_request_t *r = data; | |
1935 | |
1936 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1937 "cleanup http upstream request: \"%V\"", &r->uri); | |
1938 | |
1939 ngx_http_upstream_finalize_request(r, r->upstream, NGX_DONE); | |
1940 } | |
1941 | |
1942 | |
1943 static void | |
487 | 1944 ngx_http_upstream_finalize_request(ngx_http_request_t *r, |
1945 ngx_http_upstream_t *u, ngx_int_t rc) | |
479 | 1946 { |
563 | 1947 ngx_time_t *tp; |
1948 | |
483 | 1949 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1950 "finalize http upstream request: %i", rc); | |
479 | 1951 |
569 | 1952 *u->cleanup = NULL; |
1953 | |
563 | 1954 if (u->state->response_time) { |
1955 tp = ngx_timeofday(); | |
1956 u->state->response_time = tp->sec * 1000 + tp->msec | |
1957 - u->state->response_time; | |
1958 } | |
1959 | |
479 | 1960 u->finalize_request(r, rc); |
1961 | |
1962 if (u->peer.connection) { | |
1963 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1964 "close http upstream connection: %d", | |
1965 u->peer.connection->fd); | |
577 | 1966 #if (NGX_HTTP_SSL) |
1967 | |
1968 /* TODO: do not shutdown persistent connection */ | |
1969 | |
1970 if (u->peer.connection->ssl) { | |
1971 ngx_http_upstream_ssl_shutdown(u->peer.connection, | |
1972 &u->peer.peers->peer[u->peer.cur_peer]); | |
1973 } | |
1974 #endif | |
479 | 1975 ngx_close_connection(u->peer.connection); |
1976 } | |
1977 | |
483 | 1978 u->peer.connection = NULL; |
1979 | |
563 | 1980 if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE)) |
479 | 1981 { |
1982 rc = 0; | |
1983 } | |
1984 | |
581 | 1985 if (u->pipe && u->pipe->temp_file) { |
479 | 1986 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1987 "http upstream temp fd: %d", | |
581 | 1988 u->pipe->temp_file->file.fd); |
479 | 1989 } |
1990 | |
1991 #if 0 | |
1992 if (u->cache) { | |
1993 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
483 | 1994 "http upstream cache fd: %d", |
479 | 1995 u->cache->ctx.file.fd); |
1996 } | |
1997 #endif | |
1998 | |
515 | 1999 if (rc == NGX_DECLINED) { |
2000 return; | |
2001 } | |
2002 | |
483 | 2003 r->connection->log->action = "sending to client"; |
2004 | |
629 | 2005 if (rc == 0 && r == r->main && !r->post_action) { |
581 | 2006 rc = ngx_http_send_special(r, NGX_HTTP_LAST); |
479 | 2007 } |
2008 | |
2009 ngx_http_finalize_request(r, rc); | |
2010 } | |
2011 | |
2012 | |
577 | 2013 #if (NGX_HTTP_SSL) |
2014 | |
2015 static void | |
2016 ngx_http_upstream_ssl_shutdown(ngx_connection_t *c, ngx_peer_t *peer) | |
2017 { | |
2018 /* lock peer mutex */ | |
2019 | |
2020 if (peer->ssl_session) { | |
2021 ngx_ssl_free_session(peer->ssl_session); | |
2022 } | |
2023 | |
2024 peer->ssl_session = ngx_ssl_get_session(c); | |
2025 | |
2026 /* unlock peer mutex */ | |
2027 | |
2028 /* | |
2029 * We send the "close notify" shutdown alert to the upstream only | |
2030 * and do not wait its "close notify" shutdown alert. | |
2031 * It is acceptable according to the TLS standard. | |
2032 */ | |
2033 | |
2034 c->ssl->no_wait_shutdown = 1; | |
2035 | |
2036 (void) ngx_ssl_shutdown(c); | |
2037 } | |
2038 | |
2039 #endif | |
2040 | |
2041 | |
509 | 2042 static ngx_int_t |
2043 ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2044 ngx_uint_t offset) | |
577 | 2045 { |
509 | 2046 ngx_table_elt_t **ph; |
577 | 2047 |
509 | 2048 ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset); |
2049 | |
2050 if (*ph == NULL) { | |
2051 *ph = h; | |
2052 } | |
2053 | |
2054 return NGX_OK; | |
2055 } | |
2056 | |
2057 | |
2058 static ngx_int_t | |
2059 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r, | |
2060 ngx_table_elt_t *h, ngx_uint_t offset) | |
2061 { | |
2062 ngx_array_t *pa; | |
2063 ngx_table_elt_t **ph; | |
2064 | |
2065 pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset); | |
2066 | |
2067 if (pa->elts == NULL) { | |
2068 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) | |
2069 { | |
2070 return NGX_ERROR; | |
2071 } | |
2072 } | |
2073 | |
2074 ph = ngx_array_push(pa); | |
2075 if (ph == NULL) { | |
2076 return NGX_ERROR; | |
2077 } | |
2078 | |
2079 *ph = h; | |
2080 | |
2081 return NGX_OK; | |
2082 } | |
2083 | |
2084 | |
2085 static ngx_int_t | |
2086 ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2087 ngx_uint_t offset) | |
2088 { | |
2089 return NGX_OK; | |
2090 } | |
2091 | |
2092 | |
2093 static ngx_int_t | |
527 | 2094 ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h, |
2095 ngx_uint_t offset) | |
577 | 2096 { |
527 | 2097 ngx_int_t n; |
2098 | |
2099 r->upstream->headers_in.x_accel_limit_rate = h; | |
2100 | |
2101 n = ngx_atoi(h->value.data, h->value.len); | |
2102 | |
2103 if (n != NGX_ERROR) { | |
2104 r->limit_rate = (size_t) n; | |
2105 } | |
2106 | |
2107 return NGX_OK; | |
2108 } | |
2109 | |
2110 | |
2111 static ngx_int_t | |
649 | 2112 ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h, |
2113 ngx_uint_t offset) | |
2114 { | |
2115 u_char c0, c1, c2; | |
2116 | |
2117 if (r->upstream->conf->change_buffering) { | |
2118 | |
2119 if (h->value.len == 2) { | |
2120 c0 = ngx_tolower(h->value.data[0]); | |
2121 c1 = ngx_tolower(h->value.data[1]); | |
2122 | |
2123 if (c0 == 'n' && c1 == 'o') { | |
2124 r->upstream->buffering = 0; | |
2125 } | |
2126 | |
2127 } else if (h->value.len == 3) { | |
2128 c0 = ngx_tolower(h->value.data[0]); | |
2129 c1 = ngx_tolower(h->value.data[1]); | |
2130 c2 = ngx_tolower(h->value.data[2]); | |
2131 | |
2132 if (c0 == 'y' && c1 == 'e' && c2 == 's') { | |
2133 r->upstream->buffering = 1; | |
2134 } | |
2135 } | |
2136 } | |
2137 | |
2138 return NGX_OK; | |
2139 } | |
2140 | |
2141 | |
2142 static ngx_int_t | |
657 | 2143 ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h, |
2144 ngx_uint_t offset) | |
2145 { | |
2146 r->headers_out.override_charset = &h->value; | |
2147 | |
2148 return NGX_OK; | |
2149 } | |
2150 | |
2151 | |
2152 static ngx_int_t | |
509 | 2153 ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, |
2154 ngx_uint_t offset) | |
2155 { | |
573 | 2156 ngx_table_elt_t *ho, **ph; |
509 | 2157 |
2158 ho = ngx_list_push(&r->headers_out.headers); | |
2159 if (ho == NULL) { | |
2160 return NGX_ERROR; | |
2161 } | |
2162 | |
2163 *ho = *h; | |
2164 | |
573 | 2165 if (offset) { |
2166 ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset); | |
2167 *ph = ho; | |
2168 } | |
2169 | |
509 | 2170 return NGX_OK; |
2171 } | |
2172 | |
2173 | |
2174 static ngx_int_t | |
2175 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r, | |
2176 ngx_table_elt_t *h, ngx_uint_t offset) | |
2177 { | |
2178 ngx_array_t *pa; | |
2179 ngx_table_elt_t *ho, **ph; | |
2180 | |
2181 pa = (ngx_array_t *) ((char *) &r->headers_out + offset); | |
2182 | |
2183 if (pa->elts == NULL) { | |
2184 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) | |
2185 { | |
2186 return NGX_ERROR; | |
2187 } | |
2188 } | |
2189 | |
2190 ph = ngx_array_push(pa); | |
2191 if (ph == NULL) { | |
2192 return NGX_ERROR; | |
2193 } | |
2194 | |
2195 ho = ngx_list_push(&r->headers_out.headers); | |
2196 if (ho == NULL) { | |
2197 return NGX_ERROR; | |
2198 } | |
2199 | |
2200 *ho = *h; | |
2201 *ph = ho; | |
2202 | |
2203 return NGX_OK; | |
2204 } | |
2205 | |
2206 | |
2207 static ngx_int_t | |
2208 ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2209 ngx_uint_t offset) | |
2210 { | |
657 | 2211 u_char *p, *last; |
2212 | |
2213 r->headers_out.content_type_len = h->value.len; | |
509 | 2214 r->headers_out.content_type = h->value; |
2215 | |
657 | 2216 for (p = h->value.data; *p; p++) { |
2217 | |
2218 if (*p != ';') { | |
2219 continue; | |
2220 } | |
2221 | |
2222 last = p; | |
2223 | |
2224 while (*++p == ' ') { /* void */ } | |
2225 | |
2226 if (ngx_strncasecmp(p, "charset=", 8) != 0) { | |
2227 continue; | |
2228 } | |
2229 | |
2230 p += 8; | |
2231 | |
2232 r->headers_out.content_type_len = last - h->value.data; | |
2233 | |
2234 r->headers_out.charset.len = h->value.data + h->value.len - p; | |
2235 r->headers_out.charset.data = p; | |
2236 } | |
2237 | |
509 | 2238 return NGX_OK; |
2239 } | |
2240 | |
2241 | |
2242 static ngx_int_t | |
2243 ngx_http_upstream_copy_content_length(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2244 ngx_uint_t offset) | |
2245 { | |
2246 ngx_table_elt_t *ho; | |
2247 | |
2248 ho = ngx_list_push(&r->headers_out.headers); | |
2249 if (ho == NULL) { | |
2250 return NGX_ERROR; | |
2251 } | |
2252 | |
2253 *ho = *h; | |
2254 | |
2255 r->headers_out.content_length = ho; | |
2256 r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len); | |
2257 | |
2258 return NGX_OK; | |
2259 } | |
2260 | |
2261 | |
2262 static ngx_int_t | |
2263 ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2264 ngx_uint_t offset) | |
2265 { | |
2266 ngx_int_t rc; | |
2267 ngx_table_elt_t *ho; | |
2268 | |
2269 ho = ngx_list_push(&r->headers_out.headers); | |
2270 if (ho == NULL) { | |
2271 return NGX_ERROR; | |
2272 } | |
2273 | |
2274 *ho = *h; | |
2275 | |
2276 if (r->upstream->rewrite_redirect) { | |
2277 rc = r->upstream->rewrite_redirect(r, ho, 0); | |
2278 | |
529 | 2279 if (rc == NGX_DECLINED) { |
2280 return NGX_OK; | |
2281 } | |
2282 | |
509 | 2283 if (rc == NGX_OK) { |
2284 r->headers_out.location = ho; | |
2285 | |
2286 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2287 "rewritten location: \"%V\"", &ho->value); | |
2288 } | |
2289 | |
2290 return rc; | |
2291 } | |
2292 | |
2293 /* | |
2294 * we do not set r->headers_out.location here to avoid the handling | |
2295 * the local redirects without a host name by ngx_http_header_filter() | |
2296 */ | |
2297 | |
2298 return NGX_OK; | |
2299 } | |
2300 | |
2301 | |
2302 static ngx_int_t | |
2303 ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2304 ngx_uint_t offset) | |
2305 { | |
2306 u_char *p; | |
2307 ngx_int_t rc; | |
2308 ngx_table_elt_t *ho; | |
2309 | |
2310 ho = ngx_list_push(&r->headers_out.headers); | |
2311 if (ho == NULL) { | |
2312 return NGX_ERROR; | |
2313 } | |
2314 | |
2315 *ho = *h; | |
2316 | |
2317 if (r->upstream->rewrite_redirect) { | |
2318 | |
2319 p = (u_char *) ngx_strstr(ho->value.data, "url="); | |
2320 | |
2321 if (p) { | |
2322 rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data); | |
2323 | |
2324 } else { | |
2325 return NGX_OK; | |
2326 } | |
2327 | |
529 | 2328 if (rc == NGX_DECLINED) { |
2329 return NGX_OK; | |
2330 } | |
2331 | |
509 | 2332 #if (NGX_DEBUG) |
2333 if (rc == NGX_OK) { | |
2334 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2335 "rewritten refresh: \"%V\"", &ho->value); | |
2336 } | |
2337 #endif | |
2338 | |
2339 return rc; | |
2340 } | |
2341 | |
2342 return NGX_OK; | |
2343 } | |
2344 | |
2345 | |
2346 #if (NGX_HTTP_GZIP) | |
2347 | |
2348 static ngx_int_t | |
2349 ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r, | |
2350 ngx_table_elt_t *h, ngx_uint_t offset) | |
2351 { | |
2352 ngx_table_elt_t *ho; | |
2353 | |
2354 ho = ngx_list_push(&r->headers_out.headers); | |
2355 if (ho == NULL) { | |
2356 return NGX_ERROR; | |
2357 } | |
2358 | |
2359 *ho = *h; | |
2360 | |
2361 r->headers_out.content_encoding = ho; | |
2362 | |
2363 return NGX_OK; | |
2364 } | |
2365 | |
2366 #endif | |
2367 | |
2368 | |
487 | 2369 static size_t |
2370 ngx_http_upstream_log_status_getlen(ngx_http_request_t *r, uintptr_t data) | |
479 | 2371 { |
2372 if (r->upstream) { | |
2373 return r->upstream->states.nelts * (3 + 2); | |
2374 } | |
2375 | |
2376 return 1; | |
2377 } | |
2378 | |
2379 | |
487 | 2380 static u_char * |
2381 ngx_http_upstream_log_status(ngx_http_request_t *r, u_char *buf, | |
2382 ngx_http_log_op_t *op) | |
479 | 2383 { |
2384 ngx_uint_t i; | |
2385 ngx_http_upstream_t *u; | |
2386 ngx_http_upstream_state_t *state; | |
2387 | |
2388 u = r->upstream; | |
2389 | |
573 | 2390 if (u == NULL || u->states.nelts == 0) { |
479 | 2391 *buf = '-'; |
2392 return buf + 1; | |
2393 } | |
2394 | |
2395 i = 0; | |
2396 state = u->states.elts; | |
2397 | |
2398 for ( ;; ) { | |
2399 if (state[i].status == 0) { | |
2400 *buf++ = '-'; | |
2401 | |
2402 } else { | |
2403 buf = ngx_sprintf(buf, "%ui", state[i].status); | |
2404 } | |
2405 | |
2406 if (++i == u->states.nelts) { | |
2407 return buf; | |
2408 } | |
2409 | |
2410 *buf++ = ','; | |
2411 *buf++ = ' '; | |
2412 } | |
2413 } | |
2414 | |
2415 | |
573 | 2416 static size_t |
2417 ngx_http_upstream_log_response_time_getlen(ngx_http_request_t *r, | |
2418 uintptr_t data) | |
2419 { | |
2420 if (r->upstream) { | |
2421 return r->upstream->states.nelts * (NGX_TIME_T_LEN + 4 + 2); | |
2422 } | |
2423 | |
2424 return 1; | |
2425 } | |
2426 | |
2427 | |
509 | 2428 static u_char * |
563 | 2429 ngx_http_upstream_log_response_time(ngx_http_request_t *r, u_char *buf, |
2430 ngx_http_log_op_t *op) | |
2431 { | |
2432 ngx_uint_t i; | |
2433 ngx_http_upstream_t *u; | |
2434 ngx_http_upstream_state_t *state; | |
2435 | |
2436 u = r->upstream; | |
2437 | |
573 | 2438 if (u == NULL || u->states.nelts == 0) { |
563 | 2439 *buf = '-'; |
2440 return buf + 1; | |
2441 } | |
2442 | |
2443 i = 0; | |
2444 state = u->states.elts; | |
2445 | |
2446 for ( ;; ) { | |
2447 if (state[i].status == 0) { | |
2448 *buf++ = '-'; | |
2449 | |
2450 } else { | |
2451 buf = ngx_sprintf(buf, "%d.%03d", | |
2452 state[i].response_time / 1000, | |
2453 state[i].response_time % 1000); | |
2454 } | |
2455 | |
2456 if (++i == u->states.nelts) { | |
2457 return buf; | |
2458 } | |
2459 | |
2460 *buf++ = ','; | |
2461 *buf++ = ' '; | |
2462 } | |
2463 } | |
2464 | |
2465 | |
487 | 2466 static ngx_int_t |
573 | 2467 ngx_http_upstream_add_variables(ngx_conf_t *cf) |
479 | 2468 { |
573 | 2469 ngx_http_variable_t *var, *v; |
479 | 2470 ngx_http_log_op_name_t *op; |
2471 | |
573 | 2472 for (v = ngx_http_upstream_vars; v->name.len; v++) { |
2473 var = ngx_http_add_variable(cf, &v->name, v->flags); | |
2474 if (var == NULL) { | |
2475 return NGX_ERROR; | |
2476 } | |
2477 | |
637 | 2478 var->get_handler = v->get_handler; |
573 | 2479 var->data = v->data; |
2480 } | |
2481 | |
479 | 2482 for (op = ngx_http_upstream_log_fmt_ops; op->name.len; op++) { /* void */ } |
2483 op->run = NULL; | |
2484 | |
2485 for (op = ngx_http_log_fmt_ops; op->run; op++) { | |
2486 if (op->name.len == 0) { | |
2487 op = (ngx_http_log_op_name_t *) op->run; | |
2488 } | |
2489 } | |
2490 | |
2491 op->run = (ngx_http_log_op_run_pt) ngx_http_upstream_log_fmt_ops; | |
2492 | |
2493 return NGX_OK; | |
2494 } | |
509 | 2495 |
2496 | |
573 | 2497 static ngx_int_t |
2498 ngx_http_upstream_status_variable(ngx_http_request_t *r, | |
2499 ngx_http_variable_value_t *v, uintptr_t data) | |
2500 { | |
2501 u_char *p; | |
2502 size_t len; | |
2503 ngx_uint_t i; | |
2504 ngx_http_upstream_t *u; | |
2505 ngx_http_upstream_state_t *state; | |
2506 | |
2507 v->valid = 1; | |
2508 v->no_cachable = 0; | |
2509 v->not_found = 0; | |
2510 | |
2511 u = r->upstream; | |
2512 | |
2513 if (u == NULL || u->states.nelts == 0) { | |
2514 v->not_found = 1; | |
2515 return NGX_OK; | |
2516 } | |
2517 | |
2518 len = u->states.nelts * (3 + 2); | |
2519 | |
2520 p = ngx_palloc(r->pool, len); | |
2521 if (p == NULL) { | |
2522 return NGX_ERROR; | |
2523 } | |
2524 | |
2525 v->data = p; | |
2526 | |
2527 i = 0; | |
2528 state = u->states.elts; | |
2529 | |
2530 for ( ;; ) { | |
2531 if (state[i].status == 0) { | |
2532 *p++ = '-'; | |
2533 | |
2534 } else { | |
2535 p = ngx_sprintf(p, "%ui", state[i].status); | |
2536 } | |
2537 | |
2538 if (++i == u->states.nelts) { | |
2539 break; | |
2540 } | |
2541 | |
2542 *p++ = ','; | |
2543 *p++ = ' '; | |
2544 } | |
2545 | |
2546 v->len = p - v->data; | |
2547 | |
2548 return NGX_OK; | |
2549 } | |
2550 | |
2551 | |
2552 static ngx_int_t | |
2553 ngx_http_upstream_response_time_variable(ngx_http_request_t *r, | |
2554 ngx_http_variable_value_t *v, uintptr_t data) | |
2555 { | |
2556 u_char *p; | |
2557 size_t len; | |
2558 ngx_uint_t i; | |
2559 ngx_http_upstream_t *u; | |
2560 ngx_http_upstream_state_t *state; | |
2561 | |
2562 v->valid = 1; | |
2563 v->no_cachable = 0; | |
2564 v->not_found = 0; | |
2565 | |
2566 u = r->upstream; | |
2567 | |
2568 if (u == NULL || u->states.nelts == 0) { | |
2569 v->not_found = 1; | |
2570 return NGX_OK; | |
2571 } | |
2572 | |
2573 len = u->states.nelts * (NGX_TIME_T_LEN + 4 + 2); | |
2574 | |
2575 p = ngx_palloc(r->pool, len); | |
2576 if (p == NULL) { | |
2577 return NGX_ERROR; | |
2578 } | |
2579 | |
2580 v->data = p; | |
2581 | |
2582 i = 0; | |
2583 state = u->states.elts; | |
2584 | |
2585 for ( ;; ) { | |
2586 if (state[i].status == 0) { | |
2587 *p++ = '-'; | |
2588 | |
2589 } else { | |
2590 p = ngx_sprintf(p, "%d.%03d", | |
2591 state[i].response_time / 1000, | |
2592 state[i].response_time % 1000); | |
2593 } | |
2594 | |
2595 if (++i == u->states.nelts) { | |
2596 break; | |
2597 } | |
2598 | |
2599 *p++ = ','; | |
2600 *p++ = ' '; | |
2601 } | |
2602 | |
2603 v->len = p - v->data; | |
2604 | |
2605 return NGX_OK; | |
2606 } | |
2607 | |
2608 | |
651 | 2609 static char * |
2610 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) | |
2611 { | |
2612 char *rv; | |
2613 void *mconf; | |
2614 ngx_str_t *value; | |
2615 ngx_url_t u; | |
2616 ngx_uint_t i, j, m, n; | |
2617 ngx_conf_t pcf; | |
2618 ngx_peers_t **peers; | |
2619 ngx_http_module_t *module; | |
2620 ngx_http_conf_ctx_t *ctx; | |
2621 ngx_http_upstream_srv_conf_t *uscf; | |
2622 | |
2623 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); | |
2624 if (ctx == NULL) { | |
2625 return NGX_CONF_ERROR; | |
2626 } | |
2627 | |
2628 ngx_memzero(&u, sizeof(ngx_url_t)); | |
2629 | |
2630 value = cf->args->elts; | |
2631 u.host = value[1]; | |
2632 | |
2633 uscf = ngx_http_upstream_add(cf, &u); | |
2634 if (uscf == NULL) { | |
2635 return NGX_CONF_ERROR; | |
2636 } | |
2637 | |
2638 /* the upstream{}'s srv_conf */ | |
2639 | |
2640 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
2641 if (ctx->srv_conf == NULL) { | |
2642 return NGX_CONF_ERROR; | |
2643 } | |
2644 | |
2645 ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf; | |
2646 | |
2647 | |
2648 /* the upstream{}'s loc_conf */ | |
2649 | |
2650 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
2651 if (ctx->loc_conf == NULL) { | |
2652 return NGX_CONF_ERROR; | |
2653 } | |
2654 | |
2655 for (m = 0; ngx_modules[m]; m++) { | |
2656 if (ngx_modules[m]->type != NGX_HTTP_MODULE) { | |
2657 continue; | |
2658 } | |
2659 | |
2660 module = ngx_modules[m]->ctx; | |
2661 | |
2662 if (module->create_loc_conf) { | |
2663 mconf = module->create_loc_conf(cf); | |
2664 if (mconf == NULL) { | |
2665 return NGX_CONF_ERROR; | |
2666 } | |
2667 | |
2668 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf; | |
2669 } | |
2670 } | |
2671 | |
2672 | |
2673 /* parse inside upstream{} */ | |
2674 | |
2675 pcf = *cf; | |
2676 cf->ctx = ctx; | |
2677 cf->cmd_type = NGX_HTTP_UPS_CONF; | |
2678 | |
2679 rv = ngx_conf_parse(cf, NULL); | |
2680 | |
2681 *cf = pcf; | |
2682 | |
2683 if (rv != NGX_CONF_OK) { | |
2684 return rv; | |
2685 } | |
2686 | |
2687 if (uscf->servers == NULL) { | |
2688 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2689 "no servers are inside upstream"); | |
2690 return NGX_CONF_ERROR; | |
2691 } | |
2692 | |
2693 peers = uscf->servers->elts; | |
2694 | |
2695 if (uscf->servers->nelts == 1) { | |
2696 uscf->peers = peers[0]; | |
2697 } | |
2698 | |
2699 n = 0; | |
2700 | |
2701 for (i = 0; i < uscf->servers->nelts; i++) { | |
2702 n += peers[i]->number; | |
2703 } | |
2704 | |
2705 uscf->peers = ngx_pcalloc(cf->pool, | |
2706 sizeof(ngx_peers_t) + sizeof(ngx_peer_t) * (n - 1)); | |
2707 if (uscf->peers == NULL) { | |
2708 return NGX_CONF_ERROR; | |
2709 } | |
2710 | |
2711 uscf->peers->number = n; | |
2712 | |
2713 n = 0; | |
2714 | |
2715 for (i = 0; i < uscf->servers->nelts; i++) { | |
2716 for (j = 0; j < peers[i]->number; j++) { | |
2717 uscf->peers->peer[n++] = peers[i]->peer[j]; | |
2718 } | |
2719 } | |
2720 | |
2721 return rv; | |
2722 } | |
2723 | |
2724 | |
2725 static char * | |
2726 ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
2727 { | |
2728 ngx_http_upstream_srv_conf_t *uscf = conf; | |
2729 | |
2730 ngx_str_t *value; | |
2731 ngx_url_t u; | |
663 | 2732 ngx_int_t weight; |
2733 ngx_uint_t i; | |
651 | 2734 ngx_peers_t **peers; |
2735 | |
2736 if (uscf->servers == NULL) { | |
2737 uscf->servers = ngx_array_create(cf->pool, 4, sizeof(ngx_peers_t *)); | |
2738 if (uscf->servers == NULL) { | |
2739 return NGX_CONF_ERROR; | |
2740 } | |
2741 } | |
2742 | |
2743 peers = ngx_array_push(uscf->servers); | |
2744 if (peers == NULL) { | |
2745 return NGX_CONF_ERROR; | |
2746 } | |
2747 | |
2748 value = cf->args->elts; | |
2749 | |
2750 ngx_memzero(&u, sizeof(ngx_url_t)); | |
2751 | |
2752 u.url = value[1]; | |
2753 u.default_portn = 80; | |
2754 | |
2755 if (ngx_parse_url(cf, &u) != NGX_OK) { | |
2756 if (u.err) { | |
2757 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2758 "%s in upstream \"%V\"", u.err, &u.url); | |
2759 } | |
2760 | |
2761 return NGX_CONF_ERROR; | |
2762 } | |
2763 | |
663 | 2764 weight = 1; |
2765 | |
2766 if (cf->args->nelts == 3) { | |
2767 | |
2768 value = &value[2]; | |
2769 | |
2770 if (ngx_strncmp(value->data, "weight=", 7) == 0) { | |
2771 | |
2772 weight = ngx_atoi(&value->data[7], value->len - 7); | |
2773 | |
2774 if (weight == NGX_ERROR || weight == 0) { | |
2775 goto invalid; | |
2776 } | |
2777 | |
2778 } else { | |
2779 goto invalid; | |
2780 } | |
2781 } | |
2782 | |
2783 for (i = 0; i < u.peers->number; i++) { | |
2784 u.peers->peer[i].weight = weight; | |
2785 u.peers->peer[i].current_weight = weight; | |
2786 u.peers->peer[i].max_fails = NGX_CONF_UNSET_UINT; | |
2787 u.peers->peer[i].fail_timeout = NGX_CONF_UNSET; | |
2788 } | |
2789 | |
651 | 2790 *peers = u.peers; |
2791 | |
2792 return NGX_CONF_OK; | |
663 | 2793 |
2794 invalid: | |
2795 | |
2796 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", value); | |
2797 | |
2798 return NGX_CONF_ERROR; | |
651 | 2799 } |
2800 | |
2801 | |
2802 ngx_http_upstream_srv_conf_t * | |
2803 ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u) | |
2804 { | |
2805 ngx_uint_t i; | |
2806 ngx_http_upstream_srv_conf_t *uscf, **uscfp; | |
2807 ngx_http_upstream_main_conf_t *umcf; | |
2808 | |
2809 if (u->upstream) { | |
2810 if (ngx_parse_url(cf, u) != NGX_OK) { | |
2811 if (u->err) { | |
2812 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2813 "%s in upstream \"%V\"", u->err, &u->url); | |
2814 } | |
2815 | |
2816 return NULL; | |
2817 } | |
2818 | |
2819 if (u->peers) { | |
2820 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t)); | |
2821 if (uscf == NULL) { | |
2822 return NULL; | |
2823 } | |
2824 | |
2825 uscf->peers = u->peers; | |
2826 | |
2827 return uscf; | |
2828 } | |
2829 } | |
2830 | |
2831 umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module); | |
2832 | |
2833 uscfp = umcf->upstreams.elts; | |
2834 | |
2835 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
2836 if (uscfp[i]->host.len != u->host.len) { | |
2837 continue; | |
2838 } | |
2839 | |
2840 if (ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len) | |
2841 == 0) | |
2842 { | |
2843 return uscfp[i]; | |
2844 } | |
2845 } | |
2846 | |
2847 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t)); | |
2848 if (uscf == NULL) { | |
2849 return NULL; | |
2850 } | |
2851 | |
2852 uscf->host = u->host; | |
2853 uscf->file_name = cf->conf_file->file.name; | |
2854 uscf->line = cf->conf_file->line; | |
2855 uscf->port = u->default_portn; | |
2856 | |
2857 uscfp = ngx_array_push(&umcf->upstreams); | |
2858 if (uscfp == NULL) { | |
2859 return NULL; | |
2860 } | |
2861 | |
2862 *uscfp = uscf; | |
2863 | |
2864 return uscf; | |
2865 } | |
2866 | |
2867 | |
509 | 2868 static void * |
2869 ngx_http_upstream_create_main_conf(ngx_conf_t *cf) | |
2870 { | |
2871 ngx_http_upstream_main_conf_t *umcf; | |
2872 | |
2873 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t)); | |
2874 if (umcf == NULL) { | |
2875 return NULL; | |
2876 } | |
2877 | |
651 | 2878 if (ngx_array_init(&umcf->upstreams, cf->pool, 4, |
2879 sizeof(ngx_http_upstream_srv_conf_t *)) | |
2880 != NGX_OK) | |
2881 { | |
2882 return NGX_CONF_ERROR; | |
2883 } | |
2884 | |
509 | 2885 return umcf; |
2886 } | |
2887 | |
2888 | |
2889 static char * | |
651 | 2890 ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf) |
509 | 2891 { |
2892 ngx_http_upstream_main_conf_t *umcf = conf; | |
2893 | |
651 | 2894 ngx_uint_t i; |
2895 ngx_array_t headers_in; | |
2896 ngx_hash_key_t *hk; | |
2897 ngx_hash_init_t hash; | |
2898 ngx_http_upstream_header_t *header; | |
2899 ngx_http_upstream_srv_conf_t **uscfp; | |
2900 | |
2901 uscfp = umcf->upstreams.elts; | |
2902 | |
2903 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
2904 if (uscfp[i]->peers) { | |
2905 continue; | |
2906 } | |
2907 | |
2908 uscfp[i]->peers = ngx_inet_resolve_peer(cf, &uscfp[i]->host, | |
663 | 2909 uscfp[i]->port); |
651 | 2910 if (uscfp[i]->peers == NULL) { |
2911 return NGX_CONF_ERROR; | |
2912 } | |
2913 | |
2914 if (uscfp[i]->peers == NGX_CONF_ERROR) { | |
2915 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
2916 "upstream host \"%V\" is not found in %s:%ui", | |
2917 &uscfp[i]->host, uscfp[i]->file_name.data, | |
2918 uscfp[i]->line); | |
2919 return NGX_CONF_ERROR; | |
2920 } | |
2921 } | |
649 | 2922 |
663 | 2923 |
649 | 2924 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t)) |
2925 != NGX_OK) | |
509 | 2926 { |
2927 return NGX_CONF_ERROR; | |
2928 } | |
2929 | |
649 | 2930 for (header = ngx_http_upstream_headers_in; header->name.len; header++) { |
2931 hk = ngx_array_push(&headers_in); | |
2932 if (hk == NULL) { | |
2933 return NGX_CONF_ERROR; | |
2934 } | |
2935 | |
2936 hk->key = header->name; | |
2937 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len); | |
2938 hk->value = header; | |
2939 } | |
2940 | |
2941 hash.hash = &umcf->headers_in_hash; | |
2942 hash.key = ngx_hash_key_lc; | |
2943 hash.max_size = 512; | |
751
bae59a740c40
align hash bucket size to cache line
Igor Sysoev <igor@sysoev.ru>
parents:
750
diff
changeset
|
2944 hash.bucket_size = ngx_align(64, ngx_cacheline_size); |
649 | 2945 hash.name = "upstream_headers_in_hash"; |
2946 hash.pool = cf->pool; | |
2947 hash.temp_pool = NULL; | |
2948 | |
2949 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) { | |
2950 return NGX_CONF_ERROR; | |
2951 } | |
509 | 2952 |
2953 return NGX_CONF_OK; | |
2954 } |