[core] quiet pedantic cc warning for excess comma
[lighttpd.git] / src / base.h
blob1d35c17bdaa0e61f06f1a30771c31ac3a65e2921
1 #ifndef _BASE_H_
2 #define _BASE_H_
3 #include "first.h"
5 #include "settings.h"
7 #include <sys/types.h>
8 #include <sys/time.h>
9 #include <sys/stat.h>
11 #include <limits.h>
13 #ifdef HAVE_STDINT_H
14 # include <stdint.h>
15 #endif
17 #ifdef HAVE_INTTYPES_H
18 # include <inttypes.h>
19 #endif
21 #include "base_decls.h"
22 #include "buffer.h"
23 #include "array.h"
24 #include "chunk.h"
25 #include "keyvalue.h"
26 #include "sock_addr.h"
27 #include "etag.h"
29 struct fdevents; /* declaration */
30 struct stat_cache; /* declaration */
32 #ifndef O_BINARY
33 # define O_BINARY 0
34 #endif
36 #ifndef SIZE_MAX
37 # ifdef SIZE_T_MAX
38 # define SIZE_MAX SIZE_T_MAX
39 # else
40 # define SIZE_MAX ((size_t)~0)
41 # endif
42 #endif
44 #ifndef SSIZE_MAX
45 # define SSIZE_MAX ((size_t)~0 >> 1)
46 #endif
48 #ifdef __APPLE__
49 #include <crt_externs.h>
50 #define environ (* _NSGetEnviron())
51 #else
52 extern char **environ;
53 #endif
55 /* for solaris 2.5 and NetBSD 1.3.x */
56 #ifndef HAVE_SOCKLEN_T
57 typedef int socklen_t;
58 #endif
60 /* solaris and NetBSD 1.3.x again */
61 #if (!defined(HAVE_STDINT_H)) && (!defined(HAVE_INTTYPES_H)) && (!defined(uint32_t))
62 # define uint32_t u_int32_t
63 #endif
66 #ifndef SHUT_WR
67 # define SHUT_WR 1
68 #endif
70 typedef enum { T_CONFIG_UNSET,
71 T_CONFIG_STRING,
72 T_CONFIG_SHORT,
73 T_CONFIG_INT,
74 T_CONFIG_BOOLEAN,
75 T_CONFIG_ARRAY,
76 T_CONFIG_LOCAL,
77 T_CONFIG_DEPRECATED,
78 T_CONFIG_UNSUPPORTED
79 } config_values_type_t;
81 typedef enum { T_CONFIG_SCOPE_UNSET,
82 T_CONFIG_SCOPE_SERVER,
83 T_CONFIG_SCOPE_CONNECTION
84 } config_scope_type_t;
86 typedef struct {
87 const char *key;
88 void *destination;
90 config_values_type_t type;
91 config_scope_type_t scope;
92 } config_values_t;
94 typedef enum { DIRECT, EXTERNAL } connection_type;
96 typedef struct {
97 char *key;
98 connection_type type;
99 char *value;
100 } request_handler;
103 /* fcgi_response_header contains ... */
104 #define HTTP_STATUS BV(0)
105 #define HTTP_CONNECTION BV(1)
106 #define HTTP_CONTENT_LENGTH BV(2)
107 #define HTTP_DATE BV(3)
108 #define HTTP_LOCATION BV(4)
109 #define HTTP_TRANSFER_ENCODING BV(5)
110 #define HTTP_CONTENT_LOCATION BV(6)
111 #define HTTP_SET_COOKIE BV(7)
112 #define HTTP_UPGRADE BV(8)
114 typedef struct {
115 /** HEADER */
116 /* the request-line */
117 buffer *request;
118 buffer *uri;
120 buffer *orig_uri;
122 http_method_t http_method;
123 http_version_t http_version;
125 buffer *request_line;
127 /* strings to the header */
128 buffer *http_host; /* not alloced */
129 const char *http_range;
130 const char *http_content_type;
131 const char *http_if_modified_since;
132 const char *http_if_none_match;
134 array *headers;
136 /* CONTENT */
137 off_t content_length; /* returned by strtoll() */
138 off_t te_chunked;
140 /* internal representation */
141 int accept_encoding;
143 /* internal */
144 buffer *pathinfo;
145 } request;
147 typedef struct {
148 off_t content_length;
149 int keep_alive; /* used by the subrequests in proxy, cgi and fcgi to say the subrequest was keep-alive or not */
151 array *headers;
153 enum {
154 HTTP_TRANSFER_ENCODING_IDENTITY, HTTP_TRANSFER_ENCODING_CHUNKED
155 } transfer_encoding;
156 } response;
158 typedef struct {
159 buffer *scheme; /* scheme without colon or slashes ( "http" or "https" ) */
161 /* authority with optional portnumber ("site.name" or "site.name:8080" ) NOTE: without "username:password@" */
162 buffer *authority;
164 /* path including leading slash ("/" or "/index.html") - urldecoded, and sanitized ( buffer_path_simplify() && buffer_urldecode_path() ) */
165 buffer *path;
166 buffer *path_raw; /* raw path, as sent from client. no urldecoding or path simplifying */
167 buffer *query; /* querystring ( everything after "?", ie: in "/index.php?foo=1", query is "foo=1" ) */
168 } request_uri;
170 typedef struct {
171 buffer *path;
172 buffer *basedir; /* path = "(basedir)(.*)" */
174 buffer *doc_root; /* path = doc_root + rel_path */
175 buffer *rel_path;
177 buffer *etag;
178 } physical;
180 typedef struct {
181 buffer *name;
182 buffer *etag;
184 struct stat st;
186 time_t stat_ts;
188 #ifdef HAVE_LSTAT
189 char is_symlink;
190 #endif
192 #ifdef HAVE_FAM_H
193 int dir_version;
194 #endif
196 buffer *content_type;
197 } stat_cache_entry;
199 typedef struct {
200 array *mimetypes;
202 /* virtual-servers */
203 buffer *document_root;
204 buffer *server_name;
205 buffer *error_handler;
206 buffer *error_handler_404;
207 buffer *server_tag;
208 buffer *dirlist_encoding;
209 buffer *errorfile_prefix;
210 buffer *socket_perms;
212 unsigned short high_precision_timestamps;
213 unsigned short max_keep_alive_requests;
214 unsigned short max_keep_alive_idle;
215 unsigned short max_read_idle;
216 unsigned short max_write_idle;
217 unsigned short use_xattr;
218 unsigned short follow_symlink;
219 unsigned short range_requests;
220 unsigned short stream_request_body;
221 unsigned short stream_response_body;
222 unsigned short error_intercept;
224 /* debug */
226 unsigned short log_file_not_found;
227 unsigned short log_request_header;
228 unsigned short log_request_handling;
229 unsigned short log_response_header;
230 unsigned short log_condition_handling;
231 unsigned short log_timeouts;
234 /* server wide */
235 unsigned short use_ipv6, set_v6only; /* set_v6only is only a temporary option */
236 unsigned short defer_accept;
237 unsigned short ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */
238 unsigned short allow_http11;
239 unsigned short etag_use_inode;
240 unsigned short etag_use_mtime;
241 unsigned short etag_use_size;
242 unsigned short force_lowercase_filenames; /* if the FS is case-insensitive, force all files to lower-case */
243 unsigned int http_parseopts;
244 unsigned int max_request_size;
245 int listen_backlog;
247 unsigned short kbytes_per_second; /* connection kb/s limit */
249 /* configside */
250 unsigned short global_kbytes_per_second; /* */
252 off_t global_bytes_per_second_cnt;
253 /* server-wide traffic-shaper
255 * each context has the counter which is inited once
256 * a second by the global_kbytes_per_second config-var
258 * as soon as global_kbytes_per_second gets below 0
259 * the connected conns are "offline" a little bit
261 * the problem:
262 * we somehow have to loose our "we are writable" signal
263 * on the way.
266 off_t *global_bytes_per_second_cnt_ptr; /* */
268 #if defined(__FreeBSD__) || defined(__NetBSD__) \
269 || defined(__OpenBSD__) || defined(__DragonFly__)
270 buffer *bsd_accept_filter;
271 #endif
273 } specific_config;
275 /* the order of the items should be the same as they are processed
276 * read before write as we use this later */
277 typedef enum {
278 CON_STATE_CONNECT,
279 CON_STATE_REQUEST_START,
280 CON_STATE_READ,
281 CON_STATE_REQUEST_END,
282 CON_STATE_READ_POST,
283 CON_STATE_HANDLE_REQUEST,
284 CON_STATE_RESPONSE_START,
285 CON_STATE_WRITE,
286 CON_STATE_RESPONSE_END,
287 CON_STATE_ERROR,
288 CON_STATE_CLOSE
289 } connection_state_t;
291 typedef enum {
292 /* condition not active at the moment because itself or some
293 * pre-condition depends on data not available yet
295 COND_RESULT_UNSET,
297 /* special "unset" for branches not selected due to pre-conditions
298 * not met (but pre-conditions are not "unset" anymore)
300 COND_RESULT_SKIP,
302 /* actually evaluated the condition itself */
303 COND_RESULT_FALSE, /* not active */
304 COND_RESULT_TRUE /* active */
305 } cond_result_t;
307 typedef struct {
308 /* current result (with preconditions) */
309 cond_result_t result;
310 /* result without preconditions (must never be "skip") */
311 cond_result_t local_result;
312 int patterncount;
313 int matches[3 * 10];
314 buffer *comp_value; /* just a pointer */
315 } cond_cache_t;
317 struct connection {
318 connection_state_t state;
320 /* timestamps */
321 time_t read_idle_ts;
322 time_t close_timeout_ts;
323 time_t write_request_ts;
325 time_t connection_start;
326 time_t request_start;
327 struct timespec request_start_hp;
329 size_t request_count; /* number of requests handled in this connection */
330 size_t loops_per_request; /* to catch endless loops in a single request
332 * used by mod_rewrite, mod_fastcgi, ... and others
333 * this is self-protection
336 int fd; /* the FD for this connection */
337 int fde_ndx; /* index for the fdevent-handler */
338 int ndx; /* reverse mapping to server->connection[ndx] */
340 /* fd states */
341 int is_readable;
342 int is_writable;
344 int keep_alive; /* only request.c can enable it, all other just disable */
345 int keep_alive_idle; /* remember max_keep_alive_idle from config */
347 int file_started;
348 int file_finished;
350 chunkqueue *write_queue; /* a large queue for low-level write ( HTTP response ) [ file, mem ] */
351 chunkqueue *read_queue; /* a small queue for low-level read ( HTTP request ) [ mem ] */
352 chunkqueue *request_content_queue; /* takes request-content into tempfile if necessary [ tempfile, mem ]*/
354 int traffic_limit_reached;
356 off_t bytes_written; /* used by mod_accesslog, mod_rrd */
357 off_t bytes_written_cur_second; /* used by mod_accesslog, mod_rrd */
358 off_t bytes_read; /* used by mod_accesslog, mod_rrd */
359 off_t bytes_header;
361 int http_status;
363 sock_addr dst_addr;
364 buffer *dst_addr_buf;
366 /* request */
367 buffer *parse_request;
368 unsigned int parsed_response; /* bitfield which contains the important header-fields of the parsed response header */
370 request request;
371 request_uri uri;
372 physical physical;
373 response response;
375 size_t header_len;
377 array *environment; /* used to pass lighttpd internal stuff to the FastCGI/CGI apps, setenv does that */
379 connection_type mode;
381 void **plugin_ctx; /* plugin connection specific config */
383 specific_config conf; /* global connection specific config */
384 cond_cache_t *cond_cache;
386 buffer *server_name;
387 buffer *proto;
389 /* error-handler */
390 int error_handler_saved_status;
391 http_method_t error_handler_saved_method;
393 struct server_socket *srv_socket; /* reference to the server-socket */
394 int (* network_write)(struct server *srv, struct connection *con, chunkqueue *cq, off_t max_bytes);
395 int (* network_read)(struct server *srv, struct connection *con, chunkqueue *cq, off_t max_bytes);
397 /* etag handling */
398 etag_flags_t etag_flags;
400 int conditional_is_valid[COMP_LAST_ELEMENT];
403 typedef struct {
404 connection **ptr;
405 size_t size;
406 size_t used;
407 } connections;
410 typedef struct {
411 buffer *uri;
412 time_t mtime;
413 int http_status;
414 } realpath_cache_type;
416 typedef struct {
417 time_t mtime; /* the key */
418 buffer *str; /* a buffer for the string represenation */
419 } mtime_cache_type;
421 typedef struct {
422 void *ptr;
423 size_t used;
424 size_t size;
425 } buffer_plugin;
427 typedef struct {
428 unsigned short port;
429 buffer *bindhost;
431 buffer *errorlog_file;
432 unsigned short errorlog_use_syslog;
433 buffer *breakagelog_file;
435 unsigned short dont_daemonize;
436 unsigned short preflight_check;
437 buffer *changeroot;
438 buffer *username;
439 buffer *groupname;
441 buffer *pid_file;
443 buffer *event_handler;
445 buffer *modules_dir;
446 buffer *network_backend;
447 array *modules;
448 array *upload_tempdirs;
449 unsigned int upload_temp_file_size;
450 unsigned int max_request_field_size;
452 unsigned short max_worker;
453 unsigned short max_fds;
454 unsigned short max_conns;
456 unsigned short log_request_header_on_error;
457 unsigned short log_state_handling;
459 int stat_cache_engine;
460 unsigned short enable_cores;
461 unsigned short reject_expect_100_with_417;
462 buffer *xattr_name;
464 unsigned short http_header_strict;
465 unsigned short http_host_strict;
466 unsigned short http_host_normalize;
467 unsigned short high_precision_timestamps;
468 time_t loadts;
469 double loadavg[3];
470 buffer *syslog_facility;
471 } server_config;
473 typedef struct server_socket {
474 sock_addr addr;
475 int fd;
476 int fde_ndx;
478 unsigned short is_ssl;
479 unsigned short sidx;
481 buffer *srv_token;
482 } server_socket;
484 typedef struct {
485 server_socket **ptr;
487 size_t size;
488 size_t used;
489 } server_socket_array;
491 struct server {
492 server_socket_array srv_sockets;
494 /* the errorlog */
495 int errorlog_fd;
496 enum { ERRORLOG_FILE, ERRORLOG_FD, ERRORLOG_SYSLOG, ERRORLOG_PIPE } errorlog_mode;
497 buffer *errorlog_buf;
499 struct fdevents *ev;
501 buffer_plugin plugins;
502 void *plugin_slots;
504 /* counters */
505 int con_opened;
506 int con_read;
507 int con_written;
508 int con_closed;
510 int max_fds; /* max possible fds */
511 int cur_fds; /* currently used fds */
512 int want_fds; /* waiting fds */
513 int sockets_disabled;
515 size_t max_conns;
517 /* buffers */
518 buffer *parse_full_path;
519 buffer *response_header;
520 buffer *response_range;
521 buffer *tmp_buf;
523 buffer *tmp_chunk_len;
525 buffer *empty_string; /* is necessary for cond_match */
527 buffer *cond_check_buf;
529 /* caches */
530 mtime_cache_type mtime_cache[FILE_CACHE_MAX];
532 array *split_vals;
534 /* Timestamps */
535 time_t cur_ts;
536 time_t last_generated_date_ts;
537 time_t last_generated_debug_ts;
538 time_t startup_ts;
540 buffer *ts_debug_str;
541 buffer *ts_date_str;
543 /* config-file */
544 array *config_touched;
546 array *config_context;
547 specific_config **config_storage;
549 server_config srvconf;
551 short int config_deprecated;
552 short int config_unsupported;
554 connections *conns;
555 connections *joblist;
556 connections *fdwaitqueue;
558 struct stat_cache *stat_cache;
561 * The status array can carry all the status information you want
562 * the key to the array is <module-prefix>.<name>
563 * and the values are counters
565 * example:
566 * fastcgi.backends = 10
567 * fastcgi.active-backends = 6
568 * fastcgi.backend.<key>.load = 24
569 * fastcgi.backend.<key>....
571 * fastcgi.backend.<key>.disconnects = ...
573 array *status;
575 int event_handler;
577 int (* network_backend_write)(struct server *srv, int fd, chunkqueue *cq, off_t max_bytes);
578 handler_t (* request_env)(struct server *srv, connection *con);
580 uid_t uid;
581 gid_t gid;
582 pid_t pid;
586 #endif