8 #include "http_chunk.h"
10 #include "connections.h"
16 #include "inet_ntop_cache.h"
17 #include "stat_cache.h"
18 #include "status_counter.h"
20 #include <sys/types.h>
30 #ifdef HAVE_FASTCGI_FASTCGI_H
31 # include <fastcgi/fastcgi.h>
33 # ifdef HAVE_FASTCGI_H
38 #endif /* HAVE_FASTCGI_FASTCGI_H */
42 #include "sys-socket.h"
47 #ifdef HAVE_SYS_WAIT_H
57 * - add timeout for a connect to a non-fastcgi process
58 * (use state_timestamp + state)
62 typedef struct fcgi_proc
{
63 size_t id
; /* id will be between 1 and max_procs */
64 buffer
*unixsocket
; /* config.socket + "-" + id */
65 unsigned port
; /* config.port + pno */
67 buffer
*connection_name
; /* either tcp:<host>:<port> or unix:<socket> for debugging purposes */
69 pid_t pid
; /* PID of the spawned process (0 if not spawned locally) */
72 size_t load
; /* number of requests waiting on this process */
74 size_t requests
; /* see max_requests */
75 struct fcgi_proc
*prev
, *next
; /* see first */
77 time_t disabled_until
; /* this proc is disabled until, use something else until then */
82 PROC_STATE_UNSET
, /* init-phase */
83 PROC_STATE_RUNNING
, /* alive */
84 PROC_STATE_OVERLOADED
, /* listen-queue is full,
85 don't send anything to this proc for the next 2 seconds */
86 PROC_STATE_DIED_WAIT_FOR_PID
, /* */
87 PROC_STATE_DIED
, /* marked as dead, should be restarted */
88 PROC_STATE_KILLED
/* was killed as we don't have the load anymore */
93 /* the key that is used to reference this value */
96 /* list of processes handling this extension
97 * sorted by lowest load
99 * whenever a job is done move it up in the list
100 * until it is sorted, move it down as soon as the
104 fcgi_proc
*unused_procs
;
107 * spawn at least min_procs, at max_procs.
109 * as soon as the load of the first entry
110 * is max_load_per_proc we spawn a new one
111 * and add it to the first entry and give it
116 unsigned short max_procs
;
117 size_t num_procs
; /* how many procs are started */
118 size_t active_procs
; /* how many of them are really running, i.e. state = PROC_STATE_RUNNING */
121 * time after a disabled remote connection is tried to be re-enabled
126 unsigned short disable_time
;
129 * some fastcgi processes get a little bit larger
130 * than wanted. max_requests_per_proc kills a
131 * process after a number of handled requests.
134 size_t max_requests_per_proc
;
142 * if host is one of the local IP adresses the
143 * whole connection is local
145 * if port is not 0, and host is not specified,
146 * "localhost" (INADDR_LOOPBACK) is assumed.
156 * instead of TCP/IP we can use Unix Domain Sockets
157 * - more secure (you have fileperms to play with)
158 * - more control (on locally)
159 * - more speed (no extra overhead)
163 /* if socket is local we can start the fastcgi
166 * bin-path is the path to the binary
168 * check min_procs and max_procs for the number
169 * of process to start up
173 /* bin-path is set bin-environment is taken to
174 * create the environement before starting the
183 * docroot-translation between URL->phys and the
187 * - different dir-layout if remote
202 * check_local tells you if the phys file is stat()ed
203 * or not. FastCGI doesn't care if the service is
204 * remote. If the web-server side doesn't contain
205 * the fastcgi-files we should not stat() for them
206 * and say '404 not found'.
208 unsigned short check_local
;
211 * append PATH_INFO to SCRIPT_FILENAME
213 * php needs this if cgi.fix_pathinfo is provided
217 unsigned short break_scriptfilename_for_php
;
220 * workaround for program when prefix="/"
222 * rule to build PATH_INFO is hardcoded for when check_local is disabled
223 * enable this option to use the workaround
227 unsigned short fix_root_path_name
;
230 * If the backend includes X-Sendfile in the response
231 * we use the value as filename and ignore the content.
234 unsigned short xsendfile_allow
;
235 array
*xsendfile_docroot
;
237 ssize_t load
; /* replace by host->load */
239 size_t max_id
; /* corresponds most of the time to
242 only if a process is killed max_id waits for the process itself
243 to die and decrements it afterwards */
245 buffer
*strip_request_uri
;
247 unsigned short kill_signal
; /* we need a setting for this as libfcgi
248 applications prefer SIGUSR1 while the
249 rest of the world would use SIGTERM
253 } fcgi_extension_host
;
256 * one extension can have multiple hosts assigned
257 * one host can spawn additional processes on the same
258 * socket (if we control it)
260 * ext -> host -> procs
263 * if the fastcgi process is remote that whole goes down
266 * ext -> host -> procs
269 * in case of PHP and FCGI_CHILDREN we have again a procs
270 * but we don't control it directly.
275 buffer
*key
; /* like .php */
280 fcgi_extension_host
**hosts
;
287 fcgi_extension
**exts
;
309 /* generic plugin data, shared between all connections */
319 plugin_config
**config_storage
;
321 plugin_config conf
; /* this is only used as long as no handler_ctx is setup */
324 /* connection specific data */
327 FCGI_STATE_CONNECT_DELAYED
,
328 FCGI_STATE_PREPARE_WRITE
,
331 } fcgi_connection_state_t
;
335 fcgi_extension_host
*host
;
338 fcgi_connection_state_t state
;
339 time_t state_timestamp
;
341 int reconnects
; /* number of reconnect attempts */
343 chunkqueue
*rb
; /* read queue */
344 chunkqueue
*wb
; /* write queue */
346 buffer
*response_header
;
349 int fd
; /* fd to the fastcgi process */
350 int fde_ndx
; /* index into the fd-event buffer */
355 int send_content_body
;
359 connection
*remote_conn
; /* dumb pointer */
360 plugin_data
*plugin_data
; /* dumb pointer */
364 /* ok, we need a prototype */
365 static handler_t
fcgi_handle_fdevent(server
*srv
, void *ctx
, int revents
);
367 static void reset_signals(void) {
369 signal(SIGTTOU
, SIG_DFL
);
372 signal(SIGTTIN
, SIG_DFL
);
375 signal(SIGTSTP
, SIG_DFL
);
377 signal(SIGHUP
, SIG_DFL
);
378 signal(SIGPIPE
, SIG_DFL
);
379 signal(SIGUSR1
, SIG_DFL
);
382 static void fastcgi_status_copy_procname(buffer
*b
, fcgi_extension_host
*host
, fcgi_proc
*proc
) {
383 buffer_copy_string_len(b
, CONST_STR_LEN("fastcgi.backend."));
384 buffer_append_string_buffer(b
, host
->id
);
386 buffer_append_string_len(b
, CONST_STR_LEN("."));
387 buffer_append_int(b
, proc
->id
);
391 static void fcgi_proc_load_inc(server
*srv
, handler_ctx
*hctx
) {
392 plugin_data
*p
= hctx
->plugin_data
;
395 status_counter_inc(srv
, CONST_STR_LEN("fastcgi.active-requests"));
397 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
398 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".load"));
400 status_counter_set(srv
, CONST_BUF_LEN(p
->statuskey
), hctx
->proc
->load
);
403 static void fcgi_proc_load_dec(server
*srv
, handler_ctx
*hctx
) {
404 plugin_data
*p
= hctx
->plugin_data
;
407 status_counter_dec(srv
, CONST_STR_LEN("fastcgi.active-requests"));
409 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
410 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".load"));
412 status_counter_set(srv
, CONST_BUF_LEN(p
->statuskey
), hctx
->proc
->load
);
415 static void fcgi_host_assign(server
*srv
, handler_ctx
*hctx
, fcgi_extension_host
*host
) {
416 plugin_data
*p
= hctx
->plugin_data
;
420 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, NULL
);
421 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".load"));
423 status_counter_set(srv
, CONST_BUF_LEN(p
->statuskey
), hctx
->host
->load
);
426 static void fcgi_host_reset(server
*srv
, handler_ctx
*hctx
) {
427 plugin_data
*p
= hctx
->plugin_data
;
430 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, NULL
);
431 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".load"));
433 status_counter_set(srv
, CONST_BUF_LEN(p
->statuskey
), hctx
->host
->load
);
438 static void fcgi_host_disable(server
*srv
, handler_ctx
*hctx
) {
439 plugin_data
*p
= hctx
->plugin_data
;
441 if (hctx
->host
->disable_time
|| hctx
->proc
->is_local
) {
442 if (hctx
->proc
->state
== PROC_STATE_RUNNING
) hctx
->host
->active_procs
--;
443 hctx
->proc
->disabled_until
= srv
->cur_ts
+ hctx
->host
->disable_time
;
444 hctx
->proc
->state
= hctx
->proc
->is_local
? PROC_STATE_DIED_WAIT_FOR_PID
: PROC_STATE_DIED
;
447 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
448 "backend disabled for", hctx
->host
->disable_time
, "seconds");
453 static int fastcgi_status_init(server
*srv
, buffer
*b
, fcgi_extension_host
*host
, fcgi_proc
*proc
) {
455 fastcgi_status_copy_procname(b, host, proc); \
456 buffer_append_string_len(b, CONST_STR_LEN(x)); \
457 status_counter_set(srv, CONST_BUF_LEN(b), 0);
461 CLEAN(".overloaded");
468 fastcgi_status_copy_procname(b, host, NULL); \
469 buffer_append_string_len(b, CONST_STR_LEN(x)); \
470 status_counter_set(srv, CONST_BUF_LEN(b), 0);
479 static handler_ctx
* handler_ctx_init(void) {
482 hctx
= calloc(1, sizeof(*hctx
));
487 hctx
->response_header
= buffer_init();
489 hctx
->request_id
= 0;
490 hctx
->state
= FCGI_STATE_INIT
;
495 hctx
->reconnects
= 0;
496 hctx
->send_content_body
= 1;
498 hctx
->rb
= chunkqueue_init();
499 hctx
->wb
= chunkqueue_init();
504 static void handler_ctx_free(server
*srv
, handler_ctx
*hctx
) {
506 fcgi_host_reset(srv
, hctx
);
509 buffer_free(hctx
->response_header
);
511 chunkqueue_free(hctx
->rb
);
512 chunkqueue_free(hctx
->wb
);
517 static fcgi_proc
*fastcgi_process_init(void) {
520 f
= calloc(1, sizeof(*f
));
521 f
->unixsocket
= buffer_init();
522 f
->connection_name
= buffer_init();
530 static void fastcgi_process_free(fcgi_proc
*f
) {
533 fastcgi_process_free(f
->next
);
535 buffer_free(f
->unixsocket
);
536 buffer_free(f
->connection_name
);
541 static fcgi_extension_host
*fastcgi_host_init(void) {
542 fcgi_extension_host
*f
;
544 f
= calloc(1, sizeof(*f
));
546 f
->id
= buffer_init();
547 f
->host
= buffer_init();
548 f
->unixsocket
= buffer_init();
549 f
->docroot
= buffer_init();
550 f
->bin_path
= buffer_init();
551 f
->bin_env
= array_init();
552 f
->bin_env_copy
= array_init();
553 f
->strip_request_uri
= buffer_init();
554 f
->xsendfile_docroot
= array_init();
559 static void fastcgi_host_free(fcgi_extension_host
*h
) {
563 buffer_free(h
->host
);
564 buffer_free(h
->unixsocket
);
565 buffer_free(h
->docroot
);
566 buffer_free(h
->bin_path
);
567 buffer_free(h
->strip_request_uri
);
568 array_free(h
->bin_env
);
569 array_free(h
->bin_env_copy
);
570 array_free(h
->xsendfile_docroot
);
572 fastcgi_process_free(h
->first
);
573 fastcgi_process_free(h
->unused_procs
);
579 static fcgi_exts
*fastcgi_extensions_init(void) {
582 f
= calloc(1, sizeof(*f
));
587 static void fastcgi_extensions_free(fcgi_exts
*f
) {
592 for (i
= 0; i
< f
->used
; i
++) {
598 for (j
= 0; j
< fe
->used
; j
++) {
599 fcgi_extension_host
*h
;
603 fastcgi_host_free(h
);
606 buffer_free(fe
->key
);
617 static int fastcgi_extension_insert(fcgi_exts
*ext
, buffer
*key
, fcgi_extension_host
*fh
) {
621 /* there is something */
623 for (i
= 0; i
< ext
->used
; i
++) {
624 if (buffer_is_equal(key
, ext
->exts
[i
]->key
)) {
629 if (i
== ext
->used
) {
630 /* filextension is new */
631 fe
= calloc(1, sizeof(*fe
));
633 fe
->key
= buffer_init();
634 fe
->last_used_ndx
= -1;
635 buffer_copy_buffer(fe
->key
, key
);
639 if (ext
->size
== 0) {
641 ext
->exts
= malloc(ext
->size
* sizeof(*(ext
->exts
)));
642 force_assert(ext
->exts
);
643 } else if (ext
->used
== ext
->size
) {
645 ext
->exts
= realloc(ext
->exts
, ext
->size
* sizeof(*(ext
->exts
)));
646 force_assert(ext
->exts
);
648 ext
->exts
[ext
->used
++] = fe
;
655 fe
->hosts
= malloc(fe
->size
* sizeof(*(fe
->hosts
)));
656 force_assert(fe
->hosts
);
657 } else if (fe
->size
== fe
->used
) {
659 fe
->hosts
= realloc(fe
->hosts
, fe
->size
* sizeof(*(fe
->hosts
)));
660 force_assert(fe
->hosts
);
663 fe
->hosts
[fe
->used
++] = fh
;
669 INIT_FUNC(mod_fastcgi_init
) {
672 p
= calloc(1, sizeof(*p
));
674 p
->fcgi_env
= buffer_init();
676 p
->path
= buffer_init();
678 p
->statuskey
= buffer_init();
684 FREE_FUNC(mod_fastcgi_free
) {
685 plugin_data
*p
= p_d
;
689 buffer_free(p
->fcgi_env
);
690 buffer_free(p
->path
);
691 buffer_free(p
->statuskey
);
693 if (p
->config_storage
) {
695 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
696 plugin_config
*s
= p
->config_storage
[i
];
699 if (NULL
== s
) continue;
703 for (j
= 0; j
< exts
->used
; j
++) {
708 for (n
= 0; n
< ex
->used
; n
++) {
710 fcgi_extension_host
*host
;
714 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
715 if (proc
->pid
!= 0) {
716 kill(proc
->pid
, host
->kill_signal
);
719 if (proc
->is_local
&&
720 !buffer_string_is_empty(proc
->unixsocket
)) {
721 unlink(proc
->unixsocket
->ptr
);
725 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
726 if (proc
->pid
!= 0) {
727 kill(proc
->pid
, host
->kill_signal
);
729 if (proc
->is_local
&&
730 !buffer_string_is_empty(proc
->unixsocket
)) {
731 unlink(proc
->unixsocket
->ptr
);
737 fastcgi_extensions_free(s
->exts
);
738 array_free(s
->ext_mapping
);
742 free(p
->config_storage
);
747 return HANDLER_GO_ON
;
750 static int env_add(char_array
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
754 if (!key
|| !val
) return -1;
756 dst
= malloc(key_len
+ val_len
+ 3);
757 memcpy(dst
, key
, key_len
);
759 memcpy(dst
+ key_len
+ 1, val
, val_len
);
760 dst
[key_len
+ 1 + val_len
] = '\0';
762 for (i
= 0; i
< env
->used
; i
++) {
763 if (0 == strncmp(dst
, env
->ptr
[i
], key_len
+ 1)) {
764 /* don't care about free as we are in a forked child which is going to exec(...) */
765 /* free(env->ptr[i]); */
771 if (env
->size
== 0) {
773 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
774 } else if (env
->size
== env
->used
+ 1) {
776 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
779 env
->ptr
[env
->used
++] = dst
;
784 static int parse_binpath(char_array
*env
, buffer
*b
) {
787 /* search for spaces */
790 for (i
= 0; i
< buffer_string_length(b
); i
++) {
794 /* a WS, stop here and copy the argument */
796 if (env
->size
== 0) {
798 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
799 } else if (env
->size
== env
->used
) {
801 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
806 env
->ptr
[env
->used
++] = start
;
808 start
= b
->ptr
+ i
+ 1;
815 if (env
->size
== 0) {
817 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
818 } else if (env
->size
== env
->used
) { /* we need one extra for the terminating NULL */
820 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
824 env
->ptr
[env
->used
++] = start
;
826 if (env
->size
== 0) {
828 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
829 } else if (env
->size
== env
->used
) { /* we need one extra for the terminating NULL */
831 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
835 env
->ptr
[env
->used
++] = NULL
;
840 #if !defined(HAVE_FORK)
841 static int fcgi_spawn_connection(server
*srv
,
843 fcgi_extension_host
*host
,
852 #else /* -> defined(HAVE_FORK) */
854 static int fcgi_spawn_connection(server
*srv
,
856 fcgi_extension_host
*host
,
860 struct timeval tv
= { 0, 100 * 1000 };
862 struct sockaddr_un fcgi_addr_un
;
864 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
865 struct sockaddr_in6 fcgi_addr_in6
;
867 struct sockaddr_in fcgi_addr_in
;
868 struct sockaddr
*fcgi_addr
;
873 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
874 "new proc, socket:", proc
->port
, proc
->unixsocket
);
877 if (!buffer_string_is_empty(proc
->unixsocket
)) {
879 memset(&fcgi_addr_un
, 0, sizeof(fcgi_addr_un
));
880 fcgi_addr_un
.sun_family
= AF_UNIX
;
881 if (buffer_string_length(proc
->unixsocket
) + 1 > sizeof(fcgi_addr_un
.sun_path
)) {
882 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
883 "ERROR: Unix Domain socket filename too long:",
887 memcpy(fcgi_addr_un
.sun_path
, proc
->unixsocket
->ptr
, buffer_string_length(proc
->unixsocket
) + 1);
890 servlen
= SUN_LEN(&fcgi_addr_un
);
893 servlen
= buffer_string_length(proc
->unixsocket
) + 1 + sizeof(fcgi_addr_un
.sun_family
);
895 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_un
;
897 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("unix:"));
898 buffer_append_string_buffer(proc
->connection_name
, proc
->unixsocket
);
901 log_error_write(srv
, __FILE__
, __LINE__
, "s",
902 "ERROR: Unix Domain sockets are not supported.");
905 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
906 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
907 memset(&fcgi_addr_in6
, 0, sizeof(fcgi_addr_in6
));
908 fcgi_addr_in6
.sin6_family
= AF_INET6
;
909 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &fcgi_addr_in6
.sin6_addr
);
910 fcgi_addr_in6
.sin6_port
= htons(proc
->port
);
911 servlen
= sizeof(fcgi_addr_in6
);
912 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in6
;
915 memset(&fcgi_addr_in
, 0, sizeof(fcgi_addr_in
));
916 fcgi_addr_in
.sin_family
= AF_INET
;
918 if (buffer_string_is_empty(host
->host
)) {
919 fcgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
923 /* set a useful default */
924 fcgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
927 if (NULL
== (he
= gethostbyname(host
->host
->ptr
))) {
928 log_error_write(srv
, __FILE__
, __LINE__
,
929 "sdb", "gethostbyname failed: ",
930 h_errno
, host
->host
);
934 if (he
->h_addrtype
!= AF_INET
) {
935 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-type != AF_INET: ", he
->h_addrtype
);
939 if (he
->h_length
!= sizeof(struct in_addr
)) {
940 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-length != sizeof(in_addr): ", he
->h_length
);
944 memcpy(&(fcgi_addr_in
.sin_addr
.s_addr
), he
->h_addr_list
[0], he
->h_length
);
947 fcgi_addr_in
.sin_port
= htons(proc
->port
);
948 servlen
= sizeof(fcgi_addr_in
);
950 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in
;
953 if (buffer_string_is_empty(proc
->unixsocket
)) {
954 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("tcp:"));
955 if (!buffer_string_is_empty(host
->host
)) {
956 buffer_append_string_buffer(proc
->connection_name
, host
->host
);
958 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN("localhost"));
960 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN(":"));
961 buffer_append_int(proc
->connection_name
, proc
->port
);
964 if (-1 == (fcgi_fd
= socket(fcgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
965 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
966 "failed:", strerror(errno
));
970 if (-1 == connect(fcgi_fd
, fcgi_addr
, servlen
)) {
971 /* server is not up, spawn it */
975 if (errno
!= ENOENT
&&
976 !buffer_string_is_empty(proc
->unixsocket
)) {
977 unlink(proc
->unixsocket
->ptr
);
983 if (-1 == (fcgi_fd
= socket(fcgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
984 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
985 "socket failed:", strerror(errno
));
990 if (setsockopt(fcgi_fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
)) < 0) {
991 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
992 "socketsockopt failed:", strerror(errno
));
998 if (-1 == bind(fcgi_fd
, fcgi_addr
, servlen
)) {
999 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1001 proc
->connection_name
,
1007 if (-1 == listen(fcgi_fd
, host
->listen_backlog
)) {
1008 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1009 "listen failed:", strerror(errno
));
1014 switch ((child
= fork())) {
1021 /* create environment */
1030 if(fcgi_fd
!= FCGI_LISTENSOCK_FILENO
) {
1031 close(FCGI_LISTENSOCK_FILENO
);
1032 dup2(fcgi_fd
, FCGI_LISTENSOCK_FILENO
);
1036 /* we don't need the client socket */
1037 for (i
= 3; i
< 256; i
++) {
1041 /* build clean environment */
1042 if (host
->bin_env_copy
->used
) {
1043 for (i
= 0; i
< host
->bin_env_copy
->used
; i
++) {
1044 data_string
*ds
= (data_string
*)host
->bin_env_copy
->data
[i
];
1047 if (NULL
!= (ge
= getenv(ds
->value
->ptr
))) {
1048 env_add(&env
, CONST_BUF_LEN(ds
->value
), ge
, strlen(ge
));
1052 char ** const e
= environ
;
1053 for (i
= 0; e
[i
]; ++i
) {
1056 if (NULL
!= (eq
= strchr(e
[i
], '='))) {
1057 env_add(&env
, e
[i
], eq
- e
[i
], eq
+1, strlen(eq
+1));
1062 /* create environment */
1063 for (i
= 0; i
< host
->bin_env
->used
; i
++) {
1064 data_string
*ds
= (data_string
*)host
->bin_env
->data
[i
];
1066 env_add(&env
, CONST_BUF_LEN(ds
->key
), CONST_BUF_LEN(ds
->value
));
1069 for (i
= 0; i
< env
.used
; i
++) {
1070 /* search for PHP_FCGI_CHILDREN */
1071 if (0 == strncmp(env
.ptr
[i
], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
1074 /* not found, add a default */
1075 if (i
== env
.used
) {
1076 env_add(&env
, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
1079 env
.ptr
[env
.used
] = NULL
;
1081 parse_binpath(&arg
, host
->bin_path
);
1083 /* chdir into the base of the bin-path,
1084 * search for the last / */
1085 if (NULL
!= (c
= strrchr(arg
.ptr
[0], '/'))) {
1088 /* change to the physical directory */
1089 if (-1 == chdir(arg
.ptr
[0])) {
1091 log_error_write(srv
, __FILE__
, __LINE__
, "sss", "chdir failed:", strerror(errno
), arg
.ptr
[0]);
1099 execve(arg
.ptr
[0], arg
.ptr
, env
.ptr
);
1101 /* log_error_write(srv, __FILE__, __LINE__, "sbs",
1102 "execve failed for:", host->bin_path, strerror(errno)); */
1117 select(0, NULL
, NULL
, NULL
, &tv
);
1119 switch (waitpid(child
, &status
, WNOHANG
)) {
1121 /* child still running after timeout, good */
1124 /* no PID found ? should never happen */
1125 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1126 "pid not found:", strerror(errno
));
1129 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1130 "the fastcgi-backend", host
->bin_path
, "failed to start:");
1131 /* the child should not terminate at all */
1132 if (WIFEXITED(status
)) {
1133 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
1134 "child exited with status",
1135 WEXITSTATUS(status
), host
->bin_path
);
1136 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1137 "If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version.\n"
1138 "If this is PHP on Gentoo, add 'fastcgi' to the USE flags.");
1139 } else if (WIFSIGNALED(status
)) {
1140 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1141 "terminated by signal:",
1144 if (WTERMSIG(status
) == 11) {
1145 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1146 "to be exact: it segfaulted, crashed, died, ... you get the idea." );
1147 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1148 "If this is PHP, try removing the bytecode caches for now and try again.");
1151 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1152 "child died somehow:",
1158 /* register process */
1169 if (p
->conf
.debug
) {
1170 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1171 "(debug) socket is already used; won't spawn:",
1172 proc
->connection_name
);
1176 proc
->state
= PROC_STATE_RUNNING
;
1177 host
->active_procs
++;
1182 #endif /* HAVE_FORK */
1184 static int unixsocket_is_dup(plugin_data
*p
, size_t used
, buffer
*unixsocket
) {
1186 for (i
= 0; i
< used
; ++i
) {
1187 fcgi_exts
*exts
= p
->config_storage
[i
]->exts
;
1188 for (j
= 0; j
< exts
->used
; ++j
) {
1189 fcgi_extension
*ex
= exts
->exts
[j
];
1190 for (n
= 0; n
< ex
->used
; ++n
) {
1191 fcgi_extension_host
*host
= ex
->hosts
[n
];
1192 if (!buffer_string_is_empty(host
->unixsocket
)
1193 && buffer_is_equal(host
->unixsocket
, unixsocket
))
1202 SETDEFAULTS_FUNC(mod_fastcgi_set_defaults
) {
1203 plugin_data
*p
= p_d
;
1206 buffer
*fcgi_mode
= buffer_init();
1207 fcgi_extension_host
*host
= NULL
;
1209 config_values_t cv
[] = {
1210 { "fastcgi.server", NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
1211 { "fastcgi.debug", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
1212 { "fastcgi.map-extensions", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
1213 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
1216 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
1218 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
1219 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
1222 s
= malloc(sizeof(plugin_config
));
1223 s
->exts
= fastcgi_extensions_init();
1225 s
->ext_mapping
= array_init();
1227 cv
[0].destination
= s
->exts
;
1228 cv
[1].destination
= &(s
->debug
);
1229 cv
[2].destination
= s
->ext_mapping
;
1231 p
->config_storage
[i
] = s
;
1233 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
1241 if (NULL
!= (du
= array_get_element(config
->value
, "fastcgi.server"))) {
1243 data_array
*da
= (data_array
*)du
;
1245 if (du
->type
!= TYPE_ARRAY
) {
1246 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
1247 "unexpected type for key: ", "fastcgi.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1254 * fastcgi.server = ( "<ext>" => ( ... ),
1255 * "<ext>" => ( ... ) )
1258 for (j
= 0; j
< da
->value
->used
; j
++) {
1260 data_array
*da_ext
= (data_array
*)da
->value
->data
[j
];
1262 if (da
->value
->data
[j
]->type
!= TYPE_ARRAY
) {
1263 log_error_write(srv
, __FILE__
, __LINE__
, "sssbs",
1264 "unexpected type for key: ", "fastcgi.server",
1265 "[", da
->value
->data
[j
]->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1271 * da_ext->key == name of the extension
1275 * fastcgi.server = ( "<ext>" =>
1276 * ( "<host>" => ( ... ),
1277 * "<host>" => ( ... )
1282 for (n
= 0; n
< da_ext
->value
->used
; n
++) {
1283 data_array
*da_host
= (data_array
*)da_ext
->value
->data
[n
];
1285 config_values_t fcv
[] = {
1286 { "host", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
1287 { "docroot", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
1288 { "mode", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
1289 { "socket", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
1290 { "bin-path", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 4 */
1292 { "check-local", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 5 */
1293 { "port", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 6 */
1294 { "max-procs", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 */
1295 { "disable-time", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 8 */
1297 { "bin-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 9 */
1298 { "bin-copy-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 10 */
1300 { "broken-scriptfilename", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 11 */
1301 { "allow-x-send-file", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 12 */
1302 { "strip-request-uri", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 13 */
1303 { "kill-signal", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 14 */
1304 { "fix-root-scriptname", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 15 */
1305 { "listen-backlog", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_CONNECTION
}, /* 16 */
1306 { "x-sendfile", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 17 */
1307 { "x-sendfile-docroot",NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 18 */
1309 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
1312 if (da_host
->type
!= TYPE_ARRAY
) {
1313 log_error_write(srv
, __FILE__
, __LINE__
, "ssSBS",
1314 "unexpected type for key:",
1316 "[", da_host
->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1321 host
= fastcgi_host_init();
1322 buffer_reset(fcgi_mode
);
1324 buffer_copy_buffer(host
->id
, da_host
->key
);
1326 host
->check_local
= 1;
1327 host
->max_procs
= 4;
1328 host
->mode
= FCGI_RESPONDER
;
1329 host
->disable_time
= 1;
1330 host
->break_scriptfilename_for_php
= 0;
1331 host
->xsendfile_allow
= 0;
1332 host
->kill_signal
= SIGTERM
;
1333 host
->fix_root_path_name
= 0;
1334 host
->listen_backlog
= 1024;
1336 fcv
[0].destination
= host
->host
;
1337 fcv
[1].destination
= host
->docroot
;
1338 fcv
[2].destination
= fcgi_mode
;
1339 fcv
[3].destination
= host
->unixsocket
;
1340 fcv
[4].destination
= host
->bin_path
;
1342 fcv
[5].destination
= &(host
->check_local
);
1343 fcv
[6].destination
= &(host
->port
);
1344 fcv
[7].destination
= &(host
->max_procs
);
1345 fcv
[8].destination
= &(host
->disable_time
);
1347 fcv
[9].destination
= host
->bin_env
;
1348 fcv
[10].destination
= host
->bin_env_copy
;
1349 fcv
[11].destination
= &(host
->break_scriptfilename_for_php
);
1350 fcv
[12].destination
= &(host
->xsendfile_allow
);
1351 fcv
[13].destination
= host
->strip_request_uri
;
1352 fcv
[14].destination
= &(host
->kill_signal
);
1353 fcv
[15].destination
= &(host
->fix_root_path_name
);
1354 fcv
[16].destination
= &(host
->listen_backlog
);
1355 fcv
[17].destination
= &(host
->xsendfile_allow
);
1356 fcv
[18].destination
= host
->xsendfile_docroot
;
1358 if (0 != config_insert_values_internal(srv
, da_host
->value
, fcv
, T_CONFIG_SCOPE_CONNECTION
)) {
1362 if ((!buffer_string_is_empty(host
->host
) || host
->port
) &&
1363 !buffer_string_is_empty(host
->unixsocket
)) {
1364 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1365 "either host/port or socket have to be set in:",
1367 da_ext
->key
, " => (",
1368 da_host
->key
, " ( ...");
1373 if (!buffer_string_is_empty(host
->unixsocket
)) {
1374 /* unix domain socket */
1375 struct sockaddr_un un
;
1377 if (buffer_string_length(host
->unixsocket
) + 1 > sizeof(un
.sun_path
) - 2) {
1378 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1379 "unixsocket is too long in:",
1381 da_ext
->key
, " => (",
1382 da_host
->key
, " ( ...");
1387 if (!buffer_string_is_empty(host
->bin_path
)
1388 && unixsocket_is_dup(p
, i
+1, host
->unixsocket
)) {
1389 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1390 "duplicate unixsocket path:",
1395 host
->family
= AF_UNIX
;
1399 if (buffer_string_is_empty(host
->host
) &&
1400 buffer_string_is_empty(host
->bin_path
)) {
1401 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1402 "host or binpath have to be set in:",
1404 da_ext
->key
, " => (",
1405 da_host
->key
, " ( ...");
1408 } else if (host
->port
== 0) {
1409 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1410 "port has to be set in:",
1412 da_ext
->key
, " => (",
1413 da_host
->key
, " ( ...");
1418 host
->family
= (!buffer_string_is_empty(host
->host
) && NULL
!= strchr(host
->host
->ptr
, ':')) ? AF_INET6
: AF_INET
;
1421 if (!buffer_string_is_empty(host
->bin_path
)) {
1422 /* a local socket + self spawning */
1426 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsbsd",
1427 "--- fastcgi spawning local",
1428 "\n\tproc:", host
->bin_path
,
1429 "\n\tport:", host
->port
,
1430 "\n\tsocket", host
->unixsocket
,
1431 "\n\tmax-procs:", host
->max_procs
);
1434 for (pno
= 0; pno
< host
->max_procs
; pno
++) {
1437 proc
= fastcgi_process_init();
1438 proc
->id
= host
->num_procs
++;
1441 if (buffer_string_is_empty(host
->unixsocket
)) {
1442 proc
->port
= host
->port
+ pno
;
1444 buffer_copy_buffer(proc
->unixsocket
, host
->unixsocket
);
1445 buffer_append_string_len(proc
->unixsocket
, CONST_STR_LEN("-"));
1446 buffer_append_int(proc
->unixsocket
, pno
);
1450 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsdsd",
1451 "--- fastcgi spawning",
1452 "\n\tport:", host
->port
,
1453 "\n\tsocket", host
->unixsocket
,
1454 "\n\tcurrent:", pno
, "/", host
->max_procs
);
1457 if (!srv
->srvconf
.preflight_check
1458 && fcgi_spawn_connection(srv
, p
, host
, proc
)) {
1459 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1460 "[ERROR]: spawning fcgi failed.");
1461 fastcgi_process_free(proc
);
1465 fastcgi_status_init(srv
, p
->statuskey
, host
, proc
);
1467 proc
->next
= host
->first
;
1468 if (host
->first
) host
->first
->prev
= proc
;
1475 proc
= fastcgi_process_init();
1476 proc
->id
= host
->num_procs
++;
1478 host
->active_procs
++;
1479 proc
->state
= PROC_STATE_RUNNING
;
1481 if (buffer_string_is_empty(host
->unixsocket
)) {
1482 proc
->port
= host
->port
;
1484 buffer_copy_buffer(proc
->unixsocket
, host
->unixsocket
);
1487 fastcgi_status_init(srv
, p
->statuskey
, host
, proc
);
1491 host
->max_procs
= 1;
1494 if (!buffer_string_is_empty(fcgi_mode
)) {
1495 if (strcmp(fcgi_mode
->ptr
, "responder") == 0) {
1496 host
->mode
= FCGI_RESPONDER
;
1497 } else if (strcmp(fcgi_mode
->ptr
, "authorizer") == 0) {
1498 host
->mode
= FCGI_AUTHORIZER
;
1499 if (buffer_string_is_empty(host
->docroot
)) {
1500 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1501 "ERROR: docroot is required for authorizer mode.");
1505 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1506 "WARNING: unknown fastcgi mode:",
1507 fcgi_mode
, "(ignored, mode set to responder)");
1511 if (host
->xsendfile_docroot
->used
) {
1513 for (k
= 0; k
< host
->xsendfile_docroot
->used
; ++k
) {
1514 data_string
*ds
= (data_string
*)host
->xsendfile_docroot
->data
[k
];
1515 if (ds
->type
!= TYPE_STRING
) {
1516 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1517 "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
1520 if (ds
->value
->ptr
[0] != '/') {
1521 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
1522 "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds
->value
, "\"");
1525 buffer_path_simplify(ds
->value
, ds
->value
);
1526 buffer_append_slash(ds
->value
);
1530 /* if extension already exists, take it */
1531 fastcgi_extension_insert(s
->exts
, da_ext
->key
, host
);
1538 buffer_free(fcgi_mode
);
1539 return HANDLER_GO_ON
;
1542 if (NULL
!= host
) fastcgi_host_free(host
);
1543 buffer_free(fcgi_mode
);
1544 return HANDLER_ERROR
;
1547 static int fcgi_set_state(server
*srv
, handler_ctx
*hctx
, fcgi_connection_state_t state
) {
1548 hctx
->state
= state
;
1549 hctx
->state_timestamp
= srv
->cur_ts
;
1555 static void fcgi_connection_close(server
*srv
, handler_ctx
*hctx
) {
1559 p
= hctx
->plugin_data
;
1560 con
= hctx
->remote_conn
;
1562 if (hctx
->fd
!= -1) {
1563 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
1564 fdevent_unregister(srv
->ev
, hctx
->fd
);
1569 if (hctx
->host
&& hctx
->proc
) {
1570 if (hctx
->got_proc
) {
1571 /* after the connect the process gets a load */
1572 fcgi_proc_load_dec(srv
, hctx
);
1574 if (p
->conf
.debug
) {
1575 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsd",
1577 "pid:", hctx
->proc
->pid
,
1578 "socket:", hctx
->proc
->connection_name
,
1579 "load:", hctx
->proc
->load
);
1585 handler_ctx_free(srv
, hctx
);
1586 con
->plugin_ctx
[p
->id
] = NULL
;
1588 /* finish response (if not already finished) */
1589 if (con
->mode
== p
->id
1590 && (con
->state
== CON_STATE_HANDLE_REQUEST
|| con
->state
== CON_STATE_READ_POST
)) {
1591 /* (not CON_STATE_ERROR and not CON_STATE_RESPONSE_END,
1592 * i.e. not called from fcgi_connection_reset()) */
1594 /* Send an error if we haven't sent any data yet */
1595 if (0 == con
->file_started
) {
1596 con
->http_status
= 500;
1599 else if (!con
->file_finished
) {
1600 http_chunk_close(srv
, con
);
1601 con
->file_finished
= 1;
1606 static int fcgi_reconnect(server
*srv
, handler_ctx
*hctx
) {
1607 plugin_data
*p
= hctx
->plugin_data
;
1613 * connect was ok, connection was accepted
1614 * but the php accept loop checks after the accept if it should die or not.
1616 * if yes we can only detect it at a write()
1618 * next step is resetting this attemp and setup a connection again
1620 * if we have more than 5 reconnects for the same request, die
1624 * we have a connection but the child died by some other reason
1628 if (hctx
->fd
!= -1) {
1629 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
1630 fdevent_unregister(srv
->ev
, hctx
->fd
);
1636 fcgi_set_state(srv
, hctx
, FCGI_STATE_INIT
);
1638 hctx
->request_id
= 0;
1641 if (p
->conf
.debug
> 2) {
1643 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
1644 "release proc for reconnect:",
1645 hctx
->proc
->pid
, hctx
->proc
->connection_name
);
1647 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1648 "release proc for reconnect:",
1649 hctx
->host
->unixsocket
);
1653 if (hctx
->proc
&& hctx
->got_proc
) {
1654 fcgi_proc_load_dec(srv
, hctx
);
1657 /* perhaps another host gives us more luck */
1658 fcgi_host_reset(srv
, hctx
);
1664 static handler_t
fcgi_connection_reset(server
*srv
, connection
*con
, void *p_d
) {
1665 plugin_data
*p
= p_d
;
1666 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1667 if (hctx
) fcgi_connection_close(srv
, hctx
);
1669 return HANDLER_GO_ON
;
1673 static int fcgi_env_add(buffer
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
1676 size_t len_enc_len
= 0;
1678 if (!key
|| !val
) return -1;
1680 len
= key_len
+ val_len
;
1682 len
+= key_len
> 127 ? 4 : 1;
1683 len
+= val_len
> 127 ? 4 : 1;
1685 if (buffer_string_length(env
) + len
>= FCGI_MAX_LENGTH
) {
1687 * we can't append more headers, ignore it
1693 * field length can be 31bit max
1695 * HINT: this can't happen as FCGI_MAX_LENGTH is only 16bit
1697 force_assert(key_len
< 0x7fffffffu
);
1698 force_assert(val_len
< 0x7fffffffu
);
1700 buffer_string_prepare_append(env
, len
);
1702 if (key_len
> 127) {
1703 len_enc
[len_enc_len
++] = ((key_len
>> 24) & 0xff) | 0x80;
1704 len_enc
[len_enc_len
++] = (key_len
>> 16) & 0xff;
1705 len_enc
[len_enc_len
++] = (key_len
>> 8) & 0xff;
1706 len_enc
[len_enc_len
++] = (key_len
>> 0) & 0xff;
1708 len_enc
[len_enc_len
++] = (key_len
>> 0) & 0xff;
1711 if (val_len
> 127) {
1712 len_enc
[len_enc_len
++] = ((val_len
>> 24) & 0xff) | 0x80;
1713 len_enc
[len_enc_len
++] = (val_len
>> 16) & 0xff;
1714 len_enc
[len_enc_len
++] = (val_len
>> 8) & 0xff;
1715 len_enc
[len_enc_len
++] = (val_len
>> 0) & 0xff;
1717 len_enc
[len_enc_len
++] = (val_len
>> 0) & 0xff;
1720 buffer_append_string_len(env
, len_enc
, len_enc_len
);
1721 buffer_append_string_len(env
, key
, key_len
);
1722 buffer_append_string_len(env
, val
, val_len
);
1727 static int fcgi_header(FCGI_Header
* header
, unsigned char type
, size_t request_id
, int contentLength
, unsigned char paddingLength
) {
1728 force_assert(contentLength
<= FCGI_MAX_LENGTH
);
1730 header
->version
= FCGI_VERSION_1
;
1731 header
->type
= type
;
1732 header
->requestIdB0
= request_id
& 0xff;
1733 header
->requestIdB1
= (request_id
>> 8) & 0xff;
1734 header
->contentLengthB0
= contentLength
& 0xff;
1735 header
->contentLengthB1
= (contentLength
>> 8) & 0xff;
1736 header
->paddingLength
= paddingLength
;
1737 header
->reserved
= 0;
1744 CONNECTION_DELAYED
, /* retry after event, take same host */
1745 CONNECTION_OVERLOADED
, /* disable for 1 second, take another backend */
1746 CONNECTION_DEAD
/* disable for 60 seconds, take another backend */
1747 } connection_result_t
;
1749 static connection_result_t
fcgi_establish_connection(server
*srv
, handler_ctx
*hctx
) {
1750 struct sockaddr
*fcgi_addr
;
1751 struct sockaddr_in fcgi_addr_in
;
1752 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1753 struct sockaddr_in6 fcgi_addr_in6
;
1755 #ifdef HAVE_SYS_UN_H
1756 struct sockaddr_un fcgi_addr_un
;
1760 fcgi_extension_host
*host
= hctx
->host
;
1761 fcgi_proc
*proc
= hctx
->proc
;
1762 int fcgi_fd
= hctx
->fd
;
1764 if (!buffer_string_is_empty(proc
->unixsocket
)) {
1765 #ifdef HAVE_SYS_UN_H
1766 /* use the unix domain socket */
1767 memset(&fcgi_addr_un
, 0, sizeof(fcgi_addr_un
));
1768 fcgi_addr_un
.sun_family
= AF_UNIX
;
1769 if (buffer_string_length(proc
->unixsocket
) + 1 > sizeof(fcgi_addr_un
.sun_path
)) {
1770 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
1771 "ERROR: Unix Domain socket filename too long:",
1775 memcpy(fcgi_addr_un
.sun_path
, proc
->unixsocket
->ptr
, buffer_string_length(proc
->unixsocket
) + 1);
1778 servlen
= SUN_LEN(&fcgi_addr_un
);
1781 servlen
= buffer_string_length(proc
->unixsocket
) + 1 + sizeof(fcgi_addr_un
.sun_family
);
1783 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_un
;
1785 if (buffer_string_is_empty(proc
->connection_name
)) {
1786 /* on remote spawing we have to set the connection-name now */
1787 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("unix:"));
1788 buffer_append_string_buffer(proc
->connection_name
, proc
->unixsocket
);
1791 return CONNECTION_DEAD
;
1793 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1794 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
1795 memset(&fcgi_addr_in6
, 0, sizeof(fcgi_addr_in6
));
1796 fcgi_addr_in6
.sin6_family
= AF_INET6
;
1797 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &fcgi_addr_in6
.sin6_addr
);
1798 fcgi_addr_in6
.sin6_port
= htons(proc
->port
);
1799 servlen
= sizeof(fcgi_addr_in6
);
1800 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in6
;
1803 memset(&fcgi_addr_in
, 0, sizeof(fcgi_addr_in
));
1804 fcgi_addr_in
.sin_family
= AF_INET
;
1805 if (!buffer_string_is_empty(host
->host
)) {
1806 if (0 == inet_aton(host
->host
->ptr
, &(fcgi_addr_in
.sin_addr
))) {
1807 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1808 "converting IP address failed for", host
->host
,
1809 "\nBe sure to specify an IP address here");
1811 return CONNECTION_DEAD
;
1814 fcgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1816 fcgi_addr_in
.sin_port
= htons(proc
->port
);
1817 servlen
= sizeof(fcgi_addr_in
);
1819 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in
;
1822 if (buffer_string_is_empty(proc
->unixsocket
)) {
1823 if (buffer_string_is_empty(proc
->connection_name
)) {
1824 /* on remote spawing we have to set the connection-name now */
1825 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("tcp:"));
1826 if (!buffer_string_is_empty(host
->host
)) {
1827 buffer_append_string_buffer(proc
->connection_name
, host
->host
);
1829 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN("localhost"));
1831 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN(":"));
1832 buffer_append_int(proc
->connection_name
, proc
->port
);
1836 if (-1 == connect(fcgi_fd
, fcgi_addr
, servlen
)) {
1837 if (errno
== EINPROGRESS
||
1838 errno
== EALREADY
||
1840 if (hctx
->conf
.debug
> 2) {
1841 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1842 "connect delayed; will continue later:", proc
->connection_name
);
1845 return CONNECTION_DELAYED
;
1846 } else if (errno
== EAGAIN
) {
1847 if (hctx
->conf
.debug
) {
1848 log_error_write(srv
, __FILE__
, __LINE__
, "sbsd",
1849 "This means that you have more incoming requests than your FastCGI backend can handle in parallel."
1850 "It might help to spawn more FastCGI backends or PHP children; if not, decrease server.max-connections."
1851 "The load for this FastCGI backend", proc
->connection_name
, "is", proc
->load
);
1854 return CONNECTION_OVERLOADED
;
1856 log_error_write(srv
, __FILE__
, __LINE__
, "sssb",
1858 strerror(errno
), "on",
1859 proc
->connection_name
);
1861 return CONNECTION_DEAD
;
1865 hctx
->reconnects
= 0;
1866 if (hctx
->conf
.debug
> 1) {
1867 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1868 "connect succeeded: ", fcgi_fd
);
1871 return CONNECTION_OK
;
1874 #define FCGI_ENV_ADD_CHECK(ret, con) \
1876 con->http_status = 400; \
1877 con->file_finished = 1; \
1880 static int fcgi_env_add_request_headers(server
*srv
, connection
*con
, plugin_data
*p
) {
1883 for (i
= 0; i
< con
->request
.headers
->used
; i
++) {
1886 ds
= (data_string
*)con
->request
.headers
->data
[i
];
1888 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
1889 buffer_copy_string_encoded_cgi_varnames(srv
->tmp_buf
, CONST_BUF_LEN(ds
->key
), 1);
1891 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_BUF_LEN(srv
->tmp_buf
), CONST_BUF_LEN(ds
->value
)),con
);
1895 for (i
= 0; i
< con
->environment
->used
; i
++) {
1898 ds
= (data_string
*)con
->environment
->data
[i
];
1900 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
1901 buffer_copy_string_encoded_cgi_varnames(srv
->tmp_buf
, CONST_BUF_LEN(ds
->key
), 0);
1903 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_BUF_LEN(srv
->tmp_buf
), CONST_BUF_LEN(ds
->value
)), con
);
1910 static int fcgi_create_env(server
*srv
, handler_ctx
*hctx
, size_t request_id
) {
1911 FCGI_BeginRequestRecord beginRecord
;
1914 char buf
[LI_ITOSTRING_LENGTH
];
1917 char b2
[INET6_ADDRSTRLEN
+ 1];
1920 plugin_data
*p
= hctx
->plugin_data
;
1921 fcgi_extension_host
*host
= hctx
->host
;
1923 connection
*con
= hctx
->remote_conn
;
1924 buffer
* const req_uri
= (con
->error_handler_saved_status
>= 0) ? con
->request
.uri
: con
->request
.orig_uri
;
1925 server_socket
*srv_sock
= con
->srv_socket
;
1928 socklen_t our_addr_len
;
1930 /* send FCGI_BEGIN_REQUEST */
1932 fcgi_header(&(beginRecord
.header
), FCGI_BEGIN_REQUEST
, request_id
, sizeof(beginRecord
.body
), 0);
1933 beginRecord
.body
.roleB0
= host
->mode
;
1934 beginRecord
.body
.roleB1
= 0;
1935 beginRecord
.body
.flags
= 0;
1936 memset(beginRecord
.body
.reserved
, 0, sizeof(beginRecord
.body
.reserved
));
1938 /* send FCGI_PARAMS */
1939 buffer_string_prepare_copy(p
->fcgi_env
, 1023);
1942 if (buffer_is_empty(con
->conf
.server_tag
)) {
1943 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_STR_LEN(PACKAGE_DESC
)),con
)
1945 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_BUF_LEN(con
->conf
.server_tag
)),con
)
1948 if (!buffer_is_empty(con
->server_name
)) {
1949 size_t len
= buffer_string_length(con
->server_name
);
1951 if (con
->server_name
->ptr
[0] == '[') {
1952 const char *colon
= strstr(con
->server_name
->ptr
, "]:");
1953 if (colon
) len
= (colon
+ 1) - con
->server_name
->ptr
;
1955 const char *colon
= strchr(con
->server_name
->ptr
, ':');
1956 if (colon
) len
= colon
- con
->server_name
->ptr
;
1959 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_NAME"), con
->server_name
->ptr
, len
),con
)
1962 s
= inet_ntop(srv_sock
->addr
.plain
.sa_family
,
1963 srv_sock
->addr
.plain
.sa_family
== AF_INET6
?
1964 (const void *) &(srv_sock
->addr
.ipv6
.sin6_addr
) :
1965 (const void *) &(srv_sock
->addr
.ipv4
.sin_addr
),
1968 s
= inet_ntoa(srv_sock
->addr
.ipv4
.sin_addr
);
1970 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_NAME"), s
, strlen(s
)),con
)
1973 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("GATEWAY_INTERFACE"), CONST_STR_LEN("CGI/1.1")),con
)
1975 li_utostrn(buf
, sizeof(buf
),
1977 ntohs(srv_sock
->addr
.plain
.sa_family
? srv_sock
->addr
.ipv6
.sin6_port
: srv_sock
->addr
.ipv4
.sin_port
)
1979 ntohs(srv_sock
->addr
.ipv4
.sin_port
)
1983 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_PORT"), buf
, strlen(buf
)),con
)
1985 /* get the server-side of the connection to the client */
1986 our_addr_len
= sizeof(our_addr
);
1988 if (-1 == getsockname(con
->fd
, (struct sockaddr
*)&our_addr
, &our_addr_len
)
1989 || our_addr_len
> sizeof(our_addr
)) {
1990 s
= inet_ntop_cache_get_ip(srv
, &(srv_sock
->addr
));
1992 s
= inet_ntop_cache_get_ip(srv
, &(our_addr
));
1994 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_ADDR"), s
, strlen(s
)),con
)
1996 li_utostrn(buf
, sizeof(buf
),
1998 ntohs(con
->dst_addr
.plain
.sa_family
? con
->dst_addr
.ipv6
.sin6_port
: con
->dst_addr
.ipv4
.sin_port
)
2000 ntohs(con
->dst_addr
.ipv4
.sin_port
)
2004 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REMOTE_PORT"), buf
, strlen(buf
)),con
)
2006 s
= inet_ntop_cache_get_ip(srv
, &(con
->dst_addr
));
2007 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REMOTE_ADDR"), s
, strlen(s
)),con
)
2009 if (con
->request
.content_length
> 0 && host
->mode
!= FCGI_AUTHORIZER
) {
2010 /* CGI-SPEC 6.1.2 and FastCGI spec 6.3 */
2012 li_itostrn(buf
, sizeof(buf
), con
->request
.content_length
);
2013 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("CONTENT_LENGTH"), buf
, strlen(buf
)),con
)
2016 if (host
->mode
!= FCGI_AUTHORIZER
) {
2018 * SCRIPT_NAME, PATH_INFO and PATH_TRANSLATED according to
2019 * http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html
2020 * (6.1.14, 6.1.6, 6.1.7)
2021 * For AUTHORIZER mode these headers should be omitted.
2024 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SCRIPT_NAME"), CONST_BUF_LEN(con
->uri
.path
)),con
)
2026 if (!buffer_string_is_empty(con
->request
.pathinfo
)) {
2027 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("PATH_INFO"), CONST_BUF_LEN(con
->request
.pathinfo
)),con
)
2029 /* PATH_TRANSLATED is only defined if PATH_INFO is set */
2031 if (!buffer_string_is_empty(host
->docroot
)) {
2032 buffer_copy_buffer(p
->path
, host
->docroot
);
2034 buffer_copy_buffer(p
->path
, con
->physical
.basedir
);
2036 buffer_append_string_buffer(p
->path
, con
->request
.pathinfo
);
2037 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("PATH_TRANSLATED"), CONST_BUF_LEN(p
->path
)),con
)
2039 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("PATH_INFO"), CONST_STR_LEN("")),con
)
2044 * SCRIPT_FILENAME and DOCUMENT_ROOT for php. The PHP manual
2045 * http://www.php.net/manual/en/reserved.variables.php
2046 * treatment of PATH_TRANSLATED is different from the one of CGI specs.
2047 * TODO: this code should be checked against cgi.fix_pathinfo php
2051 if (!buffer_string_is_empty(host
->docroot
)) {
2053 * rewrite SCRIPT_FILENAME
2057 buffer_copy_buffer(p
->path
, host
->docroot
);
2058 buffer_append_string_buffer(p
->path
, con
->uri
.path
);
2060 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p
->path
)),con
)
2061 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(host
->docroot
)),con
)
2063 buffer_copy_buffer(p
->path
, con
->physical
.path
);
2065 /* cgi.fix_pathinfo need a broken SCRIPT_FILENAME to find out what PATH_INFO is itself
2067 * see src/sapi/cgi_main.c, init_request_info()
2069 if (host
->break_scriptfilename_for_php
) {
2070 buffer_append_string_buffer(p
->path
, con
->request
.pathinfo
);
2073 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p
->path
)),con
)
2074 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(con
->physical
.basedir
)),con
)
2077 if (!buffer_string_is_empty(host
->strip_request_uri
)) {
2078 /* we need at least one char to strip off */
2082 * stripping /app1 or /app1/ should lead to
2088 if ('/' != host
->strip_request_uri
->ptr
[buffer_string_length(host
->strip_request_uri
) - 1]) {
2089 /* fix the user-input to have / as last char */
2090 buffer_append_string_len(host
->strip_request_uri
, CONST_STR_LEN("/"));
2093 if (buffer_string_length(req_uri
) >= buffer_string_length(host
->strip_request_uri
) &&
2094 0 == strncmp(req_uri
->ptr
, host
->strip_request_uri
->ptr
, buffer_string_length(host
->strip_request_uri
))) {
2095 /* the left is the same */
2097 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_URI"),
2098 req_uri
->ptr
+ (buffer_string_length(host
->strip_request_uri
) - 1),
2099 buffer_string_length(req_uri
) - (buffer_string_length(host
->strip_request_uri
) - 1)), con
)
2101 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(req_uri
)),con
)
2104 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(req_uri
)),con
)
2106 if (!buffer_string_is_empty(con
->uri
.query
)) {
2107 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("QUERY_STRING"), CONST_BUF_LEN(con
->uri
.query
)),con
)
2109 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("QUERY_STRING"), CONST_STR_LEN("")),con
)
2112 s
= get_http_method_name(con
->request
.http_method
);
2114 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_METHOD"), s
, strlen(s
)),con
)
2115 /* set REDIRECT_STATUS for php compiled with --force-redirect
2116 * (if REDIRECT_STATUS has not already been set by error handler) */
2117 if (0 == con
->error_handler_saved_status
) {
2118 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REDIRECT_STATUS"), CONST_STR_LEN("200")), con
);
2120 s
= get_http_version_name(con
->request
.http_version
);
2122 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_PROTOCOL"), s
, strlen(s
)),con
)
2124 if (buffer_is_equal_caseless_string(con
->uri
.scheme
, CONST_STR_LEN("https"))) {
2125 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on")),con
)
2128 FCGI_ENV_ADD_CHECK(fcgi_env_add_request_headers(srv
, con
, p
), con
);
2131 buffer
*b
= buffer_init();
2133 buffer_copy_string_len(b
, (const char *)&beginRecord
, sizeof(beginRecord
));
2135 fcgi_header(&(header
), FCGI_PARAMS
, request_id
, buffer_string_length(p
->fcgi_env
), 0);
2136 buffer_append_string_len(b
, (const char *)&header
, sizeof(header
));
2137 buffer_append_string_buffer(b
, p
->fcgi_env
);
2139 fcgi_header(&(header
), FCGI_PARAMS
, request_id
, 0, 0);
2140 buffer_append_string_len(b
, (const char *)&header
, sizeof(header
));
2142 chunkqueue_append_buffer(hctx
->wb
, b
);
2146 if (con
->request
.content_length
) {
2147 chunkqueue
*req_cq
= con
->request_content_queue
;
2150 /* something to send ? */
2151 for (offset
= 0; offset
!= req_cq
->bytes_in
; ) {
2152 off_t weWant
= req_cq
->bytes_in
- offset
> FCGI_MAX_LENGTH
? FCGI_MAX_LENGTH
: req_cq
->bytes_in
- offset
;
2154 /* we announce toWrite octets
2155 * now take all the request_content chunks that we need to fill this request
2158 fcgi_header(&(header
), FCGI_STDIN
, request_id
, weWant
, 0);
2159 chunkqueue_append_mem(hctx
->wb
, (const char *)&header
, sizeof(header
));
2161 if (p
->conf
.debug
> 10) {
2162 log_error_write(srv
, __FILE__
, __LINE__
, "soso", "tosend:", offset
, "/", req_cq
->bytes_in
);
2165 chunkqueue_steal(hctx
->wb
, req_cq
, weWant
);
2171 /* terminate STDIN */
2172 fcgi_header(&(header
), FCGI_STDIN
, request_id
, 0, 0);
2173 chunkqueue_append_mem(hctx
->wb
, (const char *)&header
, sizeof(header
));
2178 static int fcgi_response_parse(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*in
) {
2181 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
2182 fcgi_extension_host
*host
= hctx
->host
;
2183 int have_sendfile2
= 0;
2184 off_t sendfile2_content_length
= 0;
2189 for (s
= in
->ptr
; NULL
!= (ns
= strchr(s
, '\n')); s
= ns
+ 1) {
2193 /* a good day. Someone has read the specs and is sending a \r\n to us */
2203 if (NULL
== (value
= strchr(s
, ':'))) {
2204 /* we expect: "<key>: <value>\n" */
2208 key_len
= value
- key
;
2212 while (*value
== ' ' || *value
== '\t') value
++;
2214 if (host
->mode
!= FCGI_AUTHORIZER
||
2215 !(con
->http_status
== 0 ||
2216 con
->http_status
== 200)) {
2217 /* authorizers shouldn't affect the response headers sent back to the client */
2219 /* don't forward Status: */
2220 if (0 != strncasecmp(key
, "Status", key_len
)) {
2222 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
2223 ds
= data_response_init();
2225 buffer_copy_string_len(ds
->key
, key
, key_len
);
2226 buffer_copy_string(ds
->value
, value
);
2228 array_insert_unique(con
->response
.headers
, (data_unset
*)ds
);
2234 if (0 == strncasecmp(key
, "Date", key_len
)) {
2235 con
->parsed_response
|= HTTP_DATE
;
2239 if (0 == strncasecmp(key
, "Status", key_len
)) {
2240 int status
= strtol(value
, NULL
, 10);
2241 if (status
>= 100 && status
< 1000) {
2242 con
->http_status
= status
;
2243 con
->parsed_response
|= HTTP_STATUS
;
2245 con
->http_status
= 502;
2250 if (0 == strncasecmp(key
, "Location", key_len
)) {
2251 con
->parsed_response
|= HTTP_LOCATION
;
2255 if (0 == strncasecmp(key
, "Connection", key_len
)) {
2256 con
->response
.keep_alive
= (0 == strcasecmp(value
, "Keep-Alive")) ? 1 : 0;
2257 con
->parsed_response
|= HTTP_CONNECTION
;
2261 if (host
->xsendfile_allow
&& 0 == strncasecmp(key
, "X-Sendfile2", key_len
) && hctx
->send_content_body
) {
2266 char *filename
, *range
;
2267 stat_cache_entry
*sce
;
2268 off_t begin_range
, end_range
, range_len
;
2270 while (' ' == *pos
) pos
++;
2274 if (NULL
== (range
= strchr(pos
, ' '))) {
2276 if (p
->conf
.debug
) {
2277 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "Couldn't find range after filename:", filename
);
2281 buffer_copy_string_len(srv
->tmp_buf
, filename
, range
- filename
);
2283 /* find end of range */
2284 for (pos
= ++range
; *pos
&& *pos
!= ' ' && *pos
!= ','; pos
++) ;
2286 buffer_urldecode_path(srv
->tmp_buf
);
2287 buffer_path_simplify(srv
->tmp_buf
, srv
->tmp_buf
);
2288 if (con
->conf
.force_lowercase_filenames
) {
2289 buffer_to_lower(srv
->tmp_buf
);
2291 if (host
->xsendfile_docroot
->used
) {
2292 size_t i
, xlen
= buffer_string_length(srv
->tmp_buf
);
2293 for (i
= 0; i
< host
->xsendfile_docroot
->used
; ++i
) {
2294 data_string
*ds
= (data_string
*)host
->xsendfile_docroot
->data
[i
];
2295 size_t dlen
= buffer_string_length(ds
->value
);
2297 && (!con
->conf
.force_lowercase_filenames
2298 ? 0 == memcmp(srv
->tmp_buf
->ptr
, ds
->value
->ptr
, dlen
)
2299 : 0 == strncasecmp(srv
->tmp_buf
->ptr
, ds
->value
->ptr
, dlen
))) {
2303 if (i
== host
->xsendfile_docroot
->used
) {
2304 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
2305 "X-Sendfile2 (", srv
->tmp_buf
,
2306 ") not under configured x-sendfile-docroot(s)");
2311 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, srv
->tmp_buf
, &sce
)) {
2312 if (p
->conf
.debug
) {
2313 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
2314 "send-file error: couldn't get stat_cache entry for X-Sendfile2:",
2318 } else if (!S_ISREG(sce
->st
.st_mode
)) {
2319 if (p
->conf
.debug
) {
2320 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
2321 "send-file error: wrong filetype for X-Sendfile2:",
2326 /* found the file */
2329 begin_range
= 0; end_range
= sce
->st
.st_size
- 1;
2333 begin_range
= strtoll(range
, &rpos
, 10);
2334 if (errno
!= 0 || begin_range
< 0 || rpos
== range
) goto range_failed
;
2335 if ('-' != *rpos
++) goto range_failed
;
2338 end_range
= strtoll(range
, &rpos
, 10);
2339 if (errno
!= 0 || end_range
< 0 || rpos
== range
) goto range_failed
;
2341 if (rpos
!= pos
) goto range_failed
;
2346 if (p
->conf
.debug
) {
2347 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "Couldn't decode range after filename:", filename
);
2354 /* no parameters accepted */
2356 while (*pos
== ' ') pos
++;
2357 if (*pos
!= '\0' && *pos
!= ',') return 502;
2359 range_len
= end_range
- begin_range
+ 1;
2360 if (range_len
< 0) return 502;
2361 if (range_len
!= 0) {
2362 if (0 != http_chunk_append_file_range(srv
, con
, srv
->tmp_buf
, begin_range
, range_len
)) {
2366 sendfile2_content_length
+= range_len
;
2368 if (*pos
== ',') pos
++;
2373 if (0 == strncasecmp(key
, "Content-Length", key_len
)) {
2374 con
->response
.content_length
= strtoul(value
, NULL
, 10);
2375 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
2377 if (con
->response
.content_length
< 0) con
->response
.content_length
= 0;
2385 if (have_sendfile2
) {
2388 hctx
->send_content_body
= 0;
2389 joblist_append(srv
, con
);
2391 /* fix content-length */
2392 if (NULL
== (dcls
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
2393 dcls
= data_response_init();
2396 buffer_copy_string_len(dcls
->key
, "Content-Length", sizeof("Content-Length")-1);
2397 buffer_copy_int(dcls
->value
, sendfile2_content_length
);
2398 array_replace(con
->response
.headers
, (data_unset
*)dcls
);
2400 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
2401 con
->response
.content_length
= sendfile2_content_length
;
2404 /* CGI/1.1 rev 03 - 7.2.1.2 */
2405 if ((con
->parsed_response
& HTTP_LOCATION
) &&
2406 !(con
->parsed_response
& HTTP_STATUS
)) {
2407 con
->http_status
= 302;
2419 } fastcgi_response_packet
;
2421 static int fastcgi_get_packet(server
*srv
, handler_ctx
*hctx
, fastcgi_response_packet
*packet
) {
2425 FCGI_Header
*header
;
2427 if (!hctx
->rb
->first
) return -1;
2429 packet
->b
= buffer_init();
2432 packet
->padding
= 0;
2433 packet
->request_id
= 0;
2435 offset
= 0; toread
= 8;
2436 /* get at least the FastCGI header */
2437 for (c
= hctx
->rb
->first
; c
; c
= c
->next
) {
2438 size_t weHave
= buffer_string_length(c
->mem
) - c
->offset
;
2440 if (weHave
> toread
) weHave
= toread
;
2442 buffer_append_string_len(packet
->b
, c
->mem
->ptr
+ c
->offset
, weHave
);
2444 offset
= weHave
; /* skip offset bytes in chunk for "real" data */
2446 if (0 == toread
) break;
2449 if (buffer_string_length(packet
->b
) < sizeof(FCGI_Header
)) {
2451 if (hctx
->plugin_data
->conf
.debug
) {
2452 log_error_write(srv
, __FILE__
, __LINE__
, "sdsds", "FastCGI: header too small:", buffer_string_length(packet
->b
), "bytes <", sizeof(FCGI_Header
), "bytes, waiting for more data");
2455 buffer_free(packet
->b
);
2460 /* we have at least a header, now check how much me have to fetch */
2461 header
= (FCGI_Header
*)(packet
->b
->ptr
);
2463 packet
->len
= (header
->contentLengthB0
| (header
->contentLengthB1
<< 8)) + header
->paddingLength
;
2464 packet
->request_id
= (header
->requestIdB0
| (header
->requestIdB1
<< 8));
2465 packet
->type
= header
->type
;
2466 packet
->padding
= header
->paddingLength
;
2468 /* ->b should only be the content */
2469 buffer_string_set_length(packet
->b
, 0);
2472 /* copy the content */
2473 for (; c
&& (buffer_string_length(packet
->b
) < packet
->len
); c
= c
->next
) {
2474 size_t weWant
= packet
->len
- buffer_string_length(packet
->b
);
2475 size_t weHave
= buffer_string_length(c
->mem
) - c
->offset
- offset
;
2477 if (weHave
> weWant
) weHave
= weWant
;
2479 buffer_append_string_len(packet
->b
, c
->mem
->ptr
+ c
->offset
+ offset
, weHave
);
2481 /* we only skipped the first bytes as they belonged to the fcgi header */
2485 if (buffer_string_length(packet
->b
) < packet
->len
) {
2486 /* we didn't get the full packet */
2488 buffer_free(packet
->b
);
2492 buffer_string_set_length(packet
->b
, buffer_string_length(packet
->b
) - packet
->padding
);
2495 chunkqueue_mark_written(hctx
->rb
, packet
->len
+ sizeof(FCGI_Header
));
2500 static int fcgi_demux_response(server
*srv
, handler_ctx
*hctx
) {
2505 plugin_data
*p
= hctx
->plugin_data
;
2506 connection
*con
= hctx
->remote_conn
;
2507 int fcgi_fd
= hctx
->fd
;
2508 fcgi_extension_host
*host
= hctx
->host
;
2509 fcgi_proc
*proc
= hctx
->proc
;
2512 * check how much we have to read
2514 if (ioctl(hctx
->fd
, FIONREAD
, &toread
)) {
2515 if (errno
== EAGAIN
) return 0;
2516 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2517 "unexpected end-of-file (perhaps the fastcgi process died):",
2526 chunkqueue_get_memory(hctx
->rb
, &mem
, &mem_len
, 0, toread
);
2527 r
= read(hctx
->fd
, mem
, mem_len
);
2528 chunkqueue_use_memory(hctx
->rb
, r
> 0 ? r
: 0);
2531 if (errno
== EAGAIN
) return 0;
2532 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
2533 "unexpected end-of-file (perhaps the fastcgi process died):",
2534 fcgi_fd
, strerror(errno
));
2538 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsb",
2539 "unexpected end-of-file (perhaps the fastcgi process died):",
2541 "socket:", proc
->connection_name
);
2547 * parse the fastcgi packets and forward the content to the write-queue
2551 fastcgi_response_packet packet
;
2553 /* check if we have at least one packet */
2554 if (0 != fastcgi_get_packet(srv
, hctx
, &packet
)) {
2555 /* no full packet */
2559 switch(packet
.type
) {
2561 if (packet
.len
== 0) break;
2563 /* is the header already finished */
2564 if (0 == con
->file_started
) {
2568 /* search for header terminator
2570 * if we start with \r\n check if last packet terminated with \r\n
2571 * if we start with \n check if last packet terminated with \n
2572 * search for \r\n\r\n
2576 buffer_append_string_buffer(hctx
->response_header
, packet
.b
);
2578 if (NULL
!= (c
= buffer_search_string_len(hctx
->response_header
, CONST_STR_LEN("\r\n\r\n")))) {
2579 char *hend
= c
+ 4; /* header end == body start */
2580 size_t hlen
= hend
- hctx
->response_header
->ptr
;
2581 buffer_copy_string_len(packet
.b
, hend
, buffer_string_length(hctx
->response_header
) - hlen
);
2582 buffer_string_set_length(hctx
->response_header
, hlen
);
2583 } else if (NULL
!= (c
= buffer_search_string_len(hctx
->response_header
, CONST_STR_LEN("\n\n")))) {
2584 char *hend
= c
+ 2; /* header end == body start */
2585 size_t hlen
= hend
- hctx
->response_header
->ptr
;
2586 buffer_copy_string_len(packet
.b
, hend
, buffer_string_length(hctx
->response_header
) - hlen
);
2587 buffer_string_set_length(hctx
->response_header
, hlen
);
2589 /* no luck, no header found */
2593 /* parse the response header */
2594 if ((ret
= fcgi_response_parse(srv
, con
, p
, hctx
->response_header
))) {
2595 con
->http_status
= ret
;
2596 hctx
->send_content_body
= 0;
2597 con
->file_started
= 1;
2601 con
->file_started
= 1;
2603 if (host
->mode
== FCGI_AUTHORIZER
&&
2604 (con
->http_status
== 0 ||
2605 con
->http_status
== 200)) {
2606 /* a authorizer with approved the static request, ignore the content here */
2607 hctx
->send_content_body
= 0;
2610 if (host
->xsendfile_allow
&& hctx
->send_content_body
&&
2611 (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-LIGHTTPD-send-file"))
2612 || NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-Sendfile")))) {
2613 http_response_xsendfile(srv
, con
, ds
->value
, host
->xsendfile_docroot
);
2614 if (con
->mode
== DIRECT
) {
2618 hctx
->send_content_body
= 0; /* ignore the content */
2622 /* enable chunked-transfer-encoding */
2623 if (con
->request
.http_version
== HTTP_VERSION_1_1
&&
2624 !(con
->parsed_response
& HTTP_CONTENT_LENGTH
)) {
2625 con
->response
.transfer_encoding
= HTTP_TRANSFER_ENCODING_CHUNKED
;
2629 if (hctx
->send_content_body
&& !buffer_string_is_empty(packet
.b
)) {
2630 http_chunk_append_buffer(srv
, con
, packet
.b
);
2634 if (packet
.len
== 0) break;
2636 log_error_write_multiline_buffer(srv
, __FILE__
, __LINE__
, packet
.b
, "s",
2640 case FCGI_END_REQUEST
:
2641 con
->file_finished
= 1;
2643 if (host
->mode
!= FCGI_AUTHORIZER
||
2644 !(con
->http_status
== 0 ||
2645 con
->http_status
== 200)) {
2646 /* send chunk-end if necessary */
2647 http_chunk_close(srv
, con
);
2653 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2654 "FastCGI: header.type not handled: ", packet
.type
);
2657 buffer_free(packet
.b
);
2663 static int fcgi_restart_dead_procs(server
*srv
, plugin_data
*p
, fcgi_extension_host
*host
) {
2666 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
2669 if (p
->conf
.debug
> 2) {
2670 log_error_write(srv
, __FILE__
, __LINE__
, "sbdddd",
2672 proc
->connection_name
,
2680 * if the remote side is overloaded, we check back after <n> seconds
2683 switch (proc
->state
) {
2684 case PROC_STATE_KILLED
:
2685 case PROC_STATE_UNSET
:
2686 /* this should never happen as long as adaptive spawing is disabled */
2690 case PROC_STATE_RUNNING
:
2692 case PROC_STATE_OVERLOADED
:
2693 if (srv
->cur_ts
<= proc
->disabled_until
) break;
2695 proc
->state
= PROC_STATE_RUNNING
;
2696 host
->active_procs
++;
2698 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb",
2699 "fcgi-server re-enabled:",
2700 host
->host
, host
->port
,
2703 case PROC_STATE_DIED_WAIT_FOR_PID
:
2704 /* non-local procs don't have PIDs to wait for */
2705 if (!proc
->is_local
) {
2706 proc
->state
= PROC_STATE_DIED
;
2708 /* the child should not terminate at all */
2711 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
2713 /* child is still alive */
2714 if (srv
->cur_ts
<= proc
->disabled_until
) break;
2716 proc
->state
= PROC_STATE_RUNNING
;
2717 host
->active_procs
++;
2719 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb",
2720 "fcgi-server re-enabled:",
2721 host
->host
, host
->port
,
2725 if (errno
== EINTR
) continue;
2727 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2728 "child died somehow, waitpid failed:",
2730 proc
->state
= PROC_STATE_DIED
;
2733 if (WIFEXITED(status
)) {
2735 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
2736 "child exited, pid:", proc
->pid
,
2737 "status:", WEXITSTATUS(status
));
2739 } else if (WIFSIGNALED(status
)) {
2740 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2744 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2745 "child died somehow:",
2749 proc
->state
= PROC_STATE_DIED
;
2756 /* fall through if we have a dead proc now */
2757 if (proc
->state
!= PROC_STATE_DIED
) break;
2759 case PROC_STATE_DIED
:
2760 /* local procs get restarted by us,
2761 * remote ones hopefully by the admin */
2763 if (!buffer_string_is_empty(host
->bin_path
)) {
2764 /* we still have connections bound to this proc,
2765 * let them terminate first */
2766 if (proc
->load
!= 0) break;
2768 /* restart the child */
2770 if (p
->conf
.debug
) {
2771 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsd",
2772 "--- fastcgi spawning",
2773 "\n\tsocket", proc
->connection_name
,
2774 "\n\tcurrent:", 1, "/", host
->max_procs
);
2777 if (fcgi_spawn_connection(srv
, p
, host
, proc
)) {
2778 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2779 "ERROR: spawning fcgi failed.");
2780 return HANDLER_ERROR
;
2783 if (srv
->cur_ts
<= proc
->disabled_until
) break;
2785 proc
->state
= PROC_STATE_RUNNING
;
2786 host
->active_procs
++;
2788 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
2789 "fcgi-server re-enabled:",
2790 proc
->connection_name
);
2799 static handler_t
fcgi_write_request(server
*srv
, handler_ctx
*hctx
) {
2800 plugin_data
*p
= hctx
->plugin_data
;
2801 fcgi_extension_host
*host
= hctx
->host
;
2802 connection
*con
= hctx
->remote_conn
;
2810 * - tcp socket (do not check host->host->uses, as it may be not set which means INADDR_LOOPBACK)
2814 log_error_write(srv
, __FILE__
, __LINE__
, "s", "fatal error: host = NULL");
2815 return HANDLER_ERROR
;
2817 if ((!host
->port
&& buffer_string_is_empty(host
->unixsocket
))) {
2818 log_error_write(srv
, __FILE__
, __LINE__
, "s", "fatal error: neither host->port nor host->unixsocket is set");
2819 return HANDLER_ERROR
;
2822 /* we can't handle this in the switch as we have to fall through in it */
2823 if (hctx
->state
== FCGI_STATE_CONNECT_DELAYED
) {
2825 socklen_t socket_error_len
= sizeof(socket_error
);
2827 /* try to finish the connect() */
2828 if (0 != getsockopt(hctx
->fd
, SOL_SOCKET
, SO_ERROR
, &socket_error
, &socket_error_len
)) {
2829 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2830 "getsockopt failed:", strerror(errno
));
2832 fcgi_host_disable(srv
, hctx
);
2834 return HANDLER_ERROR
;
2836 if (socket_error
!= 0) {
2837 if (!hctx
->proc
->is_local
|| p
->conf
.debug
) {
2838 /* local procs get restarted */
2840 log_error_write(srv
, __FILE__
, __LINE__
, "sssb",
2841 "establishing connection failed:", strerror(socket_error
),
2842 "socket:", hctx
->proc
->connection_name
);
2845 fcgi_host_disable(srv
, hctx
);
2846 log_error_write(srv
, __FILE__
, __LINE__
, "sdssdsd",
2847 "backend is overloaded; we'll disable it for", hctx
->host
->disable_time
, "seconds and send the request to another backend instead:",
2848 "reconnects:", hctx
->reconnects
,
2849 "load:", host
->load
);
2851 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
2852 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".died"));
2854 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
2856 return HANDLER_ERROR
;
2858 /* go on with preparing the request */
2859 hctx
->state
= FCGI_STATE_PREPARE_WRITE
;
2863 switch(hctx
->state
) {
2864 case FCGI_STATE_CONNECT_DELAYED
:
2865 /* should never happen */
2866 return HANDLER_WAIT_FOR_EVENT
;
2867 case FCGI_STATE_INIT
:
2868 /* do we have a running process for this host (max-procs) ? */
2871 for (proc
= hctx
->host
->first
;
2872 proc
&& proc
->state
!= PROC_STATE_RUNNING
;
2875 /* all children are dead */
2879 return HANDLER_ERROR
;
2884 /* check the other procs if they have a lower load */
2885 for (proc
= proc
->next
; proc
; proc
= proc
->next
) {
2886 if (proc
->state
!= PROC_STATE_RUNNING
) continue;
2887 if (proc
->load
< hctx
->proc
->load
) hctx
->proc
= proc
;
2890 if (-1 == (hctx
->fd
= socket(host
->family
, SOCK_STREAM
, 0))) {
2891 if (errno
== EMFILE
||
2893 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2894 "wait for fd at connection:", con
->fd
);
2896 return HANDLER_WAIT_FOR_FD
;
2899 log_error_write(srv
, __FILE__
, __LINE__
, "ssdd",
2900 "socket failed:", strerror(errno
), srv
->cur_fds
, srv
->max_fds
);
2901 return HANDLER_ERROR
;
2907 fdevent_register(srv
->ev
, hctx
->fd
, fcgi_handle_fdevent
, hctx
);
2909 if (-1 == fdevent_fcntl_set(srv
->ev
, hctx
->fd
)) {
2910 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2911 "fcntl failed:", strerror(errno
));
2913 return HANDLER_ERROR
;
2916 if (hctx
->proc
->is_local
) {
2917 hctx
->pid
= hctx
->proc
->pid
;
2920 switch (fcgi_establish_connection(srv
, hctx
)) {
2921 case CONNECTION_DELAYED
:
2922 /* connection is in progress, wait for an event and call getsockopt() below */
2924 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2926 fcgi_set_state(srv
, hctx
, FCGI_STATE_CONNECT_DELAYED
);
2927 return HANDLER_WAIT_FOR_EVENT
;
2928 case CONNECTION_OVERLOADED
:
2929 /* cool down the backend, it is overloaded
2932 if (hctx
->host
->disable_time
) {
2933 log_error_write(srv
, __FILE__
, __LINE__
, "sdssdsd",
2934 "backend is overloaded; we'll disable it for", hctx
->host
->disable_time
, "seconds and send the request to another backend instead:",
2935 "reconnects:", hctx
->reconnects
,
2936 "load:", host
->load
);
2938 hctx
->proc
->disabled_until
= srv
->cur_ts
+ hctx
->host
->disable_time
;
2939 if (hctx
->proc
->state
== PROC_STATE_RUNNING
) hctx
->host
->active_procs
--;
2940 hctx
->proc
->state
= PROC_STATE_OVERLOADED
;
2943 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
2944 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".overloaded"));
2946 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
2948 return HANDLER_ERROR
;
2949 case CONNECTION_DEAD
:
2950 /* we got a hard error from the backend like
2951 * - ECONNREFUSED for tcp-ip sockets
2952 * - ENOENT for unix-domain-sockets
2954 * for check if the host is back in hctx->host->disable_time seconds
2957 fcgi_host_disable(srv
, hctx
);
2959 log_error_write(srv
, __FILE__
, __LINE__
, "sdssdsd",
2960 "backend died; we'll disable it for", hctx
->host
->disable_time
, "seconds and send the request to another backend instead:",
2961 "reconnects:", hctx
->reconnects
,
2962 "load:", host
->load
);
2964 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
2965 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".died"));
2967 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
2969 return HANDLER_ERROR
;
2971 /* everything is ok, go on */
2973 fcgi_set_state(srv
, hctx
, FCGI_STATE_PREPARE_WRITE
);
2978 case FCGI_STATE_PREPARE_WRITE
:
2979 /* ok, we have the connection */
2981 fcgi_proc_load_inc(srv
, hctx
);
2984 status_counter_inc(srv
, CONST_STR_LEN("fastcgi.requests"));
2986 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
2987 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".connected"));
2989 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
2991 if (p
->conf
.debug
) {
2992 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsd",
2994 "pid:", hctx
->proc
->pid
,
2995 "socket:", hctx
->proc
->connection_name
,
2996 "load:", hctx
->proc
->load
);
2999 /* move the proc-list entry down the list */
3000 if (hctx
->request_id
== 0) {
3001 hctx
->request_id
= 1; /* always use id 1 as we don't use multiplexing */
3003 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3004 "fcgi-request is already in use:", hctx
->request_id
);
3007 if (-1 == fcgi_create_env(srv
, hctx
, hctx
->request_id
)) return HANDLER_ERROR
;
3009 fcgi_set_state(srv
, hctx
, FCGI_STATE_WRITE
);
3011 case FCGI_STATE_WRITE
:
3012 ret
= srv
->network_backend_write(srv
, con
, hctx
->fd
, hctx
->wb
, MAX_WRITE_LIMIT
);
3014 chunkqueue_remove_finished_chunks(hctx
->wb
);
3021 /* the connection got dropped after accept()
3022 * we don't care about that - if you accept() it, you have to handle it.
3025 log_error_write(srv
, __FILE__
, __LINE__
, "ssosb",
3026 "connection was dropped after accept() (perhaps the fastcgi process died),",
3027 "write-offset:", hctx
->wb
->bytes_out
,
3028 "socket:", hctx
->proc
->connection_name
);
3030 return HANDLER_ERROR
;
3032 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
3033 "write failed:", strerror(errno
), errno
);
3035 return HANDLER_ERROR
;
3039 if (hctx
->wb
->bytes_out
== hctx
->wb
->bytes_in
) {
3040 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
3041 fcgi_set_state(srv
, hctx
, FCGI_STATE_READ
);
3043 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
|FDEVENT_OUT
);
3046 return HANDLER_WAIT_FOR_EVENT
;
3047 case FCGI_STATE_READ
:
3048 /* waiting for a response */
3049 return HANDLER_WAIT_FOR_EVENT
;
3051 log_error_write(srv
, __FILE__
, __LINE__
, "s", "(debug) unknown state");
3052 return HANDLER_ERROR
;
3057 /* might be called on fdevent after a connect() is delay too
3059 static handler_t
fcgi_send_request(server
*srv
, handler_ctx
*hctx
) {
3060 fcgi_extension_host
*host
;
3063 /* we don't have a host yet, choose one
3064 * -> this happens in the first round
3065 * and when the host died and we have to select a new one */
3066 if (hctx
->host
== NULL
) {
3070 /* check if the next server has no load. */
3071 ndx
= hctx
->ext
->last_used_ndx
+ 1;
3072 if(ndx
>= (int) hctx
->ext
->used
|| ndx
< 0) ndx
= 0;
3073 host
= hctx
->ext
->hosts
[ndx
];
3074 if (host
->load
> 0) {
3075 /* get backend with the least load. */
3076 for (k
= 0, ndx
= -1; k
< hctx
->ext
->used
; k
++) {
3077 host
= hctx
->ext
->hosts
[k
];
3079 /* we should have at least one proc that can do something */
3080 if (host
->active_procs
== 0) continue;
3082 if (used
== -1 || host
->load
< used
) {
3090 /* found a server */
3092 /* all hosts are down */
3094 fcgi_connection_close(srv
, hctx
);
3096 return HANDLER_FINISHED
;
3099 hctx
->ext
->last_used_ndx
= ndx
;
3100 host
= hctx
->ext
->hosts
[ndx
];
3103 * if check-local is disabled, use the uri.path handler
3107 /* init handler-context */
3109 /* we put a connection on this host, move the other new connections to other hosts
3111 * as soon as hctx->host is unassigned, decrease the load again */
3112 fcgi_host_assign(srv
, hctx
, host
);
3118 /* ok, create the request */
3119 rc
= fcgi_write_request(srv
, hctx
);
3120 if (HANDLER_ERROR
!= rc
) {
3123 plugin_data
*p
= hctx
->plugin_data
;
3124 connection
*con
= hctx
->remote_conn
;
3126 if (hctx
->state
== FCGI_STATE_INIT
||
3127 hctx
->state
== FCGI_STATE_CONNECT_DELAYED
) {
3128 fcgi_restart_dead_procs(srv
, p
, host
);
3130 /* cleanup this request and let the request handler start this request again */
3131 if (hctx
->reconnects
< 5) {
3132 fcgi_reconnect(srv
, hctx
);
3134 return HANDLER_COMEBACK
;
3136 fcgi_connection_close(srv
, hctx
);
3137 con
->http_status
= 503;
3139 return HANDLER_FINISHED
;
3142 int status
= con
->http_status
;
3143 fcgi_connection_close(srv
, hctx
);
3144 con
->http_status
= (status
== 400) ? 400 : 503; /* see FCGI_ENV_ADD_CHECK() for 400 error */
3146 return HANDLER_FINISHED
;
3152 SUBREQUEST_FUNC(mod_fastcgi_handle_subrequest
) {
3153 plugin_data
*p
= p_d
;
3155 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
3157 if (NULL
== hctx
) return HANDLER_GO_ON
;
3160 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
3162 if (con
->state
== CON_STATE_READ_POST
) {
3163 handler_t r
= connection_handle_read_post_state(srv
, con
);
3164 if (r
!= HANDLER_GO_ON
) return r
;
3167 return (hctx
->state
!= FCGI_STATE_READ
)
3168 ? fcgi_send_request(srv
, hctx
)
3169 : HANDLER_WAIT_FOR_EVENT
;
3172 static handler_t
fcgi_handle_fdevent(server
*srv
, void *ctx
, int revents
) {
3173 handler_ctx
*hctx
= ctx
;
3174 connection
*con
= hctx
->remote_conn
;
3175 plugin_data
*p
= hctx
->plugin_data
;
3177 fcgi_proc
*proc
= hctx
->proc
;
3178 fcgi_extension_host
*host
= hctx
->host
;
3180 joblist_append(srv
, con
);
3182 if ((revents
& FDEVENT_IN
) &&
3183 hctx
->state
== FCGI_STATE_READ
) {
3184 switch (fcgi_demux_response(srv
, hctx
)) {
3189 if (host
->mode
== FCGI_AUTHORIZER
&&
3190 (con
->http_status
== 200 ||
3191 con
->http_status
== 0)) {
3193 * If we are here in AUTHORIZER mode then a request for authorizer
3194 * was processed already, and status 200 has been returned. We need
3195 * now to handle authorized request.
3198 buffer_copy_buffer(con
->physical
.doc_root
, host
->docroot
);
3199 buffer_copy_buffer(con
->physical
.basedir
, host
->docroot
);
3201 buffer_copy_buffer(con
->physical
.path
, host
->docroot
);
3202 buffer_append_string_buffer(con
->physical
.path
, con
->uri
.path
);
3204 con
->mode
= DIRECT
;/*(avoid changing con->state, con->http_status)*/
3205 fcgi_connection_close(srv
, hctx
);
3206 con
->http_status
= 0;
3207 con
->file_started
= 1; /* fcgi_extension won't touch the request afterwards */
3210 fcgi_connection_close(srv
, hctx
);
3213 return HANDLER_FINISHED
;
3215 if (proc
->pid
&& proc
->state
!= PROC_STATE_DIED
) {
3218 /* only fetch the zombie if it is not already done */
3220 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
3222 /* child is still alive */
3227 /* the child should not terminate at all */
3228 if (WIFEXITED(status
)) {
3229 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
3230 "child exited, pid:", proc
->pid
,
3231 "status:", WEXITSTATUS(status
));
3232 } else if (WIFSIGNALED(status
)) {
3233 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3237 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3238 "child died somehow:",
3242 if (p
->conf
.debug
) {
3243 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsd",
3244 "--- fastcgi spawning",
3245 "\n\tsocket", proc
->connection_name
,
3246 "\n\tcurrent:", 1, "/", host
->max_procs
);
3249 if (fcgi_spawn_connection(srv
, p
, host
, proc
)) {
3250 /* respawning failed, retry later */
3251 proc
->state
= PROC_STATE_DIED
;
3253 log_error_write(srv
, __FILE__
, __LINE__
, "s",
3254 "respawning failed, will retry later");
3261 if (con
->file_started
== 0) {
3262 /* nothing has been sent out yet, try to use another child */
3264 if (hctx
->wb
->bytes_out
== 0 &&
3265 hctx
->reconnects
< 5) {
3267 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsBSBs",
3268 "response not received, request not sent",
3269 "on socket:", proc
->connection_name
,
3270 "for", con
->uri
.path
, "?", con
->uri
.query
, ", reconnecting");
3272 fcgi_reconnect(srv
, hctx
);
3274 return HANDLER_COMEBACK
;
3277 log_error_write(srv
, __FILE__
, __LINE__
, "sosbsBSBs",
3278 "response not received, request sent:", hctx
->wb
->bytes_out
,
3279 "on socket:", proc
->connection_name
,
3280 "for", con
->uri
.path
, "?", con
->uri
.query
, ", closing connection");
3282 fcgi_connection_close(srv
, hctx
);
3284 /* response might have been already started, kill the connection */
3285 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsBSBs",
3286 "response already sent out, but backend returned error",
3287 "on socket:", proc
->connection_name
,
3288 "for", con
->uri
.path
, "?", con
->uri
.query
, ", terminating connection");
3290 con
->keep_alive
= 0;
3291 con
->file_finished
= 1;
3292 con
->mode
= DIRECT
; /*(avoid sending final chunked block)*/
3293 fcgi_connection_close(srv
, hctx
);
3296 return HANDLER_FINISHED
;
3300 if (revents
& FDEVENT_OUT
) {
3301 if (hctx
->state
== FCGI_STATE_CONNECT_DELAYED
||
3302 hctx
->state
== FCGI_STATE_WRITE
) {
3303 /* we are allowed to send something out
3305 * 1. in an unfinished connect() call
3306 * 2. in an unfinished write() call (long POST request)
3308 return fcgi_send_request(srv
, hctx
); /*(might invalidate hctx)*/
3310 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3311 "got a FDEVENT_OUT and didn't know why:",
3316 /* perhaps this issue is already handled */
3317 if (revents
& FDEVENT_HUP
) {
3318 if (hctx
->state
== FCGI_STATE_CONNECT_DELAYED
) {
3319 /* getoptsock will catch this one (right ?)
3321 * if we are in connect we might get an EINPROGRESS
3322 * in the first call and an FDEVENT_HUP in the
3325 * FIXME: as it is a bit ugly.
3328 fcgi_send_request(srv
, hctx
);
3329 } else if (hctx
->state
== FCGI_STATE_READ
&&
3330 hctx
->proc
->port
== 0) {
3333 * ioctl says 8192 bytes to read from PHP and we receive directly a HUP for the socket
3334 * even if the FCGI_FIN packet is not received yet
3337 log_error_write(srv
, __FILE__
, __LINE__
, "sBSbsbsd",
3338 "error: unexpected close of fastcgi connection for",
3339 con
->uri
.path
, "?", con
->uri
.query
,
3340 "(no fastcgi process on socket:", proc
->connection_name
, "?)",
3343 fcgi_connection_close(srv
, hctx
);
3345 } else if (revents
& FDEVENT_ERR
) {
3346 log_error_write(srv
, __FILE__
, __LINE__
, "s",
3347 "fcgi: got a FDEVENT_ERR. Don't know why.");
3349 if (con
->file_started
) {
3350 con
->keep_alive
= 0;
3351 con
->file_finished
= 1;
3352 con
->mode
= DIRECT
; /*(avoid sending final chunked block)*/
3354 fcgi_connection_close(srv
, hctx
);
3357 return HANDLER_FINISHED
;
3362 static int fcgi_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
3364 plugin_config
*s
= p
->config_storage
[0];
3370 /* skip the first, the global context */
3371 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
3372 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
3373 s
= p
->config_storage
[i
];
3375 /* condition didn't match */
3376 if (!config_check_cond(srv
, con
, dc
)) continue;
3379 for (j
= 0; j
< dc
->value
->used
; j
++) {
3380 data_unset
*du
= dc
->value
->data
[j
];
3382 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("fastcgi.server"))) {
3384 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("fastcgi.debug"))) {
3386 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("fastcgi.map-extensions"))) {
3397 static handler_t
fcgi_check_extension(server
*srv
, connection
*con
, void *p_d
, int uri_path_handler
) {
3398 plugin_data
*p
= p_d
;
3402 fcgi_extension
*extension
= NULL
;
3403 fcgi_extension_host
*host
= NULL
;
3405 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
3407 /* Possibly, we processed already this request */
3408 if (con
->file_started
== 1) return HANDLER_GO_ON
;
3410 fn
= uri_path_handler
? con
->uri
.path
: con
->physical
.path
;
3412 if (buffer_string_is_empty(fn
)) return HANDLER_GO_ON
;
3414 s_len
= buffer_string_length(fn
);
3416 fcgi_patch_connection(srv
, con
, p
);
3418 /* fastcgi.map-extensions maps extensions to existing fastcgi.server entries
3420 * fastcgi.map-extensions = ( ".php3" => ".php" )
3422 * fastcgi.server = ( ".php" => ... )
3426 /* check if extension-mapping matches */
3427 for (k
= 0; k
< p
->conf
.ext_mapping
->used
; k
++) {
3428 data_string
*ds
= (data_string
*)p
->conf
.ext_mapping
->data
[k
];
3429 size_t ct_len
; /* length of the config entry */
3431 if (buffer_is_empty(ds
->key
)) continue;
3433 ct_len
= buffer_string_length(ds
->key
);
3435 if (s_len
< ct_len
) continue;
3437 /* found a mapping */
3438 if (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ds
->key
->ptr
, ct_len
)) {
3439 /* check if we know the extension */
3441 /* we can reuse k here */
3442 for (k
= 0; k
< p
->conf
.exts
->used
; k
++) {
3443 extension
= p
->conf
.exts
->exts
[k
];
3445 if (buffer_is_equal(ds
->value
, extension
->key
)) {
3450 if (k
== p
->conf
.exts
->used
) {
3458 if (extension
== NULL
) {
3459 size_t uri_path_len
= buffer_string_length(con
->uri
.path
);
3461 /* check if extension matches */
3462 for (k
= 0; k
< p
->conf
.exts
->used
; k
++) {
3463 size_t ct_len
; /* length of the config entry */
3464 fcgi_extension
*ext
= p
->conf
.exts
->exts
[k
];
3466 if (buffer_is_empty(ext
->key
)) continue;
3468 ct_len
= buffer_string_length(ext
->key
);
3470 /* check _url_ in the form "/fcgi_pattern" */
3471 if (ext
->key
->ptr
[0] == '/') {
3472 if ((ct_len
<= uri_path_len
) &&
3473 (strncmp(con
->uri
.path
->ptr
, ext
->key
->ptr
, ct_len
) == 0)) {
3477 } else if ((ct_len
<= s_len
) && (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ext
->key
->ptr
, ct_len
))) {
3478 /* check extension in the form ".fcg" */
3483 /* extension doesn't match */
3484 if (NULL
== extension
) {
3485 return HANDLER_GO_ON
;
3489 /* check if we have at least one server for this extension up and running */
3490 for (k
= 0; k
< extension
->used
; k
++) {
3491 fcgi_extension_host
*h
= extension
->hosts
[k
];
3493 /* we should have at least one proc that can do something */
3494 if (h
->active_procs
== 0) {
3498 /* we found one host that is alive */
3504 /* sorry, we don't have a server alive for this ext */
3505 con
->http_status
= 500;
3508 /* only send the 'no handler' once */
3509 if (!extension
->note_is_sent
) {
3510 extension
->note_is_sent
= 1;
3512 log_error_write(srv
, __FILE__
, __LINE__
, "sBSbsbs",
3513 "all handlers for", con
->uri
.path
, "?", con
->uri
.query
,
3514 "on", extension
->key
,
3518 return HANDLER_FINISHED
;
3521 /* a note about no handler is not sent yet */
3522 extension
->note_is_sent
= 0;
3525 * if check-local is disabled, use the uri.path handler
3529 /* init handler-context */
3530 if (uri_path_handler
) {
3531 if (host
->check_local
== 0) {
3535 hctx
= handler_ctx_init();
3537 hctx
->remote_conn
= con
;
3538 hctx
->plugin_data
= p
;
3540 hctx
->ext
= extension
;
3543 hctx
->conf
.exts
= p
->conf
.exts
;
3544 hctx
->conf
.debug
= p
->conf
.debug
;
3546 con
->plugin_ctx
[p
->id
] = hctx
;
3550 if (con
->conf
.log_request_handling
) {
3551 log_error_write(srv
, __FILE__
, __LINE__
, "s",
3552 "handling it in mod_fastcgi");
3555 /* do not split path info for authorizer */
3556 if (host
->mode
!= FCGI_AUTHORIZER
) {
3557 /* the prefix is the SCRIPT_NAME,
3558 * everything from start to the next slash
3559 * this is important for check-local = "disable"
3561 * if prefix = /admin.fcgi
3563 * /admin.fcgi/foo/bar
3565 * SCRIPT_NAME = /admin.fcgi
3566 * PATH_INFO = /foo/bar
3568 * if prefix = /fcgi-bin/
3572 * SCRIPT_NAME = /fcgi-bin/foo
3575 * if prefix = /, and fix-root-path-name is enable
3579 * SCRIPT_NAME = /fcgi-bin/foo
3584 /* the rewrite is only done for /prefix/? matches */
3585 if (host
->fix_root_path_name
&& extension
->key
->ptr
[0] == '/' && extension
->key
->ptr
[1] == '\0') {
3586 buffer_copy_string(con
->request
.pathinfo
, con
->uri
.path
->ptr
);
3587 buffer_string_set_length(con
->uri
.path
, 0);
3588 } else if (extension
->key
->ptr
[0] == '/' &&
3589 buffer_string_length(con
->uri
.path
) > buffer_string_length(extension
->key
) &&
3590 NULL
!= (pathinfo
= strchr(con
->uri
.path
->ptr
+ buffer_string_length(extension
->key
), '/'))) {
3591 /* rewrite uri.path and pathinfo */
3593 buffer_copy_string(con
->request
.pathinfo
, pathinfo
);
3594 buffer_string_set_length(con
->uri
.path
, buffer_string_length(con
->uri
.path
) - buffer_string_length(con
->request
.pathinfo
));
3600 hctx
= handler_ctx_init();
3602 hctx
->remote_conn
= con
;
3603 hctx
->plugin_data
= p
;
3605 hctx
->ext
= extension
;
3607 hctx
->conf
.exts
= p
->conf
.exts
;
3608 hctx
->conf
.debug
= p
->conf
.debug
;
3610 con
->plugin_ctx
[p
->id
] = hctx
;
3614 if (con
->conf
.log_request_handling
) {
3615 log_error_write(srv
, __FILE__
, __LINE__
, "s", "handling it in mod_fastcgi");
3619 return HANDLER_GO_ON
;
3622 /* uri-path handler */
3623 static handler_t
fcgi_check_extension_1(server
*srv
, connection
*con
, void *p_d
) {
3624 return fcgi_check_extension(srv
, con
, p_d
, 1);
3627 /* start request handler */
3628 static handler_t
fcgi_check_extension_2(server
*srv
, connection
*con
, void *p_d
) {
3629 return fcgi_check_extension(srv
, con
, p_d
, 0);
3633 TRIGGER_FUNC(mod_fastcgi_handle_trigger
) {
3634 plugin_data
*p
= p_d
;
3638 /* perhaps we should kill a connect attempt after 10-15 seconds
3640 * currently we wait for the TCP timeout which is 180 seconds on Linux
3646 /* check all children if they are still up */
3648 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
3649 plugin_config
*conf
;
3652 conf
= p
->config_storage
[i
];
3656 for (j
= 0; j
< exts
->used
; j
++) {
3661 for (n
= 0; n
< ex
->used
; n
++) {
3664 fcgi_extension_host
*host
;
3666 host
= ex
->hosts
[n
];
3668 fcgi_restart_dead_procs(srv
, p
, host
);
3670 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
3673 if (proc
->pid
== 0) continue;
3675 switch (waitpid(proc
->pid
, &status
, WNOHANG
)) {
3677 /* child still running after timeout, good */
3680 if (errno
!= EINTR
) {
3681 /* no PID found ? should never happen */
3682 log_error_write(srv
, __FILE__
, __LINE__
, "sddss",
3683 "pid ", proc
->pid
, proc
->state
,
3684 "not found:", strerror(errno
));
3687 if (errno
== ECHILD
) {
3688 /* someone else has cleaned up for us */
3690 proc
->state
= PROC_STATE_UNSET
;
3696 /* the child should not terminate at all */
3697 if (WIFEXITED(status
)) {
3698 if (proc
->state
!= PROC_STATE_KILLED
) {
3699 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
3701 WEXITSTATUS(status
), proc
->connection_name
);
3703 } else if (WIFSIGNALED(status
)) {
3704 if (WTERMSIG(status
) != SIGTERM
) {
3705 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3710 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3711 "child died somehow:",
3715 if (proc
->state
== PROC_STATE_RUNNING
) host
->active_procs
--;
3716 proc
->state
= PROC_STATE_UNSET
;
3724 return HANDLER_GO_ON
;
3728 int mod_fastcgi_plugin_init(plugin
*p
);
3729 int mod_fastcgi_plugin_init(plugin
*p
) {
3730 p
->version
= LIGHTTPD_VERSION_ID
;
3731 p
->name
= buffer_init_string("fastcgi");
3733 p
->init
= mod_fastcgi_init
;
3734 p
->cleanup
= mod_fastcgi_free
;
3735 p
->set_defaults
= mod_fastcgi_set_defaults
;
3736 p
->connection_reset
= fcgi_connection_reset
;
3737 p
->handle_connection_close
= fcgi_connection_reset
;
3738 p
->handle_uri_clean
= fcgi_check_extension_1
;
3739 p
->handle_subrequest_start
= fcgi_check_extension_2
;
3740 p
->handle_subrequest
= mod_fastcgi_handle_subrequest
;
3741 p
->handle_trigger
= mod_fastcgi_handle_trigger
;