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
|
384
|
1102 for ( ;; ) {
|
|
1103
|
|
1104 n = c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last);
|
|
1105
|
|
1106 if (n == NGX_AGAIN) {
|
28
|
1107 #if 0
|
384
|
1108 ngx_add_timer(rev, u->read_timeout);
|
28
|
1109 #endif
|
|
1110
|
384
|
1111 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
1112 ngx_http_upstream_finalize_request(r, u,
|
28
|
1113 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
384
|
1114 return;
|
|
1115 }
|
|
1116
|
|
1117 return;
|
|
1118 }
|
|
1119
|
|
1120 if (n == 0) {
|
|
1121 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1122 "upstream prematurely closed connection");
|
|
1123 }
|
|
1124
|
|
1125 if (n == NGX_ERROR || n == 0) {
|
|
1126 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
|
28
|
1127 return;
|
|
1128 }
|
|
1129
|
384
|
1130 u->buffer.last += n;
|
28
|
1131
|
|
1132 #if 0
|
384
|
1133 u->valid_header_in = 0;
|
|
1134
|
|
1135 u->peer.cached = 0;
|
28
|
1136 #endif
|
|
1137
|
384
|
1138 rc = u->process_header(r);
|
|
1139
|
|
1140 if (rc == NGX_AGAIN) {
|
|
1141
|
|
1142 if (u->buffer.pos == u->buffer.end) {
|
|
1143 ngx_log_error(NGX_LOG_ERR, rev->log, 0,
|
|
1144 "upstream sent too big header");
|
|
1145
|
|
1146 ngx_http_upstream_next(r, u,
|
|
1147 NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);
|
|
1148 return;
|
|
1149 }
|
|
1150
|
|
1151 continue;
|
28
|
1152 }
|
|
1153
|
384
|
1154 break;
|
28
|
1155 }
|
|
1156
|
|
1157 if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) {
|
|
1158 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);
|
|
1159 return;
|
|
1160 }
|
|
1161
|
356
|
1162 if (rc == NGX_ERROR) {
|
28
|
1163 ngx_http_upstream_finalize_request(r, u,
|
|
1164 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1165 return;
|
|
1166 }
|
|
1167
|
|
1168 /* rc == NGX_OK */
|
|
1169
|
248
|
1170 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST
|
|
1171 && r->subrequest_in_memory)
|
|
1172 {
|
|
1173 u->buffer.last = u->buffer.pos;
|
|
1174 }
|
|
1175
|
78
|
1176 if (u->headers_in.status_n == NGX_HTTP_INTERNAL_SERVER_ERROR) {
|
58
|
1177
|
|
1178 if (u->peer.tries > 1
|
|
1179 && (u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_500))
|
|
1180 {
|
|
1181 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_500);
|
|
1182 return;
|
|
1183 }
|
|
1184
|
|
1185 #if (NGX_HTTP_CACHE)
|
|
1186
|
|
1187 if (u->peer.tries == 0
|
|
1188 && u->stale
|
|
1189 && (u->conf->use_stale & NGX_HTTP_UPSTREAM_FT_HTTP_500))
|
|
1190 {
|
|
1191 ngx_http_upstream_finalize_request(r, u,
|
78
|
1192 ngx_http_send_cached_response(r));
|
58
|
1193 return;
|
|
1194 }
|
|
1195
|
|
1196 #endif
|
|
1197 }
|
|
1198
|
146
|
1199 if (u->headers_in.status_n == NGX_HTTP_NOT_FOUND) {
|
|
1200
|
|
1201 if (u->peer.tries > 1
|
|
1202 && u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_404)
|
|
1203 {
|
|
1204 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_404);
|
|
1205 return;
|
|
1206 }
|
|
1207
|
224
|
1208 if (u->conf->intercept_404) {
|
|
1209 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_NOT_FOUND);
|
146
|
1210 return;
|
|
1211 }
|
58
|
1212 }
|
|
1213
|
|
1214
|
78
|
1215 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST
|
224
|
1216 && u->conf->intercept_errors)
|
36
|
1217 {
|
|
1218 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1219
|
|
1220 if (clcf->error_pages) {
|
|
1221
|
|
1222 err_page = clcf->error_pages->elts;
|
|
1223 for (i = 0; i < clcf->error_pages->nelts; i++) {
|
78
|
1224 if (err_page[i].status == (ngx_int_t) u->headers_in.status_n) {
|
|
1225
|
|
1226 if (u->headers_in.status_n == NGX_HTTP_UNAUTHORIZED) {
|
|
1227
|
|
1228 r->headers_out.www_authenticate =
|
|
1229 ngx_list_push(&r->headers_out.headers);
|
|
1230
|
|
1231 if (r->headers_out.www_authenticate == NULL) {
|
|
1232 ngx_http_upstream_finalize_request(r, u,
|
|
1233 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1234 return;
|
|
1235 }
|
|
1236
|
126
|
1237 *r->headers_out.www_authenticate =
|
78
|
1238 *u->headers_in.www_authenticate;
|
|
1239 }
|
|
1240
|
36
|
1241 ngx_http_upstream_finalize_request(r, u,
|
78
|
1242 u->headers_in.status_n);
|
36
|
1243 return;
|
|
1244 }
|
|
1245 }
|
|
1246 }
|
|
1247 }
|
|
1248
|
230
|
1249 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
|
|
1250
|
356
|
1251 if (u->headers_in.x_accel_redirect) {
|
244
|
1252
|
64
|
1253 ngx_http_upstream_finalize_request(r, u, NGX_DECLINED);
|
|
1254
|
356
|
1255 part = &u->headers_in.headers.part;
|
64
|
1256 h = part->elts;
|
|
1257
|
|
1258 for (i = 0; /* void */; i++) {
|
|
1259
|
|
1260 if (i >= part->nelts) {
|
|
1261 if (part->next == NULL) {
|
|
1262 break;
|
|
1263 }
|
126
|
1264
|
64
|
1265 part = part->next;
|
|
1266 h = part->elts;
|
|
1267 i = 0;
|
|
1268 }
|
|
1269
|
198
|
1270 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
|
|
1271 h[i].lowcase_key, h[i].key.len);
|
|
1272
|
|
1273 if (hh && hh->redirect) {
|
|
1274 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
|
250
|
1275 ngx_http_finalize_request(r,
|
244
|
1276 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
64
|
1277 return;
|
|
1278 }
|
|
1279 }
|
|
1280 }
|
|
1281
|
356
|
1282 uri = &u->headers_in.x_accel_redirect->value;
|
122
|
1283 args.len = 0;
|
|
1284 args.data = NULL;
|
|
1285 flags = 0;
|
|
1286
|
|
1287 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
|
244
|
1288 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
|
122
|
1289 return;
|
|
1290 }
|
|
1291
|
|
1292 if (flags & NGX_HTTP_ZERO_IN_URI) {
|
|
1293 r->zero_in_uri = 1;
|
|
1294 }
|
|
1295
|
202
|
1296 if (r->method != NGX_HTTP_HEAD) {
|
|
1297 r->method = NGX_HTTP_GET;
|
|
1298 }
|
|
1299
|
122
|
1300 ngx_http_internal_redirect(r, uri, &args);
|
64
|
1301 return;
|
|
1302 }
|
|
1303
|
356
|
1304 part = &u->headers_in.headers.part;
|
58
|
1305 h = part->elts;
|
|
1306
|
|
1307 for (i = 0; /* void */; i++) {
|
28
|
1308
|
58
|
1309 if (i >= part->nelts) {
|
|
1310 if (part->next == NULL) {
|
|
1311 break;
|
|
1312 }
|
126
|
1313
|
58
|
1314 part = part->next;
|
|
1315 h = part->elts;
|
|
1316 i = 0;
|
|
1317 }
|
|
1318
|
198
|
1319 if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash,
|
|
1320 h[i].lowcase_key, h[i].key.len))
|
58
|
1321 {
|
198
|
1322 continue;
|
|
1323 }
|
|
1324
|
|
1325 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
|
|
1326 h[i].lowcase_key, h[i].key.len);
|
|
1327
|
|
1328 if (hh) {
|
|
1329 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
|
58
|
1330 ngx_http_upstream_finalize_request(r, u,
|
|
1331 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1332 return;
|
|
1333 }
|
|
1334
|
|
1335 continue;
|
|
1336 }
|
|
1337
|
|
1338 if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) {
|
|
1339 ngx_http_upstream_finalize_request(r, u,
|
|
1340 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1341 return;
|
|
1342 }
|
|
1343 }
|
|
1344
|
198
|
1345 if (r->headers_out.server && r->headers_out.server->value.data == NULL) {
|
|
1346 r->headers_out.server->hash = 0;
|
|
1347 }
|
|
1348
|
|
1349 if (r->headers_out.date && r->headers_out.date->value.data == NULL) {
|
|
1350 r->headers_out.date->hash = 0;
|
|
1351 }
|
|
1352
|
78
|
1353 r->headers_out.status = u->headers_in.status_n;
|
|
1354 r->headers_out.status_line = u->headers_in.status_line;
|
|
1355
|
364
|
1356 u->headers_in.content_length_n = r->headers_out.content_length_n;
|
|
1357
|
130
|
1358 if (r->headers_out.content_length_n != -1) {
|
|
1359 u->length = (size_t) r->headers_out.content_length_n;
|
|
1360
|
|
1361 } else {
|
|
1362 u->length = NGX_MAX_SIZE_T_VALUE;
|
|
1363 }
|
|
1364
|
248
|
1365 if (!r->subrequest_in_memory) {
|
|
1366 ngx_http_upstream_send_response(r, u);
|
|
1367 return;
|
|
1368 }
|
|
1369
|
|
1370 /* subrequest content in memory */
|
|
1371
|
|
1372 if (u->input_filter == NULL) {
|
|
1373 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
|
|
1374 u->input_filter = ngx_http_upstream_non_buffered_filter;
|
|
1375 u->input_filter_ctx = r;
|
|
1376 }
|
|
1377
|
|
1378 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
|
|
1379 ngx_http_upstream_finalize_request(r, u,
|
|
1380 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1381 return;
|
|
1382 }
|
|
1383
|
|
1384 if (u->buffer.last - u->buffer.pos >= (ssize_t) u->length) {
|
|
1385 if (u->input_filter(u->input_filter_ctx, 0) == NGX_ERROR) {
|
|
1386 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
|
|
1387 return;
|
|
1388 }
|
|
1389
|
|
1390 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1391 return;
|
|
1392 }
|
|
1393
|
|
1394 rev->handler = ngx_http_upstream_process_body_in_memory;
|
|
1395
|
|
1396 ngx_http_upstream_process_body_in_memory(rev);
|
|
1397 }
|
|
1398
|
|
1399
|
294
|
1400 static ngx_int_t
|
|
1401 ngx_http_upstream_test_connect(ngx_connection_t *c)
|
|
1402 {
|
|
1403 int err;
|
|
1404 socklen_t len;
|
|
1405
|
|
1406 #if (NGX_HAVE_KQUEUE)
|
|
1407
|
|
1408 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
296
|
1409 if (c->write->pending_eof) {
|
336
|
1410 c->log->action = "connecting to upstream";
|
296
|
1411 (void) ngx_connection_error(c, c->write->kq_errno,
|
|
1412 "kevent() reported that connect() failed");
|
|
1413 return NGX_ERROR;
|
|
1414 }
|
294
|
1415
|
|
1416 } else
|
|
1417 #endif
|
|
1418 {
|
296
|
1419 err = 0;
|
|
1420 len = sizeof(int);
|
|
1421
|
|
1422 /*
|
|
1423 * BSDs and Linux return 0 and set a pending error in err
|
|
1424 * Solaris returns -1 and sets errno
|
|
1425 */
|
|
1426
|
|
1427 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len)
|
|
1428 == -1)
|
|
1429 {
|
|
1430 err = ngx_errno;
|
|
1431 }
|
|
1432
|
|
1433 if (err) {
|
336
|
1434 c->log->action = "connecting to upstream";
|
296
|
1435 (void) ngx_connection_error(c, err, "connect() failed");
|
|
1436 return NGX_ERROR;
|
|
1437 }
|
294
|
1438 }
|
|
1439
|
|
1440 return NGX_OK;
|
|
1441 }
|
|
1442
|
|
1443
|
248
|
1444 static void
|
|
1445 ngx_http_upstream_process_body_in_memory(ngx_event_t *rev)
|
|
1446 {
|
|
1447 size_t size;
|
|
1448 ssize_t n;
|
|
1449 ngx_buf_t *b;
|
|
1450 ngx_connection_t *c;
|
|
1451 ngx_http_request_t *r;
|
|
1452 ngx_http_upstream_t *u;
|
|
1453
|
|
1454 c = rev->data;
|
|
1455 r = c->data;
|
|
1456 u = r->upstream;
|
|
1457
|
|
1458 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1459 "http upstream process body on memory");
|
|
1460
|
|
1461 if (rev->timedout) {
|
|
1462 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
|
|
1463 ngx_http_upstream_finalize_request(r, u, NGX_ETIMEDOUT);
|
|
1464 return;
|
|
1465 }
|
|
1466
|
|
1467 b = &u->buffer;
|
|
1468
|
|
1469 for ( ;; ) {
|
|
1470
|
|
1471 size = b->end - b->last;
|
|
1472
|
|
1473 if (size == 0) {
|
|
1474 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
|
1475 "upstream buffer is too small to read repsonse");
|
|
1476 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
|
|
1477 return;
|
|
1478 }
|
|
1479
|
|
1480 n = c->recv(c, b->last, size);
|
|
1481
|
|
1482 if (n == NGX_AGAIN) {
|
|
1483 break;
|
|
1484 }
|
|
1485
|
|
1486 if (n == 0 || n == NGX_ERROR) {
|
|
1487 ngx_http_upstream_finalize_request(r, u, n);
|
|
1488 return;
|
|
1489 }
|
|
1490
|
|
1491 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
|
|
1492 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
|
|
1493 return;
|
|
1494 }
|
|
1495
|
|
1496 if (!rev->ready) {
|
|
1497 break;
|
|
1498 }
|
|
1499 }
|
|
1500
|
|
1501 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
1502 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
|
|
1503 return;
|
|
1504 }
|
|
1505
|
|
1506 if (rev->active) {
|
|
1507 ngx_add_timer(rev, u->conf->read_timeout);
|
|
1508
|
|
1509 } else if (rev->timer_set) {
|
|
1510 ngx_del_timer(rev);
|
|
1511 }
|
230
|
1512 }
|
|
1513
|
|
1514
|
|
1515 static void
|
|
1516 ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
|
|
1517 {
|
|
1518 int tcp_nodelay;
|
|
1519 ssize_t size;
|
|
1520 ngx_int_t rc;
|
|
1521 ngx_event_pipe_t *p;
|
|
1522 ngx_connection_t *c;
|
|
1523 ngx_pool_cleanup_t *cl;
|
|
1524 ngx_pool_cleanup_file_t *clf;
|
|
1525 ngx_http_core_loc_conf_t *clcf;
|
|
1526
|
58
|
1527 rc = ngx_http_send_header(r);
|
28
|
1528
|
274
|
1529 if (rc == NGX_ERROR || rc > NGX_OK || r->post_action || r->header_only) {
|
34
|
1530 ngx_http_upstream_finalize_request(r, u, rc);
|
28
|
1531 return;
|
|
1532 }
|
|
1533
|
|
1534 u->header_sent = 1;
|
|
1535
|
122
|
1536 if (r->request_body && r->request_body->temp_file) {
|
112
|
1537 for (cl = r->pool->cleanup; cl; cl = cl->next) {
|
|
1538 if (cl->handler == ngx_pool_cleanup_file) {
|
|
1539 clf = cl->data;
|
|
1540
|
|
1541 if (clf->fd == r->request_body->temp_file->file.fd) {
|
|
1542 cl->handler(clf);
|
|
1543 cl->handler = NULL;
|
208
|
1544 r->request_body->temp_file->file.fd = NGX_INVALID_FILE;
|
112
|
1545 break;
|
|
1546 }
|
|
1547 }
|
|
1548 }
|
|
1549 }
|
|
1550
|
132
|
1551 c = r->connection;
|
|
1552
|
130
|
1553 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1554
|
198
|
1555 if (!u->buffering) {
|
130
|
1556
|
|
1557 if (u->input_filter == NULL) {
|
|
1558 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
|
|
1559 u->input_filter = ngx_http_upstream_non_buffered_filter;
|
|
1560 u->input_filter_ctx = r;
|
|
1561 }
|
|
1562
|
|
1563 u->peer.connection->read->handler =
|
|
1564 ngx_http_upstream_process_non_buffered_body;
|
|
1565 r->write_event_handler =
|
|
1566 ngx_http_upstream_process_non_buffered_downstream;
|
|
1567
|
|
1568 r->limit_rate = 0;
|
|
1569
|
|
1570 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
|
|
1571 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1572 return;
|
|
1573 }
|
|
1574
|
132
|
1575 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) {
|
|
1576 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
|
|
1577
|
|
1578 tcp_nodelay = 1;
|
|
1579
|
|
1580 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
|
|
1581 (const void *) &tcp_nodelay, sizeof(int)) == -1)
|
|
1582 {
|
|
1583 ngx_connection_error(c, ngx_socket_errno,
|
|
1584 "setsockopt(TCP_NODELAY) failed");
|
|
1585 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1586 return;
|
|
1587 }
|
|
1588
|
|
1589 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
|
|
1590 }
|
|
1591
|
130
|
1592 size = u->buffer.last - u->buffer.pos;
|
|
1593
|
|
1594 if (size) {
|
|
1595 u->buffer.last = u->buffer.pos;
|
|
1596
|
|
1597 if (u->input_filter(u->input_filter_ctx, size) == NGX_ERROR) {
|
|
1598 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1599 return;
|
|
1600 }
|
|
1601
|
132
|
1602 ngx_http_upstream_process_non_buffered_body(c->write);
|
130
|
1603
|
|
1604 } else {
|
|
1605 u->buffer.pos = u->buffer.start;
|
|
1606 u->buffer.last = u->buffer.start;
|
|
1607
|
|
1608 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) {
|
|
1609 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1610 return;
|
|
1611 }
|
336
|
1612
|
|
1613 if (u->peer.connection->read->ready) {
|
|
1614 ngx_http_upstream_process_non_buffered_body(
|
|
1615 u->peer.connection->read);
|
|
1616 }
|
130
|
1617 }
|
|
1618
|
|
1619 return;
|
|
1620 }
|
|
1621
|
28
|
1622 /* TODO: preallocate event_pipe bufs, look "Content-Length" */
|
|
1623
|
|
1624 #if 0
|
|
1625
|
|
1626 if (u->cache && u->cache->ctx.file.fd != NGX_INVALID_FILE) {
|
|
1627 if (ngx_close_file(u->cache->ctx.file.fd) == NGX_FILE_ERROR) {
|
132
|
1628 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
28
|
1629 ngx_close_file_n " \"%s\" failed",
|
|
1630 u->cache->ctx.file.name.data);
|
|
1631 }
|
|
1632 }
|
|
1633
|
340
|
1634 if (u->cacheable) {
|
130
|
1635 header = (ngx_http_cache_header_t *) u->buffer->start;
|
28
|
1636
|
|
1637 header->expires = u->cache->ctx.expires;
|
|
1638 header->last_modified = u->cache->ctx.last_modified;
|
|
1639 header->date = u->cache->ctx.date;
|
|
1640 header->length = r->headers_out.content_length_n;
|
|
1641 u->cache->ctx.length = r->headers_out.content_length_n;
|
|
1642
|
|
1643 header->key_len = u->cache->ctx.key0.len;
|
|
1644 ngx_memcpy(&header->key, u->cache->ctx.key0.data, header->key_len);
|
|
1645 header->key[header->key_len] = LF;
|
|
1646 }
|
|
1647
|
|
1648 #endif
|
|
1649
|
130
|
1650 p = u->pipe;
|
28
|
1651
|
|
1652 p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter;
|
|
1653 p->output_ctx = r;
|
|
1654 p->tag = u->output.tag;
|
126
|
1655 p->bufs = u->conf->bufs;
|
28
|
1656 p->busy_size = u->conf->busy_buffers_size;
|
|
1657 p->upstream = u->peer.connection;
|
132
|
1658 p->downstream = c;
|
28
|
1659 p->pool = r->pool;
|
132
|
1660 p->log = c->log;
|
126
|
1661
|
340
|
1662 p->cacheable = u->cacheable || u->store;
|
28
|
1663
|
50
|
1664 p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
|
|
1665 if (p->temp_file == NULL) {
|
28
|
1666 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1667 return;
|
|
1668 }
|
|
1669
|
|
1670 p->temp_file->file.fd = NGX_INVALID_FILE;
|
132
|
1671 p->temp_file->file.log = c->log;
|
28
|
1672 p->temp_file->path = u->conf->temp_path;
|
|
1673 p->temp_file->pool = r->pool;
|
126
|
1674
|
340
|
1675 if (u->cacheable || u->store) {
|
28
|
1676 p->temp_file->persistent = 1;
|
318
|
1677
|
28
|
1678 } else {
|
216
|
1679 p->temp_file->log_level = NGX_LOG_WARN;
|
28
|
1680 p->temp_file->warn = "an upstream response is buffered "
|
|
1681 "to a temporary file";
|
|
1682 }
|
|
1683
|
|
1684 p->max_temp_file_size = u->conf->max_temp_file_size;
|
|
1685 p->temp_file_write_size = u->conf->temp_file_write_size;
|
|
1686
|
50
|
1687 p->preread_bufs = ngx_alloc_chain_link(r->pool);
|
|
1688 if (p->preread_bufs == NULL) {
|
28
|
1689 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1690 return;
|
|
1691 }
|
318
|
1692
|
130
|
1693 p->preread_bufs->buf = &u->buffer;
|
28
|
1694 p->preread_bufs->next = NULL;
|
130
|
1695 u->buffer.recycled = 1;
|
|
1696
|
|
1697 p->preread_size = u->buffer.last - u->buffer.pos;
|
28
|
1698
|
340
|
1699 if (u->cacheable) {
|
318
|
1700
|
28
|
1701 p->buf_to_file = ngx_calloc_buf(r->pool);
|
|
1702 if (p->buf_to_file == NULL) {
|
|
1703 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1704 return;
|
|
1705 }
|
318
|
1706
|
130
|
1707 p->buf_to_file->pos = u->buffer.start;
|
|
1708 p->buf_to_file->last = u->buffer.pos;
|
28
|
1709 p->buf_to_file->temporary = 1;
|
|
1710 }
|
|
1711
|
|
1712 if (ngx_event_flags & NGX_USE_AIO_EVENT) {
|
|
1713 /* the posted aio operation may currupt a shadow buffer */
|
|
1714 p->single_buf = 1;
|
|
1715 }
|
|
1716
|
|
1717 /* TODO: p->free_bufs = 0 if use ngx_create_chain_of_bufs() */
|
|
1718 p->free_bufs = 1;
|
|
1719
|
|
1720 /*
|
130
|
1721 * event_pipe would do u->buffer.last += p->preread_size
|
28
|
1722 * as though these bytes were read
|
|
1723 */
|
130
|
1724 u->buffer.last = u->buffer.pos;
|
28
|
1725
|
|
1726 if (u->conf->cyclic_temp_file) {
|
|
1727
|
|
1728 /*
|
|
1729 * we need to disable the use of sendfile() if we use cyclic temp file
|
|
1730 * because the writing a new data may interfere with sendfile()
|
|
1731 * that uses the same kernel file pages (at least on FreeBSD)
|
|
1732 */
|
|
1733
|
|
1734 p->cyclic_temp_file = 1;
|
132
|
1735 c->sendfile = 0;
|
28
|
1736
|
|
1737 } else {
|
|
1738 p->cyclic_temp_file = 0;
|
|
1739 }
|
|
1740
|
|
1741 p->read_timeout = u->conf->read_timeout;
|
|
1742 p->send_timeout = clcf->send_timeout;
|
|
1743 p->send_lowat = clcf->send_lowat;
|
|
1744
|
58
|
1745 u->peer.connection->read->handler = ngx_http_upstream_process_body;
|
|
1746 r->write_event_handler = ngx_http_upstream_process_downstream;
|
28
|
1747
|
|
1748 ngx_http_upstream_process_body(u->peer.connection->read);
|
|
1749 }
|
|
1750
|
|
1751
|
36
|
1752 static void
|
130
|
1753 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r)
|
|
1754 {
|
|
1755 ngx_http_upstream_process_non_buffered_body(r->connection->write);
|
|
1756 }
|
|
1757
|
|
1758
|
|
1759 static void
|
|
1760 ngx_http_upstream_process_non_buffered_body(ngx_event_t *ev)
|
|
1761 {
|
|
1762 size_t size;
|
|
1763 ssize_t n;
|
|
1764 ngx_buf_t *b;
|
132
|
1765 ngx_int_t rc;
|
130
|
1766 ngx_uint_t do_write;
|
260
|
1767 ngx_connection_t *c, *downstream, *upstream;
|
130
|
1768 ngx_http_request_t *r;
|
|
1769 ngx_http_upstream_t *u;
|
|
1770 ngx_http_core_loc_conf_t *clcf;
|
|
1771
|
|
1772 c = ev->data;
|
244
|
1773 r = c->data;
|
|
1774 u = r->upstream;
|
130
|
1775
|
|
1776 if (ev->write) {
|
|
1777 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1778 "http upstream process non buffered downstream");
|
|
1779 c->log->action = "sending to client";
|
|
1780
|
|
1781 } else {
|
|
1782 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1783 "http upstream process non buffered upstream");
|
|
1784 c->log->action = "reading upstream";
|
|
1785 }
|
|
1786
|
|
1787 if (ev->timedout) {
|
|
1788 if (ev->write) {
|
|
1789 c->timedout = 1;
|
160
|
1790 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
|
|
1791
|
130
|
1792 } else {
|
160
|
1793 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
|
130
|
1794 }
|
244
|
1795
|
|
1796 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1797 return;
|
130
|
1798 }
|
|
1799
|
260
|
1800 downstream = r->connection;
|
|
1801 upstream = u->peer.connection;
|
130
|
1802
|
|
1803 b = &u->buffer;
|
|
1804
|
|
1805 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1806
|
206
|
1807 do_write = ev->write || u->length == 0;
|
130
|
1808
|
|
1809 for ( ;; ) {
|
|
1810
|
|
1811 if (do_write) {
|
|
1812
|
|
1813 if (u->out_bufs || u->busy_bufs) {
|
132
|
1814 rc = ngx_http_output_filter(r, u->out_bufs);
|
|
1815
|
260
|
1816 if (downstream->destroyed) {
|
132
|
1817 return;
|
|
1818 }
|
|
1819
|
|
1820 if (rc == NGX_ERROR) {
|
130
|
1821 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1822 return;
|
|
1823 }
|
|
1824
|
|
1825 ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs,
|
|
1826 &u->out_bufs, u->output.tag);
|
|
1827 }
|
|
1828
|
|
1829 if (u->busy_bufs == NULL) {
|
|
1830
|
|
1831 if (u->length == 0
|
260
|
1832 || upstream->read->eof
|
|
1833 || upstream->read->error)
|
130
|
1834 {
|
|
1835 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1836 return;
|
|
1837 }
|
|
1838
|
|
1839 b->pos = b->start;
|
|
1840 b->last = b->start;
|
|
1841 }
|
|
1842 }
|
|
1843
|
|
1844 size = b->end - b->last;
|
|
1845
|
|
1846 if (size > u->length) {
|
|
1847 size = u->length;
|
|
1848 }
|
|
1849
|
260
|
1850 if (size && upstream->read->ready) {
|
|
1851
|
|
1852 n = upstream->recv(upstream, b->last, size);
|
130
|
1853
|
|
1854 if (n == NGX_AGAIN) {
|
|
1855 break;
|
|
1856 }
|
|
1857
|
|
1858 if (n > 0) {
|
|
1859 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
|
|
1860 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1861 return;
|
|
1862 }
|
|
1863 }
|
|
1864
|
|
1865 do_write = 1;
|
|
1866
|
|
1867 continue;
|
|
1868 }
|
|
1869
|
|
1870 break;
|
|
1871 }
|
|
1872
|
260
|
1873 if (downstream->data == r) {
|
|
1874 if (ngx_handle_write_event(downstream->write, clcf->send_lowat)
|
132
|
1875 == NGX_ERROR)
|
|
1876 {
|
|
1877 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1878 return;
|
|
1879 }
|
130
|
1880 }
|
|
1881
|
276
|
1882 if (downstream->write->active && !downstream->write->ready) {
|
260
|
1883 ngx_add_timer(downstream->write, clcf->send_timeout);
|
|
1884
|
|
1885 } else if (downstream->write->timer_set) {
|
|
1886 ngx_del_timer(downstream->write);
|
130
|
1887 }
|
|
1888
|
260
|
1889 if (ngx_handle_read_event(upstream->read, 0) == NGX_ERROR) {
|
130
|
1890 ngx_http_upstream_finalize_request(r, u, 0);
|
|
1891 return;
|
|
1892 }
|
|
1893
|
276
|
1894 if (upstream->read->active && !upstream->read->ready) {
|
260
|
1895 ngx_add_timer(upstream->read, u->conf->read_timeout);
|
|
1896
|
|
1897 } else if (upstream->read->timer_set) {
|
|
1898 ngx_del_timer(upstream->read);
|
130
|
1899 }
|
|
1900 }
|
|
1901
|
|
1902
|
|
1903 static ngx_int_t
|
|
1904 ngx_http_upstream_non_buffered_filter_init(void *data)
|
|
1905 {
|
|
1906 return NGX_OK;
|
|
1907 }
|
|
1908
|
|
1909
|
|
1910 static ngx_int_t
|
|
1911 ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes)
|
|
1912 {
|
|
1913 ngx_http_request_t *r = data;
|
|
1914
|
|
1915 ngx_buf_t *b;
|
|
1916 ngx_chain_t *cl, **ll;
|
|
1917 ngx_http_upstream_t *u;
|
|
1918
|
|
1919 u = r->upstream;
|
|
1920
|
|
1921 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
1922 ll = &cl->next;
|
|
1923 }
|
|
1924
|
|
1925 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
1926 if (cl == NULL) {
|
|
1927 return NGX_ERROR;
|
|
1928 }
|
|
1929
|
|
1930 *ll = cl;
|
|
1931
|
|
1932 cl->buf->flush = 1;
|
|
1933 cl->buf->memory = 1;
|
|
1934
|
|
1935 b = &u->buffer;
|
|
1936
|
|
1937 cl->buf->pos = b->last;
|
|
1938 b->last += bytes;
|
|
1939 cl->buf->last = b->last;
|
366
|
1940 cl->buf->tag = u->output.tag;
|
130
|
1941
|
|
1942 if (u->length == NGX_MAX_SIZE_T_VALUE) {
|
|
1943 return NGX_OK;
|
|
1944 }
|
|
1945
|
|
1946 u->length -= bytes;
|
|
1947
|
|
1948 return NGX_OK;
|
|
1949 }
|
|
1950
|
|
1951
|
|
1952 static void
|
58
|
1953 ngx_http_upstream_process_downstream(ngx_http_request_t *r)
|
|
1954 {
|
|
1955 ngx_http_upstream_process_body(r->connection->write);
|
|
1956 }
|
|
1957
|
|
1958
|
|
1959 static void
|
36
|
1960 ngx_http_upstream_process_body(ngx_event_t *ev)
|
28
|
1961 {
|
384
|
1962 ngx_uint_t del;
|
364
|
1963 ngx_temp_file_t *tf;
|
58
|
1964 ngx_event_pipe_t *p;
|
132
|
1965 ngx_connection_t *c, *downstream;
|
284
|
1966 ngx_http_log_ctx_t *ctx;
|
28
|
1967 ngx_http_request_t *r;
|
|
1968 ngx_http_upstream_t *u;
|
|
1969
|
|
1970 c = ev->data;
|
|
1971 r = c->data;
|
|
1972 u = r->upstream;
|
132
|
1973 downstream = r->connection;
|
28
|
1974
|
|
1975 if (ev->write) {
|
32
|
1976 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1977 "http upstream process downstream");
|
|
1978 c->log->action = "sending to client";
|
126
|
1979
|
28
|
1980 } else {
|
32
|
1981 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1982 "http upstream process upstream");
|
|
1983 c->log->action = "reading upstream";
|
284
|
1984
|
|
1985 ctx = c->log->data;
|
|
1986 ctx->current_request = r;
|
28
|
1987 }
|
126
|
1988
|
130
|
1989 p = u->pipe;
|
28
|
1990
|
|
1991 if (ev->timedout) {
|
|
1992 if (ev->write) {
|
76
|
1993 if (ev->delayed) {
|
|
1994
|
|
1995 ev->timedout = 0;
|
|
1996 ev->delayed = 0;
|
|
1997
|
|
1998 if (!ev->ready) {
|
|
1999 ngx_add_timer(ev, p->send_timeout);
|
|
2000
|
|
2001 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR)
|
|
2002 {
|
|
2003 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2004 return;
|
|
2005 }
|
|
2006
|
|
2007 return;
|
|
2008 }
|
|
2009
|
|
2010 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) {
|
132
|
2011
|
|
2012 if (downstream->destroyed) {
|
|
2013 return;
|
|
2014 }
|
|
2015
|
76
|
2016 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2017 return;
|
|
2018 }
|
|
2019
|
|
2020 } else {
|
|
2021 p->downstream_error = 1;
|
126
|
2022 c->timedout = 1;
|
160
|
2023 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
|
76
|
2024 }
|
28
|
2025
|
|
2026 } else {
|
126
|
2027 p->upstream_error = 1;
|
160
|
2028 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
|
28
|
2029 }
|
|
2030
|
|
2031 } else {
|
76
|
2032 if (ev->write && ev->delayed) {
|
|
2033 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2034 "http downstream delayed");
|
|
2035
|
|
2036 if (ngx_handle_write_event(ev, p->send_lowat) == NGX_ERROR) {
|
|
2037 return;
|
|
2038 }
|
|
2039
|
|
2040 return;
|
|
2041 }
|
|
2042
|
28
|
2043 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) {
|
132
|
2044
|
|
2045 if (downstream->destroyed) {
|
|
2046 return;
|
|
2047 }
|
|
2048
|
28
|
2049 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2050 return;
|
|
2051 }
|
|
2052 }
|
126
|
2053
|
28
|
2054 if (u->peer.connection) {
|
|
2055
|
318
|
2056 if (u->store) {
|
|
2057
|
384
|
2058 del = p->upstream_error;
|
|
2059
|
364
|
2060 tf = u->pipe->temp_file;
|
|
2061
|
384
|
2062 if (p->upstream_eof || p->upstream_done) {
|
|
2063
|
|
2064 if (u->headers_in.status_n == NGX_HTTP_OK
|
|
2065 && (u->headers_in.content_length_n == -1
|
|
2066 || (u->headers_in.content_length_n == tf->offset)))
|
|
2067 {
|
|
2068 ngx_http_upstream_store(r, u);
|
|
2069
|
|
2070 } else {
|
|
2071 del = 1;
|
|
2072 }
|
|
2073 }
|
|
2074
|
|
2075 if (del && tf->file.fd != NGX_INVALID_FILE) {
|
|
2076
|
364
|
2077 if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {
|
|
2078
|
318
|
2079 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
2080 ngx_delete_file_n " \"%s\" failed",
|
|
2081 u->pipe->temp_file->file.name.data);
|
|
2082 }
|
|
2083 }
|
|
2084 }
|
|
2085
|
28
|
2086 #if (NGX_HTTP_FILE_CACHE)
|
|
2087
|
340
|
2088 if (p->upstream_done && u->cacheable) {
|
28
|
2089 if (ngx_http_cache_update(r) == NGX_ERROR) {
|
|
2090 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
|
|
2091 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2092 return;
|
|
2093 }
|
|
2094
|
340
|
2095 } else if (p->upstream_eof && u->cacheable) {
|
28
|
2096
|
|
2097 /* TODO: check length & update cache */
|
|
2098
|
|
2099 if (ngx_http_cache_update(r) == NGX_ERROR) {
|
|
2100 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
|
|
2101 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2102 return;
|
|
2103 }
|
|
2104 }
|
|
2105
|
|
2106 #endif
|
|
2107
|
|
2108 if (p->upstream_done || p->upstream_eof || p->upstream_error) {
|
32
|
2109 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2110 "http upstream exit: %p", p->out);
|
28
|
2111 #if 0
|
|
2112 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
|
|
2113 #endif
|
|
2114 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2115 return;
|
|
2116 }
|
|
2117 }
|
|
2118
|
|
2119 if (p->downstream_error) {
|
32
|
2120 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2121 "http upstream downstream error");
|
28
|
2122
|
340
|
2123 if (!u->cacheable && u->peer.connection) {
|
28
|
2124 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2125 }
|
|
2126 }
|
|
2127 }
|
|
2128
|
|
2129
|
36
|
2130 static void
|
318
|
2131 ngx_http_upstream_store(ngx_http_request_t *r, ngx_http_upstream_t *u)
|
|
2132 {
|
370
|
2133 size_t root;
|
|
2134 time_t lm;
|
|
2135 ngx_str_t path;
|
|
2136 ngx_temp_file_t *tf;
|
|
2137 ngx_ext_rename_file_t ext;
|
318
|
2138
|
358
|
2139 tf = u->pipe->temp_file;
|
|
2140
|
|
2141 if (tf->file.fd == NGX_INVALID_FILE) {
|
318
|
2142
|
|
2143 /* create file for empty 200 response */
|
|
2144
|
|
2145 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
|
|
2146 if (tf == NULL) {
|
|
2147 return;
|
|
2148 }
|
|
2149
|
|
2150 tf->file.fd = NGX_INVALID_FILE;
|
|
2151 tf->file.log = r->connection->log;
|
|
2152 tf->path = u->conf->temp_path;
|
|
2153 tf->pool = r->pool;
|
|
2154 tf->persistent = 1;
|
|
2155
|
|
2156 if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
|
|
2157 tf->persistent, tf->clean, tf->access)
|
|
2158 != NGX_OK)
|
|
2159 {
|
|
2160 return;
|
|
2161 }
|
|
2162
|
|
2163 u->pipe->temp_file = tf;
|
|
2164 }
|
|
2165
|
370
|
2166 ext.access = u->conf->store_access;
|
|
2167 ext.time = -1;
|
|
2168 ext.create_path = 1;
|
|
2169 ext.delete_file = 1;
|
|
2170 ext.log = r->connection->log;
|
318
|
2171
|
356
|
2172 if (u->headers_in.last_modified) {
|
|
2173
|
370
|
2174 lm = ngx_http_parse_time(u->headers_in.last_modified->value.data,
|
|
2175 u->headers_in.last_modified->value.len);
|
318
|
2176
|
|
2177 if (lm != NGX_ERROR) {
|
370
|
2178 ext.time = lm;
|
|
2179 ext.fd = tf->file.fd;
|
318
|
2180 }
|
|
2181 }
|
|
2182
|
320
|
2183 if (u->conf->store_lengths == NULL) {
|
|
2184
|
|
2185 ngx_http_map_uri_to_path(r, &path, &root, 0);
|
|
2186
|
|
2187 } else {
|
|
2188 if (ngx_http_script_run(r, &path, u->conf->store_lengths->elts, 0,
|
|
2189 u->conf->store_values->elts)
|
|
2190 == NULL)
|
|
2191 {
|
|
2192 return;
|
|
2193 }
|
|
2194 }
|
318
|
2195
|
|
2196 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
370
|
2197 "upstream stores \"%s\" to \"%s\"",
|
|
2198 tf->file.name.data, path.data);
|
|
2199
|
|
2200 (void) ngx_ext_rename_file(&tf->file.name, &path, &ext);
|
318
|
2201 }
|
|
2202
|
|
2203
|
|
2204 static void
|
36
|
2205 ngx_http_upstream_dummy_handler(ngx_event_t *wev)
|
28
|
2206 {
|
|
2207 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
2208 "http upstream dummy handler");
|
|
2209 }
|
|
2210
|
|
2211
|
36
|
2212 static void
|
|
2213 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
|
|
2214 ngx_uint_t ft_type)
|
28
|
2215 {
|
260
|
2216 ngx_uint_t status, state;
|
28
|
2217
|
|
2218 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
260
|
2219 "http next upstream, %xi", ft_type);
|
28
|
2220
|
|
2221 #if 0
|
|
2222 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
|
|
2223 #endif
|
|
2224
|
74
|
2225 if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) {
|
260
|
2226 state = NGX_PEER_NEXT;
|
74
|
2227 } else {
|
260
|
2228 state = NGX_PEER_FAILED;
|
28
|
2229 }
|
74
|
2230
|
326
|
2231 if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) {
|
|
2232 u->peer.free(&u->peer, u->peer.data, state);
|
|
2233 }
|
74
|
2234
|
28
|
2235 if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {
|
|
2236 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,
|
|
2237 "upstream timed out");
|
|
2238 }
|
126
|
2239
|
28
|
2240 if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {
|
|
2241 status = 0;
|
|
2242
|
|
2243 } else {
|
|
2244 switch(ft_type) {
|
|
2245
|
|
2246 case NGX_HTTP_UPSTREAM_FT_TIMEOUT:
|
|
2247 status = NGX_HTTP_GATEWAY_TIME_OUT;
|
|
2248 break;
|
|
2249
|
|
2250 case NGX_HTTP_UPSTREAM_FT_HTTP_500:
|
|
2251 status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
2252 break;
|
|
2253
|
|
2254 case NGX_HTTP_UPSTREAM_FT_HTTP_404:
|
|
2255 status = NGX_HTTP_NOT_FOUND;
|
|
2256 break;
|
|
2257
|
|
2258 /*
|
|
2259 * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING
|
|
2260 * never reach here
|
|
2261 */
|
|
2262
|
|
2263 default:
|
|
2264 status = NGX_HTTP_BAD_GATEWAY;
|
|
2265 }
|
|
2266 }
|
|
2267
|
132
|
2268 if (r->connection->error) {
|
28
|
2269 ngx_http_upstream_finalize_request(r, u,
|
|
2270 NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
2271 return;
|
|
2272 }
|
|
2273
|
|
2274 if (status) {
|
|
2275 u->state->status = status;
|
|
2276
|
74
|
2277 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {
|
28
|
2278
|
|
2279 #if (NGX_HTTP_CACHE)
|
|
2280
|
|
2281 if (u->stale && (u->conf->use_stale & ft_type)) {
|
|
2282 ngx_http_upstream_finalize_request(r, u,
|
74
|
2283 ngx_http_send_cached_response(r));
|
28
|
2284 return;
|
|
2285 }
|
|
2286
|
|
2287 #endif
|
|
2288
|
|
2289 ngx_http_upstream_finalize_request(r, u, status);
|
|
2290 return;
|
|
2291 }
|
|
2292 }
|
|
2293
|
|
2294 if (u->peer.connection) {
|
|
2295 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2296 "close http upstream connection: %d",
|
|
2297 u->peer.connection->fd);
|
126
|
2298 #if (NGX_HTTP_SSL)
|
260
|
2299
|
126
|
2300 if (u->peer.connection->ssl) {
|
260
|
2301 u->peer.connection->ssl->no_wait_shutdown = 1;
|
|
2302 u->peer.connection->ssl->no_send_shutdown = 1;
|
|
2303
|
|
2304 (void) ngx_ssl_shutdown(u->peer.connection);
|
126
|
2305 }
|
|
2306 #endif
|
260
|
2307
|
28
|
2308 ngx_close_connection(u->peer.connection);
|
|
2309 }
|
|
2310
|
|
2311 #if 0
|
|
2312 if (u->conf->busy_lock && !u->busy_locked) {
|
|
2313 ngx_http_upstream_busy_lock(p);
|
|
2314 return;
|
|
2315 }
|
|
2316 #endif
|
|
2317
|
|
2318 ngx_http_upstream_connect(r, u);
|
|
2319 }
|
|
2320
|
|
2321
|
36
|
2322 static void
|
118
|
2323 ngx_http_upstream_cleanup(void *data)
|
|
2324 {
|
|
2325 ngx_http_request_t *r = data;
|
|
2326
|
356
|
2327 ngx_http_upstream_t *u;
|
|
2328
|
118
|
2329 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2330 "cleanup http upstream request: \"%V\"", &r->uri);
|
|
2331
|
356
|
2332 u = r->upstream;
|
|
2333
|
|
2334 if (u->resolved && u->resolved->ctx) {
|
|
2335 ngx_resolve_name_done(u->resolved->ctx);
|
348
|
2336 }
|
|
2337
|
356
|
2338 ngx_http_upstream_finalize_request(r, u, NGX_DONE);
|
118
|
2339 }
|
|
2340
|
|
2341
|
|
2342 static void
|
36
|
2343 ngx_http_upstream_finalize_request(ngx_http_request_t *r,
|
|
2344 ngx_http_upstream_t *u, ngx_int_t rc)
|
28
|
2345 {
|
262
|
2346 ngx_time_t *tp;
|
112
|
2347
|
32
|
2348 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2349 "finalize http upstream request: %i", rc);
|
28
|
2350
|
382
|
2351 if (u->cleanup) {
|
|
2352 *u->cleanup = NULL;
|
|
2353 }
|
118
|
2354
|
348
|
2355 if (u->state && u->state->response_sec) {
|
112
|
2356 tp = ngx_timeofday();
|
262
|
2357 u->state->response_sec = tp->sec - u->state->response_sec;
|
|
2358 u->state->response_msec = tp->msec - u->state->response_msec;
|
112
|
2359 }
|
|
2360
|
28
|
2361 u->finalize_request(r, rc);
|
|
2362
|
348
|
2363 if (u->peer.free) {
|
|
2364 u->peer.free(&u->peer, u->peer.data, 0);
|
|
2365 }
|
260
|
2366
|
28
|
2367 if (u->peer.connection) {
|
260
|
2368
|
126
|
2369 #if (NGX_HTTP_SSL)
|
|
2370
|
|
2371 /* TODO: do not shutdown persistent connection */
|
|
2372
|
|
2373 if (u->peer.connection->ssl) {
|
260
|
2374
|
|
2375 /*
|
|
2376 * We send the "close notify" shutdown alert to the upstream only
|
|
2377 * and do not wait its "close notify" shutdown alert.
|
|
2378 * It is acceptable according to the TLS standard.
|
|
2379 */
|
|
2380
|
|
2381 u->peer.connection->ssl->no_wait_shutdown = 1;
|
|
2382
|
|
2383 (void) ngx_ssl_shutdown(u->peer.connection);
|
126
|
2384 }
|
|
2385 #endif
|
260
|
2386
|
|
2387 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2388 "close http upstream connection: %d",
|
|
2389 u->peer.connection->fd);
|
|
2390
|
28
|
2391 ngx_close_connection(u->peer.connection);
|
|
2392 }
|
|
2393
|
32
|
2394 u->peer.connection = NULL;
|
|
2395
|
112
|
2396 if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
|
28
|
2397 {
|
|
2398 rc = 0;
|
|
2399 }
|
|
2400
|
130
|
2401 if (u->pipe && u->pipe->temp_file) {
|
28
|
2402 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2403 "http upstream temp fd: %d",
|
130
|
2404 u->pipe->temp_file->file.fd);
|
28
|
2405 }
|
|
2406
|
|
2407 #if 0
|
|
2408 if (u->cache) {
|
|
2409 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
32
|
2410 "http upstream cache fd: %d",
|
28
|
2411 u->cache->ctx.file.fd);
|
|
2412 }
|
|
2413 #endif
|
|
2414
|
64
|
2415 if (rc == NGX_DECLINED) {
|
|
2416 return;
|
|
2417 }
|
|
2418
|
32
|
2419 r->connection->log->action = "sending to client";
|
|
2420
|
314
|
2421 if (rc == 0) {
|
|
2422 if (r == r->main) {
|
|
2423 if (!r->post_action) {
|
|
2424 rc = ngx_http_send_special(r, NGX_HTTP_LAST);
|
|
2425 }
|
|
2426
|
|
2427 } else {
|
|
2428 if (r->out) {
|
|
2429 rc = NGX_AGAIN;
|
|
2430 }
|
|
2431 }
|
28
|
2432 }
|
|
2433
|
|
2434 ngx_http_finalize_request(r, rc);
|
|
2435 }
|
|
2436
|
|
2437
|
58
|
2438 static ngx_int_t
|
|
2439 ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2440 ngx_uint_t offset)
|
126
|
2441 {
|
58
|
2442 ngx_table_elt_t **ph;
|
126
|
2443
|
58
|
2444 ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset);
|
|
2445
|
|
2446 if (*ph == NULL) {
|
|
2447 *ph = h;
|
|
2448 }
|
|
2449
|
|
2450 return NGX_OK;
|
|
2451 }
|
|
2452
|
|
2453
|
|
2454 static ngx_int_t
|
|
2455 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r,
|
|
2456 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
2457 {
|
|
2458 ngx_array_t *pa;
|
|
2459 ngx_table_elt_t **ph;
|
|
2460
|
|
2461 pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset);
|
|
2462
|
|
2463 if (pa->elts == NULL) {
|
|
2464 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
|
|
2465 {
|
|
2466 return NGX_ERROR;
|
|
2467 }
|
|
2468 }
|
|
2469
|
|
2470 ph = ngx_array_push(pa);
|
|
2471 if (ph == NULL) {
|
|
2472 return NGX_ERROR;
|
|
2473 }
|
|
2474
|
|
2475 *ph = h;
|
|
2476
|
|
2477 return NGX_OK;
|
|
2478 }
|
|
2479
|
|
2480
|
|
2481 static ngx_int_t
|
|
2482 ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2483 ngx_uint_t offset)
|
|
2484 {
|
|
2485 return NGX_OK;
|
|
2486 }
|
|
2487
|
|
2488
|
|
2489 static ngx_int_t
|
76
|
2490 ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2491 ngx_uint_t offset)
|
126
|
2492 {
|
76
|
2493 ngx_int_t n;
|
|
2494
|
|
2495 r->upstream->headers_in.x_accel_limit_rate = h;
|
|
2496
|
|
2497 n = ngx_atoi(h->value.data, h->value.len);
|
|
2498
|
|
2499 if (n != NGX_ERROR) {
|
|
2500 r->limit_rate = (size_t) n;
|
|
2501 }
|
|
2502
|
|
2503 return NGX_OK;
|
|
2504 }
|
|
2505
|
|
2506
|
|
2507 static ngx_int_t
|
198
|
2508 ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2509 ngx_uint_t offset)
|
|
2510 {
|
|
2511 u_char c0, c1, c2;
|
|
2512
|
|
2513 if (r->upstream->conf->change_buffering) {
|
|
2514
|
|
2515 if (h->value.len == 2) {
|
|
2516 c0 = ngx_tolower(h->value.data[0]);
|
|
2517 c1 = ngx_tolower(h->value.data[1]);
|
|
2518
|
|
2519 if (c0 == 'n' && c1 == 'o') {
|
|
2520 r->upstream->buffering = 0;
|
|
2521 }
|
|
2522
|
|
2523 } else if (h->value.len == 3) {
|
|
2524 c0 = ngx_tolower(h->value.data[0]);
|
|
2525 c1 = ngx_tolower(h->value.data[1]);
|
|
2526 c2 = ngx_tolower(h->value.data[2]);
|
|
2527
|
|
2528 if (c0 == 'y' && c1 == 'e' && c2 == 's') {
|
|
2529 r->upstream->buffering = 1;
|
|
2530 }
|
|
2531 }
|
|
2532 }
|
|
2533
|
|
2534 return NGX_OK;
|
|
2535 }
|
|
2536
|
|
2537
|
|
2538 static ngx_int_t
|
206
|
2539 ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2540 ngx_uint_t offset)
|
|
2541 {
|
|
2542 r->headers_out.override_charset = &h->value;
|
|
2543
|
|
2544 return NGX_OK;
|
|
2545 }
|
|
2546
|
|
2547
|
|
2548 static ngx_int_t
|
58
|
2549 ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2550 ngx_uint_t offset)
|
|
2551 {
|
122
|
2552 ngx_table_elt_t *ho, **ph;
|
58
|
2553
|
|
2554 ho = ngx_list_push(&r->headers_out.headers);
|
|
2555 if (ho == NULL) {
|
|
2556 return NGX_ERROR;
|
|
2557 }
|
|
2558
|
|
2559 *ho = *h;
|
|
2560
|
122
|
2561 if (offset) {
|
|
2562 ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset);
|
|
2563 *ph = ho;
|
|
2564 }
|
|
2565
|
58
|
2566 return NGX_OK;
|
|
2567 }
|
|
2568
|
|
2569
|
|
2570 static ngx_int_t
|
|
2571 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r,
|
|
2572 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
2573 {
|
|
2574 ngx_array_t *pa;
|
|
2575 ngx_table_elt_t *ho, **ph;
|
|
2576
|
|
2577 pa = (ngx_array_t *) ((char *) &r->headers_out + offset);
|
|
2578
|
|
2579 if (pa->elts == NULL) {
|
|
2580 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
|
|
2581 {
|
|
2582 return NGX_ERROR;
|
|
2583 }
|
|
2584 }
|
|
2585
|
|
2586 ph = ngx_array_push(pa);
|
|
2587 if (ph == NULL) {
|
|
2588 return NGX_ERROR;
|
|
2589 }
|
|
2590
|
|
2591 ho = ngx_list_push(&r->headers_out.headers);
|
|
2592 if (ho == NULL) {
|
|
2593 return NGX_ERROR;
|
|
2594 }
|
|
2595
|
|
2596 *ho = *h;
|
|
2597 *ph = ho;
|
|
2598
|
|
2599 return NGX_OK;
|
|
2600 }
|
|
2601
|
|
2602
|
|
2603 static ngx_int_t
|
|
2604 ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2605 ngx_uint_t offset)
|
|
2606 {
|
206
|
2607 u_char *p, *last;
|
|
2608
|
|
2609 r->headers_out.content_type_len = h->value.len;
|
58
|
2610 r->headers_out.content_type = h->value;
|
|
2611
|
206
|
2612 for (p = h->value.data; *p; p++) {
|
|
2613
|
|
2614 if (*p != ';') {
|
|
2615 continue;
|
|
2616 }
|
|
2617
|
|
2618 last = p;
|
|
2619
|
|
2620 while (*++p == ' ') { /* void */ }
|
|
2621
|
372
|
2622 if (*p == '\0') {
|
|
2623 return NGX_OK;
|
|
2624 }
|
|
2625
|
286
|
2626 if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) {
|
206
|
2627 continue;
|
|
2628 }
|
|
2629
|
|
2630 p += 8;
|
|
2631
|
|
2632 r->headers_out.content_type_len = last - h->value.data;
|
|
2633
|
378
|
2634 if (*p == '"') {
|
|
2635 p++;
|
|
2636 }
|
|
2637
|
|
2638 last = h->value.data + h->value.len;
|
|
2639
|
|
2640 if (*(last - 1) == '"') {
|
|
2641 last--;
|
|
2642 }
|
|
2643
|
|
2644 r->headers_out.charset.len = last - p;
|
206
|
2645 r->headers_out.charset.data = p;
|
292
|
2646
|
|
2647 return NGX_OK;
|
206
|
2648 }
|
|
2649
|
58
|
2650 return NGX_OK;
|
|
2651 }
|
|
2652
|
|
2653
|
|
2654 static ngx_int_t
|
|
2655 ngx_http_upstream_copy_content_length(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2656 ngx_uint_t offset)
|
|
2657 {
|
|
2658 ngx_table_elt_t *ho;
|
|
2659
|
|
2660 ho = ngx_list_push(&r->headers_out.headers);
|
|
2661 if (ho == NULL) {
|
|
2662 return NGX_ERROR;
|
|
2663 }
|
|
2664
|
|
2665 *ho = *h;
|
|
2666
|
|
2667 r->headers_out.content_length = ho;
|
|
2668 r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len);
|
|
2669
|
|
2670 return NGX_OK;
|
|
2671 }
|
|
2672
|
|
2673
|
|
2674 static ngx_int_t
|
|
2675 ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2676 ngx_uint_t offset)
|
|
2677 {
|
|
2678 ngx_int_t rc;
|
|
2679 ngx_table_elt_t *ho;
|
|
2680
|
|
2681 ho = ngx_list_push(&r->headers_out.headers);
|
|
2682 if (ho == NULL) {
|
|
2683 return NGX_ERROR;
|
|
2684 }
|
|
2685
|
|
2686 *ho = *h;
|
|
2687
|
|
2688 if (r->upstream->rewrite_redirect) {
|
|
2689 rc = r->upstream->rewrite_redirect(r, ho, 0);
|
|
2690
|
78
|
2691 if (rc == NGX_DECLINED) {
|
|
2692 return NGX_OK;
|
|
2693 }
|
|
2694
|
58
|
2695 if (rc == NGX_OK) {
|
|
2696 r->headers_out.location = ho;
|
|
2697
|
|
2698 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2699 "rewritten location: \"%V\"", &ho->value);
|
|
2700 }
|
|
2701
|
|
2702 return rc;
|
|
2703 }
|
|
2704
|
348
|
2705 if (ho->value.data[0] != '/') {
|
|
2706 r->headers_out.location = ho;
|
|
2707 }
|
|
2708
|
58
|
2709 /*
|
|
2710 * we do not set r->headers_out.location here to avoid the handling
|
|
2711 * the local redirects without a host name by ngx_http_header_filter()
|
|
2712 */
|
|
2713
|
|
2714 return NGX_OK;
|
|
2715 }
|
|
2716
|
|
2717
|
|
2718 static ngx_int_t
|
|
2719 ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2720 ngx_uint_t offset)
|
|
2721 {
|
|
2722 u_char *p;
|
|
2723 ngx_int_t rc;
|
|
2724 ngx_table_elt_t *ho;
|
|
2725
|
|
2726 ho = ngx_list_push(&r->headers_out.headers);
|
|
2727 if (ho == NULL) {
|
|
2728 return NGX_ERROR;
|
|
2729 }
|
|
2730
|
|
2731 *ho = *h;
|
|
2732
|
|
2733 if (r->upstream->rewrite_redirect) {
|
|
2734
|
340
|
2735 p = ngx_strcasestrn(ho->value.data, "url=", 4 - 1);
|
58
|
2736
|
|
2737 if (p) {
|
|
2738 rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data);
|
|
2739
|
|
2740 } else {
|
|
2741 return NGX_OK;
|
|
2742 }
|
|
2743
|
78
|
2744 if (rc == NGX_DECLINED) {
|
|
2745 return NGX_OK;
|
|
2746 }
|
|
2747
|
58
|
2748 if (rc == NGX_OK) {
|
348
|
2749 r->headers_out.refresh = ho;
|
|
2750
|
58
|
2751 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2752 "rewritten refresh: \"%V\"", &ho->value);
|
|
2753 }
|
|
2754
|
|
2755 return rc;
|
|
2756 }
|
|
2757
|
348
|
2758 r->headers_out.refresh = ho;
|
|
2759
|
58
|
2760 return NGX_OK;
|
|
2761 }
|
|
2762
|
|
2763
|
|
2764 #if (NGX_HTTP_GZIP)
|
|
2765
|
|
2766 static ngx_int_t
|
|
2767 ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r,
|
|
2768 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
2769 {
|
|
2770 ngx_table_elt_t *ho;
|
|
2771
|
|
2772 ho = ngx_list_push(&r->headers_out.headers);
|
|
2773 if (ho == NULL) {
|
|
2774 return NGX_ERROR;
|
|
2775 }
|
|
2776
|
|
2777 *ho = *h;
|
|
2778
|
|
2779 r->headers_out.content_encoding = ho;
|
|
2780
|
|
2781 return NGX_OK;
|
|
2782 }
|
|
2783
|
|
2784 #endif
|
|
2785
|
|
2786
|
36
|
2787 static ngx_int_t
|
122
|
2788 ngx_http_upstream_add_variables(ngx_conf_t *cf)
|
28
|
2789 {
|
260
|
2790 ngx_http_variable_t *var, *v;
|
28
|
2791
|
122
|
2792 for (v = ngx_http_upstream_vars; v->name.len; v++) {
|
|
2793 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
2794 if (var == NULL) {
|
|
2795 return NGX_ERROR;
|
|
2796 }
|
|
2797
|
186
|
2798 var->get_handler = v->get_handler;
|
122
|
2799 var->data = v->data;
|
|
2800 }
|
|
2801
|
28
|
2802 return NGX_OK;
|
|
2803 }
|
58
|
2804
|
|
2805
|
122
|
2806 static ngx_int_t
|
298
|
2807 ngx_http_upstream_addr_variable(ngx_http_request_t *r,
|
|
2808 ngx_http_variable_value_t *v, uintptr_t data)
|
|
2809 {
|
|
2810 u_char *p;
|
|
2811 size_t len;
|
|
2812 ngx_uint_t i;
|
|
2813 ngx_http_upstream_state_t *state;
|
|
2814
|
|
2815 v->valid = 1;
|
340
|
2816 v->no_cacheable = 0;
|
298
|
2817 v->not_found = 0;
|
|
2818
|
|
2819 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
|
|
2820 v->not_found = 1;
|
|
2821 return NGX_OK;
|
|
2822 }
|
|
2823
|
|
2824 len = 0;
|
|
2825 state = r->upstream_states->elts;
|
|
2826
|
|
2827 for (i = 0; i < r->upstream_states->nelts; i++) {
|
|
2828 if (state[i].peer) {
|
|
2829 len += state[i].peer->len + 2;
|
|
2830
|
|
2831 } else {
|
|
2832 len += 3;
|
|
2833 }
|
|
2834 }
|
|
2835
|
|
2836 p = ngx_palloc(r->pool, len);
|
|
2837 if (p == NULL) {
|
|
2838 return NGX_ERROR;
|
|
2839 }
|
|
2840
|
|
2841 v->data = p;
|
|
2842
|
|
2843 i = 0;
|
|
2844
|
|
2845 for ( ;; ) {
|
|
2846 if (state[i].peer) {
|
|
2847 p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len);
|
|
2848 }
|
|
2849
|
|
2850 if (++i == r->upstream_states->nelts) {
|
|
2851 break;
|
|
2852 }
|
|
2853
|
|
2854 if (state[i].peer) {
|
|
2855 *p++ = ',';
|
|
2856 *p++ = ' ';
|
|
2857
|
|
2858 } else {
|
|
2859 *p++ = ' ';
|
|
2860 *p++ = ':';
|
|
2861 *p++ = ' ';
|
|
2862
|
|
2863 if (++i == r->upstream_states->nelts) {
|
|
2864 break;
|
|
2865 }
|
|
2866
|
|
2867 continue;
|
|
2868 }
|
|
2869 }
|
|
2870
|
|
2871 v->len = p - v->data;
|
|
2872
|
|
2873 return NGX_OK;
|
|
2874 }
|
|
2875
|
|
2876
|
|
2877 static ngx_int_t
|
122
|
2878 ngx_http_upstream_status_variable(ngx_http_request_t *r,
|
|
2879 ngx_http_variable_value_t *v, uintptr_t data)
|
|
2880 {
|
|
2881 u_char *p;
|
|
2882 size_t len;
|
|
2883 ngx_uint_t i;
|
|
2884 ngx_http_upstream_state_t *state;
|
|
2885
|
|
2886 v->valid = 1;
|
340
|
2887 v->no_cacheable = 0;
|
122
|
2888 v->not_found = 0;
|
|
2889
|
296
|
2890 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
|
122
|
2891 v->not_found = 1;
|
|
2892 return NGX_OK;
|
|
2893 }
|
|
2894
|
296
|
2895 len = r->upstream_states->nelts * (3 + 2);
|
122
|
2896
|
|
2897 p = ngx_palloc(r->pool, len);
|
|
2898 if (p == NULL) {
|
|
2899 return NGX_ERROR;
|
|
2900 }
|
|
2901
|
|
2902 v->data = p;
|
|
2903
|
|
2904 i = 0;
|
296
|
2905 state = r->upstream_states->elts;
|
122
|
2906
|
|
2907 for ( ;; ) {
|
296
|
2908 if (state[i].status) {
|
|
2909 p = ngx_sprintf(p, "%ui", state[i].status);
|
122
|
2910
|
|
2911 } else {
|
296
|
2912 *p++ = '-';
|
122
|
2913 }
|
|
2914
|
296
|
2915 if (++i == r->upstream_states->nelts) {
|
122
|
2916 break;
|
|
2917 }
|
|
2918
|
296
|
2919 if (state[i].peer) {
|
|
2920 *p++ = ',';
|
|
2921 *p++ = ' ';
|
|
2922
|
|
2923 } else {
|
|
2924 *p++ = ' ';
|
|
2925 *p++ = ':';
|
|
2926 *p++ = ' ';
|
|
2927
|
|
2928 if (++i == r->upstream_states->nelts) {
|
|
2929 break;
|
|
2930 }
|
|
2931
|
|
2932 continue;
|
|
2933 }
|
122
|
2934 }
|
|
2935
|
|
2936 v->len = p - v->data;
|
|
2937
|
|
2938 return NGX_OK;
|
|
2939 }
|
|
2940
|
|
2941
|
|
2942 static ngx_int_t
|
|
2943 ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
|
|
2944 ngx_http_variable_value_t *v, uintptr_t data)
|
|
2945 {
|
|
2946 u_char *p;
|
|
2947 size_t len;
|
|
2948 ngx_uint_t i;
|
262
|
2949 ngx_msec_int_t ms;
|
122
|
2950 ngx_http_upstream_state_t *state;
|
|
2951
|
|
2952 v->valid = 1;
|
340
|
2953 v->no_cacheable = 0;
|
122
|
2954 v->not_found = 0;
|
|
2955
|
296
|
2956 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
|
122
|
2957 v->not_found = 1;
|
|
2958 return NGX_OK;
|
|
2959 }
|
|
2960
|
296
|
2961 len = r->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2);
|
122
|
2962
|
|
2963 p = ngx_palloc(r->pool, len);
|
|
2964 if (p == NULL) {
|
|
2965 return NGX_ERROR;
|
|
2966 }
|
|
2967
|
|
2968 v->data = p;
|
|
2969
|
|
2970 i = 0;
|
296
|
2971 state = r->upstream_states->elts;
|
122
|
2972
|
|
2973 for ( ;; ) {
|
296
|
2974 if (state[i].status) {
|
346
|
2975 ms = (ngx_msec_int_t)
|
|
2976 (state[i].response_sec * 1000 + state[i].response_msec);
|
262
|
2977 ms = (ms >= 0) ? ms : 0;
|
|
2978 p = ngx_sprintf(p, "%d.%03d", ms / 1000, ms % 1000);
|
296
|
2979
|
|
2980 } else {
|
|
2981 *p++ = '-';
|
122
|
2982 }
|
|
2983
|
296
|
2984 if (++i == r->upstream_states->nelts) {
|
122
|
2985 break;
|
|
2986 }
|
|
2987
|
296
|
2988 if (state[i].peer) {
|
|
2989 *p++ = ',';
|
|
2990 *p++ = ' ';
|
|
2991
|
|
2992 } else {
|
|
2993 *p++ = ' ';
|
|
2994 *p++ = ':';
|
|
2995 *p++ = ' ';
|
|
2996
|
|
2997 if (++i == r->upstream_states->nelts) {
|
|
2998 break;
|
|
2999 }
|
|
3000
|
|
3001 continue;
|
|
3002 }
|
122
|
3003 }
|
|
3004
|
|
3005 v->len = p - v->data;
|
|
3006
|
|
3007 return NGX_OK;
|
|
3008 }
|
|
3009
|
|
3010
|
296
|
3011 ngx_int_t
|
|
3012 ngx_http_upstream_header_variable(ngx_http_request_t *r,
|
|
3013 ngx_http_variable_value_t *v, uintptr_t data)
|
|
3014 {
|
|
3015 if (r->upstream == NULL) {
|
|
3016 v->not_found = 1;
|
|
3017 return NGX_OK;
|
|
3018 }
|
|
3019
|
|
3020 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
|
|
3021 &r->upstream->headers_in.headers.part,
|
|
3022 sizeof("upstream_http_") - 1);
|
|
3023 }
|
|
3024
|
|
3025
|
200
|
3026 static char *
|
|
3027 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
|
|
3028 {
|
260
|
3029 char *rv;
|
|
3030 void *mconf;
|
|
3031 ngx_str_t *value;
|
|
3032 ngx_url_t u;
|
|
3033 ngx_uint_t m;
|
|
3034 ngx_conf_t pcf;
|
|
3035 ngx_http_module_t *module;
|
|
3036 ngx_http_conf_ctx_t *ctx, *http_ctx;
|
|
3037 ngx_http_upstream_srv_conf_t *uscf;
|
|
3038
|
|
3039 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
3040
|
|
3041 value = cf->args->elts;
|
|
3042 u.host = value[1];
|
|
3043 u.no_resolve = 1;
|
|
3044
|
|
3045 uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE
|
|
3046 |NGX_HTTP_UPSTREAM_WEIGHT
|
|
3047 |NGX_HTTP_UPSTREAM_MAX_FAILS
|
|
3048 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
|
|
3049 |NGX_HTTP_UPSTREAM_DOWN
|
|
3050 |NGX_HTTP_UPSTREAM_BACKUP);
|
|
3051 if (uscf == NULL) {
|
|
3052 return NGX_CONF_ERROR;
|
|
3053 }
|
|
3054
|
200
|
3055
|
|
3056 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
|
|
3057 if (ctx == NULL) {
|
|
3058 return NGX_CONF_ERROR;
|
|
3059 }
|
|
3060
|
260
|
3061 http_ctx = cf->ctx;
|
|
3062 ctx->main_conf = http_ctx->main_conf;
|
200
|
3063
|
|
3064 /* the upstream{}'s srv_conf */
|
|
3065
|
|
3066 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
3067 if (ctx->srv_conf == NULL) {
|
|
3068 return NGX_CONF_ERROR;
|
|
3069 }
|
|
3070
|
|
3071 ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf;
|
|
3072
|
260
|
3073 uscf->srv_conf = ctx->srv_conf;
|
|
3074
|
200
|
3075
|
|
3076 /* the upstream{}'s loc_conf */
|
|
3077
|
|
3078 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
3079 if (ctx->loc_conf == NULL) {
|
|
3080 return NGX_CONF_ERROR;
|
|
3081 }
|
|
3082
|
|
3083 for (m = 0; ngx_modules[m]; m++) {
|
|
3084 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
3085 continue;
|
|
3086 }
|
|
3087
|
|
3088 module = ngx_modules[m]->ctx;
|
|
3089
|
260
|
3090 if (module->create_srv_conf) {
|
|
3091 mconf = module->create_srv_conf(cf);
|
|
3092 if (mconf == NULL) {
|
|
3093 return NGX_CONF_ERROR;
|
|
3094 }
|
|
3095
|
|
3096 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
|
|
3097 }
|
|
3098
|
200
|
3099 if (module->create_loc_conf) {
|
|
3100 mconf = module->create_loc_conf(cf);
|
|
3101 if (mconf == NULL) {
|
|
3102 return NGX_CONF_ERROR;
|
|
3103 }
|
|
3104
|
|
3105 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf;
|
|
3106 }
|
|
3107 }
|
|
3108
|
|
3109
|
|
3110 /* parse inside upstream{} */
|
|
3111
|
|
3112 pcf = *cf;
|
|
3113 cf->ctx = ctx;
|
|
3114 cf->cmd_type = NGX_HTTP_UPS_CONF;
|
|
3115
|
|
3116 rv = ngx_conf_parse(cf, NULL);
|
|
3117
|
|
3118 *cf = pcf;
|
|
3119
|
|
3120 if (rv != NGX_CONF_OK) {
|
|
3121 return rv;
|
|
3122 }
|
|
3123
|
|
3124 if (uscf->servers == NULL) {
|
|
3125 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3126 "no servers are inside upstream");
|
|
3127 return NGX_CONF_ERROR;
|
|
3128 }
|
|
3129
|
|
3130 return rv;
|
|
3131 }
|
|
3132
|
|
3133
|
|
3134 static char *
|
|
3135 ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3136 {
|
|
3137 ngx_http_upstream_srv_conf_t *uscf = conf;
|
|
3138
|
260
|
3139 time_t fail_timeout;
|
|
3140 ngx_str_t *value, s;
|
|
3141 ngx_url_t u;
|
|
3142 ngx_int_t weight, max_fails;
|
|
3143 ngx_uint_t i;
|
|
3144 ngx_http_upstream_server_t *us;
|
200
|
3145
|
|
3146 if (uscf->servers == NULL) {
|
260
|
3147 uscf->servers = ngx_array_create(cf->pool, 4,
|
|
3148 sizeof(ngx_http_upstream_server_t));
|
200
|
3149 if (uscf->servers == NULL) {
|
|
3150 return NGX_CONF_ERROR;
|
|
3151 }
|
|
3152 }
|
|
3153
|
260
|
3154 us = ngx_array_push(uscf->servers);
|
|
3155 if (us == NULL) {
|
200
|
3156 return NGX_CONF_ERROR;
|
|
3157 }
|
|
3158
|
260
|
3159 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
|
|
3160
|
200
|
3161 value = cf->args->elts;
|
|
3162
|
|
3163 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
3164
|
|
3165 u.url = value[1];
|
266
|
3166 u.default_port = 80;
|
200
|
3167
|
340
|
3168 if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
|
200
|
3169 if (u.err) {
|
|
3170 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3171 "%s in upstream \"%V\"", u.err, &u.url);
|
|
3172 }
|
|
3173
|
|
3174 return NGX_CONF_ERROR;
|
|
3175 }
|
|
3176
|
212
|
3177 weight = 1;
|
260
|
3178 max_fails = 1;
|
|
3179 fail_timeout = 10;
|
|
3180
|
|
3181 for (i = 2; i < cf->args->nelts; i++) {
|
|
3182
|
|
3183 if (ngx_strncmp(value[i].data, "weight=", 7) == 0) {
|
|
3184
|
|
3185 if (!(uscf->flags & NGX_HTTP_UPSTREAM_WEIGHT)) {
|
|
3186 goto invalid;
|
|
3187 }
|
|
3188
|
|
3189 weight = ngx_atoi(&value[i].data[7], value[i].len - 7);
|
212
|
3190
|
|
3191 if (weight == NGX_ERROR || weight == 0) {
|
|
3192 goto invalid;
|
|
3193 }
|
|
3194
|
260
|
3195 continue;
|
|
3196 }
|
|
3197
|
|
3198 if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) {
|
|
3199
|
|
3200 if (!(uscf->flags & NGX_HTTP_UPSTREAM_MAX_FAILS)) {
|
|
3201 goto invalid;
|
|
3202 }
|
|
3203
|
|
3204 max_fails = ngx_atoi(&value[i].data[10], value[i].len - 10);
|
|
3205
|
|
3206 if (max_fails == NGX_ERROR) {
|
|
3207 goto invalid;
|
|
3208 }
|
|
3209
|
|
3210 continue;
|
212
|
3211 }
|
260
|
3212
|
|
3213 if (ngx_strncmp(value[i].data, "fail_timeout=", 13) == 0) {
|
|
3214
|
|
3215 if (!(uscf->flags & NGX_HTTP_UPSTREAM_FAIL_TIMEOUT)) {
|
|
3216 goto invalid;
|
|
3217 }
|
|
3218
|
|
3219 s.len = value[i].len - 13;
|
|
3220 s.data = &value[i].data[13];
|
|
3221
|
|
3222 fail_timeout = ngx_parse_time(&s, 1);
|
|
3223
|
372
|
3224 if (fail_timeout == NGX_ERROR) {
|
260
|
3225 goto invalid;
|
|
3226 }
|
|
3227
|
|
3228 continue;
|
|
3229 }
|
|
3230
|
326
|
3231 if (ngx_strncmp(value[i].data, "backup", 6) == 0) {
|
|
3232
|
|
3233 if (!(uscf->flags & NGX_HTTP_UPSTREAM_BACKUP)) {
|
|
3234 goto invalid;
|
|
3235 }
|
|
3236
|
|
3237 us->backup = 1;
|
|
3238
|
|
3239 continue;
|
|
3240 }
|
|
3241
|
260
|
3242 if (ngx_strncmp(value[i].data, "down", 4) == 0) {
|
|
3243
|
|
3244 if (!(uscf->flags & NGX_HTTP_UPSTREAM_DOWN)) {
|
|
3245 goto invalid;
|
|
3246 }
|
|
3247
|
|
3248 us->down = 1;
|
|
3249
|
|
3250 continue;
|
|
3251 }
|
|
3252
|
|
3253 goto invalid;
|
212
|
3254 }
|
|
3255
|
260
|
3256 us->addrs = u.addrs;
|
|
3257 us->naddrs = u.naddrs;
|
|
3258 us->weight = weight;
|
|
3259 us->max_fails = max_fails;
|
|
3260 us->fail_timeout = fail_timeout;
|
200
|
3261
|
|
3262 return NGX_CONF_OK;
|
212
|
3263
|
|
3264 invalid:
|
|
3265
|
260
|
3266 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3267 "invalid parameter \"%V\"", &value[i]);
|
212
|
3268
|
|
3269 return NGX_CONF_ERROR;
|
200
|
3270 }
|
|
3271
|
|
3272
|
|
3273 ngx_http_upstream_srv_conf_t *
|
260
|
3274 ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
|
200
|
3275 {
|
|
3276 ngx_uint_t i;
|
260
|
3277 ngx_http_upstream_server_t *us;
|
200
|
3278 ngx_http_upstream_srv_conf_t *uscf, **uscfp;
|
|
3279 ngx_http_upstream_main_conf_t *umcf;
|
|
3280
|
260
|
3281 if (!(flags & NGX_HTTP_UPSTREAM_CREATE)) {
|
|
3282
|
340
|
3283 if (ngx_parse_url(cf->pool, u) != NGX_OK) {
|
200
|
3284 if (u->err) {
|
|
3285 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3286 "%s in upstream \"%V\"", u->err, &u->url);
|
|
3287 }
|
|
3288
|
|
3289 return NULL;
|
|
3290 }
|
|
3291 }
|
|
3292
|
|
3293 umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module);
|
|
3294
|
|
3295 uscfp = umcf->upstreams.elts;
|
|
3296
|
|
3297 for (i = 0; i < umcf->upstreams.nelts; i++) {
|
266
|
3298
|
|
3299 if (uscfp[i]->host.len != u->host.len
|
260
|
3300 || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)
|
|
3301 != 0)
|
|
3302 {
|
200
|
3303 continue;
|
|
3304 }
|
|
3305
|
260
|
3306 if ((flags & NGX_HTTP_UPSTREAM_CREATE)
|
|
3307 && (uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE))
|
200
|
3308 {
|
260
|
3309 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3310 "duplicate upstream \"%V\"", &u->host);
|
|
3311 return NULL;
|
200
|
3312 }
|
260
|
3313
|
266
|
3314 if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) {
|
260
|
3315 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
266
|
3316 "upstream \"%V\" may not have port %d",
|
|
3317 &u->host, u->port);
|
|
3318 return NULL;
|
|
3319 }
|
|
3320
|
|
3321 if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) {
|
|
3322 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
|
3323 "upstream \"%V\" may not have port %d in %s:%ui",
|
|
3324 &u->host, uscfp[i]->port,
|
336
|
3325 uscfp[i]->file_name, uscfp[i]->line);
|
266
|
3326 return NULL;
|
|
3327 }
|
|
3328
|
|
3329 if (uscfp[i]->port != u->port) {
|
|
3330 continue;
|
260
|
3331 }
|
|
3332
|
324
|
3333 if (uscfp[i]->default_port && u->default_port
|
|
3334 && uscfp[i]->default_port != u->default_port)
|
|
3335 {
|
|
3336 continue;
|
|
3337 }
|
|
3338
|
260
|
3339 return uscfp[i];
|
200
|
3340 }
|
|
3341
|
|
3342 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t));
|
|
3343 if (uscf == NULL) {
|
|
3344 return NULL;
|
|
3345 }
|
|
3346
|
260
|
3347 uscf->flags = flags;
|
200
|
3348 uscf->host = u->host;
|
336
|
3349 uscf->file_name = cf->conf_file->file.name.data;
|
200
|
3350 uscf->line = cf->conf_file->line;
|
266
|
3351 uscf->port = u->port;
|
|
3352 uscf->default_port = u->default_port;
|
260
|
3353
|
|
3354 if (u->naddrs == 1) {
|
|
3355 uscf->servers = ngx_array_create(cf->pool, 1,
|
|
3356 sizeof(ngx_http_upstream_server_t));
|
|
3357 if (uscf->servers == NULL) {
|
|
3358 return NGX_CONF_ERROR;
|
|
3359 }
|
|
3360
|
|
3361 us = ngx_array_push(uscf->servers);
|
|
3362 if (us == NULL) {
|
|
3363 return NGX_CONF_ERROR;
|
|
3364 }
|
|
3365
|
|
3366 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
|
|
3367
|
|
3368 us->addrs = u->addrs;
|
|
3369 us->naddrs = u->naddrs;
|
|
3370 }
|
200
|
3371
|
|
3372 uscfp = ngx_array_push(&umcf->upstreams);
|
|
3373 if (uscfp == NULL) {
|
|
3374 return NULL;
|
|
3375 }
|
|
3376
|
|
3377 *uscfp = uscf;
|
|
3378
|
|
3379 return uscf;
|
|
3380 }
|
|
3381
|
|
3382
|
356
|
3383 ngx_int_t
|
|
3384 ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
|
|
3385 ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
|
|
3386 ngx_str_t *default_hide_headers, ngx_hash_init_t *hash)
|
|
3387 {
|
|
3388 ngx_str_t *h;
|
|
3389 ngx_uint_t i, j;
|
|
3390 ngx_array_t hide_headers;
|
|
3391 ngx_hash_key_t *hk;
|
|
3392
|
|
3393 if (conf->hide_headers == NGX_CONF_UNSET_PTR
|
|
3394 && conf->pass_headers == NGX_CONF_UNSET_PTR)
|
|
3395 {
|
|
3396 conf->hide_headers_hash = prev->hide_headers_hash;
|
|
3397
|
|
3398 if (conf->hide_headers_hash.buckets) {
|
|
3399 return NGX_OK;
|
|
3400 }
|
|
3401
|
|
3402 conf->hide_headers = prev->hide_headers;
|
|
3403 conf->pass_headers = prev->pass_headers;
|
|
3404
|
|
3405 } else {
|
|
3406 if (conf->hide_headers == NGX_CONF_UNSET_PTR) {
|
|
3407 conf->hide_headers = prev->hide_headers;
|
|
3408 }
|
|
3409
|
|
3410 if (conf->pass_headers == NGX_CONF_UNSET_PTR) {
|
|
3411 conf->pass_headers = prev->pass_headers;
|
|
3412 }
|
|
3413 }
|
|
3414
|
|
3415 if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
|
|
3416 != NGX_OK)
|
|
3417 {
|
|
3418 return NGX_ERROR;
|
|
3419 }
|
|
3420
|
|
3421 for (h = default_hide_headers; h->len; h++) {
|
|
3422 hk = ngx_array_push(&hide_headers);
|
|
3423 if (hk == NULL) {
|
|
3424 return NGX_ERROR;
|
|
3425 }
|
|
3426
|
|
3427 hk->key = *h;
|
|
3428 hk->key_hash = ngx_hash_key_lc(h->data, h->len);
|
|
3429 hk->value = (void *) 1;
|
|
3430 }
|
|
3431
|
|
3432 if (conf->hide_headers != NGX_CONF_UNSET_PTR) {
|
|
3433
|
|
3434 h = conf->hide_headers->elts;
|
|
3435
|
|
3436 for (i = 0; i < conf->hide_headers->nelts; i++) {
|
|
3437
|
|
3438 hk = hide_headers.elts;
|
|
3439
|
|
3440 for (j = 0; j < hide_headers.nelts; j++) {
|
|
3441 if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {
|
|
3442 goto exist;
|
|
3443 }
|
|
3444 }
|
|
3445
|
|
3446 hk = ngx_array_push(&hide_headers);
|
|
3447 if (hk == NULL) {
|
|
3448 return NGX_ERROR;
|
|
3449 }
|
|
3450
|
|
3451 hk->key = h[i];
|
|
3452 hk->key_hash = ngx_hash_key_lc(h[i].data, h[i].len);
|
|
3453 hk->value = (void *) 1;
|
|
3454
|
|
3455 exist:
|
|
3456
|
|
3457 continue;
|
|
3458 }
|
|
3459 }
|
|
3460
|
|
3461 if (conf->pass_headers != NGX_CONF_UNSET_PTR) {
|
|
3462
|
|
3463 h = conf->pass_headers->elts;
|
|
3464 hk = hide_headers.elts;
|
|
3465
|
|
3466 for (i = 0; i < conf->pass_headers->nelts; i++) {
|
|
3467 for (j = 0; j < hide_headers.nelts; j++) {
|
|
3468
|
|
3469 if (hk[j].key.data == NULL) {
|
|
3470 continue;
|
|
3471 }
|
|
3472
|
|
3473 if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {
|
|
3474 hk[j].key.data = NULL;
|
|
3475 break;
|
|
3476 }
|
|
3477 }
|
|
3478 }
|
|
3479 }
|
|
3480
|
|
3481 hash->hash = &conf->hide_headers_hash;
|
|
3482 hash->key = ngx_hash_key_lc;
|
|
3483 hash->pool = cf->pool;
|
|
3484 hash->temp_pool = NULL;
|
|
3485
|
|
3486 return ngx_hash_init(hash, hide_headers.elts, hide_headers.nelts);
|
|
3487 }
|
|
3488
|
|
3489
|
58
|
3490 static void *
|
|
3491 ngx_http_upstream_create_main_conf(ngx_conf_t *cf)
|
|
3492 {
|
|
3493 ngx_http_upstream_main_conf_t *umcf;
|
|
3494
|
|
3495 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t));
|
|
3496 if (umcf == NULL) {
|
|
3497 return NULL;
|
|
3498 }
|
|
3499
|
200
|
3500 if (ngx_array_init(&umcf->upstreams, cf->pool, 4,
|
|
3501 sizeof(ngx_http_upstream_srv_conf_t *))
|
|
3502 != NGX_OK)
|
|
3503 {
|
|
3504 return NGX_CONF_ERROR;
|
|
3505 }
|
|
3506
|
58
|
3507 return umcf;
|
|
3508 }
|
|
3509
|
|
3510
|
|
3511 static char *
|
200
|
3512 ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf)
|
58
|
3513 {
|
|
3514 ngx_http_upstream_main_conf_t *umcf = conf;
|
|
3515
|
200
|
3516 ngx_uint_t i;
|
|
3517 ngx_array_t headers_in;
|
|
3518 ngx_hash_key_t *hk;
|
|
3519 ngx_hash_init_t hash;
|
260
|
3520 ngx_http_upstream_init_pt init;
|
200
|
3521 ngx_http_upstream_header_t *header;
|
|
3522 ngx_http_upstream_srv_conf_t **uscfp;
|
|
3523
|
|
3524 uscfp = umcf->upstreams.elts;
|
|
3525
|
|
3526 for (i = 0; i < umcf->upstreams.nelts; i++) {
|
260
|
3527
|
|
3528 init = uscfp[i]->peer.init_upstream ? uscfp[i]->peer.init_upstream:
|
|
3529 ngx_http_upstream_init_round_robin;
|
|
3530
|
|
3531 if (init(cf, uscfp[i]) != NGX_OK) {
|
200
|
3532 return NGX_CONF_ERROR;
|
|
3533 }
|
|
3534 }
|
198
|
3535
|
212
|
3536
|
260
|
3537 /* upstream_headers_in_hash */
|
|
3538
|
198
|
3539 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
|
|
3540 != NGX_OK)
|
58
|
3541 {
|
|
3542 return NGX_CONF_ERROR;
|
|
3543 }
|
|
3544
|
198
|
3545 for (header = ngx_http_upstream_headers_in; header->name.len; header++) {
|
|
3546 hk = ngx_array_push(&headers_in);
|
|
3547 if (hk == NULL) {
|
|
3548 return NGX_CONF_ERROR;
|
|
3549 }
|
|
3550
|
|
3551 hk->key = header->name;
|
|
3552 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
|
|
3553 hk->value = header;
|
|
3554 }
|
|
3555
|
|
3556 hash.hash = &umcf->headers_in_hash;
|
|
3557 hash.key = ngx_hash_key_lc;
|
|
3558 hash.max_size = 512;
|
244
|
3559 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
|
198
|
3560 hash.name = "upstream_headers_in_hash";
|
|
3561 hash.pool = cf->pool;
|
|
3562 hash.temp_pool = NULL;
|
|
3563
|
|
3564 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
|
|
3565 return NGX_CONF_ERROR;
|
|
3566 }
|
58
|
3567
|
|
3568 return NGX_CONF_OK;
|
|
3569 }
|