1 /* ====================================================================
2 * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
59 * mod_dir.c: handle default index files, and trailing-/ redirects
63 #include "http_config.h"
64 #include "http_core.h"
65 #include "http_request.h"
66 #include "http_protocol.h"
68 #include "http_main.h"
69 #include "util_script.h"
71 module MODULE_VAR_EXPORT dir_module;
73 typedef struct dir_config_struct {
74 ap_array_header_t *index_names;
77 #define DIR_CMD_PERMS OR_INDEXES
79 static const char *add_index(cmd_parms *cmd, void *dummy, char *arg)
81 dir_config_rec *d = dummy;
83 if (!d->index_names) {
84 d->index_names = ap_make_array(cmd->pool, 2, sizeof(char *));
86 *(char **)ap_push_array(d->index_names) = arg;
90 static const command_rec dir_cmds[] =
92 {"DirectoryIndex", add_index, NULL,
93 DIR_CMD_PERMS, ITERATE,
94 "a list of file names"},
98 static void *create_dir_config(ap_context_t *p, char *dummy)
100 dir_config_rec *new =
101 (dir_config_rec *) ap_pcalloc(p, sizeof(dir_config_rec));
103 new->index_names = NULL;
107 static void *merge_dir_configs(ap_context_t *p, void *basev, void *addv)
109 dir_config_rec *new = (dir_config_rec *) ap_pcalloc(p, sizeof(dir_config_rec));
110 dir_config_rec *base = (dir_config_rec *) basev;
111 dir_config_rec *add = (dir_config_rec *) addv;
113 new->index_names = add->index_names ? add->index_names : base->index_names;
117 static int handle_dir(request_rec *r)
120 (dir_config_rec *) ap_get_module_config(r->per_dir_config,
125 int error_notfound = 0;
127 if (r->uri[0] == '\0' || r->uri[strlen(r->uri) - 1] != '/') {
130 ifile = ap_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
131 "/", "?", r->args, NULL);
133 ifile = ap_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
136 ap_table_setn(r->headers_out, "Location",
137 ap_construct_url(r->pool, ifile, r));
138 return HTTP_MOVED_PERMANENTLY;
141 /* KLUDGE --- make the sub_req lookups happen in the right directory.
142 * Fixing this in the sub_req_lookup functions themselves is difficult,
143 * and would probably break virtual includes...
146 if (r->filename[strlen(r->filename) - 1] != '/') {
147 r->filename = ap_pstrcat(r->pool, r->filename, "/", NULL);
150 if (d->index_names) {
151 names_ptr = (char **)d->index_names->elts;
152 num_names = d->index_names->nelts;
155 dummy_ptr[0] = AP_DEFAULT_INDEX;
156 names_ptr = dummy_ptr;
160 for (; num_names; ++names_ptr, --num_names) {
161 char *name_ptr = *names_ptr;
162 request_rec *rr = ap_sub_req_lookup_uri(name_ptr, r);
164 if (rr->status == HTTP_OK && rr->finfo.filetype == APR_REG) {
165 char *new_uri = ap_escape_uri(r->pool, rr->uri);
167 if (rr->args != NULL)
168 new_uri = ap_pstrcat(r->pool, new_uri, "?", rr->args, NULL);
169 else if (r->args != NULL)
170 new_uri = ap_pstrcat(r->pool, new_uri, "?", r->args, NULL);
172 ap_destroy_sub_req(rr);
173 ap_internal_redirect(new_uri, r);
177 /* If the request returned a redirect, propagate it to the client */
179 if (ap_is_HTTP_REDIRECT(rr->status) ||
180 (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)) {
182 ap_pool_join(r->pool, rr->pool);
183 error_notfound = rr->status;
184 r->notes = ap_overlay_tables(r->pool, r->notes, rr->notes);
185 r->headers_out = ap_overlay_tables(r->pool, r->headers_out,
187 r->err_headers_out = ap_overlay_tables(r->pool, r->err_headers_out,
188 rr->err_headers_out);
189 return error_notfound;
192 /* If the request returned something other than 404 (or 200),
193 * it means the module encountered some sort of problem. To be
194 * secure, we should return the error, rather than create
195 * along a (possibly unsafe) directory index.
197 * So we store the error, and if none of the listed files
198 * exist, we return the last error response we got, instead
199 * of a directory listing.
201 if (rr->status && rr->status != HTTP_NOT_FOUND && rr->status != HTTP_OK)
202 error_notfound = rr->status;
204 ap_destroy_sub_req(rr);
208 return error_notfound;
210 if (r->method_number != M_GET)
213 /* nothing for us to do, pass on through */
219 static const handler_rec dir_handlers[] =
221 {DIR_MAGIC_TYPE, handle_dir},
225 module MODULE_VAR_EXPORT dir_module = {
226 STANDARD20_MODULE_STUFF,
227 create_dir_config, /* create per-directory config structure */
228 merge_dir_configs, /* merge per-directory config structures */
229 NULL, /* create per-server config structure */
230 NULL, /* merge per-server config structures */
231 dir_cmds, /* command ap_table_t */
232 dir_handlers, /* handlers */
233 NULL /* register hooks */