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