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