content-bozo.c revision 1.17 1 1.17 rhialto /* $NetBSD: content-bozo.c,v 1.17 2020/09/12 12:39:28 rhialto Exp $ */
2 1.2 tls
3 1.7 mrg /* $eterna: content-bozo.c,v 1.17 2011/11/18 09:21:15 mrg Exp $ */
4 1.1 tls
5 1.1 tls /*
6 1.15 mrg * Copyright (c) 1997-2018 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.5 mrg static bozo_content_map_t static_content_map[] = {
50 1.12 mrg { ".html", "text/html", "", "", NULL },
51 1.12 mrg { ".htm", "text/html", "", "", NULL },
52 1.12 mrg { ".gif", "image/gif", "", "", NULL },
53 1.12 mrg { ".jpeg", "image/jpeg", "", "", NULL },
54 1.12 mrg { ".jpg", "image/jpeg", "", "", NULL },
55 1.12 mrg { ".jpe", "image/jpeg", "", "", NULL },
56 1.12 mrg { ".png", "image/png", "", "", NULL },
57 1.12 mrg { ".mp3", "audio/mpeg", "", "", NULL },
58 1.12 mrg { ".css", "text/css", "", "", NULL },
59 1.12 mrg { ".txt", "text/plain", "", "", NULL },
60 1.12 mrg { ".swf", "application/x-shockwave-flash","", "", NULL },
61 1.12 mrg { ".dcr", "application/x-director", "", "", NULL },
62 1.12 mrg { ".pac", "application/x-ns-proxy-autoconfig", "", "", NULL },
63 1.12 mrg { ".pa", "application/x-ns-proxy-autoconfig", "", "", NULL },
64 1.12 mrg { ".tar", "multipart/x-tar", "", "", NULL },
65 1.14 shm { ".gtar", "application/x-gtar-compressed", "", "", NULL },
66 1.14 shm { ".tar.Z", "application/x-gtar-compressed", "", "", NULL },
67 1.14 shm { ".tar.gz", "application/x-gtar-compressed", "", "", NULL },
68 1.14 shm { ".taz", "application/x-gtar-compressed", "", "", NULL },
69 1.14 shm { ".tgz", "application/x-gtar-compressed", "", "", NULL },
70 1.14 shm { ".tar.z", "application/x-gtar-compressed", "", "", NULL },
71 1.14 shm { ".Z", "application/x-compress", "", "", NULL },
72 1.14 shm { ".gz", "application/x-gzip", "", "", NULL },
73 1.14 shm { ".z", "unknown", "", "", NULL },
74 1.14 shm { ".bz2", "application/x-bzip2", "", "", NULL },
75 1.12 mrg { ".ogg", "application/x-ogg", "", "", NULL },
76 1.12 mrg { ".mkv", "video/x-matroska", "", "", NULL },
77 1.12 mrg { ".xbel", "text/xml", "", "", NULL },
78 1.12 mrg { ".xml", "text/xml", "", "", NULL },
79 1.12 mrg { ".xsl", "text/xml", "", "", NULL },
80 1.12 mrg { ".hqx", "application/mac-binhex40", "", "", NULL },
81 1.12 mrg { ".cpt", "application/mac-compactpro", "", "", NULL },
82 1.12 mrg { ".doc", "application/msword", "", "", NULL },
83 1.12 mrg { ".bin", "application/octet-stream", "", "", NULL },
84 1.12 mrg { ".dms", "application/octet-stream", "", "", NULL },
85 1.12 mrg { ".lha", "application/octet-stream", "", "", NULL },
86 1.12 mrg { ".lzh", "application/octet-stream", "", "", NULL },
87 1.12 mrg { ".exe", "application/octet-stream", "", "", NULL },
88 1.12 mrg { ".class", "application/octet-stream", "", "", NULL },
89 1.12 mrg { ".oda", "application/oda", "", "", NULL },
90 1.12 mrg { ".pdf", "application/pdf", "", "", NULL },
91 1.12 mrg { ".ai", "application/postscript", "", "", NULL },
92 1.12 mrg { ".eps", "application/postscript", "", "", NULL },
93 1.12 mrg { ".ps", "application/postscript", "", "", NULL },
94 1.12 mrg { ".ppt", "application/powerpoint", "", "", NULL },
95 1.12 mrg { ".rtf", "application/rtf", "", "", NULL },
96 1.12 mrg { ".bcpio", "application/x-bcpio", "", "", NULL },
97 1.12 mrg { ".torrent", "application/x-bittorrent", "", "", NULL },
98 1.12 mrg { ".vcd", "application/x-cdlink", "", "", NULL },
99 1.12 mrg { ".cpio", "application/x-cpio", "", "", NULL },
100 1.12 mrg { ".csh", "application/x-csh", "", "", NULL },
101 1.12 mrg { ".dir", "application/x-director", "", "", NULL },
102 1.12 mrg { ".dxr", "application/x-director", "", "", NULL },
103 1.12 mrg { ".dvi", "application/x-dvi", "", "", NULL },
104 1.12 mrg { ".hdf", "application/x-hdf", "", "", NULL },
105 1.12 mrg { ".cgi", "application/x-httpd-cgi", "", "", NULL },
106 1.12 mrg { ".skp", "application/x-koan", "", "", NULL },
107 1.12 mrg { ".skd", "application/x-koan", "", "", NULL },
108 1.12 mrg { ".skt", "application/x-koan", "", "", NULL },
109 1.12 mrg { ".skm", "application/x-koan", "", "", NULL },
110 1.12 mrg { ".latex", "application/x-latex", "", "", NULL },
111 1.12 mrg { ".mif", "application/x-mif", "", "", NULL },
112 1.12 mrg { ".nc", "application/x-netcdf", "", "", NULL },
113 1.12 mrg { ".cdf", "application/x-netcdf", "", "", NULL },
114 1.12 mrg { ".patch", "application/x-patch", "", "", NULL },
115 1.12 mrg { ".sh", "application/x-sh", "", "", NULL },
116 1.12 mrg { ".shar", "application/x-shar", "", "", NULL },
117 1.12 mrg { ".sit", "application/x-stuffit", "", "", NULL },
118 1.12 mrg { ".sv4cpio", "application/x-sv4cpio", "", "", NULL },
119 1.12 mrg { ".sv4crc", "application/x-sv4crc", "", "", NULL },
120 1.12 mrg { ".tar", "application/x-tar", "", "", NULL },
121 1.12 mrg { ".tcl", "application/x-tcl", "", "", NULL },
122 1.12 mrg { ".tex", "application/x-tex", "", "", NULL },
123 1.12 mrg { ".texinfo", "application/x-texinfo", "", "", NULL },
124 1.12 mrg { ".texi", "application/x-texinfo", "", "", NULL },
125 1.12 mrg { ".t", "application/x-troff", "", "", NULL },
126 1.12 mrg { ".tr", "application/x-troff", "", "", NULL },
127 1.12 mrg { ".roff", "application/x-troff", "", "", NULL },
128 1.12 mrg { ".man", "application/x-troff-man", "", "", NULL },
129 1.12 mrg { ".me", "application/x-troff-me", "", "", NULL },
130 1.12 mrg { ".ms", "application/x-troff-ms", "", "", NULL },
131 1.12 mrg { ".ustar", "application/x-ustar", "", "", NULL },
132 1.12 mrg { ".src", "application/x-wais-source", "", "", NULL },
133 1.12 mrg { ".zip", "application/zip", "", "", NULL },
134 1.12 mrg { ".au", "audio/basic", "", "", NULL },
135 1.12 mrg { ".snd", "audio/basic", "", "", NULL },
136 1.12 mrg { ".mpga", "audio/mpeg", "", "", NULL },
137 1.12 mrg { ".mp2", "audio/mpeg", "", "", NULL },
138 1.17 rhialto { ".m4a", "audio/mpeg", "", "", NULL },
139 1.12 mrg { ".aif", "audio/x-aiff", "", "", NULL },
140 1.12 mrg { ".aiff", "audio/x-aiff", "", "", NULL },
141 1.12 mrg { ".aifc", "audio/x-aiff", "", "", NULL },
142 1.12 mrg { ".ram", "audio/x-pn-realaudio", "", "", NULL },
143 1.12 mrg { ".rpm", "audio/x-pn-realaudio-plugin", "", "", NULL },
144 1.12 mrg { ".ra", "audio/x-realaudio", "", "", NULL },
145 1.12 mrg { ".wav", "audio/x-wav", "", "", NULL },
146 1.12 mrg { ".pdb", "chemical/x-pdb", "", "", NULL },
147 1.12 mrg { ".xyz", "chemical/x-pdb", "", "", NULL },
148 1.12 mrg { ".ief", "image/ief", "", "", NULL },
149 1.12 mrg { ".tiff", "image/tiff", "", "", NULL },
150 1.12 mrg { ".tif", "image/tiff", "", "", NULL },
151 1.12 mrg { ".ras", "image/x-cmu-raster", "", "", NULL },
152 1.12 mrg { ".pnm", "image/x-portable-anymap", "", "", NULL },
153 1.12 mrg { ".pbm", "image/x-portable-bitmap", "", "", NULL },
154 1.12 mrg { ".pgm", "image/x-portable-graymap", "", "", NULL },
155 1.12 mrg { ".ppm", "image/x-portable-pixmap", "", "", NULL },
156 1.12 mrg { ".rgb", "image/x-rgb", "", "", NULL },
157 1.12 mrg { ".xbm", "image/x-xbitmap", "", "", NULL },
158 1.12 mrg { ".xpm", "image/x-xpixmap", "", "", NULL },
159 1.12 mrg { ".xwd", "image/x-xwindowdump", "", "", NULL },
160 1.12 mrg { ".rtx", "text/richtext", "", "", NULL },
161 1.12 mrg { ".tsv", "text/tab-separated-values", "", "", NULL },
162 1.12 mrg { ".etx", "text/x-setext", "", "", NULL },
163 1.12 mrg { ".sgml", "text/x-sgml", "", "", NULL },
164 1.12 mrg { ".sgm", "text/x-sgml", "", "", NULL },
165 1.12 mrg { ".mpeg", "video/mpeg", "", "", NULL },
166 1.12 mrg { ".mpg", "video/mpeg", "", "", NULL },
167 1.12 mrg { ".mpe", "video/mpeg", "", "", NULL },
168 1.12 mrg { ".ts", "video/mpeg", "", "", NULL },
169 1.12 mrg { ".vob", "video/mpeg", "", "", NULL },
170 1.12 mrg { ".mp4", "video/mp4", "", "", NULL },
171 1.17 rhialto { ".m4v", "video/mp4", "", "", NULL },
172 1.12 mrg { ".qt", "video/quicktime", "", "", NULL },
173 1.12 mrg { ".mov", "video/quicktime", "", "", NULL },
174 1.12 mrg { ".avi", "video/x-msvideo", "", "", NULL },
175 1.12 mrg { ".movie", "video/x-sgi-movie", "", "", NULL },
176 1.12 mrg { ".ice", "x-conference/x-cooltalk", "", "", NULL },
177 1.12 mrg { ".wrl", "x-world/x-vrml", "", "", NULL },
178 1.12 mrg { ".vrml", "x-world/x-vrml", "", "", NULL },
179 1.12 mrg { ".svg", "image/svg+xml", "", "", NULL },
180 1.12 mrg { NULL, NULL, NULL, NULL, NULL }
181 1.1 tls };
182 1.1 tls
183 1.5 mrg static bozo_content_map_t *
184 1.5 mrg search_map(bozo_content_map_t *map, const char *name, size_t len)
185 1.5 mrg {
186 1.16 mrg
187 1.5 mrg for ( ; map && map->name; map++) {
188 1.12 mrg const size_t namelen = strlen(map->name);
189 1.12 mrg
190 1.12 mrg if (namelen < len &&
191 1.12 mrg strcasecmp(map->name, name + (len - namelen)) == 0)
192 1.5 mrg return map;
193 1.5 mrg }
194 1.5 mrg return NULL;
195 1.5 mrg }
196 1.1 tls
197 1.5 mrg /* match a suffix on a file - dynamiconly means no static content search */
198 1.5 mrg bozo_content_map_t *
199 1.5 mrg bozo_match_content_map(bozohttpd_t *httpd, const char *name,
200 1.16 mrg const int dynamiconly)
201 1.1 tls {
202 1.5 mrg bozo_content_map_t *map;
203 1.5 mrg size_t len;
204 1.1 tls
205 1.5 mrg len = strlen(name);
206 1.16 mrg map = search_map(httpd->dynamic_content_map, name, len);
207 1.16 mrg if (map == NULL && !dynamiconly)
208 1.16 mrg map = search_map(static_content_map, name, len);
209 1.16 mrg
210 1.16 mrg return map;
211 1.1 tls }
212 1.1 tls
213 1.1 tls /*
214 1.1 tls * given the file name, return a valid Content-Type: value.
215 1.1 tls */
216 1.1 tls /* ARGSUSED */
217 1.1 tls const char *
218 1.5 mrg bozo_content_type(bozo_httpreq_t *request, const char *file)
219 1.1 tls {
220 1.5 mrg bozohttpd_t *httpd = request->hr_httpd;
221 1.5 mrg bozo_content_map_t *map;
222 1.1 tls
223 1.5 mrg map = bozo_match_content_map(httpd, file, 0);
224 1.1 tls if (map)
225 1.5 mrg return map->type;
226 1.5 mrg return httpd->consts.text_plain;
227 1.1 tls }
228 1.1 tls
229 1.1 tls /*
230 1.1 tls * given the file name, return a valid Content-Encoding: value.
231 1.1 tls */
232 1.1 tls const char *
233 1.5 mrg bozo_content_encoding(bozo_httpreq_t *request, const char *file)
234 1.1 tls {
235 1.5 mrg bozohttpd_t *httpd = request->hr_httpd;
236 1.5 mrg bozo_content_map_t *map;
237 1.1 tls
238 1.5 mrg map = bozo_match_content_map(httpd, file, 0);
239 1.1 tls if (map)
240 1.5 mrg return (request->hr_proto == httpd->consts.http_11) ?
241 1.5 mrg map->encoding11 : map->encoding;
242 1.5 mrg return NULL;
243 1.1 tls }
244 1.1 tls
245 1.1 tls #ifndef NO_DYNAMIC_CONTENT
246 1.1 tls
247 1.5 mrg bozo_content_map_t *
248 1.5 mrg bozo_get_content_map(bozohttpd_t *httpd, const char *name)
249 1.1 tls {
250 1.5 mrg bozo_content_map_t *map;
251 1.1 tls
252 1.5 mrg if ((map = bozo_match_content_map(httpd, name, 1)) != NULL)
253 1.5 mrg return map;
254 1.1 tls
255 1.5 mrg httpd->dynamic_content_map_size++;
256 1.5 mrg httpd->dynamic_content_map = bozorealloc(httpd,
257 1.5 mrg httpd->dynamic_content_map,
258 1.5 mrg (httpd->dynamic_content_map_size + 1) * sizeof *map);
259 1.5 mrg if (httpd->dynamic_content_map == NULL)
260 1.13 mrg bozoerr(httpd, 1, "out of memory allocating content map");
261 1.5 mrg map = &httpd->dynamic_content_map[httpd->dynamic_content_map_size];
262 1.1 tls map->name = map->type = map->encoding = map->encoding11 =
263 1.5 mrg map->cgihandler = NULL;
264 1.1 tls map--;
265 1.1 tls
266 1.5 mrg return map;
267 1.1 tls }
268 1.1 tls
269 1.1 tls /*
270 1.1 tls * mime content maps look like:
271 1.1 tls * ".name type encoding encoding11"
272 1.1 tls * where any of type, encoding or encoding11 a dash "-" means "".
273 1.1 tls * eg the .gtar, .tar.Z from above could be written like:
274 1.1 tls * ".gtar multipart/x-gtar - -"
275 1.1 tls * ".tar.Z multipart/x-tar x-compress compress"
276 1.1 tls * or
277 1.1 tls * ".gtar multipart/x-gtar"
278 1.1 tls * ".tar.Z multipart/x-tar x-compress compress"
279 1.1 tls * NOTE: we destroy 'arg'
280 1.1 tls */
281 1.1 tls void
282 1.5 mrg bozo_add_content_map_mime(bozohttpd_t *httpd, const char *cmap0,
283 1.5 mrg const char *cmap1, const char *cmap2, const char *cmap3)
284 1.1 tls {
285 1.5 mrg bozo_content_map_t *map;
286 1.1 tls
287 1.5 mrg debug((httpd, DEBUG_FAT,
288 1.5 mrg "add_content_map: name %s type %s enc %s enc11 %s ",
289 1.1 tls cmap0, cmap1, cmap2, cmap3));
290 1.1 tls
291 1.5 mrg map = bozo_get_content_map(httpd, cmap0);
292 1.1 tls #define CHECKMAP(s) (!s || ((s)[0] == '-' && (s)[1] == '\0') ? "" : (s))
293 1.1 tls map->name = CHECKMAP(cmap0);
294 1.1 tls map->type = CHECKMAP(cmap1);
295 1.1 tls map->encoding = CHECKMAP(cmap2);
296 1.1 tls map->encoding11 = CHECKMAP(cmap3);
297 1.1 tls #undef CHECKMAP
298 1.1 tls map->cgihandler = NULL;
299 1.1 tls }
300 1.1 tls #endif /* NO_DYNAMIC_CONTENT */
301