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