1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 * mod_dir.c: handle default index files, and trailing-/ redirects
63 #include "apr_strings.h"
64 #include "ap_config.h"
66 #include "http_config.h"
67 #include "http_core.h"
68 #include "http_request.h"
69 #include "http_protocol.h"
71 #include "http_main.h"
72 #include "util_script.h"
74 module AP_MODULE_DECLARE_DATA dir_module;
76 typedef struct dir_config_struct {
77 apr_array_header_t *index_names;
80 #define DIR_CMD_PERMS OR_INDEXES
82 static const char *add_index(cmd_parms *cmd, void *dummy, const char *arg)
84 dir_config_rec *d = dummy;
86 if (!d->index_names) {
87 d->index_names = apr_make_array(cmd->pool, 2, sizeof(char *));
89 *(const char **)apr_push_array(d->index_names) = arg;
93 static const command_rec dir_cmds[] =
95 AP_INIT_ITERATE("DirectoryIndex", add_index, NULL, DIR_CMD_PERMS,
96 "a list of file names"),
100 static void *create_dir_config(apr_pool_t *p, char *dummy)
102 dir_config_rec *new =
103 (dir_config_rec *) apr_pcalloc(p, sizeof(dir_config_rec));
105 new->index_names = NULL;
109 static void *merge_dir_configs(apr_pool_t *p, void *basev, void *addv)
111 dir_config_rec *new = (dir_config_rec *) apr_pcalloc(p, sizeof(dir_config_rec));
112 dir_config_rec *base = (dir_config_rec *) basev;
113 dir_config_rec *add = (dir_config_rec *) addv;
115 new->index_names = add->index_names ? add->index_names : base->index_names;
119 static int handle_dir(const char *handler,request_rec *r)
125 int error_notfound = 0;
127 if(strcmp(handler,DIR_MAGIC_TYPE))
130 d = (dir_config_rec *) ap_get_module_config(r->per_dir_config,
133 if (r->uri[0] == '\0' || r->uri[strlen(r->uri) - 1] != '/') {
136 ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
137 "/", "?", r->args, NULL);
139 ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
142 apr_table_setn(r->headers_out, "Location",
143 ap_construct_url(r->pool, ifile, r));
144 return HTTP_MOVED_PERMANENTLY;
147 /* KLUDGE --- make the sub_req lookups happen in the right directory.
148 * Fixing this in the sub_req_lookup functions themselves is difficult,
149 * and would probably break virtual includes...
152 if (r->filename[strlen(r->filename) - 1] != '/') {
153 r->filename = apr_pstrcat(r->pool, r->filename, "/", NULL);
156 if (d->index_names) {
157 names_ptr = (char **)d->index_names->elts;
158 num_names = d->index_names->nelts;
161 dummy_ptr[0] = AP_DEFAULT_INDEX;
162 names_ptr = dummy_ptr;
166 for (; num_names; ++names_ptr, --num_names) {
167 char *name_ptr = *names_ptr;
168 request_rec *rr = ap_sub_req_lookup_uri(name_ptr, r, NULL);
170 if (rr->status == HTTP_OK && rr->finfo.filetype == APR_REG) {
171 char *new_uri = ap_escape_uri(r->pool, rr->uri);
173 if (rr->args != NULL)
174 new_uri = apr_pstrcat(r->pool, new_uri, "?", rr->args, NULL);
175 else if (r->args != NULL)
176 new_uri = apr_pstrcat(r->pool, new_uri, "?", r->args, NULL);
178 ap_destroy_sub_req(rr);
179 ap_internal_redirect(new_uri, r);
183 /* If the request returned a redirect, propagate it to the client */
185 if (ap_is_HTTP_REDIRECT(rr->status) ||
186 (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)) {
188 apr_pool_join(r->pool, rr->pool);
189 error_notfound = rr->status;
190 r->notes = apr_overlay_tables(r->pool, r->notes, rr->notes);
191 r->headers_out = apr_overlay_tables(r->pool, r->headers_out,
193 r->err_headers_out = apr_overlay_tables(r->pool, r->err_headers_out,
194 rr->err_headers_out);
195 return error_notfound;
198 /* If the request returned something other than 404 (or 200),
199 * it means the module encountered some sort of problem. To be
200 * secure, we should return the error, rather than create
201 * along a (possibly unsafe) directory index.
203 * So we store the error, and if none of the listed files
204 * exist, we return the last error response we got, instead
205 * of a directory listing.
207 if (rr->status && rr->status != HTTP_NOT_FOUND && rr->status != HTTP_OK)
208 error_notfound = rr->status;
210 ap_destroy_sub_req(rr);
214 return error_notfound;
216 if (r->method_number != M_GET)
219 /* nothing for us to do, pass on through */
225 static void register_hooks(void)
227 static const char * const aszSucc[]={ "mod_autoindex.c", NULL };
229 ap_hook_handler(handle_dir,NULL,aszSucc,AP_HOOK_MIDDLE);
232 module AP_MODULE_DECLARE_DATA dir_module = {
233 STANDARD20_MODULE_STUFF,
234 create_dir_config, /* create per-directory config structure */
235 merge_dir_configs, /* merge per-directory config structures */
236 NULL, /* create per-server config structure */
237 NULL, /* merge per-server config structures */
238 dir_cmds, /* command apr_table_t */
239 register_hooks /* register hooks */