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