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