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