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
55 * - add timeout for a connect to a non-fastcgi process
56 * (use state_timestamp + state)
60 typedef struct fcgi_proc
{
61 size_t id
; /* id will be between 1 and max_procs */
62 buffer
*unixsocket
; /* config.socket + "-" + id */
63 unsigned port
; /* config.port + pno */
65 buffer
*connection_name
; /* either tcp:<host>:<port> or unix:<socket> for debugging purposes */
67 pid_t pid
; /* PID of the spawned process (0 if not spawned locally) */
70 size_t load
; /* number of requests waiting on this process */
72 size_t requests
; /* see max_requests */
73 struct fcgi_proc
*prev
, *next
; /* see first */
75 time_t disabled_until
; /* this proc is disabled until, use something else until then */
80 PROC_STATE_UNSET
, /* init-phase */
81 PROC_STATE_RUNNING
, /* alive */
82 PROC_STATE_OVERLOADED
, /* listen-queue is full,
83 don't send anything to this proc for the next 2 seconds */
84 PROC_STATE_DIED_WAIT_FOR_PID
, /* */
85 PROC_STATE_DIED
, /* marked as dead, should be restarted */
86 PROC_STATE_KILLED
/* was killed as we don't have the load anymore */
91 /* the key that is used to reference this value */
94 /* list of processes handling this extension
95 * sorted by lowest load
97 * whenever a job is done move it up in the list
98 * until it is sorted, move it down as soon as the
102 fcgi_proc
*unused_procs
;
105 * spawn at least min_procs, at max_procs.
107 * as soon as the load of the first entry
108 * is max_load_per_proc we spawn a new one
109 * and add it to the first entry and give it
114 unsigned short max_procs
;
115 size_t num_procs
; /* how many procs are started */
116 size_t active_procs
; /* how many of them are really running, i.e. state = PROC_STATE_RUNNING */
119 * time after a disabled remote connection is tried to be re-enabled
124 unsigned short disable_time
;
127 * some fastcgi processes get a little bit larger
128 * than wanted. max_requests_per_proc kills a
129 * process after a number of handled requests.
132 size_t max_requests_per_proc
;
140 * if host is one of the local IP adresses the
141 * whole connection is local
143 * if port is not 0, and host is not specified,
144 * "localhost" (INADDR_LOOPBACK) is assumed.
154 * instead of TCP/IP we can use Unix Domain Sockets
155 * - more secure (you have fileperms to play with)
156 * - more control (on locally)
157 * - more speed (no extra overhead)
161 /* if socket is local we can start the fastcgi
164 * bin-path is the path to the binary
166 * check min_procs and max_procs for the number
167 * of process to start up
171 /* bin-path is set bin-environment is taken to
172 * create the environement before starting the
181 * docroot-translation between URL->phys and the
185 * - different dir-layout if remote
200 * check_local tells you if the phys file is stat()ed
201 * or not. FastCGI doesn't care if the service is
202 * remote. If the web-server side doesn't contain
203 * the fastcgi-files we should not stat() for them
204 * and say '404 not found'.
206 unsigned short check_local
;
209 * append PATH_INFO to SCRIPT_FILENAME
211 * php needs this if cgi.fix_pathinfo is provided
215 unsigned short break_scriptfilename_for_php
;
218 * workaround for program when prefix="/"
220 * rule to build PATH_INFO is hardcoded for when check_local is disabled
221 * enable this option to use the workaround
225 unsigned short fix_root_path_name
;
228 * If the backend includes X-Sendfile in the response
229 * we use the value as filename and ignore the content.
232 unsigned short xsendfile_allow
;
233 array
*xsendfile_docroot
;
235 ssize_t load
; /* replace by host->load */
237 size_t max_id
; /* corresponds most of the time to
240 only if a process is killed max_id waits for the process itself
241 to die and decrements it afterwards */
243 buffer
*strip_request_uri
;
245 unsigned short kill_signal
; /* we need a setting for this as libfcgi
246 applications prefer SIGUSR1 while the
247 rest of the world would use SIGTERM
252 } fcgi_extension_host
;
255 * one extension can have multiple hosts assigned
256 * one host can spawn additional processes on the same
257 * socket (if we control it)
259 * ext -> host -> procs
262 * if the fastcgi process is remote that whole goes down
265 * ext -> host -> procs
268 * in case of PHP and FCGI_CHILDREN we have again a procs
269 * but we don't control it directly.
274 buffer
*key
; /* like .php */
279 fcgi_extension_host
**hosts
;
286 fcgi_extension
**exts
;
308 /* generic plugin data, shared between all connections */
318 plugin_config
**config_storage
;
320 plugin_config conf
; /* this is only used as long as no handler_ctx is setup */
323 /* connection specific data */
326 FCGI_STATE_CONNECT_DELAYED
,
327 FCGI_STATE_PREPARE_WRITE
,
330 } fcgi_connection_state_t
;
334 fcgi_extension_host
*host
;
337 fcgi_connection_state_t state
;
338 time_t state_timestamp
;
340 chunkqueue
*rb
; /* read queue */
341 chunkqueue
*wb
; /* write queue */
344 buffer
*response_header
;
346 int fd
; /* fd to the fastcgi process */
347 int fde_ndx
; /* index into the fd-event buffer */
351 int reconnects
; /* number of reconnect attempts */
354 int send_content_body
;
358 connection
*remote_conn
; /* dumb pointer */
359 plugin_data
*plugin_data
; /* dumb pointer */
363 /* ok, we need a prototype */
364 static handler_t
fcgi_handle_fdevent(server
*srv
, void *ctx
, int revents
);
366 static void reset_signals(void) {
368 signal(SIGTTOU
, SIG_DFL
);
371 signal(SIGTTIN
, SIG_DFL
);
374 signal(SIGTSTP
, SIG_DFL
);
376 signal(SIGHUP
, SIG_DFL
);
377 signal(SIGPIPE
, SIG_DFL
);
378 signal(SIGUSR1
, SIG_DFL
);
381 static void fastcgi_status_copy_procname(buffer
*b
, fcgi_extension_host
*host
, fcgi_proc
*proc
) {
382 buffer_copy_string_len(b
, CONST_STR_LEN("fastcgi.backend."));
383 buffer_append_string_buffer(b
, host
->id
);
385 buffer_append_string_len(b
, CONST_STR_LEN("."));
386 buffer_append_int(b
, proc
->id
);
390 static void fcgi_proc_load_inc(server
*srv
, handler_ctx
*hctx
) {
391 plugin_data
*p
= hctx
->plugin_data
;
394 status_counter_inc(srv
, CONST_STR_LEN("fastcgi.active-requests"));
396 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
397 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".load"));
399 status_counter_set(srv
, CONST_BUF_LEN(p
->statuskey
), hctx
->proc
->load
);
402 static void fcgi_proc_load_dec(server
*srv
, handler_ctx
*hctx
) {
403 plugin_data
*p
= hctx
->plugin_data
;
406 status_counter_dec(srv
, CONST_STR_LEN("fastcgi.active-requests"));
408 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
409 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".load"));
411 status_counter_set(srv
, CONST_BUF_LEN(p
->statuskey
), hctx
->proc
->load
);
414 static void fcgi_host_assign(server
*srv
, handler_ctx
*hctx
, fcgi_extension_host
*host
) {
415 plugin_data
*p
= hctx
->plugin_data
;
419 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, NULL
);
420 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".load"));
422 status_counter_set(srv
, CONST_BUF_LEN(p
->statuskey
), hctx
->host
->load
);
425 static void fcgi_host_reset(server
*srv
, handler_ctx
*hctx
) {
426 plugin_data
*p
= hctx
->plugin_data
;
429 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, NULL
);
430 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".load"));
432 status_counter_set(srv
, CONST_BUF_LEN(p
->statuskey
), hctx
->host
->load
);
437 static void fcgi_host_disable(server
*srv
, handler_ctx
*hctx
) {
438 plugin_data
*p
= hctx
->plugin_data
;
440 if (hctx
->host
->disable_time
|| hctx
->proc
->is_local
) {
441 if (hctx
->proc
->state
== PROC_STATE_RUNNING
) hctx
->host
->active_procs
--;
442 hctx
->proc
->disabled_until
= srv
->cur_ts
+ hctx
->host
->disable_time
;
443 hctx
->proc
->state
= hctx
->proc
->is_local
? PROC_STATE_DIED_WAIT_FOR_PID
: PROC_STATE_DIED
;
446 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
447 "backend disabled for", hctx
->host
->disable_time
, "seconds");
452 static int fastcgi_status_init(server
*srv
, buffer
*b
, fcgi_extension_host
*host
, fcgi_proc
*proc
) {
454 fastcgi_status_copy_procname(b, host, proc); \
455 buffer_append_string_len(b, CONST_STR_LEN(x)); \
456 status_counter_set(srv, CONST_BUF_LEN(b), 0);
460 CLEAN(".overloaded");
467 fastcgi_status_copy_procname(b, host, NULL); \
468 buffer_append_string_len(b, CONST_STR_LEN(x)); \
469 status_counter_set(srv, CONST_BUF_LEN(b), 0);
478 static handler_ctx
* handler_ctx_init(void) {
481 hctx
= calloc(1, sizeof(*hctx
));
486 hctx
->response_header
= buffer_init();
488 hctx
->request_id
= 0;
489 hctx
->state
= FCGI_STATE_INIT
;
494 hctx
->reconnects
= 0;
495 hctx
->send_content_body
= 1;
497 hctx
->rb
= chunkqueue_init();
498 hctx
->wb
= chunkqueue_init();
504 static void handler_ctx_free(server
*srv
, handler_ctx
*hctx
) {
506 fcgi_host_reset(srv
, hctx
);
509 buffer_free(hctx
->response_header
);
511 chunkqueue_free(hctx
->rb
);
512 chunkqueue_free(hctx
->wb
);
517 static fcgi_proc
*fastcgi_process_init(void) {
520 f
= calloc(1, sizeof(*f
));
521 f
->unixsocket
= buffer_init();
522 f
->connection_name
= buffer_init();
530 static void fastcgi_process_free(fcgi_proc
*f
) {
533 fastcgi_process_free(f
->next
);
535 buffer_free(f
->unixsocket
);
536 buffer_free(f
->connection_name
);
541 static fcgi_extension_host
*fastcgi_host_init(void) {
542 fcgi_extension_host
*f
;
544 f
= calloc(1, sizeof(*f
));
546 f
->id
= buffer_init();
547 f
->host
= buffer_init();
548 f
->unixsocket
= buffer_init();
549 f
->docroot
= buffer_init();
550 f
->bin_path
= buffer_init();
551 f
->bin_env
= array_init();
552 f
->bin_env_copy
= array_init();
553 f
->strip_request_uri
= buffer_init();
554 f
->xsendfile_docroot
= array_init();
559 static void fastcgi_host_free(fcgi_extension_host
*h
) {
567 buffer_free(h
->host
);
568 buffer_free(h
->unixsocket
);
569 buffer_free(h
->docroot
);
570 buffer_free(h
->bin_path
);
571 buffer_free(h
->strip_request_uri
);
572 array_free(h
->bin_env
);
573 array_free(h
->bin_env_copy
);
574 array_free(h
->xsendfile_docroot
);
576 fastcgi_process_free(h
->first
);
577 fastcgi_process_free(h
->unused_procs
);
583 static fcgi_exts
*fastcgi_extensions_init(void) {
586 f
= calloc(1, sizeof(*f
));
591 static void fastcgi_extensions_free(fcgi_exts
*f
) {
596 for (i
= 0; i
< f
->used
; i
++) {
602 for (j
= 0; j
< fe
->used
; j
++) {
603 fcgi_extension_host
*h
;
607 fastcgi_host_free(h
);
610 buffer_free(fe
->key
);
621 static int fastcgi_extension_insert(fcgi_exts
*ext
, buffer
*key
, fcgi_extension_host
*fh
) {
625 /* there is something */
627 for (i
= 0; i
< ext
->used
; i
++) {
628 if (buffer_is_equal(key
, ext
->exts
[i
]->key
)) {
633 if (i
== ext
->used
) {
634 /* filextension is new */
635 fe
= calloc(1, sizeof(*fe
));
637 fe
->key
= buffer_init();
638 fe
->last_used_ndx
= -1;
639 buffer_copy_buffer(fe
->key
, key
);
643 if (ext
->size
== 0) {
645 ext
->exts
= malloc(ext
->size
* sizeof(*(ext
->exts
)));
646 force_assert(ext
->exts
);
647 } else if (ext
->used
== ext
->size
) {
649 ext
->exts
= realloc(ext
->exts
, ext
->size
* sizeof(*(ext
->exts
)));
650 force_assert(ext
->exts
);
652 ext
->exts
[ext
->used
++] = fe
;
659 fe
->hosts
= malloc(fe
->size
* sizeof(*(fe
->hosts
)));
660 force_assert(fe
->hosts
);
661 } else if (fe
->size
== fe
->used
) {
663 fe
->hosts
= realloc(fe
->hosts
, fe
->size
* sizeof(*(fe
->hosts
)));
664 force_assert(fe
->hosts
);
667 fe
->hosts
[fe
->used
++] = fh
;
673 INIT_FUNC(mod_fastcgi_init
) {
676 p
= calloc(1, sizeof(*p
));
678 p
->fcgi_env
= buffer_init();
680 p
->path
= buffer_init();
682 p
->statuskey
= buffer_init();
688 FREE_FUNC(mod_fastcgi_free
) {
689 plugin_data
*p
= p_d
;
693 buffer_free(p
->fcgi_env
);
694 buffer_free(p
->path
);
695 buffer_free(p
->statuskey
);
697 if (p
->config_storage
) {
699 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
700 plugin_config
*s
= p
->config_storage
[i
];
703 if (NULL
== s
) continue;
707 for (j
= 0; j
< exts
->used
; j
++) {
712 for (n
= 0; n
< ex
->used
; n
++) {
714 fcgi_extension_host
*host
;
718 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
719 if (proc
->pid
!= 0) {
720 kill(proc
->pid
, host
->kill_signal
);
723 if (proc
->is_local
&&
724 !buffer_string_is_empty(proc
->unixsocket
)) {
725 unlink(proc
->unixsocket
->ptr
);
729 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
730 if (proc
->pid
!= 0) {
731 kill(proc
->pid
, host
->kill_signal
);
733 if (proc
->is_local
&&
734 !buffer_string_is_empty(proc
->unixsocket
)) {
735 unlink(proc
->unixsocket
->ptr
);
741 fastcgi_extensions_free(s
->exts
);
742 array_free(s
->ext_mapping
);
746 free(p
->config_storage
);
751 return HANDLER_GO_ON
;
754 static int env_add(char_array
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
758 if (!key
|| !val
) return -1;
760 dst
= malloc(key_len
+ val_len
+ 3);
761 memcpy(dst
, key
, key_len
);
763 memcpy(dst
+ key_len
+ 1, val
, val_len
);
764 dst
[key_len
+ 1 + val_len
] = '\0';
766 for (i
= 0; i
< env
->used
; i
++) {
767 if (0 == strncmp(dst
, env
->ptr
[i
], key_len
+ 1)) {
768 /* don't care about free as we are in a forked child which is going to exec(...) */
769 /* free(env->ptr[i]); */
775 if (env
->size
== 0) {
777 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
778 } else if (env
->size
== env
->used
+ 1) {
780 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
783 env
->ptr
[env
->used
++] = dst
;
788 static int parse_binpath(char_array
*env
, buffer
*b
) {
791 /* search for spaces */
794 for (i
= 0; i
< buffer_string_length(b
); i
++) {
798 /* a WS, stop here and copy the argument */
800 if (env
->size
== 0) {
802 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
803 } else if (env
->size
== env
->used
) {
805 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
810 env
->ptr
[env
->used
++] = start
;
812 start
= b
->ptr
+ i
+ 1;
819 if (env
->size
== 0) {
821 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
822 } else if (env
->size
== env
->used
) { /* we need one extra for the terminating NULL */
824 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
828 env
->ptr
[env
->used
++] = start
;
830 if (env
->size
== 0) {
832 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
833 } else if (env
->size
== env
->used
) { /* we need one extra for the terminating NULL */
835 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
839 env
->ptr
[env
->used
++] = NULL
;
844 #if !defined(HAVE_FORK)
845 static int fcgi_spawn_connection(server
*srv
,
847 fcgi_extension_host
*host
,
856 #else /* -> defined(HAVE_FORK) */
858 static int fcgi_spawn_connection(server
*srv
,
860 fcgi_extension_host
*host
,
864 struct timeval tv
= { 0, 100 * 1000 };
866 struct sockaddr_un fcgi_addr_un
;
868 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
869 struct sockaddr_in6 fcgi_addr_in6
;
871 struct sockaddr_in fcgi_addr_in
;
872 struct sockaddr
*fcgi_addr
;
877 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
878 "new proc, socket:", proc
->port
, proc
->unixsocket
);
881 if (!buffer_string_is_empty(proc
->unixsocket
)) {
883 memset(&fcgi_addr_un
, 0, sizeof(fcgi_addr_un
));
884 fcgi_addr_un
.sun_family
= AF_UNIX
;
885 if (buffer_string_length(proc
->unixsocket
) + 1 > sizeof(fcgi_addr_un
.sun_path
)) {
886 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
887 "ERROR: Unix Domain socket filename too long:",
891 memcpy(fcgi_addr_un
.sun_path
, proc
->unixsocket
->ptr
, buffer_string_length(proc
->unixsocket
) + 1);
894 servlen
= SUN_LEN(&fcgi_addr_un
);
897 servlen
= buffer_string_length(proc
->unixsocket
) + 1 + sizeof(fcgi_addr_un
.sun_family
);
899 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_un
;
901 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("unix:"));
902 buffer_append_string_buffer(proc
->connection_name
, proc
->unixsocket
);
905 log_error_write(srv
, __FILE__
, __LINE__
, "s",
906 "ERROR: Unix Domain sockets are not supported.");
909 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
910 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
911 memset(&fcgi_addr_in6
, 0, sizeof(fcgi_addr_in6
));
912 fcgi_addr_in6
.sin6_family
= AF_INET6
;
913 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &fcgi_addr_in6
.sin6_addr
);
914 fcgi_addr_in6
.sin6_port
= htons(proc
->port
);
915 servlen
= sizeof(fcgi_addr_in6
);
916 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in6
;
919 memset(&fcgi_addr_in
, 0, sizeof(fcgi_addr_in
));
920 fcgi_addr_in
.sin_family
= AF_INET
;
922 if (buffer_string_is_empty(host
->host
)) {
923 fcgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
927 /* set a useful default */
928 fcgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
931 if (NULL
== (he
= gethostbyname(host
->host
->ptr
))) {
932 log_error_write(srv
, __FILE__
, __LINE__
,
933 "sdb", "gethostbyname failed: ",
934 h_errno
, host
->host
);
938 if (he
->h_addrtype
!= AF_INET
) {
939 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-type != AF_INET: ", he
->h_addrtype
);
943 if (he
->h_length
!= sizeof(struct in_addr
)) {
944 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-length != sizeof(in_addr): ", he
->h_length
);
948 memcpy(&(fcgi_addr_in
.sin_addr
.s_addr
), he
->h_addr_list
[0], he
->h_length
);
951 fcgi_addr_in
.sin_port
= htons(proc
->port
);
952 servlen
= sizeof(fcgi_addr_in
);
954 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in
;
957 if (buffer_string_is_empty(proc
->unixsocket
)) {
958 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("tcp:"));
959 if (!buffer_string_is_empty(host
->host
)) {
960 buffer_append_string_buffer(proc
->connection_name
, host
->host
);
962 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN("localhost"));
964 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN(":"));
965 buffer_append_int(proc
->connection_name
, proc
->port
);
968 if (-1 == (fcgi_fd
= socket(fcgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
969 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
970 "failed:", strerror(errno
));
974 if (-1 == connect(fcgi_fd
, fcgi_addr
, servlen
)) {
975 /* server is not up, spawn it */
979 if (errno
!= ENOENT
&&
980 !buffer_string_is_empty(proc
->unixsocket
)) {
981 unlink(proc
->unixsocket
->ptr
);
987 if (-1 == (fcgi_fd
= socket(fcgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
988 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
989 "socket failed:", strerror(errno
));
994 if (setsockopt(fcgi_fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
)) < 0) {
995 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
996 "socketsockopt failed:", strerror(errno
));
1002 if (-1 == bind(fcgi_fd
, fcgi_addr
, servlen
)) {
1003 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1005 proc
->connection_name
,
1011 if (-1 == listen(fcgi_fd
, host
->listen_backlog
)) {
1012 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1013 "listen failed:", strerror(errno
));
1018 switch ((child
= fork())) {
1025 /* create environment */
1034 if(fcgi_fd
!= FCGI_LISTENSOCK_FILENO
) {
1035 close(FCGI_LISTENSOCK_FILENO
);
1036 dup2(fcgi_fd
, FCGI_LISTENSOCK_FILENO
);
1040 /* we don't need the client socket */
1041 for (i
= 3; i
< 256; i
++) {
1045 /* build clean environment */
1046 if (host
->bin_env_copy
->used
) {
1047 for (i
= 0; i
< host
->bin_env_copy
->used
; i
++) {
1048 data_string
*ds
= (data_string
*)host
->bin_env_copy
->data
[i
];
1051 if (NULL
!= (ge
= getenv(ds
->value
->ptr
))) {
1052 env_add(&env
, CONST_BUF_LEN(ds
->value
), ge
, strlen(ge
));
1056 char ** const e
= environ
;
1057 for (i
= 0; e
[i
]; ++i
) {
1060 if (NULL
!= (eq
= strchr(e
[i
], '='))) {
1061 env_add(&env
, e
[i
], eq
- e
[i
], eq
+1, strlen(eq
+1));
1066 /* create environment */
1067 for (i
= 0; i
< host
->bin_env
->used
; i
++) {
1068 data_string
*ds
= (data_string
*)host
->bin_env
->data
[i
];
1070 env_add(&env
, CONST_BUF_LEN(ds
->key
), CONST_BUF_LEN(ds
->value
));
1073 for (i
= 0; i
< env
.used
; i
++) {
1074 /* search for PHP_FCGI_CHILDREN */
1075 if (0 == strncmp(env
.ptr
[i
], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
1078 /* not found, add a default */
1079 if (i
== env
.used
) {
1080 env_add(&env
, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
1083 env
.ptr
[env
.used
] = NULL
;
1085 parse_binpath(&arg
, host
->bin_path
);
1087 /* chdir into the base of the bin-path,
1088 * search for the last / */
1089 if (NULL
!= (c
= strrchr(arg
.ptr
[0], '/'))) {
1092 /* change to the physical directory */
1093 if (-1 == chdir(arg
.ptr
[0])) {
1095 log_error_write(srv
, __FILE__
, __LINE__
, "sss", "chdir failed:", strerror(errno
), arg
.ptr
[0]);
1103 execve(arg
.ptr
[0], arg
.ptr
, env
.ptr
);
1105 /* log_error_write(srv, __FILE__, __LINE__, "sbs",
1106 "execve failed for:", host->bin_path, strerror(errno)); */
1121 select(0, NULL
, NULL
, NULL
, &tv
);
1123 switch (waitpid(child
, &status
, WNOHANG
)) {
1125 /* child still running after timeout, good */
1128 /* no PID found ? should never happen */
1129 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1130 "pid not found:", strerror(errno
));
1133 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1134 "the fastcgi-backend", host
->bin_path
, "failed to start:");
1135 /* the child should not terminate at all */
1136 if (WIFEXITED(status
)) {
1137 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
1138 "child exited with status",
1139 WEXITSTATUS(status
), host
->bin_path
);
1140 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1141 "If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version.\n"
1142 "If this is PHP on Gentoo, add 'fastcgi' to the USE flags.");
1143 } else if (WIFSIGNALED(status
)) {
1144 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1145 "terminated by signal:",
1148 if (WTERMSIG(status
) == 11) {
1149 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1150 "to be exact: it segfaulted, crashed, died, ... you get the idea." );
1151 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1152 "If this is PHP, try removing the bytecode caches for now and try again.");
1155 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1156 "child died somehow:",
1162 /* register process */
1173 if (p
->conf
.debug
) {
1174 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1175 "(debug) socket is already used; won't spawn:",
1176 proc
->connection_name
);
1180 proc
->state
= PROC_STATE_RUNNING
;
1181 host
->active_procs
++;
1186 #endif /* HAVE_FORK */
1188 static fcgi_extension_host
* unixsocket_is_dup(plugin_data
*p
, size_t used
, buffer
*unixsocket
) {
1190 for (i
= 0; i
< used
; ++i
) {
1191 fcgi_exts
*exts
= p
->config_storage
[i
]->exts
;
1192 for (j
= 0; j
< exts
->used
; ++j
) {
1193 fcgi_extension
*ex
= exts
->exts
[j
];
1194 for (n
= 0; n
< ex
->used
; ++n
) {
1195 fcgi_extension_host
*host
= ex
->hosts
[n
];
1196 if (!buffer_string_is_empty(host
->unixsocket
)
1197 && buffer_is_equal(host
->unixsocket
, unixsocket
)
1198 && !buffer_string_is_empty(host
->bin_path
))
1207 SETDEFAULTS_FUNC(mod_fastcgi_set_defaults
) {
1208 plugin_data
*p
= p_d
;
1211 buffer
*fcgi_mode
= buffer_init();
1212 fcgi_extension_host
*host
= NULL
;
1214 config_values_t cv
[] = {
1215 { "fastcgi.server", NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
1216 { "fastcgi.debug", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
1217 { "fastcgi.map-extensions", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
1218 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
1221 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
1223 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
1224 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
1227 s
= malloc(sizeof(plugin_config
));
1228 s
->exts
= fastcgi_extensions_init();
1230 s
->ext_mapping
= array_init();
1232 cv
[0].destination
= s
->exts
;
1233 cv
[1].destination
= &(s
->debug
);
1234 cv
[2].destination
= s
->ext_mapping
;
1236 p
->config_storage
[i
] = s
;
1238 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
1246 if (NULL
!= (du
= array_get_element(config
->value
, "fastcgi.server"))) {
1248 data_array
*da
= (data_array
*)du
;
1250 if (du
->type
!= TYPE_ARRAY
) {
1251 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
1252 "unexpected type for key: ", "fastcgi.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1259 * fastcgi.server = ( "<ext>" => ( ... ),
1260 * "<ext>" => ( ... ) )
1263 for (j
= 0; j
< da
->value
->used
; j
++) {
1265 data_array
*da_ext
= (data_array
*)da
->value
->data
[j
];
1267 if (da
->value
->data
[j
]->type
!= TYPE_ARRAY
) {
1268 log_error_write(srv
, __FILE__
, __LINE__
, "sssbs",
1269 "unexpected type for key: ", "fastcgi.server",
1270 "[", da
->value
->data
[j
]->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1276 * da_ext->key == name of the extension
1280 * fastcgi.server = ( "<ext>" =>
1281 * ( "<host>" => ( ... ),
1282 * "<host>" => ( ... )
1287 for (n
= 0; n
< da_ext
->value
->used
; n
++) {
1288 data_array
*da_host
= (data_array
*)da_ext
->value
->data
[n
];
1290 config_values_t fcv
[] = {
1291 { "host", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
1292 { "docroot", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
1293 { "mode", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
1294 { "socket", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
1295 { "bin-path", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 4 */
1297 { "check-local", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 5 */
1298 { "port", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 6 */
1299 { "max-procs", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 */
1300 { "disable-time", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 8 */
1302 { "bin-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 9 */
1303 { "bin-copy-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 10 */
1305 { "broken-scriptfilename", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 11 */
1306 { "allow-x-send-file", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 12 */
1307 { "strip-request-uri", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 13 */
1308 { "kill-signal", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 14 */
1309 { "fix-root-scriptname", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 15 */
1310 { "listen-backlog", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_CONNECTION
}, /* 16 */
1311 { "x-sendfile", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 17 */
1312 { "x-sendfile-docroot",NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 18 */
1314 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
1317 if (da_host
->type
!= TYPE_ARRAY
) {
1318 log_error_write(srv
, __FILE__
, __LINE__
, "ssSBS",
1319 "unexpected type for key:",
1321 "[", da_host
->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1326 host
= fastcgi_host_init();
1327 buffer_reset(fcgi_mode
);
1329 buffer_copy_buffer(host
->id
, da_host
->key
);
1331 host
->check_local
= 1;
1332 host
->max_procs
= 4;
1333 host
->mode
= FCGI_RESPONDER
;
1334 host
->disable_time
= 1;
1335 host
->break_scriptfilename_for_php
= 0;
1336 host
->xsendfile_allow
= 0;
1337 host
->kill_signal
= SIGTERM
;
1338 host
->fix_root_path_name
= 0;
1339 host
->listen_backlog
= 1024;
1342 fcv
[0].destination
= host
->host
;
1343 fcv
[1].destination
= host
->docroot
;
1344 fcv
[2].destination
= fcgi_mode
;
1345 fcv
[3].destination
= host
->unixsocket
;
1346 fcv
[4].destination
= host
->bin_path
;
1348 fcv
[5].destination
= &(host
->check_local
);
1349 fcv
[6].destination
= &(host
->port
);
1350 fcv
[7].destination
= &(host
->max_procs
);
1351 fcv
[8].destination
= &(host
->disable_time
);
1353 fcv
[9].destination
= host
->bin_env
;
1354 fcv
[10].destination
= host
->bin_env_copy
;
1355 fcv
[11].destination
= &(host
->break_scriptfilename_for_php
);
1356 fcv
[12].destination
= &(host
->xsendfile_allow
);
1357 fcv
[13].destination
= host
->strip_request_uri
;
1358 fcv
[14].destination
= &(host
->kill_signal
);
1359 fcv
[15].destination
= &(host
->fix_root_path_name
);
1360 fcv
[16].destination
= &(host
->listen_backlog
);
1361 fcv
[17].destination
= &(host
->xsendfile_allow
);
1362 fcv
[18].destination
= host
->xsendfile_docroot
;
1364 if (0 != config_insert_values_internal(srv
, da_host
->value
, fcv
, T_CONFIG_SCOPE_CONNECTION
)) {
1368 if ((!buffer_string_is_empty(host
->host
) || host
->port
) &&
1369 !buffer_string_is_empty(host
->unixsocket
)) {
1370 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1371 "either host/port or socket have to be set in:",
1373 da_ext
->key
, " => (",
1374 da_host
->key
, " ( ...");
1379 if (!buffer_string_is_empty(host
->unixsocket
)) {
1380 /* unix domain socket */
1381 struct sockaddr_un un
;
1383 if (buffer_string_length(host
->unixsocket
) + 1 > sizeof(un
.sun_path
) - 2) {
1384 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1385 "unixsocket is too long in:",
1387 da_ext
->key
, " => (",
1388 da_host
->key
, " ( ...");
1393 if (!buffer_string_is_empty(host
->bin_path
)) {
1394 fcgi_extension_host
*duplicate
= unixsocket_is_dup(p
, i
+1, host
->unixsocket
);
1395 if (NULL
!= duplicate
) {
1396 if (!buffer_is_equal(host
->bin_path
, duplicate
->bin_path
)) {
1397 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1398 "duplicate unixsocket path:",
1402 fastcgi_host_free(host
);
1408 host
->family
= AF_UNIX
;
1412 if (buffer_string_is_empty(host
->host
) &&
1413 buffer_string_is_empty(host
->bin_path
)) {
1414 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1415 "host or binpath have to be set in:",
1417 da_ext
->key
, " => (",
1418 da_host
->key
, " ( ...");
1421 } else if (host
->port
== 0) {
1422 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsbs",
1423 "port has to be set in:",
1425 da_ext
->key
, " => (",
1426 da_host
->key
, " ( ...");
1431 host
->family
= (!buffer_string_is_empty(host
->host
) && NULL
!= strchr(host
->host
->ptr
, ':')) ? AF_INET6
: AF_INET
;
1434 if (host
->refcount
) {
1435 /* already init'd; skip spawning */
1436 } else if (!buffer_string_is_empty(host
->bin_path
)) {
1437 /* a local socket + self spawning */
1441 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsbsd",
1442 "--- fastcgi spawning local",
1443 "\n\tproc:", host
->bin_path
,
1444 "\n\tport:", host
->port
,
1445 "\n\tsocket", host
->unixsocket
,
1446 "\n\tmax-procs:", host
->max_procs
);
1449 for (pno
= 0; pno
< host
->max_procs
; pno
++) {
1452 proc
= fastcgi_process_init();
1453 proc
->id
= host
->num_procs
++;
1456 if (buffer_string_is_empty(host
->unixsocket
)) {
1457 proc
->port
= host
->port
+ pno
;
1459 buffer_copy_buffer(proc
->unixsocket
, host
->unixsocket
);
1460 buffer_append_string_len(proc
->unixsocket
, CONST_STR_LEN("-"));
1461 buffer_append_int(proc
->unixsocket
, pno
);
1465 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsdsd",
1466 "--- fastcgi spawning",
1467 "\n\tport:", host
->port
,
1468 "\n\tsocket", host
->unixsocket
,
1469 "\n\tcurrent:", pno
, "/", host
->max_procs
);
1472 if (!srv
->srvconf
.preflight_check
1473 && fcgi_spawn_connection(srv
, p
, host
, proc
)) {
1474 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1475 "[ERROR]: spawning fcgi failed.");
1476 fastcgi_process_free(proc
);
1480 fastcgi_status_init(srv
, p
->statuskey
, host
, proc
);
1482 proc
->next
= host
->first
;
1483 if (host
->first
) host
->first
->prev
= proc
;
1490 proc
= fastcgi_process_init();
1491 proc
->id
= host
->num_procs
++;
1493 host
->active_procs
++;
1494 proc
->state
= PROC_STATE_RUNNING
;
1496 if (buffer_string_is_empty(host
->unixsocket
)) {
1497 proc
->port
= host
->port
;
1499 buffer_copy_buffer(proc
->unixsocket
, host
->unixsocket
);
1502 fastcgi_status_init(srv
, p
->statuskey
, host
, proc
);
1506 host
->max_procs
= 1;
1509 if (!buffer_string_is_empty(fcgi_mode
)) {
1510 if (strcmp(fcgi_mode
->ptr
, "responder") == 0) {
1511 host
->mode
= FCGI_RESPONDER
;
1512 } else if (strcmp(fcgi_mode
->ptr
, "authorizer") == 0) {
1513 host
->mode
= FCGI_AUTHORIZER
;
1514 if (buffer_string_is_empty(host
->docroot
)) {
1515 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1516 "ERROR: docroot is required for authorizer mode.");
1520 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1521 "WARNING: unknown fastcgi mode:",
1522 fcgi_mode
, "(ignored, mode set to responder)");
1526 if (host
->xsendfile_docroot
->used
) {
1528 for (k
= 0; k
< host
->xsendfile_docroot
->used
; ++k
) {
1529 data_string
*ds
= (data_string
*)host
->xsendfile_docroot
->data
[k
];
1530 if (ds
->type
!= TYPE_STRING
) {
1531 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1532 "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
1535 if (ds
->value
->ptr
[0] != '/') {
1536 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
1537 "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds
->value
, "\"");
1540 buffer_path_simplify(ds
->value
, ds
->value
);
1541 buffer_append_slash(ds
->value
);
1545 /* if extension already exists, take it */
1546 fastcgi_extension_insert(s
->exts
, da_ext
->key
, host
);
1553 buffer_free(fcgi_mode
);
1554 return HANDLER_GO_ON
;
1557 if (NULL
!= host
) fastcgi_host_free(host
);
1558 buffer_free(fcgi_mode
);
1559 return HANDLER_ERROR
;
1562 static int fcgi_set_state(server
*srv
, handler_ctx
*hctx
, fcgi_connection_state_t state
) {
1563 hctx
->state
= state
;
1564 hctx
->state_timestamp
= srv
->cur_ts
;
1570 static void fcgi_connection_close(server
*srv
, handler_ctx
*hctx
) {
1574 p
= hctx
->plugin_data
;
1575 con
= hctx
->remote_conn
;
1577 if (hctx
->fd
!= -1) {
1578 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
1579 fdevent_unregister(srv
->ev
, hctx
->fd
);
1584 if (hctx
->host
&& hctx
->proc
) {
1585 if (hctx
->got_proc
) {
1586 /* after the connect the process gets a load */
1587 fcgi_proc_load_dec(srv
, hctx
);
1589 if (p
->conf
.debug
) {
1590 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsd",
1592 "pid:", hctx
->proc
->pid
,
1593 "socket:", hctx
->proc
->connection_name
,
1594 "load:", hctx
->proc
->load
);
1600 handler_ctx_free(srv
, hctx
);
1601 con
->plugin_ctx
[p
->id
] = NULL
;
1603 /* finish response (if not already con->file_started, con->file_finished) */
1604 if (con
->mode
== p
->id
) {
1605 http_response_backend_done(srv
, con
);
1609 static int fcgi_reconnect(server
*srv
, handler_ctx
*hctx
) {
1610 plugin_data
*p
= hctx
->plugin_data
;
1616 * connect was ok, connection was accepted
1617 * but the php accept loop checks after the accept if it should die or not.
1619 * if yes we can only detect it at a write()
1621 * next step is resetting this attemp and setup a connection again
1623 * if we have more than 5 reconnects for the same request, die
1627 * we have a connection but the child died by some other reason
1631 if (hctx
->fd
!= -1) {
1632 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
1633 fdevent_unregister(srv
->ev
, hctx
->fd
);
1639 fcgi_set_state(srv
, hctx
, FCGI_STATE_INIT
);
1641 hctx
->request_id
= 0;
1644 if (p
->conf
.debug
> 2) {
1646 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
1647 "release proc for reconnect:",
1648 hctx
->proc
->pid
, hctx
->proc
->connection_name
);
1650 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1651 "release proc for reconnect:",
1652 hctx
->host
->unixsocket
);
1656 if (hctx
->proc
&& hctx
->got_proc
) {
1657 fcgi_proc_load_dec(srv
, hctx
);
1660 /* perhaps another host gives us more luck */
1661 fcgi_host_reset(srv
, hctx
);
1667 static handler_t
fcgi_connection_reset(server
*srv
, connection
*con
, void *p_d
) {
1668 plugin_data
*p
= p_d
;
1669 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1670 if (hctx
) fcgi_connection_close(srv
, hctx
);
1672 return HANDLER_GO_ON
;
1676 static int fcgi_env_add(buffer
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
1679 size_t len_enc_len
= 0;
1681 if (!key
|| !val
) return -1;
1683 len
= key_len
+ val_len
;
1685 len
+= key_len
> 127 ? 4 : 1;
1686 len
+= val_len
> 127 ? 4 : 1;
1688 if (buffer_string_length(env
) + len
>= FCGI_MAX_LENGTH
) {
1690 * we can't append more headers, ignore it
1696 * field length can be 31bit max
1698 * HINT: this can't happen as FCGI_MAX_LENGTH is only 16bit
1700 force_assert(key_len
< 0x7fffffffu
);
1701 force_assert(val_len
< 0x7fffffffu
);
1703 buffer_string_prepare_append(env
, len
);
1705 if (key_len
> 127) {
1706 len_enc
[len_enc_len
++] = ((key_len
>> 24) & 0xff) | 0x80;
1707 len_enc
[len_enc_len
++] = (key_len
>> 16) & 0xff;
1708 len_enc
[len_enc_len
++] = (key_len
>> 8) & 0xff;
1709 len_enc
[len_enc_len
++] = (key_len
>> 0) & 0xff;
1711 len_enc
[len_enc_len
++] = (key_len
>> 0) & 0xff;
1714 if (val_len
> 127) {
1715 len_enc
[len_enc_len
++] = ((val_len
>> 24) & 0xff) | 0x80;
1716 len_enc
[len_enc_len
++] = (val_len
>> 16) & 0xff;
1717 len_enc
[len_enc_len
++] = (val_len
>> 8) & 0xff;
1718 len_enc
[len_enc_len
++] = (val_len
>> 0) & 0xff;
1720 len_enc
[len_enc_len
++] = (val_len
>> 0) & 0xff;
1723 buffer_append_string_len(env
, len_enc
, len_enc_len
);
1724 buffer_append_string_len(env
, key
, key_len
);
1725 buffer_append_string_len(env
, val
, val_len
);
1730 static int fcgi_header(FCGI_Header
* header
, unsigned char type
, int request_id
, int contentLength
, unsigned char paddingLength
) {
1731 force_assert(contentLength
<= FCGI_MAX_LENGTH
);
1733 header
->version
= FCGI_VERSION_1
;
1734 header
->type
= type
;
1735 header
->requestIdB0
= request_id
& 0xff;
1736 header
->requestIdB1
= (request_id
>> 8) & 0xff;
1737 header
->contentLengthB0
= contentLength
& 0xff;
1738 header
->contentLengthB1
= (contentLength
>> 8) & 0xff;
1739 header
->paddingLength
= paddingLength
;
1740 header
->reserved
= 0;
1747 CONNECTION_DELAYED
, /* retry after event, take same host */
1748 CONNECTION_OVERLOADED
, /* disable for 1 second, take another backend */
1749 CONNECTION_DEAD
/* disable for 60 seconds, take another backend */
1750 } connection_result_t
;
1752 static connection_result_t
fcgi_establish_connection(server
*srv
, handler_ctx
*hctx
) {
1753 struct sockaddr
*fcgi_addr
;
1754 struct sockaddr_in fcgi_addr_in
;
1755 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1756 struct sockaddr_in6 fcgi_addr_in6
;
1758 #ifdef HAVE_SYS_UN_H
1759 struct sockaddr_un fcgi_addr_un
;
1763 fcgi_extension_host
*host
= hctx
->host
;
1764 fcgi_proc
*proc
= hctx
->proc
;
1765 int fcgi_fd
= hctx
->fd
;
1767 if (!buffer_string_is_empty(proc
->unixsocket
)) {
1768 #ifdef HAVE_SYS_UN_H
1769 /* use the unix domain socket */
1770 memset(&fcgi_addr_un
, 0, sizeof(fcgi_addr_un
));
1771 fcgi_addr_un
.sun_family
= AF_UNIX
;
1772 if (buffer_string_length(proc
->unixsocket
) + 1 > sizeof(fcgi_addr_un
.sun_path
)) {
1773 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
1774 "ERROR: Unix Domain socket filename too long:",
1778 memcpy(fcgi_addr_un
.sun_path
, proc
->unixsocket
->ptr
, buffer_string_length(proc
->unixsocket
) + 1);
1781 servlen
= SUN_LEN(&fcgi_addr_un
);
1784 servlen
= buffer_string_length(proc
->unixsocket
) + 1 + sizeof(fcgi_addr_un
.sun_family
);
1786 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_un
;
1788 if (buffer_string_is_empty(proc
->connection_name
)) {
1789 /* on remote spawing we have to set the connection-name now */
1790 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("unix:"));
1791 buffer_append_string_buffer(proc
->connection_name
, proc
->unixsocket
);
1794 return CONNECTION_DEAD
;
1796 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1797 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
1798 memset(&fcgi_addr_in6
, 0, sizeof(fcgi_addr_in6
));
1799 fcgi_addr_in6
.sin6_family
= AF_INET6
;
1800 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &fcgi_addr_in6
.sin6_addr
);
1801 fcgi_addr_in6
.sin6_port
= htons(proc
->port
);
1802 servlen
= sizeof(fcgi_addr_in6
);
1803 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in6
;
1806 memset(&fcgi_addr_in
, 0, sizeof(fcgi_addr_in
));
1807 fcgi_addr_in
.sin_family
= AF_INET
;
1808 if (!buffer_string_is_empty(host
->host
)) {
1809 if (0 == inet_aton(host
->host
->ptr
, &(fcgi_addr_in
.sin_addr
))) {
1810 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1811 "converting IP address failed for", host
->host
,
1812 "\nBe sure to specify an IP address here");
1814 return CONNECTION_DEAD
;
1817 fcgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1819 fcgi_addr_in
.sin_port
= htons(proc
->port
);
1820 servlen
= sizeof(fcgi_addr_in
);
1822 fcgi_addr
= (struct sockaddr
*) &fcgi_addr_in
;
1825 if (buffer_string_is_empty(proc
->unixsocket
)) {
1826 if (buffer_string_is_empty(proc
->connection_name
)) {
1827 /* on remote spawing we have to set the connection-name now */
1828 buffer_copy_string_len(proc
->connection_name
, CONST_STR_LEN("tcp:"));
1829 if (!buffer_string_is_empty(host
->host
)) {
1830 buffer_append_string_buffer(proc
->connection_name
, host
->host
);
1832 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN("localhost"));
1834 buffer_append_string_len(proc
->connection_name
, CONST_STR_LEN(":"));
1835 buffer_append_int(proc
->connection_name
, proc
->port
);
1839 if (-1 == connect(fcgi_fd
, fcgi_addr
, servlen
)) {
1840 if (errno
== EINPROGRESS
||
1841 errno
== EALREADY
||
1843 if (hctx
->conf
.debug
> 2) {
1844 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1845 "connect delayed; will continue later:", proc
->connection_name
);
1848 return CONNECTION_DELAYED
;
1849 } else if (errno
== EAGAIN
) {
1850 if (hctx
->conf
.debug
) {
1851 log_error_write(srv
, __FILE__
, __LINE__
, "sbsd",
1852 "This means that you have more incoming requests than your FastCGI backend can handle in parallel."
1853 "It might help to spawn more FastCGI backends or PHP children; if not, decrease server.max-connections."
1854 "The load for this FastCGI backend", proc
->connection_name
, "is", proc
->load
);
1857 return CONNECTION_OVERLOADED
;
1859 log_error_write(srv
, __FILE__
, __LINE__
, "sssb",
1861 strerror(errno
), "on",
1862 proc
->connection_name
);
1864 return CONNECTION_DEAD
;
1868 hctx
->reconnects
= 0;
1869 if (hctx
->conf
.debug
> 1) {
1870 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1871 "connect succeeded: ", fcgi_fd
);
1874 return CONNECTION_OK
;
1877 #define FCGI_ENV_ADD_CHECK(ret, con) \
1879 con->http_status = 400; \
1882 static int fcgi_env_add_request_headers(server
*srv
, connection
*con
, plugin_data
*p
) {
1885 for (i
= 0; i
< con
->request
.headers
->used
; i
++) {
1888 ds
= (data_string
*)con
->request
.headers
->data
[i
];
1890 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
1891 buffer_copy_string_encoded_cgi_varnames(srv
->tmp_buf
, CONST_BUF_LEN(ds
->key
), 1);
1893 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_BUF_LEN(srv
->tmp_buf
), CONST_BUF_LEN(ds
->value
)),con
);
1897 for (i
= 0; i
< con
->environment
->used
; i
++) {
1900 ds
= (data_string
*)con
->environment
->data
[i
];
1902 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
1903 buffer_copy_string_encoded_cgi_varnames(srv
->tmp_buf
, CONST_BUF_LEN(ds
->key
), 0);
1905 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_BUF_LEN(srv
->tmp_buf
), CONST_BUF_LEN(ds
->value
)), con
);
1912 static void fcgi_stdin_append(server
*srv
, connection
*con
, handler_ctx
*hctx
, int request_id
) {
1914 chunkqueue
*req_cq
= con
->request_content_queue
;
1915 plugin_data
*p
= hctx
->plugin_data
;
1916 off_t offset
, weWant
;
1917 const off_t req_cqlen
= req_cq
->bytes_in
- req_cq
->bytes_out
;
1919 /* something to send ? */
1920 for (offset
= 0; offset
!= req_cqlen
; offset
+= weWant
) {
1921 weWant
= req_cqlen
- offset
> FCGI_MAX_LENGTH
? FCGI_MAX_LENGTH
: req_cqlen
- offset
;
1923 /* we announce toWrite octets
1924 * now take all request_content chunks available
1927 fcgi_header(&(header
), FCGI_STDIN
, request_id
, weWant
, 0);
1928 chunkqueue_append_mem(hctx
->wb
, (const char *)&header
, sizeof(header
));
1929 hctx
->wb_reqlen
+= sizeof(header
);
1931 if (p
->conf
.debug
> 10) {
1932 log_error_write(srv
, __FILE__
, __LINE__
, "soso", "tosend:", offset
, "/", req_cqlen
);
1935 chunkqueue_steal(hctx
->wb
, req_cq
, weWant
);
1936 /*(hctx->wb_reqlen already includes content_length)*/
1939 if (hctx
->wb
->bytes_in
== hctx
->wb_reqlen
) {
1940 /* terminate STDIN */
1941 fcgi_header(&(header
), FCGI_STDIN
, request_id
, 0, 0);
1942 chunkqueue_append_mem(hctx
->wb
, (const char *)&header
, sizeof(header
));
1943 hctx
->wb_reqlen
+= (int)sizeof(header
);
1947 static int fcgi_create_env(server
*srv
, handler_ctx
*hctx
, int request_id
) {
1948 FCGI_BeginRequestRecord beginRecord
;
1951 char buf
[LI_ITOSTRING_LENGTH
];
1954 char b2
[INET6_ADDRSTRLEN
+ 1];
1957 plugin_data
*p
= hctx
->plugin_data
;
1958 fcgi_extension_host
*host
= hctx
->host
;
1960 connection
*con
= hctx
->remote_conn
;
1961 buffer
* const req_uri
= (con
->error_handler_saved_status
>= 0) ? con
->request
.uri
: con
->request
.orig_uri
;
1962 server_socket
*srv_sock
= con
->srv_socket
;
1965 socklen_t our_addr_len
;
1967 /* send FCGI_BEGIN_REQUEST */
1969 fcgi_header(&(beginRecord
.header
), FCGI_BEGIN_REQUEST
, request_id
, sizeof(beginRecord
.body
), 0);
1970 beginRecord
.body
.roleB0
= host
->mode
;
1971 beginRecord
.body
.roleB1
= 0;
1972 beginRecord
.body
.flags
= 0;
1973 memset(beginRecord
.body
.reserved
, 0, sizeof(beginRecord
.body
.reserved
));
1975 /* send FCGI_PARAMS */
1976 buffer_string_prepare_copy(p
->fcgi_env
, 1023);
1978 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_BUF_LEN(con
->conf
.server_tag
)),con
)
1980 if (!buffer_is_empty(con
->server_name
)) {
1981 size_t len
= buffer_string_length(con
->server_name
);
1983 if (con
->server_name
->ptr
[0] == '[') {
1984 const char *colon
= strstr(con
->server_name
->ptr
, "]:");
1985 if (colon
) len
= (colon
+ 1) - con
->server_name
->ptr
;
1987 const char *colon
= strchr(con
->server_name
->ptr
, ':');
1988 if (colon
) len
= colon
- con
->server_name
->ptr
;
1991 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_NAME"), con
->server_name
->ptr
, len
),con
)
1994 s
= inet_ntop(srv_sock
->addr
.plain
.sa_family
,
1995 srv_sock
->addr
.plain
.sa_family
== AF_INET6
?
1996 (const void *) &(srv_sock
->addr
.ipv6
.sin6_addr
) :
1997 (const void *) &(srv_sock
->addr
.ipv4
.sin_addr
),
2000 s
= inet_ntoa(srv_sock
->addr
.ipv4
.sin_addr
);
2002 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_NAME"), s
, strlen(s
)),con
)
2005 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("GATEWAY_INTERFACE"), CONST_STR_LEN("CGI/1.1")),con
)
2007 li_utostrn(buf
, sizeof(buf
),
2009 ntohs(srv_sock
->addr
.plain
.sa_family
? srv_sock
->addr
.ipv6
.sin6_port
: srv_sock
->addr
.ipv4
.sin_port
)
2011 ntohs(srv_sock
->addr
.ipv4
.sin_port
)
2015 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_PORT"), buf
, strlen(buf
)),con
)
2017 /* get the server-side of the connection to the client */
2018 our_addr_len
= sizeof(our_addr
);
2020 if (-1 == getsockname(con
->fd
, (struct sockaddr
*)&our_addr
, &our_addr_len
)
2021 || our_addr_len
> (socklen_t
)sizeof(our_addr
)) {
2022 s
= inet_ntop_cache_get_ip(srv
, &(srv_sock
->addr
));
2024 s
= inet_ntop_cache_get_ip(srv
, &(our_addr
));
2026 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_ADDR"), s
, strlen(s
)),con
)
2028 li_utostrn(buf
, sizeof(buf
),
2030 ntohs(con
->dst_addr
.plain
.sa_family
? con
->dst_addr
.ipv6
.sin6_port
: con
->dst_addr
.ipv4
.sin_port
)
2032 ntohs(con
->dst_addr
.ipv4
.sin_port
)
2036 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REMOTE_PORT"), buf
, strlen(buf
)),con
)
2038 s
= inet_ntop_cache_get_ip(srv
, &(con
->dst_addr
));
2039 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REMOTE_ADDR"), s
, strlen(s
)),con
)
2041 if (con
->request
.content_length
> 0 && host
->mode
!= FCGI_AUTHORIZER
) {
2042 /* CGI-SPEC 6.1.2 and FastCGI spec 6.3 */
2044 li_itostrn(buf
, sizeof(buf
), con
->request
.content_length
);
2045 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("CONTENT_LENGTH"), buf
, strlen(buf
)),con
)
2048 if (host
->mode
!= FCGI_AUTHORIZER
) {
2050 * SCRIPT_NAME, PATH_INFO and PATH_TRANSLATED according to
2051 * http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html
2052 * (6.1.14, 6.1.6, 6.1.7)
2053 * For AUTHORIZER mode these headers should be omitted.
2056 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SCRIPT_NAME"), CONST_BUF_LEN(con
->uri
.path
)),con
)
2058 if (!buffer_string_is_empty(con
->request
.pathinfo
)) {
2059 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("PATH_INFO"), CONST_BUF_LEN(con
->request
.pathinfo
)),con
)
2061 /* PATH_TRANSLATED is only defined if PATH_INFO is set */
2063 if (!buffer_string_is_empty(host
->docroot
)) {
2064 buffer_copy_buffer(p
->path
, host
->docroot
);
2066 buffer_copy_buffer(p
->path
, con
->physical
.basedir
);
2068 buffer_append_string_buffer(p
->path
, con
->request
.pathinfo
);
2069 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("PATH_TRANSLATED"), CONST_BUF_LEN(p
->path
)),con
)
2071 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("PATH_INFO"), CONST_STR_LEN("")),con
)
2076 * SCRIPT_FILENAME and DOCUMENT_ROOT for php. The PHP manual
2077 * http://www.php.net/manual/en/reserved.variables.php
2078 * treatment of PATH_TRANSLATED is different from the one of CGI specs.
2079 * TODO: this code should be checked against cgi.fix_pathinfo php
2083 if (!buffer_string_is_empty(host
->docroot
)) {
2085 * rewrite SCRIPT_FILENAME
2089 buffer_copy_buffer(p
->path
, host
->docroot
);
2090 buffer_append_string_buffer(p
->path
, con
->uri
.path
);
2092 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p
->path
)),con
)
2093 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(host
->docroot
)),con
)
2095 buffer_copy_buffer(p
->path
, con
->physical
.path
);
2097 /* cgi.fix_pathinfo need a broken SCRIPT_FILENAME to find out what PATH_INFO is itself
2099 * see src/sapi/cgi_main.c, init_request_info()
2101 if (host
->break_scriptfilename_for_php
) {
2102 buffer_append_string_buffer(p
->path
, con
->request
.pathinfo
);
2105 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p
->path
)),con
)
2106 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(con
->physical
.basedir
)),con
)
2109 if (!buffer_string_is_empty(host
->strip_request_uri
)) {
2110 /* we need at least one char to strip off */
2114 * stripping /app1 or /app1/ should lead to
2120 if ('/' != host
->strip_request_uri
->ptr
[buffer_string_length(host
->strip_request_uri
) - 1]) {
2121 /* fix the user-input to have / as last char */
2122 buffer_append_string_len(host
->strip_request_uri
, CONST_STR_LEN("/"));
2125 if (buffer_string_length(req_uri
) >= buffer_string_length(host
->strip_request_uri
) &&
2126 0 == strncmp(req_uri
->ptr
, host
->strip_request_uri
->ptr
, buffer_string_length(host
->strip_request_uri
))) {
2127 /* the left is the same */
2129 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_URI"),
2130 req_uri
->ptr
+ (buffer_string_length(host
->strip_request_uri
) - 1),
2131 buffer_string_length(req_uri
) - (buffer_string_length(host
->strip_request_uri
) - 1)), con
)
2133 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(req_uri
)),con
)
2136 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(req_uri
)),con
)
2138 if (!buffer_string_is_empty(con
->uri
.query
)) {
2139 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("QUERY_STRING"), CONST_BUF_LEN(con
->uri
.query
)),con
)
2141 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("QUERY_STRING"), CONST_STR_LEN("")),con
)
2144 s
= get_http_method_name(con
->request
.http_method
);
2146 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REQUEST_METHOD"), s
, strlen(s
)),con
)
2147 /* set REDIRECT_STATUS for php compiled with --force-redirect
2148 * (if REDIRECT_STATUS has not already been set by error handler) */
2149 if (0 == con
->error_handler_saved_status
) {
2150 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("REDIRECT_STATUS"), CONST_STR_LEN("200")), con
);
2152 s
= get_http_version_name(con
->request
.http_version
);
2154 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("SERVER_PROTOCOL"), s
, strlen(s
)),con
)
2156 if (buffer_is_equal_caseless_string(con
->uri
.scheme
, CONST_STR_LEN("https"))) {
2157 FCGI_ENV_ADD_CHECK(fcgi_env_add(p
->fcgi_env
, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on")),con
)
2160 FCGI_ENV_ADD_CHECK(fcgi_env_add_request_headers(srv
, con
, p
), con
);
2163 buffer
*b
= buffer_init();
2165 buffer_copy_string_len(b
, (const char *)&beginRecord
, sizeof(beginRecord
));
2167 fcgi_header(&(header
), FCGI_PARAMS
, request_id
, buffer_string_length(p
->fcgi_env
), 0);
2168 buffer_append_string_len(b
, (const char *)&header
, sizeof(header
));
2169 buffer_append_string_buffer(b
, p
->fcgi_env
);
2171 fcgi_header(&(header
), FCGI_PARAMS
, request_id
, 0, 0);
2172 buffer_append_string_len(b
, (const char *)&header
, sizeof(header
));
2174 hctx
->wb_reqlen
= buffer_string_length(b
);
2175 chunkqueue_append_buffer(hctx
->wb
, b
);
2179 hctx
->wb_reqlen
+= con
->request
.content_length
;/* (eventual) (minimal) total request size, not necessarily including all fcgi_headers around content length yet */
2180 fcgi_stdin_append(srv
, con
, hctx
, request_id
);
2185 static int fcgi_response_parse(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*in
) {
2188 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
2189 fcgi_extension_host
*host
= hctx
->host
;
2190 int have_sendfile2
= 0;
2191 off_t sendfile2_content_length
= 0;
2196 for (s
= in
->ptr
; NULL
!= (ns
= strchr(s
, '\n')); s
= ns
+ 1) {
2200 /* a good day. Someone has read the specs and is sending a \r\n to us */
2210 if (NULL
== (value
= strchr(s
, ':'))) {
2211 /* we expect: "<key>: <value>\n" */
2215 key_len
= value
- key
;
2219 while (*value
== ' ' || *value
== '\t') value
++;
2221 if (host
->mode
!= FCGI_AUTHORIZER
||
2222 !(con
->http_status
== 0 ||
2223 con
->http_status
== 200)) {
2224 /* authorizers shouldn't affect the response headers sent back to the client */
2226 /* don't forward Status: */
2227 if (0 != strncasecmp(key
, "Status", key_len
)) {
2229 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
2230 ds
= data_response_init();
2232 buffer_copy_string_len(ds
->key
, key
, key_len
);
2233 buffer_copy_string(ds
->value
, value
);
2235 array_insert_unique(con
->response
.headers
, (data_unset
*)ds
);
2241 if (0 == strncasecmp(key
, "Date", key_len
)) {
2242 con
->parsed_response
|= HTTP_DATE
;
2246 if (0 == strncasecmp(key
, "Status", key_len
)) {
2247 int status
= strtol(value
, NULL
, 10);
2248 if (status
>= 100 && status
< 1000) {
2249 con
->http_status
= status
;
2250 con
->parsed_response
|= HTTP_STATUS
;
2252 con
->http_status
= 502;
2257 if (0 == strncasecmp(key
, "Location", key_len
)) {
2258 con
->parsed_response
|= HTTP_LOCATION
;
2262 if (0 == strncasecmp(key
, "Connection", key_len
)) {
2263 con
->response
.keep_alive
= (0 == strcasecmp(value
, "Keep-Alive")) ? 1 : 0;
2264 con
->parsed_response
|= HTTP_CONNECTION
;
2268 if (host
->xsendfile_allow
&& 0 == strncasecmp(key
, "X-Sendfile2", key_len
) && hctx
->send_content_body
) {
2273 char *filename
, *range
;
2274 stat_cache_entry
*sce
;
2275 off_t begin_range
, end_range
, range_len
;
2277 while (' ' == *pos
) pos
++;
2281 if (NULL
== (range
= strchr(pos
, ' '))) {
2283 if (p
->conf
.debug
) {
2284 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "Couldn't find range after filename:", filename
);
2288 buffer_copy_string_len(srv
->tmp_buf
, filename
, range
- filename
);
2290 /* find end of range */
2291 for (pos
= ++range
; *pos
&& *pos
!= ' ' && *pos
!= ','; pos
++) ;
2293 buffer_urldecode_path(srv
->tmp_buf
);
2294 buffer_path_simplify(srv
->tmp_buf
, srv
->tmp_buf
);
2295 if (con
->conf
.force_lowercase_filenames
) {
2296 buffer_to_lower(srv
->tmp_buf
);
2298 if (host
->xsendfile_docroot
->used
) {
2299 size_t i
, xlen
= buffer_string_length(srv
->tmp_buf
);
2300 for (i
= 0; i
< host
->xsendfile_docroot
->used
; ++i
) {
2301 data_string
*ds
= (data_string
*)host
->xsendfile_docroot
->data
[i
];
2302 size_t dlen
= buffer_string_length(ds
->value
);
2304 && (!con
->conf
.force_lowercase_filenames
2305 ? 0 == memcmp(srv
->tmp_buf
->ptr
, ds
->value
->ptr
, dlen
)
2306 : 0 == strncasecmp(srv
->tmp_buf
->ptr
, ds
->value
->ptr
, dlen
))) {
2310 if (i
== host
->xsendfile_docroot
->used
) {
2311 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
2312 "X-Sendfile2 (", srv
->tmp_buf
,
2313 ") not under configured x-sendfile-docroot(s)");
2318 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, srv
->tmp_buf
, &sce
)) {
2319 if (p
->conf
.debug
) {
2320 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
2321 "send-file error: couldn't get stat_cache entry for X-Sendfile2:",
2325 } else if (!S_ISREG(sce
->st
.st_mode
)) {
2326 if (p
->conf
.debug
) {
2327 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
2328 "send-file error: wrong filetype for X-Sendfile2:",
2333 /* found the file */
2336 begin_range
= 0; end_range
= sce
->st
.st_size
- 1;
2340 begin_range
= strtoll(range
, &rpos
, 10);
2341 if (errno
!= 0 || begin_range
< 0 || rpos
== range
) goto range_failed
;
2342 if ('-' != *rpos
++) goto range_failed
;
2345 end_range
= strtoll(range
, &rpos
, 10);
2346 if (errno
!= 0 || end_range
< 0 || rpos
== range
) goto range_failed
;
2348 if (rpos
!= pos
) goto range_failed
;
2353 if (p
->conf
.debug
) {
2354 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "Couldn't decode range after filename:", filename
);
2361 /* no parameters accepted */
2363 while (*pos
== ' ') pos
++;
2364 if (*pos
!= '\0' && *pos
!= ',') return 502;
2366 range_len
= end_range
- begin_range
+ 1;
2367 if (range_len
< 0) return 502;
2368 if (range_len
!= 0) {
2369 if (0 != http_chunk_append_file_range(srv
, con
, srv
->tmp_buf
, begin_range
, range_len
)) {
2373 sendfile2_content_length
+= range_len
;
2375 if (*pos
== ',') pos
++;
2380 if (0 == strncasecmp(key
, "Content-Length", key_len
)) {
2381 con
->response
.content_length
= strtoul(value
, NULL
, 10);
2382 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
2384 if (con
->response
.content_length
< 0) con
->response
.content_length
= 0;
2392 if (have_sendfile2
) {
2395 /* fix content-length */
2396 if (NULL
== (dcls
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
2397 dcls
= data_response_init();
2400 buffer_copy_string_len(dcls
->key
, "Content-Length", sizeof("Content-Length")-1);
2401 buffer_copy_int(dcls
->value
, sendfile2_content_length
);
2402 array_replace(con
->response
.headers
, (data_unset
*)dcls
);
2404 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
2405 con
->response
.content_length
= sendfile2_content_length
;
2409 /* CGI/1.1 rev 03 - 7.2.1.2 */
2410 if ((con
->parsed_response
& HTTP_LOCATION
) &&
2411 !(con
->parsed_response
& HTTP_STATUS
)) {
2412 con
->http_status
= 302;
2424 } fastcgi_response_packet
;
2426 static int fastcgi_get_packet(server
*srv
, handler_ctx
*hctx
, fastcgi_response_packet
*packet
) {
2430 FCGI_Header
*header
;
2432 if (!hctx
->rb
->first
) return -1;
2434 packet
->b
= buffer_init();
2437 packet
->padding
= 0;
2438 packet
->request_id
= 0;
2440 offset
= 0; toread
= 8;
2441 /* get at least the FastCGI header */
2442 for (c
= hctx
->rb
->first
; c
; c
= c
->next
) {
2443 size_t weHave
= buffer_string_length(c
->mem
) - c
->offset
;
2445 if (weHave
> toread
) weHave
= toread
;
2447 buffer_append_string_len(packet
->b
, c
->mem
->ptr
+ c
->offset
, weHave
);
2449 offset
= weHave
; /* skip offset bytes in chunk for "real" data */
2451 if (0 == toread
) break;
2454 if (buffer_string_length(packet
->b
) < sizeof(FCGI_Header
)) {
2456 if (hctx
->plugin_data
->conf
.debug
) {
2457 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");
2460 buffer_free(packet
->b
);
2465 /* we have at least a header, now check how much me have to fetch */
2466 header
= (FCGI_Header
*)(packet
->b
->ptr
);
2468 packet
->len
= (header
->contentLengthB0
| (header
->contentLengthB1
<< 8)) + header
->paddingLength
;
2469 packet
->request_id
= (header
->requestIdB0
| (header
->requestIdB1
<< 8));
2470 packet
->type
= header
->type
;
2471 packet
->padding
= header
->paddingLength
;
2473 /* ->b should only be the content */
2474 buffer_string_set_length(packet
->b
, 0);
2477 /* copy the content */
2478 for (; c
&& (buffer_string_length(packet
->b
) < packet
->len
); c
= c
->next
) {
2479 size_t weWant
= packet
->len
- buffer_string_length(packet
->b
);
2480 size_t weHave
= buffer_string_length(c
->mem
) - c
->offset
- offset
;
2482 if (weHave
> weWant
) weHave
= weWant
;
2484 buffer_append_string_len(packet
->b
, c
->mem
->ptr
+ c
->offset
+ offset
, weHave
);
2486 /* we only skipped the first bytes as they belonged to the fcgi header */
2490 if (buffer_string_length(packet
->b
) < packet
->len
) {
2491 /* we didn't get the full packet */
2493 buffer_free(packet
->b
);
2497 buffer_string_set_length(packet
->b
, buffer_string_length(packet
->b
) - packet
->padding
);
2500 chunkqueue_mark_written(hctx
->rb
, packet
->len
+ sizeof(FCGI_Header
));
2505 static int fcgi_demux_response(server
*srv
, handler_ctx
*hctx
) {
2510 plugin_data
*p
= hctx
->plugin_data
;
2511 connection
*con
= hctx
->remote_conn
;
2512 int fcgi_fd
= hctx
->fd
;
2513 fcgi_extension_host
*host
= hctx
->host
;
2514 fcgi_proc
*proc
= hctx
->proc
;
2517 * check how much we have to read
2519 #if !defined(_WIN32) && !defined(__CYGWIN__)
2520 if (ioctl(hctx
->fd
, FIONREAD
, &toread
)) {
2521 if (errno
== EAGAIN
) {
2522 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2525 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2526 "unexpected end-of-file (perhaps the fastcgi process died):",
2538 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)) {
2539 off_t cqlen
= chunkqueue_length(hctx
->rb
);
2540 if (cqlen
+ toread
> 65536 + (int)sizeof(FCGI_Header
)) { /*(max size of FastCGI packet + 1)*/
2541 if (cqlen
< 65536 + (int)sizeof(FCGI_Header
)) {
2542 toread
= 65536 + (int)sizeof(FCGI_Header
) - cqlen
;
2543 } else { /* should not happen */
2544 toread
= toread
< 1024 ? toread
: 1024;
2549 chunkqueue_get_memory(hctx
->rb
, &mem
, &mem_len
, 0, toread
);
2550 r
= read(hctx
->fd
, mem
, mem_len
);
2551 chunkqueue_use_memory(hctx
->rb
, r
> 0 ? r
: 0);
2554 if (errno
== EAGAIN
) {
2555 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2558 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
2559 "unexpected end-of-file (perhaps the fastcgi process died):",
2560 fcgi_fd
, strerror(errno
));
2565 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsb",
2566 "unexpected end-of-file (perhaps the fastcgi process died):",
2568 "socket:", proc
->connection_name
);
2574 * parse the fastcgi packets and forward the content to the write-queue
2578 fastcgi_response_packet packet
;
2580 /* check if we have at least one packet */
2581 if (0 != fastcgi_get_packet(srv
, hctx
, &packet
)) {
2582 /* no full packet */
2586 switch(packet
.type
) {
2588 if (packet
.len
== 0) break;
2590 /* is the header already finished */
2591 if (0 == con
->file_started
) {
2595 /* search for header terminator
2597 * if we start with \r\n check if last packet terminated with \r\n
2598 * if we start with \n check if last packet terminated with \n
2599 * search for \r\n\r\n
2603 buffer_append_string_buffer(hctx
->response_header
, packet
.b
);
2605 if (NULL
!= (c
= buffer_search_string_len(hctx
->response_header
, CONST_STR_LEN("\r\n\r\n")))) {
2606 char *hend
= c
+ 4; /* header end == body start */
2607 size_t hlen
= hend
- hctx
->response_header
->ptr
;
2608 buffer_copy_string_len(packet
.b
, hend
, buffer_string_length(hctx
->response_header
) - hlen
);
2609 buffer_string_set_length(hctx
->response_header
, hlen
);
2610 } else if (NULL
!= (c
= buffer_search_string_len(hctx
->response_header
, CONST_STR_LEN("\n\n")))) {
2611 char *hend
= c
+ 2; /* header end == body start */
2612 size_t hlen
= hend
- hctx
->response_header
->ptr
;
2613 buffer_copy_string_len(packet
.b
, hend
, buffer_string_length(hctx
->response_header
) - hlen
);
2614 buffer_string_set_length(hctx
->response_header
, hlen
);
2616 /* no luck, no header found */
2617 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
2618 if (buffer_string_length(hctx
->response_header
) > MAX_HTTP_REQUEST_HEADER
) {
2619 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "response headers too large for", con
->uri
.path
);
2620 con
->http_status
= 502; /* Bad Gateway */
2627 /* parse the response header */
2628 if ((ret
= fcgi_response_parse(srv
, con
, p
, hctx
->response_header
))) {
2629 if (200 != ret
) { /*(200 returned for X-Sendfile2 handled)*/
2630 con
->http_status
= ret
;
2633 con
->file_started
= 1;
2634 hctx
->send_content_body
= 0;
2639 con
->file_started
= 1;
2641 if (host
->mode
== FCGI_AUTHORIZER
&&
2642 (con
->http_status
== 0 ||
2643 con
->http_status
== 200)) {
2644 /* a authorizer with approved the static request, ignore the content here */
2645 hctx
->send_content_body
= 0;
2648 if (host
->xsendfile_allow
&& hctx
->send_content_body
&&
2649 (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-LIGHTTPD-send-file"))
2650 || NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-Sendfile")))) {
2651 http_response_xsendfile(srv
, con
, ds
->value
, host
->xsendfile_docroot
);
2652 if (con
->mode
== DIRECT
) {
2656 hctx
->send_content_body
= 0; /* ignore the content */
2661 if (hctx
->send_content_body
&& !buffer_string_is_empty(packet
.b
)) {
2662 if (0 != http_chunk_append_buffer(srv
, con
, packet
.b
)) {
2663 /* error writing to tempfile;
2664 * truncate response or send 500 if nothing sent yet */
2668 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
2669 && chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
2670 if (!con
->is_writable
) {
2671 /*(defer removal of FDEVENT_IN interest since
2672 * connection_state_machine() might be able to send data
2673 * immediately, unless !con->is_writable, where
2674 * connection_state_machine() might not loop back to call
2675 * mod_fastcgi_handle_subrequest())*/
2676 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2682 if (packet
.len
== 0) break;
2684 log_error_write_multiline_buffer(srv
, __FILE__
, __LINE__
, packet
.b
, "s",
2688 case FCGI_END_REQUEST
:
2692 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2693 "FastCGI: header.type not handled: ", packet
.type
);
2696 buffer_free(packet
.b
);
2702 static int fcgi_restart_dead_procs(server
*srv
, plugin_data
*p
, fcgi_extension_host
*host
) {
2705 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
2708 if (p
->conf
.debug
> 2) {
2709 log_error_write(srv
, __FILE__
, __LINE__
, "sbdddd",
2711 proc
->connection_name
,
2719 * if the remote side is overloaded, we check back after <n> seconds
2722 switch (proc
->state
) {
2723 case PROC_STATE_KILLED
:
2724 case PROC_STATE_UNSET
:
2725 /* this should never happen as long as adaptive spawing is disabled */
2729 case PROC_STATE_RUNNING
:
2731 case PROC_STATE_OVERLOADED
:
2732 if (srv
->cur_ts
<= proc
->disabled_until
) break;
2734 proc
->state
= PROC_STATE_RUNNING
;
2735 host
->active_procs
++;
2737 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb",
2738 "fcgi-server re-enabled:",
2739 host
->host
, host
->port
,
2742 case PROC_STATE_DIED_WAIT_FOR_PID
:
2743 /* non-local procs don't have PIDs to wait for */
2744 if (!proc
->is_local
) {
2745 proc
->state
= PROC_STATE_DIED
;
2747 /* the child should not terminate at all */
2750 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
2752 /* child is still alive */
2753 if (srv
->cur_ts
<= proc
->disabled_until
) break;
2755 proc
->state
= PROC_STATE_RUNNING
;
2756 host
->active_procs
++;
2758 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb",
2759 "fcgi-server re-enabled:",
2760 host
->host
, host
->port
,
2764 if (errno
== EINTR
) continue;
2766 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2767 "child died somehow, waitpid failed:",
2769 proc
->state
= PROC_STATE_DIED
;
2772 if (WIFEXITED(status
)) {
2774 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
2775 "child exited, pid:", proc
->pid
,
2776 "status:", WEXITSTATUS(status
));
2778 } else if (WIFSIGNALED(status
)) {
2779 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2783 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2784 "child died somehow:",
2788 proc
->state
= PROC_STATE_DIED
;
2795 /* fall through if we have a dead proc now */
2796 if (proc
->state
!= PROC_STATE_DIED
) break;
2798 case PROC_STATE_DIED
:
2799 /* local procs get restarted by us,
2800 * remote ones hopefully by the admin */
2802 if (!buffer_string_is_empty(host
->bin_path
)) {
2803 /* we still have connections bound to this proc,
2804 * let them terminate first */
2805 if (proc
->load
!= 0) break;
2807 /* restart the child */
2809 if (p
->conf
.debug
) {
2810 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsd",
2811 "--- fastcgi spawning",
2812 "\n\tsocket", proc
->connection_name
,
2813 "\n\tcurrent:", 1, "/", host
->max_procs
);
2816 if (fcgi_spawn_connection(srv
, p
, host
, proc
)) {
2817 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2818 "ERROR: spawning fcgi failed.");
2819 return HANDLER_ERROR
;
2822 if (srv
->cur_ts
<= proc
->disabled_until
) break;
2824 proc
->state
= PROC_STATE_RUNNING
;
2825 host
->active_procs
++;
2827 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
2828 "fcgi-server re-enabled:",
2829 proc
->connection_name
);
2838 static handler_t
fcgi_write_request(server
*srv
, handler_ctx
*hctx
) {
2839 plugin_data
*p
= hctx
->plugin_data
;
2840 fcgi_extension_host
*host
= hctx
->host
;
2841 connection
*con
= hctx
->remote_conn
;
2849 * - tcp socket (do not check host->host->uses, as it may be not set which means INADDR_LOOPBACK)
2853 log_error_write(srv
, __FILE__
, __LINE__
, "s", "fatal error: host = NULL");
2854 return HANDLER_ERROR
;
2856 if ((!host
->port
&& buffer_string_is_empty(host
->unixsocket
))) {
2857 log_error_write(srv
, __FILE__
, __LINE__
, "s", "fatal error: neither host->port nor host->unixsocket is set");
2858 return HANDLER_ERROR
;
2861 /* we can't handle this in the switch as we have to fall through in it */
2862 if (hctx
->state
== FCGI_STATE_CONNECT_DELAYED
) {
2864 socklen_t socket_error_len
= sizeof(socket_error
);
2866 /* try to finish the connect() */
2867 if (0 != getsockopt(hctx
->fd
, SOL_SOCKET
, SO_ERROR
, &socket_error
, &socket_error_len
)) {
2868 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2869 "getsockopt failed:", strerror(errno
));
2871 fcgi_host_disable(srv
, hctx
);
2873 return HANDLER_ERROR
;
2875 if (socket_error
!= 0) {
2876 if (!hctx
->proc
->is_local
|| p
->conf
.debug
) {
2877 /* local procs get restarted */
2879 log_error_write(srv
, __FILE__
, __LINE__
, "sssb",
2880 "establishing connection failed:", strerror(socket_error
),
2881 "socket:", hctx
->proc
->connection_name
);
2884 fcgi_host_disable(srv
, hctx
);
2885 log_error_write(srv
, __FILE__
, __LINE__
, "sdssdsd",
2886 "backend is overloaded; we'll disable it for", hctx
->host
->disable_time
, "seconds and send the request to another backend instead:",
2887 "reconnects:", hctx
->reconnects
,
2888 "load:", host
->load
);
2890 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
2891 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".died"));
2893 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
2895 return HANDLER_ERROR
;
2897 /* go on with preparing the request */
2898 hctx
->state
= FCGI_STATE_PREPARE_WRITE
;
2902 switch(hctx
->state
) {
2903 case FCGI_STATE_CONNECT_DELAYED
:
2904 /* should never happen */
2905 return HANDLER_WAIT_FOR_EVENT
;
2906 case FCGI_STATE_INIT
:
2907 /* do we have a running process for this host (max-procs) ? */
2910 for (proc
= hctx
->host
->first
;
2911 proc
&& proc
->state
!= PROC_STATE_RUNNING
;
2914 /* all children are dead */
2918 return HANDLER_ERROR
;
2923 /* check the other procs if they have a lower load */
2924 for (proc
= proc
->next
; proc
; proc
= proc
->next
) {
2925 if (proc
->state
!= PROC_STATE_RUNNING
) continue;
2926 if (proc
->load
< hctx
->proc
->load
) hctx
->proc
= proc
;
2929 if (-1 == (hctx
->fd
= socket(host
->family
, SOCK_STREAM
, 0))) {
2930 if (errno
== EMFILE
||
2932 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2933 "wait for fd at connection:", con
->fd
);
2935 return HANDLER_WAIT_FOR_FD
;
2938 log_error_write(srv
, __FILE__
, __LINE__
, "ssdd",
2939 "socket failed:", strerror(errno
), srv
->cur_fds
, srv
->max_fds
);
2940 return HANDLER_ERROR
;
2946 fdevent_register(srv
->ev
, hctx
->fd
, fcgi_handle_fdevent
, hctx
);
2948 if (-1 == fdevent_fcntl_set(srv
->ev
, hctx
->fd
)) {
2949 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2950 "fcntl failed:", strerror(errno
));
2952 return HANDLER_ERROR
;
2955 if (hctx
->proc
->is_local
) {
2956 hctx
->pid
= hctx
->proc
->pid
;
2959 switch (fcgi_establish_connection(srv
, hctx
)) {
2960 case CONNECTION_DELAYED
:
2961 /* connection is in progress, wait for an event and call getsockopt() below */
2963 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2965 fcgi_set_state(srv
, hctx
, FCGI_STATE_CONNECT_DELAYED
);
2966 return HANDLER_WAIT_FOR_EVENT
;
2967 case CONNECTION_OVERLOADED
:
2968 /* cool down the backend, it is overloaded
2971 if (hctx
->host
->disable_time
) {
2972 log_error_write(srv
, __FILE__
, __LINE__
, "sdssdsd",
2973 "backend is overloaded; we'll disable it for", hctx
->host
->disable_time
, "seconds and send the request to another backend instead:",
2974 "reconnects:", hctx
->reconnects
,
2975 "load:", host
->load
);
2977 hctx
->proc
->disabled_until
= srv
->cur_ts
+ hctx
->host
->disable_time
;
2978 if (hctx
->proc
->state
== PROC_STATE_RUNNING
) hctx
->host
->active_procs
--;
2979 hctx
->proc
->state
= PROC_STATE_OVERLOADED
;
2982 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
2983 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".overloaded"));
2985 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
2987 return HANDLER_ERROR
;
2988 case CONNECTION_DEAD
:
2989 /* we got a hard error from the backend like
2990 * - ECONNREFUSED for tcp-ip sockets
2991 * - ENOENT for unix-domain-sockets
2993 * for check if the host is back in hctx->host->disable_time seconds
2996 fcgi_host_disable(srv
, hctx
);
2998 log_error_write(srv
, __FILE__
, __LINE__
, "sdssdsd",
2999 "backend died; we'll disable it for", hctx
->host
->disable_time
, "seconds and send the request to another backend instead:",
3000 "reconnects:", hctx
->reconnects
,
3001 "load:", host
->load
);
3003 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
3004 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".died"));
3006 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
3008 return HANDLER_ERROR
;
3010 /* everything is ok, go on */
3012 fcgi_set_state(srv
, hctx
, FCGI_STATE_PREPARE_WRITE
);
3017 case FCGI_STATE_PREPARE_WRITE
:
3018 /* ok, we have the connection */
3020 fcgi_proc_load_inc(srv
, hctx
);
3023 status_counter_inc(srv
, CONST_STR_LEN("fastcgi.requests"));
3025 fastcgi_status_copy_procname(p
->statuskey
, hctx
->host
, hctx
->proc
);
3026 buffer_append_string_len(p
->statuskey
, CONST_STR_LEN(".connected"));
3028 status_counter_inc(srv
, CONST_BUF_LEN(p
->statuskey
));
3030 if (p
->conf
.debug
) {
3031 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsd",
3033 "pid:", hctx
->proc
->pid
,
3034 "socket:", hctx
->proc
->connection_name
,
3035 "load:", hctx
->proc
->load
);
3038 /* move the proc-list entry down the list */
3039 if (hctx
->request_id
== 0) {
3040 hctx
->request_id
= 1; /* always use id 1 as we don't use multiplexing */
3042 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3043 "fcgi-request is already in use:", hctx
->request_id
);
3046 if (-1 == fcgi_create_env(srv
, hctx
, hctx
->request_id
)) return HANDLER_ERROR
;
3048 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
3049 fcgi_set_state(srv
, hctx
, FCGI_STATE_WRITE
);
3051 case FCGI_STATE_WRITE
:
3052 ret
= srv
->network_backend_write(srv
, con
, hctx
->fd
, hctx
->wb
, MAX_WRITE_LIMIT
);
3054 chunkqueue_remove_finished_chunks(hctx
->wb
);
3061 /* the connection got dropped after accept()
3062 * we don't care about that - if you accept() it, you have to handle it.
3065 log_error_write(srv
, __FILE__
, __LINE__
, "ssosb",
3066 "connection was dropped after accept() (perhaps the fastcgi process died),",
3067 "write-offset:", hctx
->wb
->bytes_out
,
3068 "socket:", hctx
->proc
->connection_name
);
3070 return HANDLER_ERROR
;
3072 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
3073 "write failed:", strerror(errno
), errno
);
3075 return HANDLER_ERROR
;
3079 if (hctx
->wb
->bytes_out
== hctx
->wb_reqlen
) {
3080 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
3081 fcgi_set_state(srv
, hctx
, FCGI_STATE_READ
);
3083 off_t wblen
= hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
;
3084 if (hctx
->wb
->bytes_in
< hctx
->wb_reqlen
&& wblen
< 65536 - 16384) {
3085 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
3086 if (!(con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
)) {
3087 con
->conf
.stream_request_body
|= FDEVENT_STREAM_REQUEST_POLLIN
;
3088 con
->is_readable
= 1; /* trigger optimistic read from client */
3092 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
3094 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
3098 return HANDLER_WAIT_FOR_EVENT
;
3099 case FCGI_STATE_READ
:
3100 /* waiting for a response */
3101 return HANDLER_WAIT_FOR_EVENT
;
3103 log_error_write(srv
, __FILE__
, __LINE__
, "s", "(debug) unknown state");
3104 return HANDLER_ERROR
;
3109 /* might be called on fdevent after a connect() is delay too
3111 static handler_t
fcgi_send_request(server
*srv
, handler_ctx
*hctx
) {
3112 fcgi_extension_host
*host
;
3115 /* we don't have a host yet, choose one
3116 * -> this happens in the first round
3117 * and when the host died and we have to select a new one */
3118 if (hctx
->host
== NULL
) {
3122 /* check if the next server has no load. */
3123 ndx
= hctx
->ext
->last_used_ndx
+ 1;
3124 if(ndx
>= (int) hctx
->ext
->used
|| ndx
< 0) ndx
= 0;
3125 host
= hctx
->ext
->hosts
[ndx
];
3126 if (host
->load
> 0) {
3127 /* get backend with the least load. */
3128 for (k
= 0, ndx
= -1; k
< hctx
->ext
->used
; k
++) {
3129 host
= hctx
->ext
->hosts
[k
];
3131 /* we should have at least one proc that can do something */
3132 if (host
->active_procs
== 0) continue;
3134 if (used
== -1 || host
->load
< used
) {
3142 /* found a server */
3144 /* all hosts are down */
3146 fcgi_connection_close(srv
, hctx
);
3148 return HANDLER_FINISHED
;
3151 hctx
->ext
->last_used_ndx
= ndx
;
3152 host
= hctx
->ext
->hosts
[ndx
];
3155 * if check-local is disabled, use the uri.path handler
3159 /* init handler-context */
3161 /* we put a connection on this host, move the other new connections to other hosts
3163 * as soon as hctx->host is unassigned, decrease the load again */
3164 fcgi_host_assign(srv
, hctx
, host
);
3170 /* ok, create the request */
3171 rc
= fcgi_write_request(srv
, hctx
);
3172 if (HANDLER_ERROR
!= rc
) {
3175 plugin_data
*p
= hctx
->plugin_data
;
3176 connection
*con
= hctx
->remote_conn
;
3178 if (hctx
->state
== FCGI_STATE_INIT
||
3179 hctx
->state
== FCGI_STATE_CONNECT_DELAYED
) {
3180 fcgi_restart_dead_procs(srv
, p
, host
);
3182 /* cleanup this request and let the request handler start this request again */
3183 if (hctx
->reconnects
< 5) {
3184 fcgi_reconnect(srv
, hctx
);
3186 return HANDLER_COMEBACK
;
3188 fcgi_connection_close(srv
, hctx
);
3189 con
->http_status
= 503;
3191 return HANDLER_FINISHED
;
3194 int status
= con
->http_status
;
3195 fcgi_connection_close(srv
, hctx
);
3196 con
->http_status
= (status
== 400) ? 400 : 503; /* see FCGI_ENV_ADD_CHECK() for 400 error */
3198 return HANDLER_FINISHED
;
3204 static handler_t
fcgi_recv_response(server
*srv
, handler_ctx
*hctx
);
3207 SUBREQUEST_FUNC(mod_fastcgi_handle_subrequest
) {
3208 plugin_data
*p
= p_d
;
3210 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
3212 if (NULL
== hctx
) return HANDLER_GO_ON
;
3215 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
3217 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
3218 && con
->file_started
) {
3219 if (chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
3220 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
3221 } else if (!(fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_IN
)) {
3222 /* optimistic read from backend, which might re-enable FDEVENT_IN */
3223 handler_t rc
= fcgi_recv_response(srv
, hctx
); /*(might invalidate hctx)*/
3224 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
3228 if (0 == hctx
->wb
->bytes_in
3229 ? con
->state
== CON_STATE_READ_POST
3230 : hctx
->wb
->bytes_in
< hctx
->wb_reqlen
) {
3231 /*(64k - 4k to attempt to avoid temporary files
3232 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
3233 if (hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
> 65536 - 4096
3234 && (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_BUFMIN
)){
3235 con
->conf
.stream_request_body
&= ~FDEVENT_STREAM_REQUEST_POLLIN
;
3236 if (0 != hctx
->wb
->bytes_in
) return HANDLER_WAIT_FOR_EVENT
;
3238 handler_t r
= connection_handle_read_post_state(srv
, con
);
3239 chunkqueue
*req_cq
= con
->request_content_queue
;
3240 if (0 != hctx
->wb
->bytes_in
&& !chunkqueue_is_empty(req_cq
)) {
3241 fcgi_stdin_append(srv
, con
, hctx
, hctx
->request_id
);
3242 if (fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_OUT
) {
3243 return (r
== HANDLER_GO_ON
) ? HANDLER_WAIT_FOR_EVENT
: r
;
3246 if (r
!= HANDLER_GO_ON
) return r
;
3250 return (0 == hctx
->wb
->bytes_in
|| !chunkqueue_is_empty(hctx
->wb
))
3251 ? fcgi_send_request(srv
, hctx
)
3252 : HANDLER_WAIT_FOR_EVENT
;
3256 static handler_t
fcgi_recv_response(server
*srv
, handler_ctx
*hctx
) {
3257 connection
*con
= hctx
->remote_conn
;
3258 plugin_data
*p
= hctx
->plugin_data
;
3260 fcgi_proc
*proc
= hctx
->proc
;
3261 fcgi_extension_host
*host
= hctx
->host
;
3263 switch (fcgi_demux_response(srv
, hctx
)) {
3268 if (host
->mode
== FCGI_AUTHORIZER
&&
3269 (con
->http_status
== 200 ||
3270 con
->http_status
== 0)) {
3272 * If we are here in AUTHORIZER mode then a request for authorizer
3273 * was processed already, and status 200 has been returned. We need
3274 * now to handle authorized request.
3277 buffer_copy_buffer(con
->physical
.doc_root
, host
->docroot
);
3278 buffer_copy_buffer(con
->physical
.basedir
, host
->docroot
);
3280 buffer_copy_buffer(con
->physical
.path
, host
->docroot
);
3281 buffer_append_string_buffer(con
->physical
.path
, con
->uri
.path
);
3283 con
->mode
= DIRECT
;/*(avoid changing con->state, con->http_status)*/
3284 fcgi_connection_close(srv
, hctx
);
3285 con
->http_status
= 0;
3286 con
->file_started
= 1; /* fcgi_extension won't touch the request afterwards */
3289 fcgi_connection_close(srv
, hctx
);
3292 return HANDLER_FINISHED
;
3294 if (proc
->pid
&& proc
->state
!= PROC_STATE_DIED
) {
3297 /* only fetch the zombie if it is not already done */
3299 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
3301 /* child is still alive */
3306 /* the child should not terminate at all */
3307 if (WIFEXITED(status
)) {
3308 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
3309 "child exited, pid:", proc
->pid
,
3310 "status:", WEXITSTATUS(status
));
3311 } else if (WIFSIGNALED(status
)) {
3312 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3316 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3317 "child died somehow:",
3321 if (p
->conf
.debug
) {
3322 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsd",
3323 "--- fastcgi spawning",
3324 "\n\tsocket", proc
->connection_name
,
3325 "\n\tcurrent:", 1, "/", host
->max_procs
);
3328 if (fcgi_spawn_connection(srv
, p
, host
, proc
)) {
3329 /* respawning failed, retry later */
3330 proc
->state
= PROC_STATE_DIED
;
3332 log_error_write(srv
, __FILE__
, __LINE__
, "s",
3333 "respawning failed, will retry later");
3340 if (con
->file_started
== 0) {
3341 /* nothing has been sent out yet, try to use another child */
3343 if (hctx
->wb
->bytes_out
== 0 &&
3344 hctx
->reconnects
< 5) {
3346 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsBSBs",
3347 "response not received, request not sent",
3348 "on socket:", proc
->connection_name
,
3349 "for", con
->uri
.path
, "?", con
->uri
.query
, ", reconnecting");
3351 fcgi_reconnect(srv
, hctx
);
3353 return HANDLER_COMEBACK
;
3356 log_error_write(srv
, __FILE__
, __LINE__
, "sosbsBSBs",
3357 "response not received, request sent:", hctx
->wb
->bytes_out
,
3358 "on socket:", proc
->connection_name
,
3359 "for", con
->uri
.path
, "?", con
->uri
.query
, ", closing connection");
3361 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsBSBs",
3362 "response already sent out, but backend returned error",
3363 "on socket:", proc
->connection_name
,
3364 "for", con
->uri
.path
, "?", con
->uri
.query
, ", terminating connection");
3367 http_response_backend_error(srv
, con
);
3368 fcgi_connection_close(srv
, hctx
);
3369 return HANDLER_FINISHED
;
3372 return HANDLER_GO_ON
;
3376 static handler_t
fcgi_handle_fdevent(server
*srv
, void *ctx
, int revents
) {
3377 handler_ctx
*hctx
= ctx
;
3378 connection
*con
= hctx
->remote_conn
;
3380 joblist_append(srv
, con
);
3382 if (revents
& FDEVENT_IN
) {
3383 handler_t rc
= fcgi_recv_response(srv
, hctx
);/*(might invalidate hctx)*/
3384 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
3387 if (revents
& FDEVENT_OUT
) {
3388 return fcgi_send_request(srv
, hctx
); /*(might invalidate hctx)*/
3391 /* perhaps this issue is already handled */
3392 if (revents
& FDEVENT_HUP
) {
3393 if (hctx
->state
== FCGI_STATE_CONNECT_DELAYED
) {
3394 /* getoptsock will catch this one (right ?)
3396 * if we are in connect we might get an EINPROGRESS
3397 * in the first call and an FDEVENT_HUP in the
3400 * FIXME: as it is a bit ugly.
3403 fcgi_send_request(srv
, hctx
);
3404 } else if (con
->file_started
) {
3405 /* drain any remaining data from kernel pipe buffers
3406 * even if (con->conf.stream_response_body
3407 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
3408 * since event loop will spin on fd FDEVENT_HUP event
3409 * until unregistered. */
3412 rc
= fcgi_recv_response(srv
,hctx
);/*(might invalidate hctx)*/
3413 } while (rc
== HANDLER_GO_ON
); /*(unless HANDLER_GO_ON)*/
3414 return rc
; /* HANDLER_FINISHED or HANDLER_ERROR */
3416 fcgi_proc
*proc
= hctx
->proc
;
3417 log_error_write(srv
, __FILE__
, __LINE__
, "sBSbsbsd",
3418 "error: unexpected close of fastcgi connection for",
3419 con
->uri
.path
, "?", con
->uri
.query
,
3420 "(no fastcgi process on socket:", proc
->connection_name
, "?)",
3423 fcgi_connection_close(srv
, hctx
);
3425 } else if (revents
& FDEVENT_ERR
) {
3426 log_error_write(srv
, __FILE__
, __LINE__
, "s",
3427 "fcgi: got a FDEVENT_ERR. Don't know why.");
3429 http_response_backend_error(srv
, con
);
3430 fcgi_connection_close(srv
, hctx
);
3433 return HANDLER_FINISHED
;
3438 static int fcgi_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
3440 plugin_config
*s
= p
->config_storage
[0];
3446 /* skip the first, the global context */
3447 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
3448 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
3449 s
= p
->config_storage
[i
];
3451 /* condition didn't match */
3452 if (!config_check_cond(srv
, con
, dc
)) continue;
3455 for (j
= 0; j
< dc
->value
->used
; j
++) {
3456 data_unset
*du
= dc
->value
->data
[j
];
3458 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("fastcgi.server"))) {
3460 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("fastcgi.debug"))) {
3462 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("fastcgi.map-extensions"))) {
3473 static handler_t
fcgi_check_extension(server
*srv
, connection
*con
, void *p_d
, int uri_path_handler
) {
3474 plugin_data
*p
= p_d
;
3478 fcgi_extension
*extension
= NULL
;
3479 fcgi_extension_host
*host
= NULL
;
3481 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
3483 /* Possibly, we processed already this request */
3484 if (con
->file_started
== 1) return HANDLER_GO_ON
;
3486 fn
= uri_path_handler
? con
->uri
.path
: con
->physical
.path
;
3488 if (buffer_string_is_empty(fn
)) return HANDLER_GO_ON
;
3490 s_len
= buffer_string_length(fn
);
3492 fcgi_patch_connection(srv
, con
, p
);
3494 /* fastcgi.map-extensions maps extensions to existing fastcgi.server entries
3496 * fastcgi.map-extensions = ( ".php3" => ".php" )
3498 * fastcgi.server = ( ".php" => ... )
3502 /* check if extension-mapping matches */
3503 for (k
= 0; k
< p
->conf
.ext_mapping
->used
; k
++) {
3504 data_string
*ds
= (data_string
*)p
->conf
.ext_mapping
->data
[k
];
3505 size_t ct_len
; /* length of the config entry */
3507 if (buffer_is_empty(ds
->key
)) continue;
3509 ct_len
= buffer_string_length(ds
->key
);
3511 if (s_len
< ct_len
) continue;
3513 /* found a mapping */
3514 if (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ds
->key
->ptr
, ct_len
)) {
3515 /* check if we know the extension */
3517 /* we can reuse k here */
3518 for (k
= 0; k
< p
->conf
.exts
->used
; k
++) {
3519 extension
= p
->conf
.exts
->exts
[k
];
3521 if (buffer_is_equal(ds
->value
, extension
->key
)) {
3526 if (k
== p
->conf
.exts
->used
) {
3534 if (extension
== NULL
) {
3535 size_t uri_path_len
= buffer_string_length(con
->uri
.path
);
3537 /* check if extension matches */
3538 for (k
= 0; k
< p
->conf
.exts
->used
; k
++) {
3539 size_t ct_len
; /* length of the config entry */
3540 fcgi_extension
*ext
= p
->conf
.exts
->exts
[k
];
3542 if (buffer_is_empty(ext
->key
)) continue;
3544 ct_len
= buffer_string_length(ext
->key
);
3546 /* check _url_ in the form "/fcgi_pattern" */
3547 if (ext
->key
->ptr
[0] == '/') {
3548 if ((ct_len
<= uri_path_len
) &&
3549 (strncmp(con
->uri
.path
->ptr
, ext
->key
->ptr
, ct_len
) == 0)) {
3553 } else if ((ct_len
<= s_len
) && (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ext
->key
->ptr
, ct_len
))) {
3554 /* check extension in the form ".fcg" */
3559 /* extension doesn't match */
3560 if (NULL
== extension
) {
3561 return HANDLER_GO_ON
;
3565 /* check if we have at least one server for this extension up and running */
3566 for (k
= 0; k
< extension
->used
; k
++) {
3567 fcgi_extension_host
*h
= extension
->hosts
[k
];
3569 /* we should have at least one proc that can do something */
3570 if (h
->active_procs
== 0) {
3574 /* we found one host that is alive */
3580 /* sorry, we don't have a server alive for this ext */
3581 con
->http_status
= 500;
3584 /* only send the 'no handler' once */
3585 if (!extension
->note_is_sent
) {
3586 extension
->note_is_sent
= 1;
3588 log_error_write(srv
, __FILE__
, __LINE__
, "sBSbsbs",
3589 "all handlers for", con
->uri
.path
, "?", con
->uri
.query
,
3590 "on", extension
->key
,
3594 return HANDLER_FINISHED
;
3597 /* a note about no handler is not sent yet */
3598 extension
->note_is_sent
= 0;
3601 * if check-local is disabled, use the uri.path handler
3605 /* init handler-context */
3606 if (uri_path_handler
) {
3607 if (host
->check_local
== 0) {
3611 hctx
= handler_ctx_init();
3613 hctx
->remote_conn
= con
;
3614 hctx
->plugin_data
= p
;
3616 hctx
->ext
= extension
;
3619 hctx
->conf
.exts
= p
->conf
.exts
;
3620 hctx
->conf
.debug
= p
->conf
.debug
;
3622 con
->plugin_ctx
[p
->id
] = hctx
;
3626 if (con
->conf
.log_request_handling
) {
3627 log_error_write(srv
, __FILE__
, __LINE__
, "s",
3628 "handling it in mod_fastcgi");
3631 /* do not split path info for authorizer */
3632 if (host
->mode
!= FCGI_AUTHORIZER
) {
3633 /* the prefix is the SCRIPT_NAME,
3634 * everything from start to the next slash
3635 * this is important for check-local = "disable"
3637 * if prefix = /admin.fcgi
3639 * /admin.fcgi/foo/bar
3641 * SCRIPT_NAME = /admin.fcgi
3642 * PATH_INFO = /foo/bar
3644 * if prefix = /fcgi-bin/
3648 * SCRIPT_NAME = /fcgi-bin/foo
3651 * if prefix = /, and fix-root-path-name is enable
3655 * SCRIPT_NAME = /fcgi-bin/foo
3660 /* the rewrite is only done for /prefix/? matches */
3661 if (host
->fix_root_path_name
&& extension
->key
->ptr
[0] == '/' && extension
->key
->ptr
[1] == '\0') {
3662 buffer_copy_string(con
->request
.pathinfo
, con
->uri
.path
->ptr
);
3663 buffer_string_set_length(con
->uri
.path
, 0);
3664 } else if (extension
->key
->ptr
[0] == '/' &&
3665 buffer_string_length(con
->uri
.path
) > buffer_string_length(extension
->key
) &&
3666 NULL
!= (pathinfo
= strchr(con
->uri
.path
->ptr
+ buffer_string_length(extension
->key
), '/'))) {
3667 /* rewrite uri.path and pathinfo */
3669 buffer_copy_string(con
->request
.pathinfo
, pathinfo
);
3670 buffer_string_set_length(con
->uri
.path
, buffer_string_length(con
->uri
.path
) - buffer_string_length(con
->request
.pathinfo
));
3676 hctx
= handler_ctx_init();
3678 hctx
->remote_conn
= con
;
3679 hctx
->plugin_data
= p
;
3681 hctx
->ext
= extension
;
3683 hctx
->conf
.exts
= p
->conf
.exts
;
3684 hctx
->conf
.debug
= p
->conf
.debug
;
3686 con
->plugin_ctx
[p
->id
] = hctx
;
3690 if (con
->conf
.log_request_handling
) {
3691 log_error_write(srv
, __FILE__
, __LINE__
, "s", "handling it in mod_fastcgi");
3695 return HANDLER_GO_ON
;
3698 /* uri-path handler */
3699 static handler_t
fcgi_check_extension_1(server
*srv
, connection
*con
, void *p_d
) {
3700 return fcgi_check_extension(srv
, con
, p_d
, 1);
3703 /* start request handler */
3704 static handler_t
fcgi_check_extension_2(server
*srv
, connection
*con
, void *p_d
) {
3705 return fcgi_check_extension(srv
, con
, p_d
, 0);
3709 TRIGGER_FUNC(mod_fastcgi_handle_trigger
) {
3710 plugin_data
*p
= p_d
;
3714 /* perhaps we should kill a connect attempt after 10-15 seconds
3716 * currently we wait for the TCP timeout which is 180 seconds on Linux
3722 /* check all children if they are still up */
3724 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
3725 plugin_config
*conf
;
3728 conf
= p
->config_storage
[i
];
3732 for (j
= 0; j
< exts
->used
; j
++) {
3737 for (n
= 0; n
< ex
->used
; n
++) {
3740 fcgi_extension_host
*host
;
3742 host
= ex
->hosts
[n
];
3744 fcgi_restart_dead_procs(srv
, p
, host
);
3746 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
3749 if (proc
->pid
== 0) continue;
3751 switch (waitpid(proc
->pid
, &status
, WNOHANG
)) {
3753 /* child still running after timeout, good */
3756 if (errno
!= EINTR
) {
3757 /* no PID found ? should never happen */
3758 log_error_write(srv
, __FILE__
, __LINE__
, "sddss",
3759 "pid ", proc
->pid
, proc
->state
,
3760 "not found:", strerror(errno
));
3763 if (errno
== ECHILD
) {
3764 /* someone else has cleaned up for us */
3766 proc
->state
= PROC_STATE_UNSET
;
3772 /* the child should not terminate at all */
3773 if (WIFEXITED(status
)) {
3774 if (proc
->state
!= PROC_STATE_KILLED
) {
3775 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
3777 WEXITSTATUS(status
), proc
->connection_name
);
3779 } else if (WIFSIGNALED(status
)) {
3780 if (WTERMSIG(status
) != SIGTERM
) {
3781 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3786 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3787 "child died somehow:",
3791 if (proc
->state
== PROC_STATE_RUNNING
) host
->active_procs
--;
3792 proc
->state
= PROC_STATE_UNSET
;
3800 return HANDLER_GO_ON
;
3804 int mod_fastcgi_plugin_init(plugin
*p
);
3805 int mod_fastcgi_plugin_init(plugin
*p
) {
3806 p
->version
= LIGHTTPD_VERSION_ID
;
3807 p
->name
= buffer_init_string("fastcgi");
3809 p
->init
= mod_fastcgi_init
;
3810 p
->cleanup
= mod_fastcgi_free
;
3811 p
->set_defaults
= mod_fastcgi_set_defaults
;
3812 p
->connection_reset
= fcgi_connection_reset
;
3813 p
->handle_connection_close
= fcgi_connection_reset
;
3814 p
->handle_uri_clean
= fcgi_check_extension_1
;
3815 p
->handle_subrequest_start
= fcgi_check_extension_2
;
3816 p
->handle_subrequest
= mod_fastcgi_handle_subrequest
;
3817 p
->handle_trigger
= mod_fastcgi_handle_trigger
;