bozohttpd.h revision 1.1.1.7 1 /* $eterna: bozohttpd.h,v 1.31 2010/05/10 14:36:37 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1997-2010 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 size_t page_size; /* page size */
77 char *slashdir; /* www slash directory */
78 char *bindport; /* bind port; default "http" */
79 char *bindaddress; /* address for binding - INADDR_ANY */
80 int debug; /* debugging level */
81 char *virthostname; /* my name */
82 const char *server_software;/* our brand :-) */
83 const char *index_html; /* our home page */
84 const char *public_html; /* ~user/public_html page */
85 int enable_users; /* enable public_html */
86 int *sock; /* bound sockets */
87 int nsock; /* number of above */
88 struct pollfd *fds; /* current poll fd set */
89 int request_times; /* # times a request was processed */
90 int dir_indexing; /* handle directories */
91 int hide_dots; /* hide .* */
92 int process_cgi; /* use the cgi handler */
93 char *cgibin; /* cgi-bin directory */
94 void *sslinfo; /* pointer to ssl struct */
95 int dynamic_content_map_size;/* size of dyn cont map */
96 bozo_content_map_t *dynamic_content_map;/* dynamic content map */
97 size_t mmapsz; /* size of region to mmap */
98 char *getln_buffer; /* space for getln buffer */
99 ssize_t getln_buflen; /* length of allocated space */
100 char *errorbuf; /* no dynamic allocation allowed */
101 bozo_consts_t consts; /* various constants */
102 } bozohttpd_t;
103
104 /* bozo_httpreq_t */
105 typedef struct bozo_httpreq_t {
106 bozohttpd_t *hr_httpd;
107 int hr_method;
108 #define HTTP_GET 0x01
109 #define HTTP_POST 0x02
110 #define HTTP_HEAD 0x03
111 #define HTTP_OPTIONS 0x04 /* not supported */
112 #define HTTP_PUT 0x05 /* not supported */
113 #define HTTP_DELETE 0x06 /* not supported */
114 #define HTTP_TRACE 0x07 /* not supported */
115 #define HTTP_CONNECT 0x08 /* not supported */
116 const char *hr_methodstr;
117 char *hr_file;
118 char *hr_query;
119 const char *hr_proto;
120 const char *hr_content_type;
121 const char *hr_content_length;
122 const char *hr_allow;
123 const char *hr_host; /* HTTP/1.1 Host: */
124 const char *hr_referrer;
125 const char *hr_range;
126 const char *hr_if_modified_since;
127 int hr_have_range;
128 off_t hr_first_byte_pos;
129 off_t hr_last_byte_pos;
130 /*const*/ char *hr_remotehost;
131 /*const*/ char *hr_remoteaddr;
132 /*const*/ char *hr_serverport;
133 #ifdef DO_HTPASSWD
134 /*const*/ char *hr_authrealm;
135 /*const*/ char *hr_authuser;
136 /*const*/ char *hr_authpass;
137 #endif
138 SIMPLEQ_HEAD(, bozoheaders) hr_headers;
139 int hr_nheaders;
140 } bozo_httpreq_t;
141
142 /* structure to hold string based (name, value) pairs with preferences */
143 typedef struct bozoprefs_t {
144 unsigned size; /* size of the two arrays */
145 unsigned c; /* # of entries in arrays */
146 char **name; /* names of each entry */
147 char **value; /* values for the name entries */
148 } bozoprefs_t;
149
150 /* write in upto 64KiB chunks, and mmap in upto 64MiB chunks */
151 #define BOZO_WRSZ (64 * 1024)
152 #define BOZO_MMAPSZ (BOZO_WRSZ * 1024)
153
154 /* debug flags */
155 #define DEBUG_NORMAL 1
156 #define DEBUG_FAT 2
157 #define DEBUG_OBESE 3
158 #define DEBUG_EXPLODING 4
159
160 #define strornull(x) ((x) ? (x) : "<null>")
161
162 #ifdef DEBUG
163 void debug__(bozohttpd_t *, int, const char *, ...)
164 __attribute__((__format__(__printf__, 3, 4)));
165 #define debug(x) debug__ x
166 #else
167 #define debug(x)
168 #endif /* DEBUG */
169
170 void bozo_warn(bozohttpd_t *, const char *, ...)
171 __attribute__((__format__(__printf__, 2, 3)));
172 void bozo_err(bozohttpd_t *, int, const char *, ...)
173 __attribute__((__format__(__printf__, 3, 4)))
174 __attribute__((__noreturn__));
175 int bozo_http_error(bozohttpd_t *, int, bozo_httpreq_t *, const char *)
176 __attribute__((__noreturn__));
177
178 int bozo_check_special_files(bozo_httpreq_t *, const char *);
179 char *bozo_http_date(char *, size_t);
180 void bozo_print_header(bozo_httpreq_t *, struct stat *, const char *, const char *);
181
182 char *bozodgetln(bozohttpd_t *, int, ssize_t *, ssize_t (*)(bozohttpd_t *, int, void *, size_t));
183 char *bozostrnsep(char **, const char *, ssize_t *);
184
185 void *bozomalloc(bozohttpd_t *, size_t);
186 void *bozorealloc(bozohttpd_t *, void *, size_t);
187 char *bozostrdup(bozohttpd_t *, const char *);
188
189 /* ssl-bozo.c */
190 #ifdef NO_SSL_SUPPORT
191 #define bozo_ssl_set_opts(w, x, y) /* nothing */
192 #define bozo_ssl_init(x) /* nothing */
193 #define bozo_ssl_accept(x) /* nothing */
194 #define bozo_ssl_destroy(x) /* nothing */
195 #else
196 extern void bozo_ssl_set_opts(bozohttpd_t *, const char *, const char *);
197 extern void bozo_ssl_init(bozohttpd_t *);
198 extern void bozo_ssl_accept(bozohttpd_t *);
199 extern void bozo_ssl_destroy(bozohttpd_t *);
200 #endif
201
202
203 /* auth-bozo.c */
204 #ifdef DO_HTPASSWD
205 extern int bozo_auth_check(bozo_httpreq_t *, const char *);
206 extern void bozo_auth_cleanup(bozo_httpreq_t *);
207 extern int bozo_auth_check_headers(bozo_httpreq_t *, char *, char *, ssize_t);
208 extern int bozo_auth_check_special_files(bozo_httpreq_t *, const char *);
209 extern void bozo_auth_check_401(bozo_httpreq_t *, int);
210 extern void bozo_auth_cgi_setenv(bozo_httpreq_t *, char ***);
211 extern int bozo_auth_cgi_count(bozo_httpreq_t *);
212 #else
213 #define bozo_auth_check(x, y) 0
214 #define bozo_auth_cleanup(x) /* nothing */
215 #define bozo_auth_check_headers(y, z, a, b) 0
216 #define bozo_auth_check_special_files(x, y) 0
217 #define bozo_auth_check_401(x, y) /* nothing */
218 #define bozo_auth_cgi_setenv(x, y) /* nothing */
219 #define bozo_auth_cgi_count(x) 0
220 #endif /* DO_HTPASSWD */
221
222
223 /* cgi-bozo.c */
224 #ifdef NO_CGIBIN_SUPPORT
225 #define bozo_process_cgi(h, r) 0
226 #else
227 extern void bozo_cgi_setbin(bozohttpd_t *, const char *);
228 extern void bozo_setenv(bozohttpd_t *, const char *, const char *, char **);
229 extern int bozo_process_cgi(bozo_httpreq_t *);
230 extern void bozo_add_content_map_cgi(bozohttpd_t *, const char *, const char *);
231 #endif /* NO_CGIBIN_SUPPORT */
232
233
234 /* daemon-bozo.c */
235 #ifdef NO_DAEMON_MODE
236 #define bozo_daemon_init(x) /* nothing */
237 #define bozo_daemon_fork(x) /* nothing */
238 #define bozo_daemon_closefds() /* nothing */
239 #else
240 extern void bozo_daemon_init(bozohttpd_t *);
241 extern void bozo_daemon_fork(bozohttpd_t *);
242 extern void bozo_daemon_closefds(bozohttpd_t *);
243 #endif /* NO_DAEMON_MODE */
244
245
246 /* tilde-luzah-bozo.c */
247 #ifdef NO_USER_SUPPORT
248 #define bozo_user_transform(a, b, c) 0
249 #else
250 int bozo_user_transform(bozo_httpreq_t *, int *);
251 #endif /* NO_USER_SUPPORT */
252
253
254 /* dir-index-bozo.c */
255 #ifdef NO_DIRINDEX_SUPPORT
256 #define bozo_dir_index(a, b, c, d) 0
257 #else
258 int bozo_dir_index(bozo_httpreq_t *, const char *, int);
259 #endif /* NO_DIRINDEX_SUPPORT */
260
261
262 /* content-bozo.c */
263 extern const char *bozo_content_type(bozo_httpreq_t *, const char *);
264 extern const char *bozo_content_encoding(bozo_httpreq_t *, const char *);
265 extern bozo_content_map_t *bozo_match_content_map(bozohttpd_t *, const char *, int);
266 extern bozo_content_map_t *bozo_get_content_map(bozohttpd_t *, const char *);
267 #ifndef NO_DYNAMIC_CONTENT
268 void bozo_add_content_map_mime(bozohttpd_t *, const char *, const char *, const char *, const char *);
269 #endif
270
271 /* I/O */
272 int bozo_printf(bozohttpd_t *, const char *, ...);
273 ssize_t bozo_read(bozohttpd_t *, int, void *, size_t);
274 ssize_t bozo_write(bozohttpd_t *, int, const void *, size_t);
275 int bozo_flush(bozohttpd_t *, FILE *);
276
277 /* misc */
278 int bozo_init_httpd(bozohttpd_t *);
279 int bozo_init_prefs(bozoprefs_t *);
280 int bozo_set_defaults(bozohttpd_t *, bozoprefs_t *);
281 int bozo_setup(bozohttpd_t *, bozoprefs_t *, const char *, const char *);
282 bozo_httpreq_t *bozo_read_request(bozohttpd_t *);
283 void bozo_process_request(bozo_httpreq_t *);
284 void bozo_clean_request(bozo_httpreq_t *);
285
286 /* variables */
287 int bozo_set_pref(bozoprefs_t *, const char *, const char *);
288 char *bozo_get_pref(bozoprefs_t *, const char *);
289
290 #endif /* BOZOHTTOPD_H_ */
291