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