home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / linux / apache / contrib / modules / mod_speling.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  7.5 KB  |  219 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1996 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. #include "httpd.h"
  55. #include "http_config.h"
  56. #include "http_log.h"
  57.  
  58. /* mod_speling.c - by Alexei Kosut <akosut@organic.com> June, 1996
  59.  *
  60.  * This module is transparent, and simple. It attemps to correct
  61.  * mispellings of URLs that users might have entered, namely by checking
  62.  * capitalizations. If it finds a match, it sends a redirect.
  63.  *
  64.  * Activate it with "CheckSpelling On"
  65.  */
  66.  
  67. module speling_module;
  68.  
  69. /* We use the "unconventional" mod_userdir approach here. And heck,
  70.  * here it's just one int!
  71.  */
  72.  
  73. void *create_speling_config (pool *dummy, server_rec *s) { 
  74.     return (void *)0;
  75. }
  76.  
  77. char *set_speling (cmd_parms *cmd, void *dummy, int arg)
  78. {
  79.     void *server_conf = cmd->server->module_config;
  80.     
  81.     set_module_config (server_conf, &speling_module, (void *)arg);
  82.     return NULL;
  83. }
  84.  
  85. command_rec speling_cmds[] = {
  86. { "CheckSpelling", set_speling, NULL, RSRC_CONF, FLAG,
  87.     "whether or not to fix miscapitalized requests" },
  88. { NULL }
  89. };
  90.  
  91.  
  92. int check_speling (request_rec *r)
  93. {
  94.     void *server_conf = r->server->module_config;
  95.     char *good, *bad, *postgood, *url;
  96.     int filoc, dotloc, urlen, pglen;
  97. DIR *dirp;
  98.     struct DIR_TYPE *dir_entry;
  99.  
  100.     if (!(int)get_module_config(server_conf, &speling_module))
  101.       return DECLINED;
  102.  
  103.     /* We only want to worry about GETs */
  104.     if (r->method_number != M_GET) return DECLINED;
  105.  
  106.     /* We've already got a file of some kind or another */
  107.     if (r->proxyreq || (r->finfo.st_mode != 0)) return DECLINED;
  108.  
  109.     /* This is a sub request - don't mess with it */
  110.     if (r->main) return DECLINED;
  111.  
  112.     /* The request should end up looking like this:
  113.      * r->uri: /correct-url/mispelling/more
  114.      * r->filename: /correct-file/mispelling r->path_info: /more
  115.      *
  116.      * So we do this in steps. First break r->filename into two peices
  117.      */
  118.  
  119.     filoc = rind(r->filename, '/');
  120.     if (filoc == -1) return DECLINED;
  121.  
  122.     /* good = /correct-file */
  123.     good = pstrndup(r->pool, r->filename, filoc);
  124.     /* bad = mispelling */
  125.     bad = pstrdup(r->pool, r->filename+filoc+1);
  126.     /* postgood = mispelling/more */
  127.     postgood = pstrcat(r->pool, bad, r->path_info, NULL);
  128.  
  129.     urlen = strlen(r->uri);
  130.     pglen = strlen(postgood);
  131.  
  132.     /* Check to see if the URL peices add up */
  133.     if (strcmp(postgood, r->uri + (urlen-pglen)))
  134.       return DECLINED;
  135.  
  136.     /* url = /correct-url */
  137.     url = pstrndup(r->pool, r->uri, (urlen-pglen));
  138.  
  139.     /* Now open the directory and do ourselves a check... */
  140.     dirp = opendir (good);
  141.     if (dirp == NULL)    /* Oops, not a directory... */
  142.       return DECLINED;
  143.  
  144.     while ((dir_entry = readdir (dirp))) {
  145.       if (!strcasecmp(bad, dir_entry->d_name)) {
  146.     /* Wow... we found us a mispelling. Construct a fixed url */
  147.     char *nuri = pstrcat(r->pool, url, dir_entry->d_name, r->path_info,
  148.                  NULL);
  149.     char *ref = table_get(r->headers_in, "Referer");
  150.     
  151.     table_set(r->headers_out, "Location", construct_url(r->pool,
  152.          nuri, r->server));
  153.     log_error(pstrcat(r->pool, "Fixed spelling: ", r->uri, " to ", nuri,
  154.               ref ? " from " : NULL, ref, NULL), r->server);
  155.     closedir(dirp);
  156.     return REDIRECT;
  157.       }
  158.     }
  159.  
  160.     /* Okay... we didn't find anything. Now we take out the hard-core
  161.      * power tools. There are several cases here. Someone might have
  162.      * entered a wrong extension (.htm instead of .html or vice versa)
  163.      * or the document could be negotated. At any rate, now we just compare
  164.      * stuff before the first dot. If it matches, we figure we got us a
  165.      * match. This can result in wrong things if there are files of
  166.      * different content types but the same prefix (e.g. foo.gif and foo.html)
  167.      * This code will pick the first one it finds. Better than a Not Found,
  168.      * though.
  169.      */
  170.  
  171.     rewinddir(dirp);
  172.  
  173.     dotloc = ind(bad, '.');
  174.     if (dotloc == -1)
  175.       dotloc = strlen(bad);
  176.  
  177.     while ((dir_entry = readdir (dirp))) {
  178.       int entloc = ind(dir_entry->d_name, '.');
  179.       if (entloc == -1)
  180.     entloc = strlen(dir_entry->d_name);
  181.  
  182.       if ((dotloc == entloc) && !strncasecmp(bad, dir_entry->d_name, dotloc)) {
  183.     /* Wow... we found us a mispelling. Construct a fixed url */
  184.     char *nuri = pstrcat(r->pool, url, dir_entry->d_name, r->path_info,
  185.                  NULL);
  186.     char *ref = table_get(r->headers_in, "Referer");
  187.     
  188.     table_set(r->headers_out, "Location", construct_url(r->pool,
  189.          nuri, r->server));
  190.     log_error(pstrcat(r->pool, "Fixed spelling: ", r->uri, " to ", nuri,
  191.               ref ? " from " : NULL, ref, NULL), r->server);
  192.     closedir(dirp);
  193.     return REDIRECT;
  194.       }
  195.     }
  196.  
  197.     closedir(dirp);
  198.  
  199.     return OK;
  200. }
  201.  
  202. module speling_module = {
  203.    STANDARD_MODULE_STUFF,
  204.    NULL,            /* initializer */
  205.    NULL,            /* create per-dir config */
  206.    NULL,            /* merge per-dir config */
  207.    create_speling_config,        /* server config */
  208.    NULL,                   /* merge server config */
  209.    speling_cmds,               /* command table */
  210.    NULL,            /* handlers */
  211.    NULL,            /* filename translation */
  212.    NULL,            /* check_user_id */
  213.    NULL,            /* check auth */
  214.    NULL,            /* check access */
  215.    NULL,            /* type_checker */
  216.    check_speling,             /* fixups */
  217.    NULL                /* logger */
  218. };
  219.