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