[mod_openssl] remove erroneous SSL_set_shutdown()
[lighttpd.git] / src / base.h
blob0ddae6eae656503b79617c062552d92f7a763358
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 "sys-socket.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 O_LARGEFILE
37 # define O_LARGEFILE 0
38 #endif
40 #ifndef SIZE_MAX
41 # ifdef SIZE_T_MAX
42 # define SIZE_MAX SIZE_T_MAX
43 # else
44 # define SIZE_MAX ((size_t)~0)
45 # endif
46 #endif
48 #ifndef SSIZE_MAX
49 # define SSIZE_MAX ((size_t)~0 >> 1)
50 #endif
52 #ifdef __APPLE__
53 #include <crt_externs.h>
54 #define environ (* _NSGetEnviron())
55 #else
56 extern char **environ;
57 #endif
59 /* for solaris 2.5 and NetBSD 1.3.x */
60 #ifndef HAVE_SOCKLEN_T
61 typedef int socklen_t;
62 #endif
64 /* solaris and NetBSD 1.3.x again */
65 #if (!defined(HAVE_STDINT_H)) && (!defined(HAVE_INTTYPES_H)) && (!defined(uint32_t))
66 # define uint32_t u_int32_t
67 #endif
70 #ifndef SHUT_WR
71 # define SHUT_WR 1
72 #endif
74 typedef enum { T_CONFIG_UNSET,
75 T_CONFIG_STRING,
76 T_CONFIG_SHORT,
77 T_CONFIG_INT,
78 T_CONFIG_BOOLEAN,
79 T_CONFIG_ARRAY,
80 T_CONFIG_LOCAL,
81 T_CONFIG_DEPRECATED,
82 T_CONFIG_UNSUPPORTED
83 } config_values_type_t;
85 typedef enum { T_CONFIG_SCOPE_UNSET,
86 T_CONFIG_SCOPE_SERVER,
87 T_CONFIG_SCOPE_CONNECTION
88 } config_scope_type_t;
90 typedef struct {
91 const char *key;
92 void *destination;
94 config_values_type_t type;
95 config_scope_type_t scope;
96 } config_values_t;
98 typedef enum { DIRECT, EXTERNAL } connection_type;
100 typedef struct {
101 char *key;
102 connection_type type;
103 char *value;
104 } request_handler;
107 union sock_addr {
108 #ifdef HAVE_IPV6
109 struct sockaddr_in6 ipv6;
110 #endif
111 struct sockaddr_in ipv4;
112 #ifdef HAVE_SYS_UN_H
113 struct sockaddr_un un;
114 #endif
115 struct sockaddr plain;
118 /* fcgi_response_header contains ... */
119 #define HTTP_STATUS BV(0)
120 #define HTTP_CONNECTION BV(1)
121 #define HTTP_CONTENT_LENGTH BV(2)
122 #define HTTP_DATE BV(3)
123 #define HTTP_LOCATION BV(4)
124 #define HTTP_TRANSFER_ENCODING BV(5)
125 #define HTTP_CONTENT_LOCATION BV(6)
126 #define HTTP_SET_COOKIE BV(7)
127 #define HTTP_UPGRADE BV(8)
129 typedef struct {
130 /** HEADER */
131 /* the request-line */
132 buffer *request;
133 buffer *uri;
135 buffer *orig_uri;
137 http_method_t http_method;
138 http_version_t http_version;
140 buffer *request_line;
142 /* strings to the header */
143 buffer *http_host; /* not alloced */
144 const char *http_range;
145 const char *http_content_type;
146 const char *http_if_modified_since;
147 const char *http_if_none_match;
149 array *headers;
151 /* CONTENT */
152 off_t content_length; /* returned by strtoll() */
153 off_t te_chunked;
155 /* internal representation */
156 int accept_encoding;
158 /* internal */
159 buffer *pathinfo;
160 } request;
162 typedef struct {
163 off_t content_length;
164 int keep_alive; /* used by the subrequests in proxy, cgi and fcgi to say the subrequest was keep-alive or not */
166 array *headers;
168 enum {
169 HTTP_TRANSFER_ENCODING_IDENTITY, HTTP_TRANSFER_ENCODING_CHUNKED
170 } transfer_encoding;
171 } response;
173 typedef struct {
174 buffer *scheme; /* scheme without colon or slashes ( "http" or "https" ) */
176 /* authority with optional portnumber ("site.name" or "site.name:8080" ) NOTE: without "username:password@" */
177 buffer *authority;
179 /* path including leading slash ("/" or "/index.html") - urldecoded, and sanitized ( buffer_path_simplify() && buffer_urldecode_path() ) */
180 buffer *path;
181 buffer *path_raw; /* raw path, as sent from client. no urldecoding or path simplifying */
182 buffer *query; /* querystring ( everything after "?", ie: in "/index.php?foo=1", query is "foo=1" ) */
183 } request_uri;
185 typedef struct {
186 buffer *path;
187 buffer *basedir; /* path = "(basedir)(.*)" */
189 buffer *doc_root; /* path = doc_root + rel_path */
190 buffer *rel_path;
192 buffer *etag;
193 } physical;
195 typedef struct {
196 buffer *name;
197 buffer *etag;
199 struct stat st;
201 time_t stat_ts;
203 #ifdef HAVE_LSTAT
204 char is_symlink;
205 #endif
207 #ifdef HAVE_FAM_H
208 int dir_version;
209 #endif
211 buffer *content_type;
212 } stat_cache_entry;
214 typedef struct {
215 array *mimetypes;
217 /* virtual-servers */
218 buffer *document_root;
219 buffer *server_name;
220 buffer *error_handler;
221 buffer *error_handler_404;
222 buffer *server_tag;
223 buffer *dirlist_encoding;
224 buffer *errorfile_prefix;
225 buffer *socket_perms;
227 unsigned short high_precision_timestamps;
228 unsigned short max_keep_alive_requests;
229 unsigned short max_keep_alive_idle;
230 unsigned short max_read_idle;
231 unsigned short max_write_idle;
232 unsigned short use_xattr;
233 unsigned short follow_symlink;
234 unsigned short range_requests;
235 unsigned short stream_request_body;
236 unsigned short stream_response_body;
237 unsigned short error_intercept;
239 /* debug */
241 unsigned short log_file_not_found;
242 unsigned short log_request_header;
243 unsigned short log_request_handling;
244 unsigned short log_response_header;
245 unsigned short log_condition_handling;
246 unsigned short log_timeouts;
249 /* server wide */
250 unsigned short use_ipv6, set_v6only; /* set_v6only is only a temporary option */
251 unsigned short defer_accept;
252 unsigned short ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */
253 unsigned short allow_http11;
254 unsigned short etag_use_inode;
255 unsigned short etag_use_mtime;
256 unsigned short etag_use_size;
257 unsigned short force_lowercase_filenames; /* if the FS is case-insensitive, force all files to lower-case */
258 unsigned int http_parseopts;
259 unsigned int max_request_size;
260 int listen_backlog;
262 unsigned short kbytes_per_second; /* connection kb/s limit */
264 /* configside */
265 unsigned short global_kbytes_per_second; /* */
267 off_t global_bytes_per_second_cnt;
268 /* server-wide traffic-shaper
270 * each context has the counter which is inited once
271 * a second by the global_kbytes_per_second config-var
273 * as soon as global_kbytes_per_second gets below 0
274 * the connected conns are "offline" a little bit
276 * the problem:
277 * we somehow have to loose our "we are writable" signal
278 * on the way.
281 off_t *global_bytes_per_second_cnt_ptr; /* */
283 #if defined(__FreeBSD__) || defined(__NetBSD__) \
284 || defined(__OpenBSD__) || defined(__DragonFly__)
285 buffer *bsd_accept_filter;
286 #endif
288 } specific_config;
290 /* the order of the items should be the same as they are processed
291 * read before write as we use this later */
292 typedef enum {
293 CON_STATE_CONNECT,
294 CON_STATE_REQUEST_START,
295 CON_STATE_READ,
296 CON_STATE_REQUEST_END,
297 CON_STATE_READ_POST,
298 CON_STATE_HANDLE_REQUEST,
299 CON_STATE_RESPONSE_START,
300 CON_STATE_WRITE,
301 CON_STATE_RESPONSE_END,
302 CON_STATE_ERROR,
303 CON_STATE_CLOSE
304 } connection_state_t;
306 typedef enum {
307 /* condition not active at the moment because itself or some
308 * pre-condition depends on data not available yet
310 COND_RESULT_UNSET,
312 /* special "unset" for branches not selected due to pre-conditions
313 * not met (but pre-conditions are not "unset" anymore)
315 COND_RESULT_SKIP,
317 /* actually evaluated the condition itself */
318 COND_RESULT_FALSE, /* not active */
319 COND_RESULT_TRUE, /* active */
320 } cond_result_t;
322 typedef struct {
323 /* current result (with preconditions) */
324 cond_result_t result;
325 /* result without preconditions (must never be "skip") */
326 cond_result_t local_result;
327 int patterncount;
328 int matches[3 * 10];
329 buffer *comp_value; /* just a pointer */
330 } cond_cache_t;
332 struct connection {
333 connection_state_t state;
335 /* timestamps */
336 time_t read_idle_ts;
337 time_t close_timeout_ts;
338 time_t write_request_ts;
340 time_t connection_start;
341 time_t request_start;
342 struct timespec request_start_hp;
344 size_t request_count; /* number of requests handled in this connection */
345 size_t loops_per_request; /* to catch endless loops in a single request
347 * used by mod_rewrite, mod_fastcgi, ... and others
348 * this is self-protection
351 int fd; /* the FD for this connection */
352 int fde_ndx; /* index for the fdevent-handler */
353 int ndx; /* reverse mapping to server->connection[ndx] */
355 /* fd states */
356 int is_readable;
357 int is_writable;
359 int keep_alive; /* only request.c can enable it, all other just disable */
360 int keep_alive_idle; /* remember max_keep_alive_idle from config */
362 int file_started;
363 int file_finished;
365 chunkqueue *write_queue; /* a large queue for low-level write ( HTTP response ) [ file, mem ] */
366 chunkqueue *read_queue; /* a small queue for low-level read ( HTTP request ) [ mem ] */
367 chunkqueue *request_content_queue; /* takes request-content into tempfile if necessary [ tempfile, mem ]*/
369 int traffic_limit_reached;
371 off_t bytes_written; /* used by mod_accesslog, mod_rrd */
372 off_t bytes_written_cur_second; /* used by mod_accesslog, mod_rrd */
373 off_t bytes_read; /* used by mod_accesslog, mod_rrd */
374 off_t bytes_header;
376 int http_status;
378 sock_addr dst_addr;
379 buffer *dst_addr_buf;
381 /* request */
382 buffer *parse_request;
383 unsigned int parsed_response; /* bitfield which contains the important header-fields of the parsed response header */
385 request request;
386 request_uri uri;
387 physical physical;
388 response response;
390 size_t header_len;
392 array *environment; /* used to pass lighttpd internal stuff to the FastCGI/CGI apps, setenv does that */
394 connection_type mode;
396 void **plugin_ctx; /* plugin connection specific config */
398 specific_config conf; /* global connection specific config */
399 cond_cache_t *cond_cache;
401 buffer *server_name;
402 buffer *proto;
404 /* error-handler */
405 int error_handler_saved_status;
406 http_method_t error_handler_saved_method;
408 struct server_socket *srv_socket; /* reference to the server-socket */
409 int (* network_write)(struct server *srv, struct connection *con, chunkqueue *cq, off_t max_bytes);
410 int (* network_read)(struct server *srv, struct connection *con, chunkqueue *cq, off_t max_bytes);
412 /* etag handling */
413 etag_flags_t etag_flags;
415 int conditional_is_valid[COMP_LAST_ELEMENT];
418 typedef struct {
419 connection **ptr;
420 size_t size;
421 size_t used;
422 } connections;
425 typedef struct {
426 buffer *uri;
427 time_t mtime;
428 int http_status;
429 } realpath_cache_type;
431 typedef struct {
432 time_t mtime; /* the key */
433 buffer *str; /* a buffer for the string represenation */
434 } mtime_cache_type;
436 typedef struct {
437 void *ptr;
438 size_t used;
439 size_t size;
440 } buffer_plugin;
442 typedef struct {
443 unsigned short port;
444 buffer *bindhost;
446 buffer *errorlog_file;
447 unsigned short errorlog_use_syslog;
448 buffer *breakagelog_file;
450 unsigned short dont_daemonize;
451 unsigned short preflight_check;
452 buffer *changeroot;
453 buffer *username;
454 buffer *groupname;
456 buffer *pid_file;
458 buffer *event_handler;
460 buffer *modules_dir;
461 buffer *network_backend;
462 array *modules;
463 array *upload_tempdirs;
464 unsigned int upload_temp_file_size;
465 unsigned int max_request_field_size;
467 unsigned short max_worker;
468 unsigned short max_fds;
469 unsigned short max_conns;
471 unsigned short log_request_header_on_error;
472 unsigned short log_state_handling;
474 enum { STAT_CACHE_ENGINE_UNSET,
475 STAT_CACHE_ENGINE_NONE,
476 STAT_CACHE_ENGINE_SIMPLE
477 #ifdef HAVE_FAM_H
478 , STAT_CACHE_ENGINE_FAM
479 #endif
480 } stat_cache_engine;
481 unsigned short enable_cores;
482 unsigned short reject_expect_100_with_417;
483 buffer *xattr_name;
485 unsigned short http_header_strict;
486 unsigned short http_host_strict;
487 unsigned short http_host_normalize;
488 unsigned short high_precision_timestamps;
489 time_t loadts;
490 double loadavg[3];
491 buffer *syslog_facility;
492 } server_config;
494 typedef struct server_socket {
495 sock_addr addr;
496 int fd;
497 int fde_ndx;
499 unsigned short is_ssl;
500 unsigned short sidx;
502 buffer *srv_token;
503 } server_socket;
505 typedef struct {
506 server_socket **ptr;
508 size_t size;
509 size_t used;
510 } server_socket_array;
512 struct server {
513 server_socket_array srv_sockets;
515 /* the errorlog */
516 int errorlog_fd;
517 enum { ERRORLOG_FILE, ERRORLOG_FD, ERRORLOG_SYSLOG, ERRORLOG_PIPE } errorlog_mode;
518 buffer *errorlog_buf;
520 struct fdevents *ev;
522 buffer_plugin plugins;
523 void *plugin_slots;
525 /* counters */
526 int con_opened;
527 int con_read;
528 int con_written;
529 int con_closed;
531 int max_fds; /* max possible fds */
532 int cur_fds; /* currently used fds */
533 int want_fds; /* waiting fds */
534 int sockets_disabled;
536 size_t max_conns;
538 /* buffers */
539 buffer *parse_full_path;
540 buffer *response_header;
541 buffer *response_range;
542 buffer *tmp_buf;
544 buffer *tmp_chunk_len;
546 buffer *empty_string; /* is necessary for cond_match */
548 buffer *cond_check_buf;
550 /* caches */
551 mtime_cache_type mtime_cache[FILE_CACHE_MAX];
553 array *split_vals;
555 /* Timestamps */
556 time_t cur_ts;
557 time_t last_generated_date_ts;
558 time_t last_generated_debug_ts;
559 time_t startup_ts;
561 buffer *ts_debug_str;
562 buffer *ts_date_str;
564 /* config-file */
565 array *config_touched;
567 array *config_context;
568 specific_config **config_storage;
570 server_config srvconf;
572 short int config_deprecated;
573 short int config_unsupported;
575 connections *conns;
576 connections *joblist;
577 connections *fdwaitqueue;
579 struct stat_cache *stat_cache;
582 * The status array can carry all the status information you want
583 * the key to the array is <module-prefix>.<name>
584 * and the values are counters
586 * example:
587 * fastcgi.backends = 10
588 * fastcgi.active-backends = 6
589 * fastcgi.backend.<key>.load = 24
590 * fastcgi.backend.<key>....
592 * fastcgi.backend.<key>.disconnects = ...
594 array *status;
596 int event_handler;
598 int (* network_backend_write)(struct server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes);
599 handler_t (* request_env)(struct server *srv, connection *con);
601 uid_t uid;
602 gid_t gid;
603 pid_t pid;
607 #endif