mod_cache.c revision fa0c562f0ab33ed019baf3c1dcd37ae72af6b29d
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose/* Licensed to the Apache Software Foundation (ASF) under one or more
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * contributor license agreements. See the NOTICE file distributed with
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * this work for additional information regarding copyright ownership.
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * The ASF licenses this file to You under the Apache License, Version 2.0
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * (the "License"); you may not use this file except in compliance with
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * the License. You may obtain a copy of the License at
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * Unless required by applicable law or agreed to in writing, software
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * distributed under the License is distributed on an "AS IS" BASIS,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * See the License for the specific language governing permissions and
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * limitations under the License.
a0ab15ceb80290db80c2052520830a95390de385Sumit BoseAPR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose/* -------------------------------------------------------------- */
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose/* Handles for cache filters, resolved at startup to eliminate
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * a name-to-function mapping on each request
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bosestatic ap_filter_rec_t *cache_save_subreq_filter_handle;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bosestatic ap_filter_rec_t *cache_out_subreq_filter_handle;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bosestatic ap_filter_rec_t *cache_remove_url_filter_handle;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * CACHE handler
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * -------------
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * Can we deliver this request from the cache?
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * deliver the content by installing the CACHE_OUT filter.
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * check whether we're allowed to try cache it
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * add CACHE_SAVE filter
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * By default, the cache handler runs in the quick handler, bypassing
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * virtually all server processing and offering the cache its optimal
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * performance. In this mode, the cache bolts onto the front of the
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * server, and behaves as a discrete RFC2616 caching proxy
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * implementation.
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * Under certain circumstances, an admin might want to run the cache as
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * a normal handler instead of a quick handler, allowing the cache to
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * run after the authorisation hooks, or by allowing fine control over
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * the placement of the cache in the filter chain. This option comes at
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * a performance penalty, and should only be used to achieve specific
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * caching goals where the admin understands what they are doing.
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bosestatic int cache_quick_handler(request_rec *r, int lookup)
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose const char *auth;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose /* only run if the quick handler is enabled */
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * Which cache module (if any) should handle this request?
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose if (!(providers = cache_get_providers(r, conf, r->parsed_uri))) {
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose /* make space for the per request config */
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose cache->out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose /* save away the possible providers */
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * Are we allowed to serve cached info at all?
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose /* find certain cache controlling headers */
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose auth = apr_table_get(r->headers_in, "Authorization");
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose /* First things first - does the request allow us to return
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * cached information at all? If not, just decline the request.
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose /* Are we something other than GET or HEAD? If so, invalidate
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * the cached entities.
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose "Invalidating all cached entities in response to '%s' request for %s",
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose /* we've got a cache invalidate! tell everyone who cares */
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose cache_run_cache_status(cache->handle, r, r->headers_out,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * Try to serve this request from the cache.
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * If no existing cache file (DECLINED)
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * add cache_save filter
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * If cached file (OK)
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * clear filter stack
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * add cache_out filter
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose /* try to obtain a cache lock at this point. if we succeed,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * we are the first to try and cache this url. if we fail,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * it means someone else is already trying to cache this
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * url, and we should just let the request through to the
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * backend without any attempt to cache. this stops
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * duplicated simultaneous attempts to cache an entity.
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * Add cache_save filter to cache this request. Choose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * the correct filter by checking if we are a subrequest
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose r, "Adding CACHE_SAVE_SUBREQ filter for %s",
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose cache->save_filter = ap_add_output_filter_handle(
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose r, "Adding CACHE_SAVE filter for %s",
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose cache->save_filter = ap_add_output_filter_handle(
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose apr_pool_userdata_setn(cache, CACHE_CTX_KEY, NULL, r->pool);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose "Adding CACHE_REMOVE_URL filter for %s",
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose /* Add cache_remove_url filter to this request to remove a
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose * stale cache entry if needed. Also put the current cache
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * request rec in the filter context, as the request that
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * is available later during running the filter may be
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * different due to an internal redirect.
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose cache->remove_url_filter = ap_add_output_filter_handle(
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose r, "Cache locked for url, not caching "
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose r, "Restoring request headers for %s",
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose /* we've got a cache hit! tell everyone who cares */
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_HIT,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose "cache hit");
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose /* if we are a lookup, we are exiting soon one way or another; Restore
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose * the headers. */
if (lookup) {
return DECLINED;
return rv;
if (lookup) {
return OK;
if (r->main) {
return HTTP_INTERNAL_SERVER_ERROR;
return rv;
return OK;
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);
return HTTP_INTERNAL_SERVER_ERROR;
return rv;
return OK;
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;
const char *tmppath;
if (tmppath) {
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,