Mercurial > hg > nginx-quic
annotate src/http/ngx_http_upstream.c @ 1579:aa1835ad46ba
style fix
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 17 Oct 2007 13:21:20 +0000 |
parents | 4c43e25d11ea |
children | 02a22cd5282a |
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 | |
1565 | 437 if (!u->cacheable && !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 |
1565 | 503 if (!u->cacheable && !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 | |
1558 | 983 n = c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last); |
479 | 984 |
985 if (n == NGX_AGAIN) { | |
986 #if 0 | |
987 ngx_add_timer(rev, u->read_timeout); | |
988 #endif | |
989 | |
990 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { | |
991 ngx_http_upstream_finalize_request(r, u, | |
992 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
993 return; | |
994 } | |
995 | |
996 return; | |
997 } | |
998 | |
999 if (n == 0) { | |
1000 ngx_log_error(NGX_LOG_ERR, rev->log, 0, | |
1001 "upstream prematurely closed connection"); | |
1002 } | |
1003 | |
1004 if (n == NGX_ERROR || n == 0) { | |
1005 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); | |
1006 return; | |
1007 } | |
1008 | |
581 | 1009 u->buffer.last += n; |
479 | 1010 |
1011 #if 0 | |
1012 u->valid_header_in = 0; | |
1013 | |
1014 u->peer.cached = 0; | |
1015 #endif | |
1016 | |
1017 rc = u->process_header(r); | |
1018 | |
1019 if (rc == NGX_AGAIN) { | |
1020 #if 0 | |
1021 ngx_add_timer(rev, u->read_timeout); | |
1022 #endif | |
1023 | |
581 | 1024 if (u->buffer.pos == u->buffer.end) { |
479 | 1025 ngx_log_error(NGX_LOG_ERR, rev->log, 0, |
1026 "upstream sent too big header"); | |
1027 | |
1028 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER); | |
1029 return; | |
1030 } | |
1031 | |
1032 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { | |
1033 ngx_http_upstream_finalize_request(r, u, | |
1034 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1035 return; | |
1036 } | |
1037 | |
1038 return; | |
1039 } | |
1040 | |
1041 if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) { | |
1042 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER); | |
1043 return; | |
1044 } | |
1045 | |
1046 if (rc == NGX_ERROR || rc == NGX_HTTP_INTERNAL_SERVER_ERROR) { | |
1047 ngx_http_upstream_finalize_request(r, u, | |
1048 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1049 return; | |
1050 } | |
1051 | |
1052 /* rc == NGX_OK */ | |
1053 | |
777
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1054 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1055 && r->subrequest_in_memory) |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1056 { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1057 u->buffer.last = u->buffer.pos; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1058 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1059 |
529 | 1060 if (u->headers_in.status_n == NGX_HTTP_INTERNAL_SERVER_ERROR) { |
509 | 1061 |
1062 if (u->peer.tries > 1 | |
1063 && (u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_500)) | |
1064 { | |
1065 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_500); | |
1066 return; | |
1067 } | |
1068 | |
1069 #if (NGX_HTTP_CACHE) | |
1070 | |
1071 if (u->peer.tries == 0 | |
1072 && u->stale | |
1073 && (u->conf->use_stale & NGX_HTTP_UPSTREAM_FT_HTTP_500)) | |
1074 { | |
1075 ngx_http_upstream_finalize_request(r, u, | |
529 | 1076 ngx_http_send_cached_response(r)); |
509 | 1077 return; |
1078 } | |
1079 | |
1080 #endif | |
1081 } | |
1082 | |
597 | 1083 if (u->headers_in.status_n == NGX_HTTP_NOT_FOUND) { |
1084 | |
1085 if (u->peer.tries > 1 | |
1086 && u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_404) | |
1087 { | |
1088 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_404); | |
1089 return; | |
1090 } | |
1091 | |
675 | 1092 if (u->conf->intercept_404) { |
1093 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_NOT_FOUND); | |
597 | 1094 return; |
1095 } | |
509 | 1096 } |
1097 | |
1098 | |
529 | 1099 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST |
675 | 1100 && u->conf->intercept_errors) |
487 | 1101 { |
1102 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1103 | |
1104 if (clcf->error_pages) { | |
1105 | |
1106 err_page = clcf->error_pages->elts; | |
1107 for (i = 0; i < clcf->error_pages->nelts; i++) { | |
529 | 1108 if (err_page[i].status == (ngx_int_t) u->headers_in.status_n) { |
1109 | |
1110 if (u->headers_in.status_n == NGX_HTTP_UNAUTHORIZED) { | |
1111 | |
1112 r->headers_out.www_authenticate = | |
1113 ngx_list_push(&r->headers_out.headers); | |
1114 | |
1115 if (r->headers_out.www_authenticate == NULL) { | |
1116 ngx_http_upstream_finalize_request(r, u, | |
1117 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1118 return; | |
1119 } | |
1120 | |
577 | 1121 *r->headers_out.www_authenticate = |
529 | 1122 *u->headers_in.www_authenticate; |
1123 } | |
1124 | |
487 | 1125 ngx_http_upstream_finalize_request(r, u, |
529 | 1126 u->headers_in.status_n); |
487 | 1127 return; |
1128 } | |
1129 } | |
1130 } | |
1131 } | |
1132 | |
681 | 1133 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); |
1134 | |
515 | 1135 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
|
1136 |
515 | 1137 ngx_http_upstream_finalize_request(r, u, NGX_DECLINED); |
1138 | |
1139 part = &r->upstream->headers_in.headers.part; | |
1140 h = part->elts; | |
1141 | |
1142 for (i = 0; /* void */; i++) { | |
1143 | |
1144 if (i >= part->nelts) { | |
1145 if (part->next == NULL) { | |
1146 break; | |
1147 } | |
577 | 1148 |
515 | 1149 part = part->next; |
1150 h = part->elts; | |
1151 i = 0; | |
1152 } | |
1153 | |
649 | 1154 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash, |
1155 h[i].lowcase_key, h[i].key.len); | |
1156 | |
1157 if (hh && hh->redirect) { | |
1158 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
|
1159 ngx_http_finalize_request(r, |
757
a138c4e6031f
not do ngx_http_upstream_finalize_request() twice
Igor Sysoev <igor@sysoev.ru>
parents:
756
diff
changeset
|
1160 NGX_HTTP_INTERNAL_SERVER_ERROR); |
515 | 1161 return; |
1162 } | |
1163 } | |
1164 } | |
1165 | |
573 | 1166 uri = &r->upstream->headers_in.x_accel_redirect->value; |
1167 args.len = 0; | |
1168 args.data = NULL; | |
1169 flags = 0; | |
1170 | |
1171 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
|
1172 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND); |
573 | 1173 return; |
1174 } | |
1175 | |
1176 if (flags & NGX_HTTP_ZERO_IN_URI) { | |
1177 r->zero_in_uri = 1; | |
1178 } | |
1179 | |
653 | 1180 if (r->method != NGX_HTTP_HEAD) { |
1181 r->method = NGX_HTTP_GET; | |
1182 } | |
1183 | |
573 | 1184 ngx_http_internal_redirect(r, uri, &args); |
515 | 1185 return; |
1186 } | |
1187 | |
509 | 1188 part = &r->upstream->headers_in.headers.part; |
1189 h = part->elts; | |
1190 | |
1191 for (i = 0; /* void */; i++) { | |
479 | 1192 |
509 | 1193 if (i >= part->nelts) { |
1194 if (part->next == NULL) { | |
1195 break; | |
1196 } | |
577 | 1197 |
509 | 1198 part = part->next; |
1199 h = part->elts; | |
1200 i = 0; | |
1201 } | |
1202 | |
649 | 1203 if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash, |
1204 h[i].lowcase_key, h[i].key.len)) | |
509 | 1205 { |
649 | 1206 continue; |
1207 } | |
1208 | |
1209 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash, | |
1210 h[i].lowcase_key, h[i].key.len); | |
1211 | |
1212 if (hh) { | |
1213 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) { | |
509 | 1214 ngx_http_upstream_finalize_request(r, u, |
1215 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1216 return; | |
1217 } | |
1218 | |
1219 continue; | |
1220 } | |
1221 | |
1222 if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) { | |
1223 ngx_http_upstream_finalize_request(r, u, | |
1224 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
1225 return; | |
1226 } | |
1227 } | |
1228 | |
649 | 1229 if (r->headers_out.server && r->headers_out.server->value.data == NULL) { |
1230 r->headers_out.server->hash = 0; | |
1231 } | |
1232 | |
1233 if (r->headers_out.date && r->headers_out.date->value.data == NULL) { | |
1234 r->headers_out.date->hash = 0; | |
1235 } | |
1236 | |
529 | 1237 r->headers_out.status = u->headers_in.status_n; |
1238 r->headers_out.status_line = u->headers_in.status_line; | |
1239 | |
581 | 1240 if (r->headers_out.content_length_n != -1) { |
1241 u->length = (size_t) r->headers_out.content_length_n; | |
1242 | |
1243 } else { | |
1244 u->length = NGX_MAX_SIZE_T_VALUE; | |
1245 } | |
1246 | |
777
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1247 if (!r->subrequest_in_memory) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1248 ngx_http_upstream_send_response(r, u); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1249 return; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1250 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1251 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1252 /* subrequest content in memory */ |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1253 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1254 if (u->input_filter == NULL) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1255 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1256 u->input_filter = ngx_http_upstream_non_buffered_filter; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1257 u->input_filter_ctx = r; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1258 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1259 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1260 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1261 ngx_http_upstream_finalize_request(r, u, |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1262 NGX_HTTP_INTERNAL_SERVER_ERROR); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1263 return; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1264 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1265 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1266 if (u->buffer.last - u->buffer.pos >= (ssize_t) u->length) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1267 if (u->input_filter(u->input_filter_ctx, 0) == NGX_ERROR) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1268 ngx_http_upstream_finalize_request(r, u, NGX_ERROR); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1269 return; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1270 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1271 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1272 ngx_http_upstream_finalize_request(r, u, 0); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1273 return; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1274 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1275 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1276 rev->handler = ngx_http_upstream_process_body_in_memory; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1277 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1278 ngx_http_upstream_process_body_in_memory(rev); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1279 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1280 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1281 |
1153
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1282 static ngx_int_t |
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1283 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
|
1284 { |
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1285 int err; |
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1286 socklen_t len; |
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1287 |
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1288 #if (NGX_HAVE_KQUEUE) |
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1289 |
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1290 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { |
1166 | 1291 if (c->write->pending_eof) { |
1469
0a269c464eaf
connection error should be logged with "connecting to upstream" action,
Igor Sysoev <igor@sysoev.ru>
parents:
1468
diff
changeset
|
1292 c->log->action = "connecting to upstream"; |
1166 | 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) { | |
1469
0a269c464eaf
connection error should be logged with "connecting to upstream" action,
Igor Sysoev <igor@sysoev.ru>
parents:
1468
diff
changeset
|
1316 c->log->action = "connecting to upstream"; |
1166 | 1317 (void) ngx_connection_error(c, err, "connect() failed"); |
1318 return NGX_ERROR; | |
1319 } | |
1153
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 |
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1322 return NGX_OK; |
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 |
c843f3df3b85
separate ngx_http_upstream_test_connect() and
Igor Sysoev <igor@sysoev.ru>
parents:
1143
diff
changeset
|
1325 |
777
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1326 static void |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1327 ngx_http_upstream_process_body_in_memory(ngx_event_t *rev) |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1328 { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1329 size_t size; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1330 ssize_t n; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1331 ngx_buf_t *b; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1332 ngx_connection_t *c; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1333 ngx_http_request_t *r; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1334 ngx_http_upstream_t *u; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1335 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1336 c = rev->data; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1337 r = c->data; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1338 u = r->upstream; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1339 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1340 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1341 "http upstream process body on memory"); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1342 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1343 if (rev->timedout) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1344 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1345 ngx_http_upstream_finalize_request(r, u, NGX_ETIMEDOUT); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1346 return; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1347 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1348 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1349 b = &u->buffer; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1350 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1351 for ( ;; ) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1352 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1353 size = b->end - b->last; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1354 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1355 if (size == 0) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1356 ngx_log_error(NGX_LOG_ALERT, c->log, 0, |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1357 "upstream buffer is too small to read repsonse"); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1358 ngx_http_upstream_finalize_request(r, u, NGX_ERROR); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1359 return; |
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 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1362 n = c->recv(c, b->last, size); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1363 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1364 if (n == NGX_AGAIN) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1365 break; |
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 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1368 if (n == 0 || n == NGX_ERROR) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1369 ngx_http_upstream_finalize_request(r, u, n); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1370 return; |
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 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1373 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1374 ngx_http_upstream_finalize_request(r, u, NGX_ERROR); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1375 return; |
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 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1378 if (!rev->ready) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1379 break; |
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 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1383 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1384 ngx_http_upstream_finalize_request(r, u, NGX_ERROR); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1385 return; |
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 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1388 if (rev->active) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1389 ngx_add_timer(rev, u->conf->read_timeout); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1390 |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1391 } else if (rev->timer_set) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1392 ngx_del_timer(rev); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
757
diff
changeset
|
1393 } |
681 | 1394 } |
1395 | |
1396 | |
1397 static void | |
1398 ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u) | |
1399 { | |
1400 int tcp_nodelay; | |
1401 ssize_t size; | |
1402 ngx_int_t rc; | |
1403 ngx_event_pipe_t *p; | |
1404 ngx_connection_t *c; | |
1405 ngx_pool_cleanup_t *cl; | |
1406 ngx_pool_cleanup_file_t *clf; | |
1407 ngx_http_core_loc_conf_t *clcf; | |
1408 | |
509 | 1409 rc = ngx_http_send_header(r); |
479 | 1410 |
1031
bf1785dfb75f
allow the upstream modules to send a header only
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
1411 if (rc == NGX_ERROR || rc > NGX_OK || r->post_action || r->header_only) { |
485 | 1412 ngx_http_upstream_finalize_request(r, u, rc); |
479 | 1413 return; |
1414 } | |
1415 | |
1416 u->header_sent = 1; | |
1417 | |
573 | 1418 if (r->request_body && r->request_body->temp_file) { |
563 | 1419 for (cl = r->pool->cleanup; cl; cl = cl->next) { |
1420 if (cl->handler == ngx_pool_cleanup_file) { | |
1421 clf = cl->data; | |
1422 | |
1423 if (clf->fd == r->request_body->temp_file->file.fd) { | |
1424 cl->handler(clf); | |
1425 cl->handler = NULL; | |
659 | 1426 r->request_body->temp_file->file.fd = NGX_INVALID_FILE; |
563 | 1427 break; |
1428 } | |
1429 } | |
1430 } | |
1431 } | |
1432 | |
583 | 1433 c = r->connection; |
1434 | |
581 | 1435 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
1436 | |
649 | 1437 if (!u->buffering) { |
581 | 1438 |
1439 if (u->input_filter == NULL) { | |
1440 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init; | |
1441 u->input_filter = ngx_http_upstream_non_buffered_filter; | |
1442 u->input_filter_ctx = r; | |
1443 } | |
1444 | |
1445 u->peer.connection->read->handler = | |
1446 ngx_http_upstream_process_non_buffered_body; | |
1447 r->write_event_handler = | |
1448 ngx_http_upstream_process_non_buffered_downstream; | |
1449 | |
1450 r->limit_rate = 0; | |
1451 | |
1452 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) { | |
1453 ngx_http_upstream_finalize_request(r, u, 0); | |
1454 return; | |
1455 } | |
1456 | |
583 | 1457 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { |
1458 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay"); | |
1459 | |
1460 tcp_nodelay = 1; | |
1461 | |
1462 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, | |
1463 (const void *) &tcp_nodelay, sizeof(int)) == -1) | |
1464 { | |
1465 ngx_connection_error(c, ngx_socket_errno, | |
1466 "setsockopt(TCP_NODELAY) failed"); | |
1467 ngx_http_upstream_finalize_request(r, u, 0); | |
1468 return; | |
1469 } | |
1470 | |
1471 c->tcp_nodelay = NGX_TCP_NODELAY_SET; | |
1472 } | |
1473 | |
581 | 1474 size = u->buffer.last - u->buffer.pos; |
1475 | |
1476 if (size) { | |
1477 u->buffer.last = u->buffer.pos; | |
1478 | |
1479 if (u->input_filter(u->input_filter_ctx, size) == NGX_ERROR) { | |
1480 ngx_http_upstream_finalize_request(r, u, 0); | |
1481 return; | |
1482 } | |
1483 | |
583 | 1484 ngx_http_upstream_process_non_buffered_body(c->write); |
581 | 1485 |
1486 } else { | |
1487 u->buffer.pos = u->buffer.start; | |
1488 u->buffer.last = u->buffer.start; | |
1489 | |
1490 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) { | |
1491 ngx_http_upstream_finalize_request(r, u, 0); | |
1492 return; | |
1493 } | |
1468
491e5059ea19
read EOF of header only responses in non-buffered proxying
Igor Sysoev <igor@sysoev.ru>
parents:
1397
diff
changeset
|
1494 |
491e5059ea19
read EOF of header only responses in non-buffered proxying
Igor Sysoev <igor@sysoev.ru>
parents:
1397
diff
changeset
|
1495 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
|
1496 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
|
1497 u->peer.connection->read); |
491e5059ea19
read EOF of header only responses in non-buffered proxying
Igor Sysoev <igor@sysoev.ru>
parents:
1397
diff
changeset
|
1498 } |
581 | 1499 } |
1500 | |
1501 return; | |
1502 } | |
1503 | |
479 | 1504 /* TODO: preallocate event_pipe bufs, look "Content-Length" */ |
1505 | |
1506 #if 0 | |
1507 | |
1508 if (u->cache && u->cache->ctx.file.fd != NGX_INVALID_FILE) { | |
1509 if (ngx_close_file(u->cache->ctx.file.fd) == NGX_FILE_ERROR) { | |
583 | 1510 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
479 | 1511 ngx_close_file_n " \"%s\" failed", |
1512 u->cache->ctx.file.name.data); | |
1513 } | |
1514 } | |
1515 | |
1565 | 1516 if (u->cacheable) { |
581 | 1517 header = (ngx_http_cache_header_t *) u->buffer->start; |
479 | 1518 |
1519 header->expires = u->cache->ctx.expires; | |
1520 header->last_modified = u->cache->ctx.last_modified; | |
1521 header->date = u->cache->ctx.date; | |
1522 header->length = r->headers_out.content_length_n; | |
1523 u->cache->ctx.length = r->headers_out.content_length_n; | |
1524 | |
1525 header->key_len = u->cache->ctx.key0.len; | |
1526 ngx_memcpy(&header->key, u->cache->ctx.key0.data, header->key_len); | |
1527 header->key[header->key_len] = LF; | |
1528 } | |
1529 | |
1530 #endif | |
1531 | |
581 | 1532 p = u->pipe; |
479 | 1533 |
1534 p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter; | |
1535 p->output_ctx = r; | |
1536 p->tag = u->output.tag; | |
577 | 1537 p->bufs = u->conf->bufs; |
479 | 1538 p->busy_size = u->conf->busy_buffers_size; |
1539 p->upstream = u->peer.connection; | |
583 | 1540 p->downstream = c; |
479 | 1541 p->pool = r->pool; |
583 | 1542 p->log = c->log; |
577 | 1543 |
1565 | 1544 p->cacheable = u->cacheable || u->store; |
479 | 1545 |
501 | 1546 p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)); |
1547 if (p->temp_file == NULL) { | |
479 | 1548 ngx_http_upstream_finalize_request(r, u, 0); |
1549 return; | |
1550 } | |
1551 | |
1552 p->temp_file->file.fd = NGX_INVALID_FILE; | |
583 | 1553 p->temp_file->file.log = c->log; |
479 | 1554 p->temp_file->path = u->conf->temp_path; |
1555 p->temp_file->pool = r->pool; | |
577 | 1556 |
1565 | 1557 if (u->cacheable || u->store) { |
479 | 1558 p->temp_file->persistent = 1; |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1559 |
479 | 1560 } else { |
667 | 1561 p->temp_file->log_level = NGX_LOG_WARN; |
479 | 1562 p->temp_file->warn = "an upstream response is buffered " |
1563 "to a temporary file"; | |
1564 } | |
1565 | |
1566 p->max_temp_file_size = u->conf->max_temp_file_size; | |
1567 p->temp_file_write_size = u->conf->temp_file_write_size; | |
1568 | |
501 | 1569 p->preread_bufs = ngx_alloc_chain_link(r->pool); |
1570 if (p->preread_bufs == NULL) { | |
479 | 1571 ngx_http_upstream_finalize_request(r, u, 0); |
1572 return; | |
1573 } | |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1574 |
581 | 1575 p->preread_bufs->buf = &u->buffer; |
479 | 1576 p->preread_bufs->next = NULL; |
581 | 1577 u->buffer.recycled = 1; |
1578 | |
1579 p->preread_size = u->buffer.last - u->buffer.pos; | |
479 | 1580 |
1565 | 1581 if (u->cacheable) { |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1582 |
479 | 1583 p->buf_to_file = ngx_calloc_buf(r->pool); |
1584 if (p->buf_to_file == NULL) { | |
1585 ngx_http_upstream_finalize_request(r, u, 0); | |
1586 return; | |
1587 } | |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1588 |
581 | 1589 p->buf_to_file->pos = u->buffer.start; |
1590 p->buf_to_file->last = u->buffer.pos; | |
479 | 1591 p->buf_to_file->temporary = 1; |
1592 } | |
1593 | |
1594 if (ngx_event_flags & NGX_USE_AIO_EVENT) { | |
1595 /* the posted aio operation may currupt a shadow buffer */ | |
1596 p->single_buf = 1; | |
1597 } | |
1598 | |
1599 /* TODO: p->free_bufs = 0 if use ngx_create_chain_of_bufs() */ | |
1600 p->free_bufs = 1; | |
1601 | |
1602 /* | |
581 | 1603 * event_pipe would do u->buffer.last += p->preread_size |
479 | 1604 * as though these bytes were read |
1605 */ | |
581 | 1606 u->buffer.last = u->buffer.pos; |
479 | 1607 |
1608 if (u->conf->cyclic_temp_file) { | |
1609 | |
1610 /* | |
1611 * we need to disable the use of sendfile() if we use cyclic temp file | |
1612 * because the writing a new data may interfere with sendfile() | |
1613 * that uses the same kernel file pages (at least on FreeBSD) | |
1614 */ | |
1615 | |
1616 p->cyclic_temp_file = 1; | |
583 | 1617 c->sendfile = 0; |
479 | 1618 |
1619 } else { | |
1620 p->cyclic_temp_file = 0; | |
1621 } | |
1622 | |
1623 p->read_timeout = u->conf->read_timeout; | |
1624 p->send_timeout = clcf->send_timeout; | |
1625 p->send_lowat = clcf->send_lowat; | |
1626 | |
509 | 1627 u->peer.connection->read->handler = ngx_http_upstream_process_body; |
1628 r->write_event_handler = ngx_http_upstream_process_downstream; | |
479 | 1629 |
1630 ngx_http_upstream_process_body(u->peer.connection->read); | |
1631 } | |
1632 | |
1633 | |
487 | 1634 static void |
581 | 1635 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r) |
1636 { | |
1637 ngx_http_upstream_process_non_buffered_body(r->connection->write); | |
1638 } | |
1639 | |
1640 | |
1641 static void | |
1642 ngx_http_upstream_process_non_buffered_body(ngx_event_t *ev) | |
1643 { | |
1644 size_t size; | |
1645 ssize_t n; | |
1646 ngx_buf_t *b; | |
583 | 1647 ngx_int_t rc; |
581 | 1648 ngx_uint_t do_write; |
884 | 1649 ngx_connection_t *c, *downstream, *upstream; |
581 | 1650 ngx_http_request_t *r; |
1651 ngx_http_upstream_t *u; | |
1652 ngx_http_core_loc_conf_t *clcf; | |
1653 | |
1654 c = ev->data; | |
756
d904b5cb3bba
fix building introduced by previous commit
Igor Sysoev <igor@sysoev.ru>
parents:
755
diff
changeset
|
1655 r = c->data; |
d904b5cb3bba
fix building introduced by previous commit
Igor Sysoev <igor@sysoev.ru>
parents:
755
diff
changeset
|
1656 u = r->upstream; |
581 | 1657 |
1658 if (ev->write) { | |
1659 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1660 "http upstream process non buffered downstream"); | |
1661 c->log->action = "sending to client"; | |
1662 | |
1663 } else { | |
1664 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1665 "http upstream process non buffered upstream"); | |
1666 c->log->action = "reading upstream"; | |
1667 } | |
1668 | |
1669 if (ev->timedout) { | |
1670 if (ev->write) { | |
1671 c->timedout = 1; | |
611 | 1672 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out"); |
1673 | |
581 | 1674 } else { |
611 | 1675 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
581 | 1676 } |
755
63b9dc652c3d
finalize nonbuffered request on timeout
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
1677 |
63b9dc652c3d
finalize nonbuffered request on timeout
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
1678 ngx_http_upstream_finalize_request(r, u, 0); |
63b9dc652c3d
finalize nonbuffered request on timeout
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
1679 return; |
581 | 1680 } |
1681 | |
884 | 1682 downstream = r->connection; |
1683 upstream = u->peer.connection; | |
581 | 1684 |
1685 b = &u->buffer; | |
1686 | |
1687 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1688 | |
657 | 1689 do_write = ev->write || u->length == 0; |
581 | 1690 |
1691 for ( ;; ) { | |
1692 | |
1693 if (do_write) { | |
1694 | |
1695 if (u->out_bufs || u->busy_bufs) { | |
583 | 1696 rc = ngx_http_output_filter(r, u->out_bufs); |
1697 | |
884 | 1698 if (downstream->destroyed) { |
583 | 1699 return; |
1700 } | |
1701 | |
1702 if (rc == NGX_ERROR) { | |
581 | 1703 ngx_http_upstream_finalize_request(r, u, 0); |
1704 return; | |
1705 } | |
1706 | |
1707 ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs, | |
1708 &u->out_bufs, u->output.tag); | |
1709 } | |
1710 | |
1711 if (u->busy_bufs == NULL) { | |
1712 | |
1713 if (u->length == 0 | |
884 | 1714 || upstream->read->eof |
1715 || upstream->read->error) | |
581 | 1716 { |
1717 ngx_http_upstream_finalize_request(r, u, 0); | |
1718 return; | |
1719 } | |
1720 | |
1721 b->pos = b->start; | |
1722 b->last = b->start; | |
1723 } | |
1724 } | |
1725 | |
1726 size = b->end - b->last; | |
1727 | |
1728 if (size > u->length) { | |
1729 size = u->length; | |
1730 } | |
1731 | |
884 | 1732 if (size && upstream->read->ready) { |
1733 | |
1734 n = upstream->recv(upstream, b->last, size); | |
581 | 1735 |
1736 if (n == NGX_AGAIN) { | |
1737 break; | |
1738 } | |
1739 | |
1740 if (n > 0) { | |
1741 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { | |
1742 ngx_http_upstream_finalize_request(r, u, 0); | |
1743 return; | |
1744 } | |
1745 } | |
1746 | |
1747 do_write = 1; | |
1748 | |
1749 continue; | |
1750 } | |
1751 | |
1752 break; | |
1753 } | |
1754 | |
884 | 1755 if (downstream->data == r) { |
1756 if (ngx_handle_write_event(downstream->write, clcf->send_lowat) | |
583 | 1757 == NGX_ERROR) |
1758 { | |
1759 ngx_http_upstream_finalize_request(r, u, 0); | |
1760 return; | |
1761 } | |
581 | 1762 } |
1763 | |
1063
e2a6ecc5a3ae
a non-active client connection must not closed after send timeout
Igor Sysoev <igor@sysoev.ru>
parents:
1031
diff
changeset
|
1764 if (downstream->write->active && !downstream->write->ready) { |
884 | 1765 ngx_add_timer(downstream->write, clcf->send_timeout); |
1766 | |
1767 } else if (downstream->write->timer_set) { | |
1768 ngx_del_timer(downstream->write); | |
581 | 1769 } |
1770 | |
884 | 1771 if (ngx_handle_read_event(upstream->read, 0) == NGX_ERROR) { |
581 | 1772 ngx_http_upstream_finalize_request(r, u, 0); |
1773 return; | |
1774 } | |
1775 | |
1063
e2a6ecc5a3ae
a non-active client connection must not closed after send timeout
Igor Sysoev <igor@sysoev.ru>
parents:
1031
diff
changeset
|
1776 if (upstream->read->active && !upstream->read->ready) { |
884 | 1777 ngx_add_timer(upstream->read, u->conf->read_timeout); |
1778 | |
1779 } else if (upstream->read->timer_set) { | |
1780 ngx_del_timer(upstream->read); | |
581 | 1781 } |
1782 } | |
1783 | |
1784 | |
1785 static ngx_int_t | |
1786 ngx_http_upstream_non_buffered_filter_init(void *data) | |
1787 { | |
1788 return NGX_OK; | |
1789 } | |
1790 | |
1791 | |
1792 static ngx_int_t | |
1793 ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes) | |
1794 { | |
1795 ngx_http_request_t *r = data; | |
1796 | |
1797 ngx_buf_t *b; | |
1798 ngx_chain_t *cl, **ll; | |
1799 ngx_http_upstream_t *u; | |
1800 | |
1801 u = r->upstream; | |
1802 | |
1803 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { | |
1804 ll = &cl->next; | |
1805 } | |
1806 | |
1807 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); | |
1808 if (cl == NULL) { | |
1809 return NGX_ERROR; | |
1810 } | |
1811 | |
1812 *ll = cl; | |
1813 | |
1814 cl->buf->flush = 1; | |
1815 cl->buf->memory = 1; | |
1816 | |
1817 b = &u->buffer; | |
1818 | |
1819 cl->buf->pos = b->last; | |
1820 b->last += bytes; | |
1821 cl->buf->last = b->last; | |
1822 | |
1823 if (u->length == NGX_MAX_SIZE_T_VALUE) { | |
1824 return NGX_OK; | |
1825 } | |
1826 | |
1827 u->length -= bytes; | |
1828 | |
1829 return NGX_OK; | |
1830 } | |
1831 | |
1832 | |
1833 static void | |
509 | 1834 ngx_http_upstream_process_downstream(ngx_http_request_t *r) |
1835 { | |
1836 ngx_http_upstream_process_body(r->connection->write); | |
1837 } | |
1838 | |
1839 | |
1840 static void | |
487 | 1841 ngx_http_upstream_process_body(ngx_event_t *ev) |
479 | 1842 { |
509 | 1843 ngx_event_pipe_t *p; |
583 | 1844 ngx_connection_t *c, *downstream; |
1098 | 1845 ngx_http_log_ctx_t *ctx; |
479 | 1846 ngx_http_request_t *r; |
1847 ngx_http_upstream_t *u; | |
1848 | |
1849 c = ev->data; | |
1850 r = c->data; | |
1851 u = r->upstream; | |
583 | 1852 downstream = r->connection; |
479 | 1853 |
1854 if (ev->write) { | |
483 | 1855 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1856 "http upstream process downstream"); | |
1857 c->log->action = "sending to client"; | |
577 | 1858 |
479 | 1859 } else { |
483 | 1860 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1861 "http upstream process upstream"); | |
1862 c->log->action = "reading upstream"; | |
1098 | 1863 |
1864 ctx = c->log->data; | |
1865 ctx->current_request = r; | |
479 | 1866 } |
577 | 1867 |
581 | 1868 p = u->pipe; |
479 | 1869 |
1870 if (ev->timedout) { | |
1871 if (ev->write) { | |
527 | 1872 if (ev->delayed) { |
1873 | |
1874 ev->timedout = 0; | |
1875 ev->delayed = 0; | |
1876 | |
1877 if (!ev->ready) { | |
1878 ngx_add_timer(ev, p->send_timeout); | |
1879 | |
1880 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) | |
1881 { | |
1882 ngx_http_upstream_finalize_request(r, u, 0); | |
1883 return; | |
1884 } | |
1885 | |
1886 return; | |
1887 } | |
1888 | |
1889 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) { | |
583 | 1890 |
1891 if (downstream->destroyed) { | |
1892 return; | |
1893 } | |
1894 | |
527 | 1895 ngx_http_upstream_finalize_request(r, u, 0); |
1896 return; | |
1897 } | |
1898 | |
1899 } else { | |
1900 p->downstream_error = 1; | |
577 | 1901 c->timedout = 1; |
611 | 1902 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out"); |
527 | 1903 } |
479 | 1904 |
1905 } else { | |
577 | 1906 p->upstream_error = 1; |
611 | 1907 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
479 | 1908 } |
1909 | |
1910 } else { | |
527 | 1911 if (ev->write && ev->delayed) { |
1912 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
1913 "http downstream delayed"); | |
1914 | |
1915 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) { | |
1916 return; | |
1917 } | |
1918 | |
1919 return; | |
1920 } | |
1921 | |
479 | 1922 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) { |
583 | 1923 |
1924 if (downstream->destroyed) { | |
1925 return; | |
1926 } | |
1927 | |
479 | 1928 ngx_http_upstream_finalize_request(r, u, 0); |
1929 return; | |
1930 } | |
1931 } | |
577 | 1932 |
479 | 1933 if (u->peer.connection) { |
1934 | |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1935 if (u->store) { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1936 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1937 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
|
1938 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1939 ngx_http_upstream_store(r, u); |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1940 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1941 } else if ((p->upstream_error |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1942 || (p->upstream_eof |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1943 && u->headers_in.status_n != NGX_HTTP_OK)) |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1944 && u->pipe->temp_file->file.fd != NGX_INVALID_FILE) |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1945 { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1946 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
|
1947 == NGX_FILE_ERROR) |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1948 { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1949 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
|
1950 ngx_delete_file_n " \"%s\" failed", |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1951 u->pipe->temp_file->file.name.data); |
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 } |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
1955 |
479 | 1956 #if (NGX_HTTP_FILE_CACHE) |
1957 | |
1565 | 1958 if (p->upstream_done && u->cacheable) { |
479 | 1959 if (ngx_http_cache_update(r) == NGX_ERROR) { |
1960 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1961 ngx_http_upstream_finalize_request(r, u, 0); | |
1962 return; | |
1963 } | |
1964 | |
1565 | 1965 } else if (p->upstream_eof && u->cacheable) { |
479 | 1966 |
1967 /* TODO: check length & update cache */ | |
1968 | |
1969 if (ngx_http_cache_update(r) == NGX_ERROR) { | |
1970 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1971 ngx_http_upstream_finalize_request(r, u, 0); | |
1972 return; | |
1973 } | |
1974 } | |
1975 | |
1976 #endif | |
1977 | |
1978 if (p->upstream_done || p->upstream_eof || p->upstream_error) { | |
483 | 1979 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1980 "http upstream exit: %p", p->out); | |
479 | 1981 #if 0 |
1982 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
1983 #endif | |
1984 ngx_http_upstream_finalize_request(r, u, 0); | |
1985 return; | |
1986 } | |
1987 } | |
1988 | |
1989 if (p->downstream_error) { | |
483 | 1990 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
1991 "http upstream downstream error"); | |
479 | 1992 |
1565 | 1993 if (!u->cacheable && u->peer.connection) { |
479 | 1994 ngx_http_upstream_finalize_request(r, u, 0); |
1995 } | |
1996 } | |
1997 } | |
1998 | |
1999 | |
487 | 2000 static void |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2001 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
|
2002 { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2003 char *failed; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2004 u_char *name; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2005 size_t root; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2006 time_t lm; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2007 ngx_err_t err; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2008 ngx_str_t *temp, path, *last_modified; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2009 ngx_temp_file_t *tf; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2010 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2011 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
|
2012 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2013 /* create file for empty 200 response */ |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2014 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2015 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
|
2016 if (tf == NULL) { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2017 return; |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2020 tf->file.fd = NGX_INVALID_FILE; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2021 tf->file.log = r->connection->log; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2022 tf->path = u->conf->temp_path; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2023 tf->pool = r->pool; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2024 tf->persistent = 1; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2025 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2026 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
|
2027 tf->persistent, tf->clean, tf->access) |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2028 != NGX_OK) |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2029 { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2030 return; |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2033 u->pipe->temp_file = tf; |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2036 temp = &u->pipe->temp_file->file.name; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2037 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2038 #if !(NGX_WIN32) |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2039 |
1295
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2040 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
|
2041 == NGX_FILE_ERROR) |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2042 { |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2043 err = ngx_errno; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2044 failed = ngx_change_file_access_n; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2045 name = temp->data; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2046 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2047 goto failed; |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2050 #endif |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2051 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2052 if (r->upstream->headers_in.last_modified) { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2053 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2054 last_modified = &r->upstream->headers_in.last_modified->value; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2055 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2056 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
|
2057 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2058 if (lm != NGX_ERROR) { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2059 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
|
2060 != NGX_OK) |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2061 { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2062 err = ngx_errno; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2063 failed = ngx_set_file_time_n; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2064 name = temp->data; |
1295
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2065 |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2066 goto failed; |
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 } |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2070 |
1295
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2071 if (u->conf->store_lengths == NULL) { |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2072 |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2073 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
|
2074 |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2075 } else { |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2076 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
|
2077 u->conf->store_values->elts) |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2078 == NULL) |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2079 { |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2080 return; |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2081 } |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2082 } |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2083 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2084 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
|
2085 "upstream stores \"%s\" to \"%s\"", temp->data, path.data); |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2086 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2087 failed = ngx_rename_file_n; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2088 name = path.data; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2089 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2090 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
|
2091 return; |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2094 err = ngx_errno; |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2095 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2096 if (err == NGX_ENOENT) { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2097 |
1295
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2098 err = ngx_create_full_path(path.data, |
5383f5232364
proxy_store and fastcgi_store were changed,
Igor Sysoev <igor@sysoev.ru>
parents:
1291
diff
changeset
|
2099 ngx_dir_access(u->conf->store_access)); |
1291
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2100 if (err == 0) { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2101 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
|
2102 return; |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2105 err = ngx_errno; |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2109 #if (NGX_WIN32) |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2110 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2111 if (err == NGX_EEXIST) { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2112 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
|
2113 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2114 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
|
2115 return; |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2119 err = ngx_errno; |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2122 #endif |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2123 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2124 failed: |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2125 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2126 if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) { |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2127 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
|
2128 ngx_delete_file_n " \"%s\" failed", |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2129 temp->data); |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2132 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
|
2133 "%s \"%s\" failed", failed, name); |
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 |
13a4ce6e7f1e
proxy_store and fastcgi_store
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
2137 static void |
487 | 2138 ngx_http_upstream_dummy_handler(ngx_event_t *wev) |
479 | 2139 { |
2140 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, | |
2141 "http upstream dummy handler"); | |
2142 } | |
2143 | |
2144 | |
487 | 2145 static void |
2146 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u, | |
2147 ngx_uint_t ft_type) | |
479 | 2148 { |
884 | 2149 ngx_uint_t status, state; |
479 | 2150 |
2151 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
884 | 2152 "http next upstream, %xi", ft_type); |
479 | 2153 |
2154 #if 0 | |
2155 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); | |
2156 #endif | |
2157 | |
525 | 2158 if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) { |
884 | 2159 state = NGX_PEER_NEXT; |
525 | 2160 } else { |
884 | 2161 state = NGX_PEER_FAILED; |
479 | 2162 } |
525 | 2163 |
1378 | 2164 if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) { |
2165 u->peer.free(&u->peer, u->peer.data, state); | |
2166 } | |
525 | 2167 |
479 | 2168 if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) { |
2169 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT, | |
2170 "upstream timed out"); | |
2171 } | |
577 | 2172 |
479 | 2173 if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) { |
2174 status = 0; | |
2175 | |
2176 } else { | |
2177 switch(ft_type) { | |
2178 | |
2179 case NGX_HTTP_UPSTREAM_FT_TIMEOUT: | |
2180 status = NGX_HTTP_GATEWAY_TIME_OUT; | |
2181 break; | |
2182 | |
2183 case NGX_HTTP_UPSTREAM_FT_HTTP_500: | |
2184 status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
2185 break; | |
2186 | |
2187 case NGX_HTTP_UPSTREAM_FT_HTTP_404: | |
2188 status = NGX_HTTP_NOT_FOUND; | |
2189 break; | |
2190 | |
2191 /* | |
2192 * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING | |
2193 * never reach here | |
2194 */ | |
2195 | |
2196 default: | |
2197 status = NGX_HTTP_BAD_GATEWAY; | |
2198 } | |
2199 } | |
2200 | |
583 | 2201 if (r->connection->error) { |
479 | 2202 ngx_http_upstream_finalize_request(r, u, |
2203 NGX_HTTP_CLIENT_CLOSED_REQUEST); | |
2204 return; | |
2205 } | |
2206 | |
2207 if (status) { | |
2208 u->state->status = status; | |
2209 | |
525 | 2210 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) { |
479 | 2211 |
2212 #if (NGX_HTTP_CACHE) | |
2213 | |
2214 if (u->stale && (u->conf->use_stale & ft_type)) { | |
2215 ngx_http_upstream_finalize_request(r, u, | |
525 | 2216 ngx_http_send_cached_response(r)); |
479 | 2217 return; |
2218 } | |
2219 | |
2220 #endif | |
2221 | |
2222 ngx_http_upstream_finalize_request(r, u, status); | |
2223 return; | |
2224 } | |
2225 } | |
2226 | |
2227 if (u->peer.connection) { | |
2228 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2229 "close http upstream connection: %d", | |
2230 u->peer.connection->fd); | |
577 | 2231 #if (NGX_HTTP_SSL) |
884 | 2232 |
577 | 2233 if (u->peer.connection->ssl) { |
884 | 2234 u->peer.connection->ssl->no_wait_shutdown = 1; |
2235 u->peer.connection->ssl->no_send_shutdown = 1; | |
2236 | |
2237 (void) ngx_ssl_shutdown(u->peer.connection); | |
577 | 2238 } |
2239 #endif | |
884 | 2240 |
479 | 2241 ngx_close_connection(u->peer.connection); |
2242 } | |
2243 | |
2244 #if 0 | |
2245 if (u->conf->busy_lock && !u->busy_locked) { | |
2246 ngx_http_upstream_busy_lock(p); | |
2247 return; | |
2248 } | |
2249 #endif | |
2250 | |
2251 ngx_http_upstream_connect(r, u); | |
2252 } | |
2253 | |
2254 | |
487 | 2255 static void |
569 | 2256 ngx_http_upstream_cleanup(void *data) |
2257 { | |
2258 ngx_http_request_t *r = data; | |
2259 | |
2260 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2261 "cleanup http upstream request: \"%V\"", &r->uri); | |
2262 | |
2263 ngx_http_upstream_finalize_request(r, r->upstream, NGX_DONE); | |
2264 } | |
2265 | |
2266 | |
2267 static void | |
487 | 2268 ngx_http_upstream_finalize_request(ngx_http_request_t *r, |
2269 ngx_http_upstream_t *u, ngx_int_t rc) | |
479 | 2270 { |
889
a9a7e4b1a72b
the previous fix does not actually fix overflow
Igor Sysoev <igor@sysoev.ru>
parents:
888
diff
changeset
|
2271 ngx_time_t *tp; |
563 | 2272 |
483 | 2273 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
2274 "finalize http upstream request: %i", rc); | |
479 | 2275 |
569 | 2276 *u->cleanup = NULL; |
2277 | |
889
a9a7e4b1a72b
the previous fix does not actually fix overflow
Igor Sysoev <igor@sysoev.ru>
parents:
888
diff
changeset
|
2278 if (u->state->response_sec) { |
563 | 2279 tp = ngx_timeofday(); |
889
a9a7e4b1a72b
the previous fix does not actually fix overflow
Igor Sysoev <igor@sysoev.ru>
parents:
888
diff
changeset
|
2280 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
|
2281 u->state->response_msec = tp->msec - u->state->response_msec; |
563 | 2282 } |
2283 | |
479 | 2284 u->finalize_request(r, rc); |
2285 | |
884 | 2286 u->peer.free(&u->peer, u->peer.data, 0); |
2287 | |
479 | 2288 if (u->peer.connection) { |
884 | 2289 |
577 | 2290 #if (NGX_HTTP_SSL) |
2291 | |
2292 /* TODO: do not shutdown persistent connection */ | |
2293 | |
2294 if (u->peer.connection->ssl) { | |
884 | 2295 |
2296 /* | |
2297 * We send the "close notify" shutdown alert to the upstream only | |
2298 * and do not wait its "close notify" shutdown alert. | |
2299 * It is acceptable according to the TLS standard. | |
2300 */ | |
2301 | |
2302 u->peer.connection->ssl->no_wait_shutdown = 1; | |
2303 | |
2304 (void) ngx_ssl_shutdown(u->peer.connection); | |
577 | 2305 } |
2306 #endif | |
884 | 2307 |
2308 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2309 "close http upstream connection: %d", | |
2310 u->peer.connection->fd); | |
2311 | |
479 | 2312 ngx_close_connection(u->peer.connection); |
2313 } | |
2314 | |
483 | 2315 u->peer.connection = NULL; |
2316 | |
563 | 2317 if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE)) |
479 | 2318 { |
2319 rc = 0; | |
2320 } | |
2321 | |
581 | 2322 if (u->pipe && u->pipe->temp_file) { |
479 | 2323 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
2324 "http upstream temp fd: %d", | |
581 | 2325 u->pipe->temp_file->file.fd); |
479 | 2326 } |
2327 | |
2328 #if 0 | |
2329 if (u->cache) { | |
2330 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
483 | 2331 "http upstream cache fd: %d", |
479 | 2332 u->cache->ctx.file.fd); |
2333 } | |
2334 #endif | |
2335 | |
515 | 2336 if (rc == NGX_DECLINED) { |
2337 return; | |
2338 } | |
2339 | |
483 | 2340 r->connection->log->action = "sending to client"; |
2341 | |
1259
05b5236c2ff5
if subrequest response was buffered in file, then subrequest was finalized
Igor Sysoev <igor@sysoev.ru>
parents:
1245
diff
changeset
|
2342 if (rc == 0) { |
05b5236c2ff5
if subrequest response was buffered in file, then subrequest was finalized
Igor Sysoev <igor@sysoev.ru>
parents:
1245
diff
changeset
|
2343 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
|
2344 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
|
2345 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
|
2346 } |
05b5236c2ff5
if subrequest response was buffered in file, then subrequest was finalized
Igor Sysoev <igor@sysoev.ru>
parents:
1245
diff
changeset
|
2347 |
05b5236c2ff5
if subrequest response was buffered in file, then subrequest was finalized
Igor Sysoev <igor@sysoev.ru>
parents:
1245
diff
changeset
|
2348 } else { |
05b5236c2ff5
if subrequest response was buffered in file, then subrequest was finalized
Igor Sysoev <igor@sysoev.ru>
parents:
1245
diff
changeset
|
2349 if (r->out) { |
05b5236c2ff5
if subrequest response was buffered in file, then subrequest was finalized
Igor Sysoev <igor@sysoev.ru>
parents:
1245
diff
changeset
|
2350 rc = NGX_AGAIN; |
05b5236c2ff5
if subrequest response was buffered in file, then subrequest was finalized
Igor Sysoev <igor@sysoev.ru>
parents:
1245
diff
changeset
|
2351 } |
05b5236c2ff5
if subrequest response was buffered in file, then subrequest was finalized
Igor Sysoev <igor@sysoev.ru>
parents:
1245
diff
changeset
|
2352 } |
479 | 2353 } |
2354 | |
2355 ngx_http_finalize_request(r, rc); | |
2356 } | |
2357 | |
2358 | |
509 | 2359 static ngx_int_t |
2360 ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2361 ngx_uint_t offset) | |
577 | 2362 { |
509 | 2363 ngx_table_elt_t **ph; |
577 | 2364 |
509 | 2365 ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset); |
2366 | |
2367 if (*ph == NULL) { | |
2368 *ph = h; | |
2369 } | |
2370 | |
2371 return NGX_OK; | |
2372 } | |
2373 | |
2374 | |
2375 static ngx_int_t | |
2376 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r, | |
2377 ngx_table_elt_t *h, ngx_uint_t offset) | |
2378 { | |
2379 ngx_array_t *pa; | |
2380 ngx_table_elt_t **ph; | |
2381 | |
2382 pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset); | |
2383 | |
2384 if (pa->elts == NULL) { | |
2385 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) | |
2386 { | |
2387 return NGX_ERROR; | |
2388 } | |
2389 } | |
2390 | |
2391 ph = ngx_array_push(pa); | |
2392 if (ph == NULL) { | |
2393 return NGX_ERROR; | |
2394 } | |
2395 | |
2396 *ph = h; | |
2397 | |
2398 return NGX_OK; | |
2399 } | |
2400 | |
2401 | |
2402 static ngx_int_t | |
2403 ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2404 ngx_uint_t offset) | |
2405 { | |
2406 return NGX_OK; | |
2407 } | |
2408 | |
2409 | |
2410 static ngx_int_t | |
527 | 2411 ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h, |
2412 ngx_uint_t offset) | |
577 | 2413 { |
527 | 2414 ngx_int_t n; |
2415 | |
2416 r->upstream->headers_in.x_accel_limit_rate = h; | |
2417 | |
2418 n = ngx_atoi(h->value.data, h->value.len); | |
2419 | |
2420 if (n != NGX_ERROR) { | |
2421 r->limit_rate = (size_t) n; | |
2422 } | |
2423 | |
2424 return NGX_OK; | |
2425 } | |
2426 | |
2427 | |
2428 static ngx_int_t | |
649 | 2429 ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h, |
2430 ngx_uint_t offset) | |
2431 { | |
2432 u_char c0, c1, c2; | |
2433 | |
2434 if (r->upstream->conf->change_buffering) { | |
2435 | |
2436 if (h->value.len == 2) { | |
2437 c0 = ngx_tolower(h->value.data[0]); | |
2438 c1 = ngx_tolower(h->value.data[1]); | |
2439 | |
2440 if (c0 == 'n' && c1 == 'o') { | |
2441 r->upstream->buffering = 0; | |
2442 } | |
2443 | |
2444 } else if (h->value.len == 3) { | |
2445 c0 = ngx_tolower(h->value.data[0]); | |
2446 c1 = ngx_tolower(h->value.data[1]); | |
2447 c2 = ngx_tolower(h->value.data[2]); | |
2448 | |
2449 if (c0 == 'y' && c1 == 'e' && c2 == 's') { | |
2450 r->upstream->buffering = 1; | |
2451 } | |
2452 } | |
2453 } | |
2454 | |
2455 return NGX_OK; | |
2456 } | |
2457 | |
2458 | |
2459 static ngx_int_t | |
657 | 2460 ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h, |
2461 ngx_uint_t offset) | |
2462 { | |
2463 r->headers_out.override_charset = &h->value; | |
2464 | |
2465 return NGX_OK; | |
2466 } | |
2467 | |
2468 | |
2469 static ngx_int_t | |
509 | 2470 ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, |
2471 ngx_uint_t offset) | |
2472 { | |
573 | 2473 ngx_table_elt_t *ho, **ph; |
509 | 2474 |
2475 ho = ngx_list_push(&r->headers_out.headers); | |
2476 if (ho == NULL) { | |
2477 return NGX_ERROR; | |
2478 } | |
2479 | |
2480 *ho = *h; | |
2481 | |
573 | 2482 if (offset) { |
2483 ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset); | |
2484 *ph = ho; | |
2485 } | |
2486 | |
509 | 2487 return NGX_OK; |
2488 } | |
2489 | |
2490 | |
2491 static ngx_int_t | |
2492 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r, | |
2493 ngx_table_elt_t *h, ngx_uint_t offset) | |
2494 { | |
2495 ngx_array_t *pa; | |
2496 ngx_table_elt_t *ho, **ph; | |
2497 | |
2498 pa = (ngx_array_t *) ((char *) &r->headers_out + offset); | |
2499 | |
2500 if (pa->elts == NULL) { | |
2501 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) | |
2502 { | |
2503 return NGX_ERROR; | |
2504 } | |
2505 } | |
2506 | |
2507 ph = ngx_array_push(pa); | |
2508 if (ph == NULL) { | |
2509 return NGX_ERROR; | |
2510 } | |
2511 | |
2512 ho = ngx_list_push(&r->headers_out.headers); | |
2513 if (ho == NULL) { | |
2514 return NGX_ERROR; | |
2515 } | |
2516 | |
2517 *ho = *h; | |
2518 *ph = ho; | |
2519 | |
2520 return NGX_OK; | |
2521 } | |
2522 | |
2523 | |
2524 static ngx_int_t | |
2525 ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2526 ngx_uint_t offset) | |
2527 { | |
657 | 2528 u_char *p, *last; |
2529 | |
2530 r->headers_out.content_type_len = h->value.len; | |
509 | 2531 r->headers_out.content_type = h->value; |
2532 | |
657 | 2533 for (p = h->value.data; *p; p++) { |
2534 | |
2535 if (*p != ';') { | |
2536 continue; | |
2537 } | |
2538 | |
2539 last = p; | |
2540 | |
2541 while (*++p == ' ') { /* void */ } | |
2542 | |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
1098
diff
changeset
|
2543 if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) { |
657 | 2544 continue; |
2545 } | |
2546 | |
2547 p += 8; | |
2548 | |
2549 r->headers_out.content_type_len = last - h->value.data; | |
2550 | |
2551 r->headers_out.charset.len = h->value.data + h->value.len - p; | |
2552 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
|
2553 |
c6c42497106c
fix segfault if upstream sends trailing ";" in "charset="
Igor Sysoev <igor@sysoev.ru>
parents:
1109
diff
changeset
|
2554 return NGX_OK; |
657 | 2555 } |
2556 | |
509 | 2557 return NGX_OK; |
2558 } | |
2559 | |
2560 | |
2561 static ngx_int_t | |
2562 ngx_http_upstream_copy_content_length(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2563 ngx_uint_t offset) | |
2564 { | |
2565 ngx_table_elt_t *ho; | |
2566 | |
2567 ho = ngx_list_push(&r->headers_out.headers); | |
2568 if (ho == NULL) { | |
2569 return NGX_ERROR; | |
2570 } | |
2571 | |
2572 *ho = *h; | |
2573 | |
2574 r->headers_out.content_length = ho; | |
2575 r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len); | |
2576 | |
2577 return NGX_OK; | |
2578 } | |
2579 | |
2580 | |
2581 static ngx_int_t | |
2582 ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2583 ngx_uint_t offset) | |
2584 { | |
2585 ngx_int_t rc; | |
2586 ngx_table_elt_t *ho; | |
2587 | |
2588 ho = ngx_list_push(&r->headers_out.headers); | |
2589 if (ho == NULL) { | |
2590 return NGX_ERROR; | |
2591 } | |
2592 | |
2593 *ho = *h; | |
2594 | |
2595 if (r->upstream->rewrite_redirect) { | |
2596 rc = r->upstream->rewrite_redirect(r, ho, 0); | |
2597 | |
529 | 2598 if (rc == NGX_DECLINED) { |
2599 return NGX_OK; | |
2600 } | |
2601 | |
509 | 2602 if (rc == NGX_OK) { |
2603 r->headers_out.location = ho; | |
2604 | |
2605 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2606 "rewritten location: \"%V\"", &ho->value); | |
2607 } | |
2608 | |
2609 return rc; | |
2610 } | |
2611 | |
2612 /* | |
2613 * we do not set r->headers_out.location here to avoid the handling | |
2614 * the local redirects without a host name by ngx_http_header_filter() | |
2615 */ | |
2616 | |
2617 return NGX_OK; | |
2618 } | |
2619 | |
2620 | |
2621 static ngx_int_t | |
2622 ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h, | |
2623 ngx_uint_t offset) | |
2624 { | |
2625 u_char *p; | |
2626 ngx_int_t rc; | |
2627 ngx_table_elt_t *ho; | |
2628 | |
2629 ho = ngx_list_push(&r->headers_out.headers); | |
2630 if (ho == NULL) { | |
2631 return NGX_ERROR; | |
2632 } | |
2633 | |
2634 *ho = *h; | |
2635 | |
2636 if (r->upstream->rewrite_redirect) { | |
2637 | |
1549 | 2638 p = ngx_strcasestrn(ho->value.data, "url=", 4 - 1); |
509 | 2639 |
2640 if (p) { | |
2641 rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data); | |
2642 | |
2643 } else { | |
2644 return NGX_OK; | |
2645 } | |
2646 | |
529 | 2647 if (rc == NGX_DECLINED) { |
2648 return NGX_OK; | |
2649 } | |
2650 | |
509 | 2651 #if (NGX_DEBUG) |
2652 if (rc == NGX_OK) { | |
2653 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2654 "rewritten refresh: \"%V\"", &ho->value); | |
2655 } | |
2656 #endif | |
2657 | |
2658 return rc; | |
2659 } | |
2660 | |
2661 return NGX_OK; | |
2662 } | |
2663 | |
2664 | |
2665 #if (NGX_HTTP_GZIP) | |
2666 | |
2667 static ngx_int_t | |
2668 ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r, | |
2669 ngx_table_elt_t *h, ngx_uint_t offset) | |
2670 { | |
2671 ngx_table_elt_t *ho; | |
2672 | |
2673 ho = ngx_list_push(&r->headers_out.headers); | |
2674 if (ho == NULL) { | |
2675 return NGX_ERROR; | |
2676 } | |
2677 | |
2678 *ho = *h; | |
2679 | |
2680 r->headers_out.content_encoding = ho; | |
2681 | |
2682 return NGX_OK; | |
2683 } | |
2684 | |
2685 #endif | |
2686 | |
2687 | |
487 | 2688 static ngx_int_t |
573 | 2689 ngx_http_upstream_add_variables(ngx_conf_t *cf) |
479 | 2690 { |
880 | 2691 ngx_http_variable_t *var, *v; |
479 | 2692 |
573 | 2693 for (v = ngx_http_upstream_vars; v->name.len; v++) { |
2694 var = ngx_http_add_variable(cf, &v->name, v->flags); | |
2695 if (var == NULL) { | |
2696 return NGX_ERROR; | |
2697 } | |
2698 | |
637 | 2699 var->get_handler = v->get_handler; |
573 | 2700 var->data = v->data; |
2701 } | |
2702 | |
479 | 2703 return NGX_OK; |
2704 } | |
509 | 2705 |
2706 | |
573 | 2707 static ngx_int_t |
1181 | 2708 ngx_http_upstream_addr_variable(ngx_http_request_t *r, |
2709 ngx_http_variable_value_t *v, uintptr_t data) | |
2710 { | |
2711 u_char *p; | |
2712 size_t len; | |
2713 ngx_uint_t i; | |
2714 ngx_http_upstream_state_t *state; | |
2715 | |
2716 v->valid = 1; | |
1565 | 2717 v->no_cacheable = 0; |
1181 | 2718 v->not_found = 0; |
2719 | |
2720 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) { | |
2721 v->not_found = 1; | |
2722 return NGX_OK; | |
2723 } | |
2724 | |
2725 len = 0; | |
2726 state = r->upstream_states->elts; | |
2727 | |
2728 for (i = 0; i < r->upstream_states->nelts; i++) { | |
2729 if (state[i].peer) { | |
2730 len += state[i].peer->len + 2; | |
2731 | |
2732 } else { | |
2733 len += 3; | |
2734 } | |
2735 } | |
2736 | |
2737 p = ngx_palloc(r->pool, len); | |
2738 if (p == NULL) { | |
2739 return NGX_ERROR; | |
2740 } | |
2741 | |
2742 v->data = p; | |
2743 | |
2744 i = 0; | |
2745 | |
2746 for ( ;; ) { | |
2747 if (state[i].peer) { | |
2748 p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len); | |
2749 } | |
2750 | |
2751 if (++i == r->upstream_states->nelts) { | |
2752 break; | |
2753 } | |
2754 | |
2755 if (state[i].peer) { | |
2756 *p++ = ','; | |
2757 *p++ = ' '; | |
2758 | |
2759 } else { | |
2760 *p++ = ' '; | |
2761 *p++ = ':'; | |
2762 *p++ = ' '; | |
2763 | |
2764 if (++i == r->upstream_states->nelts) { | |
2765 break; | |
2766 } | |
2767 | |
2768 continue; | |
2769 } | |
2770 } | |
2771 | |
2772 v->len = p - v->data; | |
2773 | |
2774 return NGX_OK; | |
2775 } | |
2776 | |
2777 | |
2778 static ngx_int_t | |
573 | 2779 ngx_http_upstream_status_variable(ngx_http_request_t *r, |
2780 ngx_http_variable_value_t *v, uintptr_t data) | |
2781 { | |
2782 u_char *p; | |
2783 size_t len; | |
2784 ngx_uint_t i; | |
2785 ngx_http_upstream_state_t *state; | |
2786 | |
2787 v->valid = 1; | |
1565 | 2788 v->no_cacheable = 0; |
573 | 2789 v->not_found = 0; |
2790 | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2791 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) { |
573 | 2792 v->not_found = 1; |
2793 return NGX_OK; | |
2794 } | |
2795 | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2796 len = r->upstream_states->nelts * (3 + 2); |
573 | 2797 |
2798 p = ngx_palloc(r->pool, len); | |
2799 if (p == NULL) { | |
2800 return NGX_ERROR; | |
2801 } | |
2802 | |
2803 v->data = p; | |
2804 | |
2805 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
|
2806 state = r->upstream_states->elts; |
573 | 2807 |
2808 for ( ;; ) { | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2809 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
|
2810 p = ngx_sprintf(p, "%ui", state[i].status); |
573 | 2811 |
2812 } else { | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2813 *p++ = '-'; |
573 | 2814 } |
2815 | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2816 if (++i == r->upstream_states->nelts) { |
573 | 2817 break; |
2818 } | |
2819 | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2820 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
|
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 *p++ = ' '; |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2823 |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2824 } else { |
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 *p++ = ' '; |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2828 |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2829 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
|
2830 break; |
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 |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2833 continue; |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2834 } |
573 | 2835 } |
2836 | |
2837 v->len = p - v->data; | |
2838 | |
2839 return NGX_OK; | |
2840 } | |
2841 | |
2842 | |
2843 static ngx_int_t | |
2844 ngx_http_upstream_response_time_variable(ngx_http_request_t *r, | |
2845 ngx_http_variable_value_t *v, uintptr_t data) | |
2846 { | |
2847 u_char *p; | |
2848 size_t len; | |
2849 ngx_uint_t i; | |
889
a9a7e4b1a72b
the previous fix does not actually fix overflow
Igor Sysoev <igor@sysoev.ru>
parents:
888
diff
changeset
|
2850 ngx_msec_int_t ms; |
573 | 2851 ngx_http_upstream_state_t *state; |
2852 | |
2853 v->valid = 1; | |
1565 | 2854 v->no_cacheable = 0; |
573 | 2855 v->not_found = 0; |
2856 | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2857 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) { |
573 | 2858 v->not_found = 1; |
2859 return NGX_OK; | |
2860 } | |
2861 | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2862 len = r->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2); |
573 | 2863 |
2864 p = ngx_palloc(r->pool, len); | |
2865 if (p == NULL) { | |
2866 return NGX_ERROR; | |
2867 } | |
2868 | |
2869 v->data = p; | |
2870 | |
2871 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
|
2872 state = r->upstream_states->elts; |
573 | 2873 |
2874 for ( ;; ) { | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2875 if (state[i].status) { |
889
a9a7e4b1a72b
the previous fix does not actually fix overflow
Igor Sysoev <igor@sysoev.ru>
parents:
888
diff
changeset
|
2876 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
|
2877 ms = (ms >= 0) ? ms : 0; |
a9a7e4b1a72b
the previous fix does not actually fix overflow
Igor Sysoev <igor@sysoev.ru>
parents:
888
diff
changeset
|
2878 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
|
2879 |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2880 } else { |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2881 *p++ = '-'; |
573 | 2882 } |
2883 | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2884 if (++i == r->upstream_states->nelts) { |
573 | 2885 break; |
2886 } | |
2887 | |
1168
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2888 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
|
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 *p++ = ' '; |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2891 |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2892 } else { |
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 *p++ = ' '; |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2896 |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2897 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
|
2898 break; |
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 |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2901 continue; |
4d0d12446c3b
store the upstream states before X-Accel-Redirect to a next upstream
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
2902 } |
573 | 2903 } |
2904 | |
2905 v->len = p - v->data; | |
2906 | |
2907 return NGX_OK; | |
2908 } | |
2909 | |
2910 | |
1162 | 2911 ngx_int_t |
2912 ngx_http_upstream_header_variable(ngx_http_request_t *r, | |
2913 ngx_http_variable_value_t *v, uintptr_t data) | |
2914 { | |
2915 if (r->upstream == NULL) { | |
2916 v->not_found = 1; | |
2917 return NGX_OK; | |
2918 } | |
2919 | |
2920 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data, | |
2921 &r->upstream->headers_in.headers.part, | |
2922 sizeof("upstream_http_") - 1); | |
2923 } | |
2924 | |
2925 | |
651 | 2926 static char * |
2927 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) | |
2928 { | |
884 | 2929 char *rv; |
2930 void *mconf; | |
2931 ngx_str_t *value; | |
2932 ngx_url_t u; | |
2933 ngx_uint_t m; | |
2934 ngx_conf_t pcf; | |
2935 ngx_http_module_t *module; | |
2936 ngx_http_conf_ctx_t *ctx, *http_ctx; | |
2937 ngx_http_upstream_srv_conf_t *uscf; | |
2938 | |
2939 ngx_memzero(&u, sizeof(ngx_url_t)); | |
2940 | |
2941 value = cf->args->elts; | |
2942 u.host = value[1]; | |
2943 u.no_resolve = 1; | |
2944 | |
2945 uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE | |
2946 |NGX_HTTP_UPSTREAM_WEIGHT | |
2947 |NGX_HTTP_UPSTREAM_MAX_FAILS | |
2948 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT | |
2949 |NGX_HTTP_UPSTREAM_DOWN | |
2950 |NGX_HTTP_UPSTREAM_BACKUP); | |
2951 if (uscf == NULL) { | |
2952 return NGX_CONF_ERROR; | |
2953 } | |
2954 | |
651 | 2955 |
2956 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); | |
2957 if (ctx == NULL) { | |
2958 return NGX_CONF_ERROR; | |
2959 } | |
2960 | |
884 | 2961 http_ctx = cf->ctx; |
2962 ctx->main_conf = http_ctx->main_conf; | |
651 | 2963 |
2964 /* the upstream{}'s srv_conf */ | |
2965 | |
2966 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
2967 if (ctx->srv_conf == NULL) { | |
2968 return NGX_CONF_ERROR; | |
2969 } | |
2970 | |
2971 ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf; | |
2972 | |
884 | 2973 uscf->srv_conf = ctx->srv_conf; |
2974 | |
651 | 2975 |
2976 /* the upstream{}'s loc_conf */ | |
2977 | |
2978 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
2979 if (ctx->loc_conf == NULL) { | |
2980 return NGX_CONF_ERROR; | |
2981 } | |
2982 | |
2983 for (m = 0; ngx_modules[m]; m++) { | |
2984 if (ngx_modules[m]->type != NGX_HTTP_MODULE) { | |
2985 continue; | |
2986 } | |
2987 | |
2988 module = ngx_modules[m]->ctx; | |
2989 | |
884 | 2990 if (module->create_srv_conf) { |
2991 mconf = module->create_srv_conf(cf); | |
2992 if (mconf == NULL) { | |
2993 return NGX_CONF_ERROR; | |
2994 } | |
2995 | |
2996 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf; | |
2997 } | |
2998 | |
651 | 2999 if (module->create_loc_conf) { |
3000 mconf = module->create_loc_conf(cf); | |
3001 if (mconf == NULL) { | |
3002 return NGX_CONF_ERROR; | |
3003 } | |
3004 | |
3005 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf; | |
3006 } | |
3007 } | |
3008 | |
3009 | |
3010 /* parse inside upstream{} */ | |
3011 | |
3012 pcf = *cf; | |
3013 cf->ctx = ctx; | |
3014 cf->cmd_type = NGX_HTTP_UPS_CONF; | |
3015 | |
3016 rv = ngx_conf_parse(cf, NULL); | |
3017 | |
3018 *cf = pcf; | |
3019 | |
3020 if (rv != NGX_CONF_OK) { | |
3021 return rv; | |
3022 } | |
3023 | |
3024 if (uscf->servers == NULL) { | |
3025 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
3026 "no servers are inside upstream"); | |
3027 return NGX_CONF_ERROR; | |
3028 } | |
3029 | |
3030 return rv; | |
3031 } | |
3032 | |
3033 | |
3034 static char * | |
3035 ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
3036 { | |
3037 ngx_http_upstream_srv_conf_t *uscf = conf; | |
3038 | |
884 | 3039 time_t fail_timeout; |
3040 ngx_str_t *value, s; | |
3041 ngx_url_t u; | |
3042 ngx_int_t weight, max_fails; | |
3043 ngx_uint_t i; | |
3044 ngx_http_upstream_server_t *us; | |
651 | 3045 |
3046 if (uscf->servers == NULL) { | |
884 | 3047 uscf->servers = ngx_array_create(cf->pool, 4, |
3048 sizeof(ngx_http_upstream_server_t)); | |
651 | 3049 if (uscf->servers == NULL) { |
3050 return NGX_CONF_ERROR; | |
3051 } | |
3052 } | |
3053 | |
884 | 3054 us = ngx_array_push(uscf->servers); |
3055 if (us == NULL) { | |
651 | 3056 return NGX_CONF_ERROR; |
3057 } | |
3058 | |
884 | 3059 ngx_memzero(us, sizeof(ngx_http_upstream_server_t)); |
3060 | |
651 | 3061 value = cf->args->elts; |
3062 | |
3063 ngx_memzero(&u, sizeof(ngx_url_t)); | |
3064 | |
3065 u.url = value[1]; | |
906 | 3066 u.default_port = 80; |
651 | 3067 |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1558
diff
changeset
|
3068 if (ngx_parse_url(cf->pool, &u) != NGX_OK) { |
651 | 3069 if (u.err) { |
3070 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
3071 "%s in upstream \"%V\"", u.err, &u.url); | |
3072 } | |
3073 | |
3074 return NGX_CONF_ERROR; | |
3075 } | |
3076 | |
663 | 3077 weight = 1; |
884 | 3078 max_fails = 1; |
3079 fail_timeout = 10; | |
3080 | |
3081 for (i = 2; i < cf->args->nelts; i++) { | |
3082 | |
3083 if (ngx_strncmp(value[i].data, "weight=", 7) == 0) { | |
3084 | |
3085 if (!(uscf->flags & NGX_HTTP_UPSTREAM_WEIGHT)) { | |
3086 goto invalid; | |
3087 } | |
3088 | |
3089 weight = ngx_atoi(&value[i].data[7], value[i].len - 7); | |
663 | 3090 |
3091 if (weight == NGX_ERROR || weight == 0) { | |
3092 goto invalid; | |
3093 } | |
3094 | |
884 | 3095 continue; |
3096 } | |
3097 | |
3098 if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) { | |
3099 | |
3100 if (!(uscf->flags & NGX_HTTP_UPSTREAM_MAX_FAILS)) { | |
3101 goto invalid; | |
3102 } | |
3103 | |
3104 max_fails = ngx_atoi(&value[i].data[10], value[i].len - 10); | |
3105 | |
3106 if (max_fails == NGX_ERROR) { | |
3107 goto invalid; | |
3108 } | |
3109 | |
3110 continue; | |
663 | 3111 } |
884 | 3112 |
3113 if (ngx_strncmp(value[i].data, "fail_timeout=", 13) == 0) { | |
3114 | |
3115 if (!(uscf->flags & NGX_HTTP_UPSTREAM_FAIL_TIMEOUT)) { | |
3116 goto invalid; | |
3117 } | |
3118 | |
3119 s.len = value[i].len - 13; | |
3120 s.data = &value[i].data[13]; | |
3121 | |
3122 fail_timeout = ngx_parse_time(&s, 1); | |
3123 | |
3124 if (fail_timeout < 0) { | |
3125 goto invalid; | |
3126 } | |
3127 | |
3128 continue; | |
3129 } | |
3130 | |
1378 | 3131 if (ngx_strncmp(value[i].data, "backup", 6) == 0) { |
3132 | |
3133 if (!(uscf->flags & NGX_HTTP_UPSTREAM_BACKUP)) { | |
3134 goto invalid; | |
3135 } | |
3136 | |
3137 us->backup = 1; | |
3138 | |
3139 continue; | |
3140 } | |
3141 | |
884 | 3142 if (ngx_strncmp(value[i].data, "down", 4) == 0) { |
3143 | |
3144 if (!(uscf->flags & NGX_HTTP_UPSTREAM_DOWN)) { | |
3145 goto invalid; | |
3146 } | |
3147 | |
3148 us->down = 1; | |
3149 | |
3150 continue; | |
3151 } | |
3152 | |
3153 goto invalid; | |
663 | 3154 } |
3155 | |
884 | 3156 us->addrs = u.addrs; |
3157 us->naddrs = u.naddrs; | |
3158 us->weight = weight; | |
3159 us->max_fails = max_fails; | |
3160 us->fail_timeout = fail_timeout; | |
651 | 3161 |
3162 return NGX_CONF_OK; | |
663 | 3163 |
3164 invalid: | |
3165 | |
884 | 3166 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
3167 "invalid parameter \"%V\"", &value[i]); | |
663 | 3168 |
3169 return NGX_CONF_ERROR; | |
651 | 3170 } |
3171 | |
3172 | |
3173 ngx_http_upstream_srv_conf_t * | |
884 | 3174 ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags) |
651 | 3175 { |
3176 ngx_uint_t i; | |
884 | 3177 ngx_http_upstream_server_t *us; |
651 | 3178 ngx_http_upstream_srv_conf_t *uscf, **uscfp; |
3179 ngx_http_upstream_main_conf_t *umcf; | |
3180 | |
884 | 3181 if (!(flags & NGX_HTTP_UPSTREAM_CREATE)) { |
3182 | |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1558
diff
changeset
|
3183 if (ngx_parse_url(cf->pool, u) != NGX_OK) { |
651 | 3184 if (u->err) { |
3185 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
3186 "%s in upstream \"%V\"", u->err, &u->url); | |
3187 } | |
3188 | |
3189 return NULL; | |
3190 } | |
3191 } | |
3192 | |
3193 umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module); | |
3194 | |
3195 uscfp = umcf->upstreams.elts; | |
3196 | |
3197 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
906 | 3198 |
3199 if (uscfp[i]->host.len != u->host.len | |
884 | 3200 || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len) |
3201 != 0) | |
3202 { | |
651 | 3203 continue; |
3204 } | |
3205 | |
884 | 3206 if ((flags & NGX_HTTP_UPSTREAM_CREATE) |
3207 && (uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE)) | |
651 | 3208 { |
884 | 3209 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
3210 "duplicate upstream \"%V\"", &u->host); | |
3211 return NULL; | |
651 | 3212 } |
884 | 3213 |
906 | 3214 if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) { |
884 | 3215 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
906 | 3216 "upstream \"%V\" may not have port %d", |
3217 &u->host, u->port); | |
3218 return NULL; | |
3219 } | |
3220 | |
3221 if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) { | |
3222 ngx_log_error(NGX_LOG_WARN, cf->log, 0, | |
3223 "upstream \"%V\" may not have port %d in %s:%ui", | |
3224 &u->host, uscfp[i]->port, | |
1489
56f1ea5baabb
u_char* is enough to keep file name
Igor Sysoev <igor@sysoev.ru>
parents:
1469
diff
changeset
|
3225 uscfp[i]->file_name, uscfp[i]->line); |
906 | 3226 return NULL; |
3227 } | |
3228 | |
3229 if (uscfp[i]->port != u->port) { | |
3230 continue; | |
884 | 3231 } |
3232 | |
1340 | 3233 if (uscfp[i]->default_port && u->default_port |
3234 && uscfp[i]->default_port != u->default_port) | |
3235 { | |
3236 continue; | |
3237 } | |
3238 | |
884 | 3239 return uscfp[i]; |
651 | 3240 } |
3241 | |
3242 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t)); | |
3243 if (uscf == NULL) { | |
3244 return NULL; | |
3245 } | |
3246 | |
884 | 3247 uscf->flags = flags; |
651 | 3248 uscf->host = u->host; |
1489
56f1ea5baabb
u_char* is enough to keep file name
Igor Sysoev <igor@sysoev.ru>
parents:
1469
diff
changeset
|
3249 uscf->file_name = cf->conf_file->file.name.data; |
651 | 3250 uscf->line = cf->conf_file->line; |
906 | 3251 uscf->port = u->port; |
3252 uscf->default_port = u->default_port; | |
884 | 3253 |
3254 if (u->naddrs == 1) { | |
3255 uscf->servers = ngx_array_create(cf->pool, 1, | |
3256 sizeof(ngx_http_upstream_server_t)); | |
3257 if (uscf->servers == NULL) { | |
3258 return NGX_CONF_ERROR; | |
3259 } | |
3260 | |
3261 us = ngx_array_push(uscf->servers); | |
3262 if (us == NULL) { | |
3263 return NGX_CONF_ERROR; | |
3264 } | |
3265 | |
3266 ngx_memzero(us, sizeof(ngx_http_upstream_server_t)); | |
3267 | |
3268 us->addrs = u->addrs; | |
3269 us->naddrs = u->naddrs; | |
3270 } | |
651 | 3271 |
3272 uscfp = ngx_array_push(&umcf->upstreams); | |
3273 if (uscfp == NULL) { | |
3274 return NULL; | |
3275 } | |
3276 | |
3277 *uscfp = uscf; | |
3278 | |
3279 return uscf; | |
3280 } | |
3281 | |
3282 | |
509 | 3283 static void * |
3284 ngx_http_upstream_create_main_conf(ngx_conf_t *cf) | |
3285 { | |
3286 ngx_http_upstream_main_conf_t *umcf; | |
3287 | |
3288 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t)); | |
3289 if (umcf == NULL) { | |
3290 return NULL; | |
3291 } | |
3292 | |
651 | 3293 if (ngx_array_init(&umcf->upstreams, cf->pool, 4, |
3294 sizeof(ngx_http_upstream_srv_conf_t *)) | |
3295 != NGX_OK) | |
3296 { | |
3297 return NGX_CONF_ERROR; | |
3298 } | |
3299 | |
509 | 3300 return umcf; |
3301 } | |
3302 | |
3303 | |
3304 static char * | |
651 | 3305 ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf) |
509 | 3306 { |
3307 ngx_http_upstream_main_conf_t *umcf = conf; | |
3308 | |
651 | 3309 ngx_uint_t i; |
3310 ngx_array_t headers_in; | |
3311 ngx_hash_key_t *hk; | |
3312 ngx_hash_init_t hash; | |
884 | 3313 ngx_http_upstream_init_pt init; |
651 | 3314 ngx_http_upstream_header_t *header; |
3315 ngx_http_upstream_srv_conf_t **uscfp; | |
3316 | |
3317 uscfp = umcf->upstreams.elts; | |
3318 | |
3319 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
884 | 3320 |
3321 init = uscfp[i]->peer.init_upstream ? uscfp[i]->peer.init_upstream: | |
3322 ngx_http_upstream_init_round_robin; | |
3323 | |
3324 if (init(cf, uscfp[i]) != NGX_OK) { | |
651 | 3325 return NGX_CONF_ERROR; |
3326 } | |
3327 } | |
649 | 3328 |
663 | 3329 |
884 | 3330 /* upstream_headers_in_hash */ |
3331 | |
649 | 3332 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t)) |
3333 != NGX_OK) | |
509 | 3334 { |
3335 return NGX_CONF_ERROR; | |
3336 } | |
3337 | |
649 | 3338 for (header = ngx_http_upstream_headers_in; header->name.len; header++) { |
3339 hk = ngx_array_push(&headers_in); | |
3340 if (hk == NULL) { | |
3341 return NGX_CONF_ERROR; | |
3342 } | |
3343 | |
3344 hk->key = header->name; | |
3345 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len); | |
3346 hk->value = header; | |
3347 } | |
3348 | |
3349 hash.hash = &umcf->headers_in_hash; | |
3350 hash.key = ngx_hash_key_lc; | |
3351 hash.max_size = 512; | |
751
bae59a740c40
align hash bucket size to cache line
Igor Sysoev <igor@sysoev.ru>
parents:
750
diff
changeset
|
3352 hash.bucket_size = ngx_align(64, ngx_cacheline_size); |
649 | 3353 hash.name = "upstream_headers_in_hash"; |
3354 hash.pool = cf->pool; | |
3355 hash.temp_pool = NULL; | |
3356 | |
3357 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) { | |
3358 return NGX_CONF_ERROR; | |
3359 } | |
509 | 3360 |
3361 return NGX_CONF_OK; | |
3362 } |