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