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