8 #include "http_chunk.h"
10 #include "connections.h"
16 #include "inet_ntop_cache.h"
18 #include <sys/types.h>
30 #include "sys-socket.h"
31 #include "sys-endian.h"
37 #ifdef HAVE_SYS_WAIT_H
38 # include <sys/wait.h>
41 enum {EOL_UNSET
, EOL_N
, EOL_RN
};
47 * - add timeout for a connect to a non-scgi process
48 * (use state_timestamp + state)
52 typedef struct scgi_proc
{
53 size_t id
; /* id will be between 1 and max_procs */
54 buffer
*socket
; /* config.socket + "-" + id */
55 unsigned port
; /* config.port + pno */
57 pid_t pid
; /* PID of the spawned process (0 if not spawned locally) */
60 size_t load
; /* number of requests waiting on this process */
62 time_t last_used
; /* see idle_timeout */
63 size_t requests
; /* see max_requests */
64 struct scgi_proc
*prev
, *next
; /* see first */
66 time_t disable_ts
; /* replace by host->something */
70 enum { PROC_STATE_UNSET
, /* init-phase */
71 PROC_STATE_RUNNING
, /* alive */
72 PROC_STATE_DIED_WAIT_FOR_PID
,
73 PROC_STATE_KILLED
, /* was killed as we don't have the load anymore */
74 PROC_STATE_DIED
, /* marked as dead, should be restarted */
75 PROC_STATE_DISABLED
/* proc disabled as it resulted in an error */
80 /* list of processes handling this extension
81 * sorted by lowest load
83 * whenever a job is done move it up in the list
84 * until it is sorted, move it down as soon as the
88 scgi_proc
*unused_procs
;
91 * spawn at least min_procs, at max_procs.
93 * as soon as the load of the first entry
94 * is max_load_per_proc we spawn a new one
95 * and add it to the first entry and give it
100 unsigned short min_procs
;
101 unsigned short max_procs
;
102 size_t num_procs
; /* how many procs are started */
103 size_t active_procs
; /* how many of them are really running */
105 unsigned short max_load_per_proc
;
108 * kick the process from the list if it was not
109 * used for idle_timeout until min_procs is
110 * reached. this helps to get the processlist
111 * small again we had a small peak load.
115 unsigned short idle_timeout
;
118 * time after a disabled remote connection is tried to be re-enabled
123 unsigned short disable_time
;
126 * same scgi processes get a little bit larger
127 * than wanted. max_requests_per_proc kills a
128 * process after a number of handled requests.
131 size_t max_requests_per_proc
;
139 * if host is one of the local IP adresses the
140 * whole connection is local
142 * if tcp/ip should be used host AND port have
153 * instead of TCP/IP we can use Unix Domain Sockets
154 * - more secure (you have fileperms to play with)
155 * - more control (on locally)
156 * - more speed (no extra overhead)
160 /* if socket is local we can start the scgi
163 * bin-path is the path to the binary
165 * check min_procs and max_procs for the number
166 * of process to start-up
170 /* bin-path is set bin-environment is taken to
171 * create the environement before starting the
180 * docroot-translation between URL->phys and the
184 * - different dir-layout if remote
191 * check_local tell you if the phys file is stat()ed
192 * or not. FastCGI doesn't care if the service is
193 * remote. If the web-server side doesn't contain
194 * the scgi-files we should not stat() for them
195 * and say '404 not found'.
197 unsigned short check_local
;
200 * append PATH_INFO to SCRIPT_FILENAME
202 * php needs this if cgi.fix_pathinfo is provied
207 * workaround for program when prefix="/"
209 * rule to build PATH_INFO is hardcoded for when check_local is disabled
210 * enable this option to use the workaround
214 unsigned short fix_root_path_name
;
217 * If the backend includes X-Sendfile in the response
218 * we use the value as filename and ignore the content.
221 unsigned short xsendfile_allow
;
222 array
*xsendfile_docroot
;
224 ssize_t load
; /* replace by host->load */
226 size_t max_id
; /* corresponds most of the time to
229 only if a process is killed max_id waits for the process itself
230 to die and decrements its afterwards */
234 } scgi_extension_host
;
237 * one extension can have multiple hosts assigned
238 * one host can spawn additional processes on the same
239 * socket (if we control it)
241 * ext -> host -> procs
244 * if the scgi process is remote that whole goes down
247 * ext -> host -> procs
250 * in case of PHP and FCGI_CHILDREN we have again a procs
251 * but we don't control it directly.
256 buffer
*key
; /* like .php */
259 scgi_extension_host
**hosts
;
266 scgi_extension
**exts
;
272 enum { LI_PROTOCOL_SCGI
, LI_PROTOCOL_UWSGI
};
288 /* generic plugin data, shared between all connections */
294 buffer
*parse_response
;
296 plugin_config
**config_storage
;
298 plugin_config conf
; /* this is only used as long as no handler_ctx is setup */
301 /* connection specific data */
302 typedef enum { FCGI_STATE_INIT
, FCGI_STATE_CONNECT
, FCGI_STATE_PREPARE_WRITE
,
303 FCGI_STATE_WRITE
, FCGI_STATE_READ
304 } scgi_connection_state_t
;
310 scgi_extension_host
*host
;
312 scgi_connection_state_t state
;
313 time_t state_timestamp
;
318 buffer
*response_header
;
320 int fd
; /* fd to the scgi process */
321 int fde_ndx
; /* index into the fd-event buffer */
325 int reconnects
; /* number of reconnect attempts */
329 connection
*remote_conn
; /* dumb pointer */
330 plugin_data
*plugin_data
; /* dumb pointer */
335 /* ok, we need a prototype */
336 static handler_t
scgi_handle_fdevent(server
*srv
, void *ctx
, int revents
);
338 int scgi_proclist_sort_down(server
*srv
, scgi_extension_host
*host
, scgi_proc
*proc
);
340 static void reset_signals(void) {
342 signal(SIGTTOU
, SIG_DFL
);
345 signal(SIGTTIN
, SIG_DFL
);
348 signal(SIGTSTP
, SIG_DFL
);
350 signal(SIGHUP
, SIG_DFL
);
351 signal(SIGPIPE
, SIG_DFL
);
352 signal(SIGUSR1
, SIG_DFL
);
355 static handler_ctx
* handler_ctx_init(void) {
358 hctx
= calloc(1, sizeof(*hctx
));
363 hctx
->response
= buffer_init();
364 hctx
->response_header
= buffer_init();
366 hctx
->state
= FCGI_STATE_INIT
;
371 hctx
->reconnects
= 0;
373 hctx
->wb
= chunkqueue_init();
379 static void handler_ctx_free(handler_ctx
*hctx
) {
380 buffer_free(hctx
->response
);
381 buffer_free(hctx
->response_header
);
383 chunkqueue_free(hctx
->wb
);
388 static scgi_proc
*scgi_process_init(void) {
391 f
= calloc(1, sizeof(*f
));
393 f
->socket
= buffer_init();
401 static void scgi_process_free(scgi_proc
*f
) {
404 scgi_process_free(f
->next
);
406 buffer_free(f
->socket
);
411 static scgi_extension_host
*scgi_host_init(void) {
412 scgi_extension_host
*f
;
414 f
= calloc(1, sizeof(*f
));
416 f
->host
= buffer_init();
417 f
->unixsocket
= buffer_init();
418 f
->docroot
= buffer_init();
419 f
->bin_path
= buffer_init();
420 f
->bin_env
= array_init();
421 f
->bin_env_copy
= array_init();
422 f
->xsendfile_docroot
= array_init();
427 static void scgi_host_free(scgi_extension_host
*h
) {
434 buffer_free(h
->host
);
435 buffer_free(h
->unixsocket
);
436 buffer_free(h
->docroot
);
437 buffer_free(h
->bin_path
);
438 array_free(h
->bin_env
);
439 array_free(h
->bin_env_copy
);
440 array_free(h
->xsendfile_docroot
);
442 scgi_process_free(h
->first
);
443 scgi_process_free(h
->unused_procs
);
449 static scgi_exts
*scgi_extensions_init(void) {
452 f
= calloc(1, sizeof(*f
));
458 static void scgi_extensions_free(scgi_exts
*f
) {
463 for (i
= 0; i
< f
->used
; i
++) {
469 for (j
= 0; j
< fe
->used
; j
++) {
470 scgi_extension_host
*h
;
477 buffer_free(fe
->key
);
488 static int scgi_extension_insert(scgi_exts
*ext
, buffer
*key
, scgi_extension_host
*fh
) {
492 /* there is something */
494 for (i
= 0; i
< ext
->used
; i
++) {
495 if (buffer_is_equal(key
, ext
->exts
[i
]->key
)) {
500 if (i
== ext
->used
) {
501 /* filextension is new */
502 fe
= calloc(1, sizeof(*fe
));
504 fe
->key
= buffer_init();
505 buffer_copy_buffer(fe
->key
, key
);
509 if (ext
->size
== 0) {
511 ext
->exts
= malloc(ext
->size
* sizeof(*(ext
->exts
)));
512 force_assert(ext
->exts
);
513 } else if (ext
->used
== ext
->size
) {
515 ext
->exts
= realloc(ext
->exts
, ext
->size
* sizeof(*(ext
->exts
)));
516 force_assert(ext
->exts
);
518 ext
->exts
[ext
->used
++] = fe
;
525 fe
->hosts
= malloc(fe
->size
* sizeof(*(fe
->hosts
)));
526 force_assert(fe
->hosts
);
527 } else if (fe
->size
== fe
->used
) {
529 fe
->hosts
= realloc(fe
->hosts
, fe
->size
* sizeof(*(fe
->hosts
)));
530 force_assert(fe
->hosts
);
533 fe
->hosts
[fe
->used
++] = fh
;
539 INIT_FUNC(mod_scgi_init
) {
542 p
= calloc(1, sizeof(*p
));
545 p
->scgi_env
= buffer_init();
547 p
->parse_response
= buffer_init();
553 FREE_FUNC(mod_scgi_free
) {
554 plugin_data
*p
= p_d
;
558 buffer_free(p
->scgi_env
);
559 buffer_free(p
->parse_response
);
561 if (p
->config_storage
) {
563 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
564 plugin_config
*s
= p
->config_storage
[i
];
567 if (NULL
== s
) continue;
571 for (j
= 0; j
< exts
->used
; j
++) {
576 for (n
= 0; n
< ex
->used
; n
++) {
578 scgi_extension_host
*host
;
582 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
583 if (proc
->pid
!= 0) kill(proc
->pid
, SIGTERM
);
585 if (proc
->is_local
&&
586 !buffer_string_is_empty(proc
->socket
)) {
587 unlink(proc
->socket
->ptr
);
591 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
592 if (proc
->pid
!= 0) kill(proc
->pid
, SIGTERM
);
594 if (proc
->is_local
&&
595 !buffer_string_is_empty(proc
->socket
)) {
596 unlink(proc
->socket
->ptr
);
602 scgi_extensions_free(s
->exts
);
606 free(p
->config_storage
);
611 return HANDLER_GO_ON
;
614 static int env_add(char_array
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
618 if (!key
|| !val
) return -1;
620 dst
= malloc(key_len
+ val_len
+ 3);
622 memcpy(dst
, key
, key_len
);
624 /* add the \0 from the value */
625 memcpy(dst
+ key_len
+ 1, val
, val_len
+ 1);
627 for (i
= 0; i
< env
->used
; i
++) {
628 if (0 == strncmp(dst
, env
->ptr
[i
], key_len
+ 1)) {
629 /* don't care about free as we are in a forked child which is going to exec(...) */
630 /* free(env->ptr[i]); */
636 if (env
->size
== 0) {
638 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
639 force_assert(env
->ptr
);
640 } else if (env
->size
== env
->used
) {
642 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
643 force_assert(env
->ptr
);
646 env
->ptr
[env
->used
++] = dst
;
651 #if !defined(HAVE_FORK)
652 static int scgi_spawn_connection(server
*srv
,
654 scgi_extension_host
*host
,
663 #else /* -> defined(HAVE_FORK) */
665 static int scgi_spawn_connection(server
*srv
,
667 scgi_extension_host
*host
,
671 struct timeval tv
= { 0, 100 * 1000 };
673 struct sockaddr_un scgi_addr_un
;
675 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
676 struct sockaddr_in6 scgi_addr_in6
;
678 struct sockaddr_in scgi_addr_in
;
679 struct sockaddr
*scgi_addr
;
684 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
685 "new proc, socket:", proc
->port
, proc
->socket
);
689 if (!buffer_string_is_empty(proc
->socket
)) {
691 memset(&scgi_addr_un
, 0, sizeof(scgi_addr_un
));
692 scgi_addr_un
.sun_family
= AF_UNIX
;
693 if (buffer_string_length(proc
->socket
) + 1 > sizeof(scgi_addr_un
.sun_path
)) {
694 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
695 "ERROR: Unix Domain socket filename too long:",
699 memcpy(scgi_addr_un
.sun_path
, proc
->socket
->ptr
, buffer_string_length(proc
->socket
) + 1);
702 servlen
= SUN_LEN(&scgi_addr_un
);
705 servlen
= buffer_string_length(proc
->socket
) + 1 + sizeof(scgi_addr_un
.sun_family
);
707 scgi_addr
= (struct sockaddr
*) &scgi_addr_un
;
709 log_error_write(srv
, __FILE__
, __LINE__
, "s",
710 "ERROR: Unix Domain sockets are not supported.");
713 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
714 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
715 memset(&scgi_addr_in6
, 0, sizeof(scgi_addr_in6
));
716 scgi_addr_in6
.sin6_family
= AF_INET6
;
717 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &scgi_addr_in6
.sin6_addr
);
718 scgi_addr_in6
.sin6_port
= htons(proc
->port
);
719 servlen
= sizeof(scgi_addr_in6
);
720 scgi_addr
= (struct sockaddr
*) &scgi_addr_in6
;
723 memset(&scgi_addr_in
, 0, sizeof(scgi_addr_in
));
724 scgi_addr_in
.sin_family
= AF_INET
;
726 if (buffer_string_is_empty(host
->host
)) {
727 scgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
731 /* set a usefull default */
732 scgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
735 if (NULL
== (he
= gethostbyname(host
->host
->ptr
))) {
736 log_error_write(srv
, __FILE__
, __LINE__
,
737 "sdb", "gethostbyname failed: ",
738 h_errno
, host
->host
);
742 if (he
->h_addrtype
!= AF_INET
) {
743 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-type != AF_INET: ", he
->h_addrtype
);
747 if (he
->h_length
!= sizeof(struct in_addr
)) {
748 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-length != sizeof(in_addr): ", he
->h_length
);
752 memcpy(&(scgi_addr_in
.sin_addr
.s_addr
), he
->h_addr_list
[0], he
->h_length
);
755 scgi_addr_in
.sin_port
= htons(proc
->port
);
756 servlen
= sizeof(scgi_addr_in
);
758 scgi_addr
= (struct sockaddr
*) &scgi_addr_in
;
761 if (-1 == (scgi_fd
= fdevent_socket_cloexec(scgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
762 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
763 "failed:", strerror(errno
));
767 if (-1 == connect(scgi_fd
, scgi_addr
, servlen
)) {
768 /* server is not up, spawn in */
772 if (!buffer_string_is_empty(proc
->socket
)) {
773 unlink(proc
->socket
->ptr
);
779 if (-1 == (scgi_fd
= fdevent_socket_cloexec(scgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
780 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
781 "socket failed:", strerror(errno
));
786 if (setsockopt(scgi_fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
)) < 0) {
787 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
788 "socketsockopt failed:", strerror(errno
));
794 if (-1 == bind(scgi_fd
, scgi_addr
, servlen
)) {
795 log_error_write(srv
, __FILE__
, __LINE__
, "sbds",
804 if (-1 == listen(scgi_fd
, host
->listen_backlog
)) {
805 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
806 "listen failed:", strerror(errno
));
811 switch ((child
= fork())) {
819 /* create environment */
830 (void)fcntl(scgi_fd
, F_SETFD
, 0); /* clear cloexec */
833 /* we don't need the client socket */
834 for (fd
= 3; fd
< 256; fd
++) {
838 /* build clean environment */
839 if (host
->bin_env_copy
->used
) {
840 for (i
= 0; i
< host
->bin_env_copy
->used
; i
++) {
841 data_string
*ds
= (data_string
*)host
->bin_env_copy
->data
[i
];
844 if (NULL
!= (ge
= getenv(ds
->value
->ptr
))) {
845 env_add(&env
, CONST_BUF_LEN(ds
->value
), ge
, strlen(ge
));
849 char ** const e
= environ
;
850 for (i
= 0; e
[i
]; ++i
) {
853 if (NULL
!= (eq
= strchr(e
[i
], '='))) {
854 env_add(&env
, e
[i
], eq
- e
[i
], eq
+1, strlen(eq
+1));
859 /* create environment */
860 for (i
= 0; i
< host
->bin_env
->used
; i
++) {
861 data_string
*ds
= (data_string
*)host
->bin_env
->data
[i
];
863 env_add(&env
, CONST_BUF_LEN(ds
->key
), CONST_BUF_LEN(ds
->value
));
866 for (i
= 0; i
< env
.used
; i
++) {
867 /* search for PHP_FCGI_CHILDREN */
868 if (0 == strncmp(env
.ptr
[i
], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
871 /* not found, add a default */
873 env_add(&env
, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
876 env
.ptr
[env
.used
] = NULL
;
879 buffer_copy_string_len(b
, CONST_STR_LEN("exec "));
880 buffer_append_string_buffer(b
, host
->bin_path
);
885 execle("/bin/sh", "sh", "-c", b
->ptr
, (char *)NULL
, env
.ptr
);
887 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
888 "execl failed for:", host
->bin_path
, strerror(errno
));
903 select(0, NULL
, NULL
, NULL
, &tv
);
905 switch (waitpid(child
, &status
, WNOHANG
)) {
907 /* child still running after timeout, good */
910 /* no PID found ? should never happen */
911 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
912 "pid not found:", strerror(errno
));
915 /* the child should not terminate at all */
916 if (WIFEXITED(status
)) {
917 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
918 "child exited (is this a SCGI binary ?):",
919 WEXITSTATUS(status
));
920 } else if (WIFSIGNALED(status
)) {
921 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
925 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
926 "child died somehow:",
932 /* register process */
934 proc
->last_used
= srv
->cur_ts
;
946 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
947 "(debug) socket is already used, won't spawn:",
952 proc
->state
= PROC_STATE_RUNNING
;
953 host
->active_procs
++;
958 #endif /* HAVE_FORK */
960 static scgi_extension_host
* unixsocket_is_dup(plugin_data
*p
, size_t used
, buffer
*unixsocket
) {
962 for (i
= 0; i
< used
; ++i
) {
963 scgi_exts
*exts
= p
->config_storage
[i
]->exts
;
964 for (j
= 0; j
< exts
->used
; ++j
) {
965 scgi_extension
*ex
= exts
->exts
[j
];
966 for (n
= 0; n
< ex
->used
; ++n
) {
967 scgi_extension_host
*host
= ex
->hosts
[n
];
968 if (!buffer_string_is_empty(host
->unixsocket
)
969 && buffer_is_equal(host
->unixsocket
, unixsocket
)
970 && !buffer_string_is_empty(host
->bin_path
))
979 SETDEFAULTS_FUNC(mod_scgi_set_defaults
) {
980 plugin_data
*p
= p_d
;
983 scgi_extension_host
*df
= NULL
;
985 config_values_t cv
[] = {
986 { "scgi.server", NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
987 { "scgi.debug", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
988 { "scgi.protocol", NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
989 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
992 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
993 force_assert(p
->config_storage
);
995 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
996 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
999 s
= malloc(sizeof(plugin_config
));
1001 s
->exts
= scgi_extensions_init();
1003 s
->proto
= LI_PROTOCOL_SCGI
;
1005 cv
[0].destination
= s
->exts
;
1006 cv
[1].destination
= &(s
->debug
);
1007 cv
[2].destination
= NULL
; /* T_CONFIG_LOCAL */
1009 p
->config_storage
[i
] = s
;
1011 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
1019 if (NULL
!= (du
= array_get_element(config
->value
, "scgi.protocol"))) {
1020 data_string
*ds
= (data_string
*)du
;
1021 if (du
->type
== TYPE_STRING
1022 && buffer_is_equal_string(ds
->value
, CONST_STR_LEN("scgi"))) {
1023 s
->proto
= LI_PROTOCOL_SCGI
;
1024 } else if (du
->type
== TYPE_STRING
1025 && buffer_is_equal_string(ds
->value
, CONST_STR_LEN("uwsgi"))) {
1026 s
->proto
= LI_PROTOCOL_UWSGI
;
1028 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
1029 "unexpected type for key: ", "scgi.protocol", "expected \"scgi\" or \"uwsgi\"");
1035 if (NULL
!= (du
= array_get_element(config
->value
, "scgi.server"))) {
1037 data_array
*da
= (data_array
*)du
;
1039 if (du
->type
!= TYPE_ARRAY
) {
1040 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
1041 "unexpected type for key: ", "scgi.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1048 * scgi.server = ( "<ext>" => ( ... ),
1049 * "<ext>" => ( ... ) )
1052 for (j
= 0; j
< da
->value
->used
; j
++) {
1054 data_array
*da_ext
= (data_array
*)da
->value
->data
[j
];
1056 if (da
->value
->data
[j
]->type
!= TYPE_ARRAY
) {
1057 log_error_write(srv
, __FILE__
, __LINE__
, "sssbs",
1058 "unexpected type for key: ", "scgi.server",
1059 "[", da
->value
->data
[j
]->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1065 * da_ext->key == name of the extension
1069 * scgi.server = ( "<ext>" =>
1070 * ( "<host>" => ( ... ),
1071 * "<host>" => ( ... )
1076 for (n
= 0; n
< da_ext
->value
->used
; n
++) {
1077 data_array
*da_host
= (data_array
*)da_ext
->value
->data
[n
];
1079 config_values_t fcv
[] = {
1080 { "host", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
1081 { "docroot", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
1082 { "socket", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
1083 { "bin-path", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
1085 { "check-local", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 4 */
1086 { "port", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 5 */
1087 { "min-procs-not-working", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 this is broken for now */
1088 { "max-procs", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 */
1089 { "max-load-per-proc", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 8 */
1090 { "idle-timeout", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 9 */
1091 { "disable-time", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 10 */
1093 { "bin-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 11 */
1094 { "bin-copy-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 12 */
1095 { "fix-root-scriptname", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 13 */
1096 { "listen-backlog", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_CONNECTION
}, /* 14 */
1097 { "x-sendfile", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 15 */
1098 { "x-sendfile-docroot",NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 16 */
1101 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
1104 if (da_host
->type
!= TYPE_ARRAY
) {
1105 log_error_write(srv
, __FILE__
, __LINE__
, "ssSBS",
1106 "unexpected type for key:",
1108 "[", da_host
->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1113 df
= scgi_host_init();
1115 df
->check_local
= 1;
1118 df
->max_load_per_proc
= 1;
1119 df
->idle_timeout
= 60;
1120 df
->disable_time
= 60;
1121 df
->fix_root_path_name
= 0;
1122 df
->listen_backlog
= 1024;
1123 df
->xsendfile_allow
= 0;
1126 fcv
[0].destination
= df
->host
;
1127 fcv
[1].destination
= df
->docroot
;
1128 fcv
[2].destination
= df
->unixsocket
;
1129 fcv
[3].destination
= df
->bin_path
;
1131 fcv
[4].destination
= &(df
->check_local
);
1132 fcv
[5].destination
= &(df
->port
);
1133 fcv
[6].destination
= &(df
->min_procs
);
1134 fcv
[7].destination
= &(df
->max_procs
);
1135 fcv
[8].destination
= &(df
->max_load_per_proc
);
1136 fcv
[9].destination
= &(df
->idle_timeout
);
1137 fcv
[10].destination
= &(df
->disable_time
);
1139 fcv
[11].destination
= df
->bin_env
;
1140 fcv
[12].destination
= df
->bin_env_copy
;
1141 fcv
[13].destination
= &(df
->fix_root_path_name
);
1142 fcv
[14].destination
= &(df
->listen_backlog
);
1143 fcv
[15].destination
= &(df
->xsendfile_allow
);
1144 fcv
[16].destination
= df
->xsendfile_docroot
;
1147 if (0 != config_insert_values_internal(srv
, da_host
->value
, fcv
, T_CONFIG_SCOPE_CONNECTION
)) {
1151 if ((!buffer_string_is_empty(df
->host
) || df
->port
) &&
1152 !buffer_string_is_empty(df
->unixsocket
)) {
1153 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1154 "either host+port or socket");
1159 if (!buffer_string_is_empty(df
->unixsocket
)) {
1160 /* unix domain socket */
1161 struct sockaddr_un un
;
1163 if (buffer_string_length(df
->unixsocket
) + 1 > sizeof(un
.sun_path
) - 2) {
1164 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1165 "path of the unixdomain socket is too large");
1169 if (!buffer_string_is_empty(df
->bin_path
)) {
1170 scgi_extension_host
*duplicate
= unixsocket_is_dup(p
, i
+1, df
->unixsocket
);
1171 if (NULL
!= duplicate
) {
1172 if (!buffer_is_equal(df
->bin_path
, duplicate
->bin_path
)) {
1173 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1174 "duplicate unixsocket path:",
1184 df
->family
= AF_UNIX
;
1188 if (buffer_string_is_empty(df
->host
) &&
1189 buffer_string_is_empty(df
->bin_path
)) {
1190 log_error_write(srv
, __FILE__
, __LINE__
, "sbbbs",
1191 "missing key (string):",
1198 } else if (df
->port
== 0) {
1199 log_error_write(srv
, __FILE__
, __LINE__
, "sbbbs",
1200 "missing key (short):",
1208 df
->family
= (!buffer_string_is_empty(df
->host
) && NULL
!= strchr(df
->host
->ptr
, ':')) ? AF_INET6
: AF_INET
;
1212 /* already init'd; skip spawning */
1213 } else if (!buffer_string_is_empty(df
->bin_path
)) {
1214 /* a local socket + self spawning */
1218 size_t nchars
= strcspn(df
->bin_path
->ptr
, " \t");
1219 char c
= df
->bin_path
->ptr
[nchars
];
1220 df
->bin_path
->ptr
[nchars
] = '\0';
1221 if (0 == nchars
|| 0 != stat(df
->bin_path
->ptr
, &st
) || !S_ISREG(st
.st_mode
) || !(st
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))) {
1222 df
->bin_path
->ptr
[nchars
] = c
;
1223 log_error_write(srv
, __FILE__
, __LINE__
, "SSs",
1224 "invalid \"bin-path\" => \"", df
->bin_path
->ptr
,
1225 "\" (check that file exists, is regular file, and is executable by lighttpd)");
1227 df
->bin_path
->ptr
[nchars
] = c
;
1229 /* HACK: just to make sure the adaptive spawing is disabled */
1230 df
->min_procs
= df
->max_procs
;
1232 if (df
->min_procs
> df
->max_procs
) df
->max_procs
= df
->min_procs
;
1233 if (df
->max_load_per_proc
< 1) df
->max_load_per_proc
= 0;
1236 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsbsdsd",
1237 "--- scgi spawning local",
1238 "\n\tproc:", df
->bin_path
,
1239 "\n\tport:", df
->port
,
1240 "\n\tsocket", df
->unixsocket
,
1241 "\n\tmin-procs:", df
->min_procs
,
1242 "\n\tmax-procs:", df
->max_procs
);
1245 for (pno
= 0; pno
< df
->min_procs
; pno
++) {
1248 proc
= scgi_process_init();
1249 proc
->id
= df
->num_procs
++;
1252 if (buffer_string_is_empty(df
->unixsocket
)) {
1253 proc
->port
= df
->port
+ pno
;
1255 buffer_copy_buffer(proc
->socket
, df
->unixsocket
);
1256 buffer_append_string_len(proc
->socket
, CONST_STR_LEN("-"));
1257 buffer_append_int(proc
->socket
, pno
);
1261 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsdsd",
1262 "--- scgi spawning",
1263 "\n\tport:", df
->port
,
1264 "\n\tsocket", df
->unixsocket
,
1265 "\n\tcurrent:", pno
, "/", df
->min_procs
);
1268 if (!srv
->srvconf
.preflight_check
1269 && scgi_spawn_connection(srv
, p
, df
, proc
)) {
1270 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1271 "[ERROR]: spawning fcgi failed.");
1272 scgi_process_free(proc
);
1276 proc
->next
= df
->first
;
1277 if (df
->first
) df
->first
->prev
= proc
;
1284 fp
= scgi_process_init();
1285 fp
->id
= df
->num_procs
++;
1288 fp
->state
= PROC_STATE_RUNNING
;
1290 if (buffer_string_is_empty(df
->unixsocket
)) {
1291 fp
->port
= df
->port
;
1293 buffer_copy_buffer(fp
->socket
, df
->unixsocket
);
1302 if (df
->xsendfile_docroot
->used
) {
1304 for (k
= 0; k
< df
->xsendfile_docroot
->used
; ++k
) {
1305 data_string
*ds
= (data_string
*)df
->xsendfile_docroot
->data
[k
];
1306 if (ds
->type
!= TYPE_STRING
) {
1307 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1308 "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
1311 if (ds
->value
->ptr
[0] != '/') {
1312 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
1313 "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds
->value
, "\"");
1316 buffer_path_simplify(ds
->value
, ds
->value
);
1317 buffer_append_slash(ds
->value
);
1321 /* if extension already exists, take it */
1322 scgi_extension_insert(s
->exts
, da_ext
->key
, df
);
1329 return HANDLER_GO_ON
;
1332 if (NULL
!= df
) scgi_host_free(df
);
1333 return HANDLER_ERROR
;
1336 static int scgi_set_state(server
*srv
, handler_ctx
*hctx
, scgi_connection_state_t state
) {
1337 hctx
->state
= state
;
1338 hctx
->state_timestamp
= srv
->cur_ts
;
1344 static void scgi_backend_close(server
*srv
, handler_ctx
*hctx
) {
1345 if (hctx
->fd
!= -1) {
1346 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
1347 fdevent_unregister(srv
->ev
, hctx
->fd
);
1348 fdevent_sched_close(srv
->ev
, hctx
->fd
, 1);
1355 /* after the connect the process gets a load */
1356 if (hctx
->got_proc
) hctx
->proc
->load
--;
1357 scgi_proclist_sort_down(srv
, hctx
->host
, hctx
->proc
);
1359 if (hctx
->conf
.debug
) {
1360 log_error_write(srv
, __FILE__
, __LINE__
, "sddb",
1363 hctx
->proc
->pid
, hctx
->proc
->socket
);
1372 static scgi_extension_host
* scgi_extension_host_get(server
*srv
, connection
*con
, plugin_data
*p
, scgi_extension
*extension
) {
1374 scgi_extension_host
*host
= NULL
;
1377 /* get best server */
1378 for (size_t k
= 0; k
< extension
->used
; ++k
) {
1379 scgi_extension_host
*h
= extension
->hosts
[k
];
1381 /* we should have at least one proc that can do something */
1382 if (h
->active_procs
== 0) {
1386 if (used
== -1 || h
->load
< used
) {
1394 /* sorry, we don't have a server alive for this ext */
1395 con
->http_status
= 503; /* Service Unavailable */
1398 /* only send the 'no handler' once */
1399 if (!extension
->note_is_sent
) {
1400 extension
->note_is_sent
= 1;
1402 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbs",
1403 "all handlers for ", con
->uri
.path
,
1404 "on", extension
->key
,
1412 static void scgi_connection_close(server
*srv
, handler_ctx
*hctx
) {
1416 p
= hctx
->plugin_data
;
1417 con
= hctx
->remote_conn
;
1419 scgi_backend_close(srv
, hctx
);
1420 handler_ctx_free(hctx
);
1421 con
->plugin_ctx
[p
->id
] = NULL
;
1423 /* finish response (if not already con->file_started, con->file_finished) */
1424 if (con
->mode
== p
->id
) {
1425 http_response_backend_done(srv
, con
);
1429 static handler_t
scgi_reconnect(server
*srv
, handler_ctx
*hctx
) {
1430 scgi_backend_close(srv
, hctx
);
1432 hctx
->host
= scgi_extension_host_get(srv
, hctx
->remote_conn
, hctx
->plugin_data
, hctx
->ext
);
1433 if (NULL
== hctx
->host
) return HANDLER_FINISHED
;
1436 scgi_set_state(srv
, hctx
, FCGI_STATE_INIT
);
1437 return HANDLER_COMEBACK
;
1441 static handler_t
scgi_connection_reset(server
*srv
, connection
*con
, void *p_d
) {
1442 plugin_data
*p
= p_d
;
1443 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1444 if (hctx
) scgi_connection_close(srv
, hctx
);
1446 return HANDLER_GO_ON
;
1450 static int scgi_env_add_scgi(void *venv
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
1454 if (!key
|| !val
) return -1;
1456 len
= key_len
+ val_len
+ 2;
1458 buffer_string_prepare_append(env
, len
);
1460 buffer_append_string_len(env
, key
, key_len
);
1461 buffer_append_string_len(env
, "", 1);
1462 buffer_append_string_len(env
, val
, val_len
);
1463 buffer_append_string_len(env
, "", 1);
1469 #ifdef __LITTLE_ENDIAN__
1470 #define uwsgi_htole16(x) (x)
1471 #else /* __BIG_ENDIAN__ */
1472 #define uwsgi_htole16(x) ((uint16_t) (((x) & 0xff) << 8 | ((x) & 0xff00) >> 8))
1476 static int scgi_env_add_uwsgi(void *venv
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
1481 if (!key
|| !val
) return -1;
1482 if (key_len
> USHRT_MAX
|| val_len
> USHRT_MAX
) return -1;
1484 len
= 2 + key_len
+ 2 + val_len
;
1486 buffer_string_prepare_append(env
, len
);
1488 uwlen
= uwsgi_htole16((uint16_t)key_len
);
1489 buffer_append_string_len(env
, (char *)&uwlen
, 2);
1490 buffer_append_string_len(env
, key
, key_len
);
1491 uwlen
= uwsgi_htole16((uint16_t)val_len
);
1492 buffer_append_string_len(env
, (char *)&uwlen
, 2);
1493 buffer_append_string_len(env
, val
, val_len
);
1504 * 1 not connected yet
1507 static int scgi_establish_connection(server
*srv
, handler_ctx
*hctx
) {
1508 struct sockaddr
*scgi_addr
;
1509 struct sockaddr_in scgi_addr_in
;
1510 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1511 struct sockaddr_in6 scgi_addr_in6
;
1513 #ifdef HAVE_SYS_UN_H
1514 struct sockaddr_un scgi_addr_un
;
1518 scgi_extension_host
*host
= hctx
->host
;
1519 scgi_proc
*proc
= hctx
->proc
;
1520 int scgi_fd
= hctx
->fd
;
1522 if (!buffer_string_is_empty(proc
->socket
)) {
1523 #ifdef HAVE_SYS_UN_H
1524 /* use the unix domain socket */
1525 memset(&scgi_addr_un
, 0, sizeof(scgi_addr_un
));
1526 scgi_addr_un
.sun_family
= AF_UNIX
;
1527 if (buffer_string_length(proc
->socket
) + 1 > sizeof(scgi_addr_un
.sun_path
)) {
1528 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
1529 "ERROR: Unix Domain socket filename too long:",
1533 memcpy(scgi_addr_un
.sun_path
, proc
->socket
->ptr
, buffer_string_length(proc
->socket
) + 1);
1536 servlen
= SUN_LEN(&scgi_addr_un
);
1539 servlen
= buffer_string_length(proc
->socket
) + 1 + sizeof(scgi_addr_un
.sun_family
);
1541 scgi_addr
= (struct sockaddr
*) &scgi_addr_un
;
1545 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1546 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
1547 memset(&scgi_addr_in6
, 0, sizeof(scgi_addr_in6
));
1548 scgi_addr_in6
.sin6_family
= AF_INET6
;
1549 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &scgi_addr_in6
.sin6_addr
);
1550 scgi_addr_in6
.sin6_port
= htons(proc
->port
);
1551 servlen
= sizeof(scgi_addr_in6
);
1552 scgi_addr
= (struct sockaddr
*) &scgi_addr_in6
;
1555 memset(&scgi_addr_in
, 0, sizeof(scgi_addr_in
));
1556 scgi_addr_in
.sin_family
= AF_INET
;
1557 if (0 == inet_aton(host
->host
->ptr
, &(scgi_addr_in
.sin_addr
))) {
1558 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1559 "converting IP-adress failed for", host
->host
,
1560 "\nBe sure to specify an IP address here");
1564 scgi_addr_in
.sin_port
= htons(proc
->port
);
1565 servlen
= sizeof(scgi_addr_in
);
1567 scgi_addr
= (struct sockaddr
*) &scgi_addr_in
;
1570 if (-1 == connect(scgi_fd
, scgi_addr
, servlen
)) {
1571 if (errno
== EINPROGRESS
||
1572 errno
== EALREADY
||
1574 if (hctx
->conf
.debug
) {
1575 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1576 "connect delayed, will continue later:", scgi_fd
);
1581 log_error_write(srv
, __FILE__
, __LINE__
, "sdsddb",
1582 "connect failed:", scgi_fd
,
1583 strerror(errno
), errno
,
1584 proc
->port
, proc
->socket
);
1586 if (errno
== EAGAIN
) {
1587 /* this is Linux only */
1589 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1590 "If this happend on Linux: You have been run out of local ports. "
1591 "Check the manual, section Performance how to handle this.");
1597 if (hctx
->conf
.debug
> 1) {
1598 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1599 "connect succeeded: ", scgi_fd
);
1608 static int scgi_create_env(server
*srv
, handler_ctx
*hctx
) {
1611 plugin_data
*p
= hctx
->plugin_data
;
1612 scgi_extension_host
*host
= hctx
->host
;
1614 connection
*con
= hctx
->remote_conn
;
1616 http_cgi_opts opts
= { 0, 0, host
->docroot
, NULL
};
1618 http_cgi_header_append_cb scgi_env_add
= hctx
->conf
.proto
== LI_PROTOCOL_SCGI
1620 : scgi_env_add_uwsgi
;
1622 buffer_string_prepare_copy(p
->scgi_env
, 1023);
1624 if (0 != http_cgi_headers(srv
, con
, &opts
, scgi_env_add
, p
->scgi_env
)) {
1625 con
->http_status
= 400;
1629 if (hctx
->conf
.proto
== LI_PROTOCOL_SCGI
) {
1630 scgi_env_add(p
->scgi_env
, CONST_STR_LEN("SCGI"), CONST_STR_LEN("1"));
1632 buffer_append_int(b
, buffer_string_length(p
->scgi_env
));
1633 buffer_append_string_len(b
, CONST_STR_LEN(":"));
1634 buffer_append_string_buffer(b
, p
->scgi_env
);
1635 buffer_append_string_len(b
, CONST_STR_LEN(","));
1636 } else { /* LI_PROTOCOL_UWSGI */
1637 /* http://uwsgi-docs.readthedocs.io/en/latest/Protocol.html */
1638 size_t len
= buffer_string_length(p
->scgi_env
);
1639 uint32_t uwsgi_header
;
1640 if (len
> USHRT_MAX
) {
1641 con
->http_status
= 431; /* Request Header Fields Too Large */
1643 return -1; /* trigger return of HANDLER_FINISHED */
1646 buffer_string_prepare_copy(b
, 4 + len
);
1647 uwsgi_header
= ((uint32_t)uwsgi_htole16((uint16_t)len
)) << 8;
1648 memcpy(b
->ptr
, (char *)&uwsgi_header
, 4);
1649 buffer_commit(b
, 4);
1650 buffer_append_string_buffer(b
, p
->scgi_env
);
1653 hctx
->wb_reqlen
= buffer_string_length(b
);
1654 chunkqueue_append_buffer(hctx
->wb
, b
);
1657 if (con
->request
.content_length
) {
1658 chunkqueue_append_chunkqueue(hctx
->wb
, con
->request_content_queue
);
1659 hctx
->wb_reqlen
+= con
->request
.content_length
;/* (eventual) total request size */
1665 static int scgi_response_parse(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*in
, int eol
) {
1672 buffer_copy_buffer(p
->parse_response
, in
);
1674 for (s
= p
->parse_response
->ptr
;
1675 NULL
!= (ns
= (eol
== EOL_RN
? strstr(s
, "\r\n") : strchr(s
, '\n')));
1676 s
= ns
+ (eol
== EOL_RN
? 2 : 1), line
++) {
1677 const char *key
, *value
;
1684 0 == strncmp(s
, "HTTP/1.", 7)) {
1685 /* non-parsed header ... we parse them anyway */
1691 /* after the space should be a status code for us */
1693 status
= strtol(s
+9, NULL
, 10);
1695 if (status
>= 100 && status
< 1000) {
1696 /* we expected 3 digits got them */
1697 con
->parsed_response
|= HTTP_STATUS
;
1698 con
->http_status
= status
;
1704 if (NULL
== (value
= strchr(s
, ':'))) {
1705 /* we expect: "<key>: <value>\r\n" */
1709 key_len
= value
- key
;
1713 while (*value
== ' ' || *value
== '\t') value
++;
1715 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
1716 ds
= data_response_init();
1718 buffer_copy_string_len(ds
->key
, key
, key_len
);
1719 buffer_copy_string(ds
->value
, value
);
1721 array_insert_unique(con
->response
.headers
, (data_unset
*)ds
);
1725 if (0 == strncasecmp(key
, "Date", key_len
)) {
1726 con
->parsed_response
|= HTTP_DATE
;
1730 if (0 == strncasecmp(key
, "Status", key_len
)) {
1731 int status
= strtol(value
, NULL
, 10);
1732 if (status
>= 100 && status
< 1000) {
1733 con
->http_status
= status
;
1734 con
->parsed_response
|= HTTP_STATUS
;
1736 con
->http_status
= 502;
1741 if (0 == strncasecmp(key
, "Location", key_len
)) {
1742 con
->parsed_response
|= HTTP_LOCATION
;
1746 if (0 == strncasecmp(key
, "Connection", key_len
)) {
1747 con
->response
.keep_alive
= (0 == strcasecmp(value
, "Keep-Alive")) ? 1 : 0;
1748 con
->parsed_response
|= HTTP_CONNECTION
;
1752 if (0 == strncasecmp(key
, "Content-Length", key_len
)) {
1753 con
->response
.content_length
= strtoul(value
, NULL
, 10);
1754 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
1763 /* CGI/1.1 rev 03 - 7.2.1.2 */
1764 if ((con
->parsed_response
& HTTP_LOCATION
) &&
1765 !(con
->parsed_response
& HTTP_STATUS
)) {
1766 con
->http_status
= 302;
1773 static int scgi_demux_response(server
*srv
, handler_ctx
*hctx
) {
1774 plugin_data
*p
= hctx
->plugin_data
;
1775 connection
*con
= hctx
->remote_conn
;
1780 buffer_string_prepare_copy(hctx
->response
, 1023);
1781 if (-1 == (n
= read(hctx
->fd
, hctx
->response
->ptr
, hctx
->response
->size
- 1))) {
1782 if (errno
== EAGAIN
|| errno
== EINTR
) {
1783 /* would block, wait for signal */
1784 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1788 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", strerror(errno
), con
->fd
, hctx
->fd
);
1797 buffer_commit(hctx
->response
, n
);
1799 /* split header from body */
1801 if (con
->file_started
== 0) {
1805 int cp
, eol
= EOL_UNSET
;
1809 buffer_append_string_buffer(hctx
->response_header
, hctx
->response
);
1811 /* nph (non-parsed headers) */
1812 if (0 == strncmp(hctx
->response_header
->ptr
, "HTTP/1.", 7)) in_header
= 1;
1814 /* search for the \r\n\r\n or \n\n in the string */
1815 for (c
= hctx
->response_header
->ptr
, cp
= 0, used
= buffer_string_length(hctx
->response_header
); used
; c
++, cp
++, used
--) {
1816 if (*c
== ':') in_header
= 1;
1817 else if (*c
== '\n') {
1818 if (in_header
== 0) {
1819 /* got a response without a response header */
1826 if (eol
== EOL_UNSET
) eol
= EOL_N
;
1828 if (*(c
+1) == '\n') {
1834 } else if (used
> 1 && *c
== '\r' && *(c
+1) == '\n') {
1835 if (in_header
== 0) {
1836 /* got a response without a response header */
1843 if (eol
== EOL_UNSET
) eol
= EOL_RN
;
1862 /* no header, but a body */
1863 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response_header
)) {
1864 /* error writing to tempfile;
1865 * truncate response or send 500 if nothing sent yet */
1869 size_t blen
= buffer_string_length(hctx
->response_header
) - hlen
;
1871 /* a small hack: terminate after at the second \r */
1872 buffer_string_set_length(hctx
->response_header
, hlen
- 1);
1874 /* parse the response header */
1875 scgi_response_parse(srv
, con
, p
, hctx
->response_header
, eol
);
1877 if (hctx
->host
->xsendfile_allow
) {
1879 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-Sendfile"))) {
1880 http_response_xsendfile(srv
, con
, ds
->value
, hctx
->host
->xsendfile_docroot
);
1886 if (0 != http_chunk_append_mem(srv
, con
, hctx
->response_header
->ptr
+ hlen
, blen
)) {
1887 /* error writing to tempfile;
1888 * truncate response or send 500 if nothing sent yet */
1894 con
->file_started
= 1;
1896 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
1897 if (buffer_string_length(hctx
->response_header
) > MAX_HTTP_REQUEST_HEADER
) {
1898 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "response headers too large for", con
->uri
.path
);
1899 con
->http_status
= 502; /* Bad Gateway */
1905 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response
)) {
1906 /* error writing to tempfile;
1907 * truncate response or send 500 if nothing sent yet */
1910 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
1911 && chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
1912 if (!con
->is_writable
) {
1913 /*(defer removal of FDEVENT_IN interest since
1914 * connection_state_machine() might be able to send data
1915 * immediately, unless !con->is_writable, where
1916 * connection_state_machine() might not loop back to call
1917 * mod_scgi_handle_subrequest())*/
1918 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1925 log_error_write(srv
, __FILE__
, __LINE__
, "ddss", con
->fd
, hctx
->fd
, connection_get_state(con
->state
), b
->ptr
);
1933 static int scgi_proclist_sort_up(server
*srv
, scgi_extension_host
*host
, scgi_proc
*proc
) {
1938 /* we have been the smallest of the current list
1939 * and we want to insert the node sorted as soon
1949 /* nothing to sort, only one element */
1950 if (host
->first
== proc
&& proc
->next
== NULL
) return 0;
1952 for (p
= proc
; p
->next
&& p
->next
->load
< proc
->load
; p
= p
->next
);
1954 /* no need to move something
1962 if (p
== proc
) return 0;
1964 if (host
->first
== proc
) {
1965 /* we have been the first elememt */
1967 host
->first
= proc
->next
;
1968 host
->first
->prev
= NULL
;
1971 /* disconnect proc */
1973 if (proc
->prev
) proc
->prev
->next
= proc
->next
;
1974 if (proc
->next
) proc
->next
->prev
= proc
->prev
;
1976 /* proc should be right of p */
1978 proc
->next
= p
->next
;
1980 if (p
->next
) p
->next
->prev
= proc
;
1983 for(p
= host
->first
; p
; p
= p
->next
) {
1984 log_error_write(srv
, __FILE__
, __LINE__
, "dd",
1994 int scgi_proclist_sort_down(server
*srv
, scgi_extension_host
*host
, scgi_proc
*proc
) {
1999 /* we have been the smallest of the current list
2000 * and we want to insert the node sorted as soon
2009 * the basic is idea is:
2010 * - the last active scgi process should be still
2011 * in ram and is not swapped out yet
2012 * - processes that are not reused will be killed
2013 * after some time by the trigger-handler
2015 * everything > 0 is hot
2016 * all unused procs are colder the more right they are
2017 * ice-cold processes are propably unused since more
2018 * than 'unused-timeout', are swaped out and won't be
2019 * reused in the next seconds anyway.
2023 /* nothing to sort, only one element */
2024 if (host
->first
== proc
&& proc
->next
== NULL
) return 0;
2026 for (p
= host
->first
; p
!= proc
&& p
->load
< proc
->load
; p
= p
->next
);
2029 /* no need to move something
2037 if (p
== proc
) return 0;
2039 /* we have to move left. If we are already the first element
2041 if (host
->first
== proc
) return 0;
2044 if (proc
->prev
) proc
->prev
->next
= proc
->next
;
2045 if (proc
->next
) proc
->next
->prev
= proc
->prev
;
2047 /* proc should be left of p */
2049 proc
->prev
= p
->prev
;
2050 if (p
->prev
) p
->prev
->next
= proc
;
2053 if (proc
->prev
== NULL
) host
->first
= proc
;
2055 for(p
= host
->first
; p
; p
= p
->next
) {
2056 log_error_write(srv
, __FILE__
, __LINE__
, "dd",
2066 static int scgi_restart_dead_procs(server
*srv
, plugin_data
*p
, scgi_extension_host
*host
) {
2069 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
2070 if (p
->conf
.debug
) {
2071 log_error_write(srv
, __FILE__
, __LINE__
, "sbdbdddd",
2073 host
->host
, proc
->port
,
2081 if (0 == proc
->is_local
) {
2083 * external servers might get disabled
2085 * enable the server again, perhaps it is back again
2088 if ((proc
->state
== PROC_STATE_DISABLED
) &&
2089 (srv
->cur_ts
- proc
->disable_ts
> host
->disable_time
)) {
2090 proc
->state
= PROC_STATE_RUNNING
;
2091 host
->active_procs
++;
2093 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb",
2094 "fcgi-server re-enabled:",
2095 host
->host
, host
->port
,
2099 /* the child should not terminate at all */
2102 if (proc
->state
== PROC_STATE_DIED_WAIT_FOR_PID
) {
2103 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
2105 /* child is still alive */
2110 if (WIFEXITED(status
)) {
2112 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
2113 "child exited, pid:", proc
->pid
,
2114 "status:", WEXITSTATUS(status
));
2116 } else if (WIFSIGNALED(status
)) {
2117 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2121 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2122 "child died somehow:",
2126 proc
->state
= PROC_STATE_DIED
;
2132 * local servers might died, but we restart them
2135 if (proc
->state
== PROC_STATE_DIED
&&
2137 /* restart the child */
2139 if (p
->conf
.debug
) {
2140 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsdsd",
2141 "--- scgi spawning",
2142 "\n\tport:", host
->port
,
2143 "\n\tsocket", host
->unixsocket
,
2144 "\n\tcurrent:", 1, "/", host
->min_procs
);
2147 if (scgi_spawn_connection(srv
, p
, host
, proc
)) {
2148 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2149 "ERROR: spawning fcgi failed.");
2150 return HANDLER_ERROR
;
2153 scgi_proclist_sort_down(srv
, host
, proc
);
2162 static handler_t
scgi_write_request(server
*srv
, handler_ctx
*hctx
) {
2163 scgi_extension_host
*host
= hctx
->host
;
2164 connection
*con
= hctx
->remote_conn
;
2168 switch(hctx
->state
) {
2169 case FCGI_STATE_INIT
:
2170 if (-1 == (hctx
->fd
= fdevent_socket_nb_cloexec(host
->family
, SOCK_STREAM
, 0))) {
2171 if (errno
== EMFILE
||
2173 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2174 "wait for fd at connection:", con
->fd
);
2176 return HANDLER_WAIT_FOR_FD
;
2179 log_error_write(srv
, __FILE__
, __LINE__
, "ssdd",
2180 "socket failed:", strerror(errno
), srv
->cur_fds
, srv
->max_fds
);
2181 return HANDLER_ERROR
;
2187 fdevent_register(srv
->ev
, hctx
->fd
, scgi_handle_fdevent
, hctx
);
2189 if (-1 == fdevent_fcntl_set(srv
->ev
, hctx
->fd
)) {
2190 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2191 "fcntl failed: ", strerror(errno
));
2192 return HANDLER_ERROR
;
2196 case FCGI_STATE_CONNECT
:
2197 if (hctx
->state
== FCGI_STATE_INIT
) {
2198 for (hctx
->proc
= hctx
->host
->first
;
2199 hctx
->proc
&& hctx
->proc
->state
!= PROC_STATE_RUNNING
;
2200 hctx
->proc
= hctx
->proc
->next
);
2202 /* all childs are dead */
2203 if (hctx
->proc
== NULL
) {
2206 return HANDLER_ERROR
;
2209 if (hctx
->proc
->is_local
) {
2210 hctx
->pid
= hctx
->proc
->pid
;
2213 switch (scgi_establish_connection(srv
, hctx
)) {
2215 scgi_set_state(srv
, hctx
, FCGI_STATE_CONNECT
);
2217 /* connection is in progress, wait for an event and call getsockopt() below */
2219 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2221 return HANDLER_WAIT_FOR_EVENT
;
2223 /* if ECONNREFUSED; choose another connection */
2226 return HANDLER_ERROR
;
2228 /* everything is ok, go on */
2235 socklen_t socket_error_len
= sizeof(socket_error
);
2237 /* try to finish the connect() */
2238 if (0 != getsockopt(hctx
->fd
, SOL_SOCKET
, SO_ERROR
, &socket_error
, &socket_error_len
)) {
2239 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2240 "getsockopt failed:", strerror(errno
));
2242 return HANDLER_ERROR
;
2244 if (socket_error
!= 0) {
2245 if (!hctx
->proc
->is_local
|| hctx
->conf
.debug
) {
2246 /* local procs get restarted */
2248 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2249 "establishing connection failed:", strerror(socket_error
),
2250 "port:", hctx
->proc
->port
);
2253 return HANDLER_ERROR
;
2257 /* ok, we have the connection */
2260 hctx
->proc
->last_used
= srv
->cur_ts
;
2263 if (hctx
->conf
.debug
) {
2264 log_error_write(srv
, __FILE__
, __LINE__
, "sddbdd",
2273 /* move the proc-list entry down the list */
2274 scgi_proclist_sort_up(srv
, hctx
->host
, hctx
->proc
);
2276 scgi_set_state(srv
, hctx
, FCGI_STATE_PREPARE_WRITE
);
2278 case FCGI_STATE_PREPARE_WRITE
:
2279 if (0 != scgi_create_env(srv
, hctx
)) {
2280 return HANDLER_FINISHED
;
2283 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2284 scgi_set_state(srv
, hctx
, FCGI_STATE_WRITE
);
2287 case FCGI_STATE_WRITE
:
2288 ret
= srv
->network_backend_write(srv
, con
, hctx
->fd
, hctx
->wb
, MAX_WRITE_LIMIT
);
2290 chunkqueue_remove_finished_chunks(hctx
->wb
);
2293 if (errno
== ENOTCONN
|| ret
== -2) {
2294 /* the connection got dropped after accept()
2296 * this is most of the time a PHP which dies
2297 * after PHP_FCGI_MAX_REQUESTS
2300 if (hctx
->wb
->bytes_out
== 0 &&
2301 hctx
->reconnects
++ < 5) {
2302 usleep(10000); /* take away the load of the webserver
2303 * to let the php a chance to restart
2306 return scgi_reconnect(srv
, hctx
);
2309 /* not reconnected ... why
2311 * far@#lighttpd report this for FreeBSD
2315 log_error_write(srv
, __FILE__
, __LINE__
, "ssosd",
2316 "connection was dropped after accept(). reconnect() denied:",
2317 "write-offset:", hctx
->wb
->bytes_out
,
2318 "reconnect attempts:", hctx
->reconnects
);
2320 return HANDLER_ERROR
;
2322 /* -1 == ret => error on our side */
2323 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
2324 "write failed:", strerror(errno
), errno
);
2326 return HANDLER_ERROR
;
2330 if (hctx
->wb
->bytes_out
== hctx
->wb_reqlen
) {
2331 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2332 scgi_set_state(srv
, hctx
, FCGI_STATE_READ
);
2334 off_t wblen
= hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
;
2335 if (hctx
->wb
->bytes_in
< hctx
->wb_reqlen
&& wblen
< 65536 - 16384) {
2336 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
2337 if (!(con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
)) {
2338 con
->conf
.stream_request_body
|= FDEVENT_STREAM_REQUEST_POLLIN
;
2339 con
->is_readable
= 1; /* trigger optimistic read from client */
2343 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2345 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2349 return HANDLER_WAIT_FOR_EVENT
;
2350 case FCGI_STATE_READ
:
2351 /* waiting for a response */
2352 return HANDLER_WAIT_FOR_EVENT
;
2354 log_error_write(srv
, __FILE__
, __LINE__
, "s", "(debug) unknown state");
2355 return HANDLER_ERROR
;
2359 static handler_t
scgi_send_request(server
*srv
, handler_ctx
*hctx
) {
2360 /* ok, create the request */
2361 handler_t rc
= scgi_write_request(srv
, hctx
);
2362 if (HANDLER_ERROR
!= rc
) {
2365 scgi_proc
*proc
= hctx
->proc
;
2366 scgi_extension_host
*host
= hctx
->host
;
2367 plugin_data
*p
= hctx
->plugin_data
;
2368 connection
*con
= hctx
->remote_conn
;
2371 0 == proc
->is_local
&&
2372 proc
->state
!= PROC_STATE_DISABLED
) {
2373 /* only disable remote servers as we don't manage them*/
2375 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb", "fcgi-server disabled:",
2380 /* disable this server */
2381 proc
->disable_ts
= srv
->cur_ts
;
2382 proc
->state
= PROC_STATE_DISABLED
;
2383 host
->active_procs
--;
2386 if (hctx
->state
== FCGI_STATE_INIT
||
2387 hctx
->state
== FCGI_STATE_CONNECT
) {
2388 /* connect() or getsockopt() failed,
2389 * restart the request-handling
2391 if (proc
&& proc
->is_local
) {
2393 if (hctx
->conf
.debug
) {
2394 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb", "connect() to scgi failed, restarting the request-handling:",
2401 * several hctx might reference the same proc
2403 * Only one of them should mark the proc as dead all the other
2404 * ones should just take a new one.
2406 * If a new proc was started with the old struct this might lead
2407 * the mark a perfect proc as dead otherwise
2410 if (proc
->state
== PROC_STATE_RUNNING
&&
2411 hctx
->pid
== proc
->pid
) {
2412 proc
->state
= PROC_STATE_DIED_WAIT_FOR_PID
;
2415 scgi_restart_dead_procs(srv
, p
, host
);
2417 return scgi_reconnect(srv
, hctx
);
2419 scgi_connection_close(srv
, hctx
);
2420 con
->http_status
= 503;
2422 return HANDLER_FINISHED
;
2428 static handler_t
scgi_recv_response(server
*srv
, handler_ctx
*hctx
);
2431 SUBREQUEST_FUNC(mod_scgi_handle_subrequest
) {
2432 plugin_data
*p
= p_d
;
2434 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
2436 if (NULL
== hctx
) return HANDLER_GO_ON
;
2439 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
2441 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
2442 && con
->file_started
) {
2443 if (chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
2444 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2445 } else if (!(fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_IN
)) {
2446 /* optimistic read from backend, which might re-enable FDEVENT_IN */
2447 handler_t rc
= scgi_recv_response(srv
, hctx
); /*(might invalidate hctx)*/
2448 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
2452 if (0 == hctx
->wb
->bytes_in
2453 ? con
->state
== CON_STATE_READ_POST
2454 : hctx
->wb
->bytes_in
< hctx
->wb_reqlen
) {
2455 /*(64k - 4k to attempt to avoid temporary files
2456 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
2457 if (hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
> 65536 - 4096
2458 && (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_BUFMIN
)){
2459 con
->conf
.stream_request_body
&= ~FDEVENT_STREAM_REQUEST_POLLIN
;
2460 if (0 != hctx
->wb
->bytes_in
) return HANDLER_WAIT_FOR_EVENT
;
2462 handler_t r
= connection_handle_read_post_state(srv
, con
);
2463 chunkqueue
*req_cq
= con
->request_content_queue
;
2464 if (0 != hctx
->wb
->bytes_in
&& !chunkqueue_is_empty(req_cq
)) {
2465 chunkqueue_append_chunkqueue(hctx
->wb
, req_cq
);
2466 if (fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_OUT
) {
2467 return (r
== HANDLER_GO_ON
) ? HANDLER_WAIT_FOR_EVENT
: r
;
2470 if (r
!= HANDLER_GO_ON
) return r
;
2474 return ((0 == hctx
->wb
->bytes_in
|| !chunkqueue_is_empty(hctx
->wb
))
2475 && hctx
->state
!= FCGI_STATE_CONNECT
)
2476 ? scgi_send_request(srv
, hctx
)
2477 : HANDLER_WAIT_FOR_EVENT
;
2481 static handler_t
scgi_recv_response(server
*srv
, handler_ctx
*hctx
) {
2483 switch (scgi_demux_response(srv
, hctx
)) {
2488 scgi_connection_close(srv
, hctx
);
2490 return HANDLER_FINISHED
;
2492 connection
*con
= hctx
->remote_conn
;
2493 plugin_data
*p
= hctx
->plugin_data
;
2495 scgi_proc
*proc
= hctx
->proc
;
2496 scgi_extension_host
*host
= hctx
->host
;
2498 if (proc
->pid
&& proc
->state
!= PROC_STATE_DIED
) {
2501 /* only fetch the zombie if it is not already done */
2503 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
2505 /* child is still alive */
2510 /* the child should not terminate at all */
2511 if (WIFEXITED(status
)) {
2512 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
2513 "child exited, pid:", proc
->pid
,
2514 "status:", WEXITSTATUS(status
));
2515 } else if (WIFSIGNALED(status
)) {
2516 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2520 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2521 "child died somehow:",
2525 if (hctx
->conf
.debug
) {
2526 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsdsd",
2527 "--- scgi spawning",
2528 "\n\tport:", host
->port
,
2529 "\n\tsocket", host
->unixsocket
,
2530 "\n\tcurrent:", 1, "/", host
->min_procs
);
2533 if (scgi_spawn_connection(srv
, p
, host
, proc
)) {
2535 proc
->state
= PROC_STATE_DIED
;
2537 scgi_proclist_sort_down(srv
, host
, proc
);
2544 if (con
->file_started
== 0) {
2545 /* nothing has been send out yet, try to use another child */
2547 if (hctx
->wb
->bytes_out
== 0 &&
2548 hctx
->reconnects
++ < 5) {
2550 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsd",
2551 "response not sent, request not sent, reconnection.",
2552 "connection-fd:", con
->fd
,
2553 "fcgi-fd:", hctx
->fd
);
2555 return scgi_reconnect(srv
, hctx
);
2558 log_error_write(srv
, __FILE__
, __LINE__
, "sosdsd",
2559 "response not sent, request sent:", hctx
->wb
->bytes_out
,
2560 "connection-fd:", con
->fd
,
2561 "fcgi-fd:", hctx
->fd
);
2563 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsd",
2564 "response already sent out, termination connection",
2565 "connection-fd:", con
->fd
,
2566 "fcgi-fd:", hctx
->fd
);
2569 http_response_backend_error(srv
, con
);
2570 scgi_connection_close(srv
, hctx
);
2571 return HANDLER_FINISHED
;
2575 return HANDLER_GO_ON
;
2579 static handler_t
scgi_handle_fdevent(server
*srv
, void *ctx
, int revents
) {
2580 handler_ctx
*hctx
= ctx
;
2581 connection
*con
= hctx
->remote_conn
;
2583 joblist_append(srv
, con
);
2585 if (revents
& FDEVENT_IN
) {
2586 handler_t rc
= scgi_recv_response(srv
, hctx
);/*(might invalidate hctx)*/
2587 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
2590 if (revents
& FDEVENT_OUT
) {
2591 return scgi_send_request(srv
, hctx
); /*(might invalidate hctx)*/
2594 /* perhaps this issue is already handled */
2595 if (revents
& FDEVENT_HUP
) {
2596 if (hctx
->state
== FCGI_STATE_CONNECT
) {
2597 /* getoptsock will catch this one (right ?)
2599 * if we are in connect we might get a EINPROGRESS
2600 * in the first call and a FDEVENT_HUP in the
2603 * FIXME: as it is a bit ugly.
2606 scgi_send_request(srv
, hctx
);
2607 } else if (con
->file_started
) {
2608 /* drain any remaining data from kernel pipe buffers
2609 * even if (con->conf.stream_response_body
2610 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
2611 * since event loop will spin on fd FDEVENT_HUP event
2612 * until unregistered. */
2615 rc
= scgi_recv_response(srv
,hctx
);/*(might invalidate hctx)*/
2616 } while (rc
== HANDLER_GO_ON
); /*(unless HANDLER_GO_ON)*/
2617 return rc
; /* HANDLER_FINISHED or HANDLER_ERROR */
2619 scgi_extension_host
*host
= hctx
->host
;
2620 log_error_write(srv
, __FILE__
, __LINE__
, "sbSBSDSd",
2621 "error: unexpected close of scgi connection for",
2623 "(no scgi process on host: ",
2630 scgi_connection_close(srv
, hctx
);
2632 } else if (revents
& FDEVENT_ERR
) {
2633 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2634 "fcgi: got a FDEVENT_ERR. Don't know why.");
2636 http_response_backend_error(srv
, con
);
2637 scgi_connection_close(srv
, hctx
);
2640 return HANDLER_FINISHED
;
2644 static int scgi_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
2646 plugin_config
*s
= p
->config_storage
[0];
2652 /* skip the first, the global context */
2653 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
2654 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
2655 s
= p
->config_storage
[i
];
2657 /* condition didn't match */
2658 if (!config_check_cond(srv
, con
, dc
)) continue;
2661 for (j
= 0; j
< dc
->value
->used
; j
++) {
2662 data_unset
*du
= dc
->value
->data
[j
];
2664 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("scgi.server"))) {
2666 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("scgi.protocol"))) {
2668 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("scgi.debug"))) {
2679 static handler_t
scgi_check_extension(server
*srv
, connection
*con
, void *p_d
, int uri_path_handler
) {
2680 plugin_data
*p
= p_d
;
2681 size_t s_len
, uri_path_len
;
2684 scgi_extension
*extension
= NULL
;
2685 scgi_extension_host
*host
= NULL
;
2687 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
2689 /* Possibly, we processed already this request */
2690 if (con
->file_started
== 1) return HANDLER_GO_ON
;
2692 fn
= uri_path_handler
? con
->uri
.path
: con
->physical
.path
;
2694 if (buffer_string_is_empty(fn
)) return HANDLER_GO_ON
;
2696 s_len
= buffer_string_length(fn
);
2697 uri_path_len
= buffer_string_length(con
->uri
.path
);
2699 scgi_patch_connection(srv
, con
, p
);
2701 /* check if extension matches */
2702 for (k
= 0; k
< p
->conf
.exts
->used
; k
++) {
2704 scgi_extension
*ext
= p
->conf
.exts
->exts
[k
];
2706 if (buffer_is_empty(ext
->key
)) continue;
2708 ct_len
= buffer_string_length(ext
->key
);
2710 /* check _url_ in the form "/scgi_pattern" */
2711 if (ext
->key
->ptr
[0] == '/') {
2712 if (ct_len
<= uri_path_len
2713 && 0 == strncmp(con
->uri
.path
->ptr
, ext
->key
->ptr
, ct_len
)) {
2717 } else if (ct_len
<= s_len
2718 && 0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ext
->key
->ptr
, ct_len
)) {
2719 /* check extension in the form ".fcg" */
2725 /* extension doesn't match */
2726 if (NULL
== extension
) {
2727 return HANDLER_GO_ON
;
2730 /* get best server */
2731 host
= scgi_extension_host_get(srv
, con
, p
, extension
);
2733 return HANDLER_FINISHED
;
2736 /* a note about no handler is not sent yet */
2737 extension
->note_is_sent
= 0;
2739 /* SCGI requires that Content-Length be set.
2740 * Send 411 Length Required if Content-Length missing.
2741 * (Alternatively, collect full request body before proceeding
2742 * in mod_scgi_handle_subrequest()) */
2743 if (0 == con
->request
.content_length
2744 && array_get_element(con
->request
.headers
, "Transfer-Encoding")) {
2745 con
->keep_alive
= 0;
2746 con
->http_status
= 411; /* Length Required */
2748 return HANDLER_FINISHED
;
2752 * if check-local is disabled, use the uri.path handler
2756 /* init handler-context */
2757 if (uri_path_handler
) {
2758 if (host
->check_local
== 0) {
2762 hctx
= handler_ctx_init();
2764 hctx
->remote_conn
= con
;
2765 hctx
->plugin_data
= p
;
2768 hctx
->ext
= extension
;
2770 /*hctx->conf.exts = p->conf.exts;*/
2771 hctx
->conf
.proto
= p
->conf
.proto
;
2772 hctx
->conf
.debug
= p
->conf
.debug
;
2774 con
->plugin_ctx
[p
->id
] = hctx
;
2780 if (con
->conf
.log_request_handling
) {
2781 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2782 "handling it in mod_scgi");
2785 /* the prefix is the SCRIPT_NAME,
2786 * everything from start to the next slash
2787 * this is important for check-local = "disable"
2789 * if prefix = /admin.fcgi
2791 * /admin.fcgi/foo/bar
2793 * SCRIPT_NAME = /admin.fcgi
2794 * PATH_INFO = /foo/bar
2796 * if prefix = /fcgi-bin/
2800 * SCRIPT_NAME = /fcgi-bin/foo
2805 /* the rewrite is only done for /prefix/? matches */
2806 if (host
->fix_root_path_name
&& extension
->key
->ptr
[0] == '/' && extension
->key
->ptr
[1] == '\0') {
2807 buffer_copy_string(con
->request
.pathinfo
, con
->uri
.path
->ptr
);
2808 buffer_string_set_length(con
->uri
.path
, 0);
2809 } else if (extension
->key
->ptr
[0] == '/' &&
2810 buffer_string_length(con
->uri
.path
) > buffer_string_length(extension
->key
) &&
2811 NULL
!= (pathinfo
= strchr(con
->uri
.path
->ptr
+ buffer_string_length(extension
->key
), '/'))) {
2812 /* rewrite uri.path and pathinfo */
2814 buffer_copy_string(con
->request
.pathinfo
, pathinfo
);
2815 buffer_string_set_length(con
->uri
.path
, buffer_string_length(con
->uri
.path
) - buffer_string_length(con
->request
.pathinfo
));
2820 hctx
= handler_ctx_init();
2822 hctx
->remote_conn
= con
;
2823 hctx
->plugin_data
= p
;
2826 hctx
->ext
= extension
;
2828 /*hctx->conf.exts = p->conf.exts;*/
2829 hctx
->conf
.proto
= p
->conf
.proto
;
2830 hctx
->conf
.debug
= p
->conf
.debug
;
2832 con
->plugin_ctx
[p
->id
] = hctx
;
2838 if (con
->conf
.log_request_handling
) {
2839 log_error_write(srv
, __FILE__
, __LINE__
, "s", "handling it in mod_scgi");
2843 return HANDLER_GO_ON
;
2846 /* uri-path handler */
2847 static handler_t
scgi_check_extension_1(server
*srv
, connection
*con
, void *p_d
) {
2848 return scgi_check_extension(srv
, con
, p_d
, 1);
2851 /* start request handler */
2852 static handler_t
scgi_check_extension_2(server
*srv
, connection
*con
, void *p_d
) {
2853 return scgi_check_extension(srv
, con
, p_d
, 0);
2857 TRIGGER_FUNC(mod_scgi_handle_trigger
) {
2858 plugin_data
*p
= p_d
;
2862 /* perhaps we should kill a connect attempt after 10-15 seconds
2864 * currently we wait for the TCP timeout which is on Linux 180 seconds
2870 /* check all childs if they are still up */
2872 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
2873 plugin_config
*conf
;
2876 conf
= p
->config_storage
[i
];
2880 for (j
= 0; j
< exts
->used
; j
++) {
2885 for (n
= 0; n
< ex
->used
; n
++) {
2888 unsigned long sum_load
= 0;
2889 scgi_extension_host
*host
;
2891 host
= ex
->hosts
[n
];
2893 scgi_restart_dead_procs(srv
, p
, host
);
2895 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
2896 sum_load
+= proc
->load
;
2899 if (host
->num_procs
&&
2900 host
->num_procs
< host
->max_procs
&&
2901 (sum_load
/ host
->num_procs
) > host
->max_load_per_proc
) {
2902 /* overload, spawn new child */
2903 scgi_proc
*fp
= NULL
;
2905 if (p
->conf
.debug
) {
2906 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2907 "overload detected, spawning a new child");
2910 for (fp
= host
->unused_procs
; fp
&& fp
->pid
!= 0; fp
= fp
->next
);
2913 if (fp
== host
->unused_procs
) host
->unused_procs
= fp
->next
;
2915 if (fp
->next
) fp
->next
->prev
= NULL
;
2919 fp
= scgi_process_init();
2920 fp
->id
= host
->max_id
++;
2925 if (buffer_string_is_empty(host
->unixsocket
)) {
2926 fp
->port
= host
->port
+ fp
->id
;
2928 buffer_copy_buffer(fp
->socket
, host
->unixsocket
);
2929 buffer_append_string_len(fp
->socket
, CONST_STR_LEN("-"));
2930 buffer_append_int(fp
->socket
, fp
->id
);
2933 if (scgi_spawn_connection(srv
, p
, host
, fp
)) {
2934 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2935 "ERROR: spawning fcgi failed.");
2936 scgi_process_free(fp
);
2937 return HANDLER_ERROR
;
2941 fp
->next
= host
->first
;
2943 host
->first
->prev
= fp
;
2948 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
2949 if (proc
->load
!= 0) break;
2950 if (host
->num_procs
<= host
->min_procs
) break;
2951 if (proc
->pid
== 0) continue;
2953 if (srv
->cur_ts
- proc
->last_used
> host
->idle_timeout
) {
2954 /* a proc is idling for a long time now,
2957 if (p
->conf
.debug
) {
2958 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsd",
2959 "idle-timeout reached, terminating child:",
2960 "socket:", proc
->socket
,
2965 if (proc
->next
) proc
->next
->prev
= proc
->prev
;
2966 if (proc
->prev
) proc
->prev
->next
= proc
->next
;
2968 if (proc
->prev
== NULL
) host
->first
= proc
->next
;
2971 proc
->next
= host
->unused_procs
;
2973 if (host
->unused_procs
) host
->unused_procs
->prev
= proc
;
2974 host
->unused_procs
= proc
;
2976 kill(proc
->pid
, SIGTERM
);
2978 proc
->state
= PROC_STATE_KILLED
;
2980 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsd",
2982 "socket:", proc
->socket
,
2987 /* proc is now in unused, let the next second handle the next process */
2992 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
2995 if (proc
->pid
== 0) continue;
2997 switch (waitpid(proc
->pid
, &status
, WNOHANG
)) {
2999 /* child still running after timeout, good */
3002 if (errno
!= EINTR
) {
3003 /* no PID found ? should never happen */
3004 log_error_write(srv
, __FILE__
, __LINE__
, "sddss",
3005 "pid ", proc
->pid
, proc
->state
,
3006 "not found:", strerror(errno
));
3009 if (errno
== ECHILD
) {
3010 /* someone else has cleaned up for us */
3012 proc
->state
= PROC_STATE_UNSET
;
3018 /* the child should not terminate at all */
3019 if (WIFEXITED(status
)) {
3020 if (proc
->state
!= PROC_STATE_KILLED
) {
3021 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
3023 WEXITSTATUS(status
), proc
->socket
);
3025 } else if (WIFSIGNALED(status
)) {
3026 if (WTERMSIG(status
) != SIGTERM
) {
3027 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3032 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3033 "child died somehow:",
3037 proc
->state
= PROC_STATE_UNSET
;
3045 return HANDLER_GO_ON
;
3049 int mod_scgi_plugin_init(plugin
*p
);
3050 int mod_scgi_plugin_init(plugin
*p
) {
3051 p
->version
= LIGHTTPD_VERSION_ID
;
3052 p
->name
= buffer_init_string("scgi");
3054 p
->init
= mod_scgi_init
;
3055 p
->cleanup
= mod_scgi_free
;
3056 p
->set_defaults
= mod_scgi_set_defaults
;
3057 p
->connection_reset
= scgi_connection_reset
;
3058 p
->handle_connection_close
= scgi_connection_reset
;
3059 p
->handle_uri_clean
= scgi_check_extension_1
;
3060 p
->handle_subrequest_start
= scgi_check_extension_2
;
3061 p
->handle_subrequest
= mod_scgi_handle_subrequest
;
3062 p
->handle_trigger
= mod_scgi_handle_trigger
;