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 /* XXX: (filetype == APR_REG) - we can't use a non-file index??? */
186 if ( rr->status == HTTP_OK
187 && ( (rr->handler && !strcmp(rr->handler, "proxy-server"))
188 || rr->finfo.filetype == APR_REG)) {
189 ap_internal_fast_redirect(rr, r);
193 /* If the request returned a redirect, propagate it to the client */
195 if (ap_is_HTTP_REDIRECT(rr->status)
196 || (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)
197 || (rr->status == HTTP_UNAUTHORIZED && num_names == 1)) {
199 apr_pool_join(r->pool, rr->pool);
200 error_notfound = rr->status;
201 r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
202 r->headers_out = apr_table_overlay(r->pool, r->headers_out,
204 r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
205 rr->err_headers_out);
206 return error_notfound;
209 /* If the request returned something other than 404 (or 200),
210 * it means the module encountered some sort of problem. To be
211 * secure, we should return the error, rather than allow autoindex
212 * to create a (possibly unsafe) directory index.
214 * So we store the error, and if none of the listed files
215 * exist, we return the last error response we got, instead
216 * of a directory listing.
218 if (rr->status && rr->status != HTTP_NOT_FOUND
219 && rr->status != HTTP_OK) {
220 error_notfound = rr->status;
223 ap_destroy_sub_req(rr);
226 if (error_notfound) {
227 return error_notfound;
230 /* nothing for us to do, pass on through */
234 static void register_hooks(apr_pool_t *p)
236 ap_hook_fixups(fixup_dir,NULL,NULL,APR_HOOK_LAST);
239 module AP_MODULE_DECLARE_DATA dir_module = {
240 STANDARD20_MODULE_STUFF,
241 create_dir_config, /* create per-directory config structure */
242 merge_dir_configs, /* merge per-directory config structures */
243 NULL, /* create per-server config structure */
244 NULL, /* merge per-server config structures */
245 dir_cmds, /* command apr_table_t */
246 register_hooks /* register hooks */