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