Home | History | Annotate | Line # | Download | only in httpd
content-bozo.c revision 1.4
      1 /*	$NetBSD: content-bozo.c,v 1.4 2009/04/18 07:28:24 mrg Exp $	*/
      2 
      3 /*	$eterna: content-bozo.c,v 1.12 2009/04/17 22:52:20 mrg Exp $	*/
      4 
      5 /*
      6  * Copyright (c) 1997-2009 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 struct content_map 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 	{ ".xbel",	"text/xml",			"",		"", NULL },
     77 	{ ".xml",	"text/xml",			"",		"", NULL },
     78 	{ ".xsl",	"text/xml",			"",		"", NULL },
     79 	{ ".hqx",	"application/mac-binhex40",	"",		"", NULL },
     80 	{ ".cpt",	"application/mac-compactpro",	"",		"", NULL },
     81 	{ ".doc",	"application/msword",		"",		"", NULL },
     82 	{ ".bin",	"application/octet-stream",	"",		"", NULL },
     83 	{ ".dms",	"application/octet-stream",	"",		"", NULL },
     84 	{ ".lha",	"application/octet-stream",	"",		"", NULL },
     85 	{ ".lzh",	"application/octet-stream",	"",		"", NULL },
     86 	{ ".exe",	"application/octet-stream",	"",		"", NULL },
     87 	{ ".class",	"application/octet-stream",	"",		"", NULL },
     88 	{ ".oda",	"application/oda",		"",		"", NULL },
     89 	{ ".pdf",	"application/pdf",		"",		"", NULL },
     90 	{ ".ai",	"application/postscript",	"",		"", NULL },
     91 	{ ".eps",	"application/postscript",	"",		"", NULL },
     92 	{ ".ps",	"application/postscript",	"",		"", NULL },
     93 	{ ".ppt",	"application/powerpoint",	"",		"", NULL },
     94 	{ ".rtf",	"application/rtf",		"",		"", NULL },
     95 	{ ".bcpio",	"application/x-bcpio",		"",		"", NULL },
     96 	{ ".torrent",	"application/x-bittorrent",	"",		"", NULL },
     97 	{ ".vcd",	"application/x-cdlink",		"",		"", NULL },
     98 	{ ".cpio",	"application/x-cpio",		"",		"", NULL },
     99 	{ ".csh",	"application/x-csh",		"",		"", NULL },
    100 	{ ".dir",	"application/x-director",	"",		"", NULL },
    101 	{ ".dxr",	"application/x-director",	"",		"", NULL },
    102 	{ ".dvi",	"application/x-dvi",		"",		"", NULL },
    103 	{ ".hdf",	"application/x-hdf",		"",		"", NULL },
    104 	{ ".cgi",	"application/x-httpd-cgi",	"",		"", NULL },
    105 	{ ".skp",	"application/x-koan",		"",		"", NULL },
    106 	{ ".skd",	"application/x-koan",		"",		"", NULL },
    107 	{ ".skt",	"application/x-koan",		"",		"", NULL },
    108 	{ ".skm",	"application/x-koan",		"",		"", NULL },
    109 	{ ".latex",	"application/x-latex",		"",		"", NULL },
    110 	{ ".mif",	"application/x-mif",		"",		"", NULL },
    111 	{ ".nc",	"application/x-netcdf",		"",		"", NULL },
    112 	{ ".cdf",	"application/x-netcdf",		"",		"", NULL },
    113 	{ ".patch",	"application/x-patch",		"",		"", NULL },
    114 	{ ".sh",	"application/x-sh",		"",		"", NULL },
    115 	{ ".shar",	"application/x-shar",		"",		"", NULL },
    116 	{ ".sit",	"application/x-stuffit",	"",		"", NULL },
    117 	{ ".sv4cpio",	"application/x-sv4cpio",	"",		"", NULL },
    118 	{ ".sv4crc",	"application/x-sv4crc",		"",		"", NULL },
    119 	{ ".tar",	"application/x-tar",		"",		"", NULL },
    120 	{ ".tcl",	"application/x-tcl",		"",		"", NULL },
    121 	{ ".tex",	"application/x-tex",		"",		"", NULL },
    122 	{ ".texinfo",	"application/x-texinfo",	"",		"", NULL },
    123 	{ ".texi",	"application/x-texinfo",	"",		"", NULL },
    124 	{ ".t",		"application/x-troff",		"",		"", NULL },
    125 	{ ".tr",	"application/x-troff",		"",		"", NULL },
    126 	{ ".roff",	"application/x-troff",		"",		"", NULL },
    127 	{ ".man",	"application/x-troff-man",	"",		"", NULL },
    128 	{ ".me",	"application/x-troff-me",	"",		"", NULL },
    129 	{ ".ms",	"application/x-troff-ms",	"",		"", NULL },
    130 	{ ".ustar",	"application/x-ustar",		"",		"", NULL },
    131 	{ ".src",	"application/x-wais-source",	"",		"", NULL },
    132 	{ ".zip",	"application/zip",		"",		"", NULL },
    133 	{ ".au",	"audio/basic",			"",		"", NULL },
    134 	{ ".snd",	"audio/basic",			"",		"", NULL },
    135 	{ ".mpga",	"audio/mpeg",			"",		"", NULL },
    136 	{ ".mp2",	"audio/mpeg",			"",		"", NULL },
    137 	{ ".aif",	"audio/x-aiff",			"",		"", NULL },
    138 	{ ".aiff",	"audio/x-aiff",			"",		"", NULL },
    139 	{ ".aifc",	"audio/x-aiff",			"",		"", NULL },
    140 	{ ".ram",	"audio/x-pn-realaudio",		"",		"", NULL },
    141 	{ ".rpm",	"audio/x-pn-realaudio-plugin",	"",		"", NULL },
    142 	{ ".ra",	"audio/x-realaudio",		"",		"", NULL },
    143 	{ ".wav",	"audio/x-wav",			"",		"", NULL },
    144 	{ ".pdb",	"chemical/x-pdb",		"",		"", NULL },
    145 	{ ".xyz",	"chemical/x-pdb",		"",		"", NULL },
    146 	{ ".ief",	"image/ief",			"",		"", NULL },
    147 	{ ".tiff",	"image/tiff",			"",		"", NULL },
    148 	{ ".tif",	"image/tiff",			"",		"", NULL },
    149 	{ ".ras",	"image/x-cmu-raster",		"",		"", NULL },
    150 	{ ".pnm",	"image/x-portable-anymap",	"",		"", NULL },
    151 	{ ".pbm",	"image/x-portable-bitmap",	"",		"", NULL },
    152 	{ ".pgm",	"image/x-portable-graymap",	"",		"", NULL },
    153 	{ ".ppm",	"image/x-portable-pixmap",	"",		"", NULL },
    154 	{ ".rgb",	"image/x-rgb",			"",		"", NULL },
    155 	{ ".xbm",	"image/x-xbitmap",		"",		"", NULL },
    156 	{ ".xpm",	"image/x-xpixmap",		"",		"", NULL },
    157 	{ ".xwd",	"image/x-xwindowdump",		"",		"", NULL },
    158 	{ ".rtx",	"text/richtext",		"",		"", NULL },
    159 	{ ".tsv",	"text/tab-separated-values",	"",		"", NULL },
    160 	{ ".etx",	"text/x-setext",		"",		"", NULL },
    161 	{ ".sgml",	"text/x-sgml",			"",		"", NULL },
    162 	{ ".sgm",	"text/x-sgml",			"",		"", NULL },
    163 	{ ".mpeg",	"video/mpeg",			"",		"", NULL },
    164 	{ ".mpg",	"video/mpeg",			"",		"", NULL },
    165 	{ ".mpe",	"video/mpeg",			"",		"", NULL },
    166 	{ ".qt",	"video/quicktime",		"",		"", NULL },
    167 	{ ".mov",	"video/quicktime",		"",		"", NULL },
    168 	{ ".avi",	"video/x-msvideo",		"",		"", NULL },
    169 	{ ".movie",	"video/x-sgi-movie",		"",		"", NULL },
    170 	{ ".ice",	"x-conference/x-cooltalk",	"",		"", NULL },
    171 	{ ".wrl",	"x-world/x-vrml",		"",		"", NULL },
    172 	{ ".vrml",	"x-world/x-vrml",		"",		"", NULL },
    173 	{ NULL,		NULL,				NULL,		NULL, NULL, },
    174 };
    175 
    176 static struct content_map *dynamic_content_map;
    177 
    178 /* call with stage == 0 for full search, stage == 1 for dynamic only */
    179 struct content_map *
    180 match_content_map(const char *name, int stage)
    181 {
    182 	size_t	len = strlen(name), nlen;
    183 	struct	content_map	*map;
    184 
    185 	for (map = dynamic_content_map; map && map->name; map++) {
    186 again:
    187 		nlen = strlen(map->name);
    188 		if (nlen > len || strcasecmp(map->name, name + (len - nlen)) != 0)
    189 			continue;
    190 		return (map);
    191 	}
    192 	if (stage++ == 0) {
    193 		map = content_map;
    194 		goto again;
    195 	}
    196 
    197 	return (NULL);
    198 }
    199 
    200 /*
    201  * given the file name, return a valid Content-Type: value.
    202  */
    203 /* ARGSUSED */
    204 const char *
    205 content_type(http_req *request, const char *file)
    206 {
    207 	struct	content_map	*map;
    208 
    209 	map = match_content_map(file, 0);
    210 	if (map)
    211 		return (map->type);
    212 	return (text_plain);
    213 }
    214 
    215 /*
    216  * given the file name, return a valid Content-Encoding: value.
    217  */
    218 const char *
    219 content_encoding(http_req *request, const char *file)
    220 {
    221 	struct	content_map	*map;
    222 
    223 	map = match_content_map(file, 0);
    224 	if (map)
    225 		return (request->hr_proto == http_11 ?
    226 		    map->encoding11 : map->encoding);
    227 	return (NULL);
    228 }
    229 
    230 #ifndef NO_DYNAMIC_CONTENT
    231 static int dynamic_content_map_size;
    232 
    233 struct content_map *
    234 get_content_map(const char *name)
    235 {
    236 	struct	content_map	*map;
    237 
    238 	if ((map = match_content_map(name, 1)))
    239 		return (map);
    240 
    241 	dynamic_content_map_size++;
    242 	dynamic_content_map = bozorealloc(dynamic_content_map,
    243 	    (dynamic_content_map_size + 1) * sizeof *map);
    244 	if (dynamic_content_map == NULL)
    245 		error(1, "out of memory allocating content map");
    246 	map = &dynamic_content_map[dynamic_content_map_size];
    247 	map->name = map->type = map->encoding = map->encoding11 =
    248 	    map->cgihandler = NULL;
    249 	map--;
    250 
    251 	return (map);
    252 }
    253 
    254 /*
    255  * mime content maps look like:
    256  *	".name type encoding encoding11"
    257  * where any of type, encoding or encoding11 a dash "-" means "".
    258  * eg the .gtar, .tar.Z from above  could be written like:
    259  *	".gtar multipart/x-gtar - -"
    260  *	".tar.Z multipart/x-tar x-compress compress"
    261  * or
    262  *	".gtar multipart/x-gtar"
    263  *	".tar.Z multipart/x-tar x-compress compress"
    264  * NOTE: we destroy 'arg'
    265  */
    266 void
    267 add_content_map_mime(char *cmap0, char *cmap1, char *cmap2, char *cmap3)
    268 {
    269 	struct content_map *map;
    270 
    271 	debug((DEBUG_FAT, "add_content_map: name %s type %s enc %s enc11 %s ",
    272 		cmap0, cmap1, cmap2, cmap3));
    273 
    274 	map = get_content_map(cmap0);
    275 #define CHECKMAP(s)	(!s || ((s)[0] == '-' && (s)[1] == '\0') ? "" : (s))
    276 	map->name = CHECKMAP(cmap0);
    277 	map->type = CHECKMAP(cmap1);
    278 	map->encoding = CHECKMAP(cmap2);
    279 	map->encoding11 = CHECKMAP(cmap3);
    280 #undef CHECKMAP
    281 	map->cgihandler = NULL;
    282 }
    283 #endif /* NO_DYNAMIC_CONTENT */
    284