mod_alias.c revision 66a73d4405f9d941672c0343b36f6c494413a6b5
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * http_alias.c: Stuff for dealing with directory aliases
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Original by Rob McCool, rewritten in succession by David Robinson
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben const char *real;
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben const char *fake;
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *create_alias_config(apr_pool_t *p, server_rec *s)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (alias_server_conf *) apr_pcalloc(p, sizeof(alias_server_conf));
066877f1a045103acfdd376d48cdd473c33f409bdougm a->aliases = apr_array_make(p, 20, sizeof(alias_entry));
066877f1a045103acfdd376d48cdd473c33f409bdougm a->redirects = apr_array_make(p, 20, sizeof(alias_entry));
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *create_alias_dir_config(apr_pool_t *p, char *d)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
066877f1a045103acfdd376d48cdd473c33f409bdougm a->redirects = apr_array_make(p, 2, sizeof(alias_entry));
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *merge_alias_config(apr_pool_t *p, void *basev, void *overridesv)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (alias_server_conf *) apr_pcalloc(p, sizeof(alias_server_conf));
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz alias_server_conf *base = (alias_server_conf *) basev;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz alias_server_conf *overrides = (alias_server_conf *) overridesv;
066877f1a045103acfdd376d48cdd473c33f409bdougm a->aliases = apr_array_append(p, overrides->aliases, base->aliases);
066877f1a045103acfdd376d48cdd473c33f409bdougm a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *merge_alias_dir_config(apr_pool_t *p, void *basev, void *overridesv)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz alias_dir_conf *base = (alias_dir_conf *) basev;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz alias_dir_conf *overrides = (alias_dir_conf *) overridesv;
066877f1a045103acfdd376d48cdd473c33f409bdougm a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
4d7e28c869788fb00bffda29a67f1b10e19f159dnd/* need prototype for overlap check */
4d7e28c869788fb00bffda29a67f1b10e19f159dndstatic int alias_matches(const char *uri, const char *alias_fakename);
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_alias_internal(cmd_parms *cmd, void *dummy,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *f, const char *r,
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein alias_server_conf *conf = ap_get_module_config(s->module_config,
4d7e28c869788fb00bffda29a67f1b10e19f159dnd alias_entry *entries = (alias_entry *)conf->aliases->elts;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* XX r can NOT be relative to DocumentRoot here... compat bug. */
ef5650b61a8e35f3cc93ec07e73efc17ea329894jorton new->regexp = ap_pregcomp(cmd->pool, f, AP_REG_EXTENDED);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return "Regular expression could not be compiled.";
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe /* XXX This may be optimized, but we must know that new->real
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe * exists. If so, we can dir merge later, trusing new->real
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe * and just canonicalizing the remainder. Not till I finish
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe * cleaning out the old ap_canonical stuff first.
4d7e28c869788fb00bffda29a67f1b10e19f159dnd /* check for overlapping (Script)Alias directives
4d7e28c869788fb00bffda29a67f1b10e19f159dnd * and throw a warning if found one
615244e3f9a19f00f305be7520a604c65ebcc193trawick || (p->regexp && !ap_regexec(p->regexp, f, 0, NULL, 0))) {
1f0ca94141196628ecadf1a91f2b60a33349872fnd "The %s directive in %s at line %d will probably "
1f0ca94141196628ecadf1a91f2b60a33349872fnd "never match because it overlaps an earlier "
1f0ca94141196628ecadf1a91f2b60a33349872fnd "%sAlias%s.",
4d7e28c869788fb00bffda29a67f1b10e19f159dnd break; /* one warning per alias should be sufficient */
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_alias(cmd_parms *cmd, void *dummy, const char *f,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *r)
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_alias_regex(cmd_parms *cmd, void *dummy, const char *f,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *r)
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein alias_server_conf *serverconf = ap_get_module_config(s->module_config,
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben const char *f = arg2;
172e83c0f024fe6396dd1f3ca3492fd83c304db5jim else if (arg3) {
da128c59ec571c4dff70f41ecba9c8a9974c6cd6niq return "Redirect: invalid first argument (of three)";
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return "Regular expression could not be compiled.";
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return "URL to redirect to is missing";
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq /* PR#35314: we can allow path components here;
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq * they get correctly resolved to full URLs.
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return "Redirect to non-URL";
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return "Redirect URL not valid for this status";
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_redirect(cmd_parms *cmd, void *dirconf,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *arg3)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 0);
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_redirect2(cmd_parms *cmd, void *dirconf,
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben return add_redirect_internal(cmd, dirconf, arg1, arg2, NULL, 0);
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_redirect_regex(cmd_parms *cmd, void *dirconf,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *arg3)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 1);
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "a fakename and a realname"),
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick AP_INIT_TAKE2("ScriptAlias", add_alias, "cgi-script", RSRC_CONF,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "a fakename and a realname"),
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick AP_INIT_TAKE23("Redirect", add_redirect, (void *) HTTP_MOVED_TEMPORARILY,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "an optional status, then document to be redirected and "
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "destination URL"),
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick AP_INIT_TAKE2("AliasMatch", add_alias_regex, NULL, RSRC_CONF,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "a regular expression and a filename"),
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick AP_INIT_TAKE2("ScriptAliasMatch", add_alias_regex, "cgi-script", RSRC_CONF,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "a regular expression and a filename"),
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "an optional status, then a regular expression and "
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "destination URL"),
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "a document to be redirected, then the destination URL"),
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "a document to be redirected, then the destination URL"),
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic int alias_matches(const char *uri, const char *alias_fakename)
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* any number of '/' in the alias matches any number in
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz * the supplied URI, but there must be at least one...
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* Other characters are compared literally */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Check last alias path component matched all the way */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Return number of characters from URI which matched (may be
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * greater than length of alias, since we may have matched
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * doubled slashes)
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantzstatic char *try_alias_list(request_rec *r, apr_array_header_t *aliases,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding alias_entry *entries = (alias_entry *) aliases->elts;
bbe046d7cbb950ab3e372e4119ae001a5fe52ed4striker if (!ap_regexec(p->regexp, r->uri, AP_MAX_REG_MATCH, regm, 0)) {
376febd94e4df1954cffe683ed62fc285d95834djerenkrantz /* Do not escape the query string or fragment. */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* need something non-null */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (l > 0) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz escurl = ap_os_escape_path(r->pool, r->uri + l, 1);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz found = apr_pstrcat(r->pool, p->real, escurl, NULL);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz found = apr_pstrcat(r->pool, p->real, r->uri + l, NULL);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (p->handler) { /* Set handler, and leave a note for mod_cgi */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz apr_table_setn(r->notes, "alias-forced-type", r->handler);
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe /* XXX This is as SLOW as can be, next step, we optimize
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe * and merge to whatever part of the found path was already
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe * canonicalized. After I finish eliminating os canonical.
32c4bc04f89b16521718145dc731f750144d7b38wrowe * Better fail test for ap_server_root_relative needed here.
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein alias_server_conf *serverconf = ap_get_module_config(sconf, &alias_module);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((ret = try_alias_list(r, serverconf->redirects, 1, &status)) != NULL) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* include QUERY_STRING if any */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz apr_table_setn(r->headers_out, "Location", ret);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((ret = try_alias_list(r, serverconf->aliases, 0, &status)) != NULL) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (alias_dir_conf *) ap_get_module_config(dconf, &alias_module);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* It may have changed since last time, so try again */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((ret = try_alias_list(r, dirconf->redirects, 1, &status)) != NULL) {
449efc4dc68e42cc4421d15498a689618aab5dc3coar "incomplete redirection target of '%s' for "
449efc4dc68e42cc4421d15498a689618aab5dc3coar "URI '%s' modified to '%s'",
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar "cannot redirect '%s' to '%s'; "
449efc4dc68e42cc4421d15498a689618aab5dc3coar "target is not a valid absoluteURI or abs_path",
affb82a2d7fc07c1a862d800ef47af966b898768nd /* append requested query only, if the config didn't
affb82a2d7fc07c1a862d800ef47af966b898768nd * supply its own.
e8c95302287c86cd1f984eeb25cf3bfa9e2d33bbslive static const char * const aszSucc[]={ "mod_userdir.c",
1376737cb6afa24d3e12f3a223318fe1bd71bb1fslive ap_hook_translate_name(translate_alias_redir,NULL,aszSucc,APR_HOOK_MIDDLE);
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe ap_hook_fixups(fixup_redir,NULL,NULL,APR_HOOK_MIDDLE);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz create_alias_dir_config, /* dir config creater */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz merge_alias_dir_config, /* dir merger --- default is to override */