mod_file_cache.c revision 2fc50921b88defeb7127985dfe4b4130175e069e
beaad6ac31022179c44d88536811e9ccd9425696nd/* ====================================================================
beaad6ac31022179c44d88536811e9ccd9425696nd * The Apache Software License, Version 1.1
db878466c5e95073429dda0bdd001f45e9486e16fielding * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
db878466c5e95073429dda0bdd001f45e9486e16fielding * reserved.
db878466c5e95073429dda0bdd001f45e9486e16fielding * Redistribution and use in source and binary forms, with or without
db878466c5e95073429dda0bdd001f45e9486e16fielding * modification, are permitted provided that the following conditions
beaad6ac31022179c44d88536811e9ccd9425696nd * 1. Redistributions of source code must retain the above copyright
beaad6ac31022179c44d88536811e9ccd9425696nd * notice, this list of conditions and the following disclaimer.
beaad6ac31022179c44d88536811e9ccd9425696nd * 2. Redistributions in binary form must reproduce the above copyright
beaad6ac31022179c44d88536811e9ccd9425696nd * notice, this list of conditions and the following disclaimer in
beaad6ac31022179c44d88536811e9ccd9425696nd * the documentation and/or other materials provided with the
beaad6ac31022179c44d88536811e9ccd9425696nd * distribution.
beaad6ac31022179c44d88536811e9ccd9425696nd * 3. The end-user documentation included with the redistribution,
01c674544bd4c211141bcd9fb09b96ffc18c6c3dnd * if any, must include the following acknowledgment:
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * "This product includes software developed by the
01c674544bd4c211141bcd9fb09b96ffc18c6c3dnd * Apache Software Foundation (http://www.apache.org/)."
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * Alternately, this acknowledgment may appear in the software itself,
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * if and wherever such third-party acknowledgments normally appear.
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * 4. The names "Apache" and "Apache Software Foundation" must
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * not be used to endorse or promote products derived from this
a8bc9b37495d26276b0e506d644a07f32ad95626rbowen * software without prior written permission. For written
a8bc9b37495d26276b0e506d644a07f32ad95626rbowen * permission, please contact apache@apache.org.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * 5. Products derived from this software may not be called "Apache",
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * nor may "Apache" appear in their name, without prior written
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * permission of the Apache Software Foundation.
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * SUCH DAMAGE.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * ====================================================================
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * This software consists of voluntary contributions made by many
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * individuals on behalf of the Apache Software Foundation. For more
66e48875fca2df6669dab4f2690ee36c6913c0ffnd * information on the Apache Software Foundation, please see
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * Author: mod_file_cache by Bill Stoddard <stoddard@apache.org>
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * Based on mod_mmap_static by Dean Gaudet <dgaudet@arctic.org>
f1110149b5b6c28ecc39d1958d98ad9cfa8e41f3nd * v0.01: initial implementation
f5f948a91315652103ddae08be22d76f63ba96d4nd Documentation:
8cfbcde8e416fd60132dd4324c42a5098da156cfnd Some sites have a set of static files that are really busy, and
7e68fce3cbd2246164e045a51ecd77f9f26680ednd change infrequently (or even on a regular schedule). Save time
7e68fce3cbd2246164e045a51ecd77f9f26680ednd by caching open handles to these files. This module, unlike
7e68fce3cbd2246164e045a51ecd77f9f26680ednd mod_mmap_static, caches open file handles, not file content.
f5f948a91315652103ddae08be22d76f63ba96d4nd On systems (like Windows) with heavy system call overhead and
7e68fce3cbd2246164e045a51ecd77f9f26680ednd that have an efficient sendfile implementation, caching file handles
7e68fce3cbd2246164e045a51ecd77f9f26680ednd offers several advantages over caching content. First, the file system
7e68fce3cbd2246164e045a51ecd77f9f26680ednd can manage the memory, allowing infrequently hit cached files to
7e68fce3cbd2246164e045a51ecd77f9f26680ednd be paged out. Second, since caching open handles does not consume
7e68fce3cbd2246164e045a51ecd77f9f26680ednd significant resources, it will be possible to enable an AutoLoadCache
7e68fce3cbd2246164e045a51ecd77f9f26680ednd feature where static files are dynamically loaded in the cache
7e68fce3cbd2246164e045a51ecd77f9f26680ednd as the server runs. On systems that have file change notification,
7e68fce3cbd2246164e045a51ecd77f9f26680ednd this module can be enhanced to automatically garbage collect
7e68fce3cbd2246164e045a51ecd77f9f26680ednd cached files that change on disk.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd This module should work on Unix systems that have sendfile. Place
7e68fce3cbd2246164e045a51ecd77f9f26680ednd cachefile directives into your configuration to direct files to
7e68fce3cbd2246164e045a51ecd77f9f26680ednd be cached.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd cachefile /path/to/file1
7e68fce3cbd2246164e045a51ecd77f9f26680ednd cachefile /path/to/file2
7e68fce3cbd2246164e045a51ecd77f9f26680ednd These files are only cached when the server is restarted, so if you
7e68fce3cbd2246164e045a51ecd77f9f26680ednd change the list, or if the files are changed, then you'll need to
7e68fce3cbd2246164e045a51ecd77f9f26680ednd restart the server.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd To reiterate that point: if the files are modified *in place*
7e68fce3cbd2246164e045a51ecd77f9f26680ednd without restarting the server you may end up serving requests that
7e68fce3cbd2246164e045a51ecd77f9f26680ednd are completely bogus. You should update files by unlinking the old
7e68fce3cbd2246164e045a51ecd77f9f26680ednd copy and putting a new copy in place.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd There's no such thing as inheriting these files across vhosts or
7e68fce3cbd2246164e045a51ecd77f9f26680ednd whatever... place the directives in the main server only.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd Known problems:
7e68fce3cbd2246164e045a51ecd77f9f26680ednd Don't use Alias or RewriteRule to move these files around... unless
f5f948a91315652103ddae08be22d76f63ba96d4nd you feel like paying for an extra stat() on each request. This is
7e68fce3cbd2246164e045a51ecd77f9f26680ednd a deficiency in the Apache API that will hopefully be solved some day.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd The file will be served out of the file handle cache, but there will be
7e68fce3cbd2246164e045a51ecd77f9f26680ednd an extra stat() that's a waste.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#error mod_file_cache only works on systems with APR_HAS_SENDFILE or APR_HAS_MMAP
7e68fce3cbd2246164e045a51ecd77f9f26680edndtypedef struct {
7e68fce3cbd2246164e045a51ecd77f9f26680ednd const char *filename;
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd char sizestr[21]; /* big enough to hold any 64-bit file size + null */
7e68fce3cbd2246164e045a51ecd77f9f26680edndtypedef struct {
7e68fce3cbd2246164e045a51ecd77f9f26680edndstatic void *create_server_config(apr_pool_t *p, server_rec *s)
66e48875fca2df6669dab4f2690ee36c6913c0ffnd /* Iterate over the file hash table and clean up each entry */
bdbf46e4950b6f633073f803486962e82c2f086and for (hi = apr_hash_first(p, sconf->fileht); hi; hi=apr_hash_next(hi)) {
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26ndstatic void cache_the_file(cmd_parms *cmd, const char *filename, int mmap)
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd const char *fspec;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd ap_log_error(APLOG_MARK, APLOG_WARNING, APR_EBADPATH, cmd->server,
7e68fce3cbd2246164e045a51ecd77f9f26680ednd "mod_file_cache: invalid file path "
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, cmd->server,
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd "mod_file_cache: %s isn't a regular file, skipping", fspec);
#if APR_HAS_MMAP
if (mmap) {
#if APR_HAS_SENDFILE
if (!mmap) {
#if APR_HAS_SENDFILE
return NULL;
#if APR_HAS_MMAP
return NULL;
return OK;
int res;
return DECLINED;
return res;
return DECLINED;
return OK;
#if APR_HAS_MMAP
apr_bucket *b;
c->bucket_alloc);
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
#if APR_HAS_SENDFILE
apr_bucket *b;
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
int errstatus;
* see comment in http_core.c:default_handler
return DECLINED;
return DECLINED;
return errstatus;
char *datestr;
ap_set_etag(r);
return errstatus;
if (!r->header_only) {
return rc;
{NULL}