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