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