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