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