1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 * http_alias.c: Stuff for dealing with directory aliases
62 * Original by Rob McCool, rewritten in succession by David Robinson
67 #include "ap_config.h"
69 #include "http_config.h"
70 #include "http_request.h"
77 int redir_status; /* 301, 302, 303, 410, etc */
81 ap_array_header_t *aliases;
82 ap_array_header_t *redirects;
86 ap_array_header_t *redirects;
89 module MODULE_VAR_EXPORT alias_module;
91 static void *create_alias_config(ap_pool_t *p, server_rec *s)
93 alias_server_conf *a =
94 (alias_server_conf *) ap_pcalloc(p, sizeof(alias_server_conf));
96 a->aliases = ap_make_array(p, 20, sizeof(alias_entry));
97 a->redirects = ap_make_array(p, 20, sizeof(alias_entry));
101 static void *create_alias_dir_config(ap_pool_t *p, char *d)
104 (alias_dir_conf *) ap_pcalloc(p, sizeof(alias_dir_conf));
105 a->redirects = ap_make_array(p, 2, sizeof(alias_entry));
109 static void *merge_alias_config(ap_pool_t *p, void *basev, void *overridesv)
111 alias_server_conf *a =
112 (alias_server_conf *) ap_pcalloc(p, sizeof(alias_server_conf));
113 alias_server_conf *base = (alias_server_conf *) basev, *overrides = (alias_server_conf *) overridesv;
115 a->aliases = ap_append_arrays(p, overrides->aliases, base->aliases);
116 a->redirects = ap_append_arrays(p, overrides->redirects, base->redirects);
120 static void *merge_alias_dir_config(ap_pool_t *p, void *basev, void *overridesv)
123 (alias_dir_conf *) ap_pcalloc(p, sizeof(alias_dir_conf));
124 alias_dir_conf *base = (alias_dir_conf *) basev, *overrides = (alias_dir_conf *) overridesv;
125 a->redirects = ap_append_arrays(p, overrides->redirects, base->redirects);
129 static const char *add_alias_internal(cmd_parms *cmd, void *dummy, char *f, char *r,
132 server_rec *s = cmd->server;
133 alias_server_conf *conf =
134 (alias_server_conf *) ap_get_module_config(s->module_config, &alias_module);
135 alias_entry *new = ap_push_array(conf->aliases);
137 /* XX r can NOT be relative to DocumentRoot here... compat bug. */
140 new->regexp = ap_pregcomp(cmd->pool, f, REG_EXTENDED);
141 if (new->regexp == NULL)
142 return "Regular expression could not be compiled.";
147 new->handler = cmd->info;
152 static const char *add_alias(cmd_parms *cmd, void *dummy, char *f, char *r)
154 return add_alias_internal(cmd, dummy, f, r, 0);
157 static const char *add_alias_regex(cmd_parms *cmd, void *dummy, char *f, char *r)
159 return add_alias_internal(cmd, dummy, f, r, 1);
162 static const char *add_redirect_internal(cmd_parms *cmd, alias_dir_conf * dirconf,
163 const char *arg1, const char *arg2,
164 const char *arg3, int use_regex)
167 server_rec *s = cmd->server;
168 alias_server_conf *serverconf =
169 (alias_server_conf *) ap_get_module_config(s->module_config, &alias_module);
170 int status = (int) (long) cmd->info;
175 if (!strcasecmp(arg1, "gone"))
177 else if (!strcasecmp(arg1, "permanent"))
178 status = HTTP_MOVED_PERMANENTLY;
179 else if (!strcasecmp(arg1, "temp"))
180 status = HTTP_MOVED_TEMPORARILY;
181 else if (!strcasecmp(arg1, "seeother"))
182 status = HTTP_SEE_OTHER;
183 else if (ap_isdigit(*arg1))
191 r = ap_pregcomp(cmd->pool, f, REG_EXTENDED);
193 return "Regular expression could not be compiled.";
196 if (ap_is_HTTP_REDIRECT(status)) {
198 return "URL to redirect to is missing";
199 if (!use_regex && !ap_is_url(url))
200 return "Redirect to non-URL";
204 return "Redirect URL not valid for this status";
208 new = ap_push_array(dirconf->redirects);
210 new = ap_push_array(serverconf->redirects);
215 new->redir_status = status;
219 static const char *add_redirect(cmd_parms *cmd, alias_dir_conf * dirconf,
220 const char *arg1, const char *arg2, const char *arg3)
222 return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 0);
225 static const char *add_redirect_regex(cmd_parms *cmd, alias_dir_conf * dirconf,
226 char *arg1, char *arg2, char *arg3)
228 return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 1);
231 static const command_rec alias_cmds[] =
233 AP_INIT_TAKE2("Alias", add_alias, NULL, RSRC_CONF,
234 "a fakename and a realname"),
235 AP_INIT_TAKE2("ScriptAlias", add_alias, "cgi-script", RSRC_CONF,
236 "a fakename and a realname"),
237 AP_INIT_TAKE23("Redirect", add_redirect, (void *) HTTP_MOVED_TEMPORARILY,
239 "an optional status, then document to be redirected and "
241 AP_INIT_TAKE2("AliasMatch", add_alias_regex, NULL, RSRC_CONF,
242 "a regular expression and a filename"),
243 AP_INIT_TAKE2("ScriptAliasMatch", add_alias_regex, "cgi-script", RSRC_CONF,
244 "a regular expression and a filename"),
245 AP_INIT_TAKE23("RedirectMatch", add_redirect_regex,
246 (void *) HTTP_MOVED_TEMPORARILY, OR_FILEINFO,
247 "an optional status, then a regular expression and "
249 AP_INIT_TAKE2("RedirectTemp", add_redirect, (void *) HTTP_MOVED_TEMPORARILY,
251 "a document to be redirected, then the destination URL"),
252 AP_INIT_TAKE2("RedirectPermanent", add_redirect,
253 (void *) HTTP_MOVED_PERMANENTLY, OR_FILEINFO,
254 "a document to be redirected, then the destination URL"),
258 static int alias_matches(const char *uri, const char *alias_fakename)
260 const char *end_fakename = alias_fakename + strlen(alias_fakename);
261 const char *aliasp = alias_fakename, *urip = uri;
263 while (aliasp < end_fakename) {
264 if (*aliasp == '/') {
265 /* any number of '/' in the alias matches any number in
266 * the supplied URI, but there must be at least one...
271 while (*aliasp == '/')
277 /* Other characters are compared literally */
278 if (*urip++ != *aliasp++)
283 /* Check last alias path component matched all the way */
285 if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
288 /* Return number of characters from URI which matched (may be
289 * greater than length of alias, since we may have matched
296 static char *try_alias_list(request_rec *r, ap_array_header_t *aliases, int doesc, int *status)
298 alias_entry *entries = (alias_entry *) aliases->elts;
303 for (i = 0; i < aliases->nelts; ++i) {
304 alias_entry *p = &entries[i];
308 if (!ap_regexec(p->regexp, r->uri, p->regexp->re_nsub + 1, regm, 0)) {
310 found = ap_pregsub(r->pool, p->real, r->uri,
311 p->regexp->re_nsub + 1, regm);
312 if (found && doesc) {
313 found = ap_escape_uri(r->pool, found);
317 /* need something non-null */
318 found = ap_pstrdup(r->pool, "");
323 l = alias_matches(r->uri, p->fake);
328 escurl = ap_os_escape_path(r->pool, r->uri + l, 1);
330 found = ap_pstrcat(r->pool, p->real, escurl, NULL);
333 found = ap_pstrcat(r->pool, p->real, r->uri + l, NULL);
338 if (p->handler) { /* Set handler, and leave a note for mod_cgi */
339 r->handler = p->handler;
340 ap_table_setn(r->notes, "alias-forced-type", r->handler);
343 *status = p->redir_status;
353 static int translate_alias_redir(request_rec *r)
355 void *sconf = r->server->module_config;
356 alias_server_conf *serverconf =
357 (alias_server_conf *) ap_get_module_config(sconf, &alias_module);
361 if (r->uri[0] != '/' && r->uri[0] != '\0')
364 if ((ret = try_alias_list(r, serverconf->redirects, 1, &status)) != NULL) {
365 if (ap_is_HTTP_REDIRECT(status)) {
366 /* include QUERY_STRING if any */
368 ret = ap_pstrcat(r->pool, ret, "?", r->args, NULL);
370 ap_table_setn(r->headers_out, "Location", ret);
375 if ((ret = try_alias_list(r, serverconf->aliases, 0, &status)) != NULL) {
383 static int fixup_redir(request_rec *r)
385 void *dconf = r->per_dir_config;
386 alias_dir_conf *dirconf =
387 (alias_dir_conf *) ap_get_module_config(dconf, &alias_module);
391 /* It may have changed since last time, so try again */
393 if ((ret = try_alias_list(r, dirconf->redirects, 1, &status)) != NULL) {
394 if (ap_is_HTTP_REDIRECT(status))
395 ap_table_setn(r->headers_out, "Location", ret);
402 static void register_hooks(void)
404 static const char * const aszPre[]={ "mod_userdir.c",NULL };
406 ap_hook_translate_name(translate_alias_redir,aszPre,NULL,AP_HOOK_MIDDLE);
407 ap_hook_fixups(fixup_redir,NULL,NULL,AP_HOOK_MIDDLE);
410 module MODULE_VAR_EXPORT alias_module =
412 STANDARD20_MODULE_STUFF,
413 create_alias_dir_config, /* dir config creater */
414 merge_alias_dir_config, /* dir merger --- default is to override */
415 create_alias_config, /* server config */
416 merge_alias_config, /* merge server configs */
417 alias_cmds, /* command ap_table_t */
419 register_hooks /* register hooks */