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