File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / lighttpd / src / base.h
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jun 15 20:20:06 2014 UTC (10 years ago) by misho
Branches: lighttpd, MAIN
CVS tags: v1_4_35p0, v1_4_35, HEAD
lighttpd 1.4.35

    1: #ifndef _BASE_H_
    2: #define _BASE_H_
    3: 
    4: #ifdef HAVE_CONFIG_H
    5: # include "config.h"
    6: #endif
    7: #include "settings.h"
    8: 
    9: #include <sys/types.h>
   10: #include <sys/time.h>
   11: #include <sys/stat.h>
   12: 
   13: #include <limits.h>
   14: 
   15: #ifdef HAVE_STDINT_H
   16: # include <stdint.h>
   17: #endif
   18: 
   19: #ifdef HAVE_INTTYPES_H
   20: # include <inttypes.h>
   21: #endif
   22: 
   23: #include "buffer.h"
   24: #include "array.h"
   25: #include "chunk.h"
   26: #include "keyvalue.h"
   27: #include "fdevent.h"
   28: #include "sys-socket.h"
   29: #include "splaytree.h"
   30: #include "etag.h"
   31: 
   32: 
   33: #if defined HAVE_LIBSSL && defined HAVE_OPENSSL_SSL_H
   34: # define USE_OPENSSL
   35: # include <openssl/ssl.h>
   36: # if ! defined OPENSSL_NO_TLSEXT && ! defined SSL_CTRL_SET_TLSEXT_HOSTNAME
   37: #  define OPENSSL_NO_TLSEXT
   38: # endif
   39: #endif
   40: 
   41: #ifdef HAVE_FAM_H
   42: # include <fam.h>
   43: #endif
   44: 
   45: #ifndef O_BINARY
   46: # define O_BINARY 0
   47: #endif
   48: 
   49: #ifndef O_LARGEFILE
   50: # define O_LARGEFILE 0
   51: #endif
   52: 
   53: #ifndef SIZE_MAX
   54: # ifdef SIZE_T_MAX
   55: #  define SIZE_MAX SIZE_T_MAX
   56: # else
   57: #  define SIZE_MAX ((size_t)~0)
   58: # endif
   59: #endif
   60: 
   61: #ifndef SSIZE_MAX
   62: # define SSIZE_MAX ((size_t)~0 >> 1)
   63: #endif
   64: 
   65: #ifdef __APPLE__
   66: #include <crt_externs.h>
   67: #define environ (* _NSGetEnviron())
   68: #else
   69: extern char **environ;
   70: #endif
   71: 
   72: /* for solaris 2.5 and NetBSD 1.3.x */
   73: #ifndef HAVE_SOCKLEN_T
   74: typedef int socklen_t;
   75: #endif
   76: 
   77: /* solaris and NetBSD 1.3.x again */
   78: #if (!defined(HAVE_STDINT_H)) && (!defined(HAVE_INTTYPES_H)) && (!defined(uint32_t))
   79: # define uint32_t u_int32_t
   80: #endif
   81: 
   82: 
   83: #ifndef SHUT_WR
   84: # define SHUT_WR 1
   85: #endif
   86: 
   87: typedef enum { T_CONFIG_UNSET,
   88: 		T_CONFIG_STRING,
   89: 		T_CONFIG_SHORT,
   90: 		T_CONFIG_INT,
   91: 		T_CONFIG_BOOLEAN,
   92: 		T_CONFIG_ARRAY,
   93: 		T_CONFIG_LOCAL,
   94: 		T_CONFIG_DEPRECATED,
   95: 		T_CONFIG_UNSUPPORTED
   96: } config_values_type_t;
   97: 
   98: typedef enum { T_CONFIG_SCOPE_UNSET,
   99: 		T_CONFIG_SCOPE_SERVER,
  100: 		T_CONFIG_SCOPE_CONNECTION
  101: } config_scope_type_t;
  102: 
  103: typedef struct {
  104: 	const char *key;
  105: 	void *destination;
  106: 
  107: 	config_values_type_t type;
  108: 	config_scope_type_t scope;
  109: } config_values_t;
  110: 
  111: typedef enum { DIRECT, EXTERNAL } connection_type;
  112: 
  113: typedef struct {
  114: 	char *key;
  115: 	connection_type type;
  116: 	char *value;
  117: } request_handler;
  118: 
  119: typedef struct {
  120: 	char *key;
  121: 	char *host;
  122: 	unsigned short port;
  123: 	int used;
  124: 	short factor;
  125: } fcgi_connections;
  126: 
  127: 
  128: typedef union {
  129: #ifdef HAVE_IPV6
  130: 	struct sockaddr_in6 ipv6;
  131: #endif
  132: 	struct sockaddr_in ipv4;
  133: #ifdef HAVE_SYS_UN_H
  134: 	struct sockaddr_un un;
  135: #endif
  136: 	struct sockaddr plain;
  137: } sock_addr;
  138: 
  139: /* fcgi_response_header contains ... */
  140: #define HTTP_STATUS         BV(0)
  141: #define HTTP_CONNECTION     BV(1)
  142: #define HTTP_CONTENT_LENGTH BV(2)
  143: #define HTTP_DATE           BV(3)
  144: #define HTTP_LOCATION       BV(4)
  145: 
  146: typedef struct {
  147: 	/** HEADER */
  148: 	/* the request-line */
  149: 	buffer *request;
  150: 	buffer *uri;
  151: 
  152: 	buffer *orig_uri;
  153: 
  154: 	http_method_t  http_method;
  155: 	http_version_t http_version;
  156: 
  157: 	buffer *request_line;
  158: 
  159: 	/* strings to the header */
  160: 	buffer *http_host; /* not alloced */
  161: 	const char   *http_range;
  162: 	const char   *http_content_type;
  163: 	const char   *http_if_modified_since;
  164: 	const char   *http_if_none_match;
  165: 
  166: 	array  *headers;
  167: 
  168: 	/* CONTENT */
  169: 	size_t content_length; /* returned by strtoul() */
  170: 
  171: 	/* internal representation */
  172: 	int     accept_encoding;
  173: 
  174: 	/* internal */
  175: 	buffer *pathinfo;
  176: } request;
  177: 
  178: typedef struct {
  179: 	off_t   content_length;
  180: 	int     keep_alive;               /* used by  the subrequests in proxy, cgi and fcgi to say the subrequest was keep-alive or not */
  181: 
  182: 	array  *headers;
  183: 
  184: 	enum {
  185: 		HTTP_TRANSFER_ENCODING_IDENTITY, HTTP_TRANSFER_ENCODING_CHUNKED
  186: 	} transfer_encoding;
  187: } response;
  188: 
  189: typedef struct {
  190: 	buffer *scheme; /* scheme without colon or slashes ( "http" or "https" ) */
  191: 
  192: 	/* authority with optional portnumber ("site.name" or "site.name:8080" ) NOTE: without "username:password@" */
  193: 	buffer *authority;
  194: 
  195: 	/* path including leading slash ("/" or "/index.html") - urldecoded, and sanitized  ( buffer_path_simplify() && buffer_urldecode_path() ) */
  196: 	buffer *path;
  197: 	buffer *path_raw; /* raw path, as sent from client. no urldecoding or path simplifying */
  198: 	buffer *query; /* querystring ( everything after "?", ie: in "/index.php?foo=1", query is "foo=1" ) */
  199: } request_uri;
  200: 
  201: typedef struct {
  202: 	buffer *path;
  203: 	buffer *basedir; /* path = "(basedir)(.*)" */
  204: 
  205: 	buffer *doc_root; /* path = doc_root + rel_path */
  206: 	buffer *rel_path;
  207: 
  208: 	buffer *etag;
  209: } physical;
  210: 
  211: typedef struct {
  212: 	buffer *name;
  213: 	buffer *etag;
  214: 
  215: 	struct stat st;
  216: 
  217: 	time_t stat_ts;
  218: 
  219: #ifdef HAVE_LSTAT
  220: 	char is_symlink;
  221: #endif
  222: 
  223: #ifdef HAVE_FAM_H
  224: 	int    dir_version;
  225: #endif
  226: 
  227: 	buffer *content_type;
  228: } stat_cache_entry;
  229: 
  230: typedef struct {
  231: 	splay_tree *files; /* the nodes of the tree are stat_cache_entry's */
  232: 
  233: 	buffer *dir_name; /* for building the dirname from the filename */
  234: #ifdef HAVE_FAM_H
  235: 	splay_tree *dirs; /* the nodes of the tree are fam_dir_entry */
  236: 
  237: 	FAMConnection fam;
  238: 	int    fam_fcce_ndx;
  239: #endif
  240: 	buffer *hash_key;  /* temp-store for the hash-key */
  241: } stat_cache;
  242: 
  243: typedef struct {
  244: 	array *mimetypes;
  245: 
  246: 	/* virtual-servers */
  247: 	buffer *document_root;
  248: 	buffer *server_name;
  249: 	buffer *error_handler;
  250: 	buffer *server_tag;
  251: 	buffer *dirlist_encoding;
  252: 	buffer *errorfile_prefix;
  253: 
  254: 	unsigned short max_keep_alive_requests;
  255: 	unsigned short max_keep_alive_idle;
  256: 	unsigned short max_read_idle;
  257: 	unsigned short max_write_idle;
  258: 	unsigned short use_xattr;
  259: 	unsigned short follow_symlink;
  260: 	unsigned short range_requests;
  261: 
  262: 	/* debug */
  263: 
  264: 	unsigned short log_file_not_found;
  265: 	unsigned short log_request_header;
  266: 	unsigned short log_request_handling;
  267: 	unsigned short log_response_header;
  268: 	unsigned short log_condition_handling;
  269: 	unsigned short log_ssl_noise;
  270: 	unsigned short log_timeouts;
  271: 
  272: 
  273: 	/* server wide */
  274: 	buffer *ssl_pemfile;
  275: 	buffer *ssl_ca_file;
  276: 	buffer *ssl_cipher_list;
  277: 	buffer *ssl_dh_file;
  278: 	buffer *ssl_ec_curve;
  279: 	unsigned short ssl_honor_cipher_order; /* determine SSL cipher in server-preferred order, not client-order */
  280: 	unsigned short ssl_empty_fragments; /* whether to not set SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS */
  281: 	unsigned short ssl_use_sslv2;
  282: 	unsigned short ssl_use_sslv3;
  283: 	unsigned short ssl_verifyclient;
  284: 	unsigned short ssl_verifyclient_enforce;
  285: 	unsigned short ssl_verifyclient_depth;
  286: 	buffer *ssl_verifyclient_username;
  287: 	unsigned short ssl_verifyclient_export_cert;
  288: 	unsigned short ssl_disable_client_renegotiation;
  289: 
  290: 	unsigned short use_ipv6, set_v6only; /* set_v6only is only a temporary option */
  291: 	unsigned short defer_accept;
  292: 	unsigned short ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */
  293: 	unsigned short allow_http11;
  294: 	unsigned short etag_use_inode;
  295: 	unsigned short etag_use_mtime;
  296: 	unsigned short etag_use_size;
  297: 	unsigned short force_lowercase_filenames; /* if the FS is case-insensitive, force all files to lower-case */
  298: 	unsigned int max_request_size;
  299: 
  300: 	unsigned short kbytes_per_second; /* connection kb/s limit */
  301: 
  302: 	/* configside */
  303: 	unsigned short global_kbytes_per_second; /*  */
  304: 
  305: 	off_t  global_bytes_per_second_cnt;
  306: 	/* server-wide traffic-shaper
  307: 	 *
  308: 	 * each context has the counter which is inited once
  309: 	 * a second by the global_kbytes_per_second config-var
  310: 	 *
  311: 	 * as soon as global_kbytes_per_second gets below 0
  312: 	 * the connected conns are "offline" a little bit
  313: 	 *
  314: 	 * the problem:
  315: 	 * we somehow have to loose our "we are writable" signal
  316: 	 * on the way.
  317: 	 *
  318: 	 */
  319: 	off_t *global_bytes_per_second_cnt_ptr; /*  */
  320: 
  321: #ifdef USE_OPENSSL
  322: 	SSL_CTX *ssl_ctx; /* not patched */
  323: 	/* SNI per host: with COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */
  324: 	EVP_PKEY *ssl_pemfile_pkey;
  325: 	X509 *ssl_pemfile_x509;
  326: 	STACK_OF(X509_NAME) *ssl_ca_file_cert_names;
  327: #endif
  328: } specific_config;
  329: 
  330: /* the order of the items should be the same as they are processed
  331:  * read before write as we use this later */
  332: typedef enum {
  333: 	CON_STATE_CONNECT,
  334: 	CON_STATE_REQUEST_START,
  335: 	CON_STATE_READ,
  336: 	CON_STATE_REQUEST_END,
  337: 	CON_STATE_READ_POST,
  338: 	CON_STATE_HANDLE_REQUEST,
  339: 	CON_STATE_RESPONSE_START,
  340: 	CON_STATE_WRITE,
  341: 	CON_STATE_RESPONSE_END,
  342: 	CON_STATE_ERROR,
  343: 	CON_STATE_CLOSE
  344: } connection_state_t;
  345: 
  346: typedef enum { COND_RESULT_UNSET, COND_RESULT_FALSE, COND_RESULT_TRUE } cond_result_t;
  347: typedef struct {
  348: 	cond_result_t result;
  349: 	int patterncount;
  350: 	int matches[3 * 10];
  351: 	buffer *comp_value; /* just a pointer */
  352: 	
  353: 	comp_key_t comp_type;
  354: } cond_cache_t;
  355: 
  356: typedef struct {
  357: 	connection_state_t state;
  358: 
  359: 	/* timestamps */
  360: 	time_t read_idle_ts;
  361: 	time_t close_timeout_ts;
  362: 	time_t write_request_ts;
  363: 
  364: 	time_t connection_start;
  365: 	time_t request_start;
  366: 
  367: 	struct timeval start_tv;
  368: 
  369: 	size_t request_count;        /* number of requests handled in this connection */
  370: 	size_t loops_per_request;    /* to catch endless loops in a single request
  371: 				      *
  372: 				      * used by mod_rewrite, mod_fastcgi, ... and others
  373: 				      * this is self-protection
  374: 				      */
  375: 
  376: 	int fd;                      /* the FD for this connection */
  377: 	int fde_ndx;                 /* index for the fdevent-handler */
  378: 	int ndx;                     /* reverse mapping to server->connection[ndx] */
  379: 
  380: 	/* fd states */
  381: 	int is_readable;
  382: 	int is_writable;
  383: 
  384: 	int keep_alive;              /* only request.c can enable it, all other just disable */
  385: 	int keep_alive_idle;         /* remember max_keep_alive_idle from config */
  386: 
  387: 	int file_started;
  388: 	int file_finished;
  389: 
  390: 	chunkqueue *write_queue;      /* a large queue for low-level write ( HTTP response ) [ file, mem ] */
  391: 	chunkqueue *read_queue;       /* a small queue for low-level read ( HTTP request ) [ mem ] */
  392: 	chunkqueue *request_content_queue; /* takes request-content into tempfile if necessary [ tempfile, mem ]*/
  393: 
  394: 	int traffic_limit_reached;
  395: 
  396: 	off_t bytes_written;          /* used by mod_accesslog, mod_rrd */
  397: 	off_t bytes_written_cur_second; /* used by mod_accesslog, mod_rrd */
  398: 	off_t bytes_read;             /* used by mod_accesslog, mod_rrd */
  399: 	off_t bytes_header;
  400: 
  401: 	int http_status;
  402: 
  403: 	sock_addr dst_addr;
  404: 	buffer *dst_addr_buf;
  405: 
  406: 	/* request */
  407: 	buffer *parse_request;
  408: 	unsigned int parsed_response; /* bitfield which contains the important header-fields of the parsed response header */
  409: 
  410: 	request  request;
  411: 	request_uri uri;
  412: 	physical physical;
  413: 	response response;
  414: 
  415: 	size_t header_len;
  416: 
  417: 	array  *environment; /* used to pass lighttpd internal stuff to the FastCGI/CGI apps, setenv does that */
  418: 
  419: 	/* response */
  420: 	int    got_response;
  421: 
  422: 	int    in_joblist;
  423: 
  424: 	connection_type mode;
  425: 
  426: 	void **plugin_ctx;           /* plugin connection specific config */
  427: 
  428: 	specific_config conf;        /* global connection specific config */
  429: 	cond_cache_t *cond_cache;
  430: 
  431: 	buffer *server_name;
  432: 
  433: 	/* error-handler */
  434: 	buffer *error_handler;
  435: 	int error_handler_saved_status;
  436: 	int in_error_handler;
  437: 
  438: 	struct server_socket *srv_socket;   /* reference to the server-socket */
  439: 
  440: #ifdef USE_OPENSSL
  441: 	SSL *ssl;
  442: # ifndef OPENSSL_NO_TLSEXT
  443: 	buffer *tlsext_server_name;
  444: # endif
  445: 	unsigned int renegotiations; /* count of SSL_CB_HANDSHAKE_START */
  446: #endif
  447: 	/* etag handling */
  448: 	etag_flags_t etag_flags;
  449: 
  450: 	int conditional_is_valid[COMP_LAST_ELEMENT]; 
  451: } connection;
  452: 
  453: typedef struct {
  454: 	connection **ptr;
  455: 	size_t size;
  456: 	size_t used;
  457: } connections;
  458: 
  459: 
  460: #ifdef HAVE_IPV6
  461: typedef struct {
  462: 	int family;
  463: 	union {
  464: 		struct in6_addr ipv6;
  465: 		struct in_addr  ipv4;
  466: 	} addr;
  467: 	char b2[INET6_ADDRSTRLEN + 1];
  468: 	time_t ts;
  469: } inet_ntop_cache_type;
  470: #endif
  471: 
  472: 
  473: typedef struct {
  474: 	buffer *uri;
  475: 	time_t mtime;
  476: 	int http_status;
  477: } realpath_cache_type;
  478: 
  479: typedef struct {
  480: 	time_t  mtime;  /* the key */
  481: 	buffer *str;    /* a buffer for the string represenation */
  482: } mtime_cache_type;
  483: 
  484: typedef struct {
  485: 	void  *ptr;
  486: 	size_t used;
  487: 	size_t size;
  488: } buffer_plugin;
  489: 
  490: typedef struct {
  491: 	unsigned short port;
  492: 	buffer *bindhost;
  493: 
  494: 	buffer *errorlog_file;
  495: 	unsigned short errorlog_use_syslog;
  496: 	buffer *breakagelog_file;
  497: 
  498: 	unsigned short dont_daemonize;
  499: 	buffer *changeroot;
  500: 	buffer *username;
  501: 	buffer *groupname;
  502: 
  503: 	buffer *pid_file;
  504: 
  505: 	buffer *event_handler;
  506: 
  507: 	buffer *modules_dir;
  508: 	buffer *network_backend;
  509: 	array *modules;
  510: 	array *upload_tempdirs;
  511: 
  512: 	unsigned short max_worker;
  513: 	unsigned short max_fds;
  514: 	unsigned short max_conns;
  515: 	unsigned int max_request_size;
  516: 
  517: 	unsigned short log_request_header_on_error;
  518: 	unsigned short log_state_handling;
  519: 
  520: 	enum { STAT_CACHE_ENGINE_UNSET,
  521: 			STAT_CACHE_ENGINE_NONE,
  522: 			STAT_CACHE_ENGINE_SIMPLE
  523: #ifdef HAVE_FAM_H
  524: 			, STAT_CACHE_ENGINE_FAM
  525: #endif
  526: 	} stat_cache_engine;
  527: 	unsigned short enable_cores;
  528: 	unsigned short reject_expect_100_with_417;
  529: } server_config;
  530: 
  531: typedef struct server_socket {
  532: 	sock_addr addr;
  533: 	int       fd;
  534: 	int       fde_ndx;
  535: 
  536: 	unsigned short is_ssl;
  537: 
  538: 	buffer *srv_token;
  539: 
  540: #ifdef USE_OPENSSL
  541: 	SSL_CTX *ssl_ctx;
  542: #endif
  543: } server_socket;
  544: 
  545: typedef struct {
  546: 	server_socket **ptr;
  547: 
  548: 	size_t size;
  549: 	size_t used;
  550: } server_socket_array;
  551: 
  552: typedef struct server {
  553: 	server_socket_array srv_sockets;
  554: 
  555: 	/* the errorlog */
  556: 	int errorlog_fd;
  557: 	enum { ERRORLOG_FILE, ERRORLOG_FD, ERRORLOG_SYSLOG, ERRORLOG_PIPE } errorlog_mode;
  558: 	buffer *errorlog_buf;
  559: 
  560: 	fdevents *ev, *ev_ins;
  561: 
  562: 	buffer_plugin plugins;
  563: 	void *plugin_slots;
  564: 
  565: 	/* counters */
  566: 	int con_opened;
  567: 	int con_read;
  568: 	int con_written;
  569: 	int con_closed;
  570: 
  571: 	int ssl_is_init;
  572: 
  573: 	int max_fds;    /* max possible fds */
  574: 	int cur_fds;    /* currently used fds */
  575: 	int want_fds;   /* waiting fds */
  576: 	int sockets_disabled;
  577: 
  578: 	size_t max_conns;
  579: 
  580: 	/* buffers */
  581: 	buffer *parse_full_path;
  582: 	buffer *response_header;
  583: 	buffer *response_range;
  584: 	buffer *tmp_buf;
  585: 
  586: 	buffer *tmp_chunk_len;
  587: 
  588: 	buffer *empty_string; /* is necessary for cond_match */
  589: 
  590: 	buffer *cond_check_buf;
  591: 
  592: 	/* caches */
  593: #ifdef HAVE_IPV6
  594: 	inet_ntop_cache_type inet_ntop_cache[INET_NTOP_CACHE_MAX];
  595: #endif
  596: 	mtime_cache_type mtime_cache[FILE_CACHE_MAX];
  597: 
  598: 	array *split_vals;
  599: 
  600: 	/* Timestamps */
  601: 	time_t cur_ts;
  602: 	time_t last_generated_date_ts;
  603: 	time_t last_generated_debug_ts;
  604: 	time_t startup_ts;
  605: 
  606: 	char entropy[8]; /* from /dev/[u]random if possible, otherwise rand() */
  607: 	char is_real_entropy; /* whether entropy is from /dev/[u]random */
  608: 
  609: 	buffer *ts_debug_str;
  610: 	buffer *ts_date_str;
  611: 
  612: 	/* config-file */
  613: 	array *config;
  614: 	array *config_touched;
  615: 
  616: 	array *config_context;
  617: 	specific_config **config_storage;
  618: 
  619: 	server_config  srvconf;
  620: 
  621: 	short int config_deprecated;
  622: 	short int config_unsupported;
  623: 
  624: 	connections *conns;
  625: 	connections *joblist;
  626: 	connections *fdwaitqueue;
  627: 
  628: 	stat_cache  *stat_cache;
  629: 
  630: 	/**
  631: 	 * The status array can carry all the status information you want
  632: 	 * the key to the array is <module-prefix>.<name>
  633: 	 * and the values are counters
  634: 	 *
  635: 	 * example:
  636: 	 *   fastcgi.backends        = 10
  637: 	 *   fastcgi.active-backends = 6
  638: 	 *   fastcgi.backend.<key>.load = 24
  639: 	 *   fastcgi.backend.<key>....
  640: 	 *
  641: 	 *   fastcgi.backend.<key>.disconnects = ...
  642: 	 */
  643: 	array *status;
  644: 
  645: 	fdevent_handler_t event_handler;
  646: 
  647: 	int (* network_backend_write)(struct server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes);
  648: #ifdef USE_OPENSSL
  649: 	int (* network_ssl_backend_write)(struct server *srv, connection *con, SSL *ssl, chunkqueue *cq, off_t max_bytes);
  650: #endif
  651: 
  652: 	uid_t uid;
  653: 	gid_t gid;
  654: } server;
  655: 
  656: 
  657: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>