1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
* mod_dir.c: handle default index files, and trailing-/ redirects
21
#include "apr_strings.h"
22
#include "ap_config.h"
24
#include "http_config.h"
25
#include "http_core.h"
26
#include "http_request.h"
27
#include "http_protocol.h"
29
#include "http_main.h"
30
#include "util_script.h"
32
module AP_MODULE_DECLARE_DATA dir_module;
40
typedef struct dir_config_struct {
41
apr_array_header_t *index_names;
45
#define DIR_CMD_PERMS OR_INDEXES
47
static const char *add_index(cmd_parms *cmd, void *dummy, const char *arg)
49
dir_config_rec *d = dummy;
51
if (!d->index_names) {
52
d->index_names = apr_array_make(cmd->pool, 2, sizeof(char *));
54
*(const char **)apr_array_push(d->index_names) = arg;
58
static const char *configure_slash(cmd_parms *cmd, void *d_, int arg)
60
dir_config_rec *d = d_;
62
d->do_slash = arg ? SLASH_ON : SLASH_OFF;
66
static const command_rec dir_cmds[] =
68
AP_INIT_ITERATE("DirectoryIndex", add_index, NULL, DIR_CMD_PERMS,
69
"a list of file names"),
70
AP_INIT_FLAG("DirectorySlash", configure_slash, NULL, DIR_CMD_PERMS,
75
static void *create_dir_config(apr_pool_t *p, char *dummy)
77
dir_config_rec *new = apr_pcalloc(p, sizeof(dir_config_rec));
79
new->index_names = NULL;
80
new->do_slash = SLASH_UNSET;
84
static void *merge_dir_configs(apr_pool_t *p, void *basev, void *addv)
86
dir_config_rec *new = apr_pcalloc(p, sizeof(dir_config_rec));
87
dir_config_rec *base = (dir_config_rec *)basev;
88
dir_config_rec *add = (dir_config_rec *)addv;
90
new->index_names = add->index_names ? add->index_names : base->index_names;
92
(add->do_slash == SLASH_UNSET) ? base->do_slash : add->do_slash;
96
static int fixup_dir(request_rec *r)
102
int error_notfound = 0;
104
/* only handle requests against directories */
105
if (r->finfo.filetype != APR_DIR) {
109
/* In case mod_mime wasn't present, and no handler was assigned. */
111
r->handler = DIR_MAGIC_TYPE;
114
/* Never tolerate path_info on dir requests */
115
if (r->path_info && *r->path_info) {
119
d = (dir_config_rec *)ap_get_module_config(r->per_dir_config,
122
/* Redirect requests that are not '/' terminated */
123
if (r->uri[0] == '\0' || r->uri[strlen(r->uri) - 1] != '/')
131
/* Only redirect non-get requests if we have no note to warn
132
* that this browser cannot handle redirs on non-GET requests
133
* (such as Microsoft's WebFolders).
135
if ((r->method_number != M_GET)
136
&& apr_table_get(r->subprocess_env, "redirect-carefully")) {
140
if (r->args != NULL) {
141
ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
142
"/", "?", r->args, NULL);
145
ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
149
apr_table_setn(r->headers_out, "Location",
150
ap_construct_url(r->pool, ifile, r));
151
return HTTP_MOVED_PERMANENTLY;
154
if (strcmp(r->handler, DIR_MAGIC_TYPE)) {
158
if (d->index_names) {
159
names_ptr = (char **)d->index_names->elts;
160
num_names = d->index_names->nelts;
163
dummy_ptr[0] = AP_DEFAULT_INDEX;
164
names_ptr = dummy_ptr;
168
for (; num_names; ++names_ptr, --num_names) {
169
/* XXX: Is this name_ptr considered escaped yet, or not??? */
170
char *name_ptr = *names_ptr;
173
/* Once upon a time args were handled _after_ the successful redirect.
174
* But that redirect might then _refuse_ the given r->args, creating
175
* a nasty tangle. It seems safer to consider the r->args while we
176
* determine if name_ptr is our viable index, and therefore set them
177
* up correctly on redirect.
179
if (r->args != NULL) {
180
name_ptr = apr_pstrcat(r->pool, name_ptr, "?", r->args, NULL);
183
rr = ap_sub_req_lookup_uri(name_ptr, r, NULL);
185
/* The sub request lookup is very liberal, and the core map_to_storage
186
* handler will almost always result in HTTP_OK as /foo/index.html
187
* may be /foo with PATH_INFO="/index.html", or even / with
188
* PATH_INFO="/foo/index.html". To get around this we insist that the
189
* the index be a regular filetype.
191
* Another reason is that the core handler also makes the assumption
192
* that if r->finfo is still NULL by the time it gets called, the
193
* file does not exist.
195
if (rr->status == HTTP_OK
196
&& ( (rr->handler && !strcmp(rr->handler, "proxy-server"))
197
|| rr->finfo.filetype == APR_REG)) {
198
ap_internal_fast_redirect(rr, r);
202
/* If the request returned a redirect, propagate it to the client */
204
if (ap_is_HTTP_REDIRECT(rr->status)
205
|| (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)
206
|| (rr->status == HTTP_UNAUTHORIZED && num_names == 1)) {
208
apr_pool_join(r->pool, rr->pool);
209
error_notfound = rr->status;
210
r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
211
r->headers_out = apr_table_overlay(r->pool, r->headers_out,
213
r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
214
rr->err_headers_out);
215
return error_notfound;
218
/* If the request returned something other than 404 (or 200),
219
* it means the module encountered some sort of problem. To be
220
* secure, we should return the error, rather than allow autoindex
221
* to create a (possibly unsafe) directory index.
223
* So we store the error, and if none of the listed files
224
* exist, we return the last error response we got, instead
225
* of a directory listing.
227
if (rr->status && rr->status != HTTP_NOT_FOUND
228
&& rr->status != HTTP_OK) {
229
error_notfound = rr->status;
232
ap_destroy_sub_req(rr);
235
if (error_notfound) {
236
return error_notfound;
239
/* nothing for us to do, pass on through */
243
static void register_hooks(apr_pool_t *p)
245
ap_hook_fixups(fixup_dir,NULL,NULL,APR_HOOK_LAST);
248
module AP_MODULE_DECLARE_DATA dir_module = {
249
STANDARD20_MODULE_STUFF,
250
create_dir_config, /* create per-directory config structure */
251
merge_dir_configs, /* merge per-directory config structures */
252
NULL, /* create per-server config structure */
253
NULL, /* merge per-server config structures */
254
dir_cmds, /* command apr_table_t */
255
register_hooks /* register hooks */