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