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