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