xref: /lighttpd1.4/src/base.h (revision 49e9f0ac)
1 #ifndef _BASE_H_
2 #define _BASE_H_
3 #include "first.h"
4 
5 #include "settings.h"
6 
7 #include <sys/types.h>
8 #include <sys/time.h>
9 
10 #include "base_decls.h"
11 #include "buffer.h"
12 #include "array.h"
13 #include "chunk.h"
14 #include "http_kv.h"
15 #include "sock_addr.h"
16 #include "etag.h"
17 
18 struct fdevents;        /* declaration */
19 struct stat_cache;      /* declaration */
20 
21 #define DIRECT 0        /* con->mode */
22 
23 
24 typedef struct {
25 	/** HEADER */
26 	/* the request-line */
27 	buffer *request;
28 	buffer *uri;
29 
30 	buffer *orig_uri;
31 
32 	http_method_t  http_method;
33 	http_version_t http_version;
34 
35 	/* strings to the header */
36 	buffer *http_host; /* not alloced */
37 
38 	unsigned int htags; /* bitfield of flagged headers present in request */
39 	array  *headers;
40 
41 	/* CONTENT */
42 	off_t content_length; /* returned by strtoll() */
43 	off_t te_chunked;
44 
45 	/* internal */
46 	buffer *pathinfo;
47 } request;
48 
49 typedef struct {
50 	off_t   content_length;
51 	unsigned int htags; /* bitfield of flagged headers present in response */
52 	array  *headers;
53 	int send_chunked;
54 } response;
55 
56 typedef struct {
57 	buffer *scheme; /* scheme without colon or slashes ( "http" or "https" ) */
58 
59 	/* authority with optional portnumber ("site.name" or "site.name:8080" ) NOTE: without "username:password@" */
60 	buffer *authority;
61 
62 	/* path including leading slash ("/" or "/index.html") - urldecoded, and sanitized  ( buffer_path_simplify() && buffer_urldecode_path() ) */
63 	buffer *path;
64 	buffer *path_raw; /* raw path, as sent from client. no urldecoding or path simplifying */
65 	buffer *query; /* querystring ( everything after "?", ie: in "/index.php?foo=1", query is "foo=1" ) */
66 } request_uri;
67 
68 typedef struct {
69 	buffer *path;
70 	buffer *basedir; /* path = "(basedir)(.*)" */
71 
72 	buffer *doc_root; /* path = doc_root + rel_path */
73 	buffer *rel_path;
74 
75 	buffer *etag;
76 } physical;
77 
78 typedef struct {
79 	array *mimetypes;
80 
81 	/* virtual-servers */
82 	buffer *document_root;
83 	buffer *server_name;
84 	buffer *error_handler;
85 	buffer *error_handler_404;
86 	buffer *server_tag;
87 	buffer *dirlist_encoding;
88 	buffer *errorfile_prefix;
89 	buffer *socket_perms;
90 
91 	unsigned short high_precision_timestamps;
92 	unsigned short max_keep_alive_requests;
93 	unsigned short max_keep_alive_idle;
94 	unsigned short max_read_idle;
95 	unsigned short max_write_idle;
96 	unsigned short use_xattr;
97 	unsigned short follow_symlink;
98 	unsigned short range_requests;
99 	unsigned short stream_request_body;
100 	unsigned short stream_response_body;
101 	unsigned short error_intercept;
102 
103 	/* debug */
104 
105 	unsigned short log_file_not_found;
106 	unsigned short log_request_header;
107 	unsigned short log_request_handling;
108 	unsigned short log_response_header;
109 	unsigned short log_condition_handling;
110 	unsigned short log_timeouts;
111 
112 
113 	/* server wide */
114 	unsigned short use_ipv6, set_v6only; /* set_v6only is only a temporary option */
115 	unsigned short defer_accept;
116 	unsigned short ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */
117 	unsigned short allow_http11;
118 	unsigned short etag_use_inode;
119 	unsigned short etag_use_mtime;
120 	unsigned short etag_use_size;
121 	unsigned short force_lowercase_filenames; /* if the FS is case-insensitive, force all files to lower-case */
122 	unsigned int http_parseopts;
123 	unsigned int max_request_size;
124 	int listen_backlog;
125 
126 	unsigned short kbytes_per_second; /* connection kb/s limit */
127 
128 	/* configside */
129 	unsigned short global_kbytes_per_second; /*  */
130 
131 	off_t  global_bytes_per_second_cnt;
132 	/* server-wide traffic-shaper
133 	 *
134 	 * each context has the counter which is inited once
135 	 * a second by the global_kbytes_per_second config-var
136 	 *
137 	 * as soon as global_kbytes_per_second gets below 0
138 	 * the connected conns are "offline" a little bit
139 	 *
140 	 * the problem:
141 	 * we somehow have to loose our "we are writable" signal
142 	 * on the way.
143 	 *
144 	 */
145 	off_t *global_bytes_per_second_cnt_ptr; /*  */
146 
147 #if defined(__FreeBSD__) || defined(__NetBSD__) \
148  || defined(__OpenBSD__) || defined(__DragonFly__)
149 	buffer *bsd_accept_filter;
150 #endif
151 
152 } specific_config;
153 
154 /* the order of the items should be the same as they are processed
155  * read before write as we use this later */
156 typedef enum {
157 	CON_STATE_CONNECT,
158 	CON_STATE_REQUEST_START,
159 	CON_STATE_READ,
160 	CON_STATE_REQUEST_END,
161 	CON_STATE_READ_POST,
162 	CON_STATE_HANDLE_REQUEST,
163 	CON_STATE_RESPONSE_START,
164 	CON_STATE_WRITE,
165 	CON_STATE_RESPONSE_END,
166 	CON_STATE_ERROR,
167 	CON_STATE_CLOSE
168 } connection_state_t;
169 
170 typedef enum {
171 	/* condition not active at the moment because itself or some
172 	 * pre-condition depends on data not available yet
173 	 */
174 	COND_RESULT_UNSET,
175 
176 	/* special "unset" for branches not selected due to pre-conditions
177 	 * not met (but pre-conditions are not "unset" anymore)
178 	 */
179 	COND_RESULT_SKIP,
180 
181 	/* actually evaluated the condition itself */
182 	COND_RESULT_FALSE, /* not active */
183 	COND_RESULT_TRUE   /* active */
184 } cond_result_t;
185 
186 typedef struct cond_cache_t {
187 	/* current result (with preconditions) */
188 	cond_result_t result;
189 	/* result without preconditions (must never be "skip") */
190 	cond_result_t local_result;
191 	int patterncount;
192 	int matches[3 * 10];
193 	buffer *comp_value; /* just a pointer */
194 } cond_cache_t;
195 
196 struct connection {
197 	connection_state_t state;
198 
199 	/* timestamps */
200 	time_t read_idle_ts;
201 	time_t close_timeout_ts;
202 	time_t write_request_ts;
203 
204 	time_t connection_start;
205 	time_t request_start;
206 	struct timespec request_start_hp;
207 
208 	size_t request_count;        /* number of requests handled in this connection */
209 	size_t loops_per_request;    /* to catch endless loops in a single request
210 				      *
211 				      * used by mod_rewrite, mod_fastcgi, ... and others
212 				      * this is self-protection
213 				      */
214 
215 	fdnode *fdn;                 /* fdevent (fdnode *) object */
216 	int fd;                      /* the FD for this connection */
217 	int ndx;                     /* reverse mapping to server->connection[ndx] */
218 
219 	/* fd states */
220 	int is_readable;
221 	int is_writable;
222 	int is_ssl_sock;
223 
224 	int keep_alive;              /* only request.c can enable it, all other just disable */
225 	int keep_alive_idle;         /* remember max_keep_alive_idle from config */
226 
227 	int file_started;
228 	int file_finished;
229 
230 	chunkqueue *write_queue;      /* a large queue for low-level write ( HTTP response ) [ file, mem ] */
231 	chunkqueue *read_queue;       /* a small queue for low-level read ( HTTP request ) [ mem ] */
232 	chunkqueue *request_content_queue; /* takes request-content into tempfile if necessary [ tempfile, mem ]*/
233 
234 	int traffic_limit_reached;
235 
236 	off_t bytes_written;          /* used by mod_accesslog, mod_rrd */
237 	off_t bytes_written_cur_second; /* used by mod_accesslog, mod_rrd */
238 	off_t bytes_read;             /* used by mod_accesslog, mod_rrd */
239 	off_t bytes_header;
240 
241 	int http_status;
242 
243 	sock_addr dst_addr;
244 	buffer *dst_addr_buf;
245 
246 	/* request */
247 	request  request;
248 	request_uri uri;
249 	physical physical;
250 	response response;
251 
252 	size_t header_len;
253 
254 	array  *environment; /* used to pass lighttpd internal stuff to the FastCGI/CGI apps, setenv does that */
255 
256 	unsigned int mode;           /* DIRECT (0) or plugin id */
257 	int async_callback;
258 
259 	log_error_st *errh;
260 
261 	void **plugin_ctx;           /* plugin connection specific config */
262 
263 	specific_config conf;        /* global connection specific config */
264 	cond_cache_t *cond_cache;
265 
266 	buffer *server_name;
267 	buffer *proto;
268 
269 	/* error-handler */
270 	int error_handler_saved_status;
271 	http_method_t error_handler_saved_method;
272 
273 	struct server_socket *srv_socket;   /* reference to the server-socket */
274 	int (* network_write)(struct server *srv, struct connection *con, chunkqueue *cq, off_t max_bytes);
275 	int (* network_read)(struct server *srv, struct connection *con, chunkqueue *cq, off_t max_bytes);
276 
277 	/* etag handling */
278 	etag_flags_t etag_flags;
279 
280 	int8_t conditional_is_valid[16]; /* MUST be >= COMP_LAST_ELEMENT] */
281 };
282 
283 typedef struct {
284 	connection **ptr;
285 	size_t size;
286 	size_t used;
287 } connections;
288 
289 typedef struct {
290 	time_t  mtime;  /* the key */
291 	buffer *str;    /* a buffer for the string represenation */
292 } mtime_cache_type;
293 
294 typedef struct {
295 	void  *ptr;
296 	size_t used;
297 	size_t size;
298 } buffer_plugin;
299 
300 typedef struct {
301 	unsigned short port;
302 	buffer *bindhost;
303 
304 	buffer *errorlog_file;
305 	unsigned short errorlog_use_syslog;
306 	buffer *breakagelog_file;
307 
308 	unsigned short dont_daemonize;
309 	unsigned short preflight_check;
310 	buffer *changeroot;
311 	buffer *username;
312 	buffer *groupname;
313 
314 	buffer *pid_file;
315 
316 	buffer *event_handler;
317 
318 	buffer *modules_dir;
319 	buffer *network_backend;
320 	array *modules;
321 	array *upload_tempdirs;
322 	unsigned int upload_temp_file_size;
323 	unsigned int max_request_field_size;
324 
325 	unsigned short max_worker;
326 	unsigned short max_fds;
327 	unsigned short max_conns;
328 
329 	unsigned short log_request_header_on_error;
330 	unsigned short log_state_handling;
331 
332 	int stat_cache_engine;
333 	unsigned short enable_cores;
334 	unsigned short reject_expect_100_with_417;
335 	buffer *xattr_name;
336 
337 	unsigned short http_header_strict;
338 	unsigned short http_host_strict;
339 	unsigned short http_host_normalize;
340 	unsigned short http_url_normalize;
341 	unsigned short http_method_get_body;
342 	unsigned short high_precision_timestamps;
343 	time_t loadts;
344 	double loadavg[3];
345 	buffer *syslog_facility;
346 
347 	unsigned short compat_module_load;
348 	unsigned short systemd_socket_activation;
349 } server_config;
350 
351 typedef struct server_socket {
352 	sock_addr addr;
353 	int       fd;
354 
355 	unsigned short is_ssl;
356 	unsigned short sidx;
357 
358 	fdnode *fdn;
359 	buffer *srv_token;
360 } server_socket;
361 
362 typedef struct {
363 	server_socket **ptr;
364 
365 	size_t size;
366 	size_t used;
367 } server_socket_array;
368 
369 struct server {
370 	server_socket_array srv_sockets;
371 
372 	struct fdevents *ev;
373 
374 	buffer_plugin plugins;
375 	void *plugin_slots;
376 
377 	/* counters */
378 	int con_opened;
379 	int con_read;
380 	int con_written;
381 	int con_closed;
382 
383 	int max_fds;    /* max possible fds */
384 	int max_fds_lowat;/* low  watermark */
385 	int max_fds_hiwat;/* high watermark */
386 	int cur_fds;    /* currently used fds */
387 	int want_fds;   /* waiting fds */
388 	int sockets_disabled;
389 
390 	size_t max_conns;
391 
392 	/* buffers */
393 	buffer *parse_full_path;
394 	buffer *response_header;
395 	buffer *response_range;
396 	buffer *tmp_buf;
397 
398 	buffer *tmp_chunk_len;
399 
400 	buffer *empty_string; /* is necessary for cond_match */
401 
402 	buffer *cond_check_buf;
403 
404 	/* caches */
405 	mtime_cache_type mtime_cache[FILE_CACHE_MAX];
406 
407 	array *split_vals;
408 
409 	log_error_st *errh;
410 
411 	/* Timestamps */
412 	time_t cur_ts;
413 	time_t last_generated_date_ts;
414 	time_t last_generated_debug_ts;
415 	time_t startup_ts;
416 
417 	buffer *ts_debug_str;
418 	buffer *ts_date_str;
419 
420 	/* config-file */
421 	array *config_touched;
422 
423 	array *config_context;
424 	specific_config **config_storage;
425 
426 	server_config  srvconf;
427 
428 	short int config_deprecated;
429 	short int config_unsupported;
430 
431 	connections *conns;
432 	connections *joblist;
433 	connections *fdwaitqueue;
434 
435 	struct stat_cache *stat_cache;
436 
437 	/**
438 	 * The status array can carry all the status information you want
439 	 * the key to the array is <module-prefix>.<name>
440 	 * and the values are counters
441 	 *
442 	 * example:
443 	 *   fastcgi.backends        = 10
444 	 *   fastcgi.active-backends = 6
445 	 *   fastcgi.backend.<key>.load = 24
446 	 *   fastcgi.backend.<key>....
447 	 *
448 	 *   fastcgi.backend.<key>.disconnects = ...
449 	 */
450 	array *status;
451 
452 	int event_handler;
453 
454 	int (* network_backend_write)(struct server *srv, int fd, chunkqueue *cq, off_t max_bytes);
455 	handler_t (* request_env)(struct server *srv, connection *con);
456 
457 	uid_t uid;
458 	gid_t gid;
459 	pid_t pid;
460 
461 	server_socket_array srv_sockets_inherited;
462 };
463 
464 
465 #endif
466