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