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