[core] remove fde_ndx member outside fdevents
[lighttpd.git] / src / gw_backend.h
blob88ba16e7c271a21c7cf116c80307dbde35df9493
1 #ifndef INCLUDED_GW_BACKEND_H
2 #define INCLUDED_GW_BACKEND_H
4 #include "first.h"
6 #include <sys/types.h>
7 #include "sys-socket.h"
9 #include "array.h"
10 #include "buffer.h"
12 typedef struct {
13 char **ptr;
15 size_t size;
16 size_t used;
17 } char_array;
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 */
23 socklen_t saddrlen;
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 */
40 int is_local;
42 enum {
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) */
48 } state;
49 } gw_proc;
51 typedef struct {
52 /* the key that is used to reference this value */
53 buffer *id;
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
60 * job is started
62 gw_proc *first;
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
71 * the load
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;
109 /* config */
112 * host:port
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.
121 buffer *host;
122 unsigned short port;
123 unsigned short family; /* sa_family_t */
126 * Unix Domain Socket
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)
133 buffer *unixsocket;
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
142 buffer *bin_path;
144 /* bin-path is set bin-environment is taken to
145 * create the environement before starting the
146 * FastCGI process
149 array *bin_env;
151 array *bin_env_copy;
154 * docroot-translation between URL->phys and the
155 * remote host
157 * reasons:
158 * - different dir-layout if remote
159 * - chroot if local
162 buffer *docroot;
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;
200 ssize_t load;
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
210 *sigh* */
212 int listen_backlog;
213 int refcount;
215 char_array args;
216 } gw_host;
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
224 * 1:n 1:n
226 * if the gw process is remote that whole goes down
227 * to
229 * ext -> host -> procs
230 * 1:n 1:1
232 * in case of PHP and FCGI_CHILDREN we have again a procs
233 * but we don't control it directly.
237 typedef struct {
238 buffer *key; /* like .php */
240 int note_is_sent;
241 int last_used_ndx;
243 gw_host **hosts;
245 size_t used;
246 size_t size;
247 } gw_extension;
249 typedef struct {
250 gw_extension **exts;
252 size_t used;
253 size_t size;
254 } gw_exts;
259 #include "base_decls.h"
260 #include "chunk.h"
261 #include "plugin.h"
262 #include "response.h"
264 typedef struct gw_plugin_config {
265 gw_exts *exts;
266 gw_exts *exts_auth;
267 gw_exts *exts_resp;
269 array *ext_mapping;
271 int balance;
272 int proto;
273 int debug;
274 } gw_plugin_config;
276 /* generic plugin data, shared between all connections */
277 typedef struct gw_plugin_data {
278 PLUGIN_DATA;
279 gw_plugin_config **config_storage;
281 gw_plugin_config conf; /* used only as long as no gw_handler_ctx is setup */
282 pid_t srv_pid;
283 } gw_plugin_data;
285 /* connection specific data */
286 typedef enum {
287 GW_STATE_INIT,
288 GW_STATE_CONNECT_DELAYED,
289 GW_STATE_PREPARE_WRITE,
290 GW_STATE_WRITE,
291 GW_STATE_READ
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 {
299 gw_proc *proc;
300 gw_host *host;
301 gw_extension *ext;
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 */
310 off_t wb_reqlen;
312 buffer *response;
314 int fd; /* fd to the gw process */
316 pid_t pid;
317 int reconnects; /* number of reconnect attempts */
319 int request_id;
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);
331 } gw_handler_ctx;
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);
347 #endif