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