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