#define CORE_PRIVATE
#include "mod_cache.h"
#include <ap_provider.h>
extern APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
extern module AP_MODULE_DECLARE_DATA cache_module;
static int uri_meets_conditions(apr_uri_t filter, int pathlen, apr_uri_t url)
{
if(filter.hostname) {
if (!url.hostname) {
return 0;
}
else if (strcasecmp(filter.hostname, url.hostname)) {
return 0;
}
}
if(filter.scheme) {
if (!url.scheme) {
return 0;
}
else if (strcasecmp(filter.scheme, url.scheme)) {
return 0;
}
}
if(filter.port_str) {
if (url.port_str && filter.port != url.port) {
return 0;
}
else if (filter.port != apr_uri_port_of_scheme(url.scheme)) {
return 0;
}
}
else if(url.port_str && filter.scheme) {
if (apr_uri_port_of_scheme(filter.scheme) == url.port) {
return 0;
}
}
if (!url.path) {
if (*filter.path == '/' && pathlen == 1) {
return 1;
}
else {
return 0;
}
}
return !strncmp(filter.path, url.path, pathlen);
}
CACHE_DECLARE(cache_provider_list *)ap_cache_get_providers(request_rec *r,
cache_server_conf *conf,
apr_uri_t uri)
{
cache_provider_list *providers = NULL;
int i;
for (i = 0; i < conf->cacheenable->nelts; i++) {
struct cache_enable *ent =
(struct cache_enable *)conf->cacheenable->elts;
if (uri_meets_conditions(ent[i].url, ent[i].pathlen, uri)) {
cache_provider *provider;
provider = ap_lookup_provider(CACHE_PROVIDER_GROUP, ent[i].type,
"0");
if (!provider) {
}
else {
cache_provider_list *newp;
newp = apr_pcalloc(r->pool, sizeof(cache_provider_list));
newp->provider_name = ent[i].type;
newp->provider = provider;
if (!providers) {
providers = newp;
}
else {
cache_provider_list *last = providers;
while (last->next) {
last = last->next;
}
last->next = newp;
}
}
}
}
for (i = 0; i < conf->cachedisable->nelts; i++) {
struct cache_disable *ent =
(struct cache_disable *)conf->cachedisable->elts;
if (uri_meets_conditions(ent[i].url, ent[i].pathlen, uri)) {
return NULL;
}
}
return providers;
}
CACHE_DECLARE(apr_int64_t) ap_cache_current_age(cache_info *info,
const apr_time_t age_value,
apr_time_t now)
{
apr_time_t apparent_age, corrected_received_age, response_delay,
corrected_initial_age, resident_time, current_age,
age_value_usec;
age_value_usec = apr_time_from_sec(age_value);
apparent_age = MAX(0, info->response_time - info->date);
corrected_received_age = MAX(apparent_age, age_value_usec);
response_delay = info->response_time - info->request_time;
corrected_initial_age = corrected_received_age + response_delay;
resident_time = now - info->response_time;
current_age = corrected_initial_age + resident_time;
return apr_time_sec(current_age);
}
CACHE_DECLARE(apr_status_t) ap_cache_try_lock(cache_server_conf *conf,
request_rec *r, char *key) {
apr_status_t status;
const char *lockname;
const char *path;
char dir[5];
apr_time_t now = apr_time_now();
apr_finfo_t finfo;
apr_file_t *lockfile;
void *dummy;
finfo.mtime = 0;
if (!conf || !conf->lock || !conf->lockpath) {
return APR_SUCCESS;
}
apr_pool_userdata_get(&dummy, CACHE_LOCKFILE_KEY, r->pool);
if (dummy) {
return APR_SUCCESS;
}
if (!key) {
cache_generate_key(r, r->pool, &key);
}
lockname = ap_cache_generate_name(r->pool, 0, 0, key);
dir[0] = '/';
dir[1] = lockname[0];
dir[2] = '/';
dir[3] = lockname[1];
dir[4] = 0;
path = apr_pstrcat(r->pool, conf->lockpath, dir, NULL);
if (APR_SUCCESS != (status = apr_dir_make_recursive(path,
APR_UREAD|APR_UWRITE|APR_UEXECUTE, r->pool))) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
"Could not create a cache lock directory: %s",
path);
return status;
}
lockname = apr_pstrcat(r->pool, path, "/", lockname, NULL);
apr_pool_userdata_set(lockname, CACHE_LOCKNAME_KEY, NULL, r->pool);
status = apr_stat(&finfo, lockname,
APR_FINFO_MTIME | APR_FINFO_NLINK, r->pool);
if (!(APR_STATUS_IS_ENOENT(status)) && APR_SUCCESS != status) {
ap_log_error(APLOG_MARK, APLOG_ERR, APR_EEXIST, r->server,
"Could not stat a cache lock file: %s",
lockname);
return status;
}
if ((status == APR_SUCCESS) && (((now - finfo.mtime) > conf->lockmaxage)
|| (now < finfo.mtime))) {
ap_log_error(APLOG_MARK, APLOG_INFO, status, r->server,
"Cache lock file for '%s' too old, removing: %s",
r->uri, lockname);
apr_file_remove(lockname, r->pool);
}
if (APR_SUCCESS == (status = apr_file_open(&lockfile, lockname,
APR_WRITE | APR_CREATE | APR_EXCL | APR_DELONCLOSE,
APR_UREAD | APR_UWRITE, r->pool))) {
apr_pool_userdata_set(lockfile, CACHE_LOCKFILE_KEY, NULL, r->pool);
}
return status;
}
CACHE_DECLARE(apr_status_t) ap_cache_remove_lock(cache_server_conf *conf,
request_rec *r, char *key, apr_bucket_brigade *bb) {
void *dummy;
const char *lockname;
if (!conf || !conf->lock || !conf->lockpath) {
return APR_SUCCESS;
}
if (bb) {
apr_bucket *e;
int eos_found = 0;
for (e = APR_BRIGADE_FIRST(bb);
e != APR_BRIGADE_SENTINEL(bb);
e = APR_BUCKET_NEXT(e))
{
if (APR_BUCKET_IS_EOS(e)) {
eos_found = 1;
break;
}
}
if (!eos_found) {
return APR_SUCCESS;
}
}
apr_pool_userdata_get(&dummy, CACHE_LOCKFILE_KEY, r->pool);
if (dummy) {
return apr_file_close((apr_file_t *)dummy);
}
apr_pool_userdata_get(&dummy, CACHE_LOCKNAME_KEY, r->pool);
lockname = (const char *)dummy;
if (!lockname) {
char dir[5];
if (!key) {
cache_generate_key(r, r->pool, &key);
}
lockname = ap_cache_generate_name(r->pool, 0, 0, key);
dir[0] = '/';
dir[1] = lockname[0];
dir[2] = '/';
dir[3] = lockname[1];
dir[4] = 0;
lockname = apr_pstrcat(r->pool, conf->lockpath, dir, "/", lockname, NULL);
}
return apr_file_remove(lockname, r->pool);
}
CACHE_DECLARE(int) ap_cache_check_allowed(request_rec *r) {
const char *cc_req;
const char *pragma;
cache_server_conf *conf =
(cache_server_conf *)ap_get_module_config(r->server->module_config,
&cache_module);
cc_req = apr_table_get(r->headers_in, "Cache-Control");
pragma = apr_table_get(r->headers_in, "Pragma");
if (ap_cache_liststr(NULL, pragma, "no-cache", NULL)
|| ap_cache_liststr(NULL, cc_req, "no-cache", NULL)) {
if (!conf->ignorecachecontrol) {
return 0;
}
else {
ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
"Incoming request is asking for an uncached version of "
"%s, but we have been configured to ignore it and serve "
"cached content anyway", r->unparsed_uri);
}
}
if (ap_cache_liststr(NULL, cc_req, "no-store", NULL)) {
if (!conf->ignorecachecontrol) {
return 0;
}
else {
ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
"Incoming request is asking for a no-store version of "
"%s, but we have been configured to ignore it and serve "
"cached content anyway", r->unparsed_uri);
}
}
return 1;
}
CACHE_DECLARE(int) ap_cache_check_freshness(cache_handle_t *h,
request_rec *r)
{
apr_status_t status;
apr_int64_t age, maxage_req, maxage_cresp, maxage, smaxage, maxstale;
apr_int64_t minfresh;
const char *cc_cresp, *cc_req;
const char *pragma;
const char *agestr = NULL;
const char *expstr = NULL;
char *val;
apr_time_t age_c = 0;
cache_info *info = &(h->cache_obj->info);
const char *warn_head;
cache_server_conf *conf =
(cache_server_conf *)ap_get_module_config(r->server->module_config,
&cache_module);
cc_req = apr_table_get(r->headers_in, "Cache-Control");
pragma = apr_table_get(r->headers_in, "Pragma");
if (ap_cache_liststr(NULL, pragma, "no-cache", NULL)
|| ap_cache_liststr(NULL, cc_req, "no-cache", NULL)) {
if (!conf->ignorecachecontrol) {
return 0;
}
ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
"Incoming request is asking for a uncached version of "
"%s, but we have been configured to ignore it and "
"serve a cached response anyway",
r->unparsed_uri);
}
cc_cresp = apr_table_get(h->resp_hdrs, "Cache-Control");
expstr = apr_table_get(h->resp_hdrs, "Expires");
if (ap_cache_liststr(NULL, cc_cresp, "no-cache", NULL)) {
return 0;
}
if ((agestr = apr_table_get(h->resp_hdrs, "Age"))) {
age_c = apr_atoi64(agestr);
}
age = ap_cache_current_age(info, age_c, r->request_time);
if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "s-maxage", &val)
&& val != NULL) {
smaxage = apr_atoi64(val);
}
else {
smaxage = -1;
}
if (!conf->ignorecachecontrol
&& cc_req && ap_cache_liststr(r->pool, cc_req, "max-age", &val)
&& val != NULL) {
maxage_req = apr_atoi64(val);
}
else {
maxage_req = -1;
}
if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "max-age", &val)
&& val != NULL) {
maxage_cresp = apr_atoi64(val);
}
else {
maxage_cresp = -1;
}
if (maxage_req == -1) {
maxage = maxage_cresp;
}
else if (maxage_cresp == -1) {
maxage = maxage_req;
}
else {
maxage = MIN(maxage_req, maxage_cresp);
}
if (cc_req && ap_cache_liststr(r->pool, cc_req, "max-stale", &val)) {
if(val != NULL) {
maxstale = apr_atoi64(val);
}
else {
maxstale = APR_INT64_C(86400*365);
}
}
else {
maxstale = 0;
}
if (!conf->ignorecachecontrol
&& cc_req && ap_cache_liststr(r->pool, cc_req, "min-fresh", &val)
&& val != NULL) {
minfresh = apr_atoi64(val);
}
else {
minfresh = 0;
}
if (maxstale && ((cc_cresp &&
ap_cache_liststr(NULL, cc_cresp,
"must-revalidate", NULL)) ||
(cc_cresp &&
ap_cache_liststr(NULL, cc_cresp,
"proxy-revalidate", NULL)))) {
maxstale = 0;
}
if (((smaxage != -1) && (age < (smaxage - minfresh))) ||
((maxage != -1) && (age < (maxage + maxstale - minfresh))) ||
((smaxage == -1) && (maxage == -1) &&
(info->expire != APR_DATE_BAD) &&
(age < (apr_time_sec(info->expire - info->date) + maxstale - minfresh)))) {
warn_head = apr_table_get(h->resp_hdrs, "Warning");
apr_table_set(h->resp_hdrs, "Age",
apr_psprintf(r->pool, "%lu", (unsigned long)age));
if (!(((smaxage != -1) && age < smaxage) ||
((maxage != -1) && age < maxage) ||
(info->expire != APR_DATE_BAD &&
(apr_time_sec(info->expire - info->date)) > age))) {
if ((warn_head == NULL) ||
((warn_head != NULL) && (ap_strstr_c(warn_head, "110") == NULL))) {
apr_table_merge(h->resp_hdrs, "Warning",
"110 Response is stale");
}
}
if ((maxage_cresp == -1) && (smaxage == -1) &&
(expstr == NULL) && (age > 86400)) {
if ((warn_head == NULL) ||
((warn_head != NULL) && (ap_strstr_c(warn_head, "113") == NULL))) {
apr_table_merge(h->resp_hdrs, "Warning",
"113 Heuristic expiration");
}
}
return 1;
}
status = ap_cache_try_lock(conf, r, (char *)h->cache_obj->key);
if (APR_SUCCESS == status) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"Cache lock obtained for stale cached URL, "
"revalidating entry: %s",
r->unparsed_uri);
return 0;
}
else if (APR_EEXIST == status) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"Cache already locked for stale cached URL, "
"pretend it is fresh: %s",
r->unparsed_uri);
warn_head = apr_table_get(h->resp_hdrs, "Warning");
if ((warn_head == NULL) ||
((warn_head != NULL) && (ap_strstr_c(warn_head, "110") == NULL))) {
apr_table_merge(h->resp_hdrs, "Warning",
"110 Response is stale");
}
return 1;
}
else {
ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
"Attempt to obtain a cache lock for stale "
"cached URL failed, revalidating entry anyway: %s",
r->unparsed_uri);
return 0;
}
}
CACHE_DECLARE(int) ap_cache_liststr(apr_pool_t *p, const char *list,
const char *key, char **val)
{
apr_size_t key_len;
const char *next;
if (!list) {
return 0;
}
key_len = strlen(key);
next = list;
for (;;) {
while (*next && (apr_isspace(*next) || (*next == ','))) {
next++;
}
if (!*next) {
return 0;
}
if (!strncasecmp(next, key, key_len)) {
next += key_len;
if (!*next || (*next == '=') || apr_isspace(*next) ||
(*next == ',')) {
if (val) {
while (*next && (*next != '=') && (*next != ',')) {
next++;
}
if (*next == '=') {
next++;
while (*next && apr_isspace(*next )) {
next++;
}
if (!*next) {
*val = NULL;
}
else {
const char *val_start = next;
while (*next && !apr_isspace(*next) &&
(*next != ',')) {
next++;
}
*val = apr_pstrmemdup(p, val_start,
next - val_start);
}
}
else {
*val = NULL;
}
}
return 1;
}
}
do {
next++;
if (!*next) {
return 0;
}
} while (*next != ',');
}
}
CACHE_DECLARE(const char *)ap_cache_tokstr(apr_pool_t *p, const char *list,
const char **str)
{
apr_size_t i;
const char *s;
s = ap_strchr_c(list, ',');
if (s != NULL) {
i = s - list;
do
s++;
while (apr_isspace(*s))
;
}
else
i = strlen(list);
while (i > 0 && apr_isspace(list[i - 1]))
i--;
*str = s;
if (i)
return apr_pstrndup(p, list, i);
else
return NULL;
}
CACHE_DECLARE(apr_time_t) ap_cache_hex2usec(const char *x)
{
int i, ch;
apr_time_t j;
for (i = 0, j = 0; i < sizeof(j) * 2; i++) {
ch = x[i];
j <<= 4;
if (apr_isdigit(ch))
j |= ch - '0';
else if (apr_isupper(ch))
j |= ch - ('A' - 10);
else
j |= ch - ('a' - 10);
}
return j;
}
CACHE_DECLARE(void) ap_cache_usec2hex(apr_time_t j, char *y)
{
int i, ch;
for (i = (sizeof(j) * 2)-1; i >= 0; i--) {
ch = (int)(j & 0xF);
j >>= 4;
if (ch >= 10)
y[i] = ch + ('A' - 10);
else
y[i] = ch + '0';
}
y[sizeof(j) * 2] = '\0';
}
static void cache_hash(const char *it, char *val, int ndepth, int nlength)
{
apr_md5_ctx_t context;
unsigned char digest[16];
char tmp[22];
int i, k, d;
unsigned int x;
static const char enc_table[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
apr_md5_init(&context);
apr_md5_update(&context, (const unsigned char *) it, strlen(it));
apr_md5_final(digest, &context);
for (i = 0, k = 0; i < 15; i += 3) {
x = (digest[i] << 16) | (digest[i + 1] << 8) | digest[i + 2];
tmp[k++] = enc_table[x >> 18];
tmp[k++] = enc_table[(x >> 12) & 0x3f];
tmp[k++] = enc_table[(x >> 6) & 0x3f];
tmp[k++] = enc_table[x & 0x3f];
}
x = digest[15];
tmp[k++] = enc_table[x >> 2];
tmp[k++] = enc_table[(x << 4) & 0x3f];
for (i = k = d = 0; d < ndepth; ++d) {
memcpy(&val[i], &tmp[k], nlength);
k += nlength;
val[i + nlength] = '/';
i += nlength + 1;
}
memcpy(&val[i], &tmp[k], 22 - k);
val[i + 22 - k] = '\0';
}
CACHE_DECLARE(char *)ap_cache_generate_name(apr_pool_t *p, int dirlevels,
int dirlength, const char *name)
{
char hashfile[66];
cache_hash(name, hashfile, dirlevels, dirlength);
return apr_pstrdup(p, hashfile);
}
CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_hdrs_out(apr_pool_t *pool,
apr_table_t *t,
server_rec *s)
{
cache_server_conf *conf;
char **header;
int i;
apr_table_t *headers_out;
headers_out = apr_table_copy(pool, t);
apr_table_unset(headers_out, "Connection");
apr_table_unset(headers_out, "Keep-Alive");
apr_table_unset(headers_out, "Proxy-Authenticate");
apr_table_unset(headers_out, "Proxy-Authorization");
apr_table_unset(headers_out, "TE");
apr_table_unset(headers_out, "Trailers");
apr_table_unset(headers_out, "Transfer-Encoding");
apr_table_unset(headers_out, "Upgrade");
conf = (cache_server_conf *)ap_get_module_config(s->module_config,
&cache_module);
header = (char **)conf->ignore_headers->elts;
for (i = 0; i < conf->ignore_headers->nelts; i++) {
apr_table_unset(headers_out, header[i]);
}
return headers_out;
}