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