content-bozo.c revision 1.1 1 /* $eterna: content-bozo.c,v 1.8 2006/05/17 08:19:10 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1997-2006 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 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
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
33 /* this code implements content-type handling for bozohttpd */
34
35 #include <sys/param.h>
36
37 #include <string.h>
38
39 #include "bozohttpd.h"
40
41 /*
42 * this map and the functions below map between filenames and the
43 * content type and content encoding definitions. this should become
44 * a configuration file, perhaps like apache's mime.types (but that
45 * has less info per-entry).
46 */
47
48 static struct content_map content_map[] = {
49 { ".html", "text/html", "", "", NULL },
50 { ".htm", "text/html", "", "", NULL },
51 { ".gif", "image/gif", "", "", NULL },
52 { ".jpeg", "image/jpeg", "", "", NULL },
53 { ".jpg", "image/jpeg", "", "", NULL },
54 { ".jpe", "image/jpeg", "", "", NULL },
55 { ".png", "image/png", "", "", NULL },
56 { ".mp3", "audio/mpeg", "", "", NULL },
57 { ".css", "text/css", "", "", NULL },
58 { ".txt", "text/plain", "", "", NULL },
59 { ".swf", "application/x-shockwave-flash","", "", NULL },
60 { ".dcr", "application/x-director", "", "", NULL },
61 { ".pac", "application/x-ns-proxy-autoconfig", "", "", NULL },
62 { ".pa", "application/x-ns-proxy-autoconfig", "", "", NULL },
63 { ".tar", "multipart/x-tar", "", "", NULL },
64 { ".gtar", "multipart/x-gtar", "", "", NULL },
65 { ".tar.Z", "multipart/x-tar", "x-compress", "compress", NULL },
66 { ".tar.gz", "multipart/x-tar", "x-gzip", "gzip", NULL },
67 { ".taz", "multipart/x-tar", "x-gzip", "gzip", NULL },
68 { ".tgz", "multipart/x-tar", "x-gzip", "gzip", NULL },
69 { ".tar.z", "multipart/x-tar", "x-pack", "x-pack", NULL },
70 { ".Z", "application/x-compress", "x-compress", "compress", NULL },
71 { ".gz", "application/x-gzip", "x-gzip", "gzip", NULL },
72 { ".z", "unknown", "x-pack", "x-pack", NULL },
73 { ".bz2", "application/x-bzip2", "x-bzip2", "x-bzip2", NULL },
74 { ".ogg", "application/x-ogg", "", "", NULL },
75 { ".xbel", "text/xml", "", "", NULL },
76 { ".xml", "text/xml", "", "", NULL },
77 { ".xsl", "text/xml", "", "", NULL },
78 { ".hqx", "application/mac-binhex40", "", "", NULL },
79 { ".cpt", "application/mac-compactpro", "", "", NULL },
80 { ".doc", "application/msword", "", "", NULL },
81 { ".bin", "application/octet-stream", "", "", NULL },
82 { ".dms", "application/octet-stream", "", "", NULL },
83 { ".lha", "application/octet-stream", "", "", NULL },
84 { ".lzh", "application/octet-stream", "", "", NULL },
85 { ".exe", "application/octet-stream", "", "", NULL },
86 { ".class", "application/octet-stream", "", "", NULL },
87 { ".oda", "application/oda", "", "", NULL },
88 { ".pdf", "application/pdf", "", "", NULL },
89 { ".ai", "application/postscript", "", "", NULL },
90 { ".eps", "application/postscript", "", "", NULL },
91 { ".ps", "application/postscript", "", "", NULL },
92 { ".ppt", "application/powerpoint", "", "", NULL },
93 { ".rtf", "application/rtf", "", "", NULL },
94 { ".bcpio", "application/x-bcpio", "", "", NULL },
95 { ".torrent", "application/x-bittorrent", "", "", NULL },
96 { ".vcd", "application/x-cdlink", "", "", NULL },
97 { ".cpio", "application/x-cpio", "", "", NULL },
98 { ".csh", "application/x-csh", "", "", NULL },
99 { ".dir", "application/x-director", "", "", NULL },
100 { ".dxr", "application/x-director", "", "", NULL },
101 { ".dvi", "application/x-dvi", "", "", NULL },
102 { ".hdf", "application/x-hdf", "", "", NULL },
103 { ".cgi", "application/x-httpd-cgi", "", "", NULL },
104 { ".skp", "application/x-koan", "", "", NULL },
105 { ".skd", "application/x-koan", "", "", NULL },
106 { ".skt", "application/x-koan", "", "", NULL },
107 { ".skm", "application/x-koan", "", "", NULL },
108 { ".latex", "application/x-latex", "", "", NULL },
109 { ".mif", "application/x-mif", "", "", NULL },
110 { ".nc", "application/x-netcdf", "", "", NULL },
111 { ".cdf", "application/x-netcdf", "", "", NULL },
112 { ".patch", "application/x-patch", "", "", NULL },
113 { ".sh", "application/x-sh", "", "", NULL },
114 { ".shar", "application/x-shar", "", "", NULL },
115 { ".sit", "application/x-stuffit", "", "", NULL },
116 { ".sv4cpio", "application/x-sv4cpio", "", "", NULL },
117 { ".sv4crc", "application/x-sv4crc", "", "", NULL },
118 { ".tar", "application/x-tar", "", "", NULL },
119 { ".tcl", "application/x-tcl", "", "", NULL },
120 { ".tex", "application/x-tex", "", "", NULL },
121 { ".texinfo", "application/x-texinfo", "", "", NULL },
122 { ".texi", "application/x-texinfo", "", "", NULL },
123 { ".t", "application/x-troff", "", "", NULL },
124 { ".tr", "application/x-troff", "", "", NULL },
125 { ".roff", "application/x-troff", "", "", NULL },
126 { ".man", "application/x-troff-man", "", "", NULL },
127 { ".me", "application/x-troff-me", "", "", NULL },
128 { ".ms", "application/x-troff-ms", "", "", NULL },
129 { ".ustar", "application/x-ustar", "", "", NULL },
130 { ".src", "application/x-wais-source", "", "", NULL },
131 { ".zip", "application/zip", "", "", NULL },
132 { ".au", "audio/basic", "", "", NULL },
133 { ".snd", "audio/basic", "", "", NULL },
134 { ".mpga", "audio/mpeg", "", "", NULL },
135 { ".mp2", "audio/mpeg", "", "", NULL },
136 { ".aif", "audio/x-aiff", "", "", NULL },
137 { ".aiff", "audio/x-aiff", "", "", NULL },
138 { ".aifc", "audio/x-aiff", "", "", NULL },
139 { ".ram", "audio/x-pn-realaudio", "", "", NULL },
140 { ".rpm", "audio/x-pn-realaudio-plugin", "", "", NULL },
141 { ".ra", "audio/x-realaudio", "", "", NULL },
142 { ".wav", "audio/x-wav", "", "", NULL },
143 { ".pdb", "chemical/x-pdb", "", "", NULL },
144 { ".xyz", "chemical/x-pdb", "", "", NULL },
145 { ".ief", "image/ief", "", "", NULL },
146 { ".tiff", "image/tiff", "", "", NULL },
147 { ".tif", "image/tiff", "", "", NULL },
148 { ".ras", "image/x-cmu-raster", "", "", NULL },
149 { ".pnm", "image/x-portable-anymap", "", "", NULL },
150 { ".pbm", "image/x-portable-bitmap", "", "", NULL },
151 { ".pgm", "image/x-portable-graymap", "", "", NULL },
152 { ".ppm", "image/x-portable-pixmap", "", "", NULL },
153 { ".rgb", "image/x-rgb", "", "", NULL },
154 { ".xbm", "image/x-xbitmap", "", "", NULL },
155 { ".xpm", "image/x-xpixmap", "", "", NULL },
156 { ".xwd", "image/x-xwindowdump", "", "", NULL },
157 { ".rtx", "text/richtext", "", "", NULL },
158 { ".tsv", "text/tab-separated-values", "", "", NULL },
159 { ".etx", "text/x-setext", "", "", NULL },
160 { ".sgml", "text/x-sgml", "", "", NULL },
161 { ".sgm", "text/x-sgml", "", "", NULL },
162 { ".mpeg", "video/mpeg", "", "", NULL },
163 { ".mpg", "video/mpeg", "", "", NULL },
164 { ".mpe", "video/mpeg", "", "", NULL },
165 { ".qt", "video/quicktime", "", "", NULL },
166 { ".mov", "video/quicktime", "", "", NULL },
167 { ".avi", "video/x-msvideo", "", "", NULL },
168 { ".movie", "video/x-sgi-movie", "", "", NULL },
169 { ".ice", "x-conference/x-cooltalk", "", "", NULL },
170 { ".wrl", "x-world/x-vrml", "", "", NULL },
171 { ".vrml", "x-world/x-vrml", "", "", NULL },
172 { NULL, NULL, NULL, NULL, NULL, },
173 };
174
175 static struct content_map *dynamic_content_map;
176
177 /* call with stage == 0 for full search, stage == 1 for dynamic only */
178 struct content_map *
179 match_content_map(const char *name, int stage)
180 {
181 size_t len = strlen(name), nlen;
182 struct content_map *map;
183
184 for (map = dynamic_content_map; map && map->name; map++) {
185 again:
186 nlen = strlen(map->name);
187 if (nlen > len || strcasecmp(map->name, name + (len - nlen)) != 0)
188 continue;
189 return (map);
190 }
191 if (stage++ == 0) {
192 map = content_map;
193 goto again;
194 }
195
196 return (NULL);
197 }
198
199 /*
200 * given the file name, return a valid Content-Type: value.
201 */
202 /* ARGSUSED */
203 const char *
204 content_type(http_req *request, const char *file)
205 {
206 struct content_map *map;
207
208 map = match_content_map(file, 0);
209 if (map)
210 return (map->type);
211 return (text_plain);
212 }
213
214 /*
215 * given the file name, return a valid Content-Encoding: value.
216 */
217 const char *
218 content_encoding(http_req *request, const char *file)
219 {
220 struct content_map *map;
221
222 map = match_content_map(file, 0);
223 if (map)
224 return (request->hr_proto == http_11 ?
225 map->encoding11 : map->encoding);
226 return (NULL);
227 }
228
229 #ifndef NO_DYNAMIC_CONTENT
230 static int dynamic_content_map_size;
231
232 struct content_map *
233 get_content_map(const char *name)
234 {
235 struct content_map *map;
236
237 if ((map = match_content_map(name, 1)))
238 return (map);
239
240 dynamic_content_map_size++;
241 dynamic_content_map = bozorealloc(dynamic_content_map,
242 (dynamic_content_map_size + 1) * sizeof *map);
243 map = &dynamic_content_map[dynamic_content_map_size];
244 map->name = map->type = map->encoding = map->encoding11 =
245 map->cgihandler = NULL;
246 map--;
247
248 return (map);
249 }
250
251 /*
252 * mime content maps look like:
253 * ".name type encoding encoding11"
254 * where any of type, encoding or encoding11 a dash "-" means "".
255 * eg the .gtar, .tar.Z from above could be written like:
256 * ".gtar multipart/x-gtar - -"
257 * ".tar.Z multipart/x-tar x-compress compress"
258 * or
259 * ".gtar multipart/x-gtar"
260 * ".tar.Z multipart/x-tar x-compress compress"
261 * NOTE: we destroy 'arg'
262 */
263 void
264 add_content_map_mime(char *cmap0, char *cmap1, char *cmap2, char *cmap3)
265 {
266 struct content_map *map;
267
268 debug((DEBUG_FAT, "add_content_map: name %s type %s enc %s enc11 %s ",
269 cmap0, cmap1, cmap2, cmap3));
270
271 map = get_content_map(cmap0);
272 #define CHECKMAP(s) (!s || ((s)[0] == '-' && (s)[1] == '\0') ? "" : (s))
273 map->name = CHECKMAP(cmap0);
274 map->type = CHECKMAP(cmap1);
275 map->encoding = CHECKMAP(cmap2);
276 map->encoding11 = CHECKMAP(cmap3);
277 #undef CHECKMAP
278 map->cgihandler = NULL;
279 }
280 #endif /* NO_DYNAMIC_CONTENT */
281