mod_cache.c revision 77c22c493161004a20e2591319197b7272a68f0c
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek/* Licensed to the Apache Software Foundation (ASF) under one or more
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * contributor license agreements. See the NOTICE file distributed with
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * this work for additional information regarding copyright ownership.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * The ASF licenses this file to You under the Apache License, Version 2.0
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * (the "License"); you may not use this file except in compliance with
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * the License. You may obtain a copy of the License at
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * http://www.apache.org/licenses/LICENSE-2.0
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * Unless required by applicable law or agreed to in writing, software
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * distributed under the License is distributed on an "AS IS" BASIS,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * See the License for the specific language governing permissions and
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * limitations under the License.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekmodule AP_MODULE_DECLARE_DATA cache_module;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-SzmekAPR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek/* -------------------------------------------------------------- */
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek/* Handles for cache filters, resolved at startup to eliminate
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * a name-to-function mapping on each request
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekstatic ap_filter_rec_t *cache_filter_handle;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekstatic ap_filter_rec_t *cache_save_filter_handle;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekstatic ap_filter_rec_t *cache_save_subreq_filter_handle;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekstatic ap_filter_rec_t *cache_out_filter_handle;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekstatic ap_filter_rec_t *cache_out_subreq_filter_handle;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekstatic ap_filter_rec_t *cache_remove_url_filter_handle;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * Can we deliver this request from the cache?
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * deliver the content by installing the CACHE_OUT filter.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * check whether we're allowed to try cache it
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * add CACHE_SAVE filter
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * By default, the cache handler runs in the quick handler, bypassing
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * virtually all server processing and offering the cache its optimal
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * performance. In this mode, the cache bolts onto the front of the
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * server, and behaves as a discrete RFC2616 caching proxy
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * implementation.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * Under certain circumstances, an admin might want to run the cache as
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * a normal handler instead of a quick handler, allowing the cache to
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * run after the authorisation hooks, or by allowing fine control over
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * the placement of the cache in the filter chain. This option comes at
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * a performance penalty, and should only be used to achieve specific
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * caching goals where the admin understands what they are doing.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmekstatic int cache_quick_handler(request_rec *r, int lookup)
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek ap_filter_rec_t *cache_out_handle;
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek /* Delay initialization until we know we are handling a GET */
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek if (r->method_number != M_GET) {
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek /* only run if the quick handler is enabled */
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * Which cache module (if any) should handle this request?
8c9571d0ae50656f730a5e37378d5c3dcf3b9789Lennart Poettering if (!(providers = cache_get_providers(r, conf, r->parsed_uri))) {
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek /* make space for the per request config */
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek cache->out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek /* save away the possible providers */
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * Are we allowed to serve cached info at all?
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek /* find certain cache controlling headers */
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek auth = apr_table_get(r->headers_in, "Authorization");
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek /* First things first - does the request allow us to return
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * cached information at all? If not, just decline the request.
0dc5d23c85db85f96b141d4d32deee8018e56a6aLennart Poettering * Try to serve this request from the cache.
0dc5d23c85db85f96b141d4d32deee8018e56a6aLennart Poettering * If no existing cache file (DECLINED)
0dc5d23c85db85f96b141d4d32deee8018e56a6aLennart Poettering * add cache_save filter
0dc5d23c85db85f96b141d4d32deee8018e56a6aLennart Poettering * If cached file (OK)
0dc5d23c85db85f96b141d4d32deee8018e56a6aLennart Poettering * clear filter stack
0dc5d23c85db85f96b141d4d32deee8018e56a6aLennart Poettering * add cache_out filter
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek /* try to obtain a cache lock at this point. if we succeed,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * we are the first to try and cache this url. if we fail,
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * it means someone else is already trying to cache this
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * url, and we should just let the request through to the
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek * backend without any attempt to cache. this stops
0dc5d23c85db85f96b141d4d32deee8018e56a6aLennart Poettering * duplicated simultaneous attempts to cache an entity.
3cc765d2718ac9b4ff978044ceabf5ad59d73edfZbigniew Jędrzejewski-Szmek rv = cache_try_lock(conf, cache, r);
if (r->main) {
r->server,
r->uri);
r->connection);
r->uri);
r->connection);
r->uri);
r->uri);
return DECLINED;
if (lookup) {
if (lookup) {
return DECLINED;
return rv;
if (lookup) {
return OK;
if (r->main) {
return HTTP_INTERNAL_SERVER_ERROR;
return rv;
return OK;
while (next) {
if (ffrom) {
while (next) {
return next;
apr_bucket *e;
return DECLINED;
&cache_module);
return DECLINED;
return DECLINED;
if (r->main) {
r->server,
r->uri);
r->uri);
r->connection);
r->uri);
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;
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) {
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) {
*conf =
&cache_module);
switch (status) {
case AP_CACHE_HIT: {
case AP_CACHE_REVALIDATE: {
case AP_CACHE_MISS: {
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;
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,