Home | History | Annotate | Line # | Download | only in httpd
content-bozo.c revision 1.1.1.3
      1  1.1.1.3  mrg /*	$eterna: content-bozo.c,v 1.12 2009/04/17 22:52:20 mrg Exp $	*/
      2      1.1  tls 
      3      1.1  tls /*
      4  1.1.1.3  mrg  * Copyright (c) 1997-2009 Matthew R. Green
      5      1.1  tls  * All rights reserved.
      6      1.1  tls  *
      7      1.1  tls  * Redistribution and use in source and binary forms, with or without
      8      1.1  tls  * modification, are permitted provided that the following conditions
      9      1.1  tls  * are met:
     10      1.1  tls  * 1. Redistributions of source code must retain the above copyright
     11      1.1  tls  *    notice, this list of conditions and the following disclaimer.
     12      1.1  tls  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  tls  *    notice, this list of conditions and the following disclaimer and
     14      1.1  tls  *    dedication in the documentation and/or other materials provided
     15      1.1  tls  *    with the distribution.
     16      1.1  tls  *
     17      1.1  tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18      1.1  tls  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19      1.1  tls  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20      1.1  tls  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21      1.1  tls  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22      1.1  tls  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23      1.1  tls  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24      1.1  tls  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25      1.1  tls  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26      1.1  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27      1.1  tls  * SUCH DAMAGE.
     28      1.1  tls  *
     29      1.1  tls  */
     30      1.1  tls 
     31      1.1  tls /* this code implements content-type handling for bozohttpd */
     32      1.1  tls 
     33      1.1  tls #include <sys/param.h>
     34      1.1  tls 
     35  1.1.1.3  mrg #include <errno.h>
     36      1.1  tls #include <string.h>
     37      1.1  tls 
     38      1.1  tls #include "bozohttpd.h"
     39      1.1  tls 
     40      1.1  tls /*
     41      1.1  tls  * this map and the functions below map between filenames and the
     42      1.1  tls  * content type and content encoding definitions.  this should become
     43      1.1  tls  * a configuration file, perhaps like apache's mime.types (but that
     44      1.1  tls  * has less info per-entry).
     45      1.1  tls  */
     46      1.1  tls 
     47      1.1  tls static struct content_map content_map[] = {
     48      1.1  tls 	{ ".html",	"text/html",			"",		"", NULL },
     49      1.1  tls 	{ ".htm",	"text/html",			"",		"", NULL },
     50      1.1  tls 	{ ".gif",	"image/gif",			"",		"", NULL },
     51      1.1  tls 	{ ".jpeg",	"image/jpeg",			"",		"", NULL },
     52      1.1  tls 	{ ".jpg",	"image/jpeg",			"",		"", NULL },
     53      1.1  tls 	{ ".jpe",	"image/jpeg",			"",		"", NULL },
     54      1.1  tls 	{ ".png",	"image/png",			"",		"", NULL },
     55      1.1  tls 	{ ".mp3",	"audio/mpeg",			"",		"", NULL },
     56      1.1  tls 	{ ".css",	"text/css",			"",		"", NULL },
     57      1.1  tls 	{ ".txt",	"text/plain",			"",		"", NULL },
     58      1.1  tls 	{ ".swf",	"application/x-shockwave-flash","",		"", NULL },
     59      1.1  tls 	{ ".dcr",	"application/x-director",	"",		"", NULL },
     60      1.1  tls 	{ ".pac",	"application/x-ns-proxy-autoconfig", "",	"", NULL },
     61      1.1  tls 	{ ".pa",	"application/x-ns-proxy-autoconfig", "",	"", NULL },
     62      1.1  tls 	{ ".tar",	"multipart/x-tar",		"",		"", NULL },
     63      1.1  tls 	{ ".gtar",	"multipart/x-gtar",		"",		"", NULL },
     64      1.1  tls 	{ ".tar.Z",	"multipart/x-tar",		"x-compress",	"compress", NULL },
     65      1.1  tls 	{ ".tar.gz",	"multipart/x-tar",		"x-gzip",	"gzip", NULL },
     66      1.1  tls 	{ ".taz",	"multipart/x-tar",		"x-gzip",	"gzip", NULL },
     67      1.1  tls 	{ ".tgz",	"multipart/x-tar",		"x-gzip",	"gzip", NULL },
     68      1.1  tls 	{ ".tar.z",	"multipart/x-tar",		"x-pack",	"x-pack", NULL },
     69      1.1  tls 	{ ".Z",		"application/x-compress",	"x-compress",	"compress", NULL },
     70      1.1  tls 	{ ".gz",	"application/x-gzip",		"x-gzip",	"gzip", NULL },
     71      1.1  tls 	{ ".z",		"unknown",			"x-pack",	"x-pack", NULL },
     72      1.1  tls 	{ ".bz2",	"application/x-bzip2",		"x-bzip2",	"x-bzip2", NULL },
     73      1.1  tls 	{ ".ogg",	"application/x-ogg",		"",		"", NULL },
     74      1.1  tls 	{ ".xbel",	"text/xml",			"",		"", NULL },
     75      1.1  tls 	{ ".xml",	"text/xml",			"",		"", NULL },
     76      1.1  tls 	{ ".xsl",	"text/xml",			"",		"", NULL },
     77      1.1  tls 	{ ".hqx",	"application/mac-binhex40",	"",		"", NULL },
     78      1.1  tls 	{ ".cpt",	"application/mac-compactpro",	"",		"", NULL },
     79      1.1  tls 	{ ".doc",	"application/msword",		"",		"", NULL },
     80      1.1  tls 	{ ".bin",	"application/octet-stream",	"",		"", NULL },
     81      1.1  tls 	{ ".dms",	"application/octet-stream",	"",		"", NULL },
     82      1.1  tls 	{ ".lha",	"application/octet-stream",	"",		"", NULL },
     83      1.1  tls 	{ ".lzh",	"application/octet-stream",	"",		"", NULL },
     84      1.1  tls 	{ ".exe",	"application/octet-stream",	"",		"", NULL },
     85      1.1  tls 	{ ".class",	"application/octet-stream",	"",		"", NULL },
     86      1.1  tls 	{ ".oda",	"application/oda",		"",		"", NULL },
     87      1.1  tls 	{ ".pdf",	"application/pdf",		"",		"", NULL },
     88      1.1  tls 	{ ".ai",	"application/postscript",	"",		"", NULL },
     89      1.1  tls 	{ ".eps",	"application/postscript",	"",		"", NULL },
     90      1.1  tls 	{ ".ps",	"application/postscript",	"",		"", NULL },
     91      1.1  tls 	{ ".ppt",	"application/powerpoint",	"",		"", NULL },
     92      1.1  tls 	{ ".rtf",	"application/rtf",		"",		"", NULL },
     93      1.1  tls 	{ ".bcpio",	"application/x-bcpio",		"",		"", NULL },
     94      1.1  tls 	{ ".torrent",	"application/x-bittorrent",	"",		"", NULL },
     95      1.1  tls 	{ ".vcd",	"application/x-cdlink",		"",		"", NULL },
     96      1.1  tls 	{ ".cpio",	"application/x-cpio",		"",		"", NULL },
     97      1.1  tls 	{ ".csh",	"application/x-csh",		"",		"", NULL },
     98      1.1  tls 	{ ".dir",	"application/x-director",	"",		"", NULL },
     99      1.1  tls 	{ ".dxr",	"application/x-director",	"",		"", NULL },
    100      1.1  tls 	{ ".dvi",	"application/x-dvi",		"",		"", NULL },
    101      1.1  tls 	{ ".hdf",	"application/x-hdf",		"",		"", NULL },
    102      1.1  tls 	{ ".cgi",	"application/x-httpd-cgi",	"",		"", NULL },
    103      1.1  tls 	{ ".skp",	"application/x-koan",		"",		"", NULL },
    104      1.1  tls 	{ ".skd",	"application/x-koan",		"",		"", NULL },
    105      1.1  tls 	{ ".skt",	"application/x-koan",		"",		"", NULL },
    106      1.1  tls 	{ ".skm",	"application/x-koan",		"",		"", NULL },
    107      1.1  tls 	{ ".latex",	"application/x-latex",		"",		"", NULL },
    108      1.1  tls 	{ ".mif",	"application/x-mif",		"",		"", NULL },
    109      1.1  tls 	{ ".nc",	"application/x-netcdf",		"",		"", NULL },
    110      1.1  tls 	{ ".cdf",	"application/x-netcdf",		"",		"", NULL },
    111      1.1  tls 	{ ".patch",	"application/x-patch",		"",		"", NULL },
    112      1.1  tls 	{ ".sh",	"application/x-sh",		"",		"", NULL },
    113      1.1  tls 	{ ".shar",	"application/x-shar",		"",		"", NULL },
    114      1.1  tls 	{ ".sit",	"application/x-stuffit",	"",		"", NULL },
    115      1.1  tls 	{ ".sv4cpio",	"application/x-sv4cpio",	"",		"", NULL },
    116      1.1  tls 	{ ".sv4crc",	"application/x-sv4crc",		"",		"", NULL },
    117      1.1  tls 	{ ".tar",	"application/x-tar",		"",		"", NULL },
    118      1.1  tls 	{ ".tcl",	"application/x-tcl",		"",		"", NULL },
    119      1.1  tls 	{ ".tex",	"application/x-tex",		"",		"", NULL },
    120      1.1  tls 	{ ".texinfo",	"application/x-texinfo",	"",		"", NULL },
    121      1.1  tls 	{ ".texi",	"application/x-texinfo",	"",		"", NULL },
    122      1.1  tls 	{ ".t",		"application/x-troff",		"",		"", NULL },
    123      1.1  tls 	{ ".tr",	"application/x-troff",		"",		"", NULL },
    124      1.1  tls 	{ ".roff",	"application/x-troff",		"",		"", NULL },
    125      1.1  tls 	{ ".man",	"application/x-troff-man",	"",		"", NULL },
    126      1.1  tls 	{ ".me",	"application/x-troff-me",	"",		"", NULL },
    127      1.1  tls 	{ ".ms",	"application/x-troff-ms",	"",		"", NULL },
    128      1.1  tls 	{ ".ustar",	"application/x-ustar",		"",		"", NULL },
    129      1.1  tls 	{ ".src",	"application/x-wais-source",	"",		"", NULL },
    130      1.1  tls 	{ ".zip",	"application/zip",		"",		"", NULL },
    131      1.1  tls 	{ ".au",	"audio/basic",			"",		"", NULL },
    132      1.1  tls 	{ ".snd",	"audio/basic",			"",		"", NULL },
    133      1.1  tls 	{ ".mpga",	"audio/mpeg",			"",		"", NULL },
    134      1.1  tls 	{ ".mp2",	"audio/mpeg",			"",		"", NULL },
    135      1.1  tls 	{ ".aif",	"audio/x-aiff",			"",		"", NULL },
    136      1.1  tls 	{ ".aiff",	"audio/x-aiff",			"",		"", NULL },
    137      1.1  tls 	{ ".aifc",	"audio/x-aiff",			"",		"", NULL },
    138      1.1  tls 	{ ".ram",	"audio/x-pn-realaudio",		"",		"", NULL },
    139      1.1  tls 	{ ".rpm",	"audio/x-pn-realaudio-plugin",	"",		"", NULL },
    140      1.1  tls 	{ ".ra",	"audio/x-realaudio",		"",		"", NULL },
    141      1.1  tls 	{ ".wav",	"audio/x-wav",			"",		"", NULL },
    142      1.1  tls 	{ ".pdb",	"chemical/x-pdb",		"",		"", NULL },
    143      1.1  tls 	{ ".xyz",	"chemical/x-pdb",		"",		"", NULL },
    144      1.1  tls 	{ ".ief",	"image/ief",			"",		"", NULL },
    145      1.1  tls 	{ ".tiff",	"image/tiff",			"",		"", NULL },
    146      1.1  tls 	{ ".tif",	"image/tiff",			"",		"", NULL },
    147      1.1  tls 	{ ".ras",	"image/x-cmu-raster",		"",		"", NULL },
    148      1.1  tls 	{ ".pnm",	"image/x-portable-anymap",	"",		"", NULL },
    149      1.1  tls 	{ ".pbm",	"image/x-portable-bitmap",	"",		"", NULL },
    150      1.1  tls 	{ ".pgm",	"image/x-portable-graymap",	"",		"", NULL },
    151      1.1  tls 	{ ".ppm",	"image/x-portable-pixmap",	"",		"", NULL },
    152      1.1  tls 	{ ".rgb",	"image/x-rgb",			"",		"", NULL },
    153      1.1  tls 	{ ".xbm",	"image/x-xbitmap",		"",		"", NULL },
    154      1.1  tls 	{ ".xpm",	"image/x-xpixmap",		"",		"", NULL },
    155      1.1  tls 	{ ".xwd",	"image/x-xwindowdump",		"",		"", NULL },
    156      1.1  tls 	{ ".rtx",	"text/richtext",		"",		"", NULL },
    157      1.1  tls 	{ ".tsv",	"text/tab-separated-values",	"",		"", NULL },
    158      1.1  tls 	{ ".etx",	"text/x-setext",		"",		"", NULL },
    159      1.1  tls 	{ ".sgml",	"text/x-sgml",			"",		"", NULL },
    160      1.1  tls 	{ ".sgm",	"text/x-sgml",			"",		"", NULL },
    161      1.1  tls 	{ ".mpeg",	"video/mpeg",			"",		"", NULL },
    162      1.1  tls 	{ ".mpg",	"video/mpeg",			"",		"", NULL },
    163      1.1  tls 	{ ".mpe",	"video/mpeg",			"",		"", NULL },
    164      1.1  tls 	{ ".qt",	"video/quicktime",		"",		"", NULL },
    165      1.1  tls 	{ ".mov",	"video/quicktime",		"",		"", NULL },
    166      1.1  tls 	{ ".avi",	"video/x-msvideo",		"",		"", NULL },
    167      1.1  tls 	{ ".movie",	"video/x-sgi-movie",		"",		"", NULL },
    168      1.1  tls 	{ ".ice",	"x-conference/x-cooltalk",	"",		"", NULL },
    169      1.1  tls 	{ ".wrl",	"x-world/x-vrml",		"",		"", NULL },
    170      1.1  tls 	{ ".vrml",	"x-world/x-vrml",		"",		"", NULL },
    171      1.1  tls 	{ NULL,		NULL,				NULL,		NULL, NULL, },
    172      1.1  tls };
    173      1.1  tls 
    174      1.1  tls static struct content_map *dynamic_content_map;
    175      1.1  tls 
    176      1.1  tls /* call with stage == 0 for full search, stage == 1 for dynamic only */
    177      1.1  tls struct content_map *
    178      1.1  tls match_content_map(const char *name, int stage)
    179      1.1  tls {
    180      1.1  tls 	size_t	len = strlen(name), nlen;
    181      1.1  tls 	struct	content_map	*map;
    182      1.1  tls 
    183      1.1  tls 	for (map = dynamic_content_map; map && map->name; map++) {
    184      1.1  tls again:
    185      1.1  tls 		nlen = strlen(map->name);
    186      1.1  tls 		if (nlen > len || strcasecmp(map->name, name + (len - nlen)) != 0)
    187      1.1  tls 			continue;
    188      1.1  tls 		return (map);
    189      1.1  tls 	}
    190      1.1  tls 	if (stage++ == 0) {
    191      1.1  tls 		map = content_map;
    192      1.1  tls 		goto again;
    193      1.1  tls 	}
    194      1.1  tls 
    195      1.1  tls 	return (NULL);
    196      1.1  tls }
    197      1.1  tls 
    198      1.1  tls /*
    199      1.1  tls  * given the file name, return a valid Content-Type: value.
    200      1.1  tls  */
    201      1.1  tls /* ARGSUSED */
    202      1.1  tls const char *
    203      1.1  tls content_type(http_req *request, const char *file)
    204      1.1  tls {
    205      1.1  tls 	struct	content_map	*map;
    206      1.1  tls 
    207      1.1  tls 	map = match_content_map(file, 0);
    208      1.1  tls 	if (map)
    209      1.1  tls 		return (map->type);
    210      1.1  tls 	return (text_plain);
    211      1.1  tls }
    212      1.1  tls 
    213      1.1  tls /*
    214      1.1  tls  * given the file name, return a valid Content-Encoding: value.
    215      1.1  tls  */
    216      1.1  tls const char *
    217      1.1  tls content_encoding(http_req *request, const char *file)
    218      1.1  tls {
    219      1.1  tls 	struct	content_map	*map;
    220      1.1  tls 
    221      1.1  tls 	map = match_content_map(file, 0);
    222      1.1  tls 	if (map)
    223      1.1  tls 		return (request->hr_proto == http_11 ?
    224      1.1  tls 		    map->encoding11 : map->encoding);
    225      1.1  tls 	return (NULL);
    226      1.1  tls }
    227      1.1  tls 
    228      1.1  tls #ifndef NO_DYNAMIC_CONTENT
    229      1.1  tls static int dynamic_content_map_size;
    230      1.1  tls 
    231      1.1  tls struct content_map *
    232      1.1  tls get_content_map(const char *name)
    233      1.1  tls {
    234      1.1  tls 	struct	content_map	*map;
    235      1.1  tls 
    236      1.1  tls 	if ((map = match_content_map(name, 1)))
    237      1.1  tls 		return (map);
    238      1.1  tls 
    239      1.1  tls 	dynamic_content_map_size++;
    240      1.1  tls 	dynamic_content_map = bozorealloc(dynamic_content_map,
    241      1.1  tls 	    (dynamic_content_map_size + 1) * sizeof *map);
    242  1.1.1.3  mrg 	if (dynamic_content_map == NULL)
    243  1.1.1.3  mrg 		error(1, "out of memory allocating content map");
    244      1.1  tls 	map = &dynamic_content_map[dynamic_content_map_size];
    245      1.1  tls 	map->name = map->type = map->encoding = map->encoding11 =
    246      1.1  tls 	    map->cgihandler = NULL;
    247      1.1  tls 	map--;
    248      1.1  tls 
    249      1.1  tls 	return (map);
    250      1.1  tls }
    251      1.1  tls 
    252      1.1  tls /*
    253      1.1  tls  * mime content maps look like:
    254      1.1  tls  *	".name type encoding encoding11"
    255      1.1  tls  * where any of type, encoding or encoding11 a dash "-" means "".
    256      1.1  tls  * eg the .gtar, .tar.Z from above  could be written like:
    257      1.1  tls  *	".gtar multipart/x-gtar - -"
    258      1.1  tls  *	".tar.Z multipart/x-tar x-compress compress"
    259      1.1  tls  * or
    260      1.1  tls  *	".gtar multipart/x-gtar"
    261      1.1  tls  *	".tar.Z multipart/x-tar x-compress compress"
    262      1.1  tls  * NOTE: we destroy 'arg'
    263      1.1  tls  */
    264      1.1  tls void
    265      1.1  tls add_content_map_mime(char *cmap0, char *cmap1, char *cmap2, char *cmap3)
    266      1.1  tls {
    267      1.1  tls 	struct content_map *map;
    268      1.1  tls 
    269      1.1  tls 	debug((DEBUG_FAT, "add_content_map: name %s type %s enc %s enc11 %s ",
    270      1.1  tls 		cmap0, cmap1, cmap2, cmap3));
    271      1.1  tls 
    272      1.1  tls 	map = get_content_map(cmap0);
    273      1.1  tls #define CHECKMAP(s)	(!s || ((s)[0] == '-' && (s)[1] == '\0') ? "" : (s))
    274      1.1  tls 	map->name = CHECKMAP(cmap0);
    275      1.1  tls 	map->type = CHECKMAP(cmap1);
    276      1.1  tls 	map->encoding = CHECKMAP(cmap2);
    277      1.1  tls 	map->encoding11 = CHECKMAP(cmap3);
    278      1.1  tls #undef CHECKMAP
    279      1.1  tls 	map->cgihandler = NULL;
    280      1.1  tls }
    281      1.1  tls #endif /* NO_DYNAMIC_CONTENT */
    282