Mercurial > hg > nginx-quic
annotate src/http/ngx_http_upstream.c @ 774:589841f06b87
previous commit broke two modules
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 12 Oct 2006 13:56:16 +0000 |
parents | a138c4e6031f |
children | 4ab852b691f5 |
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) { |
757
a138c4e6031f
not do ngx_http_upstream_finalize_request() twice
Igor Sysoev <igor@sysoev.ru>
parents:
756
diff
changeset
|
1140 |
515 | 1141 ngx_http_upstream_finalize_request(r, u, NGX_DECLINED); |
1142 | |
1143 part = &r->upstream->headers_in.headers.part; | |
1144 h = part->elts; | |
1145 | |
1146 for (i = 0; /* void */; i++) { | |
1147 | |
1148 if (i >= part->nelts) { | |
1149 if (part->next == NULL) { | |
1150 break; | |
1151 } | |
577 | 1152 |
515 | 1153 part = part->next; |
1154 h = part->elts; | |
1155 i = 0; | |
1156 } | |
1157 | |
649 | 1158 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash, |
1159 h[i].lowcase_key, h[i].key.len); | |
1160 | |
1161 if (hh && hh->redirect) { | |
1162 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) { | |
757
a138c4e6031f
not do ngx_http_upstream_finalize_request() twice
Igor Sysoev <igor@sysoev.ru>
parents:
756
diff
changeset
|
1163 ngx_http_finalize_request(r, |
a138c4e6031f
not do ngx_http_upstream_finalize_request() twice
Igor Sysoev <igor@sysoev.ru>
parents:
756
diff
changeset
|
1164 NGX_HTTP_INTERNAL_SERVER_ERROR); |
515 | 1165 return; |
1166 } | |
1167 } | |
1168 } | |
1169 | |
573 | 1170 uri = &r->upstream->headers_in.x_accel_redirect->value; |
1171 args.len = 0; | |
1172 args.data = NULL; | |
1173 flags = 0; | |
1174 | |
1175 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) { | |
757
a138c4e6031f
not do ngx_http_upstream_finalize_request() twice
Igor Sysoev <igor@sysoev.ru>
parents:
756
diff
changeset
|
1176 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND); |
573 | 1177 return; |
1178 } | |
1179 | |
1180 if (flags & NGX_HTTP_ZERO_IN_URI) { | |
1181 r->zero_in_uri = 1; | |
1182 } | |
1183 | |
653 | 1184 if (r->method != NGX_HTTP_HEAD) { |
1185 r->method = NGX_HTTP_GET; | |
1186 } | |
1187 | |
573 | 1188 ngx_http_internal_redirect(r, uri, &args); |
515 | 1189 return; |
1190 } | |
1191 | |
509 | 1192 part = &r->upstream->headers_in.headers.part; |
1193 h = part->elts; | |
1194 | |
1195 for (i = 0; /* void */; i++) { | |
479 | 1196 |
509 | 1197 if (i >= part->nelts) { |
1198 if (part->next == NULL) { | |
1199 break; | |
1200 } | |
577 | 1201 |
509 | 1202 part = part->next; |
1203 h = part->elts; | |
1204 i = 0; | |
1205 } | |
1206 | |
649 | 1207 if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash, |
1208 h[i].lowcase_key, h[i].key.len)) | |
509 | 1209 { |
649 | 1210 continue; |
1211 } | |
1212 | |
1213 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash, | |
1214 h[i].lowcase_key, h[i].key.len); | |
1215 | |
1216 if (hh) { | |
1217 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) { | |
509 | 1218 ngx_http_upstream_finalize_request(r, u, |
1219 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1220 return; | |
1221 } | |
1222 | |
1223 continue; | |
1224 } | |
1225 | |
1226 if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) { | |
1227 ngx_http_upstream_finalize_request(r, u, | |
1228 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1229 return; | |
1230 } | |
1231 } | |
1232 | |
649 | 1233 if (r->headers_out.server && r->headers_out.server->value.data == NULL) { |
1234 r->headers_out.server->hash = 0; | |
1235 } | |
1236 | |
1237 if (r->headers_out.date && r->headers_out.date->value.data == NULL) { | |
1238 r->headers_out.date->hash = 0; | |
1239 } | |
1240 | |
529 | 1241 r->headers_out.status = u->headers_in.status_n; |
1242 r->headers_out.status_line = u->headers_in.status_line; | |
1243 | |
581 | 1244 if (r->headers_out.content_length_n != -1) { |
1245 u->length = (size_t) r->headers_out.content_length_n; | |
1246 | |
1247 } else { | |
1248 u->length = NGX_MAX_SIZE_T_VALUE; | |
1249 } | |
1250 | |
681 | 1251 ngx_http_upstream_send_response(r, u); |
1252 } | |
1253 | |
1254 | |
1255 static void | |
1256 ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u) | |
1257 { | |
1258 int tcp_nodelay; | |
1259 ssize_t size; | |
1260 ngx_int_t rc; | |
1261 ngx_event_pipe_t *p; | |
1262 ngx_connection_t *c; | |
1263 ngx_pool_cleanup_t *cl; | |
1264 ngx_pool_cleanup_file_t *clf; | |
1265 ngx_http_core_loc_conf_t *clcf; | |
1266 | |
509 | 1267 rc = ngx_http_send_header(r); |
479 | 1268 |
629 | 1269 if (rc == NGX_ERROR || rc > NGX_OK || r->post_action) { |
485 | 1270 ngx_http_upstream_finalize_request(r, u, rc); |
479 | 1271 return; |
1272 } | |
1273 | |
1274 u->header_sent = 1; | |
1275 | |
573 | 1276 if (r->request_body && r->request_body->temp_file) { |
563 | 1277 for (cl = r->pool->cleanup; cl; cl = cl->next) { |
1278 if (cl->handler == ngx_pool_cleanup_file) { | |
1279 clf = cl->data; | |
1280 | |
1281 if (clf->fd == r->request_body->temp_file->file.fd) { | |
1282 cl->handler(clf); | |
1283 cl->handler = NULL; | |
659 | 1284 r->request_body->temp_file->file.fd = NGX_INVALID_FILE; |
563 | 1285 break; |
1286 } | |
1287 } | |
1288 } | |
1289 } | |
1290 | |
583 | 1291 c = r->connection; |
1292 | |
581 | 1293 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
1294 | |
649 | 1295 if (!u->buffering) { |
581 | 1296 |
1297 if (u->input_filter == NULL) { | |
1298 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init; | |
1299 u->input_filter = ngx_http_upstream_non_buffered_filter; | |
1300 u->input_filter_ctx = r; | |
1301 } | |
1302 | |
1303 u->peer.connection->read->handler = | |
1304 ngx_http_upstream_process_non_buffered_body; | |
1305 r->write_event_handler = | |
1306 ngx_http_upstream_process_non_buffered_downstream; | |
1307 | |
1308 r->limit_rate = 0; | |
1309 | |
1310 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) { | |
1311 ngx_http_upstream_finalize_request(r, u, 0); | |
1312 return; | |
1313 } | |
1314 | |
583 | 1315 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { |
1316 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay"); | |
1317 | |
1318 tcp_nodelay = 1; | |
1319 | |
1320 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, | |
1321 (const void *) &tcp_nodelay, sizeof(int)) == -1) | |
1322 { | |
1323 ngx_connection_error(c, ngx_socket_errno, | |
1324 "setsockopt(TCP_NODELAY) failed"); | |
1325 ngx_http_upstream_finalize_request(r, u, 0); | |
1326 return; | |
1327 } | |
1328 | |
1329 c->tcp_nodelay = NGX_TCP_NODELAY_SET; | |
1330 } | |
1331 | |
581 | 1332 size = u->buffer.last - u->buffer.pos; |
1333 | |
1334 if (size) { | |
1335 u->buffer.last = u->buffer.pos; | |
1336 | |
1337 if (u->input_filter(u->input_filter_ctx, size) == NGX_ERROR) { | |
1338 ngx_http_upstream_finalize_request(r, u, 0); | |
1339 return; | |
1340 } | |
1341 | |
583 | 1342 ngx_http_upstream_process_non_buffered_body(c->write); |
581 | 1343 |
1344 } else { | |
1345 u->buffer.pos = u->buffer.start; | |
1346 u->buffer.last = u->buffer.start; | |
1347 | |
1348 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) { | |
1349 ngx_http_upstream_finalize_request(r, u, 0); | |
1350 return; | |
1351 } | |
1352 } | |
1353 | |
1354 return; | |
1355 } | |
1356 | |
479 | 1357 /* TODO: preallocate event_pipe bufs, look "Content-Length" */ |
1358 | |
1359 #if 0 | |
1360 | |
1361 if (u->cache && u->cache->ctx.file.fd != NGX_INVALID_FILE) { | |
1362 if (ngx_close_file(u->cache->ctx.file.fd) == NGX_FILE_ERROR) { | |
583 | 1363 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
479 | 1364 ngx_close_file_n " \"%s\" failed", |
1365 u->cache->ctx.file.name.data); | |
1366 } | |
1367 } | |
1368 | |
1369 if (u->cachable) { | |
581 | 1370 header = (ngx_http_cache_header_t *) u->buffer->start; |
479 | 1371 |
1372 header->expires = u->cache->ctx.expires; | |
1373 header->last_modified = u->cache->ctx.last_modified; | |
1374 header->date = u->cache->ctx.date; | |
1375 header->length = r->headers_out.content_length_n; | |
1376 u->cache->ctx.length = r->headers_out.content_length_n; | |
1377 | |
1378 header->key_len = u->cache->ctx.key0.len; | |
1379 ngx_memcpy(&header->key, u->cache->ctx.key0.data, header->key_len); | |
1380 header->key[header->key_len] = LF; | |
1381 } | |
1382 | |
1383 #endif | |
1384 | |
581 | 1385 p = u->pipe; |
479 | 1386 |
1387 p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter; | |
1388 p->output_ctx = r; | |
1389 p->tag = u->output.tag; | |
577 | 1390 p->bufs = u->conf->bufs; |
479 | 1391 p->busy_size = u->conf->busy_buffers_size; |
1392 p->upstream = u->peer.connection; | |
583 | 1393 p->downstream = c; |
479 | 1394 p->pool = r->pool; |
583 | 1395 p->log = c->log; |
577 | 1396 |
479 | 1397 p->cachable = u->cachable; |
1398 | |
501 | 1399 p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)); |
1400 if (p->temp_file == NULL) { | |
479 | 1401 ngx_http_upstream_finalize_request(r, u, 0); |
1402 return; | |
1403 } | |
1404 | |
1405 p->temp_file->file.fd = NGX_INVALID_FILE; | |
583 | 1406 p->temp_file->file.log = c->log; |
479 | 1407 p->temp_file->path = u->conf->temp_path; |
1408 p->temp_file->pool = r->pool; | |
577 | 1409 |
479 | 1410 if (u->cachable) { |
1411 p->temp_file->persistent = 1; | |
1412 } else { | |
667 | 1413 p->temp_file->log_level = NGX_LOG_WARN; |
479 | 1414 p->temp_file->warn = "an upstream response is buffered " |
1415 "to a temporary file"; | |
1416 } | |
1417 | |
1418 p->max_temp_file_size = u->conf->max_temp_file_size; | |
1419 p->temp_file_write_size = u->conf->temp_file_write_size; | |
1420 | |
501 | 1421 p->preread_bufs = ngx_alloc_chain_link(r->pool); |
1422 if (p->preread_bufs == NULL) { | |
479 | 1423 ngx_http_upstream_finalize_request(r, u, 0); |
1424 return; | |
1425 } | |
581 | 1426 p->preread_bufs->buf = &u->buffer; |
479 | 1427 p->preread_bufs->next = NULL; |
581 | 1428 u->buffer.recycled = 1; |
1429 | |
1430 p->preread_size = u->buffer.last - u->buffer.pos; | |
479 | 1431 |
1432 if (u->cachable) { | |
1433 p->buf_to_file = ngx_calloc_buf(r->pool); | |
1434 if (p->buf_to_file == NULL) { | |
1435 ngx_http_upstream_finalize_request(r, u, 0); | |
1436 return; | |
1437 } | |
581 | 1438 p->buf_to_file->pos = u->buffer.start; |
1439 p->buf_to_file->last = u->buffer.pos; | |
479 | 1440 p->buf_to_file->temporary = 1; |
1441 } | |
1442 | |
1443 if (ngx_event_flags & NGX_USE_AIO_EVENT) { | |
1444 /* the posted aio operation may currupt a shadow buffer */ | |
1445 p->single_buf = 1; | |
1446 } | |
1447 | |
1448 /* TODO: p->free_bufs = 0 if use ngx_create_chain_of_bufs() */ | |
1449 p->free_bufs = 1; | |
1450 | |
1451 /* | |
581 | 1452 * event_pipe would do u->buffer.last += p->preread_size |
479 | 1453 * as though these bytes were read |
1454 */ | |
581 | 1455 u->buffer.last = u->buffer.pos; |
479 | 1456 |
1457 if (u->conf->cyclic_temp_file) { | |
1458 | |
1459 /* | |
1460 * we need to disable the use of sendfile() if we use cyclic temp file | |
1461 * because the writing a new data may interfere with sendfile() | |
1462 * that uses the same kernel file pages (at least on FreeBSD) | |
1463 */ | |
1464 | |
1465 p->cyclic_temp_file = 1; | |
583 | 1466 c->sendfile = 0; |
479 | 1467 |
1468 } else { | |
1469 p->cyclic_temp_file = 0; | |
1470 } | |
1471 | |
1472 p->read_timeout = u->conf->read_timeout; | |
1473 p->send_timeout = clcf->send_timeout; | |
1474 p->send_lowat = clcf->send_lowat; | |
1475 | |
509 | 1476 u->peer.connection->read->handler = ngx_http_upstream_process_body; |
1477 r->write_event_handler = ngx_http_upstream_process_downstream; | |
479 | 1478 |
1479 ngx_http_upstream_process_body(u->peer.connection->read); | |
1480 } | |
1481 | |
1482 | |
487 | 1483 static void |
581 | 1484 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r) |
1485 { | |
1486 ngx_http_upstream_process_non_buffered_body(r->connection->write); | |
1487 } | |
1488 | |
1489 | |
1490 static void | |
1491 ngx_http_upstream_process_non_buffered_body(ngx_event_t *ev) | |
1492 { | |
1493 size_t size; | |
1494 ssize_t n; | |
1495 ngx_buf_t *b; | |
583 | 1496 ngx_int_t rc; |
581 | 1497 ngx_uint_t do_write; |
583 | 1498 ngx_connection_t *c, *client; |
581 | 1499 ngx_http_request_t *r; |
1500 ngx_http_upstream_t *u; | |
1501 ngx_http_core_loc_conf_t *clcf; | |
1502 | |
1503 c = ev->data; | |
756
d904b5cb3bba
fix building introduced by previous commit
Igor Sysoev <igor@sysoev.ru>
parents:
755
diff
changeset
|
1504 r = c->data; |
d904b5cb3bba
fix building introduced by previous commit
Igor Sysoev <igor@sysoev.ru>
parents:
755
diff
changeset
|
1505 u = r->upstream; |
581 | 1506 |
1507 if (ev->write) { | |
1508 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1509 "http upstream process non buffered downstream"); | |
1510 c->log->action = "sending to client"; | |
1511 | |
1512 } else { | |
1513 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1514 "http upstream process non buffered upstream"); | |
1515 c->log->action = "reading upstream"; | |
1516 } | |
1517 | |
1518 if (ev->timedout) { | |
1519 if (ev->write) { | |
1520 c->timedout = 1; | |
611 | 1521 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out"); |
1522 | |
581 | 1523 } else { |
611 | 1524 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
581 | 1525 } |
755
63b9dc652c3d
finalize nonbuffered request on timeout
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
1526 |
63b9dc652c3d
finalize nonbuffered request on timeout
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
1527 ngx_http_upstream_finalize_request(r, u, 0); |
63b9dc652c3d
finalize nonbuffered request on timeout
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
1528 return; |
581 | 1529 } |
1530 | |
583 | 1531 client = r->connection; |
581 | 1532 |
1533 b = &u->buffer; | |
1534 | |
1535 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1536 | |
657 | 1537 do_write = ev->write || u->length == 0; |
581 | 1538 |
1539 for ( ;; ) { | |
1540 | |
1541 if (do_write) { | |
1542 | |
1543 if (u->out_bufs || u->busy_bufs) { | |
583 | 1544 rc = ngx_http_output_filter(r, u->out_bufs); |
1545 | |
1546 if (client->destroyed) { | |
1547 return; | |
1548 } | |
1549 | |
1550 if (rc == NGX_ERROR) { | |
581 | 1551 ngx_http_upstream_finalize_request(r, u, 0); |
1552 return; | |
1553 } | |
1554 | |
1555 ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs, | |
1556 &u->out_bufs, u->output.tag); | |
1557 } | |
1558 | |
1559 if (u->busy_bufs == NULL) { | |
1560 | |
1561 if (u->length == 0 | |
1562 || u->peer.connection->read->eof | |
1563 || u->peer.connection->read->error) | |
1564 { | |
1565 ngx_http_upstream_finalize_request(r, u, 0); | |
1566 return; | |
1567 } | |
1568 | |
1569 b->pos = b->start; | |
1570 b->last = b->start; | |
1571 } | |
1572 } | |
1573 | |
1574 size = b->end - b->last; | |
1575 | |
1576 if (size > u->length) { | |
1577 size = u->length; | |
1578 } | |
1579 | |
1580 if (size && u->peer.connection->read->ready) { | |
657 | 1581 |
581 | 1582 n = u->peer.connection->recv(u->peer.connection, b->last, size); |
1583 | |
1584 if (n == NGX_AGAIN) { | |
1585 break; | |
1586 } | |
1587 | |
1588 if (n > 0) { | |
1589 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { | |
1590 ngx_http_upstream_finalize_request(r, u, 0); | |
1591 return; | |
1592 } | |
1593 } | |
1594 | |
1595 do_write = 1; | |
1596 | |
1597 continue; | |
1598 } | |
1599 | |
1600 break; | |
1601 } | |
1602 | |
583 | 1603 if (client->data == r) { |
1604 if (ngx_handle_write_event(client->write, clcf->send_lowat) | |
1605 == NGX_ERROR) | |
1606 { | |
1607 ngx_http_upstream_finalize_request(r, u, 0); | |
1608 return; | |
1609 } | |
581 | 1610 } |
1611 | |
583 | 1612 if (client->write->active) { |
1613 ngx_add_timer(client->write, clcf->send_timeout); | |
1614 | |
1615 } else if (client->write->timer_set) { | |
1616 ngx_del_timer(client->write); | |
581 | 1617 } |
1618 | |
1619 if (ngx_handle_read_event(u->peer.connection->read, 0) == NGX_ERROR) { | |
1620 ngx_http_upstream_finalize_request(r, u, 0); | |
1621 return; | |
1622 } | |
1623 | |
1624 if (u->peer.connection->read->active) { | |
1625 ngx_add_timer(u->peer.connection->read, u->conf->read_timeout); | |
1626 | |
583 | 1627 } else if (u->peer.connection->read->timer_set) { |
1628 ngx_del_timer(u->peer.connection->read); | |
581 | 1629 } |
1630 } | |
1631 | |
1632 | |
1633 static ngx_int_t | |
1634 ngx_http_upstream_non_buffered_filter_init(void *data) | |
1635 { | |
1636 return NGX_OK; | |
1637 } | |
1638 | |
1639 | |
1640 static ngx_int_t | |
1641 ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes) | |
1642 { | |
1643 ngx_http_request_t *r = data; | |
1644 | |
1645 ngx_buf_t *b; | |
1646 ngx_chain_t *cl, **ll; | |
1647 ngx_http_upstream_t *u; | |
1648 | |
1649 u = r->upstream; | |
1650 | |
1651 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { | |
1652 ll = &cl->next; | |
1653 } | |
1654 | |
1655 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); | |
1656 if (cl == NULL) { | |
1657 return NGX_ERROR; | |
1658 } | |
1659 | |
1660 *ll = cl; | |
1661 | |
1662 cl->buf->flush = 1; | |
1663 cl->buf->memory = 1; | |
1664 | |
1665 b = &u->buffer; | |
1666 | |
1667 cl->buf->pos = b->last; | |
1668 b->last += bytes; | |
1669 cl->buf->last = b->last; | |
1670 | |
1671 if (u->length == NGX_MAX_SIZE_T_VALUE) { | |
1672 return NGX_OK; | |
1673 } | |
1674 | |
1675 u->length -= bytes; | |
1676 | |
1677 return NGX_OK; | |
1678 } | |
1679 | |
1680 | |
1681 static void | |
509 | 1682 ngx_http_upstream_process_downstream(ngx_http_request_t *r) |
1683 { | |
1684 ngx_http_upstream_process_body(r->connection->write); | |
1685 } | |
1686 | |
1687 | |
1688 static void | |
487 | 1689 ngx_http_upstream_process_body(ngx_event_t *ev) |
479 | 1690 { |
509 | 1691 ngx_event_pipe_t *p; |
583 | 1692 ngx_connection_t *c, *downstream; |
479 | 1693 ngx_http_request_t *r; |
1694 ngx_http_upstream_t *u; | |
1695 | |
1696 c = ev->data; | |
1697 r = c->data; | |
1698 u = r->upstream; | |
583 | 1699 downstream = r->connection; |
479 | 1700 |
1701 if (ev->write) { | |
483 | 1702 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1703 "http upstream process downstream"); | |
1704 c->log->action = "sending to client"; | |
577 | 1705 |
479 | 1706 } else { |
483 | 1707 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1708 "http upstream process upstream"); | |
1709 c->log->action = "reading upstream"; | |
479 | 1710 } |
577 | 1711 |
581 | 1712 p = u->pipe; |
479 | 1713 |
1714 if (ev->timedout) { | |
1715 if (ev->write) { | |
527 | 1716 if (ev->delayed) { |
1717 | |
1718 ev->timedout = 0; | |
1719 ev->delayed = 0; | |
1720 | |
1721 if (!ev->ready) { | |
1722 ngx_add_timer(ev, p->send_timeout); | |
1723 | |
1724 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) | |
1725 { | |
1726 ngx_http_upstream_finalize_request(r, u, 0); | |
1727 return; | |
1728 } | |
1729 | |
1730 return; | |
1731 } | |
1732 | |
1733 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) { | |
583 | 1734 |
1735 if (downstream->destroyed) { | |
1736 return; | |
1737 } | |
1738 | |
527 | 1739 ngx_http_upstream_finalize_request(r, u, 0); |
1740 return; | |
1741 } | |
1742 | |
1743 } else { | |
1744 p->downstream_error = 1; | |
577 | 1745 c->timedout = 1; |
611 | 1746 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out"); |
527 | 1747 } |
479 | 1748 |
1749 } else { | |
577 | 1750 p->upstream_error = 1; |
611 | 1751 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
479 | 1752 } |
1753 | |
1754 } else { | |
527 | 1755 if (ev->write && ev->delayed) { |
1756 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1757 "http downstream delayed"); | |
1758 | |
1759 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) { | |
1760 return; | |
1761 } | |
1762 | |
1763 return; | |
1764 } | |
1765 | |
479 | 1766 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) { |
583 | 1767 |
1768 if (downstream->destroyed) { | |
1769 return; | |
1770 } | |
1771 | |
479 | 1772 ngx_http_upstream_finalize_request(r, u, 0); |
1773 return; | |
1774 } | |
1775 } | |
577 | 1776 |
479 | 1777 if (u->peer.connection) { |
1778 | |
1779 #if (NGX_HTTP_FILE_CACHE) | |
1780 | |
1781 if (p->upstream_done && u->cachable) { | |
1782 if (ngx_http_cache_update(r) == NGX_ERROR) { | |
1783 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1784 ngx_http_upstream_finalize_request(r, u, 0); | |
1785 return; | |
1786 } | |
1787 | |
1788 } else if (p->upstream_eof && u->cachable) { | |
1789 | |
1790 /* TODO: check length & update cache */ | |
1791 | |
1792 if (ngx_http_cache_update(r) == NGX_ERROR) { | |
1793 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1794 ngx_http_upstream_finalize_request(r, u, 0); | |
1795 return; | |
1796 } | |
1797 } | |
1798 | |
1799 #endif | |
1800 | |
1801 if (p->upstream_done || p->upstream_eof || p->upstream_error) { | |
483 | 1802 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1803 "http upstream exit: %p", p->out); | |
479 | 1804 #if 0 |
1805 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1806 #endif | |
1807 ngx_http_upstream_finalize_request(r, u, 0); | |
1808 return; | |
1809 } | |
1810 } | |
1811 | |
1812 if (p->downstream_error) { | |
483 | 1813 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1814 "http upstream downstream error"); | |
479 | 1815 |
1816 if (!u->cachable && u->peer.connection) { | |
1817 ngx_http_upstream_finalize_request(r, u, 0); | |
1818 } | |
1819 } | |
1820 } | |
1821 | |
1822 | |
487 | 1823 static void |
1824 ngx_http_upstream_dummy_handler(ngx_event_t *wev) | |
479 | 1825 { |
1826 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, | |
1827 "http upstream dummy handler"); | |
1828 } | |
1829 | |
1830 | |
487 | 1831 static void |
1832 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u, | |
1833 ngx_uint_t ft_type) | |
479 | 1834 { |
525 | 1835 ngx_uint_t status, down; |
479 | 1836 |
1837 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1838 "http next upstream, %xD", ft_type); | |
1839 | |
1840 #if 0 | |
1841 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1842 #endif | |
1843 | |
525 | 1844 if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) { |
1845 down = 0; | |
1846 } else { | |
1847 down = 1; | |
479 | 1848 } |
525 | 1849 |
1850 ngx_event_connect_peer_failed(&u->peer, down); | |
1851 | |
479 | 1852 if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) { |
1853 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT, | |
1854 "upstream timed out"); | |
1855 } | |
577 | 1856 |
479 | 1857 if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) { |
1858 status = 0; | |
1859 | |
1860 } else { | |
1861 switch(ft_type) { | |
1862 | |
1863 case NGX_HTTP_UPSTREAM_FT_TIMEOUT: | |
1864 status = NGX_HTTP_GATEWAY_TIME_OUT; | |
1865 break; | |
1866 | |
1867 case NGX_HTTP_UPSTREAM_FT_HTTP_500: | |
1868 status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1869 break; | |
1870 | |
1871 case NGX_HTTP_UPSTREAM_FT_HTTP_404: | |
1872 status = NGX_HTTP_NOT_FOUND; | |
1873 break; | |
1874 | |
1875 /* | |
1876 * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING | |
1877 * never reach here | |
1878 */ | |
1879 | |
1880 default: | |
1881 status = NGX_HTTP_BAD_GATEWAY; | |
1882 } | |
1883 } | |
1884 | |
583 | 1885 if (r->connection->error) { |
479 | 1886 ngx_http_upstream_finalize_request(r, u, |
1887 NGX_HTTP_CLIENT_CLOSED_REQUEST); | |
1888 return; | |
1889 } | |
1890 | |
1891 if (status) { | |
1892 u->state->status = status; | |
1893 | |
525 | 1894 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) { |
479 | 1895 |
1896 #if (NGX_HTTP_CACHE) | |
1897 | |
1898 if (u->stale && (u->conf->use_stale & ft_type)) { | |
1899 ngx_http_upstream_finalize_request(r, u, | |
525 | 1900 ngx_http_send_cached_response(r)); |
479 | 1901 return; |
1902 } | |
1903 | |
1904 #endif | |
1905 | |
1906 ngx_http_upstream_finalize_request(r, u, status); | |
1907 return; | |
1908 } | |
1909 } | |
1910 | |
1911 if (u->peer.connection) { | |
1912 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1913 "close http upstream connection: %d", | |
1914 u->peer.connection->fd); | |
577 | 1915 #if (NGX_HTTP_SSL) |
1916 if (u->peer.connection->ssl) { | |
1917 ngx_http_upstream_ssl_shutdown(u->peer.connection, | |
1918 &u->peer.peers->peer[u->peer.cur_peer]); | |
1919 } | |
1920 #endif | |
479 | 1921 ngx_close_connection(u->peer.connection); |
1922 } | |
1923 | |
1924 #if 0 | |
1925 if (u->conf->busy_lock && !u->busy_locked) { | |
1926 ngx_http_upstream_busy_lock(p); | |
1927 return; | |
1928 } | |
1929 #endif | |
1930 | |
1931 ngx_http_upstream_connect(r, u); | |
1932 } | |
1933 | |
1934 | |
487 | 1935 static void |
569 | 1936 ngx_http_upstream_cleanup(void *data) |
1937 { | |
1938 ngx_http_request_t *r = data; | |
1939 | |
1940 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1941 "cleanup http upstream request: \"%V\"", &r->uri); | |
1942 | |
1943 ngx_http_upstream_finalize_request(r, r->upstream, NGX_DONE); | |
1944 } | |
1945 | |
1946 | |
1947 static void | |
487 | 1948 ngx_http_upstream_finalize_request(ngx_http_request_t *r, |
1949 ngx_http_upstream_t *u, ngx_int_t rc) | |
479 | 1950 { |
563 | 1951 ngx_time_t *tp; |
1952 | |
483 | 1953 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1954 "finalize http upstream request: %i", rc); | |
479 | 1955 |
569 | 1956 *u->cleanup = NULL; |
1957 | |
563 | 1958 if (u->state->response_time) { |
1959 tp = ngx_timeofday(); | |
1960 u->state->response_time = tp->sec * 1000 + tp->msec | |
1961 - u->state->response_time; | |
1962 } | |
1963 | |
479 | 1964 u->finalize_request(r, rc); |
1965 | |
1966 if (u->peer.connection) { | |
1967 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1968 "close http upstream connection: %d", | |
1969 u->peer.connection->fd); | |
577 | 1970 #if (NGX_HTTP_SSL) |
1971 | |
1972 /* TODO: do not shutdown persistent connection */ | |
1973 | |
1974 if (u->peer.connection->ssl) { | |
1975 ngx_http_upstream_ssl_shutdown(u->peer.connection, | |
1976 &u->peer.peers->peer[u->peer.cur_peer]); | |
1977 } | |
1978 #endif | |
479 | 1979 ngx_close_connection(u->peer.connection); |
1980 } | |
1981 | |
483 | 1982 u->peer.connection = NULL; |
1983 | |
563 | 1984 if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE)) |
479 | 1985 { |
1986 rc = 0; | |
1987 } | |
1988 | |
581 | 1989 if (u->pipe && u->pipe->temp_file) { |
479 | 1990 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1991 "http upstream temp fd: %d", | |
581 | 1992 u->pipe->temp_file->file.fd); |
479 | 1993 } |
1994 | |
1995 #if 0 | |
1996 if (u->cache) { | |
1997 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
483 | 1998 "http upstream cache fd: %d", |
479 | 1999 u->cache->ctx.file.fd); |
2000 } | |
2001 #endif | |
2002 | |
515 | 2003 if (rc == NGX_DECLINED) { |
2004 return; | |
2005 } | |
2006 | |
483 | 2007 r->connection->log->action = "sending to client"; |
2008 | |
629 | 2009 if (rc == 0 && r == r->main && !r->post_action) { |
581 | 2010 rc = ngx_http_send_special(r, NGX_HTTP_LAST); |
479 | 2011 } |
2012 | |
2013 ngx_http_finalize_request(r, rc); | |
2014 } | |
2015 | |
2016 | |
577 | 2017 #if (NGX_HTTP_SSL) |
2018 | |
2019 static void | |
2020 ngx_http_upstream_ssl_shutdown(ngx_connection_t *c, ngx_peer_t *peer) | |
2021 { | |
2022 /* lock peer mutex */ | |
2023 | |
2024 if (peer->ssl_session) { | |
2025 ngx_ssl_free_session(peer->ssl_session); | |
2026 } | |
2027 | |
2028 peer->ssl_session = ngx_ssl_get_session(c); | |
2029 | |
2030 /* unlock peer mutex */ | |
2031 | |
2032 /* | |
2033 * We send the "close notify" shutdown alert to the upstream only | |
2034 * and do not wait its "close notify" shutdown alert. | |
2035 * It is acceptable according to the TLS standard. | |
2036 */ | |
2037 | |
2038 c->ssl->no_wait_shutdown = 1; | |
2039 | |
2040 (void) ngx_ssl_shutdown(c); | |
2041 } | |
2042 | |
2043 #endif | |
2044 | |
2045 | |
509 | 2046 static ngx_int_t |
2047 ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2048 ngx_uint_t offset) | |
577 | 2049 { |
509 | 2050 ngx_table_elt_t **ph; |
577 | 2051 |
509 | 2052 ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset); |
2053 | |
2054 if (*ph == NULL) { | |
2055 *ph = h; | |
2056 } | |
2057 | |
2058 return NGX_OK; | |
2059 } | |
2060 | |
2061 | |
2062 static ngx_int_t | |
2063 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r, | |
2064 ngx_table_elt_t *h, ngx_uint_t offset) | |
2065 { | |
2066 ngx_array_t *pa; | |
2067 ngx_table_elt_t **ph; | |
2068 | |
2069 pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset); | |
2070 | |
2071 if (pa->elts == NULL) { | |
2072 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) | |
2073 { | |
2074 return NGX_ERROR; | |
2075 } | |
2076 } | |
2077 | |
2078 ph = ngx_array_push(pa); | |
2079 if (ph == NULL) { | |
2080 return NGX_ERROR; | |
2081 } | |
2082 | |
2083 *ph = h; | |
2084 | |
2085 return NGX_OK; | |
2086 } | |
2087 | |
2088 | |
2089 static ngx_int_t | |
2090 ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2091 ngx_uint_t offset) | |
2092 { | |
2093 return NGX_OK; | |
2094 } | |
2095 | |
2096 | |
2097 static ngx_int_t | |
527 | 2098 ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h, |
2099 ngx_uint_t offset) | |
577 | 2100 { |
527 | 2101 ngx_int_t n; |
2102 | |
2103 r->upstream->headers_in.x_accel_limit_rate = h; | |
2104 | |
2105 n = ngx_atoi(h->value.data, h->value.len); | |
2106 | |
2107 if (n != NGX_ERROR) { | |
2108 r->limit_rate = (size_t) n; | |
2109 } | |
2110 | |
2111 return NGX_OK; | |
2112 } | |
2113 | |
2114 | |
2115 static ngx_int_t | |
649 | 2116 ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h, |
2117 ngx_uint_t offset) | |
2118 { | |
2119 u_char c0, c1, c2; | |
2120 | |
2121 if (r->upstream->conf->change_buffering) { | |
2122 | |
2123 if (h->value.len == 2) { | |
2124 c0 = ngx_tolower(h->value.data[0]); | |
2125 c1 = ngx_tolower(h->value.data[1]); | |
2126 | |
2127 if (c0 == 'n' && c1 == 'o') { | |
2128 r->upstream->buffering = 0; | |
2129 } | |
2130 | |
2131 } else if (h->value.len == 3) { | |
2132 c0 = ngx_tolower(h->value.data[0]); | |
2133 c1 = ngx_tolower(h->value.data[1]); | |
2134 c2 = ngx_tolower(h->value.data[2]); | |
2135 | |
2136 if (c0 == 'y' && c1 == 'e' && c2 == 's') { | |
2137 r->upstream->buffering = 1; | |
2138 } | |
2139 } | |
2140 } | |
2141 | |
2142 return NGX_OK; | |
2143 } | |
2144 | |
2145 | |
2146 static ngx_int_t | |
657 | 2147 ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h, |
2148 ngx_uint_t offset) | |
2149 { | |
2150 r->headers_out.override_charset = &h->value; | |
2151 | |
2152 return NGX_OK; | |
2153 } | |
2154 | |
2155 | |
2156 static ngx_int_t | |
509 | 2157 ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, |
2158 ngx_uint_t offset) | |
2159 { | |
573 | 2160 ngx_table_elt_t *ho, **ph; |
509 | 2161 |
2162 ho = ngx_list_push(&r->headers_out.headers); | |
2163 if (ho == NULL) { | |
2164 return NGX_ERROR; | |
2165 } | |
2166 | |
2167 *ho = *h; | |
2168 | |
573 | 2169 if (offset) { |
2170 ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset); | |
2171 *ph = ho; | |
2172 } | |
2173 | |
509 | 2174 return NGX_OK; |
2175 } | |
2176 | |
2177 | |
2178 static ngx_int_t | |
2179 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r, | |
2180 ngx_table_elt_t *h, ngx_uint_t offset) | |
2181 { | |
2182 ngx_array_t *pa; | |
2183 ngx_table_elt_t *ho, **ph; | |
2184 | |
2185 pa = (ngx_array_t *) ((char *) &r->headers_out + offset); | |
2186 | |
2187 if (pa->elts == NULL) { | |
2188 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) | |
2189 { | |
2190 return NGX_ERROR; | |
2191 } | |
2192 } | |
2193 | |
2194 ph = ngx_array_push(pa); | |
2195 if (ph == NULL) { | |
2196 return NGX_ERROR; | |
2197 } | |
2198 | |
2199 ho = ngx_list_push(&r->headers_out.headers); | |
2200 if (ho == NULL) { | |
2201 return NGX_ERROR; | |
2202 } | |
2203 | |
2204 *ho = *h; | |
2205 *ph = ho; | |
2206 | |
2207 return NGX_OK; | |
2208 } | |
2209 | |
2210 | |
2211 static ngx_int_t | |
2212 ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2213 ngx_uint_t offset) | |
2214 { | |
657 | 2215 u_char *p, *last; |
2216 | |
2217 r->headers_out.content_type_len = h->value.len; | |
509 | 2218 r->headers_out.content_type = h->value; |
2219 | |
657 | 2220 for (p = h->value.data; *p; p++) { |
2221 | |
2222 if (*p != ';') { | |
2223 continue; | |
2224 } | |
2225 | |
2226 last = p; | |
2227 | |
2228 while (*++p == ' ') { /* void */ } | |
2229 | |
2230 if (ngx_strncasecmp(p, "charset=", 8) != 0) { | |
2231 continue; | |
2232 } | |
2233 | |
2234 p += 8; | |
2235 | |
2236 r->headers_out.content_type_len = last - h->value.data; | |
2237 | |
2238 r->headers_out.charset.len = h->value.data + h->value.len - p; | |
2239 r->headers_out.charset.data = p; | |
2240 } | |
2241 | |
509 | 2242 return NGX_OK; |
2243 } | |
2244 | |
2245 | |
2246 static ngx_int_t | |
2247 ngx_http_upstream_copy_content_length(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2248 ngx_uint_t offset) | |
2249 { | |
2250 ngx_table_elt_t *ho; | |
2251 | |
2252 ho = ngx_list_push(&r->headers_out.headers); | |
2253 if (ho == NULL) { | |
2254 return NGX_ERROR; | |
2255 } | |
2256 | |
2257 *ho = *h; | |
2258 | |
2259 r->headers_out.content_length = ho; | |
2260 r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len); | |
2261 | |
2262 return NGX_OK; | |
2263 } | |
2264 | |
2265 | |
2266 static ngx_int_t | |
2267 ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2268 ngx_uint_t offset) | |
2269 { | |
2270 ngx_int_t rc; | |
2271 ngx_table_elt_t *ho; | |
2272 | |
2273 ho = ngx_list_push(&r->headers_out.headers); | |
2274 if (ho == NULL) { | |
2275 return NGX_ERROR; | |
2276 } | |
2277 | |
2278 *ho = *h; | |
2279 | |
2280 if (r->upstream->rewrite_redirect) { | |
2281 rc = r->upstream->rewrite_redirect(r, ho, 0); | |
2282 | |
529 | 2283 if (rc == NGX_DECLINED) { |
2284 return NGX_OK; | |
2285 } | |
2286 | |
509 | 2287 if (rc == NGX_OK) { |
2288 r->headers_out.location = ho; | |
2289 | |
2290 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2291 "rewritten location: \"%V\"", &ho->value); | |
2292 } | |
2293 | |
2294 return rc; | |
2295 } | |
2296 | |
2297 /* | |
2298 * we do not set r->headers_out.location here to avoid the handling | |
2299 * the local redirects without a host name by ngx_http_header_filter() | |
2300 */ | |
2301 | |
2302 return NGX_OK; | |
2303 } | |
2304 | |
2305 | |
2306 static ngx_int_t | |
2307 ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2308 ngx_uint_t offset) | |
2309 { | |
2310 u_char *p; | |
2311 ngx_int_t rc; | |
2312 ngx_table_elt_t *ho; | |
2313 | |
2314 ho = ngx_list_push(&r->headers_out.headers); | |
2315 if (ho == NULL) { | |
2316 return NGX_ERROR; | |
2317 } | |
2318 | |
2319 *ho = *h; | |
2320 | |
2321 if (r->upstream->rewrite_redirect) { | |
2322 | |
2323 p = (u_char *) ngx_strstr(ho->value.data, "url="); | |
2324 | |
2325 if (p) { | |
2326 rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data); | |
2327 | |
2328 } else { | |
2329 return NGX_OK; | |
2330 } | |
2331 | |
529 | 2332 if (rc == NGX_DECLINED) { |
2333 return NGX_OK; | |
2334 } | |
2335 | |
509 | 2336 #if (NGX_DEBUG) |
2337 if (rc == NGX_OK) { | |
2338 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2339 "rewritten refresh: \"%V\"", &ho->value); | |
2340 } | |
2341 #endif | |
2342 | |
2343 return rc; | |
2344 } | |
2345 | |
2346 return NGX_OK; | |
2347 } | |
2348 | |
2349 | |
2350 #if (NGX_HTTP_GZIP) | |
2351 | |
2352 static ngx_int_t | |
2353 ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r, | |
2354 ngx_table_elt_t *h, ngx_uint_t offset) | |
2355 { | |
2356 ngx_table_elt_t *ho; | |
2357 | |
2358 ho = ngx_list_push(&r->headers_out.headers); | |
2359 if (ho == NULL) { | |
2360 return NGX_ERROR; | |
2361 } | |
2362 | |
2363 *ho = *h; | |
2364 | |
2365 r->headers_out.content_encoding = ho; | |
2366 | |
2367 return NGX_OK; | |
2368 } | |
2369 | |
2370 #endif | |
2371 | |
2372 | |
487 | 2373 static size_t |
2374 ngx_http_upstream_log_status_getlen(ngx_http_request_t *r, uintptr_t data) | |
479 | 2375 { |
2376 if (r->upstream) { | |
2377 return r->upstream->states.nelts * (3 + 2); | |
2378 } | |
2379 | |
2380 return 1; | |
2381 } | |
2382 | |
2383 | |
487 | 2384 static u_char * |
2385 ngx_http_upstream_log_status(ngx_http_request_t *r, u_char *buf, | |
2386 ngx_http_log_op_t *op) | |
479 | 2387 { |
2388 ngx_uint_t i; | |
2389 ngx_http_upstream_t *u; | |
2390 ngx_http_upstream_state_t *state; | |
2391 | |
2392 u = r->upstream; | |
2393 | |
573 | 2394 if (u == NULL || u->states.nelts == 0) { |
479 | 2395 *buf = '-'; |
2396 return buf + 1; | |
2397 } | |
2398 | |
2399 i = 0; | |
2400 state = u->states.elts; | |
2401 | |
2402 for ( ;; ) { | |
2403 if (state[i].status == 0) { | |
2404 *buf++ = '-'; | |
2405 | |
2406 } else { | |
2407 buf = ngx_sprintf(buf, "%ui", state[i].status); | |
2408 } | |
2409 | |
2410 if (++i == u->states.nelts) { | |
2411 return buf; | |
2412 } | |
2413 | |
2414 *buf++ = ','; | |
2415 *buf++ = ' '; | |
2416 } | |
2417 } | |
2418 | |
2419 | |
573 | 2420 static size_t |
2421 ngx_http_upstream_log_response_time_getlen(ngx_http_request_t *r, | |
2422 uintptr_t data) | |
2423 { | |
2424 if (r->upstream) { | |
2425 return r->upstream->states.nelts * (NGX_TIME_T_LEN + 4 + 2); | |
2426 } | |
2427 | |
2428 return 1; | |
2429 } | |
2430 | |
2431 | |
509 | 2432 static u_char * |
563 | 2433 ngx_http_upstream_log_response_time(ngx_http_request_t *r, u_char *buf, |
2434 ngx_http_log_op_t *op) | |
2435 { | |
2436 ngx_uint_t i; | |
2437 ngx_http_upstream_t *u; | |
2438 ngx_http_upstream_state_t *state; | |
2439 | |
2440 u = r->upstream; | |
2441 | |
573 | 2442 if (u == NULL || u->states.nelts == 0) { |
563 | 2443 *buf = '-'; |
2444 return buf + 1; | |
2445 } | |
2446 | |
2447 i = 0; | |
2448 state = u->states.elts; | |
2449 | |
2450 for ( ;; ) { | |
2451 if (state[i].status == 0) { | |
2452 *buf++ = '-'; | |
2453 | |
2454 } else { | |
2455 buf = ngx_sprintf(buf, "%d.%03d", | |
2456 state[i].response_time / 1000, | |
2457 state[i].response_time % 1000); | |
2458 } | |
2459 | |
2460 if (++i == u->states.nelts) { | |
2461 return buf; | |
2462 } | |
2463 | |
2464 *buf++ = ','; | |
2465 *buf++ = ' '; | |
2466 } | |
2467 } | |
2468 | |
2469 | |
487 | 2470 static ngx_int_t |
573 | 2471 ngx_http_upstream_add_variables(ngx_conf_t *cf) |
479 | 2472 { |
573 | 2473 ngx_http_variable_t *var, *v; |
479 | 2474 ngx_http_log_op_name_t *op; |
2475 | |
573 | 2476 for (v = ngx_http_upstream_vars; v->name.len; v++) { |
2477 var = ngx_http_add_variable(cf, &v->name, v->flags); | |
2478 if (var == NULL) { | |
2479 return NGX_ERROR; | |
2480 } | |
2481 | |
637 | 2482 var->get_handler = v->get_handler; |
573 | 2483 var->data = v->data; |
2484 } | |
2485 | |
479 | 2486 for (op = ngx_http_upstream_log_fmt_ops; op->name.len; op++) { /* void */ } |
2487 op->run = NULL; | |
2488 | |
2489 for (op = ngx_http_log_fmt_ops; op->run; op++) { | |
2490 if (op->name.len == 0) { | |
2491 op = (ngx_http_log_op_name_t *) op->run; | |
2492 } | |
2493 } | |
2494 | |
2495 op->run = (ngx_http_log_op_run_pt) ngx_http_upstream_log_fmt_ops; | |
2496 | |
2497 return NGX_OK; | |
2498 } | |
509 | 2499 |
2500 | |
573 | 2501 static ngx_int_t |
2502 ngx_http_upstream_status_variable(ngx_http_request_t *r, | |
2503 ngx_http_variable_value_t *v, uintptr_t data) | |
2504 { | |
2505 u_char *p; | |
2506 size_t len; | |
2507 ngx_uint_t i; | |
2508 ngx_http_upstream_t *u; | |
2509 ngx_http_upstream_state_t *state; | |
2510 | |
2511 v->valid = 1; | |
2512 v->no_cachable = 0; | |
2513 v->not_found = 0; | |
2514 | |
2515 u = r->upstream; | |
2516 | |
2517 if (u == NULL || u->states.nelts == 0) { | |
2518 v->not_found = 1; | |
2519 return NGX_OK; | |
2520 } | |
2521 | |
2522 len = u->states.nelts * (3 + 2); | |
2523 | |
2524 p = ngx_palloc(r->pool, len); | |
2525 if (p == NULL) { | |
2526 return NGX_ERROR; | |
2527 } | |
2528 | |
2529 v->data = p; | |
2530 | |
2531 i = 0; | |
2532 state = u->states.elts; | |
2533 | |
2534 for ( ;; ) { | |
2535 if (state[i].status == 0) { | |
2536 *p++ = '-'; | |
2537 | |
2538 } else { | |
2539 p = ngx_sprintf(p, "%ui", state[i].status); | |
2540 } | |
2541 | |
2542 if (++i == u->states.nelts) { | |
2543 break; | |
2544 } | |
2545 | |
2546 *p++ = ','; | |
2547 *p++ = ' '; | |
2548 } | |
2549 | |
2550 v->len = p - v->data; | |
2551 | |
2552 return NGX_OK; | |
2553 } | |
2554 | |
2555 | |
2556 static ngx_int_t | |
2557 ngx_http_upstream_response_time_variable(ngx_http_request_t *r, | |
2558 ngx_http_variable_value_t *v, uintptr_t data) | |
2559 { | |
2560 u_char *p; | |
2561 size_t len; | |
2562 ngx_uint_t i; | |
2563 ngx_http_upstream_t *u; | |
2564 ngx_http_upstream_state_t *state; | |
2565 | |
2566 v->valid = 1; | |
2567 v->no_cachable = 0; | |
2568 v->not_found = 0; | |
2569 | |
2570 u = r->upstream; | |
2571 | |
2572 if (u == NULL || u->states.nelts == 0) { | |
2573 v->not_found = 1; | |
2574 return NGX_OK; | |
2575 } | |
2576 | |
2577 len = u->states.nelts * (NGX_TIME_T_LEN + 4 + 2); | |
2578 | |
2579 p = ngx_palloc(r->pool, len); | |
2580 if (p == NULL) { | |
2581 return NGX_ERROR; | |
2582 } | |
2583 | |
2584 v->data = p; | |
2585 | |
2586 i = 0; | |
2587 state = u->states.elts; | |
2588 | |
2589 for ( ;; ) { | |
2590 if (state[i].status == 0) { | |
2591 *p++ = '-'; | |
2592 | |
2593 } else { | |
2594 p = ngx_sprintf(p, "%d.%03d", | |
2595 state[i].response_time / 1000, | |
2596 state[i].response_time % 1000); | |
2597 } | |
2598 | |
2599 if (++i == u->states.nelts) { | |
2600 break; | |
2601 } | |
2602 | |
2603 *p++ = ','; | |
2604 *p++ = ' '; | |
2605 } | |
2606 | |
2607 v->len = p - v->data; | |
2608 | |
2609 return NGX_OK; | |
2610 } | |
2611 | |
2612 | |
651 | 2613 static char * |
2614 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) | |
2615 { | |
2616 char *rv; | |
2617 void *mconf; | |
2618 ngx_str_t *value; | |
2619 ngx_url_t u; | |
2620 ngx_uint_t i, j, m, n; | |
2621 ngx_conf_t pcf; | |
2622 ngx_peers_t **peers; | |
2623 ngx_http_module_t *module; | |
2624 ngx_http_conf_ctx_t *ctx; | |
2625 ngx_http_upstream_srv_conf_t *uscf; | |
2626 | |
2627 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); | |
2628 if (ctx == NULL) { | |
2629 return NGX_CONF_ERROR; | |
2630 } | |
2631 | |
2632 ngx_memzero(&u, sizeof(ngx_url_t)); | |
2633 | |
2634 value = cf->args->elts; | |
2635 u.host = value[1]; | |
2636 | |
2637 uscf = ngx_http_upstream_add(cf, &u); | |
2638 if (uscf == NULL) { | |
2639 return NGX_CONF_ERROR; | |
2640 } | |
2641 | |
2642 /* the upstream{}'s srv_conf */ | |
2643 | |
2644 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
2645 if (ctx->srv_conf == NULL) { | |
2646 return NGX_CONF_ERROR; | |
2647 } | |
2648 | |
2649 ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf; | |
2650 | |
2651 | |
2652 /* the upstream{}'s loc_conf */ | |
2653 | |
2654 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
2655 if (ctx->loc_conf == NULL) { | |
2656 return NGX_CONF_ERROR; | |
2657 } | |
2658 | |
2659 for (m = 0; ngx_modules[m]; m++) { | |
2660 if (ngx_modules[m]->type != NGX_HTTP_MODULE) { | |
2661 continue; | |
2662 } | |
2663 | |
2664 module = ngx_modules[m]->ctx; | |
2665 | |
2666 if (module->create_loc_conf) { | |
2667 mconf = module->create_loc_conf(cf); | |
2668 if (mconf == NULL) { | |
2669 return NGX_CONF_ERROR; | |
2670 } | |
2671 | |
2672 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf; | |
2673 } | |
2674 } | |
2675 | |
2676 | |
2677 /* parse inside upstream{} */ | |
2678 | |
2679 pcf = *cf; | |
2680 cf->ctx = ctx; | |
2681 cf->cmd_type = NGX_HTTP_UPS_CONF; | |
2682 | |
2683 rv = ngx_conf_parse(cf, NULL); | |
2684 | |
2685 *cf = pcf; | |
2686 | |
2687 if (rv != NGX_CONF_OK) { | |
2688 return rv; | |
2689 } | |
2690 | |
2691 if (uscf->servers == NULL) { | |
2692 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2693 "no servers are inside upstream"); | |
2694 return NGX_CONF_ERROR; | |
2695 } | |
2696 | |
2697 peers = uscf->servers->elts; | |
2698 | |
2699 if (uscf->servers->nelts == 1) { | |
2700 uscf->peers = peers[0]; | |
2701 } | |
2702 | |
2703 n = 0; | |
2704 | |
2705 for (i = 0; i < uscf->servers->nelts; i++) { | |
2706 n += peers[i]->number; | |
2707 } | |
2708 | |
2709 uscf->peers = ngx_pcalloc(cf->pool, | |
2710 sizeof(ngx_peers_t) + sizeof(ngx_peer_t) * (n - 1)); | |
2711 if (uscf->peers == NULL) { | |
2712 return NGX_CONF_ERROR; | |
2713 } | |
2714 | |
2715 uscf->peers->number = n; | |
2716 | |
2717 n = 0; | |
2718 | |
2719 for (i = 0; i < uscf->servers->nelts; i++) { | |
2720 for (j = 0; j < peers[i]->number; j++) { | |
2721 uscf->peers->peer[n++] = peers[i]->peer[j]; | |
2722 } | |
2723 } | |
2724 | |
2725 return rv; | |
2726 } | |
2727 | |
2728 | |
2729 static char * | |
2730 ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
2731 { | |
2732 ngx_http_upstream_srv_conf_t *uscf = conf; | |
2733 | |
2734 ngx_str_t *value; | |
2735 ngx_url_t u; | |
663 | 2736 ngx_int_t weight; |
2737 ngx_uint_t i; | |
651 | 2738 ngx_peers_t **peers; |
2739 | |
2740 if (uscf->servers == NULL) { | |
2741 uscf->servers = ngx_array_create(cf->pool, 4, sizeof(ngx_peers_t *)); | |
2742 if (uscf->servers == NULL) { | |
2743 return NGX_CONF_ERROR; | |
2744 } | |
2745 } | |
2746 | |
2747 peers = ngx_array_push(uscf->servers); | |
2748 if (peers == NULL) { | |
2749 return NGX_CONF_ERROR; | |
2750 } | |
2751 | |
2752 value = cf->args->elts; | |
2753 | |
2754 ngx_memzero(&u, sizeof(ngx_url_t)); | |
2755 | |
2756 u.url = value[1]; | |
2757 u.default_portn = 80; | |
2758 | |
2759 if (ngx_parse_url(cf, &u) != NGX_OK) { | |
2760 if (u.err) { | |
2761 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2762 "%s in upstream \"%V\"", u.err, &u.url); | |
2763 } | |
2764 | |
2765 return NGX_CONF_ERROR; | |
2766 } | |
2767 | |
663 | 2768 weight = 1; |
2769 | |
2770 if (cf->args->nelts == 3) { | |
2771 | |
2772 value = &value[2]; | |
2773 | |
2774 if (ngx_strncmp(value->data, "weight=", 7) == 0) { | |
2775 | |
2776 weight = ngx_atoi(&value->data[7], value->len - 7); | |
2777 | |
2778 if (weight == NGX_ERROR || weight == 0) { | |
2779 goto invalid; | |
2780 } | |
2781 | |
2782 } else { | |
2783 goto invalid; | |
2784 } | |
2785 } | |
2786 | |
2787 for (i = 0; i < u.peers->number; i++) { | |
2788 u.peers->peer[i].weight = weight; | |
2789 u.peers->peer[i].current_weight = weight; | |
2790 u.peers->peer[i].max_fails = NGX_CONF_UNSET_UINT; | |
2791 u.peers->peer[i].fail_timeout = NGX_CONF_UNSET; | |
2792 } | |
2793 | |
651 | 2794 *peers = u.peers; |
2795 | |
2796 return NGX_CONF_OK; | |
663 | 2797 |
2798 invalid: | |
2799 | |
2800 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", value); | |
2801 | |
2802 return NGX_CONF_ERROR; | |
651 | 2803 } |
2804 | |
2805 | |
2806 ngx_http_upstream_srv_conf_t * | |
2807 ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u) | |
2808 { | |
2809 ngx_uint_t i; | |
2810 ngx_http_upstream_srv_conf_t *uscf, **uscfp; | |
2811 ngx_http_upstream_main_conf_t *umcf; | |
2812 | |
2813 if (u->upstream) { | |
2814 if (ngx_parse_url(cf, u) != NGX_OK) { | |
2815 if (u->err) { | |
2816 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2817 "%s in upstream \"%V\"", u->err, &u->url); | |
2818 } | |
2819 | |
2820 return NULL; | |
2821 } | |
2822 | |
2823 if (u->peers) { | |
2824 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t)); | |
2825 if (uscf == NULL) { | |
2826 return NULL; | |
2827 } | |
2828 | |
2829 uscf->peers = u->peers; | |
2830 | |
2831 return uscf; | |
2832 } | |
2833 } | |
2834 | |
2835 umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module); | |
2836 | |
2837 uscfp = umcf->upstreams.elts; | |
2838 | |
2839 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
2840 if (uscfp[i]->host.len != u->host.len) { | |
2841 continue; | |
2842 } | |
2843 | |
2844 if (ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len) | |
2845 == 0) | |
2846 { | |
2847 return uscfp[i]; | |
2848 } | |
2849 } | |
2850 | |
2851 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t)); | |
2852 if (uscf == NULL) { | |
2853 return NULL; | |
2854 } | |
2855 | |
2856 uscf->host = u->host; | |
2857 uscf->file_name = cf->conf_file->file.name; | |
2858 uscf->line = cf->conf_file->line; | |
2859 uscf->port = u->default_portn; | |
2860 | |
2861 uscfp = ngx_array_push(&umcf->upstreams); | |
2862 if (uscfp == NULL) { | |
2863 return NULL; | |
2864 } | |
2865 | |
2866 *uscfp = uscf; | |
2867 | |
2868 return uscf; | |
2869 } | |
2870 | |
2871 | |
509 | 2872 static void * |
2873 ngx_http_upstream_create_main_conf(ngx_conf_t *cf) | |
2874 { | |
2875 ngx_http_upstream_main_conf_t *umcf; | |
2876 | |
2877 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t)); | |
2878 if (umcf == NULL) { | |
2879 return NULL; | |
2880 } | |
2881 | |
651 | 2882 if (ngx_array_init(&umcf->upstreams, cf->pool, 4, |
2883 sizeof(ngx_http_upstream_srv_conf_t *)) | |
2884 != NGX_OK) | |
2885 { | |
2886 return NGX_CONF_ERROR; | |
2887 } | |
2888 | |
509 | 2889 return umcf; |
2890 } | |
2891 | |
2892 | |
2893 static char * | |
651 | 2894 ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf) |
509 | 2895 { |
2896 ngx_http_upstream_main_conf_t *umcf = conf; | |
2897 | |
651 | 2898 ngx_uint_t i; |
2899 ngx_array_t headers_in; | |
2900 ngx_hash_key_t *hk; | |
2901 ngx_hash_init_t hash; | |
2902 ngx_http_upstream_header_t *header; | |
2903 ngx_http_upstream_srv_conf_t **uscfp; | |
2904 | |
2905 uscfp = umcf->upstreams.elts; | |
2906 | |
2907 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
2908 if (uscfp[i]->peers) { | |
2909 continue; | |
2910 } | |
2911 | |
2912 uscfp[i]->peers = ngx_inet_resolve_peer(cf, &uscfp[i]->host, | |
663 | 2913 uscfp[i]->port); |
651 | 2914 if (uscfp[i]->peers == NULL) { |
2915 return NGX_CONF_ERROR; | |
2916 } | |
2917 | |
2918 if (uscfp[i]->peers == NGX_CONF_ERROR) { | |
2919 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
2920 "upstream host \"%V\" is not found in %s:%ui", | |
2921 &uscfp[i]->host, uscfp[i]->file_name.data, | |
2922 uscfp[i]->line); | |
2923 return NGX_CONF_ERROR; | |
2924 } | |
2925 } | |
649 | 2926 |
663 | 2927 |
649 | 2928 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t)) |
2929 != NGX_OK) | |
509 | 2930 { |
2931 return NGX_CONF_ERROR; | |
2932 } | |
2933 | |
649 | 2934 for (header = ngx_http_upstream_headers_in; header->name.len; header++) { |
2935 hk = ngx_array_push(&headers_in); | |
2936 if (hk == NULL) { | |
2937 return NGX_CONF_ERROR; | |
2938 } | |
2939 | |
2940 hk->key = header->name; | |
2941 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len); | |
2942 hk->value = header; | |
2943 } | |
2944 | |
2945 hash.hash = &umcf->headers_in_hash; | |
2946 hash.key = ngx_hash_key_lc; | |
2947 hash.max_size = 512; | |
751
bae59a740c40
align hash bucket size to cache line
Igor Sysoev <igor@sysoev.ru>
parents:
750
diff
changeset
|
2948 hash.bucket_size = ngx_align(64, ngx_cacheline_size); |
649 | 2949 hash.name = "upstream_headers_in_hash"; |
2950 hash.pool = cf->pool; | |
2951 hash.temp_pool = NULL; | |
2952 | |
2953 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) { | |
2954 return NGX_CONF_ERROR; | |
2955 } | |
509 | 2956 |
2957 return NGX_CONF_OK; | |
2958 } |