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