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