home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / mod_alia.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-08  |  8.8 KB  |  286 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_alias.c: Stuff for dealing with directory aliases
  57.  * 
  58.  * Original by Rob McCool, rewritten in succession by David Robinson
  59.  * and rst.
  60.  * 
  61.  */
  62.  
  63. #include "httpd.h"
  64. #include "http_config.h"
  65.  
  66. typedef struct {
  67.     char *real;
  68.     char *fake;
  69.     char *handler;
  70. } alias_entry;
  71.  
  72. typedef struct {
  73.     array_header *aliases;
  74.     array_header *redirects;
  75. } alias_server_conf;
  76.  
  77. typedef struct {
  78.     array_header *redirects;
  79. } alias_dir_conf;
  80. module alias_module;
  81.  
  82. void *create_alias_config (pool *p, server_rec *s)
  83. {
  84.     alias_server_conf *a =
  85.       (alias_server_conf *)pcalloc (p, sizeof(alias_server_conf));
  86.  
  87.     a->aliases = make_array (p, 20, sizeof(alias_entry));
  88.     a->redirects = make_array (p, 20, sizeof(alias_entry));
  89.     return a;
  90. }
  91.  
  92. void *create_alias_dir_config (pool *p, char *d)
  93. {
  94.     alias_dir_conf *a =
  95.       (alias_dir_conf *)pcalloc (p, sizeof(alias_dir_conf));
  96.     a->redirects = make_array (p, 2, sizeof(alias_entry));
  97.     return a;
  98. }
  99. void *merge_alias_config (pool *p, void *basev, void *overridesv)
  100. {
  101.     alias_server_conf *a =
  102.     (alias_server_conf *)pcalloc (p, sizeof(alias_server_conf));
  103.     alias_server_conf *base = (alias_server_conf *)basev,
  104.     *overrides = (alias_server_conf *)overridesv;
  105.  
  106.     a->aliases = append_arrays (p, overrides->aliases, base->aliases);
  107.     a->redirects = append_arrays (p, overrides->redirects, base->redirects);
  108.     return a;
  109. }
  110.  
  111. void *merge_alias_dir_config (pool *p, void *basev, void *overridesv)
  112. {
  113.     alias_dir_conf *a =
  114.       (alias_dir_conf *)pcalloc (p, sizeof(alias_dir_conf));
  115.     alias_dir_conf *base = (alias_dir_conf *)basev,
  116.       *overrides = (alias_dir_conf *)overridesv;
  117.     a->redirects = append_arrays (p, overrides->redirects, base->redirects);
  118.     return a;
  119. }
  120. char *add_alias(cmd_parms *cmd, void *dummy, char *f, char *r)
  121. {
  122.     server_rec *s = cmd->server;
  123.     alias_server_conf *conf =
  124.         (alias_server_conf *)get_module_config(s->module_config,&alias_module);
  125.     alias_entry *new = push_array (conf->aliases);
  126.  
  127.     /* XX r can NOT be relative to DocumentRoot here... compat bug. */
  128.     
  129.     new->fake = f; new->real = r; new->handler = cmd->info;
  130.     return NULL;
  131. }
  132.  
  133. char *add_redirect(cmd_parms *cmd, alias_dir_conf *dirconf, char *f, char *url)
  134. {
  135.     alias_entry *new;
  136.     server_rec *s = cmd->server;
  137.     alias_server_conf *serverconf =
  138.         (alias_server_conf *)get_module_config(s->module_config,&alias_module);
  139.  
  140.     if (!is_url (url)) return "Redirect to non-URL";
  141.     if ( cmd->path )
  142.     {
  143.         new = push_array (dirconf->redirects);
  144.     }
  145.     else
  146.     {
  147.         new = push_array (serverconf->redirects);
  148.     }
  149.     new->fake = f; new->real = url;
  150.     return NULL;
  151. }
  152.  
  153. command_rec alias_cmds[] = {
  154. { "Alias", add_alias, NULL, RSRC_CONF, TAKE2, 
  155.     "a fakename and a realname"},
  156. { "ScriptAlias", add_alias, "cgi-script", RSRC_CONF, TAKE2, 
  157.     "a fakename and a realname"},
  158. { "Redirect", add_redirect, NULL, OR_FILEINFO, TAKE2, 
  159.     "a document to be redirected, then the destination URL" },
  160. { NULL }
  161. };
  162.  
  163. int alias_matches (char *uri, char *alias_fakename)
  164. {
  165.     char *end_fakename = alias_fakename + strlen (alias_fakename);
  166.     char *aliasp = alias_fakename, *urip = uri;
  167.  
  168.     while (aliasp < end_fakename) {
  169.     if (*aliasp == '/') {
  170.         /* any number of '/' in the alias matches any number in
  171.          * the supplied URI, but there must be at least one...
  172.          */
  173.         if (*urip != '/') return 0;
  174.         
  175.         while (*aliasp == '/') ++ aliasp;
  176.         while (*urip == '/') ++ urip;
  177.     }
  178.     else {
  179.         /* Other characters are compared literally */
  180.         if (*urip++ != *aliasp++) return 0;
  181.     }
  182.     }
  183.  
  184.     /* Check last alias path component matched all the way */
  185.  
  186.     if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
  187.     return 0;
  188.  
  189.     /* Return number of characters from URI which matched (may be
  190.      * greater than length of alias, since we may have matched
  191.      * doubled slashes)
  192.      */
  193.  
  194.     return urip - uri;
  195. }
  196.  
  197. char *try_alias_list (request_rec *r, array_header *aliases, int doesc)
  198. {
  199.     alias_entry *entries = (alias_entry *)aliases->elts;
  200.     int i;
  201.     
  202.     for (i = 0; i < aliases->nelts; ++i) {
  203.         alias_entry *p = &entries[i];
  204.         int l = alias_matches (r->uri, p->fake);
  205.  
  206.         if (l > 0) {
  207.         if (p->handler) { /* Set handler, and leave a note for mod_cgi */
  208.             r->handler = pstrdup(r->pool, p->handler);
  209.         table_set (r->notes, "alias-forced-type", p->handler);
  210.         }
  211.  
  212.         if (doesc) {
  213.         char *escurl;
  214.         escurl = os_escape_path(r->pool, r->uri + l, 1);
  215.         return pstrcat(r->pool, p->real, escurl, NULL);
  216.         } else
  217.         return pstrcat(r->pool, p->real, r->uri + l, NULL);
  218.         }
  219.     }
  220.  
  221.     return NULL;
  222. }
  223.  
  224. int translate_alias_redir(request_rec *r)
  225. {
  226.     void *sconf = r->server->module_config;
  227.     alias_server_conf *serverconf =
  228.         (alias_server_conf *)get_module_config(sconf, &alias_module);
  229.     char *ret;
  230.  
  231. #ifdef __EMX__
  232.     /* Add support for OS/2 drive names */
  233.     if ((r->uri[0] != '/' && r->uri[0] != '\0') && r->uri[1] != ':')
  234. #else    
  235.     if (r->uri[0] != '/' && r->uri[0] != '\0') 
  236. #endif    
  237.         return BAD_REQUEST;
  238.  
  239.     if ((ret = try_alias_list (r, serverconf->redirects, 1)) != NULL) {
  240.         table_set (r->headers_out, "Location", ret);
  241.         return REDIRECT;
  242.     }
  243.     
  244.     if ((ret = try_alias_list (r, serverconf->aliases, 0)) != NULL) {
  245.         r->filename = ret;
  246.         return OK;
  247.     }
  248.     
  249.     return DECLINED;
  250. }
  251.  
  252. int fixup_redir(request_rec *r)
  253. {
  254.     void *dconf = r->per_dir_config;
  255.     alias_dir_conf *dirconf =
  256.         (alias_dir_conf *)get_module_config(dconf, &alias_module);
  257.     char *ret;
  258.  
  259.     /* It may have changed since last time, so try again */
  260.  
  261.     if ((ret = try_alias_list (r, dirconf->redirects, 1)) != NULL) {
  262.         table_set (r->headers_out, "Location", ret);
  263.         return REDIRECT;
  264.     }
  265.  
  266.     return DECLINED;
  267. }
  268.  
  269. module alias_module = {
  270.    STANDARD_MODULE_STUFF,
  271.    NULL,            /* initializer */
  272.    create_alias_dir_config,    /* dir config creater */
  273.    merge_alias_dir_config,    /* dir merger --- default is to override */
  274.    create_alias_config,        /* server config */
  275.    merge_alias_config,        /* merge server configs */
  276.    alias_cmds,            /* command table */
  277.    NULL,            /* handlers */
  278.    translate_alias_redir,    /* filename translation */
  279.    NULL,            /* check_user_id */
  280.    NULL,            /* check auth */
  281.    NULL,            /* check access */
  282.    NULL,            /* type_checker */
  283.    fixup_redir,            /* fixups */
  284.    NULL                /* logger */
  285. };
  286.