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