home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / mod_mime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-07  |  9.7 KB  |  316 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55. /*
  56.  * http_mime.c: Sends/gets MIME headers for requests
  57.  * 
  58.  * Rob McCool
  59.  * 
  60.  */
  61.  
  62. #define MIME_PRIVATE
  63.  
  64. #include "httpd.h"
  65. #include "http_config.h"
  66.  
  67. typedef struct {
  68.     table *forced_types;    /* Additional AddTyped stuff */
  69.     table *encoding_types;    /* Added with AddEncoding... */
  70.     table *language_types;    /* Added with AddLanguage... */
  71.     table *handlers;        /* Added with AddHandler...  */
  72.  
  73.     char *type;            /* Type forced with ForceType  */
  74.     char *handler;        /* Handler forced with SetHandler */
  75. } mime_dir_config;
  76.  
  77. module mime_module;
  78.  
  79. void *create_mime_dir_config (pool *p, char *dummy)
  80. {
  81.     mime_dir_config *new =
  82.       (mime_dir_config *) palloc (p, sizeof(mime_dir_config));
  83.  
  84.     new->forced_types = make_table (p, 4);
  85.     new->encoding_types = make_table (p, 4);
  86.     new->language_types = make_table (p, 4);
  87.     new->handlers = make_table (p, 4);
  88.  
  89.     new->type = NULL;
  90.     new->handler = NULL;
  91.     
  92.     return new;
  93. }
  94.  
  95. void *merge_mime_dir_configs (pool *p, void *basev, void *addv)
  96. {
  97.     mime_dir_config *base = (mime_dir_config *)basev;
  98.     mime_dir_config *add = (mime_dir_config *)addv;
  99.     mime_dir_config *new =
  100.       (mime_dir_config *)palloc (p, sizeof(mime_dir_config));
  101.  
  102.     new->forced_types = overlay_tables (p, add->forced_types,
  103.                     base->forced_types);
  104.     new->encoding_types = overlay_tables (p, add->encoding_types,
  105.                       base->encoding_types);
  106.     new->language_types = overlay_tables (p, add->language_types,
  107.                       base->language_types);
  108.     new->handlers = overlay_tables (p, add->handlers,
  109.                       base->handlers);
  110.  
  111.     new->type = add->type ? add->type : base->type;
  112.     new->handler = add->handler ? add->handler : base->handler;
  113.  
  114.     return new;
  115. }
  116.  
  117. char *add_type(cmd_parms *cmd, mime_dir_config *m, char *ct, char *ext)
  118. {
  119.     if (*ext == '.') ++ext;
  120.     table_set (m->forced_types, ext, ct);
  121.     return NULL;
  122. }
  123.  
  124. char *add_encoding(cmd_parms *cmd, mime_dir_config *m, char *enc, char *ext)
  125. {
  126.     if (*ext == '.') ++ext;
  127.     table_set (m->encoding_types, ext, enc);
  128.     return NULL;
  129. }
  130.  
  131. char *add_language(cmd_parms *cmd, mime_dir_config *m, char *lang, char *ext)
  132. {
  133.     if (*ext == '.') ++ext;
  134.     table_set (m->language_types, ext, lang);
  135.     return NULL;
  136. }
  137.  
  138. char *add_handler(cmd_parms *cmd, mime_dir_config *m, char *hdlr, char *ext)
  139. {
  140.     if (*ext == '.') ++ext;
  141.     table_set (m->handlers, ext, hdlr);
  142.     return NULL;
  143. }
  144.  
  145. /* The sole bit of server configuration that the MIME module has is
  146.  * the name of its config file, so...
  147.  */
  148.  
  149. char *set_types_config (cmd_parms *cmd, void *dummy, char *arg)
  150. {
  151.     set_module_config (cmd->server->module_config, &mime_module,
  152.                pstrdup (cmd->pool, arg));
  153.     return NULL;
  154. }
  155.  
  156. command_rec mime_cmds[] = {
  157. { "AddType", add_type, NULL, OR_FILEINFO, ITERATE2,
  158.     "a mime type followed by one or more file extensions" },
  159. { "AddEncoding", add_encoding, NULL, OR_FILEINFO, ITERATE2,
  160.     "an encoding (e.g., gzip), followed by one or more file extensions" },
  161. { "AddLanguage", add_language, NULL, OR_FILEINFO, ITERATE2,
  162.     "a language (e.g., fr), followed by one or more file extensions" },
  163. { "AddHandler", add_handler, NULL, OR_FILEINFO, ITERATE2,
  164.     "a handler name followed by one or more file extensions" },
  165. { "ForceType", set_string_slot, (void*)XtOffsetOf(mime_dir_config, type),
  166.     OR_FILEINFO, TAKE1, "a media type" },
  167. { "SetHandler", set_string_slot, (void*)XtOffsetOf(mime_dir_config, handler),
  168.     OR_FILEINFO, TAKE1, "a handler name" },
  169. { "TypesConfig", set_types_config, NULL, RSRC_CONF, TAKE1,
  170.     "the MIME types config file" },
  171. { NULL }
  172. };
  173.  
  174. /* Hash table  --- only one of these per daemon; virtual hosts can
  175.  * get private versions through AddType...
  176.  */
  177.  
  178. #define MIME_HASHSIZE 27
  179. #define hash(i) (isalpha(i) ? (tolower(i)) - 'a' : 26)
  180.  
  181. static table *hash_buckets[MIME_HASHSIZE];
  182.  
  183. void init_mime (server_rec *s, pool *p)
  184. {
  185.     FILE *f;
  186.     char l[MAX_STRING_LEN];
  187.     int x;
  188.     char *types_confname = get_module_config (s->module_config, &mime_module);
  189.  
  190.     if (!types_confname) types_confname = TYPES_CONFIG_FILE;
  191.  
  192.     types_confname = server_root_relative (p, types_confname);
  193.  
  194.     if(!(f = fopen(types_confname,"r"))) {
  195.         fprintf(stderr,"httpd: could not open mime types file %s\n",
  196.                 types_confname);
  197.         perror("fopen");
  198.         exit(1);
  199.     }
  200.  
  201.     for(x=0;x<27;x++) 
  202.         hash_buckets[x] = make_table (p, 10);
  203.  
  204.     while(!(cfg_getline(l,MAX_STRING_LEN,f))) {
  205.         char *ll = l, *ct;
  206.       
  207.         if(l[0] == '#') continue;
  208.         ct = getword_conf (p, &ll);
  209.  
  210.         while(ll[0]) {
  211.             char *ext = getword_conf (p, &ll);
  212.         str_tolower (ext);    /* ??? */
  213.         table_set (hash_buckets[hash(ext[0])], ext, ct);
  214.         }
  215.     }
  216.     fclose(f);
  217. }
  218.  
  219. int find_ct(request_rec *r)
  220. {
  221.     char *fn = strrchr(r->filename, '/');
  222.     mime_dir_config *conf =
  223.       (mime_dir_config *)get_module_config(r->per_dir_config, &mime_module);
  224.     char *ext, *type, *orighandler = r->handler;
  225.  
  226.     if (S_ISDIR(r->finfo.st_mode)) {
  227.         r->content_type = DIR_MAGIC_TYPE;
  228.     return OK;
  229.     }
  230.  
  231.     /* TM -- FIXME
  232.      * 
  233.      * if r->filename does not contain a '/', the following passes a null
  234.      * pointer to getword, causing a SEGV ..
  235.      */
  236.  
  237.     if(fn == NULL) fn = r->filename;
  238.  
  239.     /* Parse filename extensions, which can be in any order */
  240.     while ((ext = getword(r->pool, &fn, '.')) && *ext) {
  241.       int found = 0;
  242.  
  243.       /* Check for Content-Type */
  244.       if ((type = table_get (conf->forced_types, ext))
  245.       || (type = table_get (hash_buckets[hash(*ext)], ext))) {
  246.           r->content_type = type;
  247.       found = 1;
  248.       }
  249.  
  250.       /* Check for Content-Language */
  251.       if ((type = table_get (conf->language_types, ext))) {
  252.       r->content_language = type;
  253.       found = 1;
  254.       }
  255.     
  256.       /* Check for Content-Encoding */
  257.       if ((type = table_get (conf->encoding_types, ext))) {
  258.       if (!r->content_encoding)
  259.           r->content_encoding = type;
  260.       else
  261.           r->content_encoding = pstrcat(r->pool, r->content_encoding,
  262.                         ", ", type, NULL);
  263.       found = 1;
  264.       }
  265.  
  266.       /* Check for a special handler, but not for proxy request */
  267.       if ((type = table_get (conf->handlers, ext)) && !r->proxyreq) {
  268.       r->handler = type;
  269.       found = 1;
  270.       }
  271.  
  272.       /* This is to deal with cases such as foo.gif.bak, which we want
  273.        * to not have a type. So if we find an unknown extension, we
  274.        * zap the type/language/encoding and reset the handler
  275.        */
  276.  
  277.       if (!found) {
  278.     r->content_type = NULL;
  279.     r->content_language = NULL;
  280.     r->content_encoding = NULL;
  281.     r->handler = orighandler;
  282.       }
  283.  
  284.     }
  285.  
  286.     /* Check for overrides with ForceType/SetHandler */
  287.  
  288.     if (conf->type && strcmp(conf->type, "none"))
  289.         r->content_type = pstrdup(r->pool, conf->type);
  290.     if (conf->handler && strcmp(conf->handler, "none"))
  291.         r->handler = pstrdup(r->pool, conf->handler);
  292.  
  293.     if (!r->content_type) return DECLINED;
  294.  
  295.     return OK;
  296. }
  297.  
  298.  
  299. module mime_module = {
  300.    STANDARD_MODULE_STUFF,
  301.    init_mime,            /* initializer */
  302.    create_mime_dir_config,
  303.    merge_mime_dir_configs,
  304.    NULL,            /* server config */
  305.    NULL,            /* merge server config */
  306.    mime_cmds,
  307.    NULL,            /* handlers */
  308.    NULL,            /* filename translation */
  309.    NULL,            /* check_user_id */
  310.    NULL,            /* check auth */
  311.    NULL,            /* check access */
  312.    find_ct,            /* type_checker */
  313.    NULL,            /* fixups */
  314.    NULL                /* logger */
  315. };
  316.