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