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
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * http://www.apache.org/licenses/LICENSE-2.0
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
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 */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/*
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * http_alias.c: Stuff for dealing with directory aliases
e8f95a682820a599fe41b22977010636be5c2717jim *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Original by Rob McCool, rewritten in succession by David Robinson
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * and rst.
e8f95a682820a599fe41b22977010636be5c2717jim *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
b6055b7832a0e4d0818416252fff5925aaebae4brbb#include "apr_strings.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "apr_lib.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#define APR_WANT_STRFUNC
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "apr_want.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
2d71630471d1c23f0137309e3c3957c633ecbfd6rbb#include "ap_config.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "httpd.h"
449efc4dc68e42cc4421d15498a689618aab5dc3coar#include "http_core.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "http_config.h"
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding#include "http_request.h"
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar#include "http_log.h"
291eb44b3adaf8247425286615b4f4b69fbea274minfrin#include "ap_expr.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben const char *real;
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben const char *fake;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *handler;
ef5650b61a8e35f3cc93ec07e73efc17ea329894jorton ap_regex_t *regexp;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz int redir_status; /* 301, 302, 303, 410, etc */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding} alias_entry;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_array_header_t *aliases;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_array_header_t *redirects;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding} alias_server_conf;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
61c688e070e72a669cb2426f85960b9e9a1f9a03minfrin unsigned int alias_set:1;
61c688e070e72a669cb2426f85960b9e9a1f9a03minfrin unsigned int redirect_set:1;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_array_header_t *redirects;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const ap_expr_info_t *alias;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin char *handler;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const ap_expr_info_t *redirect;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin int redirect_status; /* 301, 302, 303, 410, etc */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding} alias_dir_conf;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
3d96ee83babeec32482c9082c9426340cee8c44dwrowemodule AP_MODULE_DECLARE_DATA alias_module;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic char magic_error_value;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin#define PREGSUB_ERROR (&magic_error_value)
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *create_alias_config(apr_pool_t *p, server_rec *s)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding alias_server_conf *a =
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (alias_server_conf *) apr_pcalloc(p, sizeof(alias_server_conf));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
066877f1a045103acfdd376d48cdd473c33f409bdougm a->aliases = apr_array_make(p, 20, sizeof(alias_entry));
066877f1a045103acfdd376d48cdd473c33f409bdougm a->redirects = apr_array_make(p, 20, sizeof(alias_entry));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return a;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *create_alias_dir_config(apr_pool_t *p, char *d)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding alias_dir_conf *a =
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
066877f1a045103acfdd376d48cdd473c33f409bdougm a->redirects = apr_array_make(p, 2, sizeof(alias_entry));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return a;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *merge_alias_config(apr_pool_t *p, void *basev, void *overridesv)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding alias_server_conf *a =
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;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
066877f1a045103acfdd376d48cdd473c33f409bdougm a->aliases = apr_array_append(p, overrides->aliases, base->aliases);
066877f1a045103acfdd376d48cdd473c33f409bdougm a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return a;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *merge_alias_dir_config(apr_pool_t *p, void *basev, void *overridesv)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding alias_dir_conf *a =
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;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
066877f1a045103acfdd376d48cdd473c33f409bdougm a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin a->alias = (overrides->alias_set == 0) ? base->alias : overrides->alias;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin a->handler = (overrides->alias_set == 0) ? base->handler : overrides->handler;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin a->alias_set = overrides->alias_set || base->alias_set;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin a->redirect = (overrides->redirect_set == 0) ? base->redirect : overrides->redirect;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin a->redirect_status = (overrides->redirect_set == 0) ? base->redirect_status : overrides->redirect_status;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin a->redirect_set = overrides->redirect_set || base->redirect_set;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return a;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
4d7e28c869788fb00bffda29a67f1b10e19f159dnd/* need prototype for overlap check */
4d7e28c869788fb00bffda29a67f1b10e19f159dndstatic int alias_matches(const char *uri, const char *alias_fakename);
4d7e28c869788fb00bffda29a67f1b10e19f159dnd
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_alias_internal(cmd_parms *cmd, void *dummy,
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf const char *fake, const char *real,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz int use_regex)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding server_rec *s = cmd->server;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein alias_server_conf *conf = ap_get_module_config(s->module_config,
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein &alias_module);
066877f1a045103acfdd376d48cdd473c33f409bdougm alias_entry *new = apr_array_push(conf->aliases);
4d7e28c869788fb00bffda29a67f1b10e19f159dnd alias_entry *entries = (alias_entry *)conf->aliases->elts;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd int i;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
18413e519b6e5d05ff00d18efd784307442ca2f3igalic /* XXX: real can NOT be relative to DocumentRoot here... compat bug. */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (err != NULL) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return err;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (use_regex) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf new->regexp = ap_pregcomp(cmd->pool, fake, AP_REG_EXTENDED);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (new->regexp == NULL)
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return "Regular expression could not be compiled.";
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf new->real = real;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe else {
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.
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe */
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf new->real = real;
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe }
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf new->fake = fake;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding new->handler = cmd->info;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
4d7e28c869788fb00bffda29a67f1b10e19f159dnd /* check for overlapping (Script)Alias directives
4d7e28c869788fb00bffda29a67f1b10e19f159dnd * and throw a warning if found one
4d7e28c869788fb00bffda29a67f1b10e19f159dnd */
4d7e28c869788fb00bffda29a67f1b10e19f159dnd if (!use_regex) {
4d7e28c869788fb00bffda29a67f1b10e19f159dnd for (i = 0; i < conf->aliases->nelts - 1; ++i) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf alias_entry *alias = &entries[i];
4d7e28c869788fb00bffda29a67f1b10e19f159dnd
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if ( (!alias->regexp && alias_matches(fake, alias->fake) > 0)
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf || (alias->regexp && !ap_regexec(alias->regexp, fake, 0, NULL, 0))) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00671)
1f0ca94141196628ecadf1a91f2b60a33349872fnd "The %s directive in %s at line %d will probably "
1f0ca94141196628ecadf1a91f2b60a33349872fnd "never match because it overlaps an earlier "
1f0ca94141196628ecadf1a91f2b60a33349872fnd "%sAlias%s.",
1f0ca94141196628ecadf1a91f2b60a33349872fnd cmd->cmd->name, cmd->directive->filename,
1f0ca94141196628ecadf1a91f2b60a33349872fnd cmd->directive->line_num,
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf alias->handler ? "Script" : "",
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf alias->regexp ? "Match" : "");
4d7e28c869788fb00bffda29a67f1b10e19f159dnd
4d7e28c869788fb00bffda29a67f1b10e19f159dnd break; /* one warning per alias should be sufficient */
4d7e28c869788fb00bffda29a67f1b10e19f159dnd }
4d7e28c869788fb00bffda29a67f1b10e19f159dnd }
4d7e28c869788fb00bffda29a67f1b10e19f159dnd }
4d7e28c869788fb00bffda29a67f1b10e19f159dnd
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
36d38d22e0d385db01f5773a579f44b8f02e4b1fsfstatic const char *add_alias(cmd_parms *cmd, void *dummy, const char *fake,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char *real)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (real) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return add_alias_internal(cmd, dummy, fake, real, 0);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin else {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin alias_dir_conf *dirconf = (alias_dir_conf *) dummy;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (err != NULL) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return err;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!cmd->path) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return "Alias must have two arguments when used globally";
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->alias =
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_expr_parse_cmd(cmd, fake, AP_EXPR_FLAG_STRING_RESULT,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin &err, NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (err) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return apr_pstrcat(cmd->temp_pool,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "Cannot parse alias expression '", fake, "': ", err,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->handler = cmd->info;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->alias_set = 1;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
36d38d22e0d385db01f5773a579f44b8f02e4b1fsfstatic const char *add_alias_regex(cmd_parms *cmd, void *dummy,
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf const char *fake, const char *real)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf return add_alias_internal(cmd, dummy, fake, real, 1);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_redirect_internal(cmd_parms *cmd,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz alias_dir_conf *dirconf,
e8f95a682820a599fe41b22977010636be5c2717jim const char *arg1, const char *arg2,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick const char *arg3, int use_regex)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding alias_entry *new;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding server_rec *s = cmd->server;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein alias_server_conf *serverconf = ap_get_module_config(s->module_config,
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein &alias_module);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int status = (int) (long) cmd->info;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim int grokarg1 = 1;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf ap_regex_t *regex = NULL;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf const char *fake = arg2;
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben const char *url = arg3;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim /*
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * Logic flow:
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * Go ahead and try to grok the 1st arg, in case it is a
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * Redirect status. Now if we have 3 args, we expect that
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * we were able to understand that 1st argument (it's something
afadb90f859c4987505e745edee656850d7f8d49jim * we expected, so if not, then we bail
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim */
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim if (!strcasecmp(arg1, "permanent"))
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim status = HTTP_MOVED_PERMANENTLY;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim else if (!strcasecmp(arg1, "temp"))
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim status = HTTP_MOVED_TEMPORARILY;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim else if (!strcasecmp(arg1, "seeother"))
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim status = HTTP_SEE_OTHER;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim else if (!strcasecmp(arg1, "gone"))
172e83c0f024fe6396dd1f3ca3492fd83c304db5jim status = HTTP_GONE;
66a73d4405f9d941672c0343b36f6c494413a6b5rpluem else if (apr_isdigit(*arg1))
66a73d4405f9d941672c0343b36f6c494413a6b5rpluem status = atoi(arg1);
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim else
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim grokarg1 = 0;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim if (arg3 && !grokarg1)
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim return "Redirect: invalid first argument (of three)";
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /*
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * if we have the 2nd arg and we understand the 1st one, or if we have the
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * 1st arg but don't understand it, we use the expression syntax assuming
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * a path from the location.
291eb44b3adaf8247425286615b4f4b69fbea274minfrin *
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * if we understand the first arg but have no second arg, we are dealing
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * with a status like "GONE".
291eb44b3adaf8247425286615b4f4b69fbea274minfrin */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (grokarg1 && arg2 && !arg3 && HTTP_GONE != status) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char *expr_err = NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->redirect =
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_expr_parse_cmd(cmd, arg2, AP_EXPR_FLAG_STRING_RESULT,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin &expr_err, NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (expr_err) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return apr_pstrcat(cmd->temp_pool,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "Cannot parse redirect expression '", arg2, "': ", expr_err,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->redirect_status = status;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->redirect_set = 1;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin else if (grokarg1 && !arg2 && HTTP_GONE == status) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->redirect_status = status;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->redirect_set = 1;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin else if (!grokarg1 && !arg2) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char *expr_err = NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->redirect =
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_expr_parse_cmd(cmd, arg1, AP_EXPR_FLAG_STRING_RESULT,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin &expr_err, NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (expr_err) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return apr_pstrcat(cmd->temp_pool,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "Cannot parse redirect expression '", arg1, "': ", expr_err,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->redirect_status = status;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin dirconf->redirect_set = 1;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim /*
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * if we don't have the 3rd arg and we didn't understand the 1st
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * one, then assume URL-path URL. This also handles case, eg, GONE
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * we even though we don't have a 3rd arg, we did understand the 1st
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim * one, so we don't want to re-arrange
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim */
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim if (!arg3 && !grokarg1) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf fake = arg1;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz url = arg2;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (use_regex) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf regex = ap_pregcomp(cmd->pool, fake, AP_REG_EXTENDED);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (regex == NULL)
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return "Regular expression could not be compiled.";
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ap_is_HTTP_REDIRECT(status)) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (!url)
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.
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq */
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq if (!use_regex && !ap_is_url(url) && (url[0] != '/'))
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return "Redirect to non-URL";
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (url)
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return "Redirect URL not valid for this status";
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (cmd->path)
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz new = apr_array_push(dirconf->redirects);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz new = apr_array_push(serverconf->redirects);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf new->fake = fake;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding new->real = url;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf new->regexp = regex;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding new->redir_status = status;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_redirect(cmd_parms *cmd, void *dirconf,
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben const char *arg1, const char *arg2,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *arg3)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 0);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_redirect2(cmd_parms *cmd, void *dirconf,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *arg1, const char *arg2)
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben{
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben return add_redirect_internal(cmd, dirconf, arg1, arg2, NULL, 0);
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben}
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben
a7ad08f37d876bde1a32f0cf793f4799536ab1a5benstatic const char *add_redirect_regex(cmd_parms *cmd, void *dirconf,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *arg1, const char *arg2,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz const char *arg3)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 1);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const command_rec alias_cmds[] =
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin AP_INIT_TAKE12("Alias", add_alias, NULL, RSRC_CONF | ACCESS_CONF,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "a fakename and a realname, or a realname in a Location"),
291eb44b3adaf8247425286615b4f4b69fbea274minfrin AP_INIT_TAKE12("ScriptAlias", add_alias, "cgi-script", RSRC_CONF | ACCESS_CONF,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "a fakename and a realname, or a realname in a Location"),
291eb44b3adaf8247425286615b4f4b69fbea274minfrin AP_INIT_TAKE123("Redirect", add_redirect, (void *) HTTP_MOVED_TEMPORARILY,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick OR_FILEINFO,
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"),
e8f95a682820a599fe41b22977010636be5c2717jim AP_INIT_TAKE23("RedirectMatch", add_redirect_regex,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick (void *) HTTP_MOVED_TEMPORARILY, OR_FILEINFO,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "an optional status, then a regular expression and "
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "destination URL"),
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben AP_INIT_TAKE2("RedirectTemp", add_redirect2,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz (void *) HTTP_MOVED_TEMPORARILY, OR_FILEINFO,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "a document to be redirected, then the destination URL"),
e8f95a682820a599fe41b22977010636be5c2717jim AP_INIT_TAKE2("RedirectPermanent", add_redirect2,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick (void *) HTTP_MOVED_PERMANENTLY, OR_FILEINFO,
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick "a document to be redirected, then the destination URL"),
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {NULL}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding};
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic int alias_matches(const char *uri, const char *alias_fakename)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *aliasp = alias_fakename, *urip = uri;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
418c12fdca30a401f7921cf66d82f7061dc67149brianp while (*aliasp) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (*aliasp == '/') {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* any number of '/' in the alias matches any number in
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz * the supplied URI, but there must be at least one...
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (*urip != '/')
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return 0;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
fd7cb2b590294250e5b219512992cd5747289fbbbrianp do {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ++aliasp;
fd7cb2b590294250e5b219512992cd5747289fbbbrianp } while (*aliasp == '/');
fd7cb2b590294250e5b219512992cd5747289fbbbrianp do {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ++urip;
fd7cb2b590294250e5b219512992cd5747289fbbbrianp } while (*urip == '/');
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz else {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* Other characters are compared literally */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (*urip++ != *aliasp++)
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return 0;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Check last alias path component matched all the way */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return 0;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
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)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return urip - uri;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic char *try_alias(request_rec *r)
291eb44b3adaf8247425286615b4f4b69fbea274minfrin{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin alias_dir_conf *dirconf =
291eb44b3adaf8247425286615b4f4b69fbea274minfrin (alias_dir_conf *) ap_get_module_config(r->per_dir_config, &alias_module);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (dirconf->alias) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char *err = NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin char *found = apr_pstrdup(r->pool,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_expr_str_exec(r, dirconf->alias, &err));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (err) {
337489fc0f19fa2ccac530ca0e2de9fe60f28e09ylavic ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02825)
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "Can't evaluate alias expression: %s", err);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return PREGSUB_ERROR;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (dirconf->handler) { /* Set handler, and leave a note for mod_cgi */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin r->handler = dirconf->handler;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_table_setn(r->notes, "alias-forced-type", r->handler);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* XXX This is as SLOW as can be, next step, we optimize
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * and merge to whatever part of the found path was already
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * canonicalized. After I finish eliminating os canonical.
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * Better fail test for ap_server_root_relative needed here.
291eb44b3adaf8247425286615b4f4b69fbea274minfrin */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin found = ap_server_root_relative(r->pool, found);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return found;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic char *try_redirect(request_rec *r, int *status)
291eb44b3adaf8247425286615b4f4b69fbea274minfrin{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin alias_dir_conf *dirconf =
291eb44b3adaf8247425286615b4f4b69fbea274minfrin (alias_dir_conf *) ap_get_module_config(r->per_dir_config, &alias_module);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (dirconf->redirect_set) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_uri_t uri;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char *err = NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin char *found = "";
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (dirconf->redirect) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin found = apr_pstrdup(r->pool,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ap_expr_str_exec(r, dirconf->redirect, &err));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (err) {
337489fc0f19fa2ccac530ca0e2de9fe60f28e09ylavic ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02826)
337489fc0f19fa2ccac530ca0e2de9fe60f28e09ylavic "Can't evaluate redirect expression: %s", err);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return PREGSUB_ERROR;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin apr_uri_parse(r->pool, found, &uri);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* Do not escape the query string or fragment. */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin found = apr_uri_unparse(r->pool, &uri, APR_URI_UNP_OMITQUERY);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin found = ap_escape_uri(r->pool, found);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (uri.query) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin found = apr_pstrcat(r->pool, found, "?", uri.query, NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (uri.fragment) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin found = apr_pstrcat(r->pool, found, "#", uri.fragment, NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin *status = dirconf->redirect_status;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return found;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
4c138e467a7d384582aa6f8c9809af589e572feesf
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantzstatic char *try_alias_list(request_rec *r, apr_array_header_t *aliases,
33510984c759eb3da154ceb0db9b75fa0031d3b4sf int is_redir, int *status)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding alias_entry *entries = (alias_entry *) aliases->elts;
ef5650b61a8e35f3cc93ec07e73efc17ea329894jorton ap_regmatch_t regm[AP_MAX_REG_MATCH];
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *found = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int i;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding for (i = 0; i < aliases->nelts; ++i) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf alias_entry *alias = &entries[i];
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz int l;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (alias->regexp) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (!ap_regexec(alias->regexp, r->uri, AP_MAX_REG_MATCH, regm, 0)) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (alias->real) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf found = ap_pregsub(r->pool, alias->real, r->uri,
bbe046d7cbb950ab3e372e4119ae001a5fe52ed4striker AP_MAX_REG_MATCH, regm);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (found) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (is_redir) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf apr_uri_t uri;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf apr_uri_parse(r->pool, found, &uri);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf /* Do not escape the query string or fragment. */
33510984c759eb3da154ceb0db9b75fa0031d3b4sf found = apr_uri_unparse(r->pool, &uri,
33510984c759eb3da154ceb0db9b75fa0031d3b4sf APR_URI_UNP_OMITQUERY);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf found = ap_escape_uri(r->pool, found);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (uri.query) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf found = apr_pstrcat(r->pool, found, "?",
33510984c759eb3da154ceb0db9b75fa0031d3b4sf uri.query, NULL);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf }
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (uri.fragment) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf found = apr_pstrcat(r->pool, found, "#",
33510984c759eb3da154ceb0db9b75fa0031d3b4sf uri.fragment, NULL);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf }
33510984c759eb3da154ceb0db9b75fa0031d3b4sf }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
4c138e467a7d384582aa6f8c9809af589e572feesf else {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00672)
4c138e467a7d384582aa6f8c9809af589e572feesf "Regex substitution in '%s' failed. "
4c138e467a7d384582aa6f8c9809af589e572feesf "Replacement too long?", alias->real);
4c138e467a7d384582aa6f8c9809af589e572feesf return PREGSUB_ERROR;
4c138e467a7d384582aa6f8c9809af589e572feesf }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz else {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* need something non-null */
1d9e72aaaebdda86c3afc856c35872edf19c69bbsf found = "";
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz else {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf l = alias_matches(r->uri, alias->fake);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (l > 0) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf ap_set_context_info(r, alias->fake, alias->real);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (is_redir) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz char *escurl;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz escurl = ap_os_escape_path(r->pool, r->uri + l, 1);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf found = apr_pstrcat(r->pool, alias->real, escurl, NULL);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz else
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf found = apr_pstrcat(r->pool, alias->real, r->uri + l, NULL);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (found) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (alias->handler) { /* Set handler, and leave a note for mod_cgi */
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf r->handler = alias->handler;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz apr_table_setn(r->notes, "alias-forced-type", r->handler);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
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.
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe */
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (!is_redir) {
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe found = ap_server_root_relative(r->pool, found);
32c4bc04f89b16521718145dc731f750144d7b38wrowe }
32c4bc04f89b16521718145dc731f750144d7b38wrowe if (found) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf *status = alias->redir_status;
32c4bc04f89b16521718145dc731f750144d7b38wrowe }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return found;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic int translate_alias_redir(request_rec *r)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein ap_conf_vector_t *sconf = r->server->module_config;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein alias_server_conf *serverconf = ap_get_module_config(sconf, &alias_module);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *ret;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int status;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (r->uri[0] != '/' && r->uri[0] != '\0') {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return DECLINED;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if ((ret = try_redirect(r, &status)) != NULL
291eb44b3adaf8247425286615b4f4b69fbea274minfrin || (ret = try_alias_list(r, serverconf->redirects, 1, &status))
291eb44b3adaf8247425286615b4f4b69fbea274minfrin != NULL) {
4c138e467a7d384582aa6f8c9809af589e572feesf if (ret == PREGSUB_ERROR)
4c138e467a7d384582aa6f8c9809af589e572feesf return HTTP_INTERNAL_SERVER_ERROR;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (ap_is_HTTP_REDIRECT(status)) {
30e3e760b737f13ce800fa02c5930ade7659ba66niq if (ret[0] == '/') {
30e3e760b737f13ce800fa02c5930ade7659ba66niq char *orig_target = ret;
30e3e760b737f13ce800fa02c5930ade7659ba66niq
30e3e760b737f13ce800fa02c5930ade7659ba66niq ret = ap_construct_url(r->pool, ret, r);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00673)
30e3e760b737f13ce800fa02c5930ade7659ba66niq "incomplete redirection target of '%s' for "
30e3e760b737f13ce800fa02c5930ade7659ba66niq "URI '%s' modified to '%s'",
30e3e760b737f13ce800fa02c5930ade7659ba66niq orig_target, r->uri, ret);
30e3e760b737f13ce800fa02c5930ade7659ba66niq }
30e3e760b737f13ce800fa02c5930ade7659ba66niq if (!ap_is_url(ret)) {
30e3e760b737f13ce800fa02c5930ade7659ba66niq status = HTTP_INTERNAL_SERVER_ERROR;
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00674)
30e3e760b737f13ce800fa02c5930ade7659ba66niq "cannot redirect '%s' to '%s'; "
30e3e760b737f13ce800fa02c5930ade7659ba66niq "target is not a valid absoluteURI or abs_path",
30e3e760b737f13ce800fa02c5930ade7659ba66niq r->uri, ret);
30e3e760b737f13ce800fa02c5930ade7659ba66niq }
30e3e760b737f13ce800fa02c5930ade7659ba66niq else {
30e3e760b737f13ce800fa02c5930ade7659ba66niq /* append requested query only, if the config didn't
30e3e760b737f13ce800fa02c5930ade7659ba66niq * supply its own.
30e3e760b737f13ce800fa02c5930ade7659ba66niq */
30e3e760b737f13ce800fa02c5930ade7659ba66niq if (r->args && !ap_strchr(ret, '?')) {
30e3e760b737f13ce800fa02c5930ade7659ba66niq ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL);
30e3e760b737f13ce800fa02c5930ade7659ba66niq }
30e3e760b737f13ce800fa02c5930ade7659ba66niq apr_table_setn(r->headers_out, "Location", ret);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return status;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if ((ret = try_alias(r)) != NULL
291eb44b3adaf8247425286615b4f4b69fbea274minfrin || (ret = try_alias_list(r, serverconf->aliases, 0, &status))
291eb44b3adaf8247425286615b4f4b69fbea274minfrin != NULL) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz r->filename = ret;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return OK;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return DECLINED;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic int fixup_redir(request_rec *r)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding void *dconf = r->per_dir_config;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding alias_dir_conf *dirconf =
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (alias_dir_conf *) ap_get_module_config(dconf, &alias_module);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *ret;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int status;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* It may have changed since last time, so try again */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if ((ret = try_redirect(r, &status)) != NULL
291eb44b3adaf8247425286615b4f4b69fbea274minfrin || (ret = try_alias_list(r, dirconf->redirects, 1, &status))
291eb44b3adaf8247425286615b4f4b69fbea274minfrin != NULL) {
4c138e467a7d384582aa6f8c9809af589e572feesf if (ret == PREGSUB_ERROR)
4c138e467a7d384582aa6f8c9809af589e572feesf return HTTP_INTERNAL_SERVER_ERROR;
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar if (ap_is_HTTP_REDIRECT(status)) {
449efc4dc68e42cc4421d15498a689618aab5dc3coar if (ret[0] == '/') {
449efc4dc68e42cc4421d15498a689618aab5dc3coar char *orig_target = ret;
449efc4dc68e42cc4421d15498a689618aab5dc3coar
449efc4dc68e42cc4421d15498a689618aab5dc3coar ret = ap_construct_url(r->pool, ret, r);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00675)
449efc4dc68e42cc4421d15498a689618aab5dc3coar "incomplete redirection target of '%s' for "
449efc4dc68e42cc4421d15498a689618aab5dc3coar "URI '%s' modified to '%s'",
449efc4dc68e42cc4421d15498a689618aab5dc3coar orig_target, r->uri, ret);
449efc4dc68e42cc4421d15498a689618aab5dc3coar }
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar if (!ap_is_url(ret)) {
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar status = HTTP_INTERNAL_SERVER_ERROR;
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00676)
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar "cannot redirect '%s' to '%s'; "
449efc4dc68e42cc4421d15498a689618aab5dc3coar "target is not a valid absoluteURI or abs_path",
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar r->uri, ret);
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar }
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar else {
affb82a2d7fc07c1a862d800ef47af966b898768nd /* append requested query only, if the config didn't
affb82a2d7fc07c1a862d800ef47af966b898768nd * supply its own.
affb82a2d7fc07c1a862d800ef47af966b898768nd */
affb82a2d7fc07c1a862d800ef47af966b898768nd if (r->args && !ap_strchr(ret, '?')) {
affb82a2d7fc07c1a862d800ef47af966b898768nd ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL);
affb82a2d7fc07c1a862d800ef47af966b898768nd }
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar apr_table_setn(r->headers_out, "Location", ret);
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar }
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return status;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return DECLINED;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowestatic void register_hooks(apr_pool_t *p)
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding{
e8c95302287c86cd1f984eeb25cf3bfa9e2d33bbslive static const char * const aszSucc[]={ "mod_userdir.c",
e8c95302287c86cd1f984eeb25cf3bfa9e2d33bbslive "mod_vhost_alias.c",NULL };
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding
1376737cb6afa24d3e12f3a223318fe1bd71bb1fslive ap_hook_translate_name(translate_alias_redir,NULL,aszSucc,APR_HOOK_MIDDLE);
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe ap_hook_fixups(fixup_redir,NULL,NULL,APR_HOOK_MIDDLE);
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding}
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asfAP_DECLARE_MODULE(alias) =
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding STANDARD20_MODULE_STUFF,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz create_alias_dir_config, /* dir config creater */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz merge_alias_dir_config, /* dir merger --- default is to override */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz create_alias_config, /* server config */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz merge_alias_config, /* merge server configs */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz alias_cmds, /* command apr_table_t */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz register_hooks /* register hooks */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding};