mod_file_cache.c revision 032b8a34c3911bbc5ad5385ca40af65af273bff9
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen/* ====================================================================
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen * Copyright (c) 1998-1999 The Apache Group. All rights reserved.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen *
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 * are met:
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen *
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 *
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 *
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 *
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 *
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.
e877666c1553a3dd5b05b4c48090f889ffc079e6Patrik Flykt *
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 *
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 * ====================================================================
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 *
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen */
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidt
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen/*
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>
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidt *
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen * v0.01: initial implementation
85b5673b337048fa881a5afb1d00d1a7b95950fbTom Gundersen */
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidt/*
fe8db0c5ee3365a2fc80ee7ebffa238f9a0a2ae2Tom Gundersen Documentation:
49699bac94d24b444274f91f85c82e6fad04d029Susant Sahani
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.
186fe1db20902b6542cee58ba499ced914d687a8Lennart Poettering
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 be cached.
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen cachefile /path/to/file1
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen cachefile /path/to/file2
f0c4cd7a2c2835a392b5b1807d8c506ab0af44deTom Gundersen ...
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen
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.
3bef724f7e7f7eaca69881548b06e221b77d7031Tom Gundersen
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
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
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen Known problems:
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen
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.
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen*/
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen
f882c247ad59776c3a7753bb963c1f8e2386cb79Tom Gundersen#ifdef HAVE_STDIOP_H
a97dcc12e486ecff531809802930a26c4da827f2Tom Gundersen#include <stdio.h>
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen#endif
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt#ifdef HAVE_SYS_TYPES_H
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen#include <sys/types.h>
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen#endif
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen#ifdef HAVE_STRING_H
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen#include <string.h>
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen#endif
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen#define CORE_PRIVATE
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen#include "httpd.h"
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen#include "http_config.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_core.h"
#include "apr_mmap.h"
#include "apr_strings.h"
module MODULE_VAR_EXPORT file_cache_module;
static int once_through = 0;
typedef struct {
#if APR_HAS_SENDFILE
ap_file_t *file;
#endif
char *filename;
ap_finfo_t finfo;
int is_mmapped;
#if APR_HAS_MMAP
ap_mmap_t *mm;
#endif
} a_file;
typedef struct {
ap_array_header_t *files;
} a_server_config;
static void *create_server_config(ap_pool_t *p, server_rec *s)
{
a_server_config *sconf = ap_palloc(p, sizeof(*sconf));
sconf->files = ap_make_array(p, 20, sizeof(a_file));
return sconf;
}
static ap_status_t open_file(ap_file_t **file, const char *filename, int flg1, int flg2,
ap_pool_t *p)
{
ap_status_t rv;
#ifdef WIN32
/* The Windows file needs to be opened for overlapped i/o, which APR doesn't
* support.
*/
HANDLE hFile;
hFile = CreateFile(filename, /* pointer to name of the file */
GENERIC_READ, /* access (read-write) mode */
FILE_SHARE_READ, /* share mode */
NULL, /* pointer to security attributes */
OPEN_EXISTING, /* how to create */
FILE_FLAG_OVERLAPPED | FILE_FLAG_SEQUENTIAL_SCAN, /* file attributes */
NULL); /* handle to file with attributes to copy */
if (hFile != INVALID_HANDLE_VALUE) {
rv = ap_put_os_file(file, &hFile, p);
}
else {
rv = GetLastError();
*file = NULL;
}
#else
rv = ap_open(file, filename, flg1, flg2, p);
#endif
return rv;
}
static ap_status_t cleanup_file_cache(void *sconfv)
{
a_server_config *sconf = sconfv;
size_t n;
a_file *file;
n = sconf->files->nelts;
file = (a_file *)sconf->files->elts;
while(n) {
#if APR_HAS_MMAP
if (file->is_mmapped) {
ap_mmap_delete(file->mm);
}
else
#endif
#if APR_HAS_SENDFILE
ap_close(file->file);
#endif
++file;
--n;
}
return APR_SUCCESS;
}
static const char *cachefile(cmd_parms *cmd, void *dummy, const char *filename)
{
#if APR_HAS_SENDFILE
a_server_config *sconf;
a_file *new_file;
a_file tmp;
ap_file_t *fd = NULL;
ap_status_t rc;
/* canonicalize the file name? */
/* os_canonical... */
if (ap_stat(&tmp.finfo, filename, cmd->temp_pool) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, cmd->server,
"file_cache: unable to stat(%s), skipping", filename);
return NULL;
}
if (tmp.finfo.filetype != APR_REG) {
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, cmd->server,
"file_cache: %s isn't a regular file, skipping", filename);
return NULL;
}
/* Note: open_file should call ap_open for Unix and CreateFile for Windows.
* The Windows file needs to be opened for async I/O to allow multiple threads
* to serve it up at once.
*/
rc = open_file(&fd, filename, APR_READ, APR_OS_DEFAULT, cmd->pool);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rc, cmd->server,
"file_cache: unable to open(%s, O_RDONLY), skipping", filename);
return NULL;
}
tmp.file = fd;
tmp.filename = ap_pstrdup(cmd->pool, filename);
sconf = ap_get_module_config(cmd->server->module_config, &file_cache_module);
new_file = ap_push_array(sconf->files);
*new_file = tmp;
if (sconf->files->nelts == 1) {
/* first one, register the cleanup */
ap_register_cleanup(cmd->pool, sconf, cleanup_file_cache, ap_null_cleanup);
}
new_file->is_mmapped = FALSE;
return NULL;
#else
/* Sendfile not supported on this platform */
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, cmd->server,
"mod_file_cache: unable to cache file: %s. Sendfile is not supported on this OS", filename);
return NULL;
#endif
}
static const char *mmapfile(cmd_parms *cmd, void *dummy, const char *filename)
{
#if APR_HAS_MMAP
a_server_config *sconf;
a_file *new_file;
a_file tmp;
ap_file_t *fd = NULL;
if (ap_stat(&tmp.finfo, filename, cmd->temp_pool) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, cmd->server,
"mod_file_cache: unable to stat(%s), skipping", filename);
return NULL;
}
if ((tmp.finfo.filetype) != APR_REG) {
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, cmd->server,
"mod_file_cache: %s isn't a regular file, skipping", filename);
return NULL;
}
if (ap_open(&fd, filename, APR_READ, APR_OS_DEFAULT, cmd->temp_pool)
!= APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, cmd->server,
"mod_file_cache: unable to open(%s, O_RDONLY), skipping", filename);
return NULL;
}
if (ap_mmap_create(&tmp.mm, fd, 0, tmp.finfo.size, cmd->pool) != APR_SUCCESS) {
int save_errno = errno;
ap_close(fd);
errno = save_errno;
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, cmd->server,
"mod_file_cache: unable to mmap %s, skipping", filename);
return NULL;
}
ap_close(fd);
tmp.filename = ap_pstrdup(cmd->pool, filename);
sconf = ap_get_module_config(cmd->server->module_config, &file_cache_module);
new_file = ap_push_array(sconf->files);
*new_file = tmp;
if (sconf->files->nelts == 1) {
/* first one, register the cleanup */
ap_register_cleanup(cmd->pool, sconf, cleanup_file_cache, ap_null_cleanup);
}
new_file->is_mmapped = TRUE;
return NULL;
#else
/* MMAP not supported on this platform*/
return NULL;
#endif
}
static int file_compare(const void *av, const void *bv)
{
const a_file *a = av;
const a_file *b = bv;
return strcmp(a->filename, b->filename);
}
static void file_cache_post_config(ap_pool_t *p, ap_pool_t *plog,
ap_pool_t *ptemp, server_rec *s)
{
a_server_config *sconf;
a_file *elts;
int nelts;
once_through++;
/* sort the elements of the main_server, by filename */
sconf = ap_get_module_config(s->module_config, &file_cache_module);
elts = (a_file *)sconf->files->elts;
nelts = sconf->files->nelts;
qsort(elts, nelts, sizeof(a_file), file_compare);
/* and make the virtualhosts share the same thing */
for (s = s->next; s; s = s->next) {
ap_set_module_config(s->module_config, &file_cache_module, sconf);
}
}
/* 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 tmp;
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 (ap_is_empty_table(sconf->files))
return DECLINED;
res = ap_core_translate(r);
if (res != OK || !r->filename) {
return res;
}
tmp.filename = r->filename;
match = (a_file *)bsearch(&tmp, sconf->files->elts, sconf->files->nelts,
sizeof(a_file), file_compare);
if (match == NULL)
return DECLINED;
/* pass bsearch 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, int rangestatus)
{
#if APR_HAS_MMAP
if (!rangestatus) {
ap_send_mmap (file->mm, r, 0, file->finfo.size);
}
else {
long length;
ap_off_t offset;
while (ap_each_byterange(r, &offset, &length)) {
ap_send_mmap(file->mm, r, offset, length);
}
}
#endif
return OK;
}
static int sendfile_handler(request_rec *r, a_file *file, int rangestatus)
{
#if APR_HAS_SENDFILE
ap_size_t length, nbytes;
ap_off_t offset = 0;
ap_status_t rv;
if (!rangestatus) {
rv = ap_send_fd(file->file, r, 0, file->finfo.size, &nbytes);
}
else {
while (ap_each_byterange(r, &offset, &length)) {
if ((rv = ap_send_fd(file->file, r, offset, length, &nbytes)) != APR_SUCCESS)
break;
}
}
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"mod_file_cache: sendfile_handler error serving file: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
#endif
return OK;
}
#if 0
static int sendfile_handler(request_rec *r, a_file *file, int rangestatus)
{
#if APR_HAS_SENDFILE
long length;
ap_off_t offset = 0;
struct iovec iov;
ap_hdtr_t hdtr;
ap_hdtr_t *phdtr = &hdtr;
ap_status_t rv;
ap_int32_t flags = 0;
/*
* We want to send any data held in the client buffer on the
* call to iol_sendfile. So hijack it then set outcnt to 0
* to prevent the data from being sent to the client again
* when the buffer is flushed to the client at the end of the
* request.
*/
iov.iov_base = r->connection->client->outbase;
iov.iov_len = r->connection->client->outcnt;
r->connection->client->outcnt = 0;
/* initialize the ap_hdtr_t struct */
phdtr->headers = &iov;
phdtr->numheaders = 1;
phdtr->trailers = NULL;
phdtr->numtrailers = 0;
if (!rangestatus) {
length = file->finfo.size;
if (!r->connection->keepalive) {
/* Disconnect the socket after the send completes. This
* should leave the accept socket in a state ready to be
* reused for the next connection.
*/
flags |= APR_SENDFILE_DISCONNECT_SOCKET;
}
rv = iol_sendfile(r->connection->client->iol,
file->file,
phdtr,
&offset,
&length,
flags);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"mod_file_cache: iol_sendfile failed.");
}
}
else {
while (ap_each_byterange(r, &offset, &length)) {
rv =iol_sendfile(r->connection->client->iol,
file->file,
phdtr,
&offset,
&length,
0);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"mod_file_cache: iol_sendfile failed.");
}
phdtr = NULL;
}
}
#endif
return OK;
}
#endif
static int file_cache_handler(request_rec *r)
{
a_file *match;
int rangestatus, errstatus;
int rc = OK;
/* 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 |= (1 << 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(r);
ap_set_etag(r);
if (((errstatus = ap_meets_conditions(r)) != OK)
|| (errstatus = ap_set_content_length (r, match->finfo.size))) {
return errstatus;
}
rangestatus = ap_set_byterange(r);
ap_send_http_header(r);
/* Call appropriate handler */
if (!r->header_only) {
if (match->is_mmapped == TRUE)
rc = mmap_handler(r, match, rangestatus);
else
rc = sendfile_handler(r, match, rangestatus);
}
return rc;
}
static command_rec file_cache_cmds[] =
{
AP_INIT_ITERATE("cachefile", cachefile, NULL, RSRC_CONF,
"A space seperated list of files to add to the file handle cache at config time"),
AP_INIT_ITERATE("mmapfile", mmapfile, NULL, RSRC_CONF,
"A space seperated list of files to mmap at config time"),
{NULL}
};
static void register_hooks(void)
{
ap_hook_post_config(file_cache_post_config, NULL, NULL, AP_HOOK_MIDDLE);
ap_hook_translate_name(file_cache_xlat, NULL, NULL, AP_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, AP_HOOK_MIDDLE);
*/
};
static const handler_rec file_cache_handlers[] =
{
{ "*/*", file_cache_handler },
{ NULL }
};
module MODULE_VAR_EXPORT 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 */
file_cache_handlers, /* handlers */
register_hooks /* register hooks */
};