cache_util.c revision 4a315156bb61dcbae4d7b9769751832362fd148c
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * contributor license agreements. See the NOTICE file distributed with
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * this work for additional information regarding copyright ownership.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * The ASF licenses this file to You under the Apache License, Version 2.0
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * (the "License"); you may not use this file except in compliance with
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the License. You may obtain a copy of the License at
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Unless required by applicable law or agreed to in writing, software
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * distributed under the License is distributed on an "AS IS" BASIS,
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * See the License for the specific language governing permissions and
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * limitations under the License.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding/* -------------------------------------------------------------- */
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffieldingextern APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding/* Determine if "url" matches the hostname, scheme and port and path
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * in "filter". All but the path comparisons are case-insensitive.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int uri_meets_conditions(apr_uri_t filter, int pathlen, apr_uri_t url)
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding /* Compare the hostnames */
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding else if (strcasecmp(filter.hostname, url.hostname)) {
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding /* Compare the schemes */
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding /* Compare the ports */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* NOTE: ap_port_of_scheme will return 0 if given NULL input */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (filter.port != apr_uri_port_of_scheme(url.scheme)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (apr_uri_port_of_scheme(filter.scheme) == url.port) {
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein /* For HTTP caching purposes, an empty (NULL) path is equivalent to
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein * a single "/" path. RFCs 3986/2396
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein /* Url has met all of the filter conditions so far, determine
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * if the paths match.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(cache_provider_list *)ap_cache_get_providers(request_rec *r,
49d54ef49e3f7620260795178f3c6ba6ef3f21e9bjh /* loop through all the cacheenable entries */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (uri_meets_conditions(ent[i].url, ent[i].pathlen, uri)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Fetch from global config and add to the list. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding provider = ap_lookup_provider(CACHE_PROVIDER_GROUP, ent[i].type,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Log an error! */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding newp = apr_pcalloc(r->pool, sizeof(cache_provider_list));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* then loop through all the cachedisable entries
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Looking for urls that contain the full cachedisable url and possibly
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * This means we are disabling cachedisable url and below...
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (uri_meets_conditions(ent[i].url, ent[i].pathlen, uri)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Stop searching now. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* do a HTTP/1.1 age calculation */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(apr_int64_t) ap_cache_current_age(cache_info *info,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_time_t apparent_age, corrected_received_age, response_delay,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Perform an HTTP/1.1 age calculation. (RFC2616 13.2.3) */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apparent_age = MAX(0, info->response_time - info->date);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding corrected_received_age = MAX(apparent_age, age_value_usec);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding response_delay = info->response_time - info->request_time;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding corrected_initial_age = corrected_received_age + response_delay;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding current_age = corrected_initial_age + resident_time;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Try obtain a cache wide lock on the given cache key.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If we return APR_SUCCESS, we obtained the lock, and we are clear to
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * proceed to the backend. If we return APR_EEXISTS, then the lock is
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * already locked, someone else has gone to refresh the backend data
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * already, so we must return stale data with a warning in the mean
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * time. If we return anything else, then something has gone pear
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * shaped, and we allow the request through to the backend regardless.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * This lock is created from the request pool, meaning that should
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * something go wrong and the lock isn't deleted on return of the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * request headers from the backend for whatever reason, at worst the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * lock will be cleaned up when the request dies or finishes.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If something goes truly bananas and the lock isn't deleted when the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * request dies, the lock will be trashed when its max-age is reached,
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * or when a request arrives containing a Cache-Control: no-cache. At
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * no point is it possible for this lock to permanently deny access to
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * the backend.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(apr_status_t) ap_cache_try_lock(cache_server_conf *conf,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *lockname;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb const char *path;
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb /* no locks configured, leave */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* lock already obtained earlier? if so, success */
db2ef76dc744b13b9a9929ebb9e6f94712a81717rbb apr_pool_userdata_get(&dummy, CACHE_LOCKFILE_KEY, r->pool);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* create the key if it doesn't exist */
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb /* create a hashed filename from the key, and save it for later */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lockname = ap_cache_generate_name(r->pool, 0, 0, key);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* lock files represent discrete just-went-stale URLs "in flight", so
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * we support a simple two level directory structure, more is overkill.
a952e0c6ece342d88f2bc04e20dcfc563398eed3rbb /* make the directories */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (APR_SUCCESS != (status = apr_dir_make_recursive(path,
2b484455736f4c30447aa852764f53282cbeb5ddrbb "Could not create a cache lock directory: %s",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lockname = apr_pstrcat(r->pool, path, "/", lockname, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_pool_userdata_set(lockname, CACHE_LOCKNAME_KEY, NULL, r->pool);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* is an existing lock file too old? */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!(APR_STATUS_IS_ENOENT(status)) && APR_SUCCESS != status) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_error(APLOG_MARK, APLOG_ERR, APR_EEXIST, r->server,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Could not stat a cache lock file: %s",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((status == APR_SUCCESS) && (((now - finfo.mtime) > conf->lockmaxage)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_error(APLOG_MARK, APLOG_INFO, status, r->server,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Cache lock file for '%s' too old, removing: %s",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* try obtain a lock on the file */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (APR_SUCCESS == (status = apr_file_open(&lockfile, lockname,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding APR_WRITE | APR_CREATE | APR_EXCL | APR_DELONCLOSE,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_pool_userdata_set(lockfile, CACHE_LOCKFILE_KEY, NULL, r->pool);
2b484455736f4c30447aa852764f53282cbeb5ddrbb * Remove the cache lock, if present.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * First, try to close the file handle, whose delete-on-close should
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * kill the file. Otherwise, just delete the file by name.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If no lock name has yet been calculated, do the calculation of the
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * lock name first before trying to delete the file.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * If an optional bucket brigade is passed, the lock will only be
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * removed if the bucket brigade contains an EOS bucket.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmCACHE_DECLARE(apr_status_t) ap_cache_remove_lock(cache_server_conf *conf,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding request_rec *r, char *key, apr_bucket_brigade *bb) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *lockname;
c57161db43fe4cf5a62758d7d0834675e59cad0astoddard /* no locks configured, leave */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* no eos found in brigade, don't delete anything just yet,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * we are not done.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_pool_userdata_get(&dummy, CACHE_LOCKFILE_KEY, r->pool);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_pool_userdata_get(&dummy, CACHE_LOCKNAME_KEY, r->pool);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* create the key if it doesn't exist */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* create a hashed filename from the key, and save it for later */
4e1e8abb2a6e588366a4680b039b460fc5fe1ccdstoddard /* lock files represent discrete just-went-stale URLs "in flight", so
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * we support a simple two level directory structure, more is overkill.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lockname = apr_pstrcat(r->pool, conf->lockpath, dir, "/", lockname, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(int) ap_cache_check_freshness(cache_handle_t *h,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_int64_t age, maxage_req, maxage_cresp, maxage, smaxage, maxstale;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *pragma;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (cache_server_conf *)ap_get_module_config(r->server->module_config,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * We now want to check if our cached data is still fresh. This depends
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * on a few things, in this order:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - RFC2616 14.9.4 End to end reload, Cache-Control: no-cache. no-cache in
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * either the request or the cached response means that we must
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * revalidate the request unconditionally, overriding any expiration
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * mechanism. It's equivalent to max-age=0,must-revalidate.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - RFC2616 14.32 Pragma: no-cache This is treated the same as
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Cache-Control: no-cache.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - RFC2616 14.9.3 Cache-Control: max-stale, must-revalidate,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * proxy-revalidate if the max-stale request header exists, modify the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * stale calculations below so that an object can be at most <max-stale>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * seconds stale before we request a revalidation, _UNLESS_ a
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * must-revalidate or proxy-revalidate cached response header exists to
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * stop us doing this.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - RFC2616 14.9.3 Cache-Control: s-maxage the origin server specifies the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * maximum age an object can be before it is considered stale. This
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * directive has the effect of proxy|must revalidate, which in turn means
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * simple ignore any max-stale setting.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - RFC2616 14.9.4 Cache-Control: max-age this header can appear in both
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * requests and responses. If both are specified, the smaller of the two
2b484455736f4c30447aa852764f53282cbeb5ddrbb * takes priority.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * - RFC2616 14.21 Expires: if this request header exists in the cached
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe * entity, and it's value is in the past, it has expired.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* This value comes from the client's initial request. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cc_req = apr_table_get(r->headers_in, "Cache-Control");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Treat as stale, causing revalidation */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Incoming request is asking for a uncached version of "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "%s, but we know better and are ignoring it",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* These come from the cached entity. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cc_cresp = apr_table_get(h->resp_hdrs, "Cache-Control");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (ap_cache_liststr(NULL, cc_cresp, "no-cache", NULL)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * The cached entity contained Cache-Control: no-cache, so treat as
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * stale causing revalidation
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((agestr = apr_table_get(h->resp_hdrs, "Age"))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* calculate age of object */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding age = ap_cache_current_age(info, age_c, r->request_time);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* extract s-maxage */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "s-maxage", &val)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* extract max-age from request */
d2e443764d6855a4a06c8eb2b7708ab9b465ddeerbb && cc_req && ap_cache_liststr(r->pool, cc_req, "max-age", &val)
e7daae8d2ef0165e83627188bf33655c0b8266e8wrowe /* extract max-age from response */
e7daae8d2ef0165e83627188bf33655c0b8266e8wrowe if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "max-age", &val)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * if both maxage request and response, the smaller one takes priority
3597d6b10c88c252f156fe8061b659a38dea38c2rbb /* extract max-stale */
3597d6b10c88c252f156fe8061b659a38dea38c2rbb if (cc_req && ap_cache_liststr(r->pool, cc_req, "max-stale", &val)) {
3597d6b10c88c252f156fe8061b659a38dea38c2rbb * If no value is assigned to max-stale, then the client is willing
3597d6b10c88c252f156fe8061b659a38dea38c2rbb * to accept a stale response of any age (RFC2616 14.9.3). We will
3597d6b10c88c252f156fe8061b659a38dea38c2rbb * set it to one year in this case as this situation is somewhat
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * similar to a "never expires" Expires header (RFC2616 14.21)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * which is set to a date one year from the time the response is
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * sent in this case.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* extract min-fresh */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding && cc_req && ap_cache_liststr(r->pool, cc_req, "min-fresh", &val)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* override maxstale if must-revalidate or proxy-revalidate */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* handle expiration */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (((smaxage != -1) && (age < (smaxage - minfresh))) ||
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ((maxage != -1) && (age < (maxage + maxstale - minfresh))) ||
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (age < (apr_time_sec(info->expire - info->date) + maxstale - minfresh)))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding warn_head = apr_table_get(h->resp_hdrs, "Warning");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* it's fresh darlings... */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* set age header on response */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* add warning if maxstale overrode freshness calculation */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (apr_time_sec(info->expire - info->date)) > age))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* make sure we don't stomp on a previous warning */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ((warn_head != NULL) && (ap_strstr_c(warn_head, "110") == NULL))) {
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb "110 Response is stale");
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * If none of Expires, Cache-Control: max-age, or Cache-Control:
cf5ace98ead10d803fda70674cb4dda30fddb832trawick * s-maxage appears in the response, and the response header age
cf5ace98ead10d803fda70674cb4dda30fddb832trawick * calculated is more than 24 hours add the warning 113
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Make sure we don't stomp on a previous warning, and don't dup
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * a 113 marning that is already present. Also, make sure to add
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * the new warning to the correct *headers_out location.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ((warn_head != NULL) && (ap_strstr_c(warn_head, "113") == NULL))) {
3d96ee83babeec32482c9082c9426340cee8c44dwrowe "113 Heuristic expiration");
6db54801798f6021bb464de7b3fdcc9dc3150f7bdgaudet * At this point we are stale, but: if we are under load, we may let
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * a significant number of stale requests through before the first
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * stale request successfully revalidates itself, causing a sudden
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * unexpected thundering herd which in turn brings angst and drama.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * We want the first stale request to go through as normal. But the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * second and subsequent request, we must pretend to be fresh until
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the first request comes back with either new content or confirmation
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * that the stale content is still fresh.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * To achieve this, we create a very simple file based lock based on
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe * the key of the cached object. We attempt to open the lock file with
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * exclusive write access. If we succeed, woohoo! we're first, and we
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * follow the stale path to the backend server. If we fail, oh well,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * we follow the fresh path, and avoid being a thundering herd.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * The lock lives only as long as the stale request that went on ahead.
db2ef76dc744b13b9a9929ebb9e6f94712a81717rbb * If the request succeeds, the lock is deleted. If the request fails,
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * the lock is deleted, and another request gets to make a new lock
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * and try again.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * At any time, a request marked "no-cache" will force a refresh,
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * ignoring the lock, ensuring an extended lockout is impossible.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * A lock that exceeds a maximum age will be deleted, and another
e4c4fcc82268e0192db234c74a6db784b879fffdrbb * request gets to make a new lock and try again.
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb status = ap_cache_try_lock(conf, r, (char *)h->cache_obj->key);
e4c4fcc82268e0192db234c74a6db784b879fffdrbb /* we obtained a lock, follow the stale path */
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb "Cache lock obtained for stale cached URL, "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "revalidating entry: %s",
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* lock already exists, return stale data anyway, with a warning */
db2ef76dc744b13b9a9929ebb9e6f94712a81717rbb "Cache already locked for stale cached URL, "
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein "pretend it is fresh: %s",
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb /* make sure we don't stomp on a previous warning */
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb ((warn_head != NULL) && (ap_strstr_c(warn_head, "110") == NULL))) {
20c85ca0d404e29972fb94c3d6236a264d1c77abstoddard "110 Response is stale");
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb /* some other error occurred, just treat the object as stale */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Attempt to obtain a cache lock for stale "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "cached URL failed, revalidating entry anyway: %s",
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb * list is a comma-separated list of case-insensitive tokens, with
d7150b84735ff36eeb3cde9c79ab2e69ee7ffd67rbb * optional whitespace around the tokens.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * The return returns 1 if the token val is found in the list, or 0
d7150b84735ff36eeb3cde9c79ab2e69ee7ffd67rbb * otherwise.
d7150b84735ff36eeb3cde9c79ab2e69ee7ffd67rbbCACHE_DECLARE(int) ap_cache_liststr(apr_pool_t *p, const char *list,
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb const char *next;
e4c4fcc82268e0192db234c74a6db784b879fffdrbb for (;;) {
2b484455736f4c30447aa852764f53282cbeb5ddrbb /* skip whitespace and commas to find the start of the next key */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm while (*next && (apr_isspace(*next) || (*next == ','))) {
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb /* this field matches the key (though it might just be
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * a prefix match, so make sure the match is followed
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * by either a space or an equals sign)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!*next || (*next == '=') || apr_isspace(*next) ||
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb /* valid match */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (*next && (*next != '=') && (*next != ',')) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* skip to the next field */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* return each comma separated token, one at a time */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(const char *)ap_cache_tokstr(apr_pool_t *p, const char *list,
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm const char **str)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (s != NULL) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ; /* noop */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Converts apr_time_t expressed as hex digits to
2b484455736f4c30447aa852764f53282cbeb5ddrbb * a true apr_time_t.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(apr_time_t) ap_cache_hex2usec(const char *x)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0, j = 0; i < sizeof(j) * 2; i++) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * Converts apr_time_t to apr_time_t expressed as hex digits.
int i, ch;
for (i = k = d = 0; d < ndepth; ++d) {
k += nlength;
apr_table_t *t,
server_rec *s)
char **header;
if (t == NULL) {
&cache_module);
return headers_out;
apr_table_t *t,
server_rec *s)
return ap_cache_cacheable_headers(p,t,s);
r->err_headers_out);
r->server);
&& r->content_type) {
&& r->content_encoding) {
r->content_encoding);
return headers_out;