[core] support Transfer-Encoding: chunked req body (fixes #2156)
[lighttpd.git] / src / mod_proxy.c
blob2881efce0e44ccaeb8daaddb92e253c237cc7f37
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;
65 unsigned short replace_http_host;
67 proxy_balance_t balance;
68 } plugin_config;
70 typedef struct {
71 PLUGIN_DATA;
73 buffer *parse_response;
74 buffer *balance_buf;
76 plugin_config **config_storage;
78 plugin_config conf;
79 } plugin_data;
81 typedef enum {
82 PROXY_STATE_INIT,
83 PROXY_STATE_CONNECT,
84 PROXY_STATE_PREPARE_WRITE,
85 PROXY_STATE_WRITE,
86 PROXY_STATE_READ
87 } proxy_connection_state_t;
89 enum { PROXY_STDOUT, PROXY_END_REQUEST };
91 typedef struct {
92 proxy_connection_state_t state;
93 time_t state_timestamp;
95 data_proxy *host;
97 buffer *response;
98 buffer *response_header;
100 chunkqueue *wb;
101 off_t wb_reqlen;
103 int fd; /* fd to the proxy process */
104 int fde_ndx; /* index into the fd-event buffer */
106 plugin_config conf;
108 connection *remote_conn; /* dumb pointer */
109 plugin_data *plugin_data; /* dumb pointer */
110 data_array *ext;
111 } handler_ctx;
114 /* ok, we need a prototype */
115 static handler_t proxy_handle_fdevent(server *srv, void *ctx, int revents);
117 static handler_ctx * handler_ctx_init(void) {
118 handler_ctx * hctx;
121 hctx = calloc(1, sizeof(*hctx));
123 hctx->state = PROXY_STATE_INIT;
124 hctx->host = NULL;
126 hctx->response = buffer_init();
127 hctx->response_header = buffer_init();
129 hctx->wb = chunkqueue_init();
130 hctx->wb_reqlen = 0;
132 hctx->fd = -1;
133 hctx->fde_ndx = -1;
135 return hctx;
138 static void handler_ctx_free(handler_ctx *hctx) {
139 buffer_free(hctx->response);
140 buffer_free(hctx->response_header);
141 chunkqueue_free(hctx->wb);
143 free(hctx);
146 INIT_FUNC(mod_proxy_init) {
147 plugin_data *p;
149 p = calloc(1, sizeof(*p));
151 p->parse_response = buffer_init();
152 p->balance_buf = buffer_init();
154 return p;
158 FREE_FUNC(mod_proxy_free) {
159 plugin_data *p = p_d;
161 UNUSED(srv);
163 buffer_free(p->parse_response);
164 buffer_free(p->balance_buf);
166 if (p->config_storage) {
167 size_t i;
168 for (i = 0; i < srv->config_context->used; i++) {
169 plugin_config *s = p->config_storage[i];
171 if (NULL == s) continue;
173 array_free(s->extensions);
175 free(s);
177 free(p->config_storage);
180 free(p);
182 return HANDLER_GO_ON;
185 SETDEFAULTS_FUNC(mod_proxy_set_defaults) {
186 plugin_data *p = p_d;
187 data_unset *du;
188 size_t i = 0;
190 config_values_t cv[] = {
191 { "proxy.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
192 { "proxy.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
193 { "proxy.balance", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
194 { "proxy.replace-http-host", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
195 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
198 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
200 for (i = 0; i < srv->config_context->used; i++) {
201 data_config const* config = (data_config const*)srv->config_context->data[i];
202 plugin_config *s;
204 s = malloc(sizeof(plugin_config));
205 s->extensions = array_init();
206 s->debug = 0;
207 s->replace_http_host = 0;
209 cv[0].destination = s->extensions;
210 cv[1].destination = &(s->debug);
211 cv[2].destination = p->balance_buf;
212 cv[3].destination = &(s->replace_http_host);
214 buffer_reset(p->balance_buf);
216 p->config_storage[i] = s;
218 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
219 return HANDLER_ERROR;
222 if (buffer_string_is_empty(p->balance_buf)) {
223 s->balance = PROXY_BALANCE_FAIR;
224 } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("fair"))) {
225 s->balance = PROXY_BALANCE_FAIR;
226 } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("round-robin"))) {
227 s->balance = PROXY_BALANCE_RR;
228 } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("hash"))) {
229 s->balance = PROXY_BALANCE_HASH;
230 } else {
231 log_error_write(srv, __FILE__, __LINE__, "sb",
232 "proxy.balance has to be one of: fair, round-robin, hash, but not:", p->balance_buf);
233 return HANDLER_ERROR;
236 if (NULL != (du = array_get_element(config->value, "proxy.server"))) {
237 size_t j;
238 data_array *da = (data_array *)du;
240 if (du->type != TYPE_ARRAY) {
241 log_error_write(srv, __FILE__, __LINE__, "sss",
242 "unexpected type for key: ", "proxy.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
244 return HANDLER_ERROR;
248 * proxy.server = ( "<ext>" => ...,
249 * "<ext>" => ... )
252 for (j = 0; j < da->value->used; j++) {
253 data_array *da_ext = (data_array *)da->value->data[j];
254 size_t n;
256 if (da_ext->type != TYPE_ARRAY) {
257 log_error_write(srv, __FILE__, __LINE__, "sssbs",
258 "unexpected type for key: ", "proxy.server",
259 "[", da->value->data[j]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
261 return HANDLER_ERROR;
265 * proxy.server = ( "<ext>" =>
266 * ( "<host>" => ( ... ),
267 * "<host>" => ( ... )
268 * ),
269 * "<ext>" => ... )
272 for (n = 0; n < da_ext->value->used; n++) {
273 data_array *da_host = (data_array *)da_ext->value->data[n];
275 data_proxy *df;
276 data_array *dfa;
278 config_values_t pcv[] = {
279 { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
280 { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
281 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
284 if (da_host->type != TYPE_ARRAY) {
285 log_error_write(srv, __FILE__, __LINE__, "ssSBS",
286 "unexpected type for key:",
287 "proxy.server",
288 "[", da_ext->value->data[n]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
290 return HANDLER_ERROR;
293 df = data_proxy_init();
295 df->port = 80;
297 buffer_copy_buffer(df->key, da_host->key);
299 pcv[0].destination = df->host;
300 pcv[1].destination = &(df->port);
302 if (0 != config_insert_values_internal(srv, da_host->value, pcv, T_CONFIG_SCOPE_CONNECTION)) {
303 df->free((data_unset*) df);
304 return HANDLER_ERROR;
307 if (buffer_string_is_empty(df->host)) {
308 log_error_write(srv, __FILE__, __LINE__, "sbbbs",
309 "missing key (string):",
310 da->key,
311 da_ext->key,
312 da_host->key,
313 "host");
315 df->free((data_unset*) df);
316 return HANDLER_ERROR;
319 /* if extension already exists, take it */
321 if (NULL == (dfa = (data_array *)array_get_element(s->extensions, da_ext->key->ptr))) {
322 dfa = data_array_init();
324 buffer_copy_buffer(dfa->key, da_ext->key);
326 array_insert_unique(dfa->value, (data_unset *)df);
327 array_insert_unique(s->extensions, (data_unset *)dfa);
328 } else {
329 array_insert_unique(dfa->value, (data_unset *)df);
336 return HANDLER_GO_ON;
340 static void proxy_backend_close(server *srv, handler_ctx *hctx) {
341 if (hctx->fd != -1) {
342 fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
343 fdevent_unregister(srv->ev, hctx->fd);
344 fdevent_sched_close(srv->ev, hctx->fd, 1);
345 hctx->fd = -1;
346 hctx->fde_ndx = -1;
349 if (hctx->host) {
350 hctx->host->usage--;
351 hctx->host = NULL;
355 static data_proxy * mod_proxy_extension_host_get(server *srv, connection *con, data_array *extension, proxy_balance_t balance, int debug) {
356 unsigned long last_max = ULONG_MAX;
357 int max_usage = INT_MAX;
358 int ndx = -1;
359 size_t k;
361 if (extension->value->used == 1) {
362 if ( ((data_proxy *)extension->value->data[0])->is_disabled ) {
363 ndx = -1;
364 } else {
365 ndx = 0;
367 } else if (extension->value->used != 0) switch(balance) {
368 case PROXY_BALANCE_HASH:
369 /* hash balancing */
371 if (debug) {
372 log_error_write(srv, __FILE__, __LINE__, "sd",
373 "proxy - used hash balancing, hosts:", extension->value->used);
376 for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->value->used; k++) {
377 data_proxy *host = (data_proxy *)extension->value->data[k];
378 unsigned long cur_max;
380 if (host->is_disabled) continue;
382 cur_max = generate_crc32c(CONST_BUF_LEN(con->uri.path)) +
383 generate_crc32c(CONST_BUF_LEN(host->host)) + /* we can cache this */
384 generate_crc32c(CONST_BUF_LEN(con->uri.authority));
386 if (debug) {
387 log_error_write(srv, __FILE__, __LINE__, "sbbbd",
388 "proxy - election:",
389 con->uri.path,
390 host->host,
391 con->uri.authority,
392 cur_max);
395 if ((last_max == ULONG_MAX) || /* first round */
396 (cur_max > last_max)) {
397 last_max = cur_max;
399 ndx = k;
403 break;
404 case PROXY_BALANCE_FAIR:
405 /* fair balancing */
406 if (debug) {
407 log_error_write(srv, __FILE__, __LINE__, "s",
408 "proxy - used fair balancing");
411 for (k = 0, ndx = -1, max_usage = INT_MAX; k < extension->value->used; k++) {
412 data_proxy *host = (data_proxy *)extension->value->data[k];
414 if (host->is_disabled) continue;
416 if (host->usage < max_usage) {
417 max_usage = host->usage;
419 ndx = k;
423 break;
424 case PROXY_BALANCE_RR: {
425 data_proxy *host;
427 /* round robin */
428 if (debug) {
429 log_error_write(srv, __FILE__, __LINE__, "s",
430 "proxy - used round-robin balancing");
433 /* just to be sure */
434 force_assert(extension->value->used < INT_MAX);
436 host = (data_proxy *)extension->value->data[0];
438 /* Use last_used_ndx from first host in list */
439 k = host->last_used_ndx;
440 ndx = k + 1; /* use next host after the last one */
441 if (ndx < 0) ndx = 0;
443 /* Search first active host after last_used_ndx */
444 while ( ndx < (int) extension->value->used
445 && (host = (data_proxy *)extension->value->data[ndx])->is_disabled ) ndx++;
447 if (ndx >= (int) extension->value->used) {
448 /* didn't found a higher id, wrap to the start */
449 for (ndx = 0; ndx <= (int) k; ndx++) {
450 host = (data_proxy *)extension->value->data[ndx];
451 if (!host->is_disabled) break;
454 /* No active host found */
455 if (host->is_disabled) ndx = -1;
458 /* Save new index for next round */
459 ((data_proxy *)extension->value->data[0])->last_used_ndx = ndx;
461 break;
463 default:
464 break;
467 /* found a server */
468 if (ndx != -1) {
469 data_proxy *host = (data_proxy *)extension->value->data[ndx];
471 if (debug) {
472 log_error_write(srv, __FILE__, __LINE__, "sbd",
473 "proxy - found a host",
474 host->host, host->port);
477 host->usage++;
478 return host;
479 } else {
480 /* no handler found */
481 con->http_status = 503; /* Service Unavailable */
482 con->mode = DIRECT;
484 log_error_write(srv, __FILE__, __LINE__, "sb",
485 "no proxy-handler found for:",
486 con->uri.path);
488 return NULL;
492 static void proxy_connection_close(server *srv, handler_ctx *hctx) {
493 plugin_data *p;
494 connection *con;
496 p = hctx->plugin_data;
497 con = hctx->remote_conn;
499 proxy_backend_close(srv, hctx);
500 handler_ctx_free(hctx);
501 con->plugin_ctx[p->id] = NULL;
503 /* finish response (if not already con->file_started, con->file_finished) */
504 if (con->mode == p->id) {
505 http_response_backend_done(srv, con);
509 static handler_t proxy_reconnect(server *srv, handler_ctx *hctx) {
510 proxy_backend_close(srv, hctx);
512 hctx->host = mod_proxy_extension_host_get(srv, hctx->remote_conn, hctx->ext, hctx->conf.balance, (int)hctx->conf.debug);
513 if (NULL == hctx->host) return HANDLER_FINISHED;
515 hctx->state = PROXY_STATE_INIT;
516 return HANDLER_COMEBACK;
519 static int proxy_establish_connection(server *srv, handler_ctx *hctx) {
520 struct sockaddr *proxy_addr;
521 struct sockaddr_in proxy_addr_in;
522 #if defined(HAVE_SYS_UN_H)
523 struct sockaddr_un proxy_addr_un;
524 #endif
525 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
526 struct sockaddr_in6 proxy_addr_in6;
527 #endif
528 socklen_t servlen;
530 data_proxy *host= hctx->host;
531 int proxy_fd = hctx->fd;
534 #if defined(HAVE_SYS_UN_H)
535 if (strstr(host->host->ptr, "/")) {
536 if (buffer_string_length(host->host) + 1 > sizeof(proxy_addr_un.sun_path)) {
537 log_error_write(srv, __FILE__, __LINE__, "sB",
538 "ERROR: Unix Domain socket filename too long:",
539 host->host);
540 return -1;
543 memset(&proxy_addr_un, 0, sizeof(proxy_addr_un));
544 proxy_addr_un.sun_family = AF_UNIX;
545 memcpy(proxy_addr_un.sun_path, host->host->ptr, buffer_string_length(host->host) + 1);
546 servlen = sizeof(proxy_addr_un);
547 proxy_addr = (struct sockaddr *) &proxy_addr_un;
548 } else
549 #endif
550 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
551 if (strstr(host->host->ptr, ":")) {
552 memset(&proxy_addr_in6, 0, sizeof(proxy_addr_in6));
553 proxy_addr_in6.sin6_family = AF_INET6;
554 inet_pton(AF_INET6, host->host->ptr, (char *) &proxy_addr_in6.sin6_addr);
555 proxy_addr_in6.sin6_port = htons(host->port);
556 servlen = sizeof(proxy_addr_in6);
557 proxy_addr = (struct sockaddr *) &proxy_addr_in6;
558 } else
559 #endif
561 memset(&proxy_addr_in, 0, sizeof(proxy_addr_in));
562 proxy_addr_in.sin_family = AF_INET;
563 proxy_addr_in.sin_addr.s_addr = inet_addr(host->host->ptr);
564 proxy_addr_in.sin_port = htons(host->port);
565 servlen = sizeof(proxy_addr_in);
566 proxy_addr = (struct sockaddr *) &proxy_addr_in;
570 if (-1 == connect(proxy_fd, proxy_addr, servlen)) {
571 if (errno == EINPROGRESS || errno == EALREADY) {
572 if (hctx->conf.debug) {
573 log_error_write(srv, __FILE__, __LINE__, "sd",
574 "connect delayed:", proxy_fd);
577 return 1;
578 } else {
580 log_error_write(srv, __FILE__, __LINE__, "sdsd",
581 "connect failed:", proxy_fd, strerror(errno), errno);
583 return -1;
586 if (hctx->conf.debug) {
587 log_error_write(srv, __FILE__, __LINE__, "sd",
588 "connect succeeded: ", proxy_fd);
591 return 0;
594 static void proxy_set_header(connection *con, const char *key, const char *value) {
595 data_string *ds_dst;
597 if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
598 ds_dst = data_string_init();
601 buffer_copy_string(ds_dst->key, key);
602 buffer_copy_string(ds_dst->value, value);
603 array_insert_unique(con->request.headers, (data_unset *)ds_dst);
606 static void proxy_append_header(connection *con, const char *key, const char *value) {
607 data_string *ds_dst;
609 if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
610 ds_dst = data_string_init();
613 buffer_copy_string(ds_dst->key, key);
614 buffer_append_string(ds_dst->value, value);
615 array_insert_unique(con->request.headers, (data_unset *)ds_dst);
619 static int proxy_create_env(server *srv, handler_ctx *hctx) {
620 size_t i;
622 connection *con = hctx->remote_conn;
623 buffer *b;
624 int replace_http_host = 0;
626 /* build header */
628 b = buffer_init();
630 /* request line */
631 buffer_copy_string(b, get_http_method_name(con->request.http_method));
632 buffer_append_string_len(b, CONST_STR_LEN(" "));
634 buffer_append_string_buffer(b, con->request.uri);
635 buffer_append_string_len(b, CONST_STR_LEN(" HTTP/1.0\r\n"));
636 if (hctx->conf.replace_http_host && !buffer_string_is_empty(hctx->host->key)) {
637 replace_http_host = 1;
638 if (hctx->conf.debug > 1) {
639 log_error_write(srv, __FILE__, __LINE__, "SBS",
640 "proxy - using \"", hctx->host->key, "\" as HTTP Host");
642 buffer_append_string_len(b, CONST_STR_LEN("Host: "));
643 buffer_append_string_buffer(b, hctx->host->key);
644 buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
647 proxy_append_header(con, "X-Forwarded-For", (char *)inet_ntop_cache_get_ip(srv, &(con->dst_addr)));
648 /* http_host is NOT is just a pointer to a buffer
649 * which is NULL if it is not set */
650 if (!buffer_string_is_empty(con->request.http_host)) {
651 proxy_set_header(con, "X-Host", con->request.http_host->ptr);
653 proxy_set_header(con, "X-Forwarded-Proto", con->uri.scheme->ptr);
655 /* request header */
656 for (i = 0; i < con->request.headers->used; i++) {
657 data_string *ds;
659 ds = (data_string *)con->request.headers->data[i];
661 if (!buffer_is_empty(ds->value) && !buffer_is_empty(ds->key)) {
662 if (replace_http_host &&
663 buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Host"))) continue;
664 if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Connection"))) continue;
665 if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Proxy-Connection"))) continue;
666 /* Do not emit HTTP_PROXY in environment.
667 * Some executables use HTTP_PROXY to configure
668 * outgoing proxy. See also https://httpoxy.org/ */
669 if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Proxy"))) continue;
671 buffer_append_string_buffer(b, ds->key);
672 buffer_append_string_len(b, CONST_STR_LEN(": "));
673 buffer_append_string_buffer(b, ds->value);
674 buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
678 buffer_append_string_len(b, CONST_STR_LEN("Connection: close\r\n\r\n"));
680 hctx->wb_reqlen = buffer_string_length(b);
681 chunkqueue_append_buffer(hctx->wb, b);
682 buffer_free(b);
684 /* body */
686 if (con->request.content_length) {
687 chunkqueue_append_chunkqueue(hctx->wb, con->request_content_queue);
688 hctx->wb_reqlen += con->request.content_length;/* (eventual) total request size */
691 return 0;
694 static int proxy_set_state(server *srv, handler_ctx *hctx, proxy_connection_state_t state) {
695 hctx->state = state;
696 hctx->state_timestamp = srv->cur_ts;
698 return 0;
702 static int proxy_response_parse(server *srv, connection *con, plugin_data *p, buffer *in) {
703 char *s, *ns;
704 int http_response_status = -1;
706 UNUSED(srv);
708 /* [\r]\n -> [\0]\0 */
710 buffer_copy_buffer(p->parse_response, in);
712 for (s = p->parse_response->ptr; NULL != (ns = strchr(s, '\n')); s = ns + 1) {
713 char *key, *value;
714 int key_len;
715 data_string *ds;
716 int copy_header;
718 ns[0] = '\0';
719 if (s != ns && ns[-1] == '\r') ns[-1] = '\0';
721 if (-1 == http_response_status) {
722 /* The first line of a Response message is the Status-Line */
724 for (key=s; *key && *key != ' '; key++);
726 if (*key) {
727 http_response_status = (int) strtol(key, NULL, 10);
728 if (http_response_status < 100 || http_response_status >= 1000) http_response_status = 502;
729 } else {
730 http_response_status = 502;
733 con->http_status = http_response_status;
734 con->parsed_response |= HTTP_STATUS;
735 continue;
738 if (NULL == (value = strchr(s, ':'))) {
739 /* now we expect: "<key>: <value>\n" */
741 continue;
744 key = s;
745 key_len = value - key;
747 value++;
748 /* strip WS */
749 while (*value == ' ' || *value == '\t') value++;
751 copy_header = 1;
753 switch(key_len) {
754 case 4:
755 if (0 == strncasecmp(key, "Date", key_len)) {
756 con->parsed_response |= HTTP_DATE;
758 break;
759 case 8:
760 if (0 == strncasecmp(key, "Location", key_len)) {
761 con->parsed_response |= HTTP_LOCATION;
763 break;
764 case 10:
765 if (0 == strncasecmp(key, "Connection", key_len)) {
766 copy_header = 0;
768 break;
769 case 14:
770 if (0 == strncasecmp(key, "Content-Length", key_len)) {
771 con->response.content_length = strtoul(value, NULL, 10);
772 con->parsed_response |= HTTP_CONTENT_LENGTH;
774 break;
775 default:
776 break;
779 if (copy_header) {
780 if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
781 ds = data_response_init();
783 buffer_copy_string_len(ds->key, key, key_len);
784 buffer_copy_string(ds->value, value);
786 array_insert_unique(con->response.headers, (data_unset *)ds);
790 return 0;
794 static int proxy_demux_response(server *srv, handler_ctx *hctx) {
795 int fin = 0;
796 int b;
797 ssize_t r;
799 plugin_data *p = hctx->plugin_data;
800 connection *con = hctx->remote_conn;
801 int proxy_fd = hctx->fd;
803 /* check how much we have to read */
804 #if !defined(_WIN32) && !defined(__CYGWIN__)
805 if (ioctl(hctx->fd, FIONREAD, &b)) {
806 if (errno == EAGAIN) {
807 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
808 return 0;
810 log_error_write(srv, __FILE__, __LINE__, "sd",
811 "ioctl failed: ",
812 proxy_fd);
813 return -1;
815 #else
816 b = 4096;
817 #endif
820 if (hctx->conf.debug) {
821 log_error_write(srv, __FILE__, __LINE__, "sd",
822 "proxy - have to read:", b);
825 if (b > 0) {
826 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)) {
827 off_t cqlen = chunkqueue_length(con->write_queue);
828 if (cqlen + b > 65536 - 4096) {
829 if (!con->is_writable) {
830 /*(defer removal of FDEVENT_IN interest since
831 * connection_state_machine() might be able to send data
832 * immediately, unless !con->is_writable, where
833 * connection_state_machine() might not loop back to call
834 * mod_proxy_handle_subrequest())*/
835 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
837 if (cqlen >= 65536-1) return 0;
838 b = 65536 - 1 - (int)cqlen;
842 buffer_string_prepare_append(hctx->response, b);
844 if (-1 == (r = read(hctx->fd, hctx->response->ptr + buffer_string_length(hctx->response), buffer_string_space(hctx->response)))) {
845 if (errno == EAGAIN) {
846 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
847 return 0;
849 log_error_write(srv, __FILE__, __LINE__, "sds",
850 "unexpected end-of-file (perhaps the proxy process died):",
851 proxy_fd, strerror(errno));
852 return -1;
855 #if defined(_WIN32) || defined(__CYGWIN__)
856 if (0 == r) return 1; /* fin */
857 #endif
859 /* this should be catched by the b > 0 above */
860 force_assert(r);
862 buffer_commit(hctx->response, r);
864 #if 0
865 log_error_write(srv, __FILE__, __LINE__, "sdsbs",
866 "demux: Response buffer len", hctx->response->used, ":", hctx->response, ":");
867 #endif
869 if (0 == con->got_response) {
870 con->got_response = 1;
871 buffer_string_prepare_copy(hctx->response_header, 1023);
874 if (0 == con->file_started) {
875 char *c;
877 /* search for the \r\n\r\n in the string */
878 if (NULL != (c = buffer_search_string_len(hctx->response, CONST_STR_LEN("\r\n\r\n")))) {
879 size_t hlen = c - hctx->response->ptr + 4;
880 size_t blen = buffer_string_length(hctx->response) - hlen;
881 /* found */
883 buffer_append_string_len(hctx->response_header, hctx->response->ptr, hlen);
884 #if 0
885 log_error_write(srv, __FILE__, __LINE__, "sb", "Header:", hctx->response_header);
886 #endif
887 /* parse the response header */
888 proxy_response_parse(srv, con, p, hctx->response_header);
890 con->file_started = 1;
891 if (blen > 0) {
892 if (0 != http_chunk_append_mem(srv, con, c + 4, blen)) {
893 /* error writing to tempfile;
894 * truncate response or send 500 if nothing sent yet */
895 fin = 1;
896 con->file_started = 0;
899 buffer_reset(hctx->response);
900 } else {
901 /* no luck, no header found */
902 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
903 if (buffer_string_length(hctx->response) > MAX_HTTP_REQUEST_HEADER) {
904 log_error_write(srv, __FILE__, __LINE__, "sb", "response headers too large for", con->uri.path);
905 con->http_status = 502; /* Bad Gateway */
906 con->mode = DIRECT;
907 fin = 1;
910 } else {
911 if (0 != http_chunk_append_buffer(srv, con, hctx->response)) {
912 /* error writing to tempfile;
913 * truncate response or send 500 if nothing sent yet */
914 fin = 1;
916 buffer_reset(hctx->response);
918 } else {
919 /* reading from upstream done */
920 fin = 1;
923 return fin;
927 static handler_t proxy_write_request(server *srv, handler_ctx *hctx) {
928 data_proxy *host= hctx->host;
929 connection *con = hctx->remote_conn;
931 int ret;
933 switch(hctx->state) {
934 case PROXY_STATE_INIT:
935 #if defined(HAVE_SYS_UN_H)
936 if (strstr(host->host->ptr,"/")) {
937 if (-1 == (hctx->fd = fdevent_socket_nb_cloexec(AF_UNIX, SOCK_STREAM, 0))) {
938 log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
939 return HANDLER_ERROR;
941 } else
942 #endif
943 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
944 if (strstr(host->host->ptr,":")) {
945 if (-1 == (hctx->fd = fdevent_socket_nb_cloexec(AF_INET6, SOCK_STREAM, 0))) {
946 log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
947 return HANDLER_ERROR;
949 } else
950 #endif
952 if (-1 == (hctx->fd = fdevent_socket_nb_cloexec(AF_INET, SOCK_STREAM, 0))) {
953 log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
954 return HANDLER_ERROR;
957 hctx->fde_ndx = -1;
959 srv->cur_fds++;
961 fdevent_register(srv->ev, hctx->fd, proxy_handle_fdevent, hctx);
963 if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
964 log_error_write(srv, __FILE__, __LINE__, "ss", "fcntl failed: ", strerror(errno));
966 return HANDLER_ERROR;
969 /* fall through */
970 case PROXY_STATE_CONNECT:
971 if (hctx->state == PROXY_STATE_INIT) {
972 switch (proxy_establish_connection(srv, hctx)) {
973 case 1:
974 proxy_set_state(srv, hctx, PROXY_STATE_CONNECT);
976 /* connection is in progress, wait for an event and call getsockopt() below */
978 fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
980 return HANDLER_WAIT_FOR_EVENT;
981 case -1:
982 /* if ECONNREFUSED choose another connection */
983 hctx->fde_ndx = -1;
985 return HANDLER_ERROR;
986 default:
987 /* everything is ok, go on */
988 break;
990 } else {
991 int socket_error;
992 socklen_t socket_error_len = sizeof(socket_error);
994 /* try to finish the connect() */
995 if (0 != getsockopt(hctx->fd, SOL_SOCKET, SO_ERROR, &socket_error, &socket_error_len)) {
996 log_error_write(srv, __FILE__, __LINE__, "ss",
997 "getsockopt failed:", strerror(errno));
999 return HANDLER_ERROR;
1001 if (socket_error != 0) {
1002 log_error_write(srv, __FILE__, __LINE__, "ss",
1003 "establishing connection failed:", strerror(socket_error),
1004 "port:", hctx->host->port);
1006 return HANDLER_ERROR;
1008 if (hctx->conf.debug) {
1009 log_error_write(srv, __FILE__, __LINE__, "s", "proxy - connect - delayed success");
1013 /* ok, we have the connection */
1015 proxy_set_state(srv, hctx, PROXY_STATE_PREPARE_WRITE);
1016 /* fall through */
1017 case PROXY_STATE_PREPARE_WRITE:
1018 proxy_create_env(srv, hctx);
1020 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
1021 proxy_set_state(srv, hctx, PROXY_STATE_WRITE);
1023 /* fall through */
1024 case PROXY_STATE_WRITE:;
1025 ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
1027 chunkqueue_remove_finished_chunks(hctx->wb);
1029 if (-1 == ret) { /* error on our side */
1030 log_error_write(srv, __FILE__, __LINE__, "ssd", "write failed:", strerror(errno), errno);
1032 return HANDLER_ERROR;
1033 } else if (-2 == ret) { /* remote close */
1034 log_error_write(srv, __FILE__, __LINE__, "ssd", "write failed, remote connection close:", strerror(errno), errno);
1036 return HANDLER_ERROR;
1039 if (hctx->wb->bytes_out == hctx->wb_reqlen) {
1040 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
1041 proxy_set_state(srv, hctx, PROXY_STATE_READ);
1042 } else {
1043 off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
1044 if (hctx->wb->bytes_in < hctx->wb_reqlen && wblen < 65536 - 16384) {
1045 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
1046 if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
1047 con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
1048 con->is_readable = 1; /* trigger optimistic read from client */
1051 if (0 == wblen) {
1052 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
1053 } else {
1054 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
1058 return HANDLER_WAIT_FOR_EVENT;
1059 case PROXY_STATE_READ:
1060 /* waiting for a response */
1061 return HANDLER_WAIT_FOR_EVENT;
1062 default:
1063 log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
1064 return HANDLER_ERROR;
1068 #define PATCH(x) \
1069 p->conf.x = s->x;
1070 static int mod_proxy_patch_connection(server *srv, connection *con, plugin_data *p) {
1071 size_t i, j;
1072 plugin_config *s = p->config_storage[0];
1074 PATCH(extensions);
1075 PATCH(debug);
1076 PATCH(balance);
1077 PATCH(replace_http_host);
1079 /* skip the first, the global context */
1080 for (i = 1; i < srv->config_context->used; i++) {
1081 data_config *dc = (data_config *)srv->config_context->data[i];
1082 s = p->config_storage[i];
1084 /* condition didn't match */
1085 if (!config_check_cond(srv, con, dc)) continue;
1087 /* merge config */
1088 for (j = 0; j < dc->value->used; j++) {
1089 data_unset *du = dc->value->data[j];
1091 if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.server"))) {
1092 PATCH(extensions);
1093 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.debug"))) {
1094 PATCH(debug);
1095 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.balance"))) {
1096 PATCH(balance);
1097 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.replace-http-host"))) {
1098 PATCH(replace_http_host);
1103 return 0;
1105 #undef PATCH
1107 static handler_t proxy_send_request(server *srv, handler_ctx *hctx) {
1108 /* ok, create the request */
1109 handler_t rc = proxy_write_request(srv, hctx);
1110 if (HANDLER_ERROR != rc) {
1111 return rc;
1112 } else {
1113 data_proxy *host = hctx->host;
1114 log_error_write(srv, __FILE__, __LINE__, "sbdd", "proxy-server disabled:",
1115 host->host,
1116 host->port,
1117 hctx->fd);
1119 /* disable this server */
1120 host->is_disabled = 1;
1121 host->disable_ts = srv->cur_ts;
1123 /* reset the environment and restart the sub-request */
1124 return proxy_reconnect(srv, hctx);
1129 static handler_t proxy_recv_response(server *srv, handler_ctx *hctx);
1132 SUBREQUEST_FUNC(mod_proxy_handle_subrequest) {
1133 plugin_data *p = p_d;
1135 handler_ctx *hctx = con->plugin_ctx[p->id];
1137 if (NULL == hctx) return HANDLER_GO_ON;
1139 /* not my job */
1140 if (con->mode != p->id) return HANDLER_GO_ON;
1142 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
1143 && con->file_started) {
1144 if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
1145 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
1146 } else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
1147 /* optimistic read from backend, which might re-enable FDEVENT_IN */
1148 handler_t rc = proxy_recv_response(srv, hctx); /*(might invalidate hctx)*/
1149 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
1153 if (0 == hctx->wb->bytes_in
1154 ? con->state == CON_STATE_READ_POST
1155 : hctx->wb->bytes_in < hctx->wb_reqlen) {
1156 /*(64k - 4k to attempt to avoid temporary files
1157 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
1158 if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096
1159 && (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN)){
1160 con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
1161 if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
1162 } else {
1163 handler_t r = connection_handle_read_post_state(srv, con);
1164 chunkqueue *req_cq = con->request_content_queue;
1165 if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
1166 chunkqueue_append_chunkqueue(hctx->wb, req_cq);
1167 if (fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_OUT) {
1168 return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
1171 if (r != HANDLER_GO_ON) return r;
1173 /* mod_proxy sends HTTP/1.0 request and ideally should send
1174 * Content-Length with request if request body is present, so
1175 * send 411 Length Required if Content-Length missing.
1176 * (occurs here if client sends Transfer-Encoding: chunked
1177 * and module is flagged to stream request body to backend) */
1178 if (-1 == con->request.content_length) {
1179 return connection_handle_read_post_error(srv, con, 411);
1184 return ((0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
1185 && hctx->state != PROXY_STATE_CONNECT)
1186 ? proxy_send_request(srv, hctx)
1187 : HANDLER_WAIT_FOR_EVENT;
1191 static handler_t proxy_recv_response(server *srv, handler_ctx *hctx) {
1193 switch (proxy_demux_response(srv, hctx)) {
1194 case 0:
1195 break;
1196 case -1:
1197 http_response_backend_error(srv, hctx->remote_conn);
1198 /* fall through */
1199 case 1:
1200 /* we are done */
1201 proxy_connection_close(srv, hctx);
1203 return HANDLER_FINISHED;
1206 return HANDLER_GO_ON;
1210 static handler_t proxy_handle_fdevent(server *srv, void *ctx, int revents) {
1211 handler_ctx *hctx = ctx;
1212 connection *con = hctx->remote_conn;
1214 joblist_append(srv, con);
1216 if (revents & FDEVENT_IN) {
1217 handler_t rc = proxy_recv_response(srv,hctx);/*(might invalidate hctx)*/
1218 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
1221 if (revents & FDEVENT_OUT) {
1222 return proxy_send_request(srv, hctx); /*(might invalidate hctx)*/
1225 /* perhaps this issue is already handled */
1226 if (revents & FDEVENT_HUP) {
1227 if (hctx->state == PROXY_STATE_CONNECT) {
1228 /* connect() -> EINPROGRESS -> HUP */
1229 proxy_send_request(srv, hctx); /*(might invalidate hctx)*/
1230 } else if (con->file_started) {
1231 /* drain any remaining data from kernel pipe buffers
1232 * even if (con->conf.stream_response_body
1233 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
1234 * since event loop will spin on fd FDEVENT_HUP event
1235 * until unregistered. */
1236 handler_t rc;
1237 do {
1238 rc = proxy_recv_response(srv,hctx);/*(might invalidate hctx)*/
1239 } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
1240 return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
1241 } else {
1242 proxy_connection_close(srv, hctx);
1244 } else if (revents & FDEVENT_ERR) {
1245 log_error_write(srv, __FILE__, __LINE__, "sd", "proxy-FDEVENT_ERR, but no HUP", revents);
1247 http_response_backend_error(srv, con);
1248 proxy_connection_close(srv, hctx);
1251 return HANDLER_FINISHED;
1254 static handler_t mod_proxy_check_extension(server *srv, connection *con, void *p_d) {
1255 plugin_data *p = p_d;
1256 size_t s_len;
1257 size_t k;
1258 buffer *fn;
1259 data_array *extension = NULL;
1260 data_proxy *host;
1262 if (con->mode != DIRECT) return HANDLER_GO_ON;
1264 /* Possibly, we processed already this request */
1265 if (con->file_started == 1) return HANDLER_GO_ON;
1267 mod_proxy_patch_connection(srv, con, p);
1269 fn = con->uri.path;
1270 if (buffer_string_is_empty(fn)) return HANDLER_ERROR;
1271 s_len = buffer_string_length(fn);
1273 /* check if extension matches */
1274 for (k = 0; k < p->conf.extensions->used; k++) {
1275 data_array *ext = NULL;
1276 size_t ct_len;
1278 ext = (data_array *)p->conf.extensions->data[k];
1280 if (buffer_is_empty(ext->key)) continue;
1282 ct_len = buffer_string_length(ext->key);
1284 if (s_len < ct_len) continue;
1286 /* check extension in the form "/proxy_pattern" */
1287 if (*(ext->key->ptr) == '/') {
1288 if (strncmp(fn->ptr, ext->key->ptr, ct_len) == 0) {
1289 extension = ext;
1290 break;
1292 } else if (0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len)) {
1293 /* check extension in the form ".fcg" */
1294 extension = ext;
1295 break;
1299 if (NULL == extension) {
1300 return HANDLER_GO_ON;
1303 host = mod_proxy_extension_host_get(srv, con, extension, p->conf.balance, (int)p->conf.debug);
1304 if (NULL == host) {
1305 return HANDLER_FINISHED;
1308 /* found a server */
1312 * if check-local is disabled, use the uri.path handler
1316 /* init handler-context */
1317 handler_ctx *hctx;
1318 hctx = handler_ctx_init();
1320 hctx->remote_conn = con;
1321 hctx->plugin_data = p;
1322 hctx->host = host;
1323 hctx->ext = extension;
1325 hctx->conf.balance = p->conf.balance;
1326 hctx->conf.debug = p->conf.debug;
1327 hctx->conf.replace_http_host = p->conf.replace_http_host;
1329 con->plugin_ctx[p->id] = hctx;
1330 con->mode = p->id;
1332 if (p->conf.debug) {
1333 log_error_write(srv, __FILE__, __LINE__, "sbd",
1334 "proxy - found a host",
1335 host->host, host->port);
1338 return HANDLER_GO_ON;
1342 static handler_t mod_proxy_connection_reset(server *srv, connection *con, void *p_d) {
1343 plugin_data *p = p_d;
1344 handler_ctx *hctx = con->plugin_ctx[p->id];
1345 if (hctx) proxy_connection_close(srv, hctx);
1347 return HANDLER_GO_ON;
1352 * the trigger re-enables the disabled connections after the timeout is over
1354 * */
1356 TRIGGER_FUNC(mod_proxy_trigger) {
1357 plugin_data *p = p_d;
1359 if (p->config_storage) {
1360 size_t i, n, k;
1361 for (i = 0; i < srv->config_context->used; i++) {
1362 plugin_config *s = p->config_storage[i];
1364 if (!s) continue;
1366 /* get the extensions for all configs */
1368 for (k = 0; k < s->extensions->used; k++) {
1369 data_array *extension = (data_array *)s->extensions->data[k];
1371 /* get all hosts */
1372 for (n = 0; n < extension->value->used; n++) {
1373 data_proxy *host = (data_proxy *)extension->value->data[n];
1375 if (!host->is_disabled ||
1376 srv->cur_ts - host->disable_ts < 5) continue;
1378 log_error_write(srv, __FILE__, __LINE__, "sbd",
1379 "proxy - re-enabled:",
1380 host->host, host->port);
1382 host->is_disabled = 0;
1388 return HANDLER_GO_ON;
1392 int mod_proxy_plugin_init(plugin *p);
1393 int mod_proxy_plugin_init(plugin *p) {
1394 p->version = LIGHTTPD_VERSION_ID;
1395 p->name = buffer_init_string("proxy");
1397 p->init = mod_proxy_init;
1398 p->cleanup = mod_proxy_free;
1399 p->set_defaults = mod_proxy_set_defaults;
1400 p->connection_reset = mod_proxy_connection_reset; /* end of req-resp cycle */
1401 p->handle_connection_close = mod_proxy_connection_reset; /* end of client connection */
1402 p->handle_uri_clean = mod_proxy_check_extension;
1403 p->handle_subrequest = mod_proxy_handle_subrequest;
1404 p->handle_trigger = mod_proxy_trigger;
1406 p->data = NULL;
1408 return 0;