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