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