mod_alias.c revision afadb90f859c4987505e745edee656850d7f8d49
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * contributor license agreements. See the NOTICE file distributed with
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * this work for additional information regarding copyright ownership.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * The ASF licenses this file to You under the Apache License, Version 2.0
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * (the "License"); you may not use this file except in compliance with
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the License. You may obtain a copy of the License at
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Unless required by applicable law or agreed to in writing, software
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * distributed under the License is distributed on an "AS IS" BASIS,
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * See the License for the specific language governing permissions and
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * limitations under the License.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * http_alias.c: Stuff for dealing with directory aliases
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * Original by Rob McCool, rewritten in succession by David Robinson
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingtypedef struct {
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding const char *real;
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding const char *fake;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingtypedef struct {
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffieldingtypedef struct {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void *create_alias_config(apr_pool_t *p, server_rec *s)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (alias_server_conf *) apr_pcalloc(p, sizeof(alias_server_conf));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding a->aliases = apr_array_make(p, 20, sizeof(alias_entry));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding a->redirects = apr_array_make(p, 20, sizeof(alias_entry));
90211b595b36b22a529f9f68238f77fed273e08erbbstatic void *create_alias_dir_config(apr_pool_t *p, char *d)
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding a->redirects = apr_array_make(p, 2, sizeof(alias_entry));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void *merge_alias_config(apr_pool_t *p, void *basev, void *overridesv)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (alias_server_conf *) apr_pcalloc(p, sizeof(alias_server_conf));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding alias_server_conf *base = (alias_server_conf *) basev;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding alias_server_conf *overrides = (alias_server_conf *) overridesv;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding a->aliases = apr_array_append(p, overrides->aliases, base->aliases);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void *merge_alias_dir_config(apr_pool_t *p, void *basev, void *overridesv)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding alias_dir_conf *overrides = (alias_dir_conf *) overridesv;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* need prototype for overlap check */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int alias_matches(const char *uri, const char *alias_fakename);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic const char *add_alias_internal(cmd_parms *cmd, void *dummy,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *f, const char *r,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding alias_server_conf *conf = ap_get_module_config(s->module_config,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding alias_entry *entries = (alias_entry *)conf->aliases->elts;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* XX r can NOT be relative to DocumentRoot here... compat bug. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding new->regexp = ap_pregcomp(cmd->pool, f, AP_REG_EXTENDED);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return "Regular expression could not be compiled.";
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* XXX This may be optimized, but we must know that new->real
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * exists. If so, we can dir merge later, trusing new->real
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and just canonicalizing the remainder. Not till I finish
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * cleaning out the old ap_canonical stuff first.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* check for overlapping (Script)Alias directives
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and throw a warning if found one
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding || (p->regexp && !ap_regexec(p->regexp, f, 0, NULL, 0))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "The %s directive in %s at line %d will probably "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "never match because it overlaps an earlier "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "%sAlias%s.",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding break; /* one warning per alias should be sufficient */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic const char *add_alias(cmd_parms *cmd, void *dummy, const char *f,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *r)
a952e0c6ece342d88f2bc04e20dcfc563398eed3rbbstatic const char *add_alias_regex(cmd_parms *cmd, void *dummy, const char *f,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding alias_server_conf *serverconf = ap_get_module_config(s->module_config,
ec727afb817df0e5202135278c0d2902923cc980stoddard const char *f = arg2;
ec727afb817df0e5202135278c0d2902923cc980stoddard * Logic flow:
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * Go ahead and try to grok the 1st arg, in case it is a
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * Redirect status. Now if we have 3 args, we expect that
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * we were able to understand that 1st argument (it's something
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * we expected, so if not, then we bail
061c3874622b67beff724876b9bd1b1f291f2eaafielding return "Redirect: invalid first argument (of three)";
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * if we don't have the 3rd arg and we didn't understand the 1st
9ede6357edc9aff1fb2f7edebefab473673298aarbb * one, then assume URL-path URL. This also handles case, eg, GONE
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * we even though we don't have a 3rd arg, we did understand the 1st
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * one, so we don't want to re-arrange
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return "Regular expression could not be compiled.";
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return "URL to redirect to is missing";
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* PR#35314: we can allow path components here;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * they get correctly resolved to full URLs.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!use_regex && !ap_is_url(url) && (url[0] != '/'))
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return "Redirect to non-URL";
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return "Redirect URL not valid for this status";
2b484455736f4c30447aa852764f53282cbeb5ddrbbstatic const char *add_redirect(cmd_parms *cmd, void *dirconf,
2b484455736f4c30447aa852764f53282cbeb5ddrbb const char *arg3)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 0);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic const char *add_redirect2(cmd_parms *cmd, void *dirconf,
4e1e8abb2a6e588366a4680b039b460fc5fe1ccdstoddard return add_redirect_internal(cmd, dirconf, arg1, arg2, NULL, 0);
40303323ae9960bd38a0f03d2c10e897a7badd88rbbstatic const char *add_redirect_regex(cmd_parms *cmd, void *dirconf,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *arg3)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 1);
4e1e8abb2a6e588366a4680b039b460fc5fe1ccdstoddard "a fakename and a realname"),
ec727afb817df0e5202135278c0d2902923cc980stoddard AP_INIT_TAKE2("ScriptAlias", add_alias, "cgi-script", RSRC_CONF,
ec727afb817df0e5202135278c0d2902923cc980stoddard "a fakename and a realname"),
ec727afb817df0e5202135278c0d2902923cc980stoddard AP_INIT_TAKE23("Redirect", add_redirect, (void *) HTTP_MOVED_TEMPORARILY,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "an optional status, then document to be redirected and "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "destination URL"),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding AP_INIT_TAKE2("AliasMatch", add_alias_regex, NULL, RSRC_CONF,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "a regular expression and a filename"),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding AP_INIT_TAKE2("ScriptAliasMatch", add_alias_regex, "cgi-script", RSRC_CONF,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "a regular expression and a filename"),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "an optional status, then a regular expression and "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "destination URL"),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "a document to be redirected, then the destination URL"),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "a document to be redirected, then the destination URL"),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int alias_matches(const char *uri, const char *alias_fakename)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* any number of '/' in the alias matches any number in
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the supplied URI, but there must be at least one...
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Other characters are compared literally */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Check last alias path component matched all the way */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Return number of characters from URI which matched (may be
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * greater than length of alias, since we may have matched
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * doubled slashes)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic char *try_alias_list(request_rec *r, apr_array_header_t *aliases,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding alias_entry *entries = (alias_entry *) aliases->elts;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!ap_regexec(p->regexp, r->uri, AP_MAX_REG_MATCH, regm, 0)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Do not escape the query string or fragment. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* need something non-null */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (l > 0) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding escurl = ap_os_escape_path(r->pool, r->uri + l, 1);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding found = apr_pstrcat(r->pool, p->real, escurl, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding found = apr_pstrcat(r->pool, p->real, r->uri + l, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (p->handler) { /* Set handler, and leave a note for mod_cgi */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_table_setn(r->notes, "alias-forced-type", r->handler);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* XXX This is as SLOW as can be, next step, we optimize
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and merge to whatever part of the found path was already
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * canonicalized. After I finish eliminating os canonical.
a5b7afe9f897d699f46d0b668b4c87936b4628f7manoj * Better fail test for ap_server_root_relative needed here.
3597d6b10c88c252f156fe8061b659a38dea38c2rbb alias_server_conf *serverconf = ap_get_module_config(sconf, &alias_module);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((ret = try_alias_list(r, serverconf->redirects, 1, &status)) != NULL) {
a5b7afe9f897d699f46d0b668b4c87936b4628f7manoj /* include QUERY_STRING if any */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL);
70a2c0407879c11b29adc782d1665a0131f30b3frbb if ((ret = try_alias_list(r, serverconf->aliases, 0, &status)) != NULL) {
0942697a9b5de44865676345a3828741c827efe6rbb (alias_dir_conf *) ap_get_module_config(dconf, &alias_module);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* It may have changed since last time, so try again */
3597d6b10c88c252f156fe8061b659a38dea38c2rbb if ((ret = try_alias_list(r, dirconf->redirects, 1, &status)) != NULL) {
3597d6b10c88c252f156fe8061b659a38dea38c2rbb "incomplete redirection target of '%s' for "
3597d6b10c88c252f156fe8061b659a38dea38c2rbb "URI '%s' modified to '%s'",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "cannot redirect '%s' to '%s'; "
0942697a9b5de44865676345a3828741c827efe6rbb "target is not a valid absoluteURI or abs_path",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* append requested query only, if the config didn't
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * supply its own.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_hook_translate_name(translate_alias_redir,NULL,aszSucc,APR_HOOK_MIDDLE);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_hook_fixups(fixup_redir,NULL,NULL,APR_HOOK_MIDDLE);
a8577c2083e1fe27d78bf198668eb8bc2687d060martin merge_alias_dir_config, /* dir merger --- default is to override */