Home | History | Annotate | Line # | Download | only in httpd
bozohttpd.h revision 1.24
      1 /*	$NetBSD: bozohttpd.h,v 1.24 2013/06/27 10:01:31 martin Exp $	*/
      2 
      3 /*	$eterna: bozohttpd.h,v 1.39 2011/11/18 09:21:15 mrg Exp $	*/
      4 
      5 /*
      6  * Copyright (c) 1997-2011 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 <sys/queue.h>
     36 
     37 #include <sys/stat.h>
     38 
     39 #include <stdio.h>
     40 
     41 /* lots of "const" but gets free()'ed etc at times, sigh */
     42 
     43 /* headers */
     44 typedef struct bozoheaders {
     45 	/*const*/ char *h_header;
     46 	/*const*/ char *h_value;	/* this gets free()'ed etc at times */
     47 	SIMPLEQ_ENTRY(bozoheaders)	h_next;
     48 } bozoheaders_t;
     49 
     50 typedef struct bozo_content_map_t {
     51 	const char	*name;		/* postfix of file */
     52 	size_t	 	 namelen;	/* length of postfix */
     53 	const char	*type;		/* matching content-type */
     54 	const char	*encoding;	/* matching content-encoding */
     55 	const char	*encoding11;	/* matching content-encoding (HTTP/1.1) */
     56 	const char	*cgihandler;	/* optional CGI handler */
     57 } bozo_content_map_t;
     58 
     59 /* this struct holds the bozo constants */
     60 typedef struct bozo_consts_t {
     61 	const char	*http_09;	/* "HTTP/0.9" */
     62 	const char	*http_10;	/* "HTTP/1.0" */
     63 	const char	*http_11;	/* "HTTP/1.1" */
     64 	const char	*text_plain;	/* "text/plain" */
     65 } bozo_consts_t;
     66 
     67 /* this structure encapsulates all the bozo flags and control vars */
     68 typedef struct bozohttpd_t {
     69 	char		*rootdir;	/* root directory */
     70 	char		*username;	/* username to switch to */
     71 	int		 numeric;	/* avoid gethostby*() */
     72 	char		*virtbase;	/* virtual directory base */
     73 	int		 unknown_slash;	/* unknown vhosts go to normal slashdir */
     74 	int		 untrustedref;	/* make sure referrer = me unless url = / */
     75 	int		 logstderr;	/* log to stderr (even if not tty) */
     76 	int		 background;	/* drop into daemon mode */
     77 	int		 foreground;	/* keep daemon mode in foreground */
     78 	char		*pidfile;	/* path to the pid file, if any */
     79 	size_t		 page_size;	/* page size */
     80 	char		*slashdir;	/* www slash directory */
     81 	char		*bindport;	/* bind port; default "http" */
     82 	char		*bindaddress;	/* address for binding - INADDR_ANY */
     83 	int		 debug;		/* debugging level */
     84 	char		*virthostname;	/* my name */
     85 	const char	*server_software;/* our brand :-) */
     86 	const char	*index_html;	/* our home page */
     87 	const char	*public_html;	/* ~user/public_html page */
     88 	int		 enable_users;	/* enable public_html */
     89 	int		*sock;		/* bound sockets */
     90 	int		 nsock;		/* number of above */
     91 	struct pollfd	*fds;		/* current poll fd set */
     92 	int		 request_times;	/* # times a request was processed */
     93 	int		 dir_indexing;	/* handle directories */
     94 	int		 hide_dots;	/* hide .* */
     95 	int		 process_cgi;	/* use the cgi handler */
     96 	char		*cgibin;	/* cgi-bin directory */
     97 	void		*sslinfo;	/* pointer to ssl struct */
     98 	int		dynamic_content_map_size;/* size of dyn cont map */
     99 	bozo_content_map_t	*dynamic_content_map;/* dynamic content map */
    100 	size_t		 mmapsz;	/* size of region to mmap */
    101 	char		*getln_buffer;	/* space for getln buffer */
    102 	ssize_t		 getln_buflen;	/* length of allocated space */
    103 	char		*errorbuf;	/* no dynamic allocation allowed */
    104 	bozo_consts_t	 consts;	/* various constants */
    105 } bozohttpd_t;
    106 
    107 /* bozo_httpreq_t */
    108 typedef struct bozo_httpreq_t {
    109 	bozohttpd_t	*hr_httpd;
    110 	int		hr_method;
    111 #define	HTTP_GET	0x01
    112 #define HTTP_POST	0x02
    113 #define HTTP_HEAD	0x03
    114 #define HTTP_OPTIONS	0x04	/* not supported */
    115 #define HTTP_PUT	0x05	/* not supported */
    116 #define HTTP_DELETE	0x06	/* not supported */
    117 #define HTTP_TRACE	0x07	/* not supported */
    118 #define HTTP_CONNECT	0x08	/* not supported */
    119 	const char *hr_methodstr;
    120 	char	*hr_virthostname;	/* server name (if not identical
    121 					   to hr_httpd->virthostname) */
    122 	char	*hr_file;
    123 	char	*hr_oldfile;	/* if we added an index_html */
    124 	char	*hr_query;
    125 	const char *hr_proto;
    126 	const char *hr_content_type;
    127 	const char *hr_content_length;
    128 	const char *hr_allow;
    129 	const char *hr_host;		/* HTTP/1.1 Host: */
    130 	const char *hr_referrer;
    131 	const char *hr_range;
    132 	const char *hr_if_modified_since;
    133 	const char *hr_accept_encoding;
    134 	int         hr_have_range;
    135 	off_t       hr_first_byte_pos;
    136 	off_t       hr_last_byte_pos;
    137 	/*const*/ char *hr_remotehost;
    138 	/*const*/ char *hr_remoteaddr;
    139 	/*const*/ char *hr_serverport;
    140 #ifdef DO_HTPASSWD
    141 	/*const*/ char *hr_authrealm;
    142 	/*const*/ char *hr_authuser;
    143 	/*const*/ char *hr_authpass;
    144 #endif
    145 	SIMPLEQ_HEAD(, bozoheaders)	hr_headers;
    146 	int	hr_nheaders;
    147 } bozo_httpreq_t;
    148 
    149 /* helper to access the "active" host name from a httpd/request pair */
    150 #define	BOZOHOST(HTTPD,REQUEST)	((REQUEST)->hr_virthostname ?		\
    151 					(REQUEST)->hr_virthostname :	\
    152 					(HTTPD)->virthostname)
    153 
    154 /* structure to hold string based (name, value) pairs with preferences */
    155 typedef struct bozoprefs_t {
    156 	unsigned	  size;		/* size of the two arrays */
    157 	unsigned	  c;		/* # of entries in arrays */
    158 	char		**name;		/* names of each entry */
    159 	char		**value;	/* values for the name entries */
    160 } bozoprefs_t;
    161 
    162 /* write in upto 64KiB chunks, and mmap in upto 64MiB chunks */
    163 #define BOZO_WRSZ	(64 * 1024)
    164 #define BOZO_MMAPSZ	(BOZO_WRSZ * 1024)
    165 
    166 /* debug flags */
    167 #define DEBUG_NORMAL	1
    168 #define DEBUG_FAT	2
    169 #define DEBUG_OBESE	3
    170 #define DEBUG_EXPLODING	4
    171 
    172 #define	strornull(x)	((x) ? (x) : "<null>")
    173 
    174 #if defined(__GNUC__) && __GNUC__ >= 3
    175 #define BOZO_PRINTFLIKE(x,y) __attribute__((__format__(__printf__, x,y)))
    176 #define BOZO_DEAD __attribute__((__noreturn__))
    177 #endif
    178 
    179 #ifndef NO_DEBUG
    180 void	debug__(bozohttpd_t *, int, const char *, ...) BOZO_PRINTFLIKE(3, 4);
    181 #define debug(x)	debug__ x
    182 #else
    183 #define	debug(x)
    184 #endif /* NO_DEBUG */
    185 
    186 void	bozo_warn(bozohttpd_t *, const char *, ...)
    187 		BOZO_PRINTFLIKE(2, 3);
    188 void	bozo_err(bozohttpd_t *, int, const char *, ...)
    189 		BOZO_PRINTFLIKE(3, 4)
    190 		BOZO_DEAD;
    191 int	bozo_http_error(bozohttpd_t *, int, bozo_httpreq_t *, const char *);
    192 
    193 int	bozo_check_special_files(bozo_httpreq_t *, const char *);
    194 char	*bozo_http_date(char *, size_t);
    195 void	bozo_print_header(bozo_httpreq_t *, struct stat *, const char *, const char *);
    196 char	*escape_rfc3986(bozohttpd_t *httpd, const char *url);
    197 
    198 char	*bozodgetln(bozohttpd_t *, int, ssize_t *, ssize_t (*)(bozohttpd_t *, int, void *, size_t));
    199 char	*bozostrnsep(char **, const char *, ssize_t *);
    200 
    201 void	*bozomalloc(bozohttpd_t *, size_t);
    202 void	*bozorealloc(bozohttpd_t *, void *, size_t);
    203 char	*bozostrdup(bozohttpd_t *, const char *);
    204 
    205 /* ssl-bozo.c */
    206 #ifdef NO_SSL_SUPPORT
    207 #define bozo_ssl_set_opts(w, x, y)	do { /* nothing */ } while (0)
    208 #define bozo_ssl_init(x)		do { /* nothing */ } while (0)
    209 #define bozo_ssl_accept(x)		do { /* nothing */ } while (0)
    210 #define bozo_ssl_destroy(x)		do { /* nothing */ } while (0)
    211 #else
    212 void	bozo_ssl_set_opts(bozohttpd_t *, const char *, const char *);
    213 void	bozo_ssl_init(bozohttpd_t *);
    214 void	bozo_ssl_accept(bozohttpd_t *);
    215 void	bozo_ssl_destroy(bozohttpd_t *);
    216 #endif
    217 
    218 
    219 /* auth-bozo.c */
    220 #ifdef DO_HTPASSWD
    221 int	bozo_auth_check(bozo_httpreq_t *, const char *);
    222 void	bozo_auth_cleanup(bozo_httpreq_t *);
    223 int	bozo_auth_check_headers(bozo_httpreq_t *, char *, char *, ssize_t);
    224 int	bozo_auth_check_special_files(bozo_httpreq_t *, const char *);
    225 void	bozo_auth_check_401(bozo_httpreq_t *, int);
    226 void	bozo_auth_cgi_setenv(bozo_httpreq_t *, char ***);
    227 int	bozo_auth_cgi_count(bozo_httpreq_t *);
    228 #else
    229 #define	bozo_auth_check(x, y)			0
    230 #define	bozo_auth_cleanup(x)			do { /* nothing */ } while (0)
    231 #define	bozo_auth_check_headers(y, z, a, b)	0
    232 #define	bozo_auth_check_special_files(x, y)	0
    233 #define	bozo_auth_check_401(x, y)		do { /* nothing */ } while (0)
    234 #define	bozo_auth_cgi_setenv(x, y)		do { /* nothing */ } while (0)
    235 #define	bozo_auth_cgi_count(x)			0
    236 #endif /* DO_HTPASSWD */
    237 
    238 
    239 /* cgi-bozo.c */
    240 #ifdef NO_CGIBIN_SUPPORT
    241 #define	bozo_process_cgi(h)				0
    242 #else
    243 void	bozo_cgi_setbin(bozohttpd_t *, const char *);
    244 void	bozo_setenv(bozohttpd_t *, const char *, const char *, char **);
    245 int	bozo_process_cgi(bozo_httpreq_t *);
    246 void	bozo_add_content_map_cgi(bozohttpd_t *, const char *, const char *);
    247 #endif /* NO_CGIBIN_SUPPORT */
    248 
    249 
    250 /* daemon-bozo.c */
    251 #ifdef NO_DAEMON_MODE
    252 #define bozo_daemon_init(x)				do { /* nothing */ } while (0)
    253 #define bozo_daemon_fork(x)				0
    254 #define bozo_daemon_closefds(x)				do { /* nothing */ } while (0)
    255 #else
    256 void	bozo_daemon_init(bozohttpd_t *);
    257 int	bozo_daemon_fork(bozohttpd_t *);
    258 void	bozo_daemon_closefds(bozohttpd_t *);
    259 #endif /* NO_DAEMON_MODE */
    260 
    261 
    262 /* tilde-luzah-bozo.c */
    263 #ifdef NO_USER_SUPPORT
    264 #define bozo_user_transform(a, c)			0
    265 #else
    266 int	bozo_user_transform(bozo_httpreq_t *, int *);
    267 #endif /* NO_USER_SUPPORT */
    268 
    269 
    270 /* dir-index-bozo.c */
    271 #ifdef NO_DIRINDEX_SUPPORT
    272 #define bozo_dir_index(a, b, c)				0
    273 #else
    274 int	bozo_dir_index(bozo_httpreq_t *, const char *, int);
    275 #endif /* NO_DIRINDEX_SUPPORT */
    276 
    277 
    278 /* content-bozo.c */
    279 const char *bozo_content_type(bozo_httpreq_t *, const char *);
    280 const char *bozo_content_encoding(bozo_httpreq_t *, const char *);
    281 bozo_content_map_t *bozo_match_content_map(bozohttpd_t *, const char *, int);
    282 bozo_content_map_t *bozo_get_content_map(bozohttpd_t *, const char *);
    283 #ifndef NO_DYNAMIC_CONTENT
    284 void	bozo_add_content_map_mime(bozohttpd_t *, const char *, const char *, const char *, const char *);
    285 #endif
    286 
    287 /* I/O */
    288 int bozo_printf(bozohttpd_t *, const char *, ...) BOZO_PRINTFLIKE(2, 3);;
    289 ssize_t bozo_read(bozohttpd_t *, int, void *, size_t);
    290 ssize_t bozo_write(bozohttpd_t *, int, const void *, size_t);
    291 int bozo_flush(bozohttpd_t *, FILE *);
    292 
    293 /* misc */
    294 int bozo_init_httpd(bozohttpd_t *);
    295 int bozo_init_prefs(bozoprefs_t *);
    296 int bozo_set_defaults(bozohttpd_t *, bozoprefs_t *);
    297 int bozo_setup(bozohttpd_t *, bozoprefs_t *, const char *, const char *);
    298 bozo_httpreq_t *bozo_read_request(bozohttpd_t *);
    299 void bozo_process_request(bozo_httpreq_t *);
    300 void bozo_clean_request(bozo_httpreq_t *);
    301 
    302 /* variables */
    303 int bozo_set_pref(bozoprefs_t *, const char *, const char *);
    304 char *bozo_get_pref(bozoprefs_t *, const char *);
    305 
    306 #endif	/* BOZOHTTOPD_H_ */
    307