Annotation of embedaddon/lighttpd/src/base.h, revision 1.1.1.1

1.1       misho       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:        int    dir_ndx;
                    226: #endif
                    227: 
                    228:        buffer *content_type;
                    229: } stat_cache_entry;
                    230: 
                    231: typedef struct {
                    232:        splay_tree *files; /* the nodes of the tree are stat_cache_entry's */
                    233: 
                    234:        buffer *dir_name; /* for building the dirname from the filename */
                    235: #ifdef HAVE_FAM_H
                    236:        splay_tree *dirs; /* the nodes of the tree are fam_dir_entry */
                    237: 
                    238:        FAMConnection *fam;
                    239:        int    fam_fcce_ndx;
                    240: #endif
                    241:        buffer *hash_key;  /* temp-store for the hash-key */
                    242: } stat_cache;
                    243: 
                    244: typedef struct {
                    245:        array *mimetypes;
                    246: 
                    247:        /* virtual-servers */
                    248:        buffer *document_root;
                    249:        buffer *server_name;
                    250:        buffer *error_handler;
                    251:        buffer *server_tag;
                    252:        buffer *dirlist_encoding;
                    253:        buffer *errorfile_prefix;
                    254: 
                    255:        unsigned short max_keep_alive_requests;
                    256:        unsigned short max_keep_alive_idle;
                    257:        unsigned short max_read_idle;
                    258:        unsigned short max_write_idle;
                    259:        unsigned short use_xattr;
                    260:        unsigned short follow_symlink;
                    261:        unsigned short range_requests;
                    262: 
                    263:        /* debug */
                    264: 
                    265:        unsigned short log_file_not_found;
                    266:        unsigned short log_request_header;
                    267:        unsigned short log_request_handling;
                    268:        unsigned short log_response_header;
                    269:        unsigned short log_condition_handling;
                    270:        unsigned short log_ssl_noise;
                    271:        unsigned short log_timeouts;
                    272: 
                    273: 
                    274:        /* server wide */
                    275:        buffer *ssl_pemfile;
                    276:        buffer *ssl_ca_file;
                    277:        buffer *ssl_cipher_list;
                    278:        buffer *ssl_dh_file;
                    279:        buffer *ssl_ec_curve;
                    280:        unsigned short ssl_honor_cipher_order; /* determine SSL cipher in server-preferred order, not client-order */
                    281:        unsigned short ssl_empty_fragments; /* whether to not set SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS */
                    282:        unsigned short ssl_use_sslv2;
                    283:        unsigned short ssl_use_sslv3;
                    284:        unsigned short ssl_verifyclient;
                    285:        unsigned short ssl_verifyclient_enforce;
                    286:        unsigned short ssl_verifyclient_depth;
                    287:        buffer *ssl_verifyclient_username;
                    288:        unsigned short ssl_verifyclient_export_cert;
                    289:        unsigned short ssl_disable_client_renegotiation;
                    290: 
                    291:        unsigned short use_ipv6, set_v6only; /* set_v6only is only a temporary option */
                    292:        unsigned short defer_accept;
                    293:        unsigned short ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */
                    294:        unsigned short allow_http11;
                    295:        unsigned short etag_use_inode;
                    296:        unsigned short etag_use_mtime;
                    297:        unsigned short etag_use_size;
                    298:        unsigned short force_lowercase_filenames; /* if the FS is case-insensitive, force all files to lower-case */
                    299:        unsigned int max_request_size;
                    300: 
                    301:        unsigned short kbytes_per_second; /* connection kb/s limit */
                    302: 
                    303:        /* configside */
                    304:        unsigned short global_kbytes_per_second; /*  */
                    305: 
                    306:        off_t  global_bytes_per_second_cnt;
                    307:        /* server-wide traffic-shaper
                    308:         *
                    309:         * each context has the counter which is inited once
                    310:         * a second by the global_kbytes_per_second config-var
                    311:         *
                    312:         * as soon as global_kbytes_per_second gets below 0
                    313:         * the connected conns are "offline" a little bit
                    314:         *
                    315:         * the problem:
                    316:         * we somehow have to loose our "we are writable" signal
                    317:         * on the way.
                    318:         *
                    319:         */
                    320:        off_t *global_bytes_per_second_cnt_ptr; /*  */
                    321: 
                    322: #ifdef USE_OPENSSL
                    323:        SSL_CTX *ssl_ctx;
                    324: #endif
                    325: } specific_config;
                    326: 
                    327: /* the order of the items should be the same as they are processed
                    328:  * read before write as we use this later */
                    329: typedef enum {
                    330:        CON_STATE_CONNECT,
                    331:        CON_STATE_REQUEST_START,
                    332:        CON_STATE_READ,
                    333:        CON_STATE_REQUEST_END,
                    334:        CON_STATE_READ_POST,
                    335:        CON_STATE_HANDLE_REQUEST,
                    336:        CON_STATE_RESPONSE_START,
                    337:        CON_STATE_WRITE,
                    338:        CON_STATE_RESPONSE_END,
                    339:        CON_STATE_ERROR,
                    340:        CON_STATE_CLOSE
                    341: } connection_state_t;
                    342: 
                    343: typedef enum { COND_RESULT_UNSET, COND_RESULT_FALSE, COND_RESULT_TRUE } cond_result_t;
                    344: typedef struct {
                    345:        cond_result_t result;
                    346:        int patterncount;
                    347:        int matches[3 * 10];
                    348:        buffer *comp_value; /* just a pointer */
                    349:        
                    350:        comp_key_t comp_type;
                    351: } cond_cache_t;
                    352: 
                    353: typedef struct {
                    354:        connection_state_t state;
                    355: 
                    356:        /* timestamps */
                    357:        time_t read_idle_ts;
                    358:        time_t close_timeout_ts;
                    359:        time_t write_request_ts;
                    360: 
                    361:        time_t connection_start;
                    362:        time_t request_start;
                    363: 
                    364:        struct timeval start_tv;
                    365: 
                    366:        size_t request_count;        /* number of requests handled in this connection */
                    367:        size_t loops_per_request;    /* to catch endless loops in a single request
                    368:                                      *
                    369:                                      * used by mod_rewrite, mod_fastcgi, ... and others
                    370:                                      * this is self-protection
                    371:                                      */
                    372: 
                    373:        int fd;                      /* the FD for this connection */
                    374:        int fde_ndx;                 /* index for the fdevent-handler */
                    375:        int ndx;                     /* reverse mapping to server->connection[ndx] */
                    376: 
                    377:        /* fd states */
                    378:        int is_readable;
                    379:        int is_writable;
                    380: 
                    381:        int keep_alive;              /* only request.c can enable it, all other just disable */
                    382:        int keep_alive_idle;         /* remember max_keep_alive_idle from config */
                    383: 
                    384:        int file_started;
                    385:        int file_finished;
                    386: 
                    387:        chunkqueue *write_queue;      /* a large queue for low-level write ( HTTP response ) [ file, mem ] */
                    388:        chunkqueue *read_queue;       /* a small queue for low-level read ( HTTP request ) [ mem ] */
                    389:        chunkqueue *request_content_queue; /* takes request-content into tempfile if necessary [ tempfile, mem ]*/
                    390: 
                    391:        int traffic_limit_reached;
                    392: 
                    393:        off_t bytes_written;          /* used by mod_accesslog, mod_rrd */
                    394:        off_t bytes_written_cur_second; /* used by mod_accesslog, mod_rrd */
                    395:        off_t bytes_read;             /* used by mod_accesslog, mod_rrd */
                    396:        off_t bytes_header;
                    397: 
                    398:        int http_status;
                    399: 
                    400:        sock_addr dst_addr;
                    401:        buffer *dst_addr_buf;
                    402: 
                    403:        /* request */
                    404:        buffer *parse_request;
                    405:        unsigned int parsed_response; /* bitfield which contains the important header-fields of the parsed response header */
                    406: 
                    407:        request  request;
                    408:        request_uri uri;
                    409:        physical physical;
                    410:        response response;
                    411: 
                    412:        size_t header_len;
                    413: 
                    414:        array  *environment; /* used to pass lighttpd internal stuff to the FastCGI/CGI apps, setenv does that */
                    415: 
                    416:        /* response */
                    417:        int    got_response;
                    418: 
                    419:        int    in_joblist;
                    420: 
                    421:        connection_type mode;
                    422: 
                    423:        void **plugin_ctx;           /* plugin connection specific config */
                    424: 
                    425:        specific_config conf;        /* global connection specific config */
                    426:        cond_cache_t *cond_cache;
                    427: 
                    428:        buffer *server_name;
                    429: 
                    430:        /* error-handler */
                    431:        buffer *error_handler;
                    432:        int error_handler_saved_status;
                    433:        int in_error_handler;
                    434: 
                    435:        struct server_socket *srv_socket;   /* reference to the server-socket */
                    436: 
                    437: #ifdef USE_OPENSSL
                    438:        SSL *ssl;
                    439: # ifndef OPENSSL_NO_TLSEXT
                    440:        buffer *tlsext_server_name;
                    441: # endif
                    442:        unsigned int renegotiations; /* count of SSL_CB_HANDSHAKE_START */
                    443: #endif
                    444:        /* etag handling */
                    445:        etag_flags_t etag_flags;
                    446: 
                    447:        int conditional_is_valid[COMP_LAST_ELEMENT]; 
                    448: } connection;
                    449: 
                    450: typedef struct {
                    451:        connection **ptr;
                    452:        size_t size;
                    453:        size_t used;
                    454: } connections;
                    455: 
                    456: 
                    457: #ifdef HAVE_IPV6
                    458: typedef struct {
                    459:        int family;
                    460:        union {
                    461:                struct in6_addr ipv6;
                    462:                struct in_addr  ipv4;
                    463:        } addr;
                    464:        char b2[INET6_ADDRSTRLEN + 1];
                    465:        time_t ts;
                    466: } inet_ntop_cache_type;
                    467: #endif
                    468: 
                    469: 
                    470: typedef struct {
                    471:        buffer *uri;
                    472:        time_t mtime;
                    473:        int http_status;
                    474: } realpath_cache_type;
                    475: 
                    476: typedef struct {
                    477:        time_t  mtime;  /* the key */
                    478:        buffer *str;    /* a buffer for the string represenation */
                    479: } mtime_cache_type;
                    480: 
                    481: typedef struct {
                    482:        void  *ptr;
                    483:        size_t used;
                    484:        size_t size;
                    485: } buffer_plugin;
                    486: 
                    487: typedef struct {
                    488:        unsigned short port;
                    489:        buffer *bindhost;
                    490: 
                    491:        buffer *errorlog_file;
                    492:        unsigned short errorlog_use_syslog;
                    493:        buffer *breakagelog_file;
                    494: 
                    495:        unsigned short dont_daemonize;
                    496:        buffer *changeroot;
                    497:        buffer *username;
                    498:        buffer *groupname;
                    499: 
                    500:        buffer *pid_file;
                    501: 
                    502:        buffer *event_handler;
                    503: 
                    504:        buffer *modules_dir;
                    505:        buffer *network_backend;
                    506:        array *modules;
                    507:        array *upload_tempdirs;
                    508: 
                    509:        unsigned short max_worker;
                    510:        unsigned short max_fds;
                    511:        unsigned short max_conns;
                    512:        unsigned int max_request_size;
                    513: 
                    514:        unsigned short log_request_header_on_error;
                    515:        unsigned short log_state_handling;
                    516: 
                    517:        enum { STAT_CACHE_ENGINE_UNSET,
                    518:                        STAT_CACHE_ENGINE_NONE,
                    519:                        STAT_CACHE_ENGINE_SIMPLE
                    520: #ifdef HAVE_FAM_H
                    521:                        , STAT_CACHE_ENGINE_FAM
                    522: #endif
                    523:        } stat_cache_engine;
                    524:        unsigned short enable_cores;
                    525:        unsigned short reject_expect_100_with_417;
                    526: } server_config;
                    527: 
                    528: typedef struct server_socket {
                    529:        sock_addr addr;
                    530:        int       fd;
                    531:        int       fde_ndx;
                    532: 
                    533:        buffer *ssl_pemfile;
                    534:        buffer *ssl_ca_file;
                    535:        buffer *ssl_cipher_list;
                    536:        buffer *ssl_dh_file;
                    537:        buffer *ssl_ec_curve;
                    538:        unsigned short ssl_use_sslv2;
                    539:        unsigned short ssl_use_sslv3;
                    540:        unsigned short use_ipv6;
                    541:        unsigned short is_ssl;
                    542: 
                    543:        buffer *srv_token;
                    544: 
                    545: #ifdef USE_OPENSSL
                    546:        SSL_CTX *ssl_ctx;
                    547: #endif
                    548: } server_socket;
                    549: 
                    550: typedef struct {
                    551:        server_socket **ptr;
                    552: 
                    553:        size_t size;
                    554:        size_t used;
                    555: } server_socket_array;
                    556: 
                    557: typedef struct server {
                    558:        server_socket_array srv_sockets;
                    559: 
                    560:        /* the errorlog */
                    561:        int errorlog_fd;
                    562:        enum { ERRORLOG_FILE, ERRORLOG_FD, ERRORLOG_SYSLOG, ERRORLOG_PIPE } errorlog_mode;
                    563:        buffer *errorlog_buf;
                    564: 
                    565:        fdevents *ev, *ev_ins;
                    566: 
                    567:        buffer_plugin plugins;
                    568:        void *plugin_slots;
                    569: 
                    570:        /* counters */
                    571:        int con_opened;
                    572:        int con_read;
                    573:        int con_written;
                    574:        int con_closed;
                    575: 
                    576:        int ssl_is_init;
                    577: 
                    578:        int max_fds;    /* max possible fds */
                    579:        int cur_fds;    /* currently used fds */
                    580:        int want_fds;   /* waiting fds */
                    581:        int sockets_disabled;
                    582: 
                    583:        size_t max_conns;
                    584: 
                    585:        /* buffers */
                    586:        buffer *parse_full_path;
                    587:        buffer *response_header;
                    588:        buffer *response_range;
                    589:        buffer *tmp_buf;
                    590: 
                    591:        buffer *tmp_chunk_len;
                    592: 
                    593:        buffer *empty_string; /* is necessary for cond_match */
                    594: 
                    595:        buffer *cond_check_buf;
                    596: 
                    597:        /* caches */
                    598: #ifdef HAVE_IPV6
                    599:        inet_ntop_cache_type inet_ntop_cache[INET_NTOP_CACHE_MAX];
                    600: #endif
                    601:        mtime_cache_type mtime_cache[FILE_CACHE_MAX];
                    602: 
                    603:        array *split_vals;
                    604: 
                    605:        /* Timestamps */
                    606:        time_t cur_ts;
                    607:        time_t last_generated_date_ts;
                    608:        time_t last_generated_debug_ts;
                    609:        time_t startup_ts;
                    610: 
                    611:        char entropy[8]; /* from /dev/[u]random if possible, otherwise rand() */
                    612:        char is_real_entropy; /* whether entropy is from /dev/[u]random */
                    613: 
                    614:        buffer *ts_debug_str;
                    615:        buffer *ts_date_str;
                    616: 
                    617:        /* config-file */
                    618:        array *config;
                    619:        array *config_touched;
                    620: 
                    621:        array *config_context;
                    622:        specific_config **config_storage;
                    623: 
                    624:        server_config  srvconf;
                    625: 
                    626:        short int config_deprecated;
                    627:        short int config_unsupported;
                    628: 
                    629:        connections *conns;
                    630:        connections *joblist;
                    631:        connections *fdwaitqueue;
                    632: 
                    633:        stat_cache  *stat_cache;
                    634: 
                    635:        /**
                    636:         * The status array can carry all the status information you want
                    637:         * the key to the array is <module-prefix>.<name>
                    638:         * and the values are counters
                    639:         *
                    640:         * example:
                    641:         *   fastcgi.backends        = 10
                    642:         *   fastcgi.active-backends = 6
                    643:         *   fastcgi.backend.<key>.load = 24
                    644:         *   fastcgi.backend.<key>....
                    645:         *
                    646:         *   fastcgi.backend.<key>.disconnects = ...
                    647:         */
                    648:        array *status;
                    649: 
                    650:        fdevent_handler_t event_handler;
                    651: 
                    652:        int (* network_backend_write)(struct server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes);
                    653: #ifdef USE_OPENSSL
                    654:        int (* network_ssl_backend_write)(struct server *srv, connection *con, SSL *ssl, chunkqueue *cq, off_t max_bytes);
                    655: #endif
                    656: 
                    657:        uid_t uid;
                    658:        gid_t gid;
                    659: } server;
                    660: 
                    661: 
                    662: #endif

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