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