mod_dir.c revision 5bfaaf573bacb45c1cf290ce85ecc676587e8a64
2d2eda71267231c2526be701fe655db125852c1ffielding/* Licensed to the Apache Software Foundation (ASF) under one or more
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * contributor license agreements. See the NOTICE file distributed with
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * this work for additional information regarding copyright ownership.
26a4456dd6f1a5d7d7fff766551461a578687c4and * The ASF licenses this file to You under the Apache License, Version 2.0
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * (the "License"); you may not use this file except in compliance with
2d2eda71267231c2526be701fe655db125852c1ffielding * the License. You may obtain a copy of the License at
2d2eda71267231c2526be701fe655db125852c1ffielding * Unless required by applicable law or agreed to in writing, software
2d2eda71267231c2526be701fe655db125852c1ffielding * distributed under the License is distributed on an "AS IS" BASIS,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2d2eda71267231c2526be701fe655db125852c1ffielding * See the License for the specific language governing permissions and
2d2eda71267231c2526be701fe655db125852c1ffielding * limitations under the License.
2d2eda71267231c2526be701fe655db125852c1ffielding * mod_dir.c: handle default index files, and trailing-/ redirects
f062ed7bd262a37a909dd77ce5fc23b446818823fieldingtypedef enum {
f062ed7bd262a37a909dd77ce5fc23b446818823fieldingtypedef struct dir_config_struct {
2d2eda71267231c2526be701fe655db125852c1ffielding const char *dflt;
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic const char *add_index(cmd_parms *cmd, void *dummy, const char *arg)
f062ed7bd262a37a909dd77ce5fc23b446818823fielding const char *t, *w;
2d2eda71267231c2526be701fe655db125852c1ffielding d->index_names = apr_array_make(cmd->pool, 2, sizeof(char *));
2d2eda71267231c2526be701fe655db125852c1ffielding /* peek to see if "disabled" is first in a series of arguments */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb const char *tt = t;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* "disabled" is first, and alone */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic const char *configure_slash(cmd_parms *cmd, void *d_, int arg)
7c7372abe2484e7fcf81937b93496d1246e5b816gsteinstatic const char *configure_redirect(cmd_parms *cmd, void *d_, const char *arg1)
2d2eda71267231c2526be701fe655db125852c1ffielding return "DirectoryIndexRedirect only accepts values between 300 and 399";
2d2eda71267231c2526be701fe655db125852c1ffielding return "DirectoryIndexRedirect ON|OFF|permanent|temp|seeother|3xx";
2d2eda71267231c2526be701fe655db125852c1ffielding AP_INIT_TAKE1("FallbackResource", ap_set_string_slot,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb AP_INIT_RAW_ARGS("DirectoryIndex", add_index, NULL, DIR_CMD_PERMS,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb "a list of file names"),
72a4ef8eac1adef882246c5bfb9b8bbd82d613c4coar AP_INIT_FLAG("DirectorySlash", configure_slash, NULL, DIR_CMD_PERMS,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb "On or Off"),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb AP_INIT_TAKE1("DirectoryIndexRedirect", configure_redirect,
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void *create_dir_config(apr_pool_t *p, char *dummy)
2d2eda71267231c2526be701fe655db125852c1ffielding dir_config_rec *new = apr_pcalloc(p, sizeof(dir_config_rec));
2d2eda71267231c2526be701fe655db125852c1ffielding return (void *) new;
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void *merge_dir_configs(apr_pool_t *p, void *basev, void *addv)
2d2eda71267231c2526be701fe655db125852c1ffielding dir_config_rec *new = apr_pcalloc(p, sizeof(dir_config_rec));
2d2eda71267231c2526be701fe655db125852c1ffielding new->index_names = add->index_names ? add->index_names : base->index_names;
2d2eda71267231c2526be701fe655db125852c1ffielding (add->do_slash == SLASH_UNSET) ? base->do_slash : add->do_slash;
2d2eda71267231c2526be701fe655db125852c1ffielding (add->redirect_index == REDIRECT_UNSET) ? base->redirect_index : add->redirect_index;
2d2eda71267231c2526be701fe655db125852c1ffielding dir_config_rec *d = ap_get_module_config(r->per_dir_config, &dir_module);
2d2eda71267231c2526be701fe655db125852c1ffielding const char *name_ptr;
2d2eda71267231c2526be701fe655db125852c1ffielding /* XXX: if FallbackResource points to something that doesn't exist,
2d2eda71267231c2526be701fe655db125852c1ffielding * this may recurse until it hits the limit for internal redirects
2d2eda71267231c2526be701fe655db125852c1ffielding * before returning an Internal Server Error.
2d2eda71267231c2526be701fe655db125852c1ffielding /* The logic of this function is basically cloned and simplified
2d2eda71267231c2526be701fe655db125852c1ffielding * from fixup_dir below. See the comments there.
2d2eda71267231c2526be701fe655db125852c1ffielding name_ptr = apr_pstrcat(r->pool, name_ptr, "?", r->args, NULL);
dbbf1b4183ae16353011a5269b37899f02b97d81gregames rr = ap_sub_req_lookup_uri(name_ptr, r, r->output_filters);
2d2eda71267231c2526be701fe655db125852c1ffielding && ( (rr->handler && !strcmp(rr->handler, "proxy-server"))
e44e11f9fece12c783f18d033923bfc0d6b4289aake r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
2d2eda71267231c2526be701fe655db125852c1ffielding r->headers_out = apr_table_overlay(r->pool, r->headers_out,
2d2eda71267231c2526be701fe655db125852c1ffielding r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
2d2eda71267231c2526be701fe655db125852c1ffielding else if (rr->status && rr->status != HTTP_NOT_FOUND
2d2eda71267231c2526be701fe655db125852c1ffielding /* nothing for us to do, pass on through */
2d2eda71267231c2526be701fe655db125852c1ffielding /* In case mod_mime wasn't present, and no handler was assigned. */
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet /* Never tolerate path_info on dir requests */
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet d = (dir_config_rec *)ap_get_module_config(r->per_dir_config,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* Redirect requests that are not '/' terminated */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh if (r->uri[0] == '\0' || r->uri[strlen(r->uri) - 1] != '/')
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* Only redirect non-get requests if we have no note to warn
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * that this browser cannot handle redirs on non-GET requests
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * (such as Microsoft's WebFolders).
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh && apr_table_get(r->subprocess_env, "redirect-carefully")) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* XXX: Is this name_ptr considered escaped yet, or not??? */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* Once upon a time args were handled _after_ the successful redirect.
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * But that redirect might then _refuse_ the given r->args, creating
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * a nasty tangle. It seems safer to consider the r->args while we
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * determine if name_ptr is our viable index, and therefore set them
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * up correctly on redirect.
99d6d3207e24ba1f8eba77ef903948d738886cf5nd name_ptr = apr_pstrcat(r->pool, name_ptr, "?", r->args, NULL);
99d6d3207e24ba1f8eba77ef903948d738886cf5nd rr = ap_sub_req_lookup_uri(name_ptr, r, r->output_filters);
99d6d3207e24ba1f8eba77ef903948d738886cf5nd /* The sub request lookup is very liberal, and the core map_to_storage
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * handler will almost always result in HTTP_OK as /foo/index.html
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * may be /foo with PATH_INFO="/index.html", or even / with
99d6d3207e24ba1f8eba77ef903948d738886cf5nd * PATH_INFO="/foo/index.html". To get around this we insist that the
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * the index be a regular filetype.
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * Another reason is that the core handler also makes the assumption
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * that if r->finfo is still NULL by the time it gets called, the
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh * file does not exist.
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb && ( (rr->handler && !strcmp(rr->handler, "proxy-server"))
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh apr_table_setn(r->headers_out, "Location", ap_construct_url(r->pool, rr->uri, r));
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben /* If the request returned a redirect, propagate it to the client */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben || (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben || (rr->status == HTTP_UNAUTHORIZED && num_names == 1)) {
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben r->headers_out = apr_table_overlay(r->pool, r->headers_out,
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* If the request returned something other than 404 (or 200),
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * it means the module encountered some sort of problem. To be
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * secure, we should return the error, rather than allow autoindex
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh * to create a (possibly unsafe) directory index.
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * So we store the error, and if none of the listed files
bbe046d7cbb950ab3e372e4119ae001a5fe52ed4striker * exist, we return the last error response we got, instead
bbe046d7cbb950ab3e372e4119ae001a5fe52ed4striker * of a directory listing.
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* record what we tried, mostly for the benefit of mod_autoindex */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* nothing for us to do, pass on through */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh /* serve up a directory */
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh else if ((r->finfo.filetype == APR_NOFILE) && (r->handler == NULL)) {
3d0bdf16bc410722e6c42aa2ceb0677b0ae29b90ianh /* No handler and nothing in the filesystem - use fallback */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben create_dir_config, /* create per-directory config structure */
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben merge_dir_configs, /* merge per-directory config structures */