mod_file_cache.c revision 032b8a34c3911bbc5ad5385ca40af65af273bff9
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen/* ====================================================================
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * Copyright (c) 1998-1999 The Apache Group. All rights reserved.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * Redistribution and use in source and binary forms, with or without
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * modification, are permitted provided that the following conditions
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * 1. Redistributions of source code must retain the above copyright
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * notice, this list of conditions and the following disclaimer.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * 2. Redistributions in binary form must reproduce the above copyright
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * notice, this list of conditions and the following disclaimer in
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * the documentation and/or other materials provided with the
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * distribution.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * 3. All advertising materials mentioning features or use of this
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * software must display the following acknowledgment:
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * "This product includes software developed by the Apache Group
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * for use in the Apache HTTP server project (http://www.apache.org/)."
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * 4. The names "Apache Server" and "Apache Group" must not be used to
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering * endorse or promote products derived from this software without
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * prior written permission. For written permission, please contact
a553fd32d14739b746a05ae502b8acc38e5f9a50Tom Gundersen * apache@apache.org.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * 5. Products derived from this software may not be called "Apache"
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * nor may "Apache" appear in their names without prior written
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * permission of the Apache Group.
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering * 6. Redistributions of any form whatsoever must retain the following
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering * acknowledgment:
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering * "This product includes software developed by the Apache Group
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * for use in the Apache HTTP server project (http://www.apache.org/)."
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * OF THE POSSIBILITY OF SUCH DAMAGE.
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering * ====================================================================
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt * This software consists of voluntary contributions made by many
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering * individuals on behalf of the Apache Group and was originally based
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering * on public domain software written at the National Center for
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering * Supercomputing Applications, University of Illinois, Urbana-Champaign.
fe8db0c5ee3365a2fc80ee7ebffa238f9a0a2ae2Tom Gundersen * For more information on the Apache Group and the Apache HTTP server
fe8db0c5ee3365a2fc80ee7ebffa238f9a0a2ae2Tom Gundersen * project, please see <http://www.apache.org/>.
85b5673b337048fa881a5afb1d00d1a7b95950fbTom Gundersen * Author: mod_file_cache by Bill Stoddard <stoddard@apache.org>
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen * Based on mod_mmap_static by Dean Gaudet <dgaudet@arctic.org>
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen * v0.01: initial implementation
fe8db0c5ee3365a2fc80ee7ebffa238f9a0a2ae2Tom Gundersen Documentation:
49699bac94d24b444274f91f85c82e6fad04d029Susant Sahani Some sites have a set of static files that are really busy, and
49699bac94d24b444274f91f85c82e6fad04d029Susant Sahani change infrequently (or even on a regular schedule). Save time
49699bac94d24b444274f91f85c82e6fad04d029Susant Sahani by caching open handles to these files. This module, unlike
49699bac94d24b444274f91f85c82e6fad04d029Susant Sahani mod_mmap_static, caches open file handles, not file content.
49699bac94d24b444274f91f85c82e6fad04d029Susant Sahani On systems (like Windows) with heavy system call overhead and
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering that have an efficient sendfile implementation, caching file handles
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering offers several advantages over caching content. First, the file system
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering can manage the memory, allowing infrequently hit cached files to
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering be paged out. Second, since caching open handles does not consume
bddfc8afd329ac68a23f66a3512d4e249af25191Tom Gundersen significant resources, it will be possible to enable an AutoLoadCache
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering feature where static files are dynamically loaded in the cache
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering as the server runs. On systems that have file change notification,
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering this module can be enhanced to automatically garbage collect
186fe1db20902b6542cee58ba499ced914d687a8Lennart Poettering cached files that change on disk.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen This module should work on Unix systems that have sendfile. Place
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen cachefile directives into your configuration to direct files to
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt These files are only cached when the server is restarted, so if you
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen change the list, or if the files are changed, then you'll need to
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen restart the server.
505f8da7325591defe5f751f328bd26915267602Tom Gundersen To reiterate that point: if the files are modified *in place*
1346b1f0388f4100bb3c2a2bb23bc881769c020cTom Gundersen without restarting the server you may end up serving requests that
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt are completely bogus. You should update files by unlinking the old
1346b1f0388f4100bb3c2a2bb23bc881769c020cTom Gundersen copy and putting a new copy in place.
1346b1f0388f4100bb3c2a2bb23bc881769c020cTom Gundersen There's no such thing as inheriting these files across vhosts or
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen whatever... place the directives in the main server only.
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen Known problems:
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen Don't use Alias or RewriteRule to move these files around... unless
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen you feel like paying for an extra stat() on each request. This is
73432d67b590c8c8954cf2f8954d174a55d58c7bTom Gundersen a deficiency in the Apache API that will hopefully be solved some day.
73432d67b590c8c8954cf2f8954d174a55d58c7bTom Gundersen The file will be served out of the file handle cache, but there will be
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen an extra stat() that's a waste.
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_core.h"
#include "apr_mmap.h"
#include "apr_strings.h"
static int once_through = 0;
#if APR_HAS_SENDFILE
char *filename;
int is_mmapped;
#if APR_HAS_MMAP
} a_file;
return sconf;
ap_pool_t *p)
#ifdef WIN32
return rv;
size_t n;
#if APR_HAS_MMAP
#if APR_HAS_SENDFILE
++file;
return APR_SUCCESS;
#if APR_HAS_SENDFILE
return NULL;
return NULL;
return NULL;
return NULL;
return NULL;
#if APR_HAS_MMAP
return NULL;
return NULL;
!= APR_SUCCESS) {
return NULL;
return NULL;
return NULL;
return NULL;
int nelts;
once_through++;
int res;
return DECLINED;
return res;
return DECLINED;
return OK;
#if APR_HAS_MMAP
if (!rangestatus) {
long length;
return OK;
#if APR_HAS_SENDFILE
if (!rangestatus) {
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
#if APR_HAS_SENDFILE
long length;
if (!rangestatus) {
&offset,
&length,
flags);
&offset,
&length,
return OK;
return DECLINED;
return errstatus;
ap_set_etag(r);
return errstatus;
if (!r->header_only) {
return rc;
{NULL}
static void register_hooks(void)
{ NULL }