mod_dir.c revision 5bfaaf573bacb45c1cf290ce85ecc676587e8a64
2d2eda71267231c2526be701fe655db125852c1ffielding/* Licensed to the Apache Software Foundation (ASF) under one or more
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * contributor license agreements. See the NOTICE file distributed with
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * this work for additional information regarding copyright ownership.
26a4456dd6f1a5d7d7fff766551461a578687c4and * The ASF licenses this file to You under the Apache License, Version 2.0
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * (the "License"); you may not use this file except in compliance with
2d2eda71267231c2526be701fe655db125852c1ffielding * the License. You may obtain a copy of the License at
2d2eda71267231c2526be701fe655db125852c1ffielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * http://www.apache.org/licenses/LICENSE-2.0
2d2eda71267231c2526be701fe655db125852c1ffielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * Unless required by applicable law or agreed to in writing, software
2d2eda71267231c2526be701fe655db125852c1ffielding * distributed under the License is distributed on an "AS IS" BASIS,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2d2eda71267231c2526be701fe655db125852c1ffielding * See the License for the specific language governing permissions and
2d2eda71267231c2526be701fe655db125852c1ffielding * limitations under the License.
2d2eda71267231c2526be701fe655db125852c1ffielding */
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding/*
2d2eda71267231c2526be701fe655db125852c1ffielding * mod_dir.c: handle default index files, and trailing-/ redirects
f062ed7bd262a37a909dd77ce5fc23b446818823fielding */
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "apr_strings.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "apr_lib.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "ap_config.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "httpd.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_config.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "http_core.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "http_request.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "http_protocol.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "http_log.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_main.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "util_script.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
64185f9824e42f21ca7b9ae6c004484215c031a7rbbmodule AP_MODULE_DECLARE_DATA dir_module;
2d2eda71267231c2526be701fe655db125852c1ffielding
f062ed7bd262a37a909dd77ce5fc23b446818823fieldingtypedef enum {
f062ed7bd262a37a909dd77ce5fc23b446818823fielding SLASH_OFF = 0,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding SLASH_ON,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding SLASH_UNSET
2d2eda71267231c2526be701fe655db125852c1ffielding} slash_cfg;
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#define REDIRECT_OFF 0
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#define REDIRECT_UNSET 1
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fieldingtypedef struct dir_config_struct {
f062ed7bd262a37a909dd77ce5fc23b446818823fielding apr_array_header_t *index_names;
f062ed7bd262a37a909dd77ce5fc23b446818823fielding slash_cfg do_slash;
2d2eda71267231c2526be701fe655db125852c1ffielding int redirect_index;
2d2eda71267231c2526be701fe655db125852c1ffielding const char *dflt;
2d2eda71267231c2526be701fe655db125852c1ffielding} dir_config_rec;
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#define DIR_CMD_PERMS OR_INDEXES
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic const char *add_index(cmd_parms *cmd, void *dummy, const char *arg)
f062ed7bd262a37a909dd77ce5fc23b446818823fielding{
f062ed7bd262a37a909dd77ce5fc23b446818823fielding dir_config_rec *d = dummy;
f062ed7bd262a37a909dd77ce5fc23b446818823fielding const char *t, *w;
2d2eda71267231c2526be701fe655db125852c1ffielding int count = 0;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (!d->index_names) {
2d2eda71267231c2526be701fe655db125852c1ffielding d->index_names = apr_array_make(cmd->pool, 2, sizeof(char *));
2d2eda71267231c2526be701fe655db125852c1ffielding }
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben
0432a26b69eedfb9ca5f34fba590236378a24851ben t = arg;
0432a26b69eedfb9ca5f34fba590236378a24851ben while ((w = ap_getword_conf(cmd->pool, &t)) && w[0]) {
2d2eda71267231c2526be701fe655db125852c1ffielding if (count == 0 && !strcasecmp(w, "disabled")) {
2d2eda71267231c2526be701fe655db125852c1ffielding /* peek to see if "disabled" is first in a series of arguments */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb const char *tt = t;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb const char *ww = ap_getword_conf(cmd->temp_pool, &tt);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (ww == NULL || !ww[0]) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* "disabled" is first, and alone */
2d2eda71267231c2526be701fe655db125852c1ffielding break;
30c289e6bc6d28d210b21edd800ab2cfc78a8381wrowe }
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein }
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein *(const char **)apr_array_push(d->index_names) = w;
cd39d2139743ca0ef899953c6496dcf99e9c791atrawick count++;
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein }
b627048681b27fe30f979ba471b523be3a6a22adrbb
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein return NULL;
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein}
44c46ef733836b32585d135d2d90856e7cfd9929rbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic const char *configure_slash(cmd_parms *cmd, void *d_, int arg)
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb dir_config_rec *d = d_;
9cfc48b742c224c1fbc2c26a4119a3266192c7d2wrowe
9cfc48b742c224c1fbc2c26a4119a3266192c7d2wrowe d->do_slash = arg ? SLASH_ON : SLASH_OFF;
7c7372abe2484e7fcf81937b93496d1246e5b816gstein return NULL;
7c7372abe2484e7fcf81937b93496d1246e5b816gstein}
7c7372abe2484e7fcf81937b93496d1246e5b816gsteinstatic const char *configure_redirect(cmd_parms *cmd, void *d_, const char *arg1)
7c7372abe2484e7fcf81937b93496d1246e5b816gstein{
7c7372abe2484e7fcf81937b93496d1246e5b816gstein dir_config_rec *d = d_;
7c7372abe2484e7fcf81937b93496d1246e5b816gstein int status;
7c7372abe2484e7fcf81937b93496d1246e5b816gstein
7c7372abe2484e7fcf81937b93496d1246e5b816gstein if (!strcasecmp(arg1, "ON"))
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb status = HTTP_MOVED_TEMPORARILY;
2d2eda71267231c2526be701fe655db125852c1ffielding else if (!strcasecmp(arg1, "OFF"))
2d2eda71267231c2526be701fe655db125852c1ffielding status = REDIRECT_OFF;
2d2eda71267231c2526be701fe655db125852c1ffielding else if (!strcasecmp(arg1, "permanent"))
2d2eda71267231c2526be701fe655db125852c1ffielding status = HTTP_MOVED_PERMANENTLY;
2d2eda71267231c2526be701fe655db125852c1ffielding else if (!strcasecmp(arg1, "temp"))
2d2eda71267231c2526be701fe655db125852c1ffielding status = HTTP_MOVED_TEMPORARILY;
2d2eda71267231c2526be701fe655db125852c1ffielding else if (!strcasecmp(arg1, "seeother"))
2d2eda71267231c2526be701fe655db125852c1ffielding status = HTTP_SEE_OTHER;
2d2eda71267231c2526be701fe655db125852c1ffielding else if (apr_isdigit(*arg1)) {
2d2eda71267231c2526be701fe655db125852c1ffielding status = atoi(arg1);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!ap_is_HTTP_REDIRECT(status)) {
2d2eda71267231c2526be701fe655db125852c1ffielding return "DirectoryIndexRedirect only accepts values between 300 and 399";
2d2eda71267231c2526be701fe655db125852c1ffielding }
2e123e8beedc9f921448c113e2d6823a92fd5261fielding }
2e123e8beedc9f921448c113e2d6823a92fd5261fielding else {
2d2eda71267231c2526be701fe655db125852c1ffielding return "DirectoryIndexRedirect ON|OFF|permanent|temp|seeother|3xx";
dd5cbadf2df719db2f3c769d03ec847da25854e6bnicholes }
dd5cbadf2df719db2f3c769d03ec847da25854e6bnicholes
dd5cbadf2df719db2f3c769d03ec847da25854e6bnicholes d->redirect_index = status;
2d2eda71267231c2526be701fe655db125852c1ffielding return NULL;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic const command_rec dir_cmds[] =
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding AP_INIT_TAKE1("FallbackResource", ap_set_string_slot,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb (void*)APR_OFFSETOF(dir_config_rec, dflt),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb DIR_CMD_PERMS, "Set a default handler"),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb AP_INIT_RAW_ARGS("DirectoryIndex", add_index, NULL, DIR_CMD_PERMS,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb "a list of file names"),
72a4ef8eac1adef882246c5bfb9b8bbd82d613c4coar AP_INIT_FLAG("DirectorySlash", configure_slash, NULL, DIR_CMD_PERMS,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb "On or Off"),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb AP_INIT_TAKE1("DirectoryIndexRedirect", configure_redirect,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb NULL, DIR_CMD_PERMS, "On, Off, or a 3xx status code."),
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding {NULL}
2d2eda71267231c2526be701fe655db125852c1ffielding};
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void *create_dir_config(apr_pool_t *p, char *dummy)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding dir_config_rec *new = apr_pcalloc(p, sizeof(dir_config_rec));
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding new->index_names = NULL;
2d2eda71267231c2526be701fe655db125852c1ffielding new->do_slash = SLASH_UNSET;
2d2eda71267231c2526be701fe655db125852c1ffielding new->redirect_index = REDIRECT_UNSET;
2d2eda71267231c2526be701fe655db125852c1ffielding return (void *) new;
57edbe3cb9356a0b599c7b07f3aae0e721ee57e2coar}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void *merge_dir_configs(apr_pool_t *p, void *basev, void *addv)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding dir_config_rec *new = apr_pcalloc(p, sizeof(dir_config_rec));
2d2eda71267231c2526be701fe655db125852c1ffielding dir_config_rec *base = (dir_config_rec *)basev;
2d2eda71267231c2526be701fe655db125852c1ffielding dir_config_rec *add = (dir_config_rec *)addv;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding new->index_names = add->index_names ? add->index_names : base->index_names;
2d2eda71267231c2526be701fe655db125852c1ffielding new->do_slash =
2d2eda71267231c2526be701fe655db125852c1ffielding (add->do_slash == SLASH_UNSET) ? base->do_slash : add->do_slash;
2d2eda71267231c2526be701fe655db125852c1ffielding new->redirect_index=
2d2eda71267231c2526be701fe655db125852c1ffielding (add->redirect_index == REDIRECT_UNSET) ? base->redirect_index : add->redirect_index;
2d2eda71267231c2526be701fe655db125852c1ffielding new->dflt = add->dflt ? add->dflt : base->dflt;
2d2eda71267231c2526be701fe655db125852c1ffielding return new;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic int fixup_dflt(request_rec *r)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding dir_config_rec *d = ap_get_module_config(r->per_dir_config, &dir_module);
2d2eda71267231c2526be701fe655db125852c1ffielding const char *name_ptr;
2d2eda71267231c2526be701fe655db125852c1ffielding request_rec *rr;
2d2eda71267231c2526be701fe655db125852c1ffielding int error_notfound = 0;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding name_ptr = d->dflt;
2d2eda71267231c2526be701fe655db125852c1ffielding if (name_ptr == NULL) {
2d2eda71267231c2526be701fe655db125852c1ffielding return DECLINED;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding /* XXX: if FallbackResource points to something that doesn't exist,
2d2eda71267231c2526be701fe655db125852c1ffielding * this may recurse until it hits the limit for internal redirects
2d2eda71267231c2526be701fe655db125852c1ffielding * before returning an Internal Server Error.
2d2eda71267231c2526be701fe655db125852c1ffielding */
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* The logic of this function is basically cloned and simplified
2d2eda71267231c2526be701fe655db125852c1ffielding * from fixup_dir below. See the comments there.
2d2eda71267231c2526be701fe655db125852c1ffielding */
2d2eda71267231c2526be701fe655db125852c1ffielding if (r->args != NULL) {
2d2eda71267231c2526be701fe655db125852c1ffielding name_ptr = apr_pstrcat(r->pool, name_ptr, "?", r->args, NULL);
2d2eda71267231c2526be701fe655db125852c1ffielding }
dbbf1b4183ae16353011a5269b37899f02b97d81gregames rr = ap_sub_req_lookup_uri(name_ptr, r, r->output_filters);
2d2eda71267231c2526be701fe655db125852c1ffielding if (rr->status == HTTP_OK
2d2eda71267231c2526be701fe655db125852c1ffielding && ( (rr->handler && !strcmp(rr->handler, "proxy-server"))
2d2eda71267231c2526be701fe655db125852c1ffielding || rr->finfo.filetype == APR_REG)) {
2d2eda71267231c2526be701fe655db125852c1ffielding ap_internal_fast_redirect(rr, r);
e44e11f9fece12c783f18d033923bfc0d6b4289aake return OK;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding else if (ap_is_HTTP_REDIRECT(rr->status)) {
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding apr_pool_join(r->pool, rr->pool);
e44e11f9fece12c783f18d033923bfc0d6b4289aake r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
2d2eda71267231c2526be701fe655db125852c1ffielding r->headers_out = apr_table_overlay(r->pool, r->headers_out,
2d2eda71267231c2526be701fe655db125852c1ffielding rr->headers_out);
2d2eda71267231c2526be701fe655db125852c1ffielding r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
2d2eda71267231c2526be701fe655db125852c1ffielding rr->err_headers_out);
2d2eda71267231c2526be701fe655db125852c1ffielding error_notfound = rr->status;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding else if (rr->status && rr->status != HTTP_NOT_FOUND
2d2eda71267231c2526be701fe655db125852c1ffielding && rr->status != HTTP_OK) {
2d2eda71267231c2526be701fe655db125852c1ffielding error_notfound = rr->status;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding ap_destroy_sub_req(rr);
2d2eda71267231c2526be701fe655db125852c1ffielding if (error_notfound) {
2d2eda71267231c2526be701fe655db125852c1ffielding return error_notfound;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* nothing for us to do, pass on through */
2d2eda71267231c2526be701fe655db125852c1ffielding return DECLINED;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic int fixup_dir(request_rec *r)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding dir_config_rec *d;
2d2eda71267231c2526be701fe655db125852c1ffielding char *dummy_ptr[1];
2d2eda71267231c2526be701fe655db125852c1ffielding char **names_ptr;
2d2eda71267231c2526be701fe655db125852c1ffielding int num_names;
2d2eda71267231c2526be701fe655db125852c1ffielding int error_notfound = 0;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* In case mod_mime wasn't present, and no handler was assigned. */
2d2eda71267231c2526be701fe655db125852c1ffielding if (!r->handler) {
2d2eda71267231c2526be701fe655db125852c1ffielding r->handler = DIR_MAGIC_TYPE;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet /* Never tolerate path_info on dir requests */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh if (r->path_info && *r->path_info) {
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet return DECLINED;
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet }
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet d = (dir_config_rec *)ap_get_module_config(r->per_dir_config,
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet &dir_module);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* Redirect requests that are not '/' terminated */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh if (r->uri[0] == '\0' || r->uri[strlen(r->uri) - 1] != '/')
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb char *ifile;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (!d->do_slash) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return DECLINED;
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* Only redirect non-get requests if we have no note to warn
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * that this browser cannot handle redirs on non-GET requests
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * (such as Microsoft's WebFolders).
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh if ((r->method_number != M_GET)
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh && apr_table_get(r->subprocess_env, "redirect-carefully")) {
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh return DECLINED;
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh }
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (r->args != NULL) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb "/", "?", r->args, NULL);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh else {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb "/", NULL);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb apr_table_setn(r->headers_out, "Location",
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ap_construct_url(r->pool, ifile, r));
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return HTTP_MOVED_PERMANENTLY;
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (d->index_names) {
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh names_ptr = (char **)d->index_names->elts;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb num_names = d->index_names->nelts;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh else {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb dummy_ptr[0] = AP_DEFAULT_INDEX;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb names_ptr = dummy_ptr;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb num_names = 1;
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb for (; num_names; ++names_ptr, --num_names) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* XXX: Is this name_ptr considered escaped yet, or not??? */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh char *name_ptr = *names_ptr;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb request_rec *rr;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* Once upon a time args were handled _after_ the successful redirect.
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * But that redirect might then _refuse_ the given r->args, creating
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * a nasty tangle. It seems safer to consider the r->args while we
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * determine if name_ptr is our viable index, and therefore set them
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * up correctly on redirect.
99d6d3207e24ba1f8eba77ef903948d738886cf5nd */
99d6d3207e24ba1f8eba77ef903948d738886cf5nd if (r->args != NULL) {
99d6d3207e24ba1f8eba77ef903948d738886cf5nd name_ptr = apr_pstrcat(r->pool, name_ptr, "?", r->args, NULL);
99d6d3207e24ba1f8eba77ef903948d738886cf5nd }
99d6d3207e24ba1f8eba77ef903948d738886cf5nd
99d6d3207e24ba1f8eba77ef903948d738886cf5nd rr = ap_sub_req_lookup_uri(name_ptr, r, r->output_filters);
99d6d3207e24ba1f8eba77ef903948d738886cf5nd
99d6d3207e24ba1f8eba77ef903948d738886cf5nd /* The sub request lookup is very liberal, and the core map_to_storage
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * handler will almost always result in HTTP_OK as /foo/index.html
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * may be /foo with PATH_INFO="/index.html", or even / with
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * PATH_INFO="/foo/index.html". To get around this we insist that the
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * the index be a regular filetype.
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh *
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * Another reason is that the core handler also makes the assumption
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * that if r->finfo is still NULL by the time it gets called, the
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh * file does not exist.
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh if (rr->status == HTTP_OK
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb && ( (rr->handler && !strcmp(rr->handler, "proxy-server"))
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb || rr->finfo.filetype == APR_REG)) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (ap_is_HTTP_REDIRECT(d->redirect_index)) {
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh apr_table_setn(r->headers_out, "Location", ap_construct_url(r->pool, rr->uri, r));
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return d->redirect_index;
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben ap_internal_fast_redirect(rr, r);
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben return OK;
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben }
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben /* If the request returned a redirect, propagate it to the client */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (ap_is_HTTP_REDIRECT(rr->status)
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben || (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben || (rr->status == HTTP_UNAUTHORIZED && num_names == 1)) {
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben apr_pool_join(r->pool, rr->pool);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb error_notfound = rr->status;
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben r->headers_out = apr_table_overlay(r->pool, r->headers_out,
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben rr->headers_out);
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben rr->err_headers_out);
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben return error_notfound;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* If the request returned something other than 404 (or 200),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * it means the module encountered some sort of problem. To be
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * secure, we should return the error, rather than allow autoindex
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh * to create a (possibly unsafe) directory index.
1374444b4fab1475091e12a81663f379b73005efrbb *
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * So we store the error, and if none of the listed files
bbe046d7cbb950ab3e372e4119ae001a5fe52ed4striker * exist, we return the last error response we got, instead
bbe046d7cbb950ab3e372e4119ae001a5fe52ed4striker * of a directory listing.
bbe046d7cbb950ab3e372e4119ae001a5fe52ed4striker */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh if (rr->status && rr->status != HTTP_NOT_FOUND
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh && rr->status != HTTP_OK) {
c8aa00ee0cfc17b5fa08fb8a2b08d30dc9e4f1b1wrowe error_notfound = rr->status;
c8aa00ee0cfc17b5fa08fb8a2b08d30dc9e4f1b1wrowe }
4542d057c4bde23857f31d449fa3108561206139wrowe
c8aa00ee0cfc17b5fa08fb8a2b08d30dc9e4f1b1wrowe ap_destroy_sub_req(rr);
4542d057c4bde23857f31d449fa3108561206139wrowe }
c8aa00ee0cfc17b5fa08fb8a2b08d30dc9e4f1b1wrowe
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh if (error_notfound) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return error_notfound;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* record what we tried, mostly for the benefit of mod_autoindex */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh apr_table_set(r->notes, "dir-index-names",
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb d->index_names ?
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh apr_array_pstrcat(r->pool, d->index_names, ','):
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb AP_DEFAULT_INDEX);
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* nothing for us to do, pass on through */
6d74c65f131583f6832b04424c1a9e1e7cc72c8egregames return DECLINED;
6d74c65f131583f6832b04424c1a9e1e7cc72c8egregames}
6d74c65f131583f6832b04424c1a9e1e7cc72c8egregames
6d74c65f131583f6832b04424c1a9e1e7cc72c8egregamesstatic int dir_fixups(request_rec *r)
6d74c65f131583f6832b04424c1a9e1e7cc72c8egregames{
6d74c65f131583f6832b04424c1a9e1e7cc72c8egregames if (r->finfo.filetype == APR_DIR) {
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh /* serve up a directory */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh return fixup_dir(r);
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh }
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh else if ((r->finfo.filetype == APR_NOFILE) && (r->handler == NULL)) {
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh /* No handler and nothing in the filesystem - use fallback */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return fixup_dflt(r);
2e123e8beedc9f921448c113e2d6823a92fd5261fielding }
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben return DECLINED;
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben}
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben
759f4a24d09e28c4eaca9f97311b497fc15cb5c7benstatic void register_hooks(apr_pool_t *p)
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben{
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben ap_hook_fixups(dir_fixups,NULL,NULL,APR_HOOK_LAST);
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben}
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben
759f4a24d09e28c4eaca9f97311b497fc15cb5c7benAP_DECLARE_MODULE(dir) = {
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben STANDARD20_MODULE_STUFF,
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben create_dir_config, /* create per-directory config structure */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben merge_dir_configs, /* merge per-directory config structures */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben NULL, /* create per-server config structure */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben NULL, /* merge per-server config structures */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben dir_cmds, /* command apr_table_t */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben register_hooks /* register hooks */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben};
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben