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