content-bozo.c revision 1.1.1.5 1 1.1.1.5 mrg /* $eterna: content-bozo.c,v 1.17 2011/11/18 09:21:15 mrg Exp $ */
2 1.1 tls
3 1.1 tls /*
4 1.1.1.5 mrg * Copyright (c) 1997-2011 Matthew R. Green
5 1.1 tls * All rights reserved.
6 1.1 tls *
7 1.1 tls * Redistribution and use in source and binary forms, with or without
8 1.1 tls * modification, are permitted provided that the following conditions
9 1.1 tls * are met:
10 1.1 tls * 1. Redistributions of source code must retain the above copyright
11 1.1 tls * notice, this list of conditions and the following disclaimer.
12 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tls * notice, this list of conditions and the following disclaimer and
14 1.1 tls * dedication in the documentation and/or other materials provided
15 1.1 tls * with the distribution.
16 1.1 tls *
17 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 tls * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 tls * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 tls * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 tls * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 1.1 tls * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 1.1 tls * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 1.1 tls * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 1.1 tls * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 tls * SUCH DAMAGE.
28 1.1 tls *
29 1.1 tls */
30 1.1 tls
31 1.1 tls /* this code implements content-type handling for bozohttpd */
32 1.1 tls
33 1.1 tls #include <sys/param.h>
34 1.1 tls
35 1.1.1.3 mrg #include <errno.h>
36 1.1 tls #include <string.h>
37 1.1 tls
38 1.1 tls #include "bozohttpd.h"
39 1.1 tls
40 1.1 tls /*
41 1.1 tls * this map and the functions below map between filenames and the
42 1.1 tls * content type and content encoding definitions. this should become
43 1.1 tls * a configuration file, perhaps like apache's mime.types (but that
44 1.1 tls * has less info per-entry).
45 1.1 tls */
46 1.1 tls
47 1.1.1.4 mrg static bozo_content_map_t static_content_map[] = {
48 1.1.1.4 mrg { ".html", 5, "text/html", "", "", NULL },
49 1.1.1.4 mrg { ".htm", 4, "text/html", "", "", NULL },
50 1.1.1.4 mrg { ".gif", 4, "image/gif", "", "", NULL },
51 1.1.1.4 mrg { ".jpeg", 5, "image/jpeg", "", "", NULL },
52 1.1.1.4 mrg { ".jpg", 4, "image/jpeg", "", "", NULL },
53 1.1.1.4 mrg { ".jpe", 4, "image/jpeg", "", "", NULL },
54 1.1.1.4 mrg { ".png", 4, "image/png", "", "", NULL },
55 1.1.1.4 mrg { ".mp3", 4, "audio/mpeg", "", "", NULL },
56 1.1.1.4 mrg { ".css", 4, "text/css", "", "", NULL },
57 1.1.1.4 mrg { ".txt", 4, "text/plain", "", "", NULL },
58 1.1.1.4 mrg { ".swf", 4, "application/x-shockwave-flash","", "", NULL },
59 1.1.1.4 mrg { ".dcr", 4, "application/x-director", "", "", NULL },
60 1.1.1.4 mrg { ".pac", 4, "application/x-ns-proxy-autoconfig", "", "", NULL },
61 1.1.1.4 mrg { ".pa", 3, "application/x-ns-proxy-autoconfig", "", "", NULL },
62 1.1.1.4 mrg { ".tar", 4, "multipart/x-tar", "", "", NULL },
63 1.1.1.4 mrg { ".gtar", 5, "multipart/x-gtar", "", "", NULL },
64 1.1.1.4 mrg { ".tar.Z", 6, "multipart/x-tar", "x-compress", "compress", NULL },
65 1.1.1.4 mrg { ".tar.gz", 7, "multipart/x-tar", "x-gzip", "gzip", NULL },
66 1.1.1.4 mrg { ".taz", 4, "multipart/x-tar", "x-gzip", "gzip", NULL },
67 1.1.1.4 mrg { ".tgz", 4, "multipart/x-tar", "x-gzip", "gzip", NULL },
68 1.1.1.4 mrg { ".tar.z", 6, "multipart/x-tar", "x-pack", "x-pack", NULL },
69 1.1.1.4 mrg { ".Z", 2, "application/x-compress", "x-compress", "compress", NULL },
70 1.1.1.4 mrg { ".gz", 3, "application/x-gzip", "x-gzip", "gzip", NULL },
71 1.1.1.4 mrg { ".z", 2, "unknown", "x-pack", "x-pack", NULL },
72 1.1.1.4 mrg { ".bz2", 4, "application/x-bzip2", "x-bzip2", "x-bzip2", NULL },
73 1.1.1.4 mrg { ".ogg", 4, "application/x-ogg", "", "", NULL },
74 1.1.1.4 mrg { ".xbel", 5, "text/xml", "", "", NULL },
75 1.1.1.4 mrg { ".xml", 4, "text/xml", "", "", NULL },
76 1.1.1.4 mrg { ".xsl", 4, "text/xml", "", "", NULL },
77 1.1.1.4 mrg { ".hqx", 4, "application/mac-binhex40", "", "", NULL },
78 1.1.1.4 mrg { ".cpt", 4, "application/mac-compactpro","", "", NULL },
79 1.1.1.4 mrg { ".doc", 4, "application/msword", "", "", NULL },
80 1.1.1.4 mrg { ".bin", 4, "application/octet-stream", "", "", NULL },
81 1.1.1.4 mrg { ".dms", 4, "application/octet-stream", "", "", NULL },
82 1.1.1.4 mrg { ".lha", 4, "application/octet-stream", "", "", NULL },
83 1.1.1.4 mrg { ".lzh", 4, "application/octet-stream", "", "", NULL },
84 1.1.1.4 mrg { ".exe", 4, "application/octet-stream", "", "", NULL },
85 1.1.1.4 mrg { ".class", 6, "application/octet-stream", "", "", NULL },
86 1.1.1.4 mrg { ".oda", 4, "application/oda", "", "", NULL },
87 1.1.1.4 mrg { ".pdf", 4, "application/pdf", "", "", NULL },
88 1.1.1.4 mrg { ".ai", 3, "application/postscript", "", "", NULL },
89 1.1.1.4 mrg { ".eps", 4, "application/postscript", "", "", NULL },
90 1.1.1.4 mrg { ".ps", 3, "application/postscript", "", "", NULL },
91 1.1.1.4 mrg { ".ppt", 4, "application/powerpoint", "", "", NULL },
92 1.1.1.4 mrg { ".rtf", 4, "application/rtf", "", "", NULL },
93 1.1.1.4 mrg { ".bcpio", 6, "application/x-bcpio", "", "", NULL },
94 1.1.1.4 mrg { ".torrent", 8, "application/x-bittorrent", "", "", NULL },
95 1.1.1.4 mrg { ".vcd", 4, "application/x-cdlink", "", "", NULL },
96 1.1.1.4 mrg { ".cpio", 5, "application/x-cpio", "", "", NULL },
97 1.1.1.4 mrg { ".csh", 4, "application/x-csh", "", "", NULL },
98 1.1.1.4 mrg { ".dir", 4, "application/x-director", "", "", NULL },
99 1.1.1.4 mrg { ".dxr", 4, "application/x-director", "", "", NULL },
100 1.1.1.4 mrg { ".dvi", 4, "application/x-dvi", "", "", NULL },
101 1.1.1.4 mrg { ".hdf", 4, "application/x-hdf", "", "", NULL },
102 1.1.1.4 mrg { ".cgi", 4, "application/x-httpd-cgi", "", "", NULL },
103 1.1.1.4 mrg { ".skp", 4, "application/x-koan", "", "", NULL },
104 1.1.1.4 mrg { ".skd", 4, "application/x-koan", "", "", NULL },
105 1.1.1.4 mrg { ".skt", 4, "application/x-koan", "", "", NULL },
106 1.1.1.4 mrg { ".skm", 4, "application/x-koan", "", "", NULL },
107 1.1.1.4 mrg { ".latex", 6, "application/x-latex", "", "", NULL },
108 1.1.1.4 mrg { ".mif", 4, "application/x-mif", "", "", NULL },
109 1.1.1.4 mrg { ".nc", 3, "application/x-netcdf", "", "", NULL },
110 1.1.1.4 mrg { ".cdf", 4, "application/x-netcdf", "", "", NULL },
111 1.1.1.4 mrg { ".patch", 6, "application/x-patch", "", "", NULL },
112 1.1.1.4 mrg { ".sh", 3, "application/x-sh", "", "", NULL },
113 1.1.1.4 mrg { ".shar", 5, "application/x-shar", "", "", NULL },
114 1.1.1.4 mrg { ".sit", 4, "application/x-stuffit", "", "", NULL },
115 1.1.1.4 mrg { ".sv4cpio", 8, "application/x-sv4cpio", "", "", NULL },
116 1.1.1.4 mrg { ".sv4crc", 7, "application/x-sv4crc", "", "", NULL },
117 1.1.1.4 mrg { ".tar", 4, "application/x-tar", "", "", NULL },
118 1.1.1.4 mrg { ".tcl", 4, "application/x-tcl", "", "", NULL },
119 1.1.1.4 mrg { ".tex", 4, "application/x-tex", "", "", NULL },
120 1.1.1.4 mrg { ".texinfo", 8, "application/x-texinfo", "", "", NULL },
121 1.1.1.4 mrg { ".texi", 5, "application/x-texinfo", "", "", NULL },
122 1.1.1.4 mrg { ".t", 2, "application/x-troff", "", "", NULL },
123 1.1.1.4 mrg { ".tr", 3, "application/x-troff", "", "", NULL },
124 1.1.1.4 mrg { ".roff", 5, "application/x-troff", "", "", NULL },
125 1.1.1.4 mrg { ".man", 4, "application/x-troff-man", "", "", NULL },
126 1.1.1.4 mrg { ".me", 3, "application/x-troff-me", "", "", NULL },
127 1.1.1.4 mrg { ".ms", 3, "application/x-troff-ms", "", "", NULL },
128 1.1.1.4 mrg { ".ustar", 6, "application/x-ustar", "", "", NULL },
129 1.1.1.4 mrg { ".src", 4, "application/x-wais-source", "", "", NULL },
130 1.1.1.4 mrg { ".zip", 4, "application/zip", "", "", NULL },
131 1.1.1.4 mrg { ".au", 3, "audio/basic", "", "", NULL },
132 1.1.1.4 mrg { ".snd", 4, "audio/basic", "", "", NULL },
133 1.1.1.4 mrg { ".mpga", 5, "audio/mpeg", "", "", NULL },
134 1.1.1.4 mrg { ".mp2", 4, "audio/mpeg", "", "", NULL },
135 1.1.1.4 mrg { ".aif", 4, "audio/x-aiff", "", "", NULL },
136 1.1.1.4 mrg { ".aiff", 5, "audio/x-aiff", "", "", NULL },
137 1.1.1.4 mrg { ".aifc", 5, "audio/x-aiff", "", "", NULL },
138 1.1.1.4 mrg { ".ram", 4, "audio/x-pn-realaudio", "", "", NULL },
139 1.1.1.4 mrg { ".rpm", 4, "audio/x-pn-realaudio-plugin","", "", NULL },
140 1.1.1.4 mrg { ".ra", 3, "audio/x-realaudio", "", "", NULL },
141 1.1.1.4 mrg { ".wav", 4, "audio/x-wav", "", "", NULL },
142 1.1.1.4 mrg { ".pdb", 4, "chemical/x-pdb", "", "", NULL },
143 1.1.1.4 mrg { ".xyz", 4, "chemical/x-pdb", "", "", NULL },
144 1.1.1.4 mrg { ".ief", 4, "image/ief", "", "", NULL },
145 1.1.1.4 mrg { ".tiff", 5, "image/tiff", "", "", NULL },
146 1.1.1.4 mrg { ".tif", 4, "image/tiff", "", "", NULL },
147 1.1.1.4 mrg { ".ras", 4, "image/x-cmu-raster", "", "", NULL },
148 1.1.1.4 mrg { ".pnm", 4, "image/x-portable-anymap", "", "", NULL },
149 1.1.1.4 mrg { ".pbm", 4, "image/x-portable-bitmap", "", "", NULL },
150 1.1.1.4 mrg { ".pgm", 4, "image/x-portable-graymap", "", "", NULL },
151 1.1.1.4 mrg { ".ppm", 4, "image/x-portable-pixmap", "", "", NULL },
152 1.1.1.4 mrg { ".rgb", 4, "image/x-rgb", "", "", NULL },
153 1.1.1.4 mrg { ".xbm", 4, "image/x-xbitmap", "", "", NULL },
154 1.1.1.4 mrg { ".xpm", 4, "image/x-xpixmap", "", "", NULL },
155 1.1.1.4 mrg { ".xwd", 4, "image/x-xwindowdump", "", "", NULL },
156 1.1.1.4 mrg { ".rtx", 4, "text/richtext", "", "", NULL },
157 1.1.1.4 mrg { ".tsv", 4, "text/tab-separated-values", "", "", NULL },
158 1.1.1.4 mrg { ".etx", 4, "text/x-setext", "", "", NULL },
159 1.1.1.4 mrg { ".sgml", 5, "text/x-sgml", "", "", NULL },
160 1.1.1.4 mrg { ".sgm", 4, "text/x-sgml", "", "", NULL },
161 1.1.1.4 mrg { ".mpeg", 5, "video/mpeg", "", "", NULL },
162 1.1.1.4 mrg { ".mpg", 4, "video/mpeg", "", "", NULL },
163 1.1.1.4 mrg { ".mpe", 4, "video/mpeg", "", "", NULL },
164 1.1.1.5 mrg { ".mp4", 4, "video/mp4", "", "", NULL },
165 1.1.1.4 mrg { ".qt", 3, "video/quicktime", "", "", NULL },
166 1.1.1.4 mrg { ".mov", 4, "video/quicktime", "", "", NULL },
167 1.1.1.4 mrg { ".avi", 4, "video/x-msvideo", "", "", NULL },
168 1.1.1.4 mrg { ".movie", 6, "video/x-sgi-movie", "", "", NULL },
169 1.1.1.4 mrg { ".ice", 4, "x-conference/x-cooltalk", "", "", NULL },
170 1.1.1.4 mrg { ".wrl", 4, "x-world/x-vrml", "", "", NULL },
171 1.1.1.4 mrg { ".vrml", 5, "x-world/x-vrml", "", "", NULL },
172 1.1.1.4 mrg { NULL, 0, NULL, NULL, NULL, NULL }
173 1.1 tls };
174 1.1 tls
175 1.1.1.4 mrg static bozo_content_map_t *
176 1.1.1.4 mrg search_map(bozo_content_map_t *map, const char *name, size_t len)
177 1.1.1.4 mrg {
178 1.1.1.4 mrg for ( ; map && map->name; map++) {
179 1.1.1.4 mrg if (map->namelen < len &&
180 1.1.1.4 mrg strcasecmp(map->name, name + (len - map->namelen)) == 0)
181 1.1.1.4 mrg return map;
182 1.1.1.4 mrg }
183 1.1.1.4 mrg return NULL;
184 1.1.1.4 mrg }
185 1.1 tls
186 1.1.1.4 mrg /* match a suffix on a file - dynamiconly means no static content search */
187 1.1.1.4 mrg bozo_content_map_t *
188 1.1.1.4 mrg bozo_match_content_map(bozohttpd_t *httpd, const char *name,
189 1.1.1.4 mrg const int dynamiconly)
190 1.1 tls {
191 1.1.1.4 mrg bozo_content_map_t *map;
192 1.1.1.4 mrg size_t len;
193 1.1 tls
194 1.1.1.4 mrg len = strlen(name);
195 1.1.1.4 mrg if ((map = search_map(httpd->dynamic_content_map, name, len)) != NULL) {
196 1.1.1.4 mrg return map;
197 1.1 tls }
198 1.1.1.4 mrg if (!dynamiconly) {
199 1.1.1.4 mrg if ((map = search_map(static_content_map, name, len)) != NULL) {
200 1.1.1.4 mrg return map;
201 1.1.1.4 mrg }
202 1.1 tls }
203 1.1.1.4 mrg return NULL;
204 1.1 tls }
205 1.1 tls
206 1.1 tls /*
207 1.1 tls * given the file name, return a valid Content-Type: value.
208 1.1 tls */
209 1.1 tls /* ARGSUSED */
210 1.1 tls const char *
211 1.1.1.4 mrg bozo_content_type(bozo_httpreq_t *request, const char *file)
212 1.1 tls {
213 1.1.1.4 mrg bozohttpd_t *httpd = request->hr_httpd;
214 1.1.1.4 mrg bozo_content_map_t *map;
215 1.1 tls
216 1.1.1.4 mrg map = bozo_match_content_map(httpd, file, 0);
217 1.1 tls if (map)
218 1.1.1.4 mrg return map->type;
219 1.1.1.4 mrg return httpd->consts.text_plain;
220 1.1 tls }
221 1.1 tls
222 1.1 tls /*
223 1.1 tls * given the file name, return a valid Content-Encoding: value.
224 1.1 tls */
225 1.1 tls const char *
226 1.1.1.4 mrg bozo_content_encoding(bozo_httpreq_t *request, const char *file)
227 1.1 tls {
228 1.1.1.4 mrg bozohttpd_t *httpd = request->hr_httpd;
229 1.1.1.4 mrg bozo_content_map_t *map;
230 1.1 tls
231 1.1.1.4 mrg map = bozo_match_content_map(httpd, file, 0);
232 1.1 tls if (map)
233 1.1.1.4 mrg return (request->hr_proto == httpd->consts.http_11) ?
234 1.1.1.4 mrg map->encoding11 : map->encoding;
235 1.1.1.4 mrg return NULL;
236 1.1 tls }
237 1.1 tls
238 1.1 tls #ifndef NO_DYNAMIC_CONTENT
239 1.1 tls
240 1.1.1.4 mrg bozo_content_map_t *
241 1.1.1.4 mrg bozo_get_content_map(bozohttpd_t *httpd, const char *name)
242 1.1 tls {
243 1.1.1.4 mrg bozo_content_map_t *map;
244 1.1 tls
245 1.1.1.4 mrg if ((map = bozo_match_content_map(httpd, name, 1)) != NULL)
246 1.1.1.4 mrg return map;
247 1.1 tls
248 1.1.1.4 mrg httpd->dynamic_content_map_size++;
249 1.1.1.4 mrg httpd->dynamic_content_map = bozorealloc(httpd,
250 1.1.1.4 mrg httpd->dynamic_content_map,
251 1.1.1.4 mrg (httpd->dynamic_content_map_size + 1) * sizeof *map);
252 1.1.1.4 mrg if (httpd->dynamic_content_map == NULL)
253 1.1.1.4 mrg bozo_err(httpd, 1, "out of memory allocating content map");
254 1.1.1.4 mrg map = &httpd->dynamic_content_map[httpd->dynamic_content_map_size];
255 1.1 tls map->name = map->type = map->encoding = map->encoding11 =
256 1.1.1.4 mrg map->cgihandler = NULL;
257 1.1.1.4 mrg map->namelen = 0;
258 1.1 tls map--;
259 1.1 tls
260 1.1.1.4 mrg return map;
261 1.1 tls }
262 1.1 tls
263 1.1 tls /*
264 1.1 tls * mime content maps look like:
265 1.1 tls * ".name type encoding encoding11"
266 1.1 tls * where any of type, encoding or encoding11 a dash "-" means "".
267 1.1 tls * eg the .gtar, .tar.Z from above could be written like:
268 1.1 tls * ".gtar multipart/x-gtar - -"
269 1.1 tls * ".tar.Z multipart/x-tar x-compress compress"
270 1.1 tls * or
271 1.1 tls * ".gtar multipart/x-gtar"
272 1.1 tls * ".tar.Z multipart/x-tar x-compress compress"
273 1.1 tls * NOTE: we destroy 'arg'
274 1.1 tls */
275 1.1 tls void
276 1.1.1.4 mrg bozo_add_content_map_mime(bozohttpd_t *httpd, const char *cmap0,
277 1.1.1.4 mrg const char *cmap1, const char *cmap2, const char *cmap3)
278 1.1 tls {
279 1.1.1.4 mrg bozo_content_map_t *map;
280 1.1 tls
281 1.1.1.4 mrg debug((httpd, DEBUG_FAT,
282 1.1.1.4 mrg "add_content_map: name %s type %s enc %s enc11 %s ",
283 1.1 tls cmap0, cmap1, cmap2, cmap3));
284 1.1 tls
285 1.1.1.4 mrg map = bozo_get_content_map(httpd, cmap0);
286 1.1 tls #define CHECKMAP(s) (!s || ((s)[0] == '-' && (s)[1] == '\0') ? "" : (s))
287 1.1 tls map->name = CHECKMAP(cmap0);
288 1.1.1.4 mrg map->namelen = strlen(map->name);
289 1.1 tls map->type = CHECKMAP(cmap1);
290 1.1 tls map->encoding = CHECKMAP(cmap2);
291 1.1 tls map->encoding11 = CHECKMAP(cmap3);
292 1.1 tls #undef CHECKMAP
293 1.1 tls map->cgihandler = NULL;
294 1.1 tls }
295 1.1 tls #endif /* NO_DYNAMIC_CONTENT */
296