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