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