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