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