#include "mod_cache.h"
#include "cache_util.h"
#include <ap_provider.h>
APLOG_USE_MODULE(cache);
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(const apr_uri_t *filter, const int pathlen,
const apr_uri_t *url)
{
if (!filter->scheme) {
if (url->scheme || url->hostname) {
return 0;
}
}
else {
if (!url->scheme || strcasecmp(filter->scheme, url->scheme)) {
return 0;
}
if (filter->hostname && filter->hostname[0]) {
if (filter->hostname[0] == '.') {
const size_t fhostlen = strlen(filter->hostname);
const size_t uhostlen = url->hostname ? strlen(url->hostname) : 0;
if (fhostlen > uhostlen
|| (url->hostname
&& strcasecmp(filter->hostname,
url->hostname + uhostlen - fhostlen))) {
return 0;
}
}
else if (filter->hostname[0] == '*') {
const size_t fhostlen = strlen(filter->hostname + 1);
const size_t uhostlen = url->hostname ? strlen(url->hostname) : 0;
if (fhostlen > uhostlen
|| (url->hostname
&& strcasecmp(filter->hostname + 1,
url->hostname + uhostlen - fhostlen))) {
return 0;
}
}
else if (!url->hostname || strcasecmp(filter->hostname, url->hostname)) {
return 0;
}
}
if (!(filter->port_str && !filter->port_str[0])) {
const unsigned fport = filter->port_str ? filter->port
: apr_uri_port_of_scheme(filter->scheme);
const unsigned uport = (url->port_str && url->port_str[0])
? url->port : apr_uri_port_of_scheme(url->scheme);
if (fport != uport) {
return 0;
}
}
}
if (!url->path) {
if (*filter->path == '/' && pathlen == 1) {
return 1;
}
else {
return 0;
}
}
return !strncmp(filter->path, url->path, pathlen);
}
static cache_provider_list *get_provider(request_rec *r, struct cache_enable *ent,
cache_provider_list *providers)
{
cache_provider *provider;
provider = ap_lookup_provider(CACHE_PROVIDER_GROUP, ent->type,
"0");
if (!provider) {
}
else {
cache_provider_list *newp;
newp = apr_pcalloc(r->pool, sizeof(cache_provider_list));
newp->provider_name = ent->type;
newp->provider = provider;
if (!providers) {
providers = newp;
}
else {
cache_provider_list *last = providers;
while (last->next) {
if (last->provider == provider) {
return providers;
}
last = last->next;
}
if (last->provider == provider) {
return providers;
}
last->next = newp;
}
}
return providers;
}
cache_provider_list *cache_get_providers(request_rec *r,
cache_server_conf *conf,
apr_uri_t uri)
{
cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &cache_module);
cache_provider_list *providers = NULL;
int i;
if (dconf->disable) {
return NULL;
}
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;
}
}
for (i = 0; i < dconf->cacheenable->nelts; i++) {
struct cache_enable *ent =
(struct cache_enable *)dconf->cacheenable->elts;
providers = get_provider(r, &ent[i], providers);
}
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)) {
providers = get_provider(r, &ent[i], providers);
}
}
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;
if (current_age < 0) {
current_age = 0;
}
return apr_time_sec(current_age);
}
apr_status_t cache_try_lock(cache_server_conf *conf, cache_request_rec *cache,
request_rec *r)
{
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 (!cache->key) {
cache_generate_key(r, r->pool, &cache->key);
}
lockname = ap_cache_generate_name(r->pool, 0, 0, cache->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_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00778)
"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_rerror(APLOG_MARK, APLOG_ERR, APR_EEXIST, r, APLOGNO(00779)
"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_rerror(APLOG_MARK, APLOG_INFO, status, r, APLOGNO(00780)
"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;
}
apr_status_t cache_remove_lock(cache_server_conf *conf,
cache_request_rec *cache, request_rec *r, 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 (!cache->key) {
cache_generate_key(r, r->pool, &cache->key);
}
lockname = ap_cache_generate_name(r->pool, 0, 0, cache->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);
}
int ap_cache_check_no_cache(cache_request_rec *cache, request_rec *r)
{
cache_server_conf *conf =
(cache_server_conf *)ap_get_module_config(r->server->module_config,
&cache_module);
if (!cache->control_in.parsed) {
const char *cc_req = cache_table_getm(r->pool, r->headers_in,
"Cache-Control");
const char *pragma = cache_table_getm(r->pool, r->headers_in, "Pragma");
ap_cache_control(r, &cache->control_in, cc_req, pragma, r->headers_in);
}
if (cache->control_in.no_cache) {
if (!conf->ignorecachecontrol) {
return 0;
}
else {
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
"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);
}
}
return 1;
}
int ap_cache_check_no_store(cache_request_rec *cache, request_rec *r)
{
cache_server_conf *conf =
(cache_server_conf *)ap_get_module_config(r->server->module_config,
&cache_module);
if (!cache->control_in.parsed) {
const char *cc_req = cache_table_getm(r->pool, r->headers_in,
"Cache-Control");
const char *pragma = cache_table_getm(r->pool, r->headers_in, "Pragma");
ap_cache_control(r, &cache->control_in, cc_req, pragma, r->headers_in);
}
if (cache->control_in.no_store) {
if (!conf->ignorecachecontrol) {
return 0;
}
else {
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
"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;
}
int cache_check_freshness(cache_handle_t *h, cache_request_rec *cache,
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_req;
const char *pragma;
const char *agestr = NULL;
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");
ap_cache_control(r, &cache->control_in, cc_req, pragma, r->headers_in);
if (cache->control_in.no_cache) {
if (!conf->ignorecachecontrol) {
return 0;
}
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00781)
"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);
}
if (h->cache_obj->info.control.no_cache
|| h->cache_obj->info.control.invalidated) {
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);
smaxage = h->cache_obj->info.control.s_maxage_value;
maxage_req = -1;
if (!conf->ignorecachecontrol) {
maxage_req = cache->control_in.max_age_value;
}
if (smaxage != -1) {
maxage_cresp = smaxage;
}
else {
maxage_cresp = h->cache_obj->info.control.max_age_value;
}
if (maxage_req == -1) {
maxage = maxage_cresp;
}
else if (maxage_cresp == -1) {
maxage = maxage_req;
}
else {
maxage = MIN(maxage_req, maxage_cresp);
}
if (cache->control_in.max_stale) {
if(cache->control_in.max_stale_value != -1) {
maxstale = cache->control_in.max_stale_value;
}
else {
maxstale = APR_INT64_C(86400*365);
}
}
else {
maxstale = 0;
}
if (!conf->ignorecachecontrol && cache->control_in.min_fresh) {
minfresh = cache->control_in.min_fresh_value;
}
else {
minfresh = 0;
}
if (maxstale && (h->cache_obj->info.control.must_revalidate
|| h->cache_obj->info.control.proxy_revalidate || smaxage != -1)) {
maxstale = 0;
}
if (((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 (!(((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_mergen(h->resp_hdrs, "Warning",
"110 Response is stale");
}
}
if ((maxage_cresp == -1) && (smaxage == -1) && (apr_table_get(
h->resp_hdrs, "Expires") == NULL) && (age > 86400)) {
if ((warn_head == NULL) ||
((warn_head != NULL) && (ap_strstr_c(warn_head, "113") == NULL))) {
apr_table_mergen(h->resp_hdrs, "Warning",
"113 Heuristic expiration");
}
}
return 1;
}
status = cache_try_lock(conf, cache, r);
if (APR_SUCCESS == status) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00782)
"Cache lock obtained for stale cached URL, "
"revalidating entry: %s",
r->unparsed_uri);
return 0;
}
else if (APR_EEXIST == status) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00783)
"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_mergen(h->resp_hdrs, "Warning",
"110 Response is stale");
}
return 1;
}
else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00784)
"Attempt to obtain a cache lock for stale "
"cached URL failed, revalidating entry anyway: %s",
r->unparsed_uri);
return 0;
}
}
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);
}
char *cache_strqtok(char *str, const char *sep, char **last)
{
char *token;
int quoted = 0;
if (!str) {
str = *last;
}
if (!str) {
return NULL;
}
while (*str && ap_strchr_c(sep, *str)) {
++str;
}
if (!*str) {
return NULL;
}
token = str;
*last = token;
while (**last) {
if (!quoted) {
if (**last == '\"' && !ap_strchr_c(sep, '\"')) {
quoted = 1;
++*last;
}
else if (!ap_strchr_c(sep, **last)) {
++*last;
}
else {
break;
}
}
else {
if (**last == '\"') {
quoted = 0;
++*last;
}
else if (**last == '\\') {
++*last;
if (**last) {
++*last;
}
}
else {
++*last;
}
}
}
if (**last) {
**last = '\0';
++*last;
}
return token;
}
int ap_cache_control(request_rec *r, cache_control_t *cc,
const char *cc_header, const char *pragma_header, apr_table_t *headers)
{
char *last;
if (cc->parsed) {
return cc->cache_control || cc->pragma;
}
cc->parsed = 1;
cc->max_age_value = -1;
cc->max_stale_value = -1;
cc->min_fresh_value = -1;
cc->s_maxage_value = -1;
if (pragma_header) {
char *header = apr_pstrdup(r->pool, pragma_header);
const char *token = cache_strqtok(header, CACHE_SEPARATOR, &last);
while (token) {
if (!strcmp(token, "no-cache")) {
cc->no_cache = 1;
}
else if (!strcasecmp(token, "no-cache")) {
cc->no_cache = 1;
}
token = cache_strqtok(NULL, CACHE_SEPARATOR, &last);
}
cc->pragma = 1;
}
if (cc_header) {
char *header = apr_pstrdup(r->pool, cc_header);
const char *token = cache_strqtok(header, CACHE_SEPARATOR, &last);
while (token) {
switch (token[0]) {
case 'n':
case 'N': {
if (!strcmp(token, "no-cache")) {
cc->no_cache = 1;
}
else if (!strcmp(token, "no-store")) {
cc->no_store = 1;
}
else if (!strncasecmp(token, "no-cache", 8)) {
if (token[8] == '=') {
cc->no_cache_header = 1;
}
else if (!token[8]) {
cc->no_cache = 1;
}
break;
}
else if (!strcasecmp(token, "no-store")) {
cc->no_store = 1;
}
else if (!strcasecmp(token, "no-transform")) {
cc->no_transform = 1;
}
break;
}
case 'm':
case 'M': {
if (!strcmp(token, "max-age=0")) {
cc->max_age = 1;
cc->max_age_value = 0;
}
else if (!strcmp(token, "must-revalidate")) {
cc->must_revalidate = 1;
}
else if (!strncasecmp(token, "max-age", 7)) {
if (token[7] == '=') {
cc->max_age = 1;
cc->max_age_value = apr_atoi64(token + 8);
}
break;
}
else if (!strncasecmp(token, "max-stale", 9)) {
if (token[9] == '=') {
cc->max_stale = 1;
cc->max_stale_value = apr_atoi64(token + 10);
}
else if (!token[10]) {
cc->max_stale = 1;
cc->max_stale_value = -1;
}
break;
}
else if (!strncasecmp(token, "min-fresh", 9)) {
if (token[9] == '=') {
cc->min_fresh = 1;
cc->min_fresh_value = apr_atoi64(token + 10);
}
break;
}
else if (!strcasecmp(token, "must-revalidate")) {
cc->must_revalidate = 1;
}
break;
}
case 'o':
case 'O': {
if (!strcasecmp(token, "only-if-cached")) {
cc->only_if_cached = 1;
}
break;
}
case 'p':
case 'P': {
if (!strcmp(token, "private")) {
cc->private = 1;
}
else if (!strcasecmp(token, "public")) {
cc->public = 1;
}
else if (!strncasecmp(token, "private", 7)) {
if (token[7] == '=') {
cc->private_header = 1;
}
else if (!token[7]) {
cc->private = 1;
}
break;
}
else if (!strcasecmp(token, "proxy-revalidate")) {
cc->proxy_revalidate = 1;
}
break;
}
case 's':
case 'S': {
if (!strncasecmp(token, "s-maxage", 8)) {
if (token[8] == '=') {
cc->s_maxage = 1;
cc->s_maxage_value = apr_atoi64(token + 9);
}
break;
}
break;
}
}
token = cache_strqtok(NULL, CACHE_SEPARATOR, &last);
}
cc->cache_control = 1;
}
return (cc_header != NULL || pragma_header != NULL);
}
static int cache_control_remove(request_rec *r, const char *cc_header,
apr_table_t *headers)
{
char *last, *slast;
int found = 0;
if (cc_header) {
char *header = apr_pstrdup(r->pool, cc_header);
char *token = cache_strqtok(header, CACHE_SEPARATOR, &last);
while (token) {
switch (token[0]) {
case 'n':
case 'N': {
if (!strncmp(token, "no-cache", 8)
|| !strncasecmp(token, "no-cache", 8)) {
if (token[8] == '=') {
const char *header = cache_strqtok(token + 9,
CACHE_SEPARATOR "\"", &slast);
while (header) {
apr_table_unset(headers, header);
header = cache_strqtok(NULL, CACHE_SEPARATOR "\"",
&slast);
}
found = 1;
}
break;
}
break;
}
case 'p':
case 'P': {
if (!strncmp(token, "private", 7)
|| !strncasecmp(token, "private", 7)) {
if (token[7] == '=') {
const char *header = cache_strqtok(token + 8,
CACHE_SEPARATOR "\"", &slast);
while (header) {
apr_table_unset(headers, header);
header = cache_strqtok(NULL, CACHE_SEPARATOR "\"",
&slast);
}
found = 1;
}
}
break;
}
}
token = cache_strqtok(NULL, CACHE_SEPARATOR, &last);
}
}
return found;
}
CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_headers(apr_pool_t *pool,
apr_table_t *t,
server_rec *s)
{
cache_server_conf *conf;
char **header;
int i;
apr_table_t *headers_out;
if (t == NULL) {
return apr_table_make(pool, 10);
};
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;
}
CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_headers_in(request_rec *r)
{
return ap_cache_cacheable_headers(r->pool, r->headers_in, r->server);
}
CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_headers_out(request_rec *r)
{
apr_table_t *headers_out;
headers_out = apr_table_overlay(r->pool, r->headers_out,
r->err_headers_out);
apr_table_clear(r->err_headers_out);
headers_out = ap_cache_cacheable_headers(r->pool, headers_out,
r->server);
cache_control_remove(r,
cache_table_getm(r->pool, headers_out, "Cache-Control"),
headers_out);
if (!apr_table_get(headers_out, "Content-Type")
&& r->content_type) {
apr_table_setn(headers_out, "Content-Type",
ap_make_content_type(r, r->content_type));
}
if (!apr_table_get(headers_out, "Content-Encoding")
&& r->content_encoding) {
apr_table_setn(headers_out, "Content-Encoding",
r->content_encoding);
}
return headers_out;
}
typedef struct
{
apr_pool_t *p;
const char *first;
apr_array_header_t *merged;
} cache_table_getm_t;
static int cache_table_getm_do(void *v, const char *key, const char *val)
{
cache_table_getm_t *state = (cache_table_getm_t *) v;
if (!state->first) {
state->first = val;
}
else {
const char **elt;
if (!state->merged) {
state->merged = apr_array_make(state->p, 10, sizeof(const char *));
elt = apr_array_push(state->merged);
*elt = state->first;
}
elt = apr_array_push(state->merged);
*elt = val;
}
return 1;
}
const char *cache_table_getm(apr_pool_t *p, const apr_table_t *t,
const char *key)
{
cache_table_getm_t state;
state.p = p;
state.first = NULL;
state.merged = NULL;
apr_table_do(cache_table_getm_do, &state, t, key, NULL);
if (!state.first) {
return NULL;
}
else if (!state.merged) {
return state.first;
}
else {
return apr_array_pstrcat(p, state.merged, ',');
}
}