mod_speling.c revision b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering/* ====================================================================
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * Copyright (c) 1996-1999 The Apache Group. All rights reserved.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * Redistribution and use in source and binary forms, with or without
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * modification, are permitted provided that the following conditions
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * 1. Redistributions of source code must retain the above copyright
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * notice, this list of conditions and the following disclaimer.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * 2. Redistributions in binary form must reproduce the above copyright
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * notice, this list of conditions and the following disclaimer in
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * the documentation and/or other materials provided with the
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * distribution.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * 3. All advertising materials mentioning features or use of this
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * software must display the following acknowledgment:
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * "This product includes software developed by the Apache Group
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * for use in the Apache HTTP server project (http://www.apache.org/)."
dc3a1b76a6a6f9dfe9b451f534587251b50a0685Lennart Poettering * 4. The names "Apache Server" and "Apache Group" must not be used to
dc3a1b76a6a6f9dfe9b451f534587251b50a0685Lennart Poettering * endorse or promote products derived from this software without
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * prior written permission. For written permission, please contact
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * apache@apache.org.
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * 5. Products derived from this software may not be called "Apache"
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * nor may "Apache" appear in their names without prior written
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * permission of the Apache Group.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * 6. Redistributions of any form whatsoever must retain the following
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * acknowledgment:
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * "This product includes software developed by the Apache Group
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * for use in the Apache HTTP server project (http://www.apache.org/)."
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * OF THE POSSIBILITY OF SUCH DAMAGE.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * ====================================================================
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * This software consists of voluntary contributions made by many
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * individuals on behalf of the Apache Group and was originally based
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * on public domain software written at the National Center for
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * Supercomputing Applications, University of Illinois, Urbana-Champaign.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * For more information on the Apache Group and the Apache HTTP server
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * project, please see <http://www.apache.org/>.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering/* mod_speling.c - by Alexei Kosut <akosut@organic.com> June, 1996
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * This module is transparent, and simple. It attempts to correct
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * misspellings of URLs that users might have entered, namely by checking
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * capitalizations. If it finds a match, it sends a redirect.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * 08-Aug-1997 <Martin.Kraemer@Mch.SNI.De>
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * o Upgraded module interface to apache_1.3a2-dev API (more NULL's in
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering * speling_module).
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * o Integrated tcsh's "spelling correction" routine which allows one
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * misspelling (character insertion/omission/typo/transposition).
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * Rewrote it to ignore case as well. This ought to catch the majority
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * of misspelled requests.
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * o Commented out the second pass where files' suffixes are stripped.
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * Given the better hit rate of the first pass, this rather ugly
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * (request index.html, receive index.db ?!?!) solution can be
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering * o wrote a "kind of" html page for mod_speling
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * Activate it with "CheckSpelling On"
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringtypedef struct {
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * Create a configuration specific to this module for a server or directory
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * location, and fill it with the default settings.
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * The API says that in the absence of a merge function, the record for the
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * closest ancestor is used exclusively. That's what we want, so we don't
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * bother to have such a function.
19adb8a3204fefd91411b5f0f350c8bc6bcf75feZbigniew Jędrzejewski-Szmek spconfig *cfg = ap_pcalloc(p, sizeof(spconfig));
af9792ac7f39354f80e9006c42c2400c5e41c447Lennart Poettering * Respond to a callback to create configuration record for a server or
af9792ac7f39354f80e9006c42c2400c5e41c447Lennart Poettering * vhost environment.
dc3a1b76a6a6f9dfe9b451f534587251b50a0685Lennart Poetteringstatic void *create_mconfig_for_server(ap_context_t *p, server_rec *s)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * Respond to a callback to create a config record for a specific directory.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poetteringstatic void *create_mconfig_for_directory(ap_context_t *p, char *dir)
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering * Handler for the CheckSpelling directive, which is FLAG.
85a428c69465b047731b6abb5005f01824f1444eLennart Poetteringstatic const char *set_speling(cmd_parms *cmd, void *mconfig, int arg)
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek * Define the directives specific to this module. This structure is referenced
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * later by the 'module' structure.
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering { "CheckSpelling", set_speling, NULL, OR_OPTIONS, FLAG,
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek "whether or not to fix miscapitalized/misspelled requests" },
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poetteringstatic const char *sp_reason_str[] =
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering "miscapitalized",
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering "transposed characters",
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering "character missing",
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering "extra character",
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering "mistyped character",
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering "common basename",
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poetteringtypedef struct {
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * spdist() is taken from Kernighan & Pike,
return DECLINED;
return DECLINED;
return DECLINED;
if (r->main) {
return DECLINED;
* r->uri: /correct-url/mispelling/more
* r->filename: /correct-file/mispelling r->path_info: /more
return DECLINED;
/* postgood = mispelling/more */
return DECLINED;
return DECLINED;
sp_reason q;
return OK;
* missing/extra/transposed char)
* type (index.html <-> index.db).
* > If you're using MultiViews, and have a file named foobar.html,
* > it would locate "foobar.html". Not perfect, but I ran into
#ifdef WANT_BASENAME_MATCH
* (e.g. foo.gif and foo.html) This code will pick the first one
char *nuri;
const char *ref;
NULL);
return HTTP_MOVED_PERMANENTLY;
ap_context_t *p;
p = r->pool;
*(const char **)ap_push_array(t) =
*(const char **)ap_push_array(t) =
char *vuri;
const char *reason;
NULL);
*(const char **)ap_push_array(t) =
*(const char **)ap_push_array(t) =
/* Pass our ap_table_t to http_protocol.c (see mod_negotiation): */
return HTTP_MULTIPLE_CHOICES;
return OK;
static void register_hooks(void)