blob: f567a438e3e11063e5aacd3e195ed9d3a32ae417 [file] [log] [blame]
Igor Sysoev02025fd2005-01-18 13:03:58 +00001
2/*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7#include <ngx_config.h>
8#include <ngx_core.h>
Igor Sysoev899b44e2005-05-12 14:58:06 +00009#include <ngx_http.h>
Igor Sysoev02025fd2005-01-18 13:03:58 +000010
11
Igor Sysoev96dd8af2007-11-27 13:34:13 +000012static void ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx);
Igor Sysoev899b44e2005-05-12 14:58:06 +000013static void ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r);
14static void ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r);
15static void ngx_http_upstream_check_broken_connection(ngx_http_request_t *r,
16 ngx_event_t *ev);
Igor Sysoev02025fd2005-01-18 13:03:58 +000017static void ngx_http_upstream_connect(ngx_http_request_t *r,
Igor Sysoevaa828612005-02-09 14:31:07 +000018 ngx_http_upstream_t *u);
Igor Sysoev899b44e2005-05-12 14:58:06 +000019static ngx_int_t ngx_http_upstream_reinit(ngx_http_request_t *r,
Igor Sysoevaa828612005-02-09 14:31:07 +000020 ngx_http_upstream_t *u);
Igor Sysoev02025fd2005-01-18 13:03:58 +000021static void ngx_http_upstream_send_request(ngx_http_request_t *r,
Igor Sysoevaa828612005-02-09 14:31:07 +000022 ngx_http_upstream_t *u);
Igor Sysoevb9409a82008-12-09 17:25:03 +000023static void ngx_http_upstream_send_request_handler(ngx_http_request_t *r,
24 ngx_http_upstream_t *u);
25static void ngx_http_upstream_process_header(ngx_http_request_t *r,
26 ngx_http_upstream_t *u);
Igor Sysoev797c6ef2008-09-30 15:39:02 +000027static ngx_int_t ngx_http_upstream_test_next(ngx_http_request_t *r,
28 ngx_http_upstream_t *u);
Igor Sysoevd01eea12008-09-30 14:57:09 +000029static ngx_int_t ngx_http_upstream_intercept_errors(ngx_http_request_t *r,
30 ngx_http_upstream_t *u);
Igor Sysoev24a95942007-03-31 15:37:09 +000031static ngx_int_t ngx_http_upstream_test_connect(ngx_connection_t *c);
Igor Sysoevb9409a82008-12-09 17:25:03 +000032static void ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r,
33 ngx_http_upstream_t *u);
Igor Sysoev02025fd2005-01-18 13:03:58 +000034static void ngx_http_upstream_send_response(ngx_http_request_t *r,
Igor Sysoevaa828612005-02-09 14:31:07 +000035 ngx_http_upstream_t *u);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +000036static void
37 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r);
Igor Sysoevb9409a82008-12-09 17:25:03 +000038static void
39 ngx_http_upstream_process_non_buffered_upstream(ngx_http_request_t *r,
40 ngx_http_upstream_t *u);
Igor Sysoev3e15a972008-12-08 18:28:06 +000041static void
42 ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
43 ngx_uint_t do_write);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +000044static ngx_int_t ngx_http_upstream_non_buffered_filter_init(void *data);
45static ngx_int_t ngx_http_upstream_non_buffered_filter(void *data,
46 ssize_t bytes);
Igor Sysoev899b44e2005-05-12 14:58:06 +000047static void ngx_http_upstream_process_downstream(ngx_http_request_t *r);
Igor Sysoevb9409a82008-12-09 17:25:03 +000048static void ngx_http_upstream_process_upstream(ngx_http_request_t *r,
49 ngx_http_upstream_t *u);
Igor Sysoev3e15a972008-12-08 18:28:06 +000050static void ngx_http_upstream_process_request(ngx_http_request_t *r);
Igor Sysoev58feb532007-07-12 11:19:05 +000051static void ngx_http_upstream_store(ngx_http_request_t *r,
52 ngx_http_upstream_t *u);
Igor Sysoevb9409a82008-12-09 17:25:03 +000053static void ngx_http_upstream_dummy_handler(ngx_http_request_t *r,
54 ngx_http_upstream_t *u);
Igor Sysoev02025fd2005-01-18 13:03:58 +000055static void ngx_http_upstream_next(ngx_http_request_t *r,
Igor Sysoevaa828612005-02-09 14:31:07 +000056 ngx_http_upstream_t *u, ngx_uint_t ft_type);
Igor Sysoev9ac946b2005-10-24 15:09:41 +000057static void ngx_http_upstream_cleanup(void *data);
Igor Sysoev02025fd2005-01-18 13:03:58 +000058static void ngx_http_upstream_finalize_request(ngx_http_request_t *r,
Igor Sysoevaa828612005-02-09 14:31:07 +000059 ngx_http_upstream_t *u, ngx_int_t rc);
Igor Sysoev02025fd2005-01-18 13:03:58 +000060
Igor Sysoev899b44e2005-05-12 14:58:06 +000061static ngx_int_t ngx_http_upstream_process_header_line(ngx_http_request_t *r,
62 ngx_table_elt_t *h, ngx_uint_t offset);
63static ngx_int_t
64 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r,
65 ngx_table_elt_t *h, ngx_uint_t offset);
66static ngx_int_t ngx_http_upstream_ignore_header_line(ngx_http_request_t *r,
67 ngx_table_elt_t *h, ngx_uint_t offset);
Igor Sysoev5192b362005-07-08 14:34:20 +000068static ngx_int_t ngx_http_upstream_process_limit_rate(ngx_http_request_t *r,
69 ngx_table_elt_t *h, ngx_uint_t offset);
Igor Sysoev3338cfd2006-05-11 14:43:47 +000070static ngx_int_t ngx_http_upstream_process_buffering(ngx_http_request_t *r,
Igor Sysoev899b44e2005-05-12 14:58:06 +000071 ngx_table_elt_t *h, ngx_uint_t offset);
Igor Sysoevef809b82006-06-28 16:00:26 +000072static ngx_int_t ngx_http_upstream_process_charset(ngx_http_request_t *r,
73 ngx_table_elt_t *h, ngx_uint_t offset);
Igor Sysoev3338cfd2006-05-11 14:43:47 +000074static ngx_int_t ngx_http_upstream_copy_header_line(ngx_http_request_t *r,
Igor Sysoev899b44e2005-05-12 14:58:06 +000075 ngx_table_elt_t *h, ngx_uint_t offset);
76static ngx_int_t
77 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r,
78 ngx_table_elt_t *h, ngx_uint_t offset);
79static ngx_int_t ngx_http_upstream_copy_content_type(ngx_http_request_t *r,
80 ngx_table_elt_t *h, ngx_uint_t offset);
81static ngx_int_t ngx_http_upstream_copy_content_length(ngx_http_request_t *r,
82 ngx_table_elt_t *h, ngx_uint_t offset);
83static ngx_int_t ngx_http_upstream_rewrite_location(ngx_http_request_t *r,
84 ngx_table_elt_t *h, ngx_uint_t offset);
85static ngx_int_t ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r,
86 ngx_table_elt_t *h, ngx_uint_t offset);
87#if (NGX_HTTP_GZIP)
88static ngx_int_t ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r,
89 ngx_table_elt_t *h, ngx_uint_t offset);
90#endif
91
Igor Sysoev09c684b2005-11-09 17:25:55 +000092static ngx_int_t ngx_http_upstream_add_variables(ngx_conf_t *cf);
Igor Sysoev29f59122007-04-23 16:56:17 +000093static ngx_int_t ngx_http_upstream_addr_variable(ngx_http_request_t *r,
94 ngx_http_variable_value_t *v, uintptr_t data);
Igor Sysoev09c684b2005-11-09 17:25:55 +000095static ngx_int_t ngx_http_upstream_status_variable(ngx_http_request_t *r,
96 ngx_http_variable_value_t *v, uintptr_t data);
97static ngx_int_t ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
98 ngx_http_variable_value_t *v, uintptr_t data);
Igor Sysoev371766c2008-12-11 15:30:52 +000099static ngx_int_t ngx_http_upstream_response_length_variable(
100 ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
Igor Sysoev09c684b2005-11-09 17:25:55 +0000101
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000102static char *ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
103static char *ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
104 void *conf);
105
Igor Sysoev899b44e2005-05-12 14:58:06 +0000106static void *ngx_http_upstream_create_main_conf(ngx_conf_t *cf);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000107static char *ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000108
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000109#if (NGX_HTTP_SSL)
Igor Sysoev43f279d2005-12-18 16:02:44 +0000110static void ngx_http_upstream_ssl_init_connection(ngx_http_request_t *,
111 ngx_http_upstream_t *u, ngx_connection_t *c);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000112static void ngx_http_upstream_ssl_handshake(ngx_connection_t *c);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000113#endif
114
Igor Sysoev899b44e2005-05-12 14:58:06 +0000115
116ngx_http_upstream_header_t ngx_http_upstream_headers_in[] = {
117
118 { ngx_string("Status"),
119 ngx_http_upstream_process_header_line,
120 offsetof(ngx_http_upstream_headers_in_t, status),
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000121 ngx_http_upstream_copy_header_line, 0, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000122
123 { ngx_string("Content-Type"),
124 ngx_http_upstream_process_header_line,
125 offsetof(ngx_http_upstream_headers_in_t, content_type),
Igor Sysoev08e63d42006-08-14 15:09:38 +0000126 ngx_http_upstream_copy_content_type, 0, 1 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000127
128 { ngx_string("Content-Length"),
129 ngx_http_upstream_process_header_line,
130 offsetof(ngx_http_upstream_headers_in_t, content_length),
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000131 ngx_http_upstream_copy_content_length, 0, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000132
133 { ngx_string("Date"),
134 ngx_http_upstream_process_header_line,
135 offsetof(ngx_http_upstream_headers_in_t, date),
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000136 ngx_http_upstream_copy_header_line,
137 offsetof(ngx_http_headers_out_t, date), 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000138
Igor Sysoev58feb532007-07-12 11:19:05 +0000139 { ngx_string("Last-Modified"),
140 ngx_http_upstream_process_header_line,
141 offsetof(ngx_http_upstream_headers_in_t, last_modified),
142 ngx_http_upstream_copy_header_line,
143 offsetof(ngx_http_headers_out_t, last_modified), 0 },
144
Igor Sysoev899b44e2005-05-12 14:58:06 +0000145 { ngx_string("Server"),
146 ngx_http_upstream_process_header_line,
147 offsetof(ngx_http_upstream_headers_in_t, server),
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000148 ngx_http_upstream_copy_header_line,
149 offsetof(ngx_http_headers_out_t, server), 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000150
Igor Sysoev187b7d92005-07-14 12:51:53 +0000151 { ngx_string("WWW-Authenticate"),
152 ngx_http_upstream_process_header_line,
153 offsetof(ngx_http_upstream_headers_in_t, www_authenticate),
154 ngx_http_upstream_copy_header_line, 0, 0 },
155
Igor Sysoev899b44e2005-05-12 14:58:06 +0000156 { ngx_string("Location"),
Igor Sysoevac721d02008-08-04 14:53:16 +0000157 ngx_http_upstream_process_header_line,
158 offsetof(ngx_http_upstream_headers_in_t, location),
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000159 ngx_http_upstream_rewrite_location, 0, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000160
161 { ngx_string("Refresh"),
162 ngx_http_upstream_ignore_header_line, 0,
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000163 ngx_http_upstream_rewrite_refresh, 0, 0 },
164
165 { ngx_string("Set-Cookie"),
166 ngx_http_upstream_ignore_header_line, 0,
Igor Sysoev90c08142005-07-25 09:41:38 +0000167 ngx_http_upstream_copy_header_line, 0, 1 },
168
169 { ngx_string("Content-Disposition"),
170 ngx_http_upstream_ignore_header_line, 0,
171 ngx_http_upstream_copy_header_line, 0, 1 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000172
173 { ngx_string("Cache-Control"),
174 ngx_http_upstream_process_multi_header_lines,
175 offsetof(ngx_http_upstream_headers_in_t, cache_control),
176 ngx_http_upstream_copy_multi_header_lines,
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000177 offsetof(ngx_http_headers_out_t, cache_control), 1 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000178
Igor Sysoev09c684b2005-11-09 17:25:55 +0000179 { ngx_string("Expires"),
180 ngx_http_upstream_process_header_line,
181 offsetof(ngx_http_upstream_headers_in_t, expires),
182 ngx_http_upstream_copy_header_line,
183 offsetof(ngx_http_headers_out_t, expires), 1 },
184
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000185 { ngx_string("Accept-Ranges"),
186 ngx_http_upstream_process_header_line,
187 offsetof(ngx_http_upstream_headers_in_t, accept_ranges),
188 ngx_http_upstream_copy_header_line,
189 offsetof(ngx_http_headers_out_t, accept_ranges), 1 },
190
Igor Sysoev899b44e2005-05-12 14:58:06 +0000191 { ngx_string("Connection"),
192 ngx_http_upstream_ignore_header_line, 0,
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000193 ngx_http_upstream_ignore_header_line, 0, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000194
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000195 { ngx_string("Keep-Alive"),
196 ngx_http_upstream_ignore_header_line, 0,
197 ngx_http_upstream_ignore_header_line, 0, 0 },
198
Igor Sysoev899b44e2005-05-12 14:58:06 +0000199 { ngx_string("X-Powered-By"),
200 ngx_http_upstream_ignore_header_line, 0,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000201 ngx_http_upstream_copy_header_line, 0, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000202
203 { ngx_string("X-Accel-Expires"),
204 ngx_http_upstream_process_header_line,
205 offsetof(ngx_http_upstream_headers_in_t, x_accel_expires),
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000206 ngx_http_upstream_copy_header_line, 0, 0 },
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000207
208 { ngx_string("X-Accel-Redirect"),
209 ngx_http_upstream_process_header_line,
210 offsetof(ngx_http_upstream_headers_in_t, x_accel_redirect),
211 ngx_http_upstream_ignore_header_line, 0, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000212
Igor Sysoev5192b362005-07-08 14:34:20 +0000213 { ngx_string("X-Accel-Limit-Rate"),
214 ngx_http_upstream_process_limit_rate, 0,
215 ngx_http_upstream_ignore_header_line, 0, 0 },
216
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000217 { ngx_string("X-Accel-Buffering"),
218 ngx_http_upstream_process_buffering, 0,
219 ngx_http_upstream_ignore_header_line, 0, 0 },
220
Igor Sysoevef809b82006-06-28 16:00:26 +0000221 { ngx_string("X-Accel-Charset"),
222 ngx_http_upstream_process_charset, 0,
223 ngx_http_upstream_ignore_header_line, 0, 0 },
224
Igor Sysoev899b44e2005-05-12 14:58:06 +0000225#if (NGX_HTTP_GZIP)
226 { ngx_string("Content-Encoding"),
227 ngx_http_upstream_process_header_line,
228 offsetof(ngx_http_upstream_headers_in_t, content_encoding),
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000229 ngx_http_upstream_copy_content_encoding, 0, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000230#endif
231
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000232 { ngx_null_string, NULL, 0, NULL, 0, 0 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000233};
Igor Sysoev02025fd2005-01-18 13:03:58 +0000234
235
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000236static ngx_command_t ngx_http_upstream_commands[] = {
237
238 { ngx_string("upstream"),
239 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
240 ngx_http_upstream,
241 0,
242 0,
243 NULL },
244
245 { ngx_string("server"),
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000246 NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000247 ngx_http_upstream_server,
248 NGX_HTTP_SRV_CONF_OFFSET,
249 0,
250 NULL },
251
252 ngx_null_command
253};
254
255
256static ngx_http_module_t ngx_http_upstream_module_ctx = {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000257 ngx_http_upstream_add_variables, /* preconfiguration */
Igor Sysoev899b44e2005-05-12 14:58:06 +0000258 NULL, /* postconfiguration */
259
260 ngx_http_upstream_create_main_conf, /* create main configuration */
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000261 ngx_http_upstream_init_main_conf, /* init main configuration */
Igor Sysoev02025fd2005-01-18 13:03:58 +0000262
263 NULL, /* create server configuration */
264 NULL, /* merge server configuration */
265
266 NULL, /* create location configuration */
267 NULL /* merge location configuration */
268};
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000269
Igor Sysoev02025fd2005-01-18 13:03:58 +0000270
271ngx_module_t ngx_http_upstream_module = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000272 NGX_MODULE_V1,
Igor Sysoev02025fd2005-01-18 13:03:58 +0000273 &ngx_http_upstream_module_ctx, /* module context */
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000274 ngx_http_upstream_commands, /* module directives */
Igor Sysoev02025fd2005-01-18 13:03:58 +0000275 NGX_HTTP_MODULE, /* module type */
Igor Sysoeve5733802005-09-08 14:36:09 +0000276 NULL, /* init master */
Igor Sysoev02025fd2005-01-18 13:03:58 +0000277 NULL, /* init module */
Igor Sysoeve5733802005-09-08 14:36:09 +0000278 NULL, /* init process */
279 NULL, /* init thread */
280 NULL, /* exit thread */
281 NULL, /* exit process */
282 NULL, /* exit master */
283 NGX_MODULE_V1_PADDING
Igor Sysoev02025fd2005-01-18 13:03:58 +0000284};
285
286
Igor Sysoev09c684b2005-11-09 17:25:55 +0000287static ngx_http_variable_t ngx_http_upstream_vars[] = {
288
Igor Sysoev29f59122007-04-23 16:56:17 +0000289 { ngx_string("upstream_addr"), NULL,
Igor Sysoev851cd732008-12-08 14:23:20 +0000290 ngx_http_upstream_addr_variable, 0,
291 NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
Igor Sysoev29f59122007-04-23 16:56:17 +0000292
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000293 { ngx_string("upstream_status"), NULL,
Igor Sysoev851cd732008-12-08 14:23:20 +0000294 ngx_http_upstream_status_variable, 0,
295 NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
Igor Sysoev09c684b2005-11-09 17:25:55 +0000296
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000297 { ngx_string("upstream_response_time"), NULL,
Igor Sysoev851cd732008-12-08 14:23:20 +0000298 ngx_http_upstream_response_time_variable, 0,
299 NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
Igor Sysoev09c684b2005-11-09 17:25:55 +0000300
Igor Sysoev371766c2008-12-11 15:30:52 +0000301 { ngx_string("upstream_response_length"), NULL,
302 ngx_http_upstream_response_length_variable, 0,
303 NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
304
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000305 { ngx_null_string, NULL, NULL, 0, 0, 0 }
Igor Sysoev09c684b2005-11-09 17:25:55 +0000306};
307
308
Igor Sysoev797c6ef2008-09-30 15:39:02 +0000309static ngx_http_upstream_next_t ngx_http_upstream_next_errors[] = {
310 { 500, NGX_HTTP_UPSTREAM_FT_HTTP_500 },
311 { 502, NGX_HTTP_UPSTREAM_FT_HTTP_502 },
312 { 503, NGX_HTTP_UPSTREAM_FT_HTTP_503 },
313 { 504, NGX_HTTP_UPSTREAM_FT_HTTP_504 },
314 { 404, NGX_HTTP_UPSTREAM_FT_HTTP_404 },
315 { 0, 0 }
316};
317
Igor Sysoevaa828612005-02-09 14:31:07 +0000318void
319ngx_http_upstream_init(ngx_http_request_t *r)
Igor Sysoev02025fd2005-01-18 13:03:58 +0000320{
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000321 ngx_str_t *host;
322 ngx_uint_t i;
323 ngx_connection_t *c;
324 ngx_resolver_ctx_t *ctx, temp;
325 ngx_http_cleanup_t *cln;
326 ngx_http_upstream_t *u;
327 ngx_http_core_loc_conf_t *clcf;
328 ngx_http_upstream_srv_conf_t *uscf, **uscfp;
329 ngx_http_upstream_main_conf_t *umcf;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000330
331 c = r->connection;
332
333 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
334 "http init upstream, client timer: %d", c->read->timer_set);
335
336 if (c->read->timer_set) {
337 ngx_del_timer(c->read);
338 }
339
Igor Sysoev6d16e1e2006-04-05 13:40:54 +0000340 u = r->upstream;
Igor Sysoev69d73da2006-02-20 16:48:17 +0000341
Igor Sysoev6d16e1e2006-04-05 13:40:54 +0000342 if (!r->post_action && !u->conf->ignore_client_abort) {
Igor Sysoev69d73da2006-02-20 16:48:17 +0000343 r->read_event_handler = ngx_http_upstream_rd_check_broken_connection;
344 r->write_event_handler = ngx_http_upstream_wr_check_broken_connection;
345 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000346
Igor Sysoev02025fd2005-01-18 13:03:58 +0000347 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
Igor Sysoev02025fd2005-01-18 13:03:58 +0000348
349 if (!c->write->active) {
Igor Sysoev90c08142005-07-25 09:41:38 +0000350 if (ngx_add_event(c->write, NGX_WRITE_EVENT, NGX_CLEAR_EVENT)
351 == NGX_ERROR)
Igor Sysoev02025fd2005-01-18 13:03:58 +0000352 {
353 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
354 return;
355 }
356 }
357 }
358
Igor Sysoeve5035392005-08-30 10:55:07 +0000359 if (r->request_body) {
360 u->request_bufs = r->request_body->bufs;
361 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000362
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000363 if (u->create_request(r) != NGX_OK) {
364 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
365 return;
366 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000367
368 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
Igor Sysoev02025fd2005-01-18 13:03:58 +0000369
Igor Sysoev02025fd2005-01-18 13:03:58 +0000370 u->output.pool = r->pool;
371 u->output.bufs.num = 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000372 u->output.bufs.size = clcf->client_body_buffer_size;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000373 u->output.output_filter = ngx_chain_writer;
374 u->output.filter_ctx = &u->writer;
375
376 u->writer.pool = r->pool;
377
Igor Sysoeve0f86622007-04-18 20:23:19 +0000378 if (r->upstream_states == NULL) {
379
380 r->upstream_states = ngx_array_create(r->pool, 1,
381 sizeof(ngx_http_upstream_state_t));
382 if (r->upstream_states == NULL) {
383 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
384 return;
385 }
386
387 } else {
388
389 u->state = ngx_array_push(r->upstream_states);
390 if (u->state == NULL) {
391 ngx_http_upstream_finalize_request(r, u,
392 NGX_HTTP_INTERNAL_SERVER_ERROR);
393 return;
394 }
395
396 ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
Igor Sysoev02025fd2005-01-18 13:03:58 +0000397 }
398
Igor Sysoev305a9d82005-12-26 17:07:48 +0000399 cln = ngx_http_cleanup_add(r, 0);
Igor Sysoev9ac946b2005-10-24 15:09:41 +0000400 if (cln == NULL) {
401 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
402 return;
403 }
404
405 cln->handler = ngx_http_upstream_cleanup;
406 cln->data = r;
407 u->cleanup = &cln->handler;
408
Igor Sysoevfbd9b432007-07-13 08:30:34 +0000409 u->store = (u->conf->store || u->conf->store_lengths);
Igor Sysoev58feb532007-07-12 11:19:05 +0000410
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000411 if (u->resolved == NULL) {
412
413 uscf = u->conf->upstream;
414
415 } else {
416
Igor Sysoev302cedc2008-12-23 19:35:12 +0000417 if (u->resolved->sockaddr) {
418
419 if (ngx_http_upstream_create_round_robin_peer(r, u->resolved)
420 != NGX_OK)
421 {
422 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
423 return;
424 }
425
426 ngx_http_upstream_connect(r, u);
427
428 return;
429 }
430
Igor Sysoevf2932dc2007-12-10 12:29:43 +0000431 host = &u->resolved->host;
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000432
433 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
434
435 uscfp = umcf->upstreams.elts;
436
437 for (i = 0; i < umcf->upstreams.nelts; i++) {
438
439 uscf = uscfp[i];
440
441 if (uscf->host.len == host->len
Igor Sysoev694c8492008-10-30 15:59:10 +0000442 && ((uscf->port == 0 && u->resolved->no_port)
Igor Sysoev8efe9262008-03-03 20:04:06 +0000443 || uscf->port == u->resolved->port)
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000444 && ngx_memcmp(uscf->host.data, host->data, host->len) == 0)
445 {
446 goto found;
447 }
448 }
449
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000450 temp.name = *host;
451
452 ctx = ngx_resolve_start(clcf->resolver, &temp);
453 if (ctx == NULL) {
454 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
455 return;
456 }
457
Igor Sysoev6b2fce42007-12-03 10:05:19 +0000458 if (ctx == NGX_NO_RESOLVER) {
459 ngx_log_error(NGX_LOG_ERR, c->log, 0,
460 "no resolver defined to resolve %V", host);
461
462 ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY);
463 return;
464 }
465
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000466 ctx->name = *host;
467 ctx->type = NGX_RESOLVE_A;
468 ctx->handler = ngx_http_upstream_resolve_handler;
469 ctx->data = r;
470 ctx->timeout = clcf->resolver_timeout;
471
Igor Sysoevf576f0d2008-04-09 14:20:17 +0000472 u->resolved->ctx = ctx;
473
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000474 if (ngx_resolve_name(ctx) != NGX_OK) {
Igor Sysoevf576f0d2008-04-09 14:20:17 +0000475 u->resolved->ctx = NULL;
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000476 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
477 return;
478 }
479
480 return;
481 }
482
483found:
484
485 if (uscf->peer.init(r, uscf) != NGX_OK) {
486 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
487 return;
488 }
489
Igor Sysoev02025fd2005-01-18 13:03:58 +0000490 ngx_http_upstream_connect(r, u);
491}
492
493
Igor Sysoevaa828612005-02-09 14:31:07 +0000494static void
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000495ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)
496{
497 ngx_http_request_t *r;
498 ngx_http_upstream_resolved_t *ur;
499
500 r = ctx->data;
501
502 r->upstream->resolved->ctx = NULL;
503
504 if (ctx->state) {
505 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
506 "%V could not be resolved (%i: %s)",
507 &ctx->name, ctx->state,
508 ngx_resolver_strerror(ctx->state));
509
510 ngx_resolve_name_done(ctx);
511 ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY);
512 return;
513 }
514
515 ur = r->upstream->resolved;
516 ur->naddrs = ctx->naddrs;
517 ur->addrs = ctx->addrs;
518
519#if (NGX_DEBUG)
520 {
521 in_addr_t addr;
522 ngx_uint_t i;
523
524 for (i = 0; i < ctx->naddrs; i++) {
525 addr = ntohl(ur->addrs[i]);
526
527 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
528 "name was resolved to %ud.%ud.%ud.%ud",
529 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
530 (addr >> 8) & 0xff, addr & 0xff);
531 }
532 }
533#endif
534
535 if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) {
536 ngx_resolve_name_done(ctx);
537 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
538 return;
539 }
540
541 ngx_resolve_name_done(ctx);
542
543 ngx_http_upstream_connect(r, r->upstream);
544}
545
546
547static void
Igor Sysoev851cd732008-12-08 14:23:20 +0000548ngx_http_upstream_handler(ngx_event_t *ev)
549{
550 ngx_connection_t *c;
551 ngx_http_request_t *r;
552 ngx_http_log_ctx_t *ctx;
553 ngx_http_upstream_t *u;
554
555 c = ev->data;
556 r = c->data;
557
558 u = r->upstream;
559 c = r->connection;
560
561 ctx = c->log->data;
562 ctx->current_request = r;
563
564 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
565 "http upstream request: \"%V?%V\"", &r->uri, &r->args);
566
567 if (ev->write) {
Igor Sysoevb9409a82008-12-09 17:25:03 +0000568 u->write_event_handler(r, u);
Igor Sysoev851cd732008-12-08 14:23:20 +0000569
570 } else {
Igor Sysoevb9409a82008-12-09 17:25:03 +0000571 u->read_event_handler(r, u);
Igor Sysoev851cd732008-12-08 14:23:20 +0000572 }
573
574 ngx_http_run_posted_requests(c);
575}
576
577
578static void
Igor Sysoev899b44e2005-05-12 14:58:06 +0000579ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r)
580{
581 ngx_http_upstream_check_broken_connection(r, r->connection->read);
582}
583
584
585static void
586ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r)
587{
588 ngx_http_upstream_check_broken_connection(r, r->connection->write);
589}
590
591
592static void
593ngx_http_upstream_check_broken_connection(ngx_http_request_t *r,
594 ngx_event_t *ev)
Igor Sysoev02025fd2005-01-18 13:03:58 +0000595{
596 int n;
597 char buf[1];
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000598 ngx_err_t err;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000599 ngx_connection_t *c;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000600 ngx_http_upstream_t *u;
601
Igor Sysoev5192b362005-07-08 14:34:20 +0000602 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ev->log, 0,
603 "http upstream check client, write event:%d, \"%V\"",
604 ev->write, &r->uri);
Igor Sysoev02025fd2005-01-18 13:03:58 +0000605
Igor Sysoev899b44e2005-05-12 14:58:06 +0000606 c = r->connection;
Igor Sysoeve5a222c2005-01-25 12:27:35 +0000607 u = r->upstream;
608
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000609 if (c->error) {
Igor Sysoev5192b362005-07-08 14:34:20 +0000610 ngx_http_upstream_finalize_request(r, u,
611 NGX_HTTP_CLIENT_CLOSED_REQUEST);
612 return;
613 }
614
Igor Sysoeve5a222c2005-01-25 12:27:35 +0000615 if (u->peer.connection == NULL) {
616 return;
617 }
618
Igor Sysoev02025fd2005-01-18 13:03:58 +0000619#if (NGX_HAVE_KQUEUE)
620
621 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
622
623 if (!ev->pending_eof) {
624 return;
625 }
626
627 ev->eof = 1;
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000628 c->error = 1;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000629
630 if (ev->kq_errno) {
631 ev->error = 1;
632 }
633
Igor Sysoev2d3f3f62007-10-14 18:56:15 +0000634 if (!u->cacheable && !u->store && u->peer.connection) {
Igor Sysoev02025fd2005-01-18 13:03:58 +0000635 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
Igor Sysoev5192b362005-07-08 14:34:20 +0000636 "kevent() reported that client closed prematurely "
637 "connection, so upstream connection is closed too");
Igor Sysoev02025fd2005-01-18 13:03:58 +0000638 ngx_http_upstream_finalize_request(r, u,
639 NGX_HTTP_CLIENT_CLOSED_REQUEST);
640 return;
641 }
642
643 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
644 "kevent() reported that client closed "
645 "prematurely connection");
646
647 if (u->peer.connection == NULL) {
648 ngx_http_upstream_finalize_request(r, u,
649 NGX_HTTP_CLIENT_CLOSED_REQUEST);
650 return;
651 }
652
653 return;
654 }
655
656#endif
657
Igor Sysoev02025fd2005-01-18 13:03:58 +0000658 n = recv(c->fd, buf, 1, MSG_PEEK);
659
660 err = ngx_socket_errno;
661
Igor Sysoev7b2d87f2007-02-15 10:00:59 +0000662 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, err,
663 "http upstream recv(): %d", n);
664
Igor Sysoev02025fd2005-01-18 13:03:58 +0000665 /*
666 * we do not need to disable the write event because
667 * that event has NGX_USE_CLEAR_EVENT type
668 */
669
670 if (ev->write && (n >= 0 || err == NGX_EAGAIN)) {
671 return;
672 }
673
Igor Sysoev02025fd2005-01-18 13:03:58 +0000674 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) {
675 if (ngx_del_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) {
676 ngx_http_upstream_finalize_request(r, u,
677 NGX_HTTP_INTERNAL_SERVER_ERROR);
678 return;
679 }
680 }
681
682 if (n > 0) {
683 return;
684 }
685
Igor Sysoev02025fd2005-01-18 13:03:58 +0000686 if (n == -1) {
687 if (err == NGX_EAGAIN) {
688 return;
689 }
690
691 ev->error = 1;
692
Igor Sysoeve5a222c2005-01-25 12:27:35 +0000693 } else { /* n == 0 */
Igor Sysoev02025fd2005-01-18 13:03:58 +0000694 err = 0;
695 }
696
Igor Sysoev187b7d92005-07-14 12:51:53 +0000697 ev->eof = 1;
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000698 c->error = 1;
Igor Sysoev187b7d92005-07-14 12:51:53 +0000699
Igor Sysoev2d3f3f62007-10-14 18:56:15 +0000700 if (!u->cacheable && !u->store && u->peer.connection) {
Igor Sysoev02025fd2005-01-18 13:03:58 +0000701 ngx_log_error(NGX_LOG_INFO, ev->log, err,
702 "client closed prematurely connection, "
703 "so upstream connection is closed too");
704 ngx_http_upstream_finalize_request(r, u,
705 NGX_HTTP_CLIENT_CLOSED_REQUEST);
706 return;
707 }
708
709 ngx_log_error(NGX_LOG_INFO, ev->log, err,
710 "client closed prematurely connection");
711
712 if (u->peer.connection == NULL) {
713 ngx_http_upstream_finalize_request(r, u,
714 NGX_HTTP_CLIENT_CLOSED_REQUEST);
715 return;
716 }
717}
718
719
Igor Sysoevaa828612005-02-09 14:31:07 +0000720static void
721ngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u)
Igor Sysoev02025fd2005-01-18 13:03:58 +0000722{
Igor Sysoev43f279d2005-12-18 16:02:44 +0000723 ngx_int_t rc;
Igor Sysoevbb28b6d2006-07-11 13:20:19 +0000724 ngx_time_t *tp;
Igor Sysoev43f279d2005-12-18 16:02:44 +0000725 ngx_connection_t *c;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000726
Igor Sysoeve5a222c2005-01-25 12:27:35 +0000727 r->connection->log->action = "connecting to upstream";
Igor Sysoev02025fd2005-01-18 13:03:58 +0000728
729 r->connection->single_connection = 0;
730
Igor Sysoev8f985812006-12-06 11:33:18 +0000731 if (u->state && u->state->response_sec) {
Igor Sysoevbb28b6d2006-07-11 13:20:19 +0000732 tp = ngx_timeofday();
Igor Sysoev8f985812006-12-06 11:33:18 +0000733 u->state->response_sec = tp->sec - u->state->response_sec;
734 u->state->response_msec = tp->msec - u->state->response_msec;
Igor Sysoevbb28b6d2006-07-11 13:20:19 +0000735 }
736
Igor Sysoeve0f86622007-04-18 20:23:19 +0000737 u->state = ngx_array_push(r->upstream_states);
Igor Sysoevbb28b6d2006-07-11 13:20:19 +0000738 if (u->state == NULL) {
739 ngx_http_upstream_finalize_request(r, u,
740 NGX_HTTP_INTERNAL_SERVER_ERROR);
741 return;
742 }
743
744 ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
745
746 tp = ngx_timeofday();
Igor Sysoev8f985812006-12-06 11:33:18 +0000747 u->state->response_sec = tp->sec;
748 u->state->response_msec = tp->msec;
Igor Sysoevbb28b6d2006-07-11 13:20:19 +0000749
Igor Sysoev02025fd2005-01-18 13:03:58 +0000750 rc = ngx_event_connect_peer(&u->peer);
751
752 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
753 "http upstream connect: %i", rc);
754
755 if (rc == NGX_ERROR) {
756 ngx_http_upstream_finalize_request(r, u,
757 NGX_HTTP_INTERNAL_SERVER_ERROR);
758 return;
759 }
760
Igor Sysoev6d4b5a12007-12-11 16:26:56 +0000761 u->state->peer = u->peer.name;
762
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000763 if (rc == NGX_BUSY) {
764 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams");
Igor Sysoev6876bcd2007-08-09 13:54:33 +0000765 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_NOLIVE);
766 return;
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000767 }
768
Igor Sysoev6876bcd2007-08-09 13:54:33 +0000769 if (rc == NGX_DECLINED) {
Igor Sysoev02025fd2005-01-18 13:03:58 +0000770 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
771 return;
772 }
773
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000774 /* rc == NGX_OK || rc == NGX_AGAIN */
775
Igor Sysoev02025fd2005-01-18 13:03:58 +0000776 c = u->peer.connection;
777
778 c->data = r;
Igor Sysoev43f279d2005-12-18 16:02:44 +0000779
Igor Sysoev851cd732008-12-08 14:23:20 +0000780 c->write->handler = ngx_http_upstream_handler;
781 c->read->handler = ngx_http_upstream_handler;
782
783 u->write_event_handler = ngx_http_upstream_send_request_handler;
784 u->read_event_handler = ngx_http_upstream_process_header;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000785
Igor Sysoeve1013382007-08-14 20:44:09 +0000786 c->sendfile &= r->connection->sendfile;
Igor Sysoeva4ec6862008-04-04 12:07:13 +0000787 u->output.sendfile = c->sendfile;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000788
789 c->pool = r->pool;
790 c->read->log = c->write->log = c->log = r->connection->log;
791
792 /* init or reinit the ngx_output_chain() and ngx_chain_writer() contexts */
793
794 u->writer.out = NULL;
795 u->writer.last = &u->writer.out;
796 u->writer.connection = c;
797 u->writer.limit = 0;
798
799 if (u->request_sent) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000800 if (ngx_http_upstream_reinit(r, u) != NGX_OK) {
801 ngx_http_upstream_finalize_request(r, u,
802 NGX_HTTP_INTERNAL_SERVER_ERROR);
803 return;
804 }
Igor Sysoev02025fd2005-01-18 13:03:58 +0000805 }
806
Igor Sysoev8ff1e322007-01-19 12:57:02 +0000807 if (r->request_body
808 && r->request_body->buf
809 && r->request_body->temp_file
810 && r == r->main)
811 {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000812 /*
813 * the r->request_body->buf can be reused for one request only,
814 * the subrequests should allocate their own temporay bufs
815 */
Igor Sysoev02025fd2005-01-18 13:03:58 +0000816
Igor Sysoev09c684b2005-11-09 17:25:55 +0000817 u->output.free = ngx_alloc_chain_link(r->pool);
818 if (u->output.free == NULL) {
819 ngx_http_upstream_finalize_request(r, u,
820 NGX_HTTP_INTERNAL_SERVER_ERROR);
821 return;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000822 }
Igor Sysoev09c684b2005-11-09 17:25:55 +0000823
824 u->output.free->buf = r->request_body->buf;
825 u->output.free->next = NULL;
826 u->output.allocated = 1;
827
828 r->request_body->buf->pos = r->request_body->buf->start;
829 r->request_body->buf->last = r->request_body->buf->start;
830 r->request_body->buf->tag = u->output.tag;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000831 }
832
833 u->request_sent = 0;
834
835 if (rc == NGX_AGAIN) {
836 ngx_add_timer(c->write, u->conf->connect_timeout);
837 return;
838 }
839
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000840#if (NGX_HTTP_SSL)
841
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000842 if (u->ssl && c->ssl == NULL) {
Igor Sysoev43f279d2005-12-18 16:02:44 +0000843 ngx_http_upstream_ssl_init_connection(r, u, c);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000844 return;
845 }
846
847#endif
848
Igor Sysoev02025fd2005-01-18 13:03:58 +0000849 ngx_http_upstream_send_request(r, u);
850}
851
852
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000853#if (NGX_HTTP_SSL)
854
855static void
Igor Sysoev43f279d2005-12-18 16:02:44 +0000856ngx_http_upstream_ssl_init_connection(ngx_http_request_t *r,
857 ngx_http_upstream_t *u, ngx_connection_t *c)
858{
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000859 ngx_int_t rc;
Igor Sysoev43f279d2005-12-18 16:02:44 +0000860
861 if (ngx_ssl_create_connection(u->conf->ssl, c,
862 NGX_SSL_BUFFER|NGX_SSL_CLIENT)
863 == NGX_ERROR)
864 {
865 ngx_http_upstream_finalize_request(r, u,
866 NGX_HTTP_INTERNAL_SERVER_ERROR);
867 return;
868 }
869
870 c->sendfile = 0;
Igor Sysoev13c68742006-03-10 12:51:52 +0000871 u->output.sendfile = 0;
Igor Sysoev43f279d2005-12-18 16:02:44 +0000872
Igor Sysoev916ee8e2008-08-17 17:47:52 +0000873 if (u->conf->ssl_session_reuse) {
874 if (u->peer.set_session(&u->peer, u->peer.data) != NGX_OK) {
875 ngx_http_upstream_finalize_request(r, u,
876 NGX_HTTP_INTERNAL_SERVER_ERROR);
877 return;
878 }
Igor Sysoev43f279d2005-12-18 16:02:44 +0000879 }
880
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000881 r->connection->log->action = "SSL handshaking to upstream";
882
Igor Sysoev43f279d2005-12-18 16:02:44 +0000883 rc = ngx_ssl_handshake(c);
884
885 if (rc == NGX_AGAIN) {
886 c->ssl->handler = ngx_http_upstream_ssl_handshake;
887 return;
888 }
889
890 ngx_http_upstream_ssl_handshake(c);
891}
892
893
894static void
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000895ngx_http_upstream_ssl_handshake(ngx_connection_t *c)
896{
897 ngx_http_request_t *r;
898 ngx_http_upstream_t *u;
899
900 r = c->data;
901 u = r->upstream;
902
903 if (c->ssl->handshaked) {
904
Igor Sysoev916ee8e2008-08-17 17:47:52 +0000905 if (u->conf->ssl_session_reuse) {
906 u->peer.save_session(&u->peer, u->peer.data);
907 }
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000908
Igor Sysoev851cd732008-12-08 14:23:20 +0000909 c->write->handler = ngx_http_upstream_handler;
910 c->read->handler = ngx_http_upstream_handler;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000911
912 ngx_http_upstream_send_request(r, u);
913
914 return;
915 }
916
917 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
918
919}
920
921#endif
922
923
Igor Sysoev899b44e2005-05-12 14:58:06 +0000924static ngx_int_t
Igor Sysoevaa828612005-02-09 14:31:07 +0000925ngx_http_upstream_reinit(ngx_http_request_t *r, ngx_http_upstream_t *u)
Igor Sysoev02025fd2005-01-18 13:03:58 +0000926{
927 ngx_chain_t *cl;
928
Igor Sysoev899b44e2005-05-12 14:58:06 +0000929 if (u->reinit_request(r) != NGX_OK) {
930 return NGX_ERROR;
931 }
932
Igor Sysoevf2932dc2007-12-10 12:29:43 +0000933 ngx_memzero(&u->headers_in, sizeof(ngx_http_upstream_headers_in_t));
Igor Sysoev899b44e2005-05-12 14:58:06 +0000934
Igor Sysoevf2932dc2007-12-10 12:29:43 +0000935 if (ngx_list_init(&u->headers_in.headers, r->pool, 8,
Igor Sysoev43f279d2005-12-18 16:02:44 +0000936 sizeof(ngx_table_elt_t))
937 != NGX_OK)
Igor Sysoev899b44e2005-05-12 14:58:06 +0000938 {
939 return NGX_ERROR;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000940 }
941
942 /* reinit the request chain */
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000943
Igor Sysoev899b44e2005-05-12 14:58:06 +0000944 for (cl = u->request_bufs; cl; cl = cl->next) {
Igor Sysoev02025fd2005-01-18 13:03:58 +0000945 cl->buf->pos = cl->buf->start;
946 cl->buf->file_pos = 0;
947 }
948
Igor Sysoev899b44e2005-05-12 14:58:06 +0000949 /* reinit the subrequest's ngx_output_chain() context */
950
Igor Sysoev09c684b2005-11-09 17:25:55 +0000951 if (r->request_body && r->request_body->temp_file
Igor Sysoevdf3254a2006-01-11 15:26:57 +0000952 && r != r->main && u->output.buf)
Igor Sysoev09c684b2005-11-09 17:25:55 +0000953 {
954 u->output.free = ngx_alloc_chain_link(r->pool);
955 if (u->output.free == NULL) {
956 return NGX_ERROR;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000957 }
Igor Sysoev09c684b2005-11-09 17:25:55 +0000958
959 u->output.free->buf = u->output.buf;
960 u->output.free->next = NULL;
961
962 u->output.buf->pos = u->output.buf->start;
963 u->output.buf->last = u->output.buf->start;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000964 }
Igor Sysoev02025fd2005-01-18 13:03:58 +0000965
966 u->output.buf = NULL;
967 u->output.in = NULL;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000968 u->output.busy = NULL;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000969
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000970 /* reinit u->buffer */
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000971
Igor Sysoev02025fd2005-01-18 13:03:58 +0000972#if 0
973 if (u->cache) {
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000974 u->buffer.pos = u->buffer.start + u->cache->ctx.header_size;
975 u->buffer.last = u->buffer.pos;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000976
977 } else {
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000978 u->buffer.pos = u->buffer.start;
979 u->buffer.last = u->buffer.start;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000980 }
981#else
Igor Sysoev899b44e2005-05-12 14:58:06 +0000982
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000983 u->buffer.pos = u->buffer.start;
984 u->buffer.last = u->buffer.start;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000985
Igor Sysoev02025fd2005-01-18 13:03:58 +0000986#endif
987
Igor Sysoev899b44e2005-05-12 14:58:06 +0000988 return NGX_OK;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000989}
990
991
Igor Sysoevaa828612005-02-09 14:31:07 +0000992static void
993ngx_http_upstream_send_request(ngx_http_request_t *r, ngx_http_upstream_t *u)
Igor Sysoev02025fd2005-01-18 13:03:58 +0000994{
Igor Sysoev24a95942007-03-31 15:37:09 +0000995 ngx_int_t rc;
Igor Sysoeve5a222c2005-01-25 12:27:35 +0000996 ngx_connection_t *c;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000997
Igor Sysoev02025fd2005-01-18 13:03:58 +0000998 c = u->peer.connection;
999
1000 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1001 "http upstream send request");
1002
Igor Sysoev24a95942007-03-31 15:37:09 +00001003 if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
Igor Sysoevfb958942007-04-18 15:21:28 +00001004 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
Igor Sysoev24a95942007-03-31 15:37:09 +00001005 return;
Igor Sysoevc55a1042006-08-09 19:59:45 +00001006 }
Igor Sysoev02025fd2005-01-18 13:03:58 +00001007
Igor Sysoeve5a222c2005-01-25 12:27:35 +00001008 c->log->action = "sending request to upstream";
Igor Sysoev02025fd2005-01-18 13:03:58 +00001009
Igor Sysoev899b44e2005-05-12 14:58:06 +00001010 rc = ngx_output_chain(&u->output, u->request_sent ? NULL : u->request_bufs);
Igor Sysoev02025fd2005-01-18 13:03:58 +00001011
1012 u->request_sent = 1;
1013
1014 if (rc == NGX_ERROR) {
1015 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
1016 return;
1017 }
1018
1019 if (c->write->timer_set) {
1020 ngx_del_timer(c->write);
1021 }
1022
1023 if (rc == NGX_AGAIN) {
1024 ngx_add_timer(c->write, u->conf->send_timeout);
1025
Igor Sysoevb9409a82008-12-09 17:25:03 +00001026 if (ngx_handle_write_event(c->write, u->conf->send_lowat) != NGX_OK) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001027 ngx_http_upstream_finalize_request(r, u,
1028 NGX_HTTP_INTERNAL_SERVER_ERROR);
1029 return;
1030 }
1031
1032 return;
1033 }
1034
1035 /* rc == NGX_OK */
1036
1037 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
1038 if (ngx_tcp_push(c->fd) == NGX_ERROR) {
1039 ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno,
1040 ngx_tcp_push_n " failed");
1041 ngx_http_upstream_finalize_request(r, u,
1042 NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001043 return;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001044 }
1045
1046 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001047 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001048
Igor Sysoev02025fd2005-01-18 13:03:58 +00001049 ngx_add_timer(c->read, u->conf->read_timeout);
1050
1051#if 1
1052 if (c->read->ready) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001053
Igor Sysoev02025fd2005-01-18 13:03:58 +00001054 /* post aio operation */
1055
1056 /*
1057 * TODO comment
1058 * although we can post aio operation just in the end
1059 * of ngx_http_upstream_connect() CHECK IT !!!
1060 * it's better to do here because we postpone header buffer allocation
1061 */
1062
Igor Sysoevb9409a82008-12-09 17:25:03 +00001063 ngx_http_upstream_process_header(r, u);
Igor Sysoev02025fd2005-01-18 13:03:58 +00001064 return;
1065 }
1066#endif
1067
Igor Sysoev851cd732008-12-08 14:23:20 +00001068 u->write_event_handler = ngx_http_upstream_dummy_handler;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001069
Igor Sysoevb9409a82008-12-09 17:25:03 +00001070 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001071 ngx_http_upstream_finalize_request(r, u,
1072 NGX_HTTP_INTERNAL_SERVER_ERROR);
1073 return;
1074 }
1075}
1076
1077
Igor Sysoevaa828612005-02-09 14:31:07 +00001078static void
Igor Sysoevb9409a82008-12-09 17:25:03 +00001079ngx_http_upstream_send_request_handler(ngx_http_request_t *r,
1080 ngx_http_upstream_t *u)
Igor Sysoev02025fd2005-01-18 13:03:58 +00001081{
Igor Sysoevb9409a82008-12-09 17:25:03 +00001082 ngx_connection_t *c;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001083
Igor Sysoevb9409a82008-12-09 17:25:03 +00001084 c = u->peer.connection;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001085
Igor Sysoevb9409a82008-12-09 17:25:03 +00001086 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev02025fd2005-01-18 13:03:58 +00001087 "http upstream send request handler");
1088
Igor Sysoevb9409a82008-12-09 17:25:03 +00001089 if (c->write->timedout) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001090 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
1091 return;
1092 }
1093
Igor Sysoev43f279d2005-12-18 16:02:44 +00001094#if (NGX_HTTP_SSL)
1095
Igor Sysoev96dd8af2007-11-27 13:34:13 +00001096 if (u->ssl && c->ssl == NULL) {
Igor Sysoev43f279d2005-12-18 16:02:44 +00001097 ngx_http_upstream_ssl_init_connection(r, u, c);
1098 return;
1099 }
1100
1101#endif
1102
Igor Sysoev6f134cc2006-05-23 14:54:58 +00001103 if (u->header_sent) {
Igor Sysoev851cd732008-12-08 14:23:20 +00001104 u->write_event_handler = ngx_http_upstream_dummy_handler;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00001105
Igor Sysoevb9409a82008-12-09 17:25:03 +00001106 (void) ngx_handle_write_event(c->write, 0);
Igor Sysoev6f134cc2006-05-23 14:54:58 +00001107
1108 return;
1109 }
1110
Igor Sysoev02025fd2005-01-18 13:03:58 +00001111 ngx_http_upstream_send_request(r, u);
1112}
1113
1114
Igor Sysoevaa828612005-02-09 14:31:07 +00001115static void
Igor Sysoevb9409a82008-12-09 17:25:03 +00001116ngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u)
Igor Sysoev02025fd2005-01-18 13:03:58 +00001117{
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001118 ssize_t n;
1119 ngx_int_t rc;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001120 ngx_str_t *uri, args;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001121 ngx_uint_t i, flags;
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001122 ngx_list_part_t *part;
1123 ngx_table_elt_t *h;
1124 ngx_connection_t *c;
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001125 ngx_http_upstream_header_t *hh;
1126 ngx_http_upstream_main_conf_t *umcf;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001127
Igor Sysoevb9409a82008-12-09 17:25:03 +00001128 c = u->peer.connection;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001129
Igor Sysoevb9409a82008-12-09 17:25:03 +00001130 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
Igor Sysoeve5a222c2005-01-25 12:27:35 +00001131 "http upstream process header");
Igor Sysoev02025fd2005-01-18 13:03:58 +00001132
Igor Sysoeve5a222c2005-01-25 12:27:35 +00001133 c->log->action = "reading response header from upstream";
Igor Sysoev02025fd2005-01-18 13:03:58 +00001134
Igor Sysoevb9409a82008-12-09 17:25:03 +00001135 if (c->read->timedout) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001136 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
1137 return;
1138 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001139
Igor Sysoev24a95942007-03-31 15:37:09 +00001140 if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
Igor Sysoevfb958942007-04-18 15:21:28 +00001141 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
Igor Sysoev24a95942007-03-31 15:37:09 +00001142 return;
1143 }
1144
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001145 if (u->buffer.start == NULL) {
1146 u->buffer.start = ngx_palloc(r->pool, u->conf->buffer_size);
1147 if (u->buffer.start == NULL) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001148 ngx_http_upstream_finalize_request(r, u,
1149 NGX_HTTP_INTERNAL_SERVER_ERROR);
1150 return;
1151 }
1152
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001153 u->buffer.pos = u->buffer.start;
1154 u->buffer.last = u->buffer.start;
1155 u->buffer.end = u->buffer.start + u->conf->buffer_size;
1156 u->buffer.temporary = 1;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001157
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001158 u->buffer.tag = u->output.tag;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001159
Igor Sysoevf2932dc2007-12-10 12:29:43 +00001160 if (ngx_list_init(&u->headers_in.headers, r->pool, 8,
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001161 sizeof(ngx_table_elt_t))
1162 != NGX_OK)
Igor Sysoev899b44e2005-05-12 14:58:06 +00001163 {
1164 ngx_http_upstream_finalize_request(r, u,
1165 NGX_HTTP_INTERNAL_SERVER_ERROR);
1166 return;
1167 }
1168
Igor Sysoev02025fd2005-01-18 13:03:58 +00001169#if 0
1170 if (u->cache) {
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001171 u->buffer.pos += u->cache->ctx.header_size;
1172 u->buffer.last = u->buffer.pos;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001173 }
1174#endif
1175 }
1176
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001177 for ( ;; ) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001178
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001179 n = c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last);
1180
1181 if (n == NGX_AGAIN) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001182#if 0
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001183 ngx_add_timer(rev, u->read_timeout);
Igor Sysoev02025fd2005-01-18 13:03:58 +00001184#endif
1185
Igor Sysoevb9409a82008-12-09 17:25:03 +00001186 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001187 ngx_http_upstream_finalize_request(r, u,
Igor Sysoev02025fd2005-01-18 13:03:58 +00001188 NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001189 return;
1190 }
1191
Igor Sysoev02025fd2005-01-18 13:03:58 +00001192 return;
1193 }
1194
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001195 if (n == 0) {
Igor Sysoevb9409a82008-12-09 17:25:03 +00001196 ngx_log_error(NGX_LOG_ERR, c->log, 0,
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001197 "upstream prematurely closed connection");
1198 }
Igor Sysoev02025fd2005-01-18 13:03:58 +00001199
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001200 if (n == NGX_ERROR || n == 0) {
1201 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
Igor Sysoev02025fd2005-01-18 13:03:58 +00001202 return;
1203 }
1204
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001205 u->buffer.last += n;
1206
1207#if 0
1208 u->valid_header_in = 0;
1209
1210 u->peer.cached = 0;
1211#endif
1212
1213 rc = u->process_header(r);
1214
1215 if (rc == NGX_AGAIN) {
1216
1217 if (u->buffer.pos == u->buffer.end) {
Igor Sysoevb9409a82008-12-09 17:25:03 +00001218 ngx_log_error(NGX_LOG_ERR, c->log, 0,
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001219 "upstream sent too big header");
1220
1221 ngx_http_upstream_next(r, u,
1222 NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);
1223 return;
1224 }
1225
1226 continue;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001227 }
1228
Igor Sysoev15fbaa62008-12-01 14:08:00 +00001229 break;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001230 }
1231
1232 if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) {
1233 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);
1234 return;
1235 }
1236
Igor Sysoev5a55d712007-12-09 08:22:35 +00001237 if (rc == NGX_ERROR) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001238 ngx_http_upstream_finalize_request(r, u,
1239 NGX_HTTP_INTERNAL_SERVER_ERROR);
1240 return;
1241 }
1242
1243 /* rc == NGX_OK */
1244
Igor Sysoevd01eea12008-09-30 14:57:09 +00001245 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST) {
Igor Sysoevaa828612005-02-09 14:31:07 +00001246
Igor Sysoev797c6ef2008-09-30 15:39:02 +00001247 if (r->subrequest_in_memory) {
1248 u->buffer.last = u->buffer.pos;
1249 }
1250
1251 if (ngx_http_upstream_test_next(r, u) == NGX_OK) {
1252 return;
1253 }
1254
Igor Sysoevd01eea12008-09-30 14:57:09 +00001255 if (ngx_http_upstream_intercept_errors(r, u) == NGX_OK) {
1256 return;
Igor Sysoevaa828612005-02-09 14:31:07 +00001257 }
1258 }
1259
Igor Sysoevda173ab2006-08-30 10:39:17 +00001260 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
1261
Igor Sysoevf2932dc2007-12-10 12:29:43 +00001262 if (u->headers_in.x_accel_redirect) {
Igor Sysoevcea316d2006-10-10 13:17:58 +00001263
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001264 ngx_http_upstream_finalize_request(r, u, NGX_DECLINED);
1265
Igor Sysoevf2932dc2007-12-10 12:29:43 +00001266 part = &u->headers_in.headers.part;
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001267 h = part->elts;
1268
1269 for (i = 0; /* void */; i++) {
1270
1271 if (i >= part->nelts) {
1272 if (part->next == NULL) {
1273 break;
1274 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001275
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001276 part = part->next;
1277 h = part->elts;
1278 i = 0;
1279 }
1280
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001281 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
1282 h[i].lowcase_key, h[i].key.len);
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001283
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001284 if (hh && hh->redirect) {
1285 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
Igor Sysoev37188912006-10-17 16:13:49 +00001286 ngx_http_finalize_request(r,
Igor Sysoevcea316d2006-10-10 13:17:58 +00001287 NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001288 return;
1289 }
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001290 }
1291 }
1292
Igor Sysoevf2932dc2007-12-10 12:29:43 +00001293 uri = &u->headers_in.x_accel_redirect->value;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001294 args.len = 0;
1295 args.data = NULL;
1296 flags = 0;
1297
1298 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
Igor Sysoevcea316d2006-10-10 13:17:58 +00001299 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
Igor Sysoev09c684b2005-11-09 17:25:55 +00001300 return;
1301 }
1302
1303 if (flags & NGX_HTTP_ZERO_IN_URI) {
1304 r->zero_in_uri = 1;
1305 }
1306
Igor Sysoevafd7ec52006-05-29 17:28:12 +00001307 if (r->method != NGX_HTTP_HEAD) {
1308 r->method = NGX_HTTP_GET;
1309 }
1310
Igor Sysoevf60dde92008-08-17 17:44:08 +00001311 r->valid_unparsed_uri = 0;
1312
Igor Sysoev09c684b2005-11-09 17:25:55 +00001313 ngx_http_internal_redirect(r, uri, &args);
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001314 return;
1315 }
1316
Igor Sysoevf2932dc2007-12-10 12:29:43 +00001317 part = &u->headers_in.headers.part;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001318 h = part->elts;
1319
1320 for (i = 0; /* void */; i++) {
1321
1322 if (i >= part->nelts) {
1323 if (part->next == NULL) {
1324 break;
1325 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001326
Igor Sysoev899b44e2005-05-12 14:58:06 +00001327 part = part->next;
1328 h = part->elts;
1329 i = 0;
1330 }
1331
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001332 if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash,
1333 h[i].lowcase_key, h[i].key.len))
Igor Sysoev899b44e2005-05-12 14:58:06 +00001334 {
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001335 continue;
1336 }
1337
1338 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
1339 h[i].lowcase_key, h[i].key.len);
1340
1341 if (hh) {
1342 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00001343 ngx_http_upstream_finalize_request(r, u,
1344 NGX_HTTP_INTERNAL_SERVER_ERROR);
1345 return;
1346 }
1347
1348 continue;
1349 }
1350
1351 if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) {
1352 ngx_http_upstream_finalize_request(r, u,
1353 NGX_HTTP_INTERNAL_SERVER_ERROR);
1354 return;
1355 }
1356 }
1357
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001358 if (r->headers_out.server && r->headers_out.server->value.data == NULL) {
1359 r->headers_out.server->hash = 0;
1360 }
1361
1362 if (r->headers_out.date && r->headers_out.date->value.data == NULL) {
1363 r->headers_out.date->hash = 0;
1364 }
1365
Igor Sysoev187b7d92005-07-14 12:51:53 +00001366 r->headers_out.status = u->headers_in.status_n;
1367 r->headers_out.status_line = u->headers_in.status_line;
1368
Igor Sysoevbe0a61e2008-02-11 13:14:56 +00001369 u->headers_in.content_length_n = r->headers_out.content_length_n;
1370
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001371 if (r->headers_out.content_length_n != -1) {
1372 u->length = (size_t) r->headers_out.content_length_n;
1373
1374 } else {
1375 u->length = NGX_MAX_SIZE_T_VALUE;
1376 }
1377
Igor Sysoev960100e2006-10-13 15:20:10 +00001378 if (!r->subrequest_in_memory) {
1379 ngx_http_upstream_send_response(r, u);
1380 return;
1381 }
1382
1383 /* subrequest content in memory */
1384
1385 if (u->input_filter == NULL) {
1386 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
1387 u->input_filter = ngx_http_upstream_non_buffered_filter;
1388 u->input_filter_ctx = r;
1389 }
1390
1391 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
1392 ngx_http_upstream_finalize_request(r, u,
1393 NGX_HTTP_INTERNAL_SERVER_ERROR);
1394 return;
1395 }
1396
Igor Sysoeva4859092008-11-12 21:01:01 +00001397 n = u->buffer.last - u->buffer.pos;
1398
1399 if (n) {
1400 u->buffer.last -= n;
1401
Igor Sysoev371766c2008-12-11 15:30:52 +00001402 u->state->response_length += n;
1403
Igor Sysoeva4859092008-11-12 21:01:01 +00001404 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
Igor Sysoev960100e2006-10-13 15:20:10 +00001405 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1406 return;
1407 }
1408
Igor Sysoeva4859092008-11-12 21:01:01 +00001409 if (u->length == 0) {
1410 ngx_http_upstream_finalize_request(r, u, 0);
1411 return;
1412 }
Igor Sysoev960100e2006-10-13 15:20:10 +00001413 }
1414
Igor Sysoev851cd732008-12-08 14:23:20 +00001415 u->read_event_handler = ngx_http_upstream_process_body_in_memory;
Igor Sysoev960100e2006-10-13 15:20:10 +00001416
Igor Sysoevb9409a82008-12-09 17:25:03 +00001417 ngx_http_upstream_process_body_in_memory(r, u);
Igor Sysoev960100e2006-10-13 15:20:10 +00001418}
1419
1420
Igor Sysoev24a95942007-03-31 15:37:09 +00001421static ngx_int_t
Igor Sysoev797c6ef2008-09-30 15:39:02 +00001422ngx_http_upstream_test_next(ngx_http_request_t *r, ngx_http_upstream_t *u)
1423{
1424 ngx_uint_t status;
1425 ngx_http_upstream_next_t *un;
1426
1427 if (!(u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_STATUS)) {
1428 return NGX_DECLINED;
1429 }
1430
1431 status = u->headers_in.status_n;
1432
1433 for (un = ngx_http_upstream_next_errors; un->status; un++) {
1434
1435 if (status != un->status) {
1436 continue;
1437 }
1438
1439 if (u->peer.tries > 1 && (u->conf->next_upstream & un->mask)) {
1440 ngx_http_upstream_next(r, u, un->mask);
1441 return NGX_OK;
1442 }
1443
1444 if (status == NGX_HTTP_NOT_FOUND && u->conf->intercept_404) {
1445 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_NOT_FOUND);
1446 return NGX_OK;
1447 }
1448
1449#if (NGX_HTTP_CACHE)
1450
1451 if (u->peer.tries == 0 && u->stale && (u->conf->use_stale & un->mask)) {
1452 ngx_http_upstream_finalize_request(r, u,
1453 ngx_http_send_cached_response(r));
1454 return NGX_OK;
1455 }
1456
1457#endif
1458 }
1459
1460 return NGX_DECLINED;
1461}
1462
1463
1464static ngx_int_t
Igor Sysoevd01eea12008-09-30 14:57:09 +00001465ngx_http_upstream_intercept_errors(ngx_http_request_t *r,
1466 ngx_http_upstream_t *u)
1467{
1468 ngx_int_t status;
1469 ngx_uint_t i;
1470 ngx_table_elt_t *h;
1471 ngx_http_err_page_t *err_page;
1472 ngx_http_core_loc_conf_t *clcf;
1473
1474 if (!u->conf->intercept_errors) {
1475 return NGX_DECLINED;
1476 }
1477
1478 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1479
1480 if (clcf->error_pages == NULL) {
1481 return NGX_DECLINED;
1482 }
1483
1484 status = u->headers_in.status_n;
1485
1486 err_page = clcf->error_pages->elts;
1487 for (i = 0; i < clcf->error_pages->nelts; i++) {
1488
1489 if (err_page[i].status == status) {
1490
1491 if (status == NGX_HTTP_UNAUTHORIZED) {
1492
1493 h = ngx_list_push(&r->headers_out.headers);
1494
1495 if (h == NULL) {
1496 ngx_http_upstream_finalize_request(r, u,
1497 NGX_HTTP_INTERNAL_SERVER_ERROR);
1498 return NGX_OK;
1499 }
1500
1501 *h = *u->headers_in.www_authenticate;
1502
1503 r->headers_out.www_authenticate = h;
1504 }
1505
1506 ngx_http_upstream_finalize_request(r, u, status);
1507
1508 return NGX_OK;
1509 }
1510 }
1511
1512 return NGX_DECLINED;
1513}
1514
1515
1516static ngx_int_t
Igor Sysoev24a95942007-03-31 15:37:09 +00001517ngx_http_upstream_test_connect(ngx_connection_t *c)
1518{
1519 int err;
1520 socklen_t len;
1521
1522#if (NGX_HAVE_KQUEUE)
1523
1524 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
Igor Sysoevfb958942007-04-18 15:21:28 +00001525 if (c->write->pending_eof) {
Igor Sysoev9e4b7272007-09-09 18:43:16 +00001526 c->log->action = "connecting to upstream";
Igor Sysoevfb958942007-04-18 15:21:28 +00001527 (void) ngx_connection_error(c, c->write->kq_errno,
1528 "kevent() reported that connect() failed");
1529 return NGX_ERROR;
1530 }
Igor Sysoev24a95942007-03-31 15:37:09 +00001531
1532 } else
1533#endif
1534 {
Igor Sysoevfb958942007-04-18 15:21:28 +00001535 err = 0;
1536 len = sizeof(int);
Igor Sysoev24a95942007-03-31 15:37:09 +00001537
Igor Sysoevfb958942007-04-18 15:21:28 +00001538 /*
1539 * BSDs and Linux return 0 and set a pending error in err
1540 * Solaris returns -1 and sets errno
1541 */
Igor Sysoev24a95942007-03-31 15:37:09 +00001542
Igor Sysoevfb958942007-04-18 15:21:28 +00001543 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len)
1544 == -1)
1545 {
1546 err = ngx_errno;
1547 }
Igor Sysoev24a95942007-03-31 15:37:09 +00001548
Igor Sysoevfb958942007-04-18 15:21:28 +00001549 if (err) {
Igor Sysoev9e4b7272007-09-09 18:43:16 +00001550 c->log->action = "connecting to upstream";
Igor Sysoevfb958942007-04-18 15:21:28 +00001551 (void) ngx_connection_error(c, err, "connect() failed");
1552 return NGX_ERROR;
1553 }
Igor Sysoev24a95942007-03-31 15:37:09 +00001554 }
1555
1556 return NGX_OK;
1557}
1558
1559
Igor Sysoev960100e2006-10-13 15:20:10 +00001560static void
Igor Sysoevb9409a82008-12-09 17:25:03 +00001561ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r,
1562 ngx_http_upstream_t *u)
Igor Sysoev960100e2006-10-13 15:20:10 +00001563{
Igor Sysoevb9409a82008-12-09 17:25:03 +00001564 size_t size;
1565 ssize_t n;
1566 ngx_buf_t *b;
1567 ngx_event_t *rev;
1568 ngx_connection_t *c;
Igor Sysoev960100e2006-10-13 15:20:10 +00001569
Igor Sysoevb9409a82008-12-09 17:25:03 +00001570 c = u->peer.connection;
1571 rev = c->read;
Igor Sysoev960100e2006-10-13 15:20:10 +00001572
1573 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1574 "http upstream process body on memory");
1575
1576 if (rev->timedout) {
1577 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
1578 ngx_http_upstream_finalize_request(r, u, NGX_ETIMEDOUT);
1579 return;
1580 }
1581
1582 b = &u->buffer;
1583
1584 for ( ;; ) {
1585
1586 size = b->end - b->last;
1587
1588 if (size == 0) {
1589 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1590 "upstream buffer is too small to read repsonse");
1591 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1592 return;
1593 }
1594
1595 n = c->recv(c, b->last, size);
1596
1597 if (n == NGX_AGAIN) {
1598 break;
1599 }
1600
1601 if (n == 0 || n == NGX_ERROR) {
1602 ngx_http_upstream_finalize_request(r, u, n);
1603 return;
1604 }
1605
Igor Sysoev371766c2008-12-11 15:30:52 +00001606 u->state->response_length += n;
1607
Igor Sysoev960100e2006-10-13 15:20:10 +00001608 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
1609 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1610 return;
1611 }
1612
1613 if (!rev->ready) {
1614 break;
1615 }
1616 }
1617
Igor Sysoevb9409a82008-12-09 17:25:03 +00001618 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
Igor Sysoev960100e2006-10-13 15:20:10 +00001619 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1620 return;
1621 }
1622
1623 if (rev->active) {
1624 ngx_add_timer(rev, u->conf->read_timeout);
1625
1626 } else if (rev->timer_set) {
1627 ngx_del_timer(rev);
1628 }
Igor Sysoevda173ab2006-08-30 10:39:17 +00001629}
1630
1631
1632static void
1633ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
1634{
1635 int tcp_nodelay;
Igor Sysoev371766c2008-12-11 15:30:52 +00001636 ssize_t n;
Igor Sysoevda173ab2006-08-30 10:39:17 +00001637 ngx_int_t rc;
1638 ngx_event_pipe_t *p;
1639 ngx_connection_t *c;
1640 ngx_pool_cleanup_t *cl;
1641 ngx_pool_cleanup_file_t *clf;
1642 ngx_http_core_loc_conf_t *clcf;
1643
Igor Sysoev899b44e2005-05-12 14:58:06 +00001644 rc = ngx_http_send_header(r);
Igor Sysoev02025fd2005-01-18 13:03:58 +00001645
Igor Sysoev8488a1c2007-01-15 17:12:43 +00001646 if (rc == NGX_ERROR || rc > NGX_OK || r->post_action || r->header_only) {
Igor Sysoev805d9db2005-02-03 19:33:37 +00001647 ngx_http_upstream_finalize_request(r, u, rc);
Igor Sysoev02025fd2005-01-18 13:03:58 +00001648 return;
1649 }
1650
1651 u->header_sent = 1;
1652
Igor Sysoev09c684b2005-11-09 17:25:55 +00001653 if (r->request_body && r->request_body->temp_file) {
Igor Sysoevc2068d02005-10-19 12:33:58 +00001654 for (cl = r->pool->cleanup; cl; cl = cl->next) {
1655 if (cl->handler == ngx_pool_cleanup_file) {
1656 clf = cl->data;
1657
1658 if (clf->fd == r->request_body->temp_file->file.fd) {
1659 cl->handler(clf);
1660 cl->handler = NULL;
Igor Sysoeve2407622006-06-30 12:19:32 +00001661 r->request_body->temp_file->file.fd = NGX_INVALID_FILE;
Igor Sysoevc2068d02005-10-19 12:33:58 +00001662 break;
1663 }
1664 }
1665 }
1666 }
1667
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001668 c = r->connection;
1669
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001670 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1671
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001672 if (!u->buffering) {
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001673
1674 if (u->input_filter == NULL) {
1675 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
1676 u->input_filter = ngx_http_upstream_non_buffered_filter;
1677 u->input_filter_ctx = r;
1678 }
1679
Igor Sysoev3e15a972008-12-08 18:28:06 +00001680 u->read_event_handler = ngx_http_upstream_process_non_buffered_upstream;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001681 r->write_event_handler =
1682 ngx_http_upstream_process_non_buffered_downstream;
1683
1684 r->limit_rate = 0;
1685
1686 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
1687 ngx_http_upstream_finalize_request(r, u, 0);
1688 return;
1689 }
1690
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001691 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) {
1692 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
1693
1694 tcp_nodelay = 1;
1695
1696 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
1697 (const void *) &tcp_nodelay, sizeof(int)) == -1)
1698 {
1699 ngx_connection_error(c, ngx_socket_errno,
1700 "setsockopt(TCP_NODELAY) failed");
1701 ngx_http_upstream_finalize_request(r, u, 0);
1702 return;
1703 }
1704
1705 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
1706 }
1707
Igor Sysoev371766c2008-12-11 15:30:52 +00001708 n = u->buffer.last - u->buffer.pos;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001709
Igor Sysoev371766c2008-12-11 15:30:52 +00001710 if (n) {
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001711 u->buffer.last = u->buffer.pos;
1712
Igor Sysoev371766c2008-12-11 15:30:52 +00001713 u->state->response_length += n;
1714
1715 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001716 ngx_http_upstream_finalize_request(r, u, 0);
1717 return;
1718 }
1719
Igor Sysoev3e15a972008-12-08 18:28:06 +00001720 ngx_http_upstream_process_non_buffered_downstream(r);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001721
1722 } else {
1723 u->buffer.pos = u->buffer.start;
1724 u->buffer.last = u->buffer.start;
1725
1726 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) {
1727 ngx_http_upstream_finalize_request(r, u, 0);
1728 return;
1729 }
Igor Sysoev3dea9192007-09-09 18:32:53 +00001730
1731 if (u->peer.connection->read->ready) {
Igor Sysoevb9409a82008-12-09 17:25:03 +00001732 ngx_http_upstream_process_non_buffered_upstream(r, u);
Igor Sysoev3dea9192007-09-09 18:32:53 +00001733 }
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001734 }
1735
1736 return;
1737 }
1738
Igor Sysoev02025fd2005-01-18 13:03:58 +00001739 /* TODO: preallocate event_pipe bufs, look "Content-Length" */
1740
1741#if 0
1742
1743 if (u->cache && u->cache->ctx.file.fd != NGX_INVALID_FILE) {
1744 if (ngx_close_file(u->cache->ctx.file.fd) == NGX_FILE_ERROR) {
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001745 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
Igor Sysoev02025fd2005-01-18 13:03:58 +00001746 ngx_close_file_n " \"%s\" failed",
1747 u->cache->ctx.file.name.data);
1748 }
1749 }
1750
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00001751 if (u->cacheable) {
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001752 header = (ngx_http_cache_header_t *) u->buffer->start;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001753
1754 header->expires = u->cache->ctx.expires;
1755 header->last_modified = u->cache->ctx.last_modified;
1756 header->date = u->cache->ctx.date;
1757 header->length = r->headers_out.content_length_n;
1758 u->cache->ctx.length = r->headers_out.content_length_n;
1759
1760 header->key_len = u->cache->ctx.key0.len;
1761 ngx_memcpy(&header->key, u->cache->ctx.key0.data, header->key_len);
1762 header->key[header->key_len] = LF;
1763 }
1764
1765#endif
1766
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001767 p = u->pipe;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001768
1769 p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter;
1770 p->output_ctx = r;
1771 p->tag = u->output.tag;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001772 p->bufs = u->conf->bufs;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001773 p->busy_size = u->conf->busy_buffers_size;
1774 p->upstream = u->peer.connection;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001775 p->downstream = c;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001776 p->pool = r->pool;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001777 p->log = c->log;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001778
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00001779 p->cacheable = u->cacheable || u->store;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001780
Igor Sysoevc1571722005-03-19 12:38:37 +00001781 p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
1782 if (p->temp_file == NULL) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001783 ngx_http_upstream_finalize_request(r, u, 0);
1784 return;
1785 }
1786
1787 p->temp_file->file.fd = NGX_INVALID_FILE;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001788 p->temp_file->file.log = c->log;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001789 p->temp_file->path = u->conf->temp_path;
1790 p->temp_file->pool = r->pool;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001791
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00001792 if (u->cacheable || u->store) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001793 p->temp_file->persistent = 1;
Igor Sysoev58feb532007-07-12 11:19:05 +00001794
Igor Sysoev02025fd2005-01-18 13:03:58 +00001795 } else {
Igor Sysoev8f125582006-07-28 15:16:17 +00001796 p->temp_file->log_level = NGX_LOG_WARN;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001797 p->temp_file->warn = "an upstream response is buffered "
1798 "to a temporary file";
1799 }
1800
1801 p->max_temp_file_size = u->conf->max_temp_file_size;
1802 p->temp_file_write_size = u->conf->temp_file_write_size;
1803
Igor Sysoevc1571722005-03-19 12:38:37 +00001804 p->preread_bufs = ngx_alloc_chain_link(r->pool);
1805 if (p->preread_bufs == NULL) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00001806 ngx_http_upstream_finalize_request(r, u, 0);
1807 return;
1808 }
Igor Sysoev58feb532007-07-12 11:19:05 +00001809
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001810 p->preread_bufs->buf = &u->buffer;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001811 p->preread_bufs->next = NULL;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001812 u->buffer.recycled = 1;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001813
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001814 p->preread_size = u->buffer.last - u->buffer.pos;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001815
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00001816 if (u->cacheable) {
Igor Sysoev58feb532007-07-12 11:19:05 +00001817
Igor Sysoev02025fd2005-01-18 13:03:58 +00001818 p->buf_to_file = ngx_calloc_buf(r->pool);
1819 if (p->buf_to_file == NULL) {
1820 ngx_http_upstream_finalize_request(r, u, 0);
1821 return;
1822 }
Igor Sysoev58feb532007-07-12 11:19:05 +00001823
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001824 p->buf_to_file->pos = u->buffer.start;
1825 p->buf_to_file->last = u->buffer.pos;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001826 p->buf_to_file->temporary = 1;
1827 }
1828
1829 if (ngx_event_flags & NGX_USE_AIO_EVENT) {
1830 /* the posted aio operation may currupt a shadow buffer */
1831 p->single_buf = 1;
1832 }
1833
1834 /* TODO: p->free_bufs = 0 if use ngx_create_chain_of_bufs() */
1835 p->free_bufs = 1;
1836
1837 /*
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001838 * event_pipe would do u->buffer.last += p->preread_size
Igor Sysoev02025fd2005-01-18 13:03:58 +00001839 * as though these bytes were read
1840 */
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001841 u->buffer.last = u->buffer.pos;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001842
1843 if (u->conf->cyclic_temp_file) {
1844
1845 /*
1846 * we need to disable the use of sendfile() if we use cyclic temp file
1847 * because the writing a new data may interfere with sendfile()
1848 * that uses the same kernel file pages (at least on FreeBSD)
1849 */
1850
1851 p->cyclic_temp_file = 1;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001852 c->sendfile = 0;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001853
1854 } else {
1855 p->cyclic_temp_file = 0;
1856 }
1857
Igor Sysoev02025fd2005-01-18 13:03:58 +00001858 p->read_timeout = u->conf->read_timeout;
1859 p->send_timeout = clcf->send_timeout;
1860 p->send_lowat = clcf->send_lowat;
1861
Igor Sysoev3e15a972008-12-08 18:28:06 +00001862 u->read_event_handler = ngx_http_upstream_process_upstream;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001863 r->write_event_handler = ngx_http_upstream_process_downstream;
Igor Sysoev02025fd2005-01-18 13:03:58 +00001864
Igor Sysoevb9409a82008-12-09 17:25:03 +00001865 ngx_http_upstream_process_upstream(r, u);
Igor Sysoev02025fd2005-01-18 13:03:58 +00001866}
1867
1868
Igor Sysoevaa828612005-02-09 14:31:07 +00001869static void
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001870ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r)
1871{
Igor Sysoev3e15a972008-12-08 18:28:06 +00001872 ngx_event_t *wev;
1873 ngx_connection_t *c;
1874 ngx_http_upstream_t *u;
1875
1876 c = r->connection;
1877 u = r->upstream;
1878 wev = c->write;
1879
1880 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1881 "http upstream process non buffered downstream");
1882
1883 c->log->action = "sending to client";
1884
1885 if (wev->timedout) {
1886 c->timedout = 1;
1887 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
1888 ngx_http_upstream_finalize_request(r, u, 0);
1889 return;
1890 }
1891
1892 ngx_http_upstream_process_non_buffered_request(r, 1);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001893}
1894
1895
1896static void
Igor Sysoevb9409a82008-12-09 17:25:03 +00001897ngx_http_upstream_process_non_buffered_upstream(ngx_http_request_t *r,
1898 ngx_http_upstream_t *u)
Igor Sysoev3e15a972008-12-08 18:28:06 +00001899{
Igor Sysoevb9409a82008-12-09 17:25:03 +00001900 ngx_connection_t *c;
Igor Sysoev3e15a972008-12-08 18:28:06 +00001901
Igor Sysoevb9409a82008-12-09 17:25:03 +00001902 c = u->peer.connection;
Igor Sysoev3e15a972008-12-08 18:28:06 +00001903
1904 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1905 "http upstream process non buffered upstream");
1906
1907 c->log->action = "reading upstream";
1908
Igor Sysoevb9409a82008-12-09 17:25:03 +00001909 if (c->read->timedout) {
Igor Sysoev3e15a972008-12-08 18:28:06 +00001910 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
1911 ngx_http_upstream_finalize_request(r, u, 0);
1912 return;
1913 }
1914
1915 ngx_http_upstream_process_non_buffered_request(r, 0);
1916}
1917
1918
1919static void
1920ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
1921 ngx_uint_t do_write)
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001922{
1923 size_t size;
1924 ssize_t n;
1925 ngx_buf_t *b;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001926 ngx_int_t rc;
Igor Sysoev3e15a972008-12-08 18:28:06 +00001927 ngx_connection_t *downstream, *upstream;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001928 ngx_http_upstream_t *u;
1929 ngx_http_core_loc_conf_t *clcf;
1930
Igor Sysoeve3fbaea2006-10-10 13:12:13 +00001931 u = r->upstream;
Igor Sysoev3d2fd182006-12-04 16:46:13 +00001932 downstream = r->connection;
1933 upstream = u->peer.connection;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001934
1935 b = &u->buffer;
1936
Igor Sysoev3e15a972008-12-08 18:28:06 +00001937 do_write = do_write || u->length == 0;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001938
1939 for ( ;; ) {
1940
1941 if (do_write) {
1942
1943 if (u->out_bufs || u->busy_bufs) {
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001944 rc = ngx_http_output_filter(r, u->out_bufs);
1945
Igor Sysoev3d2fd182006-12-04 16:46:13 +00001946 if (downstream->destroyed) {
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001947 return;
1948 }
1949
1950 if (rc == NGX_ERROR) {
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001951 ngx_http_upstream_finalize_request(r, u, 0);
1952 return;
1953 }
1954
1955 ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs,
1956 &u->out_bufs, u->output.tag);
1957 }
1958
1959 if (u->busy_bufs == NULL) {
1960
1961 if (u->length == 0
Igor Sysoev3d2fd182006-12-04 16:46:13 +00001962 || upstream->read->eof
1963 || upstream->read->error)
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001964 {
1965 ngx_http_upstream_finalize_request(r, u, 0);
1966 return;
1967 }
1968
1969 b->pos = b->start;
1970 b->last = b->start;
1971 }
1972 }
1973
1974 size = b->end - b->last;
1975
1976 if (size > u->length) {
1977 size = u->length;
1978 }
1979
Igor Sysoev3d2fd182006-12-04 16:46:13 +00001980 if (size && upstream->read->ready) {
Igor Sysoevef809b82006-06-28 16:00:26 +00001981
Igor Sysoev3d2fd182006-12-04 16:46:13 +00001982 n = upstream->recv(upstream, b->last, size);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001983
1984 if (n == NGX_AGAIN) {
1985 break;
1986 }
1987
1988 if (n > 0) {
Igor Sysoev371766c2008-12-11 15:30:52 +00001989 u->state->response_length += n;
1990
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001991 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
1992 ngx_http_upstream_finalize_request(r, u, 0);
1993 return;
1994 }
1995 }
1996
1997 do_write = 1;
1998
1999 continue;
2000 }
2001
2002 break;
2003 }
2004
Igor Sysoev3e15a972008-12-08 18:28:06 +00002005 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2006
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002007 if (downstream->data == r) {
2008 if (ngx_handle_write_event(downstream->write, clcf->send_lowat)
Igor Sysoev851cd732008-12-08 14:23:20 +00002009 != NGX_OK)
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002010 {
2011 ngx_http_upstream_finalize_request(r, u, 0);
2012 return;
2013 }
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002014 }
2015
Igor Sysoevae2006b2007-01-19 12:22:32 +00002016 if (downstream->write->active && !downstream->write->ready) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002017 ngx_add_timer(downstream->write, clcf->send_timeout);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002018
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002019 } else if (downstream->write->timer_set) {
2020 ngx_del_timer(downstream->write);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002021 }
2022
Igor Sysoev851cd732008-12-08 14:23:20 +00002023 if (ngx_handle_read_event(upstream->read, 0) != NGX_OK) {
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002024 ngx_http_upstream_finalize_request(r, u, 0);
2025 return;
2026 }
2027
Igor Sysoevae2006b2007-01-19 12:22:32 +00002028 if (upstream->read->active && !upstream->read->ready) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002029 ngx_add_timer(upstream->read, u->conf->read_timeout);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002030
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002031 } else if (upstream->read->timer_set) {
2032 ngx_del_timer(upstream->read);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002033 }
2034}
2035
2036
2037static ngx_int_t
2038ngx_http_upstream_non_buffered_filter_init(void *data)
2039{
2040 return NGX_OK;
2041}
2042
2043
2044static ngx_int_t
2045ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes)
2046{
2047 ngx_http_request_t *r = data;
2048
2049 ngx_buf_t *b;
2050 ngx_chain_t *cl, **ll;
2051 ngx_http_upstream_t *u;
2052
2053 u = r->upstream;
2054
2055 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
2056 ll = &cl->next;
2057 }
2058
2059 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
2060 if (cl == NULL) {
2061 return NGX_ERROR;
2062 }
2063
2064 *ll = cl;
2065
2066 cl->buf->flush = 1;
2067 cl->buf->memory = 1;
2068
2069 b = &u->buffer;
2070
2071 cl->buf->pos = b->last;
2072 b->last += bytes;
2073 cl->buf->last = b->last;
Igor Sysoev7c15b752008-03-03 10:42:00 +00002074 cl->buf->tag = u->output.tag;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002075
2076 if (u->length == NGX_MAX_SIZE_T_VALUE) {
2077 return NGX_OK;
2078 }
2079
2080 u->length -= bytes;
2081
2082 return NGX_OK;
2083}
2084
2085
2086static void
Igor Sysoev899b44e2005-05-12 14:58:06 +00002087ngx_http_upstream_process_downstream(ngx_http_request_t *r)
2088{
Igor Sysoev3e15a972008-12-08 18:28:06 +00002089 ngx_event_t *wev;
2090 ngx_connection_t *c;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002091 ngx_event_pipe_t *p;
Igor Sysoev02025fd2005-01-18 13:03:58 +00002092 ngx_http_upstream_t *u;
Igor Sysoev02025fd2005-01-18 13:03:58 +00002093
Igor Sysoev3e15a972008-12-08 18:28:06 +00002094 c = r->connection;
Igor Sysoev02025fd2005-01-18 13:03:58 +00002095 u = r->upstream;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002096 p = u->pipe;
Igor Sysoev3e15a972008-12-08 18:28:06 +00002097 wev = c->write;
Igor Sysoev02025fd2005-01-18 13:03:58 +00002098
Igor Sysoev3e15a972008-12-08 18:28:06 +00002099 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2100 "http upstream process downstream");
Igor Sysoev5192b362005-07-08 14:34:20 +00002101
Igor Sysoev3e15a972008-12-08 18:28:06 +00002102 c->log->action = "sending to client";
Igor Sysoev5192b362005-07-08 14:34:20 +00002103
Igor Sysoev3e15a972008-12-08 18:28:06 +00002104 if (wev->timedout) {
Igor Sysoev5192b362005-07-08 14:34:20 +00002105
Igor Sysoev3e15a972008-12-08 18:28:06 +00002106 if (wev->delayed) {
Igor Sysoev5192b362005-07-08 14:34:20 +00002107
Igor Sysoev3e15a972008-12-08 18:28:06 +00002108 wev->timedout = 0;
2109 wev->delayed = 0;
Igor Sysoev5192b362005-07-08 14:34:20 +00002110
Igor Sysoev3e15a972008-12-08 18:28:06 +00002111 if (!wev->ready) {
2112 ngx_add_timer(wev, p->send_timeout);
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002113
Igor Sysoev3e15a972008-12-08 18:28:06 +00002114 if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) {
Igor Sysoev5192b362005-07-08 14:34:20 +00002115 ngx_http_upstream_finalize_request(r, u, 0);
Igor Sysoev3e15a972008-12-08 18:28:06 +00002116 }
2117
2118 return;
2119 }
2120
2121 if (ngx_event_pipe(p, wev->write) == NGX_ABORT) {
2122
2123 if (c->destroyed) {
Igor Sysoev5192b362005-07-08 14:34:20 +00002124 return;
2125 }
2126
Igor Sysoev3e15a972008-12-08 18:28:06 +00002127 ngx_http_upstream_finalize_request(r, u, 0);
2128 return;
Igor Sysoev5192b362005-07-08 14:34:20 +00002129 }
Igor Sysoev02025fd2005-01-18 13:03:58 +00002130
2131 } else {
Igor Sysoev3e15a972008-12-08 18:28:06 +00002132 p->downstream_error = 1;
2133 c->timedout = 1;
2134 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
Igor Sysoev02025fd2005-01-18 13:03:58 +00002135 }
2136
2137 } else {
Igor Sysoev3e15a972008-12-08 18:28:06 +00002138
2139 if (wev->delayed) {
2140
Igor Sysoev5192b362005-07-08 14:34:20 +00002141 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2142 "http downstream delayed");
2143
Igor Sysoev3e15a972008-12-08 18:28:06 +00002144 if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) {
Igor Sysoev851cd732008-12-08 14:23:20 +00002145 ngx_http_upstream_finalize_request(r, u, 0);
Igor Sysoev5192b362005-07-08 14:34:20 +00002146 }
2147
2148 return;
2149 }
2150
Igor Sysoev3e15a972008-12-08 18:28:06 +00002151 if (ngx_event_pipe(p, 1) == NGX_ABORT) {
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002152
Igor Sysoev3e15a972008-12-08 18:28:06 +00002153 if (c->destroyed) {
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002154 return;
2155 }
2156
Igor Sysoev02025fd2005-01-18 13:03:58 +00002157 ngx_http_upstream_finalize_request(r, u, 0);
2158 return;
2159 }
2160 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002161
Igor Sysoev3e15a972008-12-08 18:28:06 +00002162 ngx_http_upstream_process_request(r);
2163}
2164
2165
2166static void
Igor Sysoevb9409a82008-12-09 17:25:03 +00002167ngx_http_upstream_process_upstream(ngx_http_request_t *r,
2168 ngx_http_upstream_t *u)
Igor Sysoev3e15a972008-12-08 18:28:06 +00002169{
Igor Sysoevb9409a82008-12-09 17:25:03 +00002170 ngx_connection_t *c;
Igor Sysoev3e15a972008-12-08 18:28:06 +00002171
Igor Sysoevb9409a82008-12-09 17:25:03 +00002172 c = u->peer.connection;
Igor Sysoev3e15a972008-12-08 18:28:06 +00002173
2174 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2175 "http upstream process upstream");
2176
2177 c->log->action = "reading upstream";
2178
Igor Sysoevb9409a82008-12-09 17:25:03 +00002179 if (c->read->timedout) {
2180 u->pipe->upstream_error = 1;
Igor Sysoev3e15a972008-12-08 18:28:06 +00002181 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
2182
2183 } else {
2184 c = r->connection;
2185
Igor Sysoevb9409a82008-12-09 17:25:03 +00002186 if (ngx_event_pipe(u->pipe, 0) == NGX_ABORT) {
Igor Sysoev3e15a972008-12-08 18:28:06 +00002187
2188 if (c->destroyed) {
2189 return;
2190 }
2191
2192 ngx_http_upstream_finalize_request(r, u, 0);
2193 return;
2194 }
2195 }
2196
2197 ngx_http_upstream_process_request(r);
2198}
2199
2200
2201static void
2202ngx_http_upstream_process_request(ngx_http_request_t *r)
2203{
2204 ngx_temp_file_t *tf;
2205 ngx_event_pipe_t *p;
2206 ngx_http_upstream_t *u;
2207
2208 u = r->upstream;
2209 p = u->pipe;
2210
Igor Sysoev02025fd2005-01-18 13:03:58 +00002211 if (u->peer.connection) {
2212
Igor Sysoev58feb532007-07-12 11:19:05 +00002213 if (u->store) {
2214
Igor Sysoevbe0a61e2008-02-11 13:14:56 +00002215 tf = u->pipe->temp_file;
Igor Sysoev58feb532007-07-12 11:19:05 +00002216
Igor Sysoevbe0a61e2008-02-11 13:14:56 +00002217 if (p->upstream_eof
2218 && u->headers_in.status_n == NGX_HTTP_OK
2219 && (u->headers_in.content_length_n == -1
2220 || (u->headers_in.content_length_n == tf->offset)))
2221 {
Igor Sysoev58feb532007-07-12 11:19:05 +00002222 ngx_http_upstream_store(r, u);
2223
2224 } else if ((p->upstream_error
2225 || (p->upstream_eof
2226 && u->headers_in.status_n != NGX_HTTP_OK))
Igor Sysoevbe0a61e2008-02-11 13:14:56 +00002227 && tf->file.fd != NGX_INVALID_FILE)
Igor Sysoev58feb532007-07-12 11:19:05 +00002228 {
Igor Sysoevbe0a61e2008-02-11 13:14:56 +00002229 if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {
2230
Igor Sysoev58feb532007-07-12 11:19:05 +00002231 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
2232 ngx_delete_file_n " \"%s\" failed",
2233 u->pipe->temp_file->file.name.data);
2234 }
2235 }
2236 }
2237
Igor Sysoev02025fd2005-01-18 13:03:58 +00002238#if (NGX_HTTP_FILE_CACHE)
2239
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00002240 if (p->upstream_done && u->cacheable) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00002241 if (ngx_http_cache_update(r) == NGX_ERROR) {
2242 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
2243 ngx_http_upstream_finalize_request(r, u, 0);
2244 return;
2245 }
2246
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00002247 } else if (p->upstream_eof && u->cacheable) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00002248
2249 /* TODO: check length & update cache */
2250
2251 if (ngx_http_cache_update(r) == NGX_ERROR) {
2252 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
2253 ngx_http_upstream_finalize_request(r, u, 0);
2254 return;
2255 }
2256 }
2257
2258#endif
2259
2260 if (p->upstream_done || p->upstream_eof || p->upstream_error) {
Igor Sysoev3e15a972008-12-08 18:28:06 +00002261 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002262 "http upstream exit: %p", p->out);
Igor Sysoev02025fd2005-01-18 13:03:58 +00002263#if 0
2264 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
2265#endif
2266 ngx_http_upstream_finalize_request(r, u, 0);
2267 return;
2268 }
2269 }
2270
2271 if (p->downstream_error) {
Igor Sysoev3e15a972008-12-08 18:28:06 +00002272 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002273 "http upstream downstream error");
Igor Sysoev02025fd2005-01-18 13:03:58 +00002274
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00002275 if (!u->cacheable && u->peer.connection) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00002276 ngx_http_upstream_finalize_request(r, u, 0);
2277 }
2278 }
2279}
2280
2281
Igor Sysoevaa828612005-02-09 14:31:07 +00002282static void
Igor Sysoev58feb532007-07-12 11:19:05 +00002283ngx_http_upstream_store(ngx_http_request_t *r, ngx_http_upstream_t *u)
2284{
Igor Sysoev9ff26ce2008-03-17 13:07:35 +00002285 size_t root;
2286 time_t lm;
2287 ngx_str_t path;
2288 ngx_temp_file_t *tf;
2289 ngx_ext_rename_file_t ext;
Igor Sysoev58feb532007-07-12 11:19:05 +00002290
Igor Sysoev0123ee32007-12-26 13:24:57 +00002291 tf = u->pipe->temp_file;
2292
2293 if (tf->file.fd == NGX_INVALID_FILE) {
Igor Sysoev58feb532007-07-12 11:19:05 +00002294
2295 /* create file for empty 200 response */
2296
2297 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
2298 if (tf == NULL) {
2299 return;
2300 }
2301
2302 tf->file.fd = NGX_INVALID_FILE;
2303 tf->file.log = r->connection->log;
2304 tf->path = u->conf->temp_path;
2305 tf->pool = r->pool;
2306 tf->persistent = 1;
2307
2308 if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
2309 tf->persistent, tf->clean, tf->access)
2310 != NGX_OK)
2311 {
2312 return;
2313 }
2314
2315 u->pipe->temp_file = tf;
2316 }
2317
Igor Sysoev9ff26ce2008-03-17 13:07:35 +00002318 ext.access = u->conf->store_access;
Igor Sysoev524f54f2008-12-10 14:53:45 +00002319 ext.path_access = u->conf->store_access;
Igor Sysoev9ff26ce2008-03-17 13:07:35 +00002320 ext.time = -1;
2321 ext.create_path = 1;
2322 ext.delete_file = 1;
Igor Sysoev524f54f2008-12-10 14:53:45 +00002323 ext.log_rename_error = 1;
Igor Sysoev9ff26ce2008-03-17 13:07:35 +00002324 ext.log = r->connection->log;
Igor Sysoev58feb532007-07-12 11:19:05 +00002325
Igor Sysoevf2932dc2007-12-10 12:29:43 +00002326 if (u->headers_in.last_modified) {
Igor Sysoev58feb532007-07-12 11:19:05 +00002327
Igor Sysoev9ff26ce2008-03-17 13:07:35 +00002328 lm = ngx_http_parse_time(u->headers_in.last_modified->value.data,
2329 u->headers_in.last_modified->value.len);
Igor Sysoev58feb532007-07-12 11:19:05 +00002330
2331 if (lm != NGX_ERROR) {
Igor Sysoev9ff26ce2008-03-17 13:07:35 +00002332 ext.time = lm;
2333 ext.fd = tf->file.fd;
Igor Sysoev58feb532007-07-12 11:19:05 +00002334 }
2335 }
2336
Igor Sysoevfbd9b432007-07-13 08:30:34 +00002337 if (u->conf->store_lengths == NULL) {
2338
2339 ngx_http_map_uri_to_path(r, &path, &root, 0);
2340
2341 } else {
2342 if (ngx_http_script_run(r, &path, u->conf->store_lengths->elts, 0,
2343 u->conf->store_values->elts)
2344 == NULL)
2345 {
2346 return;
2347 }
2348 }
Igor Sysoev58feb532007-07-12 11:19:05 +00002349
2350 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev9ff26ce2008-03-17 13:07:35 +00002351 "upstream stores \"%s\" to \"%s\"",
2352 tf->file.name.data, path.data);
Igor Sysoev58feb532007-07-12 11:19:05 +00002353
Igor Sysoev9ff26ce2008-03-17 13:07:35 +00002354 (void) ngx_ext_rename_file(&tf->file.name, &path, &ext);
Igor Sysoev58feb532007-07-12 11:19:05 +00002355}
2356
2357
2358static void
Igor Sysoevb9409a82008-12-09 17:25:03 +00002359ngx_http_upstream_dummy_handler(ngx_http_request_t *r, ngx_http_upstream_t *u)
Igor Sysoev02025fd2005-01-18 13:03:58 +00002360{
Igor Sysoevb9409a82008-12-09 17:25:03 +00002361 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev02025fd2005-01-18 13:03:58 +00002362 "http upstream dummy handler");
2363}
2364
2365
Igor Sysoevaa828612005-02-09 14:31:07 +00002366static void
2367ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
2368 ngx_uint_t ft_type)
Igor Sysoev02025fd2005-01-18 13:03:58 +00002369{
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002370 ngx_uint_t status, state;
Igor Sysoev02025fd2005-01-18 13:03:58 +00002371
2372 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002373 "http next upstream, %xi", ft_type);
Igor Sysoev02025fd2005-01-18 13:03:58 +00002374
2375#if 0
2376 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
2377#endif
2378
Igor Sysoev85ef94b2005-06-23 13:41:06 +00002379 if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002380 state = NGX_PEER_NEXT;
Igor Sysoev85ef94b2005-06-23 13:41:06 +00002381 } else {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002382 state = NGX_PEER_FAILED;
Igor Sysoev02025fd2005-01-18 13:03:58 +00002383 }
Igor Sysoev85ef94b2005-06-23 13:41:06 +00002384
Igor Sysoev6876bcd2007-08-09 13:54:33 +00002385 if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) {
2386 u->peer.free(&u->peer, u->peer.data, state);
2387 }
Igor Sysoev85ef94b2005-06-23 13:41:06 +00002388
Igor Sysoev02025fd2005-01-18 13:03:58 +00002389 if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {
2390 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,
2391 "upstream timed out");
2392 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002393
Igor Sysoev02025fd2005-01-18 13:03:58 +00002394 if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {
2395 status = 0;
2396
2397 } else {
2398 switch(ft_type) {
2399
2400 case NGX_HTTP_UPSTREAM_FT_TIMEOUT:
2401 status = NGX_HTTP_GATEWAY_TIME_OUT;
2402 break;
2403
2404 case NGX_HTTP_UPSTREAM_FT_HTTP_500:
2405 status = NGX_HTTP_INTERNAL_SERVER_ERROR;
2406 break;
2407
2408 case NGX_HTTP_UPSTREAM_FT_HTTP_404:
2409 status = NGX_HTTP_NOT_FOUND;
2410 break;
2411
2412 /*
2413 * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING
2414 * never reach here
2415 */
2416
2417 default:
2418 status = NGX_HTTP_BAD_GATEWAY;
2419 }
2420 }
2421
Igor Sysoevd3283ff2005-12-05 13:18:09 +00002422 if (r->connection->error) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00002423 ngx_http_upstream_finalize_request(r, u,
2424 NGX_HTTP_CLIENT_CLOSED_REQUEST);
2425 return;
2426 }
2427
2428 if (status) {
2429 u->state->status = status;
2430
Igor Sysoev85ef94b2005-06-23 13:41:06 +00002431 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00002432
2433#if (NGX_HTTP_CACHE)
2434
2435 if (u->stale && (u->conf->use_stale & ft_type)) {
2436 ngx_http_upstream_finalize_request(r, u,
Igor Sysoev85ef94b2005-06-23 13:41:06 +00002437 ngx_http_send_cached_response(r));
Igor Sysoev02025fd2005-01-18 13:03:58 +00002438 return;
2439 }
2440
2441#endif
2442
2443 ngx_http_upstream_finalize_request(r, u, status);
2444 return;
2445 }
2446 }
2447
2448 if (u->peer.connection) {
2449 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2450 "close http upstream connection: %d",
2451 u->peer.connection->fd);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002452#if (NGX_HTTP_SSL)
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002453
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002454 if (u->peer.connection->ssl) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002455 u->peer.connection->ssl->no_wait_shutdown = 1;
2456 u->peer.connection->ssl->no_send_shutdown = 1;
2457
2458 (void) ngx_ssl_shutdown(u->peer.connection);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002459 }
2460#endif
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002461
Igor Sysoev02025fd2005-01-18 13:03:58 +00002462 ngx_close_connection(u->peer.connection);
2463 }
2464
2465#if 0
2466 if (u->conf->busy_lock && !u->busy_locked) {
2467 ngx_http_upstream_busy_lock(p);
2468 return;
2469 }
2470#endif
2471
2472 ngx_http_upstream_connect(r, u);
2473}
2474
2475
Igor Sysoevaa828612005-02-09 14:31:07 +00002476static void
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002477ngx_http_upstream_cleanup(void *data)
2478{
2479 ngx_http_request_t *r = data;
2480
Igor Sysoevf2932dc2007-12-10 12:29:43 +00002481 ngx_http_upstream_t *u;
2482
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002483 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2484 "cleanup http upstream request: \"%V\"", &r->uri);
2485
Igor Sysoevf2932dc2007-12-10 12:29:43 +00002486 u = r->upstream;
2487
2488 if (u->resolved && u->resolved->ctx) {
2489 ngx_resolve_name_done(u->resolved->ctx);
Igor Sysoev96dd8af2007-11-27 13:34:13 +00002490 }
2491
Igor Sysoevf2932dc2007-12-10 12:29:43 +00002492 ngx_http_upstream_finalize_request(r, u, NGX_DONE);
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002493}
2494
2495
2496static void
Igor Sysoevaa828612005-02-09 14:31:07 +00002497ngx_http_upstream_finalize_request(ngx_http_request_t *r,
2498 ngx_http_upstream_t *u, ngx_int_t rc)
Igor Sysoev02025fd2005-01-18 13:03:58 +00002499{
Igor Sysoev8f985812006-12-06 11:33:18 +00002500 ngx_time_t *tp;
Igor Sysoevc2068d02005-10-19 12:33:58 +00002501
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002502 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2503 "finalize http upstream request: %i", rc);
Igor Sysoev02025fd2005-01-18 13:03:58 +00002504
Igor Sysoevcf6c5822008-12-17 16:07:58 +00002505 if (u->cleanup) {
2506 *u->cleanup = NULL;
2507 }
Igor Sysoev9ac946b2005-10-24 15:09:41 +00002508
Igor Sysoev96dd8af2007-11-27 13:34:13 +00002509 if (u->state && u->state->response_sec) {
Igor Sysoevc2068d02005-10-19 12:33:58 +00002510 tp = ngx_timeofday();
Igor Sysoev8f985812006-12-06 11:33:18 +00002511 u->state->response_sec = tp->sec - u->state->response_sec;
2512 u->state->response_msec = tp->msec - u->state->response_msec;
Igor Sysoev371766c2008-12-11 15:30:52 +00002513
2514 if (u->pipe) {
2515 u->state->response_length = u->pipe->read_length;
2516 }
Igor Sysoevc2068d02005-10-19 12:33:58 +00002517 }
2518
Igor Sysoev02025fd2005-01-18 13:03:58 +00002519 u->finalize_request(r, rc);
2520
Igor Sysoev96dd8af2007-11-27 13:34:13 +00002521 if (u->peer.free) {
2522 u->peer.free(&u->peer, u->peer.data, 0);
2523 }
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002524
Igor Sysoev02025fd2005-01-18 13:03:58 +00002525 if (u->peer.connection) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002526
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002527#if (NGX_HTTP_SSL)
Igor Sysoev02025fd2005-01-18 13:03:58 +00002528
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002529 /* TODO: do not shutdown persistent connection */
2530
2531 if (u->peer.connection->ssl) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002532
2533 /*
2534 * We send the "close notify" shutdown alert to the upstream only
2535 * and do not wait its "close notify" shutdown alert.
2536 * It is acceptable according to the TLS standard.
2537 */
2538
2539 u->peer.connection->ssl->no_wait_shutdown = 1;
2540
2541 (void) ngx_ssl_shutdown(u->peer.connection);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002542 }
2543#endif
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002544
2545 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2546 "close http upstream connection: %d",
2547 u->peer.connection->fd);
2548
Igor Sysoev02025fd2005-01-18 13:03:58 +00002549 ngx_close_connection(u->peer.connection);
2550 }
2551
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002552 u->peer.connection = NULL;
2553
Igor Sysoevc2068d02005-10-19 12:33:58 +00002554 if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
Igor Sysoev02025fd2005-01-18 13:03:58 +00002555 {
2556 rc = 0;
2557 }
2558
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002559 if (u->pipe && u->pipe->temp_file) {
Igor Sysoev02025fd2005-01-18 13:03:58 +00002560 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2561 "http upstream temp fd: %d",
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00002562 u->pipe->temp_file->file.fd);
Igor Sysoev02025fd2005-01-18 13:03:58 +00002563 }
2564
2565#if 0
2566 if (u->cache) {
2567 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002568 "http upstream cache fd: %d",
Igor Sysoev02025fd2005-01-18 13:03:58 +00002569 u->cache->ctx.file.fd);
2570 }
2571#endif
2572
Igor Sysoeve31e90b2005-05-19 13:25:22 +00002573 if (rc == NGX_DECLINED) {
2574 return;
2575 }
2576
Igor Sysoeve5a222c2005-01-25 12:27:35 +00002577 r->connection->log->action = "sending to client";
2578
Igor Sysoev851cd732008-12-08 14:23:20 +00002579 if (rc == 0 && r == r->main && !r->post_action) {
2580 rc = ngx_http_send_special(r, NGX_HTTP_LAST);
Igor Sysoev02025fd2005-01-18 13:03:58 +00002581 }
2582
2583 ngx_http_finalize_request(r, rc);
2584}
2585
2586
Igor Sysoev899b44e2005-05-12 14:58:06 +00002587static ngx_int_t
2588ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
2589 ngx_uint_t offset)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002590{
Igor Sysoev899b44e2005-05-12 14:58:06 +00002591 ngx_table_elt_t **ph;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002592
Igor Sysoev899b44e2005-05-12 14:58:06 +00002593 ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset);
2594
2595 if (*ph == NULL) {
2596 *ph = h;
2597 }
2598
2599 return NGX_OK;
2600}
2601
2602
2603static ngx_int_t
2604ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r,
2605 ngx_table_elt_t *h, ngx_uint_t offset)
2606{
2607 ngx_array_t *pa;
2608 ngx_table_elt_t **ph;
2609
2610 pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset);
2611
2612 if (pa->elts == NULL) {
2613 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
2614 {
2615 return NGX_ERROR;
2616 }
2617 }
2618
2619 ph = ngx_array_push(pa);
2620 if (ph == NULL) {
2621 return NGX_ERROR;
2622 }
2623
2624 *ph = h;
2625
2626 return NGX_OK;
2627}
2628
2629
2630static ngx_int_t
2631ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
2632 ngx_uint_t offset)
2633{
2634 return NGX_OK;
2635}
2636
2637
2638static ngx_int_t
Igor Sysoev5192b362005-07-08 14:34:20 +00002639ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h,
2640 ngx_uint_t offset)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002641{
Igor Sysoev5192b362005-07-08 14:34:20 +00002642 ngx_int_t n;
2643
2644 r->upstream->headers_in.x_accel_limit_rate = h;
2645
2646 n = ngx_atoi(h->value.data, h->value.len);
2647
2648 if (n != NGX_ERROR) {
2649 r->limit_rate = (size_t) n;
2650 }
2651
2652 return NGX_OK;
2653}
2654
2655
2656static ngx_int_t
Igor Sysoev3338cfd2006-05-11 14:43:47 +00002657ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h,
2658 ngx_uint_t offset)
2659{
2660 u_char c0, c1, c2;
2661
2662 if (r->upstream->conf->change_buffering) {
2663
2664 if (h->value.len == 2) {
2665 c0 = ngx_tolower(h->value.data[0]);
2666 c1 = ngx_tolower(h->value.data[1]);
2667
2668 if (c0 == 'n' && c1 == 'o') {
2669 r->upstream->buffering = 0;
2670 }
2671
2672 } else if (h->value.len == 3) {
2673 c0 = ngx_tolower(h->value.data[0]);
2674 c1 = ngx_tolower(h->value.data[1]);
2675 c2 = ngx_tolower(h->value.data[2]);
2676
2677 if (c0 == 'y' && c1 == 'e' && c2 == 's') {
2678 r->upstream->buffering = 1;
2679 }
2680 }
2681 }
2682
2683 return NGX_OK;
2684}
2685
2686
2687static ngx_int_t
Igor Sysoevef809b82006-06-28 16:00:26 +00002688ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h,
2689 ngx_uint_t offset)
2690{
2691 r->headers_out.override_charset = &h->value;
2692
2693 return NGX_OK;
2694}
2695
2696
2697static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +00002698ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
2699 ngx_uint_t offset)
2700{
Igor Sysoev09c684b2005-11-09 17:25:55 +00002701 ngx_table_elt_t *ho, **ph;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002702
2703 ho = ngx_list_push(&r->headers_out.headers);
2704 if (ho == NULL) {
2705 return NGX_ERROR;
2706 }
2707
2708 *ho = *h;
2709
Igor Sysoev09c684b2005-11-09 17:25:55 +00002710 if (offset) {
2711 ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset);
2712 *ph = ho;
2713 }
2714
Igor Sysoev899b44e2005-05-12 14:58:06 +00002715 return NGX_OK;
2716}
2717
2718
2719static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +00002720ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r,
2721 ngx_table_elt_t *h, ngx_uint_t offset)
2722{
2723 ngx_array_t *pa;
2724 ngx_table_elt_t *ho, **ph;
2725
2726 pa = (ngx_array_t *) ((char *) &r->headers_out + offset);
2727
2728 if (pa->elts == NULL) {
2729 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
2730 {
2731 return NGX_ERROR;
2732 }
2733 }
2734
2735 ph = ngx_array_push(pa);
2736 if (ph == NULL) {
2737 return NGX_ERROR;
2738 }
2739
2740 ho = ngx_list_push(&r->headers_out.headers);
2741 if (ho == NULL) {
2742 return NGX_ERROR;
2743 }
2744
2745 *ho = *h;
2746 *ph = ho;
2747
2748 return NGX_OK;
2749}
2750
2751
2752static ngx_int_t
2753ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h,
2754 ngx_uint_t offset)
2755{
Igor Sysoevef809b82006-06-28 16:00:26 +00002756 u_char *p, *last;
2757
2758 r->headers_out.content_type_len = h->value.len;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002759 r->headers_out.content_type = h->value;
2760
Igor Sysoevef809b82006-06-28 16:00:26 +00002761 for (p = h->value.data; *p; p++) {
2762
2763 if (*p != ';') {
2764 continue;
2765 }
2766
2767 last = p;
2768
2769 while (*++p == ' ') { /* void */ }
2770
Igor Sysoev523e09e2008-04-11 09:15:07 +00002771 if (*p == '\0') {
2772 return NGX_OK;
2773 }
2774
Igor Sysoev722231f2007-02-14 18:51:19 +00002775 if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) {
Igor Sysoevef809b82006-06-28 16:00:26 +00002776 continue;
2777 }
2778
2779 p += 8;
2780
2781 r->headers_out.content_type_len = last - h->value.data;
2782
Igor Sysoev31654252008-09-09 11:58:45 +00002783 if (*p == '"') {
2784 p++;
2785 }
2786
2787 last = h->value.data + h->value.len;
2788
2789 if (*(last - 1) == '"') {
2790 last--;
2791 }
2792
2793 r->headers_out.charset.len = last - p;
Igor Sysoevef809b82006-06-28 16:00:26 +00002794 r->headers_out.charset.data = p;
Igor Sysoev77d7f802007-03-23 11:33:47 +00002795
2796 return NGX_OK;
Igor Sysoevef809b82006-06-28 16:00:26 +00002797 }
2798
Igor Sysoev899b44e2005-05-12 14:58:06 +00002799 return NGX_OK;
2800}
2801
2802
2803static ngx_int_t
2804ngx_http_upstream_copy_content_length(ngx_http_request_t *r, ngx_table_elt_t *h,
2805 ngx_uint_t offset)
2806{
2807 ngx_table_elt_t *ho;
2808
2809 ho = ngx_list_push(&r->headers_out.headers);
2810 if (ho == NULL) {
2811 return NGX_ERROR;
2812 }
2813
2814 *ho = *h;
2815
2816 r->headers_out.content_length = ho;
2817 r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len);
2818
2819 return NGX_OK;
2820}
2821
2822
2823static ngx_int_t
2824ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h,
2825 ngx_uint_t offset)
2826{
2827 ngx_int_t rc;
2828 ngx_table_elt_t *ho;
2829
2830 ho = ngx_list_push(&r->headers_out.headers);
2831 if (ho == NULL) {
2832 return NGX_ERROR;
2833 }
2834
2835 *ho = *h;
2836
2837 if (r->upstream->rewrite_redirect) {
2838 rc = r->upstream->rewrite_redirect(r, ho, 0);
2839
Igor Sysoev187b7d92005-07-14 12:51:53 +00002840 if (rc == NGX_DECLINED) {
2841 return NGX_OK;
2842 }
2843
Igor Sysoev899b44e2005-05-12 14:58:06 +00002844 if (rc == NGX_OK) {
2845 r->headers_out.location = ho;
2846
2847 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2848 "rewritten location: \"%V\"", &ho->value);
2849 }
2850
2851 return rc;
2852 }
2853
Igor Sysoev685fd082007-11-27 11:52:37 +00002854 if (ho->value.data[0] != '/') {
2855 r->headers_out.location = ho;
2856 }
2857
Igor Sysoev899b44e2005-05-12 14:58:06 +00002858 /*
2859 * we do not set r->headers_out.location here to avoid the handling
2860 * the local redirects without a host name by ngx_http_header_filter()
2861 */
2862
2863 return NGX_OK;
2864}
2865
2866
2867static ngx_int_t
2868ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h,
2869 ngx_uint_t offset)
2870{
2871 u_char *p;
2872 ngx_int_t rc;
2873 ngx_table_elt_t *ho;
2874
2875 ho = ngx_list_push(&r->headers_out.headers);
2876 if (ho == NULL) {
2877 return NGX_ERROR;
2878 }
2879
2880 *ho = *h;
2881
2882 if (r->upstream->rewrite_redirect) {
2883
Igor Sysoevf5329412007-10-01 12:57:47 +00002884 p = ngx_strcasestrn(ho->value.data, "url=", 4 - 1);
Igor Sysoev899b44e2005-05-12 14:58:06 +00002885
2886 if (p) {
2887 rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data);
2888
2889 } else {
2890 return NGX_OK;
2891 }
2892
Igor Sysoev187b7d92005-07-14 12:51:53 +00002893 if (rc == NGX_DECLINED) {
2894 return NGX_OK;
2895 }
2896
Igor Sysoev899b44e2005-05-12 14:58:06 +00002897 if (rc == NGX_OK) {
Igor Sysoev32717eb2007-11-27 11:56:18 +00002898 r->headers_out.refresh = ho;
2899
Igor Sysoev899b44e2005-05-12 14:58:06 +00002900 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2901 "rewritten refresh: \"%V\"", &ho->value);
2902 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00002903
2904 return rc;
2905 }
2906
Igor Sysoev32717eb2007-11-27 11:56:18 +00002907 r->headers_out.refresh = ho;
2908
Igor Sysoev899b44e2005-05-12 14:58:06 +00002909 return NGX_OK;
2910}
2911
2912
2913#if (NGX_HTTP_GZIP)
2914
2915static ngx_int_t
2916ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r,
2917 ngx_table_elt_t *h, ngx_uint_t offset)
2918{
2919 ngx_table_elt_t *ho;
2920
2921 ho = ngx_list_push(&r->headers_out.headers);
2922 if (ho == NULL) {
2923 return NGX_ERROR;
2924 }
2925
2926 *ho = *h;
2927
2928 r->headers_out.content_encoding = ho;
2929
2930 return NGX_OK;
2931}
2932
2933#endif
2934
2935
Igor Sysoevaa828612005-02-09 14:31:07 +00002936static ngx_int_t
Igor Sysoev09c684b2005-11-09 17:25:55 +00002937ngx_http_upstream_add_variables(ngx_conf_t *cf)
Igor Sysoev02025fd2005-01-18 13:03:58 +00002938{
Igor Sysoeva8c54c02006-11-27 14:46:15 +00002939 ngx_http_variable_t *var, *v;
Igor Sysoev02025fd2005-01-18 13:03:58 +00002940
Igor Sysoev09c684b2005-11-09 17:25:55 +00002941 for (v = ngx_http_upstream_vars; v->name.len; v++) {
2942 var = ngx_http_add_variable(cf, &v->name, v->flags);
2943 if (var == NULL) {
2944 return NGX_ERROR;
2945 }
2946
Igor Sysoev7bdb7202006-04-19 15:30:56 +00002947 var->get_handler = v->get_handler;
Igor Sysoev09c684b2005-11-09 17:25:55 +00002948 var->data = v->data;
2949 }
2950
Igor Sysoev02025fd2005-01-18 13:03:58 +00002951 return NGX_OK;
2952}
Igor Sysoev899b44e2005-05-12 14:58:06 +00002953
2954
Igor Sysoev09c684b2005-11-09 17:25:55 +00002955static ngx_int_t
Igor Sysoev29f59122007-04-23 16:56:17 +00002956ngx_http_upstream_addr_variable(ngx_http_request_t *r,
2957 ngx_http_variable_value_t *v, uintptr_t data)
2958{
2959 u_char *p;
2960 size_t len;
2961 ngx_uint_t i;
2962 ngx_http_upstream_state_t *state;
2963
2964 v->valid = 1;
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00002965 v->no_cacheable = 0;
Igor Sysoev29f59122007-04-23 16:56:17 +00002966 v->not_found = 0;
2967
2968 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
2969 v->not_found = 1;
2970 return NGX_OK;
2971 }
2972
2973 len = 0;
2974 state = r->upstream_states->elts;
2975
2976 for (i = 0; i < r->upstream_states->nelts; i++) {
2977 if (state[i].peer) {
2978 len += state[i].peer->len + 2;
2979
2980 } else {
2981 len += 3;
2982 }
2983 }
2984
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +00002985 p = ngx_pnalloc(r->pool, len);
Igor Sysoev29f59122007-04-23 16:56:17 +00002986 if (p == NULL) {
2987 return NGX_ERROR;
2988 }
2989
2990 v->data = p;
2991
2992 i = 0;
2993
2994 for ( ;; ) {
2995 if (state[i].peer) {
2996 p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len);
2997 }
2998
2999 if (++i == r->upstream_states->nelts) {
3000 break;
3001 }
3002
3003 if (state[i].peer) {
3004 *p++ = ',';
3005 *p++ = ' ';
3006
3007 } else {
3008 *p++ = ' ';
3009 *p++ = ':';
3010 *p++ = ' ';
3011
3012 if (++i == r->upstream_states->nelts) {
3013 break;
3014 }
3015
3016 continue;
3017 }
3018 }
3019
3020 v->len = p - v->data;
3021
3022 return NGX_OK;
3023}
3024
3025
3026static ngx_int_t
Igor Sysoev09c684b2005-11-09 17:25:55 +00003027ngx_http_upstream_status_variable(ngx_http_request_t *r,
3028 ngx_http_variable_value_t *v, uintptr_t data)
3029{
3030 u_char *p;
3031 size_t len;
3032 ngx_uint_t i;
Igor Sysoev09c684b2005-11-09 17:25:55 +00003033 ngx_http_upstream_state_t *state;
3034
3035 v->valid = 1;
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00003036 v->no_cacheable = 0;
Igor Sysoev09c684b2005-11-09 17:25:55 +00003037 v->not_found = 0;
3038
Igor Sysoeve0f86622007-04-18 20:23:19 +00003039 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00003040 v->not_found = 1;
3041 return NGX_OK;
3042 }
3043
Igor Sysoeve0f86622007-04-18 20:23:19 +00003044 len = r->upstream_states->nelts * (3 + 2);
Igor Sysoev09c684b2005-11-09 17:25:55 +00003045
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +00003046 p = ngx_pnalloc(r->pool, len);
Igor Sysoev09c684b2005-11-09 17:25:55 +00003047 if (p == NULL) {
3048 return NGX_ERROR;
3049 }
3050
3051 v->data = p;
3052
3053 i = 0;
Igor Sysoeve0f86622007-04-18 20:23:19 +00003054 state = r->upstream_states->elts;
Igor Sysoev09c684b2005-11-09 17:25:55 +00003055
3056 for ( ;; ) {
Igor Sysoeve0f86622007-04-18 20:23:19 +00003057 if (state[i].status) {
3058 p = ngx_sprintf(p, "%ui", state[i].status);
Igor Sysoev09c684b2005-11-09 17:25:55 +00003059
3060 } else {
Igor Sysoeve0f86622007-04-18 20:23:19 +00003061 *p++ = '-';
Igor Sysoev09c684b2005-11-09 17:25:55 +00003062 }
3063
Igor Sysoeve0f86622007-04-18 20:23:19 +00003064 if (++i == r->upstream_states->nelts) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00003065 break;
3066 }
3067
Igor Sysoeve0f86622007-04-18 20:23:19 +00003068 if (state[i].peer) {
3069 *p++ = ',';
3070 *p++ = ' ';
3071
3072 } else {
3073 *p++ = ' ';
3074 *p++ = ':';
3075 *p++ = ' ';
3076
3077 if (++i == r->upstream_states->nelts) {
3078 break;
3079 }
3080
3081 continue;
3082 }
Igor Sysoev09c684b2005-11-09 17:25:55 +00003083 }
3084
3085 v->len = p - v->data;
3086
3087 return NGX_OK;
3088}
3089
3090
3091static ngx_int_t
3092ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
3093 ngx_http_variable_value_t *v, uintptr_t data)
3094{
3095 u_char *p;
3096 size_t len;
3097 ngx_uint_t i;
Igor Sysoev8f985812006-12-06 11:33:18 +00003098 ngx_msec_int_t ms;
Igor Sysoev09c684b2005-11-09 17:25:55 +00003099 ngx_http_upstream_state_t *state;
3100
3101 v->valid = 1;
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00003102 v->no_cacheable = 0;
Igor Sysoev09c684b2005-11-09 17:25:55 +00003103 v->not_found = 0;
3104
Igor Sysoeve0f86622007-04-18 20:23:19 +00003105 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00003106 v->not_found = 1;
3107 return NGX_OK;
3108 }
3109
Igor Sysoeve0f86622007-04-18 20:23:19 +00003110 len = r->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2);
Igor Sysoev09c684b2005-11-09 17:25:55 +00003111
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +00003112 p = ngx_pnalloc(r->pool, len);
Igor Sysoev09c684b2005-11-09 17:25:55 +00003113 if (p == NULL) {
3114 return NGX_ERROR;
3115 }
3116
3117 v->data = p;
3118
3119 i = 0;
Igor Sysoeve0f86622007-04-18 20:23:19 +00003120 state = r->upstream_states->elts;
Igor Sysoev09c684b2005-11-09 17:25:55 +00003121
3122 for ( ;; ) {
Igor Sysoeve0f86622007-04-18 20:23:19 +00003123 if (state[i].status) {
Igor Sysoev1d04b142007-11-15 14:26:36 +00003124 ms = (ngx_msec_int_t)
3125 (state[i].response_sec * 1000 + state[i].response_msec);
Igor Sysoev8f985812006-12-06 11:33:18 +00003126 ms = (ms >= 0) ? ms : 0;
3127 p = ngx_sprintf(p, "%d.%03d", ms / 1000, ms % 1000);
Igor Sysoeve0f86622007-04-18 20:23:19 +00003128
3129 } else {
3130 *p++ = '-';
Igor Sysoev09c684b2005-11-09 17:25:55 +00003131 }
3132
Igor Sysoeve0f86622007-04-18 20:23:19 +00003133 if (++i == r->upstream_states->nelts) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00003134 break;
3135 }
3136
Igor Sysoeve0f86622007-04-18 20:23:19 +00003137 if (state[i].peer) {
3138 *p++ = ',';
3139 *p++ = ' ';
3140
3141 } else {
3142 *p++ = ' ';
3143 *p++ = ':';
3144 *p++ = ' ';
3145
3146 if (++i == r->upstream_states->nelts) {
3147 break;
3148 }
3149
3150 continue;
3151 }
Igor Sysoev09c684b2005-11-09 17:25:55 +00003152 }
3153
3154 v->len = p - v->data;
3155
3156 return NGX_OK;
3157}
3158
3159
Igor Sysoev371766c2008-12-11 15:30:52 +00003160static ngx_int_t
3161ngx_http_upstream_response_length_variable(ngx_http_request_t *r,
3162 ngx_http_variable_value_t *v, uintptr_t data)
3163{
3164 u_char *p;
3165 size_t len;
3166 ngx_uint_t i;
3167 ngx_http_upstream_state_t *state;
3168
3169 v->valid = 1;
3170 v->no_cacheable = 0;
3171 v->not_found = 0;
3172
3173 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
3174 v->not_found = 1;
3175 return NGX_OK;
3176 }
3177
3178 len = r->upstream_states->nelts * (NGX_OFF_T_LEN + 2);
3179
3180 p = ngx_pnalloc(r->pool, len);
3181 if (p == NULL) {
3182 return NGX_ERROR;
3183 }
3184
3185 v->data = p;
3186
3187 i = 0;
3188 state = r->upstream_states->elts;
3189
3190 for ( ;; ) {
3191 p = ngx_sprintf(p, "%O", state[i].response_length);
3192
3193 if (++i == r->upstream_states->nelts) {
3194 break;
3195 }
3196
3197 if (state[i].peer) {
3198 *p++ = ',';
3199 *p++ = ' ';
3200
3201 } else {
3202 *p++ = ' ';
3203 *p++ = ':';
3204 *p++ = ' ';
3205
3206 if (++i == r->upstream_states->nelts) {
3207 break;
3208 }
3209
3210 continue;
3211 }
3212 }
3213
3214 v->len = p - v->data;
3215
3216 return NGX_OK;
3217}
3218
3219
Igor Sysoev27233612007-04-10 07:08:06 +00003220ngx_int_t
3221ngx_http_upstream_header_variable(ngx_http_request_t *r,
3222 ngx_http_variable_value_t *v, uintptr_t data)
3223{
3224 if (r->upstream == NULL) {
3225 v->not_found = 1;
3226 return NGX_OK;
3227 }
3228
3229 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
3230 &r->upstream->headers_in.headers.part,
3231 sizeof("upstream_http_") - 1);
3232}
3233
3234
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003235static char *
3236ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
3237{
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003238 char *rv;
3239 void *mconf;
3240 ngx_str_t *value;
3241 ngx_url_t u;
3242 ngx_uint_t m;
3243 ngx_conf_t pcf;
3244 ngx_http_module_t *module;
3245 ngx_http_conf_ctx_t *ctx, *http_ctx;
3246 ngx_http_upstream_srv_conf_t *uscf;
3247
3248 ngx_memzero(&u, sizeof(ngx_url_t));
3249
3250 value = cf->args->elts;
3251 u.host = value[1];
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003252 u.no_resolve = 1;
3253
3254 uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE
3255 |NGX_HTTP_UPSTREAM_WEIGHT
3256 |NGX_HTTP_UPSTREAM_MAX_FAILS
3257 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
3258 |NGX_HTTP_UPSTREAM_DOWN
3259 |NGX_HTTP_UPSTREAM_BACKUP);
3260 if (uscf == NULL) {
3261 return NGX_CONF_ERROR;
3262 }
3263
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003264
3265 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
3266 if (ctx == NULL) {
3267 return NGX_CONF_ERROR;
3268 }
3269
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003270 http_ctx = cf->ctx;
3271 ctx->main_conf = http_ctx->main_conf;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003272
3273 /* the upstream{}'s srv_conf */
3274
3275 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
3276 if (ctx->srv_conf == NULL) {
3277 return NGX_CONF_ERROR;
3278 }
3279
3280 ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf;
3281
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003282 uscf->srv_conf = ctx->srv_conf;
3283
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003284
3285 /* the upstream{}'s loc_conf */
3286
3287 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
3288 if (ctx->loc_conf == NULL) {
3289 return NGX_CONF_ERROR;
3290 }
3291
3292 for (m = 0; ngx_modules[m]; m++) {
3293 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
3294 continue;
3295 }
3296
3297 module = ngx_modules[m]->ctx;
3298
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003299 if (module->create_srv_conf) {
3300 mconf = module->create_srv_conf(cf);
3301 if (mconf == NULL) {
3302 return NGX_CONF_ERROR;
3303 }
3304
3305 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
3306 }
3307
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003308 if (module->create_loc_conf) {
3309 mconf = module->create_loc_conf(cf);
3310 if (mconf == NULL) {
3311 return NGX_CONF_ERROR;
3312 }
3313
3314 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf;
3315 }
3316 }
3317
3318
3319 /* parse inside upstream{} */
3320
3321 pcf = *cf;
3322 cf->ctx = ctx;
3323 cf->cmd_type = NGX_HTTP_UPS_CONF;
3324
3325 rv = ngx_conf_parse(cf, NULL);
3326
3327 *cf = pcf;
3328
3329 if (rv != NGX_CONF_OK) {
3330 return rv;
3331 }
3332
3333 if (uscf->servers == NULL) {
3334 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3335 "no servers are inside upstream");
3336 return NGX_CONF_ERROR;
3337 }
3338
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003339 return rv;
3340}
3341
3342
3343static char *
3344ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3345{
3346 ngx_http_upstream_srv_conf_t *uscf = conf;
3347
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003348 time_t fail_timeout;
3349 ngx_str_t *value, s;
3350 ngx_url_t u;
3351 ngx_int_t weight, max_fails;
3352 ngx_uint_t i;
3353 ngx_http_upstream_server_t *us;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003354
3355 if (uscf->servers == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003356 uscf->servers = ngx_array_create(cf->pool, 4,
3357 sizeof(ngx_http_upstream_server_t));
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003358 if (uscf->servers == NULL) {
3359 return NGX_CONF_ERROR;
3360 }
3361 }
3362
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003363 us = ngx_array_push(uscf->servers);
3364 if (us == NULL) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003365 return NGX_CONF_ERROR;
3366 }
3367
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003368 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
3369
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003370 value = cf->args->elts;
3371
3372 ngx_memzero(&u, sizeof(ngx_url_t));
3373
3374 u.url = value[1];
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00003375 u.default_port = 80;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003376
Igor Sysoev7ed63ee2007-10-08 08:55:12 +00003377 if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003378 if (u.err) {
3379 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3380 "%s in upstream \"%V\"", u.err, &u.url);
3381 }
3382
3383 return NGX_CONF_ERROR;
3384 }
3385
Igor Sysoev1765f472006-07-07 16:33:19 +00003386 weight = 1;
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003387 max_fails = 1;
3388 fail_timeout = 10;
Igor Sysoev1765f472006-07-07 16:33:19 +00003389
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003390 for (i = 2; i < cf->args->nelts; i++) {
Igor Sysoev1765f472006-07-07 16:33:19 +00003391
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003392 if (ngx_strncmp(value[i].data, "weight=", 7) == 0) {
Igor Sysoev1765f472006-07-07 16:33:19 +00003393
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003394 if (!(uscf->flags & NGX_HTTP_UPSTREAM_WEIGHT)) {
3395 goto invalid;
3396 }
Igor Sysoev1765f472006-07-07 16:33:19 +00003397
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003398 weight = ngx_atoi(&value[i].data[7], value[i].len - 7);
Igor Sysoev1765f472006-07-07 16:33:19 +00003399
3400 if (weight == NGX_ERROR || weight == 0) {
3401 goto invalid;
3402 }
3403
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003404 continue;
Igor Sysoev1765f472006-07-07 16:33:19 +00003405 }
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003406
3407 if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) {
3408
3409 if (!(uscf->flags & NGX_HTTP_UPSTREAM_MAX_FAILS)) {
3410 goto invalid;
3411 }
3412
3413 max_fails = ngx_atoi(&value[i].data[10], value[i].len - 10);
3414
3415 if (max_fails == NGX_ERROR) {
3416 goto invalid;
3417 }
3418
3419 continue;
3420 }
3421
3422 if (ngx_strncmp(value[i].data, "fail_timeout=", 13) == 0) {
3423
3424 if (!(uscf->flags & NGX_HTTP_UPSTREAM_FAIL_TIMEOUT)) {
3425 goto invalid;
3426 }
3427
3428 s.len = value[i].len - 13;
3429 s.data = &value[i].data[13];
3430
3431 fail_timeout = ngx_parse_time(&s, 1);
3432
Igor Sysoev6a2ea3f2008-04-17 14:32:11 +00003433 if (fail_timeout == NGX_ERROR) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003434 goto invalid;
3435 }
3436
3437 continue;
3438 }
3439
Igor Sysoev6876bcd2007-08-09 13:54:33 +00003440 if (ngx_strncmp(value[i].data, "backup", 6) == 0) {
3441
3442 if (!(uscf->flags & NGX_HTTP_UPSTREAM_BACKUP)) {
3443 goto invalid;
3444 }
3445
3446 us->backup = 1;
3447
3448 continue;
3449 }
3450
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003451 if (ngx_strncmp(value[i].data, "down", 4) == 0) {
3452
3453 if (!(uscf->flags & NGX_HTTP_UPSTREAM_DOWN)) {
3454 goto invalid;
3455 }
3456
3457 us->down = 1;
3458
3459 continue;
3460 }
3461
3462 goto invalid;
Igor Sysoev1765f472006-07-07 16:33:19 +00003463 }
3464
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003465 us->addrs = u.addrs;
3466 us->naddrs = u.naddrs;
3467 us->weight = weight;
3468 us->max_fails = max_fails;
3469 us->fail_timeout = fail_timeout;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003470
3471 return NGX_CONF_OK;
Igor Sysoev1765f472006-07-07 16:33:19 +00003472
3473invalid:
3474
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003475 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3476 "invalid parameter \"%V\"", &value[i]);
Igor Sysoev1765f472006-07-07 16:33:19 +00003477
3478 return NGX_CONF_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003479}
3480
3481
3482ngx_http_upstream_srv_conf_t *
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003483ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003484{
3485 ngx_uint_t i;
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003486 ngx_http_upstream_server_t *us;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003487 ngx_http_upstream_srv_conf_t *uscf, **uscfp;
3488 ngx_http_upstream_main_conf_t *umcf;
3489
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003490 if (!(flags & NGX_HTTP_UPSTREAM_CREATE)) {
3491
Igor Sysoev7ed63ee2007-10-08 08:55:12 +00003492 if (ngx_parse_url(cf->pool, u) != NGX_OK) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003493 if (u->err) {
3494 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3495 "%s in upstream \"%V\"", u->err, &u->url);
3496 }
3497
3498 return NULL;
3499 }
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003500 }
3501
3502 umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module);
3503
3504 uscfp = umcf->upstreams.elts;
3505
3506 for (i = 0; i < umcf->upstreams.nelts; i++) {
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00003507
3508 if (uscfp[i]->host.len != u->host.len
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003509 || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)
3510 != 0)
3511 {
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003512 continue;
3513 }
3514
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003515 if ((flags & NGX_HTTP_UPSTREAM_CREATE)
3516 && (uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE))
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003517 {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003518 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3519 "duplicate upstream \"%V\"", &u->host);
3520 return NULL;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003521 }
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003522
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00003523 if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003524 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00003525 "upstream \"%V\" may not have port %d",
3526 &u->host, u->port);
3527 return NULL;
3528 }
3529
3530 if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) {
3531 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
3532 "upstream \"%V\" may not have port %d in %s:%ui",
3533 &u->host, uscfp[i]->port,
Igor Sysoev4b96baa2007-09-15 17:11:06 +00003534 uscfp[i]->file_name, uscfp[i]->line);
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00003535 return NULL;
3536 }
3537
3538 if (uscfp[i]->port != u->port) {
3539 continue;
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003540 }
3541
Igor Sysoev32b78752007-07-23 12:35:21 +00003542 if (uscfp[i]->default_port && u->default_port
3543 && uscfp[i]->default_port != u->default_port)
3544 {
3545 continue;
3546 }
3547
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003548 return uscfp[i];
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003549 }
3550
3551 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t));
3552 if (uscf == NULL) {
3553 return NULL;
3554 }
3555
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003556 uscf->flags = flags;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003557 uscf->host = u->host;
Igor Sysoev4b96baa2007-09-15 17:11:06 +00003558 uscf->file_name = cf->conf_file->file.name.data;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003559 uscf->line = cf->conf_file->line;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00003560 uscf->port = u->port;
3561 uscf->default_port = u->default_port;
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003562
3563 if (u->naddrs == 1) {
3564 uscf->servers = ngx_array_create(cf->pool, 1,
3565 sizeof(ngx_http_upstream_server_t));
3566 if (uscf->servers == NULL) {
3567 return NGX_CONF_ERROR;
3568 }
3569
3570 us = ngx_array_push(uscf->servers);
3571 if (us == NULL) {
3572 return NGX_CONF_ERROR;
3573 }
3574
3575 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
3576
3577 us->addrs = u->addrs;
3578 us->naddrs = u->naddrs;
3579 }
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003580
3581 uscfp = ngx_array_push(&umcf->upstreams);
3582 if (uscfp == NULL) {
3583 return NULL;
3584 }
3585
3586 *uscfp = uscf;
3587
3588 return uscf;
3589}
3590
3591
Igor Sysoevcb540612007-12-09 18:03:20 +00003592ngx_int_t
3593ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
3594 ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
3595 ngx_str_t *default_hide_headers, ngx_hash_init_t *hash)
3596{
3597 ngx_str_t *h;
3598 ngx_uint_t i, j;
3599 ngx_array_t hide_headers;
3600 ngx_hash_key_t *hk;
3601
3602 if (conf->hide_headers == NGX_CONF_UNSET_PTR
3603 && conf->pass_headers == NGX_CONF_UNSET_PTR)
3604 {
3605 conf->hide_headers_hash = prev->hide_headers_hash;
3606
3607 if (conf->hide_headers_hash.buckets) {
3608 return NGX_OK;
3609 }
3610
3611 conf->hide_headers = prev->hide_headers;
3612 conf->pass_headers = prev->pass_headers;
3613
3614 } else {
3615 if (conf->hide_headers == NGX_CONF_UNSET_PTR) {
3616 conf->hide_headers = prev->hide_headers;
3617 }
3618
3619 if (conf->pass_headers == NGX_CONF_UNSET_PTR) {
3620 conf->pass_headers = prev->pass_headers;
3621 }
3622 }
3623
3624 if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
3625 != NGX_OK)
3626 {
3627 return NGX_ERROR;
3628 }
3629
3630 for (h = default_hide_headers; h->len; h++) {
3631 hk = ngx_array_push(&hide_headers);
3632 if (hk == NULL) {
3633 return NGX_ERROR;
3634 }
3635
3636 hk->key = *h;
3637 hk->key_hash = ngx_hash_key_lc(h->data, h->len);
3638 hk->value = (void *) 1;
3639 }
3640
3641 if (conf->hide_headers != NGX_CONF_UNSET_PTR) {
3642
3643 h = conf->hide_headers->elts;
3644
3645 for (i = 0; i < conf->hide_headers->nelts; i++) {
3646
3647 hk = hide_headers.elts;
3648
3649 for (j = 0; j < hide_headers.nelts; j++) {
3650 if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {
3651 goto exist;
3652 }
3653 }
3654
3655 hk = ngx_array_push(&hide_headers);
3656 if (hk == NULL) {
3657 return NGX_ERROR;
3658 }
3659
3660 hk->key = h[i];
3661 hk->key_hash = ngx_hash_key_lc(h[i].data, h[i].len);
3662 hk->value = (void *) 1;
3663
3664 exist:
3665
3666 continue;
3667 }
3668 }
3669
3670 if (conf->pass_headers != NGX_CONF_UNSET_PTR) {
3671
3672 h = conf->pass_headers->elts;
3673 hk = hide_headers.elts;
3674
3675 for (i = 0; i < conf->pass_headers->nelts; i++) {
3676 for (j = 0; j < hide_headers.nelts; j++) {
3677
3678 if (hk[j].key.data == NULL) {
3679 continue;
3680 }
3681
3682 if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {
3683 hk[j].key.data = NULL;
3684 break;
3685 }
3686 }
3687 }
3688 }
3689
3690 hash->hash = &conf->hide_headers_hash;
Igor Sysoev3fc85af2007-12-10 12:30:09 +00003691 hash->key = ngx_hash_key_lc;
Igor Sysoevcb540612007-12-09 18:03:20 +00003692 hash->pool = cf->pool;
3693 hash->temp_pool = NULL;
Igor Sysoev3fc85af2007-12-10 12:30:09 +00003694
Igor Sysoevcb540612007-12-09 18:03:20 +00003695 return ngx_hash_init(hash, hide_headers.elts, hide_headers.nelts);
3696}
3697
3698
Igor Sysoev899b44e2005-05-12 14:58:06 +00003699static void *
3700ngx_http_upstream_create_main_conf(ngx_conf_t *cf)
3701{
3702 ngx_http_upstream_main_conf_t *umcf;
3703
3704 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t));
3705 if (umcf == NULL) {
3706 return NULL;
3707 }
3708
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003709 if (ngx_array_init(&umcf->upstreams, cf->pool, 4,
3710 sizeof(ngx_http_upstream_srv_conf_t *))
3711 != NGX_OK)
3712 {
3713 return NGX_CONF_ERROR;
3714 }
3715
Igor Sysoev899b44e2005-05-12 14:58:06 +00003716 return umcf;
3717}
3718
3719
3720static char *
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003721ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf)
Igor Sysoev899b44e2005-05-12 14:58:06 +00003722{
3723 ngx_http_upstream_main_conf_t *umcf = conf;
3724
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003725 ngx_uint_t i;
3726 ngx_array_t headers_in;
3727 ngx_hash_key_t *hk;
3728 ngx_hash_init_t hash;
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003729 ngx_http_upstream_init_pt init;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003730 ngx_http_upstream_header_t *header;
3731 ngx_http_upstream_srv_conf_t **uscfp;
3732
3733 uscfp = umcf->upstreams.elts;
3734
3735 for (i = 0; i < umcf->upstreams.nelts; i++) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003736
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003737 init = uscfp[i]->peer.init_upstream ? uscfp[i]->peer.init_upstream:
3738 ngx_http_upstream_init_round_robin;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003739
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003740 if (init(cf, uscfp[i]) != NGX_OK) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +00003741 return NGX_CONF_ERROR;
3742 }
3743 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00003744
Igor Sysoev1765f472006-07-07 16:33:19 +00003745
Igor Sysoev3d2fd182006-12-04 16:46:13 +00003746 /* upstream_headers_in_hash */
3747
Igor Sysoev3338cfd2006-05-11 14:43:47 +00003748 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
3749 != NGX_OK)
Igor Sysoev899b44e2005-05-12 14:58:06 +00003750 {
3751 return NGX_CONF_ERROR;
3752 }
3753
Igor Sysoev3338cfd2006-05-11 14:43:47 +00003754 for (header = ngx_http_upstream_headers_in; header->name.len; header++) {
3755 hk = ngx_array_push(&headers_in);
3756 if (hk == NULL) {
3757 return NGX_CONF_ERROR;
3758 }
3759
3760 hk->key = header->name;
3761 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
3762 hk->value = header;
3763 }
3764
3765 hash.hash = &umcf->headers_in_hash;
3766 hash.key = ngx_hash_key_lc;
3767 hash.max_size = 512;
Igor Sysoevaec57e22006-10-09 14:03:16 +00003768 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
Igor Sysoev3338cfd2006-05-11 14:43:47 +00003769 hash.name = "upstream_headers_in_hash";
3770 hash.pool = cf->pool;
3771 hash.temp_pool = NULL;
3772
3773 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
3774 return NGX_CONF_ERROR;
3775 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00003776
3777 return NGX_CONF_OK;
3778}