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