/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/
/* * http_alias.c: Stuff for dealing with directory aliases * * Original by Rob McCool, rewritten in succession by David Robinson * and rst. *
*/
if (use_regex) {
new->regexp = ap_pregcomp(cmd->pool, fake, AP_REG_EXTENDED); if (new->regexp == NULL) return"Regular expression could not be compiled.";
new->real = real;
} else { /* XXX This may be optimized, but we must know that new->real * exists. If so, we can dir merge later, trusing new->real * and just canonicalizing the remainder. Not till I finish * cleaning out the old ap_canonical stuff first.
*/
new->real = real;
}
new->fake = fake;
new->handler = cmd->info;
/* check for overlapping (Script)Alias directives * and throw a warning if found one
*/ if (!use_regex) { for (i = 0; i < conf->aliases->nelts - 1; ++i) {
alias_entry *alias = &entries[i];
if ( (!alias->regexp && alias_matches(fake, alias->fake) > 0)
|| (alias->regexp && !ap_regexec(alias->regexp, fake, 0, NULL, 0))) {
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00671) "The %s directive in %s at line %d will probably " "never match because it overlaps an earlier " "%sAlias%s.",
cmd->cmd->name, cmd->directive->filename,
cmd->directive->line_num,
alias->handler ? "Script" : "",
alias->regexp ? "Match" : "");
break; /* one warning per alias should be sufficient */
}
}
}
/* * Logic flow: * Go ahead and try to grok the 1st arg, in case it is a * Redirect status. Now if we have 3 args, we expect that * we were able to understand that 1st argument (it's something * we expected, so if not, then we bail
*/ if (!strcasecmp(arg1, "permanent"))
status = HTTP_MOVED_PERMANENTLY; elseif (!strcasecmp(arg1, "temp"))
status = HTTP_MOVED_TEMPORARILY; elseif (!strcasecmp(arg1, "seeother"))
status = HTTP_SEE_OTHER; elseif (!strcasecmp(arg1, "gone")) {
status = HTTP_GONE;
grokarg1 = -1;
} elseif (apr_isdigit(*arg1)) {
status = atoi(arg1); if (!ap_is_HTTP_REDIRECT(status)) {
grokarg1 = -1;
}
} else {
grokarg1 = 0;
}
if (arg3 && !grokarg1) return"Redirect: invalid first argument (of three)";
/* * if we have the 2nd arg and we understand the 1st one as a redirect * status (3xx, but not things like 404 /robots.txt), or if we have the * 1st arg but don't understand it, we use the expression syntax assuming * a path from the location. * * if we understand the first arg but have no second arg, we are dealing * with a status like "GONE" or a non-redirect status (e.g. 404, 503).
*/ if (!cmd->path) { /* <Location> context only for now */
;
} elseif ((grokarg1 > 0 && arg2 && !arg3) || (!grokarg1 && !arg2)) { constchar *expr_err = NULL;
/* * if we don't have the 3rd arg and we didn't understand the 1st * one, then assume URL-path URL. This also handles case, eg, GONE * we even though we don't have a 3rd arg, we did understand the 1st * one, so we don't want to re-arrange
*/ if (!arg3 && !grokarg1) {
fake = arg1;
url = arg2;
}
if (use_regex) {
regex = ap_pregcomp(cmd->pool, fake, AP_REG_EXTENDED); if (regex == NULL) return"Regular expression could not be compiled.";
}
if (ap_is_HTTP_REDIRECT(status)) { if (!url) return"URL to redirect to is missing"; /* PR#35314: we can allow path components here; * they get correctly resolved to full URLs.
*/ if (!use_regex && !ap_is_url(url) && (url[0] != '/')) return"Redirect to non-URL";
} else { if (url) return"Redirect URL not valid for this status";
}
if (cmd->path) new = apr_array_push(dirconf->redirects); else new = apr_array_push(serverconf->redirects);
while (*aliasp) { if (*aliasp == '/') { /* any number of '/' in the alias matches any number in * the supplied URI, but there must be at least one...
*/ if (*urip != '/') return 0;
do {
++aliasp;
} while (*aliasp == '/'); do {
++urip;
} while (*urip == '/');
} else { /* Other characters are compared literally */ if (*urip++ != *aliasp++) return 0;
}
}
/* Check last alias path component matched all the way */
if (dirconf->alias_fake && dirconf->alias_preserve_path == ALIAS_FLAG_ON) { int l;
l = alias_matches(r->uri, dirconf->alias_fake);
if (l > 0) {
ap_set_context_info(r, dirconf->alias_fake, found);
found = apr_pstrcat(r->pool, found, r->uri + l, NULL);
}
}
if (dirconf->handler) { /* Set handler, and leave a note for mod_cgi */
r->handler = dirconf->handler;
apr_table_setn(r->notes, "alias-forced-type", r->handler);
} /* XXX This is as SLOW as can be, next step, we optimize * and merge to whatever part of the found path was already * canonicalized. After I finish eliminating os canonical. * Better fail test for ap_server_root_relative needed here.
*/
found = ap_server_root_relative(r->pool, found); return found;
apr_uri_parse(r->pool, found, &uri); /* Do not escape the query string or fragment. */
found = apr_uri_unparse(r->pool, &uri, APR_URI_UNP_OMITQUERY);
found = ap_escape_uri(r->pool, found); if (uri.query) {
found = apr_pstrcat(r->pool, found, "?", uri.query, NULL);
} if (uri.fragment) {
found = apr_pstrcat(r->pool, found, "#", uri.fragment, NULL);
}
}
*status = dirconf->redirect_status; return found;
}
return NULL;
}
staticchar *try_alias_list(request_rec *r, apr_array_header_t *aliases, int is_redir, int *status)
{
alias_entry *entries = (alias_entry *) aliases->elts;
ap_regmatch_t regm[AP_MAX_REG_MATCH]; char *found = NULL; int i;
for (i = 0; i < aliases->nelts; ++i) {
alias_entry *alias = &entries[i]; int l;
if (alias->regexp) { if (!ap_regexec(alias->regexp, r->uri, AP_MAX_REG_MATCH, regm, 0)) { if (alias->real) {
found = ap_pregsub(r->pool, alias->real, r->uri,
AP_MAX_REG_MATCH, regm); if (found) { if (is_redir) {
apr_uri_t uri;
apr_uri_parse(r->pool, found, &uri); /* Do not escape the query string or fragment. */
found = apr_uri_unparse(r->pool, &uri,
APR_URI_UNP_OMITQUERY);
found = ap_escape_uri(r->pool, found); if (uri.query) {
found = apr_pstrcat(r->pool, found, "?",
uri.query, NULL);
} if (uri.fragment) {
found = apr_pstrcat(r->pool, found, "#",
uri.fragment, NULL);
}
}
} else {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00672) "Regex substitution in '%s' failed. " "Replacement too long?", alias->real); return PREGSUB_ERROR;
}
} else { /* need something non-null */
found = "";
}
}
} else {
l = alias_matches(r->uri, alias->fake);
if (l > 0) {
ap_set_context_info(r, alias->fake, alias->real); if (is_redir) { char *escurl;
escurl = ap_os_escape_path(r->pool, r->uri + l, 1);
found = apr_pstrcat(r->pool, alias->real, escurl, NULL);
} else
found = apr_pstrcat(r->pool, alias->real, r->uri + l, NULL);
}
}
if (found) { if (alias->handler) { /* Set handler, and leave a note for mod_cgi */
r->handler = alias->handler;
apr_table_setn(r->notes, "alias-forced-type", r->handler);
} /* XXX This is as SLOW as can be, next step, we optimize * and merge to whatever part of the found path was already * canonicalized. After I finish eliminating os canonical. * Better fail test for ap_server_root_relative needed here.
*/ if (!is_redir) {
found = ap_server_root_relative(r->pool, found);
} if (found) {
*status = alias->redir_status;
} return found;
}
/* It may have changed since last time, so try again */
if ((ret = try_redirect(r, &status)) != NULL
|| (ret = try_alias_list(r, dirconf->redirects, 1, &status))
!= NULL) { if (ret == PREGSUB_ERROR) return HTTP_INTERNAL_SERVER_ERROR; if (ap_is_HTTP_REDIRECT(status)) { if (dirconf->allow_relative != ALIAS_FLAG_ON || ret[0] != '/') { if (ret[0] == '/') { char *orig_target = ret;
ret = ap_construct_url(r->pool, ret, r);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00675) "incomplete redirection target of '%s' for " "URI '%s' modified to '%s'",
orig_target, r->uri, ret);
} if (!ap_is_url(ret)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00676) "cannot redirect '%s' to '%s'; " "target is not a valid absoluteURI or abs_path",
r->uri, ret); return HTTP_INTERNAL_SERVER_ERROR;
}
} /* append requested query only, if the config didn't * supply its own.
*/ if (r->args && !ap_strchr(ret, '?')) {
ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL);
}
apr_table_setn(r->headers_out, "Location", ret);
} return status;
}
return DECLINED;
}
staticconst command_rec alias_cmds[] =
{
AP_INIT_TAKE12("Alias", add_alias, NULL, RSRC_CONF | ACCESS_CONF, "a fakename and a realname, or a realname in a Location"),
AP_INIT_TAKE12("ScriptAlias", add_alias, "cgi-script", RSRC_CONF | ACCESS_CONF, "a fakename and a realname, or a realname in a Location"),
AP_INIT_TAKE123("Redirect", add_redirect, (void *) HTTP_MOVED_TEMPORARILY,
OR_FILEINFO, "an optional status, then document to be redirected and " "destination URL"),
AP_INIT_TAKE2("AliasMatch", add_alias_regex, NULL, RSRC_CONF, "a regular expression and a filename"),
AP_INIT_TAKE2("ScriptAliasMatch", add_alias_regex, "cgi-script", RSRC_CONF, "a regular expression and a filename"),
AP_INIT_TAKE23("RedirectMatch", add_redirect_regex,
(void *) HTTP_MOVED_TEMPORARILY, OR_FILEINFO, "an optional status, then a regular expression and " "destination URL"),
AP_INIT_TAKE2("RedirectTemp", add_redirect2,
(void *) HTTP_MOVED_TEMPORARILY, OR_FILEINFO, "a document to be redirected, then the destination URL"),
AP_INIT_TAKE2("RedirectPermanent", add_redirect2,
(void *) HTTP_MOVED_PERMANENTLY, OR_FILEINFO, "a document to be redirected, then the destination URL"),
AP_INIT_FLAG("RedirectRelative", ap_set_flag_slot,
(void*)APR_OFFSETOF(alias_dir_conf, allow_relative), OR_FILEINFO, "Set to ON to allow relative redirect targets to be issued as-is"),
AP_INIT_FLAG("AliasPreservePath", ap_set_flag_slot,
(void*)APR_OFFSETOF(alias_dir_conf, alias_preserve_path), OR_FILEINFO, "Set to ON to map the full path after the fakename to the realname."),
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.