[core] stream response to client (#949)
[lighttpd.git] / src / mod_proxy.c
blob8f3403b6e07659710859a3ae9ed6f2a08871e4dc
1 #include "first.h"
3 #include "buffer.h"
4 #include "server.h"
5 #include "keyvalue.h"
6 #include "log.h"
8 #include "http_chunk.h"
9 #include "fdevent.h"
10 #include "connections.h"
11 #include "response.h"
12 #include "joblist.h"
14 #include "plugin.h"
16 #include "inet_ntop_cache.h"
17 #include "crc32.h"
19 #include <sys/types.h>
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>
29 #include <stdio.h>
31 #include "sys-socket.h"
33 #define data_proxy data_fastcgi
34 #define data_proxy_init data_fastcgi_init
36 #define PROXY_RETRY_TIMEOUT 60
38 /**
40 * the proxy module is based on the fastcgi module
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.
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
55 typedef enum {
56 PROXY_BALANCE_UNSET,
57 PROXY_BALANCE_FAIR,
58 PROXY_BALANCE_HASH,
59 PROXY_BALANCE_RR
60 } proxy_balance_t;
62 typedef struct {
63 array *extensions;
64 unsigned short debug;
66 proxy_balance_t balance;
67 } plugin_config;
69 typedef struct {
70 PLUGIN_DATA;
72 buffer *parse_response;
73 buffer *balance_buf;
75 plugin_config **config_storage;
77 plugin_config conf;
78 } plugin_data;
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;
88 enum { PROXY_STDOUT, PROXY_END_REQUEST };
90 typedef struct {
91 proxy_connection_state_t state;
92 time_t state_timestamp;
94 data_proxy *host;
96 buffer *response;
97 buffer *response_header;
99 chunkqueue *wb;
101 int fd; /* fd to the proxy process */
102 int fde_ndx; /* index into the fd-event buffer */
104 size_t path_info_offset; /* start of path_info in uri.path */
106 connection *remote_conn; /* dump pointer */
107 plugin_data *plugin_data; /* dump pointer */
108 } handler_ctx;
111 /* ok, we need a prototype */
112 static handler_t proxy_handle_fdevent(server *srv, void *ctx, int revents);
114 static handler_ctx * handler_ctx_init(void) {
115 handler_ctx * hctx;
118 hctx = calloc(1, sizeof(*hctx));
120 hctx->state = PROXY_STATE_INIT;
121 hctx->host = NULL;
123 hctx->response = buffer_init();
124 hctx->response_header = buffer_init();
126 hctx->wb = chunkqueue_init();
128 hctx->fd = -1;
129 hctx->fde_ndx = -1;
131 return hctx;
134 static void handler_ctx_free(handler_ctx *hctx) {
135 buffer_free(hctx->response);
136 buffer_free(hctx->response_header);
137 chunkqueue_free(hctx->wb);
139 free(hctx);
142 INIT_FUNC(mod_proxy_init) {
143 plugin_data *p;
145 p = calloc(1, sizeof(*p));
147 p->parse_response = buffer_init();
148 p->balance_buf = buffer_init();
150 return p;
154 FREE_FUNC(mod_proxy_free) {
155 plugin_data *p = p_d;
157 UNUSED(srv);
159 buffer_free(p->parse_response);
160 buffer_free(p->balance_buf);
162 if (p->config_storage) {
163 size_t i;
164 for (i = 0; i < srv->config_context->used; i++) {
165 plugin_config *s = p->config_storage[i];
167 if (NULL == s) continue;
169 array_free(s->extensions);
171 free(s);
173 free(p->config_storage);
176 free(p);
178 return HANDLER_GO_ON;
181 SETDEFAULTS_FUNC(mod_proxy_set_defaults) {
182 plugin_data *p = p_d;
183 data_unset *du;
184 size_t i = 0;
186 config_values_t cv[] = {
187 { "proxy.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
188 { "proxy.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
189 { "proxy.balance", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
190 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
193 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
195 for (i = 0; i < srv->config_context->used; i++) {
196 data_config const* config = (data_config const*)srv->config_context->data[i];
197 plugin_config *s;
199 s = malloc(sizeof(plugin_config));
200 s->extensions = array_init();
201 s->debug = 0;
203 cv[0].destination = s->extensions;
204 cv[1].destination = &(s->debug);
205 cv[2].destination = p->balance_buf;
207 buffer_reset(p->balance_buf);
209 p->config_storage[i] = s;
211 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
212 return HANDLER_ERROR;
215 if (buffer_string_is_empty(p->balance_buf)) {
216 s->balance = PROXY_BALANCE_FAIR;
217 } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("fair"))) {
218 s->balance = PROXY_BALANCE_FAIR;
219 } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("round-robin"))) {
220 s->balance = PROXY_BALANCE_RR;
221 } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("hash"))) {
222 s->balance = PROXY_BALANCE_HASH;
223 } else {
224 log_error_write(srv, __FILE__, __LINE__, "sb",
225 "proxy.balance has to be one of: fair, round-robin, hash, but not:", p->balance_buf);
226 return HANDLER_ERROR;
229 if (NULL != (du = array_get_element(config->value, "proxy.server"))) {
230 size_t j;
231 data_array *da = (data_array *)du;
233 if (du->type != TYPE_ARRAY) {
234 log_error_write(srv, __FILE__, __LINE__, "sss",
235 "unexpected type for key: ", "proxy.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
237 return HANDLER_ERROR;
241 * proxy.server = ( "<ext>" => ...,
242 * "<ext>" => ... )
245 for (j = 0; j < da->value->used; j++) {
246 data_array *da_ext = (data_array *)da->value->data[j];
247 size_t n;
249 if (da_ext->type != TYPE_ARRAY) {
250 log_error_write(srv, __FILE__, __LINE__, "sssbs",
251 "unexpected type for key: ", "proxy.server",
252 "[", da->value->data[j]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
254 return HANDLER_ERROR;
258 * proxy.server = ( "<ext>" =>
259 * ( "<host>" => ( ... ),
260 * "<host>" => ( ... )
261 * ),
262 * "<ext>" => ... )
265 for (n = 0; n < da_ext->value->used; n++) {
266 data_array *da_host = (data_array *)da_ext->value->data[n];
268 data_proxy *df;
269 data_array *dfa;
271 config_values_t pcv[] = {
272 { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
273 { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
274 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
277 if (da_host->type != TYPE_ARRAY) {
278 log_error_write(srv, __FILE__, __LINE__, "ssSBS",
279 "unexpected type for key:",
280 "proxy.server",
281 "[", da_ext->value->data[n]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
283 return HANDLER_ERROR;
286 df = data_proxy_init();
288 df->port = 80;
290 buffer_copy_buffer(df->key, da_host->key);
292 pcv[0].destination = df->host;
293 pcv[1].destination = &(df->port);
295 if (0 != config_insert_values_internal(srv, da_host->value, pcv, T_CONFIG_SCOPE_CONNECTION)) {
296 df->free((data_unset*) df);
297 return HANDLER_ERROR;
300 if (buffer_string_is_empty(df->host)) {
301 log_error_write(srv, __FILE__, __LINE__, "sbbbs",
302 "missing key (string):",
303 da->key,
304 da_ext->key,
305 da_host->key,
306 "host");
308 df->free((data_unset*) df);
309 return HANDLER_ERROR;
312 /* if extension already exists, take it */
314 if (NULL == (dfa = (data_array *)array_get_element(s->extensions, da_ext->key->ptr))) {
315 dfa = data_array_init();
317 buffer_copy_buffer(dfa->key, da_ext->key);
319 array_insert_unique(dfa->value, (data_unset *)df);
320 array_insert_unique(s->extensions, (data_unset *)dfa);
321 } else {
322 array_insert_unique(dfa->value, (data_unset *)df);
329 return HANDLER_GO_ON;
332 static void proxy_connection_close(server *srv, handler_ctx *hctx) {
333 plugin_data *p;
334 connection *con;
336 p = hctx->plugin_data;
337 con = hctx->remote_conn;
339 if (hctx->fd != -1) {
340 fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
341 fdevent_unregister(srv->ev, hctx->fd);
343 close(hctx->fd);
344 srv->cur_fds--;
347 if (hctx->host) {
348 hctx->host->usage--;
351 handler_ctx_free(hctx);
352 con->plugin_ctx[p->id] = NULL;
354 /* finish response (if not already con->file_started, con->file_finished) */
355 if (con->mode == p->id) {
356 http_response_backend_done(srv, con);
360 static int proxy_establish_connection(server *srv, handler_ctx *hctx) {
361 struct sockaddr *proxy_addr;
362 struct sockaddr_in proxy_addr_in;
363 #if defined(HAVE_SYS_UN_H)
364 struct sockaddr_un proxy_addr_un;
365 #endif
366 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
367 struct sockaddr_in6 proxy_addr_in6;
368 #endif
369 socklen_t servlen;
371 plugin_data *p = hctx->plugin_data;
372 data_proxy *host= hctx->host;
373 int proxy_fd = hctx->fd;
376 #if defined(HAVE_SYS_UN_H)
377 if (strstr(host->host->ptr, "/")) {
378 if (buffer_string_length(host->host) + 1 > sizeof(proxy_addr_un.sun_path)) {
379 log_error_write(srv, __FILE__, __LINE__, "sB",
380 "ERROR: Unix Domain socket filename too long:",
381 host->host);
382 return -1;
385 memset(&proxy_addr_un, 0, sizeof(proxy_addr_un));
386 proxy_addr_un.sun_family = AF_UNIX;
387 strcpy(proxy_addr_un.sun_path, host->host->ptr);
388 servlen = sizeof(proxy_addr_un);
389 proxy_addr = (struct sockaddr *) &proxy_addr_un;
390 } else
391 #endif
392 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
393 if (strstr(host->host->ptr, ":")) {
394 memset(&proxy_addr_in6, 0, sizeof(proxy_addr_in6));
395 proxy_addr_in6.sin6_family = AF_INET6;
396 inet_pton(AF_INET6, host->host->ptr, (char *) &proxy_addr_in6.sin6_addr);
397 proxy_addr_in6.sin6_port = htons(host->port);
398 servlen = sizeof(proxy_addr_in6);
399 proxy_addr = (struct sockaddr *) &proxy_addr_in6;
400 } else
401 #endif
403 memset(&proxy_addr_in, 0, sizeof(proxy_addr_in));
404 proxy_addr_in.sin_family = AF_INET;
405 proxy_addr_in.sin_addr.s_addr = inet_addr(host->host->ptr);
406 proxy_addr_in.sin_port = htons(host->port);
407 servlen = sizeof(proxy_addr_in);
408 proxy_addr = (struct sockaddr *) &proxy_addr_in;
412 if (-1 == connect(proxy_fd, proxy_addr, servlen)) {
413 if (errno == EINPROGRESS || errno == EALREADY) {
414 if (p->conf.debug) {
415 log_error_write(srv, __FILE__, __LINE__, "sd",
416 "connect delayed:", proxy_fd);
419 return 1;
420 } else {
422 log_error_write(srv, __FILE__, __LINE__, "sdsd",
423 "connect failed:", proxy_fd, strerror(errno), errno);
425 return -1;
428 if (p->conf.debug) {
429 log_error_write(srv, __FILE__, __LINE__, "sd",
430 "connect succeeded: ", proxy_fd);
433 return 0;
436 static void proxy_set_header(connection *con, const char *key, const char *value) {
437 data_string *ds_dst;
439 if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
440 ds_dst = data_string_init();
443 buffer_copy_string(ds_dst->key, key);
444 buffer_copy_string(ds_dst->value, value);
445 array_insert_unique(con->request.headers, (data_unset *)ds_dst);
448 static void proxy_append_header(connection *con, const char *key, const char *value) {
449 data_string *ds_dst;
451 if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
452 ds_dst = data_string_init();
455 buffer_copy_string(ds_dst->key, key);
456 buffer_append_string(ds_dst->value, value);
457 array_insert_unique(con->request.headers, (data_unset *)ds_dst);
461 static int proxy_create_env(server *srv, handler_ctx *hctx) {
462 size_t i;
464 connection *con = hctx->remote_conn;
465 buffer *b;
467 /* build header */
469 b = buffer_init();
471 /* request line */
472 buffer_copy_string(b, get_http_method_name(con->request.http_method));
473 buffer_append_string_len(b, CONST_STR_LEN(" "));
475 buffer_append_string_buffer(b, con->request.uri);
476 buffer_append_string_len(b, CONST_STR_LEN(" HTTP/1.0\r\n"));
478 proxy_append_header(con, "X-Forwarded-For", (char *)inet_ntop_cache_get_ip(srv, &(con->dst_addr)));
479 /* http_host is NOT is just a pointer to a buffer
480 * which is NULL if it is not set */
481 if (!buffer_string_is_empty(con->request.http_host)) {
482 proxy_set_header(con, "X-Host", con->request.http_host->ptr);
484 proxy_set_header(con, "X-Forwarded-Proto", con->uri.scheme->ptr);
486 /* request header */
487 for (i = 0; i < con->request.headers->used; i++) {
488 data_string *ds;
490 ds = (data_string *)con->request.headers->data[i];
492 if (!buffer_is_empty(ds->value) && !buffer_is_empty(ds->key)) {
493 if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Connection"))) continue;
494 if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Proxy-Connection"))) continue;
496 buffer_append_string_buffer(b, ds->key);
497 buffer_append_string_len(b, CONST_STR_LEN(": "));
498 buffer_append_string_buffer(b, ds->value);
499 buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
503 buffer_append_string_len(b, CONST_STR_LEN("Connection: close\r\n\r\n"));
505 chunkqueue_append_buffer(hctx->wb, b);
506 buffer_free(b);
508 /* body */
510 if (con->request.content_length) {
511 chunkqueue *req_cq = con->request_content_queue;
513 chunkqueue_steal(hctx->wb, req_cq, req_cq->bytes_in);
516 return 0;
519 static int proxy_set_state(server *srv, handler_ctx *hctx, proxy_connection_state_t state) {
520 hctx->state = state;
521 hctx->state_timestamp = srv->cur_ts;
523 return 0;
527 static int proxy_response_parse(server *srv, connection *con, plugin_data *p, buffer *in) {
528 char *s, *ns;
529 int http_response_status = -1;
531 UNUSED(srv);
533 /* [\r]\n -> [\0]\0 */
535 buffer_copy_buffer(p->parse_response, in);
537 for (s = p->parse_response->ptr; NULL != (ns = strchr(s, '\n')); s = ns + 1) {
538 char *key, *value;
539 int key_len;
540 data_string *ds;
541 int copy_header;
543 ns[0] = '\0';
544 if (s != ns && ns[-1] == '\r') ns[-1] = '\0';
546 if (-1 == http_response_status) {
547 /* The first line of a Response message is the Status-Line */
549 for (key=s; *key && *key != ' '; key++);
551 if (*key) {
552 http_response_status = (int) strtol(key, NULL, 10);
553 if (http_response_status < 100 || http_response_status >= 1000) http_response_status = 502;
554 } else {
555 http_response_status = 502;
558 con->http_status = http_response_status;
559 con->parsed_response |= HTTP_STATUS;
560 continue;
563 if (NULL == (value = strchr(s, ':'))) {
564 /* now we expect: "<key>: <value>\n" */
566 continue;
569 key = s;
570 key_len = value - key;
572 value++;
573 /* strip WS */
574 while (*value == ' ' || *value == '\t') value++;
576 copy_header = 1;
578 switch(key_len) {
579 case 4:
580 if (0 == strncasecmp(key, "Date", key_len)) {
581 con->parsed_response |= HTTP_DATE;
583 break;
584 case 8:
585 if (0 == strncasecmp(key, "Location", key_len)) {
586 con->parsed_response |= HTTP_LOCATION;
588 break;
589 case 10:
590 if (0 == strncasecmp(key, "Connection", key_len)) {
591 copy_header = 0;
593 break;
594 case 14:
595 if (0 == strncasecmp(key, "Content-Length", key_len)) {
596 con->response.content_length = strtoul(value, NULL, 10);
597 con->parsed_response |= HTTP_CONTENT_LENGTH;
599 break;
600 default:
601 break;
604 if (copy_header) {
605 if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
606 ds = data_response_init();
608 buffer_copy_string_len(ds->key, key, key_len);
609 buffer_copy_string(ds->value, value);
611 array_insert_unique(con->response.headers, (data_unset *)ds);
615 return 0;
619 static int proxy_demux_response(server *srv, handler_ctx *hctx) {
620 int fin = 0;
621 int b;
622 ssize_t r;
624 plugin_data *p = hctx->plugin_data;
625 connection *con = hctx->remote_conn;
626 int proxy_fd = hctx->fd;
628 /* check how much we have to read */
629 if (ioctl(hctx->fd, FIONREAD, &b)) {
630 log_error_write(srv, __FILE__, __LINE__, "sd",
631 "ioctl failed: ",
632 proxy_fd);
633 return -1;
637 if (p->conf.debug) {
638 log_error_write(srv, __FILE__, __LINE__, "sd",
639 "proxy - have to read:", b);
642 if (b > 0) {
643 buffer_string_prepare_append(hctx->response, b);
645 if (-1 == (r = read(hctx->fd, hctx->response->ptr + buffer_string_length(hctx->response), buffer_string_space(hctx->response)))) {
646 if (errno == EAGAIN) return 0;
647 log_error_write(srv, __FILE__, __LINE__, "sds",
648 "unexpected end-of-file (perhaps the proxy process died):",
649 proxy_fd, strerror(errno));
650 return -1;
653 /* this should be catched by the b > 0 above */
654 force_assert(r);
656 buffer_commit(hctx->response, r);
658 #if 0
659 log_error_write(srv, __FILE__, __LINE__, "sdsbs",
660 "demux: Response buffer len", hctx->response->used, ":", hctx->response, ":");
661 #endif
663 if (0 == con->got_response) {
664 con->got_response = 1;
665 buffer_string_prepare_copy(hctx->response_header, 1023);
668 if (0 == con->file_started) {
669 char *c;
671 /* search for the \r\n\r\n in the string */
672 if (NULL != (c = buffer_search_string_len(hctx->response, CONST_STR_LEN("\r\n\r\n")))) {
673 size_t hlen = c - hctx->response->ptr + 4;
674 size_t blen = buffer_string_length(hctx->response) - hlen;
675 /* found */
677 buffer_append_string_len(hctx->response_header, hctx->response->ptr, hlen);
678 #if 0
679 log_error_write(srv, __FILE__, __LINE__, "sb", "Header:", hctx->response_header);
680 #endif
681 /* parse the response header */
682 proxy_response_parse(srv, con, p, hctx->response_header);
684 /* enable chunked-transfer-encoding */
685 if (con->request.http_version == HTTP_VERSION_1_1 &&
686 !(con->parsed_response & HTTP_CONTENT_LENGTH)) {
687 con->response.transfer_encoding = HTTP_TRANSFER_ENCODING_CHUNKED;
690 con->file_started = 1;
691 if (blen > 0) {
692 if (0 != http_chunk_append_mem(srv, con, c + 4, blen)) {
693 /* error writing to tempfile;
694 * truncate response or send 500 if nothing sent yet */
695 fin = 1;
696 con->file_started = 0;
699 buffer_reset(hctx->response);
701 } else {
702 if (0 != http_chunk_append_buffer(srv, con, hctx->response)) {
703 /* error writing to tempfile;
704 * truncate response or send 500 if nothing sent yet */
705 fin = 1;
707 buffer_reset(hctx->response);
710 } else {
711 /* reading from upstream done */
712 con->file_finished = 1;
714 http_chunk_close(srv, con);
716 fin = 1;
719 return fin;
723 static handler_t proxy_write_request(server *srv, handler_ctx *hctx) {
724 data_proxy *host= hctx->host;
725 connection *con = hctx->remote_conn;
727 int ret;
729 if (!host || buffer_string_is_empty(host->host) || !host->port) return HANDLER_ERROR;
731 switch(hctx->state) {
732 case PROXY_STATE_CONNECT:
733 /* wait for the connect() to finish */
735 /* connect failed ? */
736 if (-1 == hctx->fde_ndx) return HANDLER_ERROR;
738 /* wait */
739 return HANDLER_WAIT_FOR_EVENT;
741 case PROXY_STATE_INIT:
742 #if defined(HAVE_SYS_UN_H)
743 if (strstr(host->host->ptr,"/")) {
744 if (-1 == (hctx->fd = socket(AF_UNIX, SOCK_STREAM, 0))) {
745 log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
746 return HANDLER_ERROR;
748 } else
749 #endif
750 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
751 if (strstr(host->host->ptr,":")) {
752 if (-1 == (hctx->fd = socket(AF_INET6, SOCK_STREAM, 0))) {
753 log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
754 return HANDLER_ERROR;
756 } else
757 #endif
759 if (-1 == (hctx->fd = socket(AF_INET, SOCK_STREAM, 0))) {
760 log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
761 return HANDLER_ERROR;
764 hctx->fde_ndx = -1;
766 srv->cur_fds++;
768 fdevent_register(srv->ev, hctx->fd, proxy_handle_fdevent, hctx);
770 if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
771 log_error_write(srv, __FILE__, __LINE__, "ss", "fcntl failed: ", strerror(errno));
773 return HANDLER_ERROR;
776 switch (proxy_establish_connection(srv, hctx)) {
777 case 1:
778 proxy_set_state(srv, hctx, PROXY_STATE_CONNECT);
780 /* connection is in progress, wait for an event and call getsockopt() below */
782 fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
784 return HANDLER_WAIT_FOR_EVENT;
785 case -1:
786 /* if ECONNREFUSED choose another connection -> FIXME */
787 hctx->fde_ndx = -1;
789 return HANDLER_ERROR;
790 default:
791 /* everything is ok, go on */
792 proxy_set_state(srv, hctx, PROXY_STATE_PREPARE_WRITE);
793 break;
796 /* fall through */
798 case PROXY_STATE_PREPARE_WRITE:
799 proxy_create_env(srv, hctx);
801 proxy_set_state(srv, hctx, PROXY_STATE_WRITE);
803 /* fall through */
804 case PROXY_STATE_WRITE:;
805 ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
807 chunkqueue_remove_finished_chunks(hctx->wb);
809 if (-1 == ret) { /* error on our side */
810 log_error_write(srv, __FILE__, __LINE__, "ssd", "write failed:", strerror(errno), errno);
812 return HANDLER_ERROR;
813 } else if (-2 == ret) { /* remote close */
814 log_error_write(srv, __FILE__, __LINE__, "ssd", "write failed, remote connection close:", strerror(errno), errno);
816 return HANDLER_ERROR;
819 if (hctx->wb->bytes_out == hctx->wb->bytes_in) {
820 proxy_set_state(srv, hctx, PROXY_STATE_READ);
821 fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
822 } else {
823 fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN|FDEVENT_OUT);
826 return HANDLER_WAIT_FOR_EVENT;
827 case PROXY_STATE_READ:
828 /* waiting for a response */
829 return HANDLER_WAIT_FOR_EVENT;
830 default:
831 log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
832 return HANDLER_ERROR;
836 #define PATCH(x) \
837 p->conf.x = s->x;
838 static int mod_proxy_patch_connection(server *srv, connection *con, plugin_data *p) {
839 size_t i, j;
840 plugin_config *s = p->config_storage[0];
842 PATCH(extensions);
843 PATCH(debug);
844 PATCH(balance);
846 /* skip the first, the global context */
847 for (i = 1; i < srv->config_context->used; i++) {
848 data_config *dc = (data_config *)srv->config_context->data[i];
849 s = p->config_storage[i];
851 /* condition didn't match */
852 if (!config_check_cond(srv, con, dc)) continue;
854 /* merge config */
855 for (j = 0; j < dc->value->used; j++) {
856 data_unset *du = dc->value->data[j];
858 if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.server"))) {
859 PATCH(extensions);
860 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.debug"))) {
861 PATCH(debug);
862 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.balance"))) {
863 PATCH(balance);
868 return 0;
870 #undef PATCH
872 static handler_t proxy_send_request(server *srv, handler_ctx *hctx) {
873 /* ok, create the request */
874 handler_t rc = proxy_write_request(srv, hctx);
875 if (HANDLER_ERROR != rc) {
876 return rc;
877 } else {
878 data_proxy *host = hctx->host;
879 connection *con = hctx->remote_conn;
880 log_error_write(srv, __FILE__, __LINE__, "sbdd", "proxy-server disabled:",
881 host->host,
882 host->port,
883 hctx->fd);
885 /* disable this server */
886 host->is_disabled = 1;
887 host->disable_ts = srv->cur_ts;
889 /* reset the enviroment and restart the sub-request */
890 con->mode = DIRECT;/*(avoid changing con->state, con->http_status)*/
891 proxy_connection_close(srv, hctx);
892 con->mode = hctx->plugin_data->id; /* p->id */
894 return HANDLER_COMEBACK;
898 SUBREQUEST_FUNC(mod_proxy_handle_subrequest) {
899 plugin_data *p = p_d;
901 handler_ctx *hctx = con->plugin_ctx[p->id];
903 if (NULL == hctx) return HANDLER_GO_ON;
905 /* not my job */
906 if (con->mode != p->id) return HANDLER_GO_ON;
908 if (con->state == CON_STATE_READ_POST) {
909 handler_t r = connection_handle_read_post_state(srv, con);
910 if (r != HANDLER_GO_ON) return r;
913 return (hctx->state != PROXY_STATE_READ)
914 ? proxy_send_request(srv, hctx)
915 : HANDLER_WAIT_FOR_EVENT;
918 static handler_t proxy_handle_fdevent(server *srv, void *ctx, int revents) {
919 handler_ctx *hctx = ctx;
920 connection *con = hctx->remote_conn;
921 plugin_data *p = hctx->plugin_data;
923 joblist_append(srv, con);
925 if ((revents & FDEVENT_IN) &&
926 hctx->state == PROXY_STATE_READ) {
928 if (p->conf.debug) {
929 log_error_write(srv, __FILE__, __LINE__, "sd",
930 "proxy: fdevent-in", hctx->state);
933 switch (proxy_demux_response(srv, hctx)) {
934 case 0:
935 break;
936 case 1:
937 /* we are done */
938 proxy_connection_close(srv, hctx);
940 return HANDLER_FINISHED;
941 case -1:
942 if (con->file_started == 0) {
943 /* reading response headers failed */
944 } else {
945 /* response might have been already started, kill the connection */
946 con->keep_alive = 0;
947 con->file_finished = 1;
948 con->mode = DIRECT; /*(avoid sending final chunked block)*/
951 proxy_connection_close(srv, hctx);
953 return HANDLER_FINISHED;
957 if (revents & FDEVENT_OUT) {
958 if (p->conf.debug) {
959 log_error_write(srv, __FILE__, __LINE__, "sd",
960 "proxy: fdevent-out", hctx->state);
963 if (hctx->state == PROXY_STATE_CONNECT) {
964 int socket_error;
965 socklen_t socket_error_len = sizeof(socket_error);
967 /* try to finish the connect() */
968 if (0 != getsockopt(hctx->fd, SOL_SOCKET, SO_ERROR, &socket_error, &socket_error_len)) {
969 log_error_write(srv, __FILE__, __LINE__, "ss",
970 "getsockopt failed:", strerror(errno));
972 return HANDLER_FINISHED;
974 if (socket_error != 0) {
975 log_error_write(srv, __FILE__, __LINE__, "ss",
976 "establishing connection failed:", strerror(socket_error),
977 "port:", hctx->host->port);
979 return HANDLER_FINISHED;
981 if (p->conf.debug) {
982 log_error_write(srv, __FILE__, __LINE__, "s", "proxy - connect - delayed success");
985 proxy_set_state(srv, hctx, PROXY_STATE_PREPARE_WRITE);
988 if (hctx->state == PROXY_STATE_PREPARE_WRITE ||
989 hctx->state == PROXY_STATE_WRITE) {
990 /* we are allowed to send something out
992 * 1. after a just finished connect() call
993 * 2. in a unfinished write() call (long POST request)
995 return proxy_send_request(srv, hctx); /*(might invalidate hctx)*/
996 } else {
997 log_error_write(srv, __FILE__, __LINE__, "sd",
998 "proxy: out", hctx->state);
1002 /* perhaps this issue is already handled */
1003 if (revents & FDEVENT_HUP) {
1004 if (p->conf.debug) {
1005 log_error_write(srv, __FILE__, __LINE__, "sd",
1006 "proxy: fdevent-hup", hctx->state);
1009 if (hctx->state == PROXY_STATE_CONNECT) {
1010 /* connect() -> EINPROGRESS -> HUP */
1013 * what is proxy is doing if it can't reach the next hop ?
1017 if (hctx->host) {
1018 hctx->host->is_disabled = 1;
1019 hctx->host->disable_ts = srv->cur_ts;
1020 log_error_write(srv, __FILE__, __LINE__, "sbdd", "proxy-server disabled:",
1021 hctx->host->host,
1022 hctx->host->port,
1023 hctx->fd);
1025 /* disable this server */
1026 hctx->host->is_disabled = 1;
1027 hctx->host->disable_ts = srv->cur_ts;
1029 /* reset the environment and restart the sub-request */
1030 con->mode = DIRECT;/*(avoid changing con->state, con->http_status)*/
1031 proxy_connection_close(srv, hctx);
1032 con->mode = p->id;
1033 } else {
1034 proxy_connection_close(srv, hctx);
1035 con->http_status = 503;
1037 } else {
1038 proxy_connection_close(srv, hctx);
1040 } else if (revents & FDEVENT_ERR) {
1041 log_error_write(srv, __FILE__, __LINE__, "sd", "proxy-FDEVENT_ERR, but no HUP", revents);
1043 if (con->file_started) {
1044 con->keep_alive = 0;
1045 con->file_finished = 1;
1046 con->mode = DIRECT; /*(avoid sending final chunked block)*/
1048 proxy_connection_close(srv, hctx);
1051 return HANDLER_FINISHED;
1054 static handler_t mod_proxy_check_extension(server *srv, connection *con, void *p_d) {
1055 plugin_data *p = p_d;
1056 size_t s_len;
1057 unsigned long last_max = ULONG_MAX;
1058 int max_usage = INT_MAX;
1059 int ndx = -1;
1060 size_t k;
1061 buffer *fn;
1062 data_array *extension = NULL;
1063 size_t path_info_offset;
1065 if (con->mode != DIRECT) return HANDLER_GO_ON;
1067 /* Possibly, we processed already this request */
1068 if (con->file_started == 1) return HANDLER_GO_ON;
1070 mod_proxy_patch_connection(srv, con, p);
1072 fn = con->uri.path;
1073 if (buffer_string_is_empty(fn)) return HANDLER_ERROR;
1074 s_len = buffer_string_length(fn);
1076 path_info_offset = 0;
1078 if (p->conf.debug) {
1079 log_error_write(srv, __FILE__, __LINE__, "s", "proxy - start");
1082 /* check if extension matches */
1083 for (k = 0; k < p->conf.extensions->used; k++) {
1084 data_array *ext = NULL;
1085 size_t ct_len;
1087 ext = (data_array *)p->conf.extensions->data[k];
1089 if (buffer_is_empty(ext->key)) continue;
1091 ct_len = buffer_string_length(ext->key);
1093 if (s_len < ct_len) continue;
1095 /* check extension in the form "/proxy_pattern" */
1096 if (*(ext->key->ptr) == '/') {
1097 if (strncmp(fn->ptr, ext->key->ptr, ct_len) == 0) {
1098 if (s_len > ct_len + 1) {
1099 char *pi_offset;
1101 if (NULL != (pi_offset = strchr(fn->ptr + ct_len + 1, '/'))) {
1102 path_info_offset = pi_offset - fn->ptr;
1105 extension = ext;
1106 break;
1108 } else if (0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len)) {
1109 /* check extension in the form ".fcg" */
1110 extension = ext;
1111 break;
1115 if (NULL == extension) {
1116 return HANDLER_GO_ON;
1119 if (p->conf.debug) {
1120 log_error_write(srv, __FILE__, __LINE__, "s", "proxy - ext found");
1123 if (extension->value->used == 1) {
1124 if ( ((data_proxy *)extension->value->data[0])->is_disabled ) {
1125 ndx = -1;
1126 } else {
1127 ndx = 0;
1129 } else if (extension->value->used != 0) switch(p->conf.balance) {
1130 case PROXY_BALANCE_HASH:
1131 /* hash balancing */
1133 if (p->conf.debug) {
1134 log_error_write(srv, __FILE__, __LINE__, "sd",
1135 "proxy - used hash balancing, hosts:", extension->value->used);
1138 for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->value->used; k++) {
1139 data_proxy *host = (data_proxy *)extension->value->data[k];
1140 unsigned long cur_max;
1142 if (host->is_disabled) continue;
1144 cur_max = generate_crc32c(CONST_BUF_LEN(con->uri.path)) +
1145 generate_crc32c(CONST_BUF_LEN(host->host)) + /* we can cache this */
1146 generate_crc32c(CONST_BUF_LEN(con->uri.authority));
1148 if (p->conf.debug) {
1149 log_error_write(srv, __FILE__, __LINE__, "sbbbd",
1150 "proxy - election:",
1151 con->uri.path,
1152 host->host,
1153 con->uri.authority,
1154 cur_max);
1157 if ((last_max == ULONG_MAX) || /* first round */
1158 (cur_max > last_max)) {
1159 last_max = cur_max;
1161 ndx = k;
1165 break;
1166 case PROXY_BALANCE_FAIR:
1167 /* fair balancing */
1168 if (p->conf.debug) {
1169 log_error_write(srv, __FILE__, __LINE__, "s",
1170 "proxy - used fair balancing");
1173 for (k = 0, ndx = -1, max_usage = INT_MAX; k < extension->value->used; k++) {
1174 data_proxy *host = (data_proxy *)extension->value->data[k];
1176 if (host->is_disabled) continue;
1178 if (host->usage < max_usage) {
1179 max_usage = host->usage;
1181 ndx = k;
1185 break;
1186 case PROXY_BALANCE_RR: {
1187 data_proxy *host;
1189 /* round robin */
1190 if (p->conf.debug) {
1191 log_error_write(srv, __FILE__, __LINE__, "s",
1192 "proxy - used round-robin balancing");
1195 /* just to be sure */
1196 force_assert(extension->value->used < INT_MAX);
1198 host = (data_proxy *)extension->value->data[0];
1200 /* Use last_used_ndx from first host in list */
1201 k = host->last_used_ndx;
1202 ndx = k + 1; /* use next host after the last one */
1203 if (ndx < 0) ndx = 0;
1205 /* Search first active host after last_used_ndx */
1206 while ( ndx < (int) extension->value->used
1207 && (host = (data_proxy *)extension->value->data[ndx])->is_disabled ) ndx++;
1209 if (ndx >= (int) extension->value->used) {
1210 /* didn't found a higher id, wrap to the start */
1211 for (ndx = 0; ndx <= (int) k; ndx++) {
1212 host = (data_proxy *)extension->value->data[ndx];
1213 if (!host->is_disabled) break;
1216 /* No active host found */
1217 if (host->is_disabled) ndx = -1;
1220 /* Save new index for next round */
1221 ((data_proxy *)extension->value->data[0])->last_used_ndx = ndx;
1223 break;
1225 default:
1226 break;
1229 /* found a server */
1230 if (ndx != -1) {
1231 data_proxy *host = (data_proxy *)extension->value->data[ndx];
1234 * if check-local is disabled, use the uri.path handler
1238 /* init handler-context */
1239 handler_ctx *hctx;
1240 hctx = handler_ctx_init();
1242 hctx->path_info_offset = path_info_offset;
1243 hctx->remote_conn = con;
1244 hctx->plugin_data = p;
1245 hctx->host = host;
1247 con->plugin_ctx[p->id] = hctx;
1249 host->usage++;
1251 con->mode = p->id;
1253 if (p->conf.debug) {
1254 log_error_write(srv, __FILE__, __LINE__, "sbd",
1255 "proxy - found a host",
1256 host->host, host->port);
1259 return HANDLER_GO_ON;
1260 } else {
1261 /* no handler found */
1262 con->http_status = 500;
1264 log_error_write(srv, __FILE__, __LINE__, "sb",
1265 "no proxy-handler found for:",
1266 fn);
1268 return HANDLER_FINISHED;
1270 return HANDLER_GO_ON;
1273 static handler_t mod_proxy_connection_reset(server *srv, connection *con, void *p_d) {
1274 plugin_data *p = p_d;
1275 handler_ctx *hctx = con->plugin_ctx[p->id];
1276 if (hctx) proxy_connection_close(srv, hctx);
1278 return HANDLER_GO_ON;
1283 * the trigger re-enables the disabled connections after the timeout is over
1285 * */
1287 TRIGGER_FUNC(mod_proxy_trigger) {
1288 plugin_data *p = p_d;
1290 if (p->config_storage) {
1291 size_t i, n, k;
1292 for (i = 0; i < srv->config_context->used; i++) {
1293 plugin_config *s = p->config_storage[i];
1295 if (!s) continue;
1297 /* get the extensions for all configs */
1299 for (k = 0; k < s->extensions->used; k++) {
1300 data_array *extension = (data_array *)s->extensions->data[k];
1302 /* get all hosts */
1303 for (n = 0; n < extension->value->used; n++) {
1304 data_proxy *host = (data_proxy *)extension->value->data[n];
1306 if (!host->is_disabled ||
1307 srv->cur_ts - host->disable_ts < 5) continue;
1309 log_error_write(srv, __FILE__, __LINE__, "sbd",
1310 "proxy - re-enabled:",
1311 host->host, host->port);
1313 host->is_disabled = 0;
1319 return HANDLER_GO_ON;
1323 int mod_proxy_plugin_init(plugin *p);
1324 int mod_proxy_plugin_init(plugin *p) {
1325 p->version = LIGHTTPD_VERSION_ID;
1326 p->name = buffer_init_string("proxy");
1328 p->init = mod_proxy_init;
1329 p->cleanup = mod_proxy_free;
1330 p->set_defaults = mod_proxy_set_defaults;
1331 p->connection_reset = mod_proxy_connection_reset; /* end of req-resp cycle */
1332 p->handle_connection_close = mod_proxy_connection_reset; /* end of client connection */
1333 p->handle_uri_clean = mod_proxy_check_extension;
1334 p->handle_subrequest = mod_proxy_handle_subrequest;
1335 p->handle_trigger = mod_proxy_trigger;
1337 p->data = NULL;
1339 return 0;