xref: /lighttpd1.4/src/mod_proxy.c (revision d147673d)
1 #include "first.h"
2 
3 #include "buffer.h"
4 #include "server.h"
5 #include "keyvalue.h"
6 #include "log.h"
7 
8 #include "http_chunk.h"
9 #include "fdevent.h"
10 #include "connections.h"
11 #include "response.h"
12 #include "joblist.h"
13 
14 #include "plugin.h"
15 
16 #include "inet_ntop_cache.h"
17 #include "crc32.h"
18 
19 #include <sys/types.h>
20 
21 #include <unistd.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <assert.h>
28 
29 #include <stdio.h>
30 
31 #include "sys-socket.h"
32 
33 #define data_proxy data_fastcgi
34 #define data_proxy_init data_fastcgi_init
35 
36 #define PROXY_RETRY_TIMEOUT 60
37 
38 /**
39  *
40  * the proxy module is based on the fastcgi module
41  *
42  * 28.06.2004 Jan Kneschke     The first release
43  * 01.07.2004 Evgeny Rodichev  Several bugfixes and cleanups
44  *            - co-ordinate up- and downstream flows correctly (proxy_demux_response
45  *              and proxy_handle_fdevent)
46  *            - correctly transfer upstream http_response_status;
47  *            - some unused structures removed.
48  *
49  * TODO:      - delay upstream read if write_queue is too large
50  *              (to prevent memory eating, like in apache). Shoud be
51  *              configurable).
52  *            - persistent connection with upstream servers
53  *            - HTTP/1.1
54  */
55 typedef enum {
56 	PROXY_BALANCE_UNSET,
57 	PROXY_BALANCE_FAIR,
58 	PROXY_BALANCE_HASH,
59 	PROXY_BALANCE_RR
60 } proxy_balance_t;
61 
62 typedef struct {
63 	array *extensions;
64 	unsigned short debug;
65 
66 	proxy_balance_t balance;
67 } plugin_config;
68 
69 typedef struct {
70 	PLUGIN_DATA;
71 
72 	buffer *parse_response;
73 	buffer *balance_buf;
74 
75 	plugin_config **config_storage;
76 
77 	plugin_config conf;
78 } plugin_data;
79 
80 typedef enum {
81 	PROXY_STATE_INIT,
82 	PROXY_STATE_CONNECT,
83 	PROXY_STATE_PREPARE_WRITE,
84 	PROXY_STATE_WRITE,
85 	PROXY_STATE_READ
86 } proxy_connection_state_t;
87 
88 enum { PROXY_STDOUT, PROXY_END_REQUEST };
89 
90 typedef struct {
91 	proxy_connection_state_t state;
92 	time_t state_timestamp;
93 
94 	data_proxy *host;
95 
96 	buffer *response;
97 	buffer *response_header;
98 
99 	chunkqueue *wb;
100 	off_t wb_reqlen;
101 
102 	int fd; /* fd to the proxy process */
103 	int fde_ndx; /* index into the fd-event buffer */
104 
105 	size_t path_info_offset; /* start of path_info in uri.path */
106 
107 	connection *remote_conn;  /* dump pointer */
108 	plugin_data *plugin_data; /* dump pointer */
109 } handler_ctx;
110 
111 
112 /* ok, we need a prototype */
113 static handler_t proxy_handle_fdevent(server *srv, void *ctx, int revents);
114 
115 static handler_ctx * handler_ctx_init(void) {
116 	handler_ctx * hctx;
117 
118 
119 	hctx = calloc(1, sizeof(*hctx));
120 
121 	hctx->state = PROXY_STATE_INIT;
122 	hctx->host = NULL;
123 
124 	hctx->response = buffer_init();
125 	hctx->response_header = buffer_init();
126 
127 	hctx->wb = chunkqueue_init();
128 	hctx->wb_reqlen = 0;
129 
130 	hctx->fd = -1;
131 	hctx->fde_ndx = -1;
132 
133 	return hctx;
134 }
135 
136 static void handler_ctx_free(handler_ctx *hctx) {
137 	buffer_free(hctx->response);
138 	buffer_free(hctx->response_header);
139 	chunkqueue_free(hctx->wb);
140 
141 	free(hctx);
142 }
143 
144 INIT_FUNC(mod_proxy_init) {
145 	plugin_data *p;
146 
147 	p = calloc(1, sizeof(*p));
148 
149 	p->parse_response = buffer_init();
150 	p->balance_buf = buffer_init();
151 
152 	return p;
153 }
154 
155 
156 FREE_FUNC(mod_proxy_free) {
157 	plugin_data *p = p_d;
158 
159 	UNUSED(srv);
160 
161 	buffer_free(p->parse_response);
162 	buffer_free(p->balance_buf);
163 
164 	if (p->config_storage) {
165 		size_t i;
166 		for (i = 0; i < srv->config_context->used; i++) {
167 			plugin_config *s = p->config_storage[i];
168 
169 			if (NULL == s) continue;
170 
171 			array_free(s->extensions);
172 
173 			free(s);
174 		}
175 		free(p->config_storage);
176 	}
177 
178 	free(p);
179 
180 	return HANDLER_GO_ON;
181 }
182 
183 SETDEFAULTS_FUNC(mod_proxy_set_defaults) {
184 	plugin_data *p = p_d;
185 	data_unset *du;
186 	size_t i = 0;
187 
188 	config_values_t cv[] = {
189 		{ "proxy.server",              NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION },       /* 0 */
190 		{ "proxy.debug",               NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },       /* 1 */
191 		{ "proxy.balance",             NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },      /* 2 */
192 		{ NULL,                        NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
193 	};
194 
195 	p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
196 
197 	for (i = 0; i < srv->config_context->used; i++) {
198 		data_config const* config = (data_config const*)srv->config_context->data[i];
199 		plugin_config *s;
200 
201 		s = malloc(sizeof(plugin_config));
202 		s->extensions    = array_init();
203 		s->debug         = 0;
204 
205 		cv[0].destination = s->extensions;
206 		cv[1].destination = &(s->debug);
207 		cv[2].destination = p->balance_buf;
208 
209 		buffer_reset(p->balance_buf);
210 
211 		p->config_storage[i] = s;
212 
213 		if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
214 			return HANDLER_ERROR;
215 		}
216 
217 		if (buffer_string_is_empty(p->balance_buf)) {
218 			s->balance = PROXY_BALANCE_FAIR;
219 		} else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("fair"))) {
220 			s->balance = PROXY_BALANCE_FAIR;
221 		} else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("round-robin"))) {
222 			s->balance = PROXY_BALANCE_RR;
223 		} else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("hash"))) {
224 			s->balance = PROXY_BALANCE_HASH;
225 		} else {
226 			log_error_write(srv, __FILE__, __LINE__, "sb",
227 				        "proxy.balance has to be one of: fair, round-robin, hash, but not:", p->balance_buf);
228 			return HANDLER_ERROR;
229 		}
230 
231 		if (NULL != (du = array_get_element(config->value, "proxy.server"))) {
232 			size_t j;
233 			data_array *da = (data_array *)du;
234 
235 			if (du->type != TYPE_ARRAY) {
236 				log_error_write(srv, __FILE__, __LINE__, "sss",
237 						"unexpected type for key: ", "proxy.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
238 
239 				return HANDLER_ERROR;
240 			}
241 
242 			/*
243 			 * proxy.server = ( "<ext>" => ...,
244 			 *                  "<ext>" => ... )
245 			 */
246 
247 			for (j = 0; j < da->value->used; j++) {
248 				data_array *da_ext = (data_array *)da->value->data[j];
249 				size_t n;
250 
251 				if (da_ext->type != TYPE_ARRAY) {
252 					log_error_write(srv, __FILE__, __LINE__, "sssbs",
253 							"unexpected type for key: ", "proxy.server",
254 							"[", da->value->data[j]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
255 
256 					return HANDLER_ERROR;
257 				}
258 
259 				/*
260 				 * proxy.server = ( "<ext>" =>
261 				 *                     ( "<host>" => ( ... ),
262 				 *                       "<host>" => ( ... )
263 				 *                     ),
264 				 *                    "<ext>" => ... )
265 				 */
266 
267 				for (n = 0; n < da_ext->value->used; n++) {
268 					data_array *da_host = (data_array *)da_ext->value->data[n];
269 
270 					data_proxy *df;
271 					data_array *dfa;
272 
273 					config_values_t pcv[] = {
274 						{ "host",              NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },      /* 0 */
275 						{ "port",              NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },       /* 1 */
276 						{ NULL,                NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
277 					};
278 
279 					if (da_host->type != TYPE_ARRAY) {
280 						log_error_write(srv, __FILE__, __LINE__, "ssSBS",
281 								"unexpected type for key:",
282 								"proxy.server",
283 								"[", da_ext->value->data[n]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
284 
285 						return HANDLER_ERROR;
286 					}
287 
288 					df = data_proxy_init();
289 
290 					df->port = 80;
291 
292 					buffer_copy_buffer(df->key, da_host->key);
293 
294 					pcv[0].destination = df->host;
295 					pcv[1].destination = &(df->port);
296 
297 					if (0 != config_insert_values_internal(srv, da_host->value, pcv, T_CONFIG_SCOPE_CONNECTION)) {
298 						df->free((data_unset*) df);
299 						return HANDLER_ERROR;
300 					}
301 
302 					if (buffer_string_is_empty(df->host)) {
303 						log_error_write(srv, __FILE__, __LINE__, "sbbbs",
304 								"missing key (string):",
305 								da->key,
306 								da_ext->key,
307 								da_host->key,
308 								"host");
309 
310 						df->free((data_unset*) df);
311 						return HANDLER_ERROR;
312 					}
313 
314 					/* if extension already exists, take it */
315 
316 					if (NULL == (dfa = (data_array *)array_get_element(s->extensions, da_ext->key->ptr))) {
317 						dfa = data_array_init();
318 
319 						buffer_copy_buffer(dfa->key, da_ext->key);
320 
321 						array_insert_unique(dfa->value, (data_unset *)df);
322 						array_insert_unique(s->extensions, (data_unset *)dfa);
323 					} else {
324 						array_insert_unique(dfa->value, (data_unset *)df);
325 					}
326 				}
327 			}
328 		}
329 	}
330 
331 	return HANDLER_GO_ON;
332 }
333 
334 static void proxy_connection_close(server *srv, handler_ctx *hctx) {
335 	plugin_data *p;
336 	connection *con;
337 
338 	p    = hctx->plugin_data;
339 	con  = hctx->remote_conn;
340 
341 	if (hctx->fd != -1) {
342 		fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
343 		fdevent_unregister(srv->ev, hctx->fd);
344 
345 		close(hctx->fd);
346 		srv->cur_fds--;
347 	}
348 
349 	if (hctx->host) {
350 		hctx->host->usage--;
351 	}
352 
353 	handler_ctx_free(hctx);
354 	con->plugin_ctx[p->id] = NULL;
355 
356 	/* finish response (if not already con->file_started, con->file_finished) */
357 	if (con->mode == p->id) {
358 		http_response_backend_done(srv, con);
359 	}
360 }
361 
362 static int proxy_establish_connection(server *srv, handler_ctx *hctx) {
363 	struct sockaddr *proxy_addr;
364 	struct sockaddr_in proxy_addr_in;
365 #if defined(HAVE_SYS_UN_H)
366 	struct sockaddr_un proxy_addr_un;
367 #endif
368 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
369 	struct sockaddr_in6 proxy_addr_in6;
370 #endif
371 	socklen_t servlen;
372 
373 	plugin_data *p    = hctx->plugin_data;
374 	data_proxy *host= hctx->host;
375 	int proxy_fd       = hctx->fd;
376 
377 
378 #if defined(HAVE_SYS_UN_H)
379 	if (strstr(host->host->ptr, "/")) {
380 		if (buffer_string_length(host->host) + 1 > sizeof(proxy_addr_un.sun_path)) {
381 			log_error_write(srv, __FILE__, __LINE__, "sB",
382 				"ERROR: Unix Domain socket filename too long:",
383 				host->host);
384 			return -1;
385 		}
386 
387 		memset(&proxy_addr_un, 0, sizeof(proxy_addr_un));
388 		proxy_addr_un.sun_family = AF_UNIX;
389 		memcpy(proxy_addr_un.sun_path, host->host->ptr, buffer_string_length(host->host) + 1);
390 		servlen = sizeof(proxy_addr_un);
391 		proxy_addr = (struct sockaddr *) &proxy_addr_un;
392 	} else
393 #endif
394 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
395 	if (strstr(host->host->ptr, ":")) {
396 		memset(&proxy_addr_in6, 0, sizeof(proxy_addr_in6));
397 		proxy_addr_in6.sin6_family = AF_INET6;
398 		inet_pton(AF_INET6, host->host->ptr, (char *) &proxy_addr_in6.sin6_addr);
399 		proxy_addr_in6.sin6_port = htons(host->port);
400 		servlen = sizeof(proxy_addr_in6);
401 		proxy_addr = (struct sockaddr *) &proxy_addr_in6;
402 	} else
403 #endif
404 	{
405 		memset(&proxy_addr_in, 0, sizeof(proxy_addr_in));
406 		proxy_addr_in.sin_family = AF_INET;
407 		proxy_addr_in.sin_addr.s_addr = inet_addr(host->host->ptr);
408 		proxy_addr_in.sin_port = htons(host->port);
409 		servlen = sizeof(proxy_addr_in);
410 		proxy_addr = (struct sockaddr *) &proxy_addr_in;
411 	}
412 
413 
414 	if (-1 == connect(proxy_fd, proxy_addr, servlen)) {
415 		if (errno == EINPROGRESS || errno == EALREADY) {
416 			if (p->conf.debug) {
417 				log_error_write(srv, __FILE__, __LINE__, "sd",
418 						"connect delayed:", proxy_fd);
419 			}
420 
421 			return 1;
422 		} else {
423 
424 			log_error_write(srv, __FILE__, __LINE__, "sdsd",
425 					"connect failed:", proxy_fd, strerror(errno), errno);
426 
427 			return -1;
428 		}
429 	}
430 	if (p->conf.debug) {
431 		log_error_write(srv, __FILE__, __LINE__, "sd",
432 				"connect succeeded: ", proxy_fd);
433 	}
434 
435 	return 0;
436 }
437 
438 static void proxy_set_header(connection *con, const char *key, const char *value) {
439 	data_string *ds_dst;
440 
441 	if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
442 		ds_dst = data_string_init();
443 	}
444 
445 	buffer_copy_string(ds_dst->key, key);
446 	buffer_copy_string(ds_dst->value, value);
447 	array_insert_unique(con->request.headers, (data_unset *)ds_dst);
448 }
449 
450 static void proxy_append_header(connection *con, const char *key, const char *value) {
451 	data_string *ds_dst;
452 
453 	if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
454 		ds_dst = data_string_init();
455 	}
456 
457 	buffer_copy_string(ds_dst->key, key);
458 	buffer_append_string(ds_dst->value, value);
459 	array_insert_unique(con->request.headers, (data_unset *)ds_dst);
460 }
461 
462 
463 static int proxy_create_env(server *srv, handler_ctx *hctx) {
464 	size_t i;
465 
466 	connection *con   = hctx->remote_conn;
467 	buffer *b;
468 
469 	/* build header */
470 
471 	b = buffer_init();
472 
473 	/* request line */
474 	buffer_copy_string(b, get_http_method_name(con->request.http_method));
475 	buffer_append_string_len(b, CONST_STR_LEN(" "));
476 
477 	buffer_append_string_buffer(b, con->request.uri);
478 	buffer_append_string_len(b, CONST_STR_LEN(" HTTP/1.0\r\n"));
479 
480 	proxy_append_header(con, "X-Forwarded-For", (char *)inet_ntop_cache_get_ip(srv, &(con->dst_addr)));
481 	/* http_host is NOT is just a pointer to a buffer
482 	 * which is NULL if it is not set */
483 	if (!buffer_string_is_empty(con->request.http_host)) {
484 		proxy_set_header(con, "X-Host", con->request.http_host->ptr);
485 	}
486 	proxy_set_header(con, "X-Forwarded-Proto", con->uri.scheme->ptr);
487 
488 	/* request header */
489 	for (i = 0; i < con->request.headers->used; i++) {
490 		data_string *ds;
491 
492 		ds = (data_string *)con->request.headers->data[i];
493 
494 		if (!buffer_is_empty(ds->value) && !buffer_is_empty(ds->key)) {
495 			if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Connection"))) continue;
496 			if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Proxy-Connection"))) continue;
497 
498 			buffer_append_string_buffer(b, ds->key);
499 			buffer_append_string_len(b, CONST_STR_LEN(": "));
500 			buffer_append_string_buffer(b, ds->value);
501 			buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
502 		}
503 	}
504 
505 	buffer_append_string_len(b, CONST_STR_LEN("Connection: close\r\n\r\n"));
506 
507 	hctx->wb_reqlen = buffer_string_length(b);
508 	chunkqueue_append_buffer(hctx->wb, b);
509 	buffer_free(b);
510 
511 	/* body */
512 
513 	if (con->request.content_length) {
514 		chunkqueue_append_chunkqueue(hctx->wb, con->request_content_queue);
515 		hctx->wb_reqlen += con->request.content_length;/* (eventual) total request size */
516 	}
517 
518 	return 0;
519 }
520 
521 static int proxy_set_state(server *srv, handler_ctx *hctx, proxy_connection_state_t state) {
522 	hctx->state = state;
523 	hctx->state_timestamp = srv->cur_ts;
524 
525 	return 0;
526 }
527 
528 
529 static int proxy_response_parse(server *srv, connection *con, plugin_data *p, buffer *in) {
530 	char *s, *ns;
531 	int http_response_status = -1;
532 
533 	UNUSED(srv);
534 
535 	/* [\r]\n -> [\0]\0 */
536 
537 	buffer_copy_buffer(p->parse_response, in);
538 
539 	for (s = p->parse_response->ptr; NULL != (ns = strchr(s, '\n')); s = ns + 1) {
540 		char *key, *value;
541 		int key_len;
542 		data_string *ds;
543 		int copy_header;
544 
545 		ns[0] = '\0';
546 		if (s != ns && ns[-1] == '\r') ns[-1] = '\0';
547 
548 		if (-1 == http_response_status) {
549 			/* The first line of a Response message is the Status-Line */
550 
551 			for (key=s; *key && *key != ' '; key++);
552 
553 			if (*key) {
554 				http_response_status = (int) strtol(key, NULL, 10);
555 				if (http_response_status < 100 || http_response_status >= 1000) http_response_status = 502;
556 			} else {
557 				http_response_status = 502;
558 			}
559 
560 			con->http_status = http_response_status;
561 			con->parsed_response |= HTTP_STATUS;
562 			continue;
563 		}
564 
565 		if (NULL == (value = strchr(s, ':'))) {
566 			/* now we expect: "<key>: <value>\n" */
567 
568 			continue;
569 		}
570 
571 		key = s;
572 		key_len = value - key;
573 
574 		value++;
575 		/* strip WS */
576 		while (*value == ' ' || *value == '\t') value++;
577 
578 		copy_header = 1;
579 
580 		switch(key_len) {
581 		case 4:
582 			if (0 == strncasecmp(key, "Date", key_len)) {
583 				con->parsed_response |= HTTP_DATE;
584 			}
585 			break;
586 		case 8:
587 			if (0 == strncasecmp(key, "Location", key_len)) {
588 				con->parsed_response |= HTTP_LOCATION;
589 			}
590 			break;
591 		case 10:
592 			if (0 == strncasecmp(key, "Connection", key_len)) {
593 				copy_header = 0;
594 			}
595 			break;
596 		case 14:
597 			if (0 == strncasecmp(key, "Content-Length", key_len)) {
598 				con->response.content_length = strtoul(value, NULL, 10);
599 				con->parsed_response |= HTTP_CONTENT_LENGTH;
600 			}
601 			break;
602 		default:
603 			break;
604 		}
605 
606 		if (copy_header) {
607 			if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
608 				ds = data_response_init();
609 			}
610 			buffer_copy_string_len(ds->key, key, key_len);
611 			buffer_copy_string(ds->value, value);
612 
613 			array_insert_unique(con->response.headers, (data_unset *)ds);
614 		}
615 	}
616 
617 	return 0;
618 }
619 
620 
621 static int proxy_demux_response(server *srv, handler_ctx *hctx) {
622 	int fin = 0;
623 	int b;
624 	ssize_t r;
625 
626 	plugin_data *p    = hctx->plugin_data;
627 	connection *con   = hctx->remote_conn;
628 	int proxy_fd       = hctx->fd;
629 
630 	/* check how much we have to read */
631       #if !defined(_WIN32) && !defined(__CYGWIN__)
632 	if (ioctl(hctx->fd, FIONREAD, &b)) {
633 		if (errno == EAGAIN) {
634 			fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
635 			return 0;
636 		}
637 		log_error_write(srv, __FILE__, __LINE__, "sd",
638 				"ioctl failed: ",
639 				proxy_fd);
640 		return -1;
641 	}
642       #else
643 	b = 4096;
644       #endif
645 
646 
647 	if (p->conf.debug) {
648 		log_error_write(srv, __FILE__, __LINE__, "sd",
649 				"proxy - have to read:", b);
650 	}
651 
652 	if (b > 0) {
653 		if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)) {
654 			off_t cqlen = chunkqueue_length(con->write_queue);
655 			if (cqlen + b > 65536 - 4096) {
656 				if (!con->is_writable) {
657 					/*(defer removal of FDEVENT_IN interest since
658 					 * connection_state_machine() might be able to send data
659 					 * immediately, unless !con->is_writable, where
660 					 * connection_state_machine() might not loop back to call
661 					 * mod_proxy_handle_subrequest())*/
662 					fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
663 				}
664 				if (cqlen >= 65536-1) return 0;
665 				b = 65536 - 1 - (int)cqlen;
666 			}
667 		}
668 
669 		buffer_string_prepare_append(hctx->response, b);
670 
671 		if (-1 == (r = read(hctx->fd, hctx->response->ptr + buffer_string_length(hctx->response), buffer_string_space(hctx->response)))) {
672 			if (errno == EAGAIN) {
673 				fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
674 				return 0;
675 			}
676 			log_error_write(srv, __FILE__, __LINE__, "sds",
677 					"unexpected end-of-file (perhaps the proxy process died):",
678 					proxy_fd, strerror(errno));
679 			return -1;
680 		}
681 
682 	      #if defined(_WIN32) || defined(__CYGWIN__)
683 		if (0 == r) return 1; /* fin */
684 	      #endif
685 
686 		/* this should be catched by the b > 0 above */
687 		force_assert(r);
688 
689 		buffer_commit(hctx->response, r);
690 
691 #if 0
692 		log_error_write(srv, __FILE__, __LINE__, "sdsbs",
693 				"demux: Response buffer len", hctx->response->used, ":", hctx->response, ":");
694 #endif
695 
696 		if (0 == con->got_response) {
697 			con->got_response = 1;
698 			buffer_string_prepare_copy(hctx->response_header, 1023);
699 		}
700 
701 		if (0 == con->file_started) {
702 			char *c;
703 
704 			/* search for the \r\n\r\n in the string */
705 			if (NULL != (c = buffer_search_string_len(hctx->response, CONST_STR_LEN("\r\n\r\n")))) {
706 				size_t hlen = c - hctx->response->ptr + 4;
707 				size_t blen = buffer_string_length(hctx->response) - hlen;
708 				/* found */
709 
710 				buffer_append_string_len(hctx->response_header, hctx->response->ptr, hlen);
711 #if 0
712 				log_error_write(srv, __FILE__, __LINE__, "sb", "Header:", hctx->response_header);
713 #endif
714 				/* parse the response header */
715 				proxy_response_parse(srv, con, p, hctx->response_header);
716 
717 				con->file_started = 1;
718 				if (blen > 0) {
719 					if (0 != http_chunk_append_mem(srv, con, c + 4, blen)) {
720 						/* error writing to tempfile;
721 						 * truncate response or send 500 if nothing sent yet */
722 						fin = 1;
723 						con->file_started = 0;
724 					}
725 				}
726 				buffer_reset(hctx->response);
727 			} else {
728 				/* no luck, no header found */
729 				/*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
730 				if (buffer_string_length(hctx->response) > MAX_HTTP_REQUEST_HEADER) {
731 					log_error_write(srv, __FILE__, __LINE__, "sb", "response headers too large for", con->uri.path);
732 					con->http_status = 502; /* Bad Gateway */
733 					con->mode = DIRECT;
734 					fin = 1;
735 				}
736 			}
737 		} else {
738 			if (0 != http_chunk_append_buffer(srv, con, hctx->response)) {
739 				/* error writing to tempfile;
740 				 * truncate response or send 500 if nothing sent yet */
741 				fin = 1;
742 			}
743 			buffer_reset(hctx->response);
744 		}
745 	} else {
746 		/* reading from upstream done */
747 		fin = 1;
748 	}
749 
750 	return fin;
751 }
752 
753 
754 static handler_t proxy_write_request(server *srv, handler_ctx *hctx) {
755 	data_proxy *host= hctx->host;
756 	connection *con   = hctx->remote_conn;
757 
758 	int ret;
759 
760 	if (!host || buffer_string_is_empty(host->host) || !host->port) return HANDLER_ERROR;
761 
762 	switch(hctx->state) {
763 	case PROXY_STATE_CONNECT:
764 		/* wait for the connect() to finish */
765 
766 		/* connect failed ? */
767 		if (-1 == hctx->fde_ndx) return HANDLER_ERROR;
768 
769 		/* wait */
770 		return HANDLER_WAIT_FOR_EVENT;
771 
772 	case PROXY_STATE_INIT:
773 #if defined(HAVE_SYS_UN_H)
774 		if (strstr(host->host->ptr,"/")) {
775 			if (-1 == (hctx->fd = socket(AF_UNIX, SOCK_STREAM, 0))) {
776 				log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
777 				return HANDLER_ERROR;
778 			}
779 		} else
780 #endif
781 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
782 		if (strstr(host->host->ptr,":")) {
783 			if (-1 == (hctx->fd = socket(AF_INET6, SOCK_STREAM, 0))) {
784 				log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
785 				return HANDLER_ERROR;
786 			}
787 		} else
788 #endif
789 		{
790 			if (-1 == (hctx->fd = socket(AF_INET, SOCK_STREAM, 0))) {
791 				log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
792 				return HANDLER_ERROR;
793 			}
794 		}
795 		hctx->fde_ndx = -1;
796 
797 		srv->cur_fds++;
798 
799 		fdevent_register(srv->ev, hctx->fd, proxy_handle_fdevent, hctx);
800 
801 		if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
802 			log_error_write(srv, __FILE__, __LINE__, "ss", "fcntl failed: ", strerror(errno));
803 
804 			return HANDLER_ERROR;
805 		}
806 
807 		switch (proxy_establish_connection(srv, hctx)) {
808 		case 1:
809 			proxy_set_state(srv, hctx, PROXY_STATE_CONNECT);
810 
811 			/* connection is in progress, wait for an event and call getsockopt() below */
812 
813 			fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
814 
815 			return HANDLER_WAIT_FOR_EVENT;
816 		case -1:
817 			/* if ECONNREFUSED choose another connection -> FIXME */
818 			hctx->fde_ndx = -1;
819 
820 			return HANDLER_ERROR;
821 		default:
822 			/* everything is ok, go on */
823 			proxy_set_state(srv, hctx, PROXY_STATE_PREPARE_WRITE);
824 			break;
825 		}
826 
827 		/* fall through */
828 
829 	case PROXY_STATE_PREPARE_WRITE:
830 		proxy_create_env(srv, hctx);
831 
832 		fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
833 		proxy_set_state(srv, hctx, PROXY_STATE_WRITE);
834 
835 		/* fall through */
836 	case PROXY_STATE_WRITE:;
837 		ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
838 
839 		chunkqueue_remove_finished_chunks(hctx->wb);
840 
841 		if (-1 == ret) { /* error on our side */
842 			log_error_write(srv, __FILE__, __LINE__, "ssd", "write failed:", strerror(errno), errno);
843 
844 			return HANDLER_ERROR;
845 		} else if (-2 == ret) { /* remote close */
846 			log_error_write(srv, __FILE__, __LINE__, "ssd", "write failed, remote connection close:", strerror(errno), errno);
847 
848 			return HANDLER_ERROR;
849 		}
850 
851 		if (hctx->wb->bytes_out == hctx->wb_reqlen) {
852 			fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
853 			shutdown(hctx->fd, SHUT_WR);/* future: remove if HTTP/1.1 request */
854 			proxy_set_state(srv, hctx, PROXY_STATE_READ);
855 		} else {
856 			off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
857 			if (hctx->wb->bytes_in < hctx->wb_reqlen && wblen < 65536 - 16384) {
858 				/*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
859 				if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
860 					con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
861 					con->is_readable = 1; /* trigger optimistic read from client */
862 				}
863 			}
864 			if (0 == wblen) {
865 				fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
866 			} else {
867 				fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
868 			}
869 		}
870 
871 		return HANDLER_WAIT_FOR_EVENT;
872 	case PROXY_STATE_READ:
873 		/* waiting for a response */
874 		return HANDLER_WAIT_FOR_EVENT;
875 	default:
876 		log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
877 		return HANDLER_ERROR;
878 	}
879 }
880 
881 #define PATCH(x) \
882 	p->conf.x = s->x;
883 static int mod_proxy_patch_connection(server *srv, connection *con, plugin_data *p) {
884 	size_t i, j;
885 	plugin_config *s = p->config_storage[0];
886 
887 	PATCH(extensions);
888 	PATCH(debug);
889 	PATCH(balance);
890 
891 	/* skip the first, the global context */
892 	for (i = 1; i < srv->config_context->used; i++) {
893 		data_config *dc = (data_config *)srv->config_context->data[i];
894 		s = p->config_storage[i];
895 
896 		/* condition didn't match */
897 		if (!config_check_cond(srv, con, dc)) continue;
898 
899 		/* merge config */
900 		for (j = 0; j < dc->value->used; j++) {
901 			data_unset *du = dc->value->data[j];
902 
903 			if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.server"))) {
904 				PATCH(extensions);
905 			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.debug"))) {
906 				PATCH(debug);
907 			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.balance"))) {
908 				PATCH(balance);
909 			}
910 		}
911 	}
912 
913 	return 0;
914 }
915 #undef PATCH
916 
917 static handler_t proxy_send_request(server *srv, handler_ctx *hctx) {
918 	/* ok, create the request */
919 	handler_t rc = proxy_write_request(srv, hctx);
920 	if (HANDLER_ERROR != rc) {
921 		return rc;
922 	} else {
923 		data_proxy *host = hctx->host;
924 		connection *con  = hctx->remote_conn;
925 		plugin_data *p   = hctx->plugin_data;
926 		log_error_write(srv, __FILE__, __LINE__,  "sbdd", "proxy-server disabled:",
927 				host->host,
928 				host->port,
929 				hctx->fd);
930 
931 		/* disable this server */
932 		host->is_disabled = 1;
933 		host->disable_ts = srv->cur_ts;
934 
935 		/* reset the enviroment and restart the sub-request */
936 		con->mode = DIRECT;/*(avoid changing con->state, con->http_status)*/
937 		proxy_connection_close(srv, hctx);
938 		con->mode = p->id;
939 
940 		return HANDLER_COMEBACK;
941 	}
942 }
943 
944 
945 static handler_t proxy_recv_response(server *srv, handler_ctx *hctx);
946 
947 
948 SUBREQUEST_FUNC(mod_proxy_handle_subrequest) {
949 	plugin_data *p = p_d;
950 
951 	handler_ctx *hctx = con->plugin_ctx[p->id];
952 
953 	if (NULL == hctx) return HANDLER_GO_ON;
954 
955 	/* not my job */
956 	if (con->mode != p->id) return HANDLER_GO_ON;
957 
958 	if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
959 	    && con->file_started) {
960 		if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
961 			fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
962 		} else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
963 			/* optimistic read from backend, which might re-enable FDEVENT_IN */
964 			handler_t rc = proxy_recv_response(srv, hctx); /*(might invalidate hctx)*/
965 			if (rc != HANDLER_GO_ON) return rc;            /*(unless HANDLER_GO_ON)*/
966 		}
967 	}
968 
969 	if (0 == hctx->wb->bytes_in
970 	    ? con->state == CON_STATE_READ_POST
971 	    : hctx->wb->bytes_in < hctx->wb_reqlen) {
972 		/*(64k - 4k to attempt to avoid temporary files
973 		 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
974 		if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096
975 		    && (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN)){
976 			con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
977 			if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
978 		} else {
979 			handler_t r = connection_handle_read_post_state(srv, con);
980 			chunkqueue *req_cq = con->request_content_queue;
981 			if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
982 				chunkqueue_append_chunkqueue(hctx->wb, req_cq);
983 				if (fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_OUT) {
984 					return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
985 				}
986 			}
987 			if (r != HANDLER_GO_ON) return r;
988 		}
989 	}
990 
991 	return (0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
992 	  ? proxy_send_request(srv, hctx)
993 	  : HANDLER_WAIT_FOR_EVENT;
994 }
995 
996 
997 static handler_t proxy_recv_response(server *srv, handler_ctx *hctx) {
998 
999 		switch (proxy_demux_response(srv, hctx)) {
1000 		case 0:
1001 			break;
1002 		case -1:
1003 			http_response_backend_error(srv, hctx->remote_conn);
1004 			/* fall through */
1005 		case 1:
1006 			/* we are done */
1007 			proxy_connection_close(srv, hctx);
1008 
1009 			return HANDLER_FINISHED;
1010 		}
1011 
1012 		return HANDLER_GO_ON;
1013 }
1014 
1015 
1016 static handler_t proxy_handle_fdevent(server *srv, void *ctx, int revents) {
1017 	handler_ctx *hctx = ctx;
1018 	connection  *con  = hctx->remote_conn;
1019 	plugin_data *p    = hctx->plugin_data;
1020 
1021 	joblist_append(srv, con);
1022 
1023 	if (revents & FDEVENT_IN) {
1024 
1025 		if (p->conf.debug) {
1026 			log_error_write(srv, __FILE__, __LINE__, "sd",
1027 					"proxy: fdevent-in", hctx->state);
1028 		}
1029 
1030 		{
1031 		handler_t rc = proxy_recv_response(srv,hctx);/*(might invalidate hctx)*/
1032 		if (rc != HANDLER_GO_ON) return rc;          /*(unless HANDLER_GO_ON)*/
1033 		}
1034 	}
1035 
1036 	if (revents & FDEVENT_OUT) {
1037 		if (p->conf.debug) {
1038 			log_error_write(srv, __FILE__, __LINE__, "sd",
1039 					"proxy: fdevent-out", hctx->state);
1040 		}
1041 
1042 		if (hctx->state == PROXY_STATE_CONNECT) {
1043 			int socket_error;
1044 			socklen_t socket_error_len = sizeof(socket_error);
1045 
1046 			/* try to finish the connect() */
1047 			if (0 != getsockopt(hctx->fd, SOL_SOCKET, SO_ERROR, &socket_error, &socket_error_len)) {
1048 				log_error_write(srv, __FILE__, __LINE__, "ss",
1049 					"getsockopt failed:", strerror(errno));
1050 
1051 				return HANDLER_FINISHED;
1052 			}
1053 			if (socket_error != 0) {
1054 				log_error_write(srv, __FILE__, __LINE__, "ss",
1055 					"establishing connection failed:", strerror(socket_error),
1056 					"port:", hctx->host->port);
1057 
1058 				return HANDLER_FINISHED;
1059 			}
1060 			if (p->conf.debug) {
1061 				log_error_write(srv, __FILE__, __LINE__,  "s", "proxy - connect - delayed success");
1062 			}
1063 
1064 			proxy_set_state(srv, hctx, PROXY_STATE_PREPARE_WRITE);
1065 		}
1066 
1067 		return proxy_send_request(srv, hctx); /*(might invalidate hctx)*/
1068 	}
1069 
1070 	/* perhaps this issue is already handled */
1071 	if (revents & FDEVENT_HUP) {
1072 		if (p->conf.debug) {
1073 			log_error_write(srv, __FILE__, __LINE__, "sd",
1074 					"proxy: fdevent-hup", hctx->state);
1075 		}
1076 
1077 		if (hctx->state == PROXY_STATE_CONNECT) {
1078 			/* connect() -> EINPROGRESS -> HUP */
1079 
1080 			/**
1081 			 * what is proxy is doing if it can't reach the next hop ?
1082 			 *
1083 			 */
1084 
1085 			if (hctx->host) {
1086 				hctx->host->is_disabled = 1;
1087 				hctx->host->disable_ts = srv->cur_ts;
1088 				log_error_write(srv, __FILE__, __LINE__,  "sbdd", "proxy-server disabled:",
1089 						hctx->host->host,
1090 						hctx->host->port,
1091 						hctx->fd);
1092 
1093 				/* disable this server */
1094 				hctx->host->is_disabled = 1;
1095 				hctx->host->disable_ts = srv->cur_ts;
1096 
1097 				/* reset the environment and restart the sub-request */
1098 				con->mode = DIRECT;/*(avoid changing con->state, con->http_status)*/
1099 				proxy_connection_close(srv, hctx);
1100 				con->mode = p->id;
1101 			} else {
1102 				proxy_connection_close(srv, hctx);
1103 				con->http_status = 503;
1104 			}
1105 		} else if (con->file_started) {
1106 			/* drain any remaining data from kernel pipe buffers
1107 			 * even if (con->conf.stream_response_body
1108 			 *          & FDEVENT_STREAM_RESPONSE_BUFMIN)
1109 			 * since event loop will spin on fd FDEVENT_HUP event
1110 			 * until unregistered. */
1111 			handler_t rc;
1112 			do {
1113 				rc = proxy_recv_response(srv,hctx);/*(might invalidate hctx)*/
1114 			} while (rc == HANDLER_GO_ON);             /*(unless HANDLER_GO_ON)*/
1115 			return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
1116 		} else {
1117 			proxy_connection_close(srv, hctx);
1118 		}
1119 	} else if (revents & FDEVENT_ERR) {
1120 		log_error_write(srv, __FILE__, __LINE__, "sd", "proxy-FDEVENT_ERR, but no HUP", revents);
1121 
1122 		http_response_backend_error(srv, con);
1123 		proxy_connection_close(srv, hctx);
1124 	}
1125 
1126 	return HANDLER_FINISHED;
1127 }
1128 
1129 static handler_t mod_proxy_check_extension(server *srv, connection *con, void *p_d) {
1130 	plugin_data *p = p_d;
1131 	size_t s_len;
1132 	unsigned long last_max = ULONG_MAX;
1133 	int max_usage = INT_MAX;
1134 	int ndx = -1;
1135 	size_t k;
1136 	buffer *fn;
1137 	data_array *extension = NULL;
1138 	size_t path_info_offset;
1139 
1140 	if (con->mode != DIRECT) return HANDLER_GO_ON;
1141 
1142 	/* Possibly, we processed already this request */
1143 	if (con->file_started == 1) return HANDLER_GO_ON;
1144 
1145 	mod_proxy_patch_connection(srv, con, p);
1146 
1147 	fn = con->uri.path;
1148 	if (buffer_string_is_empty(fn)) return HANDLER_ERROR;
1149 	s_len = buffer_string_length(fn);
1150 
1151 	path_info_offset = 0;
1152 
1153 	if (p->conf.debug) {
1154 		log_error_write(srv, __FILE__, __LINE__,  "s", "proxy - start");
1155 	}
1156 
1157 	/* check if extension matches */
1158 	for (k = 0; k < p->conf.extensions->used; k++) {
1159 		data_array *ext = NULL;
1160 		size_t ct_len;
1161 
1162 		ext = (data_array *)p->conf.extensions->data[k];
1163 
1164 		if (buffer_is_empty(ext->key)) continue;
1165 
1166 		ct_len = buffer_string_length(ext->key);
1167 
1168 		if (s_len < ct_len) continue;
1169 
1170 		/* check extension in the form "/proxy_pattern" */
1171 		if (*(ext->key->ptr) == '/') {
1172 			if (strncmp(fn->ptr, ext->key->ptr, ct_len) == 0) {
1173 				if (s_len > ct_len + 1) {
1174 					char *pi_offset;
1175 
1176 					if (NULL != (pi_offset = strchr(fn->ptr + ct_len + 1, '/'))) {
1177 						path_info_offset = pi_offset - fn->ptr;
1178 					}
1179 				}
1180 				extension = ext;
1181 				break;
1182 			}
1183 		} else if (0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len)) {
1184 			/* check extension in the form ".fcg" */
1185 			extension = ext;
1186 			break;
1187 		}
1188 	}
1189 
1190 	if (NULL == extension) {
1191 		return HANDLER_GO_ON;
1192 	}
1193 
1194 	if (p->conf.debug) {
1195 		log_error_write(srv, __FILE__, __LINE__,  "s", "proxy - ext found");
1196 	}
1197 
1198 	if (extension->value->used == 1) {
1199 		if ( ((data_proxy *)extension->value->data[0])->is_disabled ) {
1200 			ndx = -1;
1201 		} else {
1202 			ndx = 0;
1203 		}
1204 	} else if (extension->value->used != 0) switch(p->conf.balance) {
1205 	case PROXY_BALANCE_HASH:
1206 		/* hash balancing */
1207 
1208 		if (p->conf.debug) {
1209 			log_error_write(srv, __FILE__, __LINE__,  "sd",
1210 					"proxy - used hash balancing, hosts:", extension->value->used);
1211 		}
1212 
1213 		for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->value->used; k++) {
1214 			data_proxy *host = (data_proxy *)extension->value->data[k];
1215 			unsigned long cur_max;
1216 
1217 			if (host->is_disabled) continue;
1218 
1219 			cur_max = generate_crc32c(CONST_BUF_LEN(con->uri.path)) +
1220 				generate_crc32c(CONST_BUF_LEN(host->host)) + /* we can cache this */
1221 				generate_crc32c(CONST_BUF_LEN(con->uri.authority));
1222 
1223 			if (p->conf.debug) {
1224 				log_error_write(srv, __FILE__, __LINE__,  "sbbbd",
1225 						"proxy - election:",
1226 						con->uri.path,
1227 						host->host,
1228 						con->uri.authority,
1229 						cur_max);
1230 			}
1231 
1232 			if ((last_max == ULONG_MAX) || /* first round */
1233 		   	    (cur_max > last_max)) {
1234 				last_max = cur_max;
1235 
1236 				ndx = k;
1237 			}
1238 		}
1239 
1240 		break;
1241 	case PROXY_BALANCE_FAIR:
1242 		/* fair balancing */
1243 		if (p->conf.debug) {
1244 			log_error_write(srv, __FILE__, __LINE__,  "s",
1245 					"proxy - used fair balancing");
1246 		}
1247 
1248 		for (k = 0, ndx = -1, max_usage = INT_MAX; k < extension->value->used; k++) {
1249 			data_proxy *host = (data_proxy *)extension->value->data[k];
1250 
1251 			if (host->is_disabled) continue;
1252 
1253 			if (host->usage < max_usage) {
1254 				max_usage = host->usage;
1255 
1256 				ndx = k;
1257 			}
1258 		}
1259 
1260 		break;
1261 	case PROXY_BALANCE_RR: {
1262 		data_proxy *host;
1263 
1264 		/* round robin */
1265 		if (p->conf.debug) {
1266 			log_error_write(srv, __FILE__, __LINE__,  "s",
1267 					"proxy - used round-robin balancing");
1268 		}
1269 
1270 		/* just to be sure */
1271 		force_assert(extension->value->used < INT_MAX);
1272 
1273 		host = (data_proxy *)extension->value->data[0];
1274 
1275 		/* Use last_used_ndx from first host in list */
1276 		k = host->last_used_ndx;
1277 		ndx = k + 1; /* use next host after the last one */
1278 		if (ndx < 0) ndx = 0;
1279 
1280 		/* Search first active host after last_used_ndx */
1281 		while ( ndx < (int) extension->value->used
1282 				&& (host = (data_proxy *)extension->value->data[ndx])->is_disabled ) ndx++;
1283 
1284 		if (ndx >= (int) extension->value->used) {
1285 			/* didn't found a higher id, wrap to the start */
1286 			for (ndx = 0; ndx <= (int) k; ndx++) {
1287 				host = (data_proxy *)extension->value->data[ndx];
1288 				if (!host->is_disabled) break;
1289 			}
1290 
1291 			/* No active host found */
1292 			if (host->is_disabled) ndx = -1;
1293 		}
1294 
1295 		/* Save new index for next round */
1296 		((data_proxy *)extension->value->data[0])->last_used_ndx = ndx;
1297 
1298 		break;
1299 	}
1300 	default:
1301 		break;
1302 	}
1303 
1304 	/* found a server */
1305 	if (ndx != -1) {
1306 		data_proxy *host = (data_proxy *)extension->value->data[ndx];
1307 
1308 		/*
1309 		 * if check-local is disabled, use the uri.path handler
1310 		 *
1311 		 */
1312 
1313 		/* init handler-context */
1314 		handler_ctx *hctx;
1315 		hctx = handler_ctx_init();
1316 
1317 		hctx->path_info_offset = path_info_offset;
1318 		hctx->remote_conn      = con;
1319 		hctx->plugin_data      = p;
1320 		hctx->host             = host;
1321 
1322 		con->plugin_ctx[p->id] = hctx;
1323 
1324 		host->usage++;
1325 
1326 		con->mode = p->id;
1327 
1328 		if (p->conf.debug) {
1329 			log_error_write(srv, __FILE__, __LINE__,  "sbd",
1330 					"proxy - found a host",
1331 					host->host, host->port);
1332 		}
1333 
1334 		return HANDLER_GO_ON;
1335 	} else {
1336 		/* no handler found */
1337 		con->http_status = 500;
1338 
1339 		log_error_write(srv, __FILE__, __LINE__,  "sb",
1340 				"no proxy-handler found for:",
1341 				fn);
1342 
1343 		return HANDLER_FINISHED;
1344 	}
1345 	return HANDLER_GO_ON;
1346 }
1347 
1348 static handler_t mod_proxy_connection_reset(server *srv, connection *con, void *p_d) {
1349 	plugin_data *p = p_d;
1350 	handler_ctx *hctx = con->plugin_ctx[p->id];
1351 	if (hctx) proxy_connection_close(srv, hctx);
1352 
1353 	return HANDLER_GO_ON;
1354 }
1355 
1356 /**
1357  *
1358  * the trigger re-enables the disabled connections after the timeout is over
1359  *
1360  * */
1361 
1362 TRIGGER_FUNC(mod_proxy_trigger) {
1363 	plugin_data *p = p_d;
1364 
1365 	if (p->config_storage) {
1366 		size_t i, n, k;
1367 		for (i = 0; i < srv->config_context->used; i++) {
1368 			plugin_config *s = p->config_storage[i];
1369 
1370 			if (!s) continue;
1371 
1372 			/* get the extensions for all configs */
1373 
1374 			for (k = 0; k < s->extensions->used; k++) {
1375 				data_array *extension = (data_array *)s->extensions->data[k];
1376 
1377 				/* get all hosts */
1378 				for (n = 0; n < extension->value->used; n++) {
1379 					data_proxy *host = (data_proxy *)extension->value->data[n];
1380 
1381 					if (!host->is_disabled ||
1382 					    srv->cur_ts - host->disable_ts < 5) continue;
1383 
1384 					log_error_write(srv, __FILE__, __LINE__,  "sbd",
1385 							"proxy - re-enabled:",
1386 							host->host, host->port);
1387 
1388 					host->is_disabled = 0;
1389 				}
1390 			}
1391 		}
1392 	}
1393 
1394 	return HANDLER_GO_ON;
1395 }
1396 
1397 
1398 int mod_proxy_plugin_init(plugin *p);
1399 int mod_proxy_plugin_init(plugin *p) {
1400 	p->version      = LIGHTTPD_VERSION_ID;
1401 	p->name         = buffer_init_string("proxy");
1402 
1403 	p->init         = mod_proxy_init;
1404 	p->cleanup      = mod_proxy_free;
1405 	p->set_defaults = mod_proxy_set_defaults;
1406 	p->connection_reset        = mod_proxy_connection_reset; /* end of req-resp cycle */
1407 	p->handle_connection_close = mod_proxy_connection_reset; /* end of client connection */
1408 	p->handle_uri_clean        = mod_proxy_check_extension;
1409 	p->handle_subrequest       = mod_proxy_handle_subrequest;
1410 	p->handle_trigger          = mod_proxy_trigger;
1411 
1412 	p->data         = NULL;
1413 
1414 	return 0;
1415 }
1416