]> granicus.if.org Git - apache/blob - modules/mappers/mod_dir.c
This is a huge change to the configure system. Basically, this name space
[apache] / modules / mappers / mod_dir.c
1 /* ====================================================================
2  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
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
14  *    distribution.
15  *
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/)."
20  *
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
24  *    apache@apache.org.
25  *
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.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the Apache Group
33  *    for use in the Apache HTTP server project (http://www.apache.org/)."
34  *
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  * ====================================================================
48  *
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/>.
55  *
56  */
57
58 /*
59  * mod_dir.c: handle default index files, and trailing-/ redirects
60  */
61
62 #include "httpd.h"
63 #include "http_config.h"
64 #include "http_core.h"
65 #include "http_request.h"
66 #include "http_protocol.h"
67 #include "http_log.h"
68 #include "http_main.h"
69 #include "util_script.h"
70
71 module MODULE_VAR_EXPORT dir_module;
72
73 typedef struct dir_config_struct {
74     ap_array_header_t *index_names;
75 } dir_config_rec;
76
77 #define DIR_CMD_PERMS OR_INDEXES
78
79 static const char *add_index(cmd_parms *cmd, void *dummy, char *arg)
80 {
81     dir_config_rec *d = dummy;
82
83     if (!d->index_names) {
84         d->index_names = ap_make_array(cmd->pool, 2, sizeof(char *));
85     }
86     *(char **)ap_push_array(d->index_names) = arg;
87     return NULL;
88 }
89
90 static const command_rec dir_cmds[] =
91 {
92     {"DirectoryIndex", add_index, NULL,
93      DIR_CMD_PERMS, ITERATE,
94      "a list of file names"},
95     {NULL}
96 };
97
98 static void *create_dir_config(ap_context_t *p, char *dummy)
99 {
100     dir_config_rec *new =
101     (dir_config_rec *) ap_pcalloc(p, sizeof(dir_config_rec));
102
103     new->index_names = NULL;
104     return (void *) new;
105 }
106
107 static void *merge_dir_configs(ap_context_t *p, void *basev, void *addv)
108 {
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;
112
113     new->index_names = add->index_names ? add->index_names : base->index_names;
114     return new;
115 }
116
117 static int handle_dir(request_rec *r)
118 {
119     dir_config_rec *d =
120     (dir_config_rec *) ap_get_module_config(r->per_dir_config,
121                                          &dir_module);
122     char *dummy_ptr[1];
123     char **names_ptr;
124     int num_names;
125     int error_notfound = 0;
126
127     if (r->uri[0] == '\0' || r->uri[strlen(r->uri) - 1] != '/') {
128         char *ifile;
129         if (r->args != NULL)
130             ifile = ap_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
131                             "/", "?", r->args, NULL);
132         else
133             ifile = ap_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
134                             "/", NULL);
135
136         ap_table_setn(r->headers_out, "Location",
137                   ap_construct_url(r->pool, ifile, r));
138         return HTTP_MOVED_PERMANENTLY;
139     }
140
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...
144      */
145
146     if (r->filename[strlen(r->filename) - 1] != '/') {
147         r->filename = ap_pstrcat(r->pool, r->filename, "/", NULL);
148     }
149
150     if (d->index_names) {
151         names_ptr = (char **)d->index_names->elts;
152         num_names = d->index_names->nelts;
153     }
154     else {
155         dummy_ptr[0] = AP_DEFAULT_INDEX;
156         names_ptr = dummy_ptr;
157         num_names = 1;
158     }
159
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);
163
164         if (rr->status == HTTP_OK && rr->finfo.filetype == APR_REG) {
165             char *new_uri = ap_escape_uri(r->pool, rr->uri);
166
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);
171
172             ap_destroy_sub_req(rr);
173             ap_internal_redirect(new_uri, r);
174             return OK;
175         }
176
177         /* If the request returned a redirect, propagate it to the client */
178
179         if (ap_is_HTTP_REDIRECT(rr->status) ||
180             (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)) {
181
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,
186                                             rr->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;
190         }
191
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.
196          *
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.
200          */
201         if (rr->status && rr->status != HTTP_NOT_FOUND && rr->status != HTTP_OK)
202             error_notfound = rr->status;
203
204         ap_destroy_sub_req(rr);
205     }
206
207     if (error_notfound)
208         return error_notfound;
209
210     if (r->method_number != M_GET)
211         return DECLINED;
212
213     /* nothing for us to do, pass on through */
214
215     return DECLINED;
216 }
217
218
219 static const handler_rec dir_handlers[] =
220 {
221     {DIR_MAGIC_TYPE, handle_dir},
222     {NULL}
223 };
224
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 */
234 };