content-bozo.c revision 1.2
1/* $NetBSD: content-bozo.c,v 1.2 2007/10/17 18:48:00 tls Exp $ */ 2 3/* $eterna: content-bozo.c,v 1.8 2006/05/17 08:19:10 mrg Exp $ */ 4 5/* 6 * Copyright (c) 1997-2006 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 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 */ 34 35/* this code implements content-type handling for bozohttpd */ 36 37#include <sys/param.h> 38 39#include <string.h> 40 41#include "bozohttpd.h" 42 43/* 44 * this map and the functions below map between filenames and the 45 * content type and content encoding definitions. this should become 46 * a configuration file, perhaps like apache's mime.types (but that 47 * has less info per-entry). 48 */ 49 50static struct content_map content_map[] = { 51 { ".html", "text/html", "", "", NULL }, 52 { ".htm", "text/html", "", "", NULL }, 53 { ".gif", "image/gif", "", "", NULL }, 54 { ".jpeg", "image/jpeg", "", "", NULL }, 55 { ".jpg", "image/jpeg", "", "", NULL }, 56 { ".jpe", "image/jpeg", "", "", NULL }, 57 { ".png", "image/png", "", "", NULL }, 58 { ".mp3", "audio/mpeg", "", "", NULL }, 59 { ".css", "text/css", "", "", NULL }, 60 { ".txt", "text/plain", "", "", NULL }, 61 { ".swf", "application/x-shockwave-flash","", "", NULL }, 62 { ".dcr", "application/x-director", "", "", NULL }, 63 { ".pac", "application/x-ns-proxy-autoconfig", "", "", NULL }, 64 { ".pa", "application/x-ns-proxy-autoconfig", "", "", NULL }, 65 { ".tar", "multipart/x-tar", "", "", NULL }, 66 { ".gtar", "multipart/x-gtar", "", "", NULL }, 67 { ".tar.Z", "multipart/x-tar", "x-compress", "compress", NULL }, 68 { ".tar.gz", "multipart/x-tar", "x-gzip", "gzip", NULL }, 69 { ".taz", "multipart/x-tar", "x-gzip", "gzip", NULL }, 70 { ".tgz", "multipart/x-tar", "x-gzip", "gzip", NULL }, 71 { ".tar.z", "multipart/x-tar", "x-pack", "x-pack", NULL }, 72 { ".Z", "application/x-compress", "x-compress", "compress", NULL }, 73 { ".gz", "application/x-gzip", "x-gzip", "gzip", NULL }, 74 { ".z", "unknown", "x-pack", "x-pack", NULL }, 75 { ".bz2", "application/x-bzip2", "x-bzip2", "x-bzip2", NULL }, 76 { ".ogg", "application/x-ogg", "", "", 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 { ".qt", "video/quicktime", "", "", NULL }, 168 { ".mov", "video/quicktime", "", "", NULL }, 169 { ".avi", "video/x-msvideo", "", "", NULL }, 170 { ".movie", "video/x-sgi-movie", "", "", NULL }, 171 { ".ice", "x-conference/x-cooltalk", "", "", NULL }, 172 { ".wrl", "x-world/x-vrml", "", "", NULL }, 173 { ".vrml", "x-world/x-vrml", "", "", NULL }, 174 { NULL, NULL, NULL, NULL, NULL, }, 175}; 176 177static struct content_map *dynamic_content_map; 178 179/* call with stage == 0 for full search, stage == 1 for dynamic only */ 180struct content_map * 181match_content_map(const char *name, int stage) 182{ 183 size_t len = strlen(name), nlen; 184 struct content_map *map; 185 186 for (map = dynamic_content_map; map && map->name; map++) { 187again: 188 nlen = strlen(map->name); 189 if (nlen > len || strcasecmp(map->name, name + (len - nlen)) != 0) 190 continue; 191 return (map); 192 } 193 if (stage++ == 0) { 194 map = content_map; 195 goto again; 196 } 197 198 return (NULL); 199} 200 201/* 202 * given the file name, return a valid Content-Type: value. 203 */ 204/* ARGSUSED */ 205const char * 206content_type(http_req *request, const char *file) 207{ 208 struct content_map *map; 209 210 map = match_content_map(file, 0); 211 if (map) 212 return (map->type); 213 return (text_plain); 214} 215 216/* 217 * given the file name, return a valid Content-Encoding: value. 218 */ 219const char * 220content_encoding(http_req *request, const char *file) 221{ 222 struct content_map *map; 223 224 map = match_content_map(file, 0); 225 if (map) 226 return (request->hr_proto == http_11 ? 227 map->encoding11 : map->encoding); 228 return (NULL); 229} 230 231#ifndef NO_DYNAMIC_CONTENT 232static int dynamic_content_map_size; 233 234struct content_map * 235get_content_map(const char *name) 236{ 237 struct content_map *map; 238 239 if ((map = match_content_map(name, 1))) 240 return (map); 241 242 dynamic_content_map_size++; 243 dynamic_content_map = bozorealloc(dynamic_content_map, 244 (dynamic_content_map_size + 1) * sizeof *map); 245 map = &dynamic_content_map[dynamic_content_map_size]; 246 map->name = map->type = map->encoding = map->encoding11 = 247 map->cgihandler = NULL; 248 map--; 249 250 return (map); 251} 252 253/* 254 * mime content maps look like: 255 * ".name type encoding encoding11" 256 * where any of type, encoding or encoding11 a dash "-" means "". 257 * eg the .gtar, .tar.Z from above could be written like: 258 * ".gtar multipart/x-gtar - -" 259 * ".tar.Z multipart/x-tar x-compress compress" 260 * or 261 * ".gtar multipart/x-gtar" 262 * ".tar.Z multipart/x-tar x-compress compress" 263 * NOTE: we destroy 'arg' 264 */ 265void 266add_content_map_mime(char *cmap0, char *cmap1, char *cmap2, char *cmap3) 267{ 268 struct content_map *map; 269 270 debug((DEBUG_FAT, "add_content_map: name %s type %s enc %s enc11 %s ", 271 cmap0, cmap1, cmap2, cmap3)); 272 273 map = get_content_map(cmap0); 274#define CHECKMAP(s) (!s || ((s)[0] == '-' && (s)[1] == '\0') ? "" : (s)) 275 map->name = CHECKMAP(cmap0); 276 map->type = CHECKMAP(cmap1); 277 map->encoding = CHECKMAP(cmap2); 278 map->encoding11 = CHECKMAP(cmap3); 279#undef CHECKMAP 280 map->cgihandler = NULL; 281} 282#endif /* NO_DYNAMIC_CONTENT */ 283