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 chunkqueue
*rb
; /* read queue */
342 chunkqueue
*wb
; /* write queue */
345 buffer
*response_header
;
347 int fd
; /* fd to the fastcgi process */
348 int fde_ndx
; /* index into the fd-event buffer */
352 int reconnects
; /* number of reconnect attempts */
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();
505 static void handler_ctx_free(server
*srv
, handler_ctx
*hctx
) {
507 fcgi_host_reset(srv
, hctx
);
510 buffer_free(hctx
->response_header
);
512 chunkqueue_free(hctx
->rb
);
513 chunkqueue_free(hctx
->wb
);
518 static fcgi_proc
*fastcgi_process_init(void) {
521 f
= calloc(1, sizeof(*f
));
522 f
->unixsocket
= buffer_init();
523 f
->connection_name
= buffer_init();
531 static void fastcgi_process_free(fcgi_proc
*f
) {
534 fastcgi_process_free(f
->next
);
536 buffer_free(f
->unixsocket
);
537 buffer_free(f
->connection_name
);
542 static fcgi_extension_host
*fastcgi_host_init(void) {
543 fcgi_extension_host
*f
;
545 f
= calloc(1, sizeof(*f
));
547 f
->id
= buffer_init();
548 f
->host
= buffer_init();
549 f
->unixsocket
= buffer_init();
550 f
->docroot
= buffer_init();
551 f
->bin_path
= buffer_init();
552 f
->bin_env
= array_init();
553 f
->bin_env_copy
= array_init();
554 f
->strip_request_uri
= buffer_init();
555 f
->xsendfile_docroot
= array_init();
560 static void fastcgi_host_free(fcgi_extension_host
*h
) {
564 buffer_free(h
->host
);
565 buffer_free(h
->unixsocket
);
566 buffer_free(h
->docroot
);
567 buffer_free(h
->bin_path
);
568 buffer_free(h
->strip_request_uri
);
569 array_free(h
->bin_env
);
570 array_free(h
->bin_env_copy
);
571 array_free(h
->xsendfile_docroot
);
573 fastcgi_process_free(h
->first
);
574 fastcgi_process_free(h
->unused_procs
);
580 static fcgi_exts
*fastcgi_extensions_init(void) {
583 f
= calloc(1, sizeof(*f
));
588 static void fastcgi_extensions_free(fcgi_exts
*f
) {
593 for (i
= 0; i
< f
->used
; i
++) {
599 for (j
= 0; j
< fe
->used
; j
++) {
600 fcgi_extension_host
*h
;
604 fastcgi_host_free(h
);
607 buffer_free(fe
->key
);
618 static int fastcgi_extension_insert(fcgi_exts
*ext
, buffer
*key
, fcgi_extension_host
*fh
) {
622 /* there is something */
624 for (i
= 0; i
< ext
->used
; i
++) {
625 if (buffer_is_equal(key
, ext
->exts
[i
]->key
)) {
630 if (i
== ext
->used
) {
631 /* filextension is new */
632 fe
= calloc(1, sizeof(*fe
));
634 fe
->key
= buffer_init();
635 fe
->last_used_ndx
= -1;
636 buffer_copy_buffer(fe
->key
, key
);
640 if (ext
->size
== 0) {
642 ext
->exts
= malloc(ext
->size
* sizeof(*(ext
->exts
)));
643 force_assert(ext
->exts
);
644 } else if (ext
->used
== ext
->size
) {
646 ext
->exts
= realloc(ext
->exts
, ext
->size
* sizeof(*(ext
->exts
)));
647 force_assert(ext
->exts
);
649 ext
->exts
[ext
->used
++] = fe
;
656 fe
->hosts
= malloc(fe
->size
* sizeof(*(fe
->hosts
)));
657 force_assert(fe
->hosts
);
658 } else if (fe
->size
== fe
->used
) {
660 fe
->hosts
= realloc(fe
->hosts
, fe
->size
* sizeof(*(fe
->hosts
)));
661 force_assert(fe
->hosts
);
664 fe
->hosts
[fe
->used
++] = fh
;
670 INIT_FUNC(mod_fastcgi_init
) {
673 p
= calloc(1, sizeof(*p
));
675 p
->fcgi_env
= buffer_init();
677 p
->path
= buffer_init();
679 p
->statuskey
= buffer_init();
685 FREE_FUNC(mod_fastcgi_free
) {
686 plugin_data
*p
= p_d
;
690 buffer_free(p
->fcgi_env
);
691 buffer_free(p
->path
);
692 buffer_free(p
->statuskey
);
694 if (p
->config_storage
) {
696 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
697 plugin_config
*s
= p
->config_storage
[i
];
700 if (NULL
== s
) continue;
704 for (j
= 0; j
< exts
->used
; j
++) {
709 for (n
= 0; n
< ex
->used
; n
++) {
711 fcgi_extension_host
*host
;
715 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
716 if (proc
->pid
!= 0) {
717 kill(proc
->pid
, host
->kill_signal
);
720 if (proc
->is_local
&&
721 !buffer_string_is_empty(proc
->unixsocket
)) {
722 unlink(proc
->unixsocket
->ptr
);
726 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
727 if (proc
->pid
!= 0) {
728 kill(proc
->pid
, host
->kill_signal
);
730 if (proc
->is_local
&&
731 !buffer_string_is_empty(proc
->unixsocket
)) {
732 unlink(proc
->unixsocket
->ptr
);
738 fastcgi_extensions_free(s
->exts
);
739 array_free(s
->ext_mapping
);
743 free(p
->config_storage
);
748 return HANDLER_GO_ON
;
751 static int env_add(char_array
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
755 if (!key
|| !val
) return -1;
757 dst
= malloc(key_len
+ val_len
+ 3);
758 memcpy(dst
, key
, key_len
);
760 memcpy(dst
+ key_len
+ 1, val
, val_len
);
761 dst
[key_len
+ 1 + val_len
] = '\0';
763 for (i
= 0; i
< env
->used
; i
++) {
764 if (0 == strncmp(dst
, env
->ptr
[i
], key_len
+ 1)) {
765 /* don't care about free as we are in a forked child which is going to exec(...) */
766 /* free(env->ptr[i]); */
772 if (env
->size
== 0) {
774 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
775 } else if (env
->size
== env
->used
+ 1) {
777 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
780 env
->ptr
[env
->used
++] = dst
;
785 static int parse_binpath(char_array
*env
, buffer
*b
) {
788 /* search for spaces */
791 for (i
= 0; i
< buffer_string_length(b
); i
++) {
795 /* a WS, stop here and copy the argument */
797 if (env
->size
== 0) {
799 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
800 } else if (env
->size
== env
->used
) {
802 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
807 env
->ptr
[env
->used
++] = start
;
809 start
= b
->ptr
+ i
+ 1;
816 if (env
->size
== 0) {
818 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
819 } else if (env
->size
== env
->used
) { /* we need one extra for the terminating NULL */
821 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
825 env
->ptr
[env
->used
++] = start
;
827 if (env
->size
== 0) {
829 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
830 } else if (env
->size
== env
->used
) { /* we need one extra for the terminating NULL */
832 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
836 env
->ptr
[env
->used
++] = NULL
;
841 #if !defined(HAVE_FORK)
842 static int fcgi_spawn_connection(server
*srv
,
844 fcgi_extension_host
*host
,
853 #else /* -> defined(HAVE_FORK) */
855 static int fcgi_spawn_connection(server
*srv
,
857 fcgi_extension_host
*host
,
861 struct timeval tv
= { 0, 100 * 1000 };
863 struct sockaddr_un fcgi_addr_un
;
865 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
866 struct sockaddr_in6 fcgi_addr_in6
;
868 struct sockaddr_in fcgi_addr_in
;
869 struct sockaddr
*fcgi_addr
;
874 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
875 "new proc, socket:", proc
->port
, proc
->unixsocket
);
878 if (!buffer_string_is_empty(proc
->unixsocket
)) {
880 memset(&fcgi_addr_un
, 0, sizeof(fcgi_addr_un
));
881 fcgi_addr_un
.sun_family
= AF_UNIX
;
882 if (buffer_string_length(proc
->unixsocket
) + 1 > sizeof(fcgi_addr_un
.sun_path
)) {
883 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
884 "ERROR: Unix Domain socket filename too long:",
888 memcpy(fcgi_addr_un
.sun_path
, proc
->unixsocket
->ptr
, buffer_string_length(proc
->unixsocket
) + 1);
891 servlen
= SUN_LEN(&fcgi_addr_un
);
894 servlen
= buffer_string_length(proc
->unixsocket
) + 1 + sizeof(fcgi_addr_un
.sun_family
);
896 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_un
;
898 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("unix:"));
899 buffer_append_string_buffer(proc
->connection_name
, proc
->unixsocket
);
902 log_error_write(srv
, __FILE__
, __LINE__
, "s",
903 "ERROR: Unix Domain sockets are not supported.");
906 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
907 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
908 memset(&fcgi_addr_in6
, 0, sizeof(fcgi_addr_in6
));
909 fcgi_addr_in6
.sin6_family
= AF_INET6
;
910 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &fcgi_addr_in6
.sin6_addr
);
911 fcgi_addr_in6
.sin6_port
= htons(proc
->port
);
912 servlen
= sizeof(fcgi_addr_in6
);
913 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in6
;
916 memset(&fcgi_addr_in
, 0, sizeof(fcgi_addr_in
));
917 fcgi_addr_in
.sin_family
= AF_INET
;
919 if (buffer_string_is_empty(host
->host
)) {
920 fcgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
924 /* set a useful default */
925 fcgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
928 if (NULL
== (he
= gethostbyname(host
->host
->ptr
))) {
929 log_error_write(srv
, __FILE__
, __LINE__
,
930 "sdb", "gethostbyname failed: ",
931 h_errno
, host
->host
);
935 if (he
->h_addrtype
!= AF_INET
) {
936 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-type != AF_INET: ", he
->h_addrtype
);
940 if (he
->h_length
!= sizeof(struct in_addr
)) {
941 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-length != sizeof(in_addr): ", he
->h_length
);
945 memcpy(&(fcgi_addr_in
.sin_addr
.s_addr
), he
->h_addr_list
[0], he
->h_length
);
948 fcgi_addr_in
.sin_port
= htons(proc
->port
);
949 servlen
= sizeof(fcgi_addr_in
);
951 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in
;
954 if (buffer_string_is_empty(proc
->unixsocket
)) {
955 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("tcp:"));
956 if (!buffer_string_is_empty(host
->host
)) {
957 buffer_append_string_buffer(proc
->connection_name
, host
->host
);
959 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN("localhost"));
961 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN(":"));
962 buffer_append_int(proc
->connection_name
, proc
->port
);
965 if (-1 == (fcgi_fd
= socket(fcgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
966 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
967 "failed:", strerror(errno
));
971 if (-1 == connect(fcgi_fd
, fcgi_addr
, servlen
)) {
972 /* server is not up, spawn it */
976 if (errno
!= ENOENT
&&
977 !buffer_string_is_empty(proc
->unixsocket
)) {
978 unlink(proc
->unixsocket
->ptr
);
984 if (-1 == (fcgi_fd
= socket(fcgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
985 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
986 "socket failed:", strerror(errno
));
991 if (setsockopt(fcgi_fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
)) < 0) {
992 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
993 "socketsockopt failed:", strerror(errno
));
999 if (-1 == bind(fcgi_fd
, fcgi_addr
, servlen
)) {
1000 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1002 proc
->connection_name
,
1008 if (-1 == listen(fcgi_fd
, host
->listen_backlog
)) {
1009 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1010 "listen failed:", strerror(errno
));
1015 switch ((child
= fork())) {
1022 /* create environment */
1031 if(fcgi_fd
!= FCGI_LISTENSOCK_FILENO
) {
1032 close(FCGI_LISTENSOCK_FILENO
);
1033 dup2(fcgi_fd
, FCGI_LISTENSOCK_FILENO
);
1037 /* we don't need the client socket */
1038 for (i
= 3; i
< 256; i
++) {
1042 /* build clean environment */
1043 if (host
->bin_env_copy
->used
) {
1044 for (i
= 0; i
< host
->bin_env_copy
->used
; i
++) {
1045 data_string
*ds
= (data_string
*)host
->bin_env_copy
->data
[i
];
1048 if (NULL
!= (ge
= getenv(ds
->value
->ptr
))) {
1049 env_add(&env
, CONST_BUF_LEN(ds
->value
), ge
, strlen(ge
));
1053 char ** const e
= environ
;
1054 for (i
= 0; e
[i
]; ++i
) {
1057 if (NULL
!= (eq
= strchr(e
[i
], '='))) {
1058 env_add(&env
, e
[i
], eq
- e
[i
], eq
+1, strlen(eq
+1));
1063 /* create environment */
1064 for (i
= 0; i
< host
->bin_env
->used
; i
++) {
1065 data_string
*ds
= (data_string
*)host
->bin_env
->data
[i
];
1067 env_add(&env
, CONST_BUF_LEN(ds
->key
), CONST_BUF_LEN(ds
->value
));
1070 for (i
= 0; i
< env
.used
; i
++) {
1071 /* search for PHP_FCGI_CHILDREN */
1072 if (0 == strncmp(env
.ptr
[i
], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
1075 /* not found, add a default */
1076 if (i
== env
.used
) {
1077 env_add(&env
, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
1080 env
.ptr
[env
.used
] = NULL
;
1082 parse_binpath(&arg
, host
->bin_path
);
1084 /* chdir into the base of the bin-path,
1085 * search for the last / */
1086 if (NULL
!= (c
= strrchr(arg
.ptr
[0], '/'))) {
1089 /* change to the physical directory */
1090 if (-1 == chdir(arg
.ptr
[0])) {
1092 log_error_write(srv
, __FILE__
, __LINE__
, "sss", "chdir failed:", strerror(errno
), arg
.ptr
[0]);
1100 execve(arg
.ptr
[0], arg
.ptr
, env
.ptr
);
1102 /* log_error_write(srv, __FILE__, __LINE__, "sbs",
1103 "execve failed for:", host->bin_path, strerror(errno)); */
1118 select(0, NULL
, NULL
, NULL
, &tv
);
1120 switch (waitpid(child
, &status
, WNOHANG
)) {
1122 /* child still running after timeout, good */
1125 /* no PID found ? should never happen */
1126 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1127 "pid not found:", strerror(errno
));
1130 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1131 "the fastcgi-backend", host
->bin_path
, "failed to start:");
1132 /* the child should not terminate at all */
1133 if (WIFEXITED(status
)) {
1134 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
1135 "child exited with status",
1136 WEXITSTATUS(status
), host
->bin_path
);
1137 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1138 "If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version.\n"
1139 "If this is PHP on Gentoo, add 'fastcgi' to the USE flags.");
1140 } else if (WIFSIGNALED(status
)) {
1141 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1142 "terminated by signal:",
1145 if (WTERMSIG(status
) == 11) {
1146 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1147 "to be exact: it segfaulted, crashed, died, ... you get the idea." );
1148 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1149 "If this is PHP, try removing the bytecode caches for now and try again.");
1152 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1153 "child died somehow:",
1159 /* register process */
1170 if (p
->conf
.debug
) {
1171 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1172 "(debug) socket is already used; won't spawn:",
1173 proc
->connection_name
);
1177 proc
->state
= PROC_STATE_RUNNING
;
1178 host
->active_procs
++;
1183 #endif /* HAVE_FORK */
1185 static int unixsocket_is_dup(plugin_data
*p
, size_t used
, buffer
*unixsocket
) {
1187 for (i
= 0; i
< used
; ++i
) {
1188 fcgi_exts
*exts
= p
->config_storage
[i
]->exts
;
1189 for (j
= 0; j
< exts
->used
; ++j
) {
1190 fcgi_extension
*ex
= exts
->exts
[j
];
1191 for (n
= 0; n
< ex
->used
; ++n
) {
1192 fcgi_extension_host
*host
= ex
->hosts
[n
];
1193 if (!buffer_string_is_empty(host
->unixsocket
)
1194 && buffer_is_equal(host
->unixsocket
, unixsocket
))
1203 SETDEFAULTS_FUNC(mod_fastcgi_set_defaults
) {
1204 plugin_data
*p
= p_d
;
1207 buffer
*fcgi_mode
= buffer_init();
1208 fcgi_extension_host
*host
= NULL
;
1210 config_values_t cv
[] = {
1211 { "fastcgi.server", NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
1212 { "fastcgi.debug", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
1213 { "fastcgi.map-extensions", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
1214 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
1217 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
1219 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
1220 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
1223 s
= malloc(sizeof(plugin_config
));
1224 s
->exts
= fastcgi_extensions_init();
1226 s
->ext_mapping
= array_init();
1228 cv
[0].destination
= s
->exts
;
1229 cv
[1].destination
= &(s
->debug
);
1230 cv
[2].destination
= s
->ext_mapping
;
1232 p
->config_storage
[i
] = s
;
1234 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
1242 if (NULL
!= (du
= array_get_element(config
->value
, "fastcgi.server"))) {
1244 data_array
*da
= (data_array
*)du
;
1246 if (du
->type
!= TYPE_ARRAY
) {
1247 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
1248 "unexpected type for key: ", "fastcgi.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1255 * fastcgi.server = ( "<ext>" => ( ... ),
1256 * "<ext>" => ( ... ) )
1259 for (j
= 0; j
< da
->value
->used
; j
++) {
1261 data_array
*da_ext
= (data_array
*)da
->value
->data
[j
];
1263 if (da
->value
->data
[j
]->type
!= TYPE_ARRAY
) {
1264 log_error_write(srv
, __FILE__
, __LINE__
, "sssbs",
1265 "unexpected type for key: ", "fastcgi.server",
1266 "[", da
->value
->data
[j
]->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1272 * da_ext->key == name of the extension
1276 * fastcgi.server = ( "<ext>" =>
1277 * ( "<host>" => ( ... ),
1278 * "<host>" => ( ... )
1283 for (n
= 0; n
< da_ext
->value
->used
; n
++) {
1284 data_array
*da_host
= (data_array
*)da_ext
->value
->data
[n
];
1286 config_values_t fcv
[] = {
1287 { "host", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
1288 { "docroot", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
1289 { "mode", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
1290 { "socket", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
1291 { "bin-path", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 4 */
1293 { "check-local", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 5 */
1294 { "port", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 6 */
1295 { "max-procs", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 */
1296 { "disable-time", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 8 */
1298 { "bin-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 9 */
1299 { "bin-copy-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 10 */
1301 { "broken-scriptfilename", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 11 */
1302 { "allow-x-send-file", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 12 */
1303 { "strip-request-uri", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 13 */
1304 { "kill-signal", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 14 */
1305 { "fix-root-scriptname", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 15 */
1306 { "listen-backlog", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_CONNECTION
}, /* 16 */
1307 { "x-sendfile", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 17 */
1308 { "x-sendfile-docroot",NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 18 */
1310 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
1313 if (da_host
->type
!= TYPE_ARRAY
) {
1314 log_error_write(srv
, __FILE__
, __LINE__
, "ssSBS",
1315 "unexpected type for key:",
1317 "[", da_host
->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1322 host
= fastcgi_host_init();
1323 buffer_reset(fcgi_mode
);
1325 buffer_copy_buffer(host
->id
, da_host
->key
);
1327 host
->check_local
= 1;
1328 host
->max_procs
= 4;
1329 host
->mode
= FCGI_RESPONDER
;
1330 host
->disable_time
= 1;
1331 host
->break_scriptfilename_for_php
= 0;
1332 host
->xsendfile_allow
= 0;
1333 host
->kill_signal
= SIGTERM
;
1334 host
->fix_root_path_name
= 0;
1335 host
->listen_backlog
= 1024;
1337 fcv
[0].destination
= host
->host
;
1338 fcv
[1].destination
= host
->docroot
;
1339 fcv
[2].destination
= fcgi_mode
;
1340 fcv
[3].destination
= host
->unixsocket
;
1341 fcv
[4].destination
= host
->bin_path
;
1343 fcv
[5].destination
= &(host
->check_local
);
1344 fcv
[6].destination
= &(host
->port
);
1345 fcv
[7].destination
= &(host
->max_procs
);
1346 fcv
[8].destination
= &(host
->disable_time
);
1348 fcv
[9].destination
= host
->bin_env
;
1349 fcv
[10].destination
= host
->bin_env_copy
;
1350 fcv
[11].destination
= &(host
->break_scriptfilename_for_php
);
1351 fcv
[12].destination
= &(host
->xsendfile_allow
);
1352 fcv
[13].destination
= host
->strip_request_uri
;
1353 fcv
[14].destination
= &(host
->kill_signal
);
1354 fcv
[15].destination
= &(host
->fix_root_path_name
);
1355 fcv
[16].destination
= &(host
->listen_backlog
);
1356 fcv
[17].destination
= &(host
->xsendfile_allow
);
1357 fcv
[18].destination
= host
->xsendfile_docroot
;
1359 if (0 != config_insert_values_internal(srv
, da_host
->value
, fcv
, T_CONFIG_SCOPE_CONNECTION
)) {
1363 if ((!buffer_string_is_empty(host
->host
) || host
->port
) &&
1364 !buffer_string_is_empty(host
->unixsocket
)) {
1365 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1366 "either host/port or socket have to be set in:",
1368 da_ext
->key
, " => (",
1369 da_host
->key
, " ( ...");
1374 if (!buffer_string_is_empty(host
->unixsocket
)) {
1375 /* unix domain socket */
1376 struct sockaddr_un un
;
1378 if (buffer_string_length(host
->unixsocket
) + 1 > sizeof(un
.sun_path
) - 2) {
1379 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1380 "unixsocket is too long in:",
1382 da_ext
->key
, " => (",
1383 da_host
->key
, " ( ...");
1388 if (!buffer_string_is_empty(host
->bin_path
)
1389 && unixsocket_is_dup(p
, i
+1, host
->unixsocket
)) {
1390 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1391 "duplicate unixsocket path:",
1396 host
->family
= AF_UNIX
;
1400 if (buffer_string_is_empty(host
->host
) &&
1401 buffer_string_is_empty(host
->bin_path
)) {
1402 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1403 "host or binpath have to be set in:",
1405 da_ext
->key
, " => (",
1406 da_host
->key
, " ( ...");
1409 } else if (host
->port
== 0) {
1410 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1411 "port has to be set in:",
1413 da_ext
->key
, " => (",
1414 da_host
->key
, " ( ...");
1419 host
->family
= (!buffer_string_is_empty(host
->host
) && NULL
!= strchr(host
->host
->ptr
, ':')) ? AF_INET6
: AF_INET
;
1422 if (!buffer_string_is_empty(host
->bin_path
)) {
1423 /* a local socket + self spawning */
1427 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsbsd",
1428 "--- fastcgi spawning local",
1429 "\n\tproc:", host
->bin_path
,
1430 "\n\tport:", host
->port
,
1431 "\n\tsocket", host
->unixsocket
,
1432 "\n\tmax-procs:", host
->max_procs
);
1435 for (pno
= 0; pno
< host
->max_procs
; pno
++) {
1438 proc
= fastcgi_process_init();
1439 proc
->id
= host
->num_procs
++;
1442 if (buffer_string_is_empty(host
->unixsocket
)) {
1443 proc
->port
= host
->port
+ pno
;
1445 buffer_copy_buffer(proc
->unixsocket
, host
->unixsocket
);
1446 buffer_append_string_len(proc
->unixsocket
, CONST_STR_LEN("-"));
1447 buffer_append_int(proc
->unixsocket
, pno
);
1451 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsdsd",
1452 "--- fastcgi spawning",
1453 "\n\tport:", host
->port
,
1454 "\n\tsocket", host
->unixsocket
,
1455 "\n\tcurrent:", pno
, "/", host
->max_procs
);
1458 if (!srv
->srvconf
.preflight_check
1459 && fcgi_spawn_connection(srv
, p
, host
, proc
)) {
1460 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1461 "[ERROR]: spawning fcgi failed.");
1462 fastcgi_process_free(proc
);
1466 fastcgi_status_init(srv
, p
->statuskey
, host
, proc
);
1468 proc
->next
= host
->first
;
1469 if (host
->first
) host
->first
->prev
= proc
;
1476 proc
= fastcgi_process_init();
1477 proc
->id
= host
->num_procs
++;
1479 host
->active_procs
++;
1480 proc
->state
= PROC_STATE_RUNNING
;
1482 if (buffer_string_is_empty(host
->unixsocket
)) {
1483 proc
->port
= host
->port
;
1485 buffer_copy_buffer(proc
->unixsocket
, host
->unixsocket
);
1488 fastcgi_status_init(srv
, p
->statuskey
, host
, proc
);
1492 host
->max_procs
= 1;
1495 if (!buffer_string_is_empty(fcgi_mode
)) {
1496 if (strcmp(fcgi_mode
->ptr
, "responder") == 0) {
1497 host
->mode
= FCGI_RESPONDER
;
1498 } else if (strcmp(fcgi_mode
->ptr
, "authorizer") == 0) {
1499 host
->mode
= FCGI_AUTHORIZER
;
1500 if (buffer_string_is_empty(host
->docroot
)) {
1501 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1502 "ERROR: docroot is required for authorizer mode.");
1506 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1507 "WARNING: unknown fastcgi mode:",
1508 fcgi_mode
, "(ignored, mode set to responder)");
1512 if (host
->xsendfile_docroot
->used
) {
1514 for (k
= 0; k
< host
->xsendfile_docroot
->used
; ++k
) {
1515 data_string
*ds
= (data_string
*)host
->xsendfile_docroot
->data
[k
];
1516 if (ds
->type
!= TYPE_STRING
) {
1517 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1518 "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
1521 if (ds
->value
->ptr
[0] != '/') {
1522 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
1523 "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds
->value
, "\"");
1526 buffer_path_simplify(ds
->value
, ds
->value
);
1527 buffer_append_slash(ds
->value
);
1531 /* if extension already exists, take it */
1532 fastcgi_extension_insert(s
->exts
, da_ext
->key
, host
);
1539 buffer_free(fcgi_mode
);
1540 return HANDLER_GO_ON
;
1543 if (NULL
!= host
) fastcgi_host_free(host
);
1544 buffer_free(fcgi_mode
);
1545 return HANDLER_ERROR
;
1548 static int fcgi_set_state(server
*srv
, handler_ctx
*hctx
, fcgi_connection_state_t state
) {
1549 hctx
->state
= state
;
1550 hctx
->state_timestamp
= srv
->cur_ts
;
1556 static void fcgi_connection_close(server
*srv
, handler_ctx
*hctx
) {
1560 p
= hctx
->plugin_data
;
1561 con
= hctx
->remote_conn
;
1563 if (hctx
->fd
!= -1) {
1564 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
1565 fdevent_unregister(srv
->ev
, hctx
->fd
);
1570 if (hctx
->host
&& hctx
->proc
) {
1571 if (hctx
->got_proc
) {
1572 /* after the connect the process gets a load */
1573 fcgi_proc_load_dec(srv
, hctx
);
1575 if (p
->conf
.debug
) {
1576 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsd",
1578 "pid:", hctx
->proc
->pid
,
1579 "socket:", hctx
->proc
->connection_name
,
1580 "load:", hctx
->proc
->load
);
1586 handler_ctx_free(srv
, hctx
);
1587 con
->plugin_ctx
[p
->id
] = NULL
;
1589 /* finish response (if not already con->file_started, con->file_finished) */
1590 if (con
->mode
== p
->id
) {
1591 http_response_backend_done(srv
, con
);
1595 static int fcgi_reconnect(server
*srv
, handler_ctx
*hctx
) {
1596 plugin_data
*p
= hctx
->plugin_data
;
1602 * connect was ok, connection was accepted
1603 * but the php accept loop checks after the accept if it should die or not.
1605 * if yes we can only detect it at a write()
1607 * next step is resetting this attemp and setup a connection again
1609 * if we have more than 5 reconnects for the same request, die
1613 * we have a connection but the child died by some other reason
1617 if (hctx
->fd
!= -1) {
1618 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
1619 fdevent_unregister(srv
->ev
, hctx
->fd
);
1625 fcgi_set_state(srv
, hctx
, FCGI_STATE_INIT
);
1627 hctx
->request_id
= 0;
1630 if (p
->conf
.debug
> 2) {
1632 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
1633 "release proc for reconnect:",
1634 hctx
->proc
->pid
, hctx
->proc
->connection_name
);
1636 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1637 "release proc for reconnect:",
1638 hctx
->host
->unixsocket
);
1642 if (hctx
->proc
&& hctx
->got_proc
) {
1643 fcgi_proc_load_dec(srv
, hctx
);
1646 /* perhaps another host gives us more luck */
1647 fcgi_host_reset(srv
, hctx
);
1653 static handler_t
fcgi_connection_reset(server
*srv
, connection
*con
, void *p_d
) {
1654 plugin_data
*p
= p_d
;
1655 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1656 if (hctx
) fcgi_connection_close(srv
, hctx
);
1658 return HANDLER_GO_ON
;
1662 static int fcgi_env_add(buffer
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
1665 size_t len_enc_len
= 0;
1667 if (!key
|| !val
) return -1;
1669 len
= key_len
+ val_len
;
1671 len
+= key_len
> 127 ? 4 : 1;
1672 len
+= val_len
> 127 ? 4 : 1;
1674 if (buffer_string_length(env
) + len
>= FCGI_MAX_LENGTH
) {
1676 * we can't append more headers, ignore it
1682 * field length can be 31bit max
1684 * HINT: this can't happen as FCGI_MAX_LENGTH is only 16bit
1686 force_assert(key_len
< 0x7fffffffu
);
1687 force_assert(val_len
< 0x7fffffffu
);
1689 buffer_string_prepare_append(env
, len
);
1691 if (key_len
> 127) {
1692 len_enc
[len_enc_len
++] = ((key_len
>> 24) & 0xff) | 0x80;
1693 len_enc
[len_enc_len
++] = (key_len
>> 16) & 0xff;
1694 len_enc
[len_enc_len
++] = (key_len
>> 8) & 0xff;
1695 len_enc
[len_enc_len
++] = (key_len
>> 0) & 0xff;
1697 len_enc
[len_enc_len
++] = (key_len
>> 0) & 0xff;
1700 if (val_len
> 127) {
1701 len_enc
[len_enc_len
++] = ((val_len
>> 24) & 0xff) | 0x80;
1702 len_enc
[len_enc_len
++] = (val_len
>> 16) & 0xff;
1703 len_enc
[len_enc_len
++] = (val_len
>> 8) & 0xff;
1704 len_enc
[len_enc_len
++] = (val_len
>> 0) & 0xff;
1706 len_enc
[len_enc_len
++] = (val_len
>> 0) & 0xff;
1709 buffer_append_string_len(env
, len_enc
, len_enc_len
);
1710 buffer_append_string_len(env
, key
, key_len
);
1711 buffer_append_string_len(env
, val
, val_len
);
1716 static int fcgi_header(FCGI_Header
* header
, unsigned char type
, int request_id
, int contentLength
, unsigned char paddingLength
) {
1717 force_assert(contentLength
<= FCGI_MAX_LENGTH
);
1719 header
->version
= FCGI_VERSION_1
;
1720 header
->type
= type
;
1721 header
->requestIdB0
= request_id
& 0xff;
1722 header
->requestIdB1
= (request_id
>> 8) & 0xff;
1723 header
->contentLengthB0
= contentLength
& 0xff;
1724 header
->contentLengthB1
= (contentLength
>> 8) & 0xff;
1725 header
->paddingLength
= paddingLength
;
1726 header
->reserved
= 0;
1733 CONNECTION_DELAYED
, /* retry after event, take same host */
1734 CONNECTION_OVERLOADED
, /* disable for 1 second, take another backend */
1735 CONNECTION_DEAD
/* disable for 60 seconds, take another backend */
1736 } connection_result_t
;
1738 static connection_result_t
fcgi_establish_connection(server
*srv
, handler_ctx
*hctx
) {
1739 struct sockaddr
*fcgi_addr
;
1740 struct sockaddr_in fcgi_addr_in
;
1741 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1742 struct sockaddr_in6 fcgi_addr_in6
;
1744 #ifdef HAVE_SYS_UN_H
1745 struct sockaddr_un fcgi_addr_un
;
1749 fcgi_extension_host
*host
= hctx
->host
;
1750 fcgi_proc
*proc
= hctx
->proc
;
1751 int fcgi_fd
= hctx
->fd
;
1753 if (!buffer_string_is_empty(proc
->unixsocket
)) {
1754 #ifdef HAVE_SYS_UN_H
1755 /* use the unix domain socket */
1756 memset(&fcgi_addr_un
, 0, sizeof(fcgi_addr_un
));
1757 fcgi_addr_un
.sun_family
= AF_UNIX
;
1758 if (buffer_string_length(proc
->unixsocket
) + 1 > sizeof(fcgi_addr_un
.sun_path
)) {
1759 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
1760 "ERROR: Unix Domain socket filename too long:",
1764 memcpy(fcgi_addr_un
.sun_path
, proc
->unixsocket
->ptr
, buffer_string_length(proc
->unixsocket
) + 1);
1767 servlen
= SUN_LEN(&fcgi_addr_un
);
1770 servlen
= buffer_string_length(proc
->unixsocket
) + 1 + sizeof(fcgi_addr_un
.sun_family
);
1772 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_un
;
1774 if (buffer_string_is_empty(proc
->connection_name
)) {
1775 /* on remote spawing we have to set the connection-name now */
1776 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("unix:"));
1777 buffer_append_string_buffer(proc
->connection_name
, proc
->unixsocket
);
1780 return CONNECTION_DEAD
;
1782 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1783 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
1784 memset(&fcgi_addr_in6
, 0, sizeof(fcgi_addr_in6
));
1785 fcgi_addr_in6
.sin6_family
= AF_INET6
;
1786 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &fcgi_addr_in6
.sin6_addr
);
1787 fcgi_addr_in6
.sin6_port
= htons(proc
->port
);
1788 servlen
= sizeof(fcgi_addr_in6
);
1789 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in6
;
1792 memset(&fcgi_addr_in
, 0, sizeof(fcgi_addr_in
));
1793 fcgi_addr_in
.sin_family
= AF_INET
;
1794 if (!buffer_string_is_empty(host
->host
)) {
1795 if (0 == inet_aton(host
->host
->ptr
, &(fcgi_addr_in
.sin_addr
))) {
1796 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1797 "converting IP address failed for", host
->host
,
1798 "\nBe sure to specify an IP address here");
1800 return CONNECTION_DEAD
;
1803 fcgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1805 fcgi_addr_in
.sin_port
= htons(proc
->port
);
1806 servlen
= sizeof(fcgi_addr_in
);
1808 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in
;
1811 if (buffer_string_is_empty(proc
->unixsocket
)) {
1812 if (buffer_string_is_empty(proc
->connection_name
)) {
1813 /* on remote spawing we have to set the connection-name now */
1814 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("tcp:"));
1815 if (!buffer_string_is_empty(host
->host
)) {
1816 buffer_append_string_buffer(proc
->connection_name
, host
->host
);
1818 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN("localhost"));
1820 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN(":"));
1821 buffer_append_int(proc
->connection_name
, proc
->port
);
1825 if (-1 == connect(fcgi_fd
, fcgi_addr
, servlen
)) {
1826 if (errno
== EINPROGRESS
||
1827 errno
== EALREADY
||
1829 if (hctx
->conf
.debug
> 2) {
1830 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1831 "connect delayed; will continue later:", proc
->connection_name
);
1834 return CONNECTION_DELAYED
;
1835 } else if (errno
== EAGAIN
) {
1836 if (hctx
->conf
.debug
) {
1837 log_error_write(srv
, __FILE__
, __LINE__
, "sbsd",
1838 "This means that you have more incoming requests than your FastCGI backend can handle in parallel."
1839 "It might help to spawn more FastCGI backends or PHP children; if not, decrease server.max-connections."
1840 "The load for this FastCGI backend", proc
->connection_name
, "is", proc
->load
);
1843 return CONNECTION_OVERLOADED
;
1845 log_error_write(srv
, __FILE__
, __LINE__
, "sssb",
1847 strerror(errno
), "on",
1848 proc
->connection_name
);
1850 return CONNECTION_DEAD
;
1854 hctx
->reconnects
= 0;
1855 if (hctx
->conf
.debug
> 1) {
1856 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1857 "connect succeeded: ", fcgi_fd
);
1860 return CONNECTION_OK
;
1863 #define FCGI_ENV_ADD_CHECK(ret, con) \
1865 con->http_status = 400; \
1868 static int fcgi_env_add_request_headers(server
*srv
, connection
*con
, plugin_data
*p
) {
1871 for (i
= 0; i
< con
->request
.headers
->used
; i
++) {
1874 ds
= (data_string
*)con
->request
.headers
->data
[i
];
1876 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
1877 buffer_copy_string_encoded_cgi_varnames(srv
->tmp_buf
, CONST_BUF_LEN(ds
->key
), 1);
1879 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_BUF_LEN(srv
->tmp_buf
), CONST_BUF_LEN(ds
->value
)),con
);
1883 for (i
= 0; i
< con
->environment
->used
; i
++) {
1886 ds
= (data_string
*)con
->environment
->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
), 0);
1891 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_BUF_LEN(srv
->tmp_buf
), CONST_BUF_LEN(ds
->value
)), con
);
1898 static void fcgi_stdin_append(server
*srv
, connection
*con
, handler_ctx
*hctx
, int request_id
) {
1900 chunkqueue
*req_cq
= con
->request_content_queue
;
1901 plugin_data
*p
= hctx
->plugin_data
;
1902 off_t offset
, weWant
;
1903 const off_t req_cqlen
= req_cq
->bytes_in
- req_cq
->bytes_out
;
1905 /* something to send ? */
1906 for (offset
= 0; offset
!= req_cqlen
; offset
+= weWant
) {
1907 weWant
= req_cqlen
- offset
> FCGI_MAX_LENGTH
? FCGI_MAX_LENGTH
: req_cqlen
- offset
;
1909 /* we announce toWrite octets
1910 * now take all request_content chunks available
1913 fcgi_header(&(header
), FCGI_STDIN
, request_id
, weWant
, 0);
1914 chunkqueue_append_mem(hctx
->wb
, (const char *)&header
, sizeof(header
));
1915 hctx
->wb_reqlen
+= sizeof(header
);
1917 if (p
->conf
.debug
> 10) {
1918 log_error_write(srv
, __FILE__
, __LINE__
, "soso", "tosend:", offset
, "/", req_cqlen
);
1921 chunkqueue_steal(hctx
->wb
, req_cq
, weWant
);
1922 /*(hctx->wb_reqlen already includes content_length)*/
1925 if (hctx
->wb
->bytes_in
== hctx
->wb_reqlen
) {
1926 /* terminate STDIN */
1927 fcgi_header(&(header
), FCGI_STDIN
, request_id
, 0, 0);
1928 chunkqueue_append_mem(hctx
->wb
, (const char *)&header
, sizeof(header
));
1929 hctx
->wb_reqlen
+= (int)sizeof(header
);
1933 static int fcgi_create_env(server
*srv
, handler_ctx
*hctx
, int request_id
) {
1934 FCGI_BeginRequestRecord beginRecord
;
1937 char buf
[LI_ITOSTRING_LENGTH
];
1940 char b2
[INET6_ADDRSTRLEN
+ 1];
1943 plugin_data
*p
= hctx
->plugin_data
;
1944 fcgi_extension_host
*host
= hctx
->host
;
1946 connection
*con
= hctx
->remote_conn
;
1947 buffer
* const req_uri
= (con
->error_handler_saved_status
>= 0) ? con
->request
.uri
: con
->request
.orig_uri
;
1948 server_socket
*srv_sock
= con
->srv_socket
;
1951 socklen_t our_addr_len
;
1953 /* send FCGI_BEGIN_REQUEST */
1955 fcgi_header(&(beginRecord
.header
), FCGI_BEGIN_REQUEST
, request_id
, sizeof(beginRecord
.body
), 0);
1956 beginRecord
.body
.roleB0
= host
->mode
;
1957 beginRecord
.body
.roleB1
= 0;
1958 beginRecord
.body
.flags
= 0;
1959 memset(beginRecord
.body
.reserved
, 0, sizeof(beginRecord
.body
.reserved
));
1961 /* send FCGI_PARAMS */
1962 buffer_string_prepare_copy(p
->fcgi_env
, 1023);
1965 if (buffer_is_empty(con
->conf
.server_tag
)) {
1966 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_STR_LEN(PACKAGE_DESC
)),con
)
1968 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_BUF_LEN(con
->conf
.server_tag
)),con
)
1971 if (!buffer_is_empty(con
->server_name
)) {
1972 size_t len
= buffer_string_length(con
->server_name
);
1974 if (con
->server_name
->ptr
[0] == '[') {
1975 const char *colon
= strstr(con
->server_name
->ptr
, "]:");
1976 if (colon
) len
= (colon
+ 1) - con
->server_name
->ptr
;
1978 const char *colon
= strchr(con
->server_name
->ptr
, ':');
1979 if (colon
) len
= colon
- con
->server_name
->ptr
;
1982 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_NAME"), con
->server_name
->ptr
, len
),con
)
1985 s
= inet_ntop(srv_sock
->addr
.plain
.sa_family
,
1986 srv_sock
->addr
.plain
.sa_family
== AF_INET6
?
1987 (const void *) &(srv_sock
->addr
.ipv6
.sin6_addr
) :
1988 (const void *) &(srv_sock
->addr
.ipv4
.sin_addr
),
1991 s
= inet_ntoa(srv_sock
->addr
.ipv4
.sin_addr
);
1993 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_NAME"), s
, strlen(s
)),con
)
1996 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("GATEWAY_INTERFACE"), CONST_STR_LEN("CGI/1.1")),con
)
1998 li_utostrn(buf
, sizeof(buf
),
2000 ntohs(srv_sock
->addr
.plain
.sa_family
? srv_sock
->addr
.ipv6
.sin6_port
: srv_sock
->addr
.ipv4
.sin_port
)
2002 ntohs(srv_sock
->addr
.ipv4
.sin_port
)
2006 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_PORT"), buf
, strlen(buf
)),con
)
2008 /* get the server-side of the connection to the client */
2009 our_addr_len
= sizeof(our_addr
);
2011 if (-1 == getsockname(con
->fd
, (struct sockaddr
*)&our_addr
, &our_addr_len
)
2012 || our_addr_len
> sizeof(our_addr
)) {
2013 s
= inet_ntop_cache_get_ip(srv
, &(srv_sock
->addr
));
2015 s
= inet_ntop_cache_get_ip(srv
, &(our_addr
));
2017 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_ADDR"), s
, strlen(s
)),con
)
2019 li_utostrn(buf
, sizeof(buf
),
2021 ntohs(con
->dst_addr
.plain
.sa_family
? con
->dst_addr
.ipv6
.sin6_port
: con
->dst_addr
.ipv4
.sin_port
)
2023 ntohs(con
->dst_addr
.ipv4
.sin_port
)
2027 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REMOTE_PORT"), buf
, strlen(buf
)),con
)
2029 s
= inet_ntop_cache_get_ip(srv
, &(con
->dst_addr
));
2030 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REMOTE_ADDR"), s
, strlen(s
)),con
)
2032 if (con
->request
.content_length
> 0 && host
->mode
!= FCGI_AUTHORIZER
) {
2033 /* CGI-SPEC 6.1.2 and FastCGI spec 6.3 */
2035 li_itostrn(buf
, sizeof(buf
), con
->request
.content_length
);
2036 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("CONTENT_LENGTH"), buf
, strlen(buf
)),con
)
2039 if (host
->mode
!= FCGI_AUTHORIZER
) {
2041 * SCRIPT_NAME, PATH_INFO and PATH_TRANSLATED according to
2042 * http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html
2043 * (6.1.14, 6.1.6, 6.1.7)
2044 * For AUTHORIZER mode these headers should be omitted.
2047 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SCRIPT_NAME"), CONST_BUF_LEN(con
->uri
.path
)),con
)
2049 if (!buffer_string_is_empty(con
->request
.pathinfo
)) {
2050 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("PATH_INFO"), CONST_BUF_LEN(con
->request
.pathinfo
)),con
)
2052 /* PATH_TRANSLATED is only defined if PATH_INFO is set */
2054 if (!buffer_string_is_empty(host
->docroot
)) {
2055 buffer_copy_buffer(p
->path
, host
->docroot
);
2057 buffer_copy_buffer(p
->path
, con
->physical
.basedir
);
2059 buffer_append_string_buffer(p
->path
, con
->request
.pathinfo
);
2060 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("PATH_TRANSLATED"), CONST_BUF_LEN(p
->path
)),con
)
2062 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("PATH_INFO"), CONST_STR_LEN("")),con
)
2067 * SCRIPT_FILENAME and DOCUMENT_ROOT for php. The PHP manual
2068 * http://www.php.net/manual/en/reserved.variables.php
2069 * treatment of PATH_TRANSLATED is different from the one of CGI specs.
2070 * TODO: this code should be checked against cgi.fix_pathinfo php
2074 if (!buffer_string_is_empty(host
->docroot
)) {
2076 * rewrite SCRIPT_FILENAME
2080 buffer_copy_buffer(p
->path
, host
->docroot
);
2081 buffer_append_string_buffer(p
->path
, con
->uri
.path
);
2083 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p
->path
)),con
)
2084 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(host
->docroot
)),con
)
2086 buffer_copy_buffer(p
->path
, con
->physical
.path
);
2088 /* cgi.fix_pathinfo need a broken SCRIPT_FILENAME to find out what PATH_INFO is itself
2090 * see src/sapi/cgi_main.c, init_request_info()
2092 if (host
->break_scriptfilename_for_php
) {
2093 buffer_append_string_buffer(p
->path
, con
->request
.pathinfo
);
2096 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p
->path
)),con
)
2097 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(con
->physical
.basedir
)),con
)
2100 if (!buffer_string_is_empty(host
->strip_request_uri
)) {
2101 /* we need at least one char to strip off */
2105 * stripping /app1 or /app1/ should lead to
2111 if ('/' != host
->strip_request_uri
->ptr
[buffer_string_length(host
->strip_request_uri
) - 1]) {
2112 /* fix the user-input to have / as last char */
2113 buffer_append_string_len(host
->strip_request_uri
, CONST_STR_LEN("/"));
2116 if (buffer_string_length(req_uri
) >= buffer_string_length(host
->strip_request_uri
) &&
2117 0 == strncmp(req_uri
->ptr
, host
->strip_request_uri
->ptr
, buffer_string_length(host
->strip_request_uri
))) {
2118 /* the left is the same */
2120 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_URI"),
2121 req_uri
->ptr
+ (buffer_string_length(host
->strip_request_uri
) - 1),
2122 buffer_string_length(req_uri
) - (buffer_string_length(host
->strip_request_uri
) - 1)), con
)
2124 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(req_uri
)),con
)
2127 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(req_uri
)),con
)
2129 if (!buffer_string_is_empty(con
->uri
.query
)) {
2130 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("QUERY_STRING"), CONST_BUF_LEN(con
->uri
.query
)),con
)
2132 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("QUERY_STRING"), CONST_STR_LEN("")),con
)
2135 s
= get_http_method_name(con
->request
.http_method
);
2137 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_METHOD"), s
, strlen(s
)),con
)
2138 /* set REDIRECT_STATUS for php compiled with --force-redirect
2139 * (if REDIRECT_STATUS has not already been set by error handler) */
2140 if (0 == con
->error_handler_saved_status
) {
2141 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REDIRECT_STATUS"), CONST_STR_LEN("200")), con
);
2143 s
= get_http_version_name(con
->request
.http_version
);
2145 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_PROTOCOL"), s
, strlen(s
)),con
)
2147 if (buffer_is_equal_caseless_string(con
->uri
.scheme
, CONST_STR_LEN("https"))) {
2148 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on")),con
)
2151 FCGI_ENV_ADD_CHECK(fcgi_env_add_request_headers(srv
, con
, p
), con
);
2154 buffer
*b
= buffer_init();
2156 buffer_copy_string_len(b
, (const char *)&beginRecord
, sizeof(beginRecord
));
2158 fcgi_header(&(header
), FCGI_PARAMS
, request_id
, buffer_string_length(p
->fcgi_env
), 0);
2159 buffer_append_string_len(b
, (const char *)&header
, sizeof(header
));
2160 buffer_append_string_buffer(b
, p
->fcgi_env
);
2162 fcgi_header(&(header
), FCGI_PARAMS
, request_id
, 0, 0);
2163 buffer_append_string_len(b
, (const char *)&header
, sizeof(header
));
2165 hctx
->wb_reqlen
= buffer_string_length(b
);
2166 chunkqueue_append_buffer(hctx
->wb
, b
);
2170 hctx
->wb_reqlen
+= con
->request
.content_length
;/* (eventual) (minimal) total request size, not necessarily including all fcgi_headers around content length yet */
2171 fcgi_stdin_append(srv
, con
, hctx
, request_id
);
2176 static int fcgi_response_parse(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*in
) {
2179 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
2180 fcgi_extension_host
*host
= hctx
->host
;
2181 int have_sendfile2
= 0;
2182 off_t sendfile2_content_length
= 0;
2187 for (s
= in
->ptr
; NULL
!= (ns
= strchr(s
, '\n')); s
= ns
+ 1) {
2191 /* a good day. Someone has read the specs and is sending a \r\n to us */
2201 if (NULL
== (value
= strchr(s
, ':'))) {
2202 /* we expect: "<key>: <value>\n" */
2206 key_len
= value
- key
;
2210 while (*value
== ' ' || *value
== '\t') value
++;
2212 if (host
->mode
!= FCGI_AUTHORIZER
||
2213 !(con
->http_status
== 0 ||
2214 con
->http_status
== 200)) {
2215 /* authorizers shouldn't affect the response headers sent back to the client */
2217 /* don't forward Status: */
2218 if (0 != strncasecmp(key
, "Status", key_len
)) {
2220 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
2221 ds
= data_response_init();
2223 buffer_copy_string_len(ds
->key
, key
, key_len
);
2224 buffer_copy_string(ds
->value
, value
);
2226 array_insert_unique(con
->response
.headers
, (data_unset
*)ds
);
2232 if (0 == strncasecmp(key
, "Date", key_len
)) {
2233 con
->parsed_response
|= HTTP_DATE
;
2237 if (0 == strncasecmp(key
, "Status", key_len
)) {
2238 int status
= strtol(value
, NULL
, 10);
2239 if (status
>= 100 && status
< 1000) {
2240 con
->http_status
= status
;
2241 con
->parsed_response
|= HTTP_STATUS
;
2243 con
->http_status
= 502;
2248 if (0 == strncasecmp(key
, "Location", key_len
)) {
2249 con
->parsed_response
|= HTTP_LOCATION
;
2253 if (0 == strncasecmp(key
, "Connection", key_len
)) {
2254 con
->response
.keep_alive
= (0 == strcasecmp(value
, "Keep-Alive")) ? 1 : 0;
2255 con
->parsed_response
|= HTTP_CONNECTION
;
2259 if (host
->xsendfile_allow
&& 0 == strncasecmp(key
, "X-Sendfile2", key_len
) && hctx
->send_content_body
) {
2264 char *filename
, *range
;
2265 stat_cache_entry
*sce
;
2266 off_t begin_range
, end_range
, range_len
;
2268 while (' ' == *pos
) pos
++;
2272 if (NULL
== (range
= strchr(pos
, ' '))) {
2274 if (p
->conf
.debug
) {
2275 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "Couldn't find range after filename:", filename
);
2279 buffer_copy_string_len(srv
->tmp_buf
, filename
, range
- filename
);
2281 /* find end of range */
2282 for (pos
= ++range
; *pos
&& *pos
!= ' ' && *pos
!= ','; pos
++) ;
2284 buffer_urldecode_path(srv
->tmp_buf
);
2285 buffer_path_simplify(srv
->tmp_buf
, srv
->tmp_buf
);
2286 if (con
->conf
.force_lowercase_filenames
) {
2287 buffer_to_lower(srv
->tmp_buf
);
2289 if (host
->xsendfile_docroot
->used
) {
2290 size_t i
, xlen
= buffer_string_length(srv
->tmp_buf
);
2291 for (i
= 0; i
< host
->xsendfile_docroot
->used
; ++i
) {
2292 data_string
*ds
= (data_string
*)host
->xsendfile_docroot
->data
[i
];
2293 size_t dlen
= buffer_string_length(ds
->value
);
2295 && (!con
->conf
.force_lowercase_filenames
2296 ? 0 == memcmp(srv
->tmp_buf
->ptr
, ds
->value
->ptr
, dlen
)
2297 : 0 == strncasecmp(srv
->tmp_buf
->ptr
, ds
->value
->ptr
, dlen
))) {
2301 if (i
== host
->xsendfile_docroot
->used
) {
2302 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
2303 "X-Sendfile2 (", srv
->tmp_buf
,
2304 ") not under configured x-sendfile-docroot(s)");
2309 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, srv
->tmp_buf
, &sce
)) {
2310 if (p
->conf
.debug
) {
2311 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
2312 "send-file error: couldn't get stat_cache entry for X-Sendfile2:",
2316 } else if (!S_ISREG(sce
->st
.st_mode
)) {
2317 if (p
->conf
.debug
) {
2318 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
2319 "send-file error: wrong filetype for X-Sendfile2:",
2324 /* found the file */
2327 begin_range
= 0; end_range
= sce
->st
.st_size
- 1;
2331 begin_range
= strtoll(range
, &rpos
, 10);
2332 if (errno
!= 0 || begin_range
< 0 || rpos
== range
) goto range_failed
;
2333 if ('-' != *rpos
++) goto range_failed
;
2336 end_range
= strtoll(range
, &rpos
, 10);
2337 if (errno
!= 0 || end_range
< 0 || rpos
== range
) goto range_failed
;
2339 if (rpos
!= pos
) goto range_failed
;
2344 if (p
->conf
.debug
) {
2345 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "Couldn't decode range after filename:", filename
);
2352 /* no parameters accepted */
2354 while (*pos
== ' ') pos
++;
2355 if (*pos
!= '\0' && *pos
!= ',') return 502;
2357 range_len
= end_range
- begin_range
+ 1;
2358 if (range_len
< 0) return 502;
2359 if (range_len
!= 0) {
2360 if (0 != http_chunk_append_file_range(srv
, con
, srv
->tmp_buf
, begin_range
, range_len
)) {
2364 sendfile2_content_length
+= range_len
;
2366 if (*pos
== ',') pos
++;
2371 if (0 == strncasecmp(key
, "Content-Length", key_len
)) {
2372 con
->response
.content_length
= strtoul(value
, NULL
, 10);
2373 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
2375 if (con
->response
.content_length
< 0) con
->response
.content_length
= 0;
2383 if (have_sendfile2
) {
2386 /* fix content-length */
2387 if (NULL
== (dcls
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
2388 dcls
= data_response_init();
2391 buffer_copy_string_len(dcls
->key
, "Content-Length", sizeof("Content-Length")-1);
2392 buffer_copy_int(dcls
->value
, sendfile2_content_length
);
2393 array_replace(con
->response
.headers
, (data_unset
*)dcls
);
2395 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
2396 con
->response
.content_length
= sendfile2_content_length
;
2400 /* CGI/1.1 rev 03 - 7.2.1.2 */
2401 if ((con
->parsed_response
& HTTP_LOCATION
) &&
2402 !(con
->parsed_response
& HTTP_STATUS
)) {
2403 con
->http_status
= 302;
2415 } fastcgi_response_packet
;
2417 static int fastcgi_get_packet(server
*srv
, handler_ctx
*hctx
, fastcgi_response_packet
*packet
) {
2421 FCGI_Header
*header
;
2423 if (!hctx
->rb
->first
) return -1;
2425 packet
->b
= buffer_init();
2428 packet
->padding
= 0;
2429 packet
->request_id
= 0;
2431 offset
= 0; toread
= 8;
2432 /* get at least the FastCGI header */
2433 for (c
= hctx
->rb
->first
; c
; c
= c
->next
) {
2434 size_t weHave
= buffer_string_length(c
->mem
) - c
->offset
;
2436 if (weHave
> toread
) weHave
= toread
;
2438 buffer_append_string_len(packet
->b
, c
->mem
->ptr
+ c
->offset
, weHave
);
2440 offset
= weHave
; /* skip offset bytes in chunk for "real" data */
2442 if (0 == toread
) break;
2445 if (buffer_string_length(packet
->b
) < sizeof(FCGI_Header
)) {
2447 if (hctx
->plugin_data
->conf
.debug
) {
2448 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");
2451 buffer_free(packet
->b
);
2456 /* we have at least a header, now check how much me have to fetch */
2457 header
= (FCGI_Header
*)(packet
->b
->ptr
);
2459 packet
->len
= (header
->contentLengthB0
| (header
->contentLengthB1
<< 8)) + header
->paddingLength
;
2460 packet
->request_id
= (header
->requestIdB0
| (header
->requestIdB1
<< 8));
2461 packet
->type
= header
->type
;
2462 packet
->padding
= header
->paddingLength
;
2464 /* ->b should only be the content */
2465 buffer_string_set_length(packet
->b
, 0);
2468 /* copy the content */
2469 for (; c
&& (buffer_string_length(packet
->b
) < packet
->len
); c
= c
->next
) {
2470 size_t weWant
= packet
->len
- buffer_string_length(packet
->b
);
2471 size_t weHave
= buffer_string_length(c
->mem
) - c
->offset
- offset
;
2473 if (weHave
> weWant
) weHave
= weWant
;
2475 buffer_append_string_len(packet
->b
, c
->mem
->ptr
+ c
->offset
+ offset
, weHave
);
2477 /* we only skipped the first bytes as they belonged to the fcgi header */
2481 if (buffer_string_length(packet
->b
) < packet
->len
) {
2482 /* we didn't get the full packet */
2484 buffer_free(packet
->b
);
2488 buffer_string_set_length(packet
->b
, buffer_string_length(packet
->b
) - packet
->padding
);
2491 chunkqueue_mark_written(hctx
->rb
, packet
->len
+ sizeof(FCGI_Header
));
2496 static int fcgi_demux_response(server
*srv
, handler_ctx
*hctx
) {
2501 plugin_data
*p
= hctx
->plugin_data
;
2502 connection
*con
= hctx
->remote_conn
;
2503 int fcgi_fd
= hctx
->fd
;
2504 fcgi_extension_host
*host
= hctx
->host
;
2505 fcgi_proc
*proc
= hctx
->proc
;
2508 * check how much we have to read
2510 if (ioctl(hctx
->fd
, FIONREAD
, &toread
)) {
2511 if (errno
== EAGAIN
) {
2512 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2515 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2516 "unexpected end-of-file (perhaps the fastcgi process died):",
2525 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)) {
2526 off_t cqlen
= chunkqueue_length(hctx
->rb
);
2527 if (cqlen
+ toread
> 65536 + (int)sizeof(FCGI_Header
)) { /*(max size of FastCGI packet + 1)*/
2528 if (cqlen
< 65536 + (int)sizeof(FCGI_Header
)) {
2529 toread
= 65536 + (int)sizeof(FCGI_Header
) - cqlen
;
2530 } else { /* should not happen */
2531 toread
= toread
< 1024 ? toread
: 1024;
2536 chunkqueue_get_memory(hctx
->rb
, &mem
, &mem_len
, 0, toread
);
2537 r
= read(hctx
->fd
, mem
, mem_len
);
2538 chunkqueue_use_memory(hctx
->rb
, r
> 0 ? r
: 0);
2541 if (errno
== EAGAIN
) {
2542 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2545 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
2546 "unexpected end-of-file (perhaps the fastcgi process died):",
2547 fcgi_fd
, strerror(errno
));
2551 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsb",
2552 "unexpected end-of-file (perhaps the fastcgi process died):",
2554 "socket:", proc
->connection_name
);
2560 * parse the fastcgi packets and forward the content to the write-queue
2564 fastcgi_response_packet packet
;
2566 /* check if we have at least one packet */
2567 if (0 != fastcgi_get_packet(srv
, hctx
, &packet
)) {
2568 /* no full packet */
2572 switch(packet
.type
) {
2574 if (packet
.len
== 0) break;
2576 /* is the header already finished */
2577 if (0 == con
->file_started
) {
2581 /* search for header terminator
2583 * if we start with \r\n check if last packet terminated with \r\n
2584 * if we start with \n check if last packet terminated with \n
2585 * search for \r\n\r\n
2589 buffer_append_string_buffer(hctx
->response_header
, packet
.b
);
2591 if (NULL
!= (c
= buffer_search_string_len(hctx
->response_header
, CONST_STR_LEN("\r\n\r\n")))) {
2592 char *hend
= c
+ 4; /* header end == body start */
2593 size_t hlen
= hend
- hctx
->response_header
->ptr
;
2594 buffer_copy_string_len(packet
.b
, hend
, buffer_string_length(hctx
->response_header
) - hlen
);
2595 buffer_string_set_length(hctx
->response_header
, hlen
);
2596 } else if (NULL
!= (c
= buffer_search_string_len(hctx
->response_header
, CONST_STR_LEN("\n\n")))) {
2597 char *hend
= c
+ 2; /* header end == body start */
2598 size_t hlen
= hend
- hctx
->response_header
->ptr
;
2599 buffer_copy_string_len(packet
.b
, hend
, buffer_string_length(hctx
->response_header
) - hlen
);
2600 buffer_string_set_length(hctx
->response_header
, hlen
);
2602 /* no luck, no header found */
2603 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
2604 if (buffer_string_length(hctx
->response_header
) > MAX_HTTP_REQUEST_HEADER
) {
2605 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "response headers too large for", con
->uri
.path
);
2606 con
->http_status
= 502; /* Bad Gateway */
2613 /* parse the response header */
2614 if ((ret
= fcgi_response_parse(srv
, con
, p
, hctx
->response_header
))) {
2615 if (200 != ret
) { /*(200 returned for X-Sendfile2 handled)*/
2616 con
->http_status
= ret
;
2619 con
->file_started
= 1;
2620 hctx
->send_content_body
= 0;
2625 con
->file_started
= 1;
2627 if (host
->mode
== FCGI_AUTHORIZER
&&
2628 (con
->http_status
== 0 ||
2629 con
->http_status
== 200)) {
2630 /* a authorizer with approved the static request, ignore the content here */
2631 hctx
->send_content_body
= 0;
2634 if (host
->xsendfile_allow
&& hctx
->send_content_body
&&
2635 (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-LIGHTTPD-send-file"))
2636 || NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-Sendfile")))) {
2637 http_response_xsendfile(srv
, con
, ds
->value
, host
->xsendfile_docroot
);
2638 if (con
->mode
== DIRECT
) {
2642 hctx
->send_content_body
= 0; /* ignore the content */
2646 /* enable chunked-transfer-encoding */
2647 if (con
->request
.http_version
== HTTP_VERSION_1_1
&&
2648 !(con
->parsed_response
& HTTP_CONTENT_LENGTH
)) {
2649 con
->response
.transfer_encoding
= HTTP_TRANSFER_ENCODING_CHUNKED
;
2653 if (hctx
->send_content_body
&& !buffer_string_is_empty(packet
.b
)) {
2654 if (0 != http_chunk_append_buffer(srv
, con
, packet
.b
)) {
2655 /* error writing to tempfile;
2656 * truncate response or send 500 if nothing sent yet */
2660 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
2661 && chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
2662 if (!con
->is_writable
) {
2663 /*(defer removal of FDEVENT_IN interest since
2664 * connection_state_machine() might be able to send data
2665 * immediately, unless !con->is_writable, where
2666 * connection_state_machine() might not loop back to call
2667 * mod_fastcgi_handle_subrequest())*/
2668 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2674 if (packet
.len
== 0) break;
2676 log_error_write_multiline_buffer(srv
, __FILE__
, __LINE__
, packet
.b
, "s",
2680 case FCGI_END_REQUEST
:
2684 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2685 "FastCGI: header.type not handled: ", packet
.type
);
2688 buffer_free(packet
.b
);
2694 static int fcgi_restart_dead_procs(server
*srv
, plugin_data
*p
, fcgi_extension_host
*host
) {
2697 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
2700 if (p
->conf
.debug
> 2) {
2701 log_error_write(srv
, __FILE__
, __LINE__
, "sbdddd",
2703 proc
->connection_name
,
2711 * if the remote side is overloaded, we check back after <n> seconds
2714 switch (proc
->state
) {
2715 case PROC_STATE_KILLED
:
2716 case PROC_STATE_UNSET
:
2717 /* this should never happen as long as adaptive spawing is disabled */
2721 case PROC_STATE_RUNNING
:
2723 case PROC_STATE_OVERLOADED
:
2724 if (srv
->cur_ts
<= proc
->disabled_until
) break;
2726 proc
->state
= PROC_STATE_RUNNING
;
2727 host
->active_procs
++;
2729 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb",
2730 "fcgi-server re-enabled:",
2731 host
->host
, host
->port
,
2734 case PROC_STATE_DIED_WAIT_FOR_PID
:
2735 /* non-local procs don't have PIDs to wait for */
2736 if (!proc
->is_local
) {
2737 proc
->state
= PROC_STATE_DIED
;
2739 /* the child should not terminate at all */
2742 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
2744 /* child is still alive */
2745 if (srv
->cur_ts
<= proc
->disabled_until
) break;
2747 proc
->state
= PROC_STATE_RUNNING
;
2748 host
->active_procs
++;
2750 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb",
2751 "fcgi-server re-enabled:",
2752 host
->host
, host
->port
,
2756 if (errno
== EINTR
) continue;
2758 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2759 "child died somehow, waitpid failed:",
2761 proc
->state
= PROC_STATE_DIED
;
2764 if (WIFEXITED(status
)) {
2766 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
2767 "child exited, pid:", proc
->pid
,
2768 "status:", WEXITSTATUS(status
));
2770 } else if (WIFSIGNALED(status
)) {
2771 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2775 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2776 "child died somehow:",
2780 proc
->state
= PROC_STATE_DIED
;
2787 /* fall through if we have a dead proc now */
2788 if (proc
->state
!= PROC_STATE_DIED
) break;
2790 case PROC_STATE_DIED
:
2791 /* local procs get restarted by us,
2792 * remote ones hopefully by the admin */
2794 if (!buffer_string_is_empty(host
->bin_path
)) {
2795 /* we still have connections bound to this proc,
2796 * let them terminate first */
2797 if (proc
->load
!= 0) break;
2799 /* restart the child */
2801 if (p
->conf
.debug
) {
2802 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsd",
2803 "--- fastcgi spawning",
2804 "\n\tsocket", proc
->connection_name
,
2805 "\n\tcurrent:", 1, "/", host
->max_procs
);
2808 if (fcgi_spawn_connection(srv
, p
, host
, proc
)) {
2809 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2810 "ERROR: spawning fcgi failed.");
2811 return HANDLER_ERROR
;
2814 if (srv
->cur_ts
<= proc
->disabled_until
) break;
2816 proc
->state
= PROC_STATE_RUNNING
;
2817 host
->active_procs
++;
2819 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
2820 "fcgi-server re-enabled:",
2821 proc
->connection_name
);
2830 static handler_t
fcgi_write_request(server
*srv
, handler_ctx
*hctx
) {
2831 plugin_data
*p
= hctx
->plugin_data
;
2832 fcgi_extension_host
*host
= hctx
->host
;
2833 connection
*con
= hctx
->remote_conn
;
2841 * - tcp socket (do not check host->host->uses, as it may be not set which means INADDR_LOOPBACK)
2845 log_error_write(srv
, __FILE__
, __LINE__
, "s", "fatal error: host = NULL");
2846 return HANDLER_ERROR
;
2848 if ((!host
->port
&& buffer_string_is_empty(host
->unixsocket
))) {
2849 log_error_write(srv
, __FILE__
, __LINE__
, "s", "fatal error: neither host->port nor host->unixsocket is set");
2850 return HANDLER_ERROR
;
2853 /* we can't handle this in the switch as we have to fall through in it */
2854 if (hctx
->state
== FCGI_STATE_CONNECT_DELAYED
) {
2856 socklen_t socket_error_len
= sizeof(socket_error
);
2858 /* try to finish the connect() */
2859 if (0 != getsockopt(hctx
->fd
, SOL_SOCKET
, SO_ERROR
, &socket_error
, &socket_error_len
)) {
2860 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2861 "getsockopt failed:", strerror(errno
));
2863 fcgi_host_disable(srv
, hctx
);
2865 return HANDLER_ERROR
;
2867 if (socket_error
!= 0) {
2868 if (!hctx
->proc
->is_local
|| p
->conf
.debug
) {
2869 /* local procs get restarted */
2871 log_error_write(srv
, __FILE__
, __LINE__
, "sssb",
2872 "establishing connection failed:", strerror(socket_error
),
2873 "socket:", hctx
->proc
->connection_name
);
2876 fcgi_host_disable(srv
, hctx
);
2877 log_error_write(srv
, __FILE__
, __LINE__
, "sdssdsd",
2878 "backend is overloaded; we'll disable it for", hctx
->host
->disable_time
, "seconds and send the request to another backend instead:",
2879 "reconnects:", hctx
->reconnects
,
2880 "load:", host
->load
);
2882 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
2883 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".died"));
2885 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
2887 return HANDLER_ERROR
;
2889 /* go on with preparing the request */
2890 hctx
->state
= FCGI_STATE_PREPARE_WRITE
;
2894 switch(hctx
->state
) {
2895 case FCGI_STATE_CONNECT_DELAYED
:
2896 /* should never happen */
2897 return HANDLER_WAIT_FOR_EVENT
;
2898 case FCGI_STATE_INIT
:
2899 /* do we have a running process for this host (max-procs) ? */
2902 for (proc
= hctx
->host
->first
;
2903 proc
&& proc
->state
!= PROC_STATE_RUNNING
;
2906 /* all children are dead */
2910 return HANDLER_ERROR
;
2915 /* check the other procs if they have a lower load */
2916 for (proc
= proc
->next
; proc
; proc
= proc
->next
) {
2917 if (proc
->state
!= PROC_STATE_RUNNING
) continue;
2918 if (proc
->load
< hctx
->proc
->load
) hctx
->proc
= proc
;
2921 if (-1 == (hctx
->fd
= socket(host
->family
, SOCK_STREAM
, 0))) {
2922 if (errno
== EMFILE
||
2924 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2925 "wait for fd at connection:", con
->fd
);
2927 return HANDLER_WAIT_FOR_FD
;
2930 log_error_write(srv
, __FILE__
, __LINE__
, "ssdd",
2931 "socket failed:", strerror(errno
), srv
->cur_fds
, srv
->max_fds
);
2932 return HANDLER_ERROR
;
2938 fdevent_register(srv
->ev
, hctx
->fd
, fcgi_handle_fdevent
, hctx
);
2940 if (-1 == fdevent_fcntl_set(srv
->ev
, hctx
->fd
)) {
2941 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2942 "fcntl failed:", strerror(errno
));
2944 return HANDLER_ERROR
;
2947 if (hctx
->proc
->is_local
) {
2948 hctx
->pid
= hctx
->proc
->pid
;
2951 switch (fcgi_establish_connection(srv
, hctx
)) {
2952 case CONNECTION_DELAYED
:
2953 /* connection is in progress, wait for an event and call getsockopt() below */
2955 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2957 fcgi_set_state(srv
, hctx
, FCGI_STATE_CONNECT_DELAYED
);
2958 return HANDLER_WAIT_FOR_EVENT
;
2959 case CONNECTION_OVERLOADED
:
2960 /* cool down the backend, it is overloaded
2963 if (hctx
->host
->disable_time
) {
2964 log_error_write(srv
, __FILE__
, __LINE__
, "sdssdsd",
2965 "backend is overloaded; we'll disable it for", hctx
->host
->disable_time
, "seconds and send the request to another backend instead:",
2966 "reconnects:", hctx
->reconnects
,
2967 "load:", host
->load
);
2969 hctx
->proc
->disabled_until
= srv
->cur_ts
+ hctx
->host
->disable_time
;
2970 if (hctx
->proc
->state
== PROC_STATE_RUNNING
) hctx
->host
->active_procs
--;
2971 hctx
->proc
->state
= PROC_STATE_OVERLOADED
;
2974 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
2975 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".overloaded"));
2977 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
2979 return HANDLER_ERROR
;
2980 case CONNECTION_DEAD
:
2981 /* we got a hard error from the backend like
2982 * - ECONNREFUSED for tcp-ip sockets
2983 * - ENOENT for unix-domain-sockets
2985 * for check if the host is back in hctx->host->disable_time seconds
2988 fcgi_host_disable(srv
, hctx
);
2990 log_error_write(srv
, __FILE__
, __LINE__
, "sdssdsd",
2991 "backend died; we'll disable it for", hctx
->host
->disable_time
, "seconds and send the request to another backend instead:",
2992 "reconnects:", hctx
->reconnects
,
2993 "load:", host
->load
);
2995 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
2996 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".died"));
2998 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
3000 return HANDLER_ERROR
;
3002 /* everything is ok, go on */
3004 fcgi_set_state(srv
, hctx
, FCGI_STATE_PREPARE_WRITE
);
3009 case FCGI_STATE_PREPARE_WRITE
:
3010 /* ok, we have the connection */
3012 fcgi_proc_load_inc(srv
, hctx
);
3015 status_counter_inc(srv
, CONST_STR_LEN("fastcgi.requests"));
3017 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
3018 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".connected"));
3020 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
3022 if (p
->conf
.debug
) {
3023 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsd",
3025 "pid:", hctx
->proc
->pid
,
3026 "socket:", hctx
->proc
->connection_name
,
3027 "load:", hctx
->proc
->load
);
3030 /* move the proc-list entry down the list */
3031 if (hctx
->request_id
== 0) {
3032 hctx
->request_id
= 1; /* always use id 1 as we don't use multiplexing */
3034 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3035 "fcgi-request is already in use:", hctx
->request_id
);
3038 if (-1 == fcgi_create_env(srv
, hctx
, hctx
->request_id
)) return HANDLER_ERROR
;
3040 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
3041 fcgi_set_state(srv
, hctx
, FCGI_STATE_WRITE
);
3043 case FCGI_STATE_WRITE
:
3044 ret
= srv
->network_backend_write(srv
, con
, hctx
->fd
, hctx
->wb
, MAX_WRITE_LIMIT
);
3046 chunkqueue_remove_finished_chunks(hctx
->wb
);
3053 /* the connection got dropped after accept()
3054 * we don't care about that - if you accept() it, you have to handle it.
3057 log_error_write(srv
, __FILE__
, __LINE__
, "ssosb",
3058 "connection was dropped after accept() (perhaps the fastcgi process died),",
3059 "write-offset:", hctx
->wb
->bytes_out
,
3060 "socket:", hctx
->proc
->connection_name
);
3062 return HANDLER_ERROR
;
3064 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
3065 "write failed:", strerror(errno
), errno
);
3067 return HANDLER_ERROR
;
3071 if (hctx
->wb
->bytes_out
== hctx
->wb_reqlen
) {
3072 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
3073 fcgi_set_state(srv
, hctx
, FCGI_STATE_READ
);
3075 off_t wblen
= hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
;
3076 if (hctx
->wb
->bytes_in
< hctx
->wb_reqlen
&& wblen
< 65536 - 16384) {
3077 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
3078 if (!(con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
)) {
3079 con
->conf
.stream_request_body
|= FDEVENT_STREAM_REQUEST_POLLIN
;
3080 con
->is_readable
= 1; /* trigger optimistic read from client */
3084 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
3086 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
3090 return HANDLER_WAIT_FOR_EVENT
;
3091 case FCGI_STATE_READ
:
3092 /* waiting for a response */
3093 return HANDLER_WAIT_FOR_EVENT
;
3095 log_error_write(srv
, __FILE__
, __LINE__
, "s", "(debug) unknown state");
3096 return HANDLER_ERROR
;
3101 /* might be called on fdevent after a connect() is delay too
3103 static handler_t
fcgi_send_request(server
*srv
, handler_ctx
*hctx
) {
3104 fcgi_extension_host
*host
;
3107 /* we don't have a host yet, choose one
3108 * -> this happens in the first round
3109 * and when the host died and we have to select a new one */
3110 if (hctx
->host
== NULL
) {
3114 /* check if the next server has no load. */
3115 ndx
= hctx
->ext
->last_used_ndx
+ 1;
3116 if(ndx
>= (int) hctx
->ext
->used
|| ndx
< 0) ndx
= 0;
3117 host
= hctx
->ext
->hosts
[ndx
];
3118 if (host
->load
> 0) {
3119 /* get backend with the least load. */
3120 for (k
= 0, ndx
= -1; k
< hctx
->ext
->used
; k
++) {
3121 host
= hctx
->ext
->hosts
[k
];
3123 /* we should have at least one proc that can do something */
3124 if (host
->active_procs
== 0) continue;
3126 if (used
== -1 || host
->load
< used
) {
3134 /* found a server */
3136 /* all hosts are down */
3138 fcgi_connection_close(srv
, hctx
);
3140 return HANDLER_FINISHED
;
3143 hctx
->ext
->last_used_ndx
= ndx
;
3144 host
= hctx
->ext
->hosts
[ndx
];
3147 * if check-local is disabled, use the uri.path handler
3151 /* init handler-context */
3153 /* we put a connection on this host, move the other new connections to other hosts
3155 * as soon as hctx->host is unassigned, decrease the load again */
3156 fcgi_host_assign(srv
, hctx
, host
);
3162 /* ok, create the request */
3163 rc
= fcgi_write_request(srv
, hctx
);
3164 if (HANDLER_ERROR
!= rc
) {
3167 plugin_data
*p
= hctx
->plugin_data
;
3168 connection
*con
= hctx
->remote_conn
;
3170 if (hctx
->state
== FCGI_STATE_INIT
||
3171 hctx
->state
== FCGI_STATE_CONNECT_DELAYED
) {
3172 fcgi_restart_dead_procs(srv
, p
, host
);
3174 /* cleanup this request and let the request handler start this request again */
3175 if (hctx
->reconnects
< 5) {
3176 fcgi_reconnect(srv
, hctx
);
3178 return HANDLER_COMEBACK
;
3180 fcgi_connection_close(srv
, hctx
);
3181 con
->http_status
= 503;
3183 return HANDLER_FINISHED
;
3186 int status
= con
->http_status
;
3187 fcgi_connection_close(srv
, hctx
);
3188 con
->http_status
= (status
== 400) ? 400 : 503; /* see FCGI_ENV_ADD_CHECK() for 400 error */
3190 return HANDLER_FINISHED
;
3196 static handler_t
fcgi_recv_response(server
*srv
, handler_ctx
*hctx
);
3199 SUBREQUEST_FUNC(mod_fastcgi_handle_subrequest
) {
3200 plugin_data
*p
= p_d
;
3202 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
3204 if (NULL
== hctx
) return HANDLER_GO_ON
;
3207 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
3209 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
3210 && con
->file_started
) {
3211 if (chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
3212 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
3213 } else if (!(fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_IN
)) {
3214 /* optimistic read from backend, which might re-enable FDEVENT_IN */
3215 handler_t rc
= fcgi_recv_response(srv
, hctx
); /*(might invalidate hctx)*/
3216 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
3220 if (0 == hctx
->wb
->bytes_in
3221 ? con
->state
== CON_STATE_READ_POST
3222 : hctx
->wb
->bytes_in
< hctx
->wb_reqlen
) {
3223 /*(64k - 4k to attempt to avoid temporary files
3224 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
3225 if (hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
> 65536 - 4096
3226 && (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_BUFMIN
)){
3227 con
->conf
.stream_request_body
&= ~FDEVENT_STREAM_REQUEST_POLLIN
;
3228 if (0 != hctx
->wb
->bytes_in
) return HANDLER_WAIT_FOR_EVENT
;
3230 handler_t r
= connection_handle_read_post_state(srv
, con
);
3231 chunkqueue
*req_cq
= con
->request_content_queue
;
3232 if (0 != hctx
->wb
->bytes_in
&& !chunkqueue_is_empty(req_cq
)) {
3233 fcgi_stdin_append(srv
, con
, hctx
, hctx
->request_id
);
3234 if (fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_OUT
) {
3235 return (r
== HANDLER_GO_ON
) ? HANDLER_WAIT_FOR_EVENT
: r
;
3238 if (r
!= HANDLER_GO_ON
) return r
;
3242 return (0 == hctx
->wb
->bytes_in
|| !chunkqueue_is_empty(hctx
->wb
))
3243 ? fcgi_send_request(srv
, hctx
)
3244 : HANDLER_WAIT_FOR_EVENT
;
3248 static handler_t
fcgi_recv_response(server
*srv
, handler_ctx
*hctx
) {
3249 connection
*con
= hctx
->remote_conn
;
3250 plugin_data
*p
= hctx
->plugin_data
;
3252 fcgi_proc
*proc
= hctx
->proc
;
3253 fcgi_extension_host
*host
= hctx
->host
;
3255 switch (fcgi_demux_response(srv
, hctx
)) {
3260 if (host
->mode
== FCGI_AUTHORIZER
&&
3261 (con
->http_status
== 200 ||
3262 con
->http_status
== 0)) {
3264 * If we are here in AUTHORIZER mode then a request for authorizer
3265 * was processed already, and status 200 has been returned. We need
3266 * now to handle authorized request.
3269 buffer_copy_buffer(con
->physical
.doc_root
, host
->docroot
);
3270 buffer_copy_buffer(con
->physical
.basedir
, host
->docroot
);
3272 buffer_copy_buffer(con
->physical
.path
, host
->docroot
);
3273 buffer_append_string_buffer(con
->physical
.path
, con
->uri
.path
);
3275 con
->mode
= DIRECT
;/*(avoid changing con->state, con->http_status)*/
3276 fcgi_connection_close(srv
, hctx
);
3277 con
->http_status
= 0;
3278 con
->file_started
= 1; /* fcgi_extension won't touch the request afterwards */
3281 fcgi_connection_close(srv
, hctx
);
3284 return HANDLER_FINISHED
;
3286 if (proc
->pid
&& proc
->state
!= PROC_STATE_DIED
) {
3289 /* only fetch the zombie if it is not already done */
3291 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
3293 /* child is still alive */
3298 /* the child should not terminate at all */
3299 if (WIFEXITED(status
)) {
3300 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
3301 "child exited, pid:", proc
->pid
,
3302 "status:", WEXITSTATUS(status
));
3303 } else if (WIFSIGNALED(status
)) {
3304 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3308 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3309 "child died somehow:",
3313 if (p
->conf
.debug
) {
3314 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsd",
3315 "--- fastcgi spawning",
3316 "\n\tsocket", proc
->connection_name
,
3317 "\n\tcurrent:", 1, "/", host
->max_procs
);
3320 if (fcgi_spawn_connection(srv
, p
, host
, proc
)) {
3321 /* respawning failed, retry later */
3322 proc
->state
= PROC_STATE_DIED
;
3324 log_error_write(srv
, __FILE__
, __LINE__
, "s",
3325 "respawning failed, will retry later");
3332 if (con
->file_started
== 0) {
3333 /* nothing has been sent out yet, try to use another child */
3335 if (hctx
->wb
->bytes_out
== 0 &&
3336 hctx
->reconnects
< 5) {
3338 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsBSBs",
3339 "response not received, request not sent",
3340 "on socket:", proc
->connection_name
,
3341 "for", con
->uri
.path
, "?", con
->uri
.query
, ", reconnecting");
3343 fcgi_reconnect(srv
, hctx
);
3345 return HANDLER_COMEBACK
;
3348 log_error_write(srv
, __FILE__
, __LINE__
, "sosbsBSBs",
3349 "response not received, request sent:", hctx
->wb
->bytes_out
,
3350 "on socket:", proc
->connection_name
,
3351 "for", con
->uri
.path
, "?", con
->uri
.query
, ", closing connection");
3353 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsBSBs",
3354 "response already sent out, but backend returned error",
3355 "on socket:", proc
->connection_name
,
3356 "for", con
->uri
.path
, "?", con
->uri
.query
, ", terminating connection");
3359 http_response_backend_error(srv
, con
);
3360 fcgi_connection_close(srv
, hctx
);
3361 return HANDLER_FINISHED
;
3364 return HANDLER_GO_ON
;
3368 static handler_t
fcgi_handle_fdevent(server
*srv
, void *ctx
, int revents
) {
3369 handler_ctx
*hctx
= ctx
;
3370 connection
*con
= hctx
->remote_conn
;
3372 joblist_append(srv
, con
);
3374 if (revents
& FDEVENT_IN
) {
3375 handler_t rc
= fcgi_recv_response(srv
, hctx
);/*(might invalidate hctx)*/
3376 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
3379 if (revents
& FDEVENT_OUT
) {
3380 return fcgi_send_request(srv
, hctx
); /*(might invalidate hctx)*/
3383 /* perhaps this issue is already handled */
3384 if (revents
& FDEVENT_HUP
) {
3385 if (hctx
->state
== FCGI_STATE_CONNECT_DELAYED
) {
3386 /* getoptsock will catch this one (right ?)
3388 * if we are in connect we might get an EINPROGRESS
3389 * in the first call and an FDEVENT_HUP in the
3392 * FIXME: as it is a bit ugly.
3395 fcgi_send_request(srv
, hctx
);
3396 } else if (con
->file_started
) {
3397 /* drain any remaining data from kernel pipe buffers
3398 * even if (con->conf.stream_response_body
3399 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
3400 * since event loop will spin on fd FDEVENT_HUP event
3401 * until unregistered. */
3404 rc
= fcgi_recv_response(srv
,hctx
);/*(might invalidate hctx)*/
3405 } while (rc
== HANDLER_GO_ON
); /*(unless HANDLER_GO_ON)*/
3406 return rc
; /* HANDLER_FINISHED or HANDLER_ERROR */
3408 fcgi_proc
*proc
= hctx
->proc
;
3409 log_error_write(srv
, __FILE__
, __LINE__
, "sBSbsbsd",
3410 "error: unexpected close of fastcgi connection for",
3411 con
->uri
.path
, "?", con
->uri
.query
,
3412 "(no fastcgi process on socket:", proc
->connection_name
, "?)",
3415 fcgi_connection_close(srv
, hctx
);
3417 } else if (revents
& FDEVENT_ERR
) {
3418 log_error_write(srv
, __FILE__
, __LINE__
, "s",
3419 "fcgi: got a FDEVENT_ERR. Don't know why.");
3421 http_response_backend_error(srv
, con
);
3422 fcgi_connection_close(srv
, hctx
);
3425 return HANDLER_FINISHED
;
3430 static int fcgi_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
3432 plugin_config
*s
= p
->config_storage
[0];
3438 /* skip the first, the global context */
3439 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
3440 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
3441 s
= p
->config_storage
[i
];
3443 /* condition didn't match */
3444 if (!config_check_cond(srv
, con
, dc
)) continue;
3447 for (j
= 0; j
< dc
->value
->used
; j
++) {
3448 data_unset
*du
= dc
->value
->data
[j
];
3450 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("fastcgi.server"))) {
3452 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("fastcgi.debug"))) {
3454 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("fastcgi.map-extensions"))) {
3465 static handler_t
fcgi_check_extension(server
*srv
, connection
*con
, void *p_d
, int uri_path_handler
) {
3466 plugin_data
*p
= p_d
;
3470 fcgi_extension
*extension
= NULL
;
3471 fcgi_extension_host
*host
= NULL
;
3473 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
3475 /* Possibly, we processed already this request */
3476 if (con
->file_started
== 1) return HANDLER_GO_ON
;
3478 fn
= uri_path_handler
? con
->uri
.path
: con
->physical
.path
;
3480 if (buffer_string_is_empty(fn
)) return HANDLER_GO_ON
;
3482 s_len
= buffer_string_length(fn
);
3484 fcgi_patch_connection(srv
, con
, p
);
3486 /* fastcgi.map-extensions maps extensions to existing fastcgi.server entries
3488 * fastcgi.map-extensions = ( ".php3" => ".php" )
3490 * fastcgi.server = ( ".php" => ... )
3494 /* check if extension-mapping matches */
3495 for (k
= 0; k
< p
->conf
.ext_mapping
->used
; k
++) {
3496 data_string
*ds
= (data_string
*)p
->conf
.ext_mapping
->data
[k
];
3497 size_t ct_len
; /* length of the config entry */
3499 if (buffer_is_empty(ds
->key
)) continue;
3501 ct_len
= buffer_string_length(ds
->key
);
3503 if (s_len
< ct_len
) continue;
3505 /* found a mapping */
3506 if (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ds
->key
->ptr
, ct_len
)) {
3507 /* check if we know the extension */
3509 /* we can reuse k here */
3510 for (k
= 0; k
< p
->conf
.exts
->used
; k
++) {
3511 extension
= p
->conf
.exts
->exts
[k
];
3513 if (buffer_is_equal(ds
->value
, extension
->key
)) {
3518 if (k
== p
->conf
.exts
->used
) {
3526 if (extension
== NULL
) {
3527 size_t uri_path_len
= buffer_string_length(con
->uri
.path
);
3529 /* check if extension matches */
3530 for (k
= 0; k
< p
->conf
.exts
->used
; k
++) {
3531 size_t ct_len
; /* length of the config entry */
3532 fcgi_extension
*ext
= p
->conf
.exts
->exts
[k
];
3534 if (buffer_is_empty(ext
->key
)) continue;
3536 ct_len
= buffer_string_length(ext
->key
);
3538 /* check _url_ in the form "/fcgi_pattern" */
3539 if (ext
->key
->ptr
[0] == '/') {
3540 if ((ct_len
<= uri_path_len
) &&
3541 (strncmp(con
->uri
.path
->ptr
, ext
->key
->ptr
, ct_len
) == 0)) {
3545 } else if ((ct_len
<= s_len
) && (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ext
->key
->ptr
, ct_len
))) {
3546 /* check extension in the form ".fcg" */
3551 /* extension doesn't match */
3552 if (NULL
== extension
) {
3553 return HANDLER_GO_ON
;
3557 /* check if we have at least one server for this extension up and running */
3558 for (k
= 0; k
< extension
->used
; k
++) {
3559 fcgi_extension_host
*h
= extension
->hosts
[k
];
3561 /* we should have at least one proc that can do something */
3562 if (h
->active_procs
== 0) {
3566 /* we found one host that is alive */
3572 /* sorry, we don't have a server alive for this ext */
3573 con
->http_status
= 500;
3576 /* only send the 'no handler' once */
3577 if (!extension
->note_is_sent
) {
3578 extension
->note_is_sent
= 1;
3580 log_error_write(srv
, __FILE__
, __LINE__
, "sBSbsbs",
3581 "all handlers for", con
->uri
.path
, "?", con
->uri
.query
,
3582 "on", extension
->key
,
3586 return HANDLER_FINISHED
;
3589 /* a note about no handler is not sent yet */
3590 extension
->note_is_sent
= 0;
3593 * if check-local is disabled, use the uri.path handler
3597 /* init handler-context */
3598 if (uri_path_handler
) {
3599 if (host
->check_local
== 0) {
3603 hctx
= handler_ctx_init();
3605 hctx
->remote_conn
= con
;
3606 hctx
->plugin_data
= p
;
3608 hctx
->ext
= extension
;
3611 hctx
->conf
.exts
= p
->conf
.exts
;
3612 hctx
->conf
.debug
= p
->conf
.debug
;
3614 con
->plugin_ctx
[p
->id
] = hctx
;
3618 if (con
->conf
.log_request_handling
) {
3619 log_error_write(srv
, __FILE__
, __LINE__
, "s",
3620 "handling it in mod_fastcgi");
3623 /* do not split path info for authorizer */
3624 if (host
->mode
!= FCGI_AUTHORIZER
) {
3625 /* the prefix is the SCRIPT_NAME,
3626 * everything from start to the next slash
3627 * this is important for check-local = "disable"
3629 * if prefix = /admin.fcgi
3631 * /admin.fcgi/foo/bar
3633 * SCRIPT_NAME = /admin.fcgi
3634 * PATH_INFO = /foo/bar
3636 * if prefix = /fcgi-bin/
3640 * SCRIPT_NAME = /fcgi-bin/foo
3643 * if prefix = /, and fix-root-path-name is enable
3647 * SCRIPT_NAME = /fcgi-bin/foo
3652 /* the rewrite is only done for /prefix/? matches */
3653 if (host
->fix_root_path_name
&& extension
->key
->ptr
[0] == '/' && extension
->key
->ptr
[1] == '\0') {
3654 buffer_copy_string(con
->request
.pathinfo
, con
->uri
.path
->ptr
);
3655 buffer_string_set_length(con
->uri
.path
, 0);
3656 } else if (extension
->key
->ptr
[0] == '/' &&
3657 buffer_string_length(con
->uri
.path
) > buffer_string_length(extension
->key
) &&
3658 NULL
!= (pathinfo
= strchr(con
->uri
.path
->ptr
+ buffer_string_length(extension
->key
), '/'))) {
3659 /* rewrite uri.path and pathinfo */
3661 buffer_copy_string(con
->request
.pathinfo
, pathinfo
);
3662 buffer_string_set_length(con
->uri
.path
, buffer_string_length(con
->uri
.path
) - buffer_string_length(con
->request
.pathinfo
));
3668 hctx
= handler_ctx_init();
3670 hctx
->remote_conn
= con
;
3671 hctx
->plugin_data
= p
;
3673 hctx
->ext
= extension
;
3675 hctx
->conf
.exts
= p
->conf
.exts
;
3676 hctx
->conf
.debug
= p
->conf
.debug
;
3678 con
->plugin_ctx
[p
->id
] = hctx
;
3682 if (con
->conf
.log_request_handling
) {
3683 log_error_write(srv
, __FILE__
, __LINE__
, "s", "handling it in mod_fastcgi");
3687 return HANDLER_GO_ON
;
3690 /* uri-path handler */
3691 static handler_t
fcgi_check_extension_1(server
*srv
, connection
*con
, void *p_d
) {
3692 return fcgi_check_extension(srv
, con
, p_d
, 1);
3695 /* start request handler */
3696 static handler_t
fcgi_check_extension_2(server
*srv
, connection
*con
, void *p_d
) {
3697 return fcgi_check_extension(srv
, con
, p_d
, 0);
3701 TRIGGER_FUNC(mod_fastcgi_handle_trigger
) {
3702 plugin_data
*p
= p_d
;
3706 /* perhaps we should kill a connect attempt after 10-15 seconds
3708 * currently we wait for the TCP timeout which is 180 seconds on Linux
3714 /* check all children if they are still up */
3716 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
3717 plugin_config
*conf
;
3720 conf
= p
->config_storage
[i
];
3724 for (j
= 0; j
< exts
->used
; j
++) {
3729 for (n
= 0; n
< ex
->used
; n
++) {
3732 fcgi_extension_host
*host
;
3734 host
= ex
->hosts
[n
];
3736 fcgi_restart_dead_procs(srv
, p
, host
);
3738 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
3741 if (proc
->pid
== 0) continue;
3743 switch (waitpid(proc
->pid
, &status
, WNOHANG
)) {
3745 /* child still running after timeout, good */
3748 if (errno
!= EINTR
) {
3749 /* no PID found ? should never happen */
3750 log_error_write(srv
, __FILE__
, __LINE__
, "sddss",
3751 "pid ", proc
->pid
, proc
->state
,
3752 "not found:", strerror(errno
));
3755 if (errno
== ECHILD
) {
3756 /* someone else has cleaned up for us */
3758 proc
->state
= PROC_STATE_UNSET
;
3764 /* the child should not terminate at all */
3765 if (WIFEXITED(status
)) {
3766 if (proc
->state
!= PROC_STATE_KILLED
) {
3767 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
3769 WEXITSTATUS(status
), proc
->connection_name
);
3771 } else if (WIFSIGNALED(status
)) {
3772 if (WTERMSIG(status
) != SIGTERM
) {
3773 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3778 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3779 "child died somehow:",
3783 if (proc
->state
== PROC_STATE_RUNNING
) host
->active_procs
--;
3784 proc
->state
= PROC_STATE_UNSET
;
3792 return HANDLER_GO_ON
;
3796 int mod_fastcgi_plugin_init(plugin
*p
);
3797 int mod_fastcgi_plugin_init(plugin
*p
) {
3798 p
->version
= LIGHTTPD_VERSION_ID
;
3799 p
->name
= buffer_init_string("fastcgi");
3801 p
->init
= mod_fastcgi_init
;
3802 p
->cleanup
= mod_fastcgi_free
;
3803 p
->set_defaults
= mod_fastcgi_set_defaults
;
3804 p
->connection_reset
= fcgi_connection_reset
;
3805 p
->handle_connection_close
= fcgi_connection_reset
;
3806 p
->handle_uri_clean
= fcgi_check_extension_1
;
3807 p
->handle_subrequest_start
= fcgi_check_extension_2
;
3808 p
->handle_subrequest
= mod_fastcgi_handle_subrequest
;
3809 p
->handle_trigger
= mod_fastcgi_handle_trigger
;