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