cache_util.c revision a80dd6ffd7a1484e7f45e4665689bdd84fc97153
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* Copyright 2001-2004 The Apache Software Foundation
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Licensed under the Apache License, Version 2.0 (the "License");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * you may not use this file except in compliance with the License.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * You may obtain a copy of the License at
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Unless required by applicable law or agreed to in writing, software
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg * distributed under the License is distributed on an "AS IS" BASIS,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * See the License for the specific language governing permissions and
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * limitations under the License.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* -------------------------------------------------------------- */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* return true if the request is conditional */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(int) ap_cache_request_is_conditional(apr_table_t *table)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(cache_provider_list *)ap_cache_get_providers(request_rec *r,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes const char *url)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* we can't cache if there's no URL */
5b0c702735f2049038c50c7dc5dd2606086ee110bnicholes /* Is this case even possible?? */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* loop through all the cacheenable entries */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if ((ent[i].url) && !strncasecmp(url, ent[i].url, ent[i].urllen)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* Fetch from global config and add to the list. */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes provider = ap_lookup_provider(CACHE_PROVIDER_GROUP, ent[i].type,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* Log an error! */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes newp = apr_pcalloc(r->pool, sizeof(cache_provider_list));
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg /* then loop through all the cachedisable entries
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Looking for urls that contain the full cachedisable url and possibly
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * This means we are disabling cachedisable url and below...
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if ((ent[i].url) && !strncasecmp(url, ent[i].url, ent[i].urllen)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* Stop searching now. */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* do a HTTP/1.1 age calculation */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(apr_int64_t) ap_cache_current_age(cache_info *info,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_time_t apparent_age, corrected_received_age, response_delay,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes corrected_initial_age, resident_time, current_age,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* Perform an HTTP/1.1 age calculation. (RFC2616 13.2.3) */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apparent_age = MAX(0, info->response_time - info->date);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes corrected_received_age = MAX(apparent_age, age_value_usec);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes response_delay = info->response_time - info->request_time;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes corrected_initial_age = corrected_received_age + response_delay;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(int) ap_cache_check_freshness(cache_handle_t *h,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_int64_t age, maxage_req, maxage_cresp, maxage, smaxage, maxstale;
41022996c916eb4ab2ec3204eb491b64779eb100bnicholes * We now want to check if our cached data is still fresh. This depends
41022996c916eb4ab2ec3204eb491b64779eb100bnicholes * on a few things, in this order:
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg * - RFC2616 14.9.4 End to end reload, Cache-Control: no-cache. no-cache in
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * either the request or the cached response means that we must
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * revalidate the request unconditionally, overriding any expiration
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg * mechanism. It's equivalent to max-age=0,must-revalidate.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * - RFC2616 14.32 Pragma: no-cache This is treated the same as
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Cache-Control: no-cache.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * - RFC2616 14.9.3 Cache-Control: max-stale, must-revalidate,
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg * proxy-revalidate if the max-stale request header exists, modify the
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * stale calculations below so that an object can be at most <max-stale>
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * seconds stale before we request a revalidation, _UNLESS_ a
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * must-revalidate or proxy-revalidate cached response header exists to
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * stop us doing this.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * - RFC2616 14.9.3 Cache-Control: s-maxage the origin server specifies the
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * maximum age an object can be before it is considered stale. This
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * directive has the effect of proxy|must revalidate, which in turn means
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * simple ignore any max-stale setting.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * - RFC2616 14.9.4 Cache-Control: max-age this header can appear in both
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * requests and responses. If both are specified, the smaller of the two
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * takes priority.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * - RFC2616 14.21 Expires: if this request header exists in the cached
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * entity, and it's value is in the past, it has expired.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes cc_cresp = apr_table_get(h->resp_hdrs, "Cache-Control");
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg cc_ceresp = apr_table_get(h->resp_err_hdrs, "Cache-Control");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes cc_req = apr_table_get(h->req_hdrs, "Cache-Control");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else if ((agestr = apr_table_get(h->resp_err_hdrs, "Age"))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (!(expstr = apr_table_get(h->resp_err_hdrs, "Expires"))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* calculate age of object */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes age = ap_cache_current_age(info, age_c, r->request_time);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* extract s-maxage */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "s-maxage", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else if (cc_ceresp && ap_cache_liststr(r->pool, cc_ceresp, "s-maxage", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* extract max-age from request */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (cc_req && ap_cache_liststr(r->pool, cc_req, "max-age", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* extract max-age from response */
56ab8639aed4d3b2f031d9c1160c5f40af01bdebjerenkrantz if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "max-age", &val)) {
56ab8639aed4d3b2f031d9c1160c5f40af01bdebjerenkrantz else if (cc_ceresp && ap_cache_liststr(r->pool, cc_ceresp, "max-age", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * if both maxage request and response, the smaller one takes priority
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* extract max-stale */
440cda576ca9ff6476e4a04bdb253c5023da15eejerenkrantz if (cc_req && ap_cache_liststr(r->pool, cc_req, "max-stale", &val)) {
6f2fa094a76c27135a9825ca9492f9db0a1a3bc9bnicholes /* extract min-fresh */
78a20a6e7ad3a0229900ee54c7d11a65f647b663niq if (cc_req && ap_cache_liststr(r->pool, cc_req, "min-fresh", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* override maxstale if must-revalidate or proxy-revalidate */
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg /* handle expiration */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (((smaxage != -1) && (age < (smaxage - minfresh))) ||
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ((maxage != -1) && (age < (maxage + maxstale - minfresh))) ||
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes (age < (apr_time_sec(info->expire - info->date) + maxstale - minfresh)))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes warn_head = apr_table_get(h->resp_hdrs, "Warning");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes warn_head = apr_table_get(h->resp_err_hdrs, "Warning");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* it's fresh darlings... */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* set age header on response */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_psprintf(r->pool, "%lu", (unsigned long)age));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_psprintf(r->pool, "%lu", (unsigned long)age));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* add warning if maxstale overrode freshness calculation */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* make sure we don't stomp on a previous warning */
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes ((warn_head != NULL) && (ap_strstr_c(warn_head, "110") == NULL))) {
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes apr_table_merge(head_ptr, "Warning", "110 Response is stale");
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes * If none of Expires, Cache-Control: max-age, or Cache-Control:
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg * s-maxage appears in the response, and the respose header age
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg * calculated is more than 24 hours add the warning 113
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* Make sure we don't stomp on a previous warning, and don't dup
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg * a 113 marning that is already present. Also, make sure to add
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg * the new warning to the correct *headers_out location.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ((warn_head != NULL) && (ap_strstr_c(warn_head, "113") == NULL))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_table_merge(head_ptr, "Warning", "113 Heuristic expiration");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes return 0; /* Cache object is stale */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * list is a comma-separated list of case-insensitive tokens, with
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * optional whitespace around the tokens.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * The return returns 1 if the token val is found in the list, or 0
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg * otherwise.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(int) ap_cache_liststr(apr_pool_t *p, const char *list,
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes const char *next;
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg /* skip whitespace and commas to find the start of the next key */
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg while (*next && (apr_isspace(*next) || (*next == ','))) {
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes /* this field matches the key (though it might just be
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes * a prefix match, so make sure the match is followed
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes * by either a space or an equals sign)
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg if (!*next || (*next == '=') || apr_isspace(*next) ||
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg /* valid match */
0f7cc4b1d3c42262bcdced99f682778963e83ea7bnicholes while (*next && (*next != '=') && (*next != ',')) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* skip to the next field */
const char **str)
apr_size_t i;
if (s != NULL) {
i = s - list;
while (apr_isspace(*s))
*str = s;
return NULL;
int i, ch;
apr_time_t j;
ch = x[i];
int i, ch;
for (i = k = d = 0; d < ndepth; ++d) {
k += nlength;
apr_table_t *t,
server_rec *s)
char **header;
&cache_module);
return headers_out;