xref: /lighttpd1.4/src/gw_backend.c (revision 9570cc2f)
1 /*
2  * gw_backend - gateway backend code shared by dynamic socket backends
3  *
4  * Copyright(c) 2017 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
5  * License: BSD 3-clause (same as lighttpd)
6  */
7 #include "first.h"
8 
9 #include "gw_backend.h"
10 
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include "sys-socket.h"
14 #ifdef HAVE_SYS_UIO_H
15 #include <sys/uio.h>
16 #endif
17 #ifdef HAVE_SYS_WAIT_H
18 #include <sys/wait.h>
19 #endif
20 
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include "base.h"
30 #include "array.h"
31 #include "buffer.h"
32 #include "crc32.h"
33 #include "fdevent.h"
34 #include "log.h"
35 #include "sock_addr.h"
36 #include "settings.h"   /* MAX_WRITE_LIMIT */
37 
38 
39 
40 
41 #include "status_counter.h"
42 
43 __attribute_noinline__
44 static int * gw_status_get_counter(gw_host *host, gw_proc *proc, const char *tag, size_t tlen) {
45     /*(At the cost of some memory, could prepare strings for host and for proc
46      * so that here we would copy ready made string for proc (or if NULL,
47      * for host), and then append tag to produce key)*/
48     char label[288];
49     size_t llen = sizeof("gw.backend.")-1, len;
50     memcpy(label, "gw.backend.", llen);
51 
52     len = buffer_string_length(host->id);
53     force_assert(len < sizeof(label) - llen);
54     memcpy(label+llen, host->id->ptr, len);
55     llen += len;
56 
57     if (proc) {
58         force_assert(llen < sizeof(label) - (LI_ITOSTRING_LENGTH + 1));
59         label[llen++] = '.';
60         len = li_utostrn(label+llen, LI_ITOSTRING_LENGTH, proc->id);
61         llen += len;
62     }
63 
64     force_assert(tlen < sizeof(label) - llen);
65     memcpy(label+llen, tag, tlen);
66     llen += tlen;
67     label[llen] = '\0';
68 
69     return status_counter_get_counter(label, llen);
70 }
71 
72 static void gw_proc_tag_inc(gw_host *host, gw_proc *proc, const char *tag, size_t len) {
73     ++(*gw_status_get_counter(host, proc, tag, len));
74 }
75 
76 static void gw_proc_load_inc(gw_host *host, gw_proc *proc) {
77     *gw_status_get_counter(host, proc, CONST_STR_LEN(".load")) = ++proc->load;
78 
79     status_counter_inc(CONST_STR_LEN("gw.active-requests"));
80 }
81 
82 static void gw_proc_load_dec(gw_host *host, gw_proc *proc) {
83     *gw_status_get_counter(host, proc, CONST_STR_LEN(".load")) = --proc->load;
84 
85     status_counter_dec(CONST_STR_LEN("gw.active-requests"));
86 }
87 
88 static void gw_host_assign(gw_host *host) {
89     *gw_status_get_counter(host, NULL, CONST_STR_LEN(".load")) = ++host->load;
90 }
91 
92 static void gw_host_reset(gw_host *host) {
93     *gw_status_get_counter(host, NULL, CONST_STR_LEN(".load")) = --host->load;
94 }
95 
96 static int gw_status_init(gw_host *host, gw_proc *proc) {
97     *gw_status_get_counter(host, proc, CONST_STR_LEN(".disabled")) = 0;
98     *gw_status_get_counter(host, proc, CONST_STR_LEN(".died")) = 0;
99     *gw_status_get_counter(host, proc, CONST_STR_LEN(".overloaded")) = 0;
100     *gw_status_get_counter(host, proc, CONST_STR_LEN(".connected")) = 0;
101     *gw_status_get_counter(host, proc, CONST_STR_LEN(".load")) = 0;
102 
103     *gw_status_get_counter(host, NULL, CONST_STR_LEN(".load")) = 0;
104 
105     return 0;
106 }
107 
108 
109 
110 
111 static void gw_proc_set_state(gw_host *host, gw_proc *proc, int state) {
112     if ((int)proc->state == state) return;
113     if (proc->state == PROC_STATE_RUNNING) {
114         --host->active_procs;
115     } else if (state == PROC_STATE_RUNNING) {
116         ++host->active_procs;
117     }
118     proc->state = state;
119 }
120 
121 
122 static gw_proc *gw_proc_init(void) {
123     gw_proc *f = calloc(1, sizeof(*f));
124     force_assert(f);
125 
126     f->unixsocket = buffer_init();
127     f->connection_name = buffer_init();
128 
129     f->prev = NULL;
130     f->next = NULL;
131     f->state = PROC_STATE_DIED;
132 
133     return f;
134 }
135 
136 static void gw_proc_free(gw_proc *f) {
137     if (!f) return;
138 
139     gw_proc_free(f->next);
140 
141     buffer_free(f->unixsocket);
142     buffer_free(f->connection_name);
143     free(f->saddr);
144 
145     free(f);
146 }
147 
148 static gw_host *gw_host_init(void) {
149     gw_host *f = calloc(1, sizeof(*f));
150     force_assert(f);
151     return f;
152 }
153 
154 static void gw_host_free(gw_host *h) {
155     if (!h) return;
156     if (h->refcount) {
157         --h->refcount;
158         return;
159     }
160 
161     gw_proc_free(h->first);
162     gw_proc_free(h->unused_procs);
163 
164     for (uint32_t i = 0; i < h->args.used; ++i) free(h->args.ptr[i]);
165     free(h->args.ptr);
166     free(h);
167 }
168 
169 static gw_exts *gw_extensions_init(void) {
170     gw_exts *f = calloc(1, sizeof(*f));
171     force_assert(f);
172     return f;
173 }
174 
175 static void gw_extensions_free(gw_exts *f) {
176     if (!f) return;
177     for (uint32_t i = 0; i < f->used; ++i) {
178         gw_extension *fe = f->exts+i;
179         for (uint32_t j = 0; j < fe->used; ++j) {
180             gw_host_free(fe->hosts[j]);
181         }
182         free(fe->hosts);
183     }
184     free(f->exts);
185     free(f);
186 }
187 
188 static int gw_extension_insert(gw_exts *ext, const buffer *key, gw_host *fh) {
189     gw_extension *fe = NULL;
190     for (uint32_t i = 0; i < ext->used; ++i) {
191         if (buffer_is_equal(key, &ext->exts[i].key)) {
192             fe = ext->exts+i;
193             break;
194         }
195     }
196 
197     if (NULL == fe) {
198         if (ext->used == ext->size) {
199             ext->size += 8;
200             ext->exts = realloc(ext->exts, ext->size * sizeof(gw_extension));
201             force_assert(ext->exts);
202             memset(ext->exts + ext->used, 0, 8 * sizeof(gw_extension));
203         }
204         fe = ext->exts + ext->used++;
205         fe->last_used_ndx = -1;
206         buffer *b;
207         *(const buffer **)&b = &fe->key;
208         memcpy(b, key, sizeof(buffer)); /*(copy; not later free'd)*/
209     }
210 
211     if (fe->size == fe->used) {
212         fe->size += 4;
213         fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts)));
214         force_assert(fe->hosts);
215     }
216 
217     fe->hosts[fe->used++] = fh;
218     return 0;
219 }
220 
221 static void gw_proc_connect_success(gw_host *host, gw_proc *proc, int debug, request_st * const r) {
222     gw_proc_tag_inc(host, proc, CONST_STR_LEN(".connected"));
223     proc->last_used = log_epoch_secs;
224 
225     if (debug) {
226         log_error(r->conf.errh, __FILE__, __LINE__,
227           "got proc: pid: %d socket: %s load: %d",
228           proc->pid, proc->connection_name->ptr, proc->load);
229     }
230 }
231 
232 __attribute_cold__
233 static void gw_proc_connect_error(request_st * const r, gw_host *host, gw_proc *proc, pid_t pid, int errnum, int debug) {
234     const time_t cur_ts = log_epoch_secs;
235     log_error_st * const errh = r->conf.errh;
236     log_error(errh, __FILE__, __LINE__,
237       "establishing connection failed: socket: %s: %s",
238       proc->connection_name->ptr, strerror(errnum));
239 
240     if (!proc->is_local) {
241         proc->disabled_until = cur_ts + host->disable_time;
242         gw_proc_set_state(host, proc, PROC_STATE_OVERLOADED);
243     }
244     else if (proc->pid == pid && proc->state == PROC_STATE_RUNNING) {
245         /* several requests from lighttpd might reference the same proc
246          *
247          * Only one of them should mark the proc
248          * and all other ones should just take a new one.
249          *
250          * If a new proc was started with the old struct, this might
251          * otherwise lead to marking a perfectly good proc as dead
252          */
253         log_error(errh, __FILE__, __LINE__,
254           "backend error; we'll disable for %d"
255           "secs and send the request to another backend instead:"
256           "load: %d", host->disable_time, host->load);
257         if (EAGAIN == errnum) {
258             /* - EAGAIN: cool down the backend; it is overloaded */
259           #ifdef __linux__
260             log_error(errh, __FILE__, __LINE__,
261               "If this happened on Linux: You have run out of local ports. "
262               "Check the manual, section Performance how to handle this.");
263           #endif
264             if (debug) {
265                 log_error(errh, __FILE__, __LINE__,
266                   "This means that you have more incoming requests than your "
267                   "FastCGI backend can handle in parallel.  It might help to "
268                   "spawn more FastCGI backends or PHP children; if not, "
269                   "decrease server.max-connections.  The load for this FastCGI "
270                   "backend %s is %d", proc->connection_name->ptr, proc->load);
271             }
272             proc->disabled_until = cur_ts + host->disable_time;
273             gw_proc_set_state(host, proc, PROC_STATE_OVERLOADED);
274         }
275         else {
276             /* we got a hard error from the backend like
277              * - ECONNREFUSED for tcp-ip sockets
278              * - ENOENT for unix-domain-sockets
279              */
280           #if 0
281             gw_proc_set_state(host, proc, PROC_STATE_DIED_WAIT_FOR_PID);
282           #else  /* treat as overloaded (future: unless we send kill() signal)*/
283             proc->disabled_until = cur_ts + host->disable_time;
284             gw_proc_set_state(host, proc, PROC_STATE_OVERLOADED);
285           #endif
286         }
287     }
288 
289     if (EAGAIN == errnum) {
290         gw_proc_tag_inc(host, proc, CONST_STR_LEN(".overloaded"));
291     }
292     else {
293         gw_proc_tag_inc(host, proc, CONST_STR_LEN(".died"));
294     }
295 }
296 
297 static void gw_proc_release(gw_host *host, gw_proc *proc, int debug, log_error_st *errh) {
298     gw_proc_load_dec(host, proc);
299 
300     if (debug) {
301         log_error(errh, __FILE__, __LINE__,
302           "released proc: pid: %d socket: %s load: %u",
303           proc->pid, proc->connection_name->ptr, proc->load);
304     }
305 }
306 
307 static void gw_proc_check_enable(gw_host * const host, gw_proc * const proc, log_error_st * const errh) {
308     if (log_epoch_secs <= proc->disabled_until) return;
309     if (proc->state != PROC_STATE_OVERLOADED) return;
310 
311     gw_proc_set_state(host, proc, PROC_STATE_RUNNING);
312 
313     log_error(errh, __FILE__, __LINE__,
314       "gw-server re-enabled: %s %s %hu %s",
315       proc->connection_name->ptr, host->host->ptr, host->port,
316       host->unixsocket->ptr ? host->unixsocket->ptr : "");
317 }
318 
319 static void gw_proc_waitpid_log(const gw_host * const host, const gw_proc * const proc, log_error_st * const errh, const int status) {
320     if (WIFEXITED(status)) {
321         if (proc->state != PROC_STATE_KILLED) {
322             log_error(errh, __FILE__, __LINE__,
323               "child exited: %d %s",
324               WEXITSTATUS(status), proc->connection_name->ptr);
325         }
326     } else if (WIFSIGNALED(status)) {
327         if (WTERMSIG(status) != SIGTERM && WTERMSIG(status) != SIGINT
328             && WTERMSIG(status) != host->kill_signal) {
329             log_error(errh, __FILE__, __LINE__,
330               "child signalled: %d", WTERMSIG(status));
331         }
332     } else {
333         log_error(errh, __FILE__, __LINE__,
334           "child died somehow: %d", status);
335     }
336 }
337 
338 static int gw_proc_waitpid(gw_host *host, gw_proc *proc, log_error_st *errh) {
339     int rc, status;
340 
341     if (!proc->is_local) return 0;
342     if (proc->pid <= 0) return 0;
343 
344     do {
345         rc = waitpid(proc->pid, &status, WNOHANG);
346     } while (-1 == rc && errno == EINTR);
347     if (0 == rc) return 0; /* child still running */
348 
349     /* child terminated */
350     if (-1 == rc) {
351         /* EINVAL or ECHILD no child processes */
352         /* should not happen; someone else has cleaned up for us */
353         log_perror(errh, __FILE__, __LINE__,
354           "pid %d %d not found", proc->pid, proc->state);
355     }
356     else {
357         gw_proc_waitpid_log(host, proc, errh, status);
358     }
359 
360     proc->pid = 0;
361     if (proc->state != PROC_STATE_KILLED)
362         proc->disabled_until = log_epoch_secs;
363     gw_proc_set_state(host, proc, PROC_STATE_DIED);
364     return 1;
365 }
366 
367 static int gw_proc_sockaddr_init(gw_host * const host, gw_proc * const proc, log_error_st * const errh) {
368     sock_addr addr;
369     socklen_t addrlen;
370 
371     if (!buffer_string_is_empty(proc->unixsocket)) {
372         if (1 != sock_addr_from_str_hints(&addr,&addrlen,proc->unixsocket->ptr,
373                                           AF_UNIX, 0, errh)) {
374             errno = EINVAL;
375             return -1;
376         }
377         buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("unix:"));
378         buffer_append_string_buffer(proc->connection_name, proc->unixsocket);
379     }
380     else {
381         /*(note: name resolution here is *blocking* if IP string not supplied)*/
382         if (1 != sock_addr_from_str_hints(&addr, &addrlen, host->host->ptr,
383                                           0, proc->port, errh)) {
384             errno = EINVAL;
385             return -1;
386         }
387         else {
388             /* overwrite host->host buffer with IP addr string so that
389              * any further use of gw_host does not block on DNS lookup */
390             buffer *h;
391             *(const buffer **)&h = host->host;
392             sock_addr_inet_ntop_copy_buffer(h, &addr);
393             host->family = sock_addr_get_family(&addr);
394         }
395         buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("tcp:"));
396         buffer_append_string_buffer(proc->connection_name, host->host);
397         buffer_append_string_len(proc->connection_name, CONST_STR_LEN(":"));
398         buffer_append_int(proc->connection_name, proc->port);
399     }
400 
401     if (NULL != proc->saddr && proc->saddrlen < addrlen) {
402         free(proc->saddr);
403         proc->saddr = NULL;
404     }
405     if (NULL == proc->saddr) {
406         proc->saddr = (struct sockaddr *)malloc(addrlen);
407         force_assert(proc->saddr);
408     }
409     proc->saddrlen = addrlen;
410     memcpy(proc->saddr, &addr, addrlen);
411     return 0;
412 }
413 
414 static int env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
415     char *dst;
416 
417     if (!key || !val) return -1;
418 
419     dst = malloc(key_len + val_len + 3);
420     force_assert(dst);
421     memcpy(dst, key, key_len);
422     dst[key_len] = '=';
423     memcpy(dst + key_len + 1, val, val_len + 1); /* add the \0 from the value */
424 
425     for (uint32_t i = 0; i < env->used; ++i) {
426         if (0 == strncmp(dst, env->ptr[i], key_len + 1)) {
427             free(env->ptr[i]);
428             env->ptr[i] = dst;
429             return 0;
430         }
431     }
432 
433     if (env->size <= env->used + 1) {
434         env->size += 16;
435         env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
436         force_assert(env->ptr);
437     }
438 
439     env->ptr[env->used++] = dst;
440 
441     return 0;
442 }
443 
444 static int gw_spawn_connection(gw_host * const host, gw_proc * const proc, log_error_st * const errh, int debug) {
445     int gw_fd;
446     int status;
447     struct timeval tv = { 0, 10 * 1000 };
448 
449     if (debug) {
450         log_error(errh, __FILE__, __LINE__,
451           "new proc, socket: %hu %s",
452           proc->port, proc->unixsocket->ptr ? proc->unixsocket->ptr : "");
453     }
454 
455     gw_fd = fdevent_socket_cloexec(proc->saddr->sa_family, SOCK_STREAM, 0);
456     if (-1 == gw_fd) {
457         log_perror(errh, __FILE__, __LINE__, "socket()");
458         return -1;
459     }
460 
461     do {
462         status = connect(gw_fd, proc->saddr, proc->saddrlen);
463     } while (-1 == status && errno == EINTR);
464 
465     if (-1 == status && errno != ENOENT
466         && !buffer_string_is_empty(proc->unixsocket)) {
467         log_perror(errh, __FILE__, __LINE__,
468           "unlink %s after connect failed", proc->unixsocket->ptr);
469         unlink(proc->unixsocket->ptr);
470     }
471 
472     close(gw_fd);
473 
474     if (-1 == status) {
475         /* server is not up, spawn it  */
476         char_array env;
477         uint32_t i;
478         int dfd = -1;
479 
480         /* reopen socket */
481         gw_fd = fdevent_socket_cloexec(proc->saddr->sa_family, SOCK_STREAM, 0);
482         if (-1 == gw_fd) {
483             log_perror(errh, __FILE__, __LINE__, "socket()");
484             return -1;
485         }
486 
487         if (fdevent_set_so_reuseaddr(gw_fd, 1) < 0) {
488             log_perror(errh, __FILE__, __LINE__, "socketsockopt()");
489             close(gw_fd);
490             return -1;
491         }
492 
493         /* create socket */
494         if (-1 == bind(gw_fd, proc->saddr, proc->saddrlen)) {
495             log_perror(errh, __FILE__, __LINE__,
496               "bind failed for: %s", proc->connection_name->ptr);
497             close(gw_fd);
498             return -1;
499         }
500 
501         if (-1 == listen(gw_fd, host->listen_backlog)) {
502             log_perror(errh, __FILE__, __LINE__, "listen()");
503             close(gw_fd);
504             return -1;
505         }
506 
507         {
508             /* create environment */
509             env.ptr = NULL;
510             env.size = 0;
511             env.used = 0;
512 
513             /* build clean environment */
514             if (host->bin_env_copy && host->bin_env_copy->used) {
515                 for (i = 0; i < host->bin_env_copy->used; ++i) {
516                     data_string *ds=(data_string *)host->bin_env_copy->data[i];
517                     char *ge;
518 
519                     if (NULL != (ge = getenv(ds->value.ptr))) {
520                         env_add(&env,CONST_BUF_LEN(&ds->value),ge,strlen(ge));
521                     }
522                 }
523             } else {
524                 char ** const e = fdevent_environ();
525                 for (i = 0; e[i]; ++i) {
526                     char *eq;
527 
528                     if (NULL != (eq = strchr(e[i], '='))) {
529                         env_add(&env, e[i], eq - e[i], eq+1, strlen(eq+1));
530                     }
531                 }
532             }
533 
534             /* create environment */
535             if (host->bin_env) {
536                 for (i = 0; i < host->bin_env->used; ++i) {
537                     data_string *ds = (data_string *)host->bin_env->data[i];
538                     env_add(&env, CONST_BUF_LEN(&ds->key),
539                                   CONST_BUF_LEN(&ds->value));
540                 }
541             }
542 
543             for (i = 0; i < env.used; ++i) {
544                 /* search for PHP_FCGI_CHILDREN */
545                 if (0 == strncmp(env.ptr[i], "PHP_FCGI_CHILDREN=",
546                                       sizeof("PHP_FCGI_CHILDREN=")-1)) {
547                     break;
548                 }
549             }
550 
551             /* not found, add a default */
552             if (i == env.used) {
553                 env_add(&env, CONST_STR_LEN("PHP_FCGI_CHILDREN"),
554                               CONST_STR_LEN("1"));
555             }
556 
557             env.ptr[env.used] = NULL;
558         }
559 
560         dfd = fdevent_open_dirname(host->args.ptr[0], 1); /* permit symlinks */
561         if (-1 == dfd) {
562             log_perror(errh, __FILE__, __LINE__,
563               "open dirname failed: %s", host->args.ptr[0]);
564         }
565 
566         /*(FCGI_LISTENSOCK_FILENO == STDIN_FILENO == 0)*/
567         proc->pid = (dfd >= 0)
568           ? fdevent_fork_execve(host->args.ptr[0], host->args.ptr,
569                                 env.ptr, gw_fd, -1, -1, dfd)
570           : -1;
571 
572         for (i = 0; i < env.used; ++i) free(env.ptr[i]);
573         free(env.ptr);
574         if (-1 != dfd) close(dfd);
575         close(gw_fd);
576 
577         if (-1 == proc->pid) {
578             log_error(errh, __FILE__, __LINE__,
579               "gw-backend failed to start: %s", host->bin_path->ptr);
580             proc->pid = 0;
581             proc->disabled_until = log_epoch_secs;
582             return -1;
583         }
584 
585         /* register process */
586         proc->last_used = log_epoch_secs;
587         proc->is_local = 1;
588 
589         /* wait */
590         select(0, NULL, NULL, NULL, &tv);
591 
592         if (0 != gw_proc_waitpid(host, proc, errh)) {
593             log_error(errh, __FILE__, __LINE__,
594               "gw-backend failed to start: %s", host->bin_path->ptr);
595             log_error(errh, __FILE__, __LINE__,
596               "If you're trying to run your app as a FastCGI backend, make "
597               "sure you're using the FastCGI-enabled version.  If this is PHP "
598               "on Gentoo, add 'fastcgi' to the USE flags.  If this is PHP, try "
599               "removing the bytecode caches for now and try again.");
600             return -1;
601         }
602     } else {
603         proc->is_local = 0;
604         proc->pid = 0;
605 
606         if (debug) {
607             log_error(errh, __FILE__, __LINE__,
608               "(debug) socket is already used; won't spawn: %s",
609               proc->connection_name->ptr);
610         }
611     }
612 
613     gw_proc_set_state(host, proc, PROC_STATE_RUNNING);
614     return 0;
615 }
616 
617 static void gw_proc_spawn(gw_host * const host, log_error_st * const errh, const int debug) {
618     gw_proc *proc;
619     for (proc = host->unused_procs; proc; proc = proc->next) {
620         /* (proc->pid <= 0 indicates PROC_STATE_DIED, not PROC_STATE_KILLED) */
621         if (proc->pid > 0) continue;
622         /* (do not attempt to spawn another proc if a proc just exited) */
623         if (proc->disabled_until >= log_epoch_secs) return;
624         break;
625     }
626     if (proc) {
627         if (proc == host->unused_procs)
628             host->unused_procs = proc->next;
629         else
630             proc->prev->next = proc->next;
631 
632         if (proc->next) {
633             proc->next->prev = proc->prev;
634             proc->next = NULL;
635         }
636 
637         proc->prev = NULL;
638     } else {
639         proc = gw_proc_init();
640         proc->id = host->max_id++;
641     }
642 
643     ++host->num_procs;
644 
645     if (buffer_string_is_empty(host->unixsocket)) {
646         proc->port = host->port + proc->id;
647     } else {
648         buffer_copy_buffer(proc->unixsocket, host->unixsocket);
649         buffer_append_string_len(proc->unixsocket, CONST_STR_LEN("-"));
650         buffer_append_int(proc->unixsocket, proc->id);
651     }
652 
653     if (0 != gw_proc_sockaddr_init(host, proc, errh)) {
654         /*(should not happen if host->host validated at startup,
655          * and translated from name to IP address at startup)*/
656         log_error(errh, __FILE__, __LINE__,
657           "ERROR: spawning backend failed.");
658         --host->num_procs;
659         if (proc->id == host->max_id-1) --host->max_id;
660         gw_proc_free(proc);
661     } else if (gw_spawn_connection(host, proc, errh, debug)) {
662         log_error(errh, __FILE__, __LINE__,
663           "ERROR: spawning backend failed.");
664         proc->next = host->unused_procs;
665         if (host->unused_procs)
666             host->unused_procs->prev = proc;
667         host->unused_procs = proc;
668     } else {
669         proc->next = host->first;
670         if (host->first)
671             host->first->prev = proc;
672         host->first = proc;
673     }
674 }
675 
676 static void gw_proc_kill(gw_host *host, gw_proc *proc) {
677     if (proc->next) proc->next->prev = proc->prev;
678     if (proc->prev) proc->prev->next = proc->next;
679 
680     if (proc->prev == NULL) host->first = proc->next;
681 
682     proc->prev = NULL;
683     proc->next = host->unused_procs;
684     proc->disabled_until = 0;
685 
686     if (host->unused_procs)
687         host->unused_procs->prev = proc;
688     host->unused_procs = proc;
689 
690     kill(proc->pid, host->kill_signal);
691 
692     gw_proc_set_state(host, proc, PROC_STATE_KILLED);
693 
694     --host->num_procs;
695 }
696 
697 static gw_host * unixsocket_is_dup(gw_plugin_data *p, const buffer *unixsocket) {
698     if (NULL == p->cvlist) return NULL;
699     /* (init i to 0 if global context; to 1 to skip empty global context) */
700     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
701         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
702         gw_plugin_config *conf = NULL;
703         for (; -1 != cpv->k_id; ++cpv) {
704             switch (cpv->k_id) {
705               case 0: /* xxxxx.server */
706                 if (cpv->vtype == T_CONFIG_LOCAL) conf = cpv->v.v;
707                 break;
708               default:
709                 break;
710             }
711         }
712 
713         if (NULL == conf || NULL == conf->exts) continue;
714 
715         gw_exts *exts = conf->exts;
716         for (uint32_t j = 0; j < exts->used; ++j) {
717             gw_extension *ex = exts->exts+j;
718             for (uint32_t n = 0; n < ex->used; ++n) {
719                 gw_host *host = ex->hosts[n];
720                 if (!buffer_string_is_empty(host->unixsocket)
721                     && buffer_is_equal(host->unixsocket, unixsocket)
722                     && !buffer_string_is_empty(host->bin_path))
723                     return host;
724             }
725         }
726     }
727 
728     return NULL;
729 }
730 
731 static int parse_binpath(char_array *env, const buffer *b) {
732     char *start = b->ptr;
733     char c;
734     /* search for spaces */
735     for (size_t i = 0; i < buffer_string_length(b); ++i) {
736         switch(b->ptr[i]) {
737         case ' ':
738         case '\t':
739             /* a WS, stop here and copy the argument */
740 
741             if (env->size == env->used) {
742                 env->size += 16;
743                 env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
744             }
745 
746             c = b->ptr[i];
747             b->ptr[i] = '\0';
748             env->ptr[env->used++] = strdup(start);
749             b->ptr[i] = c;
750 
751             start = b->ptr + i + 1;
752             break;
753         default:
754             break;
755         }
756     }
757 
758     if (env->size == env->used) { /*need one extra for terminating NULL*/
759         env->size += 16;
760         env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
761     }
762 
763     /* the rest */
764     env->ptr[env->used++] = strdup(start);
765 
766     if (env->size == env->used) { /*need one extra for terminating NULL*/
767         env->size += 16;
768         env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
769     }
770 
771     /* terminate */
772     env->ptr[env->used++] = NULL;
773 
774     return 0;
775 }
776 
777 enum {
778   GW_BALANCE_LEAST_CONNECTION,
779   GW_BALANCE_RR,
780   GW_BALANCE_HASH,
781   GW_BALANCE_STICKY
782 };
783 
784 static gw_host * gw_host_get(request_st * const r, gw_extension *extension, int balance, int debug) {
785     gw_host *host;
786     buffer *dst_addr_buf;
787     unsigned long last_max = ULONG_MAX;
788     unsigned long base_crc32c;
789     int max_usage = INT_MAX;
790     int ndx = -1;
791     uint32_t k;
792 
793     if (extension->used <= 1) {
794         if (1 == extension->used && extension->hosts[0]->active_procs > 0) {
795             ndx = 0;
796         }
797     } else switch(balance) {
798     case GW_BALANCE_HASH:
799         /* hash balancing */
800 
801         if (debug) {
802             log_error(r->conf.errh, __FILE__, __LINE__,
803               "proxy - used hash balancing, hosts: %u", extension->used);
804         }
805 
806         base_crc32c = generate_crc32c(CONST_BUF_LEN(&r->uri.path))
807                     + generate_crc32c(CONST_BUF_LEN(&r->uri.authority));
808 
809         for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->used; ++k) {
810             unsigned long cur_max;
811             host = extension->hosts[k];
812             if (0 == host->active_procs) continue;
813 
814             cur_max = base_crc32c
815                     + generate_crc32c(CONST_BUF_LEN(host->host)); /* cacheable */
816 
817             if (debug) {
818                 log_error(r->conf.errh, __FILE__, __LINE__,
819                   "proxy - election: %s %s %s %lu", r->uri.path.ptr,
820                   host->host->ptr, r->uri.authority.ptr, cur_max);
821             }
822 
823             if (last_max < cur_max || last_max == ULONG_MAX) {
824                 last_max = cur_max;
825                 ndx = k;
826             }
827         }
828 
829         break;
830     case GW_BALANCE_LEAST_CONNECTION:
831         /* fair balancing */
832         if (debug) {
833             log_error(r->conf.errh, __FILE__, __LINE__,
834               "proxy - used least connection");
835         }
836 
837         for (k = 0, ndx = -1, max_usage = INT_MAX; k < extension->used; ++k) {
838             host = extension->hosts[k];
839             if (0 == host->active_procs) continue;
840 
841             if (host->load < max_usage) {
842                 max_usage = host->load;
843                 ndx = k;
844             }
845         }
846 
847         break;
848     case GW_BALANCE_RR:
849         /* round robin */
850         if (debug) {
851             log_error(r->conf.errh, __FILE__, __LINE__,
852               "proxy - used round-robin balancing");
853         }
854 
855         /* just to be sure */
856         force_assert(extension->used < INT_MAX);
857 
858         host = extension->hosts[0];
859 
860         /* Use last_used_ndx from first host in list */
861         k = extension->last_used_ndx;
862         ndx = k + 1; /* use next host after the last one */
863         if (ndx < 0) ndx = 0;
864 
865         /* Search first active host after last_used_ndx */
866         while (ndx < (int) extension->used
867                && 0 == (host = extension->hosts[ndx])->active_procs) ++ndx;
868 
869         if (ndx >= (int) extension->used) {
870             /* didn't find a higher id, wrap to the start */
871             for (ndx = 0; ndx <= (int) k; ++ndx) {
872                 host = extension->hosts[ndx];
873                 if (0 != host->active_procs) break;
874             }
875 
876             /* No active host found */
877             if (0 == host->active_procs) ndx = -1;
878         }
879 
880         /* Save new index for next round */
881         extension->last_used_ndx = ndx;
882 
883         break;
884     case GW_BALANCE_STICKY:
885         /* source sticky balancing */
886         dst_addr_buf = r->con->dst_addr_buf;
887 
888         if (debug) {
889             log_error(r->conf.errh, __FILE__, __LINE__,
890               "proxy - used sticky balancing, hosts: %u", extension->used);
891         }
892 
893         base_crc32c = generate_crc32c(CONST_BUF_LEN(dst_addr_buf));
894 
895         for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->used; ++k) {
896             unsigned long cur_max;
897             host = extension->hosts[k];
898 
899             if (0 == host->active_procs) continue;
900 
901             cur_max = base_crc32c
902                     + generate_crc32c(CONST_BUF_LEN(host->host)) /* cacheable */
903                     + host->port;
904 
905             if (debug) {
906                 log_error(r->conf.errh, __FILE__, __LINE__,
907                   "proxy - election: %s %s %hu %ld", dst_addr_buf->ptr,
908                   host->host->ptr, host->port, cur_max);
909             }
910 
911             if (last_max < cur_max || last_max == ULONG_MAX) {
912                 last_max = cur_max;
913                 ndx = k;
914             }
915         }
916 
917         break;
918     default:
919         break;
920     }
921 
922     if (-1 != ndx) {
923         /* found a server */
924         host = extension->hosts[ndx];
925 
926         if (debug) {
927             log_error(r->conf.errh, __FILE__, __LINE__,
928               "gw - found a host %s %hu", host->host->ptr, host->port);
929         }
930 
931         return host;
932     } else if (0 == r->con->srv->srvconf.max_worker) {
933         /* special-case adaptive spawning and 0 == host->min_procs */
934         for (k = 0; k < extension->used; ++k) {
935             host = extension->hosts[k];
936             if (0 == host->min_procs && 0 == host->num_procs
937                 && !buffer_string_is_empty(host->bin_path)) {
938                 gw_proc_spawn(host, r->con->srv->errh, debug);
939                 if (host->num_procs) return host;
940             }
941         }
942     }
943 
944     /* all hosts are down */
945     /* sorry, we don't have a server alive for this ext */
946     r->http_status = 503; /* Service Unavailable */
947     r->handler_module = NULL;
948 
949     /* only send the 'no handler' once */
950     if (!extension->note_is_sent) {
951         extension->note_is_sent = 1;
952         log_error(r->conf.errh, __FILE__, __LINE__,
953           "all handlers for %s?%.*s on %s are down.",
954           r->uri.path.ptr, BUFFER_INTLEN_PTR(&r->uri.query),
955           extension->key.ptr);
956     }
957 
958     return NULL;
959 }
960 
961 static int gw_establish_connection(request_st * const r, gw_host *host, gw_proc *proc, pid_t pid, int gw_fd, int debug) {
962     if (-1 == connect(gw_fd, proc->saddr, proc->saddrlen)) {
963         if (errno == EINPROGRESS || errno == EALREADY || errno == EINTR) {
964             if (debug > 2) {
965                 log_error(r->conf.errh, __FILE__, __LINE__,
966                   "connect delayed; will continue later: %s",
967                   proc->connection_name->ptr);
968             }
969 
970             return 1;
971         } else {
972             gw_proc_connect_error(r, host, proc, pid, errno, debug);
973             return -1;
974         }
975     }
976 
977     if (debug > 1) {
978         log_error(r->conf.errh, __FILE__, __LINE__,
979           "connect succeeded: %d", gw_fd);
980     }
981 
982     return 0;
983 }
984 
985 static void gw_restart_dead_procs(gw_host * const host, log_error_st * const errh, const int debug, const int trigger) {
986     for (gw_proc *proc = host->first; proc; proc = proc->next) {
987         if (debug > 2) {
988             log_error(errh, __FILE__, __LINE__,
989               "proc: %s %d %d %d %d", proc->connection_name->ptr,
990               proc->state, proc->is_local, proc->load, proc->pid);
991         }
992 
993         switch (proc->state) {
994         case PROC_STATE_RUNNING:
995             break;
996         case PROC_STATE_OVERLOADED:
997             gw_proc_check_enable(host, proc, errh);
998             break;
999         case PROC_STATE_KILLED:
1000             if (trigger && ++proc->disabled_until > 4) {
1001                 int sig = (proc->disabled_until <= 8)
1002                   ? host->kill_signal
1003                   : proc->disabled_until <= 16 ? SIGTERM : SIGKILL;
1004                 kill(proc->pid, sig);
1005             }
1006             break;
1007         case PROC_STATE_DIED_WAIT_FOR_PID:
1008             /*(state should not happen in workers if server.max-worker > 0)*/
1009             /*(if PROC_STATE_DIED_WAIT_FOR_PID is used in future, might want
1010              * to save proc->disabled_until before gw_proc_waitpid() since
1011              * gw_proc_waitpid will set proc->disabled_until to log_epoch_secs,
1012              * and so process will not be restarted below until one sec later)*/
1013             if (0 == gw_proc_waitpid(host, proc, errh)) {
1014                 gw_proc_check_enable(host, proc, errh);
1015             }
1016 
1017             if (proc->state != PROC_STATE_DIED) break;
1018             /* fall through *//*(we have a dead proc now)*/
1019 
1020         case PROC_STATE_DIED:
1021             /* local procs get restarted by us,
1022              * remote ones hopefully by the admin */
1023 
1024             if (!buffer_string_is_empty(host->bin_path)) {
1025                 /* we still have connections bound to this proc,
1026                  * let them terminate first */
1027                 if (proc->load != 0) break;
1028 
1029                 /* avoid spinning if child exits too quickly */
1030                 if (proc->disabled_until >= log_epoch_secs) break;
1031 
1032                 /* restart the child */
1033 
1034                 if (debug) {
1035                     log_error(errh, __FILE__, __LINE__,
1036                       "--- gw spawning"
1037                       "\n\tsocket %s"
1038                       "\n\tcurrent: 1 / %u",
1039                       proc->connection_name->ptr, host->max_procs);
1040                 }
1041 
1042                 if (gw_spawn_connection(host, proc, errh, debug)) {
1043                     log_error(errh, __FILE__, __LINE__,
1044                       "ERROR: spawning gw failed.");
1045                 }
1046             } else {
1047                 gw_proc_check_enable(host, proc, errh);
1048             }
1049             break;
1050         }
1051     }
1052 }
1053 
1054 
1055 
1056 
1057 #include "base.h"
1058 #include "connections.h"
1059 #include "response.h"
1060 
1061 
1062 /* ok, we need a prototype */
1063 static handler_t gw_handle_fdevent(void *ctx, int revents);
1064 
1065 
1066 static gw_handler_ctx * handler_ctx_init(size_t sz) {
1067     gw_handler_ctx *hctx = calloc(1, 0 == sz ? sizeof(*hctx) : sz);
1068     force_assert(hctx);
1069 
1070     /*hctx->response = chunk_buffer_acquire();*//*(allocated when needed)*/
1071 
1072     hctx->request_id = 0;
1073     hctx->gw_mode = GW_RESPONDER;
1074     hctx->state = GW_STATE_INIT;
1075     hctx->proc = NULL;
1076 
1077     hctx->fd = -1;
1078 
1079     hctx->reconnects = 0;
1080     hctx->send_content_body = 1;
1081 
1082     /*hctx->rb = chunkqueue_init();*//*(allocated when needed)*/
1083     hctx->wb = chunkqueue_init();
1084     hctx->wb_reqlen = 0;
1085 
1086     return hctx;
1087 }
1088 
1089 static void handler_ctx_free(gw_handler_ctx *hctx) {
1090     /* caller MUST have called gw_backend_close(hctx, r) if necessary */
1091     if (hctx->handler_ctx_free) hctx->handler_ctx_free(hctx);
1092     chunk_buffer_release(hctx->response);
1093 
1094     chunkqueue_free(hctx->rb);
1095     chunkqueue_free(hctx->wb);
1096 
1097     free(hctx);
1098 }
1099 
1100 static void handler_ctx_clear(gw_handler_ctx *hctx) {
1101     /* caller MUST have called gw_backend_close(hctx, r) if necessary */
1102 
1103     hctx->proc = NULL;
1104     hctx->host = NULL;
1105     hctx->ext  = NULL;
1106     /*hctx->ext_auth is intentionally preserved to flag prior authorizer*/
1107 
1108     hctx->gw_mode = GW_RESPONDER;
1109     hctx->state = GW_STATE_INIT;
1110     /*hctx->state_timestamp = 0;*//*(unused; left as-is)*/
1111 
1112     if (hctx->rb) chunkqueue_reset(hctx->rb);
1113     if (hctx->wb) chunkqueue_reset(hctx->wb);
1114     hctx->wb_reqlen = 0;
1115 
1116     if (hctx->response) buffer_clear(hctx->response);
1117 
1118     hctx->fd = -1;
1119     hctx->reconnects = 0;
1120     hctx->request_id = 0;
1121     hctx->send_content_body = 1;
1122 
1123     /*plugin_config conf;*//*(no need to reset for same request)*/
1124 
1125     /*hctx->r           = NULL;*//*(no need to reset for same request)*/
1126     /*hctx->plugin_data = NULL;*//*(no need to reset for same request)*/
1127 }
1128 
1129 
1130 void * gw_init(void) {
1131     return calloc(1, sizeof(gw_plugin_data));
1132 }
1133 
1134 
1135 void gw_plugin_config_free(gw_plugin_config *s) {
1136     gw_exts *exts = s->exts;
1137     if (exts) {
1138         for (uint32_t j = 0; j < exts->used; ++j) {
1139             gw_extension *ex = exts->exts+j;
1140             for (uint32_t n = 0; n < ex->used; ++n) {
1141                 gw_proc *proc;
1142                 gw_host *host = ex->hosts[n];
1143 
1144                 for (proc = host->first; proc; proc = proc->next) {
1145                     if (proc->pid > 0) {
1146                         kill(proc->pid, host->kill_signal);
1147                     }
1148 
1149                     if (proc->is_local &&
1150                         !buffer_string_is_empty(proc->unixsocket)) {
1151                         unlink(proc->unixsocket->ptr);
1152                     }
1153                 }
1154 
1155                 for (proc = host->unused_procs; proc; proc = proc->next) {
1156                     if (proc->pid > 0) {
1157                         kill(proc->pid, host->kill_signal);
1158                     }
1159                     if (proc->is_local &&
1160                         !buffer_string_is_empty(proc->unixsocket)) {
1161                         unlink(proc->unixsocket->ptr);
1162                     }
1163                 }
1164             }
1165         }
1166 
1167         gw_extensions_free(s->exts);
1168         gw_extensions_free(s->exts_auth);
1169         gw_extensions_free(s->exts_resp);
1170     }
1171     free(s);
1172 }
1173 
1174 void gw_free(void *p_d) {
1175     gw_plugin_data * const p = p_d;
1176     if (NULL == p->cvlist) return;
1177     /* (init i to 0 if global context; to 1 to skip empty global context) */
1178     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
1179         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
1180         for (; -1 != cpv->k_id; ++cpv) {
1181             switch (cpv->k_id) {
1182               case 0: /* xxxxx.server */
1183                 if (cpv->vtype == T_CONFIG_LOCAL)
1184                     gw_plugin_config_free(cpv->v.v);
1185                 break;
1186               default:
1187                 break;
1188             }
1189         }
1190     }
1191 }
1192 
1193 void gw_exts_clear_check_local(gw_exts *exts) {
1194     for (uint32_t j = 0; j < exts->used; ++j) {
1195         gw_extension *ex = exts->exts+j;
1196         for (uint32_t n = 0; n < ex->used; ++n) {
1197             ex->hosts[n]->check_local = 0;
1198         }
1199     }
1200 }
1201 
1202 int gw_set_defaults_backend(server *srv, gw_plugin_data *p, const array *a, gw_plugin_config *s, int sh_exec, const char *cpkkey) {
1203     /* per-module plugin_config MUST have common "base class" gw_plugin_config*/
1204     /* per-module plugin_data MUST have pointer-compatible common "base class"
1205      * with gw_plugin_data (stemming from gw_plugin_config compatibility) */
1206 
1207     static const config_plugin_keys_t cpk[] = {
1208       { CONST_STR_LEN("host"),
1209         T_CONFIG_STRING,
1210         T_CONFIG_SCOPE_CONNECTION }
1211      ,{ CONST_STR_LEN("port"),
1212         T_CONFIG_SHORT,
1213         T_CONFIG_SCOPE_CONNECTION }
1214      ,{ CONST_STR_LEN("socket"),
1215         T_CONFIG_STRING,
1216         T_CONFIG_SCOPE_CONNECTION }
1217      ,{ CONST_STR_LEN("listen-backlog"),
1218         T_CONFIG_INT,
1219         T_CONFIG_SCOPE_CONNECTION }
1220      ,{ CONST_STR_LEN("bin-path"),
1221         T_CONFIG_STRING,
1222         T_CONFIG_SCOPE_CONNECTION }
1223      ,{ CONST_STR_LEN("kill-signal"),
1224         T_CONFIG_SHORT,
1225         T_CONFIG_SCOPE_CONNECTION }
1226      ,{ CONST_STR_LEN("check-local"),
1227         T_CONFIG_BOOL,
1228         T_CONFIG_SCOPE_CONNECTION }
1229      ,{ CONST_STR_LEN("mode"),
1230         T_CONFIG_STRING,
1231         T_CONFIG_SCOPE_CONNECTION }
1232      ,{ CONST_STR_LEN("docroot"),
1233         T_CONFIG_STRING,
1234         T_CONFIG_SCOPE_CONNECTION }
1235      ,{ CONST_STR_LEN("min-procs"),
1236         T_CONFIG_SHORT,
1237         T_CONFIG_SCOPE_CONNECTION }
1238      ,{ CONST_STR_LEN("max-procs"),
1239         T_CONFIG_SHORT,
1240         T_CONFIG_SCOPE_CONNECTION }
1241      ,{ CONST_STR_LEN("max-load-per-proc"),
1242         T_CONFIG_SHORT,
1243         T_CONFIG_SCOPE_CONNECTION }
1244      ,{ CONST_STR_LEN("idle-timeout"),
1245         T_CONFIG_SHORT,
1246         T_CONFIG_SCOPE_CONNECTION }
1247      ,{ CONST_STR_LEN("disable-time"),
1248         T_CONFIG_SHORT,
1249         T_CONFIG_SCOPE_CONNECTION }
1250      ,{ CONST_STR_LEN("bin-environment"),
1251         T_CONFIG_ARRAY_KVSTRING,
1252         T_CONFIG_SCOPE_CONNECTION }
1253      ,{ CONST_STR_LEN("bin-copy-environment"),
1254         T_CONFIG_ARRAY_VLIST,
1255         T_CONFIG_SCOPE_CONNECTION }
1256      ,{ CONST_STR_LEN("broken-scriptfilename"),
1257         T_CONFIG_BOOL,
1258         T_CONFIG_SCOPE_CONNECTION }
1259      ,{ CONST_STR_LEN("strip-request-uri"),
1260         T_CONFIG_STRING,
1261         T_CONFIG_SCOPE_CONNECTION }
1262      ,{ CONST_STR_LEN("fix-root-scriptname"),
1263         T_CONFIG_BOOL,
1264         T_CONFIG_SCOPE_CONNECTION }
1265      ,{ CONST_STR_LEN("allow-x-send-file"),
1266         T_CONFIG_BOOL,
1267         T_CONFIG_SCOPE_CONNECTION }
1268      ,{ CONST_STR_LEN("x-sendfile"),
1269         T_CONFIG_BOOL,
1270         T_CONFIG_SCOPE_CONNECTION }
1271      ,{ CONST_STR_LEN("x-sendfile-docroot"),
1272         T_CONFIG_ARRAY_VLIST,
1273         T_CONFIG_SCOPE_CONNECTION }
1274      ,{ CONST_STR_LEN("tcp-fin-propagate"),
1275         T_CONFIG_BOOL,
1276         T_CONFIG_SCOPE_CONNECTION }
1277      ,{ NULL, 0,
1278         T_CONFIG_UNSET,
1279         T_CONFIG_SCOPE_UNSET }
1280     };
1281 
1282     gw_host *host = NULL;
1283 
1284     p->srv_pid = srv->pid;
1285 
1286     s->exts      = gw_extensions_init();
1287     s->exts_auth = gw_extensions_init();
1288     s->exts_resp = gw_extensions_init();
1289     /*s->balance = GW_BALANCE_LEAST_CONNECTION;*//*(default)*/
1290 
1291     /*
1292      * gw.server = ( "<ext>" => ( ... ),
1293      *               "<ext>" => ( ... ) )
1294      */
1295 
1296     for (uint32_t j = 0; j < a->used; ++j) {
1297         data_array *da_ext = (data_array *)a->data[j];
1298 
1299         /*
1300          * da_ext->key == name of the extension
1301          */
1302 
1303         /*
1304          * gw.server = ( "<ext>" =>
1305          *                     ( "<host>" => ( ... ),
1306          *                       "<host>" => ( ... )
1307          *                     ),
1308          *               "<ext>" => ... )
1309          */
1310 
1311         for (uint32_t n = 0; n < da_ext->value.used; ++n) {
1312             data_array * const da_host = (data_array *)da_ext->value.data[n];
1313 
1314             if (da_host->type != TYPE_ARRAY
1315                 || !array_is_kvany(&da_host->value)){
1316                 log_error(srv->errh, __FILE__, __LINE__,
1317                   "unexpected value for gw.server near [%s](string); "
1318                   "expected ( \"ext\" => "
1319                   "( \"backend-label\" => ( \"key\" => \"value\" )))",
1320                   da_host->key.ptr ? da_host->key.ptr : "");
1321                 goto error;
1322             }
1323 
1324             config_plugin_value_t cvlist[sizeof(cpk)/sizeof(cpk[0])+1];
1325             memset(cvlist, 0, sizeof(cvlist));
1326 
1327             array *ca = &da_host->value;
1328             if (!config_plugin_values_init_block(srv, ca, cpk, cpkkey, cvlist))
1329                 goto error;
1330 
1331             unsigned short host_mode = GW_RESPONDER;
1332 
1333             host = gw_host_init();
1334             host->id = &da_host->key;
1335             host->check_local  = 1;
1336             host->min_procs    = 4;
1337             host->max_procs    = 4;
1338             host->max_load_per_proc = 1;
1339             host->idle_timeout = 60;
1340             host->disable_time = 1;
1341             host->break_scriptfilename_for_php = 0;
1342             host->kill_signal = SIGTERM;
1343             host->fix_root_path_name = 0;
1344             host->listen_backlog = 1024;
1345             host->xsendfile_allow = 0;
1346             host->refcount = 0;
1347 
1348             config_plugin_value_t *cpv = cvlist;
1349             for (; -1 != cpv->k_id; ++cpv) {
1350                 switch (cpv->k_id) {
1351                   case 0: /* host */
1352                     host->host = cpv->v.b;
1353                     break;
1354                   case 1: /* port */
1355                     host->port = cpv->v.shrt;
1356                     break;
1357                   case 2: /* socket */
1358                     host->unixsocket = cpv->v.b;
1359                     break;
1360                   case 3: /* listen-backlog */
1361                     host->listen_backlog = cpv->v.u;
1362                     break;
1363                   case 4: /* bin-path */
1364                     host->bin_path = cpv->v.b;
1365                     break;
1366                   case 5: /* kill-signal */
1367                     host->kill_signal = cpv->v.shrt;
1368                     break;
1369                   case 6: /* check-local */
1370                     host->check_local = (0 != cpv->v.u);
1371                     break;
1372                   case 7: /* mode */
1373                     if (!buffer_string_is_empty(cpv->v.b)) {
1374                         const buffer *b = cpv->v.b;
1375                         if (buffer_eq_slen(b, CONST_STR_LEN("responder")))
1376                             host_mode = GW_RESPONDER;
1377                         else if (buffer_eq_slen(b, CONST_STR_LEN("authorizer")))
1378                             host_mode = GW_AUTHORIZER;
1379                         else
1380                             log_error(srv->errh, __FILE__, __LINE__,
1381                               "WARNING: unknown gw mode: %s "
1382                               "(ignored, mode set to responder)", b->ptr);
1383                     }
1384                     break;
1385                   case 8: /* docroot */
1386                     host->docroot = cpv->v.b;
1387                     break;
1388                   case 9: /* min-procs */
1389                     host->min_procs = cpv->v.shrt;
1390                     break;
1391                   case 10:/* max-procs */
1392                     host->max_procs = cpv->v.shrt;
1393                     break;
1394                   case 11:/* max-load-per-proc */
1395                     host->max_load_per_proc = cpv->v.shrt;
1396                     break;
1397                   case 12:/* idle-timeout */
1398                     host->idle_timeout = cpv->v.shrt;
1399                     break;
1400                   case 13:/* disable-time */
1401                     host->disable_time = cpv->v.shrt;
1402                     break;
1403                   case 14:/* bin-environment */
1404                     host->bin_env = cpv->v.a;
1405                     break;
1406                   case 15:/* bin-copy-environment */
1407                     host->bin_env_copy = cpv->v.a;
1408                     break;
1409                   case 16:/* broken-scriptfilename */
1410                     host->break_scriptfilename_for_php = (0 != cpv->v.u);
1411                     break;
1412                   case 17:/* strip-request-uri */
1413                     host->strip_request_uri = cpv->v.b;
1414                     break;
1415                   case 18:/* fix-root-scriptname */
1416                     host->fix_root_path_name = (0 != cpv->v.u);
1417                     break;
1418                   case 19:/* allow-x-send-file */
1419                     host->xsendfile_allow = (0 != cpv->v.u);
1420                     break;
1421                   case 20:/* x-sendfile */
1422                     host->xsendfile_allow = (0 != cpv->v.u);
1423                     break;
1424                   case 21:/* x-sendfile-docroot */
1425                     host->xsendfile_docroot = cpv->v.a;
1426                     if (cpv->v.a->used) {
1427                         for (uint32_t k = 0; k < cpv->v.a->used; ++k) {
1428                             data_string *ds = (data_string *)cpv->v.a->data[k];
1429                             if (ds->type != TYPE_STRING) {
1430                                 log_error(srv->errh, __FILE__, __LINE__,
1431                                   "unexpected type for x-sendfile-docroot; "
1432                                   "expected: \"x-sendfile-docroot\" => "
1433                                   "( \"/allowed/path\", ... )");
1434                                 goto error;
1435                             }
1436                             if (ds->value.ptr[0] != '/') {
1437                                 log_error(srv->errh, __FILE__, __LINE__,
1438                                   "x-sendfile-docroot paths must begin with "
1439                                   "'/'; invalid: \"%s\"", ds->value.ptr);
1440                                 goto error;
1441                             }
1442                             buffer_path_simplify(&ds->value, &ds->value);
1443                             buffer_append_slash(&ds->value);
1444                         }
1445                     }
1446                     break;
1447                   case 22:/* tcp-fin-propagate */
1448                     host->tcp_fin_propagate = (0 != cpv->v.u);
1449                     break;
1450                   default:
1451                     break;
1452                 }
1453             }
1454 
1455             for (uint32_t m = 0; m < da_host->value.used; ++m) {
1456                 if (NULL != strchr(da_host->value.data[m]->key.ptr, '_')) {
1457                     log_error(srv->errh, __FILE__, __LINE__,
1458                       "incorrect directive contains underscore ('_') instead of dash ('-'): %s",
1459                       da_host->value.data[m]->key.ptr);
1460                 }
1461             }
1462 
1463             if ((!buffer_string_is_empty(host->host) || host->port)
1464                 && !buffer_string_is_empty(host->unixsocket)) {
1465                 log_error(srv->errh, __FILE__, __LINE__,
1466                   "either host/port or socket have to be set in: "
1467                   "%s = (%s => (%s ( ...", cpkkey, da_ext->key.ptr,
1468                   da_host->key.ptr);
1469 
1470                 goto error;
1471             }
1472 
1473             if (!buffer_string_is_empty(host->host) && *host->host->ptr == '/'
1474                 && buffer_string_is_empty(host->unixsocket)) {
1475                 host->unixsocket = host->host;
1476             }
1477 
1478             if (!buffer_string_is_empty(host->unixsocket)) {
1479                 /* unix domain socket */
1480                 struct sockaddr_un un;
1481 
1482                 if (buffer_string_length(host->unixsocket) + 1 > sizeof(un.sun_path) - 2) {
1483                     log_error(srv->errh, __FILE__, __LINE__,
1484                       "unixsocket is too long in: %s = (%s => (%s ( ...",
1485                       cpkkey, da_ext->key.ptr, da_host->key.ptr);
1486 
1487                     goto error;
1488                 }
1489 
1490                 if (!buffer_string_is_empty(host->bin_path)) {
1491                     gw_host *duplicate = unixsocket_is_dup(p, host->unixsocket);
1492                     if (NULL != duplicate) {
1493                         if (!buffer_is_equal(host->bin_path, duplicate->bin_path)) {
1494                             log_error(srv->errh, __FILE__, __LINE__,
1495                               "duplicate unixsocket path: %s",
1496                               host->unixsocket->ptr);
1497                             goto error;
1498                         }
1499                         gw_host_free(host);
1500                         host = duplicate;
1501                         ++host->refcount;
1502                     }
1503                 }
1504 
1505                 host->family = AF_UNIX;
1506             } else {
1507                 /* tcp/ip */
1508 
1509                 if (buffer_string_is_empty(host->host) &&
1510                     buffer_string_is_empty(host->bin_path)) {
1511                     log_error(srv->errh, __FILE__, __LINE__,
1512                       "host or bin-path have to be set in: "
1513                       "%s = (%s => (%s ( ...", cpkkey, da_ext->key.ptr,
1514                       da_host->key.ptr);
1515 
1516                     goto error;
1517                 } else if (0 == host->port) {
1518                     host->port = 80;
1519                 }
1520 
1521                 if (buffer_string_is_empty(host->host)) {
1522                     static const buffer lhost = {CONST_STR_LEN("127.0.0.1"), 0};
1523                     host->host = &lhost;
1524                 }
1525 
1526                 host->family = (NULL != strchr(host->host->ptr, ':'))
1527                   ? AF_INET6
1528                   : AF_INET;
1529             }
1530 
1531             if (host->refcount) {
1532                 /* already init'd; skip spawning */
1533             } else if (!buffer_string_is_empty(host->bin_path)) {
1534                 /* a local socket + self spawning */
1535                 struct stat st;
1536                 parse_binpath(&host->args, host->bin_path);
1537                 if (0 != stat(host->args.ptr[0], &st) || !S_ISREG(st.st_mode)
1538                     || !(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
1539                     log_error(srv->errh, __FILE__, __LINE__,
1540                       "invalid \"bin-path\" => \"%s\" "
1541                       "(check that file exists, is regular file, "
1542                       "and is executable by lighttpd)", host->bin_path->ptr);
1543                 }
1544 
1545                 if (sh_exec) {
1546                     /*(preserve prior behavior for SCGI exec of command)*/
1547                     /*(admin should really prefer to put
1548                      * any complex command into a script)*/
1549                     for (uint32_t m = 0; m < host->args.used; ++m)
1550                         free(host->args.ptr[m]);
1551                     free(host->args.ptr);
1552 
1553                     host->args.ptr = calloc(4, sizeof(char *));
1554                     force_assert(host->args.ptr);
1555                     host->args.used = 3;
1556                     host->args.size = 4;
1557                     host->args.ptr[0] = malloc(sizeof("/bin/sh"));
1558                     force_assert(host->args.ptr[0]);
1559                     memcpy(host->args.ptr[0], "/bin/sh", sizeof("/bin/sh"));
1560                     host->args.ptr[1] = malloc(sizeof("-c"));
1561                     force_assert(host->args.ptr[1]);
1562                     memcpy(host->args.ptr[1], "-c", sizeof("-c"));
1563                     host->args.ptr[2] =
1564                       malloc(sizeof("exec ")-1
1565                              + buffer_string_length(host->bin_path) + 1);
1566                     force_assert(host->args.ptr[2]);
1567                     memcpy(host->args.ptr[2], "exec ", sizeof("exec ")-1);
1568                     memcpy(host->args.ptr[2]+sizeof("exec ")-1,
1569                            host->bin_path->ptr,
1570                            buffer_string_length(host->bin_path)+1);
1571                     host->args.ptr[3] = NULL;
1572                 }
1573 
1574                 if (host->min_procs > host->max_procs)
1575                     host->min_procs = host->max_procs;
1576                 if (host->min_procs!= host->max_procs
1577                     && 0 != srv->srvconf.max_worker) {
1578                     host->min_procs = host->max_procs;
1579                     log_error(srv->errh, __FILE__, __LINE__,
1580                       "adaptive backend spawning disabled "
1581                       "(server.max_worker is non-zero)");
1582                 }
1583                 if (host->max_load_per_proc < 1)
1584                     host->max_load_per_proc = 0;
1585 
1586                 if (s->debug) {
1587                     log_error(srv->errh, __FILE__, __LINE__,
1588                       "--- gw spawning local"
1589                       "\n\tproc: %s"
1590                       "\n\tport: %hu"
1591                       "\n\tsocket %s"
1592                       "\n\tmin-procs: %d"
1593                       "\n\tmax-procs: %d",
1594                       host->bin_path->ptr,
1595                       host->port,
1596                       host->unixsocket && host->unixsocket->ptr
1597                         ? host->unixsocket->ptr
1598                         : "",
1599                       host->min_procs,
1600                       host->max_procs);
1601                 }
1602 
1603                 for (uint32_t pno = 0; pno < host->min_procs; ++pno) {
1604                     gw_proc *proc = gw_proc_init();
1605                     proc->id = host->num_procs++;
1606                     host->max_id++;
1607 
1608                     if (buffer_string_is_empty(host->unixsocket)) {
1609                         proc->port = host->port + pno;
1610                     } else {
1611                         buffer_copy_buffer(proc->unixsocket, host->unixsocket);
1612                         buffer_append_string_len(proc->unixsocket,
1613                                                  CONST_STR_LEN("-"));
1614                         buffer_append_int(proc->unixsocket, pno);
1615                     }
1616 
1617                     if (s->debug) {
1618                         log_error(srv->errh, __FILE__, __LINE__,
1619                           "--- gw spawning"
1620                           "\n\tport: %hu"
1621                           "\n\tsocket %s"
1622                           "\n\tcurrent: %u / %u",
1623                           host->port,
1624                           host->unixsocket && host->unixsocket->ptr
1625                             ? host->unixsocket->ptr
1626                             : "",
1627                           pno, host->max_procs);
1628                     }
1629 
1630                     if (0 != gw_proc_sockaddr_init(host, proc, srv->errh)) {
1631                         gw_proc_free(proc);
1632                         goto error;
1633                     }
1634 
1635                     if (!srv->srvconf.preflight_check
1636                         && gw_spawn_connection(host, proc, srv->errh, s->debug)) {
1637                         log_error(srv->errh, __FILE__, __LINE__,
1638                           "[ERROR]: spawning gw failed.");
1639                         gw_proc_free(proc);
1640                         goto error;
1641                     }
1642 
1643                     gw_status_init(host, proc);
1644 
1645                     proc->next = host->first;
1646                     if (host->first) host->first->prev = proc;
1647 
1648                     host->first = proc;
1649                 }
1650             } else {
1651                 gw_proc *proc;
1652 
1653                 proc = gw_proc_init();
1654                 proc->id = host->num_procs++;
1655                 host->max_id++;
1656                 gw_proc_set_state(host, proc, PROC_STATE_RUNNING);
1657 
1658                 if (buffer_string_is_empty(host->unixsocket)) {
1659                     proc->port = host->port;
1660                 } else {
1661                     buffer_copy_buffer(proc->unixsocket, host->unixsocket);
1662                 }
1663 
1664                 gw_status_init(host, proc);
1665 
1666                 host->first = proc;
1667 
1668                 host->min_procs = 1;
1669                 host->max_procs = 1;
1670 
1671                 if (0 != gw_proc_sockaddr_init(host, proc, srv->errh)) goto error;
1672             }
1673 
1674             /* s->exts is list of exts -> hosts
1675              * s->exts now used as combined list
1676              *   of authorizer and responder hosts (for backend maintenance)
1677              * s->exts_auth is list of exts -> authorizer hosts
1678              * s->exts_resp is list of exts -> responder hosts
1679              * For each path/extension:
1680              * there may be an independent GW_AUTHORIZER and GW_RESPONDER
1681              * (The GW_AUTHORIZER and GW_RESPONDER could be handled by the same
1682              *  host, and an admin might want to do that for large uploads,
1683              *  since GW_AUTHORIZER runs prior to receiving (potentially large)
1684              *  request body from client and can authorizer or deny request
1685              *  prior to receiving the full upload)
1686              */
1687             gw_extension_insert(s->exts, &da_ext->key, host);
1688 
1689             if (host_mode == GW_AUTHORIZER) {
1690                 ++host->refcount;
1691                 gw_extension_insert(s->exts_auth, &da_ext->key, host);
1692             } else if (host_mode == GW_RESPONDER) {
1693                 ++host->refcount;
1694                 gw_extension_insert(s->exts_resp, &da_ext->key, host);
1695             } /*(else should have been rejected above)*/
1696 
1697             host = NULL;
1698         }
1699     }
1700 
1701     return 1;
1702 
1703 error:
1704     if (NULL != host) gw_host_free(host);
1705     return 0;
1706 }
1707 
1708 int gw_get_defaults_balance(server *srv, const buffer *b) {
1709     if (buffer_string_is_empty(b))
1710         return GW_BALANCE_LEAST_CONNECTION;
1711     if (buffer_eq_slen(b, CONST_STR_LEN("fair")))
1712         return GW_BALANCE_LEAST_CONNECTION;
1713     if (buffer_eq_slen(b, CONST_STR_LEN("least-connection")))
1714         return GW_BALANCE_LEAST_CONNECTION;
1715     if (buffer_eq_slen(b, CONST_STR_LEN("round-robin")))
1716         return GW_BALANCE_RR;
1717     if (buffer_eq_slen(b, CONST_STR_LEN("hash")))
1718         return GW_BALANCE_HASH;
1719     if (buffer_eq_slen(b, CONST_STR_LEN("sticky")))
1720         return GW_BALANCE_STICKY;
1721 
1722     log_error(srv->errh, __FILE__, __LINE__,
1723       "xxxxx.balance has to be one of: "
1724       "least-connection, round-robin, hash, sticky, but not: %s", b->ptr);
1725     return GW_BALANCE_LEAST_CONNECTION;
1726 }
1727 
1728 
1729 static void gw_set_state(gw_handler_ctx *hctx, gw_connection_state_t state) {
1730     hctx->state = state;
1731     /*hctx->state_timestamp = log_epoch_secs;*/
1732 }
1733 
1734 
1735 void gw_set_transparent(gw_handler_ctx *hctx) {
1736     if (AF_UNIX != hctx->host->family) {
1737         if (-1 == fdevent_set_tcp_nodelay(hctx->fd, 1)) {
1738             /*(error, but not critical)*/
1739         }
1740     }
1741     hctx->wb_reqlen = -1;
1742     gw_set_state(hctx, GW_STATE_WRITE);
1743 }
1744 
1745 
1746 static void gw_backend_close(gw_handler_ctx * const hctx, request_st * const r) {
1747     if (hctx->fd >= 0) {
1748         fdevent_fdnode_event_del(hctx->ev, hctx->fdn);
1749         /*fdevent_unregister(ev, hctx->fd);*//*(handled below)*/
1750         fdevent_sched_close(hctx->ev, hctx->fd, 1);
1751         hctx->fdn = NULL;
1752         hctx->fd = -1;
1753     }
1754 
1755     if (hctx->host) {
1756         if (hctx->proc) {
1757             gw_proc_release(hctx->host, hctx->proc, hctx->conf.debug,
1758                             r->conf.errh);
1759             hctx->proc = NULL;
1760         }
1761 
1762         gw_host_reset(hctx->host);
1763         hctx->host = NULL;
1764     }
1765 }
1766 
1767 static void gw_connection_close(gw_handler_ctx * const hctx, request_st * const r) {
1768     gw_plugin_data *p = hctx->plugin_data;
1769 
1770     gw_backend_close(hctx, r);
1771     handler_ctx_free(hctx);
1772     r->plugin_ctx[p->id] = NULL;
1773 
1774     if (r->handler_module == p->self) {
1775         http_response_backend_done(r);
1776     }
1777 }
1778 
1779 static handler_t gw_reconnect(gw_handler_ctx * const hctx, request_st * const r) {
1780     gw_backend_close(hctx, r);
1781 
1782     hctx->host = gw_host_get(r,hctx->ext,hctx->conf.balance,hctx->conf.debug);
1783     if (NULL == hctx->host) return HANDLER_FINISHED;
1784 
1785     gw_host_assign(hctx->host);
1786     hctx->request_id = 0;
1787     hctx->opts.xsendfile_allow = hctx->host->xsendfile_allow;
1788     hctx->opts.xsendfile_docroot = hctx->host->xsendfile_docroot;
1789     gw_set_state(hctx, GW_STATE_INIT);
1790     return HANDLER_COMEBACK;
1791 }
1792 
1793 
1794 handler_t gw_connection_reset(request_st * const r, void *p_d) {
1795     gw_plugin_data *p = p_d;
1796     gw_handler_ctx *hctx = r->plugin_ctx[p->id];
1797     if (hctx) gw_connection_close(hctx, r);
1798 
1799     return HANDLER_GO_ON;
1800 }
1801 
1802 
1803 static void gw_conditional_tcp_fin(gw_handler_ctx * const hctx, request_st * const r) {
1804     /*assert(r->conf.stream_request_body & FDEVENT_STREAM_REQUEST_TCP_FIN);*/
1805     if (!chunkqueue_is_empty(hctx->wb)) return;
1806     if (!hctx->host->tcp_fin_propagate) return;
1807     if (hctx->gw_mode == GW_AUTHORIZER) return;
1808     if (r->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BACKEND_SHUT_WR)
1809         return;
1810 
1811     /* propagate shutdown SHUT_WR to backend if TCP half-close on con->fd */
1812     r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_BACKEND_SHUT_WR;
1813     r->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
1814     r->con->is_readable = 0;
1815     shutdown(hctx->fd, SHUT_WR);
1816     fdevent_fdnode_event_clr(hctx->ev, hctx->fdn, FDEVENT_OUT);
1817 }
1818 
1819 static handler_t gw_write_request(gw_handler_ctx * const hctx, request_st * const r) {
1820     switch(hctx->state) {
1821     case GW_STATE_INIT:
1822         /* do we have a running process for this host (max-procs) ? */
1823         hctx->proc = NULL;
1824 
1825         for (gw_proc *proc = hctx->host->first; proc; proc = proc->next) {
1826              if (proc->state == PROC_STATE_RUNNING) {
1827                  hctx->proc = proc;
1828                  break;
1829              }
1830         }
1831 
1832         /* all children are dead */
1833         if (hctx->proc == NULL) {
1834             return HANDLER_ERROR;
1835         }
1836 
1837         /* check the other procs if they have a lower load */
1838         for (gw_proc *proc = hctx->proc->next; proc; proc = proc->next) {
1839             if (proc->state != PROC_STATE_RUNNING) continue;
1840             if (proc->load < hctx->proc->load) hctx->proc = proc;
1841         }
1842 
1843         gw_proc_load_inc(hctx->host, hctx->proc);
1844 
1845         hctx->fd = fdevent_socket_nb_cloexec(hctx->host->family,SOCK_STREAM,0);
1846         if (-1 == hctx->fd) {
1847             log_error_st * const errh = r->conf.errh;
1848             if (errno == EMFILE || errno == EINTR) {
1849                 log_error(errh, __FILE__, __LINE__,
1850                   "wait for fd at connection: %d", r->con->fd);
1851                 return HANDLER_WAIT_FOR_FD;
1852             }
1853 
1854             log_perror(errh, __FILE__, __LINE__,
1855               "socket failed %d %d",r->con->srv->cur_fds,r->con->srv->max_fds);
1856             return HANDLER_ERROR;
1857         }
1858 
1859         ++r->con->srv->cur_fds;
1860 
1861         hctx->fdn = fdevent_register(hctx->ev,hctx->fd,gw_handle_fdevent,hctx);
1862 
1863         if (hctx->proc->is_local) {
1864             hctx->pid = hctx->proc->pid;
1865         }
1866 
1867         switch (gw_establish_connection(r, hctx->host, hctx->proc, hctx->pid,
1868                                         hctx->fd, hctx->conf.debug)) {
1869         case 1: /* connection is in progress */
1870             fdevent_fdnode_event_set(hctx->ev, hctx->fdn, FDEVENT_OUT);
1871             gw_set_state(hctx, GW_STATE_CONNECT_DELAYED);
1872             return HANDLER_WAIT_FOR_EVENT;
1873         case -1:/* connection error */
1874             return HANDLER_ERROR;
1875         case 0: /* everything is ok, go on */
1876             hctx->reconnects = 0;
1877             break;
1878         }
1879         /* fall through */
1880     case GW_STATE_CONNECT_DELAYED:
1881         if (hctx->state == GW_STATE_CONNECT_DELAYED) { /*(not GW_STATE_INIT)*/
1882             int socket_error = fdevent_connect_status(hctx->fd);
1883             if (socket_error != 0) {
1884                 gw_proc_connect_error(r, hctx->host, hctx->proc, hctx->pid,
1885                                       socket_error, hctx->conf.debug);
1886                 return HANDLER_ERROR;
1887             }
1888             /* go on with preparing the request */
1889         }
1890 
1891         gw_proc_connect_success(hctx->host, hctx->proc, hctx->conf.debug, r);
1892 
1893         gw_set_state(hctx, GW_STATE_PREPARE_WRITE);
1894         /* fall through */
1895     case GW_STATE_PREPARE_WRITE:
1896         /* ok, we have the connection */
1897 
1898         {
1899             handler_t rc = hctx->create_env(hctx);
1900             if (HANDLER_GO_ON != rc) {
1901                 if (HANDLER_FINISHED != rc && HANDLER_ERROR != rc)
1902                     fdevent_fdnode_event_clr(hctx->ev, hctx->fdn, FDEVENT_OUT);
1903                 return rc;
1904             }
1905         }
1906 
1907         /*(disable Nagle algorithm if streaming and content-length unknown)*/
1908         if (AF_UNIX != hctx->host->family) {
1909             if (r->reqbody_length < 0) {
1910                 if (-1 == fdevent_set_tcp_nodelay(hctx->fd, 1)) {
1911                     /*(error, but not critical)*/
1912                 }
1913             }
1914         }
1915 
1916         fdevent_fdnode_event_add(hctx->ev, hctx->fdn, FDEVENT_IN|FDEVENT_RDHUP);
1917         gw_set_state(hctx, GW_STATE_WRITE);
1918         /* fall through */
1919     case GW_STATE_WRITE:
1920         if (!chunkqueue_is_empty(hctx->wb)) {
1921             log_error_st * const errh = r->conf.errh;
1922           #if 0
1923             if (hctx->conf.debug > 1) {
1924                 log_error(errh, __FILE__, __LINE__, "sdsx",
1925                   "send data to backend (fd=%d), size=%zu",
1926                   hctx->fd, chunkqueue_length(hctx->wb));
1927             }
1928           #endif
1929             if (r->con->srv->network_backend_write(hctx->fd, hctx->wb,
1930                                                    MAX_WRITE_LIMIT, errh) < 0) {
1931                 switch(errno) {
1932                 case EPIPE:
1933                 case ENOTCONN:
1934                 case ECONNRESET:
1935                     /* the connection got dropped after accept()
1936                      * we don't care about that --
1937                      * if you accept() it, you have to handle it.
1938                      */
1939                     log_error(errh, __FILE__, __LINE__,
1940                       "connection was dropped after accept() "
1941                       "(perhaps the gw process died), "
1942                       "write-offset: %lld socket: %s",
1943                       (long long)hctx->wb->bytes_out,
1944                       hctx->proc->connection_name->ptr);
1945                     return HANDLER_ERROR;
1946                 default:
1947                     log_perror(errh, __FILE__, __LINE__, "write failed");
1948                     return HANDLER_ERROR;
1949                 }
1950             }
1951         }
1952 
1953         if (hctx->wb->bytes_out == hctx->wb_reqlen) {
1954             fdevent_fdnode_event_clr(hctx->ev, hctx->fdn, FDEVENT_OUT);
1955             gw_set_state(hctx, GW_STATE_READ);
1956         } else {
1957             off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
1958             if ((hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0)
1959                 && wblen < 65536 - 16384) {
1960                 /*(r->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
1961                 if (!(r->conf.stream_request_body
1962                       & FDEVENT_STREAM_REQUEST_POLLIN)) {
1963                     r->conf.stream_request_body |=
1964                         FDEVENT_STREAM_REQUEST_POLLIN;
1965                     r->con->is_readable = 1; /* trigger optimistic client read */
1966                 }
1967             }
1968             if (0 == wblen) {
1969                 fdevent_fdnode_event_clr(hctx->ev, hctx->fdn, FDEVENT_OUT);
1970             } else {
1971                 fdevent_fdnode_event_add(hctx->ev, hctx->fdn, FDEVENT_OUT);
1972             }
1973         }
1974 
1975         if (r->conf.stream_request_body
1976             & FDEVENT_STREAM_REQUEST_TCP_FIN)
1977             gw_conditional_tcp_fin(hctx, r);
1978 
1979         return HANDLER_WAIT_FOR_EVENT;
1980     case GW_STATE_READ:
1981         /* waiting for a response */
1982         return HANDLER_WAIT_FOR_EVENT;
1983     default:
1984         log_error(r->conf.errh, __FILE__, __LINE__,
1985           "(debug) unknown state");
1986         return HANDLER_ERROR;
1987     }
1988 }
1989 
1990 __attribute_cold__
1991 static handler_t gw_write_error(gw_handler_ctx * const hctx, request_st * const r) {
1992     int status = r->http_status;
1993 
1994     if (hctx->state == GW_STATE_INIT ||
1995         hctx->state == GW_STATE_CONNECT_DELAYED) {
1996 
1997         /* (optimization to detect backend process exit while processing a
1998          *  large number of ready events; (this block could be removed)) */
1999         server * const srv = r->con->srv;
2000         if (0 == srv->srvconf.max_worker)
2001             gw_restart_dead_procs(hctx->host, srv->errh, hctx->conf.debug, 0);
2002 
2003         /* cleanup this request and let request handler start request again */
2004         if (hctx->reconnects++ < 5) return gw_reconnect(hctx, r);
2005     }
2006 
2007     if (hctx->backend_error) hctx->backend_error(hctx);
2008     gw_connection_close(hctx, r);
2009     r->http_status = (status == 400) ? 400 : 503;
2010     return HANDLER_FINISHED;
2011 }
2012 
2013 static handler_t gw_send_request(gw_handler_ctx * const hctx, request_st * const r) {
2014     handler_t rc = gw_write_request(hctx, r);
2015     return (HANDLER_ERROR != rc) ? rc : gw_write_error(hctx, r);
2016 }
2017 
2018 
2019 static handler_t gw_recv_response(gw_handler_ctx *hctx, request_st *r);
2020 
2021 
2022 handler_t gw_handle_subrequest(request_st * const r, void *p_d) {
2023     gw_plugin_data *p = p_d;
2024     gw_handler_ctx *hctx = r->plugin_ctx[p->id];
2025     if (NULL == hctx) return HANDLER_GO_ON;
2026 
2027     if ((r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
2028         && r->resp_body_started) {
2029         if (chunkqueue_length(r->write_queue) > 65536 - 4096) {
2030             fdevent_fdnode_event_clr(hctx->ev, hctx->fdn, FDEVENT_IN);
2031         }
2032         else if (!(fdevent_fdnode_interest(hctx->fdn) & FDEVENT_IN)) {
2033             /* optimistic read from backend */
2034             handler_t rc;
2035             rc = gw_recv_response(hctx, r);          /*(might invalidate hctx)*/
2036             if (rc != HANDLER_GO_ON) return rc;      /*(unless HANDLER_GO_ON)*/
2037             fdevent_fdnode_event_add(hctx->ev, hctx->fdn, FDEVENT_IN);
2038         }
2039     }
2040 
2041     /* (do not receive request body before GW_AUTHORIZER has run or else
2042      *  the request body is discarded with handler_ctx_clear() after running
2043      *  the FastCGI Authorizer) */
2044 
2045     if (hctx->gw_mode != GW_AUTHORIZER
2046         && (0 == hctx->wb->bytes_in
2047             ? (r->state == CON_STATE_READ_POST || -1 == hctx->wb_reqlen)
2048             : (hctx->wb->bytes_in < hctx->wb_reqlen || hctx->wb_reqlen < 0))) {
2049         /* leave excess data in r->reqbody_queue, which is
2050          * buffered to disk if too large and backend can not keep up */
2051         /*(64k - 4k to attempt to avoid temporary files
2052          * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
2053         if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096) {
2054             if (r->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
2055                 r->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
2056             }
2057             if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
2058         }
2059         else {
2060             handler_t rc = connection_handle_read_post_state(r);
2061 
2062             /* XXX: create configurable flag */
2063             /* CGI environment requires that Content-Length be set.
2064              * Send 411 Length Required if Content-Length missing.
2065              * (occurs here if client sends Transfer-Encoding: chunked
2066              *  and module is flagged to stream request body to backend) */
2067             if (-1 == r->reqbody_length && hctx->opts.backend != BACKEND_PROXY){
2068                 return (r->conf.stream_request_body & FDEVENT_STREAM_REQUEST)
2069                   ? connection_handle_read_post_error(r, 411)
2070                   : HANDLER_WAIT_FOR_EVENT;
2071             }
2072 
2073             if (hctx->wb_reqlen < -1 && r->reqbody_length >= 0) {
2074                 /* (completed receiving Transfer-Encoding: chunked) */
2075                 hctx->wb_reqlen = -hctx->wb_reqlen;
2076                 if (hctx->stdin_append) {
2077                     handler_t rca = hctx->stdin_append(hctx);
2078                     if (HANDLER_GO_ON != rca) return rca;
2079                 }
2080             }
2081 
2082             if ((0 != hctx->wb->bytes_in || -1 == hctx->wb_reqlen)
2083                 && !chunkqueue_is_empty(r->reqbody_queue)) {
2084                 if (hctx->stdin_append) {
2085                     handler_t rca = hctx->stdin_append(hctx);
2086                     if (HANDLER_GO_ON != rca) return rca;
2087                 }
2088                 else
2089                     chunkqueue_append_chunkqueue(hctx->wb, r->reqbody_queue);
2090                 if (fdevent_fdnode_interest(hctx->fdn) & FDEVENT_OUT) {
2091                     return (rc == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : rc;
2092                 }
2093             }
2094             if (rc != HANDLER_GO_ON) return rc;
2095         }
2096     }
2097 
2098     {
2099         handler_t rc =((0==hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
2100                        && hctx->state != GW_STATE_CONNECT_DELAYED)
2101           ? gw_send_request(hctx, r)
2102           : HANDLER_WAIT_FOR_EVENT;
2103         if (HANDLER_WAIT_FOR_EVENT != rc) return rc;
2104     }
2105 
2106     if (r->conf.stream_request_body & FDEVENT_STREAM_REQUEST_TCP_FIN)
2107         gw_conditional_tcp_fin(hctx, r);
2108 
2109     return HANDLER_WAIT_FOR_EVENT;
2110 }
2111 
2112 
2113 static handler_t gw_recv_response(gw_handler_ctx * const hctx, request_st * const r) {
2114     gw_proc *proc = hctx->proc;
2115     gw_host *host = hctx->host;
2116     /*(XXX: make this a configurable flag for other protocols)*/
2117     buffer *b = hctx->opts.backend == BACKEND_FASTCGI
2118       ? chunk_buffer_acquire()
2119       : hctx->response;
2120 
2121     handler_t rc = http_response_read(r, &hctx->opts, b, hctx->fdn);
2122 
2123     if (b != hctx->response) chunk_buffer_release(b);
2124 
2125     switch (rc) {
2126     default:
2127         return HANDLER_GO_ON;
2128     case HANDLER_FINISHED:
2129         if (hctx->gw_mode == GW_AUTHORIZER
2130             && (200 == r->http_status || 0 == r->http_status)) {
2131             /*
2132              * If we are here in AUTHORIZER mode then a request for authorizer
2133              * was processed already, and status 200 has been returned. We need
2134              * now to handle authorized request.
2135              */
2136             char *physpath = NULL;
2137 
2138             if (!buffer_string_is_empty(host->docroot)) {
2139                 buffer_copy_buffer(&r->physical.doc_root, host->docroot);
2140                 buffer_copy_buffer(&r->physical.basedir, host->docroot);
2141 
2142                 buffer_copy_buffer(&r->physical.path, host->docroot);
2143                 buffer_append_string_buffer(&r->physical.path, &r->uri.path);
2144                 physpath = r->physical.path.ptr;
2145             }
2146 
2147             proc->last_used = log_epoch_secs;
2148             gw_backend_close(hctx, r);
2149             handler_ctx_clear(hctx);
2150 
2151             /* don't do more than 6 loops here; normally shouldn't happen */
2152             if (++r->loops_per_request > 5) {
2153                 log_error(r->conf.errh, __FILE__, __LINE__,
2154                   "too many loops while processing request: %s",
2155                   r->target_orig.ptr);
2156                 r->http_status = 500; /* Internal Server Error */
2157                 r->handler_module = NULL;
2158                 return HANDLER_FINISHED;
2159             }
2160 
2161             /* restart the request so other handlers can process it */
2162 
2163             if (physpath) r->physical.path.ptr = NULL;
2164             connection_response_reset(r); /*(includes r->http_status=0)*/
2165             /* preserve r->physical.path.ptr with modified docroot */
2166             if (physpath) r->physical.path.ptr = physpath;
2167 
2168             /*(FYI: if multiple FastCGI authorizers were to be supported,
2169              * next one could be started here instead of restarting request)*/
2170 
2171             r->handler_module = NULL;
2172             return HANDLER_COMEBACK;
2173         } else {
2174             /* we are done */
2175             gw_connection_close(hctx, r);
2176         }
2177 
2178         return HANDLER_FINISHED;
2179     case HANDLER_COMEBACK: /*(not expected; treat as error)*/
2180     case HANDLER_ERROR:
2181         /* (optimization to detect backend process exit while processing a
2182          *  large number of ready events; (this block could be removed)) */
2183         if (proc->is_local && 1 == proc->load && proc->pid == hctx->pid
2184             && proc->state != PROC_STATE_DIED
2185             && 0 == r->con->srv->srvconf.max_worker) {
2186             /* intentionally check proc->disabed_until before gw_proc_waitpid */
2187             log_error_st * const errh = r->con->srv->errh;
2188             if (proc->disabled_until < log_epoch_secs
2189                 && 0 != gw_proc_waitpid(host, proc, errh)) {
2190                 if (hctx->conf.debug) {
2191                     log_error(errh, __FILE__, __LINE__,
2192                       "--- gw spawning\n\tsocket %s\n\tcurrent: 1/%d",
2193                       proc->connection_name->ptr, host->num_procs);
2194                 }
2195 
2196                 if (gw_spawn_connection(host, proc, errh, hctx->conf.debug)) {
2197                     log_error(errh, __FILE__, __LINE__,
2198                       "respawning failed, will retry later");
2199                 }
2200             }
2201         }
2202 
2203         if (r->resp_body_started == 0) {
2204             /* nothing has been sent out yet, try to use another child */
2205 
2206             if (hctx->wb->bytes_out == 0 &&
2207                 hctx->reconnects++ < 5) {
2208 
2209                 log_error(r->conf.errh, __FILE__, __LINE__,
2210                   "response not received, request not sent on "
2211                   "socket: %s for %s?%.*s, reconnecting",
2212                   proc->connection_name->ptr,
2213                   r->uri.path.ptr, BUFFER_INTLEN_PTR(&r->uri.query));
2214 
2215                 return gw_reconnect(hctx, r);
2216             }
2217 
2218             log_error(r->conf.errh, __FILE__, __LINE__,
2219               "response not received, request sent: %lld on "
2220               "socket: %s for %s?%.*s, closing connection",
2221               (long long)hctx->wb->bytes_out, proc->connection_name->ptr,
2222               r->uri.path.ptr, BUFFER_INTLEN_PTR(&r->uri.query));
2223         } else {
2224             log_error(r->conf.errh, __FILE__, __LINE__,
2225               "response already sent out, but backend returned error on "
2226               "socket: %s for %s?%.*s, terminating connection",
2227               proc->connection_name->ptr,
2228               r->uri.path.ptr, BUFFER_INTLEN_PTR(&r->uri.query));
2229         }
2230 
2231         if (hctx->backend_error) hctx->backend_error(hctx);
2232         http_response_backend_error(r);
2233         gw_connection_close(hctx, r);
2234         return HANDLER_FINISHED;
2235     }
2236 }
2237 
2238 
2239 static handler_t gw_handle_fdevent(void *ctx, int revents) {
2240     gw_handler_ctx *hctx = ctx;
2241     request_st * const r = hctx->r;
2242 
2243     joblist_append(r->con);
2244 
2245     if (revents & FDEVENT_IN) {
2246         handler_t rc = gw_recv_response(hctx, r);   /*(might invalidate hctx)*/
2247         if (rc != HANDLER_GO_ON) return rc;         /*(unless HANDLER_GO_ON)*/
2248     }
2249 
2250     if (revents & FDEVENT_OUT) {
2251         return gw_send_request(hctx, r); /*(might invalidate hctx)*/
2252     }
2253 
2254     /* perhaps this issue is already handled */
2255     if (revents & (FDEVENT_HUP|FDEVENT_RDHUP)) {
2256         if (hctx->state == GW_STATE_CONNECT_DELAYED) {
2257             /* getoptsock will catch this one (right ?)
2258              *
2259              * if we are in connect we might get an EINPROGRESS
2260              * in the first call and an FDEVENT_HUP in the
2261              * second round
2262              *
2263              * FIXME: as it is a bit ugly.
2264              *
2265              */
2266             gw_send_request(hctx, r);
2267         } else if (r->resp_body_started) {
2268             /* drain any remaining data from kernel pipe buffers
2269              * even if (r->conf.stream_response_body
2270              *          & FDEVENT_STREAM_RESPONSE_BUFMIN)
2271              * since event loop will spin on fd FDEVENT_HUP event
2272              * until unregistered. */
2273             handler_t rc;
2274             const unsigned short flags = r->conf.stream_response_body;
2275             r->conf.stream_response_body &= ~FDEVENT_STREAM_RESPONSE_BUFMIN;
2276             r->conf.stream_response_body |= FDEVENT_STREAM_RESPONSE_POLLRDHUP;
2277             do {
2278                 rc = gw_recv_response(hctx, r);  /*(might invalidate hctx)*/
2279             } while (rc == HANDLER_GO_ON);       /*(unless HANDLER_GO_ON)*/
2280             r->conf.stream_response_body = flags;
2281             return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
2282         } else {
2283             gw_proc *proc = hctx->proc;
2284             log_error(r->conf.errh, __FILE__, __LINE__,
2285               "error: unexpected close of gw connection for %s?%.*s "
2286               "(no gw process on socket: %s ?) %d",
2287               r->uri.path.ptr, BUFFER_INTLEN_PTR(&r->uri.query),
2288               proc->connection_name->ptr, hctx->state);
2289 
2290             gw_connection_close(hctx, r);
2291         }
2292     } else if (revents & FDEVENT_ERR) {
2293         log_error(r->conf.errh, __FILE__, __LINE__,
2294           "gw: got a FDEVENT_ERR. Don't know why.");
2295 
2296         if (hctx->backend_error) hctx->backend_error(hctx);
2297         http_response_backend_error(r);
2298         gw_connection_close(hctx, r);
2299     }
2300 
2301     return HANDLER_FINISHED;
2302 }
2303 
2304 handler_t gw_check_extension(request_st * const r, gw_plugin_data * const p, int uri_path_handler, size_t hctx_sz) {
2305   #if 0 /*(caller must handle)*/
2306     if (NULL != r->handler_module) return HANDLER_GO_ON;
2307     gw_patch_connection(r, p);
2308     if (NULL == p->conf.exts) return HANDLER_GO_ON;
2309   #endif
2310 
2311     buffer *fn = uri_path_handler ? &r->uri.path : &r->physical.path;
2312     size_t s_len = buffer_string_length(fn);
2313     gw_extension *extension = NULL;
2314     gw_host *host = NULL;
2315     gw_handler_ctx *hctx;
2316     unsigned short gw_mode;
2317 
2318     if (0 == s_len) return HANDLER_GO_ON; /*(not expected)*/
2319 
2320     /* check p->conf.exts_auth list and then p->conf.ext_resp list
2321      * (skip p->conf.exts_auth if array is empty
2322      *  or if GW_AUTHORIZER already ran in this request) */
2323     hctx = r->plugin_ctx[p->id];
2324     /*(hctx not NULL if GW_AUTHORIZER ran; hctx->ext_auth check is redundant)*/
2325     gw_mode = (NULL == hctx || NULL == hctx->ext_auth)
2326       ? 0              /*GW_AUTHORIZER p->conf.exts_auth will be searched next*/
2327       : GW_AUTHORIZER; /*GW_RESPONDER p->conf.exts_resp will be searched next*/
2328 
2329     do {
2330 
2331         gw_exts *exts;
2332         if (0 == gw_mode) {
2333             gw_mode = GW_AUTHORIZER;
2334             exts = p->conf.exts_auth;
2335         } else {
2336             gw_mode = GW_RESPONDER;
2337             exts = p->conf.exts_resp;
2338         }
2339 
2340         if (0 == exts->used) continue;
2341 
2342         /* gw.map-extensions maps extensions to existing gw.server entries
2343          *
2344          * gw.map-extensions = ( ".php3" => ".php" )
2345          *
2346          * gw.server = ( ".php" => ... )
2347          *
2348          * */
2349 
2350         /* check if extension-mapping matches */
2351         if (p->conf.ext_mapping) {
2352             data_string *ds =
2353               (data_string *)array_match_key_suffix(p->conf.ext_mapping, fn);
2354             if (NULL != ds) {
2355                 /* found a mapping */
2356                     /* check if we know the extension */
2357                     uint32_t k;
2358                     for (k = 0; k < exts->used; ++k) {
2359                         extension = exts->exts+k;
2360 
2361                         if (buffer_is_equal(&ds->value, &extension->key)) {
2362                             break;
2363                         }
2364                     }
2365 
2366                     if (k == exts->used) {
2367                         /* found nothing */
2368                         extension = NULL;
2369                     }
2370             }
2371         }
2372 
2373         if (extension == NULL) {
2374             size_t uri_path_len = buffer_string_length(&r->uri.path);
2375 
2376             /* check if extension matches */
2377             for (uint32_t k = 0; k < exts->used; ++k) {
2378                 gw_extension *ext = exts->exts+k;
2379                 size_t ct_len = buffer_string_length(&ext->key);
2380 
2381                 /* check _url_ in the form "/gw_pattern" */
2382                 if (ext->key.ptr[0] == '/') {
2383                     if (ct_len <= uri_path_len
2384                         && 0 == memcmp(r->uri.path.ptr, ext->key.ptr, ct_len)) {
2385                         extension = ext;
2386                         break;
2387                     }
2388                 } else if (ct_len <= s_len
2389                            && 0 == memcmp(fn->ptr + s_len - ct_len,
2390                                           ext->key.ptr, ct_len)) {
2391                     /* check extension in the form ".fcg" */
2392                     extension = ext;
2393                     break;
2394                 }
2395             }
2396         }
2397 
2398     } while (NULL == extension && gw_mode != GW_RESPONDER);
2399 
2400     /* extension doesn't match */
2401     if (NULL == extension) {
2402         return HANDLER_GO_ON;
2403     }
2404 
2405     /* check if we have at least one server for this extension up and running */
2406     host = gw_host_get(r, extension, p->conf.balance, p->conf.debug);
2407     if (NULL == host) {
2408         return HANDLER_FINISHED;
2409     }
2410 
2411     /* a note about no handler is not sent yet */
2412     extension->note_is_sent = 0;
2413 
2414     /*
2415      * if check-local is disabled, use the uri.path handler
2416      *
2417      */
2418 
2419     /* init handler-context */
2420     if (uri_path_handler) {
2421         if (host->check_local != 0) {
2422             return HANDLER_GO_ON;
2423         } else {
2424             /* do not split path info for authorizer */
2425             if (gw_mode != GW_AUTHORIZER) {
2426                 /* the prefix is the SCRIPT_NAME,
2427                 * everything from start to the next slash
2428                 * this is important for check-local = "disable"
2429                 *
2430                 * if prefix = /admin.gw
2431                 *
2432                 * /admin.gw/foo/bar
2433                 *
2434                 * SCRIPT_NAME = /admin.gw
2435                 * PATH_INFO   = /foo/bar
2436                 *
2437                 * if prefix = /cgi-bin/
2438                 *
2439                 * /cgi-bin/foo/bar
2440                 *
2441                 * SCRIPT_NAME = /cgi-bin/foo
2442                 * PATH_INFO   = /bar
2443                 *
2444                 * if prefix = /, and fix-root-path-name is enable
2445                 *
2446                 * /cgi-bin/foo/bar
2447                 *
2448                 * SCRIPT_NAME = /cgi-bin/foo
2449                 * PATH_INFO   = /bar
2450                 *
2451                 */
2452                 char *pathinfo;
2453 
2454                 /* the rewrite is only done for /prefix/? matches */
2455                 if (host->fix_root_path_name && extension->key.ptr[0] == '/'
2456                                              && extension->key.ptr[1] == '\0') {
2457                     buffer_copy_buffer(&r->pathinfo, &r->uri.path);
2458                     buffer_string_set_length(&r->uri.path, 0);
2459                 } else if (extension->key.ptr[0] == '/'
2460                            && buffer_string_length(&r->uri.path)
2461                               > buffer_string_length(&extension->key)
2462                            && (pathinfo =
2463                                  strchr(r->uri.path.ptr
2464                                         + buffer_string_length(&extension->key),
2465                                         '/')) != NULL) {
2466                     /* rewrite uri.path and pathinfo */
2467 
2468                     buffer_copy_string(&r->pathinfo, pathinfo);
2469                     buffer_string_set_length(
2470                       &r->uri.path,
2471                       buffer_string_length(&r->uri.path)
2472                       - buffer_string_length(&r->pathinfo));
2473                 }
2474             }
2475         }
2476     }
2477 
2478     if (!hctx) hctx = handler_ctx_init(hctx_sz);
2479 
2480     hctx->ev               = r->con->srv->ev;
2481     hctx->r                = r;
2482     hctx->plugin_data      = p;
2483     hctx->host             = host;
2484     hctx->proc             = NULL;
2485     hctx->ext              = extension;
2486     gw_host_assign(host);
2487 
2488     hctx->gw_mode = gw_mode;
2489     if (gw_mode == GW_AUTHORIZER) {
2490         hctx->ext_auth = hctx->ext;
2491     }
2492 
2493     /*hctx->conf.exts        = p->conf.exts;*/
2494     /*hctx->conf.exts_auth   = p->conf.exts_auth;*/
2495     /*hctx->conf.exts_resp   = p->conf.exts_resp;*/
2496     /*hctx->conf.ext_mapping = p->conf.ext_mapping;*/
2497     hctx->conf.balance     = p->conf.balance;
2498     hctx->conf.proto       = p->conf.proto;
2499     hctx->conf.debug       = p->conf.debug;
2500 
2501     hctx->opts.fdfmt = S_IFSOCK;
2502     hctx->opts.authorizer = (gw_mode == GW_AUTHORIZER);
2503     hctx->opts.local_redir = 0;
2504     hctx->opts.xsendfile_allow = host->xsendfile_allow;
2505     hctx->opts.xsendfile_docroot = host->xsendfile_docroot;
2506 
2507     r->plugin_ctx[p->id] = hctx;
2508 
2509     r->handler_module = p->self;
2510 
2511     if (r->conf.log_request_handling) {
2512         log_error(r->conf.errh, __FILE__, __LINE__, "handling it in mod_gw");
2513     }
2514 
2515     return HANDLER_GO_ON;
2516 }
2517 
2518 static void gw_handle_trigger_host(gw_host * const host, log_error_st * const errh, const int debug) {
2519     /*
2520      * TODO:
2521      *
2522      * - add timeout for a connect to a non-gw process
2523      *   (use state_timestamp + state)
2524      *
2525      * perhaps we should kill a connect attempt after 10-15 seconds
2526      *
2527      * currently we wait for the TCP timeout which is 180 seconds on Linux
2528      */
2529 
2530     /* check each child proc to detect if proc exited */
2531 
2532     gw_proc *proc;
2533     time_t idle_timestamp;
2534     int overload = 1;
2535 
2536     for (proc = host->first; proc; proc = proc->next) {
2537         gw_proc_waitpid(host, proc, errh);
2538     }
2539 
2540     gw_restart_dead_procs(host, errh, debug, 1);
2541 
2542     /* check if adaptive spawning enabled */
2543     if (host->min_procs == host->max_procs) return;
2544     if (buffer_string_is_empty(host->bin_path)) return;
2545 
2546     for (proc = host->first; proc; proc = proc->next) {
2547         if (proc->load <= host->max_load_per_proc) {
2548             overload = 0;
2549             break;
2550         }
2551     }
2552 
2553     if (overload && host->num_procs && host->num_procs < host->max_procs) {
2554         /* overload, spawn new child */
2555         if (debug) {
2556             log_error(errh, __FILE__, __LINE__,
2557               "overload detected, spawning a new child");
2558         }
2559 
2560         gw_proc_spawn(host, errh, debug);
2561     }
2562 
2563     idle_timestamp = log_epoch_secs - host->idle_timeout;
2564     for (proc = host->first; proc; proc = proc->next) {
2565         if (host->num_procs <= host->min_procs) break;
2566         if (0 != proc->load) continue;
2567         if (proc->pid <= 0) continue;
2568         if (proc->last_used >= idle_timestamp) continue;
2569 
2570         /* terminate proc that has been idling for a long time */
2571         if (debug) {
2572             log_error(errh, __FILE__, __LINE__,
2573               "idle-timeout reached, terminating child: socket: %s pid %d",
2574               proc->unixsocket->ptr, proc->pid);
2575         }
2576 
2577         gw_proc_kill(host, proc);
2578 
2579         /* proc is now in unused, let next second handle next process */
2580         break;
2581     }
2582 
2583     for (proc = host->unused_procs; proc; proc = proc->next) {
2584         gw_proc_waitpid(host, proc, errh);
2585     }
2586 }
2587 
2588 static void gw_handle_trigger_exts(gw_exts * const exts, log_error_st * const errh, const int debug) {
2589     for (uint32_t j = 0; j < exts->used; ++j) {
2590         gw_extension *ex = exts->exts+j;
2591         for (uint32_t n = 0; n < ex->used; ++n) {
2592             gw_handle_trigger_host(ex->hosts[n], errh, debug);
2593         }
2594     }
2595 }
2596 
2597 static void gw_handle_trigger_exts_wkr(gw_exts *exts, log_error_st *errh) {
2598     for (uint32_t j = 0; j < exts->used; ++j) {
2599         gw_extension * const ex = exts->exts+j;
2600         for (uint32_t n = 0; n < ex->used; ++n) {
2601             gw_host * const host = ex->hosts[n];
2602             for (gw_proc *proc = host->first; proc; proc = proc->next) {
2603                 if (proc->state == PROC_STATE_OVERLOADED)
2604                     gw_proc_check_enable(host, proc, errh);
2605             }
2606         }
2607     }
2608 }
2609 
2610 handler_t gw_handle_trigger(server *srv, void *p_d) {
2611     gw_plugin_data * const p = p_d;
2612     int wkr = (0 != srv->srvconf.max_worker && p->srv_pid != srv->pid);
2613     log_error_st * const errh = srv->errh;
2614     int global_debug = 0;
2615 
2616     if (NULL == p->cvlist) return HANDLER_GO_ON;
2617     /* (init i to 0 if global context; to 1 to skip empty global context) */
2618     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
2619         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
2620         gw_plugin_config *conf = NULL;
2621         int debug = global_debug;
2622         for (; -1 != cpv->k_id; ++cpv) {
2623             switch (cpv->k_id) {
2624               case 0: /* xxxxx.server */
2625                 if (cpv->vtype == T_CONFIG_LOCAL) conf = cpv->v.v;
2626                 break;
2627               case 2: /* xxxxx.debug */
2628                 debug = (int)cpv->v.u;
2629                 if (0 == i) global_debug = (int)cpv->v.u;
2630               default:
2631                 break;
2632             }
2633         }
2634 
2635         if (NULL == conf || NULL == conf->exts) continue;
2636 
2637         /* (debug flag is only active if set in same scope as xxxxx.server
2638          *  or global scope (for convenience))
2639          * (unable to use p->defaults.debug since gw_plugin_config
2640          *  might be part of a larger plugin_config) */
2641         wkr
2642           ? gw_handle_trigger_exts_wkr(conf->exts, errh)
2643           : gw_handle_trigger_exts(conf->exts, errh, debug);
2644     }
2645 
2646     return HANDLER_GO_ON;
2647 }
2648 
2649 handler_t gw_handle_waitpid_cb(server *srv, void *p_d, pid_t pid, int status) {
2650     gw_plugin_data * const p = p_d;
2651     if (0 != srv->srvconf.max_worker && p->srv_pid != srv->pid)
2652         return HANDLER_GO_ON;
2653     log_error_st * const errh = srv->errh;
2654     int global_debug = 0;
2655 
2656     if (NULL == p->cvlist) return HANDLER_GO_ON;
2657     /* (init i to 0 if global context; to 1 to skip empty global context) */
2658     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
2659         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
2660         gw_plugin_config *conf = NULL;
2661         int debug = global_debug;
2662         for (; -1 != cpv->k_id; ++cpv) {
2663             switch (cpv->k_id) {
2664               case 0: /* xxxxx.server */
2665                 if (cpv->vtype == T_CONFIG_LOCAL) conf = cpv->v.v;
2666                 break;
2667               case 2: /* xxxxx.debug */
2668                 debug = (int)cpv->v.u;
2669                 if (0 == i) global_debug = (int)cpv->v.u;
2670               default:
2671                 break;
2672             }
2673         }
2674 
2675         if (NULL == conf || NULL == conf->exts) continue;
2676 
2677         /* (debug flag is only active if set in same scope as xxxxx.server
2678          *  or global scope (for convenience))
2679          * (unable to use p->defaults.debug since gw_plugin_config
2680          *  might be part of a larger plugin_config) */
2681         const time_t cur_ts = log_epoch_secs;
2682         gw_exts *exts = conf->exts;
2683         for (uint32_t j = 0; j < exts->used; ++j) {
2684             gw_extension *ex = exts->exts+j;
2685             for (uint32_t n = 0; n < ex->used; ++n) {
2686                 gw_host *host = ex->hosts[n];
2687                 gw_proc *proc;
2688                 for (proc = host->first; proc; proc = proc->next) {
2689                     if (!proc->is_local || proc->pid != pid) continue;
2690 
2691                     gw_proc_waitpid_log(host, proc, errh, status);
2692                     gw_proc_set_state(host, proc, PROC_STATE_DIED);
2693                     proc->pid = 0;
2694 
2695                     /* restart, but avoid spinning if child exits too quickly */
2696                     if (proc->disabled_until < cur_ts) {
2697                         if (proc->state != PROC_STATE_KILLED)
2698                             proc->disabled_until = cur_ts;
2699                         if (gw_spawn_connection(host, proc, errh, debug)) {
2700                             log_error(errh, __FILE__, __LINE__,
2701                               "ERROR: spawning gw failed.");
2702                         }
2703                     }
2704 
2705                     return HANDLER_FINISHED;
2706                 }
2707                 for (proc = host->unused_procs; proc; proc = proc->next) {
2708                     if (!proc->is_local || proc->pid != pid) continue;
2709 
2710                     gw_proc_waitpid_log(host, proc, errh, status);
2711                     if (proc->state != PROC_STATE_KILLED)
2712                         proc->disabled_until = cur_ts;
2713                     gw_proc_set_state(host, proc, PROC_STATE_DIED);
2714                     proc->pid = 0;
2715                     return HANDLER_FINISHED;
2716                 }
2717             }
2718         }
2719     }
2720 
2721     return HANDLER_GO_ON;
2722 }
2723