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