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