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