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) {
|
296
|
1812 if (c->write->pending_eof) {
|
336
|
1813 c->log->action = "connecting to upstream";
|
296
|
1814 (void) ngx_connection_error(c, c->write->kq_errno,
|
|
1815 "kevent() reported that connect() failed");
|
|
1816 return NGX_ERROR;
|
|
1817 }
|
294
|
1818
|
|
1819 } else
|
|
1820 #endif
|
|
1821 {
|
296
|
1822 err = 0;
|
|
1823 len = sizeof(int);
|
|
1824
|
|
1825 /*
|
|
1826 * BSDs and Linux return 0 and set a pending error in err
|
|
1827 * Solaris returns -1 and sets errno
|
|
1828 */
|
|
1829
|
|
1830 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len)
|
|
1831 == -1)
|
|
1832 {
|
|
1833 err = ngx_errno;
|
|
1834 }
|
|
1835
|
|
1836 if (err) {
|
336
|
1837 c->log->action = "connecting to upstream";
|
296
|
1838 (void) ngx_connection_error(c, err, "connect() failed");
|
|
1839 return NGX_ERROR;
|
|
1840 }
|
294
|
1841 }
|
|
1842
|
|
1843 return NGX_OK;
|
|
1844 }
|
|
1845
|
|
1846
|
464
|
1847 static ngx_int_t
|
|
1848 ngx_http_upstream_process_headers(ngx_http_request_t *r, ngx_http_upstream_t *u)
|
|
1849 {
|
|
1850 ngx_str_t *uri, args;
|
|
1851 ngx_uint_t i, flags;
|
|
1852 ngx_list_part_t *part;
|
|
1853 ngx_table_elt_t *h;
|
|
1854 ngx_http_upstream_header_t *hh;
|
|
1855 ngx_http_upstream_main_conf_t *umcf;
|
|
1856
|
|
1857 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
|
|
1858
|
484
|
1859 if (u->headers_in.x_accel_redirect
|
|
1860 && !(u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT))
|
|
1861 {
|
464
|
1862 ngx_http_upstream_finalize_request(r, u, NGX_DECLINED);
|
|
1863
|
|
1864 part = &u->headers_in.headers.part;
|
|
1865 h = part->elts;
|
|
1866
|
|
1867 for (i = 0; /* void */; i++) {
|
|
1868
|
|
1869 if (i >= part->nelts) {
|
|
1870 if (part->next == NULL) {
|
|
1871 break;
|
|
1872 }
|
|
1873
|
|
1874 part = part->next;
|
|
1875 h = part->elts;
|
|
1876 i = 0;
|
|
1877 }
|
|
1878
|
|
1879 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
|
|
1880 h[i].lowcase_key, h[i].key.len);
|
|
1881
|
|
1882 if (hh && hh->redirect) {
|
|
1883 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
|
|
1884 ngx_http_finalize_request(r,
|
|
1885 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1886 return NGX_DONE;
|
|
1887 }
|
|
1888 }
|
|
1889 }
|
|
1890
|
|
1891 uri = &u->headers_in.x_accel_redirect->value;
|
570
|
1892 ngx_str_null(&args);
|
530
|
1893 flags = NGX_HTTP_LOG_UNSAFE;
|
464
|
1894
|
|
1895 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
|
|
1896 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
|
|
1897 return NGX_DONE;
|
|
1898 }
|
|
1899
|
|
1900 if (r->method != NGX_HTTP_HEAD) {
|
|
1901 r->method = NGX_HTTP_GET;
|
|
1902 }
|
|
1903
|
|
1904 ngx_http_internal_redirect(r, uri, &args);
|
526
|
1905 ngx_http_finalize_request(r, NGX_DONE);
|
464
|
1906 return NGX_DONE;
|
|
1907 }
|
|
1908
|
|
1909 part = &u->headers_in.headers.part;
|
|
1910 h = part->elts;
|
|
1911
|
|
1912 for (i = 0; /* void */; i++) {
|
|
1913
|
|
1914 if (i >= part->nelts) {
|
|
1915 if (part->next == NULL) {
|
|
1916 break;
|
|
1917 }
|
|
1918
|
|
1919 part = part->next;
|
|
1920 h = part->elts;
|
|
1921 i = 0;
|
|
1922 }
|
|
1923
|
|
1924 if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash,
|
|
1925 h[i].lowcase_key, h[i].key.len))
|
|
1926 {
|
|
1927 continue;
|
|
1928 }
|
|
1929
|
|
1930 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
|
|
1931 h[i].lowcase_key, h[i].key.len);
|
|
1932
|
|
1933 if (hh) {
|
|
1934 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
|
|
1935 ngx_http_upstream_finalize_request(r, u,
|
|
1936 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1937 return NGX_DONE;
|
|
1938 }
|
|
1939
|
|
1940 continue;
|
|
1941 }
|
|
1942
|
|
1943 if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) {
|
|
1944 ngx_http_upstream_finalize_request(r, u,
|
|
1945 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1946 return NGX_DONE;
|
|
1947 }
|
|
1948 }
|
|
1949
|
|
1950 if (r->headers_out.server && r->headers_out.server->value.data == NULL) {
|
|
1951 r->headers_out.server->hash = 0;
|
|
1952 }
|
|
1953
|
|
1954 if (r->headers_out.date && r->headers_out.date->value.data == NULL) {
|
|
1955 r->headers_out.date->hash = 0;
|
|
1956 }
|
|
1957
|
|
1958 r->headers_out.status = u->headers_in.status_n;
|
|
1959 r->headers_out.status_line = u->headers_in.status_line;
|
|
1960
|
640
|
1961 r->headers_out.content_length_n = u->headers_in.content_length_n;
|
|
1962
|
|
1963 u->length = u->headers_in.content_length_n;
|
464
|
1964
|
|
1965 return NGX_OK;
|
|
1966 }
|
|
1967
|
|
1968
|
248
|
1969 static void
|
430
|
1970 ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r,
|
|
1971 ngx_http_upstream_t *u)
|
248
|
1972 {
|
430
|
1973 size_t size;
|
|
1974 ssize_t n;
|
|
1975 ngx_buf_t *b;
|
|
1976 ngx_event_t *rev;
|
|
1977 ngx_connection_t *c;
|
|
1978
|
|
1979 c = u->peer.connection;
|
|
1980 rev = c->read;
|
248
|
1981
|
|
1982 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1983 "http upstream process body on memory");
|
|
1984
|
|
1985 if (rev->timedout) {
|
|
1986 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
|
|
1987 ngx_http_upstream_finalize_request(r, u, NGX_ETIMEDOUT);
|
|
1988 return;
|
|
1989 }
|
|
1990
|
|
1991 b = &u->buffer;
|
|
1992
|
|
1993 for ( ;; ) {
|
|
1994
|
|
1995 size = b->end - b->last;
|
|
1996
|
|
1997 if (size == 0) {
|
|
1998 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
590
|
1999 "upstream buffer is too small to read response");
|
248
|
2000 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
|
|
2001 return;
|
|
2002 }
|
|
2003
|
|
2004 n = c->recv(c, b->last, size);
|
|
2005
|
|
2006 if (n == NGX_AGAIN) {
|
|
2007 break;
|
|
2008 }
|
|
2009
|
|
2010 if (n == 0 || n == NGX_ERROR) {
|
|
2011 ngx_http_upstream_finalize_request(r, u, n);
|
|
2012 return;
|
|
2013 }
|
|
2014
|
430
|
2015 u->state->response_length += n;
|
|
2016
|
248
|
2017 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
|
|
2018 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
|
|
2019 return;
|
|
2020 }
|
|
2021
|
|
2022 if (!rev->ready) {
|
|
2023 break;
|
|
2024 }
|
|
2025 }
|
|
2026
|
640
|
2027 if (u->length == 0) {
|
|
2028 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2029 return;
|
|
2030 }
|
|
2031
|
430
|
2032 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
248
|
2033 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
|
|
2034 return;
|
|
2035 }
|
|
2036
|
|
2037 if (rev->active) {
|
|
2038 ngx_add_timer(rev, u->conf->read_timeout);
|
|
2039
|
|
2040 } else if (rev->timer_set) {
|
|
2041 ngx_del_timer(rev);
|
|
2042 }
|
230
|
2043 }
|
|
2044
|
|
2045
|
|
2046 static void
|
|
2047 ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
|
|
2048 {
|
|
2049 int tcp_nodelay;
|
430
|
2050 ssize_t n;
|
230
|
2051 ngx_int_t rc;
|
|
2052 ngx_event_pipe_t *p;
|
|
2053 ngx_connection_t *c;
|
|
2054 ngx_http_core_loc_conf_t *clcf;
|
|
2055
|
58
|
2056 rc = ngx_http_send_header(r);
|
28
|
2057
|
480
|
2058 if (rc == NGX_ERROR || rc > NGX_OK || r->post_action) {
|
34
|
2059 ngx_http_upstream_finalize_request(r, u, rc);
|
28
|
2060 return;
|
|
2061 }
|
|
2062
|
480
|
2063 c = r->connection;
|
|
2064
|
|
2065 if (r->header_only) {
|
|
2066
|
|
2067 if (u->cacheable || u->store) {
|
|
2068
|
|
2069 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
|
|
2070 ngx_connection_error(c, ngx_socket_errno,
|
|
2071 ngx_shutdown_socket_n " failed");
|
|
2072 }
|
|
2073
|
|
2074 r->read_event_handler = ngx_http_request_empty_handler;
|
|
2075 r->write_event_handler = ngx_http_request_empty_handler;
|
|
2076 c->error = 1;
|
|
2077
|
|
2078 } else {
|
|
2079 ngx_http_upstream_finalize_request(r, u, rc);
|
|
2080 return;
|
|
2081 }
|
|
2082 }
|
|
2083
|
28
|
2084 u->header_sent = 1;
|
|
2085
|
122
|
2086 if (r->request_body && r->request_body->temp_file) {
|
464
|
2087 ngx_pool_run_cleanup_file(r->pool, r->request_body->temp_file->file.fd);
|
|
2088 r->request_body->temp_file->file.fd = NGX_INVALID_FILE;
|
112
|
2089 }
|
|
2090
|
130
|
2091 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2092
|
198
|
2093 if (!u->buffering) {
|
130
|
2094
|
|
2095 if (u->input_filter == NULL) {
|
|
2096 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
|
|
2097 u->input_filter = ngx_http_upstream_non_buffered_filter;
|
|
2098 u->input_filter_ctx = r;
|
|
2099 }
|
|
2100
|
428
|
2101 u->read_event_handler = ngx_http_upstream_process_non_buffered_upstream;
|
130
|
2102 r->write_event_handler =
|
|
2103 ngx_http_upstream_process_non_buffered_downstream;
|
|
2104
|
|
2105 r->limit_rate = 0;
|
|
2106
|
|
2107 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
|
|
2108 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2109 return;
|
|
2110 }
|
|
2111
|
132
|
2112 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) {
|
|
2113 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
|
|
2114
|
|
2115 tcp_nodelay = 1;
|
|
2116
|
|
2117 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
|
|
2118 (const void *) &tcp_nodelay, sizeof(int)) == -1)
|
|
2119 {
|
|
2120 ngx_connection_error(c, ngx_socket_errno,
|
|
2121 "setsockopt(TCP_NODELAY) failed");
|
|
2122 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2123 return;
|
|
2124 }
|
|
2125
|
|
2126 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
|
|
2127 }
|
|
2128
|
430
|
2129 n = u->buffer.last - u->buffer.pos;
|
|
2130
|
|
2131 if (n) {
|
130
|
2132 u->buffer.last = u->buffer.pos;
|
|
2133
|
430
|
2134 u->state->response_length += n;
|
|
2135
|
|
2136 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
|
130
|
2137 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2138 return;
|
|
2139 }
|
|
2140
|
428
|
2141 ngx_http_upstream_process_non_buffered_downstream(r);
|
130
|
2142
|
|
2143 } else {
|
|
2144 u->buffer.pos = u->buffer.start;
|
|
2145 u->buffer.last = u->buffer.start;
|
|
2146
|
|
2147 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) {
|
|
2148 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2149 return;
|
|
2150 }
|
336
|
2151
|
640
|
2152 if (u->peer.connection->read->ready || u->length == 0) {
|
430
|
2153 ngx_http_upstream_process_non_buffered_upstream(r, u);
|
336
|
2154 }
|
130
|
2155 }
|
|
2156
|
|
2157 return;
|
|
2158 }
|
|
2159
|
28
|
2160 /* TODO: preallocate event_pipe bufs, look "Content-Length" */
|
|
2161
|
464
|
2162 #if (NGX_HTTP_CACHE)
|
|
2163
|
|
2164 if (r->cache && r->cache->file.fd != NGX_INVALID_FILE) {
|
|
2165 ngx_pool_run_cleanup_file(r->pool, r->cache->file.fd);
|
|
2166 r->cache->file.fd = NGX_INVALID_FILE;
|
28
|
2167 }
|
|
2168
|
588
|
2169 switch (ngx_http_test_predicates(r, u->conf->no_cache)) {
|
|
2170
|
|
2171 case NGX_ERROR:
|
|
2172 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2173 return;
|
|
2174
|
|
2175 case NGX_DECLINED:
|
|
2176 u->cacheable = 0;
|
|
2177 break;
|
|
2178
|
|
2179 default: /* NGX_OK */
|
|
2180
|
|
2181 if (u->cache_status == NGX_HTTP_CACHE_BYPASS) {
|
|
2182
|
|
2183 r->cache->min_uses = u->conf->cache_min_uses;
|
|
2184 r->cache->body_start = u->conf->buffer_size;
|
|
2185 r->cache->file_cache = u->conf->cache->data;
|
|
2186
|
|
2187 if (ngx_http_file_cache_create(r) != NGX_OK) {
|
|
2188 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2189 return;
|
|
2190 }
|
|
2191 }
|
|
2192
|
|
2193 break;
|
|
2194 }
|
|
2195
|
340
|
2196 if (u->cacheable) {
|
464
|
2197 time_t now, valid;
|
|
2198
|
472
|
2199 now = ngx_time();
|
|
2200
|
|
2201 valid = r->cache->valid_sec;
|
|
2202
|
|
2203 if (valid == 0) {
|
|
2204 valid = ngx_http_file_cache_valid(u->conf->cache_valid,
|
|
2205 u->headers_in.status_n);
|
|
2206 if (valid) {
|
|
2207 r->cache->valid_sec = now + valid;
|
|
2208 }
|
|
2209 }
|
|
2210
|
464
|
2211 if (valid) {
|
|
2212 r->cache->last_modified = r->headers_out.last_modified_time;
|
|
2213 r->cache->date = now;
|
|
2214 r->cache->body_start = (u_short) (u->buffer.pos - u->buffer.start);
|
|
2215
|
|
2216 ngx_http_file_cache_set_header(r, u->buffer.start);
|
|
2217
|
|
2218 } else {
|
|
2219 u->cacheable = 0;
|
480
|
2220 r->headers_out.last_modified_time = -1;
|
464
|
2221 }
|
28
|
2222 }
|
|
2223
|
478
|
2224 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2225 "http cacheable: %d", u->cacheable);
|
|
2226
|
524
|
2227 if (u->cacheable == 0 && r->cache) {
|
590
|
2228 ngx_http_file_cache_free(r->cache, u->pipe->temp_file);
|
524
|
2229 }
|
|
2230
|
28
|
2231 #endif
|
|
2232
|
130
|
2233 p = u->pipe;
|
28
|
2234
|
|
2235 p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter;
|
|
2236 p->output_ctx = r;
|
|
2237 p->tag = u->output.tag;
|
126
|
2238 p->bufs = u->conf->bufs;
|
28
|
2239 p->busy_size = u->conf->busy_buffers_size;
|
|
2240 p->upstream = u->peer.connection;
|
132
|
2241 p->downstream = c;
|
28
|
2242 p->pool = r->pool;
|
132
|
2243 p->log = c->log;
|
126
|
2244
|
340
|
2245 p->cacheable = u->cacheable || u->store;
|
28
|
2246
|
50
|
2247 p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
|
|
2248 if (p->temp_file == NULL) {
|
28
|
2249 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2250 return;
|
|
2251 }
|
|
2252
|
|
2253 p->temp_file->file.fd = NGX_INVALID_FILE;
|
132
|
2254 p->temp_file->file.log = c->log;
|
28
|
2255 p->temp_file->path = u->conf->temp_path;
|
|
2256 p->temp_file->pool = r->pool;
|
126
|
2257
|
464
|
2258 if (p->cacheable) {
|
28
|
2259 p->temp_file->persistent = 1;
|
318
|
2260
|
28
|
2261 } else {
|
216
|
2262 p->temp_file->log_level = NGX_LOG_WARN;
|
28
|
2263 p->temp_file->warn = "an upstream response is buffered "
|
|
2264 "to a temporary file";
|
|
2265 }
|
|
2266
|
|
2267 p->max_temp_file_size = u->conf->max_temp_file_size;
|
|
2268 p->temp_file_write_size = u->conf->temp_file_write_size;
|
|
2269
|
50
|
2270 p->preread_bufs = ngx_alloc_chain_link(r->pool);
|
|
2271 if (p->preread_bufs == NULL) {
|
28
|
2272 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2273 return;
|
|
2274 }
|
318
|
2275
|
130
|
2276 p->preread_bufs->buf = &u->buffer;
|
28
|
2277 p->preread_bufs->next = NULL;
|
130
|
2278 u->buffer.recycled = 1;
|
|
2279
|
|
2280 p->preread_size = u->buffer.last - u->buffer.pos;
|
28
|
2281
|
340
|
2282 if (u->cacheable) {
|
318
|
2283
|
28
|
2284 p->buf_to_file = ngx_calloc_buf(r->pool);
|
|
2285 if (p->buf_to_file == NULL) {
|
|
2286 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2287 return;
|
|
2288 }
|
318
|
2289
|
690
|
2290 p->buf_to_file->start = u->buffer.start;
|
130
|
2291 p->buf_to_file->pos = u->buffer.start;
|
|
2292 p->buf_to_file->last = u->buffer.pos;
|
28
|
2293 p->buf_to_file->temporary = 1;
|
|
2294 }
|
|
2295
|
|
2296 if (ngx_event_flags & NGX_USE_AIO_EVENT) {
|
664
|
2297 /* the posted aio operation may corrupt a shadow buffer */
|
28
|
2298 p->single_buf = 1;
|
|
2299 }
|
|
2300
|
|
2301 /* TODO: p->free_bufs = 0 if use ngx_create_chain_of_bufs() */
|
|
2302 p->free_bufs = 1;
|
|
2303
|
|
2304 /*
|
130
|
2305 * event_pipe would do u->buffer.last += p->preread_size
|
28
|
2306 * as though these bytes were read
|
|
2307 */
|
130
|
2308 u->buffer.last = u->buffer.pos;
|
28
|
2309
|
|
2310 if (u->conf->cyclic_temp_file) {
|
|
2311
|
|
2312 /*
|
|
2313 * we need to disable the use of sendfile() if we use cyclic temp file
|
|
2314 * because the writing a new data may interfere with sendfile()
|
|
2315 * that uses the same kernel file pages (at least on FreeBSD)
|
|
2316 */
|
|
2317
|
|
2318 p->cyclic_temp_file = 1;
|
132
|
2319 c->sendfile = 0;
|
28
|
2320
|
|
2321 } else {
|
|
2322 p->cyclic_temp_file = 0;
|
|
2323 }
|
|
2324
|
|
2325 p->read_timeout = u->conf->read_timeout;
|
|
2326 p->send_timeout = clcf->send_timeout;
|
|
2327 p->send_lowat = clcf->send_lowat;
|
|
2328
|
640
|
2329 p->length = -1;
|
|
2330
|
|
2331 if (u->input_filter_init
|
|
2332 && u->input_filter_init(p->input_ctx) != NGX_OK)
|
|
2333 {
|
|
2334 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2335 return;
|
|
2336 }
|
|
2337
|
428
|
2338 u->read_event_handler = ngx_http_upstream_process_upstream;
|
58
|
2339 r->write_event_handler = ngx_http_upstream_process_downstream;
|
28
|
2340
|
430
|
2341 ngx_http_upstream_process_upstream(r, u);
|
28
|
2342 }
|
|
2343
|
|
2344
|
36
|
2345 static void
|
130
|
2346 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r)
|
|
2347 {
|
428
|
2348 ngx_event_t *wev;
|
|
2349 ngx_connection_t *c;
|
|
2350 ngx_http_upstream_t *u;
|
|
2351
|
|
2352 c = r->connection;
|
|
2353 u = r->upstream;
|
|
2354 wev = c->write;
|
|
2355
|
|
2356 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2357 "http upstream process non buffered downstream");
|
|
2358
|
|
2359 c->log->action = "sending to client";
|
|
2360
|
|
2361 if (wev->timedout) {
|
|
2362 c->timedout = 1;
|
|
2363 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
|
632
|
2364 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_REQUEST_TIME_OUT);
|
428
|
2365 return;
|
|
2366 }
|
|
2367
|
|
2368 ngx_http_upstream_process_non_buffered_request(r, 1);
|
130
|
2369 }
|
|
2370
|
|
2371
|
|
2372 static void
|
430
|
2373 ngx_http_upstream_process_non_buffered_upstream(ngx_http_request_t *r,
|
|
2374 ngx_http_upstream_t *u)
|
428
|
2375 {
|
430
|
2376 ngx_connection_t *c;
|
|
2377
|
|
2378 c = u->peer.connection;
|
428
|
2379
|
|
2380 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2381 "http upstream process non buffered upstream");
|
|
2382
|
|
2383 c->log->action = "reading upstream";
|
|
2384
|
430
|
2385 if (c->read->timedout) {
|
428
|
2386 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
|
|
2387 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2388 return;
|
|
2389 }
|
|
2390
|
|
2391 ngx_http_upstream_process_non_buffered_request(r, 0);
|
|
2392 }
|
|
2393
|
|
2394
|
|
2395 static void
|
|
2396 ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
|
|
2397 ngx_uint_t do_write)
|
130
|
2398 {
|
|
2399 size_t size;
|
|
2400 ssize_t n;
|
|
2401 ngx_buf_t *b;
|
132
|
2402 ngx_int_t rc;
|
428
|
2403 ngx_connection_t *downstream, *upstream;
|
130
|
2404 ngx_http_upstream_t *u;
|
|
2405 ngx_http_core_loc_conf_t *clcf;
|
|
2406
|
244
|
2407 u = r->upstream;
|
260
|
2408 downstream = r->connection;
|
|
2409 upstream = u->peer.connection;
|
130
|
2410
|
|
2411 b = &u->buffer;
|
|
2412
|
428
|
2413 do_write = do_write || u->length == 0;
|
130
|
2414
|
|
2415 for ( ;; ) {
|
|
2416
|
|
2417 if (do_write) {
|
|
2418
|
|
2419 if (u->out_bufs || u->busy_bufs) {
|
132
|
2420 rc = ngx_http_output_filter(r, u->out_bufs);
|
|
2421
|
|
2422 if (rc == NGX_ERROR) {
|
130
|
2423 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2424 return;
|
|
2425 }
|
|
2426
|
640
|
2427 ngx_chain_update_chains(r->pool, &u->free_bufs, &u->busy_bufs,
|
130
|
2428 &u->out_bufs, u->output.tag);
|
|
2429 }
|
|
2430
|
|
2431 if (u->busy_bufs == NULL) {
|
|
2432
|
|
2433 if (u->length == 0
|
260
|
2434 || upstream->read->eof
|
|
2435 || upstream->read->error)
|
130
|
2436 {
|
|
2437 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2438 return;
|
|
2439 }
|
|
2440
|
|
2441 b->pos = b->start;
|
|
2442 b->last = b->start;
|
|
2443 }
|
|
2444 }
|
|
2445
|
|
2446 size = b->end - b->last;
|
|
2447
|
260
|
2448 if (size && upstream->read->ready) {
|
|
2449
|
|
2450 n = upstream->recv(upstream, b->last, size);
|
130
|
2451
|
|
2452 if (n == NGX_AGAIN) {
|
|
2453 break;
|
|
2454 }
|
|
2455
|
|
2456 if (n > 0) {
|
430
|
2457 u->state->response_length += n;
|
|
2458
|
130
|
2459 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
|
|
2460 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2461 return;
|
|
2462 }
|
|
2463 }
|
|
2464
|
|
2465 do_write = 1;
|
|
2466
|
|
2467 continue;
|
|
2468 }
|
|
2469
|
|
2470 break;
|
|
2471 }
|
|
2472
|
428
|
2473 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2474
|
260
|
2475 if (downstream->data == r) {
|
|
2476 if (ngx_handle_write_event(downstream->write, clcf->send_lowat)
|
426
|
2477 != NGX_OK)
|
132
|
2478 {
|
|
2479 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2480 return;
|
|
2481 }
|
130
|
2482 }
|
|
2483
|
276
|
2484 if (downstream->write->active && !downstream->write->ready) {
|
260
|
2485 ngx_add_timer(downstream->write, clcf->send_timeout);
|
|
2486
|
|
2487 } else if (downstream->write->timer_set) {
|
|
2488 ngx_del_timer(downstream->write);
|
130
|
2489 }
|
|
2490
|
426
|
2491 if (ngx_handle_read_event(upstream->read, 0) != NGX_OK) {
|
130
|
2492 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2493 return;
|
|
2494 }
|
|
2495
|
276
|
2496 if (upstream->read->active && !upstream->read->ready) {
|
260
|
2497 ngx_add_timer(upstream->read, u->conf->read_timeout);
|
|
2498
|
|
2499 } else if (upstream->read->timer_set) {
|
|
2500 ngx_del_timer(upstream->read);
|
130
|
2501 }
|
|
2502 }
|
|
2503
|
|
2504
|
|
2505 static ngx_int_t
|
|
2506 ngx_http_upstream_non_buffered_filter_init(void *data)
|
|
2507 {
|
|
2508 return NGX_OK;
|
|
2509 }
|
|
2510
|
|
2511
|
|
2512 static ngx_int_t
|
|
2513 ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes)
|
|
2514 {
|
|
2515 ngx_http_request_t *r = data;
|
|
2516
|
|
2517 ngx_buf_t *b;
|
|
2518 ngx_chain_t *cl, **ll;
|
|
2519 ngx_http_upstream_t *u;
|
|
2520
|
|
2521 u = r->upstream;
|
|
2522
|
|
2523 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
2524 ll = &cl->next;
|
|
2525 }
|
|
2526
|
|
2527 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
2528 if (cl == NULL) {
|
|
2529 return NGX_ERROR;
|
|
2530 }
|
|
2531
|
|
2532 *ll = cl;
|
|
2533
|
|
2534 cl->buf->flush = 1;
|
|
2535 cl->buf->memory = 1;
|
|
2536
|
|
2537 b = &u->buffer;
|
|
2538
|
|
2539 cl->buf->pos = b->last;
|
|
2540 b->last += bytes;
|
|
2541 cl->buf->last = b->last;
|
366
|
2542 cl->buf->tag = u->output.tag;
|
130
|
2543
|
640
|
2544 if (u->length == -1) {
|
130
|
2545 return NGX_OK;
|
|
2546 }
|
|
2547
|
|
2548 u->length -= bytes;
|
|
2549
|
|
2550 return NGX_OK;
|
|
2551 }
|
|
2552
|
|
2553
|
|
2554 static void
|
58
|
2555 ngx_http_upstream_process_downstream(ngx_http_request_t *r)
|
|
2556 {
|
428
|
2557 ngx_event_t *wev;
|
|
2558 ngx_connection_t *c;
|
58
|
2559 ngx_event_pipe_t *p;
|
28
|
2560 ngx_http_upstream_t *u;
|
|
2561
|
428
|
2562 c = r->connection;
|
28
|
2563 u = r->upstream;
|
130
|
2564 p = u->pipe;
|
428
|
2565 wev = c->write;
|
|
2566
|
|
2567 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2568 "http upstream process downstream");
|
|
2569
|
|
2570 c->log->action = "sending to client";
|
|
2571
|
|
2572 if (wev->timedout) {
|
|
2573
|
|
2574 if (wev->delayed) {
|
|
2575
|
|
2576 wev->timedout = 0;
|
|
2577 wev->delayed = 0;
|
|
2578
|
|
2579 if (!wev->ready) {
|
|
2580 ngx_add_timer(wev, p->send_timeout);
|
|
2581
|
|
2582 if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) {
|
|
2583 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2584 }
|
|
2585
|
|
2586 return;
|
|
2587 }
|
|
2588
|
|
2589 if (ngx_event_pipe(p, wev->write) == NGX_ABORT) {
|
|
2590 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2591 return;
|
76
|
2592 }
|
28
|
2593
|
|
2594 } else {
|
428
|
2595 p->downstream_error = 1;
|
|
2596 c->timedout = 1;
|
|
2597 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
|
28
|
2598 }
|
|
2599
|
|
2600 } else {
|
428
|
2601
|
|
2602 if (wev->delayed) {
|
|
2603
|
76
|
2604 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2605 "http downstream delayed");
|
|
2606
|
428
|
2607 if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) {
|
426
|
2608 ngx_http_upstream_finalize_request(r, u, 0);
|
76
|
2609 }
|
|
2610
|
|
2611 return;
|
|
2612 }
|
|
2613
|
428
|
2614 if (ngx_event_pipe(p, 1) == NGX_ABORT) {
|
28
|
2615 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2616 return;
|
|
2617 }
|
|
2618 }
|
126
|
2619
|
428
|
2620 ngx_http_upstream_process_request(r);
|
|
2621 }
|
|
2622
|
|
2623
|
|
2624 static void
|
430
|
2625 ngx_http_upstream_process_upstream(ngx_http_request_t *r,
|
|
2626 ngx_http_upstream_t *u)
|
428
|
2627 {
|
430
|
2628 ngx_connection_t *c;
|
|
2629
|
|
2630 c = u->peer.connection;
|
428
|
2631
|
|
2632 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2633 "http upstream process upstream");
|
|
2634
|
|
2635 c->log->action = "reading upstream";
|
|
2636
|
430
|
2637 if (c->read->timedout) {
|
|
2638 u->pipe->upstream_error = 1;
|
428
|
2639 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
|
|
2640
|
|
2641 } else {
|
430
|
2642 if (ngx_event_pipe(u->pipe, 0) == NGX_ABORT) {
|
428
|
2643 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2644 return;
|
|
2645 }
|
|
2646 }
|
|
2647
|
|
2648 ngx_http_upstream_process_request(r);
|
|
2649 }
|
|
2650
|
|
2651
|
|
2652 static void
|
|
2653 ngx_http_upstream_process_request(ngx_http_request_t *r)
|
|
2654 {
|
|
2655 ngx_temp_file_t *tf;
|
|
2656 ngx_event_pipe_t *p;
|
|
2657 ngx_http_upstream_t *u;
|
|
2658
|
|
2659 u = r->upstream;
|
|
2660 p = u->pipe;
|
|
2661
|
28
|
2662 if (u->peer.connection) {
|
|
2663
|
318
|
2664 if (u->store) {
|
|
2665
|
440
|
2666 if (p->upstream_eof || p->upstream_done) {
|
438
|
2667
|
662
|
2668 tf = u->pipe->temp_file;
|
|
2669
|
438
|
2670 if (u->headers_in.status_n == NGX_HTTP_OK
|
|
2671 && (u->headers_in.content_length_n == -1
|
|
2672 || (u->headers_in.content_length_n == tf->offset)))
|
|
2673 {
|
|
2674 ngx_http_upstream_store(r, u);
|
662
|
2675 u->store = 0;
|
318
|
2676 }
|
|
2677 }
|
|
2678 }
|
|
2679
|
464
|
2680 #if (NGX_HTTP_CACHE)
|
|
2681
|
|
2682 if (u->cacheable) {
|
|
2683
|
|
2684 if (p->upstream_done) {
|
|
2685 ngx_http_file_cache_update(r, u->pipe->temp_file);
|
|
2686
|
|
2687 } else if (p->upstream_eof) {
|
|
2688
|
650
|
2689 tf = u->pipe->temp_file;
|
|
2690
|
|
2691 if (u->headers_in.content_length_n == -1
|
|
2692 || u->headers_in.content_length_n
|
|
2693 == tf->offset - (off_t) r->cache->body_start)
|
|
2694 {
|
|
2695 ngx_http_file_cache_update(r, tf);
|
|
2696
|
|
2697 } else {
|
|
2698 ngx_http_file_cache_free(r->cache, tf);
|
|
2699 }
|
464
|
2700
|
|
2701 } else if (p->upstream_error) {
|
590
|
2702 ngx_http_file_cache_free(r->cache, u->pipe->temp_file);
|
28
|
2703 }
|
|
2704 }
|
|
2705
|
|
2706 #endif
|
|
2707
|
|
2708 if (p->upstream_done || p->upstream_eof || p->upstream_error) {
|
428
|
2709 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
32
|
2710 "http upstream exit: %p", p->out);
|
28
|
2711 #if 0
|
|
2712 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
|
|
2713 #endif
|
|
2714 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2715 return;
|
|
2716 }
|
|
2717 }
|
|
2718
|
|
2719 if (p->downstream_error) {
|
428
|
2720 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
32
|
2721 "http upstream downstream error");
|
28
|
2722
|
482
|
2723 if (!u->cacheable && !u->store && u->peer.connection) {
|
28
|
2724 ngx_http_upstream_finalize_request(r, u, 0);
|
|
2725 }
|
|
2726 }
|
|
2727 }
|
|
2728
|
|
2729
|
36
|
2730 static void
|
318
|
2731 ngx_http_upstream_store(ngx_http_request_t *r, ngx_http_upstream_t *u)
|
|
2732 {
|
370
|
2733 size_t root;
|
|
2734 time_t lm;
|
|
2735 ngx_str_t path;
|
|
2736 ngx_temp_file_t *tf;
|
|
2737 ngx_ext_rename_file_t ext;
|
318
|
2738
|
358
|
2739 tf = u->pipe->temp_file;
|
|
2740
|
|
2741 if (tf->file.fd == NGX_INVALID_FILE) {
|
318
|
2742
|
|
2743 /* create file for empty 200 response */
|
|
2744
|
|
2745 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
|
|
2746 if (tf == NULL) {
|
|
2747 return;
|
|
2748 }
|
|
2749
|
|
2750 tf->file.fd = NGX_INVALID_FILE;
|
|
2751 tf->file.log = r->connection->log;
|
|
2752 tf->path = u->conf->temp_path;
|
|
2753 tf->pool = r->pool;
|
|
2754 tf->persistent = 1;
|
|
2755
|
|
2756 if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
|
|
2757 tf->persistent, tf->clean, tf->access)
|
|
2758 != NGX_OK)
|
|
2759 {
|
|
2760 return;
|
|
2761 }
|
|
2762
|
|
2763 u->pipe->temp_file = tf;
|
|
2764 }
|
|
2765
|
370
|
2766 ext.access = u->conf->store_access;
|
430
|
2767 ext.path_access = u->conf->store_access;
|
370
|
2768 ext.time = -1;
|
|
2769 ext.create_path = 1;
|
|
2770 ext.delete_file = 1;
|
|
2771 ext.log = r->connection->log;
|
318
|
2772
|
356
|
2773 if (u->headers_in.last_modified) {
|
|
2774
|
370
|
2775 lm = ngx_http_parse_time(u->headers_in.last_modified->value.data,
|
|
2776 u->headers_in.last_modified->value.len);
|
318
|
2777
|
|
2778 if (lm != NGX_ERROR) {
|
370
|
2779 ext.time = lm;
|
|
2780 ext.fd = tf->file.fd;
|
318
|
2781 }
|
|
2782 }
|
|
2783
|
320
|
2784 if (u->conf->store_lengths == NULL) {
|
|
2785
|
|
2786 ngx_http_map_uri_to_path(r, &path, &root, 0);
|
|
2787
|
|
2788 } else {
|
|
2789 if (ngx_http_script_run(r, &path, u->conf->store_lengths->elts, 0,
|
|
2790 u->conf->store_values->elts)
|
|
2791 == NULL)
|
|
2792 {
|
|
2793 return;
|
|
2794 }
|
|
2795 }
|
318
|
2796
|
516
|
2797 path.len--;
|
|
2798
|
318
|
2799 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
370
|
2800 "upstream stores \"%s\" to \"%s\"",
|
|
2801 tf->file.name.data, path.data);
|
|
2802
|
|
2803 (void) ngx_ext_rename_file(&tf->file.name, &path, &ext);
|
318
|
2804 }
|
|
2805
|
|
2806
|
|
2807 static void
|
430
|
2808 ngx_http_upstream_dummy_handler(ngx_http_request_t *r, ngx_http_upstream_t *u)
|
28
|
2809 {
|
430
|
2810 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
28
|
2811 "http upstream dummy handler");
|
|
2812 }
|
|
2813
|
|
2814
|
36
|
2815 static void
|
|
2816 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
|
|
2817 ngx_uint_t ft_type)
|
28
|
2818 {
|
260
|
2819 ngx_uint_t status, state;
|
28
|
2820
|
|
2821 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
260
|
2822 "http next upstream, %xi", ft_type);
|
28
|
2823
|
|
2824 #if 0
|
|
2825 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
|
|
2826 #endif
|
|
2827
|
74
|
2828 if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) {
|
260
|
2829 state = NGX_PEER_NEXT;
|
74
|
2830 } else {
|
260
|
2831 state = NGX_PEER_FAILED;
|
28
|
2832 }
|
74
|
2833
|
326
|
2834 if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) {
|
|
2835 u->peer.free(&u->peer, u->peer.data, state);
|
|
2836 }
|
74
|
2837
|
28
|
2838 if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {
|
|
2839 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,
|
|
2840 "upstream timed out");
|
|
2841 }
|
126
|
2842
|
28
|
2843 if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {
|
|
2844 status = 0;
|
|
2845
|
640
|
2846 /* TODO: inform balancer instead */
|
|
2847
|
|
2848 u->peer.tries++;
|
|
2849
|
28
|
2850 } else {
|
|
2851 switch(ft_type) {
|
|
2852
|
|
2853 case NGX_HTTP_UPSTREAM_FT_TIMEOUT:
|
|
2854 status = NGX_HTTP_GATEWAY_TIME_OUT;
|
|
2855 break;
|
|
2856
|
|
2857 case NGX_HTTP_UPSTREAM_FT_HTTP_500:
|
|
2858 status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
2859 break;
|
|
2860
|
|
2861 case NGX_HTTP_UPSTREAM_FT_HTTP_404:
|
|
2862 status = NGX_HTTP_NOT_FOUND;
|
|
2863 break;
|
|
2864
|
|
2865 /*
|
|
2866 * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING
|
|
2867 * never reach here
|
|
2868 */
|
|
2869
|
|
2870 default:
|
|
2871 status = NGX_HTTP_BAD_GATEWAY;
|
|
2872 }
|
|
2873 }
|
|
2874
|
132
|
2875 if (r->connection->error) {
|
28
|
2876 ngx_http_upstream_finalize_request(r, u,
|
|
2877 NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
2878 return;
|
|
2879 }
|
|
2880
|
|
2881 if (status) {
|
|
2882 u->state->status = status;
|
|
2883
|
74
|
2884 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {
|
28
|
2885
|
|
2886 #if (NGX_HTTP_CACHE)
|
|
2887
|
502
|
2888 if (u->cache_status == NGX_HTTP_CACHE_EXPIRED
|
|
2889 && (u->conf->cache_use_stale & ft_type))
|
|
2890 {
|
486
|
2891 ngx_int_t rc;
|
|
2892
|
|
2893 rc = u->reinit_request(r);
|
|
2894
|
|
2895 if (rc == NGX_OK) {
|
502
|
2896 u->cache_status = NGX_HTTP_CACHE_STALE;
|
486
|
2897 rc = ngx_http_upstream_cache_send(r, u);
|
|
2898 }
|
|
2899
|
|
2900 ngx_http_upstream_finalize_request(r, u, rc);
|
28
|
2901 return;
|
|
2902 }
|
|
2903 #endif
|
|
2904
|
|
2905 ngx_http_upstream_finalize_request(r, u, status);
|
|
2906 return;
|
|
2907 }
|
|
2908 }
|
|
2909
|
|
2910 if (u->peer.connection) {
|
|
2911 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2912 "close http upstream connection: %d",
|
|
2913 u->peer.connection->fd);
|
126
|
2914 #if (NGX_HTTP_SSL)
|
260
|
2915
|
126
|
2916 if (u->peer.connection->ssl) {
|
260
|
2917 u->peer.connection->ssl->no_wait_shutdown = 1;
|
|
2918 u->peer.connection->ssl->no_send_shutdown = 1;
|
|
2919
|
|
2920 (void) ngx_ssl_shutdown(u->peer.connection);
|
126
|
2921 }
|
|
2922 #endif
|
260
|
2923
|
640
|
2924 if (u->peer.connection->pool) {
|
|
2925 ngx_destroy_pool(u->peer.connection->pool);
|
|
2926 }
|
|
2927
|
28
|
2928 ngx_close_connection(u->peer.connection);
|
642
|
2929 u->peer.connection = NULL;
|
28
|
2930 }
|
|
2931
|
|
2932 #if 0
|
|
2933 if (u->conf->busy_lock && !u->busy_locked) {
|
|
2934 ngx_http_upstream_busy_lock(p);
|
|
2935 return;
|
|
2936 }
|
|
2937 #endif
|
|
2938
|
|
2939 ngx_http_upstream_connect(r, u);
|
|
2940 }
|
|
2941
|
|
2942
|
36
|
2943 static void
|
118
|
2944 ngx_http_upstream_cleanup(void *data)
|
|
2945 {
|
|
2946 ngx_http_request_t *r = data;
|
|
2947
|
356
|
2948 ngx_http_upstream_t *u;
|
|
2949
|
118
|
2950 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2951 "cleanup http upstream request: \"%V\"", &r->uri);
|
|
2952
|
356
|
2953 u = r->upstream;
|
|
2954
|
|
2955 if (u->resolved && u->resolved->ctx) {
|
|
2956 ngx_resolve_name_done(u->resolved->ctx);
|
530
|
2957 u->resolved->ctx = NULL;
|
348
|
2958 }
|
|
2959
|
356
|
2960 ngx_http_upstream_finalize_request(r, u, NGX_DONE);
|
118
|
2961 }
|
|
2962
|
|
2963
|
|
2964 static void
|
36
|
2965 ngx_http_upstream_finalize_request(ngx_http_request_t *r,
|
|
2966 ngx_http_upstream_t *u, ngx_int_t rc)
|
28
|
2967 {
|
262
|
2968 ngx_time_t *tp;
|
112
|
2969
|
32
|
2970 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2971 "finalize http upstream request: %i", rc);
|
28
|
2972
|
432
|
2973 if (u->cleanup) {
|
|
2974 *u->cleanup = NULL;
|
562
|
2975 u->cleanup = NULL;
|
432
|
2976 }
|
118
|
2977
|
530
|
2978 if (u->resolved && u->resolved->ctx) {
|
|
2979 ngx_resolve_name_done(u->resolved->ctx);
|
|
2980 u->resolved->ctx = NULL;
|
|
2981 }
|
|
2982
|
348
|
2983 if (u->state && u->state->response_sec) {
|
112
|
2984 tp = ngx_timeofday();
|
262
|
2985 u->state->response_sec = tp->sec - u->state->response_sec;
|
|
2986 u->state->response_msec = tp->msec - u->state->response_msec;
|
430
|
2987
|
|
2988 if (u->pipe) {
|
|
2989 u->state->response_length = u->pipe->read_length;
|
|
2990 }
|
112
|
2991 }
|
|
2992
|
28
|
2993 u->finalize_request(r, rc);
|
|
2994
|
348
|
2995 if (u->peer.free) {
|
|
2996 u->peer.free(&u->peer, u->peer.data, 0);
|
|
2997 }
|
260
|
2998
|
28
|
2999 if (u->peer.connection) {
|
260
|
3000
|
126
|
3001 #if (NGX_HTTP_SSL)
|
|
3002
|
|
3003 /* TODO: do not shutdown persistent connection */
|
|
3004
|
|
3005 if (u->peer.connection->ssl) {
|
260
|
3006
|
|
3007 /*
|
|
3008 * We send the "close notify" shutdown alert to the upstream only
|
|
3009 * and do not wait its "close notify" shutdown alert.
|
|
3010 * It is acceptable according to the TLS standard.
|
|
3011 */
|
|
3012
|
|
3013 u->peer.connection->ssl->no_wait_shutdown = 1;
|
|
3014
|
|
3015 (void) ngx_ssl_shutdown(u->peer.connection);
|
126
|
3016 }
|
|
3017 #endif
|
260
|
3018
|
|
3019 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
3020 "close http upstream connection: %d",
|
|
3021 u->peer.connection->fd);
|
|
3022
|
640
|
3023 if (u->peer.connection->pool) {
|
|
3024 ngx_destroy_pool(u->peer.connection->pool);
|
|
3025 }
|
|
3026
|
28
|
3027 ngx_close_connection(u->peer.connection);
|
|
3028 }
|
|
3029
|
32
|
3030 u->peer.connection = NULL;
|
|
3031
|
130
|
3032 if (u->pipe && u->pipe->temp_file) {
|
28
|
3033 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
3034 "http upstream temp fd: %d",
|
130
|
3035 u->pipe->temp_file->file.fd);
|
28
|
3036 }
|
|
3037
|
662
|
3038 if (u->store && u->pipe && u->pipe->temp_file
|
|
3039 && u->pipe->temp_file->file.fd != NGX_INVALID_FILE)
|
|
3040 {
|
|
3041 if (ngx_delete_file(u->pipe->temp_file->file.name.data)
|
|
3042 == NGX_FILE_ERROR)
|
|
3043 {
|
|
3044 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
3045 ngx_delete_file_n " \"%s\" failed",
|
|
3046 u->pipe->temp_file->file.name.data);
|
|
3047 }
|
|
3048 }
|
|
3049
|
464
|
3050 #if (NGX_HTTP_CACHE)
|
|
3051
|
630
|
3052 if (r->cache) {
|
|
3053
|
|
3054 if (u->cacheable) {
|
|
3055
|
|
3056 if (rc == NGX_HTTP_BAD_GATEWAY || rc == NGX_HTTP_GATEWAY_TIME_OUT) {
|
|
3057 time_t valid;
|
|
3058
|
|
3059 valid = ngx_http_file_cache_valid(u->conf->cache_valid, rc);
|
|
3060
|
|
3061 if (valid) {
|
|
3062 r->cache->valid_sec = ngx_time() + valid;
|
|
3063 r->cache->error = rc;
|
|
3064 }
|
464
|
3065 }
|
|
3066 }
|
|
3067
|
590
|
3068 ngx_http_file_cache_free(r->cache, u->pipe->temp_file);
|
28
|
3069 }
|
464
|
3070
|
28
|
3071 #endif
|
|
3072
|
478
|
3073 if (u->header_sent
|
632
|
3074 && rc != NGX_HTTP_REQUEST_TIME_OUT
|
478
|
3075 && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
|
464
|
3076 {
|
|
3077 rc = 0;
|
|
3078 }
|
|
3079
|
64
|
3080 if (rc == NGX_DECLINED) {
|
|
3081 return;
|
|
3082 }
|
|
3083
|
32
|
3084 r->connection->log->action = "sending to client";
|
|
3085
|
642
|
3086 if (rc == 0
|
|
3087 #if (NGX_HTTP_CACHE)
|
|
3088 && !r->cached
|
|
3089 #endif
|
|
3090 )
|
|
3091 {
|
426
|
3092 rc = ngx_http_send_special(r, NGX_HTTP_LAST);
|
28
|
3093 }
|
|
3094
|
|
3095 ngx_http_finalize_request(r, rc);
|
|
3096 }
|
|
3097
|
|
3098
|
58
|
3099 static ngx_int_t
|
|
3100 ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3101 ngx_uint_t offset)
|
126
|
3102 {
|
58
|
3103 ngx_table_elt_t **ph;
|
126
|
3104
|
58
|
3105 ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset);
|
|
3106
|
|
3107 if (*ph == NULL) {
|
|
3108 *ph = h;
|
|
3109 }
|
|
3110
|
|
3111 return NGX_OK;
|
|
3112 }
|
|
3113
|
|
3114
|
|
3115 static ngx_int_t
|
472
|
3116 ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3117 ngx_uint_t offset)
|
|
3118 {
|
|
3119 return NGX_OK;
|
|
3120 }
|
|
3121
|
|
3122
|
|
3123 static ngx_int_t
|
640
|
3124 ngx_http_upstream_process_content_length(ngx_http_request_t *r,
|
|
3125 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
3126 {
|
|
3127 ngx_http_upstream_t *u;
|
|
3128
|
|
3129 u = r->upstream;
|
|
3130
|
|
3131 u->headers_in.content_length = h;
|
|
3132 u->headers_in.content_length_n = ngx_atoof(h->value.data, h->value.len);
|
|
3133
|
|
3134 return NGX_OK;
|
|
3135 }
|
|
3136
|
|
3137
|
|
3138 static ngx_int_t
|
584
|
3139 ngx_http_upstream_process_set_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3140 ngx_uint_t offset)
|
|
3141 {
|
|
3142 #if (NGX_HTTP_CACHE)
|
|
3143 ngx_http_upstream_t *u;
|
|
3144
|
|
3145 u = r->upstream;
|
|
3146
|
|
3147 if (!(u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_SET_COOKIE)) {
|
|
3148 u->cacheable = 0;
|
|
3149 }
|
|
3150 #endif
|
|
3151
|
|
3152 return NGX_OK;
|
|
3153 }
|
|
3154
|
|
3155
|
|
3156 static ngx_int_t
|
472
|
3157 ngx_http_upstream_process_cache_control(ngx_http_request_t *r,
|
58
|
3158 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
3159 {
|
484
|
3160 ngx_array_t *pa;
|
|
3161 ngx_table_elt_t **ph;
|
|
3162 ngx_http_upstream_t *u;
|
|
3163
|
|
3164 u = r->upstream;
|
|
3165 pa = &u->headers_in.cache_control;
|
58
|
3166
|
|
3167 if (pa->elts == NULL) {
|
|
3168 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
|
|
3169 {
|
|
3170 return NGX_ERROR;
|
|
3171 }
|
|
3172 }
|
|
3173
|
|
3174 ph = ngx_array_push(pa);
|
|
3175 if (ph == NULL) {
|
|
3176 return NGX_ERROR;
|
|
3177 }
|
|
3178
|
|
3179 *ph = h;
|
|
3180
|
480
|
3181 #if (NGX_HTTP_CACHE)
|
|
3182 {
|
|
3183 u_char *p, *last;
|
|
3184 ngx_int_t n;
|
|
3185
|
484
|
3186 if (u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_CACHE_CONTROL) {
|
|
3187 return NGX_OK;
|
|
3188 }
|
|
3189
|
472
|
3190 if (r->cache == NULL) {
|
|
3191 return NGX_OK;
|
|
3192 }
|
|
3193
|
|
3194 if (r->cache->valid_sec != 0) {
|
|
3195 return NGX_OK;
|
|
3196 }
|
|
3197
|
570
|
3198 p = h->value.data;
|
|
3199 last = p + h->value.len;
|
|
3200
|
|
3201 if (ngx_strlcasestrn(p, last, (u_char *) "no-cache", 8 - 1) != NULL
|
|
3202 || ngx_strlcasestrn(p, last, (u_char *) "no-store", 8 - 1) != NULL
|
|
3203 || ngx_strlcasestrn(p, last, (u_char *) "private", 7 - 1) != NULL)
|
472
|
3204 {
|
484
|
3205 u->cacheable = 0;
|
472
|
3206 return NGX_OK;
|
|
3207 }
|
|
3208
|
570
|
3209 p = ngx_strlcasestrn(p, last, (u_char *) "max-age=", 8 - 1);
|
472
|
3210
|
|
3211 if (p == NULL) {
|
|
3212 return NGX_OK;
|
|
3213 }
|
|
3214
|
|
3215 n = 0;
|
|
3216
|
|
3217 for (p += 8; p < last; p++) {
|
536
|
3218 if (*p == ',' || *p == ';' || *p == ' ') {
|
472
|
3219 break;
|
|
3220 }
|
|
3221
|
|
3222 if (*p >= '0' && *p <= '9') {
|
|
3223 n = n * 10 + *p - '0';
|
|
3224 continue;
|
|
3225 }
|
|
3226
|
484
|
3227 u->cacheable = 0;
|
472
|
3228 return NGX_OK;
|
|
3229 }
|
|
3230
|
|
3231 if (n == 0) {
|
484
|
3232 u->cacheable = 0;
|
472
|
3233 return NGX_OK;
|
|
3234 }
|
|
3235
|
|
3236 r->cache->valid_sec = ngx_time() + n;
|
480
|
3237 }
|
|
3238 #endif
|
|
3239
|
58
|
3240 return NGX_OK;
|
|
3241 }
|
|
3242
|
|
3243
|
|
3244 static ngx_int_t
|
472
|
3245 ngx_http_upstream_process_expires(ngx_http_request_t *r, ngx_table_elt_t *h,
|
58
|
3246 ngx_uint_t offset)
|
|
3247 {
|
484
|
3248 ngx_http_upstream_t *u;
|
|
3249
|
|
3250 u = r->upstream;
|
|
3251 u->headers_in.expires = h;
|
480
|
3252
|
|
3253 #if (NGX_HTTP_CACHE)
|
|
3254 {
|
472
|
3255 time_t expires;
|
|
3256
|
484
|
3257 if (u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_EXPIRES) {
|
|
3258 return NGX_OK;
|
|
3259 }
|
|
3260
|
472
|
3261 if (r->cache == NULL) {
|
|
3262 return NGX_OK;
|
|
3263 }
|
|
3264
|
|
3265 if (r->cache->valid_sec != 0) {
|
|
3266 return NGX_OK;
|
|
3267 }
|
|
3268
|
|
3269 expires = ngx_http_parse_time(h->value.data, h->value.len);
|
|
3270
|
|
3271 if (expires == NGX_ERROR || expires < ngx_time()) {
|
484
|
3272 u->cacheable = 0;
|
472
|
3273 return NGX_OK;
|
|
3274 }
|
|
3275
|
|
3276 r->cache->valid_sec = expires;
|
480
|
3277 }
|
|
3278 #endif
|
472
|
3279
|
|
3280 return NGX_OK;
|
|
3281 }
|
|
3282
|
|
3283
|
|
3284 static ngx_int_t
|
|
3285 ngx_http_upstream_process_accel_expires(ngx_http_request_t *r,
|
|
3286 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
3287 {
|
484
|
3288 ngx_http_upstream_t *u;
|
|
3289
|
|
3290 u = r->upstream;
|
|
3291 u->headers_in.x_accel_expires = h;
|
480
|
3292
|
|
3293 #if (NGX_HTTP_CACHE)
|
|
3294 {
|
472
|
3295 u_char *p;
|
|
3296 size_t len;
|
|
3297 ngx_int_t n;
|
|
3298
|
484
|
3299 if (u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_XA_EXPIRES) {
|
|
3300 return NGX_OK;
|
|
3301 }
|
|
3302
|
472
|
3303 if (r->cache == NULL) {
|
|
3304 return NGX_OK;
|
|
3305 }
|
|
3306
|
|
3307 len = h->value.len;
|
|
3308 p = h->value.data;
|
|
3309
|
|
3310 if (p[0] != '@') {
|
|
3311 n = ngx_atoi(p, len);
|
|
3312
|
|
3313 switch (n) {
|
|
3314 case 0:
|
484
|
3315 u->cacheable = 0;
|
668
|
3316 /* fall through */
|
|
3317
|
472
|
3318 case NGX_ERROR:
|
|
3319 return NGX_OK;
|
|
3320
|
|
3321 default:
|
|
3322 r->cache->valid_sec = ngx_time() + n;
|
|
3323 return NGX_OK;
|
|
3324 }
|
|
3325 }
|
|
3326
|
|
3327 p++;
|
|
3328 len--;
|
|
3329
|
|
3330 n = ngx_atoi(p, len);
|
|
3331
|
|
3332 if (n != NGX_ERROR) {
|
|
3333 r->cache->valid_sec = n;
|
|
3334 }
|
480
|
3335 }
|
|
3336 #endif
|
472
|
3337
|
58
|
3338 return NGX_OK;
|
|
3339 }
|
|
3340
|
|
3341
|
|
3342 static ngx_int_t
|
76
|
3343 ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3344 ngx_uint_t offset)
|
126
|
3345 {
|
644
|
3346 ngx_int_t n;
|
|
3347 ngx_http_upstream_t *u;
|
|
3348
|
|
3349 u = r->upstream;
|
|
3350 u->headers_in.x_accel_limit_rate = h;
|
|
3351
|
|
3352 if (u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_XA_LIMIT_RATE) {
|
|
3353 return NGX_OK;
|
|
3354 }
|
76
|
3355
|
|
3356 n = ngx_atoi(h->value.data, h->value.len);
|
|
3357
|
|
3358 if (n != NGX_ERROR) {
|
|
3359 r->limit_rate = (size_t) n;
|
|
3360 }
|
|
3361
|
|
3362 return NGX_OK;
|
|
3363 }
|
|
3364
|
|
3365
|
|
3366 static ngx_int_t
|
198
|
3367 ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3368 ngx_uint_t offset)
|
|
3369 {
|
644
|
3370 u_char c0, c1, c2;
|
|
3371 ngx_http_upstream_t *u;
|
|
3372
|
|
3373 u = r->upstream;
|
|
3374
|
|
3375 if (u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_XA_BUFFERING) {
|
|
3376 return NGX_OK;
|
|
3377 }
|
|
3378
|
|
3379 if (u->conf->change_buffering) {
|
198
|
3380
|
|
3381 if (h->value.len == 2) {
|
|
3382 c0 = ngx_tolower(h->value.data[0]);
|
|
3383 c1 = ngx_tolower(h->value.data[1]);
|
|
3384
|
|
3385 if (c0 == 'n' && c1 == 'o') {
|
644
|
3386 u->buffering = 0;
|
198
|
3387 }
|
|
3388
|
|
3389 } else if (h->value.len == 3) {
|
|
3390 c0 = ngx_tolower(h->value.data[0]);
|
|
3391 c1 = ngx_tolower(h->value.data[1]);
|
|
3392 c2 = ngx_tolower(h->value.data[2]);
|
|
3393
|
|
3394 if (c0 == 'y' && c1 == 'e' && c2 == 's') {
|
644
|
3395 u->buffering = 1;
|
198
|
3396 }
|
|
3397 }
|
|
3398 }
|
|
3399
|
|
3400 return NGX_OK;
|
|
3401 }
|
|
3402
|
|
3403
|
|
3404 static ngx_int_t
|
206
|
3405 ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3406 ngx_uint_t offset)
|
|
3407 {
|
644
|
3408 if (r->upstream->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_XA_CHARSET) {
|
|
3409 return NGX_OK;
|
|
3410 }
|
|
3411
|
206
|
3412 r->headers_out.override_charset = &h->value;
|
|
3413
|
|
3414 return NGX_OK;
|
|
3415 }
|
|
3416
|
|
3417
|
|
3418 static ngx_int_t
|
640
|
3419 ngx_http_upstream_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3420 ngx_uint_t offset)
|
|
3421 {
|
|
3422 r->upstream->headers_in.connection = h;
|
|
3423
|
|
3424 if (ngx_strlcasestrn(h->value.data, h->value.data + h->value.len,
|
|
3425 (u_char *) "close", 5 - 1)
|
|
3426 != NULL)
|
|
3427 {
|
|
3428 r->upstream->headers_in.connection_close = 1;
|
|
3429 }
|
|
3430
|
|
3431 return NGX_OK;
|
|
3432 }
|
|
3433
|
|
3434
|
|
3435 static ngx_int_t
|
|
3436 ngx_http_upstream_process_transfer_encoding(ngx_http_request_t *r,
|
|
3437 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
3438 {
|
|
3439 r->upstream->headers_in.transfer_encoding = h;
|
|
3440
|
|
3441 if (ngx_strlcasestrn(h->value.data, h->value.data + h->value.len,
|
|
3442 (u_char *) "chunked", 7 - 1)
|
|
3443 != NULL)
|
|
3444 {
|
|
3445 r->upstream->headers_in.chunked = 1;
|
|
3446 }
|
|
3447
|
|
3448 return NGX_OK;
|
|
3449 }
|
|
3450
|
|
3451
|
|
3452 static ngx_int_t
|
58
|
3453 ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3454 ngx_uint_t offset)
|
|
3455 {
|
122
|
3456 ngx_table_elt_t *ho, **ph;
|
58
|
3457
|
|
3458 ho = ngx_list_push(&r->headers_out.headers);
|
|
3459 if (ho == NULL) {
|
|
3460 return NGX_ERROR;
|
|
3461 }
|
|
3462
|
|
3463 *ho = *h;
|
|
3464
|
122
|
3465 if (offset) {
|
|
3466 ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset);
|
|
3467 *ph = ho;
|
|
3468 }
|
|
3469
|
58
|
3470 return NGX_OK;
|
|
3471 }
|
|
3472
|
|
3473
|
|
3474 static ngx_int_t
|
|
3475 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r,
|
|
3476 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
3477 {
|
|
3478 ngx_array_t *pa;
|
|
3479 ngx_table_elt_t *ho, **ph;
|
|
3480
|
|
3481 pa = (ngx_array_t *) ((char *) &r->headers_out + offset);
|
|
3482
|
|
3483 if (pa->elts == NULL) {
|
|
3484 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
|
|
3485 {
|
|
3486 return NGX_ERROR;
|
|
3487 }
|
|
3488 }
|
|
3489
|
|
3490 ph = ngx_array_push(pa);
|
|
3491 if (ph == NULL) {
|
|
3492 return NGX_ERROR;
|
|
3493 }
|
|
3494
|
|
3495 ho = ngx_list_push(&r->headers_out.headers);
|
|
3496 if (ho == NULL) {
|
|
3497 return NGX_ERROR;
|
|
3498 }
|
|
3499
|
|
3500 *ho = *h;
|
|
3501 *ph = ho;
|
|
3502
|
|
3503 return NGX_OK;
|
|
3504 }
|
|
3505
|
|
3506
|
|
3507 static ngx_int_t
|
|
3508 ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3509 ngx_uint_t offset)
|
|
3510 {
|
206
|
3511 u_char *p, *last;
|
|
3512
|
|
3513 r->headers_out.content_type_len = h->value.len;
|
58
|
3514 r->headers_out.content_type = h->value;
|
494
|
3515 r->headers_out.content_type_lowcase = NULL;
|
58
|
3516
|
206
|
3517 for (p = h->value.data; *p; p++) {
|
|
3518
|
|
3519 if (*p != ';') {
|
|
3520 continue;
|
|
3521 }
|
|
3522
|
|
3523 last = p;
|
|
3524
|
|
3525 while (*++p == ' ') { /* void */ }
|
|
3526
|
372
|
3527 if (*p == '\0') {
|
|
3528 return NGX_OK;
|
|
3529 }
|
|
3530
|
286
|
3531 if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) {
|
206
|
3532 continue;
|
|
3533 }
|
|
3534
|
|
3535 p += 8;
|
|
3536
|
|
3537 r->headers_out.content_type_len = last - h->value.data;
|
|
3538
|
410
|
3539 if (*p == '"') {
|
|
3540 p++;
|
|
3541 }
|
|
3542
|
|
3543 last = h->value.data + h->value.len;
|
|
3544
|
|
3545 if (*(last - 1) == '"') {
|
|
3546 last--;
|
|
3547 }
|
|
3548
|
|
3549 r->headers_out.charset.len = last - p;
|
206
|
3550 r->headers_out.charset.data = p;
|
292
|
3551
|
|
3552 return NGX_OK;
|
206
|
3553 }
|
|
3554
|
58
|
3555 return NGX_OK;
|
|
3556 }
|
|
3557
|
|
3558
|
|
3559 static ngx_int_t
|
464
|
3560 ngx_http_upstream_copy_last_modified(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3561 ngx_uint_t offset)
|
|
3562 {
|
|
3563 ngx_table_elt_t *ho;
|
|
3564
|
|
3565 ho = ngx_list_push(&r->headers_out.headers);
|
|
3566 if (ho == NULL) {
|
|
3567 return NGX_ERROR;
|
|
3568 }
|
|
3569
|
|
3570 *ho = *h;
|
|
3571
|
510
|
3572 r->headers_out.last_modified = ho;
|
|
3573
|
464
|
3574 #if (NGX_HTTP_CACHE)
|
|
3575
|
502
|
3576 if (r->upstream->cacheable) {
|
464
|
3577 r->headers_out.last_modified_time = ngx_http_parse_time(h->value.data,
|
|
3578 h->value.len);
|
|
3579 }
|
|
3580
|
|
3581 #endif
|
|
3582
|
|
3583 return NGX_OK;
|
|
3584 }
|
|
3585
|
|
3586
|
|
3587 static ngx_int_t
|
58
|
3588 ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3589 ngx_uint_t offset)
|
|
3590 {
|
|
3591 ngx_int_t rc;
|
|
3592 ngx_table_elt_t *ho;
|
|
3593
|
|
3594 ho = ngx_list_push(&r->headers_out.headers);
|
|
3595 if (ho == NULL) {
|
|
3596 return NGX_ERROR;
|
|
3597 }
|
|
3598
|
|
3599 *ho = *h;
|
|
3600
|
|
3601 if (r->upstream->rewrite_redirect) {
|
|
3602 rc = r->upstream->rewrite_redirect(r, ho, 0);
|
|
3603
|
78
|
3604 if (rc == NGX_DECLINED) {
|
|
3605 return NGX_OK;
|
|
3606 }
|
|
3607
|
58
|
3608 if (rc == NGX_OK) {
|
|
3609 r->headers_out.location = ho;
|
|
3610
|
|
3611 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
3612 "rewritten location: \"%V\"", &ho->value);
|
|
3613 }
|
|
3614
|
|
3615 return rc;
|
|
3616 }
|
|
3617
|
348
|
3618 if (ho->value.data[0] != '/') {
|
|
3619 r->headers_out.location = ho;
|
|
3620 }
|
|
3621
|
58
|
3622 /*
|
|
3623 * we do not set r->headers_out.location here to avoid the handling
|
|
3624 * the local redirects without a host name by ngx_http_header_filter()
|
|
3625 */
|
|
3626
|
|
3627 return NGX_OK;
|
|
3628 }
|
|
3629
|
|
3630
|
|
3631 static ngx_int_t
|
|
3632 ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3633 ngx_uint_t offset)
|
|
3634 {
|
|
3635 u_char *p;
|
|
3636 ngx_int_t rc;
|
|
3637 ngx_table_elt_t *ho;
|
|
3638
|
|
3639 ho = ngx_list_push(&r->headers_out.headers);
|
|
3640 if (ho == NULL) {
|
|
3641 return NGX_ERROR;
|
|
3642 }
|
|
3643
|
|
3644 *ho = *h;
|
|
3645
|
|
3646 if (r->upstream->rewrite_redirect) {
|
|
3647
|
340
|
3648 p = ngx_strcasestrn(ho->value.data, "url=", 4 - 1);
|
58
|
3649
|
|
3650 if (p) {
|
|
3651 rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data);
|
|
3652
|
|
3653 } else {
|
|
3654 return NGX_OK;
|
|
3655 }
|
|
3656
|
78
|
3657 if (rc == NGX_DECLINED) {
|
|
3658 return NGX_OK;
|
|
3659 }
|
|
3660
|
58
|
3661 if (rc == NGX_OK) {
|
348
|
3662 r->headers_out.refresh = ho;
|
|
3663
|
58
|
3664 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
3665 "rewritten refresh: \"%V\"", &ho->value);
|
|
3666 }
|
|
3667
|
|
3668 return rc;
|
|
3669 }
|
|
3670
|
348
|
3671 r->headers_out.refresh = ho;
|
|
3672
|
58
|
3673 return NGX_OK;
|
|
3674 }
|
|
3675
|
|
3676
|
464
|
3677 static ngx_int_t
|
662
|
3678 ngx_http_upstream_rewrite_set_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
3679 ngx_uint_t offset)
|
|
3680 {
|
676
|
3681 ngx_int_t rc;
|
662
|
3682 ngx_table_elt_t *ho;
|
|
3683
|
|
3684 ho = ngx_list_push(&r->headers_out.headers);
|
|
3685 if (ho == NULL) {
|
|
3686 return NGX_ERROR;
|
|
3687 }
|
|
3688
|
|
3689 *ho = *h;
|
|
3690
|
|
3691 if (r->upstream->rewrite_cookie) {
|
676
|
3692 rc = r->upstream->rewrite_cookie(r, ho);
|
|
3693
|
|
3694 if (rc == NGX_DECLINED) {
|
|
3695 return NGX_OK;
|
|
3696 }
|
|
3697
|
|
3698 #if (NGX_DEBUG)
|
|
3699 if (rc == NGX_OK) {
|
|
3700 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
3701 "rewritten cookie: \"%V\"", &ho->value);
|
|
3702 }
|
|
3703 #endif
|
|
3704
|
|
3705 return rc;
|
662
|
3706 }
|
|
3707
|
|
3708 return NGX_OK;
|
|
3709 }
|
|
3710
|
|
3711
|
|
3712 static ngx_int_t
|
464
|
3713 ngx_http_upstream_copy_allow_ranges(ngx_http_request_t *r,
|
|
3714 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
3715 {
|
|
3716 ngx_table_elt_t *ho;
|
|
3717
|
|
3718 #if (NGX_HTTP_CACHE)
|
|
3719
|
|
3720 if (r->cached) {
|
|
3721 r->allow_ranges = 1;
|
|
3722 return NGX_OK;
|
|
3723
|
|
3724 }
|
|
3725
|
|
3726 #endif
|
|
3727
|
|
3728 ho = ngx_list_push(&r->headers_out.headers);
|
|
3729 if (ho == NULL) {
|
|
3730 return NGX_ERROR;
|
|
3731 }
|
|
3732
|
|
3733 *ho = *h;
|
|
3734
|
510
|
3735 r->headers_out.accept_ranges = ho;
|
|
3736
|
464
|
3737 return NGX_OK;
|
|
3738 }
|
|
3739
|
|
3740
|
58
|
3741 #if (NGX_HTTP_GZIP)
|
|
3742
|
|
3743 static ngx_int_t
|
|
3744 ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r,
|
|
3745 ngx_table_elt_t *h, ngx_uint_t offset)
|
|
3746 {
|
|
3747 ngx_table_elt_t *ho;
|
|
3748
|
|
3749 ho = ngx_list_push(&r->headers_out.headers);
|
|
3750 if (ho == NULL) {
|
|
3751 return NGX_ERROR;
|
|
3752 }
|
|
3753
|
|
3754 *ho = *h;
|
|
3755
|
|
3756 r->headers_out.content_encoding = ho;
|
|
3757
|
|
3758 return NGX_OK;
|
|
3759 }
|
|
3760
|
|
3761 #endif
|
|
3762
|
|
3763
|
36
|
3764 static ngx_int_t
|
122
|
3765 ngx_http_upstream_add_variables(ngx_conf_t *cf)
|
28
|
3766 {
|
260
|
3767 ngx_http_variable_t *var, *v;
|
28
|
3768
|
122
|
3769 for (v = ngx_http_upstream_vars; v->name.len; v++) {
|
|
3770 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
3771 if (var == NULL) {
|
|
3772 return NGX_ERROR;
|
|
3773 }
|
|
3774
|
186
|
3775 var->get_handler = v->get_handler;
|
122
|
3776 var->data = v->data;
|
|
3777 }
|
|
3778
|
28
|
3779 return NGX_OK;
|
|
3780 }
|
58
|
3781
|
|
3782
|
122
|
3783 static ngx_int_t
|
298
|
3784 ngx_http_upstream_addr_variable(ngx_http_request_t *r,
|
|
3785 ngx_http_variable_value_t *v, uintptr_t data)
|
|
3786 {
|
|
3787 u_char *p;
|
|
3788 size_t len;
|
|
3789 ngx_uint_t i;
|
|
3790 ngx_http_upstream_state_t *state;
|
|
3791
|
|
3792 v->valid = 1;
|
340
|
3793 v->no_cacheable = 0;
|
298
|
3794 v->not_found = 0;
|
|
3795
|
|
3796 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
|
|
3797 v->not_found = 1;
|
|
3798 return NGX_OK;
|
|
3799 }
|
|
3800
|
|
3801 len = 0;
|
|
3802 state = r->upstream_states->elts;
|
|
3803
|
|
3804 for (i = 0; i < r->upstream_states->nelts; i++) {
|
|
3805 if (state[i].peer) {
|
|
3806 len += state[i].peer->len + 2;
|
|
3807
|
|
3808 } else {
|
|
3809 len += 3;
|
|
3810 }
|
|
3811 }
|
|
3812
|
382
|
3813 p = ngx_pnalloc(r->pool, len);
|
298
|
3814 if (p == NULL) {
|
|
3815 return NGX_ERROR;
|
|
3816 }
|
|
3817
|
|
3818 v->data = p;
|
|
3819
|
|
3820 i = 0;
|
|
3821
|
|
3822 for ( ;; ) {
|
|
3823 if (state[i].peer) {
|
|
3824 p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len);
|
|
3825 }
|
|
3826
|
|
3827 if (++i == r->upstream_states->nelts) {
|
|
3828 break;
|
|
3829 }
|
|
3830
|
|
3831 if (state[i].peer) {
|
|
3832 *p++ = ',';
|
|
3833 *p++ = ' ';
|
|
3834
|
|
3835 } else {
|
|
3836 *p++ = ' ';
|
|
3837 *p++ = ':';
|
|
3838 *p++ = ' ';
|
|
3839
|
|
3840 if (++i == r->upstream_states->nelts) {
|
|
3841 break;
|
|
3842 }
|
|
3843
|
|
3844 continue;
|
|
3845 }
|
|
3846 }
|
|
3847
|
|
3848 v->len = p - v->data;
|
|
3849
|
|
3850 return NGX_OK;
|
|
3851 }
|
|
3852
|
|
3853
|
|
3854 static ngx_int_t
|
122
|
3855 ngx_http_upstream_status_variable(ngx_http_request_t *r,
|
|
3856 ngx_http_variable_value_t *v, uintptr_t data)
|
|
3857 {
|
|
3858 u_char *p;
|
|
3859 size_t len;
|
|
3860 ngx_uint_t i;
|
|
3861 ngx_http_upstream_state_t *state;
|
|
3862
|
|
3863 v->valid = 1;
|
340
|
3864 v->no_cacheable = 0;
|
122
|
3865 v->not_found = 0;
|
|
3866
|
296
|
3867 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
|
122
|
3868 v->not_found = 1;
|
|
3869 return NGX_OK;
|
|
3870 }
|
|
3871
|
296
|
3872 len = r->upstream_states->nelts * (3 + 2);
|
122
|
3873
|
382
|
3874 p = ngx_pnalloc(r->pool, len);
|
122
|
3875 if (p == NULL) {
|
|
3876 return NGX_ERROR;
|
|
3877 }
|
|
3878
|
|
3879 v->data = p;
|
|
3880
|
|
3881 i = 0;
|
296
|
3882 state = r->upstream_states->elts;
|
122
|
3883
|
|
3884 for ( ;; ) {
|
296
|
3885 if (state[i].status) {
|
|
3886 p = ngx_sprintf(p, "%ui", state[i].status);
|
122
|
3887
|
|
3888 } else {
|
296
|
3889 *p++ = '-';
|
122
|
3890 }
|
|
3891
|
296
|
3892 if (++i == r->upstream_states->nelts) {
|
122
|
3893 break;
|
|
3894 }
|
|
3895
|
296
|
3896 if (state[i].peer) {
|
|
3897 *p++ = ',';
|
|
3898 *p++ = ' ';
|
|
3899
|
|
3900 } else {
|
|
3901 *p++ = ' ';
|
|
3902 *p++ = ':';
|
|
3903 *p++ = ' ';
|
|
3904
|
|
3905 if (++i == r->upstream_states->nelts) {
|
|
3906 break;
|
|
3907 }
|
|
3908
|
|
3909 continue;
|
|
3910 }
|
122
|
3911 }
|
|
3912
|
|
3913 v->len = p - v->data;
|
|
3914
|
|
3915 return NGX_OK;
|
|
3916 }
|
|
3917
|
|
3918
|
|
3919 static ngx_int_t
|
|
3920 ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
|
|
3921 ngx_http_variable_value_t *v, uintptr_t data)
|
|
3922 {
|
|
3923 u_char *p;
|
|
3924 size_t len;
|
|
3925 ngx_uint_t i;
|
262
|
3926 ngx_msec_int_t ms;
|
122
|
3927 ngx_http_upstream_state_t *state;
|
|
3928
|
|
3929 v->valid = 1;
|
340
|
3930 v->no_cacheable = 0;
|
122
|
3931 v->not_found = 0;
|
|
3932
|
296
|
3933 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
|
122
|
3934 v->not_found = 1;
|
|
3935 return NGX_OK;
|
|
3936 }
|
|
3937
|
296
|
3938 len = r->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2);
|
122
|
3939
|
382
|
3940 p = ngx_pnalloc(r->pool, len);
|
122
|
3941 if (p == NULL) {
|
|
3942 return NGX_ERROR;
|
|
3943 }
|
|
3944
|
|
3945 v->data = p;
|
|
3946
|
|
3947 i = 0;
|
296
|
3948 state = r->upstream_states->elts;
|
122
|
3949
|
|
3950 for ( ;; ) {
|
296
|
3951 if (state[i].status) {
|
346
|
3952 ms = (ngx_msec_int_t)
|
|
3953 (state[i].response_sec * 1000 + state[i].response_msec);
|
570
|
3954 ms = ngx_max(ms, 0);
|
262
|
3955 p = ngx_sprintf(p, "%d.%03d", ms / 1000, ms % 1000);
|
296
|
3956
|
|
3957 } else {
|
|
3958 *p++ = '-';
|
122
|
3959 }
|
|
3960
|
296
|
3961 if (++i == r->upstream_states->nelts) {
|
122
|
3962 break;
|
|
3963 }
|
|
3964
|
296
|
3965 if (state[i].peer) {
|
|
3966 *p++ = ',';
|
|
3967 *p++ = ' ';
|
|
3968
|
|
3969 } else {
|
|
3970 *p++ = ' ';
|
|
3971 *p++ = ':';
|
|
3972 *p++ = ' ';
|
|
3973
|
|
3974 if (++i == r->upstream_states->nelts) {
|
|
3975 break;
|
|
3976 }
|
|
3977
|
|
3978 continue;
|
|
3979 }
|
122
|
3980 }
|
|
3981
|
|
3982 v->len = p - v->data;
|
|
3983
|
|
3984 return NGX_OK;
|
|
3985 }
|
|
3986
|
|
3987
|
430
|
3988 static ngx_int_t
|
|
3989 ngx_http_upstream_response_length_variable(ngx_http_request_t *r,
|
|
3990 ngx_http_variable_value_t *v, uintptr_t data)
|
|
3991 {
|
|
3992 u_char *p;
|
|
3993 size_t len;
|
|
3994 ngx_uint_t i;
|
|
3995 ngx_http_upstream_state_t *state;
|
|
3996
|
|
3997 v->valid = 1;
|
|
3998 v->no_cacheable = 0;
|
|
3999 v->not_found = 0;
|
|
4000
|
|
4001 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
|
|
4002 v->not_found = 1;
|
|
4003 return NGX_OK;
|
|
4004 }
|
|
4005
|
|
4006 len = r->upstream_states->nelts * (NGX_OFF_T_LEN + 2);
|
|
4007
|
|
4008 p = ngx_pnalloc(r->pool, len);
|
|
4009 if (p == NULL) {
|
|
4010 return NGX_ERROR;
|
|
4011 }
|
|
4012
|
|
4013 v->data = p;
|
|
4014
|
|
4015 i = 0;
|
|
4016 state = r->upstream_states->elts;
|
|
4017
|
|
4018 for ( ;; ) {
|
|
4019 p = ngx_sprintf(p, "%O", state[i].response_length);
|
|
4020
|
|
4021 if (++i == r->upstream_states->nelts) {
|
|
4022 break;
|
|
4023 }
|
|
4024
|
|
4025 if (state[i].peer) {
|
|
4026 *p++ = ',';
|
|
4027 *p++ = ' ';
|
|
4028
|
|
4029 } else {
|
|
4030 *p++ = ' ';
|
|
4031 *p++ = ':';
|
|
4032 *p++ = ' ';
|
|
4033
|
|
4034 if (++i == r->upstream_states->nelts) {
|
|
4035 break;
|
|
4036 }
|
|
4037
|
|
4038 continue;
|
|
4039 }
|
|
4040 }
|
|
4041
|
|
4042 v->len = p - v->data;
|
|
4043
|
|
4044 return NGX_OK;
|
|
4045 }
|
|
4046
|
|
4047
|
296
|
4048 ngx_int_t
|
|
4049 ngx_http_upstream_header_variable(ngx_http_request_t *r,
|
|
4050 ngx_http_variable_value_t *v, uintptr_t data)
|
|
4051 {
|
|
4052 if (r->upstream == NULL) {
|
|
4053 v->not_found = 1;
|
|
4054 return NGX_OK;
|
|
4055 }
|
|
4056
|
|
4057 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
|
|
4058 &r->upstream->headers_in.headers.part,
|
|
4059 sizeof("upstream_http_") - 1);
|
|
4060 }
|
|
4061
|
|
4062
|
502
|
4063 #if (NGX_HTTP_CACHE)
|
|
4064
|
|
4065 ngx_int_t
|
|
4066 ngx_http_upstream_cache_status(ngx_http_request_t *r,
|
|
4067 ngx_http_variable_value_t *v, uintptr_t data)
|
|
4068 {
|
|
4069 ngx_uint_t n;
|
|
4070
|
|
4071 if (r->upstream == NULL || r->upstream->cache_status == 0) {
|
|
4072 v->not_found = 1;
|
|
4073 return NGX_OK;
|
|
4074 }
|
|
4075
|
|
4076 n = r->upstream->cache_status - 1;
|
|
4077
|
|
4078 v->valid = 1;
|
|
4079 v->no_cacheable = 0;
|
|
4080 v->not_found = 0;
|
|
4081 v->len = ngx_http_cache_status[n].len;
|
|
4082 v->data = ngx_http_cache_status[n].data;
|
|
4083
|
|
4084 return NGX_OK;
|
|
4085 }
|
|
4086
|
|
4087 #endif
|
|
4088
|
|
4089
|
200
|
4090 static char *
|
|
4091 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
|
|
4092 {
|
260
|
4093 char *rv;
|
|
4094 void *mconf;
|
|
4095 ngx_str_t *value;
|
|
4096 ngx_url_t u;
|
|
4097 ngx_uint_t m;
|
|
4098 ngx_conf_t pcf;
|
|
4099 ngx_http_module_t *module;
|
|
4100 ngx_http_conf_ctx_t *ctx, *http_ctx;
|
|
4101 ngx_http_upstream_srv_conf_t *uscf;
|
|
4102
|
|
4103 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
4104
|
|
4105 value = cf->args->elts;
|
|
4106 u.host = value[1];
|
|
4107 u.no_resolve = 1;
|
|
4108
|
|
4109 uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE
|
|
4110 |NGX_HTTP_UPSTREAM_WEIGHT
|
|
4111 |NGX_HTTP_UPSTREAM_MAX_FAILS
|
|
4112 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
|
|
4113 |NGX_HTTP_UPSTREAM_DOWN
|
|
4114 |NGX_HTTP_UPSTREAM_BACKUP);
|
|
4115 if (uscf == NULL) {
|
|
4116 return NGX_CONF_ERROR;
|
|
4117 }
|
|
4118
|
200
|
4119
|
|
4120 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
|
|
4121 if (ctx == NULL) {
|
|
4122 return NGX_CONF_ERROR;
|
|
4123 }
|
|
4124
|
260
|
4125 http_ctx = cf->ctx;
|
|
4126 ctx->main_conf = http_ctx->main_conf;
|
200
|
4127
|
|
4128 /* the upstream{}'s srv_conf */
|
|
4129
|
|
4130 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
4131 if (ctx->srv_conf == NULL) {
|
|
4132 return NGX_CONF_ERROR;
|
|
4133 }
|
|
4134
|
|
4135 ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf;
|
|
4136
|
260
|
4137 uscf->srv_conf = ctx->srv_conf;
|
|
4138
|
200
|
4139
|
|
4140 /* the upstream{}'s loc_conf */
|
|
4141
|
|
4142 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
4143 if (ctx->loc_conf == NULL) {
|
|
4144 return NGX_CONF_ERROR;
|
|
4145 }
|
|
4146
|
|
4147 for (m = 0; ngx_modules[m]; m++) {
|
|
4148 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
4149 continue;
|
|
4150 }
|
|
4151
|
|
4152 module = ngx_modules[m]->ctx;
|
|
4153
|
260
|
4154 if (module->create_srv_conf) {
|
|
4155 mconf = module->create_srv_conf(cf);
|
|
4156 if (mconf == NULL) {
|
|
4157 return NGX_CONF_ERROR;
|
|
4158 }
|
|
4159
|
|
4160 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
|
|
4161 }
|
|
4162
|
200
|
4163 if (module->create_loc_conf) {
|
|
4164 mconf = module->create_loc_conf(cf);
|
|
4165 if (mconf == NULL) {
|
|
4166 return NGX_CONF_ERROR;
|
|
4167 }
|
|
4168
|
|
4169 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf;
|
|
4170 }
|
|
4171 }
|
|
4172
|
|
4173
|
|
4174 /* parse inside upstream{} */
|
|
4175
|
|
4176 pcf = *cf;
|
|
4177 cf->ctx = ctx;
|
|
4178 cf->cmd_type = NGX_HTTP_UPS_CONF;
|
|
4179
|
|
4180 rv = ngx_conf_parse(cf, NULL);
|
|
4181
|
|
4182 *cf = pcf;
|
|
4183
|
|
4184 if (rv != NGX_CONF_OK) {
|
|
4185 return rv;
|
|
4186 }
|
|
4187
|
|
4188 if (uscf->servers == NULL) {
|
|
4189 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
4190 "no servers are inside upstream");
|
|
4191 return NGX_CONF_ERROR;
|
|
4192 }
|
|
4193
|
|
4194 return rv;
|
|
4195 }
|
|
4196
|
|
4197
|
|
4198 static char *
|
|
4199 ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
4200 {
|
|
4201 ngx_http_upstream_srv_conf_t *uscf = conf;
|
|
4202
|
260
|
4203 time_t fail_timeout;
|
|
4204 ngx_str_t *value, s;
|
|
4205 ngx_url_t u;
|
|
4206 ngx_int_t weight, max_fails;
|
|
4207 ngx_uint_t i;
|
|
4208 ngx_http_upstream_server_t *us;
|
200
|
4209
|
|
4210 if (uscf->servers == NULL) {
|
260
|
4211 uscf->servers = ngx_array_create(cf->pool, 4,
|
|
4212 sizeof(ngx_http_upstream_server_t));
|
200
|
4213 if (uscf->servers == NULL) {
|
|
4214 return NGX_CONF_ERROR;
|
|
4215 }
|
|
4216 }
|
|
4217
|
260
|
4218 us = ngx_array_push(uscf->servers);
|
|
4219 if (us == NULL) {
|
200
|
4220 return NGX_CONF_ERROR;
|
|
4221 }
|
|
4222
|
260
|
4223 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
|
|
4224
|
200
|
4225 value = cf->args->elts;
|
|
4226
|
|
4227 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
4228
|
|
4229 u.url = value[1];
|
266
|
4230 u.default_port = 80;
|
200
|
4231
|
340
|
4232 if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
|
200
|
4233 if (u.err) {
|
|
4234 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
4235 "%s in upstream \"%V\"", u.err, &u.url);
|
|
4236 }
|
|
4237
|
|
4238 return NGX_CONF_ERROR;
|
|
4239 }
|
|
4240
|
212
|
4241 weight = 1;
|
260
|
4242 max_fails = 1;
|
|
4243 fail_timeout = 10;
|
|
4244
|
|
4245 for (i = 2; i < cf->args->nelts; i++) {
|
|
4246
|
|
4247 if (ngx_strncmp(value[i].data, "weight=", 7) == 0) {
|
|
4248
|
|
4249 if (!(uscf->flags & NGX_HTTP_UPSTREAM_WEIGHT)) {
|
|
4250 goto invalid;
|
|
4251 }
|
|
4252
|
|
4253 weight = ngx_atoi(&value[i].data[7], value[i].len - 7);
|
212
|
4254
|
|
4255 if (weight == NGX_ERROR || weight == 0) {
|
|
4256 goto invalid;
|
|
4257 }
|
|
4258
|
260
|
4259 continue;
|
|
4260 }
|
|
4261
|
|
4262 if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) {
|
|
4263
|
|
4264 if (!(uscf->flags & NGX_HTTP_UPSTREAM_MAX_FAILS)) {
|
|
4265 goto invalid;
|
|
4266 }
|
|
4267
|
|
4268 max_fails = ngx_atoi(&value[i].data[10], value[i].len - 10);
|
|
4269
|
|
4270 if (max_fails == NGX_ERROR) {
|
|
4271 goto invalid;
|
|
4272 }
|
|
4273
|
|
4274 continue;
|
212
|
4275 }
|
260
|
4276
|
|
4277 if (ngx_strncmp(value[i].data, "fail_timeout=", 13) == 0) {
|
|
4278
|
|
4279 if (!(uscf->flags & NGX_HTTP_UPSTREAM_FAIL_TIMEOUT)) {
|
|
4280 goto invalid;
|
|
4281 }
|
|
4282
|
|
4283 s.len = value[i].len - 13;
|
|
4284 s.data = &value[i].data[13];
|
|
4285
|
|
4286 fail_timeout = ngx_parse_time(&s, 1);
|
|
4287
|
662
|
4288 if (fail_timeout == (time_t) NGX_ERROR) {
|
260
|
4289 goto invalid;
|
|
4290 }
|
|
4291
|
|
4292 continue;
|
|
4293 }
|
|
4294
|
326
|
4295 if (ngx_strncmp(value[i].data, "backup", 6) == 0) {
|
|
4296
|
|
4297 if (!(uscf->flags & NGX_HTTP_UPSTREAM_BACKUP)) {
|
|
4298 goto invalid;
|
|
4299 }
|
|
4300
|
|
4301 us->backup = 1;
|
|
4302
|
|
4303 continue;
|
|
4304 }
|
|
4305
|
260
|
4306 if (ngx_strncmp(value[i].data, "down", 4) == 0) {
|
|
4307
|
|
4308 if (!(uscf->flags & NGX_HTTP_UPSTREAM_DOWN)) {
|
|
4309 goto invalid;
|
|
4310 }
|
|
4311
|
|
4312 us->down = 1;
|
|
4313
|
|
4314 continue;
|
|
4315 }
|
|
4316
|
|
4317 goto invalid;
|
212
|
4318 }
|
|
4319
|
260
|
4320 us->addrs = u.addrs;
|
|
4321 us->naddrs = u.naddrs;
|
|
4322 us->weight = weight;
|
|
4323 us->max_fails = max_fails;
|
|
4324 us->fail_timeout = fail_timeout;
|
200
|
4325
|
|
4326 return NGX_CONF_OK;
|
212
|
4327
|
|
4328 invalid:
|
|
4329
|
260
|
4330 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
4331 "invalid parameter \"%V\"", &value[i]);
|
212
|
4332
|
|
4333 return NGX_CONF_ERROR;
|
200
|
4334 }
|
|
4335
|
|
4336
|
|
4337 ngx_http_upstream_srv_conf_t *
|
260
|
4338 ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
|
200
|
4339 {
|
|
4340 ngx_uint_t i;
|
260
|
4341 ngx_http_upstream_server_t *us;
|
200
|
4342 ngx_http_upstream_srv_conf_t *uscf, **uscfp;
|
|
4343 ngx_http_upstream_main_conf_t *umcf;
|
|
4344
|
260
|
4345 if (!(flags & NGX_HTTP_UPSTREAM_CREATE)) {
|
|
4346
|
340
|
4347 if (ngx_parse_url(cf->pool, u) != NGX_OK) {
|
200
|
4348 if (u->err) {
|
|
4349 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
4350 "%s in upstream \"%V\"", u->err, &u->url);
|
|
4351 }
|
|
4352
|
|
4353 return NULL;
|
|
4354 }
|
|
4355 }
|
|
4356
|
|
4357 umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module);
|
|
4358
|
|
4359 uscfp = umcf->upstreams.elts;
|
|
4360
|
|
4361 for (i = 0; i < umcf->upstreams.nelts; i++) {
|
266
|
4362
|
|
4363 if (uscfp[i]->host.len != u->host.len
|
260
|
4364 || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)
|
|
4365 != 0)
|
|
4366 {
|
200
|
4367 continue;
|
|
4368 }
|
|
4369
|
260
|
4370 if ((flags & NGX_HTTP_UPSTREAM_CREATE)
|
|
4371 && (uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE))
|
200
|
4372 {
|
260
|
4373 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
4374 "duplicate upstream \"%V\"", &u->host);
|
|
4375 return NULL;
|
200
|
4376 }
|
260
|
4377
|
266
|
4378 if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) {
|
260
|
4379 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
266
|
4380 "upstream \"%V\" may not have port %d",
|
|
4381 &u->host, u->port);
|
|
4382 return NULL;
|
|
4383 }
|
|
4384
|
|
4385 if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) {
|
|
4386 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
|
4387 "upstream \"%V\" may not have port %d in %s:%ui",
|
|
4388 &u->host, uscfp[i]->port,
|
336
|
4389 uscfp[i]->file_name, uscfp[i]->line);
|
266
|
4390 return NULL;
|
|
4391 }
|
|
4392
|
|
4393 if (uscfp[i]->port != u->port) {
|
|
4394 continue;
|
260
|
4395 }
|
|
4396
|
324
|
4397 if (uscfp[i]->default_port && u->default_port
|
|
4398 && uscfp[i]->default_port != u->default_port)
|
|
4399 {
|
|
4400 continue;
|
|
4401 }
|
|
4402
|
642
|
4403 if (flags & NGX_HTTP_UPSTREAM_CREATE) {
|
|
4404 uscfp[i]->flags = flags;
|
|
4405 }
|
|
4406
|
260
|
4407 return uscfp[i];
|
200
|
4408 }
|
|
4409
|
|
4410 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t));
|
|
4411 if (uscf == NULL) {
|
|
4412 return NULL;
|
|
4413 }
|
|
4414
|
260
|
4415 uscf->flags = flags;
|
200
|
4416 uscf->host = u->host;
|
336
|
4417 uscf->file_name = cf->conf_file->file.name.data;
|
200
|
4418 uscf->line = cf->conf_file->line;
|
266
|
4419 uscf->port = u->port;
|
|
4420 uscf->default_port = u->default_port;
|
260
|
4421
|
|
4422 if (u->naddrs == 1) {
|
|
4423 uscf->servers = ngx_array_create(cf->pool, 1,
|
|
4424 sizeof(ngx_http_upstream_server_t));
|
|
4425 if (uscf->servers == NULL) {
|
682
|
4426 return NULL;
|
260
|
4427 }
|
|
4428
|
|
4429 us = ngx_array_push(uscf->servers);
|
|
4430 if (us == NULL) {
|
682
|
4431 return NULL;
|
260
|
4432 }
|
|
4433
|
|
4434 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
|
|
4435
|
|
4436 us->addrs = u->addrs;
|
682
|
4437 us->naddrs = 1;
|
260
|
4438 }
|
200
|
4439
|
|
4440 uscfp = ngx_array_push(&umcf->upstreams);
|
|
4441 if (uscfp == NULL) {
|
|
4442 return NULL;
|
|
4443 }
|
|
4444
|
|
4445 *uscfp = uscf;
|
|
4446
|
|
4447 return uscf;
|
|
4448 }
|
|
4449
|
|
4450
|
540
|
4451 char *
|
558
|
4452 ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
540
|
4453 void *conf)
|
|
4454 {
|
|
4455 char *p = conf;
|
|
4456
|
|
4457 ngx_int_t rc;
|
|
4458 ngx_str_t *value;
|
|
4459 ngx_addr_t **paddr;
|
|
4460
|
|
4461 paddr = (ngx_addr_t **) (p + cmd->offset);
|
|
4462
|
|
4463 *paddr = ngx_palloc(cf->pool, sizeof(ngx_addr_t));
|
|
4464 if (*paddr == NULL) {
|
|
4465 return NGX_CONF_ERROR;
|
|
4466 }
|
|
4467
|
|
4468 value = cf->args->elts;
|
|
4469
|
|
4470 rc = ngx_parse_addr(cf->pool, *paddr, value[1].data, value[1].len);
|
|
4471
|
|
4472 switch (rc) {
|
|
4473 case NGX_OK:
|
|
4474 (*paddr)->name = value[1];
|
|
4475 return NGX_CONF_OK;
|
|
4476
|
|
4477 case NGX_DECLINED:
|
|
4478 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
4479 "invalid address \"%V\"", &value[1]);
|
668
|
4480 /* fall through */
|
|
4481
|
540
|
4482 default:
|
|
4483 return NGX_CONF_ERROR;
|
|
4484 }
|
|
4485 }
|
|
4486
|
|
4487
|
654
|
4488 char *
|
|
4489 ngx_http_upstream_param_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
4490 void *conf)
|
|
4491 {
|
|
4492 char *p = conf;
|
|
4493
|
|
4494 ngx_str_t *value;
|
|
4495 ngx_array_t **a;
|
|
4496 ngx_http_upstream_param_t *param;
|
|
4497
|
|
4498 a = (ngx_array_t **) (p + cmd->offset);
|
|
4499
|
|
4500 if (*a == NULL) {
|
|
4501 *a = ngx_array_create(cf->pool, 4, sizeof(ngx_http_upstream_param_t));
|
|
4502 if (*a == NULL) {
|
|
4503 return NGX_CONF_ERROR;
|
|
4504 }
|
|
4505 }
|
|
4506
|
|
4507 param = ngx_array_push(*a);
|
|
4508 if (param == NULL) {
|
|
4509 return NGX_CONF_ERROR;
|
|
4510 }
|
|
4511
|
|
4512 value = cf->args->elts;
|
|
4513
|
|
4514 param->key = value[1];
|
|
4515 param->value = value[2];
|
|
4516 param->skip_empty = 0;
|
|
4517
|
|
4518 if (cf->args->nelts == 4) {
|
|
4519 if (ngx_strcmp(value[3].data, "if_not_empty") != 0) {
|
|
4520 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
4521 "invalid parameter \"%V\"", &value[3]);
|
|
4522 return NGX_CONF_ERROR;
|
|
4523 }
|
|
4524
|
|
4525 param->skip_empty = 1;
|
|
4526 }
|
|
4527
|
|
4528 return NGX_CONF_OK;
|
|
4529 }
|
|
4530
|
|
4531
|
356
|
4532 ngx_int_t
|
|
4533 ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
|
|
4534 ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
|
|
4535 ngx_str_t *default_hide_headers, ngx_hash_init_t *hash)
|
|
4536 {
|
|
4537 ngx_str_t *h;
|
|
4538 ngx_uint_t i, j;
|
|
4539 ngx_array_t hide_headers;
|
|
4540 ngx_hash_key_t *hk;
|
|
4541
|
|
4542 if (conf->hide_headers == NGX_CONF_UNSET_PTR
|
|
4543 && conf->pass_headers == NGX_CONF_UNSET_PTR)
|
|
4544 {
|
682
|
4545 conf->hide_headers = prev->hide_headers;
|
|
4546 conf->pass_headers = prev->pass_headers;
|
|
4547
|
356
|
4548 conf->hide_headers_hash = prev->hide_headers_hash;
|
|
4549
|
498
|
4550 if (conf->hide_headers_hash.buckets
|
502
|
4551 #if (NGX_HTTP_CACHE)
|
|
4552 && ((conf->cache == NULL) == (prev->cache == NULL))
|
|
4553 #endif
|
|
4554 )
|
498
|
4555 {
|
356
|
4556 return NGX_OK;
|
|
4557 }
|
|
4558
|
|
4559 } else {
|
|
4560 if (conf->hide_headers == NGX_CONF_UNSET_PTR) {
|
|
4561 conf->hide_headers = prev->hide_headers;
|
|
4562 }
|
|
4563
|
|
4564 if (conf->pass_headers == NGX_CONF_UNSET_PTR) {
|
|
4565 conf->pass_headers = prev->pass_headers;
|
|
4566 }
|
|
4567 }
|
|
4568
|
|
4569 if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
|
|
4570 != NGX_OK)
|
|
4571 {
|
|
4572 return NGX_ERROR;
|
|
4573 }
|
|
4574
|
|
4575 for (h = default_hide_headers; h->len; h++) {
|
|
4576 hk = ngx_array_push(&hide_headers);
|
|
4577 if (hk == NULL) {
|
|
4578 return NGX_ERROR;
|
|
4579 }
|
|
4580
|
|
4581 hk->key = *h;
|
|
4582 hk->key_hash = ngx_hash_key_lc(h->data, h->len);
|
|
4583 hk->value = (void *) 1;
|
|
4584 }
|
|
4585
|
|
4586 if (conf->hide_headers != NGX_CONF_UNSET_PTR) {
|
|
4587
|
|
4588 h = conf->hide_headers->elts;
|
|
4589
|
|
4590 for (i = 0; i < conf->hide_headers->nelts; i++) {
|
|
4591
|
|
4592 hk = hide_headers.elts;
|
|
4593
|
|
4594 for (j = 0; j < hide_headers.nelts; j++) {
|
|
4595 if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {
|
|
4596 goto exist;
|
|
4597 }
|
|
4598 }
|
|
4599
|
|
4600 hk = ngx_array_push(&hide_headers);
|
|
4601 if (hk == NULL) {
|
|
4602 return NGX_ERROR;
|
|
4603 }
|
|
4604
|
|
4605 hk->key = h[i];
|
|
4606 hk->key_hash = ngx_hash_key_lc(h[i].data, h[i].len);
|
|
4607 hk->value = (void *) 1;
|
|
4608
|
|
4609 exist:
|
|
4610
|
|
4611 continue;
|
|
4612 }
|
|
4613 }
|
|
4614
|
|
4615 if (conf->pass_headers != NGX_CONF_UNSET_PTR) {
|
|
4616
|
|
4617 h = conf->pass_headers->elts;
|
|
4618 hk = hide_headers.elts;
|
|
4619
|
|
4620 for (i = 0; i < conf->pass_headers->nelts; i++) {
|
|
4621 for (j = 0; j < hide_headers.nelts; j++) {
|
|
4622
|
|
4623 if (hk[j].key.data == NULL) {
|
|
4624 continue;
|
|
4625 }
|
|
4626
|
|
4627 if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {
|
|
4628 hk[j].key.data = NULL;
|
|
4629 break;
|
|
4630 }
|
|
4631 }
|
|
4632 }
|
|
4633 }
|
|
4634
|
|
4635 hash->hash = &conf->hide_headers_hash;
|
|
4636 hash->key = ngx_hash_key_lc;
|
|
4637 hash->pool = cf->pool;
|
|
4638 hash->temp_pool = NULL;
|
|
4639
|
|
4640 return ngx_hash_init(hash, hide_headers.elts, hide_headers.nelts);
|
|
4641 }
|
|
4642
|
|
4643
|
58
|
4644 static void *
|
|
4645 ngx_http_upstream_create_main_conf(ngx_conf_t *cf)
|
|
4646 {
|
|
4647 ngx_http_upstream_main_conf_t *umcf;
|
|
4648
|
|
4649 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t));
|
|
4650 if (umcf == NULL) {
|
|
4651 return NULL;
|
|
4652 }
|
|
4653
|
200
|
4654 if (ngx_array_init(&umcf->upstreams, cf->pool, 4,
|
|
4655 sizeof(ngx_http_upstream_srv_conf_t *))
|
|
4656 != NGX_OK)
|
|
4657 {
|
496
|
4658 return NULL;
|
200
|
4659 }
|
|
4660
|
58
|
4661 return umcf;
|
|
4662 }
|
|
4663
|
|
4664
|
|
4665 static char *
|
200
|
4666 ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf)
|
58
|
4667 {
|
|
4668 ngx_http_upstream_main_conf_t *umcf = conf;
|
|
4669
|
200
|
4670 ngx_uint_t i;
|
|
4671 ngx_array_t headers_in;
|
|
4672 ngx_hash_key_t *hk;
|
|
4673 ngx_hash_init_t hash;
|
260
|
4674 ngx_http_upstream_init_pt init;
|
200
|
4675 ngx_http_upstream_header_t *header;
|
|
4676 ngx_http_upstream_srv_conf_t **uscfp;
|
|
4677
|
|
4678 uscfp = umcf->upstreams.elts;
|
|
4679
|
|
4680 for (i = 0; i < umcf->upstreams.nelts; i++) {
|
260
|
4681
|
|
4682 init = uscfp[i]->peer.init_upstream ? uscfp[i]->peer.init_upstream:
|
|
4683 ngx_http_upstream_init_round_robin;
|
|
4684
|
|
4685 if (init(cf, uscfp[i]) != NGX_OK) {
|
200
|
4686 return NGX_CONF_ERROR;
|
|
4687 }
|
|
4688 }
|
198
|
4689
|
212
|
4690
|
260
|
4691 /* upstream_headers_in_hash */
|
|
4692
|
198
|
4693 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
|
|
4694 != NGX_OK)
|
58
|
4695 {
|
|
4696 return NGX_CONF_ERROR;
|
|
4697 }
|
|
4698
|
198
|
4699 for (header = ngx_http_upstream_headers_in; header->name.len; header++) {
|
|
4700 hk = ngx_array_push(&headers_in);
|
|
4701 if (hk == NULL) {
|
|
4702 return NGX_CONF_ERROR;
|
|
4703 }
|
|
4704
|
|
4705 hk->key = header->name;
|
|
4706 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
|
|
4707 hk->value = header;
|
|
4708 }
|
|
4709
|
|
4710 hash.hash = &umcf->headers_in_hash;
|
|
4711 hash.key = ngx_hash_key_lc;
|
|
4712 hash.max_size = 512;
|
244
|
4713 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
|
198
|
4714 hash.name = "upstream_headers_in_hash";
|
|
4715 hash.pool = cf->pool;
|
|
4716 hash.temp_pool = NULL;
|
|
4717
|
|
4718 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
|
|
4719 return NGX_CONF_ERROR;
|
|
4720 }
|
58
|
4721
|
|
4722 return NGX_CONF_OK;
|
|
4723 }
|