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