cache_util.c revision a80dd6ffd7a1484e7f45e4665689bdd84fc97153
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* Copyright 2001-2004 The Apache Software Foundation
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes *
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
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes *
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * http://www.apache.org/licenses/LICENSE-2.0
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes *
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
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define CORE_PRIVATE
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#include "mod_cache.h"
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#include <ap_provider.h>
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* -------------------------------------------------------------- */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesextern module AP_MODULE_DECLARE_DATA cache_module;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* return true if the request is conditional */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(int) ap_cache_request_is_conditional(apr_table_t *table)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes{
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (apr_table_get(table, "If-Match") ||
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_table_get(table, "If-None-Match") ||
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes apr_table_get(table, "If-Modified-Since") ||
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes apr_table_get(table, "If-Unmodified-Since")) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes return 1;
f7d723f1ad88ed006c6caf4c2c6604b7c59dd172bnicholes }
f300a833b20ab4dee4839f186ebb01a488c59752fuankg return 0;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes}
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(cache_provider_list *)ap_cache_get_providers(request_rec *r,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes cache_server_conf *conf,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes const char *url)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes{
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes cache_provider_list *providers = NULL;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes int i;
148d643cf7bf8b3067f3d949e3e01e27d4c8b4fcbnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* we can't cache if there's no URL */
5b0c702735f2049038c50c7dc5dd2606086ee110bnicholes /* Is this case even possible?? */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (!url) return NULL;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* loop through all the cacheenable entries */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes for (i = 0; i < conf->cacheenable->nelts; i++) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes struct cache_enable *ent =
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes (struct cache_enable *)conf->cacheenable->elts;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if ((ent[i].url) && !strncasecmp(url, ent[i].url, ent[i].urllen)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* Fetch from global config and add to the list. */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes cache_provider *provider;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes provider = ap_lookup_provider(CACHE_PROVIDER_GROUP, ent[i].type,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes "0");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (!provider) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* Log an error! */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes cache_provider_list *newp;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes newp = apr_pcalloc(r->pool, sizeof(cache_provider_list));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes newp->provider_name = ent[i].type;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes newp->provider = provider;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (!providers) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes providers = newp;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes cache_provider_list *last = providers;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes while (last->next) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes last = last->next;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes last->next = newp;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg /* then loop through all the cachedisable entries
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Looking for urls that contain the full cachedisable url and possibly
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * more.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * This means we are disabling cachedisable url and below...
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes for (i = 0; i < conf->cachedisable->nelts; i++) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes struct cache_disable *ent =
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes (struct cache_disable *)conf->cachedisable->elts;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if ((ent[i].url) && !strncasecmp(url, ent[i].url, ent[i].urllen)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* Stop searching now. */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes return NULL;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg return providers;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes}
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* do a HTTP/1.1 age calculation */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(apr_int64_t) ap_cache_current_age(cache_info *info,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes const apr_time_t age_value,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_time_t now)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes{
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_time_t apparent_age, corrected_received_age, response_delay,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes corrected_initial_age, resident_time, current_age,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes age_value_usec;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes age_value_usec = apr_time_from_sec(age_value);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* Perform an HTTP/1.1 age calculation. (RFC2616 13.2.3) */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
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;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes resident_time = now - info->response_time;
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg current_age = corrected_initial_age + resident_time;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes return apr_time_sec(current_age);
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg}
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(int) ap_cache_check_freshness(cache_handle_t *h,
6c080a25f5991f40225209541c989d7e76c4a39dbnicholes request_rec *r)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes{
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_int64_t age, maxage_req, maxage_cresp, maxage, smaxage, maxstale;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_int64_t minfresh;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes int age_in_errhdr = 0;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes const char *cc_cresp, *cc_ceresp, *cc_req;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes const char *agestr = NULL;
41022996c916eb4ab2ec3204eb491b64779eb100bnicholes const char *expstr = NULL;
41022996c916eb4ab2ec3204eb491b64779eb100bnicholes char *val;
41022996c916eb4ab2ec3204eb491b64779eb100bnicholes apr_time_t age_c = 0;
41022996c916eb4ab2ec3204eb491b64779eb100bnicholes cache_info *info = &(h->cache_obj->info);
41022996c916eb4ab2ec3204eb491b64779eb100bnicholes
036436f4f4cdcd76186c0058891216545967043bbnicholes /*
41022996c916eb4ab2ec3204eb491b64779eb100bnicholes * We now want to check if our cached data is still fresh. This depends
41022996c916eb4ab2ec3204eb491b64779eb100bnicholes * on a few things, in this order:
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes *
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 *
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * - RFC2616 14.32 Pragma: no-cache This is treated the same as
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Cache-Control: no-cache.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes *
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 *
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 *
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 *
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 *
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes */
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
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg if ((agestr = apr_table_get(h->resp_hdrs, "Age"))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes age_c = apr_atoi64(agestr);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else if ((agestr = apr_table_get(h->resp_err_hdrs, "Age"))) {
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg age_c = apr_atoi64(agestr);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes age_in_errhdr = 1;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (!(expstr = apr_table_get(h->resp_err_hdrs, "Expires"))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes expstr = apr_table_get(h->resp_hdrs, "Expires");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* calculate age of object */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes age = ap_cache_current_age(info, age_c, r->request_time);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* extract s-maxage */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "s-maxage", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes smaxage = apr_atoi64(val);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else if (cc_ceresp && ap_cache_liststr(r->pool, cc_ceresp, "s-maxage", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes smaxage = apr_atoi64(val);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes smaxage = -1;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* extract max-age from request */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (cc_req && ap_cache_liststr(r->pool, cc_req, "max-age", &val)) {
56ab8639aed4d3b2f031d9c1160c5f40af01bdebjerenkrantz maxage_req = apr_atoi64(val);
286fed73f9d1474652034465d4048247c6e7341fbnicholes }
be88e49281c5becee364ab9c6a0576f9b9844e0fbnicholes else {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes maxage_req = -1;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* extract max-age from response */
56ab8639aed4d3b2f031d9c1160c5f40af01bdebjerenkrantz if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "max-age", &val)) {
56ab8639aed4d3b2f031d9c1160c5f40af01bdebjerenkrantz maxage_cresp = apr_atoi64(val);
56ab8639aed4d3b2f031d9c1160c5f40af01bdebjerenkrantz }
56ab8639aed4d3b2f031d9c1160c5f40af01bdebjerenkrantz else if (cc_ceresp && ap_cache_liststr(r->pool, cc_ceresp, "max-age", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes maxage_cresp = apr_atoi64(val);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else
f7d723f1ad88ed006c6caf4c2c6604b7c59dd172bnicholes {
f7d723f1ad88ed006c6caf4c2c6604b7c59dd172bnicholes maxage_cresp = -1;
4f935de4900dc064a1e145be5e48f6af77ac24fcbnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /*
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * if both maxage request and response, the smaller one takes priority
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (-1 == maxage_req) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes maxage = maxage_cresp;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
e961abd49ab1b184b356f63591d37083a5651451bnicholes else if (-1 == maxage_cresp) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes maxage = maxage_req;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes maxage = MIN(maxage_req, maxage_cresp);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* extract max-stale */
440cda576ca9ff6476e4a04bdb253c5023da15eejerenkrantz if (cc_req && ap_cache_liststr(r->pool, cc_req, "max-stale", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes maxstale = apr_atoi64(val);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes maxstale = 0;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
4a59e9d628fb857a45d516ad1cdc2a3499427e97fuankg
6f2fa094a76c27135a9825ca9492f9db0a1a3bc9bnicholes /* extract min-fresh */
78a20a6e7ad3a0229900ee54c7d11a65f647b663niq if (cc_req && ap_cache_liststr(r->pool, cc_req, "min-fresh", &val)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes minfresh = apr_atoi64(val);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
91f672664302529bb620e3265cccd861661d258cbnicholes else {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes minfresh = 0;
3163ad7b2076b0f6961dc1a1ddaa06b240eecb7cjorton }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* override maxstale if must-revalidate or proxy-revalidate */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (maxstale && ((cc_cresp &&
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ap_cache_liststr(NULL, cc_cresp,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes "must-revalidate", NULL)) ||
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes (cc_cresp &&
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes ap_cache_liststr(NULL, cc_cresp,
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes "proxy-revalidate", NULL)) ||
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes (cc_ceresp &&
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg ap_cache_liststr(NULL, cc_ceresp,
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes "must-revalidate", NULL)) ||
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes (cc_ceresp &&
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ap_cache_liststr(NULL, cc_ceresp,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes "proxy-revalidate", NULL)))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes maxstale = 0;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg /* handle expiration */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (((smaxage != -1) && (age < (smaxage - minfresh))) ||
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ((maxage != -1) && (age < (maxage + maxstale - minfresh))) ||
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ((smaxage == -1) && (maxage == -1) &&
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes (info->expire != APR_DATE_BAD) &&
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes (age < (apr_time_sec(info->expire - info->date) + maxstale - minfresh)))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes const char *warn_head;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_table_t *head_ptr;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes warn_head = apr_table_get(h->resp_hdrs, "Warning");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (warn_head != NULL) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes head_ptr = h->resp_hdrs;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes warn_head = apr_table_get(h->resp_err_hdrs, "Warning");
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes head_ptr = h->resp_err_hdrs;
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* it's fresh darlings... */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* set age header on response */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (age_in_errhdr) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_table_set(h->resp_err_hdrs, "Age",
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_psprintf(r->pool, "%lu", (unsigned long)age));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_table_set(h->resp_hdrs, "Age",
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_psprintf(r->pool, "%lu", (unsigned long)age));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* add warning if maxstale overrode freshness calculation */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (!(((smaxage != -1) && age < smaxage) ||
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ((maxage != -1) && age < maxage) ||
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes (info->expire != APR_DATE_BAD &&
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes (info->expire - info->date) > age))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* make sure we don't stomp on a previous warning */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if ((warn_head == NULL) ||
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes ((warn_head != NULL) && (ap_strstr_c(warn_head, "110") == NULL))) {
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes apr_table_merge(head_ptr, "Warning", "110 Response is stale");
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes }
9558e9fdb620dd6f42ca93beac6c3ab734086706bnicholes }
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg /*
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 */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if ((maxage_cresp == -1) && (smaxage == -1) &&
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes (expstr == NULL) && (age > 86400)) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
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 */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if ((warn_head == NULL) ||
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ((warn_head != NULL) && (ap_strstr_c(warn_head, "113") == NULL))) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes apr_table_merge(head_ptr, "Warning", "113 Heuristic expiration");
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes return 1; /* Cache object is fresh (enough) */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes return 0; /* Cache object is stale */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes}
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/*
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.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesCACHE_DECLARE(int) ap_cache_liststr(apr_pool_t *p, const char *list,
25b3aa26ee94824bb1e9ff2631aa1588b37e5f72fuankg const char *key, char **val)
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes{
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes apr_size_t key_len;
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes const char *next;
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes if (!list) {
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes return 0;
03ff116275bf4439816b8af6ea3ddabd469f8365fuankg }
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes key_len = strlen(key);
03ff116275bf4439816b8af6ea3ddabd469f8365fuankg next = list;
4883fd339815130dbe680e816ef00256e10b844ffuankg
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes for (;;) {
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg /* skip whitespace and commas to find the start of the next key */
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg while (*next && (apr_isspace(*next) || (*next == ','))) {
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg next++;
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg }
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg if (!*next) {
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg return 0;
755d7c84cbdd7bad94beede1d6b5a6526d3a1ccdfuankg }
4883fd339815130dbe680e816ef00256e10b844ffuankg
4883fd339815130dbe680e816ef00256e10b844ffuankg if (!strncasecmp(next, key, key_len)) {
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)
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes */
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg next += key_len;
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg if (!*next || (*next == '=') || apr_isspace(*next) ||
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg (*next == ',')) {
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg /* valid match */
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg if (val) {
0f7cc4b1d3c42262bcdced99f682778963e83ea7bnicholes while (*next && (*next != '=') && (*next != ',')) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes next++;
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes }
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes if (*next == '=') {
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes next++;
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes while (*next && apr_isspace(*next )) {
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes next++;
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes }
f2f3f241c00a7a4bd597e57a19023940e072918abnicholes if (!*next) {
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg *val = NULL;
273e1eccdc9e5b94f1e3e13e3ffca7360b6f461fbnicholes }
273e1eccdc9e5b94f1e3e13e3ffca7360b6f461fbnicholes else {
148d643cf7bf8b3067f3d949e3e01e27d4c8b4fcbnicholes const char *val_start = next;
273e1eccdc9e5b94f1e3e13e3ffca7360b6f461fbnicholes while (*next && !apr_isspace(*next) &&
273e1eccdc9e5b94f1e3e13e3ffca7360b6f461fbnicholes (*next != ',')) {
148d643cf7bf8b3067f3d949e3e01e27d4c8b4fcbnicholes next++;
273e1eccdc9e5b94f1e3e13e3ffca7360b6f461fbnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes *val = apr_pstrmemdup(p, val_start,
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes next - val_start);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
8410c53aaf5e0372a19d5f4d2bc696b9c609ce3cbnicholes }
8410c53aaf5e0372a19d5f4d2bc696b9c609ce3cbnicholes return 1;
f300a833b20ab4dee4839f186ebb01a488c59752fuankg }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes /* skip to the next field */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes do {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes next++;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (!*next) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes return 0;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
25b3aa26ee94824bb1e9ff2631aa1588b37e5f72fuankg } while (*next != ',');
25b3aa26ee94824bb1e9ff2631aa1588b37e5f72fuankg }
ce22ce4743d79a889dca64df4459c598e2c188c7fuankg}
/* return each comma separated token, one at a time */
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))
; /* noop */
}
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;
}
/*
* Converts apr_time_t expressed as hex digits to
* a true apr_time_t.
*/
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;
}
/*
* Converts apr_time_t to apr_time_t expressed as hex digits.
*/
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);
/* encode 128 bits as 22 characters, using a modified uuencoding
* the encoding is 3 bytes -> 4 characters* i.e. 128 bits is
* 5 x 3 bytes + 1 byte -> 5 * 4 characters + 2 characters
*/
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];
}
/* one byte left */
x = digest[15];
tmp[k++] = enc_table[x >> 2]; /* use up 6 bits */
tmp[k++] = enc_table[(x << 4) & 0x3f];
/* now split into directory levels */
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 *)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);
}
/* Create a new table consisting of those elements from an input
* headers table that are allowed to be stored in a cache.
*/
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;
/* Make a copy of the headers, and remove from
* the copy any hop-by-hop headers, as defined in Section
* 13.5.1 of RFC 2616
*/
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);
/* Remove the user defined headers set with CacheIgnoreHeaders.
* This may break RFC 2616 compliance on behalf of the administrator.
*/
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;
}