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