mod_cache.c revision c967bf3bc89e8aa60dbd30d9da388e448ddc1cc4
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder/* Licensed to the Apache Software Foundation (ASF) under one or more
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * contributor license agreements. See the NOTICE file distributed with
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens Elkner * this work for additional information regarding copyright ownership.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * The ASF licenses this file to You under the Apache License, Version 2.0
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance * (the "License"); you may not use this file except in compliance with
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * the License. You may obtain a copy of the License at
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance * http://www.apache.org/licenses/LICENSE-2.0
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * Unless required by applicable law or agreed to in writing, software
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * distributed under the License is distributed on an "AS IS" BASIS,
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * See the License for the specific language governing permissions and
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * limitations under the License.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiumodule AP_MODULE_DECLARE_DATA cache_module;
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel ManceAPR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu/* -------------------------------------------------------------- */
0dd6e7830de0887c9a12356447975a826b3b3db2Christian Maeder/* Handles for cache filters, resolved at startup to eliminate
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * a name-to-function mapping on each request
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiustatic ap_filter_rec_t *cache_save_filter_handle;
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiustatic ap_filter_rec_t *cache_save_subreq_filter_handle;
1a38107941725211e7c3f051f7a8f5e12199f03acmaederstatic ap_filter_rec_t *cache_out_subreq_filter_handle;
32bbac77828be0233953f8fe476edb0a9585408dChristian Maederstatic ap_filter_rec_t *cache_remove_url_filter_handle;
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * CACHE handler
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * -------------
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * Can we deliver this request from the cache?
27fdf879983dd28e211b41f3be6c0e930b7c816bFelix Gabriel Mance * deliver the content by installing the CACHE_OUT filter.
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder * check whether we're allowed to try cache it
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * add CACHE_SAVE filter
b84c87f199dc287d235d7dad6ea344f6912ef531Christian Maeder * By default, the cache handler runs in the quick handler, bypassing
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * virtually all server processing and offering the cache its optimal
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * performance. In this mode, the cache bolts onto the front of the
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * server, and behaves as a discrete RFC2616 caching proxy
be00381168b3f10192afabbba136fb06d3a9f358Christian Maeder * implementation.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * Under certain circumstances, an admin might want to run the cache as
27fdf879983dd28e211b41f3be6c0e930b7c816bFelix Gabriel Mance * a normal handler instead of a quick handler, allowing the cache to
7852de3551fc797566ee71165bafe05b6d81728cnotanartist * run after the authorisation hooks, or by allowing fine control over
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * the placement of the cache in the filter chain. This option comes at
27fdf879983dd28e211b41f3be6c0e930b7c816bFelix Gabriel Mance * a performance penalty, and should only be used to achieve specific
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * caching goals where the admin understands what they are doing.
d0f58d27c2536eba454d8f77de8617bc6a2c99cdFelix Gabriel Mancestatic int cache_quick_handler(request_rec *r, int lookup)
424860079d47bf490fa98d5d7498096a0447c569mcodescu conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
d6d81ead61a5f9fb7d047e623f7898e730c258camcodescu /* only run if the quick handler is enabled */
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * Which cache module (if any) should handle this request?
9475501a6acf48434052d9e6f4a05ed6681eaaabFrancisc Nicolae Bungiu if (!(providers = cache_get_providers(r, conf, r->parsed_uri))) {
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski /* make space for the per request config */
424860079d47bf490fa98d5d7498096a0447c569mcodescu cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
0dd6e7830de0887c9a12356447975a826b3b3db2Christian Maeder cache->out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
0dd6e7830de0887c9a12356447975a826b3b3db2Christian Maeder /* save away the possible providers */
d0f58d27c2536eba454d8f77de8617bc6a2c99cdFelix Gabriel Mance * Are we allowed to serve cached info at all?
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu /* find certain cache controlling headers */
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu auth = apr_table_get(r->headers_in, "Authorization");
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu /* First things first - does the request allow us to return
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * cached information at all? If not, just decline the request.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu /* Are we something other than GET or HEAD? If so, invalidate
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder * the cached entities.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00748)
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu "Invalidating all cached entities in response to '%s' request for %s",
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu /* we've got a cache invalidate! tell everyone who cares */
656f17ae9b7610ff2de1b6eedeeadea0c3bcdc8dChristian Maeder cache_run_cache_status(cache->handle, r, r->headers_out,
d3cb3401882f6956de016f8eecbec1cd3b868acbFelix Gabriel Mance AP_CACHE_INVALIDATE, apr_psprintf(r->pool,
7852de3551fc797566ee71165bafe05b6d81728cnotanartist * Try to serve this request from the cache.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * If no existing cache file (DECLINED)
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * add cache_save filter
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * If cached file (OK)
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * clear filter stack
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * add cache_out filter
06acd8a23b2f06e7b2373d53f738cf56c7f03223Francisc Nicolae Bungiu /* try to obtain a cache lock at this point. if we succeed,
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * we are the first to try and cache this url. if we fail,
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * it means someone else is already trying to cache this
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * url, and we should just let the request through to the
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * backend without any attempt to cache. this stops
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * duplicated simultaneous attempts to cache an entity.
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski * Add cache_save filter to cache this request. Choose
32bbac77828be0233953f8fe476edb0a9585408dChristian Maeder * the correct filter by checking if we are a subrequest
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder r, APLOGNO(00749) "Adding CACHE_SAVE_SUBREQ filter for %s",
b84c87f199dc287d235d7dad6ea344f6912ef531Christian Maeder cache->save_filter = ap_add_output_filter_handle(
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder r, APLOGNO(00750) "Adding CACHE_SAVE filter for %s",
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder cache->save_filter = ap_add_output_filter_handle(
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu apr_pool_userdata_setn(cache, CACHE_CTX_KEY, NULL, r->pool);
7852de3551fc797566ee71165bafe05b6d81728cnotanartist ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00751)
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu "Adding CACHE_REMOVE_URL filter for %s",
624f8c31bd8d6746b93f4b5966aa6fc7680fefc5Felix Gabriel Mance /* Add cache_remove_url filter to this request to remove a
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski * stale cache entry if needed. Also put the current cache
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * request rec in the filter context, as the request that
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * is available later during running the filter may be
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * different due to an internal redirect.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu cache->remove_url_filter = ap_add_output_filter_handle(
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu cache_remove_url_filter_handle, cache, r,
624f8c31bd8d6746b93f4b5966aa6fc7680fefc5Felix Gabriel Mance ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv,
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder r, APLOGNO(00752) "Cache locked for url, not caching "
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu r, APLOGNO(00753) "Restoring request headers for %s",
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu /* error */
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu /* we've got a cache hit! tell everyone who cares */
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_HIT,
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu "cache hit");
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu /* if we are a lookup, we are exiting soon one way or another; Restore
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu * the headers. */
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00754)
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu "Restoring request headers.");
if (lookup) {
return DECLINED;
return rv;
if (lookup) {
return OK;
if (r->main) {
if (ffrom) {
while (next) {
return next;
apr_bucket *e;
&cache_module);
return DECLINED;
return DECLINED;
return DECLINED;
if (r->main) {
r->uri);
r->uri);
r->connection);
r->uri);
r->connection);
return rv;
return DECLINED;
return rv;
if (r->main) {
r->uri);
request_rec *r = f->r;
apr_bucket *e;
if (!cache) {
e = APR_BUCKET_NEXT(e))
if (APR_BUCKET_IS_EOS(e)) {
return APR_SUCCESS;
apr_bucket *e;
e = APR_BUCKET_NEXT(e))
if (APR_BUCKET_IS_EOS(e)) {
return rv;
request_rec *r = f->r;
char *reason;
apr_pool_t *p;
apr_bucket *e;
&cache_module);
if (!cache) {
"CACHE/CACHE_SAVE filter enabled while caching is disabled, ignoring");
p = r->pool;
return APR_SUCCESS;
const char *warn_head;
if (r->main) {
r->status));
if (!cc_out && !pragma) {
if (reason) {
/* Note: mod-include clears last_modified/expires/etags - this
reason = "No Last-Modified; Etag; Expires; Cache-Control:max-age or Cache-Control:s-maxage headers";
else if (r->no_cache) {
int status;
r->headers_out,
r->pool,
reason));
if (reason) {
reason);
reason);
if (cl) {
char *errp;
if (!cl) {
int all_buckets_here=0;
size=0;
e = APR_BUCKET_NEXT(e))
if (APR_BUCKET_IS_EOS(e)) {
if (APR_BUCKET_IS_FLUSH(e)) {
if (!all_buckets_here) {
apr_int64_t x;
errno = 0;
if (errno) {
x = x * MSEC_ONE_SEC;
int status;
request_rec *r = f->r;
if (!cache) {
*conf =
&cache_module);
f->r->unparsed_uri);
*conf =
&cache_module);
switch (status) {
case AP_CACHE_HIT: {
case AP_CACHE_REVALIDATE: {
case AP_CACHE_MISS: {
case AP_CACHE_INVALIDATE: {
if (x_cache) {
if (x_cache_detail) {
return OK;
void *dummy;
if (dummy) {
const char *warn_head;
*conf =
&cache_module);
if (r->main) {
r->err_headers_out,
r->pool,
r->status));
return dconf;
new->no_last_mod_ignore = (add->no_last_mod_ignore_set == 0) ? base->no_last_mod_ignore : add->no_last_mod_ignore;
return new;
return ps;
return ps;
int flag)
conf =
&cache_module);
return NULL;
int flag)
return NULL;
conf =
&cache_module);
return NULL;
int flag)
return NULL;
int flag)
return NULL;
int flag)
return NULL;
const char *header)
char **new;
conf =
&cache_module);
return NULL;
const char *identifier)
char **new;
conf =
&cache_module);
return NULL;
const char *type,
const char *url)
return err;
type);
if (!url) {
if (!url) {
conf =
&cache_module);
return NULL;
return NULL;
const char *url)
return err;
conf =
&cache_module);
return NULL;
return NULL;
return NULL;
const char *arg)
return NULL;
const char *arg)
return NULL;
const char *arg)
return NULL;
const char *arg)
double val;
return NULL;
int flag)
conf =
&cache_module);
return NULL;
int flag)
conf =
&cache_module);
return NULL;
const char *arg)
conf =
&cache_module);
return NULL;
const char *arg)
conf =
&cache_module);
if (seconds <= 0) {
return NULL;
&cache_module);
return NULL;
&cache_module);
return NULL;
const char *arg)
conf =
&cache_module);
return apr_psprintf(parms->pool, "URL '%s' must contain at least one of a scheme, a hostname or a port.", arg);
return NULL;
int flag)
return NULL;
if (!cache_generate_key) {
return OK;
{NULL}
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,