1 #ifndef INCLUDED_GW_BACKEND_H
2 #define INCLUDED_GW_BACKEND_H
7 #include "sys-socket.h"
19 typedef struct gw_proc
{
20 size_t id
; /* id will be between 1 and max_procs */
21 buffer
*unixsocket
; /* config.socket + "-" + id */
22 unsigned port
; /* config.port + pno */
24 struct sockaddr
*saddr
;
26 /* either tcp:<host>:<port> or unix:<socket> for debugging purposes */
27 buffer
*connection_name
;
29 pid_t pid
; /* PID of the spawned process (0 if not spawned locally) */
32 size_t load
; /* number of requests waiting on this process */
34 time_t last_used
; /* see idle_timeout */
35 size_t requests
; /* see max_requests */
36 struct gw_proc
*prev
, *next
; /* see first */
38 time_t disabled_until
; /* proc disabled until given time */
43 PROC_STATE_RUNNING
, /* alive */
44 PROC_STATE_OVERLOADED
, /* listen-queue is full */
45 PROC_STATE_DIED_WAIT_FOR_PID
, /* */
46 PROC_STATE_DIED
, /* marked as dead, should be restarted */
47 PROC_STATE_KILLED
/* killed (signal sent to proc) */
52 /* the key that is used to reference this value */
55 /* list of processes handling this extension
56 * sorted by lowest load
58 * whenever a job is done move it up in the list
59 * until it is sorted, move it down as soon as the
63 gw_proc
*unused_procs
;
66 * spawn at least min_procs, at max_procs.
68 * as soon as the load of the first entry
69 * is max_load_per_proc we spawn a new one
70 * and add it to the first entry and give it
75 unsigned short min_procs
;
76 unsigned short max_procs
;
77 size_t num_procs
; /* how many procs are started */
78 size_t active_procs
; /* how many procs in state PROC_STATE_RUNNING */
80 unsigned short max_load_per_proc
;
83 * kick the process from the list if it was not
84 * used for idle_timeout until min_procs is
85 * reached. this helps to get the processlist
86 * small again we had a small peak load.
90 unsigned short idle_timeout
;
93 * time after a disabled remote connection is tried to be re-enabled
98 unsigned short disable_time
;
101 * some gw processes get a little bit larger
102 * than wanted. max_requests_per_proc kills a
103 * process after a number of handled requests.
106 size_t max_requests_per_proc
;
114 * if host is one of the local IP adresses the
115 * whole connection is local
117 * if port is not 0, and host is not specified,
118 * "localhost" (INADDR_LOOPBACK) is assumed.
123 unsigned short family
; /* sa_family_t */
128 * instead of TCP/IP we can use Unix Domain Sockets
129 * - more secure (you have fileperms to play with)
130 * - more control (on locally)
131 * - more speed (no extra overhead)
135 /* if socket is local we can start the gw process ourself
137 * bin-path is the path to the binary
139 * check min_procs and max_procs for the number
140 * of process to start up
144 /* bin-path is set bin-environment is taken to
145 * create the environement before starting the
154 * docroot-translation between URL->phys and the
158 * - different dir-layout if remote
165 * check_local tells you if the phys file is stat()ed
166 * or not. FastCGI doesn't care if the service is
167 * remote. If the web-server side doesn't contain
168 * the FastCGI-files we should not stat() for them
169 * and say '404 not found'.
171 unsigned short check_local
;
174 * append PATH_INFO to SCRIPT_FILENAME
176 * php needs this if cgi.fix_pathinfo is provided
180 unsigned short break_scriptfilename_for_php
;
183 * workaround for program when prefix="/"
185 * rule to build PATH_INFO is hardcoded for when check_local is disabled
186 * enable this option to use the workaround
190 unsigned short fix_root_path_name
;
193 * If the backend includes X-Sendfile in the response
194 * we use the value as filename and ignore the content.
197 unsigned short xsendfile_allow
;
198 array
*xsendfile_docroot
;
202 size_t max_id
; /* corresponds most of the time to num_procs */
204 buffer
*strip_request_uri
;
206 unsigned short tcp_fin_propagate
;
207 unsigned short kill_signal
; /* we need a setting for this as libfcgi
208 applications prefer SIGUSR1 while the
209 rest of the world would use SIGTERM
219 * one extension can have multiple hosts assigned
220 * one host can spawn additional processes on the same
221 * socket (if we control it)
223 * ext -> host -> procs
226 * if the gw process is remote that whole goes down
229 * ext -> host -> procs
232 * in case of PHP and FCGI_CHILDREN we have again a procs
233 * but we don't control it directly.
238 buffer
*key
; /* like .php */
259 #include "base_decls.h"
262 #include "response.h"
264 typedef struct gw_plugin_config
{
276 /* generic plugin data, shared between all connections */
277 typedef struct gw_plugin_data
{
279 gw_plugin_config
**config_storage
;
281 gw_plugin_config conf
; /* used only as long as no gw_handler_ctx is setup */
285 /* connection specific data */
288 GW_STATE_CONNECT_DELAYED
,
289 GW_STATE_PREPARE_WRITE
,
292 } gw_connection_state_t
;
294 #define GW_RESPONDER 1
295 #define GW_AUTHORIZER 2
296 #define GW_FILTER 3 /*(not implemented)*/
298 typedef struct gw_handler_ctx
{
302 gw_extension
*ext_auth
; /* (future: might allow multiple authorizers)*/
303 unsigned short gw_mode
; /* mode: GW_AUTHORIZER or GW_RESPONDER */
305 gw_connection_state_t state
;
306 time_t state_timestamp
;
308 chunkqueue
*rb
; /* read queue */
309 chunkqueue
*wb
; /* write queue */
314 int fd
; /* fd to the gw process */
317 int reconnects
; /* number of reconnect attempts */
320 int send_content_body
;
322 http_response_opts opts
;
323 gw_plugin_config conf
;
325 connection
*remote_conn
; /* dumb pointer */
326 gw_plugin_data
*plugin_data
; /* dumb pointer */
327 handler_t(*stdin_append
)(server
*srv
, struct gw_handler_ctx
*hctx
);
328 handler_t(*create_env
)(server
*srv
, struct gw_handler_ctx
*hctx
);
329 void(*backend_error
)(struct gw_handler_ctx
*hctx
);
330 void(*handler_ctx_free
)(void *hctx
);
334 void * gw_init(void);
335 void gw_plugin_config_free(gw_plugin_config
*s
);
336 handler_t
gw_free(server
*srv
, void *p_d
);
337 int gw_set_defaults_backend(server
*srv
, gw_plugin_data
*p
, data_unset
*du
, size_t i
, int sh_exec
);
338 int gw_set_defaults_balance(server
*srv
, gw_plugin_config
*s
, data_unset
*du
);
339 handler_t
gw_check_extension(server
*srv
, connection
*con
, gw_plugin_data
*p
, int uri_path_handler
, size_t hctx_sz
);
340 handler_t
gw_connection_reset(server
*srv
, connection
*con
, void *p_d
);
341 handler_t
gw_handle_subrequest(server
*srv
, connection
*con
, void *p_d
);
342 handler_t
gw_handle_trigger(server
*srv
, void *p_d
);
343 handler_t
gw_handle_waitpid_cb(server
*srv
, void *p_d
, pid_t pid
, int status
);
345 void gw_set_transparent(server
*srv
, gw_handler_ctx
*hctx
);