#define CORE_PRIVATE
#include "mod_cache.h"
module AP_MODULE_DECLARE_DATA cache_module;
APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
static ap_filter_rec_t *cache_save_filter_handle;
static ap_filter_rec_t *cache_save_subreq_filter_handle;
static ap_filter_rec_t *cache_out_filter_handle;
static ap_filter_rec_t *cache_out_subreq_filter_handle;
static ap_filter_rec_t *cache_remove_url_filter_handle;
static int cache_url_handler(request_rec *r, int lookup)
{
apr_status_t rv;
const char *auth;
cache_provider_list *providers;
cache_request_rec *cache;
cache_server_conf *conf;
apr_bucket_brigade *out;
ap_filter_t *next;
ap_filter_rec_t *cache_out_handle;
if (r->method_number != M_GET) {
return DECLINED;
}
conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
&cache_module);
if (!(providers = ap_cache_get_providers(r, conf, r->parsed_uri))) {
return DECLINED;
}
cache = (cache_request_rec *) ap_get_module_config(r->request_config,
&cache_module);
if (!cache) {
cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
ap_set_module_config(r->request_config, &cache_module, cache);
}
cache->providers = providers;
auth = apr_table_get(r->headers_in, "Authorization");
if (auth) {
return DECLINED;
}
rv = cache_select(r);
if (rv != OK) {
if (rv == DECLINED) {
if (!lookup) {
rv = ap_cache_try_lock(conf, r, NULL);
if (APR_SUCCESS == rv) {
if (r->main) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r->server,
"Adding CACHE_SAVE_SUBREQ filter for %s",
r->uri);
ap_add_output_filter_handle(cache_save_subreq_filter_handle,
NULL, r, r->connection);
}
else {
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r->server, "Adding CACHE_SAVE filter for %s",
r->uri);
ap_add_output_filter_handle(cache_save_filter_handle,
NULL, r, r->connection);
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
"Adding CACHE_REMOVE_URL filter for %s",
r->uri);
cache->remove_url_filter =
ap_add_output_filter_handle(cache_remove_url_filter_handle,
cache, r, r->connection);
}
else {
ap_log_error(APLOG_MARK, APLOG_DEBUG, rv,
r->server, "Cache locked for url, not caching "
"response: %s", r->uri);
}
}
else {
if (cache->stale_headers) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r->server, "Restoring request headers for %s",
r->uri);
r->headers_in = cache->stale_headers;
}
ap_set_module_config(r->request_config, &cache_module, NULL);
}
}
else {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"cache: error returned while checking for cached "
"file by '%s' cache", cache->provider_name);
}
return DECLINED;
}
if (lookup) {
if (cache->stale_headers) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
"Restoring request headers.");
r->headers_in = cache->stale_headers;
}
ap_set_module_config(r->request_config, &cache_module, NULL);
}
rv = ap_meets_conditions(r);
if (rv != OK) {
if (lookup) {
return DECLINED;
}
return rv;
}
if (lookup) {
return OK;
}
ap_run_insert_filter(r);
if (r->main) {
cache_out_handle = cache_out_subreq_filter_handle;
}
else {
cache_out_handle = cache_out_filter_handle;
}
ap_add_output_filter_handle(cache_out_handle, NULL, r, r->connection);
next = r->output_filters;
while (next && (next->frec != cache_out_handle)) {
ap_remove_output_filter(next);
next = next->next;
}
out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
rv = ap_pass_brigade(r->output_filters, out);
if (rv != APR_SUCCESS) {
if (rv != AP_FILTER_ERROR) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"cache: error returned while trying to return %s "
"cached data",
cache->provider_name);
}
return rv;
}
return OK;
}
static int cache_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
{
request_rec *r = f->r;
cache_request_rec *cache;
cache = (cache_request_rec *) ap_get_module_config(r->request_config,
&cache_module);
if (!cache) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"CACHE_OUT enabled unexpectedly");
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb);
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
"cache: running CACHE_OUT filter");
r->status = cache->handle->cache_obj->info.status;
cache->provider->recall_body(cache->handle, r->pool, bb);
ap_remove_output_filter(f);
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
"cache: serving %s", r->uri);
return ap_pass_brigade(f->next, bb);
}
static int cache_save_filter(ap_filter_t *f, apr_bucket_brigade *in)
{
int rv = !OK;
request_rec *r = f->r;
cache_request_rec *cache;
cache_server_conf *conf;
const char *cc_out, *cl;
const char *exps, *lastmods, *dates, *etag;
apr_time_t exp, date, lastmod, now;
apr_off_t size;
cache_info *info = NULL;
char *reason;
apr_pool_t *p;
apr_bucket *e;
conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
&cache_module);
cache = (cache_request_rec *) ap_get_module_config(r->request_config,
&cache_module);
if (!cache) {
cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
ap_set_module_config(r->request_config, &cache_module, cache);
}
reason = NULL;
p = r->pool;
if (cache->block_response) {
return APR_SUCCESS;
}
if (cache->in_checked) {
rv = cache->provider->store_body(cache->handle, r, in);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server,
"cache: Cache provider's store_body failed!");
ap_remove_output_filter(f);
ap_cache_remove_lock(conf, r, cache->handle ?
(char *)cache->handle->cache_obj->key : NULL, NULL);
}
else {
ap_cache_remove_lock(conf, r, cache->handle ?
(char *)cache->handle->cache_obj->key : NULL, in);
}
return ap_pass_brigade(f->next, in);
}
exps = apr_table_get(r->err_headers_out, "Expires");
if (exps == NULL) {
exps = apr_table_get(r->headers_out, "Expires");
}
if (exps != NULL) {
if (APR_DATE_BAD == (exp = apr_date_parse_http(exps))) {
exps = NULL;
}
}
else {
exp = APR_DATE_BAD;
}
lastmods = apr_table_get(r->err_headers_out, "Last-Modified");
if (lastmods == NULL) {
lastmods = apr_table_get(r->headers_out, "Last-Modified");
}
if (lastmods != NULL) {
lastmod = apr_date_parse_http(lastmods);
if (lastmod == APR_DATE_BAD) {
lastmods = NULL;
}
}
else {
lastmod = APR_DATE_BAD;
}
etag = apr_table_get(r->err_headers_out, "Etag");
if (etag == NULL) {
etag = apr_table_get(r->headers_out, "Etag");
}
cc_out = apr_table_get(r->err_headers_out, "Cache-Control");
if (cc_out == NULL) {
cc_out = apr_table_get(r->headers_out, "Cache-Control");
}
if (r->status != HTTP_OK && r->status != HTTP_NON_AUTHORITATIVE
&& r->status != HTTP_MULTIPLE_CHOICES
&& r->status != HTTP_MOVED_PERMANENTLY
&& r->status != HTTP_NOT_MODIFIED) {
if (exps != NULL || cc_out != NULL) {
}
else {
reason = apr_psprintf(p, "Response status %d", r->status);
}
}
if (reason) {
}
else if (exps != NULL && exp == APR_DATE_BAD) {
reason = apr_pstrcat(p, "Broken expires header: ", exps, NULL);
}
else if (exp != APR_DATE_BAD && exp < r->request_time)
{
reason = "Expires header already expired, not cacheable";
}
else if (!conf->ignorequerystring && r->parsed_uri.query && exps == NULL &&
!ap_cache_liststr(NULL, cc_out, "max-age", NULL) &&
!ap_cache_liststr(NULL, cc_out, "s-maxage", NULL)) {
reason = "Query string present but no explicit expiration time";
}
else if (r->status == HTTP_NOT_MODIFIED &&
!cache->handle && !cache->stale_handle) {
reason = "HTTP Status 304 Not Modified";
}
else if (r->status == HTTP_OK && lastmods == NULL && etag == NULL
&& (exps == NULL) && (conf->no_last_mod_ignore ==0) &&
!ap_cache_liststr(NULL, cc_out, "max-age", NULL) &&
!ap_cache_liststr(NULL, cc_out, "s-maxage", NULL)) {
reason = "No Last-Modified, Etag, Expires, Cache-Control:max-age or Cache-Control:s-maxage headers";
}
else if (r->header_only && !cache->stale_handle) {
reason = "HTTP HEAD request";
}
else if (!conf->store_nostore &&
ap_cache_liststr(NULL, cc_out, "no-store", NULL)) {
reason = "Cache-Control: no-store present";
}
else if (!conf->store_private &&
ap_cache_liststr(NULL, cc_out, "private", NULL)) {
reason = "Cache-Control: private present";
}
else if (apr_table_get(r->headers_in, "Authorization") != NULL
&& !(ap_cache_liststr(NULL, cc_out, "s-maxage", NULL)
|| ap_cache_liststr(NULL, cc_out, "must-revalidate", NULL)
|| ap_cache_liststr(NULL, cc_out, "public", NULL))) {
reason = "Authorization required";
}
else if (ap_cache_liststr(NULL,
apr_table_get(r->headers_out, "Vary"),
"*", NULL)) {
reason = "Vary header contains '*'";
}
else if (apr_table_get(r->subprocess_env, "no-cache") != NULL) {
reason = "environment variable 'no-cache' is set";
}
else if (r->no_cache) {
reason = "r->no_cache present";
}
if (reason && r->status == HTTP_NOT_MODIFIED &&
cache->stale_handle) {
apr_bucket_brigade *bb;
apr_bucket *bkt;
int status;
cache->handle = cache->stale_handle;
info = &cache->handle->cache_obj->info;
r->status = info->status;
r->status_line = NULL;
bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
r->headers_in = cache->stale_headers;
status = ap_meets_conditions(r);
if (status != OK) {
r->status = status;
bkt = apr_bucket_flush_create(bb->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, bkt);
}
else {
cache->provider->recall_body(cache->handle, r->pool, bb);
}
cache->block_response = 1;
ap_cache_remove_lock(conf, r, cache->handle ?
(char *)cache->handle->cache_obj->key : NULL, NULL);
return ap_pass_brigade(f->next, bb);
}
if (reason) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"cache: %s not cached. Reason: %s", r->unparsed_uri,
reason);
ap_remove_output_filter(f);
ap_cache_remove_lock(conf, r, cache->handle ?
(char *)cache->handle->cache_obj->key : NULL, NULL);
return ap_pass_brigade(f->next, in);
}
cache->in_checked = 1;
cl = apr_table_get(r->err_headers_out, "Content-Length");
if (cl == NULL) {
cl = apr_table_get(r->headers_out, "Content-Length");
}
if (cl) {
char *errp;
if (apr_strtoff(&size, cl, &errp, 10) || *errp || size < 0) {
cl = NULL;
}
}
if (!cl) {
int all_buckets_here=0;
int unresolved_length = 0;
size=0;
for (e = APR_BRIGADE_FIRST(in);
e != APR_BRIGADE_SENTINEL(in);
e = APR_BUCKET_NEXT(e))
{
if (APR_BUCKET_IS_EOS(e)) {
all_buckets_here=1;
break;
}
if (APR_BUCKET_IS_FLUSH(e)) {
unresolved_length = 1;
continue;
}
if (e->length == (apr_size_t)-1) {
break;
}
size += e->length;
}
if (!all_buckets_here) {
size = -1;
}
}
if (cache->stale_handle) {
if (r->status == HTTP_NOT_MODIFIED) {
cache->handle = cache->stale_handle;
info = &cache->handle->cache_obj->info;
rv = OK;
}
else if (!r->header_only) {
cache->provider->remove_entity(cache->stale_handle);
cache->stale_handle = NULL;
r->headers_in = cache->stale_headers;
}
}
if (!cache->handle && !r->header_only) {
rv = cache_create_entity(r, size);
info = apr_pcalloc(r->pool, sizeof(cache_info));
info->status = r->status;
}
if (rv != OK) {
ap_remove_output_filter(f);
ap_cache_remove_lock(conf, r, cache->handle ?
(char *)cache->handle->cache_obj->key : NULL, NULL);
return ap_pass_brigade(f->next, in);
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"cache: Caching url: %s", r->unparsed_uri);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"cache: Removing CACHE_REMOVE_URL filter.");
ap_remove_output_filter(cache->remove_url_filter);
dates = apr_table_get(r->err_headers_out, "Date");
if (dates == NULL) {
dates = apr_table_get(r->headers_out, "Date");
}
if (dates != NULL) {
info->date = apr_date_parse_http(dates);
}
else {
info->date = APR_DATE_BAD;
}
now = apr_time_now();
if (info->date == APR_DATE_BAD) {
info->date = now;
}
date = info->date;
info->response_time = now;
info->request_time = r->request_time;
if (lastmod != APR_DATE_BAD && lastmod > date) {
lastmod = date;
lastmods = dates;
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
r->server,
"cache: Last modified is in the future, "
"replacing with now");
}
if (exp == APR_DATE_BAD) {
char *max_age_val;
if (ap_cache_liststr(r->pool, cc_out, "max-age", &max_age_val) &&
max_age_val != NULL) {
apr_int64_t x;
errno = 0;
x = apr_atoi64(max_age_val);
if (errno) {
x = conf->defex;
}
else {
x = x * MSEC_ONE_SEC;
}
if (x > conf->maxex) {
x = conf->maxex;
}
exp = date + x;
}
else if ((lastmod != APR_DATE_BAD) && (lastmod < date)) {
apr_time_t x = (apr_time_t) ((date - lastmod) * conf->factor);
if (x > conf->maxex) {
x = conf->maxex;
}
exp = date + x;
}
else {
exp = date + conf->defex;
}
}
info->expire = exp;
if (cache->stale_handle) {
r->status = info->status;
r->status_line = NULL;
r->headers_out = apr_table_overlay(r->pool, r->headers_out,
r->err_headers_out);
r->headers_out = ap_cache_cacheable_hdrs_out(r->pool, r->headers_out,
r->server);
apr_table_clear(r->err_headers_out);
ap_cache_accept_headers(cache->handle, r, 1);
}
rv = cache->provider->store_headers(cache->handle, r, info);
if (cache->stale_handle) {
apr_bucket_brigade *bb;
apr_bucket *bkt;
int status;
bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
r->headers_in = cache->stale_headers;
status = ap_meets_conditions(r);
if (status != OK) {
r->status = status;
bkt = apr_bucket_flush_create(bb->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, bkt);
}
else {
cache->provider->recall_body(cache->handle, r->pool, bb);
}
cache->block_response = 1;
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server,
"cache: updating headers with store_headers failed. "
"Removing cached url.");
rv = cache->provider->remove_url(cache->stale_handle, r->pool);
if (rv != OK) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server,
"cache: attempt to remove url from cache unsuccessful.");
}
}
ap_cache_remove_lock(conf, r, cache->handle ?
(char *)cache->handle->cache_obj->key : NULL, NULL);
return ap_pass_brigade(f->next, bb);
}
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server,
"cache: store_headers failed");
ap_remove_output_filter(f);
ap_cache_remove_lock(conf, r, cache->handle ?
(char *)cache->handle->cache_obj->key : NULL, NULL);
return ap_pass_brigade(f->next, in);
}
rv = cache->provider->store_body(cache->handle, r, in);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server,
"cache: store_body failed");
ap_remove_output_filter(f);
ap_cache_remove_lock(conf, r, cache->handle ?
(char *)cache->handle->cache_obj->key : NULL, NULL);
return ap_pass_brigade(f->next, in);
}
ap_cache_remove_lock(conf, r, cache->handle ?
(char *)cache->handle->cache_obj->key : NULL, in);
return ap_pass_brigade(f->next, in);
}
static int cache_remove_url_filter(ap_filter_t *f, apr_bucket_brigade *in)
{
request_rec *r = f->r;
cache_request_rec *cache;
cache = (cache_request_rec *) f->ctx;
if (!cache) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"cache: CACHE_REMOVE_URL enabled unexpectedly");
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, in);
}
cache_remove_url(cache, r->pool);
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, in);
}
static void * create_cache_config(apr_pool_t *p, server_rec *s)
{
const char *tmppath;
cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf));
ps->cacheenable = apr_array_make(p, 10, sizeof(struct cache_enable));
ps->cachedisable = apr_array_make(p, 10, sizeof(struct cache_disable));
ps->maxex = DEFAULT_CACHE_MAXEXPIRE;
ps->maxex_set = 0;
ps->defex = DEFAULT_CACHE_EXPIRE;
ps->defex_set = 0;
ps->factor = DEFAULT_CACHE_LMFACTOR;
ps->factor_set = 0;
ps->no_last_mod_ignore_set = 0;
ps->no_last_mod_ignore = 0;
ps->ignorecachecontrol = 0;
ps->ignorecachecontrol_set = 0;
ps->store_private = 0;
ps->store_private_set = 0;
ps->store_nostore = 0;
ps->store_nostore_set = 0;
ps->ignore_headers = apr_array_make(p, 10, sizeof(char *));
ps->ignore_headers_set = CACHE_IGNORE_HEADERS_UNSET;
ps->ignorequerystring = 0;
ps->ignorequerystring_set = 0;
ps->ignore_session_id = apr_array_make(p, 10, sizeof(char *));
ps->ignore_session_id_set = CACHE_IGNORE_SESSION_ID_UNSET;
ps->lock = 0;
ps->lock_set = 0;
apr_temp_dir_get(&tmppath, p);
if (tmppath) {
ps->lockpath = apr_pstrcat(p, tmppath, DEFAULT_CACHE_LOCKPATH, NULL);
}
ps->lockmaxage = apr_time_from_sec(DEFAULT_CACHE_MAXAGE);
return ps;
}
static void * merge_cache_config(apr_pool_t *p, void *basev, void *overridesv)
{
cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf));
cache_server_conf *base = (cache_server_conf *) basev;
cache_server_conf *overrides = (cache_server_conf *) overridesv;
ps->cachedisable = apr_array_append(p,
base->cachedisable,
overrides->cachedisable);
ps->cacheenable = apr_array_append(p,
base->cacheenable,
overrides->cacheenable);
ps->maxex = (overrides->maxex_set == 0) ? base->maxex : overrides->maxex;
ps->defex = (overrides->defex_set == 0) ? base->defex : overrides->defex;
ps->factor =
(overrides->factor_set == 0) ? base->factor : overrides->factor;
ps->no_last_mod_ignore =
(overrides->no_last_mod_ignore_set == 0)
? base->no_last_mod_ignore
: overrides->no_last_mod_ignore;
ps->ignorecachecontrol =
(overrides->ignorecachecontrol_set == 0)
? base->ignorecachecontrol
: overrides->ignorecachecontrol;
ps->store_private =
(overrides->store_private_set == 0)
? base->store_private
: overrides->store_private;
ps->store_nostore =
(overrides->store_nostore_set == 0)
? base->store_nostore
: overrides->store_nostore;
ps->ignore_headers =
(overrides->ignore_headers_set == CACHE_IGNORE_HEADERS_UNSET)
? base->ignore_headers
: overrides->ignore_headers;
ps->ignorequerystring =
(overrides->ignorequerystring_set == 0)
? base->ignorequerystring
: overrides->ignorequerystring;
ps->ignore_session_id =
(overrides->ignore_session_id_set == CACHE_IGNORE_SESSION_ID_UNSET)
? base->ignore_session_id
: overrides->ignore_session_id;
ps->lock =
(overrides->lock_set == 0)
? base->lock
: overrides->lock;
ps->lockpath =
(overrides->lockpath_set == 0)
? base->lockpath
: overrides->lockpath;
ps->lockmaxage =
(overrides->lockmaxage_set == 0)
? base->lockmaxage
: overrides->lockmaxage;
return ps;
}
static const char *set_cache_ignore_no_last_mod(cmd_parms *parms, void *dummy,
int flag)
{
cache_server_conf *conf;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
conf->no_last_mod_ignore = flag;
conf->no_last_mod_ignore_set = 1;
return NULL;
}
static const char *set_cache_ignore_cachecontrol(cmd_parms *parms,
void *dummy, int flag)
{
cache_server_conf *conf;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
conf->ignorecachecontrol = flag;
conf->ignorecachecontrol_set = 1;
return NULL;
}
static const char *set_cache_store_private(cmd_parms *parms, void *dummy,
int flag)
{
cache_server_conf *conf;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
conf->store_private = flag;
conf->store_private_set = 1;
return NULL;
}
static const char *set_cache_store_nostore(cmd_parms *parms, void *dummy,
int flag)
{
cache_server_conf *conf;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
conf->store_nostore = flag;
conf->store_nostore_set = 1;
return NULL;
}
static const char *add_ignore_header(cmd_parms *parms, void *dummy,
const char *header)
{
cache_server_conf *conf;
char **new;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
if (!strncasecmp(header, "None", 4)) {
conf->ignore_headers->nelts = 0;
}
else {
if ((conf->ignore_headers_set == CACHE_IGNORE_HEADERS_UNSET) ||
(conf->ignore_headers->nelts)) {
new = (char **)apr_array_push(conf->ignore_headers);
(*new) = (char *)header;
}
}
conf->ignore_headers_set = CACHE_IGNORE_HEADERS_SET;
return NULL;
}
static const char *add_ignore_session_id(cmd_parms *parms, void *dummy,
const char *identifier)
{
cache_server_conf *conf;
char **new;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
if (!strncasecmp(identifier, "None", 4)) {
conf->ignore_session_id->nelts = 0;
}
else {
if ((conf->ignore_session_id_set == CACHE_IGNORE_SESSION_ID_UNSET) ||
(conf->ignore_session_id->nelts)) {
new = (char **)apr_array_push(conf->ignore_session_id);
(*new) = (char *)identifier;
}
}
conf->ignore_session_id_set = CACHE_IGNORE_SESSION_ID_SET;
return NULL;
}
static const char *add_cache_enable(cmd_parms *parms, void *dummy,
const char *type,
const char *url)
{
cache_server_conf *conf;
struct cache_enable *new;
if (*type == '/') {
return apr_psprintf(parms->pool,
"provider (%s) starts with a '/'. Are url and provider switched?",
type);
}
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
new = apr_array_push(conf->cacheenable);
new->type = type;
if (apr_uri_parse(parms->pool, url, &(new->url))) {
return NULL;
}
if (new->url.path) {
new->pathlen = strlen(new->url.path);
} else {
new->pathlen = 1;
new->url.path = "/";
}
return NULL;
}
static const char *add_cache_disable(cmd_parms *parms, void *dummy,
const char *url)
{
cache_server_conf *conf;
struct cache_disable *new;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
new = apr_array_push(conf->cachedisable);
if (apr_uri_parse(parms->pool, url, &(new->url))) {
return NULL;
}
if (new->url.path) {
new->pathlen = strlen(new->url.path);
} else {
new->pathlen = 1;
new->url.path = "/";
}
return NULL;
}
static const char *set_cache_maxex(cmd_parms *parms, void *dummy,
const char *arg)
{
cache_server_conf *conf;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
conf->maxex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
conf->maxex_set = 1;
return NULL;
}
static const char *set_cache_defex(cmd_parms *parms, void *dummy,
const char *arg)
{
cache_server_conf *conf;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
conf->defex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
conf->defex_set = 1;
return NULL;
}
static const char *set_cache_factor(cmd_parms *parms, void *dummy,
const char *arg)
{
cache_server_conf *conf;
double val;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
if (sscanf(arg, "%lg", &val) != 1) {
return "CacheLastModifiedFactor value must be a float";
}
conf->factor = val;
conf->factor_set = 1;
return NULL;
}
static const char *set_cache_ignore_querystring(cmd_parms *parms, void *dummy,
int flag)
{
cache_server_conf *conf;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
conf->ignorequerystring = flag;
conf->ignorequerystring_set = 1;
return NULL;
}
static const char *set_cache_lock(cmd_parms *parms, void *dummy,
int flag)
{
cache_server_conf *conf;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
conf->lock = flag;
conf->lock_set = 1;
return NULL;
}
static const char *set_cache_lock_path(cmd_parms *parms, void *dummy,
const char *arg)
{
cache_server_conf *conf;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
conf->lockpath = ap_server_root_relative(parms->pool, arg);
if (!conf->lockpath) {
return apr_pstrcat(parms->pool, "Invalid CacheLockPath path ",
arg, NULL);
}
conf->lockpath_set = 1;
return NULL;
}
static const char *set_cache_lock_maxage(cmd_parms *parms, void *dummy,
const char *arg)
{
cache_server_conf *conf;
apr_int64_t seconds;
conf =
(cache_server_conf *)ap_get_module_config(parms->server->module_config,
&cache_module);
seconds = apr_atoi64(arg);
if (seconds <= 0) {
return "CacheLockMaxAge value must be a non-zero positive integer";
}
conf->lockmaxage = apr_time_from_sec(seconds);
conf->lockmaxage_set = 1;
return NULL;
}
static int cache_post_config(apr_pool_t *p, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
cache_generate_key = APR_RETRIEVE_OPTIONAL_FN(ap_cache_generate_key);
if (!cache_generate_key) {
cache_generate_key = cache_generate_key_default;
}
return OK;
}
static const command_rec cache_cmds[] =
{
AP_INIT_TAKE2("CacheEnable", add_cache_enable, NULL, RSRC_CONF,
"A cache type and partial URL prefix below which "
"caching is enabled"),
AP_INIT_TAKE1("CacheDisable", add_cache_disable, NULL, RSRC_CONF,
"A partial URL prefix below which caching is disabled"),
AP_INIT_TAKE1("CacheMaxExpire", set_cache_maxex, NULL, RSRC_CONF,
"The maximum time in seconds to cache a document"),
AP_INIT_TAKE1("CacheDefaultExpire", set_cache_defex, NULL, RSRC_CONF,
"The default time in seconds to cache a document"),
AP_INIT_FLAG("CacheIgnoreNoLastMod", set_cache_ignore_no_last_mod, NULL,
RSRC_CONF,
"Ignore Responses where there is no Last Modified Header"),
AP_INIT_FLAG("CacheIgnoreCacheControl", set_cache_ignore_cachecontrol,
NULL, RSRC_CONF,
"Ignore requests from the client for uncached content"),
AP_INIT_FLAG("CacheStorePrivate", set_cache_store_private,
NULL, RSRC_CONF,
"Ignore 'Cache-Control: private' and store private content"),
AP_INIT_FLAG("CacheStoreNoStore", set_cache_store_nostore,
NULL, RSRC_CONF,
"Ignore 'Cache-Control: no-store' and store sensitive content"),
AP_INIT_ITERATE("CacheIgnoreHeaders", add_ignore_header, NULL, RSRC_CONF,
"A space separated list of headers that should not be "
"stored by the cache"),
AP_INIT_FLAG("CacheIgnoreQueryString", set_cache_ignore_querystring,
NULL, RSRC_CONF,
"Ignore query-string when caching"),
AP_INIT_ITERATE("CacheIgnoreURLSessionIdentifiers", add_ignore_session_id,
NULL, RSRC_CONF, "A space separated list of session "
"identifiers that should be ignored for creating the key "
"of the cached entity."),
AP_INIT_TAKE1("CacheLastModifiedFactor", set_cache_factor, NULL, RSRC_CONF,
"The factor used to estimate Expires date from "
"LastModified date"),
AP_INIT_FLAG("CacheLock", set_cache_lock,
NULL, RSRC_CONF,
"Enable or disable the thundering herd lock."),
AP_INIT_TAKE1("CacheLockPath", set_cache_lock_path, NULL, RSRC_CONF,
"The thundering herd lock path. Defaults to the '"
DEFAULT_CACHE_LOCKPATH "' directory in the system "
"temp directory."),
AP_INIT_TAKE1("CacheLockMaxAge", set_cache_lock_maxage, NULL, RSRC_CONF,
"Maximum age of any thundering herd lock."),
{NULL}
};
static void register_hooks(apr_pool_t *p)
{
ap_hook_quick_handler(cache_url_handler, NULL, NULL, APR_HOOK_FIRST);
cache_save_filter_handle =
ap_register_output_filter("CACHE_SAVE",
cache_save_filter,
NULL,
AP_FTYPE_CONTENT_SET+1);
cache_save_subreq_filter_handle =
ap_register_output_filter("CACHE_SAVE_SUBREQ",
cache_save_filter,
NULL,
AP_FTYPE_CONTENT_SET-1);
cache_out_filter_handle =
ap_register_output_filter("CACHE_OUT",
cache_out_filter,
NULL,
AP_FTYPE_CONTENT_SET+1);
cache_out_subreq_filter_handle =
ap_register_output_filter("CACHE_OUT_SUBREQ",
cache_out_filter,
NULL,
AP_FTYPE_CONTENT_SET-1);
cache_remove_url_filter_handle =
ap_register_output_filter("CACHE_REMOVE_URL",
cache_remove_url_filter,
NULL,
AP_FTYPE_PROTOCOL);
ap_hook_post_config(cache_post_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
}
module AP_MODULE_DECLARE_DATA cache_module =
{
STANDARD20_MODULE_STUFF,
NULL,
NULL,
create_cache_config,
merge_cache_config,
cache_cmds,
register_hooks
};