summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn Strauss <gstrauss@gluelogic.com>2017-07-14 01:29:18 -0400
committerGlenn Strauss <gstrauss@gluelogic.com>2017-07-23 19:02:00 -0400
commit45b970e69b144c7eaa5508fbb55728cb7c731caf (patch)
treeea5f83a2c4f56910402a2e54fd1a62f721af0fb7
parent81b8fffd311355cb65ab335c760d2e63471eb782 (diff)
downloadlighttpd1.4-45b970e69b144c7eaa5508fbb55728cb7c731caf.tar.gz
lighttpd1.4-45b970e69b144c7eaa5508fbb55728cb7c731caf.zip
[core] shared code for socket backends
common codebase for socket backends, based off mod_fastcgi with some features added for mod_proxy (mostly intended to reduce code duplication and enhance code isolation) mod_fastcgi and mod_scgi can now use fastcgi.balance and scgi.balance for similar behavior as proxy.balance, but the balancing is per-host and not per-proc. proxy.balance is also per-host and not per-proc. mod_proxy and mod_scgi can now use proxy.map-extensions and scgi.map-extensions, similar to fastcgi.map-extensions. mod_fastcgi behavior change (affects only mod_status): - statistics tags have been renamed from "fastcgi.*" to "gw.*" "fastcgi.backend.*" -> "gw.backend.*" "fastcgi.active-requests" -> "gw.active-requests" ("fastcgi.requests" remains "fastcgi.requests") ("proxy.requests" is new) ("scgi.requests" is new) mod_scgi behavior change (likely minor): - removed scgi_proclist_sort_down() and scgi_proclist_sort_up(). procs now chosen based on load as measured by num socket connnections Note: modules using gw_backend.[ch] are currently still independent modules. If it had been written as a single module with fastcgi, scgi, proxy implementations, then there would have been a chance of breaking some existing user configurations where module ordering made a difference for which module handled a given request, though for most people, this would have made no difference. Details about mod_fastcgi code transformations: unsigned int debug -> int debug fastcgi_env member removed from plugin_config renamed "fcgi" and "fastcgi" to "gw", and "FCGI" to "GW" reorganize routines for high-level and lower-level interfaces some lower-level internal interfaces changed to use host,proc,debug args rather than knowing about higher-level (app) hctx and plugin_data tabs->spaces and reformatting
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/Makefile.am4
-rw-r--r--src/SConscript2
-rw-r--r--src/gw_backend.c2447
-rw-r--r--src/gw_backend.h343
-rw-r--r--src/http-header-glue.c3
-rw-r--r--src/mod_cgi.c20
-rw-r--r--src/mod_fastcgi.c2494
-rw-r--r--src/mod_proxy.c1102
-rw-r--r--src/mod_scgi.c2315
-rw-r--r--src/response.h3
11 files changed, 3015 insertions, 5720 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c6622894..36ce5efa 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -542,7 +542,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set(COMMON_SRC
base64.c buffer.c log.c
keyvalue.c chunk.c
- http_chunk.c stream.c fdevent.c
+ http_chunk.c stream.c fdevent.c gw_backend.c
stat_cache.c plugin.c joblist.c etag.c array.c
data_string.c data_array.c
data_integer.c md5.c
diff --git a/src/Makefile.am b/src/Makefile.am
index a4707108..892dcf2d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -57,7 +57,7 @@ CLEANFILES = versionstamp.h versionstamp.h.tmp lemon$(BUILD_EXEEXT)
common_src=base64.c buffer.c log.c \
keyvalue.c chunk.c \
- http_chunk.c stream.c fdevent.c \
+ http_chunk.c stream.c fdevent.c gw_backend.c \
stat_cache.c plugin.c joblist.c etag.c array.c \
data_string.c data_array.c \
data_integer.c md5.c \
@@ -375,7 +375,7 @@ hdr = server.h base64.h buffer.h network.h log.h keyvalue.h \
response.h request.h fastcgi.h chunk.h \
first.h settings.h http_chunk.h \
md5.h http_auth.h http_vhostdb.h stream.h \
- fdevent.h connections.h base.h stat_cache.h \
+ fdevent.h gw_backend.h connections.h base.h stat_cache.h \
plugin.h \
etag.h joblist.h array.h vector.h crc32.h \
network_backends.h configfile.h \
diff --git a/src/SConscript b/src/SConscript
index 0667b79c..da81225a 100644
--- a/src/SConscript
+++ b/src/SConscript
@@ -51,7 +51,7 @@ def GatherLibs(env, *libs):
common_src = Split("base64.c buffer.c log.c \
keyvalue.c chunk.c \
- http_chunk.c stream.c fdevent.c \
+ http_chunk.c stream.c fdevent.c gw_backend.c \
stat_cache.c plugin.c joblist.c etag.c array.c \
data_string.c data_array.c \
data_integer.c md5.c \
diff --git a/src/gw_backend.c b/src/gw_backend.c
new file mode 100644
index 00000000..0037ed14
--- /dev/null
+++ b/src/gw_backend.c
@@ -0,0 +1,2447 @@
+#include "first.h"
+
+#include "gw_backend.h"
+
+#include <sys/types.h>
+#include "sys-socket.h"
+#ifdef HAVE_SYS_UIO_H
+#include <sys/uio.h>
+#endif
+#ifdef HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "array.h"
+#include "buffer.h"
+#include "crc32.h"
+#include "fdevent.h"
+#include "inet_ntop_cache.h"
+#include "log.h"
+
+
+
+
+#include "status_counter.h"
+
+static data_integer * gw_status_get_di(server *srv, gw_host *host, gw_proc *proc, const char *tag, size_t len) {
+ buffer *b = srv->tmp_buf;
+ buffer_copy_string_len(b, CONST_STR_LEN("gw.backend."));
+ buffer_append_string_buffer(b, host->id);
+ if (proc) {
+ buffer_append_string_len(b, CONST_STR_LEN("."));
+ buffer_append_int(b, proc->id);
+ }
+ buffer_append_string_len(b, tag, len);
+ return status_counter_get_counter(srv, CONST_BUF_LEN(b));
+}
+
+static void gw_proc_tag_inc(server *srv, gw_host *host, gw_proc *proc, const char *tag, size_t len) {
+ data_integer *di = gw_status_get_di(srv, host, proc, tag, len);
+ ++di->value;
+}
+
+static void gw_proc_load_inc(server *srv, gw_host *host, gw_proc *proc) {
+ data_integer *di = gw_status_get_di(srv,host,proc,CONST_STR_LEN(".load"));
+ di->value = ++proc->load;
+
+ status_counter_inc(srv, CONST_STR_LEN("gw.active-requests"));
+}
+
+static void gw_proc_load_dec(server *srv, gw_host *host, gw_proc *proc) {
+ data_integer *di = gw_status_get_di(srv,host,proc,CONST_STR_LEN(".load"));
+ di->value = --proc->load;
+
+ status_counter_dec(srv, CONST_STR_LEN("gw.active-requests"));
+}
+
+static void gw_host_assign(server *srv, gw_host *host) {
+ data_integer *di = gw_status_get_di(srv,host,NULL,CONST_STR_LEN(".load"));
+ di->value = ++host->load;
+}
+
+static void gw_host_reset(server *srv, gw_host *host) {
+ data_integer *di = gw_status_get_di(srv,host,NULL,CONST_STR_LEN(".load"));
+ di->value = --host->load;
+}
+
+static int gw_status_init(server *srv, gw_host *host, gw_proc *proc) {
+ gw_status_get_di(srv, host, proc, CONST_STR_LEN(".disabled"))->value = 0;
+ gw_status_get_di(srv, host, proc, CONST_STR_LEN(".died"))->value = 0;
+ gw_status_get_di(srv, host, proc, CONST_STR_LEN(".overloaded"))->value = 0;
+ gw_status_get_di(srv, host, proc, CONST_STR_LEN(".connected"))->value = 0;
+ gw_status_get_di(srv, host, proc, CONST_STR_LEN(".load"))->value = 0;
+
+ gw_status_get_di(srv, host, NULL, CONST_STR_LEN(".load"))->value = 0;
+
+ return 0;
+}
+
+
+
+
+static void gw_proc_set_state(gw_host *host, gw_proc *proc, int state) {
+ if ((int)proc->state == state) return;
+ if (proc->state == PROC_STATE_RUNNING) {
+ --host->active_procs;
+ } else if (state == PROC_STATE_RUNNING) {
+ ++host->active_procs;
+ }
+ proc->state = state;
+}
+
+
+static gw_proc *gw_proc_init(void) {
+ gw_proc *f = calloc(1, sizeof(*f));
+ force_assert(f);
+
+ f->unixsocket = buffer_init();
+ f->connection_name = buffer_init();
+
+ f->prev = NULL;
+ f->next = NULL;
+ f->state = PROC_STATE_DIED;
+
+ return f;
+}
+
+static void gw_proc_free(gw_proc *f) {
+ if (!f) return;
+
+ gw_proc_free(f->next);
+
+ buffer_free(f->unixsocket);
+ buffer_free(f->connection_name);
+
+ free(f);
+}
+
+static gw_host *gw_host_init(void) {
+ gw_host *f = calloc(1, sizeof(*f));
+ force_assert(f);
+
+ f->id = buffer_init();
+ f->host = buffer_init();
+ f->unixsocket = buffer_init();
+ f->docroot = buffer_init();
+ f->bin_path = buffer_init();
+ f->bin_env = array_init();
+ f->bin_env_copy = array_init();
+ f->strip_request_uri = buffer_init();
+ f->xsendfile_docroot = array_init();
+
+ return f;
+}
+
+static void gw_host_free(gw_host *h) {
+ if (!h) return;
+ if (h->refcount) {
+ --h->refcount;
+ return;
+ }
+
+ buffer_free(h->id);
+ buffer_free(h->host);
+ buffer_free(h->unixsocket);
+ buffer_free(h->docroot);
+ buffer_free(h->bin_path);
+ buffer_free(h->strip_request_uri);
+ array_free(h->bin_env);
+ array_free(h->bin_env_copy);
+ array_free(h->xsendfile_docroot);
+
+ gw_proc_free(h->first);
+ gw_proc_free(h->unused_procs);
+
+ for (size_t i = 0; i < h->args.used; ++i) free(h->args.ptr[i]);
+ free(h->args.ptr);
+ free(h);
+}
+
+static gw_exts *gw_extensions_init(void) {
+ gw_exts *f = calloc(1, sizeof(*f));
+ force_assert(f);
+ return f;
+}
+
+static void gw_extensions_free(gw_exts *f) {
+ if (!f) return;
+ for (size_t i = 0; i < f->used; ++i) {
+ gw_extension *fe = f->exts[i];
+ for (size_t j = 0; j < fe->used; ++j) {
+ gw_host_free(fe->hosts[j]);
+ }
+ buffer_free(fe->key);
+ free(fe->hosts);
+ free(fe);
+ }
+ free(f->exts);
+ free(f);
+}
+
+static int gw_extension_insert(gw_exts *ext, buffer *key, gw_host *fh) {
+ gw_extension *fe = NULL;
+ for (size_t i = 0; i < ext->used; ++i) {
+ if (buffer_is_equal(key, ext->exts[i]->key)) {
+ fe = ext->exts[i];
+ break;
+ }
+ }
+
+ if (NULL == fe) {
+ fe = calloc(1, sizeof(*fe));
+ force_assert(fe);
+ fe->key = buffer_init();
+ fe->last_used_ndx = -1;
+ buffer_copy_buffer(fe->key, key);
+
+ if (ext->size == 0) {
+ ext->size = 8;
+ ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
+ force_assert(ext->exts);
+ } else if (ext->used == ext->size) {
+ ext->size += 8;
+ ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
+ force_assert(ext->exts);
+ }
+ ext->exts[ext->used++] = fe;
+ fe->size = 4;
+ fe->hosts = malloc(fe->size * sizeof(*(fe->hosts)));
+ force_assert(fe->hosts);
+ } else if (fe->size == fe->used) {
+ fe->size += 4;
+ fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts)));
+ force_assert(fe->hosts);
+ }
+
+ fe->hosts[fe->used++] = fh;
+ return 0;
+}
+
+static void gw_proc_connect_success(server *srv, gw_host *host, gw_proc *proc, int debug) {
+ gw_proc_load_inc(srv, host, proc);
+ gw_proc_tag_inc(srv, host, proc, CONST_STR_LEN(".connected"));
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "ssdsbsd",
+ "got proc:",
+ "pid:", proc->pid,
+ "socket:", proc->connection_name,
+ "load:", proc->load);
+ }
+}
+
+static void gw_proc_connect_error(server *srv, gw_host *host, gw_proc *proc, pid_t pid, int errnum, int debug) {
+ log_error_write(srv, __FILE__, __LINE__, "sssb",
+ "establishing connection failed:", strerror(errnum),
+ "socket:", proc->connection_name);
+
+ if (!proc->is_local) {
+ proc->disabled_until = srv->cur_ts + host->disable_time;
+ gw_proc_set_state(host, proc, PROC_STATE_OVERLOADED);
+ }
+ else if (proc->pid == pid && proc->state == PROC_STATE_RUNNING) {
+ /* several requests from lighttpd might reference the same proc
+ *
+ * Only one of them should mark the proc
+ * and all other ones should just take a new one.
+ *
+ * If a new proc was started with the old struct, this might
+ * otherwise lead to marking a perfectly good proc as dead
+ */
+ log_error_write(srv, __FILE__, __LINE__, "sdssd",
+ "backend error; we'll disable for", host->disable_time,
+ "secs and send the request to another backend instead:",
+ "load:", host->load);
+ if (EAGAIN == errnum) {
+ /* - EAGAIN: cool down the backend; it is overloaded */
+ #ifdef __linux__
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "If this happened on Linux: You have run out of local ports. "
+ "Check the manual, section Performance how to handle this.");
+ #endif
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "sbsd",
+ "This means that you have more incoming requests than your "
+ "FastCGI backend can handle in parallel. It might help to "
+ "spawn more FastCGI backends or PHP children; if not, "
+ "decrease server.max-connections. The load for this FastCGI "
+ "backend", proc->connection_name, "is", proc->load);
+ }
+ proc->disabled_until = srv->cur_ts + host->disable_time;
+ gw_proc_set_state(host, proc, PROC_STATE_OVERLOADED);
+ }
+ else {
+ /* we got a hard error from the backend like
+ * - ECONNREFUSED for tcp-ip sockets
+ * - ENOENT for unix-domain-sockets
+ */
+ gw_proc_set_state(host, proc, PROC_STATE_DIED_WAIT_FOR_PID);
+ }
+ }
+
+ if (EAGAIN == errnum) {
+ gw_proc_tag_inc(srv, host, proc, CONST_STR_LEN(".overloaded"));
+ }
+ else {
+ gw_proc_tag_inc(srv, host, proc, CONST_STR_LEN(".died"));
+ }
+}
+
+static void gw_proc_release(server *srv, gw_host *host, gw_proc *proc, int debug) {
+ gw_proc_load_dec(srv, host, proc);
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "ssdsbsd",
+ "released proc:",
+ "pid:", proc->pid,
+ "socket:", proc->connection_name,
+ "load:", proc->load);
+ }
+}
+
+static void gw_proc_check_enable(server *srv, gw_host *host, gw_proc *proc) {
+ if (srv->cur_ts <= proc->disabled_until) return;
+ if (proc->state != PROC_STATE_OVERLOADED) return;
+
+ gw_proc_set_state(host, proc, PROC_STATE_RUNNING);
+
+ log_error_write(srv, __FILE__, __LINE__, "sbbdb",
+ "gw-server re-enabled:", proc->connection_name,
+ host->host, host->port, host->unixsocket);
+}
+
+static int gw_proc_waitpid(server *srv, gw_host *host, gw_proc *proc) {
+ int rc, status;
+
+ if (!proc->is_local) return 0;
+ if (proc->pid <= 0) return 0;
+
+ do {
+ rc = waitpid(proc->pid, &status, WNOHANG);
+ } while (-1 == rc && errno == EINTR);
+ if (0 == rc) return 0; /* child still running */
+
+ /* child terminated */
+ if (-1 == rc) {
+ /* EINVAL or ECHILD no child processes */
+ /* should not happen; someone else has cleaned up for us */
+ log_error_write(srv, __FILE__, __LINE__, "sddss",
+ "pid ", proc->pid, proc->state,
+ "not found:", strerror(errno));
+ } else if (WIFEXITED(status)) {
+ if (proc->state != PROC_STATE_KILLED) {
+ log_error_write(srv, __FILE__, __LINE__, "sdb",
+ "child exited:",
+ WEXITSTATUS(status), proc->connection_name);
+ }
+ } else if (WIFSIGNALED(status)) {
+ if (WTERMSIG(status) != SIGTERM && WTERMSIG(status) != SIGINT) {
+ log_error_write(srv, __FILE__, __LINE__, "sd",
+ "child signalled:", WTERMSIG(status));
+ }
+ } else {
+ log_error_write(srv, __FILE__, __LINE__, "sd",
+ "child died somehow:", status);
+ }
+
+ proc->pid = 0;
+ gw_proc_set_state(host, proc, PROC_STATE_DIED);
+ return 1;
+}
+
+static int env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
+ char *dst;
+
+ if (!key || !val) return -1;
+
+ dst = malloc(key_len + val_len + 3);
+ force_assert(dst);
+ memcpy(dst, key, key_len);
+ dst[key_len] = '=';
+ memcpy(dst + key_len + 1, val, val_len + 1); /* add the \0 from the value */
+
+ for (size_t i = 0; i < env->used; ++i) {
+ if (0 == strncmp(dst, env->ptr[i], key_len + 1)) {
+ free(env->ptr[i]);
+ env->ptr[i] = dst;
+ return 0;
+ }
+ }
+
+ if (env->size == 0) {
+ env->size = 16;
+ env->ptr = malloc(env->size * sizeof(*env->ptr));
+ force_assert(env->ptr);
+ } else if (env->size == env->used + 1) {
+ env->size += 16;
+ env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
+ force_assert(env->ptr);
+ }
+
+ env->ptr[env->used++] = dst;
+
+ return 0;
+}
+
+static int gw_spawn_connection(server *srv, gw_host *host, gw_proc *proc, int debug) {
+ int gw_fd;
+ int status;
+ struct timeval tv = { 0, 10 * 1000 };
+ sock_addr addr;
+ struct sockaddr *gw_addr = (struct sockaddr *)&addr;
+ socklen_t servlen;
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "sdb",
+ "new proc, socket:", proc->port, proc->unixsocket);
+ }
+
+ if (!buffer_string_is_empty(proc->unixsocket)) {
+ if (1 != sock_addr_from_str_hints(srv, &addr, &servlen,
+ proc->unixsocket->ptr, AF_UNIX, 0)) {
+ return -1;
+ }
+ } else {
+ if (1 != sock_addr_from_buffer_hints_numeric(srv, &addr, &servlen,
+ host->host, host->family,
+ proc->port)) {
+ return -1;
+ }
+ }
+
+ if (!buffer_string_is_empty(proc->unixsocket)) {
+ buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("unix:"));
+ buffer_append_string_buffer(proc->connection_name, proc->unixsocket);
+ } else {
+ buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("tcp:"));
+ if (!buffer_string_is_empty(host->host)) {
+ buffer_append_string_buffer(proc->connection_name, host->host);
+ } else {
+ buffer_append_string_len(proc->connection_name,
+ CONST_STR_LEN("localhost"));
+ }
+ buffer_append_string_len(proc->connection_name, CONST_STR_LEN(":"));
+ buffer_append_int(proc->connection_name, proc->port);
+ }
+
+ gw_fd = fdevent_socket_cloexec(gw_addr->sa_family, SOCK_STREAM, 0);
+ if (-1 == gw_fd) {
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "failed:", strerror(errno));
+ return -1;
+ }
+
+ do {
+ status = connect(gw_fd, gw_addr, servlen);
+ } while (-1 == status && errno == EINTR);
+
+ if (-1 == status && errno != ENOENT
+ && !buffer_string_is_empty(proc->unixsocket)) {
+ log_error_write(srv, __FILE__, __LINE__, "sbss",
+ "unlink", proc->unixsocket,
+ "after connect failed:", strerror(errno));
+ unlink(proc->unixsocket->ptr);
+ }
+
+ close(gw_fd);
+
+ if (-1 == status) {
+ /* server is not up, spawn it */
+ char_array env;
+ size_t i;
+ int val;
+ int dfd = -1;
+
+ /* reopen socket */
+ gw_fd = fdevent_socket_cloexec(gw_addr->sa_family, SOCK_STREAM, 0);
+ if (-1 == gw_fd) {
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "socket failed:", strerror(errno));
+ return -1;
+ }
+
+ val = 1;
+ if (setsockopt(gw_fd,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(val)) < 0) {
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "socketsockopt failed:", strerror(errno));
+ close(gw_fd);
+ return -1;
+ }
+
+ /* create socket */
+ if (-1 == bind(gw_fd, gw_addr, servlen)) {
+ log_error_write(srv, __FILE__, __LINE__, "sbs",
+ "bind failed for:",
+ proc->connection_name,
+ strerror(errno));
+ close(gw_fd);
+ return -1;
+ }
+
+ if (-1 == listen(gw_fd, host->listen_backlog)) {
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "listen failed:", strerror(errno));
+ close(gw_fd);
+ return -1;
+ }
+
+ {
+ /* create environment */
+ env.ptr = NULL;
+ env.size = 0;
+ env.used = 0;
+
+ /* build clean environment */
+ if (host->bin_env_copy->used) {
+ for (i = 0; i < host->bin_env_copy->used; ++i) {
+ data_string *ds=(data_string *)host->bin_env_copy->data[i];
+ char *ge;
+
+ if (NULL != (ge = getenv(ds->value->ptr))) {
+ env_add(&env, CONST_BUF_LEN(ds->value), ge, strlen(ge));
+ }
+ }
+ } else {
+ char ** const e = environ;
+ for (i = 0; e[i]; ++i) {
+ char *eq;
+
+ if (NULL != (eq = strchr(e[i], '='))) {
+ env_add(&env, e[i], eq - e[i], eq+1, strlen(eq+1));
+ }
+ }
+ }
+
+ /* create environment */
+ for (i = 0; i < host->bin_env->used; ++i) {
+ data_string *ds = (data_string *)host->bin_env->data[i];
+
+ env_add(&env, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
+ }
+
+ for (i = 0; i < env.used; ++i) {
+ /* search for PHP_FCGI_CHILDREN */
+ if (0 == strncmp(env.ptr[i], "PHP_FCGI_CHILDREN=",
+ sizeof("PHP_FCGI_CHILDREN=")-1)) {
+ break;
+ }
+ }
+
+ /* not found, add a default */
+ if (i == env.used) {
+ env_add(&env, CONST_STR_LEN("PHP_FCGI_CHILDREN"),
+ CONST_STR_LEN("1"));
+ }
+
+ env.ptr[env.used] = NULL;
+ }
+
+ dfd = fdevent_open_dirname(host->args.ptr[0]);
+ if (-1 == dfd) {
+ log_error_write(srv, __FILE__, __LINE__, "sss",
+ "open dirname failed:", strerror(errno),
+ host->args.ptr[0]);
+ }
+
+ /*(FCGI_LISTENSOCK_FILENO == STDIN_FILENO == 0)*/
+ proc->pid = (dfd >= 0)
+ ? fdevent_fork_execve(host->args.ptr[0], host->args.ptr,
+ env.ptr, gw_fd, -1, -1, dfd)
+ : -1;
+
+ for (i = 0; i < env.used; ++i) free(env.ptr[i]);
+ free(env.ptr);
+ if (-1 != dfd) close(dfd);
+ close(gw_fd);
+
+ if (-1 == proc->pid) {
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "gw-backend failed to start:", host->bin_path);
+ return -1;
+ }
+
+ /* register process */
+ proc->last_used = srv->cur_ts;
+ proc->is_local = 1;
+
+ /* wait */
+ select(0, NULL, NULL, NULL, &tv);
+
+ if (0 != gw_proc_waitpid(srv, host, proc)) {
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "gw-backend failed to start:", host->bin_path);
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "If you're trying to run your app as a FastCGI backend, make "
+ "sure you're using the FastCGI-enabled version. If this is PHP "
+ "on Gentoo, add 'fastcgi' to the USE flags. If this is PHP, try "
+ "removing the bytecode caches for now and try again.");
+ return -1;
+ }
+ } else {
+ proc->is_local = 0;
+ proc->pid = 0;
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "(debug) socket is already used; won't spawn:",
+ proc->connection_name);
+ }
+ }
+
+ gw_proc_set_state(host, proc, PROC_STATE_RUNNING);
+ return 0;
+}
+
+static void gw_proc_spawn(server *srv, gw_host *host, int debug) {
+ gw_proc *proc;
+ for (proc=host->unused_procs; proc && proc->pid != 0; proc=proc->next);
+ if (proc) {
+ if (proc == host->unused_procs)
+ host->unused_procs = proc->next;
+ else
+ proc->prev->next = proc->next;
+
+ if (proc->next) {
+ proc->next->prev = proc->prev;
+ proc->next = NULL;
+ }
+
+ proc->prev = NULL;
+ } else {
+ proc = gw_proc_init();
+ proc->id = host->max_id++;
+ }
+
+ ++host->num_procs;
+
+ if (buffer_string_is_empty(host->unixsocket)) {
+ proc->port = host->port + proc->id;
+ } else {
+ buffer_copy_buffer(proc->unixsocket, host->unixsocket);
+ buffer_append_string_len(proc->unixsocket, CONST_STR_LEN("-"));
+ buffer_append_int(proc->unixsocket, proc->id);
+ }
+
+ if (gw_spawn_connection(srv, host, proc, debug)) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "ERROR: spawning backend failed.");
+ proc->next = host->unused_procs;
+ if (host->unused_procs)
+ host->unused_procs->prev = proc;
+ host->unused_procs = proc;
+ } else {
+ proc->next = host->first;
+ if (host->first)
+ host->first->prev = proc;
+ host->first = proc;
+ }
+}
+
+static void gw_proc_kill(server *srv, gw_host *host, gw_proc *proc) {
+ UNUSED(srv);
+ if (proc->next) proc->next->prev = proc->prev;
+ if (proc->prev) proc->prev->next = proc->next;
+
+ if (proc->prev == NULL) host->first = proc->next;
+
+ proc->prev = NULL;
+ proc->next = host->unused_procs;
+
+ if (host->unused_procs)
+ host->unused_procs->prev = proc;
+ host->unused_procs = proc;
+
+ kill(proc->pid, SIGTERM);
+
+ gw_proc_set_state(host, proc, PROC_STATE_KILLED);
+
+ --host->num_procs;
+}
+
+static gw_host * unixsocket_is_dup(gw_plugin_data *p, size_t used, buffer *unixsocket) {
+ for (size_t i = 0; i < used; ++i) {
+ gw_exts *exts = p->config_storage[i]->exts;
+ if (NULL == exts) continue;
+ for (size_t j = 0; j < exts->used; ++j) {
+ gw_extension *ex = exts->exts[j];
+ for (size_t n = 0; n < ex->used; ++n) {
+ gw_host *host = ex->hosts[n];
+ if (!buffer_string_is_empty(host->unixsocket)
+ && buffer_is_equal(host->unixsocket, unixsocket)
+ && !buffer_string_is_empty(host->bin_path))
+ return host;
+ }
+ }
+ }
+
+ return NULL;
+}
+
+static int parse_binpath(char_array *env, buffer *b) {
+ char *start = b->ptr;
+ char c;
+ /* search for spaces */
+ for (size_t i = 0; i < buffer_string_length(b); ++i) {
+ switch(b->ptr[i]) {
+ case ' ':
+ case '\t':
+ /* a WS, stop here and copy the argument */
+
+ if (env->size == 0) {
+ env->size = 16;
+ env->ptr = malloc(env->size * sizeof(*env->ptr));
+ } else if (env->size == env->used) {
+ env->size += 16;
+ env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
+ }
+
+ c = b->ptr[i];
+ b->ptr[i] = '\0';
+ env->ptr[env->used++] = strdup(start);
+ b->ptr[i] = c;
+
+ start = b->ptr + i + 1;
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (env->size == 0) {
+ env->size = 16;
+ env->ptr = malloc(env->size * sizeof(*env->ptr));
+ } else if (env->size == env->used) { /*need one extra for terminating NULL*/
+ env->size += 16;
+ env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
+ }
+
+ /* the rest */
+ env->ptr[env->used++] = strdup(start);
+
+ if (env->size == 0) {
+ env->size = 16;
+ env->ptr = malloc(env->size * sizeof(*env->ptr));
+ } else if (env->size == env->used) { /*need one extra for terminating NULL*/
+ env->size += 16;
+ env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
+ }
+
+ /* terminate */
+ env->ptr[env->used++] = NULL;
+
+ return 0;
+}
+
+enum {
+ GW_BALANCE_LEAST_CONNECTION,
+ GW_BALANCE_RR,
+ GW_BALANCE_HASH,
+ GW_BALANCE_STICKY
+};
+
+static gw_host * gw_host_get(server *srv, connection *con, gw_extension *extension, int balance, int debug) {
+ gw_host *host;
+ unsigned long last_max = ULONG_MAX;
+ int max_usage = INT_MAX;
+ int ndx = -1;
+ size_t k;
+
+ if (extension->used <= 1) {
+ if (1 == extension->used && extension->hosts[0]->active_procs > 0) {
+ ndx = 0;
+ }
+ } else switch(balance) {
+ case GW_BALANCE_HASH:
+ /* hash balancing */
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "sd",
+ "proxy - used hash balancing, hosts:",
+ extension->used);
+ }
+
+ for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->used; ++k) {
+ unsigned long cur_max;
+ host = extension->hosts[k];
+ if (0 == host->active_procs) continue;
+
+ cur_max = generate_crc32c(CONST_BUF_LEN(con->uri.path))
+ + generate_crc32c(CONST_BUF_LEN(host->host)) /* cachable */
+ + generate_crc32c(CONST_BUF_LEN(con->uri.authority));
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "sbbbd",
+ "proxy - election:", con->uri.path,
+ host->host, con->uri.authority, cur_max);
+ }
+
+ if (last_max < cur_max || last_max == ULONG_MAX) {
+ last_max = cur_max;
+ ndx = k;
+ }
+ }
+
+ break;
+ case GW_BALANCE_LEAST_CONNECTION:
+ /* fair balancing */
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "proxy - used least connection");
+ }
+
+ for (k = 0, ndx = -1, max_usage = INT_MAX; k < extension->used; ++k) {
+ host = extension->hosts[k];
+ if (0 == host->active_procs) continue;
+
+ if (host->load < max_usage) {
+ max_usage = host->load;
+ ndx = k;
+ }
+ }
+
+ break;
+ case GW_BALANCE_RR:
+ /* round robin */
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "proxy - used round-robin balancing");
+ }
+
+ /* just to be sure */
+ force_assert(extension->used < INT_MAX);
+
+ host = extension->hosts[0];
+
+ /* Use last_used_ndx from first host in list */
+ k = extension->last_used_ndx;
+ ndx = k + 1; /* use next host after the last one */
+ if (ndx < 0) ndx = 0;
+
+ /* Search first active host after last_used_ndx */
+ while (ndx < (int) extension->used
+ && 0 == (host = extension->hosts[ndx])->active_procs) ++ndx;
+
+ if (ndx >= (int) extension->used) {
+ /* didn't find a higher id, wrap to the start */
+ for (ndx = 0; ndx <= (int) k; ++ndx) {
+ host = extension->hosts[ndx];
+ if (0 != host->active_procs) break;
+ }
+
+ /* No active host found */
+ if (0 == host->active_procs) ndx = -1;
+ }
+
+ /* Save new index for next round */
+ extension->last_used_ndx = ndx;
+
+ break;
+ case GW_BALANCE_STICKY:
+ /* source sticky balancing */
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "sd",
+ "proxy - used sticky balancing, hosts:",
+ extension->used);
+ }
+
+ for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->used; ++k) {
+ unsigned long cur_max;
+ host = extension->hosts[k];
+
+ if (0 == host->active_procs) continue;
+
+ cur_max = generate_crc32c(CONST_BUF_LEN(con->dst_addr_buf))
+ + generate_crc32c(CONST_BUF_LEN(host->host))
+ + host->port;
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "sbbdd",
+ "proxy - election:", con->dst_addr_buf,
+ host->host, host->port, cur_max);
+ }
+
+ if (last_max < cur_max || last_max == ULONG_MAX) {
+ last_max = cur_max;
+ ndx = k;
+ }
+ }
+
+ break;
+ default:
+ break;
+ }
+
+ if (-1 != ndx) {
+ /* found a server */
+ host = extension->hosts[ndx];
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "sbd",
+ "gw - found a host", host->host, host->port);
+ }
+
+ return host;
+ }
+
+ /* all hosts are down */
+ /* sorry, we don't have a server alive for this ext */
+ con->http_status = 503; /* Service Unavailable */
+ con->mode = DIRECT;
+
+ /* only send the 'no handler' once */
+ if (!extension->note_is_sent) {
+ extension->note_is_sent = 1;
+ log_error_write(srv, __FILE__, __LINE__, "sBSbsbs",
+ "all handlers for", con->uri.path, "?",
+ con->uri.query, "on", extension->key, "are down.");
+ }
+
+ return NULL;
+}
+
+static int gw_establish_connection(server *srv, gw_host *host, gw_proc *proc, pid_t pid, int gw_fd, int debug) {
+ sock_addr addr;
+ struct sockaddr *gw_addr = (struct sockaddr *)&addr;
+ socklen_t servlen;
+
+ if (!buffer_string_is_empty(proc->unixsocket)) {
+ if (1 != sock_addr_from_str_hints(srv, &addr, &servlen,
+ proc->unixsocket->ptr, AF_UNIX, 0)) {
+ errno = EINVAL;
+ return -1;
+ }
+ } else {
+ if (1 != sock_addr_from_buffer_hints_numeric(srv, &addr, &servlen,
+ host->host, host->family,
+ proc->port)) {
+ errno = EINVAL;
+ return -1;
+ }
+ }
+
+ if (!buffer_string_is_empty(proc->unixsocket)) {
+ if (buffer_string_is_empty(proc->connection_name)) {
+ /* on remote spawning we have to set the connection-name now */
+ buffer_copy_string_len(proc->connection_name,
+ CONST_STR_LEN("unix:"));
+ buffer_append_string_buffer(proc->connection_name,proc->unixsocket);
+ }
+ } else {
+ if (buffer_string_is_empty(proc->connection_name)) {
+ /* on remote spawning we have to set the connection-name now */
+ buffer_copy_string_len(proc->connection_name,CONST_STR_LEN("tcp:"));
+ if (!buffer_string_is_empty(host->host)) {
+ buffer_append_string_buffer(proc->connection_name, host->host);
+ } else {
+ buffer_append_string_len(proc->connection_name,
+ CONST_STR_LEN("localhost"));
+ }
+ buffer_append_string_len(proc->connection_name, CONST_STR_LEN(":"));
+ buffer_append_int(proc->connection_name, proc->port);
+ }
+ }
+
+ if (-1 == connect(gw_fd, gw_addr, servlen)) {
+ if (errno == EINPROGRESS ||
+ errno == EALREADY ||
+ errno == EINTR) {
+ if (debug > 2) {
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "connect delayed; will continue later:",
+ proc->connection_name);
+ }
+
+ return 1;
+ } else {
+ gw_proc_connect_error(srv, host, proc, pid, errno, debug);
+ return -1;
+ }
+ }
+
+ if (debug > 1) {
+ log_error_write(srv, __FILE__, __LINE__, "sd",
+ "connect succeeded: ", gw_fd);
+ }
+
+ return 0;
+}
+
+static void gw_restart_dead_procs(server *srv, gw_host *host, int debug) {
+ for (gw_proc *proc = host->first; proc; proc = proc->next) {
+ if (debug > 2) {
+ log_error_write(srv, __FILE__, __LINE__, "sbdddd",
+ "proc:", proc->connection_name, proc->state,
+ proc->is_local, proc->load, proc->pid);
+ }
+
+ /*
+ * if the remote side is overloaded, we check back after <n> seconds
+ *
+ */
+ switch (proc->state) {
+ case PROC_STATE_KILLED:
+ /* should never happen as long as adaptive spawing is disabled */
+ force_assert(0);
+
+ break;
+ case PROC_STATE_RUNNING:
+ break;
+ case PROC_STATE_OVERLOADED:
+ case PROC_STATE_DIED_WAIT_FOR_PID:
+ if (0 == gw_proc_waitpid(srv, host, proc)) {
+ gw_proc_check_enable(srv, host, proc);
+ }
+
+ if (proc->state != PROC_STATE_DIED) break;
+ /* fall through *//*(we have a dead proc now)*/
+
+ case PROC_STATE_DIED:
+ /* local procs get restarted by us,
+ * remote ones hopefully by the admin */
+
+ if (!buffer_string_is_empty(host->bin_path)) {
+ /* we still have connections bound to this proc,
+ * let them terminate first */
+ if (proc->load != 0) break;
+
+ /* restart the child */
+
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "ssbsdsd",
+ "--- gw spawning",
+ "\n\tsocket", proc->connection_name,
+ "\n\tcurrent:", 1, "/", host->max_procs);
+ }
+
+ if (gw_spawn_connection(srv, host, proc, debug)) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "ERROR: spawning gw failed.");
+ }
+ } else {
+ gw_proc_check_enable(srv, host, proc);
+ }
+ break;
+ }
+ }
+}
+
+
+
+
+#include "base.h"
+#include "connections.h"
+#include "joblist.h"
+#include "keyvalue.h"
+#include "plugin.h"
+#include "response.h"
+
+
+/* ok, we need a prototype */
+static handler_t gw_handle_fdevent(server *srv, void *ctx, int revents);
+
+
+static gw_handler_ctx * handler_ctx_init(size_t sz) {
+ gw_handler_ctx *hctx = calloc(1, 0 == sz ? sizeof(*hctx) : sz);
+ force_assert(hctx);
+
+ hctx->fde_ndx = -1;
+
+ /*hctx->response = buffer_init();*//*(allocated when needed)*/
+
+ hctx->request_id = 0;
+ hctx->gw_mode = GW_RESPONDER;
+ hctx->state = GW_STATE_INIT;
+ hctx->proc = NULL;
+
+ hctx->fd = -1;
+
+ hctx->reconnects = 0;
+ hctx->send_content_body = 1;
+
+ /*hctx->rb = chunkqueue_init();*//*(allocated when needed)*/
+ hctx->wb = chunkqueue_init();
+ hctx->wb_reqlen = 0;
+
+ return hctx;
+}
+
+static void handler_ctx_free(gw_handler_ctx *hctx) {
+ /* caller MUST have called gw_backend_close(srv, hctx) if necessary */
+ if (hctx->handler_ctx_free) hctx->handler_ctx_free(hctx);
+ buffer_free(hctx->response);
+
+ chunkqueue_free(hctx->rb);
+ chunkqueue_free(hctx->wb);
+
+ free(hctx);
+}
+
+static void handler_ctx_clear(gw_handler_ctx *hctx) {
+ /* caller MUST have called gw_backend_close(srv, hctx) if necessary */
+
+ hctx->proc = NULL;
+ hctx->host = NULL;
+ hctx->ext = NULL;
+ /*hctx->ext_auth is intentionally preserved to flag prior authorizer*/
+
+ hctx->gw_mode = GW_RESPONDER;
+ hctx->state = GW_STATE_INIT;
+ /*hctx->state_timestamp = 0;*//*(unused; left as-is)*/
+
+ if (hctx->rb) chunkqueue_reset(hctx->rb);
+ if (hctx->wb) chunkqueue_reset(hctx->wb);
+ hctx->wb_reqlen = 0;
+
+ buffer_reset(hctx->response);
+
+ hctx->fd = -1;
+ hctx->fde_ndx = -1;
+ hctx->got_proc = 0;
+ hctx->reconnects = 0;
+ hctx->request_id = 0;
+ hctx->send_content_body = 1;
+
+ /*plugin_config conf;*//*(no need to reset for same request)*/
+
+ /*hctx->remote_conn = NULL;*//*(no need to reset for same request)*/
+ /*hctx->plugin_data = NULL;*//*(no need to reset for same request)*/
+}
+
+
+void * gw_init(void) {
+ return calloc(1, sizeof(gw_plugin_data));
+}
+
+
+void gw_plugin_config_free(gw_plugin_config *s) {
+ gw_exts *exts = s->exts;
+ if (exts) {
+ for (size_t j = 0; j < exts->used; ++j) {
+ gw_extension *ex = exts->exts[j];
+ for (size_t n = 0; n < ex->used; ++n) {
+ gw_proc *proc;
+ gw_host *host = ex->hosts[n];
+
+ for (proc = host->first; proc; proc = proc->next) {
+ if (proc->pid > 0) {
+ kill(proc->pid, host->kill_signal);
+ }
+
+ if (proc->is_local &&
+ !buffer_string_is_empty(proc->unixsocket)) {
+ unlink(proc->unixsocket->ptr);
+ }
+ }
+
+ for (proc = host->unused_procs; proc; proc = proc->next) {
+ if (proc->pid > 0) {
+ kill(proc->pid, host->kill_signal);
+ }
+ if (proc->is_local &&
+ !buffer_string_is_empty(proc->unixsocket)) {
+ unlink(proc->unixsocket->ptr);
+ }
+ }
+ }
+ }
+
+ gw_extensions_free(s->exts);
+ gw_extensions_free(s->exts_auth);
+ gw_extensions_free(s->exts_resp);
+ }
+ array_free(s->ext_mapping);
+ free(s);
+}
+
+handler_t gw_free(server *srv, void *p_d) {
+ gw_plugin_data *p = p_d;
+ if (p->config_storage) {
+ for (size_t i = 0; i < srv->config_context->used; ++i) {
+ gw_plugin_config *s = p->config_storage[i];
+ if (NULL == s) continue;
+ gw_plugin_config_free(s);
+ }
+ free(p->config_storage);
+ }
+ free(p);
+ return HANDLER_GO_ON;
+}
+
+int gw_set_defaults_backend(server *srv, gw_plugin_data *p, data_unset *du, size_t i, int sh_exec) {
+ /* per-module plugin_config MUST have common "base class" gw_plugin_config*/
+ /* per-module plugin_data MUST have pointer-compatible common "base class"
+ * with gw_plugin_data (stemming from gw_plugin_config compatibility) */
+
+ data_array *da = (data_array *)du;
+ gw_plugin_config *s = p->config_storage[i];
+ buffer *gw_mode;
+ gw_host *host = NULL;
+
+ if (NULL == da) return 1;
+
+ if (da->type != TYPE_ARRAY || !array_is_kvarray(da->value)) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "unexpected value for xxxxx.server; expected "
+ "( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
+ return 0;
+ }
+
+ gw_mode = buffer_init();
+
+ s->exts = gw_extensions_init();
+ s->exts_auth = gw_extensions_init();
+ s->exts_resp = gw_extensions_init();
+ /*s->balance = GW_BALANCE_LEAST_CONNECTION;*//*(default)*/
+
+ /*
+ * gw.server = ( "<ext>" => ( ... ),
+ * "<ext>" => ( ... ) )
+ */
+
+ for (size_t j = 0; j < da->value->used; ++j) {
+ data_array *da_ext = (data_array *)da->value->data[j];
+
+ /*
+ * da_ext->key == name of the extension
+ */
+
+ /*
+ * gw.server = ( "<ext>" =>
+ * ( "<host>" => ( ... ),
+ * "<host>" => ( ... )
+ * ),
+ * "<ext>" => ... )
+ */
+
+ for (size_t n = 0; n < da_ext->value->used; ++n) {
+ data_array *da_host = (data_array *)da_ext->value->data[n];
+
+ config_values_t fcv[] = {
+ { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
+ { "docroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
+ { "mode", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
+ { "socket", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
+ { "bin-path", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
+
+ { "check-local", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
+ { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
+ { "min-procs-not-working", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 this is broken for now */
+ { "max-procs", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
+ { "max-load-per-proc", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
+ { "idle-timeout", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
+ { "disable-time", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
+
+ { "bin-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
+ { "bin-copy-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
+
+ { "broken-scriptfilename", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
+ { "allow-x-send-file", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
+ { "strip-request-uri", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
+ { "kill-signal", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 17 */
+ { "fix-root-scriptname", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 18 */
+ { "listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 19 */
+ { "x-sendfile", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 20 */
+ { "x-sendfile-docroot",NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 21 */
+
+ { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
+ };
+ unsigned short host_mode = GW_RESPONDER;
+
+ if (da_host->type != TYPE_ARRAY || !array_is_kvany(da_host->value)){
+ log_error_write(srv, __FILE__, __LINE__, "SBS",
+ "unexpected value for gw.server near [",
+ da_host->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
+
+ goto error;
+ }
+
+ host = gw_host_init();
+ buffer_reset(gw_mode);
+
+ buffer_copy_buffer(host->id, da_host->key);
+
+ host->check_local = 1;
+ host->min_procs = 4;
+ host->max_procs = 4;
+ host->max_load_per_proc = 1;
+ host->idle_timeout = 60;
+ host->disable_time = 1;
+ host->break_scriptfilename_for_php = 0;
+ host->kill_signal = SIGTERM;
+ host->fix_root_path_name = 0;
+ host->listen_backlog = 1024;
+ host->xsendfile_allow = 0;
+ host->refcount = 0;
+
+ fcv[0].destination = host->host;
+ fcv[1].destination = host->docroot;
+ fcv[2].destination = gw_mode;
+ fcv[3].destination = host->unixsocket;
+ fcv[4].destination = host->bin_path;
+
+ fcv[5].destination = &(host->check_local);
+ fcv[6].destination = &(host->port);
+ fcv[7].destination = &(host->min_procs);
+ fcv[8].destination = &(host->max_procs);
+ fcv[9].destination = &(host->max_load_per_proc);
+ fcv[10].destination = &(host->idle_timeout);
+ fcv[11].destination = &(host->disable_time);
+
+ fcv[12].destination = host->bin_env;
+ fcv[13].destination = host->bin_env_copy;
+ fcv[14].destination = &(host->break_scriptfilename_for_php);
+ fcv[15].destination = &(host->xsendfile_allow);
+ fcv[16].destination = host->strip_request_uri;
+ fcv[17].destination = &(host->kill_signal);
+ fcv[18].destination = &(host->fix_root_path_name);
+ fcv[19].destination = &(host->listen_backlog);
+ fcv[20].destination = &(host->xsendfile_allow);
+ fcv[21].destination = host->xsendfile_docroot;
+
+ if (0 != config_insert_values_internal(srv, da_host->value, fcv, T_CONFIG_SCOPE_CONNECTION)) {
+ goto error;
+ }
+
+ if ((!buffer_string_is_empty(host->host) || host->port)
+ && !buffer_string_is_empty(host->unixsocket)) {
+ log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
+ "either host/port or socket have to be set in:",
+ da->key, "= (",
+ da_ext->key, " => (",
+ da_host->key, " ( ...");
+
+ goto error;
+ }
+
+ if (!buffer_string_is_empty(host->host) && *host->host->ptr == '/'
+ && buffer_string_is_empty(host->unixsocket)) {
+ buffer_copy_buffer(host->unixsocket, host->host);
+ }
+
+ if (!buffer_string_is_empty(host->unixsocket)) {
+ /* unix domain socket */
+ struct sockaddr_un un;
+
+ if (buffer_string_length(host->unixsocket) + 1 > sizeof(un.sun_path) - 2) {
+ log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
+ "unixsocket is too long in:",
+ da->key, "= (",
+ da_ext->key, " => (",
+ da_host->key, " ( ...");
+
+ goto error;
+ }
+
+ if (!buffer_string_is_empty(host->bin_path)) {
+ gw_host *duplicate = unixsocket_is_dup(p, i+1, host->unixsocket);
+ if (NULL != duplicate) {
+ if (!buffer_is_equal(host->bin_path, duplicate->bin_path)) {
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "duplicate unixsocket path:",
+ host->unixsocket);
+ goto error;
+ }
+ gw_host_free(host);
+ host = duplicate;
+ ++host->refcount;
+ }
+ }
+
+ host->family = AF_UNIX;
+ } else {
+ /* tcp/ip */
+
+ if (buffer_string_is_empty(host->host) &&
+ buffer_string_is_empty(host->bin_path)) {
+ log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
+ "host or binpath have to be set in:",
+ da->key, "= (",
+ da_ext->key, " => (",
+ da_host->key, " ( ...");
+
+ goto error;
+ } else if (0 == host->port) {
+ host->port = 80;
+ }
+
+ host->family = (!buffer_string_is_empty(host->host)
+ && NULL != strchr(host->host->ptr, ':'))
+ ? AF_INET6
+ : AF_INET;
+ }
+
+ if (host->refcount) {
+ /* already init'd; skip spawning */
+ } else if (!buffer_string_is_empty(host->bin_path)) {
+ /* a local socket + self spawning */
+ struct stat st;
+ parse_binpath(&host->args, host->bin_path);
+ if (0 != stat(host->args.ptr[0], &st) || !S_ISREG(st.st_mode)
+ || !(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
+ log_error_write(srv, __FILE__, __LINE__, "SSs",
+ "invalid \"bin-path\" => \"", host->bin_path->ptr,
+ "\" (check that file exists, is regular file, "
+ "and is executable by lighttpd)");
+ }
+
+ if (sh_exec) {
+ /*(preserve prior behavior for SCGI exec of command)*/
+ /*(admin should really prefer to put
+ * any complex command into a script)*/
+ for (size_t m = 0; m < host->args.used; ++m)
+ free(host->args.ptr[m]);
+ free(host->args.ptr);
+
+ host->args.ptr = calloc(4, sizeof(char *));
+ force_assert(host->args.ptr);
+ host->args.used = 3;
+ host->args.size = 4;
+ host->args.ptr[0] = malloc(sizeof("/bin/sh"));
+ force_assert(host->args.ptr[0]);
+ memcpy(host->args.ptr[0], "/bin/sh", sizeof("/bin/sh"));
+ host->args.ptr[1] = malloc(sizeof("-c"));
+ force_assert(host->args.ptr[1]);
+ memcpy(host->args.ptr[1], "-c", sizeof("-c"));
+ host->args.ptr[2] =
+ malloc(sizeof("exec ")-1
+ + buffer_string_length(host->bin_path) + 1);
+ force_assert(host->args.ptr[2]);
+ memcpy(host->args.ptr[2], "exec ", sizeof("exec ")-1);
+ memcpy(host->args.ptr[2]+sizeof("exec ")-1,
+ host->bin_path->ptr,
+ buffer_string_length(host->bin_path)+1);
+ host->args.ptr[3] = NULL;
+ }
+
+ /* HACK: just to make sure the adaptive spawing is disabled */
+ host->min_procs = host->max_procs;
+
+ if (host->min_procs > host->max_procs)
+ host->max_procs = host->min_procs;
+ if (host->max_load_per_proc < 1)
+ host->max_load_per_proc = 0;
+
+ if (s->debug) {
+ log_error_write(srv, __FILE__, __LINE__, "ssbsdsbsdsd",
+ "--- gw spawning local",
+ "\n\tproc:", host->bin_path,
+ "\n\tport:", host->port,
+ "\n\tsocket", host->unixsocket,
+ "\n\tmin-procs:", host->min_procs,
+ "\n\tmax-procs:", host->max_procs);
+ }
+
+ for (size_t pno = 0; pno < host->max_procs; ++pno) {
+ gw_proc *proc = gw_proc_init();
+ proc->id = host->num_procs++;
+ host->max_id++;
+
+ if (buffer_string_is_empty(host->unixsocket)) {
+ proc->port = host->port + pno;
+ } else {
+ buffer_copy_buffer(proc->unixsocket, host->unixsocket);
+ buffer_append_string_len(proc->unixsocket,
+ CONST_STR_LEN("-"));
+ buffer_append_int(proc->unixsocket, pno);
+ }
+
+ if (s->debug) {
+ log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
+ "--- gw spawning",
+ "\n\tport:", host->port,
+ "\n\tsocket", host->unixsocket,
+ "\n\tcurrent:", pno, "/", host->max_procs);
+ }
+
+ if (!srv->srvconf.preflight_check
+ && gw_spawn_connection(srv, host, proc, s->debug)) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "[ERROR]: spawning gw failed.");
+ gw_proc_free(proc);
+ goto error;
+ }
+
+ gw_status_init(srv, host, proc);
+
+ proc->next = host->first;
+ if (host->first) host->first->prev = proc;
+
+ host->first = proc;
+ }
+ } else {
+ gw_proc *proc;
+
+ proc = gw_proc_init();
+ proc->id = host->num_procs++;
+ host->max_id++;
+ gw_proc_set_state(host, proc, PROC_STATE_RUNNING);
+
+ if (buffer_string_is_empty(host->unixsocket)) {
+ proc->port = host->port;
+ } else {
+ buffer_copy_buffer(proc->unixsocket, host->unixsocket);
+ }
+
+ gw_status_init(srv, host, proc);
+
+ host->first = proc;
+
+ host->min_procs = 1;
+ host->max_procs = 1;
+ }
+
+ if (!buffer_string_is_empty(gw_mode)) {
+ if (strcmp(gw_mode->ptr, "responder") == 0) {
+ host_mode = GW_RESPONDER;
+ } else if (strcmp(gw_mode->ptr, "authorizer") == 0) {
+ host_mode = GW_AUTHORIZER;
+ } else {
+ log_error_write(srv, __FILE__, __LINE__, "sbs",
+ "WARNING: unknown gw mode:",
+ gw_mode,"(ignored, mode set to responder)");
+ }
+ }
+
+ if (host->xsendfile_docroot->used) {
+ size_t k;
+ for (k = 0; k < host->xsendfile_docroot->used; ++k) {
+ data_string *ds = (data_string *)host->xsendfile_docroot->data[k];
+ if (ds->type != TYPE_STRING) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
+ goto error;
+ }
+ if (ds->value->ptr[0] != '/') {
+ log_error_write(srv, __FILE__, __LINE__, "SBs",
+ "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds->value, "\"");
+ goto error;
+ }
+ buffer_path_simplify(ds->value, ds->value);
+ buffer_append_slash(ds->value);
+ }
+ }
+
+ /* s->exts is list of exts -> hosts
+ * s->exts now used as combined list
+ * of authorizer and responder hosts (for backend maintenance)
+ * s->exts_auth is list of exts -> authorizer hosts
+ * s->exts_resp is list of exts -> responder hosts
+ * For each path/extension:
+ * there may be an independent GW_AUTHORIZER and GW_RESPONDER
+ * (The GW_AUTHORIZER and GW_RESPONDER could be handled by the same
+ * host, and an admin might want to do that for large uploads,
+ * since GW_AUTHORIZER runs prior to receiving (potentially large)
+ * request body from client and can authorizer or deny request
+ * prior to receiving the full upload)
+ */
+ gw_extension_insert(s->exts, da_ext->key, host);
+
+ if (host_mode == GW_AUTHORIZER) {
+ ++host->refcount;
+ gw_extension_insert(s->exts_auth, da_ext->key, host);
+ } else if (host_mode == GW_RESPONDER) {
+ ++host->refcount;
+ gw_extension_insert(s->exts_resp, da_ext->key, host);
+ } /*(else should have been rejected above)*/
+
+ host = NULL;
+ }
+ }
+
+ buffer_free(gw_mode);
+ return 1;
+
+error:
+ if (NULL != host) gw_host_free(host);
+ buffer_free(gw_mode);
+ return 0;
+}
+
+int gw_set_defaults_balance(server *srv, gw_plugin_config *s, data_unset *du) {
+ buffer *b;
+ if (NULL == du) {
+ b = NULL;
+ } else if (du->type == TYPE_STRING) {
+ b = ((data_string *)du)->value;
+ } else {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "unexpected type for xxxxx.balance; expected string");
+ return 0;
+ }
+ if (buffer_string_is_empty(b)) {
+ s->balance = GW_BALANCE_LEAST_CONNECTION;
+ } else if (buffer_is_equal_string(b, CONST_STR_LEN("fair"))) {
+ s->balance = GW_BALANCE_LEAST_CONNECTION;
+ } else if (buffer_is_equal_string(b, CONST_STR_LEN("least-connection"))) {
+ s->balance = GW_BALANCE_LEAST_CONNECTION;
+ } else if (buffer_is_equal_string(b, CONST_STR_LEN("round-robin"))) {
+ s->balance = GW_BALANCE_RR;
+ } else if (buffer_is_equal_string(b, CONST_STR_LEN("hash"))) {
+ s->balance = GW_BALANCE_HASH;
+ } else if (buffer_is_equal_string(b, CONST_STR_LEN("sticky"))) {
+ s->balance = GW_BALANCE_STICKY;
+ } else {
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "xxxxx.balance has to be one of: "
+ "least-connection, round-robin, hash, sticky, but not:",
+ b);
+ return 0;
+ }
+ return 1;
+}
+
+static void gw_set_state(server *srv, gw_handler_ctx *hctx, gw_connection_state_t state) {
+ hctx->state = state;
+ hctx->state_timestamp = srv->cur_ts;
+}
+
+
+void gw_set_transparent(server *srv, gw_handler_ctx *hctx) {
+ if (AF_UNIX != hctx->host->family) {
+ int v = 1;
+ if (-1 == setsockopt(hctx->fd,IPPROTO_TCP,TCP_NODELAY,&v,sizeof(v))) {
+ /*(error, but not critical)*/
+ }
+ }
+ hctx->wb_reqlen = -1;
+ gw_set_state(srv, hctx, GW_STATE_WRITE);
+}
+
+
+static void gw_backend_close(server *srv, gw_handler_ctx *hctx) {
+ if (hctx->fd >= 0) {
+ fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
+ fdevent_unregister(srv->ev, hctx->fd);
+ fdevent_sched_close(srv->ev, hctx->fd, 1);
+ hctx->fd = -1;
+ hctx->fde_ndx = -1;
+ }
+
+ if (hctx->host) {
+ if (hctx->proc) {
+ if (hctx->got_proc) {
+ /* after the connect the process gets a load */
+ hctx->got_proc = 0;
+ gw_proc_release(srv, hctx->host, hctx->proc, hctx->conf.debug);
+ }
+ hctx->proc = NULL;
+ }
+
+ gw_host_reset(srv, hctx->host);
+ hctx->host = NULL;
+ }
+}
+
+static void gw_connection_close(server *srv, gw_handler_ctx *hctx) {
+ gw_plugin_data *p = hctx->plugin_data;
+ connection *con = hctx->remote_conn;
+
+ gw_backend_close(srv, hctx);
+ handler_ctx_free(hctx);
+ con->plugin_ctx[p->id] = NULL;
+
+ if (con->mode == p->id) {
+ http_response_backend_done(srv, con);
+ }
+}
+
+static handler_t gw_reconnect(server *srv, gw_handler_ctx *hctx) {
+ gw_backend_close(srv, hctx);
+
+ hctx->host = gw_host_get(srv, hctx->remote_conn, hctx->ext,
+ hctx->conf.balance, hctx->conf.debug);
+ if (NULL == hctx->host) return HANDLER_FINISHED;
+
+ gw_host_assign(srv, hctx->host);
+ hctx->request_id = 0;
+ hctx->opts.xsendfile_allow = hctx->host->xsendfile_allow;
+ hctx->opts.xsendfile_docroot = hctx->host->xsendfile_docroot;
+ gw_set_state(srv, hctx, GW_STATE_INIT);
+ return HANDLER_COMEBACK;
+}
+
+
+handler_t gw_connection_reset(server *srv, connection *con, void *p_d) {
+ gw_plugin_data *p = p_d;
+ gw_handler_ctx *hctx = con->plugin_ctx[p->id];
+ if (hctx) gw_connection_close(srv, hctx);
+
+ return HANDLER_GO_ON;
+}
+
+
+static handler_t gw_write_request(server *srv, gw_handler_ctx *hctx) {
+ switch(hctx->state) {
+ case GW_STATE_INIT:
+ /* do we have a running process for this host (max-procs) ? */
+ hctx->proc = NULL;
+
+ for (gw_proc *proc = hctx->host->first; proc; proc = proc->next) {
+ if (proc->state == PROC_STATE_RUNNING) {
+ hctx->proc = proc;
+ break;
+ }
+ }
+
+ /* all children are dead */
+ if (hctx->proc == NULL) {
+ return HANDLER_ERROR;
+ }
+
+ /* check the other procs if they have a lower load */
+ for (gw_proc *proc = hctx->proc->next; proc; proc = proc->next) {
+ if (proc->state != PROC_STATE_RUNNING) continue;
+ if (proc->load < hctx->proc->load) hctx->proc = proc;
+ }
+
+ hctx->fd = fdevent_socket_nb_cloexec(hctx->host->family,SOCK_STREAM,0);
+ if (-1 == hctx->fd) {
+ if (errno == EMFILE || errno == EINTR) {
+ log_error_write(srv, __FILE__, __LINE__, "sd",
+ "wait for fd at connection:",
+ hctx->remote_conn->fd);
+ return HANDLER_WAIT_FOR_FD;
+ }
+
+ log_error_write(srv, __FILE__, __LINE__, "ssdd",
+ "socket failed:", strerror(errno),
+ srv->cur_fds, srv->max_fds);
+ return HANDLER_ERROR;
+ }
+
+ srv->cur_fds++;
+
+ fdevent_register(srv->ev, hctx->fd, gw_handle_fdevent, hctx);
+
+ if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "fcntl failed:", strerror(errno));
+ return HANDLER_ERROR;
+ }
+
+ if (hctx->proc->is_local) {
+ hctx->pid = hctx->proc->pid;
+ }
+
+ switch (gw_establish_connection(srv, hctx->host, hctx->proc, hctx->pid,
+ hctx->fd, hctx->conf.debug)) {
+ case 1: /* connection is in progress */
+ fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
+ gw_set_state(srv, hctx, GW_STATE_CONNECT_DELAYED);
+ return HANDLER_WAIT_FOR_EVENT;
+ case -1:/* connection error */
+ return HANDLER_ERROR;
+ case 0: /* everything is ok, go on */
+ hctx->reconnects = 0;
+ break;
+ }
+ /* fall through */
+ case GW_STATE_CONNECT_DELAYED:
+ if (hctx->state == GW_STATE_CONNECT_DELAYED) { /*(not GW_STATE_INIT)*/
+ int socket_error = fdevent_connect_status(hctx->fd);
+ if (socket_error != 0) {
+ gw_proc_connect_error(srv, hctx->host, hctx->proc, hctx->pid,
+ socket_error, hctx->conf.debug);
+ return HANDLER_ERROR;
+ }
+ /* go on with preparing the request */
+ }
+
+ gw_proc_connect_success(srv, hctx->host, hctx->proc, hctx->conf.debug);
+ hctx->got_proc = 1;
+
+ gw_set_state(srv, hctx, GW_STATE_PREPARE_WRITE);
+ /* fall through */
+ case GW_STATE_PREPARE_WRITE:
+ /* ok, we have the connection */
+
+ {
+ handler_t rc = hctx->create_env(srv, hctx);
+ if (HANDLER_GO_ON != rc) return rc;
+ }
+
+ fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
+ gw_set_state(srv, hctx, GW_STATE_WRITE);
+ /* fall through */
+ case GW_STATE_WRITE:
+ if (!chunkqueue_is_empty(hctx->wb)) {
+ connection *con = hctx->remote_conn;
+ int ret;
+ #if 0
+ if (hctx->conf.debug > 1) {
+ log_error_write(srv, __FILE__, __LINE__, "sdsx",
+ "send data to backend ( fd =", hctx->fd,
+ "), size =", chunkqueue_length(hctx->wb));
+ }
+ #endif
+ ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb,
+ MAX_WRITE_LIMIT);
+
+ chunkqueue_remove_finished_chunks(hctx->wb);
+
+ if (ret < 0) {
+ switch(errno) {
+ case EPIPE:
+ case ENOTCONN:
+ case ECONNRESET:
+ /* the connection got dropped after accept()
+ * we don't care about that --
+ * if you accept() it, you have to handle it.
+ */
+ log_error_write(srv, __FILE__, __LINE__, "ssosb",
+ "connection was dropped after accept() "
+ "(perhaps the gw process died),",
+ "write-offset:", hctx->wb->bytes_out,
+ "socket:", hctx->proc->connection_name);
+ return HANDLER_ERROR;
+ default:
+ log_error_write(srv, __FILE__, __LINE__, "ssd",
+ "write failed:", strerror(errno), errno);
+ return HANDLER_ERROR;
+ }
+ }
+ }
+
+ if (hctx->wb->bytes_out == hctx->wb_reqlen) {
+ fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
+ gw_set_state(srv, hctx, GW_STATE_READ);
+ } else {
+ off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
+ if ((hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0)
+ && wblen < 65536 - 16384) {
+ connection *con = hctx->remote_conn;
+ /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
+ if (!(con->conf.stream_request_body
+ & FDEVENT_STREAM_REQUEST_POLLIN)) {
+ con->conf.stream_request_body |=
+ FDEVENT_STREAM_REQUEST_POLLIN;
+ con->is_readable = 1;/*trigger optimistic read from client*/
+ }
+ }
+ if (0 == wblen) {
+ fdevent_event_clr(srv->ev,&hctx->fde_ndx,hctx->fd,FDEVENT_OUT);
+ } else {
+ fdevent_event_add(srv->ev,&hctx->fde_ndx,hctx->fd,FDEVENT_OUT);
+ }
+ }
+
+ return HANDLER_WAIT_FOR_EVENT;
+ case GW_STATE_READ:
+ /* waiting for a response */
+ return HANDLER_WAIT_FOR_EVENT;
+ default:
+ log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
+ return HANDLER_ERROR;
+ }
+}
+
+static handler_t gw_write_error(server *srv, gw_handler_ctx *hctx) {
+ connection *con = hctx->remote_conn;
+ int status = con->http_status;
+
+ if (hctx->state == GW_STATE_INIT ||
+ hctx->state == GW_STATE_CONNECT_DELAYED) {
+
+ gw_restart_dead_procs(srv, hctx->host, hctx->conf.debug);
+
+ /* cleanup this request and let request handler start request again */
+ if (hctx->reconnects++ < 5) return gw_reconnect(srv, hctx);
+ }
+
+ if (hctx->backend_error) hctx->backend_error(hctx);
+ gw_connection_close(srv, hctx);
+ con->http_status = (status == 400) ? 400 : 503;
+ return HANDLER_FINISHED;
+}
+
+static handler_t gw_send_request(server *srv, gw_handler_ctx *hctx) {
+ handler_t rc = gw_write_request(srv, hctx);
+ return (HANDLER_ERROR != rc) ? rc : gw_write_error(srv, hctx);
+}
+
+
+static handler_t gw_recv_response(server *srv, gw_handler_ctx *hctx);
+
+
+handler_t gw_handle_subrequest(server *srv, connection *con, void *p_d) {
+ gw_plugin_data *p = p_d;
+ gw_handler_ctx *hctx = con->plugin_ctx[p->id];
+ if (NULL == hctx) return HANDLER_GO_ON;
+ if (con->mode != p->id) return HANDLER_GO_ON; /* not my job */
+
+ if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
+ && con->file_started) {
+ if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
+ fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
+ }
+ else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)){
+ /* optimistic read from backend */
+ handler_t rc;
+ rc = gw_recv_response(srv, hctx); /*(might invalidate hctx)*/
+ if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
+ fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
+ }
+ }
+
+ /* (do not receive request body before GW_AUTHORIZER has run or else
+ * the request body is discarded with handler_ctx_clear() after running
+ * the FastCGI Authorizer) */
+
+ if (hctx->gw_mode != GW_AUTHORIZER
+ && (0 == hctx->wb->bytes_in
+ ? con->state == CON_STATE_READ_POST
+ : (hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0))) {
+ /* leave excess data in con->request_content_queue, which is
+ * buffered to disk if too large and backend can not keep up */
+ /*(64k - 4k to attempt to avoid temporary files
+ * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
+ if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096) {
+ if (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
+ con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
+ }
+ if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
+ }
+ else {
+ handler_t r = connection_handle_read_post_state(srv, con);
+ chunkqueue *req_cq = con->request_content_queue;
+ #if 0 /*(not reached since we send 411 Length Required below)*/
+ if (hctx->wb_reqlen < -1 && con->request.content_length >= 0) {
+ /* (completed receiving Transfer-Encoding: chunked) */
+ hctx->wb_reqlen= -hctx->wb_reqlen + con->request.content_length;
+ if (hctx->stdin_append) {
+ handler_t rc = hctx->stdin_append(srv, hctx);
+ if (HANDLER_GO_ON != rc) return rc;
+ }
+ }
+ #endif
+ if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
+ if (hctx->stdin_append) {
+ handler_t rc = hctx->stdin_append(srv, hctx);
+ if (HANDLER_GO_ON != rc) return rc;
+ }
+ else
+ chunkqueue_append_chunkqueue(hctx->wb, req_cq);
+ if (fdevent_event_get_interest(srv->ev,hctx->fd) & FDEVENT_OUT){
+ return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
+ }
+ }
+ if (r != HANDLER_GO_ON) return r;
+
+
+ /* XXX: create configurable flag */
+ /* CGI environment requires that Content-Length be set.
+ * Send 411 Length Required if Content-Length missing.
+ * (occurs here if client sends Transfer-Encoding: chunked
+ * and module is flagged to stream request body to backend) */
+ /* proxy currently sends HTTP/1.0 request and ideally should send
+ * Content-Length with request if request body is present, so
+ * send 411 Length Required if Content-Length missing. */
+ if (-1 == con->request.content_length) {
+ return connection_handle_read_post_error(srv, con, 411);
+ }
+ }
+ }
+
+ return ((0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
+ && hctx->state != GW_STATE_CONNECT_DELAYED)
+ ? gw_send_request(srv, hctx)
+ : HANDLER_WAIT_FOR_EVENT;
+}
+
+
+static handler_t gw_recv_response(server *srv, gw_handler_ctx *hctx) {
+ connection *con = hctx->remote_conn;
+ gw_proc *proc = hctx->proc;
+ gw_host *host = hctx->host;
+ /*(XXX: make this a configurable flag for other protocols)*/
+ buffer *b = hctx->opts.backend == BACKEND_FASTCGI
+ ? buffer_init()
+ : hctx->response;
+
+ switch (http_response_read(srv, hctx->remote_conn, &hctx->opts,
+ b, hctx->fd, &hctx->fde_ndx)) {
+ default:
+ break;
+ case HANDLER_FINISHED:
+ if (b != hctx->response) buffer_free(b);
+ if (hctx->gw_mode == GW_AUTHORIZER
+ && (200 == con->http_status || 0 == con->http_status)) {
+ /*
+ * If we are here in AUTHORIZER mode then a request for authorizer
+ * was processed already, and status 200 has been returned. We need
+ * now to handle authorized request.
+ */
+ buffer *physpath = NULL;
+
+ if (!buffer_string_is_empty(host->docroot)) {
+ buffer_copy_buffer(con->physical.doc_root, host->docroot);
+ buffer_copy_buffer(con->physical.basedir, host->docroot);
+
+ buffer_copy_buffer(con->physical.path, host->docroot);
+ buffer_append_string_buffer(con->physical.path, con->uri.path);
+ physpath = con->physical.path;
+ }
+
+ gw_backend_close(srv, hctx);
+ handler_ctx_clear(hctx);
+
+ /* don't do more than 6 loops here; normally shouldn't happen */
+ if (++con->loops_per_request > 5) {
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "too many loops while processing request:",
+ con->request.orig_uri);
+ con->http_status = 500; /* Internal Server Error */
+ con->mode = DIRECT;
+ return HANDLER_FINISHED;
+ }
+
+ /* restart the request so other handlers can process it */
+
+ if (physpath) con->physical.path = NULL;
+ connection_response_reset(srv,con);/*(includes con->http_status=0)*/
+ /* preserve con->physical.path with modified docroot */
+ if (physpath) con->physical.path = physpath;
+
+ /*(FYI: if multiple FastCGI authorizers were to be supported,
+ * next one could be started here instead of restarting request)*/
+
+ con->mode = DIRECT;
+ return HANDLER_COMEBACK;
+ } else {
+ /* we are done */
+ gw_connection_close(srv, hctx);
+ }
+
+ return HANDLER_FINISHED;
+ case HANDLER_COMEBACK: /*(not expected; treat as error)*/
+ case HANDLER_ERROR:
+ if (b != hctx->response) buffer_free(b);
+ if (proc->is_local && 1 == proc->load && proc->pid == hctx->pid
+ && proc->state != PROC_STATE_DIED) {
+ if (0 != gw_proc_waitpid(srv, host, proc)) {
+ if (hctx->conf.debug) {
+ log_error_write(srv, __FILE__, __LINE__, "ssbsdsd",
+ "--- gw spawning",
+ "\n\tsocket", proc->connection_name,
+ "\n\tcurrent:", 1, "/", host->num_procs);
+ }
+
+ if (gw_spawn_connection(srv, host, proc, hctx->conf.debug)) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "respawning failed, will retry later");
+ }
+ }
+ }
+
+ if (con->file_started == 0) {
+ /* nothing has been sent out yet, try to use another child */
+
+ if (hctx->wb->bytes_out == 0 &&
+ hctx->reconnects++ < 5) {
+
+ log_error_write(srv, __FILE__, __LINE__, "ssbsBSBs",
+ "response not received, request not sent",
+ "on socket:", proc->connection_name,
+ "for", con->uri.path, "?", con->uri.query, ", reconnecting");
+
+ return gw_reconnect(srv, hctx);
+ }
+
+ log_error_write(srv, __FILE__, __LINE__, "sosbsBSBs",
+ "response not received, request sent:", hctx->wb->bytes_out,
+ "on socket:", proc->connection_name, "for",
+ con->uri.path, "?", con->uri.query, ", closing connection");
+ } else {
+ log_error_write(srv, __FILE__, __LINE__, "ssbsBSBs",
+ "response already sent out, but backend returned error",
+ "on socket:", proc->connection_name, "for",
+ con->uri.path, "?", con->uri.query, ", terminating connection");
+ }
+
+ if (hctx->backend_error) hctx->backend_error(hctx);
+ http_response_backend_error(srv, con);
+ gw_connection_close(srv, hctx);
+ return HANDLER_FINISHED;
+ }
+
+ if (b != hctx->response) buffer_free(b);
+ return HANDLER_GO_ON;
+}
+
+
+static handler_t gw_handle_fdevent(server *srv, void *ctx, int revents) {
+ gw_handler_ctx *hctx = ctx;
+ connection *con = hctx->remote_conn;
+
+ joblist_append(srv, con);
+
+ if (revents & FDEVENT_IN) {
+ handler_t rc = gw_recv_response(srv, hctx); /*(might invalidate hctx)*/
+ if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
+ }
+
+ if (revents & FDEVENT_OUT) {
+ return gw_send_request(srv, hctx); /*(might invalidate hctx)*/
+ }
+
+ /* perhaps this issue is already handled */
+ if (revents & FDEVENT_HUP) {
+ if (hctx->state == GW_STATE_CONNECT_DELAYED) {
+ /* getoptsock will catch this one (right ?)
+ *
+ * if we are in connect we might get an EINPROGRESS
+ * in the first call and an FDEVENT_HUP in the
+ * second round
+ *
+ * FIXME: as it is a bit ugly.
+ *
+ */
+ gw_send_request(srv, hctx);
+ } else if (con->file_started) {
+ /* drain any remaining data from kernel pipe buffers
+ * even if (con->conf.stream_response_body
+ * & FDEVENT_STREAM_RESPONSE_BUFMIN)
+ * since event loop will spin on fd FDEVENT_HUP event
+ * until unregistered. */
+ handler_t rc;
+ do {
+ rc = gw_recv_response(srv,hctx); /*(might invalidate hctx)*/
+ } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
+ return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
+ } else {
+ gw_proc *proc = hctx->proc;
+ log_error_write(srv, __FILE__, __LINE__, "sBSbsbsd",
+ "error: unexpected close of gw connection for",
+ con->uri.path, "?", con->uri.query,
+ "(no gw process on socket:", proc->connection_name, "?)",
+ hctx->state);
+
+ gw_connection_close(srv, hctx);
+ }
+ } else if (revents & FDEVENT_ERR) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "gw: got a FDEVENT_ERR. Don't know why.");
+
+ if (hctx->backend_error) hctx->backend_error(hctx);
+ http_response_backend_error(srv, con);
+ gw_connection_close(srv, hctx);
+ }
+
+ return HANDLER_FINISHED;
+}
+
+handler_t gw_check_extension(server *srv, connection *con, gw_plugin_data *p, int uri_path_handler, size_t hctx_sz) {
+ #if 0 /*(caller must handle)*/
+ if (con->mode != DIRECT) return HANDLER_GO_ON;
+ gw_patch_connection(srv, con, p);
+ if (NULL == p->conf.exts) return HANDLER_GO_ON;
+ #endif
+
+ buffer *fn = uri_path_handler ? con->uri.path : con->physical.path;
+ size_t s_len = buffer_string_length(fn);
+ gw_extension *extension = NULL;
+ gw_host *host = NULL;
+ gw_handler_ctx *hctx;
+ unsigned short gw_mode;
+
+ if (0 == s_len) return HANDLER_GO_ON; /*(not expected)*/
+
+ /* check p->conf.exts_auth list and then p->conf.ext_resp list
+ * (skip p->conf.exts_auth if array is empty
+ * or if GW_AUTHORIZER already ran in this request) */
+ hctx = con->plugin_ctx[p->id];
+ /*(hctx not NULL if GW_AUTHORIZER ran; hctx->ext_auth check is redundant)*/
+ gw_mode = (NULL == hctx || NULL == hctx->ext_auth)
+ ? 0 /*GW_AUTHORIZER p->conf.exts_auth will be searched next*/
+ : GW_AUTHORIZER; /*GW_RESPONDER p->conf.exts_resp will be searched next*/
+
+ do {
+
+ gw_exts *exts;
+ if (0 == gw_mode) {
+ gw_mode = GW_AUTHORIZER;
+ exts = p->conf.exts_auth;
+ } else {
+ gw_mode = GW_RESPONDER;
+ exts = p->conf.exts_resp;
+ }
+
+ if (0 == exts->used) continue;
+
+ /* gw.map-extensions maps extensions to existing gw.server entries
+ *
+ * gw.map-extensions = ( ".php3" => ".php" )
+ *
+ * gw.server = ( ".php" => ... )
+ *
+ * */
+
+ /* check if extension-mapping matches */
+ if (p->conf.ext_mapping) {
+ for (size_t k = 0; k < p->conf.ext_mapping->used; ++k) {
+ data_string *ds = (data_string *)p->conf.ext_mapping->data[k];
+ size_t ct_len = buffer_string_length(ds->key);
+ if (s_len < ct_len) continue;
+
+ /* found a mapping */
+ if (0 == memcmp(fn->ptr+s_len-ct_len, ds->key->ptr, ct_len)) {
+ /* check if we know the extension */
+
+ /* we can reuse k here */
+ for (k = 0; k < exts->used; ++k) {
+ extension = exts->exts[k];
+
+ if (buffer_is_equal(ds->value, extension->key)) {
+ break;
+ }
+ }
+
+ if (k == exts->used) {
+ /* found nothing */
+ extension = NULL;
+ }
+ break;
+ }
+ }
+ }
+
+ if (extension == NULL) {
+ size_t uri_path_len = buffer_string_length(con->uri.path);
+
+ /* check if extension matches */
+ for (size_t k = 0; k < exts->used; ++k) {
+ gw_extension *ext = exts->exts[k];
+ size_t ct_len = buffer_string_length(ext->key);
+
+ /* check _url_ in the form "/gw_pattern" */
+ if (ext->key->ptr[0] == '/') {
+ if (ct_len <= uri_path_len
+ && 0==memcmp(con->uri.path->ptr,ext->key->ptr,ct_len)){
+ extension = ext;
+ break;
+ }
+ } else if (ct_len <= s_len
+ && 0 == memcmp(fn->ptr + s_len - ct_len,
+ ext->key->ptr, ct_len)) {
+ /* check extension in the form ".fcg" */
+ extension = ext;
+ break;
+ }
+ }
+ }
+
+ } while (NULL == extension && gw_mode != GW_RESPONDER);
+
+ /* extension doesn't match */
+ if (NULL == extension) {
+ return HANDLER_GO_ON;
+ }
+
+ /* check if we have at least one server for this extension up and running */
+ host = gw_host_get(srv, con, extension, p->conf.balance, p->conf.debug);
+ if (NULL == host) {
+ return HANDLER_FINISHED;
+ }
+
+ /* a note about no handler is not sent yet */
+ extension->note_is_sent = 0;
+
+ /*
+ * if check-local is disabled, use the uri.path handler
+ *
+ */
+
+ /* init handler-context */
+ if (uri_path_handler) {
+ if (host->check_local != 0) {
+ return HANDLER_GO_ON;
+ } else {
+ /* do not split path info for authorizer */
+ if (gw_mode != GW_AUTHORIZER) {
+ /* the prefix is the SCRIPT_NAME,
+ * everything from start to the next slash
+ * this is important for check-local = "disable"
+ *
+ * if prefix = /admin.gw
+ *
+ * /admin.gw/foo/bar
+ *
+ * SCRIPT_NAME = /admin.gw
+ * PATH_INFO = /foo/bar
+ *
+ * if prefix = /cgi-bin/
+ *
+ * /cgi-bin/foo/bar
+ *
+ * SCRIPT_NAME = /cgi-bin/foo
+ * PATH_INFO = /bar
+ *
+ * if prefix = /, and fix-root-path-name is enable
+ *
+ * /cgi-bin/foo/bar
+ *
+ * SCRIPT_NAME = /cgi-bin/foo
+ * PATH_INFO = /bar
+ *
+ */
+ char *pathinfo;
+
+ /* the rewrite is only done for /prefix/? matches */
+ if (host->fix_root_path_name && extension->key->ptr[0] == '/'
+ && extension->key->ptr[1] == '\0'){
+ buffer_copy_buffer(con->request.pathinfo, con->uri.path);
+ buffer_string_set_length(con->uri.path, 0);
+ } else if (extension->key->ptr[0] == '/'
+ && buffer_string_length(con->uri.path)
+ > buffer_string_length(extension->key)
+ && (pathinfo =
+ strchr(con->uri.path->ptr
+ + buffer_string_length(extension->key),
+ '/')) != NULL) {
+ /* rewrite uri.path and pathinfo */
+
+ buffer_copy_string(con->request.pathinfo, pathinfo);
+ buffer_string_set_length(
+ con->uri.path,
+ buffer_string_length(con->uri.path)
+ - buffer_string_length(con->request.pathinfo));
+ }
+ }
+ }
+ }
+
+ if (!hctx) hctx = handler_ctx_init(hctx_sz);
+
+ hctx->remote_conn = con;
+ hctx->plugin_data = p;
+ hctx->host = host;
+ hctx->proc = NULL;
+ hctx->ext = extension;
+ gw_host_assign(srv, host);
+
+ hctx->gw_mode = gw_mode;
+ if (gw_mode == GW_AUTHORIZER) {
+ hctx->ext_auth = hctx->ext;
+ }
+
+ /*hctx->conf.exts = p->conf.exts;*/
+ /*hctx->conf.exts_auth = p->conf.exts_auth;*/
+ /*hctx->conf.exts_resp = p->conf.exts_resp;*/
+ /*hctx->conf.ext_mapping = p->conf.ext_mapping;*/
+ hctx->conf.balance = p->conf.balance;
+ hctx->conf.proto = p->conf.proto;
+ hctx->conf.debug = p->conf.debug;
+
+ hctx->opts.fdfmt = S_IFSOCK;
+ hctx->opts.authorizer = (gw_mode == GW_AUTHORIZER);
+ hctx->opts.local_redir = 0;
+ hctx->opts.xsendfile_allow = host->xsendfile_allow;
+ hctx->opts.xsendfile_docroot = host->xsendfile_docroot;
+
+ con->plugin_ctx[p->id] = hctx;
+
+ con->mode = p->id;
+
+ if (con->conf.log_request_handling) {
+ log_error_write(srv, __FILE__, __LINE__, "s", "handling it in mod_gw");
+ }
+
+ return HANDLER_GO_ON;
+}
+
+static void gw_handle_trigger_host(server *srv, gw_host *host, int debug) {
+ /*
+ * TODO:
+ *
+ * - add timeout for a connect to a non-gw process
+ * (use state_timestamp + state)
+ *
+ * perhaps we should kill a connect attempt after 10-15 seconds
+ *
+ * currently we wait for the TCP timeout which is 180 seconds on Linux
+ */
+
+ /* check each child proc to detect if proc exited */
+
+ gw_proc *proc;
+ time_t idle_timestamp;
+ unsigned long sum_load = 0;
+
+ for (proc = host->first; proc; proc = proc->next) {
+ gw_proc_waitpid(srv, host, proc);
+ }
+
+ gw_restart_dead_procs(srv, host, debug);
+
+ if (buffer_string_is_empty(host->bin_path)) return;
+
+ for (proc = host->first; proc; proc = proc->next) {
+ sum_load += proc->load;
+ }
+
+ if (host->num_procs && host->num_procs < host->max_procs
+ && (sum_load / host->num_procs) > host->max_load_per_proc) {
+ /* overload, spawn new child */
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "overload detected, spawning a new child");
+ }
+
+ gw_proc_spawn(srv, host, debug);
+ }
+
+ idle_timestamp = srv->cur_ts - host->idle_timeout;
+ for (proc = host->first; proc; proc = proc->next) {
+ if (host->num_procs <= host->min_procs) break;
+ if (0 != proc->load) continue;
+ if (proc->pid <= 0) continue;
+ if (proc->last_used >= idle_timestamp) continue;
+
+ /* terminate proc that has been idling for a long time */
+ if (debug) {
+ log_error_write(srv, __FILE__, __LINE__, "ssbsd",
+ "idle-timeout reached, terminating child:",
+ "socket:", proc->unixsocket, "pid", proc->pid);
+ }
+
+ gw_proc_kill(srv, host, proc);
+
+ /* proc is now in unused, let next second handle next process */
+ break;
+ }
+
+ for (proc = host->unused_procs; proc; proc = proc->next) {
+ gw_proc_waitpid(srv, host, proc);
+ }
+}
+
+void gw_handle_trigger_exts(server *srv, gw_exts *exts, int debug) {
+ for (size_t j = 0; j < exts->used; ++j) {
+ gw_extension *ex = exts->exts[j];
+ for (size_t n = 0; n < ex->used; ++n) {
+ gw_handle_trigger_host(srv, ex->hosts[n], debug);
+ }
+ }
+}
+
+handler_t gw_handle_trigger(server *srv, void *p_d) {
+ gw_plugin_data *p = p_d;
+ for (size_t i = 0; i < srv->config_context->used; i++) {
+ gw_plugin_config *conf = p->config_storage[i];
+ gw_exts *exts = conf->exts;
+ int debug = conf->debug ? conf->debug : p->config_storage[0]->debug;
+ if (NULL == exts) continue;
+ gw_handle_trigger_exts(srv, exts, debug);
+ }
+
+ return HANDLER_GO_ON;
+}
diff --git a/src/gw_backend.h b/src/gw_backend.h
new file mode 100644
index 00000000..d196939e
--- /dev/null
+++ b/src/gw_backend.h
@@ -0,0 +1,343 @@
+#ifndef INCLUDED_GW_BACKEND_H
+#define INCLUDED_GW_BACKEND_H
+
+#include "first.h"
+
+#include <sys/types.h>
+
+#include "array.h"
+#include "buffer.h"
+
+typedef struct {
+ char **ptr;
+
+ size_t size;
+ size_t used;
+} char_array;
+
+typedef struct gw_proc {
+ size_t id; /* id will be between 1 and max_procs */
+ buffer *unixsocket; /* config.socket + "-" + id */
+ unsigned port; /* config.port + pno */
+
+ /* either tcp:<host>:<port> or unix:<socket> for debugging purposes */
+ buffer *connection_name;
+
+ pid_t pid; /* PID of the spawned process (0 if not spawned locally) */
+
+
+ size_t load; /* number of requests waiting on this process */
+
+ time_t last_used; /* see idle_timeout */
+ size_t requests; /* see max_requests */
+ struct gw_proc *prev, *next; /* see first */
+
+ time_t disabled_until; /* proc disabled until given time */
+
+ int is_local;
+
+ enum {
+ PROC_STATE_RUNNING, /* alive */
+ PROC_STATE_OVERLOADED, /* listen-queue is full */
+ PROC_STATE_DIED_WAIT_FOR_PID, /* */
+ PROC_STATE_DIED, /* marked as dead, should be restarted */
+ PROC_STATE_KILLED /* killed (signal sent to proc) */
+ } state;
+} gw_proc;
+
+typedef struct {
+ /* the key that is used to reference this value */
+ buffer *id;
+
+ /* list of processes handling this extension
+ * sorted by lowest load
+ *
+ * whenever a job is done move it up in the list
+ * until it is sorted, move it down as soon as the
+ * job is started
+ */
+ gw_proc *first;
+ gw_proc *unused_procs;
+
+ /*
+ * spawn at least min_procs, at max_procs.
+ *
+ * as soon as the load of the first entry
+ * is max_load_per_proc we spawn a new one
+ * and add it to the first entry and give it
+ * the load
+ *
+ */
+
+ unsigned short min_procs;
+ unsigned short max_procs;
+ size_t num_procs; /* how many procs are started */
+ size_t active_procs; /* how many procs in state PROC_STATE_RUNNING */
+
+ unsigned short max_load_per_proc;
+
+ /*
+ * kick the process from the list if it was not
+ * used for idle_timeout until min_procs is
+ * reached. this helps to get the processlist
+ * small again we had a small peak load.
+ *
+ */
+
+ unsigned short idle_timeout;
+
+ /*
+ * time after a disabled remote connection is tried to be re-enabled
+ *
+ *
+ */
+
+ unsigned short disable_time;
+
+ /*
+ * some gw processes get a little bit larger
+ * than wanted. max_requests_per_proc kills a
+ * process after a number of handled requests.
+ *
+ */
+ size_t max_requests_per_proc;
+
+
+ /* config */
+
+ /*
+ * host:port
+ *
+ * if host is one of the local IP adresses the
+ * whole connection is local
+ *
+ * if port is not 0, and host is not specified,
+ * "localhost" (INADDR_LOOPBACK) is assumed.
+ *
+ */
+ buffer *host;
+ unsigned short port;
+ unsigned short family; /* sa_family_t */
+
+ /*
+ * Unix Domain Socket
+ *
+ * instead of TCP/IP we can use Unix Domain Sockets
+ * - more secure (you have fileperms to play with)
+ * - more control (on locally)
+ * - more speed (no extra overhead)
+ */
+ buffer *unixsocket;
+
+ /* if socket is local we can start the gw process ourself
+ *
+ * bin-path is the path to the binary
+ *
+ * check min_procs and max_procs for the number
+ * of process to start up
+ */
+ buffer *bin_path;
+
+ /* bin-path is set bin-environment is taken to
+ * create the environement before starting the
+ * FastCGI process
+ *
+ */
+ array *bin_env;
+
+ array *bin_env_copy;
+
+ /*
+ * docroot-translation between URL->phys and the
+ * remote host
+ *
+ * reasons:
+ * - different dir-layout if remote
+ * - chroot if local
+ *
+ */
+ buffer *docroot;
+
+ /*
+ * check_local tells you if the phys file is stat()ed
+ * or not. FastCGI doesn't care if the service is
+ * remote. If the web-server side doesn't contain
+ * the FastCGI-files we should not stat() for them
+ * and say '404 not found'.
+ */
+ unsigned short check_local;
+
+ /*
+ * append PATH_INFO to SCRIPT_FILENAME
+ *
+ * php needs this if cgi.fix_pathinfo is provided
+ *
+ */
+
+ unsigned short break_scriptfilename_for_php;
+
+ /*
+ * workaround for program when prefix="/"
+ *
+ * rule to build PATH_INFO is hardcoded for when check_local is disabled
+ * enable this option to use the workaround
+ *
+ */
+
+ unsigned short fix_root_path_name;
+
+ /*
+ * If the backend includes X-Sendfile in the response
+ * we use the value as filename and ignore the content.
+ *
+ */
+ unsigned short xsendfile_allow;
+ array *xsendfile_docroot;
+
+ ssize_t load;
+
+ size_t max_id; /* corresponds most of the time to num_procs */
+
+ buffer *strip_request_uri;
+
+ unsigned short kill_signal; /* we need a setting for this as libfcgi
+ applications prefer SIGUSR1 while the
+ rest of the world would use SIGTERM
+ *sigh* */
+
+ int listen_backlog;
+ int refcount;
+
+ char_array args;
+} gw_host;
+
+/*
+ * one extension can have multiple hosts assigned
+ * one host can spawn additional processes on the same
+ * socket (if we control it)
+ *
+ * ext -> host -> procs
+ * 1:n 1:n
+ *
+ * if the gw process is remote that whole goes down
+ * to
+ *
+ * ext -> host -> procs
+ * 1:n 1:1
+ *
+ * in case of PHP and FCGI_CHILDREN we have again a procs
+ * but we don't control it directly.
+ *
+ */
+
+typedef struct {
+ buffer *key; /* like .php */
+
+ int note_is_sent;
+ int last_used_ndx;
+
+ gw_host **hosts;
+
+ size_t used;
+ size_t size;
+} gw_extension;
+
+typedef struct {
+ gw_extension **exts;
+
+ size_t used;
+ size_t size;
+} gw_exts;
+
+
+
+
+#include "base.h"
+#include "plugin.h"
+#include "response.h"
+
+typedef struct gw_plugin_config {
+ gw_exts *exts;
+ gw_exts *exts_auth;
+ gw_exts *exts_resp;
+
+ array *ext_mapping;
+
+ int balance;
+ int proto;
+ int debug;
+} gw_plugin_config;
+
+/* generic plugin data, shared between all connections */
+typedef struct gw_plugin_data {
+ PLUGIN_DATA;
+ gw_plugin_config **config_storage;
+
+ gw_plugin_config conf; /* used only as long as no gw_handler_ctx is setup */
+} gw_plugin_data;
+
+/* connection specific data */
+typedef enum {
+ GW_STATE_INIT,
+ GW_STATE_CONNECT_DELAYED,
+ GW_STATE_PREPARE_WRITE,
+ GW_STATE_WRITE,
+ GW_STATE_READ
+} gw_connection_state_t;
+
+#define GW_RESPONDER 1
+#define GW_AUTHORIZER 2
+#define GW_FILTER 3 /*(not implemented)*/
+
+typedef struct gw_handler_ctx {
+ gw_proc *proc;
+ gw_host *host;
+ gw_extension *ext;
+ gw_extension *ext_auth; /* (future: might allow multiple authorizers)*/
+ unsigned short gw_mode; /* mode: GW_AUTHORIZER or GW_RESPONDER */
+
+ gw_connection_state_t state;
+ time_t state_timestamp;
+
+ chunkqueue *rb; /* read queue */
+ chunkqueue *wb; /* write queue */
+ off_t wb_reqlen;
+
+ buffer *response;
+
+ int fd; /* fd to the gw process */
+ int fde_ndx; /* index into the fd-event buffer */
+
+ pid_t pid;
+ int got_proc;
+ int reconnects; /* number of reconnect attempts */
+
+ int request_id;
+ int send_content_body;
+
+ http_response_opts opts;
+ gw_plugin_config conf;
+
+ connection *remote_conn; /* dumb pointer */
+ gw_plugin_data *plugin_data; /* dumb pointer */
+ handler_t(*stdin_append)(server *srv, struct gw_handler_ctx *hctx);
+ handler_t(*create_env)(server *srv, struct gw_handler_ctx *hctx);
+ void(*backend_error)(struct gw_handler_ctx *hctx);
+ void(*handler_ctx_free)(void *hctx);
+} gw_handler_ctx;
+
+
+void * gw_init(void);
+void gw_plugin_config_free(gw_plugin_config *s);
+handler_t gw_free(server *srv, void *p_d);
+int gw_set_defaults_backend(server *srv, gw_plugin_data *p, data_unset *du, size_t i, int sh_exec);
+int gw_set_defaults_balance(server *srv, gw_plugin_config *s, data_unset *du);
+handler_t gw_check_extension(server *srv, connection *con, gw_plugin_data *p, int uri_path_handler, size_t hctx_sz);
+handler_t gw_connection_reset(server *srv, connection *con, void *p_d);
+handler_t gw_handle_subrequest(server *srv, connection *con, void *p_d);
+void gw_handle_trigger_exts(server *srv, gw_exts *exts, int debug);
+handler_t gw_handle_trigger(server *srv, void *p_d);
+
+void gw_set_transparent(server *srv, gw_handler_ctx *hctx);
+
+#endif
diff --git a/src/http-header-glue.c b/src/http-header-glue.c
index ce7a768d..edbabe00 100644
--- a/src/http-header-glue.c
+++ b/src/http-header-glue.c
@@ -1191,7 +1191,8 @@ handler_t http_response_parse_headers(server *srv, connection *con, http_respons
}
}
- return HANDLER_GO_ON;
+ /* (callback for response headers complete) */
+ return (opts->headers) ? opts->headers(srv, con, opts) : HANDLER_GO_ON;
}
diff --git a/src/mod_cgi.c b/src/mod_cgi.c
index 1d2c050f..897d3b4d 100644
--- a/src/mod_cgi.c
+++ b/src/mod_cgi.c
@@ -37,8 +37,6 @@ static int pipe_cloexec(int pipefd[2]) {
: -1;
}
-enum {EOL_UNSET, EOL_N, EOL_RN};
-
typedef struct {
char **ptr;
@@ -413,16 +411,9 @@ static handler_t cgi_handle_fdevent_send (server *srv, void *ctx, int revents) {
}
-static handler_t cgi_response_read(server *srv, handler_ctx *hctx) {
- connection * const con = hctx->remote_conn;
- const int file_started = con->file_started;
- const handler_t rc =
- http_response_read(srv, con, &hctx->opts,
- hctx->response, hctx->fd, &hctx->fde_ndx);
-
- if (file_started || !con->file_started || con->mode == DIRECT) return rc;
-
+static handler_t cgi_response_headers(server *srv, connection *con, struct http_response_opts_t *opts) {
/* response headers just completed */
+ handler_ctx *hctx = (handler_ctx *)opts->pdata;
if (con->parsed_response & HTTP_UPGRADE) {
if (hctx->conf.upgrade && con->http_status == 101) {
@@ -449,12 +440,13 @@ static handler_t cgi_response_read(server *srv, handler_ctx *hctx) {
}
}
- return rc;
+ return HANDLER_GO_ON;
}
static int cgi_recv_response(server *srv, handler_ctx *hctx) {
- switch (cgi_response_read(srv, hctx)) {
+ switch (http_response_read(srv, hctx->remote_conn, &hctx->opts,
+ hctx->response, hctx->fd, &hctx->fde_ndx)) {
default:
return HANDLER_GO_ON;
case HANDLER_ERROR:
@@ -1008,6 +1000,8 @@ URIHANDLER_FUNC(cgi_is_handled) {
hctx->opts.local_redir = hctx->conf.local_redir;
hctx->opts.xsendfile_allow = hctx->conf.xsendfile_allow;
hctx->opts.xsendfile_docroot = hctx->conf.xsendfile_docroot;
+ hctx->opts.pdata = hctx;
+ hctx->opts.headers = cgi_response_headers;
con->plugin_ctx[p->id] = hctx;
con->mode = p->id;
}
diff --git a/src/mod_fastcgi.c b/src/mod_fastcgi.c
index 24beadc2..8bcd4757 100644
--- a/src/mod_fastcgi.c
+++ b/src/mod_fastcgi.c
@@ -1,26 +1,21 @@
#include "first.h"
-#include "buffer.h"
-#include "server.h"
-#include "keyvalue.h"
-#include "log.h"
-
-#include "http_chunk.h"
-#include "fdevent.h"
-#include "connections.h"
-#include "response.h"
-#include "joblist.h"
-#include "inet_ntop_cache.h"
-
-#include "plugin.h"
-
#include <sys/types.h>
-#include <unistd.h>
#include <errno.h>
-#include <fcntl.h>
#include <string.h>
-#include <stdlib.h>
-#include <signal.h>
+
+#include "gw_backend.h"
+typedef gw_plugin_config plugin_config;
+typedef gw_plugin_data plugin_data;
+typedef gw_handler_ctx handler_ctx;
+
+#include "base.h"
+#include "buffer.h"
+#include "fdevent.h"
+#include "http_chunk.h"
+#include "log.h"
+#include "plugin.h"
+#include "status_counter.h"
#ifdef HAVE_FASTCGI_FASTCGI_H
# include <fastcgi/fastcgi.h>
@@ -32,1130 +27,38 @@
# endif
#endif /* HAVE_FASTCGI_FASTCGI_H */
-#include "sys-socket.h"
-
-#ifdef HAVE_SYS_UIO_H
-#include <sys/uio.h>
+#if GW_RESPONDER != FCGI_RESPONDER
+#error "mismatched defines: (GW_RESPONDER != FCGI_RESPONDER)"
#endif
-#ifdef HAVE_SYS_WAIT_H
-#include <sys/wait.h>
+#if GW_AUTHORIZER != FCGI_AUTHORIZER
+#error "mismatched defines: (GW_AUTHORIZER != FCGI_AUTHORIZER)"
+#endif
+#if GW_FILTER != FCGI_FILTER
+#error "mismatched defines: (GW_FILTER != FCGI_FILTER)"
#endif
-
-/*
- *
- * TODO:
- *
- * - add timeout for a connect to a non-fastcgi process
- * (use state_timestamp + state)
- *
- */
-
-typedef struct {
- char **ptr;
-
- size_t size;
- size_t used;
-} char_array;
-
-typedef struct fcgi_proc {
- size_t id; /* id will be between 1 and max_procs */
- buffer *unixsocket; /* config.socket + "-" + id */
- unsigned port; /* config.port + pno */
-
- buffer *connection_name; /* either tcp:<host>:<port> or unix:<socket> for debugging purposes */
-
- pid_t pid; /* PID of the spawned process (0 if not spawned locally) */
-
-
- size_t load; /* number of requests waiting on this process */
-
- time_t last_used; /* see idle_timeout */
- size_t requests; /* see max_requests */
- struct fcgi_proc *prev, *next; /* see first */
-
- time_t disabled_until; /* this proc is disabled until, use something else until then */
-
- int is_local;
-
- enum {
- PROC_STATE_RUNNING, /* alive */
- PROC_STATE_OVERLOADED, /* listen-queue is full,
- don't send anything to this proc for the next 2 seconds */
- PROC_STATE_DIED_WAIT_FOR_PID, /* */
- PROC_STATE_DIED, /* marked as dead, should be restarted */
- PROC_STATE_KILLED /* was killed as we don't have the load anymore */
- } state;
-} fcgi_proc;
-
-typedef struct {
- /* the key that is used to reference this value */
- buffer *id;
-
- /* list of processes handling this extension
- * sorted by lowest load
- *
- * whenever a job is done move it up in the list
- * until it is sorted, move it down as soon as the
- * job is started
- */
- fcgi_proc *first;
- fcgi_proc *unused_procs;
-
- /*
- * spawn at least min_procs, at max_procs.
- *
- * as soon as the load of the first entry
- * is max_load_per_proc we spawn a new one
- * and add it to the first entry and give it
- * the load
- *
- */
-
- unsigned short min_procs;
- unsigned short max_procs;
- size_t num_procs; /* how many procs are started */
- size_t active_procs; /* how many of them are really running, i.e. state = PROC_STATE_RUNNING */
-
- unsigned short max_load_per_proc;
-
- /*
- * kick the process from the list if it was not
- * used for idle_timeout until min_procs is
- * reached. this helps to get the processlist
- * small again we had a small peak load.
- *
- */
-
- unsigned short idle_timeout;
-
- /*
- * time after a disabled remote connection is tried to be re-enabled
- *
- *
- */
-
- unsigned short disable_time;
-
- /*
- * some fastcgi processes get a little bit larger
- * than wanted. max_requests_per_proc kills a
- * process after a number of handled requests.
- *
- */
- size_t max_requests_per_proc;
-
-
- /* config */
-
- /*
- * host:port
- *
- * if host is one of the local IP adresses the
- * whole connection is local
- *
- * if port is not 0, and host is not specified,
- * "localhost" (INADDR_LOOPBACK) is assumed.
- *
- */
- buffer *host;
- unsigned short port;
- sa_family_t family;
-
- /*
- * Unix Domain Socket
- *
- * instead of TCP/IP we can use Unix Domain Sockets
- * - more secure (you have fileperms to play with)
- * - more control (on locally)
- * - more speed (no extra overhead)
- */
- buffer *unixsocket;
-
- /* if socket is local we can start the fastcgi
- * process ourself
- *
- * bin-path is the path to the binary
- *
- * check min_procs and max_procs for the number
- * of process to start up
- */
- buffer *bin_path;
-
- /* bin-path is set bin-environment is taken to
- * create the environement before starting the
- * FastCGI process
- *
- */
- array *bin_env;
-
- array *bin_env_copy;
-
- /*
- * docroot-translation between URL->phys and the
- * remote host
- *
- * reasons:
- * - different dir-layout if remote
- * - chroot if local
- *
- */
- buffer *docroot;
-
- /*
- * check_local tells you if the phys file is stat()ed
- * or not. FastCGI doesn't care if the service is
- * remote. If the web-server side doesn't contain
- * the fastcgi-files we should not stat() for them
- * and say '404 not found'.
- */
- unsigned short check_local;
-
- /*
- * append PATH_INFO to SCRIPT_FILENAME
- *
- * php needs this if cgi.fix_pathinfo is provided
- *
- */
-
- unsigned short break_scriptfilename_for_php;
-
- /*
- * workaround for program when prefix="/"
- *
- * rule to build PATH_INFO is hardcoded for when check_local is disabled
- * enable this option to use the workaround
- *
- */
-
- unsigned short fix_root_path_name;
-
- /*
- * If the backend includes X-Sendfile in the response
- * we use the value as filename and ignore the content.
- *
- */
- unsigned short xsendfile_allow;
- array *xsendfile_docroot;
-
- ssize_t load;
-
- size_t max_id; /* corresponds most of the time to num_procs */
-
- buffer *strip_request_uri;
-
- unsigned short kill_signal; /* we need a setting for this as libfcgi
- applications prefer SIGUSR1 while the
- rest of the world would use SIGTERM
- *sigh* */
-
- int listen_backlog;
- int refcount;
-
- char_array args;
-} fcgi_extension_host;
-
-/*
- * one extension can have multiple hosts assigned
- * one host can spawn additional processes on the same
- * socket (if we control it)
- *
- * ext -> host -> procs
- * 1:n 1:n
- *
- * if the fastcgi process is remote that whole goes down
- * to
- *
- * ext -> host -> procs
- * 1:n 1:1
- *
- * in case of PHP and FCGI_CHILDREN we have again a procs
- * but we don't control it directly.
- *
- */
-
-typedef struct {
- buffer *key; /* like .php */
-
- int note_is_sent;
- int last_used_ndx;
-
- fcgi_extension_host **hosts;
-
- size_t used;
- size_t size;
-} fcgi_extension;
-
-typedef struct {
- fcgi_extension **exts;
-
- size_t used;
- size_t size;
-} fcgi_exts;
-
-
-typedef struct {
- fcgi_exts *exts;
- fcgi_exts *exts_auth;
- fcgi_exts *exts_resp;
-
- array *ext_mapping;
-
- unsigned int debug;
-} plugin_config;
-
-/* generic plugin data, shared between all connections */
-typedef struct {
- PLUGIN_DATA;
-
- plugin_config **config_storage;
-
- plugin_config conf; /* this is only used as long as no handler_ctx is setup */
-} plugin_data;
-
-/* connection specific data */
-typedef enum {
- FCGI_STATE_INIT,
- FCGI_STATE_CONNECT_DELAYED,
- FCGI_STATE_PREPARE_WRITE,
- FCGI_STATE_WRITE,
- FCGI_STATE_READ
-} fcgi_connection_state_t;
-
-typedef struct {
- fcgi_proc *proc;
- fcgi_extension_host *host;
- fcgi_extension *ext;
- fcgi_extension *ext_auth; /* (might be used in future to allow multiple authorizers)*/
- unsigned short fcgi_mode; /* FastCGI mode: FCGI_AUTHORIZER or FCGI_RESPONDER */
-
- fcgi_connection_state_t state;
- time_t state_timestamp;
-
- chunkqueue *rb; /* read queue */
- chunkqueue *wb; /* write queue */
- off_t wb_reqlen;
-
- buffer *response_header;
-
- int fd; /* fd to the fastcgi process */
- int fde_ndx; /* index into the fd-event buffer */
-
- pid_t pid;
- int got_proc;
- int reconnects; /* number of reconnect attempts */
-
- int request_id;
- int send_content_body;
-
- http_response_opts opts;
- plugin_config conf;
-
- connection *remote_conn; /* dumb pointer */
- plugin_data *plugin_data; /* dumb pointer */
-} handler_ctx;
-
-
-#include "status_counter.h"
-
-static data_integer * fastcgi_status_get_di(server *srv, fcgi_extension_host *host, fcgi_proc *proc, const char *tag, size_t len) {
- buffer *b = srv->tmp_buf;
- buffer_copy_string_len(b, CONST_STR_LEN("fastcgi.backend."));
- buffer_append_string_buffer(b, host->id);
- if (proc) {
- buffer_append_string_len(b, CONST_STR_LEN("."));
- buffer_append_int(b, proc->id);
- }
- buffer_append_string_len(b, tag, len);
- return status_counter_get_counter(srv, CONST_BUF_LEN(b));
-}
-
-static void fcgi_proc_tag_inc(server *srv, handler_ctx *hctx, const char *tag, size_t len) {
- data_integer *di = fastcgi_status_get_di(srv, hctx->host, hctx->proc, tag, len);
- ++di->value;
-}
-
-static void fcgi_proc_load_inc(server *srv, handler_ctx *hctx) {
- data_integer *di = fastcgi_status_get_di(srv, hctx->host, hctx->proc, CONST_STR_LEN(".load"));
- di->value = ++hctx->proc->load;
-
- status_counter_inc(srv, CONST_STR_LEN("fastcgi.active-requests"));
-}
-
-static void fcgi_proc_load_dec(server *srv, handler_ctx *hctx) {
- data_integer *di = fastcgi_status_get_di(srv, hctx->host, hctx->proc, CONST_STR_LEN(".load"));
- di->value = --hctx->proc->load;
-
- status_counter_dec(srv, CONST_STR_LEN("fastcgi.active-requests"));
-}
-
-static void fcgi_host_assign(server *srv, handler_ctx *hctx) {
- data_integer *di = fastcgi_status_get_di(srv, hctx->host, NULL, CONST_STR_LEN(".load"));
- di->value = ++hctx->host->load;
-}
-
-static void fcgi_host_reset(server *srv, handler_ctx *hctx) {
- data_integer *di = fastcgi_status_get_di(srv, hctx->host, NULL, CONST_STR_LEN(".load"));
- di->value = --hctx->host->load;
-}
-
-static int fastcgi_status_init(server *srv, fcgi_extension_host *host, fcgi_proc *proc) {
- fastcgi_status_get_di(srv, host, proc, CONST_STR_LEN(".disabled"))->value = 0;
- fastcgi_status_get_di(srv, host, proc, CONST_STR_LEN(".died"))->value = 0;
- fastcgi_status_get_di(srv, host, proc, CONST_STR_LEN(".overloaded"))->value = 0;
- fastcgi_status_get_di(srv, host, proc, CONST_STR_LEN(".connected"))->value = 0;
- fastcgi_status_get_di(srv, host, proc, CONST_STR_LEN(".load"))->value = 0;
-
- fastcgi_status_get_di(srv, host, NULL, CONST_STR_LEN(".load"))->value = 0;
-
- return 0;
-}
-
-
-/* ok, we need a prototype */
-static handler_t fcgi_handle_fdevent(server *srv, void *ctx, int revents);
-
-
-static handler_ctx * handler_ctx_init(void) {
- handler_ctx * hctx;
-
- hctx = calloc(1, sizeof(*hctx));
- force_assert(hctx);
-
- hctx->fde_ndx = -1;
-
- /*hctx->response_header = buffer_init();*//*(allocated when needed)*/
-
- hctx->request_id = 0;
- hctx->fcgi_mode = FCGI_RESPONDER;
- hctx->state = FCGI_STATE_INIT;
- hctx->proc = NULL;
-
- hctx->fd = -1;
-
- hctx->reconnects = 0;
- hctx->send_content_body = 1;
-
- hctx->rb = chunkqueue_init();
- hctx->wb = chunkqueue_init();
- hctx->wb_reqlen = 0;
-
- return hctx;
-}
-
-static void handler_ctx_free(handler_ctx *hctx) {
- /* caller MUST have called fcgi_backend_close(srv, hctx) if necessary */
- buffer_free(hctx->response_header);
-
- chunkqueue_free(hctx->rb);
- chunkqueue_free(hctx->wb);
-
- free(hctx);
-}
-
-static void handler_ctx_clear(handler_ctx *hctx) {
- /* caller MUST have called fcgi_backend_close(srv, hctx) if necessary */
-
- hctx->proc = NULL;
- hctx->host = NULL;
- hctx->ext = NULL;
- /*hctx->ext_auth is intentionally preserved to flag prior authorizer*/
-
- hctx->fcgi_mode = FCGI_RESPONDER;
- hctx->state = FCGI_STATE_INIT;
- /*hctx->state_timestamp = 0;*//*(unused; left as-is)*/
-
- chunkqueue_reset(hctx->rb);
- chunkqueue_reset(hctx->wb);
- hctx->wb_reqlen = 0;
-
- buffer_reset(hctx->response_header);
-
- hctx->fd = -1;
- hctx->fde_ndx = -1;
- hctx->got_proc = 0;
- hctx->reconnects = 0;
- hctx->request_id = 0;
- hctx->send_content_body = 1;
-
- /*plugin_config conf;*//*(no need to reset for same request)*/
-
- /*hctx->remote_conn = NULL;*//*(no need to reset for same request)*/
- /*hctx->plugin_data = NULL;*//*(no need to reset for same request)*/
-}
-
-static fcgi_proc *fastcgi_process_init(void) {
- fcgi_proc *f;
-
- f = calloc(1, sizeof(*f));
- f->unixsocket = buffer_init();
- f->connection_name = buffer_init();
-
- f->prev = NULL;
- f->next = NULL;
- f->state = PROC_STATE_DIED;
-
- return f;
-}
-
-static void fastcgi_process_free(fcgi_proc *f) {
- if (!f) return;
-
- fastcgi_process_free(f->next);
-
- buffer_free(f->unixsocket);
- buffer_free(f->connection_name);
-
- free(f);
-}
-
-static fcgi_extension_host *fastcgi_host_init(void) {
- fcgi_extension_host *f;
-
- f = calloc(1, sizeof(*f));
-
- f->id = buffer_init();
- f->host = buffer_init();
- f->unixsocket = buffer_init();
- f->docroot = buffer_init();
- f->bin_path = buffer_init();
- f->bin_env = array_init();
- f->bin_env_copy = array_init();
- f->strip_request_uri = buffer_init();
- f->xsendfile_docroot = array_init();
-
- return f;
-}
-
-static void fastcgi_host_free(fcgi_extension_host *h) {
- if (!h) return;
- if (h->refcount) {
- --h->refcount;
- return;
- }
-
- buffer_free(h->id);
- buffer_free(h->host);
- buffer_free(h->unixsocket);
- buffer_free(h->docroot);
- buffer_free(h->bin_path);
- buffer_free(h->strip_request_uri);
- array_free(h->bin_env);
- array_free(h->bin_env_copy);
- array_free(h->xsendfile_docroot);
-
- fastcgi_process_free(h->first);
- fastcgi_process_free(h->unused_procs);
-
- for (size_t i = 0; i < h->args.used; ++i) free(h->args.ptr[i]);
- free(h->args.ptr);
- free(h);
-}
-
-static fcgi_exts *fastcgi_extensions_init(void) {
- fcgi_exts *f;
-
- f = calloc(1, sizeof(*f));
-
- return f;
-}
-
-static void fastcgi_extensions_free(fcgi_exts *f) {
- size_t i;
-
- if (!f) return;
-
- for (i = 0; i < f->used; i++) {
- fcgi_extension *fe;
- size_t j;
-
- fe = f->exts[i];
-
- for (j = 0; j < fe->used; j++) {
- fcgi_extension_host *h;
-
- h = fe->hosts[j];
-
- fastcgi_host_free(h);
- }
-
- buffer_free(fe->key);
- free(fe->hosts);
-
- free(fe);
- }
-
- free(f->exts);
-
- free(f);
-}
-
-static int fastcgi_extension_insert(fcgi_exts *ext, buffer *key, fcgi_extension_host *fh) {
- fcgi_extension *fe;
- size_t i;
-
- /* there is something */
-
- for (i = 0; i < ext->used; i++) {
- if (buffer_is_equal(key, ext->exts[i]->key)) {
- break;
- }
- }
-
- if (i == ext->used) {
- /* filextension is new */
- fe = calloc(1, sizeof(*fe));
- force_assert(fe);
- fe->key = buffer_init();
- fe->last_used_ndx = -1;
- buffer_copy_buffer(fe->key, key);
-
- /* */
-
- if (ext->size == 0) {
- ext->size = 8;
- ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
- force_assert(ext->exts);
- } else if (ext->used == ext->size) {
- ext->size += 8;
- ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
- force_assert(ext->exts);
- }
- ext->exts[ext->used++] = fe;
- } else {
- fe = ext->exts[i];
- }
-
- if (fe->size == 0) {
- fe->size = 4;
- fe->hosts = malloc(fe->size * sizeof(*(fe->hosts)));
- force_assert(fe->hosts);
- } else if (fe->size == fe->used) {
- fe->size += 4;
- fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts)));
- force_assert(fe->hosts);
- }
-
- fe->hosts[fe->used++] = fh;
-
- return 0;
-
-}
-
-static void fcgi_proc_set_state(fcgi_extension_host *host, fcgi_proc *proc, int state) {
- if ((int)proc->state == state) return;
- if (proc->state == PROC_STATE_RUNNING) {
- --host->active_procs;
- } else if (state == PROC_STATE_RUNNING) {
- ++host->active_procs;
- }
- proc->state = state;
-}
-
-static void fcgi_proc_connect_error(server *srv, fcgi_extension_host *host, fcgi_proc *proc, handler_ctx *hctx, int errnum) {
- log_error_write(srv, __FILE__, __LINE__, "sssb",
- "establishing connection failed:", strerror(errnum),
- "socket:", proc->connection_name);
-
- if (!proc->is_local) {
- proc->disabled_until = srv->cur_ts + host->disable_time;
- fcgi_proc_set_state(host, proc, PROC_STATE_OVERLOADED);
- }
- else if (proc->pid == hctx->pid && proc->state == PROC_STATE_RUNNING) {
- /*
- * several hctx might reference the same proc
- *
- * Only one of them should mark the proc
- * and all other ones should just take a new one.
- *
- * If a new proc was started with the old struct this might lead
- * the mark a perfect proc as dead otherwise
- *
- */
- log_error_write(srv, __FILE__, __LINE__, "sdssdsd",
- "backend error; we'll disable it for", host->disable_time,
- "seconds and send the request to another backend instead:",
- "reconnects:", hctx->reconnects,
- "load:", host->load);
- if (EAGAIN == errnum) {
- /* - EAGAIN: cool down the backend; it is overloaded */
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sbsd",
- "This means that you have more incoming requests than your FastCGI backend can handle in parallel."
- "It might help to spawn more FastCGI backends or PHP children; if not, decrease server.max-connections."
- "The load for this FastCGI backend", proc->connection_name, "is", proc->load);
- }
- proc->disabled_until = srv->cur_ts + host->disable_time;
- fcgi_proc_set_state(host, proc, PROC_STATE_OVERLOADED);
- }
- else {
- /* we got a hard error from the backend like
- * - ECONNREFUSED for tcp-ip sockets
- * - ENOENT for unix-domain-sockets
- */
- fcgi_proc_set_state(host, proc, PROC_STATE_DIED_WAIT_FOR_PID);
- }
- }
-
- if (EAGAIN == errnum) {
- fcgi_proc_tag_inc(srv, hctx, CONST_STR_LEN(".overloaded"));
- }
- else {
- fcgi_proc_tag_inc(srv, hctx, CONST_STR_LEN(".died"));
- }
-}
-
-static void fcgi_proc_check_enable(server *srv, fcgi_extension_host *host, fcgi_proc *proc) {
- if (srv->cur_ts <= proc->disabled_until) return;
- if (proc->state != PROC_STATE_OVERLOADED) return;
-
- fcgi_proc_set_state(host, proc, PROC_STATE_RUNNING);
-
- log_error_write(srv, __FILE__, __LINE__, "sbbdb",
- "fcgi-server re-enabled:", proc->connection_name,
- host->host, host->port, host->unixsocket);
-}
-
-static int fcgi_proc_waitpid(server *srv, fcgi_extension_host *host, fcgi_proc *proc) {
- int rc, status;
-
- if (!proc->is_local) return 0;
- if (proc->pid <= 0) return 0;
-
- do {
- rc = waitpid(proc->pid, &status, WNOHANG);
- } while (-1 == rc && errno == EINTR);
- if (0 == rc) return 0; /* child still running */
-
- /* child terminated */
- if (-1 == rc) {
- /* EINVAL or ECHILD no child processes */
- /* should not happen; someone else has cleaned up for us */
- log_error_write(srv, __FILE__, __LINE__, "sddss",
- "pid ", proc->pid, proc->state,
- "not found:", strerror(errno));
- } else if (WIFEXITED(status)) {
- if (proc->state != PROC_STATE_KILLED) {
- log_error_write(srv, __FILE__, __LINE__, "sdb",
- "child exited:",
- WEXITSTATUS(status), proc->connection_name);
- }
- } else if (WIFSIGNALED(status)) {
- if (WTERMSIG(status) != SIGTERM && WTERMSIG(status) != SIGINT) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "child signalled:", WTERMSIG(status));
- }
- } else {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "child died somehow:", status);
- }
-
- proc->pid = 0;
- fcgi_proc_set_state(host, proc, PROC_STATE_DIED);
- return 1;
-}
-
-INIT_FUNC(mod_fastcgi_init) {
- plugin_data *p;
-
- p = calloc(1, sizeof(*p));
-
- return p;
-}
-
-
-FREE_FUNC(mod_fastcgi_free) {
- plugin_data *p = p_d;
-
- UNUSED(srv);
-
- if (p->config_storage) {
- size_t i, j, n;
- for (i = 0; i < srv->config_context->used; i++) {
- plugin_config *s = p->config_storage[i];
- fcgi_exts *exts;
-
- if (NULL == s) continue;
-
- exts = s->exts;
-
- if (exts) {
- for (j = 0; j < exts->used; j++) {
- fcgi_extension *ex;
-
- ex = exts->exts[j];
-
- for (n = 0; n < ex->used; n++) {
- fcgi_proc *proc;
- fcgi_extension_host *host;
-
- host = ex->hosts[n];
-
- for (proc = host->first; proc; proc = proc->next) {
- if (proc->pid > 0) {
- kill(proc->pid, host->kill_signal);
- }
-
- if (proc->is_local &&
- !buffer_string_is_empty(proc->unixsocket)) {
- unlink(proc->unixsocket->ptr);
- }
- }
-
- for (proc = host->unused_procs; proc; proc = proc->next) {
- if (proc->pid > 0) {
- kill(proc->pid, host->kill_signal);
- }
- if (proc->is_local &&
- !buffer_string_is_empty(proc->unixsocket)) {
- unlink(proc->unixsocket->ptr);
- }
- }
- }
- }
-
- fastcgi_extensions_free(s->exts);
- fastcgi_extensions_free(s->exts_auth);
- fastcgi_extensions_free(s->exts_resp);
- }
- array_free(s->ext_mapping);
-
- free(s);
- }
- free(p->config_storage);
- }
-
- free(p);
-
- return HANDLER_GO_ON;
-}
-
-static int env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
- char *dst;
- size_t i;
-
- if (!key || !val) return -1;
-
- dst = malloc(key_len + val_len + 3);
- memcpy(dst, key, key_len);
- dst[key_len] = '=';
- memcpy(dst + key_len + 1, val, val_len);
- dst[key_len + 1 + val_len] = '\0';
-
- for (i = 0; i < env->used; i++) {
- if (0 == strncmp(dst, env->ptr[i], key_len + 1)) {
- free(env->ptr[i]);
- env->ptr[i] = dst;
- return 0;
- }
- }
-
- if (env->size == 0) {
- env->size = 16;
- env->ptr = malloc(env->size * sizeof(*env->ptr));
- } else if (env->size == env->used + 1) {
- env->size += 16;
- env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
- }
-
- env->ptr[env->used++] = dst;
-
- return 0;
-}
-
-static int parse_binpath(char_array *env, buffer *b) {
- char *start;
- size_t i;
- char c;
- /* search for spaces */
-
- start = b->ptr;
- for (i = 0; i < buffer_string_length(b); i++) {
- switch(b->ptr[i]) {
- case ' ':
- case '\t':
- /* a WS, stop here and copy the argument */
-
- if (env->size == 0) {
- env->size = 16;
- env->ptr = malloc(env->size * sizeof(*env->ptr));
- } else if (env->size == env->used) {
- env->size += 16;
- env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
- }
-
- c = b->ptr[i];
- b->ptr[i] = '\0';
- env->ptr[env->used++] = strdup(start);
- b->ptr[i] = c;
-
- start = b->ptr + i + 1;
- break;
- default:
- break;
- }
- }
-
- if (env->size == 0) {
- env->size = 16;
- env->ptr = malloc(env->size * sizeof(*env->ptr));
- } else if (env->size == env->used) { /* we need one extra for the terminating NULL */
- env->size += 16;
- env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
- }
-
- /* the rest */
- env->ptr[env->used++] = strdup(start);
-
- if (env->size == 0) {
- env->size = 16;
- env->ptr = malloc(env->size * sizeof(*env->ptr));
- } else if (env->size == env->used) { /* we need one extra for the terminating NULL */
- env->size += 16;
- env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
- }
-
- /* terminate */
- env->ptr[env->used++] = NULL;
-
- return 0;
-}
-
-static int fcgi_spawn_connection(server *srv,
- plugin_data *p,
- fcgi_extension_host *host,
- fcgi_proc *proc) {
- int fcgi_fd;
- int status;
- struct timeval tv = { 0, 10 * 1000 };
- sock_addr addr;
- struct sockaddr *fcgi_addr = (struct sockaddr *)&addr;
- socklen_t servlen;
-
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sdb",
- "new proc, socket:", proc->port, proc->unixsocket);
- }
-
- if (!buffer_string_is_empty(proc->unixsocket)) {
- if (1 != sock_addr_from_str_hints(srv, &addr, &servlen, proc->unixsocket->ptr, AF_UNIX, 0)) {
- return -1;
- }
- } else {
- if (1 != sock_addr_from_buffer_hints_numeric(srv, &addr, &servlen, host->host, host->family, proc->port)) {
- return -1;
- }
- }
-
- if (!buffer_string_is_empty(proc->unixsocket)) {
- buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("unix:"));
- buffer_append_string_buffer(proc->connection_name, proc->unixsocket);
- } else {
- buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("tcp:"));
- if (!buffer_string_is_empty(host->host)) {
- buffer_append_string_buffer(proc->connection_name, host->host);
- } else {
- buffer_append_string_len(proc->connection_name, CONST_STR_LEN("localhost"));
- }
- buffer_append_string_len(proc->connection_name, CONST_STR_LEN(":"));
- buffer_append_int(proc->connection_name, proc->port);
- }
-
- if (-1 == (fcgi_fd = fdevent_socket_cloexec(fcgi_addr->sa_family, SOCK_STREAM, 0))) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "failed:", strerror(errno));
- return -1;
- }
-
- do {
- status = connect(fcgi_fd, fcgi_addr, servlen);
- } while (-1 == status && errno == EINTR);
-
- if (-1 == status && errno != ENOENT
- && !buffer_string_is_empty(proc->unixsocket)) {
- log_error_write(srv, __FILE__, __LINE__, "sbss",
- "unlink", proc->unixsocket,
- "after connect failed:", strerror(errno));
- unlink(proc->unixsocket->ptr);
- }
-
- close(fcgi_fd);
-
- if (-1 == status) {
- /* server is not up, spawn it */
- char_array env;
- size_t i;
- int val;
- int dfd = -1;
-
- /* reopen socket */
- if (-1 == (fcgi_fd = fdevent_socket_cloexec(fcgi_addr->sa_family, SOCK_STREAM, 0))) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "socket failed:", strerror(errno));
- return -1;
- }
-
- val = 1;
- if (setsockopt(fcgi_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "socketsockopt failed:", strerror(errno));
- close(fcgi_fd);
- return -1;
- }
-
- /* create socket */
- if (-1 == bind(fcgi_fd, fcgi_addr, servlen)) {
- log_error_write(srv, __FILE__, __LINE__, "sbs",
- "bind failed for:",
- proc->connection_name,
- strerror(errno));
- close(fcgi_fd);
- return -1;
- }
-
- if (-1 == listen(fcgi_fd, host->listen_backlog)) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "listen failed:", strerror(errno));
- close(fcgi_fd);
- return -1;
- }
-
- {
- /* create environment */
- env.ptr = NULL;
- env.size = 0;
- env.used = 0;
-
- /* build clean environment */
- if (host->bin_env_copy->used) {
- for (i = 0; i < host->bin_env_copy->used; i++) {
- data_string *ds = (data_string *)host->bin_env_copy->data[i];
- char *ge;
-
- if (NULL != (ge = getenv(ds->value->ptr))) {
- env_add(&env, CONST_BUF_LEN(ds->value), ge, strlen(ge));
- }
- }
- } else {
- char ** const e = environ;
- for (i = 0; e[i]; ++i) {
- char *eq;
-
- if (NULL != (eq = strchr(e[i], '='))) {
- env_add(&env, e[i], eq - e[i], eq+1, strlen(eq+1));
- }
- }
- }
-
- /* create environment */
- for (i = 0; i < host->bin_env->used; i++) {
- data_string *ds = (data_string *)host->bin_env->data[i];
-
- env_add(&env, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
- }
-
- for (i = 0; i < env.used; i++) {
- /* search for PHP_FCGI_CHILDREN */
- if (0 == strncmp(env.ptr[i], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
- }
-
- /* not found, add a default */
- if (i == env.used) {
- env_add(&env, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
- }
-
- env.ptr[env.used] = NULL;
- }
-
- dfd = fdevent_open_dirname(host->args.ptr[0]);
- if (-1 == dfd) {
- log_error_write(srv, __FILE__, __LINE__, "sss", "open dirname failed:", strerror(errno), host->args.ptr[0]);
- }
-
- /*(FCGI_LISTENSOCK_FILENO == STDIN_FILENO == 0)*/
- proc->pid = (dfd >= 0) ? fdevent_fork_execve(host->args.ptr[0], host->args.ptr, env.ptr, fcgi_fd, -1, -1, dfd) : -1;
-
- for (i = 0; i < env.used; ++i) free(env.ptr[i]);
- free(env.ptr);
- if (-1 != dfd) close(dfd);
- close(fcgi_fd);
-
- if (-1 == proc->pid) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "fastcgi-backend failed to start:", host->bin_path);
- return -1;
- }
-
- /* register process */
- proc->is_local = 1;
-
- /* wait */
- select(0, NULL, NULL, NULL, &tv);
-
- if (0 != fcgi_proc_waitpid(srv, host, proc)) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "fastcgi-backend failed to start:", host->bin_path);
- log_error_write(srv, __FILE__, __LINE__, "s",
- "If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version. "
- "If this is PHP on Gentoo, add 'fastcgi' to the USE flags. "
- "If this is PHP, try removing the bytecode caches for now and try again.");
- return -1;
- }
- } else {
- proc->is_local = 0;
- proc->pid = 0;
-
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "(debug) socket is already used; won't spawn:",
- proc->connection_name);
- }
- }
-
- fcgi_proc_set_state(host, proc, PROC_STATE_RUNNING);
- return 0;
-}
-
-static fcgi_extension_host * unixsocket_is_dup(plugin_data *p, size_t used, buffer *unixsocket) {
- size_t i, j, n;
- for (i = 0; i < used; ++i) {
- fcgi_exts *exts = p->config_storage[i]->exts;
- if (NULL == exts) continue;
- for (j = 0; j < exts->used; ++j) {
- fcgi_extension *ex = exts->exts[j];
- for (n = 0; n < ex->used; ++n) {
- fcgi_extension_host *host = ex->hosts[n];
- if (!buffer_string_is_empty(host->unixsocket)
- && buffer_is_equal(host->unixsocket, unixsocket)
- && !buffer_string_is_empty(host->bin_path))
- return host;
- }
- }
- }
-
- return NULL;
-}
SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
plugin_data *p = p_d;
data_unset *du;
size_t i = 0;
- buffer *fcgi_mode = buffer_init();
- fcgi_extension_host *host = NULL;
config_values_t cv[] = {
{ "fastcgi.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
{ "fastcgi.debug", NULL, T_CONFIG_INT , T_CONFIG_SCOPE_CONNECTION }, /* 1 */
{ "fastcgi.map-extensions", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
+ { "fastcgi.balance", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
};
p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
+ force_assert(p->config_storage);
for (i = 0; i < srv->config_context->used; i++) {
data_config const* config = (data_config const*)srv->config_context->data[i];
plugin_config *s;
s = calloc(1, sizeof(plugin_config));
+ force_assert(s);
s->exts = NULL;
s->exts_auth = NULL;
s->exts_resp = NULL;
@@ -1165,477 +68,28 @@ SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
cv[0].destination = s->exts; /* not used; T_CONFIG_LOCAL */
cv[1].destination = &(s->debug);
cv[2].destination = s->ext_mapping;
+ cv[3].destination = NULL; /* not used; T_CONFIG_LOCAL */
p->config_storage[i] = s;
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
- goto error;
- }
-
- /*
- * <key> = ( ... )
- */
-
- if (NULL != (du = array_get_element(config->value, "fastcgi.server"))) {
- size_t j;
- data_array *da = (data_array *)du;
-
- if (du->type != TYPE_ARRAY || !array_is_kvarray(da->value)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "unexpected value for fastcgi.server; expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
-
- goto error;
- }
-
- s->exts = fastcgi_extensions_init();
- s->exts_auth = fastcgi_extensions_init();
- s->exts_resp = fastcgi_extensions_init();
-
- /*
- * fastcgi.server = ( "<ext>" => ( ... ),
- * "<ext>" => ( ... ) )
- */
-
- for (j = 0; j < da->value->used; j++) {
- size_t n;
- data_array *da_ext = (data_array *)da->value->data[j];
-
- /*
- * da_ext->key == name of the extension
- */
-
- /*
- * fastcgi.server = ( "<ext>" =>
- * ( "<host>" => ( ... ),
- * "<host>" => ( ... )
- * ),
- * "<ext>" => ... )
- */
-
- for (n = 0; n < da_ext->value->used; n++) {
- data_array *da_host = (data_array *)da_ext->value->data[n];
-
- config_values_t fcv[] = {
- { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
- { "docroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
- { "mode", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
- { "socket", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
- { "bin-path", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
-
- { "check-local", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
- { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
- { "max-procs", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
- { "disable-time", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
-
- { "bin-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
- { "bin-copy-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
-
- { "broken-scriptfilename", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
- { "allow-x-send-file", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
- { "strip-request-uri", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
- { "kill-signal", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
- { "fix-root-scriptname", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
- { "listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
- { "x-sendfile", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 17 */
- { "x-sendfile-docroot",NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 18 */
-
- { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
- };
- unsigned short host_mode = FCGI_RESPONDER;
-
- if (da_host->type != TYPE_ARRAY || !array_is_kvany(da_host->value)) {
- log_error_write(srv, __FILE__, __LINE__, "SBS",
- "unexpected value for fastcgi.server near [",
- da_host->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
-
- goto error;
- }
-
- host = fastcgi_host_init();
- buffer_reset(fcgi_mode);
-
- buffer_copy_buffer(host->id, da_host->key);
-
- host->check_local = 1;
- host->max_procs = 4;
- host->disable_time = 1;
- host->break_scriptfilename_for_php = 0;
- host->xsendfile_allow = 0;
- host->kill_signal = SIGTERM;
- host->fix_root_path_name = 0;
- host->listen_backlog = 1024;
- host->refcount = 0;
-
- fcv[0].destination = host->host;
- fcv[1].destination = host->docroot;
- fcv[2].destination = fcgi_mode;
- fcv[3].destination = host->unixsocket;
- fcv[4].destination = host->bin_path;
-
- fcv[5].destination = &(host->check_local);
- fcv[6].destination = &(host->port);
- fcv[7].destination = &(host->max_procs);
- fcv[8].destination = &(host->disable_time);
-
- fcv[9].destination = host->bin_env;
- fcv[10].destination = host->bin_env_copy;
- fcv[11].destination = &(host->break_scriptfilename_for_php);
- fcv[12].destination = &(host->xsendfile_allow);
- fcv[13].destination = host->strip_request_uri;
- fcv[14].destination = &(host->kill_signal);
- fcv[15].destination = &(host->fix_root_path_name);
- fcv[16].destination = &(host->listen_backlog);
- fcv[17].destination = &(host->xsendfile_allow);
- fcv[18].destination = host->xsendfile_docroot;
-
- if (0 != config_insert_values_internal(srv, da_host->value, fcv, T_CONFIG_SCOPE_CONNECTION)) {
- goto error;
- }
-
- if ((!buffer_string_is_empty(host->host) || host->port) &&
- !buffer_string_is_empty(host->unixsocket)) {
- log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
- "either host/port or socket have to be set in:",
- da->key, "= (",
- da_ext->key, " => (",
- da_host->key, " ( ...");
-
- goto error;
- }
-
- if (!buffer_string_is_empty(host->unixsocket)) {
- /* unix domain socket */
- struct sockaddr_un un;
-
- if (buffer_string_length(host->unixsocket) + 1 > sizeof(un.sun_path) - 2) {
- log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
- "unixsocket is too long in:",
- da->key, "= (",
- da_ext->key, " => (",
- da_host->key, " ( ...");
-
- goto error;
- }
-
- if (!buffer_string_is_empty(host->bin_path)) {
- fcgi_extension_host *duplicate = unixsocket_is_dup(p, i+1, host->unixsocket);
- if (NULL != duplicate) {
- if (!buffer_is_equal(host->bin_path, duplicate->bin_path)) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "duplicate unixsocket path:",
- host->unixsocket);
- goto error;
- }
- fastcgi_host_free(host);
- host = duplicate;
- ++host->refcount;
- }
- }
-
- host->family = AF_UNIX;
- } else {
- /* tcp/ip */
-
- if (buffer_string_is_empty(host->host) &&
- buffer_string_is_empty(host->bin_path)) {
- log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
- "host or binpath have to be set in:",
- da->key, "= (",
- da_ext->key, " => (",
- da_host->key, " ( ...");
-
- goto error;
- } else if (host->port == 0) {
- log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
- "port has to be set in:",
- da->key, "= (",
- da_ext->key, " => (",
- da_host->key, " ( ...");
-
- goto error;
- }
-
- host->family = (!buffer_string_is_empty(host->host) && NULL != strchr(host->host->ptr, ':')) ? AF_INET6 : AF_INET;
- }
-
- if (host->refcount) {
- /* already init'd; skip spawning */
- } else if (!buffer_string_is_empty(host->bin_path)) {
- /* a local socket + self spawning */
- size_t pno;
-
- struct stat st;
-
- parse_binpath(&host->args, host->bin_path);
- if (0 != stat(host->args.ptr[0], &st) || !S_ISREG(st.st_mode) || !(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
- log_error_write(srv, __FILE__, __LINE__, "SSs",
- "invalid \"bin-path\" => \"", host->bin_path->ptr,
- "\" (check that file exists, is regular file, and is executable by lighttpd)");
- }
-
- if (s->debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssbsdsbsd",
- "--- fastcgi spawning local",
- "\n\tproc:", host->bin_path,
- "\n\tport:", host->port,
- "\n\tsocket", host->unixsocket,
- "\n\tmax-procs:", host->max_procs);
- }
-
- for (pno = 0; pno < host->max_procs; pno++) {
- fcgi_proc *proc;
-
- proc = fastcgi_process_init();
- proc->id = host->num_procs++;
- host->max_id++;
-
- if (buffer_string_is_empty(host->unixsocket)) {
- proc->port = host->port + pno;
- } else {
- buffer_copy_buffer(proc->unixsocket, host->unixsocket);
- buffer_append_string_len(proc->unixsocket, CONST_STR_LEN("-"));
- buffer_append_int(proc->unixsocket, pno);
- }
-
- if (s->debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
- "--- fastcgi spawning",
- "\n\tport:", host->port,
- "\n\tsocket", host->unixsocket,
- "\n\tcurrent:", pno, "/", host->max_procs);
- }
-
- if (!srv->srvconf.preflight_check
- && fcgi_spawn_connection(srv, p, host, proc)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "[ERROR]: spawning fcgi failed.");
- fastcgi_process_free(proc);
- goto error;
- }
-
- fastcgi_status_init(srv, host, proc);
-
- proc->next = host->first;
- if (host->first) host->first->prev = proc;
-
- host->first = proc;
- }
- } else {
- fcgi_proc *proc;
-
- proc = fastcgi_process_init();
- proc->id = host->num_procs++;
- host->max_id++;
- fcgi_proc_set_state(host, proc, PROC_STATE_RUNNING);
-
- if (buffer_string_is_empty(host->unixsocket)) {
- proc->port = host->port;
- } else {
- buffer_copy_buffer(proc->unixsocket, host->unixsocket);
- }
-
- fastcgi_status_init(srv, host, proc);
-
- host->first = proc;
-
- host->max_procs = 1;
- }
-
- if (!buffer_string_is_empty(fcgi_mode)) {
- if (strcmp(fcgi_mode->ptr, "responder") == 0) {
- host_mode = FCGI_RESPONDER;
- } else if (strcmp(fcgi_mode->ptr, "authorizer") == 0) {
- host_mode = FCGI_AUTHORIZER;
- } else {
- log_error_write(srv, __FILE__, __LINE__, "sbs",
- "WARNING: unknown fastcgi mode:",
- fcgi_mode, "(ignored, mode set to responder)");
- }
- }
-
- if (host->xsendfile_docroot->used) {
- size_t k;
- for (k = 0; k < host->xsendfile_docroot->used; ++k) {
- data_string *ds = (data_string *)host->xsendfile_docroot->data[k];
- if (ds->type != TYPE_STRING) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
- goto error;
- }
- if (ds->value->ptr[0] != '/') {
- log_error_write(srv, __FILE__, __LINE__, "SBs",
- "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds->value, "\"");
- goto error;
- }
- buffer_path_simplify(ds->value, ds->value);
- buffer_append_slash(ds->value);
- }
- }
-
- /* s->exts is list of exts -> hosts
- * s->exts now used as combined list of authorizer and responder hosts (for backend maintenance)
- * s->exts_auth is list of exts -> authorizer hosts
- * s->exts_resp is list of exts -> responder hosts
- * For each path/extension, there may be an independent FCGI_AUTHORIZER and FCGI_RESPONDER
- * (The FCGI_AUTHORIZER and FCGI_RESPONDER could be handled by the same host,
- * and an admin might want to do that for large uploads, since FCGI_AUTHORIZER
- * runs prior to receiving (potentially large) request body from client and can
- * authorizer or deny request prior to receiving the full upload)
- */
- fastcgi_extension_insert(s->exts, da_ext->key, host);
-
- if (host_mode == FCGI_AUTHORIZER) {
- ++host->refcount;
- fastcgi_extension_insert(s->exts_auth, da_ext->key, host);
- } else if (host_mode == FCGI_RESPONDER) {
- ++host->refcount;
- fastcgi_extension_insert(s->exts_resp, da_ext->key, host);
- } /*(else should have been rejected above)*/
-
- host = NULL;
- }
- }
- }
- }
-
- buffer_free(fcgi_mode);
- return HANDLER_GO_ON;
-
-error:
- if (NULL != host) fastcgi_host_free(host);
- buffer_free(fcgi_mode);
- return HANDLER_ERROR;
-}
-
-static int fcgi_set_state(server *srv, handler_ctx *hctx, fcgi_connection_state_t state) {
- hctx->state = state;
- hctx->state_timestamp = srv->cur_ts;
-
- return 0;
-}
-
-
-static void fcgi_backend_close(server *srv, handler_ctx *hctx) {
- if (hctx->fd != -1) {
- fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
- fdevent_unregister(srv->ev, hctx->fd);
- fdevent_sched_close(srv->ev, hctx->fd, 1);
- hctx->fd = -1;
- hctx->fde_ndx = -1;
- }
-
- if (hctx->host) {
- if (hctx->proc && hctx->got_proc) {
- /* after the connect the process gets a load */
- fcgi_proc_load_dec(srv, hctx);
-
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssdsbsd",
- "released proc:",
- "pid:", hctx->proc->pid,
- "socket:", hctx->proc->connection_name,
- "load:", hctx->proc->load);
- }
-
- hctx->proc = NULL;
- hctx->got_proc = 0;
+ return HANDLER_ERROR;
}
- fcgi_host_reset(srv, hctx);
- hctx->host = NULL;
- }
-}
-
-static fcgi_extension_host * fcgi_extension_host_get(server *srv, connection *con, plugin_data *p, fcgi_extension *extension) {
- fcgi_extension_host *host;
- int ndx = extension->last_used_ndx + 1;
- if (ndx >= (int) extension->used || ndx < 0) ndx = 0;
- UNUSED(p);
-
- /* check if the next server has no load */
- host = extension->hosts[ndx];
- if (host->load > 0 || host->active_procs == 0) {
- /* get backend with the least load */
- size_t k;
- int used = -1;
- for (k = 0, ndx = -1; k < extension->used; k++) {
- host = extension->hosts[k];
-
- /* we should have at least one proc that can do something */
- if (host->active_procs == 0) continue;
-
- if (used == -1 || host->load < used) {
- used = host->load;
- ndx = k;
- }
+ du = array_get_element(config->value, "fastcgi.server");
+ if (!gw_set_defaults_backend(srv, p, du, i, 0)) {
+ return HANDLER_ERROR;
}
- }
-
- if (ndx == -1) {
- /* all hosts are down */
- /* sorry, we don't have a server alive for this ext */
- con->http_status = 503; /* Service Unavailable */
- con->mode = DIRECT;
-
- /* only send the 'no handler' once */
- if (!extension->note_is_sent) {
- extension->note_is_sent = 1;
- log_error_write(srv, __FILE__, __LINE__, "sBSbsbs",
- "all handlers for", con->uri.path, "?", con->uri.query,
- "on", extension->key,
- "are down.");
+ du = array_get_element(config->value, "fastcgi.balance");
+ if (!gw_set_defaults_balance(srv, s, du)) {
+ return HANDLER_ERROR;
}
-
- return NULL;
- }
-
- /* found a server */
- extension->last_used_ndx = ndx;
- return extension->hosts[ndx];
-}
-
-static void fcgi_connection_close(server *srv, handler_ctx *hctx) {
- plugin_data *p;
- connection *con;
-
- p = hctx->plugin_data;
- con = hctx->remote_conn;
-
- fcgi_backend_close(srv, hctx);
- handler_ctx_free(hctx);
- con->plugin_ctx[p->id] = NULL;
-
- /* finish response (if not already con->file_started, con->file_finished) */
- if (con->mode == p->id) {
- http_response_backend_done(srv, con);
}
-}
-
-static handler_t fcgi_reconnect(server *srv, handler_ctx *hctx) {
- fcgi_backend_close(srv, hctx);
-
- hctx->host = fcgi_extension_host_get(srv, hctx->remote_conn, hctx->plugin_data, hctx->ext);
- if (NULL == hctx->host) return HANDLER_FINISHED;
-
- fcgi_host_assign(srv, hctx);
- hctx->request_id = 0;
- hctx->opts.xsendfile_allow = hctx->host->xsendfile_allow;
- hctx->opts.xsendfile_docroot = hctx->host->xsendfile_docroot;
- fcgi_set_state(srv, hctx, FCGI_STATE_INIT);
- return HANDLER_COMEBACK;
-}
-
-
-static handler_t fcgi_connection_reset(server *srv, connection *con, void *p_d) {
- plugin_data *p = p_d;
- handler_ctx *hctx = con->plugin_ctx[p->id];
- if (hctx) fcgi_connection_close(srv, hctx);
return HANDLER_GO_ON;
}
-
static int fcgi_env_add(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
buffer *env = venv;
size_t len;
@@ -1691,7 +145,7 @@ static int fcgi_env_add(void *venv, const char *key, size_t key_len, const char
return 0;
}
-static int fcgi_header(FCGI_Header * header, unsigned char type, int request_id, int contentLength, unsigned char paddingLength) {
+static void fcgi_header(FCGI_Header * header, unsigned char type, int request_id, int contentLength, unsigned char paddingLength) {
force_assert(contentLength <= FCGI_MAX_LENGTH);
header->version = FCGI_VERSION_1;
@@ -1702,81 +156,15 @@ static int fcgi_header(FCGI_Header * header, unsigned char type, int request_id,
header->contentLengthB1 = (contentLength >> 8) & 0xff;
header->paddingLength = paddingLength;
header->reserved = 0;
-
- return 0;
-}
-
-static int fcgi_establish_connection(server *srv, handler_ctx *hctx) {
- sock_addr addr;
- struct sockaddr *fcgi_addr = (struct sockaddr *)&addr;
- socklen_t servlen;
-
- fcgi_extension_host *host = hctx->host;
- fcgi_proc *proc = hctx->proc;
- int fcgi_fd = hctx->fd;
-
- if (!buffer_string_is_empty(proc->unixsocket)) {
- if (1 != sock_addr_from_str_hints(srv, &addr, &servlen, proc->unixsocket->ptr, AF_UNIX, 0)) {
- errno = EINVAL;
- return -1;
- }
- } else {
- if (1 != sock_addr_from_buffer_hints_numeric(srv, &addr, &servlen, host->host, host->family, proc->port)) {
- errno = EINVAL;
- return -1;
- }
- }
-
- if (!buffer_string_is_empty(proc->unixsocket)) {
- if (buffer_string_is_empty(proc->connection_name)) {
- /* on remote spawing we have to set the connection-name now */
- buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("unix:"));
- buffer_append_string_buffer(proc->connection_name, proc->unixsocket);
- }
- } else {
- if (buffer_string_is_empty(proc->connection_name)) {
- /* on remote spawing we have to set the connection-name now */
- buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("tcp:"));
- if (!buffer_string_is_empty(host->host)) {
- buffer_append_string_buffer(proc->connection_name, host->host);
- } else {
- buffer_append_string_len(proc->connection_name, CONST_STR_LEN("localhost"));
- }
- buffer_append_string_len(proc->connection_name, CONST_STR_LEN(":"));
- buffer_append_int(proc->connection_name, proc->port);
- }
- }
-
- if (-1 == connect(fcgi_fd, fcgi_addr, servlen)) {
- if (errno == EINPROGRESS ||
- errno == EALREADY ||
- errno == EINTR) {
- if (hctx->conf.debug > 2) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "connect delayed; will continue later:", proc->connection_name);
- }
-
- return 1;
- } else {
- fcgi_proc_connect_error(srv, host, proc, hctx, errno);
- return -1;
- }
- }
-
- hctx->reconnects = 0;
- if (hctx->conf.debug > 1) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "connect succeeded: ", fcgi_fd);
- }
-
- return 0;
}
-static void fcgi_stdin_append(server *srv, connection *con, handler_ctx *hctx, int request_id) {
+static handler_t fcgi_stdin_append(server *srv, handler_ctx *hctx) {
FCGI_Header header;
+ connection *con = hctx->remote_conn;
chunkqueue *req_cq = con->request_content_queue;
off_t offset, weWant;
const off_t req_cqlen = req_cq->bytes_in - req_cq->bytes_out;
+ int request_id = hctx->request_id;
/* something to send ? */
for (offset = 0; offset != req_cqlen; offset += weWant) {
@@ -1813,20 +201,22 @@ static void fcgi_stdin_append(server *srv, connection *con, handler_ctx *hctx, i
chunkqueue_append_mem(hctx->wb, (const char *)&header, sizeof(header));
hctx->wb_reqlen += (int)sizeof(header);
}
+
+ return HANDLER_GO_ON;
}
-static int fcgi_create_env(server *srv, handler_ctx *hctx) {
+static handler_t fcgi_create_env(server *srv, handler_ctx *hctx) {
FCGI_BeginRequestRecord beginRecord;
FCGI_Header header;
int request_id;
buffer *fcgi_env = buffer_init();
- fcgi_extension_host *host= hctx->host;
+ gw_host *host = hctx->host;
connection *con = hctx->remote_conn;
http_cgi_opts opts = {
- (hctx->fcgi_mode == FCGI_AUTHORIZER),
+ (hctx->gw_mode == FCGI_AUTHORIZER),
host->break_scriptfilename_for_php,
host->docroot,
host->strip_request_uri
@@ -1843,7 +233,7 @@ static int fcgi_create_env(server *srv, handler_ctx *hctx) {
request_id = hctx->request_id;
fcgi_header(&(beginRecord.header), FCGI_BEGIN_REQUEST, request_id, sizeof(beginRecord.body), 0);
- beginRecord.body.roleB0 = hctx->fcgi_mode;
+ beginRecord.body.roleB0 = hctx->gw_mode;
beginRecord.body.roleB1 = 0;
beginRecord.body.flags = 0;
memset(beginRecord.body.reserved, 0, sizeof(beginRecord.body.reserved));
@@ -1854,7 +244,7 @@ static int fcgi_create_env(server *srv, handler_ctx *hctx) {
if (0 != http_cgi_headers(srv, con, &opts, fcgi_env_add, fcgi_env)) {
con->http_status = 400;
buffer_free(fcgi_env);
- return -1;
+ return HANDLER_FINISHED;
} else {
buffer *b = buffer_init();
@@ -1880,9 +270,10 @@ static int fcgi_create_env(server *srv, handler_ctx *hctx) {
else /* as-yet-unknown total request size (Transfer-Encoding: chunked)*/
hctx->wb_reqlen = -hctx->wb_reqlen;
}
- fcgi_stdin_append(srv, con, hctx, request_id);
+ fcgi_stdin_append(srv, hctx);
- return 0;
+ status_counter_inc(srv, CONST_STR_LEN("fastcgi.requests"));
+ return HANDLER_GO_ON;
}
typedef struct {
@@ -2008,9 +399,9 @@ static handler_t fcgi_recv_parse(server *srv, connection *con, struct http_respo
/* is the header already finished */
if (0 == con->file_started) {
/* split header from body */
- buffer *hdrs = (!hctx->response_header)
+ buffer *hdrs = (!hctx->response)
? packet.b
- : (buffer_append_string_buffer(hctx->response_header, packet.b), hctx->response_header);
+ : (buffer_append_string_buffer(hctx->response, packet.b), hctx->response);
handler_t rc = http_response_parse_headers(srv, con, &hctx->opts, hdrs);
if (rc != HANDLER_GO_ON) {
hctx->send_content_body = 0;
@@ -2018,12 +409,12 @@ static handler_t fcgi_recv_parse(server *srv, connection *con, struct http_respo
break;
}
if (0 == con->file_started) {
- if (!hctx->response_header) {
- hctx->response_header = packet.b;
+ if (!hctx->response) {
+ hctx->response = packet.b;
packet.b = NULL;
}
}
- else if (hctx->fcgi_mode == FCGI_AUTHORIZER &&
+ else if (hctx->gw_mode == GW_AUTHORIZER &&
(con->http_status == 0 || con->http_status == 200)) {
/* authorizer approved request; ignore the content here */
hctx->send_content_body = 0;
@@ -2058,510 +449,6 @@ static handler_t fcgi_recv_parse(server *srv, connection *con, struct http_respo
return 0 == fin ? HANDLER_GO_ON : HANDLER_FINISHED;
}
-static int fcgi_restart_dead_procs(server *srv, plugin_data *p, fcgi_extension_host *host) {
- fcgi_proc *proc;
-
- for (proc = host->first; proc; proc = proc->next) {
- if (p->conf.debug > 2) {
- log_error_write(srv, __FILE__, __LINE__, "sbdddd",
- "proc:",
- proc->connection_name,
- proc->state,
- proc->is_local,
- proc->load,
- proc->pid);
- }
-
- /*
- * if the remote side is overloaded, we check back after <n> seconds
- *
- */
- switch (proc->state) {
- case PROC_STATE_KILLED:
- /* this should never happen as long as adaptive spawing is disabled */
- force_assert(0);
-
- break;
- case PROC_STATE_RUNNING:
- break;
- case PROC_STATE_OVERLOADED:
- case PROC_STATE_DIED_WAIT_FOR_PID:
- if (0 == fcgi_proc_waitpid(srv, host, proc)) {
- fcgi_proc_check_enable(srv, host, proc);
- }
-
- /* fall through if we have a dead proc now */
- if (proc->state != PROC_STATE_DIED) break;
-
- case PROC_STATE_DIED:
- /* local procs get restarted by us,
- * remote ones hopefully by the admin */
-
- if (!buffer_string_is_empty(host->bin_path)) {
- /* we still have connections bound to this proc,
- * let them terminate first */
- if (proc->load != 0) break;
-
- /* restart the child */
-
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssbsdsd",
- "--- fastcgi spawning",
- "\n\tsocket", proc->connection_name,
- "\n\tcurrent:", 1, "/", host->max_procs);
- }
-
- if (fcgi_spawn_connection(srv, p, host, proc)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "ERROR: spawning fcgi failed.");
- return HANDLER_ERROR;
- }
- } else {
- fcgi_proc_check_enable(srv, host, proc);
- }
- break;
- }
- }
-
- return 0;
-}
-
-static handler_t fcgi_write_request(server *srv, handler_ctx *hctx) {
- fcgi_extension_host *host= hctx->host;
- connection *con = hctx->remote_conn;
- fcgi_proc *proc;
-
- int ret;
-
- switch(hctx->state) {
- case FCGI_STATE_INIT:
- /* do we have a running process for this host (max-procs) ? */
- hctx->proc = NULL;
-
- for (proc = hctx->host->first;
- proc && proc->state != PROC_STATE_RUNNING;
- proc = proc->next);
-
- /* all children are dead */
- if (proc == NULL) {
- return HANDLER_ERROR;
- }
-
- hctx->proc = proc;
-
- /* check the other procs if they have a lower load */
- for (proc = proc->next; proc; proc = proc->next) {
- if (proc->state != PROC_STATE_RUNNING) continue;
- if (proc->load < hctx->proc->load) hctx->proc = proc;
- }
-
- if (-1 == (hctx->fd = fdevent_socket_nb_cloexec(host->family, SOCK_STREAM, 0))) {
- if (errno == EMFILE ||
- errno == EINTR) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "wait for fd at connection:", con->fd);
-
- return HANDLER_WAIT_FOR_FD;
- }
-
- log_error_write(srv, __FILE__, __LINE__, "ssdd",
- "socket failed:", strerror(errno), srv->cur_fds, srv->max_fds);
- return HANDLER_ERROR;
- }
-
- srv->cur_fds++;
-
- fdevent_register(srv->ev, hctx->fd, fcgi_handle_fdevent, hctx);
-
- if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "fcntl failed:", strerror(errno));
-
- return HANDLER_ERROR;
- }
-
- if (hctx->proc->is_local) {
- hctx->pid = hctx->proc->pid;
- }
-
- switch (fcgi_establish_connection(srv, hctx)) {
- case 1: /* connection is in progress */
- fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- fcgi_set_state(srv, hctx, FCGI_STATE_CONNECT_DELAYED);
- return HANDLER_WAIT_FOR_EVENT;
- case -1:/* connection error */
- return HANDLER_ERROR;
- case 0: /* everything is ok, go on */
- break;
- }
- /* fallthrough */
- case FCGI_STATE_CONNECT_DELAYED:
- if (hctx->state == FCGI_STATE_CONNECT_DELAYED) { /*(not FCGI_STATE_INIT)*/
- int socket_error = fdevent_connect_status(hctx->fd);
- if (socket_error != 0) {
- fcgi_proc_connect_error(srv, hctx->host, hctx->proc, hctx, socket_error);
- return HANDLER_ERROR;
- }
- /* go on with preparing the request */
- }
-
- fcgi_set_state(srv, hctx, FCGI_STATE_PREPARE_WRITE);
- /* fallthrough */
- case FCGI_STATE_PREPARE_WRITE:
- /* ok, we have the connection */
-
- fcgi_proc_load_inc(srv, hctx);
- hctx->got_proc = 1;
-
- status_counter_inc(srv, CONST_STR_LEN("fastcgi.requests"));
- fcgi_proc_tag_inc(srv, hctx, CONST_STR_LEN(".connected"));
-
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssdsbsd",
- "got proc:",
- "pid:", hctx->proc->pid,
- "socket:", hctx->proc->connection_name,
- "load:", hctx->proc->load);
- }
-
- if (-1 == fcgi_create_env(srv, hctx)) return HANDLER_ERROR;
-
- fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
- fcgi_set_state(srv, hctx, FCGI_STATE_WRITE);
- /* fall through */
- case FCGI_STATE_WRITE:
- ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
-
- chunkqueue_remove_finished_chunks(hctx->wb);
-
- if (ret < 0) {
- switch(errno) {
- case EPIPE:
- case ENOTCONN:
- case ECONNRESET:
- /* the connection got dropped after accept()
- * we don't care about that - if you accept() it, you have to handle it.
- */
-
- log_error_write(srv, __FILE__, __LINE__, "ssosb",
- "connection was dropped after accept() (perhaps the fastcgi process died),",
- "write-offset:", hctx->wb->bytes_out,
- "socket:", hctx->proc->connection_name);
-
- return HANDLER_ERROR;
- default:
- log_error_write(srv, __FILE__, __LINE__, "ssd",
- "write failed:", strerror(errno), errno);
-
- return HANDLER_ERROR;
- }
- }
-
- if (hctx->wb->bytes_out == hctx->wb_reqlen) {
- fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- fcgi_set_state(srv, hctx, FCGI_STATE_READ);
- } else {
- off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
- if ((hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0) && wblen < 65536 - 16384) {
- /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
- if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
- con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
- con->is_readable = 1; /* trigger optimistic read from client */
- }
- }
- if (0 == wblen) {
- fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- } else {
- fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- }
- }
-
- return HANDLER_WAIT_FOR_EVENT;
- case FCGI_STATE_READ:
- /* waiting for a response */
- return HANDLER_WAIT_FOR_EVENT;
- default:
- log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
- return HANDLER_ERROR;
- }
-}
-
-static handler_t fcgi_write_error(server *srv, handler_ctx *hctx) {
- connection *con = hctx->remote_conn;
- int status = con->http_status;
-
- if (hctx->state == FCGI_STATE_INIT ||
- hctx->state == FCGI_STATE_CONNECT_DELAYED) {
-
- fcgi_restart_dead_procs(srv, hctx->plugin_data, hctx->host);
-
- /* cleanup this request and let request handler start request again */
- if (hctx->reconnects++ < 5) return fcgi_reconnect(srv, hctx);
- }
-
- fcgi_connection_close(srv, hctx);
- con->http_status = (status == 400) ? 400 : 503;
- return HANDLER_FINISHED;
-}
-
-static handler_t fcgi_send_request(server *srv, handler_ctx *hctx) {
- handler_t rc = fcgi_write_request(srv, hctx);
- return (HANDLER_ERROR != rc) ? rc : fcgi_write_error(srv, hctx);
-}
-
-
-static handler_t fcgi_recv_response(server *srv, handler_ctx *hctx);
-
-
-SUBREQUEST_FUNC(mod_fastcgi_handle_subrequest) {
- plugin_data *p = p_d;
-
- handler_ctx *hctx = con->plugin_ctx[p->id];
-
- if (NULL == hctx) return HANDLER_GO_ON;
-
- /* not my job */
- if (con->mode != p->id) return HANDLER_GO_ON;
-
- if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
- && con->file_started) {
- if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
- fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
- } else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
- /* optimistic read from backend */
- handler_t rc = fcgi_recv_response(srv, hctx); /*(might invalidate hctx)*/
- if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
- fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
- }
- }
-
- /* (do not receive request body before FCGI_AUTHORIZER has run or else
- * the request body is discarded with handler_ctx_clear() after running
- * the FastCGI Authorizer) */
-
- if (hctx->fcgi_mode != FCGI_AUTHORIZER
- && (0 == hctx->wb->bytes_in
- ? con->state == CON_STATE_READ_POST
- : (hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0))) {
- /* leave excess data in con->request_content_queue, which is
- * buffered to disk if too large and backend can not keep up */
- /*(64k - 4k to attempt to avoid temporary files
- * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
- if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096) {
- if (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
- con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
- }
- if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
- } else {
- handler_t r = connection_handle_read_post_state(srv, con);
- chunkqueue *req_cq = con->request_content_queue;
- #if 0 /*(not reached since we send 411 Length Required below)*/
- if (hctx->wb_reqlen < -1 && con->request.content_length >= 0) {
- /* (completed receiving Transfer-Encoding: chunked) */
- hctx->wb_reqlen = -hctx->wb_reqlen + con->request.content_length;
- fcgi_stdin_append(srv, con, hctx, hctx->request_id);
- }
- #endif
- if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
- fcgi_stdin_append(srv, con, hctx, hctx->request_id);
- if (fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_OUT) {
- return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
- }
- }
- if (r != HANDLER_GO_ON) return r;
-
- /* CGI environment requires that Content-Length be set.
- * Send 411 Length Required if Content-Length missing.
- * (occurs here if client sends Transfer-Encoding: chunked
- * and module is flagged to stream request body to backend) */
- if (-1 == con->request.content_length) {
- return connection_handle_read_post_error(srv, con, 411);
- }
- }
- }
-
- return ((0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
- && hctx->state != FCGI_STATE_CONNECT_DELAYED)
- ? fcgi_send_request(srv, hctx)
- : HANDLER_WAIT_FOR_EVENT;
-}
-
-
-static handler_t fcgi_recv_response(server *srv, handler_ctx *hctx) {
- connection *con = hctx->remote_conn;
- plugin_data *p = hctx->plugin_data;
-
- fcgi_proc *proc = hctx->proc;
- fcgi_extension_host *host= hctx->host;
- buffer *b = buffer_init();
-
- switch (http_response_read(srv, hctx->remote_conn, &hctx->opts,
- b, hctx->fd, &hctx->fde_ndx)) {
- default:
- break;
- case HANDLER_FINISHED:
- buffer_free(b);
- if (hctx->fcgi_mode == FCGI_AUTHORIZER &&
- (con->http_status == 200 ||
- con->http_status == 0)) {
- /*
- * If we are here in AUTHORIZER mode then a request for authorizer
- * was processed already, and status 200 has been returned. We need
- * now to handle authorized request.
- */
- buffer *physpath = NULL;
-
- if (!buffer_string_is_empty(host->docroot)) {
- buffer_copy_buffer(con->physical.doc_root, host->docroot);
- buffer_copy_buffer(con->physical.basedir, host->docroot);
-
- buffer_copy_buffer(con->physical.path, host->docroot);
- buffer_append_string_buffer(con->physical.path, con->uri.path);
- physpath = con->physical.path;
- }
-
- fcgi_backend_close(srv, hctx);
- handler_ctx_clear(hctx);
-
- /* don't do more than 6 loops here, that normally shouldn't happen */
- if (++con->loops_per_request > 5) {
- log_error_write(srv, __FILE__, __LINE__, "sb", "too many loops while processing request:", con->request.orig_uri);
- con->http_status = 500; /* Internal Server Error */
- con->mode = DIRECT;
- return HANDLER_FINISHED;
- }
-
- /* restart the request so other handlers can process it */
-
- if (physpath) con->physical.path = NULL;
- connection_response_reset(srv, con); /*(includes con->http_status = 0)*/
- if (physpath) con->physical.path = physpath; /* preserve con->physical.path with modified docroot */
-
- /*(FYI: if multiple FastCGI authorizers were to be supported,
- * next one could be started here instead of restarting request)*/
-
- con->mode = DIRECT;
- return HANDLER_COMEBACK;
- } else {
- /* we are done */
- fcgi_connection_close(srv, hctx);
- }
-
- return HANDLER_FINISHED;
- case HANDLER_COMEBACK: /*(not expected; treat as error)*/
- case HANDLER_ERROR:
- buffer_free(b);
- if (proc->is_local && 1 == proc->load && proc->pid == hctx->pid && proc->state != PROC_STATE_DIED) {
- if (0 != fcgi_proc_waitpid(srv, host, proc)) {
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssbsdsd",
- "--- fastcgi spawning",
- "\n\tsocket", proc->connection_name,
- "\n\tcurrent:", 1, "/", host->max_procs);
- }
-
- if (fcgi_spawn_connection(srv, p, host, proc)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "respawning failed, will retry later");
- }
- }
- }
-
- if (con->file_started == 0) {
- /* nothing has been sent out yet, try to use another child */
-
- if (hctx->wb->bytes_out == 0 &&
- hctx->reconnects++ < 5) {
-
- log_error_write(srv, __FILE__, __LINE__, "ssbsBSBs",
- "response not received, request not sent",
- "on socket:", proc->connection_name,
- "for", con->uri.path, "?", con->uri.query, ", reconnecting");
-
- return fcgi_reconnect(srv, hctx);
- }
-
- log_error_write(srv, __FILE__, __LINE__, "sosbsBSBs",
- "response not received, request sent:", hctx->wb->bytes_out,
- "on socket:", proc->connection_name,
- "for", con->uri.path, "?", con->uri.query, ", closing connection");
- } else {
- log_error_write(srv, __FILE__, __LINE__, "ssbsBSBs",
- "response already sent out, but backend returned error",
- "on socket:", proc->connection_name,
- "for", con->uri.path, "?", con->uri.query, ", terminating connection");
- }
-
- http_response_backend_error(srv, con);
- fcgi_connection_close(srv, hctx);
- return HANDLER_FINISHED;
- }
-
- buffer_free(b);
- return HANDLER_GO_ON;
-}
-
-
-static handler_t fcgi_handle_fdevent(server *srv, void *ctx, int revents) {
- handler_ctx *hctx = ctx;
- connection *con = hctx->remote_conn;
-
- joblist_append(srv, con);
-
- if (revents & FDEVENT_IN) {
- handler_t rc = fcgi_recv_response(srv, hctx);/*(might invalidate hctx)*/
- if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
- }
-
- if (revents & FDEVENT_OUT) {
- return fcgi_send_request(srv, hctx); /*(might invalidate hctx)*/
- }
-
- /* perhaps this issue is already handled */
- if (revents & FDEVENT_HUP) {
- if (hctx->state == FCGI_STATE_CONNECT_DELAYED) {
- /* getoptsock will catch this one (right ?)
- *
- * if we are in connect we might get an EINPROGRESS
- * in the first call and an FDEVENT_HUP in the
- * second round
- *
- * FIXME: as it is a bit ugly.
- *
- */
- fcgi_send_request(srv, hctx);
- } else if (con->file_started) {
- /* drain any remaining data from kernel pipe buffers
- * even if (con->conf.stream_response_body
- * & FDEVENT_STREAM_RESPONSE_BUFMIN)
- * since event loop will spin on fd FDEVENT_HUP event
- * until unregistered. */
- handler_t rc;
- do {
- rc = fcgi_recv_response(srv,hctx);/*(might invalidate hctx)*/
- } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
- return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
- } else {
- fcgi_proc *proc = hctx->proc;
- log_error_write(srv, __FILE__, __LINE__, "sBSbsbsd",
- "error: unexpected close of fastcgi connection for",
- con->uri.path, "?", con->uri.query,
- "(no fastcgi process on socket:", proc->connection_name, "?)",
- hctx->state);
-
- fcgi_connection_close(srv, hctx);
- }
- } else if (revents & FDEVENT_ERR) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "fcgi: got a FDEVENT_ERR. Don't know why.");
-
- http_response_backend_error(srv, con);
- fcgi_connection_close(srv, hctx);
- }
-
- return HANDLER_FINISHED;
-}
-
#define PATCH(x) \
p->conf.x = s->x;
static int fcgi_patch_connection(server *srv, connection *con, plugin_data *p) {
@@ -2602,222 +489,26 @@ static int fcgi_patch_connection(server *srv, connection *con, plugin_data *p) {
}
#undef PATCH
-
static handler_t fcgi_check_extension(server *srv, connection *con, void *p_d, int uri_path_handler) {
plugin_data *p = p_d;
- size_t s_len;
- size_t k;
- buffer *fn;
- fcgi_extension *extension = NULL;
- fcgi_extension_host *host = NULL;
- handler_ctx *hctx;
- unsigned short fcgi_mode;
+ handler_t rc;
if (con->mode != DIRECT) return HANDLER_GO_ON;
- fn = uri_path_handler ? con->uri.path : con->physical.path;
-
- if (buffer_string_is_empty(fn)) return HANDLER_GO_ON;
-
- s_len = buffer_string_length(fn);
-
fcgi_patch_connection(srv, con, p);
if (NULL == p->conf.exts) return HANDLER_GO_ON;
- /* check p->conf.exts_auth list and then p->conf.ext_resp list
- * (skip p->conf.exts_auth if array is empty or if FCGI_AUTHORIZER already ran in this request */
- hctx = con->plugin_ctx[p->id]; /*(not NULL if FCGI_AUTHORIZER ran; hctx->ext-auth check is redundant)*/
- fcgi_mode = (NULL == hctx || NULL == hctx->ext_auth)
- ? 0 /* FCGI_AUTHORIZER p->conf.exts_auth will be searched next */
- : FCGI_AUTHORIZER; /* FCGI_RESPONDER p->conf.exts_resp will be searched next */
-
- do {
-
- fcgi_exts *exts;
- if (0 == fcgi_mode) {
- fcgi_mode = FCGI_AUTHORIZER;
- exts = p->conf.exts_auth;
- } else {
- fcgi_mode = FCGI_RESPONDER;
- exts = p->conf.exts_resp;
- }
-
- if (0 == exts->used) continue;
-
- /* fastcgi.map-extensions maps extensions to existing fastcgi.server entries
- *
- * fastcgi.map-extensions = ( ".php3" => ".php" )
- *
- * fastcgi.server = ( ".php" => ... )
- *
- * */
-
- /* check if extension-mapping matches */
- for (k = 0; k < p->conf.ext_mapping->used; k++) {
- data_string *ds = (data_string *)p->conf.ext_mapping->data[k];
- size_t ct_len; /* length of the config entry */
-
- if (buffer_is_empty(ds->key)) continue;
-
- ct_len = buffer_string_length(ds->key);
-
- if (s_len < ct_len) continue;
-
- /* found a mapping */
- if (0 == strncmp(fn->ptr + s_len - ct_len, ds->key->ptr, ct_len)) {
- /* check if we know the extension */
-
- /* we can reuse k here */
- for (k = 0; k < exts->used; k++) {
- extension = exts->exts[k];
+ rc = gw_check_extension(srv, con, p, uri_path_handler, 0);
+ if (HANDLER_GO_ON != rc) return rc;
- if (buffer_is_equal(ds->value, extension->key)) {
- break;
- }
- }
-
- if (k == exts->used) {
- /* found nothing */
- extension = NULL;
- }
- break;
- }
- }
-
- if (extension == NULL) {
- size_t uri_path_len = buffer_string_length(con->uri.path);
-
- /* check if extension matches */
- for (k = 0; k < exts->used; k++) {
- size_t ct_len; /* length of the config entry */
- fcgi_extension *ext = exts->exts[k];
-
- if (buffer_is_empty(ext->key)) continue;
-
- ct_len = buffer_string_length(ext->key);
-
- /* check _url_ in the form "/fcgi_pattern" */
- if (ext->key->ptr[0] == '/') {
- if ((ct_len <= uri_path_len) &&
- (strncmp(con->uri.path->ptr, ext->key->ptr, ct_len) == 0)) {
- extension = ext;
- break;
- }
- } else if ((ct_len <= s_len) && (0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len))) {
- /* check extension in the form ".fcg" */
- extension = ext;
- break;
- }
- }
- }
-
- } while (NULL == extension && fcgi_mode != FCGI_RESPONDER);
-
- /* extension doesn't match */
- if (NULL == extension) {
- return HANDLER_GO_ON;
- }
-
- /* check if we have at least one server for this extension up and running */
- host = fcgi_extension_host_get(srv, con, p, extension);
- if (NULL == host) {
- return HANDLER_FINISHED;
- }
-
- /* a note about no handler is not sent yet */
- extension->note_is_sent = 0;
-
- /*
- * if check-local is disabled, use the uri.path handler
- *
- */
-
- /* init handler-context */
- if (uri_path_handler) {
- if (host->check_local != 0) {
- return HANDLER_GO_ON;
- } else {
- /* do not split path info for authorizer */
- if (fcgi_mode != FCGI_AUTHORIZER) {
- /* the prefix is the SCRIPT_NAME,
- * everything from start to the next slash
- * this is important for check-local = "disable"
- *
- * if prefix = /admin.fcgi
- *
- * /admin.fcgi/foo/bar
- *
- * SCRIPT_NAME = /admin.fcgi
- * PATH_INFO = /foo/bar
- *
- * if prefix = /fcgi-bin/
- *
- * /fcgi-bin/foo/bar
- *
- * SCRIPT_NAME = /fcgi-bin/foo
- * PATH_INFO = /bar
- *
- * if prefix = /, and fix-root-path-name is enable
- *
- * /fcgi-bin/foo/bar
- *
- * SCRIPT_NAME = /fcgi-bin/foo
- * PATH_INFO = /bar
- *
- */
- char *pathinfo;
-
- /* the rewrite is only done for /prefix/? matches */
- if (host->fix_root_path_name && extension->key->ptr[0] == '/' && extension->key->ptr[1] == '\0') {
- buffer_copy_buffer(con->request.pathinfo, con->uri.path);
- buffer_string_set_length(con->uri.path, 0);
- } else if (extension->key->ptr[0] == '/' &&
- buffer_string_length(con->uri.path) > buffer_string_length(extension->key) &&
- NULL != (pathinfo = strchr(con->uri.path->ptr + buffer_string_length(extension->key), '/'))) {
- /* rewrite uri.path and pathinfo */
-
- buffer_copy_string(con->request.pathinfo, pathinfo);
- buffer_string_set_length(con->uri.path, buffer_string_length(con->uri.path) - buffer_string_length(con->request.pathinfo));
- }
- }
- }
- }
-
- if (!hctx) hctx = handler_ctx_init();
-
- hctx->remote_conn = con;
- hctx->plugin_data = p;
- hctx->host = host;
- hctx->proc = NULL;
- hctx->ext = extension;
- fcgi_host_assign(srv, hctx);
-
- hctx->fcgi_mode = fcgi_mode;
- if (fcgi_mode == FCGI_AUTHORIZER) {
- hctx->ext_auth = hctx->ext;
- }
-
- /*hctx->conf.exts = p->conf.exts;*/
- /*hctx->conf.exts_auth = p->conf.exts_auth;*/
- /*hctx->conf.exts_resp = p->conf.exts_resp;*/
- /*hctx->conf.ext_mapping = p->conf.ext_mapping;*/
- hctx->conf.debug = p->conf.debug;
-
- hctx->opts.fdfmt = S_IFSOCK;
- hctx->opts.backend = BACKEND_FASTCGI;
- hctx->opts.authorizer = (fcgi_mode == FCGI_AUTHORIZER);
- hctx->opts.local_redir = 0;
- hctx->opts.xsendfile_allow = host->xsendfile_allow;
- hctx->opts.xsendfile_docroot = host->xsendfile_docroot;
- hctx->opts.parse = fcgi_recv_parse;
- hctx->opts.pdata = hctx;
-
- con->plugin_ctx[p->id] = hctx;
-
- con->mode = p->id;
-
- if (con->conf.log_request_handling) {
- log_error_write(srv, __FILE__, __LINE__, "s", "handling it in mod_fastcgi");
+ if (con->mode == p->id) {
+ handler_ctx *hctx = con->plugin_ctx[p->id];
+ hctx->opts.backend = BACKEND_FASTCGI;
+ hctx->opts.parse = fcgi_recv_parse;
+ hctx->opts.pdata = hctx;
+ hctx->stdin_append = fcgi_stdin_append;
+ hctx->create_env = fcgi_create_env;
+ hctx->rb = chunkqueue_init();
}
return HANDLER_GO_ON;
@@ -2834,72 +525,19 @@ static handler_t fcgi_check_extension_2(server *srv, connection *con, void *p_d)
}
-TRIGGER_FUNC(mod_fastcgi_handle_trigger) {
- plugin_data *p = p_d;
- size_t i, j, n;
-
-
- /* perhaps we should kill a connect attempt after 10-15 seconds
- *
- * currently we wait for the TCP timeout which is 180 seconds on Linux
- *
- *
- *
- */
-
- /* check all children if they are still up */
-
- for (i = 0; i < srv->config_context->used; i++) {
- plugin_config *conf;
- fcgi_exts *exts;
-
- conf = p->config_storage[i];
-
- exts = conf->exts;
- if (NULL == exts) continue;
-
- for (j = 0; j < exts->used; j++) {
- fcgi_extension *ex;
-
- ex = exts->exts[j];
-
- for (n = 0; n < ex->used; n++) {
-
- fcgi_proc *proc;
- fcgi_extension_host *host;
-
- host = ex->hosts[n];
-
- for (proc = host->first; proc; proc = proc->next) {
- fcgi_proc_waitpid(srv, host, proc);
- }
-
- fcgi_restart_dead_procs(srv, p, host);
-
- for (proc = host->unused_procs; proc; proc = proc->next) {
- fcgi_proc_waitpid(srv, host, proc);
- }
- }
- }
- }
-
- return HANDLER_GO_ON;
-}
-
-
int mod_fastcgi_plugin_init(plugin *p);
int mod_fastcgi_plugin_init(plugin *p) {
p->version = LIGHTTPD_VERSION_ID;
p->name = buffer_init_string("fastcgi");
- p->init = mod_fastcgi_init;
- p->cleanup = mod_fastcgi_free;
+ p->init = gw_init;
+ p->cleanup = gw_free;
p->set_defaults = mod_fastcgi_set_defaults;
- p->connection_reset = fcgi_connection_reset;
+ p->connection_reset = gw_connection_reset;
p->handle_uri_clean = fcgi_check_extension_1;
p->handle_subrequest_start = fcgi_check_extension_2;
- p->handle_subrequest = mod_fastcgi_handle_subrequest;
- p->handle_trigger = mod_fastcgi_handle_trigger;
+ p->handle_subrequest = gw_handle_subrequest;
+ p->handle_trigger = gw_handle_trigger;
p->data = NULL;
diff --git a/src/mod_proxy.c b/src/mod_proxy.c
index 0e300de4..11d3d460 100644
--- a/src/mod_proxy.c
+++ b/src/mod_proxy.c
@@ -1,128 +1,17 @@
#include "first.h"
+#include <string.h>
+#include <stdlib.h>
+
+#include "gw_backend.h"
+#include "base.h"
#include "array.h"
#include "buffer.h"
-#include "server.h"
+#include "inet_ntop_cache.h"
#include "keyvalue.h"
#include "log.h"
-
-#include "http_chunk.h"
-#include "fdevent.h"
-#include "inet_ntop_cache.h"
-#include "connections.h"
-#include "response.h"
-#include "joblist.h"
-
#include "plugin.h"
-
-#include "crc32.h"
-
-#include <sys/types.h>
-
-#include <unistd.h>
-#include <errno.h>
-#include <limits.h>
-#include <string.h>
-#include <stdlib.h>
-
-#include "sys-socket.h"
-
-
-
-
-#define data_proxy data_fastcgi
-#define data_proxy_init data_fastcgi_init
-
-
-typedef struct {
- DATA_UNSET;
-
- buffer *host;
-
- unsigned short port;
- unsigned short family;
-
- time_t disable_ts;
- int is_disabled;
- size_t balance;
-
- int usage; /* fair-balancing needs the no. of connections active on this host */
- int last_used_ndx; /* round robin */
-} data_fastcgi;
-
-static data_fastcgi *data_fastcgi_init(void);
-
-
-static data_unset *data_fastcgi_copy(const data_unset *s) {
- data_fastcgi *src = (data_fastcgi *)s;
- data_fastcgi *ds = data_fastcgi_init();
-
- buffer_copy_buffer(ds->key, src->key);
- buffer_copy_buffer(ds->host, src->host);
- ds->is_index_key = src->is_index_key;
- return (data_unset *)ds;
-}
-
-static void data_fastcgi_free(data_unset *d) {
- data_fastcgi *ds = (data_fastcgi *)d;
-
- buffer_free(ds->key);
- buffer_free(ds->host);
-
- free(d);
-}
-
-static void data_fastcgi_reset(data_unset *d) {
- data_fastcgi *ds = (data_fastcgi *)d;
-
- buffer_reset(ds->key);
- buffer_reset(ds->host);
-
-}
-
-static int data_fastcgi_insert_dup(data_unset *dst, data_unset *src) {
- UNUSED(dst);
-
- src->free(src);
-
- return 0;
-}
-
-#include <stdio.h> /* fprintf() */
-static void data_fastcgi_print(const data_unset *d, int depth) {
- data_fastcgi *ds = (data_fastcgi *)d;
- UNUSED(depth);
-
- fprintf(stdout, "fastcgi(%s)", ds->host->ptr);
-}
-
-
-static data_fastcgi *data_fastcgi_init(void) {
- data_fastcgi *ds;
-
- ds = calloc(1, sizeof(*ds));
- force_assert(NULL != ds);
-
- ds->key = buffer_init();
- ds->host = buffer_init();
- ds->port = 0;
- ds->family = 0;
- ds->is_disabled = 0;
-
- ds->copy = data_fastcgi_copy;
- ds->free = data_fastcgi_free;
- ds->reset = data_fastcgi_reset;
- ds->insert_dup = data_fastcgi_insert_dup;
- ds->print = data_fastcgi_print;
- ds->type = TYPE_OTHER;
-
- return ds;
-}
-
-
-
-
-#define PROXY_RETRY_TIMEOUT 60
+#include "status_counter.h"
/**
*
@@ -146,14 +35,6 @@ typedef struct http_header_remap_opts {
} http_header_remap_opts;
typedef enum {
- PROXY_BALANCE_UNSET,
- PROXY_BALANCE_FAIR,
- PROXY_BALANCE_HASH,
- PROXY_BALANCE_RR,
- PROXY_BALANCE_STICKY
-} proxy_balance_t;
-
-typedef enum {
PROXY_FORWARDED_NONE = 0x00,
PROXY_FORWARDED_FOR = 0x01,
PROXY_FORWARDED_PROTO = 0x02,
@@ -163,22 +44,17 @@ typedef enum {
} proxy_forwarded_t;
typedef struct {
- array *extensions;
+ gw_plugin_config gw;
array *forwarded_params;
array *header_params;
- unsigned short debug;
unsigned short replace_http_host;
unsigned int forwarded;
- proxy_balance_t balance;
http_header_remap_opts header;
} plugin_config;
typedef struct {
PLUGIN_DATA;
-
- buffer *balance_buf;
-
plugin_config **config_storage;
plugin_config conf;
@@ -186,76 +62,19 @@ typedef struct {
static int proxy_check_extforward;
-typedef enum {
- PROXY_STATE_INIT,
- PROXY_STATE_CONNECT,
- PROXY_STATE_PREPARE_WRITE,
- PROXY_STATE_WRITE,
- PROXY_STATE_READ
-} proxy_connection_state_t;
-
-enum { PROXY_STDOUT, PROXY_END_REQUEST };
-
typedef struct {
- proxy_connection_state_t state;
- time_t state_timestamp;
-
- data_proxy *host;
-
- buffer *response;
-
- chunkqueue *wb;
- off_t wb_reqlen;
-
- int fd; /* fd to the proxy process */
- int fde_ndx; /* index into the fd-event buffer */
-
+ gw_handler_ctx gw;
http_response_opts opts;
http_header_remap_opts remap_hdrs;
plugin_config conf;
-
- connection *remote_conn; /* dumb pointer */
- plugin_data *plugin_data; /* dumb pointer */
- data_array *ext;
} handler_ctx;
-/* ok, we need a prototype */
-static handler_t proxy_handle_fdevent(server *srv, void *ctx, int revents);
-
-static handler_ctx * handler_ctx_init(void) {
- handler_ctx * hctx;
-
-
- hctx = calloc(1, sizeof(*hctx));
-
- hctx->state = PROXY_STATE_INIT;
- hctx->host = NULL;
-
- hctx->response = buffer_init();
-
- hctx->wb = chunkqueue_init();
- hctx->wb_reqlen = 0;
-
- hctx->fd = -1;
- hctx->fde_ndx = -1;
-
- return hctx;
-}
-
-static void handler_ctx_free(handler_ctx *hctx) {
- buffer_free(hctx->response);
- chunkqueue_free(hctx->wb);
-
- free(hctx);
-}
INIT_FUNC(mod_proxy_init) {
plugin_data *p;
p = calloc(1, sizeof(*p));
- p->balance_buf = buffer_init();
-
return p;
}
@@ -265,8 +84,6 @@ FREE_FUNC(mod_proxy_free) {
UNUSED(srv);
- buffer_free(p->balance_buf);
-
if (p->config_storage) {
size_t i;
for (i = 0; i < srv->config_context->used; i++) {
@@ -274,11 +91,12 @@ FREE_FUNC(mod_proxy_free) {
if (NULL == s) continue;
- array_free(s->extensions);
array_free(s->forwarded_params);
array_free(s->header_params);
- free(s);
+ /*assert(0 == offsetof(s->gw));*/
+ gw_plugin_config_free(&s->gw);
+ /*free(s);*//*free'd by gw_plugin_config_free()*/
}
free(p->config_storage);
}
@@ -296,10 +114,11 @@ SETDEFAULTS_FUNC(mod_proxy_set_defaults) {
config_values_t cv[] = {
{ "proxy.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
{ "proxy.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
- { "proxy.balance", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
+ { "proxy.balance", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
{ "proxy.replace-http-host", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
{ "proxy.forwarded", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
{ "proxy.header", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
+ { "proxy.map-extensions", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
};
@@ -310,21 +129,20 @@ SETDEFAULTS_FUNC(mod_proxy_set_defaults) {
plugin_config *s;
s = calloc(1, sizeof(plugin_config));
- s->extensions = array_init();
- s->debug = 0;
+ s->gw.debug = 0;
s->replace_http_host = 0;
s->forwarded_params = array_init();
s->forwarded = PROXY_FORWARDED_NONE;
s->header_params = array_init();
+ s->gw.ext_mapping = array_init();
- cv[0].destination = s->extensions;
- cv[1].destination = &(s->debug);
- cv[2].destination = p->balance_buf;
+ cv[0].destination = NULL; /* T_CONFIG_LOCAL */
+ cv[1].destination = &(s->gw.debug);
+ cv[2].destination = NULL; /* T_CONFIG_LOCAL */
cv[3].destination = &(s->replace_http_host);
cv[4].destination = s->forwarded_params;
cv[5].destination = s->header_params;
-
- buffer_reset(p->balance_buf);
+ cv[6].destination = s->gw.ext_mapping;
p->config_storage[i] = s;
@@ -332,22 +150,26 @@ SETDEFAULTS_FUNC(mod_proxy_set_defaults) {
return HANDLER_ERROR;
}
- if (buffer_string_is_empty(p->balance_buf)) {
- s->balance = PROXY_BALANCE_FAIR;
- } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("fair"))) {
- s->balance = PROXY_BALANCE_FAIR;
- } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("round-robin"))) {
- s->balance = PROXY_BALANCE_RR;
- } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("hash"))) {
- s->balance = PROXY_BALANCE_HASH;
- } else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("sticky"))) {
- s->balance = PROXY_BALANCE_STICKY;
- } else {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "proxy.balance has to be one of: fair, round-robin, hash, sticky, but not:", p->balance_buf);
+ du = array_get_element(config->value, "proxy.server");
+ if (!gw_set_defaults_backend(srv, (gw_plugin_data *)p, du, i, 0)) {
return HANDLER_ERROR;
}
+ du = array_get_element(config->value, "proxy.balance");
+ if (!gw_set_defaults_balance(srv, &s->gw, du)) {
+ return HANDLER_ERROR;
+ }
+
+ /* disable check-local for all exts (default enabled) */
+ if (s->gw.exts) { /*(check after gw_set_defaults_backend())*/
+ for (size_t j = 0; j < s->gw.exts->used; ++j) {
+ gw_extension *ex = s->gw.exts->exts[j];
+ for (size_t n = 0; n < ex->used; ++n) {
+ ex->hosts[n]->check_local = 0;
+ }
+ }
+ }
+
if (!array_is_kvany(s->forwarded_params)) {
log_error_write(srv, __FILE__, __LINE__, "s",
"unexpected value for proxy.forwarded; expected ( \"param\" => \"value\" )");
@@ -439,108 +261,6 @@ SETDEFAULTS_FUNC(mod_proxy_set_defaults) {
return HANDLER_ERROR;
}
}
-
- if (NULL != (du = array_get_element(config->value, "proxy.server"))) {
- size_t j;
- data_array *da = (data_array *)du;
-
- if (du->type != TYPE_ARRAY || !array_is_kvarray(da->value)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "unexpected value for proxy.server; expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
-
- return HANDLER_ERROR;
- }
-
- /*
- * proxy.server = ( "<ext>" => ...,
- * "<ext>" => ... )
- */
-
- for (j = 0; j < da->value->used; j++) {
- data_array *da_ext = (data_array *)da->value->data[j];
- size_t n;
-
- /*
- * proxy.server = ( "<ext>" =>
- * ( "<host>" => ( ... ),
- * "<host>" => ( ... )
- * ),
- * "<ext>" => ... )
- */
-
- for (n = 0; n < da_ext->value->used; n++) {
- data_array *da_host = (data_array *)da_ext->value->data[n];
-
- data_proxy *df;
- data_array *dfa;
-
- config_values_t pcv[] = {
- { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
- { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
- { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
- };
-
- if (da_host->type != TYPE_ARRAY || !array_is_kvany(da_host->value)) {
- log_error_write(srv, __FILE__, __LINE__, "SBS",
- "unexpected value for proxy.server near [",
- da_host->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
-
- return HANDLER_ERROR;
- }
-
- df = data_proxy_init();
-
- df->port = 80;
-
- buffer_copy_buffer(df->key, da_host->key);
-
- pcv[0].destination = df->host;
- pcv[1].destination = &(df->port);
-
- if (0 != config_insert_values_internal(srv, da_host->value, pcv, T_CONFIG_SCOPE_CONNECTION)) {
- df->free((data_unset*) df);
- return HANDLER_ERROR;
- }
-
- if (buffer_string_is_empty(df->host)) {
- log_error_write(srv, __FILE__, __LINE__, "sbbbs",
- "missing key (string):",
- da->key,
- da_ext->key,
- da_host->key,
- "host");
-
- df->free((data_unset*) df);
- return HANDLER_ERROR;
- }
-
- df->family = AF_INET;
- #ifdef HAVE_SYS_UN_H
- if (strchr(df->host->ptr, '/')) {
- df->family = AF_UNIX;
- }
- #endif
- #if defined(HAVE_IPV6)
- if (strchr(df->host->ptr, ':')) {
- df->family = AF_INET6;
- }
- #endif
-
- /* if extension already exists, take it */
-
- if (NULL == (dfa = (data_array *)array_get_element_klen(s->extensions, CONST_BUF_LEN(da_ext->key)))) {
- dfa = data_array_init();
-
- buffer_copy_buffer(dfa->key, da_ext->key);
-
- array_insert_unique(dfa->value, (data_unset *)df);
- array_insert_unique(s->extensions, (data_unset *)dfa);
- } else {
- array_insert_unique(dfa->value, (data_unset *)df);
- }
- }
- }
- }
}
for (i = 0; i < srv->srvconf.modules->used; i++) {
@@ -555,265 +275,6 @@ SETDEFAULTS_FUNC(mod_proxy_set_defaults) {
}
-static void proxy_backend_close(server *srv, handler_ctx *hctx) {
- if (hctx->fd != -1) {
- fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
- fdevent_unregister(srv->ev, hctx->fd);
- fdevent_sched_close(srv->ev, hctx->fd, 1);
- hctx->fd = -1;
- hctx->fde_ndx = -1;
- }
-
- if (hctx->host) {
- hctx->host->usage--;
- hctx->host = NULL;
- }
-}
-
-static data_proxy * mod_proxy_extension_host_get(server *srv, connection *con, data_array *extension, proxy_balance_t balance, int debug) {
- unsigned long last_max = ULONG_MAX;
- int max_usage = INT_MAX;
- int ndx = -1;
- size_t k;
-
- if (extension->value->used == 1) {
- if ( ((data_proxy *)extension->value->data[0])->is_disabled ) {
- ndx = -1;
- } else {
- ndx = 0;
- }
- } else if (extension->value->used != 0) switch(balance) {
- case PROXY_BALANCE_HASH:
- /* hash balancing */
-
- if (debug) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "proxy - used hash balancing, hosts:", extension->value->used);
- }
-
- for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->value->used; k++) {
- data_proxy *host = (data_proxy *)extension->value->data[k];
- unsigned long cur_max;
-
- if (host->is_disabled) continue;
-
- cur_max = generate_crc32c(CONST_BUF_LEN(con->uri.path)) +
- generate_crc32c(CONST_BUF_LEN(host->host)) + /* we can cache this */
- generate_crc32c(CONST_BUF_LEN(con->uri.authority));
-
- if (debug) {
- log_error_write(srv, __FILE__, __LINE__, "sbbbd",
- "proxy - election:",
- con->uri.path,
- host->host,
- con->uri.authority,
- cur_max);
- }
-
- if ((last_max == ULONG_MAX) || /* first round */
- (cur_max > last_max)) {
- last_max = cur_max;
-
- ndx = k;
- }
- }
-
- break;
- case PROXY_BALANCE_FAIR:
- /* fair balancing */
- if (debug) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "proxy - used fair balancing");
- }
-
- for (k = 0, ndx = -1, max_usage = INT_MAX; k < extension->value->used; k++) {
- data_proxy *host = (data_proxy *)extension->value->data[k];
-
- if (host->is_disabled) continue;
-
- if (host->usage < max_usage) {
- max_usage = host->usage;
-
- ndx = k;
- }
- }
-
- break;
- case PROXY_BALANCE_RR: {
- data_proxy *host;
-
- /* round robin */
- if (debug) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "proxy - used round-robin balancing");
- }
-
- /* just to be sure */
- force_assert(extension->value->used < INT_MAX);
-
- host = (data_proxy *)extension->value->data[0];
-
- /* Use last_used_ndx from first host in list */
- k = host->last_used_ndx;
- ndx = k + 1; /* use next host after the last one */
- if (ndx < 0) ndx = 0;
-
- /* Search first active host after last_used_ndx */
- while ( ndx < (int) extension->value->used
- && (host = (data_proxy *)extension->value->data[ndx])->is_disabled ) ndx++;
-
- if (ndx >= (int) extension->value->used) {
- /* didn't found a higher id, wrap to the start */
- for (ndx = 0; ndx <= (int) k; ndx++) {
- host = (data_proxy *)extension->value->data[ndx];
- if (!host->is_disabled) break;
- }
-
- /* No active host found */
- if (host->is_disabled) ndx = -1;
- }
-
- /* Save new index for next round */
- ((data_proxy *)extension->value->data[0])->last_used_ndx = ndx;
-
- break;
- }
- case PROXY_BALANCE_STICKY:
- /* source sticky balancing */
-
- if (debug) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "proxy - used sticky balancing, hosts:", extension->value->used);
- }
-
- for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->value->used; k++) {
- data_proxy *host = (data_proxy *)extension->value->data[k];
- unsigned long cur_max;
-
- if (host->is_disabled) continue;
-
- cur_max = generate_crc32c(CONST_BUF_LEN(con->dst_addr_buf)) +
- generate_crc32c(CONST_BUF_LEN(host->host)) +
- host->port;
-
- if (debug) {
- log_error_write(srv, __FILE__, __LINE__, "sbbdd",
- "proxy - election:",
- con->dst_addr_buf,
- host->host,
- host->port,
- cur_max);
- }
-
- if ((last_max == ULONG_MAX) || /* first round */
- (cur_max > last_max)) {
- last_max = cur_max;
-
- ndx = k;
- }
- }
-
- break;
- default:
- break;
- }
-
- /* found a server */
- if (ndx != -1) {
- data_proxy *host = (data_proxy *)extension->value->data[ndx];
-
- if (debug) {
- log_error_write(srv, __FILE__, __LINE__, "sbd",
- "proxy - found a host",
- host->host, host->port);
- }
-
- host->usage++;
- return host;
- } else {
- /* no handler found */
- con->http_status = 503; /* Service Unavailable */
- con->mode = DIRECT;
-
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "no proxy-handler found for:",
- con->uri.path);
-
- return NULL;
- }
-}
-
-static void proxy_connection_close(server *srv, handler_ctx *hctx) {
- plugin_data *p;
- connection *con;
-
- p = hctx->plugin_data;
- con = hctx->remote_conn;
-
- proxy_backend_close(srv, hctx);
- handler_ctx_free(hctx);
- con->plugin_ctx[p->id] = NULL;
-
- /* finish response (if not already con->file_started, con->file_finished) */
- if (con->mode == p->id) {
- http_response_backend_done(srv, con);
- }
-}
-
-static handler_t proxy_reconnect(server *srv, handler_ctx *hctx) {
- proxy_backend_close(srv, hctx);
-
- hctx->host = mod_proxy_extension_host_get(srv, hctx->remote_conn, hctx->ext, hctx->conf.balance, (int)hctx->conf.debug);
- if (NULL == hctx->host) return HANDLER_FINISHED;
-
- hctx->state = PROXY_STATE_INIT;
- return HANDLER_COMEBACK;
-}
-
-static int proxy_establish_connection(server *srv, handler_ctx *hctx) {
- sock_addr addr;
- struct sockaddr *proxy_addr = (struct sockaddr *)&addr;
- socklen_t servlen;
-
- data_proxy *host= hctx->host;
- int proxy_fd = hctx->fd;
-
- if (host->family == AF_UNIX) {
- if (1 != sock_addr_from_str_hints(srv, &addr, &servlen, host->host->ptr, host->family, 0)) {
- return -1;
- }
- }
- else {
- if (1 != sock_addr_from_buffer_hints_numeric(srv, &addr, &servlen, host->host, host->family, host->port)) {
- return -1;
- }
- }
-
- if (-1 == connect(proxy_fd, proxy_addr, servlen)) {
- if (errno == EINPROGRESS || errno == EALREADY) {
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "connect delayed:", proxy_fd);
- }
-
- return 1;
- } else {
-
- log_error_write(srv, __FILE__, __LINE__, "sdsd",
- "connect failed:", proxy_fd, strerror(errno), errno);
-
- return -1;
- }
- }
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "connect succeeded: ", proxy_fd);
- }
-
- return 0;
-}
-
-
/* (future: might move to http-header-glue.c) */
static const buffer * http_header_remap_host_match (buffer *b, size_t off, http_header_remap_opts *remap_hdrs, int is_req, size_t alen)
{
@@ -1253,8 +714,9 @@ static void proxy_set_Forwarded(connection *con, const unsigned int flags) {
}
-static int proxy_create_env(server *srv, handler_ctx *hctx) {
- connection *con = hctx->remote_conn;
+static handler_t proxy_create_env(server *srv, gw_handler_ctx *gwhctx) {
+ handler_ctx *hctx = (handler_ctx *)gwhctx;
+ connection *con = hctx->gw.remote_conn;
buffer *b = buffer_init();
const int remap_headers = (NULL != hctx->remap_hdrs.urlpaths
|| NULL != hctx->remap_hdrs.hosts_request);
@@ -1275,13 +737,13 @@ static int proxy_create_env(server *srv, handler_ctx *hctx) {
else
buffer_append_string_len(b, CONST_STR_LEN(" HTTP/1.1\r\n"));
- if (hctx->conf.replace_http_host && !buffer_string_is_empty(hctx->host->key)) {
- if (hctx->conf.debug > 1) {
+ if (hctx->conf.replace_http_host && !buffer_string_is_empty(hctx->gw.host->id)) {
+ if (hctx->gw.conf.debug > 1) {
log_error_write(srv, __FILE__, __LINE__, "SBS",
- "proxy - using \"", hctx->host->key, "\" as HTTP Host");
+ "proxy - using \"", hctx->gw.host->id, "\" as HTTP Host");
}
buffer_append_string_len(b, CONST_STR_LEN("Host: "));
- buffer_append_string_buffer(b, hctx->host->key);
+ buffer_append_string_buffer(b, hctx->gw.host->id);
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
} else if (!buffer_string_is_empty(con->request.http_host)) {
buffer_append_string_len(b, CONST_STR_LEN("Host: "));
@@ -1382,151 +844,36 @@ static int proxy_create_env(server *srv, handler_ctx *hctx) {
else
buffer_append_string_len(b, CONST_STR_LEN("Connection: close, upgrade\r\n\r\n"));
- hctx->wb_reqlen = buffer_string_length(b);
- chunkqueue_append_buffer(hctx->wb, b);
+ hctx->gw.wb_reqlen = buffer_string_length(b);
+ chunkqueue_append_buffer(hctx->gw.wb, b);
buffer_free(b);
if (con->request.content_length) {
- chunkqueue_append_chunkqueue(hctx->wb, con->request_content_queue);
+ chunkqueue_append_chunkqueue(hctx->gw.wb, con->request_content_queue);
if (con->request.content_length > 0)
- hctx->wb_reqlen += con->request.content_length; /* total req size */
+ hctx->gw.wb_reqlen += con->request.content_length; /* total req size */
else /* as-yet-unknown total request size (Transfer-Encoding: chunked)*/
- hctx->wb_reqlen = -hctx->wb_reqlen;
+ hctx->gw.wb_reqlen = -hctx->gw.wb_reqlen;
}
- return 0;
-}
-
-static int proxy_set_state(server *srv, handler_ctx *hctx, proxy_connection_state_t state) {
- hctx->state = state;
- hctx->state_timestamp = srv->cur_ts;
-
- return 0;
-}
-
-
-static handler_t proxy_write_request(server *srv, handler_ctx *hctx) {
- data_proxy *host= hctx->host;
- connection *con = hctx->remote_conn;
-
- int ret;
-
- switch(hctx->state) {
- case PROXY_STATE_INIT:
- {
- if (-1 == (hctx->fd = fdevent_socket_nb_cloexec(host->family, SOCK_STREAM, 0))) {
- log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed: ", strerror(errno));
- return HANDLER_ERROR;
- }
- }
-
- srv->cur_fds++;
-
- fdevent_register(srv->ev, hctx->fd, proxy_handle_fdevent, hctx);
-
- if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
- log_error_write(srv, __FILE__, __LINE__, "ss", "fcntl failed: ", strerror(errno));
-
- return HANDLER_ERROR;
- }
-
- /* fall through */
- case PROXY_STATE_CONNECT:
- if (hctx->state == PROXY_STATE_INIT) {
- switch (proxy_establish_connection(srv, hctx)) {
- case 1:
- proxy_set_state(srv, hctx, PROXY_STATE_CONNECT);
-
- /* connection is in progress, wait for an event and call getsockopt() below */
-
- fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
-
- return HANDLER_WAIT_FOR_EVENT;
- case -1:
- /* if ECONNREFUSED choose another connection */
- return HANDLER_ERROR;
- default:
- /* everything is ok, go on */
- break;
- }
- } else {
- int socket_error = fdevent_connect_status(hctx->fd);
- if (socket_error != 0) {
- log_error_write(srv, __FILE__, __LINE__, "sssd",
- "establishing connection failed:", strerror(socket_error),
- "port:", hctx->host->port);
-
- return HANDLER_ERROR;
- }
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "s", "proxy - connect - delayed success");
- }
- }
-
- /* ok, we have the connection */
-
- proxy_set_state(srv, hctx, PROXY_STATE_PREPARE_WRITE);
- /* fall through */
- case PROXY_STATE_PREPARE_WRITE:
- proxy_create_env(srv, hctx);
-
- fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
- proxy_set_state(srv, hctx, PROXY_STATE_WRITE);
-
- /* fall through */
- case PROXY_STATE_WRITE:;
- ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
-
- chunkqueue_remove_finished_chunks(hctx->wb);
-
- if (-1 == ret) { /* error on our side */
- log_error_write(srv, __FILE__, __LINE__, "ssd", "write failed:", strerror(errno), errno);
-
- return HANDLER_ERROR;
- } else if (-2 == ret) { /* remote close */
- log_error_write(srv, __FILE__, __LINE__, "ssd", "write failed, remote connection close:", strerror(errno), errno);
-
- return HANDLER_ERROR;
- }
-
- if (hctx->wb->bytes_out == hctx->wb_reqlen) {
- fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- proxy_set_state(srv, hctx, PROXY_STATE_READ);
- } else {
- off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
- if ((hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0) && wblen < 65536 - 16384) {
- /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
- if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
- con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
- con->is_readable = 1; /* trigger optimistic read from client */
- }
- }
- if (0 == wblen) {
- fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- } else {
- fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- }
- }
-
- return HANDLER_WAIT_FOR_EVENT;
- case PROXY_STATE_READ:
- /* waiting for a response */
- return HANDLER_WAIT_FOR_EVENT;
- default:
- log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
- return HANDLER_ERROR;
- }
+ status_counter_inc(srv, CONST_STR_LEN("proxy.requests"));
+ return HANDLER_GO_ON;
}
#define PATCH(x) \
p->conf.x = s->x;
+#define PATCH_GW(x) \
+ p->conf.gw.x = s->gw.x;
static int mod_proxy_patch_connection(server *srv, connection *con, plugin_data *p) {
size_t i, j;
plugin_config *s = p->config_storage[0];
- PATCH(extensions);
- PATCH(debug);
- PATCH(balance);
+ PATCH_GW(exts);
+ PATCH_GW(exts_auth);
+ PATCH_GW(exts_resp);
+ PATCH_GW(debug);
+ PATCH_GW(ext_mapping);
+ PATCH_GW(balance);
PATCH(replace_http_host);
PATCH(forwarded);
PATCH(header); /*(copies struct)*/
@@ -1544,11 +891,15 @@ static int mod_proxy_patch_connection(server *srv, connection *con, plugin_data
data_unset *du = dc->value->data[j];
if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.server"))) {
- PATCH(extensions);
+ PATCH_GW(exts);
+ PATCH_GW(exts_auth);
+ PATCH_GW(exts_resp);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.debug"))) {
- PATCH(debug);
+ PATCH_GW(debug);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.balance"))) {
- PATCH(balance);
+ PATCH_GW(balance);
+ } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.map-extensions"))) {
+ PATCH_GW(ext_mapping);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.replace-http-host"))) {
PATCH(replace_http_host);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("proxy.forwarded"))) {
@@ -1561,118 +912,17 @@ static int mod_proxy_patch_connection(server *srv, connection *con, plugin_data
return 0;
}
+#undef PATCH_GW
#undef PATCH
-static handler_t proxy_send_request(server *srv, handler_ctx *hctx) {
- /* ok, create the request */
- handler_t rc = proxy_write_request(srv, hctx);
- if (HANDLER_ERROR != rc) {
- return rc;
- } else {
- data_proxy *host = hctx->host;
- log_error_write(srv, __FILE__, __LINE__, "sbdd", "proxy-server disabled:",
- host->host,
- host->port,
- hctx->fd);
-
- /* disable this server */
- host->is_disabled = 1;
- host->disable_ts = srv->cur_ts;
-
- /* reset the environment and restart the sub-request */
- return proxy_reconnect(srv, hctx);
- }
-}
-
-
-static handler_t proxy_recv_response(server *srv, handler_ctx *hctx);
-
-
-SUBREQUEST_FUNC(mod_proxy_handle_subrequest) {
- plugin_data *p = p_d;
-
- handler_ctx *hctx = con->plugin_ctx[p->id];
-
- if (NULL == hctx) return HANDLER_GO_ON;
-
- /* not my job */
- if (con->mode != p->id) return HANDLER_GO_ON;
-
- if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
- && con->file_started) {
- if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
- fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
- } else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
- /* optimistic read from backend */
- handler_t rc = proxy_recv_response(srv, hctx); /*(might invalidate hctx)*/
- if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
- fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
- }
- }
-
- if (0 == hctx->wb->bytes_in
- ? con->state == CON_STATE_READ_POST
- : (hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0)) {
- /* leave excess data in con->request_content_queue, which is
- * buffered to disk if too large and backend can not keep up */
- /*(64k - 4k to attempt to avoid temporary files
- * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
- if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096) {
- if (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
- con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
- }
- if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
- } else {
- handler_t r = connection_handle_read_post_state(srv, con);
- chunkqueue *req_cq = con->request_content_queue;
- #if 0 /*(not reached since we send 411 Length Required below)*/
- if (hctx->wb_reqlen < -1 && con->request.content_length >= 0) {
- /* (completed receiving Transfer-Encoding: chunked) */
- hctx->wb_reqlen = -hctx->wb_reqlen + con->request.content_length;
- }
- #endif
- if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
- chunkqueue_append_chunkqueue(hctx->wb, req_cq);
- if (fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_OUT) {
- return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
- }
- }
- if (r != HANDLER_GO_ON) return r;
-
- /* mod_proxy sends HTTP/1.0 request and ideally should send
- * Content-Length with request if request body is present, so
- * send 411 Length Required if Content-Length missing.
- * (occurs here if client sends Transfer-Encoding: chunked
- * and module is flagged to stream request body to backend) */
- if (-1 == con->request.content_length) {
- return connection_handle_read_post_error(srv, con, 411);
- }
- }
- }
-
- return ((0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
- && hctx->state != PROXY_STATE_CONNECT)
- ? proxy_send_request(srv, hctx)
- : HANDLER_WAIT_FOR_EVENT;
-}
-
-
-static handler_t proxy_response_read(server *srv, handler_ctx *hctx) {
- connection * const con = hctx->remote_conn;
- const int file_started = con->file_started;
- const handler_t rc =
- http_response_read(srv, con, &hctx->opts,
- hctx->response, hctx->fd, &hctx->fde_ndx);
-
- if (file_started || !con->file_started || con->mode == DIRECT) return rc;
-
+static handler_t proxy_response_headers(server *srv, connection *con, struct http_response_opts_t *opts) {
/* response headers just completed */
+ handler_ctx *hctx = (handler_ctx *)opts->pdata;
if (con->parsed_response & HTTP_UPGRADE) {
if (hctx->remap_hdrs.upgrade && con->http_status == 101) {
/* 101 Switching Protocols; transition to transparent proxy */
- hctx->wb_reqlen = -1;
- proxy_set_state(srv, hctx, PROXY_STATE_WRITE);
+ gw_set_transparent(srv, &hctx->gw);
http_response_upgrade_read_body_unknown(srv, con);
}
else {
@@ -1691,7 +941,7 @@ static handler_t proxy_response_read(server *srv, handler_ctx *hctx) {
if (NULL == hctx->remap_hdrs.urlpaths
&& NULL == hctx->remap_hdrs.hosts_response)
- return rc;
+ return HANDLER_GO_ON;
if (con->parsed_response & HTTP_LOCATION) {
data_string *ds = (data_string *)
@@ -1709,150 +959,28 @@ static handler_t proxy_response_read(server *srv, handler_ctx *hctx) {
if (ds) http_header_remap_setcookie(ds->value, 0, &hctx->remap_hdrs);
}
- return rc;
-}
-
-static handler_t proxy_recv_response(server *srv, handler_ctx *hctx) {
- switch (proxy_response_read(srv, hctx)) {
- default:
- return HANDLER_GO_ON;
- case HANDLER_ERROR:
- case HANDLER_COMEBACK: /*(not expected; treat as error)*/
- http_response_backend_error(srv, hctx->remote_conn);
- /* fall through */
- case HANDLER_FINISHED:
- proxy_connection_close(srv, hctx);
- return HANDLER_FINISHED;
- }
-}
-
-
-static handler_t proxy_handle_fdevent(server *srv, void *ctx, int revents) {
- handler_ctx *hctx = ctx;
- connection *con = hctx->remote_conn;
-
- joblist_append(srv, con);
-
- if (revents & FDEVENT_IN) {
- handler_t rc = proxy_recv_response(srv,hctx);/*(might invalidate hctx)*/
- if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
- }
-
- if (revents & FDEVENT_OUT) {
- return proxy_send_request(srv, hctx); /*(might invalidate hctx)*/
- }
-
- /* perhaps this issue is already handled */
- if (revents & FDEVENT_HUP) {
- if (hctx->state == PROXY_STATE_CONNECT) {
- /* connect() -> EINPROGRESS -> HUP */
- proxy_send_request(srv, hctx); /*(might invalidate hctx)*/
- } else if (con->file_started) {
- /* drain any remaining data from kernel pipe buffers
- * even if (con->conf.stream_response_body
- * & FDEVENT_STREAM_RESPONSE_BUFMIN)
- * since event loop will spin on fd FDEVENT_HUP event
- * until unregistered. */
- handler_t rc;
- do {
- rc = proxy_recv_response(srv,hctx);/*(might invalidate hctx)*/
- } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
- return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
- } else {
- proxy_connection_close(srv, hctx);
- }
- } else if (revents & FDEVENT_ERR) {
- log_error_write(srv, __FILE__, __LINE__, "sd", "proxy-FDEVENT_ERR, but no HUP", revents);
-
- http_response_backend_error(srv, con);
- proxy_connection_close(srv, hctx);
- }
-
- return HANDLER_FINISHED;
+ return HANDLER_GO_ON;
}
static handler_t mod_proxy_check_extension(server *srv, connection *con, void *p_d) {
plugin_data *p = p_d;
- size_t s_len;
- size_t k;
- buffer *fn;
- data_array *extension = NULL;
- data_proxy *host;
+ handler_t rc;
if (con->mode != DIRECT) return HANDLER_GO_ON;
- /* Possibly, we processed already this request */
- if (con->file_started == 1) return HANDLER_GO_ON;
-
mod_proxy_patch_connection(srv, con, p);
+ if (NULL == p->conf.gw.exts) return HANDLER_GO_ON;
- fn = con->uri.path;
- if (buffer_string_is_empty(fn)) return HANDLER_ERROR;
- s_len = buffer_string_length(fn);
-
- /* check if extension matches */
- for (k = 0; k < p->conf.extensions->used; k++) {
- data_array *ext = NULL;
- size_t ct_len;
-
- ext = (data_array *)p->conf.extensions->data[k];
-
- if (buffer_is_empty(ext->key)) continue;
-
- ct_len = buffer_string_length(ext->key);
-
- if (s_len < ct_len) continue;
-
- /* check extension in the form "/proxy_pattern" */
- if (*(ext->key->ptr) == '/') {
- if (strncmp(fn->ptr, ext->key->ptr, ct_len) == 0) {
- extension = ext;
- break;
- }
- } else if (0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len)) {
- /* check extension in the form ".fcg" */
- extension = ext;
- break;
- }
- }
-
- if (NULL == extension) {
- return HANDLER_GO_ON;
- }
-
- host = mod_proxy_extension_host_get(srv, con, extension, p->conf.balance, (int)p->conf.debug);
- if (NULL == host) {
- return HANDLER_FINISHED;
- }
-
- /* found a server */
- {
+ rc = gw_check_extension(srv, con, (gw_plugin_data *)p, 1, sizeof(handler_ctx));
+ if (HANDLER_GO_ON != rc) return rc;
- /*
- * if check-local is disabled, use the uri.path handler
- *
- */
-
- /* init handler-context */
- handler_ctx *hctx;
- hctx = handler_ctx_init();
-
- hctx->remote_conn = con;
- hctx->plugin_data = p;
- hctx->host = host;
- hctx->ext = extension;
-
- hctx->conf.balance = p->conf.balance;
- hctx->conf.debug = p->conf.debug;
- hctx->conf.replace_http_host = p->conf.replace_http_host;
- hctx->conf.forwarded = p->conf.forwarded;
-
- hctx->opts.fdfmt = S_IFSOCK;
- hctx->opts.backend = BACKEND_PROXY;
- hctx->opts.authorizer = 0;
- hctx->opts.local_redir = 0;
- hctx->opts.xsendfile_allow = 0;
- hctx->opts.xsendfile_docroot = NULL;
+ if (con->mode == p->id) {
+ handler_ctx *hctx = con->plugin_ctx[p->id];
+ hctx->gw.create_env = proxy_create_env;
+ hctx->gw.response = buffer_init();
+ hctx->gw.opts.backend = BACKEND_PROXY;
+ hctx->gw.opts.pdata = hctx;
+ hctx->gw.opts.headers = proxy_response_headers;
hctx->remap_hdrs = p->conf.header; /*(copies struct)*/
hctx->remap_hdrs.http_host = con->request.http_host;
@@ -1865,64 +993,6 @@ static handler_t mod_proxy_check_extension(server *srv, connection *con, void *p
hctx->remap_hdrs.https_remap =
buffer_is_equal_string(con->uri.scheme, CONST_STR_LEN("https"));
}
-
- con->plugin_ctx[p->id] = hctx;
- con->mode = p->id;
-
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sbd",
- "proxy - found a host",
- host->host, host->port);
- }
-
- return HANDLER_GO_ON;
- }
-}
-
-static handler_t mod_proxy_connection_reset(server *srv, connection *con, void *p_d) {
- plugin_data *p = p_d;
- handler_ctx *hctx = con->plugin_ctx[p->id];
- if (hctx) proxy_connection_close(srv, hctx);
-
- return HANDLER_GO_ON;
-}
-
-/**
- *
- * the trigger re-enables the disabled connections after the timeout is over
- *
- * */
-
-TRIGGER_FUNC(mod_proxy_trigger) {
- plugin_data *p = p_d;
-
- if (p->config_storage) {
- size_t i, n, k;
- for (i = 0; i < srv->config_context->used; i++) {
- plugin_config *s = p->config_storage[i];
-
- if (!s) continue;
-
- /* get the extensions for all configs */
-
- for (k = 0; k < s->extensions->used; k++) {
- data_array *extension = (data_array *)s->extensions->data[k];
-
- /* get all hosts */
- for (n = 0; n < extension->value->used; n++) {
- data_proxy *host = (data_proxy *)extension->value->data[n];
-
- if (!host->is_disabled ||
- srv->cur_ts - host->disable_ts < 5) continue;
-
- log_error_write(srv, __FILE__, __LINE__, "sbd",
- "proxy - re-enabled:",
- host->host, host->port);
-
- host->is_disabled = 0;
- }
- }
- }
}
return HANDLER_GO_ON;
@@ -1937,10 +1007,10 @@ int mod_proxy_plugin_init(plugin *p) {
p->init = mod_proxy_init;
p->cleanup = mod_proxy_free;
p->set_defaults = mod_proxy_set_defaults;
- p->connection_reset = mod_proxy_connection_reset; /* end of req-resp cycle */
+ p->connection_reset = gw_connection_reset;
p->handle_uri_clean = mod_proxy_check_extension;
- p->handle_subrequest = mod_proxy_handle_subrequest;
- p->handle_trigger = mod_proxy_trigger;
+ p->handle_subrequest = gw_handle_subrequest;
+ p->handle_trigger = gw_handle_trigger;
p->data = NULL;
diff --git a/src/mod_scgi.c b/src/mod_scgi.c
index b73625a7..14d86a48 100644
--- a/src/mod_scgi.c
+++ b/src/mod_scgi.c
@@ -1,947 +1,37 @@
#include "first.h"
-#include "buffer.h"
-#include "server.h"
-#include "keyvalue.h"
-#include "log.h"
-
-#include "http_chunk.h"
-#include "fdevent.h"
-#include "connections.h"
-#include "response.h"
-#include "joblist.h"
-#include "inet_ntop_cache.h"
-
-#include "plugin.h"
-
#include <sys/types.h>
-#include <unistd.h>
#include <errno.h>
-#include <fcntl.h>
#include <limits.h>
#include <string.h>
-#include <stdlib.h>
-#include <signal.h>
-#include "sys-socket.h"
-#include "sys-endian.h"
+#include "gw_backend.h"
+typedef gw_plugin_config plugin_config;
+typedef gw_plugin_data plugin_data;
+typedef gw_handler_ctx handler_ctx;
-#ifdef HAVE_SYS_UIO_H
-# include <sys/uio.h>
-#endif
-#ifdef HAVE_SYS_WAIT_H
-# include <sys/wait.h>
-#endif
+#include "base.h"
+#include "buffer.h"
+#include "log.h"
+#include "plugin.h"
+#include "status_counter.h"
-/*
- *
- * TODO:
- *
- * - add timeout for a connect to a non-scgi process
- * (use state_timestamp + state)
- *
- */
-
-typedef struct {
- char **ptr;
-
- size_t size;
- size_t used;
-} char_array;
-
-typedef struct scgi_proc {
- size_t id; /* id will be between 1 and max_procs */
- buffer *socket; /* config.socket + "-" + id */
- unsigned port; /* config.port + pno */
-
- buffer *connection_name; /* either tcp:<host>:<port> or unix:<socket> for debugging purposes */
-
- pid_t pid; /* PID of the spawned process (0 if not spawned locally) */
-
-
- size_t load; /* number of requests waiting on this process */
-
- time_t last_used; /* see idle_timeout */
- size_t requests; /* see max_requests */
- struct scgi_proc *prev, *next; /* see first */
-
- time_t disabled_until; /* this proc is disabled until, use something else until then */
-
- int is_local;
-
- enum {
- PROC_STATE_RUNNING, /* alive */
- PROC_STATE_DISABLED,/* proc disabled as it resulted in an error */
- PROC_STATE_DIED_WAIT_FOR_PID,
- PROC_STATE_DIED, /* marked as dead, should be restarted */
- PROC_STATE_KILLED /* was killed as we don't have the load anymore */
- } state;
-} scgi_proc;
-
-typedef struct {
- /* the key that is used to reference this value */
- buffer *id;
-
- /* list of processes handling this extension
- * sorted by lowest load
- *
- * whenever a job is done move it up in the list
- * until it is sorted, move it down as soon as the
- * job is started
- */
- scgi_proc *first;
- scgi_proc *unused_procs;
-
- /*
- * spawn at least min_procs, at max_procs.
- *
- * as soon as the load of the first entry
- * is max_load_per_proc we spawn a new one
- * and add it to the first entry and give it
- * the load
- *
- */
-
- unsigned short min_procs;
- unsigned short max_procs;
- size_t num_procs; /* how many procs are started */
- size_t active_procs; /* how many of them are really running */
-
- unsigned short max_load_per_proc;
-
- /*
- * kick the process from the list if it was not
- * used for idle_timeout until min_procs is
- * reached. this helps to get the processlist
- * small again we had a small peak load.
- *
- */
-
- unsigned short idle_timeout;
-
- /*
- * time after a disabled remote connection is tried to be re-enabled
- *
- *
- */
-
- unsigned short disable_time;
-
- /*
- * same scgi processes get a little bit larger
- * than wanted. max_requests_per_proc kills a
- * process after a number of handled requests.
- *
- */
- size_t max_requests_per_proc;
-
-
- /* config */
-
- /*
- * host:port
- *
- * if host is one of the local IP adresses the
- * whole connection is local
- *
- * if tcp/ip should be used host AND port have
- * to be specified
- *
- */
- buffer *host;
- unsigned short port;
- sa_family_t family;
-
- /*
- * Unix Domain Socket
- *
- * instead of TCP/IP we can use Unix Domain Sockets
- * - more secure (you have fileperms to play with)
- * - more control (on locally)
- * - more speed (no extra overhead)
- */
- buffer *unixsocket;
-
- /* if socket is local we can start the scgi
- * process ourself
- *
- * bin-path is the path to the binary
- *
- * check min_procs and max_procs for the number
- * of process to start-up
- */
- buffer *bin_path;
-
- /* bin-path is set bin-environment is taken to
- * create the environement before starting the
- * FastCGI process
- *
- */
- array *bin_env;
-
- array *bin_env_copy;
-
- /*
- * docroot-translation between URL->phys and the
- * remote host
- *
- * reasons:
- * - different dir-layout if remote
- * - chroot if local
- *
- */
- buffer *docroot;
-
- /*
- * check_local tell you if the phys file is stat()ed
- * or not. FastCGI doesn't care if the service is
- * remote. If the web-server side doesn't contain
- * the scgi-files we should not stat() for them
- * and say '404 not found'.
- */
- unsigned short check_local;
-
- /*
- * append PATH_INFO to SCRIPT_FILENAME
- *
- * php needs this if cgi.fix_pathinfo is provied
- *
- */
-
- unsigned short break_scriptfilename_for_php;
-
- /*
- * workaround for program when prefix="/"
- *
- * rule to build PATH_INFO is hardcoded for when check_local is disabled
- * enable this option to use the workaround
- *
- */
-
- unsigned short fix_root_path_name;
-
- /*
- * If the backend includes X-Sendfile in the response
- * we use the value as filename and ignore the content.
- *
- */
- unsigned short xsendfile_allow;
- array *xsendfile_docroot;
-
- ssize_t load;
-
- size_t max_id; /* corresponds most of the time to num_procs */
-
- buffer *strip_request_uri;
-
- unsigned short kill_signal; /* we need a setting for this as libfcgi
- applications prefer SIGUSR1 while the
- rest of the world would use SIGTERM
- *sigh* */
-
- int listen_backlog;
- int refcount;
-
- char_array args;
-} scgi_extension_host;
-
-/*
- * one extension can have multiple hosts assigned
- * one host can spawn additional processes on the same
- * socket (if we control it)
- *
- * ext -> host -> procs
- * 1:n 1:n
- *
- * if the scgi process is remote that whole goes down
- * to
- *
- * ext -> host -> procs
- * 1:n 1:1
- *
- * in case of PHP and FCGI_CHILDREN we have again a procs
- * but we don't control it directly.
- *
- */
-
-typedef struct {
- buffer *key; /* like .php */
-
- int note_is_sent;
- int last_used_ndx;
-
- scgi_extension_host **hosts;
-
- size_t used;
- size_t size;
-} scgi_extension;
-
-typedef struct {
- scgi_extension **exts;
-
- size_t used;
- size_t size;
-} scgi_exts;
+#include "sys-endian.h"
enum { LI_PROTOCOL_SCGI, LI_PROTOCOL_UWSGI };
-typedef struct {
- scgi_exts *exts;
-
- int proto;
- int debug;
-} plugin_config;
-
-/* generic plugin data, shared between all connections */
-typedef struct {
- PLUGIN_DATA;
-
- plugin_config **config_storage;
-
- plugin_config conf; /* this is only used as long as no handler_ctx is setup */
-} plugin_data;
-
-/* connection specific data */
-typedef enum {
- FCGI_STATE_INIT,
- FCGI_STATE_CONNECT,
- FCGI_STATE_PREPARE_WRITE,
- FCGI_STATE_WRITE,
- FCGI_STATE_READ
-} scgi_connection_state_t;
-
-typedef struct {
- scgi_proc *proc;
- scgi_extension_host *host;
- scgi_extension *ext;
- scgi_extension *ext_auth;
- unsigned short scgi_mode;
-
- scgi_connection_state_t state;
- time_t state_timestamp;
-
- chunkqueue *rb;
- chunkqueue *wb;
- off_t wb_reqlen;
-
- buffer *response;
-
- int fd; /* fd to the scgi process */
- int fde_ndx; /* index into the fd-event buffer */
-
- pid_t pid;
- int got_proc;
- int reconnects; /* number of reconnect attempts */
-
- int request_id;
- int send_content_body;
-
- http_response_opts opts;
- plugin_config conf;
-
- connection *remote_conn; /* dumb pointer */
- plugin_data *plugin_data; /* dumb pointer */
-} handler_ctx;
-
-
-/* ok, we need a prototype */
-static handler_t scgi_handle_fdevent(server *srv, void *ctx, int revents);
-
-int scgi_proclist_sort_down(server *srv, scgi_extension_host *host, scgi_proc *proc);
-
-static handler_ctx * handler_ctx_init(void) {
- handler_ctx * hctx;
-
- hctx = calloc(1, sizeof(*hctx));
- force_assert(hctx);
-
- hctx->fde_ndx = -1;
-
- hctx->response = buffer_init();
-
- hctx->state = FCGI_STATE_INIT;
- hctx->proc = NULL;
-
- hctx->fd = -1;
-
- hctx->reconnects = 0;
-
- hctx->wb = chunkqueue_init();
- hctx->wb_reqlen = 0;
-
- return hctx;
-}
-
-static void handler_ctx_free(handler_ctx *hctx) {
- buffer_free(hctx->response);
- chunkqueue_free(hctx->wb);
- free(hctx);
-}
-
-static scgi_proc *scgi_process_init(void) {
- scgi_proc *f;
-
- f = calloc(1, sizeof(*f));
- force_assert(f);
- f->socket = buffer_init();
-
- f->prev = NULL;
- f->next = NULL;
- f->state = PROC_STATE_DIED;
-
- return f;
-}
-
-static void scgi_process_free(scgi_proc *f) {
- if (!f) return;
-
- scgi_process_free(f->next);
-
- buffer_free(f->socket);
-
- free(f);
-}
-
-static scgi_extension_host *scgi_host_init(void) {
- scgi_extension_host *f;
-
- f = calloc(1, sizeof(*f));
-
- f->host = buffer_init();
- f->unixsocket = buffer_init();
- f->docroot = buffer_init();
- f->bin_path = buffer_init();
- f->bin_env = array_init();
- f->bin_env_copy = array_init();
- f->xsendfile_docroot = array_init();
-
- return f;
-}
-
-static void scgi_host_free(scgi_extension_host *h) {
- if (!h) return;
- if (h->refcount) {
- --h->refcount;
- return;
- }
-
- buffer_free(h->host);
- buffer_free(h->unixsocket);
- buffer_free(h->docroot);
- buffer_free(h->bin_path);
- array_free(h->bin_env);
- array_free(h->bin_env_copy);
- array_free(h->xsendfile_docroot);
-
- scgi_process_free(h->first);
- scgi_process_free(h->unused_procs);
-
- for (size_t i = 0; i < h->args.used; ++i) free(h->args.ptr[i]);
- free(h->args.ptr);
- free(h);
-}
-
-static scgi_exts *scgi_extensions_init(void) {
- scgi_exts *f;
-
- f = calloc(1, sizeof(*f));
- force_assert(f);
-
- return f;
-}
-
-static void scgi_extensions_free(scgi_exts *f) {
- size_t i;
-
- if (!f) return;
-
- for (i = 0; i < f->used; i++) {
- scgi_extension *fe;
- size_t j;
-
- fe = f->exts[i];
-
- for (j = 0; j < fe->used; j++) {
- scgi_extension_host *h;
-
- h = fe->hosts[j];
-
- scgi_host_free(h);
- }
-
- buffer_free(fe->key);
- free(fe->hosts);
-
- free(fe);
- }
-
- free(f->exts);
-
- free(f);
-}
-
-static int scgi_extension_insert(scgi_exts *ext, buffer *key, scgi_extension_host *fh) {
- scgi_extension *fe;
- size_t i;
-
- /* there is something */
-
- for (i = 0; i < ext->used; i++) {
- if (buffer_is_equal(key, ext->exts[i]->key)) {
- break;
- }
- }
-
- if (i == ext->used) {
- /* filextension is new */
- fe = calloc(1, sizeof(*fe));
- force_assert(fe);
- fe->key = buffer_init();
- buffer_copy_buffer(fe->key, key);
-
- /* */
-
- if (ext->size == 0) {
- ext->size = 8;
- ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
- force_assert(ext->exts);
- } else if (ext->used == ext->size) {
- ext->size += 8;
- ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
- force_assert(ext->exts);
- }
- ext->exts[ext->used++] = fe;
- } else {
- fe = ext->exts[i];
- }
-
- if (fe->size == 0) {
- fe->size = 4;
- fe->hosts = malloc(fe->size * sizeof(*(fe->hosts)));
- force_assert(fe->hosts);
- } else if (fe->size == fe->used) {
- fe->size += 4;
- fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts)));
- force_assert(fe->hosts);
- }
-
- fe->hosts[fe->used++] = fh;
-
- return 0;
-
-}
-
-static void scgi_proc_set_state(scgi_extension_host *host, scgi_proc *proc, int state) {
- if ((int)proc->state == state) return;
- if (proc->state == PROC_STATE_RUNNING) {
- --host->active_procs;
- } else if (state == PROC_STATE_RUNNING) {
- ++host->active_procs;
- }
- proc->state = state;
-}
-
-static void scgi_proc_connect_error(server *srv, scgi_extension_host *host, scgi_proc *proc, handler_ctx *hctx, int errnum) {
- log_error_write(srv, __FILE__, __LINE__, "sdsdbdb",
- "establishing connection failed:",
- hctx->fd, strerror(errnum), errnum,
- host->host, proc->port, proc->socket);
-
- if (!proc->is_local) {
- proc->disabled_until = srv->cur_ts + host->disable_time;
- scgi_proc_set_state(host, proc, PROC_STATE_DISABLED);
- }
- else if (proc->pid == hctx->pid && proc->state == PROC_STATE_RUNNING) {
- /*
- * several hctx might reference the same proc
- *
- * Only one of them should mark the proc
- * and all other ones should just take a new one.
- *
- * If a new proc was started with the old struct this might lead
- * the mark a perfect proc as dead otherwise
- *
- */
- log_error_write(srv, __FILE__, __LINE__, "sdssdsd",
- "backend error; we'll disable it for", host->disable_time,
- "seconds and send the request to another backend instead:",
- "reconnects:", hctx->reconnects,
- "load:", host->load);
- if (EAGAIN == errnum) {
- /* - EAGAIN: cool down the backend; it is overloaded */
- #ifdef __linux__
- log_error_write(srv, __FILE__, __LINE__, "s",
- "If this happened on Linux: You have been run out of local ports. "
- "Check the manual, section Performance how to handle this.");
- #endif
- proc->disabled_until = srv->cur_ts + host->disable_time;
- scgi_proc_set_state(host, proc, PROC_STATE_DISABLED);
- }
- else {
- /* we got a hard error from the backend like
- * - ECONNREFUSED for tcp-ip sockets
- * - ENOENT for unix-domain-sockets
- */
- scgi_proc_set_state(host, proc, PROC_STATE_DIED_WAIT_FOR_PID);
- }
- }
-}
-
-static void scgi_proc_check_enable(server *srv, scgi_extension_host *host, scgi_proc *proc) {
- if (srv->cur_ts <= proc->disabled_until) return;
- if (proc->state != PROC_STATE_DISABLED) return;
-
- scgi_proc_set_state(host, proc, PROC_STATE_RUNNING);
-
- log_error_write(srv, __FILE__, __LINE__, "sbbdb",
- "fcgi-server re-enabled:", proc->connection_name,
- host->host, host->port, host->unixsocket);
-}
-
-static int scgi_proc_waitpid(server *srv, scgi_extension_host *host, scgi_proc *proc) {
- int rc, status;
-
- if (!proc->is_local) return 0;
- if (proc->pid <= 0) return 0;
-
- do {
- rc = waitpid(proc->pid, &status, WNOHANG);
- } while (-1 == rc && errno == EINTR);
- if (0 == rc) return 0; /* child still running */
-
- /* child terminated */
- if (-1 == rc) {
- /* EINVAL or ECHILD no child processes */
- /* should not happen; someone else has cleaned up for us */
- log_error_write(srv, __FILE__, __LINE__, "sddss",
- "pid ", proc->pid, proc->state,
- "not found:", strerror(errno));
- } else if (WIFEXITED(status)) {
- if (proc->state != PROC_STATE_KILLED) {
- log_error_write(srv, __FILE__, __LINE__, "sdb",
- "child exited:",
- WEXITSTATUS(status), host->bin_path);
- }
- } else if (WIFSIGNALED(status)) {
- if (WTERMSIG(status) != SIGTERM && WTERMSIG(status) != SIGINT) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "child signalled:", WTERMSIG(status));
- }
- } else {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "child died somehow:", status);
- }
-
- proc->pid = 0;
- scgi_proc_set_state(host, proc, PROC_STATE_DIED);
- return 1;
-}
-
-INIT_FUNC(mod_scgi_init) {
- plugin_data *p;
-
- p = calloc(1, sizeof(*p));
- force_assert(p);
-
- return p;
-}
-
-
-FREE_FUNC(mod_scgi_free) {
- plugin_data *p = p_d;
-
- UNUSED(srv);
-
- if (p->config_storage) {
- size_t i, j, n;
- for (i = 0; i < srv->config_context->used; i++) {
- plugin_config *s = p->config_storage[i];
- scgi_exts *exts;
-
- if (NULL == s) continue;
-
- exts = s->exts;
-
- for (j = 0; j < exts->used; j++) {
- scgi_extension *ex;
-
- ex = exts->exts[j];
-
- for (n = 0; n < ex->used; n++) {
- scgi_proc *proc;
- scgi_extension_host *host;
-
- host = ex->hosts[n];
-
- for (proc = host->first; proc; proc = proc->next) {
- if (proc->pid > 0) kill(proc->pid, SIGTERM);
-
- if (proc->is_local &&
- !buffer_string_is_empty(proc->socket)) {
- unlink(proc->socket->ptr);
- }
- }
-
- for (proc = host->unused_procs; proc; proc = proc->next) {
- if (proc->pid > 0) kill(proc->pid, SIGTERM);
-
- if (proc->is_local &&
- !buffer_string_is_empty(proc->socket)) {
- unlink(proc->socket->ptr);
- }
- }
- }
- }
-
- scgi_extensions_free(s->exts);
-
- free(s);
- }
- free(p->config_storage);
- }
-
- free(p);
-
- return HANDLER_GO_ON;
-}
-
-static int env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
- char *dst;
- size_t i;
-
- if (!key || !val) return -1;
-
- dst = malloc(key_len + val_len + 3);
- force_assert(dst);
- memcpy(dst, key, key_len);
- dst[key_len] = '=';
- /* add the \0 from the value */
- memcpy(dst + key_len + 1, val, val_len + 1);
-
- for (i = 0; i < env->used; i++) {
- if (0 == strncmp(dst, env->ptr[i], key_len + 1)) {
- free(env->ptr[i]);
- env->ptr[i] = dst;
- return 0;
- }
- }
-
- if (env->size == 0) {
- env->size = 16;
- env->ptr = malloc(env->size * sizeof(*env->ptr));
- force_assert(env->ptr);
- } else if (env->size == env->used) {
- env->size += 16;
- env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
- force_assert(env->ptr);
- }
-
- env->ptr[env->used++] = dst;
-
- return 0;
-}
-
-static int scgi_spawn_connection(server *srv,
- plugin_data *p,
- scgi_extension_host *host,
- scgi_proc *proc) {
- int scgi_fd;
- int status;
- struct timeval tv = { 0, 10 * 1000 };
- sock_addr addr;
- struct sockaddr *scgi_addr = (struct sockaddr *)&addr;
- socklen_t servlen;
-
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sdb",
- "new proc, socket:", proc->port, proc->socket);
- }
-
- if (!buffer_string_is_empty(proc->socket)) {
- if (1 != sock_addr_from_str_hints(srv, &addr, &servlen, proc->socket->ptr, AF_UNIX, 0)) {
- return -1;
- }
- } else {
- if (1 != sock_addr_from_buffer_hints_numeric(srv, &addr, &servlen, host->host, host->family, proc->port)) {
- return -1;
- }
- }
-
- if (-1 == (scgi_fd = fdevent_socket_cloexec(scgi_addr->sa_family, SOCK_STREAM, 0))) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "failed:", strerror(errno));
- return -1;
- }
-
- do {
- status = connect(scgi_fd, scgi_addr, servlen);
- } while (-1 == status && errno == EINTR);
-
- if (-1 == status && errno != ENOENT
- && !buffer_string_is_empty(proc->socket)) {
- log_error_write(srv, __FILE__, __LINE__, "sbss",
- "unlink", proc->socket,
- "after connect failed:", strerror(errno));
- unlink(proc->socket->ptr);
- }
-
- close(scgi_fd);
-
- if (-1 == status) {
- /* server is not up, spawn in */
- char_array env;
- size_t i;
- int val;
-
- /* reopen socket */
- if (-1 == (scgi_fd = fdevent_socket_cloexec(scgi_addr->sa_family, SOCK_STREAM, 0))) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "socket failed:", strerror(errno));
- return -1;
- }
-
- val = 1;
- if (setsockopt(scgi_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "socketsockopt failed:", strerror(errno));
- close(scgi_fd);
- return -1;
- }
-
- /* create socket */
- if (-1 == bind(scgi_fd, scgi_addr, servlen)) {
- log_error_write(srv, __FILE__, __LINE__, "sbds",
- "bind failed for:",
- proc->socket,
- proc->port,
- strerror(errno));
- close(scgi_fd);
- return -1;
- }
-
- if (-1 == listen(scgi_fd, host->listen_backlog)) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "listen failed:", strerror(errno));
- close(scgi_fd);
- return -1;
- }
-
- {
- /* create environment */
- env.ptr = NULL;
- env.size = 0;
- env.used = 0;
-
- /* build clean environment */
- if (host->bin_env_copy->used) {
- for (i = 0; i < host->bin_env_copy->used; i++) {
- data_string *ds = (data_string *)host->bin_env_copy->data[i];
- char *ge;
-
- if (NULL != (ge = getenv(ds->value->ptr))) {
- env_add(&env, CONST_BUF_LEN(ds->value), ge, strlen(ge));
- }
- }
- } else {
- char ** const e = environ;
- for (i = 0; e[i]; ++i) {
- char *eq;
-
- if (NULL != (eq = strchr(e[i], '='))) {
- env_add(&env, e[i], eq - e[i], eq+1, strlen(eq+1));
- }
- }
- }
-
- /* create environment */
- for (i = 0; i < host->bin_env->used; i++) {
- data_string *ds = (data_string *)host->bin_env->data[i];
-
- env_add(&env, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
- }
-
- for (i = 0; i < env.used; i++) {
- /* search for PHP_FCGI_CHILDREN */
- if (0 == strncmp(env.ptr[i], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
- }
-
- /* not found, add a default */
- if (i == env.used) {
- env_add(&env, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
- }
-
- env.ptr[env.used] = NULL;
- }
-
- proc->pid = fdevent_fork_execve(host->args.ptr[0], host->args.ptr, env.ptr, scgi_fd, -1, -1, -1);
-
- for (i = 0; i < env.used; ++i) free(env.ptr[i]);
- free(env.ptr);
- close(scgi_fd);
-
- if (-1 == proc->pid) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "scgi-backend failed to start:", host->bin_path);
- return -1;
- }
-
- /* register process */
- proc->last_used = srv->cur_ts;
- proc->is_local = 1;
-
- /* wait */
- select(0, NULL, NULL, NULL, &tv);
-
- if (0 != scgi_proc_waitpid(srv, host, proc)) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "scgi-backend failed to start:", host->bin_path);
- return -1;
- }
- } else {
- proc->is_local = 0;
- proc->pid = 0;
-
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "(debug) socket is already used, won't spawn:",
- proc->socket);
- }
- }
-
- scgi_proc_set_state(host, proc, PROC_STATE_RUNNING);
- return 0;
-}
-
-static scgi_extension_host * unixsocket_is_dup(plugin_data *p, size_t used, buffer *unixsocket) {
- size_t i, j, n;
- for (i = 0; i < used; ++i) {
- scgi_exts *exts = p->config_storage[i]->exts;
- for (j = 0; j < exts->used; ++j) {
- scgi_extension *ex = exts->exts[j];
- for (n = 0; n < ex->used; ++n) {
- scgi_extension_host *host = ex->hosts[n];
- if (!buffer_string_is_empty(host->unixsocket)
- && buffer_is_equal(host->unixsocket, unixsocket)
- && !buffer_string_is_empty(host->bin_path))
- return host;
- }
- }
- }
-
- return NULL;
-}
-
SETDEFAULTS_FUNC(mod_scgi_set_defaults) {
plugin_data *p = p_d;
data_unset *du;
size_t i = 0;
- scgi_extension_host *df = NULL;
config_values_t cv[] = {
{ "scgi.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
{ "scgi.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
{ "scgi.protocol", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
- { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
+ { "scgi.map-extensions", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
+ { "scgi.balance", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
+ { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
};
p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
@@ -953,23 +43,34 @@ SETDEFAULTS_FUNC(mod_scgi_set_defaults) {
s = calloc(1, sizeof(plugin_config));
force_assert(s);
- s->exts = scgi_extensions_init();
+ s->exts = NULL;
+ s->exts_auth = NULL;
+ s->exts_resp = NULL;
s->debug = 0;
s->proto = LI_PROTOCOL_SCGI;
+ s->ext_mapping = array_init();
- cv[0].destination = s->exts;
+ cv[0].destination = s->exts; /* not used; T_CONFIG_LOCAL */
cv[1].destination = &(s->debug);
- cv[2].destination = NULL; /* T_CONFIG_LOCAL */
+ cv[2].destination = NULL; /* not used; T_CONFIG_LOCAL */
+ cv[3].destination = s->ext_mapping;
+ cv[4].destination = NULL; /* not used; T_CONFIG_LOCAL */
p->config_storage[i] = s;
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
- goto error;
+ return HANDLER_ERROR;
+ }
+
+ du = array_get_element(config->value, "scgi.server");
+ if (!gw_set_defaults_backend(srv, p, du, i, 1)) {
+ return HANDLER_ERROR;
}
- /*
- * <key> = ( ... )
- */
+ du = array_get_element(config->value, "scgi.balance");
+ if (!gw_set_defaults_balance(srv, s, du)) {
+ return HANDLER_ERROR;
+ }
if (NULL != (du = array_get_element(config->value, "scgi.protocol"))) {
data_string *ds = (data_string *)du;
@@ -983,435 +84,14 @@ SETDEFAULTS_FUNC(mod_scgi_set_defaults) {
log_error_write(srv, __FILE__, __LINE__, "sss",
"unexpected type for key: ", "scgi.protocol", "expected \"scgi\" or \"uwsgi\"");
- goto error;
- }
- }
-
- if (NULL != (du = array_get_element(config->value, "scgi.server"))) {
- size_t j;
- data_array *da = (data_array *)du;
-
- if (du->type != TYPE_ARRAY || !array_is_kvarray(da->value)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "unexpected value for scgi.server; expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
-
- goto error;
- }
-
-
- /*
- * scgi.server = ( "<ext>" => ( ... ),
- * "<ext>" => ( ... ) )
- */
-
- for (j = 0; j < da->value->used; j++) {
- size_t n;
- data_array *da_ext = (data_array *)da->value->data[j];
-
- /*
- * da_ext->key == name of the extension
- */
-
- /*
- * scgi.server = ( "<ext>" =>
- * ( "<host>" => ( ... ),
- * "<host>" => ( ... )
- * ),
- * "<ext>" => ... )
- */
-
- for (n = 0; n < da_ext->value->used; n++) {
- data_array *da_host = (data_array *)da_ext->value->data[n];
-
- config_values_t fcv[] = {
- { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
- { "docroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
- { "socket", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
- { "bin-path", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
-
- { "check-local", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
- { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
- { "min-procs-not-working", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 this is broken for now */
- { "max-procs", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
- { "max-load-per-proc", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
- { "idle-timeout", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
- { "disable-time", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
-
- { "bin-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
- { "bin-copy-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
- { "fix-root-scriptname", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
- { "listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
- { "x-sendfile", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
- { "x-sendfile-docroot",NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
-
-
- { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
- };
-
- if (da_host->type != TYPE_ARRAY || !array_is_kvany(da_host->value)) {
- log_error_write(srv, __FILE__, __LINE__, "SBS",
- "unexpected value for scgi.server near [",
- da_host->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
-
- goto error;
- }
-
- df = scgi_host_init();
-
- df->check_local = 1;
- df->min_procs = 4;
- df->max_procs = 4;
- df->max_load_per_proc = 1;
- df->idle_timeout = 60;
- df->disable_time = 1;
- df->fix_root_path_name = 0;
- df->listen_backlog = 1024;
- df->xsendfile_allow = 0;
- df->refcount = 0;
-
- fcv[0].destination = df->host;
- fcv[1].destination = df->docroot;
- fcv[2].destination = df->unixsocket;
- fcv[3].destination = df->bin_path;
-
- fcv[4].destination = &(df->check_local);
- fcv[5].destination = &(df->port);
- fcv[6].destination = &(df->min_procs);
- fcv[7].destination = &(df->max_procs);
- fcv[8].destination = &(df->max_load_per_proc);
- fcv[9].destination = &(df->idle_timeout);
- fcv[10].destination = &(df->disable_time);
-
- fcv[11].destination = df->bin_env;
- fcv[12].destination = df->bin_env_copy;
- fcv[13].destination = &(df->fix_root_path_name);
- fcv[14].destination = &(df->listen_backlog);
- fcv[15].destination = &(df->xsendfile_allow);
- fcv[16].destination = df->xsendfile_docroot;
-
-
- if (0 != config_insert_values_internal(srv, da_host->value, fcv, T_CONFIG_SCOPE_CONNECTION)) {
- goto error;
- }
-
- if ((!buffer_string_is_empty(df->host) || df->port) &&
- !buffer_string_is_empty(df->unixsocket)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "either host+port or socket");
-
- goto error;
- }
-
- if (!buffer_string_is_empty(df->unixsocket)) {
- /* unix domain socket */
- struct sockaddr_un un;
-
- if (buffer_string_length(df->unixsocket) + 1 > sizeof(un.sun_path) - 2) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "path of the unixdomain socket is too large");
- goto error;
- }
-
- if (!buffer_string_is_empty(df->bin_path)) {
- scgi_extension_host *duplicate = unixsocket_is_dup(p, i+1, df->unixsocket);
- if (NULL != duplicate) {
- if (!buffer_is_equal(df->bin_path, duplicate->bin_path)) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "duplicate unixsocket path:",
- df->unixsocket);
- goto error;
- }
- scgi_host_free(df);
- df = duplicate;
- ++df->refcount;
- }
- }
-
- df->family = AF_UNIX;
- } else {
- /* tcp/ip */
-
- if (buffer_string_is_empty(df->host) &&
- buffer_string_is_empty(df->bin_path)) {
- log_error_write(srv, __FILE__, __LINE__, "sbbbs",
- "missing key (string):",
- da->key,
- da_ext->key,
- da_host->key,
- "host");
-
- goto error;
- } else if (df->port == 0) {
- log_error_write(srv, __FILE__, __LINE__, "sbbbs",
- "missing key (short):",
- da->key,
- da_ext->key,
- da_host->key,
- "port");
- goto error;
- }
-
- df->family = (!buffer_string_is_empty(df->host) && NULL != strchr(df->host->ptr, ':')) ? AF_INET6 : AF_INET;
- }
-
- if (df->refcount) {
- /* already init'd; skip spawning */
- } else if (!buffer_string_is_empty(df->bin_path)) {
- /* a local socket + self spawning */
- size_t pno;
-
- struct stat st;
- size_t nchars = strcspn(df->bin_path->ptr, " \t");
- char c = df->bin_path->ptr[nchars];
- df->bin_path->ptr[nchars] = '\0';
- if (0 == nchars || 0 != stat(df->bin_path->ptr, &st) || !S_ISREG(st.st_mode) || !(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
- df->bin_path->ptr[nchars] = c;
- log_error_write(srv, __FILE__, __LINE__, "SSs",
- "invalid \"bin-path\" => \"", df->bin_path->ptr,
- "\" (check that file exists, is regular file, and is executable by lighttpd)");
- }
- df->bin_path->ptr[nchars] = c;
-
- /*(preserve prior behavior for exec of command)*/
- /*(admin should really prefer to put any complex command into script)*/
- df->args.ptr = calloc(4, sizeof(char *));
- force_assert(df->args.ptr);
- df->args.used = 3;
- df->args.size = 4;
- df->args.ptr[0] = malloc(sizeof("/bin/sh"));
- force_assert(df->args.ptr[0]);
- memcpy(df->args.ptr[0], "/bin/sh", sizeof("/bin/sh"));
- df->args.ptr[1] = malloc(sizeof("-c"));
- force_assert(df->args.ptr[1]);
- memcpy(df->args.ptr[1], "-c", sizeof("-c"));
- df->args.ptr[2] = malloc(sizeof("exec ")-1+buffer_string_length(df->bin_path)+1);
- force_assert(df->args.ptr[2]);
- memcpy(df->args.ptr[2], "exec ", sizeof("exec ")-1);
- memcpy(df->args.ptr[2]+sizeof("exec ")-1, df->bin_path->ptr, buffer_string_length(df->bin_path)+1);
- df->args.ptr[3] = NULL;
-
- /* HACK: just to make sure the adaptive spawing is disabled */
- df->min_procs = df->max_procs;
-
- if (df->min_procs > df->max_procs) df->max_procs = df->min_procs;
- if (df->max_load_per_proc < 1) df->max_load_per_proc = 0;
-
- if (s->debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssbsdsbsdsd",
- "--- scgi spawning local",
- "\n\tproc:", df->bin_path,
- "\n\tport:", df->port,
- "\n\tsocket", df->unixsocket,
- "\n\tmin-procs:", df->min_procs,
- "\n\tmax-procs:", df->max_procs);
- }
-
- for (pno = 0; pno < df->min_procs; pno++) {
- scgi_proc *proc;
-
- proc = scgi_process_init();
- proc->id = df->num_procs++;
- df->max_id++;
-
- if (buffer_string_is_empty(df->unixsocket)) {
- proc->port = df->port + pno;
- } else {
- buffer_copy_buffer(proc->socket, df->unixsocket);
- buffer_append_string_len(proc->socket, CONST_STR_LEN("-"));
- buffer_append_int(proc->socket, pno);
- }
-
- if (s->debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
- "--- scgi spawning",
- "\n\tport:", df->port,
- "\n\tsocket", df->unixsocket,
- "\n\tcurrent:", pno, "/", df->min_procs);
- }
-
- if (!srv->srvconf.preflight_check
- && scgi_spawn_connection(srv, p, df, proc)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "[ERROR]: spawning fcgi failed.");
- scgi_process_free(proc);
- goto error;
- }
-
- proc->next = df->first;
- if (df->first) df->first->prev = proc;
-
- df->first = proc;
- }
- } else {
- scgi_proc *fp;
-
- fp = scgi_process_init();
- fp->id = df->num_procs++;
- df->max_id++;
- scgi_proc_set_state(df, fp, PROC_STATE_RUNNING);
-
- if (buffer_string_is_empty(df->unixsocket)) {
- fp->port = df->port;
- } else {
- buffer_copy_buffer(fp->socket, df->unixsocket);
- }
-
- df->first = fp;
-
- df->min_procs = 1;
- df->max_procs = 1;
- }
-
- if (df->xsendfile_docroot->used) {
- size_t k;
- for (k = 0; k < df->xsendfile_docroot->used; ++k) {
- data_string *ds = (data_string *)df->xsendfile_docroot->data[k];
- if (ds->type != TYPE_STRING) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
- goto error;
- }
- if (ds->value->ptr[0] != '/') {
- log_error_write(srv, __FILE__, __LINE__, "SBs",
- "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds->value, "\"");
- goto error;
- }
- buffer_path_simplify(ds->value, ds->value);
- buffer_append_slash(ds->value);
- }
- }
-
- /* if extension already exists, take it */
- scgi_extension_insert(s->exts, da_ext->key, df);
- df = NULL;
- }
+ return HANDLER_ERROR;
}
}
}
return HANDLER_GO_ON;
-
-error:
- if (NULL != df) scgi_host_free(df);
- return HANDLER_ERROR;
-}
-
-static int scgi_set_state(server *srv, handler_ctx *hctx, scgi_connection_state_t state) {
- hctx->state = state;
- hctx->state_timestamp = srv->cur_ts;
-
- return 0;
-}
-
-
-static void scgi_backend_close(server *srv, handler_ctx *hctx) {
- if (hctx->fd != -1) {
- fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
- fdevent_unregister(srv->ev, hctx->fd);
- fdevent_sched_close(srv->ev, hctx->fd, 1);
- hctx->fd = -1;
- hctx->fde_ndx = -1;
- }
-
- if (hctx->host) {
- if (hctx->proc) {
- /* after the connect the process gets a load */
- if (hctx->got_proc) hctx->proc->load--;
- scgi_proclist_sort_down(srv, hctx->host, hctx->proc);
-
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sddb",
- "release proc:",
- hctx->fd,
- hctx->proc->pid, hctx->proc->socket);
- }
- }
-
- hctx->host->load--;
- hctx->host = NULL;
- }
-}
-
-static scgi_extension_host * scgi_extension_host_get(server *srv, connection *con, plugin_data *p, scgi_extension *extension) {
- int used = -1;
- scgi_extension_host *host = NULL;
- UNUSED(p);
-
- /* get best server */
- for (size_t k = 0; k < extension->used; ++k) {
- scgi_extension_host *h = extension->hosts[k];
-
- /* we should have at least one proc that can do something */
- if (h->active_procs == 0) {
- continue;
- }
-
- if (used == -1 || h->load < used) {
- used = h->load;
-
- host = h;
- }
- }
-
- if (!host) {
- /* sorry, we don't have a server alive for this ext */
- con->http_status = 503; /* Service Unavailable */
- con->mode = DIRECT;
-
- /* only send the 'no handler' once */
- if (!extension->note_is_sent) {
- extension->note_is_sent = 1;
-
- log_error_write(srv, __FILE__, __LINE__, "sbsbs",
- "all handlers for ", con->uri.path,
- "on", extension->key,
- "are down.");
- }
- }
-
- return host;
-}
-
-static void scgi_connection_close(server *srv, handler_ctx *hctx) {
- plugin_data *p;
- connection *con;
-
- p = hctx->plugin_data;
- con = hctx->remote_conn;
-
- scgi_backend_close(srv, hctx);
- handler_ctx_free(hctx);
- con->plugin_ctx[p->id] = NULL;
-
- /* finish response (if not already con->file_started, con->file_finished) */
- if (con->mode == p->id) {
- http_response_backend_done(srv, con);
- }
-}
-
-static handler_t scgi_reconnect(server *srv, handler_ctx *hctx) {
- scgi_backend_close(srv, hctx);
-
- hctx->host = scgi_extension_host_get(srv, hctx->remote_conn, hctx->plugin_data, hctx->ext);
- if (NULL == hctx->host) return HANDLER_FINISHED;
-
- hctx->host->load++;
- hctx->opts.xsendfile_allow = hctx->host->xsendfile_allow;
- hctx->opts.xsendfile_docroot = hctx->host->xsendfile_docroot;
- scgi_set_state(srv, hctx, FCGI_STATE_INIT);
- return HANDLER_COMEBACK;
}
-
-static handler_t scgi_connection_reset(server *srv, connection *con, void *p_d) {
- plugin_data *p = p_d;
- handler_ctx *hctx = con->plugin_ctx[p->id];
- if (hctx) scgi_connection_close(srv, hctx);
-
- return HANDLER_GO_ON;
-}
-
-
static int scgi_env_add_scgi(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
buffer *env = venv;
size_t len;
@@ -1461,66 +141,11 @@ static int scgi_env_add_uwsgi(void *venv, const char *key, size_t key_len, const
}
-/**
- *
- * returns
- * -1 error
- * 0 connected
- * 1 not connected yet
- */
-
-static int scgi_establish_connection(server *srv, handler_ctx *hctx) {
- sock_addr addr;
- struct sockaddr *scgi_addr = (struct sockaddr *)&addr;
- socklen_t servlen;
-
- scgi_extension_host *host = hctx->host;
- scgi_proc *proc = hctx->proc;
- int scgi_fd = hctx->fd;
-
- if (!buffer_string_is_empty(proc->socket)) {
- if (1 != sock_addr_from_str_hints(srv, &addr, &servlen, proc->socket->ptr, AF_UNIX, 0)) {
- return -1;
- }
- } else {
- if (1 != sock_addr_from_buffer_hints_numeric(srv, &addr, &servlen, host->host, host->family, proc->port)) {
- return -1;
- }
- }
-
- if (-1 == connect(scgi_fd, scgi_addr, servlen)) {
- if (errno == EINPROGRESS ||
- errno == EALREADY ||
- errno == EINTR) {
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "connect delayed, will continue later:", scgi_fd);
- }
-
- return 1;
- } else {
- scgi_proc_connect_error(srv, host, proc, hctx, errno);
- return -1;
- }
- }
-
- hctx->reconnects = 0;
- if (hctx->conf.debug > 1) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "connect succeeded: ", scgi_fd);
- }
-
-
-
- return 0;
-}
-
-
-static int scgi_create_env(server *srv, handler_ctx *hctx) {
+static handler_t scgi_create_env(server *srv, handler_ctx *hctx) {
buffer *b;
buffer *scgi_env = buffer_init();
- scgi_extension_host *host= hctx->host;
+ gw_host *host = hctx->host;
connection *con = hctx->remote_conn;
@@ -1535,7 +160,7 @@ static int scgi_create_env(server *srv, handler_ctx *hctx) {
if (0 != http_cgi_headers(srv, con, &opts, scgi_env_add, scgi_env)) {
buffer_free(scgi_env);
con->http_status = 400;
- return -1;
+ return HANDLER_FINISHED;
}
if (hctx->conf.proto == LI_PROTOCOL_SCGI) {
@@ -1554,7 +179,7 @@ static int scgi_create_env(server *srv, handler_ctx *hctx) {
buffer_free(scgi_env);
con->http_status = 431; /* Request Header Fields Too Large */
con->mode = DIRECT;
- return -1; /* trigger return of HANDLER_FINISHED */
+ return HANDLER_FINISHED;
}
b = buffer_init();
buffer_string_prepare_copy(b, 4 + len);
@@ -1577,574 +202,10 @@ static int scgi_create_env(server *srv, handler_ctx *hctx) {
hctx->wb_reqlen = -hctx->wb_reqlen;
}
- return 0;
-}
-
-
-static int scgi_proclist_sort_up(server *srv, scgi_extension_host *host, scgi_proc *proc) {
- scgi_proc *p;
-
- UNUSED(srv);
-
- /* we have been the smallest of the current list
- * and we want to insert the node sorted as soon
- * possible
- *
- * 1 0 0 0 1 1 1
- * | ^
- * | |
- * +------+
- *
- */
-
- /* nothing to sort, only one element */
- if (host->first == proc && proc->next == NULL) return 0;
-
- for (p = proc; p->next && p->next->load < proc->load; p = p->next);
-
- /* no need to move something
- *
- * 1 2 2 2 3 3 3
- * ^
- * |
- * +
- *
- */
- if (p == proc) return 0;
-
- if (host->first == proc) {
- /* we have been the first elememt */
-
- host->first = proc->next;
- host->first->prev = NULL;
- }
-
- /* disconnect proc */
-
- if (proc->prev) proc->prev->next = proc->next;
- if (proc->next) proc->next->prev = proc->prev;
-
- /* proc should be right of p */
-
- proc->next = p->next;
- proc->prev = p;
- if (p->next) p->next->prev = proc;
- p->next = proc;
-#if 0
- for(p = host->first; p; p = p->next) {
- log_error_write(srv, __FILE__, __LINE__, "dd",
- p->pid, p->load);
- }
-#else
- UNUSED(srv);
-#endif
-
- return 0;
-}
-
-int scgi_proclist_sort_down(server *srv, scgi_extension_host *host, scgi_proc *proc) {
- scgi_proc *p;
-
- UNUSED(srv);
-
- /* we have been the smallest of the current list
- * and we want to insert the node sorted as soon
- * possible
- *
- * 0 0 0 0 1 0 1
- * ^ |
- * | |
- * +----------+
- *
- *
- * the basic is idea is:
- * - the last active scgi process should be still
- * in ram and is not swapped out yet
- * - processes that are not reused will be killed
- * after some time by the trigger-handler
- * - remember it as:
- * everything > 0 is hot
- * all unused procs are colder the more right they are
- * ice-cold processes are propably unused since more
- * than 'unused-timeout', are swaped out and won't be
- * reused in the next seconds anyway.
- *
- */
-
- /* nothing to sort, only one element */
- if (host->first == proc && proc->next == NULL) return 0;
-
- for (p = host->first; p != proc && p->load < proc->load; p = p->next);
-
-
- /* no need to move something
- *
- * 1 2 2 2 3 3 3
- * ^
- * |
- * +
- *
- */
- if (p == proc) return 0;
-
- /* we have to move left. If we are already the first element
- * we are done */
- if (host->first == proc) return 0;
-
- /* release proc */
- if (proc->prev) proc->prev->next = proc->next;
- if (proc->next) proc->next->prev = proc->prev;
-
- /* proc should be left of p */
- proc->next = p;
- proc->prev = p->prev;
- if (p->prev) p->prev->next = proc;
- p->prev = proc;
-
- if (proc->prev == NULL) host->first = proc;
-#if 0
- for(p = host->first; p; p = p->next) {
- log_error_write(srv, __FILE__, __LINE__, "dd",
- p->pid, p->load);
- }
-#else
- UNUSED(srv);
-#endif
-
- return 0;
-}
-
-static int scgi_restart_dead_procs(server *srv, plugin_data *p, scgi_extension_host *host) {
- scgi_proc *proc;
-
- for (proc = host->first; proc; proc = proc->next) {
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sbdbdddd",
- "proc:",
- host->host, proc->port,
- proc->socket,
- proc->state,
- proc->is_local,
- proc->load,
- proc->pid);
- }
-
- if (0 == scgi_proc_waitpid(srv, host, proc)) {
- scgi_proc_check_enable(srv, host, proc);
- }
-
- if (proc->state == PROC_STATE_DIED && proc->is_local && 0 == proc->load) {
- /* restart local servers */
-
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
- "--- scgi spawning",
- "\n\tport:", host->port,
- "\n\tsocket", host->unixsocket,
- "\n\tcurrent:", 1, "/", host->min_procs);
- }
-
- if (scgi_spawn_connection(srv, p, host, proc)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "ERROR: spawning fcgi failed.");
- return HANDLER_ERROR;
- }
-
- scgi_proclist_sort_down(srv, host, proc);
- }
- }
-
- return 0;
-}
-
-
-static handler_t scgi_write_request(server *srv, handler_ctx *hctx) {
- scgi_extension_host *host= hctx->host;
- connection *con = hctx->remote_conn;
-
- int ret;
-
- switch(hctx->state) {
- case FCGI_STATE_INIT:
- if (-1 == (hctx->fd = fdevent_socket_nb_cloexec(host->family, SOCK_STREAM, 0))) {
- if (errno == EMFILE ||
- errno == EINTR) {
- log_error_write(srv, __FILE__, __LINE__, "sd",
- "wait for fd at connection:", con->fd);
-
- return HANDLER_WAIT_FOR_FD;
- }
-
- log_error_write(srv, __FILE__, __LINE__, "ssdd",
- "socket failed:", strerror(errno), srv->cur_fds, srv->max_fds);
- return HANDLER_ERROR;
- }
-
- srv->cur_fds++;
-
- fdevent_register(srv->ev, hctx->fd, scgi_handle_fdevent, hctx);
-
- if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
- log_error_write(srv, __FILE__, __LINE__, "ss",
- "fcntl failed: ", strerror(errno));
- return HANDLER_ERROR;
- }
-
- /* fall through */
- case FCGI_STATE_CONNECT:
- if (hctx->state == FCGI_STATE_INIT) {
- for (hctx->proc = hctx->host->first;
- hctx->proc && hctx->proc->state != PROC_STATE_RUNNING;
- hctx->proc = hctx->proc->next);
-
- /* all childs are dead */
- if (hctx->proc == NULL) {
- return HANDLER_ERROR;
- }
-
- if (hctx->proc->is_local) {
- hctx->pid = hctx->proc->pid;
- }
-
- switch (scgi_establish_connection(srv, hctx)) {
- case 1:
- scgi_set_state(srv, hctx, FCGI_STATE_CONNECT);
-
- /* connection is in progress, wait for an event and call getsockopt() below */
-
- fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
-
- return HANDLER_WAIT_FOR_EVENT;
- case -1:
- /* if ECONNREFUSED; choose another connection */
- return HANDLER_ERROR;
- default:
- /* everything is ok, go on */
- break;
- }
-
-
- } else {
- int socket_error = fdevent_connect_status(hctx->fd);
- if (socket_error != 0) {
- scgi_proc_connect_error(srv, host, hctx->proc, hctx, socket_error);
- return HANDLER_ERROR;
- }
- }
-
- /* ok, we have the connection */
-
- hctx->proc->load++;
- hctx->proc->last_used = srv->cur_ts;
- hctx->got_proc = 1;
-
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "sddbdd",
- "got proc:",
- hctx->fd,
- hctx->proc->pid,
- hctx->proc->socket,
- hctx->proc->port,
- hctx->proc->load);
- }
-
- /* move the proc-list entry down the list */
- scgi_proclist_sort_up(srv, hctx->host, hctx->proc);
-
- scgi_set_state(srv, hctx, FCGI_STATE_PREPARE_WRITE);
- /* fall through */
- case FCGI_STATE_PREPARE_WRITE:
- if (0 != scgi_create_env(srv, hctx)) {
- return HANDLER_FINISHED;
- }
-
- fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
- scgi_set_state(srv, hctx, FCGI_STATE_WRITE);
-
- /* fall through */
- case FCGI_STATE_WRITE:
- ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
-
- chunkqueue_remove_finished_chunks(hctx->wb);
-
- if (ret < 0) {
- if (errno == ENOTCONN || ret == -2) {
- /* the connection got dropped after accept()
- * we don't care about that - if you accept() it, you have to handle it.
- */
-
- log_error_write(srv, __FILE__, __LINE__, "ssosdb",
- "connection was dropped after accept() (perhaps the scgi process died),",
- "write-offset:", hctx->wb->bytes_out,
- "socket:", hctx->proc->port, hctx->proc->socket);
-
- return HANDLER_ERROR;
- } else {
- /* -1 == ret => error on our side */
- log_error_write(srv, __FILE__, __LINE__, "ssd",
- "write failed:", strerror(errno), errno);
-
- return HANDLER_ERROR;
- }
- }
-
- if (hctx->wb->bytes_out == hctx->wb_reqlen) {
- fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- scgi_set_state(srv, hctx, FCGI_STATE_READ);
- } else {
- off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
- if ((hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0) && wblen < 65536 - 16384) {
- /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
- if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
- con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
- con->is_readable = 1; /* trigger optimistic read from client */
- }
- }
- if (0 == wblen) {
- fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- } else {
- fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
- }
- }
-
- return HANDLER_WAIT_FOR_EVENT;
- case FCGI_STATE_READ:
- /* waiting for a response */
- return HANDLER_WAIT_FOR_EVENT;
- default:
- log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
- return HANDLER_ERROR;
- }
-}
-
-static handler_t scgi_write_error(server *srv, handler_ctx *hctx) {
- connection *con = hctx->remote_conn;
- int status = con->http_status;
-
- if (hctx->state == FCGI_STATE_INIT ||
- hctx->state == FCGI_STATE_CONNECT) {
-
- scgi_restart_dead_procs(srv, hctx->plugin_data, hctx->host);
-
- /* cleanup this request and let request handler start request again */
- if (hctx->reconnects++ < 5) return scgi_reconnect(srv, hctx);
- }
-
- scgi_connection_close(srv, hctx);
- con->http_status = (status == 400) ? 400 : 503;
- return HANDLER_FINISHED;
-}
-
-static handler_t scgi_send_request(server *srv, handler_ctx *hctx) {
- handler_t rc = scgi_write_request(srv, hctx);
- return (HANDLER_ERROR != rc) ? rc : scgi_write_error(srv, hctx);
-}
-
-
-static handler_t scgi_recv_response(server *srv, handler_ctx *hctx);
-
-
-SUBREQUEST_FUNC(mod_scgi_handle_subrequest) {
- plugin_data *p = p_d;
-
- handler_ctx *hctx = con->plugin_ctx[p->id];
-
- if (NULL == hctx) return HANDLER_GO_ON;
-
- /* not my job */
- if (con->mode != p->id) return HANDLER_GO_ON;
-
- if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
- && con->file_started) {
- if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
- fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
- } else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
- /* optimistic read from backend */
- handler_t rc = scgi_recv_response(srv, hctx); /*(might invalidate hctx)*/
- if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
- fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
- }
- }
-
- if (0 == hctx->wb->bytes_in
- ? con->state == CON_STATE_READ_POST
- : (hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0)) {
- /* leave excess data in con->request_content_queue, which is
- * buffered to disk if too large and backend can not keep up */
- /*(64k - 4k to attempt to avoid temporary files
- * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
- if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096) {
- if (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
- con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
- }
- if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
- } else {
- handler_t r = connection_handle_read_post_state(srv, con);
- chunkqueue *req_cq = con->request_content_queue;
- #if 0 /*(not reached since we send 411 Length Required below)*/
- if (hctx->wb_reqlen < -1 && con->request.content_length >= 0) {
- /* (completed receiving Transfer-Encoding: chunked) */
- hctx->wb_reqlen = -hctx->wb_reqlen + con->request.content_length;
- }
- #endif
- if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
- chunkqueue_append_chunkqueue(hctx->wb, req_cq);
- if (fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_OUT) {
- return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
- }
- }
- if (r != HANDLER_GO_ON) return r;
-
- /* SCGI requires that Content-Length be set.
- * Send 411 Length Required if Content-Length missing.
- * (occurs here if client sends Transfer-Encoding: chunked
- * and module is flagged to stream request body to backend) */
- if (-1 == con->request.content_length) {
- return connection_handle_read_post_error(srv, con, 411);
- }
- }
- }
-
- return ((0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
- && hctx->state != FCGI_STATE_CONNECT)
- ? scgi_send_request(srv, hctx)
- : HANDLER_WAIT_FOR_EVENT;
-}
-
-
-static handler_t scgi_recv_response(server *srv, handler_ctx *hctx) {
-
- switch (http_response_read(srv, hctx->remote_conn, &hctx->opts,
- hctx->response, hctx->fd, &hctx->fde_ndx)) {
- default:
- return HANDLER_GO_ON;
- case HANDLER_FINISHED:
- scgi_connection_close(srv, hctx);
- return HANDLER_FINISHED;
- case HANDLER_COMEBACK: /*(not expected; treat as error)*/
- case HANDLER_ERROR: {
- connection *con = hctx->remote_conn;
- plugin_data *p = hctx->plugin_data;
-
- scgi_proc *proc = hctx->proc;
- scgi_extension_host *host= hctx->host;
-
- if (proc->is_local && 1 == proc->load && proc->pid == hctx->pid && proc->state != PROC_STATE_DIED) {
- if (0 != scgi_proc_waitpid(srv, host, proc)) {
- if (hctx->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
- "--- scgi spawning",
- "\n\tport:", host->port,
- "\n\tsocket", host->unixsocket,
- "\n\tcurrent:", 1, "/", host->min_procs);
- }
-
- if (0 == scgi_spawn_connection(srv, p, host, proc)) {
- scgi_proclist_sort_down(srv, host, proc);
- }
-
- break;
- }
- }
-
- if (con->file_started == 0) {
- /* nothing has been send out yet, try to use another child */
-
- if (hctx->wb->bytes_out == 0 &&
- hctx->reconnects++ < 5) {
-
- log_error_write(srv, __FILE__, __LINE__, "ssdsd",
- "response not sent, request not sent, reconnection.",
- "connection-fd:", con->fd,
- "fcgi-fd:", hctx->fd);
-
- return scgi_reconnect(srv, hctx);
- }
-
- log_error_write(srv, __FILE__, __LINE__, "sosdsd",
- "response not sent, request sent:", hctx->wb->bytes_out,
- "connection-fd:", con->fd,
- "fcgi-fd:", hctx->fd);
- } else {
- log_error_write(srv, __FILE__, __LINE__, "ssdsd",
- "response already sent out, termination connection",
- "connection-fd:", con->fd,
- "fcgi-fd:", hctx->fd);
- }
-
- http_response_backend_error(srv, con);
- scgi_connection_close(srv, hctx);
- return HANDLER_FINISHED;
- }
- }
-
- return HANDLER_GO_ON;
+ status_counter_inc(srv, CONST_STR_LEN("scgi.requests"));
+ return HANDLER_GO_ON;
}
-
-static handler_t scgi_handle_fdevent(server *srv, void *ctx, int revents) {
- handler_ctx *hctx = ctx;
- connection *con = hctx->remote_conn;
-
- joblist_append(srv, con);
-
- if (revents & FDEVENT_IN) {
- handler_t rc = scgi_recv_response(srv, hctx);/*(might invalidate hctx)*/
- if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
- }
-
- if (revents & FDEVENT_OUT) {
- return scgi_send_request(srv, hctx); /*(might invalidate hctx)*/
- }
-
- /* perhaps this issue is already handled */
- if (revents & FDEVENT_HUP) {
- if (hctx->state == FCGI_STATE_CONNECT) {
- /* getoptsock will catch this one (right ?)
- *
- * if we are in connect we might get a EINPROGRESS
- * in the first call and a FDEVENT_HUP in the
- * second round
- *
- * FIXME: as it is a bit ugly.
- *
- */
- scgi_send_request(srv, hctx);
- } else if (con->file_started) {
- /* drain any remaining data from kernel pipe buffers
- * even if (con->conf.stream_response_body
- * & FDEVENT_STREAM_RESPONSE_BUFMIN)
- * since event loop will spin on fd FDEVENT_HUP event
- * until unregistered. */
- handler_t rc;
- do {
- rc = scgi_recv_response(srv,hctx);/*(might invalidate hctx)*/
- } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
- return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
- } else {
- scgi_extension_host *host= hctx->host;
- log_error_write(srv, __FILE__, __LINE__, "sbSBSDSd",
- "error: unexpected close of scgi connection for",
- con->uri.path,
- "(no scgi process on host: ",
- host->host,
- ", port: ",
- host->port,
- " ?)",
- hctx->state);
-
- scgi_connection_close(srv, hctx);
- }
- } else if (revents & FDEVENT_ERR) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "fcgi: got a FDEVENT_ERR. Don't know why.");
-
- http_response_backend_error(srv, con);
- scgi_connection_close(srv, hctx);
- }
-
- return HANDLER_FINISHED;
-}
#define PATCH(x) \
p->conf.x = s->x;
static int scgi_patch_connection(server *srv, connection *con, plugin_data *p) {
@@ -2152,8 +213,11 @@ static int scgi_patch_connection(server *srv, connection *con, plugin_data *p) {
plugin_config *s = p->config_storage[0];
PATCH(exts);
+ PATCH(exts_auth);
+ PATCH(exts_resp);
PATCH(proto);
PATCH(debug);
+ PATCH(ext_mapping);
/* skip the first, the global context */
for (i = 1; i < srv->config_context->used; i++) {
@@ -2169,10 +233,14 @@ static int scgi_patch_connection(server *srv, connection *con, plugin_data *p) {
if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.server"))) {
PATCH(exts);
+ PATCH(exts_auth);
+ PATCH(exts_resp);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.protocol"))) {
PATCH(proto);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.debug"))) {
PATCH(debug);
+ } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.map-extensions"))) {
+ PATCH(ext_mapping);
}
}
}
@@ -2184,141 +252,21 @@ static int scgi_patch_connection(server *srv, connection *con, plugin_data *p) {
static handler_t scgi_check_extension(server *srv, connection *con, void *p_d, int uri_path_handler) {
plugin_data *p = p_d;
- size_t s_len, uri_path_len;
- size_t k;
- buffer *fn;
- scgi_extension *extension = NULL;
- scgi_extension_host *host = NULL;
+ handler_t rc;
if (con->mode != DIRECT) return HANDLER_GO_ON;
- /* Possibly, we processed already this request */
- if (con->file_started == 1) return HANDLER_GO_ON;
-
- fn = uri_path_handler ? con->uri.path : con->physical.path;
-
- if (buffer_string_is_empty(fn)) return HANDLER_GO_ON;
-
- s_len = buffer_string_length(fn);
- uri_path_len = buffer_string_length(con->uri.path);
-
scgi_patch_connection(srv, con, p);
+ if (NULL == p->conf.exts) return HANDLER_GO_ON;
- /* check if extension matches */
- for (k = 0; k < p->conf.exts->used; k++) {
- size_t ct_len;
- scgi_extension *ext = p->conf.exts->exts[k];
-
- if (buffer_is_empty(ext->key)) continue;
+ rc = gw_check_extension(srv, con, p, uri_path_handler, 0);
+ if (HANDLER_GO_ON != rc) return rc;
- ct_len = buffer_string_length(ext->key);
-
- /* check _url_ in the form "/scgi_pattern" */
- if (ext->key->ptr[0] == '/') {
- if (ct_len <= uri_path_len
- && 0 == strncmp(con->uri.path->ptr, ext->key->ptr, ct_len)) {
- extension = ext;
- break;
- }
- } else if (ct_len <= s_len
- && 0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len)) {
- /* check extension in the form ".fcg" */
- extension = ext;
- break;
- }
- }
-
- /* extension doesn't match */
- if (NULL == extension) {
- return HANDLER_GO_ON;
- }
-
- /* get best server */
- host = scgi_extension_host_get(srv, con, p, extension);
- if (NULL == host) {
- return HANDLER_FINISHED;
- }
-
- /* a note about no handler is not sent yet */
- extension->note_is_sent = 0;
-
- /*
- * if check-local is disabled, use the uri.path handler
- *
- */
-
- /* init handler-context */
- if (uri_path_handler) {
- if (host->check_local == 0) {
- char *pathinfo;
-
- /* the prefix is the SCRIPT_NAME,
- * everything from start to the next slash
- * this is important for check-local = "disable"
- *
- * if prefix = /admin.fcgi
- *
- * /admin.fcgi/foo/bar
- *
- * SCRIPT_NAME = /admin.fcgi
- * PATH_INFO = /foo/bar
- *
- * if prefix = /fcgi-bin/
- *
- * /fcgi-bin/foo/bar
- *
- * SCRIPT_NAME = /fcgi-bin/foo
- * PATH_INFO = /bar
- *
- */
-
- /* the rewrite is only done for /prefix/? matches */
- if (host->fix_root_path_name && extension->key->ptr[0] == '/' && extension->key->ptr[1] == '\0') {
- buffer_copy_buffer(con->request.pathinfo, con->uri.path);
- buffer_string_set_length(con->uri.path, 0);
- } else if (extension->key->ptr[0] == '/' &&
- buffer_string_length(con->uri.path) > buffer_string_length(extension->key) &&
- NULL != (pathinfo = strchr(con->uri.path->ptr + buffer_string_length(extension->key), '/'))) {
- /* rewrite uri.path and pathinfo */
-
- buffer_copy_string(con->request.pathinfo, pathinfo);
- buffer_string_set_length(con->uri.path, buffer_string_length(con->uri.path) - buffer_string_length(con->request.pathinfo));
- }
- } else {
- return HANDLER_GO_ON;
- }
- }
-
- {
- handler_ctx *hctx;
- hctx = handler_ctx_init();
-
- hctx->remote_conn = con;
- hctx->plugin_data = p;
- hctx->host = host;
- hctx->proc = NULL;
- hctx->ext = extension;
-
- /*hctx->conf.exts = p->conf.exts;*/
- hctx->conf.proto = p->conf.proto;
- hctx->conf.debug = p->conf.debug;
-
- hctx->opts.fdfmt = S_IFSOCK;
+ if (con->mode == p->id) {
+ handler_ctx *hctx = con->plugin_ctx[p->id];
hctx->opts.backend = BACKEND_SCGI;
- hctx->opts.authorizer = 0;
- hctx->opts.local_redir = 0;
- hctx->opts.xsendfile_allow = host->xsendfile_allow;
- hctx->opts.xsendfile_docroot = host->xsendfile_docroot;
-
- con->plugin_ctx[p->id] = hctx;
-
- host->load++;
-
- con->mode = p->id;
-
- if (con->conf.log_request_handling) {
- log_error_write(srv, __FILE__, __LINE__, "s", "handling it in mod_scgi");
- }
+ hctx->create_env = scgi_create_env;
+ hctx->response = buffer_init();
}
return HANDLER_GO_ON;
@@ -2335,167 +283,20 @@ static handler_t scgi_check_extension_2(server *srv, connection *con, void *p_d)
}
-TRIGGER_FUNC(mod_scgi_handle_trigger) {
- plugin_data *p = p_d;
- size_t i, j, n;
-
-
- /* perhaps we should kill a connect attempt after 10-15 seconds
- *
- * currently we wait for the TCP timeout which is on Linux 180 seconds
- *
- *
- *
- */
-
- /* check all childs if they are still up */
-
- for (i = 0; i < srv->config_context->used; i++) {
- plugin_config *conf;
- scgi_exts *exts;
-
- conf = p->config_storage[i];
-
- exts = conf->exts;
-
- for (j = 0; j < exts->used; j++) {
- scgi_extension *ex;
-
- ex = exts->exts[j];
-
- for (n = 0; n < ex->used; n++) {
-
- scgi_proc *proc;
- unsigned long sum_load = 0;
- scgi_extension_host *host;
-
- host = ex->hosts[n];
-
- for (proc = host->first; proc; proc = proc->next) {
- scgi_proc_waitpid(srv, host, proc);
- }
-
- scgi_restart_dead_procs(srv, p, host);
-
- for (proc = host->first; proc; proc = proc->next) {
- sum_load += proc->load;
- }
-
- if (host->num_procs &&
- host->num_procs < host->max_procs &&
- (sum_load / host->num_procs) > host->max_load_per_proc) {
- /* overload, spawn new child */
- scgi_proc *fp = NULL;
-
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "overload detected, spawning a new child");
- }
-
- for (fp = host->unused_procs; fp && fp->pid != 0; fp = fp->next);
-
- if (fp) {
- if (fp == host->unused_procs) host->unused_procs = fp->next;
- else fp->prev->next = fp->next;
-
- if (fp->next) fp->next->prev = fp->prev;
- fp->prev = NULL;
- } else {
- fp = scgi_process_init();
- fp->id = host->max_id++;
- }
-
- host->num_procs++;
-
- if (buffer_string_is_empty(host->unixsocket)) {
- fp->port = host->port + fp->id;
- } else {
- buffer_copy_buffer(fp->socket, host->unixsocket);
- buffer_append_string_len(fp->socket, CONST_STR_LEN("-"));
- buffer_append_int(fp->socket, fp->id);
- }
-
- if (scgi_spawn_connection(srv, p, host, fp)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "ERROR: spawning fcgi failed.");
- fp->next = host->unused_procs;
- if (host->unused_procs) host->unused_procs->prev = fp;
- host->unused_procs = fp;
- } else {
- fp->next = host->first;
- if (host->first) host->first->prev = fp;
- host->first = fp;
- }
- }
-
- for (proc = host->first; proc; proc = proc->next) {
- if (proc->load != 0) break;
- if (host->num_procs <= host->min_procs) break;
- if (proc->pid <= 0) continue;
-
- if (srv->cur_ts - proc->last_used > host->idle_timeout) {
- /* a proc is idling for a long time now,
- * terminated it */
-
- if (p->conf.debug) {
- log_error_write(srv, __FILE__, __LINE__, "ssbsd",
- "idle-timeout reached, terminating child:",
- "socket:", proc->socket,
- "pid", proc->pid);
- }
-
-
- if (proc->next) proc->next->prev = proc->prev;
- if (proc->prev) proc->prev->next = proc->next;
-
- if (proc->prev == NULL) host->first = proc->next;
-
- proc->prev = NULL;
- proc->next = host->unused_procs;
-
- if (host->unused_procs) host->unused_procs->prev = proc;
- host->unused_procs = proc;
-
- kill(proc->pid, SIGTERM);
-
- scgi_proc_set_state(host, proc, PROC_STATE_KILLED);
-
- log_error_write(srv, __FILE__, __LINE__, "ssbsd",
- "killed:",
- "socket:", proc->socket,
- "pid", proc->pid);
-
- host->num_procs--;
-
- /* proc is now in unused, let the next second handle the next process */
- break;
- }
- }
-
- for (proc = host->unused_procs; proc; proc = proc->next) {
- scgi_proc_waitpid(srv, host, proc);
- }
- }
- }
- }
-
- return HANDLER_GO_ON;
-}
-
int mod_scgi_plugin_init(plugin *p);
int mod_scgi_plugin_init(plugin *p) {
p->version = LIGHTTPD_VERSION_ID;
p->name = buffer_init_string("scgi");
- p->init = mod_scgi_init;
- p->cleanup = mod_scgi_free;
+ p->init = gw_init;
+ p->cleanup = gw_free;
p->set_defaults = mod_scgi_set_defaults;
- p->connection_reset = scgi_connection_reset;
+ p->connection_reset = gw_connection_reset;
p->handle_uri_clean = scgi_check_extension_1;
p->handle_subrequest_start = scgi_check_extension_2;
- p->handle_subrequest = mod_scgi_handle_subrequest;
- p->handle_trigger = mod_scgi_handle_trigger;
+ p->handle_subrequest = gw_handle_subrequest;
+ p->handle_trigger = gw_handle_trigger;
p->data = NULL;
diff --git a/src/response.h b/src/response.h
index d0ddc90b..cd268394 100644
--- a/src/response.h
+++ b/src/response.h
@@ -35,8 +35,9 @@ typedef struct http_response_opts_t {
unsigned short local_redir;
unsigned short xsendfile_allow;
array *xsendfile_docroot;
- handler_t(*parse)(server *, connection *, struct http_response_opts_t *, buffer *, size_t);
void *pdata;
+ handler_t(*parse)(server *, connection *, struct http_response_opts_t *, buffer *, size_t);
+ handler_t(*headers)(server *, connection *, struct http_response_opts_t *);
} http_response_opts;
typedef int (*http_cgi_header_append_cb)(void *vdata, const char *k, size_t klen, const char *v, size_t vlen);