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