1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2002 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 * http_request.c: functions to get and process requests
64 * Thoroughly revamped by rst for Apache. NB this file reads
65 * best from the bottom up.
69 #include "apr_strings.h"
70 #include "apr_file_io.h"
71 #include "apr_fnmatch.h"
73 #define APR_WANT_STRFUNC
77 #include "ap_config.h"
79 #include "http_config.h"
80 #include "http_request.h"
81 #include "http_core.h"
82 #include "http_protocol.h"
84 #include "http_main.h"
85 #include "util_filter.h"
86 #include "util_charset.h"
87 #include "util_script.h"
96 APR_HOOK_LINK(translate_name)
97 APR_HOOK_LINK(map_to_storage)
98 APR_HOOK_LINK(check_user_id)
100 APR_HOOK_LINK(type_checker)
101 APR_HOOK_LINK(access_checker)
102 APR_HOOK_LINK(auth_checker)
103 APR_HOOK_LINK(insert_filter)
104 APR_HOOK_LINK(create_request)
107 AP_IMPLEMENT_HOOK_RUN_FIRST(int,translate_name,
108 (request_rec *r), (r), DECLINED)
109 AP_IMPLEMENT_HOOK_RUN_FIRST(int,map_to_storage,
110 (request_rec *r), (r), DECLINED)
111 AP_IMPLEMENT_HOOK_RUN_FIRST(int,check_user_id,
112 (request_rec *r), (r), DECLINED)
113 AP_IMPLEMENT_HOOK_RUN_ALL(int,fixups,
114 (request_rec *r), (r), OK, DECLINED)
115 AP_IMPLEMENT_HOOK_RUN_FIRST(int,type_checker,
116 (request_rec *r), (r), DECLINED)
117 AP_IMPLEMENT_HOOK_RUN_ALL(int,access_checker,
118 (request_rec *r), (r), OK, DECLINED)
119 AP_IMPLEMENT_HOOK_RUN_FIRST(int,auth_checker,
120 (request_rec *r), (r), DECLINED)
121 AP_IMPLEMENT_HOOK_VOID(insert_filter, (request_rec *r), (r))
122 AP_IMPLEMENT_HOOK_RUN_ALL(int, create_request,
123 (request_rec *r), (r), OK, DECLINED)
126 static int decl_die(int status, char *phase, request_rec *r)
128 if (status == DECLINED) {
129 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
130 "configuration error: couldn't %s: %s", phase, r->uri);
131 return HTTP_INTERNAL_SERVER_ERROR;
138 /* This is the master logic for processing requests. Do NOT duplicate
139 * this logic elsewhere, or the security model will be broken by future
140 * API changes. Each phase must be individually optimized to pick up
141 * redundant/duplicate calls by subrequests, and redirects.
143 AP_DECLARE(int) ap_process_request_internal(request_rec *r)
145 int file_req = (r->main && r->filename);
148 /* Ignore embedded %2F's in path for proxy requests */
149 if (!r->proxyreq && r->parsed_uri.path) {
150 access_status = ap_unescape_url(r->parsed_uri.path);
152 if (access_status == HTTP_NOT_FOUND) {
153 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
154 "found %%2f (encoded '/') in URI "
155 "(decoded='%s'), returning 404",
158 return access_status;
162 ap_getparents(r->uri); /* OK --- shrinking transformations... */
164 /* All file subrequests are a huge pain... they cannot bubble through the
165 * next several steps. Only file subrequests are allowed an empty uri,
166 * otherwise let translate_name kill the request.
169 if ((access_status = ap_location_walk(r))) {
170 return access_status;
173 if ((access_status = ap_run_translate_name(r))) {
174 return decl_die(access_status, "translate", r);
178 /* Reset to the server default config prior to running map_to_storage
180 r->per_dir_config = r->server->lookup_defaults;
182 if ((access_status = ap_run_map_to_storage(r))) {
183 /* This request wasn't in storage (e.g. TRACE) */
184 return access_status;
187 /* Excluding file-specific requests with no 'true' URI...
190 /* Rerun the location walk, which overrides any map_to_storage config.
192 if ((access_status = ap_location_walk(r))) {
193 return access_status;
197 /* Only on the main request! */
198 if (r->main == NULL) {
199 if ((access_status = ap_run_header_parser(r))) {
200 return access_status;
204 /* Skip authn/authz if the parent or prior request passed the authn/authz,
205 * and that configuration didn't change (this requires optimized _walk()
206 * functions in map_to_storage that use the same merge results given
207 * identical input.) If the config changes, we must re-auth.
209 if (r->main && (r->main->per_dir_config == r->per_dir_config)) {
210 r->user = r->main->user;
211 r->ap_auth_type = r->main->ap_auth_type;
213 else if (r->prev && (r->prev->per_dir_config == r->per_dir_config)) {
214 r->user = r->prev->user;
215 r->ap_auth_type = r->prev->ap_auth_type;
218 switch (ap_satisfies(r)) {
221 if ((access_status = ap_run_access_checker(r)) != 0) {
222 return decl_die(access_status, "check access", r);
225 if (ap_some_auth_required(r)) {
226 if (((access_status = ap_run_check_user_id(r)) != 0)
227 || !ap_auth_type(r)) {
228 return decl_die(access_status, ap_auth_type(r)
229 ? "check user. No user file?"
230 : "perform authentication. AuthType not set!",
234 if (((access_status = ap_run_auth_checker(r)) != 0)
235 || !ap_auth_type(r)) {
236 return decl_die(access_status, ap_auth_type(r)
237 ? "check access. No groups file?"
238 : "perform authentication. AuthType not set!",
245 if (((access_status = ap_run_access_checker(r)) != 0)
246 || !ap_auth_type(r)) {
247 if (!ap_some_auth_required(r)) {
248 return decl_die(access_status, ap_auth_type(r)
250 : "perform authentication. AuthType not set!",
254 if (((access_status = ap_run_check_user_id(r)) != 0)
255 || !ap_auth_type(r)) {
256 return decl_die(access_status, ap_auth_type(r)
257 ? "check user. No user file?"
258 : "perform authentication. AuthType not set!",
262 if (((access_status = ap_run_auth_checker(r)) != 0)
263 || !ap_auth_type(r)) {
264 return decl_die(access_status, ap_auth_type(r)
265 ? "check access. No groups file?"
266 : "perform authentication. AuthType not set!",
273 /* XXX Must make certain the ap_run_type_checker short circuits mime
274 * in mod-proxy for r->proxyreq && r->parsed_uri.scheme
275 * && !strcmp(r->parsed_uri.scheme, "http")
277 if ((access_status = ap_run_type_checker(r)) != 0) {
278 return decl_die(access_status, "find types", r);
281 if ((access_status = ap_run_fixups(r)) != 0) {
282 return access_status;
289 /* Useful caching structures to repeat _walk/merge sequences as required
290 * when a subrequest or redirect reuses substantially the same config.
292 * Directive order in the httpd.conf file and its Includes significantly
293 * impact this optimization. Grouping common blocks at the front of the
294 * config that are less likely to change between a request and
295 * its subrequests, or between a request and its redirects reduced
296 * the work of these functions significantly.
299 typedef struct walk_walked_t {
300 ap_conf_vector_t *matched; /* A dir_conf sections we matched */
301 ap_conf_vector_t *merged; /* The dir_conf merged result */
304 typedef struct walk_cache_t {
305 const char *cached; /* The identifier we matched */
306 ap_conf_vector_t **dir_conf_tested; /* The sections we matched against */
307 ap_conf_vector_t *dir_conf_merged; /* Base per_dir_config */
308 ap_conf_vector_t *per_dir_result; /* per_dir_config += walked result */
309 apr_array_header_t *walked; /* The list of walk_walked_t results */
312 static walk_cache_t *prep_walk_cache(apr_size_t t, request_rec *r)
317 /* Find the most relevant, recent entry to work from. That would be
318 * this request (on the second call), or the parent request of a
319 * subrequest, or the prior request of an internal redirect. Provide
320 * this _walk()er with a copy it is allowed to munge. If there is no
321 * parent or prior cached request, then create a new walk cache.
323 note = ap_get_request_note(r, t);
328 if (!(cache = *note)) {
332 && ((inherit_note = ap_get_request_note(r->main, t)))
335 && ((inherit_note = ap_get_request_note(r->prev, t)))
337 cache = apr_pmemdup(r->pool, *inherit_note,
339 cache->walked = apr_array_copy(r->pool, cache->walked);
342 cache = apr_pcalloc(r->pool, sizeof(*cache));
343 cache->walked = apr_array_make(r->pool, 4, sizeof(walk_walked_t));
351 /*****************************************************************
353 * Getting and checking directory configuration. Also checks the
354 * FollowSymlinks and FollowSymOwner stuff, since this is really the
355 * only place that can happen (barring a new mid_dir_walk callout).
357 * We can't do it as an access_checker module function which gets
358 * called with the final per_dir_config, since we could have a directory
359 * with FollowSymLinks disabled, which contains a symlink to another
360 * with a .htaccess file which turns FollowSymLinks back on --- and
361 * access in such a case must be denied. So, whatever it is that
362 * checks FollowSymLinks needs to know the state of the options as
363 * they change, all the way down.
367 * We don't want people able to serve up pipes, or unix sockets, or other
368 * scary things. Note that symlink tests are performed later.
370 static int check_safe_file(request_rec *r)
373 if (r->finfo.filetype == 0 /* doesn't exist */
374 || r->finfo.filetype == APR_DIR
375 || r->finfo.filetype == APR_REG
376 || r->finfo.filetype == APR_LNK) {
380 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
381 "object is not a file, directory or symlink: %s",
383 return HTTP_FORBIDDEN;
387 * resolve_symlink must _always_ be called on an APR_LNK file type!
388 * It will resolve the actual target file type, modification date, etc,
389 * and provide any processing required for symlink evaluation.
390 * Path must already be cleaned, no trailing slash, no multi-slashes,
391 * and don't call this on the root!
393 * Simply, the number of times we deref a symlink are minimal compared
394 * to the number of times we had an extra lstat() since we 'weren't sure'.
396 * To optimize, we stat() anything when given (opts & OPT_SYM_LINKS), otherwise
397 * we start off with an lstat(). Every lstat() must be dereferenced in case
398 * it points at a 'nasty' - we must always rerun check_safe_file (or similar.)
400 static int resolve_symlink(char *d, apr_finfo_t *lfi, int opts, apr_pool_t *p)
404 const char *savename;
406 if (!(opts & (OPT_SYM_OWNER | OPT_SYM_LINKS))) {
407 return HTTP_FORBIDDEN;
410 /* Save the name from the valid bits. */
411 savename = (lfi->valid & APR_FINFO_NAME) ? lfi->name : NULL;
413 if (opts & OPT_SYM_LINKS) {
414 if ((res = apr_stat(&fi, d, lfi->valid & ~(APR_FINFO_NAME
415 | APR_FINFO_LINK), p))
417 return HTTP_FORBIDDEN;
420 /* Give back the target */
421 memcpy(lfi, &fi, sizeof(fi));
423 lfi->name = savename;
424 lfi->valid |= APR_FINFO_NAME;
430 /* OPT_SYM_OWNER only works if we can get the owner of
431 * both the file and symlink. First fill in a missing
432 * owner of the symlink, then get the info of the target.
434 if (!(lfi->valid & APR_FINFO_OWNER)) {
435 if ((res = apr_lstat(&fi, d, lfi->valid | APR_FINFO_OWNER, p))
437 return HTTP_FORBIDDEN;
441 if ((res = apr_stat(&fi, d, lfi->valid & ~(APR_FINFO_NAME), p))
443 return HTTP_FORBIDDEN;
446 if (apr_compare_users(fi.user, lfi->user) != APR_SUCCESS) {
447 return HTTP_FORBIDDEN;
450 /* Give back the target */
451 memcpy(lfi, &fi, sizeof(fi));
453 lfi->name = savename;
454 lfi->valid |= APR_FINFO_NAME;
462 * As we walk the directory configuration, the merged config won't
463 * be 'rooted' to a specific vhost until the very end of the merge.
465 * We need a very fast mini-merge to a real, vhost-rooted merge
466 * of core.opts and core.override, the only options tested within
467 * directory_walk itself.
469 * See core.c::merge_core_dir_configs() for explanation.
472 typedef struct core_opts_t {
473 allow_options_t opts;
475 allow_options_t remove;
476 overrides_t override;
479 void core_opts_merge(const ap_conf_vector_t *sec, core_opts_t *opts)
481 core_dir_config *this_dir = ap_get_module_config(sec, &core_module);
487 if (this_dir->opts & OPT_UNSET) {
488 opts->add = (opts->add & ~this_dir->opts_remove)
489 | this_dir->opts_add;
490 opts->remove = (opts->remove & ~this_dir->opts_add)
491 | this_dir->opts_remove;
492 opts->opts = (opts->opts & ~opts->remove) | opts->add;
495 opts->opts = this_dir->opts;
496 opts->add = this_dir->opts_add;
497 opts->remove = this_dir->opts_remove;
500 if (!(this_dir->override & OR_UNSET)) {
501 opts->override = this_dir->override;
506 /*****************************************************************
508 * Getting and checking directory configuration. Also checks the
509 * FollowSymlinks and FollowSymOwner stuff, since this is really the
510 * only place that can happen (barring a new mid_dir_walk callout).
512 * We can't do it as an access_checker module function which gets
513 * called with the final per_dir_config, since we could have a directory
514 * with FollowSymLinks disabled, which contains a symlink to another
515 * with a .htaccess file which turns FollowSymLinks back on --- and
516 * access in such a case must be denied. So, whatever it is that
517 * checks FollowSymLinks needs to know the state of the options as
518 * they change, all the way down.
521 AP_DECLARE(int) ap_directory_walk(request_rec *r)
523 ap_conf_vector_t *now_merged = NULL;
524 core_server_config *sconf = ap_get_module_config(r->server->module_config,
526 ap_conf_vector_t **sec_ent = (ap_conf_vector_t **) sconf->sec_dir->elts;
527 int num_sec = sconf->sec_dir->nelts;
532 /* XXX: Better (faster) tests needed!!!
534 * "OK" as a response to a real problem is not _OK_, but to allow broken
535 * modules to proceed, we will permit the not-a-path filename to pass the
536 * following two tests. This behavior may be revoked in future versions
537 * of Apache. We still must catch it later if it's heading for the core
538 * handler. Leave INFO notes here for module debugging.
540 if (r->filename == NULL) {
541 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
542 "Module bug? Request filename is missing for URI %s",
547 /* Canonicalize the file path without resolving filename case or aliases
548 * so we can begin by checking the cache for a recent directory walk.
549 * This call will ensure we have an absolute path in the same pass.
551 if ((rv = apr_filepath_merge(&entry_dir, NULL, r->filename,
552 APR_FILEPATH_NOTRELATIVE, r->pool))
554 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
555 "Module bug? Request filename path %s is invalid or "
556 "or not absolute for uri %s",
557 r->filename, r->uri);
561 /* XXX Notice that this forces path_info to be canonical. That might
562 * not be desired by all apps. However, some of those same apps likely
563 * have significant security holes.
565 r->filename = entry_dir;
567 cache = prep_walk_cache(AP_NOTE_DIRECTORY_WALK, r);
569 /* If this is not a dirent subrequest with a preconstructed
570 * r->finfo value, then we can simply stat the filename to
571 * save burning mega-cycles with unneeded stats - if this is
572 * an exact file match. We don't care about failure... we
573 * will stat by component failing this meager attempt.
575 * It would be nice to distinguish APR_ENOENT from other
576 * types of failure, such as APR_ENOTDIR. We can do something
577 * with APR_ENOENT, knowing that the path is good.
579 if (!r->finfo.filetype || r->finfo.filetype == APR_LNK) {
580 apr_stat(&r->finfo, r->filename, APR_FINFO_MIN, r->pool);
582 /* some OSs will return APR_SUCCESS/APR_REG if we stat
583 * a regular file but we have '/' at the end of the name;
585 * other OSs will return APR_ENOTDIR for that situation;
587 * handle it the same everywhere by simulating a failure
588 * if it looks like a directory but really isn't
590 if (r->finfo.filetype &&
591 r->finfo.filetype != APR_DIR &&
592 r->filename[strlen(r->filename) - 1] == '/') {
593 r->finfo.filetype = 0; /* forget what we learned */
597 if (r->finfo.filetype == APR_REG) {
598 entry_dir = ap_make_dirstr_parent(r->pool, entry_dir);
600 else if (r->filename[strlen(r->filename) - 1] != '/') {
601 entry_dir = apr_pstrcat(r->pool, r->filename, "/", NULL);
604 /* If we have a file already matches the path of r->filename,
605 * and the vhost's list of directory sections hasn't changed,
606 * we can skip rewalking the directory_walk entries.
609 && ((r->finfo.filetype == APR_REG)
610 || ((r->finfo.filetype == APR_DIR)
611 && (!r->path_info || !*r->path_info)))
612 && (cache->dir_conf_tested == sec_ent)
613 && (strcmp(entry_dir, cache->cached) == 0)) {
614 /* Well this looks really familiar! If our end-result (per_dir_result)
615 * didn't change, we have absolutely nothing to do :)
616 * Otherwise (as is the case with most dir_merged/file_merged requests)
617 * we must merge our dir_conf_merged onto this new r->per_dir_config.
619 if (r->per_dir_config == cache->per_dir_result) {
623 if (r->per_dir_config == cache->dir_conf_merged) {
624 r->per_dir_config = cache->per_dir_result;
628 if (cache->walked->nelts) {
629 now_merged = ((walk_walked_t*)cache->walked->elts)
630 [cache->walked->nelts - 1].merged;
634 /* We start now_merged from NULL since we want to build
635 * a locations list that can be merged to any vhost.
638 int matches = cache->walked->nelts;
639 walk_walked_t *last_walk = (walk_walked_t*)cache->walked->elts;
640 core_dir_config *this_dir;
642 apr_finfo_t thisinfo;
643 char *save_path_info;
646 unsigned int seg, startseg;
648 /* Invariant: from the first time filename_len is set until
649 * it goes out of scope, filename_len==strlen(r->filename)
651 apr_size_t filename_len;
652 #ifdef CASE_BLIND_FILESYSTEM
653 apr_size_t canonical_len;
657 * We must play our own mimi-merge game here, for the few
658 * running dir_config values we care about within dir_walk.
659 * We didn't start the merge from r->per_dir_config, so we
660 * accumulate opts and override as we merge, from the globals.
662 this_dir = ap_get_module_config(r->per_dir_config, &core_module);
663 opts.opts = this_dir->opts;
664 opts.add = this_dir->opts_add;
665 opts.remove = this_dir->opts_remove;
666 opts.override = this_dir->override;
668 /* Set aside path_info to merge back onto path_info later.
669 * If r->filename is a directory, we must remerge the path_info,
670 * before we continue! [Directories cannot, by defintion, have
671 * path info. Either the next segment is not-found, or a file.]
673 * r->path_info tracks the unconsumed source path.
674 * r->filename tracks the path as we process it
676 if ((r->finfo.filetype == APR_DIR) && r->path_info && *r->path_info)
678 if ((rv = apr_filepath_merge(&r->path_info, r->filename,
680 APR_FILEPATH_NOTABOVEROOT, r->pool))
682 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
683 "dir_walk error, path_info %s is not relative "
684 "to the filename path %s for uri %s",
685 r->path_info, r->filename, r->uri);
686 return HTTP_INTERNAL_SERVER_ERROR;
689 save_path_info = NULL;
692 save_path_info = r->path_info;
693 r->path_info = r->filename;
696 #ifdef CASE_BLIND_FILESYSTEM
699 while (r->canonical_filename && r->canonical_filename[canonical_len]
700 && (r->canonical_filename[canonical_len]
701 == r->path_info[canonical_len])) {
706 && ((r->canonical_filename[canonical_len - 1] != '/'
707 && r->canonical_filename[canonical_len - 1])
708 || (r->path_info[canonical_len - 1] != '/'
709 && r->path_info[canonical_len - 1]))) {
714 * Now build r->filename component by component, starting
715 * with the root (on Unix, simply "/"). We will make a huge
716 * assumption here for efficiency, that any canonical path
717 * already given included a canonical root.
719 rv = apr_filepath_root((const char **)&r->filename,
720 (const char **)&r->path_info,
721 canonical_len ? 0 : APR_FILEPATH_TRUENAME,
723 filename_len = strlen(r->filename);
726 * Bad assumption above? If the root's length is longer
727 * than the canonical length, then it cannot be trusted as
728 * a truename. So try again, this time more seriously.
730 if ((rv == APR_SUCCESS) && canonical_len
731 && (filename_len > canonical_len)) {
732 rv = apr_filepath_root((const char **)&r->filename,
733 (const char **)&r->path_info,
734 APR_FILEPATH_TRUENAME, r->pool);
735 filename_len = strlen(r->filename);
739 #else /* ndef CASE_BLIND_FILESYSTEM, really this simple for Unix today; */
741 rv = apr_filepath_root((const char **)&r->filename,
742 (const char **)&r->path_info,
744 filename_len = strlen(r->filename);
748 if (rv != APR_SUCCESS) {
749 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
750 "dir_walk error, could not determine the root "
751 "path of filename %s%s for uri %s",
752 r->filename, r->path_info, r->uri);
753 return HTTP_INTERNAL_SERVER_ERROR;
756 /* Working space for terminating null and an extra / is required.
758 buflen = filename_len + strlen(r->path_info) + 2;
759 buf = apr_palloc(r->pool, buflen);
760 memcpy(buf, r->filename, filename_len + 1);
762 thisinfo.valid = APR_FINFO_TYPE;
763 thisinfo.filetype = APR_DIR; /* It's the root, of course it's a dir */
766 * seg keeps track of which segment we've copied.
767 * sec_idx keeps track of which section we're on, since sections are
768 * ordered by number of segments. See core_reorder_directories
769 * startseg tells us how many segments describe the root path
770 * e.g. the complete path "//host/foo/" to a UNC share (4)
772 startseg = seg = ap_count_dirs(r->filename);
776 * Go down the directory hierarchy. Where we have to check for
777 * symlinks, do so. Where a .htaccess file has permission to
778 * override anything, try to find one.
786 /* We have no trailing slash, but we sure would appreciate one.
787 * However, we don't want to append a / our first time through.
789 if ((seg > startseg) && r->filename[filename_len-1] != '/') {
790 r->filename[filename_len++] = '/';
791 r->filename[filename_len] = 0;
795 /* Begin *this* level by looking for matching <Directory> sections
796 * from the server config.
798 for (; sec_idx < num_sec; ++sec_idx) {
800 ap_conf_vector_t *entry_config = sec_ent[sec_idx];
801 core_dir_config *entry_core;
802 entry_core = ap_get_module_config(entry_config, &core_module);
804 /* No more possible matches for this many segments?
805 * We are done when we find relative/regex/longer components.
807 if (entry_core->r || entry_core->d_components > seg) {
811 /* We will never skip '0' element components, e.g. plain old
812 * <Directory >, and <Directory "/"> are classified as zero
813 * so that Win32/Netware/OS2 etc all pick them up.
814 * Otherwise, skip over the mismatches.
816 if (entry_core->d_components
817 && ((entry_core->d_components < seg)
818 || (entry_core->d_is_fnmatch
819 ? (apr_fnmatch(entry_core->d, r->filename,
820 FNM_PATHNAME) != APR_SUCCESS)
821 : (strcmp(r->filename, entry_core->d) != 0)))) {
825 /* If we haven't continue'd above, we have a match.
827 * Calculate our full-context core opts & override.
829 core_opts_merge(sec_ent[sec_idx], &opts);
831 /* If we merged this same section last time, reuse it
834 if (last_walk->matched == sec_ent[sec_idx]) {
835 now_merged = last_walk->merged;
841 /* We fell out of sync. This is our own copy of walked,
842 * so truncate the remaining matches and reset remaining.
844 cache->walked->nelts -= matches;
849 now_merged = ap_merge_per_dir_configs(r->pool,
854 now_merged = sec_ent[sec_idx];
857 last_walk = (walk_walked_t*)apr_array_push(cache->walked);
858 last_walk->matched = sec_ent[sec_idx];
859 last_walk->merged = now_merged;
862 /* If .htaccess files are enabled, check for one, provided we
863 * have reached a real path.
865 do { /* Not really a loop, just a break'able code block */
867 ap_conf_vector_t *htaccess_conf = NULL;
869 /* No htaccess in an incomplete root path,
870 * nor if it's disabled
872 if (seg < startseg || !opts.override) {
876 res = ap_parse_htaccess(&htaccess_conf, r, opts.override,
877 apr_pstrdup(r->pool, r->filename),
883 if (!htaccess_conf) {
887 /* If we are still here, we found our htaccess.
889 * Calculate our full-context core opts & override.
891 core_opts_merge(htaccess_conf, &opts);
893 /* If we merged this same htaccess last time, reuse it...
894 * this wouldn't work except that we cache the htaccess
895 * sections for the lifetime of the request, so we match
896 * the same conf. Good planning (no, pure luck ;)
899 if (last_walk->matched == htaccess_conf) {
900 now_merged = last_walk->merged;
906 /* We fell out of sync. This is our own copy of walked,
907 * so truncate the remaining matches and reset
910 cache->walked->nelts -= matches;
915 now_merged = ap_merge_per_dir_configs(r->pool,
920 now_merged = htaccess_conf;
923 last_walk = (walk_walked_t*)apr_array_push(cache->walked);
924 last_walk->matched = htaccess_conf;
925 last_walk->merged = now_merged;
927 } while (0); /* Only one htaccess, not a real loop */
929 /* That temporary trailing slash was useful, now drop it.
932 r->filename[--filename_len] = '\0';
935 /* Time for all good things to come to an end?
937 if (!r->path_info || !*r->path_info) {
941 /* Now it's time for the next segment...
942 * We will assume the next element is an end node, and fix it up
943 * below as necessary...
946 seg_name = r->filename + filename_len;
947 delim = strchr(r->path_info + (*r->path_info == '/' ? 1 : 0), '/');
949 size_t path_info_len = delim - r->path_info;
951 memcpy(seg_name, r->path_info, path_info_len + 1);
952 filename_len += path_info_len;
953 r->path_info = delim;
957 size_t path_info_len = strlen(r->path_info);
958 memcpy(seg_name, r->path_info, path_info_len + 1);
959 filename_len += path_info_len;
960 r->path_info += path_info_len;
962 if (*seg_name == '/')
965 /* If nothing remained but a '/' string, we are finished
971 /* First optimization;
972 * If...we knew r->filename was a file, and
973 * if...we have strict (case-sensitive) filenames, or
974 * we know the canonical_filename matches to _this_ name, and
975 * if...we have allowed symlinks
976 * skip the lstat and dummy up an APR_DIR value for thisinfo.
978 if (r->finfo.filetype
979 #ifdef CASE_BLIND_FILESYSTEM
980 && (filename_len <= canonical_len)
982 && ((opts.opts & (OPT_SYM_OWNER | OPT_SYM_LINKS)) == OPT_SYM_LINKS))
985 thisinfo.filetype = APR_DIR;
990 /* We choose apr_lstat here, rather that apr_stat, so that we
991 * capture this path object rather than its target. We will
992 * replace the info with our target's info below. We especially
993 * want the name of this 'link' object, not the name of its
994 * target, if we are fixing the filename case/resolving aliases.
996 rv = apr_lstat(&thisinfo, r->filename,
997 APR_FINFO_MIN | APR_FINFO_NAME, r->pool);
999 if (APR_STATUS_IS_ENOENT(rv)) {
1000 /* Nothing? That could be nice. But our directory
1003 thisinfo.filetype = APR_NOFILE;
1006 else if (APR_STATUS_IS_EACCES(rv)) {
1007 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1008 "access to %s denied", r->uri);
1009 return r->status = HTTP_FORBIDDEN;
1011 else if ((rv != APR_SUCCESS && rv != APR_INCOMPLETE)
1012 || !(thisinfo.valid & APR_FINFO_TYPE)) {
1013 /* If we hit ENOTDIR, we must have over-optimized, deny
1014 * rather than assume not found.
1016 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1017 "access to %s failed", r->uri);
1018 return r->status = HTTP_FORBIDDEN;
1021 if ((res = check_safe_file(r))) {
1026 /* Fix up the path now if we have a name, and they don't agree
1028 if ((thisinfo.valid & APR_FINFO_NAME)
1029 && strcmp(seg_name, thisinfo.name)) {
1030 /* TODO: provide users an option that an internal/external
1031 * redirect is required here? We need to walk the URI and
1032 * filename in tandem to properly correlate these.
1034 strcpy(seg_name, thisinfo.name);
1035 filename_len = strlen(r->filename);
1038 if (thisinfo.filetype == APR_LNK) {
1039 /* Is this a possibly acceptable symlink?
1041 if ((res = resolve_symlink(r->filename, &thisinfo,
1042 opts.opts, r->pool)) != OK) {
1043 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1044 "Symbolic link not allowed: %s",
1046 return r->status = res;
1049 /* Ok, we are done with the link's info, test the real target
1051 if (thisinfo.filetype == APR_REG) {
1052 /* That was fun, nothing left for us here
1056 else if (thisinfo.filetype != APR_DIR) {
1057 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1058 "symlink doesn't point to a file or "
1061 return r->status = HTTP_FORBIDDEN;
1066 } while (thisinfo.filetype == APR_DIR);
1068 /* If we have _not_ optimized, this is the time to recover
1069 * the final stat result.
1071 if (!r->finfo.filetype || r->finfo.filetype == APR_LNK) {
1072 r->finfo = thisinfo;
1075 /* Now splice the saved path_info back onto any new path_info
1077 if (save_path_info) {
1078 if (r->path_info && *r->path_info) {
1079 r->path_info = ap_make_full_path(r->pool, r->path_info,
1083 r->path_info = save_path_info;
1088 * Now we'll deal with the regexes, note we pick up sec_idx
1089 * where we left off (we gave up after we hit entry_core->r)
1091 for (; sec_idx < num_sec; ++sec_idx) {
1093 core_dir_config *entry_core;
1094 entry_core = ap_get_module_config(sec_ent[sec_idx], &core_module);
1096 if (!entry_core->r) {
1100 if (ap_regexec(entry_core->r, r->filename, 0, NULL, REG_NOTEOL)) {
1104 /* If we haven't already continue'd above, we have a match.
1106 * Calculate our full-context core opts & override.
1108 core_opts_merge(sec_ent[sec_idx], &opts);
1110 /* If we merged this same section last time, reuse it
1113 if (last_walk->matched == sec_ent[sec_idx]) {
1114 now_merged = last_walk->merged;
1120 /* We fell out of sync. This is our own copy of walked,
1121 * so truncate the remaining matches and reset remaining.
1123 cache->walked->nelts -= matches;
1128 now_merged = ap_merge_per_dir_configs(r->pool,
1133 now_merged = sec_ent[sec_idx];
1136 last_walk = (walk_walked_t*)apr_array_push(cache->walked);
1137 last_walk->matched = sec_ent[sec_idx];
1138 last_walk->merged = now_merged;
1141 /* Whoops - everything matched in sequence, but the original walk
1142 * found some additional matches. Truncate them.
1145 cache->walked->nelts -= matches;
1149 /* It seems this shouldn't be needed anymore. We translated the
1150 x symlink above into a real resource, and should have died up there.
1151 x Even if we keep this, it needs more thought (maybe an r->file_is_symlink)
1152 x perhaps it should actually happen in file_walk, so we catch more
1153 x obscure cases in autoindex sub requests, etc.
1155 x * Symlink permissions are determined by the parent. If the request is
1156 x * for a directory then applying the symlink test here would use the
1157 x * permissions of the directory as opposed to its parent. Consider a
1158 x * symlink pointing to a dir with a .htaccess disallowing symlinks. If
1159 x * you access /symlink (or /symlink/) you would get a 403 without this
1160 x * APR_DIR test. But if you accessed /symlink/index.html, for example,
1161 x * you would *not* get the 403.
1163 x if (r->finfo.filetype != APR_DIR
1164 x && (res = resolve_symlink(r->filename, r->info, ap_allow_options(r),
1166 x ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1167 x "Symbolic link not allowed: %s", r->filename);
1172 /* Save future sub-requestors much angst in processing
1173 * this subrequest. If dir_walk couldn't canonicalize
1174 * the file path, nothing can.
1176 r->canonical_filename = r->filename;
1178 if (r->finfo.filetype == APR_DIR) {
1179 cache->cached = r->filename;
1182 cache->cached = ap_make_dirstr_parent(r->pool, r->filename);
1185 cache->dir_conf_tested = sec_ent;
1186 cache->dir_conf_merged = r->per_dir_config;
1188 /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
1189 * and note the end result to (potentially) skip this step next time.
1192 r->per_dir_config = ap_merge_per_dir_configs(r->pool,
1196 cache->per_dir_result = r->per_dir_config;
1202 AP_DECLARE(int) ap_location_walk(request_rec *r)
1204 ap_conf_vector_t *now_merged = NULL;
1205 core_server_config *sconf = ap_get_module_config(r->server->module_config,
1207 ap_conf_vector_t **sec_ent = (ap_conf_vector_t **)sconf->sec_url->elts;
1208 int num_sec = sconf->sec_url->nelts;
1209 walk_cache_t *cache;
1210 const char *entry_uri;
1212 /* No tricks here, there are no <Locations > to parse in this vhost.
1213 * We won't destroy the cache, just in case _this_ redirect is later
1214 * redirected again to a vhost with <Location > blocks to optimize.
1220 cache = prep_walk_cache(AP_NOTE_LOCATION_WALK, r);
1222 /* Location and LocationMatch differ on their behaviour w.r.t. multiple
1223 * slashes. Location matches multiple slashes with a single slash,
1224 * LocationMatch doesn't. An exception, for backwards brokenness is
1225 * absoluteURIs... in which case neither match multiple slashes.
1227 if (r->uri[0] != '/') {
1231 char *uri = apr_pstrdup(r->pool, r->uri);
1236 /* If we have an cache->cached location that matches r->uri,
1237 * and the vhost's list of locations hasn't changed, we can skip
1238 * rewalking the location_walk entries.
1241 && (cache->dir_conf_tested == sec_ent)
1242 && (strcmp(entry_uri, cache->cached) == 0)) {
1243 /* Well this looks really familiar! If our end-result (per_dir_result)
1244 * didn't change, we have absolutely nothing to do :)
1245 * Otherwise (as is the case with most dir_merged/file_merged requests)
1246 * we must merge our dir_conf_merged onto this new r->per_dir_config.
1248 if (r->per_dir_config == cache->per_dir_result) {
1252 if (r->per_dir_config == cache->dir_conf_merged) {
1253 r->per_dir_config = cache->per_dir_result;
1257 if (cache->walked->nelts) {
1258 now_merged = ((walk_walked_t*)cache->walked->elts)
1259 [cache->walked->nelts - 1].merged;
1263 /* We start now_merged from NULL since we want to build
1264 * a locations list that can be merged to any vhost.
1267 int matches = cache->walked->nelts;
1268 walk_walked_t *last_walk = (walk_walked_t*)cache->walked->elts;
1269 cache->cached = entry_uri;
1271 /* Go through the location entries, and check for matches.
1272 * We apply the directive sections in given order, we should
1273 * really try them with the most general first.
1275 for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
1277 core_dir_config *entry_core;
1278 entry_core = ap_get_module_config(sec_ent[sec_idx], &core_module);
1280 /* ### const strlen can be optimized in location config parsing */
1281 len = strlen(entry_core->d);
1283 /* Test the regex, fnmatch or string as appropriate.
1284 * If it's a strcmp, and the <Location > pattern was
1285 * not slash terminated, then this uri must be slash
1286 * terminated (or at the end of the string) to match.
1289 ? ap_regexec(entry_core->r, r->uri, 0, NULL, 0)
1290 : (entry_core->d_is_fnmatch
1291 ? apr_fnmatch(entry_core->d, cache->cached, FNM_PATHNAME)
1292 : (strncmp(entry_core->d, cache->cached, len)
1293 || (entry_core->d[len - 1] != '/'
1294 && cache->cached[len] != '/'
1295 && cache->cached[len] != '\0')))) {
1299 /* If we merged this same section last time, reuse it
1302 if (last_walk->matched == sec_ent[sec_idx]) {
1303 now_merged = last_walk->merged;
1309 /* We fell out of sync. This is our own copy of walked,
1310 * so truncate the remaining matches and reset remaining.
1312 cache->walked->nelts -= matches;
1317 now_merged = ap_merge_per_dir_configs(r->pool,
1322 now_merged = sec_ent[sec_idx];
1325 last_walk = (walk_walked_t*)apr_array_push(cache->walked);
1326 last_walk->matched = sec_ent[sec_idx];
1327 last_walk->merged = now_merged;
1330 /* Whoops - everything matched in sequence, but the original walk
1331 * found some additional matches. Truncate them.
1334 cache->walked->nelts -= matches;
1338 cache->dir_conf_tested = sec_ent;
1339 cache->dir_conf_merged = r->per_dir_config;
1341 /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
1342 * and note the end result to (potentially) skip this step next time.
1345 r->per_dir_config = ap_merge_per_dir_configs(r->pool,
1349 cache->per_dir_result = r->per_dir_config;
1354 AP_DECLARE(int) ap_file_walk(request_rec *r)
1356 ap_conf_vector_t *now_merged = NULL;
1357 core_dir_config *dconf = ap_get_module_config(r->per_dir_config,
1359 ap_conf_vector_t **sec_ent = (ap_conf_vector_t **)dconf->sec_file->elts;
1360 int num_sec = dconf->sec_file->nelts;
1361 walk_cache_t *cache;
1362 const char *test_file;
1364 /* To allow broken modules to proceed, we allow missing filenames to pass.
1365 * We will catch it later if it's heading for the core handler.
1366 * directory_walk already posted an INFO note for module debugging.
1368 if (r->filename == NULL) {
1372 cache = prep_walk_cache(AP_NOTE_FILE_WALK, r);
1374 /* No tricks here, there are just no <Files > to parse in this context.
1375 * We won't destroy the cache, just in case _this_ redirect is later
1376 * redirected again to a context containing the same or similar <Files >.
1382 /* Get the basename .. and copy for the cache just
1383 * in case r->filename is munged by another module
1385 test_file = strrchr(r->filename, '/');
1386 if (test_file == NULL) {
1387 test_file = apr_pstrdup(r->pool, r->filename);
1390 test_file = apr_pstrdup(r->pool, ++test_file);
1393 /* If we have an cache->cached file name that matches test_file,
1394 * and the directory's list of file sections hasn't changed, we
1395 * can skip rewalking the file_walk entries.
1398 && (cache->dir_conf_tested == sec_ent)
1399 && (strcmp(test_file, cache->cached) == 0)) {
1400 /* Well this looks really familiar! If our end-result (per_dir_result)
1401 * didn't change, we have absolutely nothing to do :)
1402 * Otherwise (as is the case with most dir_merged requests)
1403 * we must merge our dir_conf_merged onto this new r->per_dir_config.
1405 if (r->per_dir_config == cache->per_dir_result) {
1409 if (r->per_dir_config == cache->dir_conf_merged) {
1410 r->per_dir_config = cache->per_dir_result;
1414 if (cache->walked->nelts) {
1415 now_merged = ((walk_walked_t*)cache->walked->elts)
1416 [cache->walked->nelts - 1].merged;
1420 /* We start now_merged from NULL since we want to build
1421 * a file section list that can be merged to any dir_walk.
1424 int matches = cache->walked->nelts;
1425 walk_walked_t *last_walk = (walk_walked_t*)cache->walked->elts;
1426 cache->cached = test_file;
1428 /* Go through the location entries, and check for matches.
1429 * We apply the directive sections in given order, we should
1430 * really try them with the most general first.
1432 for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
1434 core_dir_config *entry_core;
1435 entry_core = ap_get_module_config(sec_ent[sec_idx], &core_module);
1438 ? ap_regexec(entry_core->r, cache->cached , 0, NULL, 0)
1439 : (entry_core->d_is_fnmatch
1440 ? apr_fnmatch(entry_core->d, cache->cached, FNM_PATHNAME)
1441 : strcmp(entry_core->d, cache->cached))) {
1445 /* If we merged this same section last time, reuse it
1448 if (last_walk->matched == sec_ent[sec_idx]) {
1449 now_merged = last_walk->merged;
1455 /* We fell out of sync. This is our own copy of walked,
1456 * so truncate the remaining matches and reset remaining.
1458 cache->walked->nelts -= matches;
1463 now_merged = ap_merge_per_dir_configs(r->pool,
1468 now_merged = sec_ent[sec_idx];
1471 last_walk = (walk_walked_t*)apr_array_push(cache->walked);
1472 last_walk->matched = sec_ent[sec_idx];
1473 last_walk->merged = now_merged;
1476 /* Whoops - everything matched in sequence, but the original walk
1477 * found some additional matches. Truncate them.
1480 cache->walked->nelts -= matches;
1484 cache->dir_conf_tested = sec_ent;
1485 cache->dir_conf_merged = r->per_dir_config;
1487 /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
1488 * and note the end result to (potentially) skip this step next time.
1491 r->per_dir_config = ap_merge_per_dir_configs(r->pool,
1495 cache->per_dir_result = r->per_dir_config;
1500 /*****************************************************************
1502 * The sub_request mechanism.
1504 * Fns to look up a relative URI from, e.g., a map file or SSI document.
1505 * These do all access checks, etc., but don't actually run the transaction
1506 * ... use run_sub_req below for that. Also, be sure to use destroy_sub_req
1507 * as appropriate if you're likely to be creating more than a few of these.
1508 * (An early Apache version didn't destroy the sub_reqs used in directory
1509 * indexing. The result, when indexing a directory with 800-odd files in
1510 * it, was massively excessive storage allocation).
1512 * Note more manipulation of protocol-specific vars in the request
1516 static request_rec *make_sub_request(const request_rec *r,
1517 ap_filter_t *next_filter)
1522 apr_pool_create(&rrp, r->pool);
1523 rnew = apr_pcalloc(rrp, sizeof(request_rec));
1526 rnew->hostname = r->hostname;
1527 rnew->request_time = r->request_time;
1528 rnew->connection = r->connection;
1529 rnew->server = r->server;
1531 rnew->request_config = ap_create_request_config(rnew->pool);
1533 /* Start a clean config from this subrequest's vhost. Optimization in
1534 * Location/File/Dir walks from the parent request assure that if the
1535 * config blocks of the subrequest match the parent request, no merges
1536 * will actually occur (and generally a minimal number of merges are
1537 * required, even if the parent and subrequest aren't quite identical.)
1539 rnew->per_dir_config = r->server->lookup_defaults;
1541 rnew->htaccess = r->htaccess;
1542 rnew->allowed_methods = ap_make_method_list(rnew->pool, 2);
1544 /* make a copy of the allowed-methods list */
1545 ap_copy_method_list(rnew->allowed_methods, r->allowed_methods);
1547 /* start with the same set of output filters */
1549 /* while there are no input filters for a subrequest, we will
1550 * try to insert some, so if we don't have valid data, the code
1553 rnew->input_filters = r->input_filters;
1554 rnew->proto_input_filters = r->proto_input_filters;
1555 rnew->output_filters = next_filter;
1556 rnew->proto_output_filters = r->proto_output_filters;
1557 ap_add_output_filter_handle(ap_subreq_core_filter_handle,
1558 NULL, rnew, rnew->connection);
1561 /* If NULL - we are expecting to be internal_fast_redirect'ed
1562 * to this subrequest - or this request will never be invoked.
1563 * Ignore the original request filter stack entirely, and
1564 * drill the input and output stacks back to the connection.
1566 rnew->proto_input_filters = r->proto_input_filters;
1567 rnew->proto_output_filters = r->proto_output_filters;
1569 rnew->input_filters = r->proto_input_filters;
1570 rnew->output_filters = r->proto_output_filters;
1573 /* no input filters for a subrequest */
1575 ap_set_sub_req_protocol(rnew, r);
1577 /* We have to run this after we fill in sub req vars,
1578 * or the r->main pointer won't be setup
1580 ap_run_create_request(rnew);
1585 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_sub_req_output_filter(ap_filter_t *f,
1586 apr_bucket_brigade *bb)
1588 apr_bucket *e = APR_BRIGADE_LAST(bb);
1590 if (APR_BUCKET_IS_EOS(e)) {
1591 apr_bucket_delete(e);
1594 if (!APR_BRIGADE_EMPTY(bb)) {
1595 return ap_pass_brigade(f->next, bb);
1602 AP_DECLARE(int) ap_some_auth_required(request_rec *r)
1604 /* Is there a require line configured for the type of *this* req? */
1606 const apr_array_header_t *reqs_arr = ap_requires(r);
1614 reqs = (require_line *) reqs_arr->elts;
1616 for (i = 0; i < reqs_arr->nelts; ++i) {
1617 if (reqs[i].method_mask & (AP_METHOD_BIT << r->method_number)) {
1626 AP_DECLARE(request_rec *) ap_sub_req_method_uri(const char *method,
1627 const char *new_file,
1628 const request_rec *r,
1629 ap_filter_t *next_filter)
1635 rnew = make_sub_request(r, next_filter);
1637 /* would be nicer to pass "method" to ap_set_sub_req_protocol */
1638 rnew->method = method;
1639 rnew->method_number = ap_method_number_of(method);
1641 if (new_file[0] == '/') {
1642 ap_parse_uri(rnew, new_file);
1645 udir = ap_make_dirstr_parent(rnew->pool, r->uri);
1646 udir = ap_escape_uri(rnew->pool, udir); /* re-escape it */
1647 ap_parse_uri(rnew, ap_make_full_path(rnew->pool, udir, new_file));
1650 * If the content can be served by the quick_handler, we can
1651 * safely bypass request_internal processing.
1653 res = ap_run_quick_handler(rnew, 1);
1656 if ((res = ap_process_request_internal(rnew))) {
1664 AP_DECLARE(request_rec *) ap_sub_req_lookup_uri(const char *new_file,
1665 const request_rec *r,
1666 ap_filter_t *next_filter)
1668 return ap_sub_req_method_uri("GET", new_file, r, next_filter);
1671 AP_DECLARE(request_rec *) ap_sub_req_lookup_dirent(const apr_finfo_t *dirent,
1672 const request_rec *r,
1674 ap_filter_t *next_filter)
1681 rnew = make_sub_request(r, next_filter);
1683 /* Special case: we are looking at a relative lookup in the same directory.
1684 * This is 100% safe, since dirent->name just came from the filesystem.
1686 if (r->path_info && *r->path_info) {
1687 /* strip path_info off the end of the uri to keep it in sync
1688 * with r->filename, which has already been stripped by directory_walk,
1689 * merge the dirent->name, and then, if the caller wants us to remerge
1690 * the original path info, do so. Note we never fix the path_info back
1691 * to r->filename, since dir_walk would do so (but we don't expect it
1692 * to happen in the usual cases)
1694 udir = apr_pstrdup(rnew->pool, r->uri);
1695 udir[ap_find_path_info(udir, r->path_info)] = '\0';
1696 udir = ap_make_dirstr_parent(rnew->pool, udir);
1698 rnew->uri = ap_make_full_path(rnew->pool, udir, dirent->name);
1699 if (subtype == AP_SUBREQ_MERGE_ARGS) {
1700 rnew->uri = ap_make_full_path(rnew->pool, rnew->uri, r->path_info + 1);
1701 rnew->path_info = apr_pstrdup(rnew->pool, r->path_info);
1705 udir = ap_make_dirstr_parent(rnew->pool, r->uri);
1706 rnew->uri = ap_make_full_path(rnew->pool, udir, dirent->name);
1709 fdir = ap_make_dirstr_parent(rnew->pool, r->filename);
1710 rnew->filename = ap_make_full_path(rnew->pool, fdir, dirent->name);
1711 if (r->canonical_filename == r->filename) {
1712 rnew->canonical_filename = rnew->filename;
1715 /* XXX This is now less relevant; we will do a full location walk
1716 * these days for this case. Preserve the apr_stat results, and
1717 * perhaps we also tag that symlinks were tested and/or found for
1720 rnew->per_dir_config = r->server->lookup_defaults;
1722 if ((dirent->valid & APR_FINFO_MIN) != APR_FINFO_MIN) {
1724 * apr_dir_read isn't very complete on this platform, so
1725 * we need another apr_lstat (or simply apr_stat if we allow
1726 * all symlinks here.) If this is an APR_LNK that resolves
1727 * to an APR_DIR, then we will rerun everything anyways...
1728 * this should be safe.
1731 if (ap_allow_options(rnew) & OPT_SYM_LINKS) {
1732 if (((rv = apr_stat(&rnew->finfo, rnew->filename,
1733 APR_FINFO_MIN, rnew->pool)) != APR_SUCCESS)
1734 && (rv != APR_INCOMPLETE)) {
1735 rnew->finfo.filetype = 0;
1739 if (((rv = apr_lstat(&rnew->finfo, rnew->filename,
1740 APR_FINFO_MIN, rnew->pool)) != APR_SUCCESS)
1741 && (rv != APR_INCOMPLETE)) {
1742 rnew->finfo.filetype = 0;
1747 memcpy(&rnew->finfo, dirent, sizeof(apr_finfo_t));
1750 if (rnew->finfo.filetype == APR_LNK) {
1752 * Resolve this symlink. We should tie this back to dir_walk's cache
1754 if ((res = resolve_symlink(rnew->filename, &rnew->finfo,
1755 ap_allow_options(rnew), rnew->pool))
1762 if (rnew->finfo.filetype == APR_DIR) {
1763 /* ap_make_full_path overallocated the buffers
1764 * by one character to help us out here.
1766 strcpy(rnew->filename + strlen(rnew->filename), "/");
1767 if (!rnew->path_info || !*rnew->path_info) {
1768 strcpy(rnew->uri + strlen(rnew->uri ), "/");
1772 /* fill in parsed_uri values
1774 if (r->args && *r->args && (subtype == AP_SUBREQ_MERGE_ARGS)) {
1775 ap_parse_uri(rnew, apr_pstrcat(r->pool, rnew->uri, "?",
1779 ap_parse_uri(rnew, rnew->uri);
1782 if ((res = ap_process_request_internal(rnew))) {
1789 AP_DECLARE(request_rec *) ap_sub_req_lookup_file(const char *new_file,
1790 const request_rec *r,
1791 ap_filter_t *next_filter)
1798 rnew = make_sub_request(r, next_filter);
1800 fdir = ap_make_dirstr_parent(rnew->pool, r->filename);
1801 fdirlen = strlen(fdir);
1803 /* Translate r->filename, if it was canonical, it stays canonical
1805 if (r->canonical_filename == r->filename) {
1806 rnew->canonical_filename = (char*)(1);
1809 if (apr_filepath_merge(&rnew->filename, fdir, new_file,
1810 APR_FILEPATH_TRUENAME, rnew->pool) != APR_SUCCESS) {
1811 rnew->status = HTTP_FORBIDDEN;
1815 if (rnew->canonical_filename) {
1816 rnew->canonical_filename = rnew->filename;
1820 * Check for a special case... if there are no '/' characters in new_file
1821 * at all, and the path was the same, then we are looking at a relative
1822 * lookup in the same directory. Fixup the URI to match.
1825 if (strncmp(rnew->filename, fdir, fdirlen) == 0
1826 && rnew->filename[fdirlen]
1827 && ap_strchr_c(rnew->filename + fdirlen, '/') == NULL) {
1829 if (ap_allow_options(rnew) & OPT_SYM_LINKS) {
1830 if (((rv = apr_stat(&rnew->finfo, rnew->filename,
1831 APR_FINFO_MIN, rnew->pool)) != APR_SUCCESS)
1832 && (rv != APR_INCOMPLETE)) {
1833 rnew->finfo.filetype = 0;
1837 if (((rv = apr_lstat(&rnew->finfo, rnew->filename,
1838 APR_FINFO_MIN, rnew->pool)) != APR_SUCCESS)
1839 && (rv != APR_INCOMPLETE)) {
1840 rnew->finfo.filetype = 0;
1844 if (r->uri && *r->uri) {
1845 char *udir = ap_make_dirstr_parent(rnew->pool, r->uri);
1846 rnew->uri = ap_make_full_path(rnew->pool, udir,
1847 rnew->filename + fdirlen);
1848 ap_parse_uri(rnew, rnew->uri); /* fill in parsed_uri values */
1851 ap_parse_uri(rnew, new_file); /* fill in parsed_uri values */
1852 rnew->uri = apr_pstrdup(rnew->pool, "");
1856 /* XXX: @@@: What should be done with the parsed_uri values?
1857 * We would be better off stripping down to the 'common' elements
1858 * of the path, then reassembling the URI as best as we can.
1860 ap_parse_uri(rnew, new_file); /* fill in parsed_uri values */
1862 * XXX: this should be set properly like it is in the same-dir case
1863 * but it's actually sometimes to impossible to do it... because the
1864 * file may not have a uri associated with it -djg
1866 rnew->uri = apr_pstrdup(rnew->pool, "");
1869 if ((res = ap_process_request_internal(rnew))) {
1876 AP_DECLARE(int) ap_run_sub_req(request_rec *r)
1878 int retval = DECLINED;
1879 /* Run the quick handler if the subrequest is not a dirent or file
1882 if (!(r->filename && r->finfo.filetype)) {
1883 retval = ap_run_quick_handler(r, 0);
1886 retval = ap_invoke_handler(r);
1887 if (retval == DONE) {
1891 ap_finalize_sub_req_protocol(r);
1895 AP_DECLARE(void) ap_destroy_sub_req(request_rec *r)
1897 /* Reclaim the space */
1898 apr_pool_destroy(r->pool);
1902 * Function to set the r->mtime field to the specified value if it's later
1903 * than what's already there.
1905 AP_DECLARE(void) ap_update_mtime(request_rec *r, apr_time_t dependency_mtime)
1907 if (r->mtime < dependency_mtime) {
1908 r->mtime = dependency_mtime;
1913 * Is it the initial main request, which we only get *once* per HTTP request?
1915 AP_DECLARE(int) ap_is_initial_req(request_rec *r)
1917 return (r->main == NULL) /* otherwise, this is a sub-request */
1918 && (r->prev == NULL); /* otherwise, this is an internal redirect */