mod_file_cache.c revision 2fc50921b88defeb7127985dfe4b4130175e069e
beaad6ac31022179c44d88536811e9ccd9425696nd/* ====================================================================
beaad6ac31022179c44d88536811e9ccd9425696nd * The Apache Software License, Version 1.1
beaad6ac31022179c44d88536811e9ccd9425696nd *
db878466c5e95073429dda0bdd001f45e9486e16fielding * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
db878466c5e95073429dda0bdd001f45e9486e16fielding * reserved.
db878466c5e95073429dda0bdd001f45e9486e16fielding *
db878466c5e95073429dda0bdd001f45e9486e16fielding * Redistribution and use in source and binary forms, with or without
db878466c5e95073429dda0bdd001f45e9486e16fielding * modification, are permitted provided that the following conditions
db878466c5e95073429dda0bdd001f45e9486e16fielding * are met:
beaad6ac31022179c44d88536811e9ccd9425696nd *
beaad6ac31022179c44d88536811e9ccd9425696nd * 1. Redistributions of source code must retain the above copyright
beaad6ac31022179c44d88536811e9ccd9425696nd * notice, this list of conditions and the following disclaimer.
beaad6ac31022179c44d88536811e9ccd9425696nd *
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 *
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 *
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 *
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.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd *
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 *
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
7e68fce3cbd2246164e045a51ecd77f9f26680ednd * <http://www.apache.org/>.
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd */
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd/*
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * Author: mod_file_cache by Bill Stoddard <stoddard@apache.org>
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd * Based on mod_mmap_static by Dean Gaudet <dgaudet@arctic.org>
f1110149b5b6c28ecc39d1958d98ad9cfa8e41f3nd *
f1110149b5b6c28ecc39d1958d98ad9cfa8e41f3nd * v0.01: initial implementation
7e68fce3cbd2246164e045a51ecd77f9f26680ednd */
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd/*
f5f948a91315652103ddae08be22d76f63ba96d4nd Documentation:
8cfbcde8e416fd60132dd4324c42a5098da156cfnd
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
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
7e68fce3cbd2246164e045a51ecd77f9f26680ednd cachefile /path/to/file1
7e68fce3cbd2246164e045a51ecd77f9f26680ednd cachefile /path/to/file2
7e68fce3cbd2246164e045a51ecd77f9f26680ednd ...
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
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
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
7e68fce3cbd2246164e045a51ecd77f9f26680ednd There's no such thing as inheriting these files across vhosts or
7e68fce3cbd2246164e045a51ecd77f9f26680ednd whatever... place the directives in the main server only.
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd Known problems:
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
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*/
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "apr.h"
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#if !(APR_HAS_SENDFILE || APR_HAS_MMAP)
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#error mod_file_cache only works on systems with APR_HAS_SENDFILE or APR_HAS_MMAP
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#endif
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "apr_mmap.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "apr_strings.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "apr_hash.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "apr_buckets.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#define APR_WANT_STRFUNC
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "apr_want.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#if APR_HAVE_SYS_TYPES_H
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include <sys/types.h>
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#endif
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#define CORE_PRIVATE
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "httpd.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "http_config.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "http_log.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "http_protocol.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "http_request.h"
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#include "http_core.h"
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd
7e68fce3cbd2246164e045a51ecd77f9f26680edndmodule AP_MODULE_DECLARE_DATA file_cache_module;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680edndtypedef struct {
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#if APR_HAS_SENDFILE
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd apr_file_t *file;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#endif
7e68fce3cbd2246164e045a51ecd77f9f26680ednd const char *filename;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd apr_finfo_t finfo;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd int is_mmapped;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#if APR_HAS_MMAP
7e68fce3cbd2246164e045a51ecd77f9f26680ednd apr_mmap_t *mm;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#endif
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd char mtimestr[APR_RFC822_DATE_LEN];
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd char sizestr[21]; /* big enough to hold any 64-bit file size + null */
7e68fce3cbd2246164e045a51ecd77f9f26680ednd} a_file;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680edndtypedef struct {
7e68fce3cbd2246164e045a51ecd77f9f26680ednd apr_hash_t *fileht;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd} a_server_config;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680edndstatic void *create_server_config(apr_pool_t *p, server_rec *s)
7e68fce3cbd2246164e045a51ecd77f9f26680ednd{
7e68fce3cbd2246164e045a51ecd77f9f26680ednd a_server_config *sconf = apr_palloc(p, sizeof(*sconf));
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd sconf->fileht = apr_hash_make(p);
7e68fce3cbd2246164e045a51ecd77f9f26680ednd return sconf;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd}
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680edndstatic apr_status_t cleanup_file_cache(void *sconfv)
66e48875fca2df6669dab4f2690ee36c6913c0ffnd{
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd a_server_config *sconf = sconfv;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd apr_pool_t *p = apr_hash_pool_get(sconf->fileht);
7e68fce3cbd2246164e045a51ecd77f9f26680ednd a_file *file;
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd apr_hash_index_t *hi;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
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)) {
7e68fce3cbd2246164e045a51ecd77f9f26680ednd apr_hash_this(hi, NULL, NULL, (void **)&file);
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#if APR_HAS_MMAP
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd if (file->is_mmapped) {
7e68fce3cbd2246164e045a51ecd77f9f26680ednd apr_mmap_delete(file->mm);
7e68fce3cbd2246164e045a51ecd77f9f26680ednd }
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd#endif
b51bf223f42d43ca6b1b33c95124edcfa5a871a4nd#if APR_HAS_SENDFILE
f36af6370d0850bf789703e852778d8e8ceefd20nd if (!file->is_mmapped) {
b51bf223f42d43ca6b1b33c95124edcfa5a871a4nd apr_file_close(file->file);
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd }
7e68fce3cbd2246164e045a51ecd77f9f26680ednd#endif
7e68fce3cbd2246164e045a51ecd77f9f26680ednd }
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd return APR_SUCCESS;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd}
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26ndstatic void cache_the_file(cmd_parms *cmd, const char *filename, int mmap)
7e68fce3cbd2246164e045a51ecd77f9f26680ednd{
7e68fce3cbd2246164e045a51ecd77f9f26680ednd a_server_config *sconf;
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd a_file *new_file;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd a_file tmp;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd apr_file_t *fd = NULL;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd apr_status_t rc;
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd const char *fspec;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd
7e68fce3cbd2246164e045a51ecd77f9f26680ednd fspec = ap_server_root_relative(cmd->pool, filename);
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd if (!fspec) {
7e68fce3cbd2246164e045a51ecd77f9f26680ednd ap_log_error(APLOG_MARK, APLOG_WARNING, APR_EBADPATH, cmd->server,
7e68fce3cbd2246164e045a51ecd77f9f26680ednd "mod_file_cache: invalid file path "
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd "%s, skipping", filename);
7e68fce3cbd2246164e045a51ecd77f9f26680ednd return;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd }
7e68fce3cbd2246164e045a51ecd77f9f26680ednd if ((rc = apr_stat(&tmp.finfo, fspec, APR_FINFO_MIN,
29fb68cf24dbdb4985cbb4734cb6074ea4bbab26nd cmd->temp_pool)) != APR_SUCCESS) {
7e68fce3cbd2246164e045a51ecd77f9f26680ednd ap_log_error(APLOG_MARK, APLOG_WARNING, rc, cmd->server,
7e68fce3cbd2246164e045a51ecd77f9f26680ednd "mod_file_cache: unable to stat(%s), skipping", fspec);
7e68fce3cbd2246164e045a51ecd77f9f26680ednd return;
7e68fce3cbd2246164e045a51ecd77f9f26680ednd }
7e68fce3cbd2246164e045a51ecd77f9f26680ednd if (tmp.finfo.filetype != APR_REG) {
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);
return;
}
if (tmp.finfo.size > AP_MAX_SENDFILE) {
ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, cmd->server,
"mod_file_cache: %s is too large to cache, skipping", fspec);
return;
}
rc = apr_file_open(&fd, fspec, APR_READ | APR_BINARY | APR_XTHREAD,
APR_OS_DEFAULT, cmd->pool);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rc, cmd->server,
"mod_file_cache: unable to open(%s, O_RDONLY), skipping", fspec);
return;
}
apr_file_set_inherit(fd);
/* WooHoo, we have a file to put in the cache */
new_file = apr_pcalloc(cmd->pool, sizeof(a_file));
new_file->finfo = tmp.finfo;
#if APR_HAS_MMAP
if (mmap) {
apr_mmap_t *mm;
/* MMAPFile directive. MMAP'ing the file
* XXX: APR_HAS_LARGE_FILES issue; need to reject this request if
* size is greater than MAX(apr_size_t) (perhaps greater than 1M?).
*/
if ((rc = apr_mmap_create(&mm, fd, 0,
(apr_size_t)new_file->finfo.size,
APR_MMAP_READ, cmd->pool)) != APR_SUCCESS) {
apr_file_close(fd);
/* We want to cache an apr_mmap_t that's marked as "non-owner"
* to pass to each request so that mmap_setaside()'s call to
* apr_mmap_dup() will never try to move the apr_mmap_t to a
* different pool. This apr_mmap_t is already going to live
* longer than any request, but mmap_setaside() has no way to
* know that because it's allocated out of cmd->pool,
* which is disjoint from r->pool.
*/
apr_mmap_dup(&new_file->mm, mm, cmd->pool, 0);
ap_log_error(APLOG_MARK, APLOG_WARNING, rc, cmd->server,
"mod_file_cache: unable to mmap %s, skipping", filename);
return;
}
apr_file_close(fd);
new_file->is_mmapped = TRUE;
}
#endif
#if APR_HAS_SENDFILE
if (!mmap) {
/* CacheFile directive. Caching the file handle */
new_file->is_mmapped = FALSE;
new_file->file = fd;
}
#endif
new_file->filename = fspec;
apr_rfc822_date(new_file->mtimestr, new_file->finfo.mtime);
apr_snprintf(new_file->sizestr, sizeof new_file->sizestr, "%" APR_OFF_T_FMT, new_file->finfo.size);
sconf = ap_get_module_config(cmd->server->module_config, &file_cache_module);
apr_hash_set(sconf->fileht, new_file->filename, strlen(new_file->filename), new_file);
if (apr_hash_count(sconf->fileht) == 1) {
/* first one, register the cleanup */
apr_pool_cleanup_register(cmd->pool, sconf, cleanup_file_cache, apr_pool_cleanup_null);
}
}
static const char *cachefilehandle(cmd_parms *cmd, void *dummy, const char *filename)
{
#if APR_HAS_SENDFILE
cache_the_file(cmd, filename, 0);
#else
/* Sendfile not supported by this OS */
ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, cmd->server,
"mod_file_cache: unable to cache file: %s. Sendfile is not supported on this OS", filename);
#endif
return NULL;
}
static const char *cachefilemmap(cmd_parms *cmd, void *dummy, const char *filename)
{
#if APR_HAS_MMAP
cache_the_file(cmd, filename, 1);
#else
/* MMAP not supported by this OS */
ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, cmd->server,
"mod_file_cache: unable to cache file: %s. MMAP is not supported by this OS", filename);
#endif
return NULL;
}
static int file_cache_post_config(apr_pool_t *p, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
/* Hummm, anything to do here? */
return OK;
}
/* If it's one of ours, fill in r->finfo now to avoid extra stat()... this is a
* bit of a kludge, because we really want to run after core_translate runs.
*/
static int file_cache_xlat(request_rec *r)
{
a_server_config *sconf;
a_file *match;
int res;
sconf = ap_get_module_config(r->server->module_config, &file_cache_module);
/* we only operate when at least one cachefile directive was used */
if (!apr_hash_count(sconf->fileht)) {
return DECLINED;
}
res = ap_core_translate(r);
if (res != OK || !r->filename) {
return res;
}
/* search the cache */
match = (a_file *) apr_hash_get(sconf->fileht, r->filename, APR_HASH_KEY_STRING);
if (match == NULL)
return DECLINED;
/* pass search results to handler */
ap_set_module_config(r->request_config, &file_cache_module, match);
/* shortcircuit the get_path_info() stat() calls and stuff */
r->finfo = match->finfo;
return OK;
}
static int mmap_handler(request_rec *r, a_file *file)
{
#if APR_HAS_MMAP
conn_rec *c = r->connection;
apr_bucket *b;
apr_bucket_brigade *bb = apr_brigade_create(r->pool, c->bucket_alloc);
b = apr_bucket_mmap_create(file->mm, 0, (apr_size_t)file->finfo.size,
c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
b = apr_bucket_eos_create(c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
if (ap_pass_brigade(r->output_filters, bb) != APR_SUCCESS)
return HTTP_INTERNAL_SERVER_ERROR;
#endif
return OK;
}
static int sendfile_handler(request_rec *r, a_file *file)
{
#if APR_HAS_SENDFILE
conn_rec *c = r->connection;
apr_bucket *b;
apr_bucket_brigade *bb = apr_brigade_create(r->pool, c->bucket_alloc);
b = apr_bucket_file_create(file->file, 0, (apr_size_t)file->finfo.size,
r->pool, c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
b = apr_bucket_eos_create(c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
if (ap_pass_brigade(r->output_filters, bb) != APR_SUCCESS)
return HTTP_INTERNAL_SERVER_ERROR;
#endif
return OK;
}
static int file_cache_handler(request_rec *r)
{
a_file *match;
int errstatus;
int rc = OK;
/* XXX: not sure if this is right yet
* see comment in http_core.c:default_handler
*/
if (ap_strcmp_match(r->handler, "*/*")) {
return DECLINED;
}
/* we don't handle anything but GET */
if (r->method_number != M_GET) return DECLINED;
/* did xlat phase find the file? */
match = ap_get_module_config(r->request_config, &file_cache_module);
if (match == NULL) {
return DECLINED;
}
/* note that we would handle GET on this resource */
r->allowed |= (AP_METHOD_BIT << M_GET);
/* This handler has no use for a request body (yet), but we still
* need to read and discard it if the client sent one.
*/
if ((errstatus = ap_discard_request_body(r)) != OK)
return errstatus;
ap_update_mtime(r, match->finfo.mtime);
/* ap_set_last_modified() always converts the file mtime to a string
* which is slow. Accelerate the common case.
* ap_set_last_modified(r);
*/
{
apr_time_t mod_time;
char *datestr;
mod_time = ap_rationalize_mtime(r, r->mtime);
if (mod_time == match->finfo.mtime)
datestr = match->mtimestr;
else {
datestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
apr_rfc822_date(datestr, mod_time);
}
apr_table_setn(r->headers_out, "Last-Modified", datestr);
}
ap_set_etag(r);
if ((errstatus = ap_meets_conditions(r)) != OK) {
return errstatus;
}
/* ap_set_content_length() always converts the same number and never
* returns an error. Accelerate it.
*/
r->clength = match->finfo.size;
apr_table_setn(r->headers_out, "Content-Length", match->sizestr);
/* Call appropriate handler */
if (!r->header_only) {
if (match->is_mmapped == TRUE)
rc = mmap_handler(r, match);
else
rc = sendfile_handler(r, match);
}
return rc;
}
static command_rec file_cache_cmds[] =
{
AP_INIT_ITERATE("cachefile", cachefilehandle, NULL, RSRC_CONF,
"A space separated list of files to add to the file handle cache at config time"),
AP_INIT_ITERATE("mmapfile", cachefilemmap, NULL, RSRC_CONF,
"A space separated list of files to mmap at config time"),
{NULL}
};
static void register_hooks(apr_pool_t *p)
{
ap_hook_handler(file_cache_handler, NULL, NULL, APR_HOOK_LAST);
ap_hook_post_config(file_cache_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_translate_name(file_cache_xlat, NULL, NULL, APR_HOOK_MIDDLE);
/* This trick doesn't work apparently because the translate hooks
are single shot. If the core_hook returns OK, then our hook is
not called.
ap_hook_translate_name(file_cache_xlat, aszPre, NULL, APR_HOOK_MIDDLE);
*/
}
module AP_MODULE_DECLARE_DATA file_cache_module =
{
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
create_server_config, /* create per-server config structure */
NULL, /* merge per-server config structures */
file_cache_cmds, /* command handlers */
register_hooks /* register hooks */
};