nginx 0.5.30
[nginx-catap.git] / src / http / ngx_http_upstream.c
blobd01941a867165ad2ca32cc094922fb056acb0ad4
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
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);
16 static void ngx_http_upstream_connect(ngx_http_request_t *r,
17 ngx_http_upstream_t *u);
18 static ngx_int_t ngx_http_upstream_reinit(ngx_http_request_t *r,
19 ngx_http_upstream_t *u);
20 static void ngx_http_upstream_send_request(ngx_http_request_t *r,
21 ngx_http_upstream_t *u);
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);
24 static ngx_int_t ngx_http_upstream_test_connect(ngx_connection_t *c);
25 static void ngx_http_upstream_process_body_in_memory(ngx_event_t *rev);
26 static void ngx_http_upstream_send_response(ngx_http_request_t *r,
27 ngx_http_upstream_t *u);
28 static void
29 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r);
30 static void ngx_http_upstream_process_non_buffered_body(ngx_event_t *ev);
31 static ngx_int_t ngx_http_upstream_non_buffered_filter_init(void *data);
32 static ngx_int_t ngx_http_upstream_non_buffered_filter(void *data,
33 ssize_t bytes);
34 static void ngx_http_upstream_process_downstream(ngx_http_request_t *r);
35 static void ngx_http_upstream_process_body(ngx_event_t *ev);
36 static void ngx_http_upstream_dummy_handler(ngx_event_t *wev);
37 static void ngx_http_upstream_next(ngx_http_request_t *r,
38 ngx_http_upstream_t *u, ngx_uint_t ft_type);
39 static void ngx_http_upstream_cleanup(void *data);
40 static void ngx_http_upstream_finalize_request(ngx_http_request_t *r,
41 ngx_http_upstream_t *u, ngx_int_t rc);
43 static ngx_int_t ngx_http_upstream_process_header_line(ngx_http_request_t *r,
44 ngx_table_elt_t *h, ngx_uint_t offset);
45 static ngx_int_t
46 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r,
47 ngx_table_elt_t *h, ngx_uint_t offset);
48 static ngx_int_t ngx_http_upstream_ignore_header_line(ngx_http_request_t *r,
49 ngx_table_elt_t *h, ngx_uint_t offset);
50 static ngx_int_t ngx_http_upstream_process_limit_rate(ngx_http_request_t *r,
51 ngx_table_elt_t *h, ngx_uint_t offset);
52 static ngx_int_t ngx_http_upstream_process_buffering(ngx_http_request_t *r,
53 ngx_table_elt_t *h, ngx_uint_t offset);
54 static ngx_int_t ngx_http_upstream_process_charset(ngx_http_request_t *r,
55 ngx_table_elt_t *h, ngx_uint_t offset);
56 static ngx_int_t ngx_http_upstream_copy_header_line(ngx_http_request_t *r,
57 ngx_table_elt_t *h, ngx_uint_t offset);
58 static ngx_int_t
59 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r,
60 ngx_table_elt_t *h, ngx_uint_t offset);
61 static ngx_int_t ngx_http_upstream_copy_content_type(ngx_http_request_t *r,
62 ngx_table_elt_t *h, ngx_uint_t offset);
63 static ngx_int_t ngx_http_upstream_copy_content_length(ngx_http_request_t *r,
64 ngx_table_elt_t *h, ngx_uint_t offset);
65 static ngx_int_t ngx_http_upstream_rewrite_location(ngx_http_request_t *r,
66 ngx_table_elt_t *h, ngx_uint_t offset);
67 static ngx_int_t ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r,
68 ngx_table_elt_t *h, ngx_uint_t offset);
69 #if (NGX_HTTP_GZIP)
70 static ngx_int_t ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r,
71 ngx_table_elt_t *h, ngx_uint_t offset);
72 #endif
74 static ngx_int_t ngx_http_upstream_add_variables(ngx_conf_t *cf);
75 static ngx_int_t ngx_http_upstream_addr_variable(ngx_http_request_t *r,
76 ngx_http_variable_value_t *v, uintptr_t data);
77 static ngx_int_t ngx_http_upstream_status_variable(ngx_http_request_t *r,
78 ngx_http_variable_value_t *v, uintptr_t data);
79 static ngx_int_t ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
80 ngx_http_variable_value_t *v, uintptr_t data);
82 static char *ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
83 static char *ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
84 void *conf);
86 static void *ngx_http_upstream_create_main_conf(ngx_conf_t *cf);
87 static char *ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf);
89 #if (NGX_HTTP_SSL)
90 static void ngx_http_upstream_ssl_init_connection(ngx_http_request_t *,
91 ngx_http_upstream_t *u, ngx_connection_t *c);
92 static void ngx_http_upstream_ssl_handshake(ngx_connection_t *c);
93 #endif
96 ngx_http_upstream_header_t ngx_http_upstream_headers_in[] = {
98 { ngx_string("Status"),
99 ngx_http_upstream_process_header_line,
100 offsetof(ngx_http_upstream_headers_in_t, status),
101 ngx_http_upstream_copy_header_line, 0, 0 },
103 { ngx_string("Content-Type"),
104 ngx_http_upstream_process_header_line,
105 offsetof(ngx_http_upstream_headers_in_t, content_type),
106 ngx_http_upstream_copy_content_type, 0, 1 },
108 { ngx_string("Content-Length"),
109 ngx_http_upstream_process_header_line,
110 offsetof(ngx_http_upstream_headers_in_t, content_length),
111 ngx_http_upstream_copy_content_length, 0, 0 },
113 { ngx_string("Date"),
114 ngx_http_upstream_process_header_line,
115 offsetof(ngx_http_upstream_headers_in_t, date),
116 ngx_http_upstream_copy_header_line,
117 offsetof(ngx_http_headers_out_t, date), 0 },
119 { ngx_string("Server"),
120 ngx_http_upstream_process_header_line,
121 offsetof(ngx_http_upstream_headers_in_t, server),
122 ngx_http_upstream_copy_header_line,
123 offsetof(ngx_http_headers_out_t, server), 0 },
125 { ngx_string("WWW-Authenticate"),
126 ngx_http_upstream_process_header_line,
127 offsetof(ngx_http_upstream_headers_in_t, www_authenticate),
128 ngx_http_upstream_copy_header_line, 0, 0 },
130 { ngx_string("Location"),
131 ngx_http_upstream_ignore_header_line, 0,
132 ngx_http_upstream_rewrite_location, 0, 0 },
134 { ngx_string("Refresh"),
135 ngx_http_upstream_ignore_header_line, 0,
136 ngx_http_upstream_rewrite_refresh, 0, 0 },
138 { ngx_string("Set-Cookie"),
139 ngx_http_upstream_ignore_header_line, 0,
140 ngx_http_upstream_copy_header_line, 0, 1 },
142 { ngx_string("Content-Disposition"),
143 ngx_http_upstream_ignore_header_line, 0,
144 ngx_http_upstream_copy_header_line, 0, 1 },
146 { ngx_string("Cache-Control"),
147 ngx_http_upstream_process_multi_header_lines,
148 offsetof(ngx_http_upstream_headers_in_t, cache_control),
149 ngx_http_upstream_copy_multi_header_lines,
150 offsetof(ngx_http_headers_out_t, cache_control), 1 },
152 { ngx_string("Expires"),
153 ngx_http_upstream_process_header_line,
154 offsetof(ngx_http_upstream_headers_in_t, expires),
155 ngx_http_upstream_copy_header_line,
156 offsetof(ngx_http_headers_out_t, expires), 1 },
158 { ngx_string("Accept-Ranges"),
159 ngx_http_upstream_process_header_line,
160 offsetof(ngx_http_upstream_headers_in_t, accept_ranges),
161 ngx_http_upstream_copy_header_line,
162 offsetof(ngx_http_headers_out_t, accept_ranges), 1 },
164 { ngx_string("Connection"),
165 ngx_http_upstream_ignore_header_line, 0,
166 ngx_http_upstream_ignore_header_line, 0, 0 },
168 { ngx_string("Keep-Alive"),
169 ngx_http_upstream_ignore_header_line, 0,
170 ngx_http_upstream_ignore_header_line, 0, 0 },
172 { ngx_string("X-Powered-By"),
173 ngx_http_upstream_ignore_header_line, 0,
174 ngx_http_upstream_copy_header_line, 0, 0 },
176 { ngx_string("X-Accel-Expires"),
177 ngx_http_upstream_process_header_line,
178 offsetof(ngx_http_upstream_headers_in_t, x_accel_expires),
179 ngx_http_upstream_copy_header_line, 0, 0 },
181 { ngx_string("X-Accel-Redirect"),
182 ngx_http_upstream_process_header_line,
183 offsetof(ngx_http_upstream_headers_in_t, x_accel_redirect),
184 ngx_http_upstream_ignore_header_line, 0, 0 },
186 { ngx_string("X-Accel-Limit-Rate"),
187 ngx_http_upstream_process_limit_rate, 0,
188 ngx_http_upstream_ignore_header_line, 0, 0 },
190 { ngx_string("X-Accel-Buffering"),
191 ngx_http_upstream_process_buffering, 0,
192 ngx_http_upstream_ignore_header_line, 0, 0 },
194 { ngx_string("X-Accel-Charset"),
195 ngx_http_upstream_process_charset, 0,
196 ngx_http_upstream_ignore_header_line, 0, 0 },
198 #if (NGX_HTTP_GZIP)
199 { ngx_string("Content-Encoding"),
200 ngx_http_upstream_process_header_line,
201 offsetof(ngx_http_upstream_headers_in_t, content_encoding),
202 ngx_http_upstream_copy_content_encoding, 0, 0 },
203 #endif
205 { ngx_null_string, NULL, 0, NULL, 0, 0 }
209 static ngx_command_t ngx_http_upstream_commands[] = {
211 { ngx_string("upstream"),
212 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
213 ngx_http_upstream,
216 NULL },
218 { ngx_string("server"),
219 NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
220 ngx_http_upstream_server,
221 NGX_HTTP_SRV_CONF_OFFSET,
223 NULL },
225 ngx_null_command
229 static ngx_http_module_t ngx_http_upstream_module_ctx = {
230 ngx_http_upstream_add_variables, /* preconfiguration */
231 NULL, /* postconfiguration */
233 ngx_http_upstream_create_main_conf, /* create main configuration */
234 ngx_http_upstream_init_main_conf, /* init main configuration */
236 NULL, /* create server configuration */
237 NULL, /* merge server configuration */
239 NULL, /* create location configuration */
240 NULL /* merge location configuration */
244 ngx_module_t ngx_http_upstream_module = {
245 NGX_MODULE_V1,
246 &ngx_http_upstream_module_ctx, /* module context */
247 ngx_http_upstream_commands, /* module directives */
248 NGX_HTTP_MODULE, /* module type */
249 NULL, /* init master */
250 NULL, /* init module */
251 NULL, /* init process */
252 NULL, /* init thread */
253 NULL, /* exit thread */
254 NULL, /* exit process */
255 NULL, /* exit master */
256 NGX_MODULE_V1_PADDING
260 static ngx_http_variable_t ngx_http_upstream_vars[] = {
262 { ngx_string("upstream_addr"), NULL,
263 ngx_http_upstream_addr_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
265 { ngx_string("upstream_status"), NULL,
266 ngx_http_upstream_status_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
268 { ngx_string("upstream_response_time"), NULL,
269 ngx_http_upstream_response_time_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
271 { ngx_null_string, NULL, NULL, 0, 0, 0 }
275 void
276 ngx_http_upstream_init(ngx_http_request_t *r)
278 ngx_connection_t *c;
279 ngx_http_cleanup_t *cln;
280 ngx_http_upstream_t *u;
281 ngx_http_core_loc_conf_t *clcf;
283 c = r->connection;
285 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
286 "http init upstream, client timer: %d", c->read->timer_set);
288 if (c->read->timer_set) {
289 ngx_del_timer(c->read);
292 u = r->upstream;
294 if (!r->post_action && !u->conf->ignore_client_abort) {
295 r->read_event_handler = ngx_http_upstream_rd_check_broken_connection;
296 r->write_event_handler = ngx_http_upstream_wr_check_broken_connection;
299 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
301 if (!c->write->active) {
302 if (ngx_add_event(c->write, NGX_WRITE_EVENT, NGX_CLEAR_EVENT)
303 == NGX_ERROR)
305 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
306 return;
311 if (r->request_body) {
312 u->request_bufs = r->request_body->bufs;
315 if (u->conf->upstream->peer.init(r, u->conf->upstream) != NGX_OK) {
316 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
317 return;
320 if (u->create_request(r) != NGX_OK) {
321 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
322 return;
325 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
327 u->output.sendfile = r->connection->sendfile;
328 u->output.pool = r->pool;
329 u->output.bufs.num = 1;
330 u->output.bufs.size = clcf->client_body_buffer_size;
331 u->output.output_filter = ngx_chain_writer;
332 u->output.filter_ctx = &u->writer;
334 u->writer.pool = r->pool;
336 if (r->upstream_states == NULL) {
338 r->upstream_states = ngx_array_create(r->pool, 1,
339 sizeof(ngx_http_upstream_state_t));
340 if (r->upstream_states == NULL) {
341 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
342 return;
345 } else {
347 u->state = ngx_array_push(r->upstream_states);
348 if (u->state == NULL) {
349 ngx_http_upstream_finalize_request(r, u,
350 NGX_HTTP_INTERNAL_SERVER_ERROR);
351 return;
354 ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
357 cln = ngx_http_cleanup_add(r, 0);
358 if (cln == NULL) {
359 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
360 return;
363 cln->handler = ngx_http_upstream_cleanup;
364 cln->data = r;
365 u->cleanup = &cln->handler;
367 ngx_http_upstream_connect(r, u);
371 static void
372 ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r)
374 ngx_http_upstream_check_broken_connection(r, r->connection->read);
378 static void
379 ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r)
381 ngx_http_upstream_check_broken_connection(r, r->connection->write);
385 static void
386 ngx_http_upstream_check_broken_connection(ngx_http_request_t *r,
387 ngx_event_t *ev)
389 int n;
390 char buf[1];
391 ngx_err_t err;
392 ngx_connection_t *c;
393 ngx_http_upstream_t *u;
395 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ev->log, 0,
396 "http upstream check client, write event:%d, \"%V\"",
397 ev->write, &r->uri);
399 c = r->connection;
400 u = r->upstream;
402 if (c->error) {
403 ngx_http_upstream_finalize_request(r, u,
404 NGX_HTTP_CLIENT_CLOSED_REQUEST);
405 return;
408 if (u->peer.connection == NULL) {
409 return;
412 #if (NGX_HAVE_KQUEUE)
414 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
416 if (!ev->pending_eof) {
417 return;
420 ev->eof = 1;
421 c->error = 1;
423 if (ev->kq_errno) {
424 ev->error = 1;
427 if (!u->cachable && u->peer.connection) {
428 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
429 "kevent() reported that client closed prematurely "
430 "connection, so upstream connection is closed too");
431 ngx_http_upstream_finalize_request(r, u,
432 NGX_HTTP_CLIENT_CLOSED_REQUEST);
433 return;
436 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
437 "kevent() reported that client closed "
438 "prematurely connection");
440 if (u->peer.connection == NULL) {
441 ngx_http_upstream_finalize_request(r, u,
442 NGX_HTTP_CLIENT_CLOSED_REQUEST);
443 return;
446 return;
449 #endif
451 n = recv(c->fd, buf, 1, MSG_PEEK);
453 err = ngx_socket_errno;
455 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, err,
456 "http upstream recv(): %d", n);
459 * we do not need to disable the write event because
460 * that event has NGX_USE_CLEAR_EVENT type
463 if (ev->write && (n >= 0 || err == NGX_EAGAIN)) {
464 return;
467 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) {
468 if (ngx_del_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) {
469 ngx_http_upstream_finalize_request(r, u,
470 NGX_HTTP_INTERNAL_SERVER_ERROR);
471 return;
475 if (n > 0) {
476 return;
479 if (n == -1) {
480 if (err == NGX_EAGAIN) {
481 return;
484 ev->error = 1;
486 } else { /* n == 0 */
487 err = 0;
490 ev->eof = 1;
491 c->error = 1;
493 if (!u->cachable && u->peer.connection) {
494 ngx_log_error(NGX_LOG_INFO, ev->log, err,
495 "client closed prematurely connection, "
496 "so upstream connection is closed too");
497 ngx_http_upstream_finalize_request(r, u,
498 NGX_HTTP_CLIENT_CLOSED_REQUEST);
499 return;
502 ngx_log_error(NGX_LOG_INFO, ev->log, err,
503 "client closed prematurely connection");
505 if (u->peer.connection == NULL) {
506 ngx_http_upstream_finalize_request(r, u,
507 NGX_HTTP_CLIENT_CLOSED_REQUEST);
508 return;
513 static void
514 ngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u)
516 ngx_int_t rc;
517 ngx_time_t *tp;
518 ngx_connection_t *c;
520 r->connection->log->action = "connecting to upstream";
522 r->connection->single_connection = 0;
524 if (u->state && u->state->response_sec) {
525 tp = ngx_timeofday();
526 u->state->response_sec = tp->sec - u->state->response_sec;
527 u->state->response_msec = tp->msec - u->state->response_msec;
530 u->state = ngx_array_push(r->upstream_states);
531 if (u->state == NULL) {
532 ngx_http_upstream_finalize_request(r, u,
533 NGX_HTTP_INTERNAL_SERVER_ERROR);
534 return;
537 ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
539 tp = ngx_timeofday();
540 u->state->response_sec = tp->sec;
541 u->state->response_msec = tp->msec;
543 rc = ngx_event_connect_peer(&u->peer);
545 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
546 "http upstream connect: %i", rc);
548 if (rc == NGX_ERROR) {
549 ngx_http_upstream_finalize_request(r, u,
550 NGX_HTTP_INTERNAL_SERVER_ERROR);
551 return;
554 u->state->peer = u->peer.name;
556 if (rc == NGX_BUSY) {
557 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams");
560 if (rc == NGX_BUSY || rc == NGX_DECLINED) {
561 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
562 return;
565 /* rc == NGX_OK || rc == NGX_AGAIN */
567 c = u->peer.connection;
569 c->data = r;
571 c->write->handler = ngx_http_upstream_send_request_handler;
572 c->read->handler = ngx_http_upstream_process_header;
574 c->sendfile = r->connection->sendfile;
576 c->pool = r->pool;
577 c->read->log = c->write->log = c->log = r->connection->log;
579 /* init or reinit the ngx_output_chain() and ngx_chain_writer() contexts */
581 u->writer.out = NULL;
582 u->writer.last = &u->writer.out;
583 u->writer.connection = c;
584 u->writer.limit = 0;
586 if (u->request_sent) {
587 if (ngx_http_upstream_reinit(r, u) != NGX_OK) {
588 ngx_http_upstream_finalize_request(r, u,
589 NGX_HTTP_INTERNAL_SERVER_ERROR);
590 return;
594 if (r->request_body
595 && r->request_body->buf
596 && r->request_body->temp_file
597 && r == r->main)
600 * the r->request_body->buf can be reused for one request only,
601 * the subrequests should allocate their own temporay bufs
604 u->output.free = ngx_alloc_chain_link(r->pool);
605 if (u->output.free == NULL) {
606 ngx_http_upstream_finalize_request(r, u,
607 NGX_HTTP_INTERNAL_SERVER_ERROR);
608 return;
611 u->output.free->buf = r->request_body->buf;
612 u->output.free->next = NULL;
613 u->output.allocated = 1;
615 r->request_body->buf->pos = r->request_body->buf->start;
616 r->request_body->buf->last = r->request_body->buf->start;
617 r->request_body->buf->tag = u->output.tag;
620 u->request_sent = 0;
622 if (rc == NGX_AGAIN) {
623 ngx_add_timer(c->write, u->conf->connect_timeout);
624 return;
627 #if (NGX_HTTP_SSL)
629 if (u->conf->ssl && c->ssl == NULL) {
630 ngx_http_upstream_ssl_init_connection(r, u, c);
631 return;
634 #endif
636 ngx_http_upstream_send_request(r, u);
640 #if (NGX_HTTP_SSL)
642 static void
643 ngx_http_upstream_ssl_init_connection(ngx_http_request_t *r,
644 ngx_http_upstream_t *u, ngx_connection_t *c)
646 ngx_int_t rc;
648 if (ngx_ssl_create_connection(u->conf->ssl, c,
649 NGX_SSL_BUFFER|NGX_SSL_CLIENT)
650 == NGX_ERROR)
652 ngx_http_upstream_finalize_request(r, u,
653 NGX_HTTP_INTERNAL_SERVER_ERROR);
654 return;
657 c->sendfile = 0;
658 u->output.sendfile = 0;
660 if (u->peer.set_session(&u->peer, u->peer.data) != NGX_OK) {
661 ngx_http_upstream_finalize_request(r, u,
662 NGX_HTTP_INTERNAL_SERVER_ERROR);
663 return;
666 r->connection->log->action = "SSL handshaking to upstream";
668 rc = ngx_ssl_handshake(c);
670 if (rc == NGX_AGAIN) {
671 c->ssl->handler = ngx_http_upstream_ssl_handshake;
672 return;
675 ngx_http_upstream_ssl_handshake(c);
679 static void
680 ngx_http_upstream_ssl_handshake(ngx_connection_t *c)
682 ngx_http_request_t *r;
683 ngx_http_upstream_t *u;
685 r = c->data;
686 u = r->upstream;
688 if (c->ssl->handshaked) {
690 u->peer.save_session(&u->peer, u->peer.data);
692 c->write->handler = ngx_http_upstream_send_request_handler;
693 c->read->handler = ngx_http_upstream_process_header;
695 ngx_http_upstream_send_request(r, u);
697 return;
700 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
704 #endif
707 static ngx_int_t
708 ngx_http_upstream_reinit(ngx_http_request_t *r, ngx_http_upstream_t *u)
710 ngx_chain_t *cl;
712 if (u->reinit_request(r) != NGX_OK) {
713 return NGX_ERROR;
716 ngx_memzero(&r->upstream->headers_in,
717 sizeof(ngx_http_upstream_headers_in_t));
719 if (ngx_list_init(&r->upstream->headers_in.headers, r->pool, 8,
720 sizeof(ngx_table_elt_t))
721 != NGX_OK)
723 return NGX_ERROR;
726 /* reinit the request chain */
728 for (cl = u->request_bufs; cl; cl = cl->next) {
729 cl->buf->pos = cl->buf->start;
730 cl->buf->file_pos = 0;
733 /* reinit the subrequest's ngx_output_chain() context */
735 if (r->request_body && r->request_body->temp_file
736 && r != r->main && u->output.buf)
738 u->output.free = ngx_alloc_chain_link(r->pool);
739 if (u->output.free == NULL) {
740 return NGX_ERROR;
743 u->output.free->buf = u->output.buf;
744 u->output.free->next = NULL;
746 u->output.buf->pos = u->output.buf->start;
747 u->output.buf->last = u->output.buf->start;
750 u->output.buf = NULL;
751 u->output.in = NULL;
752 u->output.busy = NULL;
754 /* reinit u->buffer */
756 #if 0
757 if (u->cache) {
758 u->buffer.pos = u->buffer.start + u->cache->ctx.header_size;
759 u->buffer.last = u->buffer.pos;
761 } else {
762 u->buffer.pos = u->buffer.start;
763 u->buffer.last = u->buffer.start;
765 #else
767 u->buffer.pos = u->buffer.start;
768 u->buffer.last = u->buffer.start;
770 #endif
772 return NGX_OK;
776 static void
777 ngx_http_upstream_send_request(ngx_http_request_t *r, ngx_http_upstream_t *u)
779 ngx_int_t rc;
780 ngx_connection_t *c;
782 c = u->peer.connection;
784 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
785 "http upstream send request");
787 if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
788 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
789 return;
792 c->log->action = "sending request to upstream";
794 rc = ngx_output_chain(&u->output, u->request_sent ? NULL : u->request_bufs);
796 u->request_sent = 1;
798 if (rc == NGX_ERROR) {
799 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
800 return;
803 if (c->write->timer_set) {
804 ngx_del_timer(c->write);
807 if (rc == NGX_AGAIN) {
808 ngx_add_timer(c->write, u->conf->send_timeout);
810 if (ngx_handle_write_event(c->write, u->conf->send_lowat) == NGX_ERROR)
812 ngx_http_upstream_finalize_request(r, u,
813 NGX_HTTP_INTERNAL_SERVER_ERROR);
814 return;
817 return;
820 /* rc == NGX_OK */
822 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
823 if (ngx_tcp_push(c->fd) == NGX_ERROR) {
824 ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno,
825 ngx_tcp_push_n " failed");
826 ngx_http_upstream_finalize_request(r, u,
827 NGX_HTTP_INTERNAL_SERVER_ERROR);
828 return;
831 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
834 ngx_add_timer(c->read, u->conf->read_timeout);
836 #if 1
837 if (c->read->ready) {
839 /* post aio operation */
842 * TODO comment
843 * although we can post aio operation just in the end
844 * of ngx_http_upstream_connect() CHECK IT !!!
845 * it's better to do here because we postpone header buffer allocation
848 ngx_http_upstream_process_header(c->read);
849 return;
851 #endif
853 c->write->handler = ngx_http_upstream_dummy_handler;
855 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
856 ngx_http_upstream_finalize_request(r, u,
857 NGX_HTTP_INTERNAL_SERVER_ERROR);
858 return;
863 static void
864 ngx_http_upstream_send_request_handler(ngx_event_t *wev)
866 ngx_connection_t *c;
867 ngx_http_request_t *r;
868 ngx_http_upstream_t *u;
870 c = wev->data;
871 r = c->data;
872 u = r->upstream;
874 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
875 "http upstream send request handler");
877 if (wev->timedout) {
878 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
879 return;
882 #if (NGX_HTTP_SSL)
884 if (u->conf->ssl && c->ssl == NULL) {
885 ngx_http_upstream_ssl_init_connection(r, u, c);
886 return;
889 #endif
891 if (u->header_sent) {
892 wev->handler = ngx_http_upstream_dummy_handler;
894 (void) ngx_handle_write_event(wev, 0);
896 return;
899 ngx_http_upstream_send_request(r, u);
903 static void
904 ngx_http_upstream_process_header(ngx_event_t *rev)
906 ssize_t n;
907 ngx_int_t rc;
908 ngx_str_t *uri, args;
909 ngx_uint_t i, flags;
910 ngx_list_part_t *part;
911 ngx_table_elt_t *h;
912 ngx_connection_t *c;
913 ngx_http_request_t *r;
914 ngx_http_upstream_t *u;
915 ngx_http_err_page_t *err_page;
916 ngx_http_core_loc_conf_t *clcf;
917 ngx_http_upstream_header_t *hh;
918 ngx_http_upstream_main_conf_t *umcf;
920 c = rev->data;
921 r = c->data;
922 u = r->upstream;
924 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
925 "http upstream process header");
927 c->log->action = "reading response header from upstream";
929 if (rev->timedout) {
930 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
931 return;
934 if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
935 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
936 return;
939 if (u->buffer.start == NULL) {
940 u->buffer.start = ngx_palloc(r->pool, u->conf->buffer_size);
941 if (u->buffer.start == NULL) {
942 ngx_http_upstream_finalize_request(r, u,
943 NGX_HTTP_INTERNAL_SERVER_ERROR);
944 return;
947 u->buffer.pos = u->buffer.start;
948 u->buffer.last = u->buffer.start;
949 u->buffer.end = u->buffer.start + u->conf->buffer_size;
950 u->buffer.temporary = 1;
952 u->buffer.tag = u->output.tag;
954 if (ngx_list_init(&r->upstream->headers_in.headers, r->pool, 8,
955 sizeof(ngx_table_elt_t))
956 != NGX_OK)
958 ngx_http_upstream_finalize_request(r, u,
959 NGX_HTTP_INTERNAL_SERVER_ERROR);
960 return;
963 #if 0
964 if (u->cache) {
965 u->buffer.pos += u->cache->ctx.header_size;
966 u->buffer.last = u->buffer.pos;
968 #endif
971 n = u->peer.connection->recv(u->peer.connection, u->buffer.last,
972 u->buffer.end - u->buffer.last);
974 if (n == NGX_AGAIN) {
975 #if 0
976 ngx_add_timer(rev, u->read_timeout);
977 #endif
979 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
980 ngx_http_upstream_finalize_request(r, u,
981 NGX_HTTP_INTERNAL_SERVER_ERROR);
982 return;
985 return;
988 if (n == 0) {
989 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
990 "upstream prematurely closed connection");
993 if (n == NGX_ERROR || n == 0) {
994 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
995 return;
998 u->buffer.last += n;
1000 #if 0
1001 u->valid_header_in = 0;
1003 u->peer.cached = 0;
1004 #endif
1006 rc = u->process_header(r);
1008 if (rc == NGX_AGAIN) {
1009 #if 0
1010 ngx_add_timer(rev, u->read_timeout);
1011 #endif
1013 if (u->buffer.pos == u->buffer.end) {
1014 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
1015 "upstream sent too big header");
1017 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);
1018 return;
1021 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
1022 ngx_http_upstream_finalize_request(r, u,
1023 NGX_HTTP_INTERNAL_SERVER_ERROR);
1024 return;
1027 return;
1030 if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) {
1031 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);
1032 return;
1035 if (rc == NGX_ERROR || rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
1036 ngx_http_upstream_finalize_request(r, u,
1037 NGX_HTTP_INTERNAL_SERVER_ERROR);
1038 return;
1041 /* rc == NGX_OK */
1043 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST
1044 && r->subrequest_in_memory)
1046 u->buffer.last = u->buffer.pos;
1049 if (u->headers_in.status_n == NGX_HTTP_INTERNAL_SERVER_ERROR) {
1051 if (u->peer.tries > 1
1052 && (u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_500))
1054 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_500);
1055 return;
1058 #if (NGX_HTTP_CACHE)
1060 if (u->peer.tries == 0
1061 && u->stale
1062 && (u->conf->use_stale & NGX_HTTP_UPSTREAM_FT_HTTP_500))
1064 ngx_http_upstream_finalize_request(r, u,
1065 ngx_http_send_cached_response(r));
1066 return;
1069 #endif
1072 if (u->headers_in.status_n == NGX_HTTP_NOT_FOUND) {
1074 if (u->peer.tries > 1
1075 && u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_404)
1077 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_404);
1078 return;
1081 if (u->conf->intercept_404) {
1082 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_NOT_FOUND);
1083 return;
1088 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST
1089 && u->conf->intercept_errors)
1091 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1093 if (clcf->error_pages) {
1095 err_page = clcf->error_pages->elts;
1096 for (i = 0; i < clcf->error_pages->nelts; i++) {
1097 if (err_page[i].status == (ngx_int_t) u->headers_in.status_n) {
1099 if (u->headers_in.status_n == NGX_HTTP_UNAUTHORIZED) {
1101 r->headers_out.www_authenticate =
1102 ngx_list_push(&r->headers_out.headers);
1104 if (r->headers_out.www_authenticate == NULL) {
1105 ngx_http_upstream_finalize_request(r, u,
1106 NGX_HTTP_INTERNAL_SERVER_ERROR);
1107 return;
1110 *r->headers_out.www_authenticate =
1111 *u->headers_in.www_authenticate;
1114 ngx_http_upstream_finalize_request(r, u,
1115 u->headers_in.status_n);
1116 return;
1122 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
1124 if (r->upstream->headers_in.x_accel_redirect) {
1126 ngx_http_upstream_finalize_request(r, u, NGX_DECLINED);
1128 part = &r->upstream->headers_in.headers.part;
1129 h = part->elts;
1131 for (i = 0; /* void */; i++) {
1133 if (i >= part->nelts) {
1134 if (part->next == NULL) {
1135 break;
1138 part = part->next;
1139 h = part->elts;
1140 i = 0;
1143 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
1144 h[i].lowcase_key, h[i].key.len);
1146 if (hh && hh->redirect) {
1147 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
1148 ngx_http_finalize_request(r,
1149 NGX_HTTP_INTERNAL_SERVER_ERROR);
1150 return;
1155 uri = &r->upstream->headers_in.x_accel_redirect->value;
1156 args.len = 0;
1157 args.data = NULL;
1158 flags = 0;
1160 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
1161 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
1162 return;
1165 if (flags & NGX_HTTP_ZERO_IN_URI) {
1166 r->zero_in_uri = 1;
1169 if (r->method != NGX_HTTP_HEAD) {
1170 r->method = NGX_HTTP_GET;
1173 ngx_http_internal_redirect(r, uri, &args);
1174 return;
1177 part = &r->upstream->headers_in.headers.part;
1178 h = part->elts;
1180 for (i = 0; /* void */; i++) {
1182 if (i >= part->nelts) {
1183 if (part->next == NULL) {
1184 break;
1187 part = part->next;
1188 h = part->elts;
1189 i = 0;
1192 if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash,
1193 h[i].lowcase_key, h[i].key.len))
1195 continue;
1198 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
1199 h[i].lowcase_key, h[i].key.len);
1201 if (hh) {
1202 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
1203 ngx_http_upstream_finalize_request(r, u,
1204 NGX_HTTP_INTERNAL_SERVER_ERROR);
1205 return;
1208 continue;
1211 if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) {
1212 ngx_http_upstream_finalize_request(r, u,
1213 NGX_HTTP_INTERNAL_SERVER_ERROR);
1214 return;
1218 if (r->headers_out.server && r->headers_out.server->value.data == NULL) {
1219 r->headers_out.server->hash = 0;
1222 if (r->headers_out.date && r->headers_out.date->value.data == NULL) {
1223 r->headers_out.date->hash = 0;
1226 r->headers_out.status = u->headers_in.status_n;
1227 r->headers_out.status_line = u->headers_in.status_line;
1229 if (r->headers_out.content_length_n != -1) {
1230 u->length = (size_t) r->headers_out.content_length_n;
1232 } else {
1233 u->length = NGX_MAX_SIZE_T_VALUE;
1236 if (!r->subrequest_in_memory) {
1237 ngx_http_upstream_send_response(r, u);
1238 return;
1241 /* subrequest content in memory */
1243 if (u->input_filter == NULL) {
1244 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
1245 u->input_filter = ngx_http_upstream_non_buffered_filter;
1246 u->input_filter_ctx = r;
1249 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
1250 ngx_http_upstream_finalize_request(r, u,
1251 NGX_HTTP_INTERNAL_SERVER_ERROR);
1252 return;
1255 if (u->buffer.last - u->buffer.pos >= (ssize_t) u->length) {
1256 if (u->input_filter(u->input_filter_ctx, 0) == NGX_ERROR) {
1257 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1258 return;
1261 ngx_http_upstream_finalize_request(r, u, 0);
1262 return;
1265 rev->handler = ngx_http_upstream_process_body_in_memory;
1267 ngx_http_upstream_process_body_in_memory(rev);
1271 static ngx_int_t
1272 ngx_http_upstream_test_connect(ngx_connection_t *c)
1274 int err;
1275 socklen_t len;
1277 #if (NGX_HAVE_KQUEUE)
1279 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
1280 if (c->write->pending_eof) {
1281 (void) ngx_connection_error(c, c->write->kq_errno,
1282 "kevent() reported that connect() failed");
1283 return NGX_ERROR;
1286 } else
1287 #endif
1289 err = 0;
1290 len = sizeof(int);
1293 * BSDs and Linux return 0 and set a pending error in err
1294 * Solaris returns -1 and sets errno
1297 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len)
1298 == -1)
1300 err = ngx_errno;
1303 if (err) {
1304 (void) ngx_connection_error(c, err, "connect() failed");
1305 return NGX_ERROR;
1309 return NGX_OK;
1313 static void
1314 ngx_http_upstream_process_body_in_memory(ngx_event_t *rev)
1316 size_t size;
1317 ssize_t n;
1318 ngx_buf_t *b;
1319 ngx_connection_t *c;
1320 ngx_http_request_t *r;
1321 ngx_http_upstream_t *u;
1323 c = rev->data;
1324 r = c->data;
1325 u = r->upstream;
1327 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1328 "http upstream process body on memory");
1330 if (rev->timedout) {
1331 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
1332 ngx_http_upstream_finalize_request(r, u, NGX_ETIMEDOUT);
1333 return;
1336 b = &u->buffer;
1338 for ( ;; ) {
1340 size = b->end - b->last;
1342 if (size == 0) {
1343 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1344 "upstream buffer is too small to read repsonse");
1345 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1346 return;
1349 n = c->recv(c, b->last, size);
1351 if (n == NGX_AGAIN) {
1352 break;
1355 if (n == 0 || n == NGX_ERROR) {
1356 ngx_http_upstream_finalize_request(r, u, n);
1357 return;
1360 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
1361 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1362 return;
1365 if (!rev->ready) {
1366 break;
1370 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
1371 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1372 return;
1375 if (rev->active) {
1376 ngx_add_timer(rev, u->conf->read_timeout);
1378 } else if (rev->timer_set) {
1379 ngx_del_timer(rev);
1384 static void
1385 ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
1387 int tcp_nodelay;
1388 ssize_t size;
1389 ngx_int_t rc;
1390 ngx_event_pipe_t *p;
1391 ngx_connection_t *c;
1392 ngx_pool_cleanup_t *cl;
1393 ngx_pool_cleanup_file_t *clf;
1394 ngx_http_core_loc_conf_t *clcf;
1396 rc = ngx_http_send_header(r);
1398 if (rc == NGX_ERROR || rc > NGX_OK || r->post_action || r->header_only) {
1399 ngx_http_upstream_finalize_request(r, u, rc);
1400 return;
1403 u->header_sent = 1;
1405 if (r->request_body && r->request_body->temp_file) {
1406 for (cl = r->pool->cleanup; cl; cl = cl->next) {
1407 if (cl->handler == ngx_pool_cleanup_file) {
1408 clf = cl->data;
1410 if (clf->fd == r->request_body->temp_file->file.fd) {
1411 cl->handler(clf);
1412 cl->handler = NULL;
1413 r->request_body->temp_file->file.fd = NGX_INVALID_FILE;
1414 break;
1420 c = r->connection;
1422 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1424 if (!u->buffering) {
1426 if (u->input_filter == NULL) {
1427 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
1428 u->input_filter = ngx_http_upstream_non_buffered_filter;
1429 u->input_filter_ctx = r;
1432 u->peer.connection->read->handler =
1433 ngx_http_upstream_process_non_buffered_body;
1434 r->write_event_handler =
1435 ngx_http_upstream_process_non_buffered_downstream;
1437 r->limit_rate = 0;
1439 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
1440 ngx_http_upstream_finalize_request(r, u, 0);
1441 return;
1444 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) {
1445 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
1447 tcp_nodelay = 1;
1449 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
1450 (const void *) &tcp_nodelay, sizeof(int)) == -1)
1452 ngx_connection_error(c, ngx_socket_errno,
1453 "setsockopt(TCP_NODELAY) failed");
1454 ngx_http_upstream_finalize_request(r, u, 0);
1455 return;
1458 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
1461 size = u->buffer.last - u->buffer.pos;
1463 if (size) {
1464 u->buffer.last = u->buffer.pos;
1466 if (u->input_filter(u->input_filter_ctx, size) == NGX_ERROR) {
1467 ngx_http_upstream_finalize_request(r, u, 0);
1468 return;
1471 ngx_http_upstream_process_non_buffered_body(c->write);
1473 } else {
1474 u->buffer.pos = u->buffer.start;
1475 u->buffer.last = u->buffer.start;
1477 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) {
1478 ngx_http_upstream_finalize_request(r, u, 0);
1479 return;
1483 return;
1486 /* TODO: preallocate event_pipe bufs, look "Content-Length" */
1488 #if 0
1490 if (u->cache && u->cache->ctx.file.fd != NGX_INVALID_FILE) {
1491 if (ngx_close_file(u->cache->ctx.file.fd) == NGX_FILE_ERROR) {
1492 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
1493 ngx_close_file_n " \"%s\" failed",
1494 u->cache->ctx.file.name.data);
1498 if (u->cachable) {
1499 header = (ngx_http_cache_header_t *) u->buffer->start;
1501 header->expires = u->cache->ctx.expires;
1502 header->last_modified = u->cache->ctx.last_modified;
1503 header->date = u->cache->ctx.date;
1504 header->length = r->headers_out.content_length_n;
1505 u->cache->ctx.length = r->headers_out.content_length_n;
1507 header->key_len = u->cache->ctx.key0.len;
1508 ngx_memcpy(&header->key, u->cache->ctx.key0.data, header->key_len);
1509 header->key[header->key_len] = LF;
1512 #endif
1514 p = u->pipe;
1516 p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter;
1517 p->output_ctx = r;
1518 p->tag = u->output.tag;
1519 p->bufs = u->conf->bufs;
1520 p->busy_size = u->conf->busy_buffers_size;
1521 p->upstream = u->peer.connection;
1522 p->downstream = c;
1523 p->pool = r->pool;
1524 p->log = c->log;
1526 p->cachable = u->cachable;
1528 p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
1529 if (p->temp_file == NULL) {
1530 ngx_http_upstream_finalize_request(r, u, 0);
1531 return;
1534 p->temp_file->file.fd = NGX_INVALID_FILE;
1535 p->temp_file->file.log = c->log;
1536 p->temp_file->path = u->conf->temp_path;
1537 p->temp_file->pool = r->pool;
1539 if (u->cachable) {
1540 p->temp_file->persistent = 1;
1541 } else {
1542 p->temp_file->log_level = NGX_LOG_WARN;
1543 p->temp_file->warn = "an upstream response is buffered "
1544 "to a temporary file";
1547 p->max_temp_file_size = u->conf->max_temp_file_size;
1548 p->temp_file_write_size = u->conf->temp_file_write_size;
1550 p->preread_bufs = ngx_alloc_chain_link(r->pool);
1551 if (p->preread_bufs == NULL) {
1552 ngx_http_upstream_finalize_request(r, u, 0);
1553 return;
1555 p->preread_bufs->buf = &u->buffer;
1556 p->preread_bufs->next = NULL;
1557 u->buffer.recycled = 1;
1559 p->preread_size = u->buffer.last - u->buffer.pos;
1561 if (u->cachable) {
1562 p->buf_to_file = ngx_calloc_buf(r->pool);
1563 if (p->buf_to_file == NULL) {
1564 ngx_http_upstream_finalize_request(r, u, 0);
1565 return;
1567 p->buf_to_file->pos = u->buffer.start;
1568 p->buf_to_file->last = u->buffer.pos;
1569 p->buf_to_file->temporary = 1;
1572 if (ngx_event_flags & NGX_USE_AIO_EVENT) {
1573 /* the posted aio operation may currupt a shadow buffer */
1574 p->single_buf = 1;
1577 /* TODO: p->free_bufs = 0 if use ngx_create_chain_of_bufs() */
1578 p->free_bufs = 1;
1581 * event_pipe would do u->buffer.last += p->preread_size
1582 * as though these bytes were read
1584 u->buffer.last = u->buffer.pos;
1586 if (u->conf->cyclic_temp_file) {
1589 * we need to disable the use of sendfile() if we use cyclic temp file
1590 * because the writing a new data may interfere with sendfile()
1591 * that uses the same kernel file pages (at least on FreeBSD)
1594 p->cyclic_temp_file = 1;
1595 c->sendfile = 0;
1597 } else {
1598 p->cyclic_temp_file = 0;
1601 p->read_timeout = u->conf->read_timeout;
1602 p->send_timeout = clcf->send_timeout;
1603 p->send_lowat = clcf->send_lowat;
1605 u->peer.connection->read->handler = ngx_http_upstream_process_body;
1606 r->write_event_handler = ngx_http_upstream_process_downstream;
1608 ngx_http_upstream_process_body(u->peer.connection->read);
1612 static void
1613 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r)
1615 ngx_http_upstream_process_non_buffered_body(r->connection->write);
1619 static void
1620 ngx_http_upstream_process_non_buffered_body(ngx_event_t *ev)
1622 size_t size;
1623 ssize_t n;
1624 ngx_buf_t *b;
1625 ngx_int_t rc;
1626 ngx_uint_t do_write;
1627 ngx_connection_t *c, *downstream, *upstream;
1628 ngx_http_request_t *r;
1629 ngx_http_upstream_t *u;
1630 ngx_http_core_loc_conf_t *clcf;
1632 c = ev->data;
1633 r = c->data;
1634 u = r->upstream;
1636 if (ev->write) {
1637 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1638 "http upstream process non buffered downstream");
1639 c->log->action = "sending to client";
1641 } else {
1642 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1643 "http upstream process non buffered upstream");
1644 c->log->action = "reading upstream";
1647 if (ev->timedout) {
1648 if (ev->write) {
1649 c->timedout = 1;
1650 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
1652 } else {
1653 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
1656 ngx_http_upstream_finalize_request(r, u, 0);
1657 return;
1660 downstream = r->connection;
1661 upstream = u->peer.connection;
1663 b = &u->buffer;
1665 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1667 do_write = ev->write || u->length == 0;
1669 for ( ;; ) {
1671 if (do_write) {
1673 if (u->out_bufs || u->busy_bufs) {
1674 rc = ngx_http_output_filter(r, u->out_bufs);
1676 if (downstream->destroyed) {
1677 return;
1680 if (rc == NGX_ERROR) {
1681 ngx_http_upstream_finalize_request(r, u, 0);
1682 return;
1685 ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs,
1686 &u->out_bufs, u->output.tag);
1689 if (u->busy_bufs == NULL) {
1691 if (u->length == 0
1692 || upstream->read->eof
1693 || upstream->read->error)
1695 ngx_http_upstream_finalize_request(r, u, 0);
1696 return;
1699 b->pos = b->start;
1700 b->last = b->start;
1704 size = b->end - b->last;
1706 if (size > u->length) {
1707 size = u->length;
1710 if (size && upstream->read->ready) {
1712 n = upstream->recv(upstream, b->last, size);
1714 if (n == NGX_AGAIN) {
1715 break;
1718 if (n > 0) {
1719 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
1720 ngx_http_upstream_finalize_request(r, u, 0);
1721 return;
1725 do_write = 1;
1727 continue;
1730 break;
1733 if (downstream->data == r) {
1734 if (ngx_handle_write_event(downstream->write, clcf->send_lowat)
1735 == NGX_ERROR)
1737 ngx_http_upstream_finalize_request(r, u, 0);
1738 return;
1742 if (downstream->write->active && !downstream->write->ready) {
1743 ngx_add_timer(downstream->write, clcf->send_timeout);
1745 } else if (downstream->write->timer_set) {
1746 ngx_del_timer(downstream->write);
1749 if (ngx_handle_read_event(upstream->read, 0) == NGX_ERROR) {
1750 ngx_http_upstream_finalize_request(r, u, 0);
1751 return;
1754 if (upstream->read->active && !upstream->read->ready) {
1755 ngx_add_timer(upstream->read, u->conf->read_timeout);
1757 } else if (upstream->read->timer_set) {
1758 ngx_del_timer(upstream->read);
1763 static ngx_int_t
1764 ngx_http_upstream_non_buffered_filter_init(void *data)
1766 return NGX_OK;
1770 static ngx_int_t
1771 ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes)
1773 ngx_http_request_t *r = data;
1775 ngx_buf_t *b;
1776 ngx_chain_t *cl, **ll;
1777 ngx_http_upstream_t *u;
1779 u = r->upstream;
1781 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
1782 ll = &cl->next;
1785 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
1786 if (cl == NULL) {
1787 return NGX_ERROR;
1790 *ll = cl;
1792 cl->buf->flush = 1;
1793 cl->buf->memory = 1;
1795 b = &u->buffer;
1797 cl->buf->pos = b->last;
1798 b->last += bytes;
1799 cl->buf->last = b->last;
1801 if (u->length == NGX_MAX_SIZE_T_VALUE) {
1802 return NGX_OK;
1805 u->length -= bytes;
1807 return NGX_OK;
1811 static void
1812 ngx_http_upstream_process_downstream(ngx_http_request_t *r)
1814 ngx_http_upstream_process_body(r->connection->write);
1818 static void
1819 ngx_http_upstream_process_body(ngx_event_t *ev)
1821 ngx_event_pipe_t *p;
1822 ngx_connection_t *c, *downstream;
1823 ngx_http_log_ctx_t *ctx;
1824 ngx_http_request_t *r;
1825 ngx_http_upstream_t *u;
1827 c = ev->data;
1828 r = c->data;
1829 u = r->upstream;
1830 downstream = r->connection;
1832 if (ev->write) {
1833 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1834 "http upstream process downstream");
1835 c->log->action = "sending to client";
1837 } else {
1838 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1839 "http upstream process upstream");
1840 c->log->action = "reading upstream";
1842 ctx = c->log->data;
1843 ctx->current_request = r;
1846 p = u->pipe;
1848 if (ev->timedout) {
1849 if (ev->write) {
1850 if (ev->delayed) {
1852 ev->timedout = 0;
1853 ev->delayed = 0;
1855 if (!ev->ready) {
1856 ngx_add_timer(ev, p->send_timeout);
1858 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR)
1860 ngx_http_upstream_finalize_request(r, u, 0);
1861 return;
1864 return;
1867 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) {
1869 if (downstream->destroyed) {
1870 return;
1873 ngx_http_upstream_finalize_request(r, u, 0);
1874 return;
1877 } else {
1878 p->downstream_error = 1;
1879 c->timedout = 1;
1880 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
1883 } else {
1884 p->upstream_error = 1;
1885 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
1888 } else {
1889 if (ev->write && ev->delayed) {
1890 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1891 "http downstream delayed");
1893 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) {
1894 return;
1897 return;
1900 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) {
1902 if (downstream->destroyed) {
1903 return;
1906 ngx_http_upstream_finalize_request(r, u, 0);
1907 return;
1911 if (u->peer.connection) {
1913 #if (NGX_HTTP_FILE_CACHE)
1915 if (p->upstream_done && u->cachable) {
1916 if (ngx_http_cache_update(r) == NGX_ERROR) {
1917 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
1918 ngx_http_upstream_finalize_request(r, u, 0);
1919 return;
1922 } else if (p->upstream_eof && u->cachable) {
1924 /* TODO: check length & update cache */
1926 if (ngx_http_cache_update(r) == NGX_ERROR) {
1927 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
1928 ngx_http_upstream_finalize_request(r, u, 0);
1929 return;
1933 #endif
1935 if (p->upstream_done || p->upstream_eof || p->upstream_error) {
1936 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
1937 "http upstream exit: %p", p->out);
1938 #if 0
1939 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
1940 #endif
1941 ngx_http_upstream_finalize_request(r, u, 0);
1942 return;
1946 if (p->downstream_error) {
1947 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1948 "http upstream downstream error");
1950 if (!u->cachable && u->peer.connection) {
1951 ngx_http_upstream_finalize_request(r, u, 0);
1957 static void
1958 ngx_http_upstream_dummy_handler(ngx_event_t *wev)
1960 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
1961 "http upstream dummy handler");
1965 static void
1966 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
1967 ngx_uint_t ft_type)
1969 ngx_uint_t status, state;
1971 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1972 "http next upstream, %xi", ft_type);
1974 #if 0
1975 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
1976 #endif
1978 if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) {
1979 state = NGX_PEER_NEXT;
1980 } else {
1981 state = NGX_PEER_FAILED;
1984 u->peer.free(&u->peer, u->peer.data, state);
1986 if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {
1987 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,
1988 "upstream timed out");
1991 if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {
1992 status = 0;
1994 } else {
1995 switch(ft_type) {
1997 case NGX_HTTP_UPSTREAM_FT_TIMEOUT:
1998 status = NGX_HTTP_GATEWAY_TIME_OUT;
1999 break;
2001 case NGX_HTTP_UPSTREAM_FT_HTTP_500:
2002 status = NGX_HTTP_INTERNAL_SERVER_ERROR;
2003 break;
2005 case NGX_HTTP_UPSTREAM_FT_HTTP_404:
2006 status = NGX_HTTP_NOT_FOUND;
2007 break;
2010 * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING
2011 * never reach here
2014 default:
2015 status = NGX_HTTP_BAD_GATEWAY;
2019 if (r->connection->error) {
2020 ngx_http_upstream_finalize_request(r, u,
2021 NGX_HTTP_CLIENT_CLOSED_REQUEST);
2022 return;
2025 if (status) {
2026 u->state->status = status;
2028 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {
2030 #if (NGX_HTTP_CACHE)
2032 if (u->stale && (u->conf->use_stale & ft_type)) {
2033 ngx_http_upstream_finalize_request(r, u,
2034 ngx_http_send_cached_response(r));
2035 return;
2038 #endif
2040 ngx_http_upstream_finalize_request(r, u, status);
2041 return;
2045 if (u->peer.connection) {
2046 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2047 "close http upstream connection: %d",
2048 u->peer.connection->fd);
2049 #if (NGX_HTTP_SSL)
2051 if (u->peer.connection->ssl) {
2052 u->peer.connection->ssl->no_wait_shutdown = 1;
2053 u->peer.connection->ssl->no_send_shutdown = 1;
2055 (void) ngx_ssl_shutdown(u->peer.connection);
2057 #endif
2059 ngx_close_connection(u->peer.connection);
2062 #if 0
2063 if (u->conf->busy_lock && !u->busy_locked) {
2064 ngx_http_upstream_busy_lock(p);
2065 return;
2067 #endif
2069 ngx_http_upstream_connect(r, u);
2073 static void
2074 ngx_http_upstream_cleanup(void *data)
2076 ngx_http_request_t *r = data;
2078 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2079 "cleanup http upstream request: \"%V\"", &r->uri);
2081 ngx_http_upstream_finalize_request(r, r->upstream, NGX_DONE);
2085 static void
2086 ngx_http_upstream_finalize_request(ngx_http_request_t *r,
2087 ngx_http_upstream_t *u, ngx_int_t rc)
2089 ngx_time_t *tp;
2091 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2092 "finalize http upstream request: %i", rc);
2094 *u->cleanup = NULL;
2096 if (u->state->response_sec) {
2097 tp = ngx_timeofday();
2098 u->state->response_sec = tp->sec - u->state->response_sec;
2099 u->state->response_msec = tp->msec - u->state->response_msec;
2102 u->finalize_request(r, rc);
2104 u->peer.free(&u->peer, u->peer.data, 0);
2106 if (u->peer.connection) {
2108 #if (NGX_HTTP_SSL)
2110 /* TODO: do not shutdown persistent connection */
2112 if (u->peer.connection->ssl) {
2115 * We send the "close notify" shutdown alert to the upstream only
2116 * and do not wait its "close notify" shutdown alert.
2117 * It is acceptable according to the TLS standard.
2120 u->peer.connection->ssl->no_wait_shutdown = 1;
2122 (void) ngx_ssl_shutdown(u->peer.connection);
2124 #endif
2126 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2127 "close http upstream connection: %d",
2128 u->peer.connection->fd);
2130 ngx_close_connection(u->peer.connection);
2133 u->peer.connection = NULL;
2135 if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
2137 rc = 0;
2140 if (u->pipe && u->pipe->temp_file) {
2141 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2142 "http upstream temp fd: %d",
2143 u->pipe->temp_file->file.fd);
2146 #if 0
2147 if (u->cache) {
2148 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2149 "http upstream cache fd: %d",
2150 u->cache->ctx.file.fd);
2152 #endif
2154 if (rc == NGX_DECLINED) {
2155 return;
2158 r->connection->log->action = "sending to client";
2160 if (rc == 0) {
2161 if (r == r->main) {
2162 if (!r->post_action) {
2163 rc = ngx_http_send_special(r, NGX_HTTP_LAST);
2166 } else {
2167 if (r->out) {
2168 rc = NGX_AGAIN;
2173 ngx_http_finalize_request(r, rc);
2177 static ngx_int_t
2178 ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
2179 ngx_uint_t offset)
2181 ngx_table_elt_t **ph;
2183 ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset);
2185 if (*ph == NULL) {
2186 *ph = h;
2189 return NGX_OK;
2193 static ngx_int_t
2194 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r,
2195 ngx_table_elt_t *h, ngx_uint_t offset)
2197 ngx_array_t *pa;
2198 ngx_table_elt_t **ph;
2200 pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset);
2202 if (pa->elts == NULL) {
2203 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
2205 return NGX_ERROR;
2209 ph = ngx_array_push(pa);
2210 if (ph == NULL) {
2211 return NGX_ERROR;
2214 *ph = h;
2216 return NGX_OK;
2220 static ngx_int_t
2221 ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
2222 ngx_uint_t offset)
2224 return NGX_OK;
2228 static ngx_int_t
2229 ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h,
2230 ngx_uint_t offset)
2232 ngx_int_t n;
2234 r->upstream->headers_in.x_accel_limit_rate = h;
2236 n = ngx_atoi(h->value.data, h->value.len);
2238 if (n != NGX_ERROR) {
2239 r->limit_rate = (size_t) n;
2242 return NGX_OK;
2246 static ngx_int_t
2247 ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h,
2248 ngx_uint_t offset)
2250 u_char c0, c1, c2;
2252 if (r->upstream->conf->change_buffering) {
2254 if (h->value.len == 2) {
2255 c0 = ngx_tolower(h->value.data[0]);
2256 c1 = ngx_tolower(h->value.data[1]);
2258 if (c0 == 'n' && c1 == 'o') {
2259 r->upstream->buffering = 0;
2262 } else if (h->value.len == 3) {
2263 c0 = ngx_tolower(h->value.data[0]);
2264 c1 = ngx_tolower(h->value.data[1]);
2265 c2 = ngx_tolower(h->value.data[2]);
2267 if (c0 == 'y' && c1 == 'e' && c2 == 's') {
2268 r->upstream->buffering = 1;
2273 return NGX_OK;
2277 static ngx_int_t
2278 ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h,
2279 ngx_uint_t offset)
2281 r->headers_out.override_charset = &h->value;
2283 return NGX_OK;
2287 static ngx_int_t
2288 ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
2289 ngx_uint_t offset)
2291 ngx_table_elt_t *ho, **ph;
2293 ho = ngx_list_push(&r->headers_out.headers);
2294 if (ho == NULL) {
2295 return NGX_ERROR;
2298 *ho = *h;
2300 if (offset) {
2301 ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset);
2302 *ph = ho;
2305 return NGX_OK;
2309 static ngx_int_t
2310 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r,
2311 ngx_table_elt_t *h, ngx_uint_t offset)
2313 ngx_array_t *pa;
2314 ngx_table_elt_t *ho, **ph;
2316 pa = (ngx_array_t *) ((char *) &r->headers_out + offset);
2318 if (pa->elts == NULL) {
2319 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
2321 return NGX_ERROR;
2325 ph = ngx_array_push(pa);
2326 if (ph == NULL) {
2327 return NGX_ERROR;
2330 ho = ngx_list_push(&r->headers_out.headers);
2331 if (ho == NULL) {
2332 return NGX_ERROR;
2335 *ho = *h;
2336 *ph = ho;
2338 return NGX_OK;
2342 static ngx_int_t
2343 ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h,
2344 ngx_uint_t offset)
2346 u_char *p, *last;
2348 r->headers_out.content_type_len = h->value.len;
2349 r->headers_out.content_type = h->value;
2351 for (p = h->value.data; *p; p++) {
2353 if (*p != ';') {
2354 continue;
2357 last = p;
2359 while (*++p == ' ') { /* void */ }
2361 if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) {
2362 continue;
2365 p += 8;
2367 r->headers_out.content_type_len = last - h->value.data;
2369 r->headers_out.charset.len = h->value.data + h->value.len - p;
2370 r->headers_out.charset.data = p;
2372 return NGX_OK;
2375 return NGX_OK;
2379 static ngx_int_t
2380 ngx_http_upstream_copy_content_length(ngx_http_request_t *r, ngx_table_elt_t *h,
2381 ngx_uint_t offset)
2383 ngx_table_elt_t *ho;
2385 ho = ngx_list_push(&r->headers_out.headers);
2386 if (ho == NULL) {
2387 return NGX_ERROR;
2390 *ho = *h;
2392 r->headers_out.content_length = ho;
2393 r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len);
2395 return NGX_OK;
2399 static ngx_int_t
2400 ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h,
2401 ngx_uint_t offset)
2403 ngx_int_t rc;
2404 ngx_table_elt_t *ho;
2406 ho = ngx_list_push(&r->headers_out.headers);
2407 if (ho == NULL) {
2408 return NGX_ERROR;
2411 *ho = *h;
2413 if (r->upstream->rewrite_redirect) {
2414 rc = r->upstream->rewrite_redirect(r, ho, 0);
2416 if (rc == NGX_DECLINED) {
2417 return NGX_OK;
2420 if (rc == NGX_OK) {
2421 r->headers_out.location = ho;
2423 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2424 "rewritten location: \"%V\"", &ho->value);
2427 return rc;
2431 * we do not set r->headers_out.location here to avoid the handling
2432 * the local redirects without a host name by ngx_http_header_filter()
2435 return NGX_OK;
2439 static ngx_int_t
2440 ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h,
2441 ngx_uint_t offset)
2443 u_char *p;
2444 ngx_int_t rc;
2445 ngx_table_elt_t *ho;
2447 ho = ngx_list_push(&r->headers_out.headers);
2448 if (ho == NULL) {
2449 return NGX_ERROR;
2452 *ho = *h;
2454 if (r->upstream->rewrite_redirect) {
2456 p = (u_char *) ngx_strstr(ho->value.data, "url=");
2458 if (p) {
2459 rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data);
2461 } else {
2462 return NGX_OK;
2465 if (rc == NGX_DECLINED) {
2466 return NGX_OK;
2469 #if (NGX_DEBUG)
2470 if (rc == NGX_OK) {
2471 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2472 "rewritten refresh: \"%V\"", &ho->value);
2474 #endif
2476 return rc;
2479 return NGX_OK;
2483 #if (NGX_HTTP_GZIP)
2485 static ngx_int_t
2486 ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r,
2487 ngx_table_elt_t *h, ngx_uint_t offset)
2489 ngx_table_elt_t *ho;
2491 ho = ngx_list_push(&r->headers_out.headers);
2492 if (ho == NULL) {
2493 return NGX_ERROR;
2496 *ho = *h;
2498 r->headers_out.content_encoding = ho;
2500 return NGX_OK;
2503 #endif
2506 static ngx_int_t
2507 ngx_http_upstream_add_variables(ngx_conf_t *cf)
2509 ngx_http_variable_t *var, *v;
2511 for (v = ngx_http_upstream_vars; v->name.len; v++) {
2512 var = ngx_http_add_variable(cf, &v->name, v->flags);
2513 if (var == NULL) {
2514 return NGX_ERROR;
2517 var->get_handler = v->get_handler;
2518 var->data = v->data;
2521 return NGX_OK;
2525 static ngx_int_t
2526 ngx_http_upstream_addr_variable(ngx_http_request_t *r,
2527 ngx_http_variable_value_t *v, uintptr_t data)
2529 u_char *p;
2530 size_t len;
2531 ngx_uint_t i;
2532 ngx_http_upstream_state_t *state;
2534 v->valid = 1;
2535 v->no_cachable = 0;
2536 v->not_found = 0;
2538 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
2539 v->not_found = 1;
2540 return NGX_OK;
2543 len = 0;
2544 state = r->upstream_states->elts;
2546 for (i = 0; i < r->upstream_states->nelts; i++) {
2547 if (state[i].peer) {
2548 len += state[i].peer->len + 2;
2550 } else {
2551 len += 3;
2555 p = ngx_palloc(r->pool, len);
2556 if (p == NULL) {
2557 return NGX_ERROR;
2560 v->data = p;
2562 i = 0;
2564 for ( ;; ) {
2565 if (state[i].peer) {
2566 p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len);
2569 if (++i == r->upstream_states->nelts) {
2570 break;
2573 if (state[i].peer) {
2574 *p++ = ',';
2575 *p++ = ' ';
2577 } else {
2578 *p++ = ' ';
2579 *p++ = ':';
2580 *p++ = ' ';
2582 if (++i == r->upstream_states->nelts) {
2583 break;
2586 continue;
2590 v->len = p - v->data;
2592 return NGX_OK;
2596 static ngx_int_t
2597 ngx_http_upstream_status_variable(ngx_http_request_t *r,
2598 ngx_http_variable_value_t *v, uintptr_t data)
2600 u_char *p;
2601 size_t len;
2602 ngx_uint_t i;
2603 ngx_http_upstream_state_t *state;
2605 v->valid = 1;
2606 v->no_cachable = 0;
2607 v->not_found = 0;
2609 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
2610 v->not_found = 1;
2611 return NGX_OK;
2614 len = r->upstream_states->nelts * (3 + 2);
2616 p = ngx_palloc(r->pool, len);
2617 if (p == NULL) {
2618 return NGX_ERROR;
2621 v->data = p;
2623 i = 0;
2624 state = r->upstream_states->elts;
2626 for ( ;; ) {
2627 if (state[i].status) {
2628 p = ngx_sprintf(p, "%ui", state[i].status);
2630 } else {
2631 *p++ = '-';
2634 if (++i == r->upstream_states->nelts) {
2635 break;
2638 if (state[i].peer) {
2639 *p++ = ',';
2640 *p++ = ' ';
2642 } else {
2643 *p++ = ' ';
2644 *p++ = ':';
2645 *p++ = ' ';
2647 if (++i == r->upstream_states->nelts) {
2648 break;
2651 continue;
2655 v->len = p - v->data;
2657 return NGX_OK;
2661 static ngx_int_t
2662 ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
2663 ngx_http_variable_value_t *v, uintptr_t data)
2665 u_char *p;
2666 size_t len;
2667 ngx_uint_t i;
2668 ngx_msec_int_t ms;
2669 ngx_http_upstream_state_t *state;
2671 v->valid = 1;
2672 v->no_cachable = 0;
2673 v->not_found = 0;
2675 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
2676 v->not_found = 1;
2677 return NGX_OK;
2680 len = r->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2);
2682 p = ngx_palloc(r->pool, len);
2683 if (p == NULL) {
2684 return NGX_ERROR;
2687 v->data = p;
2689 i = 0;
2690 state = r->upstream_states->elts;
2692 for ( ;; ) {
2693 if (state[i].status) {
2694 ms = state[i].response_sec * 1000 + state[i].response_msec;
2695 ms = (ms >= 0) ? ms : 0;
2696 p = ngx_sprintf(p, "%d.%03d", ms / 1000, ms % 1000);
2698 } else {
2699 *p++ = '-';
2702 if (++i == r->upstream_states->nelts) {
2703 break;
2706 if (state[i].peer) {
2707 *p++ = ',';
2708 *p++ = ' ';
2710 } else {
2711 *p++ = ' ';
2712 *p++ = ':';
2713 *p++ = ' ';
2715 if (++i == r->upstream_states->nelts) {
2716 break;
2719 continue;
2723 v->len = p - v->data;
2725 return NGX_OK;
2729 ngx_int_t
2730 ngx_http_upstream_header_variable(ngx_http_request_t *r,
2731 ngx_http_variable_value_t *v, uintptr_t data)
2733 if (r->upstream == NULL) {
2734 v->not_found = 1;
2735 return NGX_OK;
2738 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
2739 &r->upstream->headers_in.headers.part,
2740 sizeof("upstream_http_") - 1);
2744 static char *
2745 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
2747 char *rv;
2748 void *mconf;
2749 ngx_str_t *value;
2750 ngx_url_t u;
2751 ngx_uint_t m;
2752 ngx_conf_t pcf;
2753 ngx_http_module_t *module;
2754 ngx_http_conf_ctx_t *ctx, *http_ctx;
2755 ngx_http_upstream_srv_conf_t *uscf;
2757 ngx_memzero(&u, sizeof(ngx_url_t));
2759 value = cf->args->elts;
2760 u.host = value[1];
2761 u.no_resolve = 1;
2763 uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE
2764 |NGX_HTTP_UPSTREAM_WEIGHT
2765 |NGX_HTTP_UPSTREAM_MAX_FAILS
2766 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
2767 |NGX_HTTP_UPSTREAM_DOWN
2768 |NGX_HTTP_UPSTREAM_BACKUP);
2769 if (uscf == NULL) {
2770 return NGX_CONF_ERROR;
2774 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
2775 if (ctx == NULL) {
2776 return NGX_CONF_ERROR;
2779 http_ctx = cf->ctx;
2780 ctx->main_conf = http_ctx->main_conf;
2782 /* the upstream{}'s srv_conf */
2784 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2785 if (ctx->srv_conf == NULL) {
2786 return NGX_CONF_ERROR;
2789 ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf;
2791 uscf->srv_conf = ctx->srv_conf;
2794 /* the upstream{}'s loc_conf */
2796 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2797 if (ctx->loc_conf == NULL) {
2798 return NGX_CONF_ERROR;
2801 for (m = 0; ngx_modules[m]; m++) {
2802 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
2803 continue;
2806 module = ngx_modules[m]->ctx;
2808 if (module->create_srv_conf) {
2809 mconf = module->create_srv_conf(cf);
2810 if (mconf == NULL) {
2811 return NGX_CONF_ERROR;
2814 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
2817 if (module->create_loc_conf) {
2818 mconf = module->create_loc_conf(cf);
2819 if (mconf == NULL) {
2820 return NGX_CONF_ERROR;
2823 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf;
2828 /* parse inside upstream{} */
2830 pcf = *cf;
2831 cf->ctx = ctx;
2832 cf->cmd_type = NGX_HTTP_UPS_CONF;
2834 rv = ngx_conf_parse(cf, NULL);
2836 *cf = pcf;
2838 if (rv != NGX_CONF_OK) {
2839 return rv;
2842 if (uscf->servers == NULL) {
2843 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2844 "no servers are inside upstream");
2845 return NGX_CONF_ERROR;
2848 return rv;
2852 static char *
2853 ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2855 ngx_http_upstream_srv_conf_t *uscf = conf;
2857 time_t fail_timeout;
2858 ngx_str_t *value, s;
2859 ngx_url_t u;
2860 ngx_int_t weight, max_fails;
2861 ngx_uint_t i;
2862 ngx_http_upstream_server_t *us;
2864 if (uscf->servers == NULL) {
2865 uscf->servers = ngx_array_create(cf->pool, 4,
2866 sizeof(ngx_http_upstream_server_t));
2867 if (uscf->servers == NULL) {
2868 return NGX_CONF_ERROR;
2872 us = ngx_array_push(uscf->servers);
2873 if (us == NULL) {
2874 return NGX_CONF_ERROR;
2877 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
2879 value = cf->args->elts;
2881 ngx_memzero(&u, sizeof(ngx_url_t));
2883 u.url = value[1];
2884 u.default_port = 80;
2886 if (ngx_parse_url(cf, &u) != NGX_OK) {
2887 if (u.err) {
2888 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2889 "%s in upstream \"%V\"", u.err, &u.url);
2892 return NGX_CONF_ERROR;
2895 weight = 1;
2896 max_fails = 1;
2897 fail_timeout = 10;
2899 for (i = 2; i < cf->args->nelts; i++) {
2901 if (ngx_strncmp(value[i].data, "weight=", 7) == 0) {
2903 if (!(uscf->flags & NGX_HTTP_UPSTREAM_WEIGHT)) {
2904 goto invalid;
2907 weight = ngx_atoi(&value[i].data[7], value[i].len - 7);
2909 if (weight == NGX_ERROR || weight == 0) {
2910 goto invalid;
2913 continue;
2916 if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) {
2918 if (!(uscf->flags & NGX_HTTP_UPSTREAM_MAX_FAILS)) {
2919 goto invalid;
2922 max_fails = ngx_atoi(&value[i].data[10], value[i].len - 10);
2924 if (max_fails == NGX_ERROR) {
2925 goto invalid;
2928 continue;
2931 if (ngx_strncmp(value[i].data, "fail_timeout=", 13) == 0) {
2933 if (!(uscf->flags & NGX_HTTP_UPSTREAM_FAIL_TIMEOUT)) {
2934 goto invalid;
2937 s.len = value[i].len - 13;
2938 s.data = &value[i].data[13];
2940 fail_timeout = ngx_parse_time(&s, 1);
2942 if (fail_timeout < 0) {
2943 goto invalid;
2946 continue;
2949 if (ngx_strncmp(value[i].data, "down", 4) == 0) {
2951 if (!(uscf->flags & NGX_HTTP_UPSTREAM_DOWN)) {
2952 goto invalid;
2955 us->down = 1;
2957 continue;
2960 goto invalid;
2963 us->addrs = u.addrs;
2964 us->naddrs = u.naddrs;
2965 us->weight = weight;
2966 us->max_fails = max_fails;
2967 us->fail_timeout = fail_timeout;
2969 return NGX_CONF_OK;
2971 invalid:
2973 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2974 "invalid parameter \"%V\"", &value[i]);
2976 return NGX_CONF_ERROR;
2980 ngx_http_upstream_srv_conf_t *
2981 ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
2983 ngx_uint_t i;
2984 ngx_http_upstream_server_t *us;
2985 ngx_http_upstream_srv_conf_t *uscf, **uscfp;
2986 ngx_http_upstream_main_conf_t *umcf;
2988 if (!(flags & NGX_HTTP_UPSTREAM_CREATE)) {
2990 if (ngx_parse_url(cf, u) != NGX_OK) {
2991 if (u->err) {
2992 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2993 "%s in upstream \"%V\"", u->err, &u->url);
2996 return NULL;
3000 umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module);
3002 uscfp = umcf->upstreams.elts;
3004 for (i = 0; i < umcf->upstreams.nelts; i++) {
3006 if (uscfp[i]->host.len != u->host.len
3007 || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)
3008 != 0)
3010 continue;
3013 if ((flags & NGX_HTTP_UPSTREAM_CREATE)
3014 && (uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE))
3016 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3017 "duplicate upstream \"%V\"", &u->host);
3018 return NULL;
3021 if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) {
3022 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
3023 "upstream \"%V\" may not have port %d",
3024 &u->host, u->port);
3025 return NULL;
3028 if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) {
3029 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
3030 "upstream \"%V\" may not have port %d in %s:%ui",
3031 &u->host, uscfp[i]->port,
3032 uscfp[i]->file_name.data, uscfp[i]->line);
3033 return NULL;
3036 if (uscfp[i]->port != u->port) {
3037 continue;
3040 return uscfp[i];
3043 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t));
3044 if (uscf == NULL) {
3045 return NULL;
3048 uscf->flags = flags;
3049 uscf->host = u->host;
3050 uscf->file_name = cf->conf_file->file.name;
3051 uscf->line = cf->conf_file->line;
3052 uscf->port = u->port;
3053 uscf->default_port = u->default_port;
3055 if (u->naddrs == 1) {
3056 uscf->servers = ngx_array_create(cf->pool, 1,
3057 sizeof(ngx_http_upstream_server_t));
3058 if (uscf->servers == NULL) {
3059 return NGX_CONF_ERROR;
3062 us = ngx_array_push(uscf->servers);
3063 if (us == NULL) {
3064 return NGX_CONF_ERROR;
3067 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
3069 us->addrs = u->addrs;
3070 us->naddrs = u->naddrs;
3073 uscfp = ngx_array_push(&umcf->upstreams);
3074 if (uscfp == NULL) {
3075 return NULL;
3078 *uscfp = uscf;
3080 return uscf;
3084 static void *
3085 ngx_http_upstream_create_main_conf(ngx_conf_t *cf)
3087 ngx_http_upstream_main_conf_t *umcf;
3089 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t));
3090 if (umcf == NULL) {
3091 return NULL;
3094 if (ngx_array_init(&umcf->upstreams, cf->pool, 4,
3095 sizeof(ngx_http_upstream_srv_conf_t *))
3096 != NGX_OK)
3098 return NGX_CONF_ERROR;
3101 return umcf;
3105 static char *
3106 ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf)
3108 ngx_http_upstream_main_conf_t *umcf = conf;
3110 ngx_uint_t i;
3111 ngx_array_t headers_in;
3112 ngx_hash_key_t *hk;
3113 ngx_hash_init_t hash;
3114 ngx_http_upstream_init_pt init;
3115 ngx_http_upstream_header_t *header;
3116 ngx_http_upstream_srv_conf_t **uscfp;
3118 uscfp = umcf->upstreams.elts;
3120 for (i = 0; i < umcf->upstreams.nelts; i++) {
3122 init = uscfp[i]->peer.init_upstream ? uscfp[i]->peer.init_upstream:
3123 ngx_http_upstream_init_round_robin;
3125 if (init(cf, uscfp[i]) != NGX_OK) {
3126 return NGX_CONF_ERROR;
3131 /* upstream_headers_in_hash */
3133 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
3134 != NGX_OK)
3136 return NGX_CONF_ERROR;
3139 for (header = ngx_http_upstream_headers_in; header->name.len; header++) {
3140 hk = ngx_array_push(&headers_in);
3141 if (hk == NULL) {
3142 return NGX_CONF_ERROR;
3145 hk->key = header->name;
3146 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
3147 hk->value = header;
3150 hash.hash = &umcf->headers_in_hash;
3151 hash.key = ngx_hash_key_lc;
3152 hash.max_size = 512;
3153 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
3154 hash.name = "upstream_headers_in_hash";
3155 hash.pool = cf->pool;
3156 hash.temp_pool = NULL;
3158 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
3159 return NGX_CONF_ERROR;
3162 return NGX_CONF_OK;