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