mod_cache.c revision 34f6b45d02039cb734332bc1cecf16c1dcd8f2a5
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.
be2439588008221e691321fdf4f75432cfb72878Felix Gabriel Mancemodule AP_MODULE_DECLARE_DATA cache_module;
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae BungiuAPR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu/* -------------------------------------------------------------- */
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu/* Handles for cache filters, resolved at startup to eliminate
0dd6e7830de0887c9a12356447975a826b3b3db2Christian Maeder * a name-to-function mapping on each request
e0f1794e365dd347e97b37d7d22b2fce27296fa1Christian Maederstatic ap_filter_rec_t *cache_filter_handle;
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiustatic ap_filter_rec_t *cache_save_subreq_filter_handle;
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiustatic ap_filter_rec_t *cache_out_filter_handle;
1a38107941725211e7c3f051f7a8f5e12199f03acmaederstatic ap_filter_rec_t *cache_out_subreq_filter_handle;
1a38107941725211e7c3f051f7a8f5e12199f03acmaederstatic 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?
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * deliver the content by installing the CACHE_OUT filter.
431571057e88a650a974adec93ea4bb5173b6213Felix Gabriel Mance * check whether we're allowed to try cache it
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * add CACHE_SAVE filter
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * By default, the cache handler runs in the quick handler, bypassing
b84c87f199dc287d235d7dad6ea344f6912ef531Christian Maeder * 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
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * implementation.
be00381168b3f10192afabbba136fb06d3a9f358Christian Maeder * Under certain circumstances, an admin might want to run the cache as
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * a normal handler instead of a quick handler, allowing the cache to
27fdf879983dd28e211b41f3be6c0e930b7c816bFelix Gabriel Mance * run after the authorisation hooks, or by allowing fine control over
7852de3551fc797566ee71165bafe05b6d81728cnotanartist * the placement of the cache in the filter chain. This option comes at
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * a performance penalty, and should only be used to achieve specific
27fdf879983dd28e211b41f3be6c0e930b7c816bFelix Gabriel Mance * caching goals where the admin understands what they are doing.
27fdf879983dd28e211b41f3be6c0e930b7c816bFelix Gabriel Mancestatic int cache_quick_handler(request_rec *r, int lookup)
0dd6e7830de0887c9a12356447975a826b3b3db2Christian Maeder /* Delay initialization until we know we are handling a GET */
d6d81ead61a5f9fb7d047e623f7898e730c258camcodescu conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu /* only run if the quick handler is enabled */
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * Which cache module (if any) should handle this request?
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski if (!(providers = ap_cache_get_providers(r, conf, r->parsed_uri))) {
0dd6e7830de0887c9a12356447975a826b3b3db2Christian Maeder /* 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);
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu /* save away the possible providers */
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * 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 * Try to serve this request from the cache.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * If no existing cache file (DECLINED)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder * add cache_save filter
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * If cached file (OK)
31e9d2a02e15b7dbc157e0d3fb3b84f6c8666482Christian Maeder * clear filter stack
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * add cache_out filter
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc 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,
7852de3551fc797566ee71165bafe05b6d81728cnotanartist * 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.
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu rv = ap_cache_try_lock(conf, cache, r, NULL);
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * Add cache_save filter to cache this request. Choose
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu * the correct filter by checking if we are a subrequest
06acd8a23b2f06e7b2373d53f738cf56c7f03223Francisc Nicolae Bungiu ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu "Adding CACHE_SAVE_SUBREQ filter for %s",
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu ap_add_output_filter_handle(cache_save_subreq_filter_handle,
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski r->server, "Adding CACHE_SAVE filter for %s",
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski ap_add_output_filter_handle(cache_save_filter_handle,
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder "Adding CACHE_REMOVE_URL filter for %s",
b84c87f199dc287d235d7dad6ea344f6912ef531Christian Maeder /* Add cache_remove_url filter to this request to remove a
7852de3551fc797566ee71165bafe05b6d81728cnotanartist * stale cache entry if needed. Also put the current cache
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski * request rec in the filter context, as the request that
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder * is available later during running the filter may be
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder * different due to an internal redirect.
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder ap_add_output_filter_handle(cache_remove_url_filter_handle,
cf0439f74f1d55a9840d38a88f9b0f4fc00d5547Christian Maeder r->server, "Cache locked for url, not caching "
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
624f8c31bd8d6746b93f4b5966aa6fc7680fefc5Felix Gabriel Mance r->server, "Restoring request headers for %s",
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu r->headers_in = cache->stale_headers;
b84c87f199dc287d235d7dad6ea344f6912ef531Christian Maeder ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
624f8c31bd8d6746b93f4b5966aa6fc7680fefc5Felix Gabriel Mance "cache: error returned while checking for cached "
ee93ea764a2b8189253e912c8447f9419033f6d4Francisc Nicolae Bungiu /* if we are a lookup, we are exiting soon one way or another; Restore
d0f58d27c2536eba454d8f77de8617bc6a2c99cdFelix Gabriel Mance * the headers. */
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu "Restoring request headers.");
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu /* If we are a lookup, we have to return DECLINED as we have no
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu * way of knowing if we will be able to serve the content.
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu /* Return cached status. */
b90f0b7fd6ccfbdd7e5adb65b1f6c02c7758ff5cmcodescu /* If we're a lookup, we can exit now instead of serving the content. */
if (r->main) {
return rv;
return OK;
while (next) {
if (ffrom) {
return DECLINED;
&cache_module);
return DECLINED;
return DECLINED;
if (r->main) {
r->server,
r->uri);
r->uri);
r->uri);
return DECLINED;
return rv;
if (r->main) {
r->uri);
return rv;
return OK;
request_rec *r = f->r;
if (!cache) {
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;
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) {
if (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) {
else if (!r->header_only) {
r->server,
char *max_age_val;
apr_int64_t x;
errno = 0;
if (errno) {
x = x * MSEC_ONE_SEC;
int status;
request_rec *r = f->r;
if (!cache) {
const char *tmppath;
if (tmppath) {
return ps;
return ps;
int flag)
conf =
&cache_module);
return NULL;
int flag)
conf =
&cache_module);
return NULL;
conf =
&cache_module);
return NULL;
int flag)
conf =
&cache_module);
return NULL;
int flag)
conf =
&cache_module);
return NULL;
int flag)
conf =
&cache_module);
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;
const char *arg)
conf =
&cache_module);
return NULL;
const char *arg)
conf =
&cache_module);
return NULL;
const char *arg)
conf =
&cache_module);
return NULL;
const char *arg)
double val;
conf =
&cache_module);
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;
if (!cache_generate_key) {
return OK;
{NULL}
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,