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