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