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