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