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