Home | History | Annotate | Line # | Download | only in httpd
bozohttpd.h revision 1.23.2.1
      1  1.23.2.1    tls /*	$NetBSD: bozohttpd.h,v 1.23.2.1 2014/08/20 00:02:22 tls Exp $	*/
      2       1.3    tls 
      3      1.20    mrg /*	$eterna: bozohttpd.h,v 1.39 2011/11/18 09:21:15 mrg Exp $	*/
      4       1.1    tls 
      5       1.1    tls /*
      6  1.23.2.1    tls  * Copyright (c) 1997-2014 Matthew R. Green
      7       1.1    tls  * All rights reserved.
      8       1.1    tls  *
      9       1.1    tls  * Redistribution and use in source and binary forms, with or without
     10       1.1    tls  * modification, are permitted provided that the following conditions
     11       1.1    tls  * are met:
     12       1.1    tls  * 1. Redistributions of source code must retain the above copyright
     13       1.1    tls  *    notice, this list of conditions and the following disclaimer.
     14       1.1    tls  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1    tls  *    notice, this list of conditions and the following disclaimer and
     16       1.1    tls  *    dedication in the documentation and/or other materials provided
     17       1.1    tls  *    with the distribution.
     18       1.1    tls  *
     19       1.1    tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20       1.1    tls  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21       1.1    tls  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22       1.1    tls  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.1    tls  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24       1.1    tls  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25       1.1    tls  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26       1.1    tls  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27       1.1    tls  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1    tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1    tls  * SUCH DAMAGE.
     30       1.1    tls  *
     31       1.1    tls  */
     32      1.12    mrg #ifndef BOZOHTTOPD_H_
     33      1.12    mrg #define BOZOHTTOPD_H_	1
     34      1.12    mrg 
     35  1.23.2.1    tls #include "netbsd_queue.h"
     36      1.12    mrg 
     37       1.1    tls #include <sys/stat.h>
     38       1.5    mrg 
     39  1.23.2.1    tls #ifndef NO_LUA_SUPPORT
     40  1.23.2.1    tls #include <lua.h>
     41  1.23.2.1    tls #endif
     42       1.1    tls #include <stdio.h>
     43       1.1    tls 
     44       1.8    mrg /* lots of "const" but gets free()'ed etc at times, sigh */
     45       1.8    mrg 
     46       1.1    tls /* headers */
     47      1.12    mrg typedef struct bozoheaders {
     48       1.4    rtr 	/*const*/ char *h_header;
     49       1.1    tls 	/*const*/ char *h_value;	/* this gets free()'ed etc at times */
     50      1.12    mrg 	SIMPLEQ_ENTRY(bozoheaders)	h_next;
     51      1.12    mrg } bozoheaders_t;
     52       1.1    tls 
     53  1.23.2.1    tls #ifndef NO_LUA_SUPPORT
     54  1.23.2.1    tls typedef struct lua_handler {
     55  1.23.2.1    tls 	const char	*name;
     56  1.23.2.1    tls 	int		 ref;
     57  1.23.2.1    tls 	SIMPLEQ_ENTRY(lua_handler)	h_next;
     58  1.23.2.1    tls } lua_handler_t;
     59  1.23.2.1    tls 
     60  1.23.2.1    tls typedef struct lua_state_map {
     61  1.23.2.1    tls 	const char 	*script;
     62  1.23.2.1    tls 	const char	*prefix;
     63  1.23.2.1    tls 	lua_State	*L;
     64  1.23.2.1    tls 	SIMPLEQ_HEAD(, lua_handler)	handlers;
     65  1.23.2.1    tls 	SIMPLEQ_ENTRY(lua_state_map)	s_next;
     66  1.23.2.1    tls } lua_state_map_t;
     67  1.23.2.1    tls #endif
     68  1.23.2.1    tls 
     69      1.12    mrg typedef struct bozo_content_map_t {
     70      1.12    mrg 	const char	*name;		/* postfix of file */
     71      1.12    mrg 	size_t	 	 namelen;	/* length of postfix */
     72      1.12    mrg 	const char	*type;		/* matching content-type */
     73      1.12    mrg 	const char	*encoding;	/* matching content-encoding */
     74      1.12    mrg 	const char	*encoding11;	/* matching content-encoding (HTTP/1.1) */
     75      1.12    mrg 	const char	*cgihandler;	/* optional CGI handler */
     76      1.12    mrg } bozo_content_map_t;
     77      1.12    mrg 
     78      1.12    mrg /* this struct holds the bozo constants */
     79      1.12    mrg typedef struct bozo_consts_t {
     80      1.12    mrg 	const char	*http_09;	/* "HTTP/0.9" */
     81      1.12    mrg 	const char	*http_10;	/* "HTTP/1.0" */
     82      1.12    mrg 	const char	*http_11;	/* "HTTP/1.1" */
     83      1.12    mrg 	const char	*text_plain;	/* "text/plain" */
     84      1.12    mrg } bozo_consts_t;
     85      1.12    mrg 
     86      1.12    mrg /* this structure encapsulates all the bozo flags and control vars */
     87      1.12    mrg typedef struct bozohttpd_t {
     88      1.12    mrg 	char		*rootdir;	/* root directory */
     89      1.12    mrg 	char		*username;	/* username to switch to */
     90      1.12    mrg 	int		 numeric;	/* avoid gethostby*() */
     91      1.12    mrg 	char		*virtbase;	/* virtual directory base */
     92      1.12    mrg 	int		 unknown_slash;	/* unknown vhosts go to normal slashdir */
     93      1.12    mrg 	int		 untrustedref;	/* make sure referrer = me unless url = / */
     94      1.12    mrg 	int		 logstderr;	/* log to stderr (even if not tty) */
     95      1.12    mrg 	int		 background;	/* drop into daemon mode */
     96      1.12    mrg 	int		 foreground;	/* keep daemon mode in foreground */
     97      1.18   jmmv 	char		*pidfile;	/* path to the pid file, if any */
     98      1.12    mrg 	size_t		 page_size;	/* page size */
     99      1.12    mrg 	char		*slashdir;	/* www slash directory */
    100      1.12    mrg 	char		*bindport;	/* bind port; default "http" */
    101      1.12    mrg 	char		*bindaddress;	/* address for binding - INADDR_ANY */
    102      1.12    mrg 	int		 debug;		/* debugging level */
    103      1.12    mrg 	char		*virthostname;	/* my name */
    104      1.12    mrg 	const char	*server_software;/* our brand :-) */
    105      1.12    mrg 	const char	*index_html;	/* our home page */
    106      1.12    mrg 	const char	*public_html;	/* ~user/public_html page */
    107      1.12    mrg 	int		 enable_users;	/* enable public_html */
    108      1.12    mrg 	int		*sock;		/* bound sockets */
    109      1.12    mrg 	int		 nsock;		/* number of above */
    110      1.12    mrg 	struct pollfd	*fds;		/* current poll fd set */
    111      1.12    mrg 	int		 request_times;	/* # times a request was processed */
    112      1.12    mrg 	int		 dir_indexing;	/* handle directories */
    113      1.12    mrg 	int		 hide_dots;	/* hide .* */
    114      1.12    mrg 	int		 process_cgi;	/* use the cgi handler */
    115      1.12    mrg 	char		*cgibin;	/* cgi-bin directory */
    116  1.23.2.1    tls #ifndef NO_LUA_SUPPORT
    117  1.23.2.1    tls 	int		 process_lua;	/* use the Lua handler */
    118  1.23.2.1    tls 	SIMPLEQ_HEAD(, lua_state_map)	lua_states;
    119  1.23.2.1    tls #endif
    120      1.12    mrg 	void		*sslinfo;	/* pointer to ssl struct */
    121      1.12    mrg 	int		dynamic_content_map_size;/* size of dyn cont map */
    122      1.12    mrg 	bozo_content_map_t	*dynamic_content_map;/* dynamic content map */
    123      1.12    mrg 	size_t		 mmapsz;	/* size of region to mmap */
    124      1.12    mrg 	char		*getln_buffer;	/* space for getln buffer */
    125      1.12    mrg 	ssize_t		 getln_buflen;	/* length of allocated space */
    126      1.12    mrg 	char		*errorbuf;	/* no dynamic allocation allowed */
    127      1.12    mrg 	bozo_consts_t	 consts;	/* various constants */
    128      1.12    mrg } bozohttpd_t;
    129      1.12    mrg 
    130      1.12    mrg /* bozo_httpreq_t */
    131      1.12    mrg typedef struct bozo_httpreq_t {
    132      1.12    mrg 	bozohttpd_t	*hr_httpd;
    133      1.12    mrg 	int		hr_method;
    134       1.1    tls #define	HTTP_GET	0x01
    135       1.1    tls #define HTTP_POST	0x02
    136       1.1    tls #define HTTP_HEAD	0x03
    137       1.1    tls #define HTTP_OPTIONS	0x04	/* not supported */
    138       1.1    tls #define HTTP_PUT	0x05	/* not supported */
    139       1.1    tls #define HTTP_DELETE	0x06	/* not supported */
    140       1.1    tls #define HTTP_TRACE	0x07	/* not supported */
    141       1.1    tls #define HTTP_CONNECT	0x08	/* not supported */
    142       1.1    tls 	const char *hr_methodstr;
    143  1.23.2.1    tls 	char	*hr_virthostname;	/* server name (if not identical
    144  1.23.2.1    tls 					   to hr_httpd->virthostname) */
    145       1.8    mrg 	char	*hr_file;
    146      1.14    mrg 	char	*hr_oldfile;	/* if we added an index_html */
    147  1.23.2.1    tls 	char	*hr_query;
    148  1.23.2.1    tls 	char	*hr_host;	/* HTTP/1.1 Host: or virtual hostname,
    149  1.23.2.1    tls 				   possibly including a port number */
    150       1.1    tls 	const char *hr_proto;
    151       1.1    tls 	const char *hr_content_type;
    152       1.1    tls 	const char *hr_content_length;
    153       1.1    tls 	const char *hr_allow;
    154       1.1    tls 	const char *hr_referrer;
    155       1.5    mrg 	const char *hr_range;
    156       1.7  joerg 	const char *hr_if_modified_since;
    157      1.21  elric 	const char *hr_accept_encoding;
    158       1.5    mrg 	int         hr_have_range;
    159       1.5    mrg 	off_t       hr_first_byte_pos;
    160       1.5    mrg 	off_t       hr_last_byte_pos;
    161       1.8    mrg 	/*const*/ char *hr_remotehost;
    162       1.8    mrg 	/*const*/ char *hr_remoteaddr;
    163       1.8    mrg 	/*const*/ char *hr_serverport;
    164       1.1    tls #ifdef DO_HTPASSWD
    165       1.9    mrg 	/*const*/ char *hr_authrealm;
    166       1.9    mrg 	/*const*/ char *hr_authuser;
    167       1.9    mrg 	/*const*/ char *hr_authpass;
    168       1.1    tls #endif
    169      1.12    mrg 	SIMPLEQ_HEAD(, bozoheaders)	hr_headers;
    170       1.1    tls 	int	hr_nheaders;
    171      1.12    mrg } bozo_httpreq_t;
    172       1.1    tls 
    173  1.23.2.1    tls /* helper to access the "active" host name from a httpd/request pair */
    174  1.23.2.1    tls #define	BOZOHOST(HTTPD,REQUEST)	((REQUEST)->hr_virthostname ?		\
    175  1.23.2.1    tls 					(REQUEST)->hr_virthostname :	\
    176  1.23.2.1    tls 					(HTTPD)->virthostname)
    177  1.23.2.1    tls 
    178      1.12    mrg /* structure to hold string based (name, value) pairs with preferences */
    179      1.12    mrg typedef struct bozoprefs_t {
    180      1.12    mrg 	unsigned	  size;		/* size of the two arrays */
    181      1.12    mrg 	unsigned	  c;		/* # of entries in arrays */
    182      1.12    mrg 	char		**name;		/* names of each entry */
    183      1.12    mrg 	char		**value;	/* values for the name entries */
    184      1.12    mrg } bozoprefs_t;
    185       1.1    tls 
    186  1.23.2.1    tls /* by default write in upto 64KiB chunks, and mmap in upto 64MiB chunks */
    187  1.23.2.1    tls #ifndef BOZO_WRSZ
    188      1.12    mrg #define BOZO_WRSZ	(64 * 1024)
    189  1.23.2.1    tls #endif
    190  1.23.2.1    tls #ifndef BOZO_MMAPSZ
    191      1.12    mrg #define BOZO_MMAPSZ	(BOZO_WRSZ * 1024)
    192  1.23.2.1    tls #endif
    193       1.1    tls 
    194       1.1    tls /* debug flags */
    195       1.1    tls #define DEBUG_NORMAL	1
    196       1.1    tls #define DEBUG_FAT	2
    197       1.1    tls #define DEBUG_OBESE	3
    198       1.1    tls #define DEBUG_EXPLODING	4
    199       1.1    tls 
    200       1.1    tls #define	strornull(x)	((x) ? (x) : "<null>")
    201       1.1    tls 
    202      1.22  joerg #if defined(__GNUC__) && __GNUC__ >= 3
    203      1.22  joerg #define BOZO_PRINTFLIKE(x,y) __attribute__((__format__(__printf__, x,y)))
    204      1.22  joerg #define BOZO_DEAD __attribute__((__noreturn__))
    205      1.22  joerg #endif
    206      1.22  joerg 
    207      1.17    mrg #ifndef NO_DEBUG
    208      1.22  joerg void	debug__(bozohttpd_t *, int, const char *, ...) BOZO_PRINTFLIKE(3, 4);
    209       1.1    tls #define debug(x)	debug__ x
    210       1.1    tls #else
    211  1.23.2.1    tls #define	debug(x)
    212      1.17    mrg #endif /* NO_DEBUG */
    213       1.1    tls 
    214      1.12    mrg void	bozo_warn(bozohttpd_t *, const char *, ...)
    215      1.19  joerg 		BOZO_PRINTFLIKE(2, 3);
    216      1.12    mrg void	bozo_err(bozohttpd_t *, int, const char *, ...)
    217      1.19  joerg 		BOZO_PRINTFLIKE(3, 4)
    218      1.19  joerg 		BOZO_DEAD;
    219      1.14    mrg int	bozo_http_error(bozohttpd_t *, int, bozo_httpreq_t *, const char *);
    220       1.1    tls 
    221      1.12    mrg int	bozo_check_special_files(bozo_httpreq_t *, const char *);
    222      1.12    mrg char	*bozo_http_date(char *, size_t);
    223      1.12    mrg void	bozo_print_header(bozo_httpreq_t *, struct stat *, const char *, const char *);
    224  1.23.2.1    tls char	*bozo_escape_rfc3986(bozohttpd_t *httpd, const char *url);
    225  1.23.2.1    tls char	*bozo_escape_html(bozohttpd_t *httpd, const char *url);
    226       1.1    tls 
    227      1.12    mrg char	*bozodgetln(bozohttpd_t *, int, ssize_t *, ssize_t (*)(bozohttpd_t *, int, void *, size_t));
    228       1.5    mrg char	*bozostrnsep(char **, const char *, ssize_t *);
    229       1.1    tls 
    230      1.12    mrg void	*bozomalloc(bozohttpd_t *, size_t);
    231      1.12    mrg void	*bozorealloc(bozohttpd_t *, void *, size_t);
    232      1.12    mrg char	*bozostrdup(bozohttpd_t *, const char *);
    233       1.1    tls 
    234       1.1    tls /* ssl-bozo.c */
    235      1.12    mrg #ifdef NO_SSL_SUPPORT
    236      1.20    mrg #define bozo_ssl_set_opts(w, x, y)	do { /* nothing */ } while (0)
    237      1.20    mrg #define bozo_ssl_init(x)		do { /* nothing */ } while (0)
    238  1.23.2.1    tls #define bozo_ssl_accept(x)		(0)
    239      1.20    mrg #define bozo_ssl_destroy(x)		do { /* nothing */ } while (0)
    240      1.12    mrg #else
    241      1.14    mrg void	bozo_ssl_set_opts(bozohttpd_t *, const char *, const char *);
    242      1.14    mrg void	bozo_ssl_init(bozohttpd_t *);
    243  1.23.2.1    tls int	bozo_ssl_accept(bozohttpd_t *);
    244      1.14    mrg void	bozo_ssl_destroy(bozohttpd_t *);
    245       1.1    tls #endif
    246       1.1    tls 
    247       1.1    tls 
    248       1.1    tls /* auth-bozo.c */
    249       1.1    tls #ifdef DO_HTPASSWD
    250      1.14    mrg int	bozo_auth_check(bozo_httpreq_t *, const char *);
    251      1.14    mrg void	bozo_auth_cleanup(bozo_httpreq_t *);
    252      1.14    mrg int	bozo_auth_check_headers(bozo_httpreq_t *, char *, char *, ssize_t);
    253      1.14    mrg int	bozo_auth_check_special_files(bozo_httpreq_t *, const char *);
    254      1.14    mrg void	bozo_auth_check_401(bozo_httpreq_t *, int);
    255      1.14    mrg void	bozo_auth_cgi_setenv(bozo_httpreq_t *, char ***);
    256      1.14    mrg int	bozo_auth_cgi_count(bozo_httpreq_t *);
    257      1.12    mrg #else
    258      1.14    mrg #define	bozo_auth_check(x, y)			0
    259      1.20    mrg #define	bozo_auth_cleanup(x)			do { /* nothing */ } while (0)
    260      1.14    mrg #define	bozo_auth_check_headers(y, z, a, b)	0
    261      1.14    mrg #define	bozo_auth_check_special_files(x, y)	0
    262      1.20    mrg #define	bozo_auth_check_401(x, y)		do { /* nothing */ } while (0)
    263      1.20    mrg #define	bozo_auth_cgi_setenv(x, y)		do { /* nothing */ } while (0)
    264      1.14    mrg #define	bozo_auth_cgi_count(x)			0
    265       1.1    tls #endif /* DO_HTPASSWD */
    266       1.1    tls 
    267       1.1    tls 
    268       1.1    tls /* cgi-bozo.c */
    269      1.12    mrg #ifdef NO_CGIBIN_SUPPORT
    270      1.14    mrg #define	bozo_process_cgi(h)				0
    271       1.1    tls #else
    272      1.14    mrg void	bozo_cgi_setbin(bozohttpd_t *, const char *);
    273      1.14    mrg void	bozo_setenv(bozohttpd_t *, const char *, const char *, char **);
    274      1.14    mrg int	bozo_process_cgi(bozo_httpreq_t *);
    275      1.14    mrg void	bozo_add_content_map_cgi(bozohttpd_t *, const char *, const char *);
    276       1.1    tls #endif /* NO_CGIBIN_SUPPORT */
    277       1.1    tls 
    278       1.1    tls 
    279  1.23.2.1    tls /* lua-bozo.c */
    280  1.23.2.1    tls #ifdef NO_LUA_SUPPORT
    281  1.23.2.1    tls #define bozo_process_lua(h)				0
    282  1.23.2.1    tls #else
    283  1.23.2.1    tls void	bozo_add_lua_map(bozohttpd_t *, const char *, const char *);
    284  1.23.2.1    tls int	bozo_process_lua(bozo_httpreq_t *);
    285  1.23.2.1    tls #endif /* NO_LUA_SUPPORT */
    286  1.23.2.1    tls 
    287  1.23.2.1    tls 
    288       1.1    tls /* daemon-bozo.c */
    289      1.12    mrg #ifdef NO_DAEMON_MODE
    290      1.20    mrg #define bozo_daemon_init(x)				do { /* nothing */ } while (0)
    291      1.14    mrg #define bozo_daemon_fork(x)				0
    292      1.20    mrg #define bozo_daemon_closefds(x)				do { /* nothing */ } while (0)
    293      1.12    mrg #else
    294      1.14    mrg void	bozo_daemon_init(bozohttpd_t *);
    295      1.14    mrg int	bozo_daemon_fork(bozohttpd_t *);
    296      1.14    mrg void	bozo_daemon_closefds(bozohttpd_t *);
    297       1.1    tls #endif /* NO_DAEMON_MODE */
    298       1.1    tls 
    299       1.1    tls 
    300       1.1    tls /* tilde-luzah-bozo.c */
    301      1.12    mrg #ifdef NO_USER_SUPPORT
    302      1.14    mrg #define bozo_user_transform(a, c)			0
    303       1.8    mrg #else
    304      1.12    mrg int	bozo_user_transform(bozo_httpreq_t *, int *);
    305       1.1    tls #endif /* NO_USER_SUPPORT */
    306       1.1    tls 
    307       1.1    tls 
    308       1.1    tls /* dir-index-bozo.c */
    309      1.12    mrg #ifdef NO_DIRINDEX_SUPPORT
    310      1.15    mrg #define bozo_dir_index(a, b, c)				0
    311       1.1    tls #else
    312      1.12    mrg int	bozo_dir_index(bozo_httpreq_t *, const char *, int);
    313       1.1    tls #endif /* NO_DIRINDEX_SUPPORT */
    314       1.1    tls 
    315       1.1    tls 
    316       1.1    tls /* content-bozo.c */
    317      1.14    mrg const char *bozo_content_type(bozo_httpreq_t *, const char *);
    318      1.14    mrg const char *bozo_content_encoding(bozo_httpreq_t *, const char *);
    319      1.14    mrg bozo_content_map_t *bozo_match_content_map(bozohttpd_t *, const char *, int);
    320      1.14    mrg bozo_content_map_t *bozo_get_content_map(bozohttpd_t *, const char *);
    321       1.1    tls #ifndef NO_DYNAMIC_CONTENT
    322      1.12    mrg void	bozo_add_content_map_mime(bozohttpd_t *, const char *, const char *, const char *, const char *);
    323       1.1    tls #endif
    324      1.12    mrg 
    325      1.12    mrg /* I/O */
    326      1.22  joerg int bozo_printf(bozohttpd_t *, const char *, ...) BOZO_PRINTFLIKE(2, 3);;
    327      1.12    mrg ssize_t bozo_read(bozohttpd_t *, int, void *, size_t);
    328      1.12    mrg ssize_t bozo_write(bozohttpd_t *, int, const void *, size_t);
    329      1.12    mrg int bozo_flush(bozohttpd_t *, FILE *);
    330      1.12    mrg 
    331      1.12    mrg /* misc */
    332      1.12    mrg int bozo_init_httpd(bozohttpd_t *);
    333      1.12    mrg int bozo_init_prefs(bozoprefs_t *);
    334      1.12    mrg int bozo_set_defaults(bozohttpd_t *, bozoprefs_t *);
    335      1.12    mrg int bozo_setup(bozohttpd_t *, bozoprefs_t *, const char *, const char *);
    336      1.12    mrg bozo_httpreq_t *bozo_read_request(bozohttpd_t *);
    337      1.12    mrg void bozo_process_request(bozo_httpreq_t *);
    338      1.12    mrg void bozo_clean_request(bozo_httpreq_t *);
    339      1.12    mrg 
    340      1.12    mrg /* variables */
    341      1.12    mrg int bozo_set_pref(bozoprefs_t *, const char *, const char *);
    342      1.12    mrg char *bozo_get_pref(bozoprefs_t *, const char *);
    343      1.12    mrg 
    344      1.12    mrg #endif	/* BOZOHTTOPD_H_ */
    345