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 *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * http://www.apache.org/licenses/LICENSE-2.0
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "mod_cache.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding#include <ap_provider.h>
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding/* -------------------------------------------------------------- */
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffieldingextern APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingextern module AP_MODULE_DECLARE_DATA cache_module;
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding/* Determine if "url" matches the hostname, scheme and port and path
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * in "filter". All but the path comparisons are case-insensitive.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int uri_meets_conditions(apr_uri_t filter, int pathlen, apr_uri_t url)
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding{
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding /* Compare the hostnames */
64185f9824e42f21ca7b9ae6c004484215c031a7rbb if(filter.hostname) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!url.hostname) {
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding return 0;
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding }
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding else if (strcasecmp(filter.hostname, url.hostname)) {
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding }
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding /* Compare the schemes */
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding if(filter.scheme) {
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding if (!url.scheme) {
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding return 0;
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (strcasecmp(filter.scheme, url.scheme)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding }
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding /* Compare the ports */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if(filter.port_str) {
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding if (url.port_str && filter.port != url.port) {
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding return 0;
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding }
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 return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if(url.port_str && filter.scheme) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (apr_uri_port_of_scheme(filter.scheme) == url.port) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein }
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein /* For HTTP caching purposes, an empty (NULL) path is equivalent to
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein * a single "/" path. RFCs 3986/2396
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein */
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein if (!url.path) {
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein if (*filter.path == '/' && pathlen == 1) {
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein return 1;
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein }
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein else {
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein return 0;
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein /* Url has met all of the filter conditions so far, determine
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * if the paths match.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return !strncmp(filter.path, url.path, pathlen);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(cache_provider_list *)ap_cache_get_providers(request_rec *r,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cache_server_conf *conf,
d41217398f0e1031adbb6f5bd37f45737c805deftrawick apr_uri_t uri)
d41217398f0e1031adbb6f5bd37f45737c805deftrawick{
d41217398f0e1031adbb6f5bd37f45737c805deftrawick cache_provider_list *providers = NULL;
49d54ef49e3f7620260795178f3c6ba6ef3f21e9bjh int i;
49d54ef49e3f7620260795178f3c6ba6ef3f21e9bjh
49d54ef49e3f7620260795178f3c6ba6ef3f21e9bjh /* loop through all the cacheenable entries */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < conf->cacheenable->nelts; i++) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding struct cache_enable *ent =
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (struct cache_enable *)conf->cacheenable->elts;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (uri_meets_conditions(ent[i].url, ent[i].pathlen, uri)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Fetch from global config and add to the list. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cache_provider *provider;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding provider = ap_lookup_provider(CACHE_PROVIDER_GROUP, ent[i].type,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "0");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!provider) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Log an error! */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cache_provider_list *newp;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding newp = apr_pcalloc(r->pool, sizeof(cache_provider_list));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding newp->provider_name = ent[i].type;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding newp->provider = provider;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!providers) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding providers = newp;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cache_provider_list *last = providers;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (last->next) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding last = last->next;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding last->next = newp;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* then loop through all the cachedisable entries
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Looking for urls that contain the full cachedisable url and possibly
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * more.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * This means we are disabling cachedisable url and below...
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < conf->cachedisable->nelts; i++) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding struct cache_disable *ent =
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (struct cache_disable *)conf->cachedisable->elts;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (uri_meets_conditions(ent[i].url, ent[i].pathlen, uri)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Stop searching now. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return providers;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* do a HTTP/1.1 age calculation */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(apr_int64_t) ap_cache_current_age(cache_info *info,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const apr_time_t age_value,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_time_t now)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_time_t apparent_age, corrected_received_age, response_delay,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding corrected_initial_age, resident_time, current_age,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding age_value_usec;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding age_value_usec = apr_time_from_sec(age_value);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Perform an HTTP/1.1 age calculation. (RFC2616 13.2.3) */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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 resident_time = now - info->response_time;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding current_age = corrected_initial_age + resident_time;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return apr_time_sec(current_age);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/**
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Try obtain a cache wide lock on the given cache key.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 *
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(apr_status_t) ap_cache_try_lock(cache_server_conf *conf,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding request_rec *r, char *key) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_status_t status;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *lockname;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb const char *path;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm char dir[5];
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_time_t now = apr_time_now();
a952e0c6ece342d88f2bc04e20dcfc563398eed3rbb apr_finfo_t finfo;
ec727afb817df0e5202135278c0d2902923cc980stoddard apr_file_t *lockfile;
ec727afb817df0e5202135278c0d2902923cc980stoddard void *dummy;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb finfo.mtime = 0;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb if (!conf || !conf->lock || !conf->lockpath) {
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb /* no locks configured, leave */
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb return APR_SUCCESS;
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* lock already obtained earlier? if so, success */
db2ef76dc744b13b9a9929ebb9e6f94712a81717rbb apr_pool_userdata_get(&dummy, CACHE_LOCKFILE_KEY, r->pool);
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein if (dummy) {
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein return APR_SUCCESS;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb }
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* create the key if it doesn't exist */
20c85ca0d404e29972fb94c3d6236a264d1c77abstoddard if (!key) {
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb cache_generate_key(r, r->pool, &key);
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb }
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb /* create a hashed filename from the key, and save it for later */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lockname = ap_cache_generate_name(r->pool, 0, 0, key);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* lock files represent discrete just-went-stale URLs "in flight", so
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * we support a simple two level directory structure, more is overkill.
061c3874622b67beff724876b9bd1b1f291f2eaafielding */
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb dir[0] = '/';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding dir[1] = lockname[0];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding dir[2] = '/';
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm dir[3] = lockname[1];
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb dir[4] = 0;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb
a952e0c6ece342d88f2bc04e20dcfc563398eed3rbb /* make the directories */
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb path = apr_pstrcat(r->pool, conf->lockpath, dir, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (APR_SUCCESS != (status = apr_dir_make_recursive(path,
2b484455736f4c30447aa852764f53282cbeb5ddrbb APR_UREAD|APR_UWRITE|APR_UEXECUTE, r->pool))) {
2b484455736f4c30447aa852764f53282cbeb5ddrbb ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
2b484455736f4c30447aa852764f53282cbeb5ddrbb "Could not create a cache lock directory: %s",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding path);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return status;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lockname = apr_pstrcat(r->pool, path, "/", lockname, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_pool_userdata_set(lockname, CACHE_LOCKNAME_KEY, NULL, r->pool);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* is an existing lock file too old? */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding status = apr_stat(&finfo, lockname,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding APR_FINFO_MTIME | APR_FINFO_NLINK, r->pool);
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 lockname);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return status;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((status == APR_SUCCESS) && (((now - finfo.mtime) > conf->lockmaxage)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding || (now < finfo.mtime))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_error(APLOG_MARK, APLOG_INFO, status, r->server,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Cache lock file for '%s' too old, removing: %s",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->uri, lockname);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_file_remove(lockname, r->pool);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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_UREAD | APR_UWRITE, r->pool))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_pool_userdata_set(lockfile, CACHE_LOCKFILE_KEY, NULL, r->pool);
061c3874622b67beff724876b9bd1b1f291f2eaafielding }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm return status;
95ed69df3d43a7d2758720bad8950f07587bdeb9bjh
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
2b484455736f4c30447aa852764f53282cbeb5ddrbb
2b484455736f4c30447aa852764f53282cbeb5ddrbb/**
2b484455736f4c30447aa852764f53282cbeb5ddrbb * Remove the cache lock, if present.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If no lock name has yet been calculated, do the calculation of the
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * lock name first before trying to delete the file.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * If an optional bucket brigade is passed, the lock will only be
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * removed if the bucket brigade contains an EOS bucket.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmCACHE_DECLARE(apr_status_t) ap_cache_remove_lock(cache_server_conf *conf,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding request_rec *r, char *key, apr_bucket_brigade *bb) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding void *dummy;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *lockname;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!conf || !conf->lock || !conf->lockpath) {
c57161db43fe4cf5a62758d7d0834675e59cad0astoddard /* no locks configured, leave */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm return APR_SUCCESS;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (bb) {
4e1e8abb2a6e588366a4680b039b460fc5fe1ccdstoddard apr_bucket *e;
ec727afb817df0e5202135278c0d2902923cc980stoddard int eos_found = 0;
ec727afb817df0e5202135278c0d2902923cc980stoddard for (e = APR_BRIGADE_FIRST(bb);
ec727afb817df0e5202135278c0d2902923cc980stoddard e != APR_BRIGADE_SENTINEL(bb);
ec727afb817df0e5202135278c0d2902923cc980stoddard e = APR_BUCKET_NEXT(e))
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (APR_BUCKET_IS_EOS(e)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding eos_found = 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding break;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
0942697a9b5de44865676345a3828741c827efe6rbb if (!eos_found) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* no eos found in brigade, don't delete anything just yet,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * we are not done.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return APR_SUCCESS;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_pool_userdata_get(&dummy, CACHE_LOCKFILE_KEY, r->pool);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (dummy) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return apr_file_close((apr_file_t *)dummy);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_pool_userdata_get(&dummy, CACHE_LOCKNAME_KEY, r->pool);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lockname = (const char *)dummy;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!lockname) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char dir[5];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* create the key if it doesn't exist */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!key) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cache_generate_key(r, r->pool, &key);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* create a hashed filename from the key, and save it for later */
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb lockname = ap_cache_generate_name(r->pool, 0, 0, key);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
4e1e8abb2a6e588366a4680b039b460fc5fe1ccdstoddard /* lock files represent discrete just-went-stale URLs "in flight", so
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * we support a simple two level directory structure, more is overkill.
40303323ae9960bd38a0f03d2c10e897a7badd88rbb */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding dir[0] = '/';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding dir[1] = lockname[0];
0942697a9b5de44865676345a3828741c827efe6rbb dir[2] = '/';
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm dir[3] = lockname[1];
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm dir[4] = 0;
3597d6b10c88c252f156fe8061b659a38dea38c2rbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lockname = apr_pstrcat(r->pool, conf->lockpath, dir, "/", lockname, NULL);
a1de70bda57b72d8cc6b47ea744e0973ed424f56wrowe }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return apr_file_remove(lockname, r->pool);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm}
d2e443764d6855a4a06c8eb2b7708ab9b465ddeerbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(int) ap_cache_check_freshness(cache_handle_t *h,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding request_rec *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_status_t status;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_int64_t age, maxage_req, maxage_cresp, maxage, smaxage, maxstale;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_int64_t minfresh;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *cc_cresp, *cc_req;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *pragma;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *agestr = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *expstr = NULL;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm char *val;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_time_t age_c = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cache_info *info = &(h->cache_obj->info);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *warn_head;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cache_server_conf *conf =
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (cache_server_conf *)ap_get_module_config(r->server->module_config,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding &cache_module);
2b484455736f4c30447aa852764f53282cbeb5ddrbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * We now want to check if our cached data is still fresh. This depends
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * on a few things, in this order:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - RFC2616 14.32 Pragma: no-cache This is treated the same as
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Cache-Control: no-cache.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 *
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 *
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.
6db54801798f6021bb464de7b3fdcc9dc3150f7bdgaudet *
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.
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe *
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* This value comes from the client's initial request. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cc_req = apr_table_get(r->headers_in, "Cache-Control");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pragma = apr_table_get(r->headers_in, "Pragma");
2b484455736f4c30447aa852764f53282cbeb5ddrbb
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (ap_cache_liststr(NULL, pragma, "no-cache", NULL)
2b484455736f4c30447aa852764f53282cbeb5ddrbb || ap_cache_liststr(NULL, cc_req, "no-cache", NULL)) {
2b484455736f4c30447aa852764f53282cbeb5ddrbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!conf->ignorecachecontrol) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Treat as stale, causing revalidation */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Incoming request is asking for a uncached version of "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "%s, but we know better and are ignoring it",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->unparsed_uri);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* These come from the cached entity. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding cc_cresp = apr_table_get(h->resp_hdrs, "Cache-Control");
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm expstr = apr_table_get(h->resp_hdrs, "Expires");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (ap_cache_liststr(NULL, cc_cresp, "no-cache", NULL)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * The cached entity contained Cache-Control: no-cache, so treat as
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * stale causing revalidation
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((agestr = apr_table_get(h->resp_hdrs, "Age"))) {
fd47393e4c08a1515601cb1f1e8deddf4163e858martin age_c = apr_atoi64(agestr);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* calculate age of object */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding age = ap_cache_current_age(info, age_c, r->request_time);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* extract s-maxage */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "s-maxage", &val)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding && val != NULL) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding smaxage = apr_atoi64(val);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
7041bead13293c6a2e47ae1babc35f2da1d6a749trawick else {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm smaxage = -1;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* extract max-age from request */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!conf->ignorecachecontrol
d2e443764d6855a4a06c8eb2b7708ab9b465ddeerbb && cc_req && ap_cache_liststr(r->pool, cc_req, "max-age", &val)
d2e443764d6855a4a06c8eb2b7708ab9b465ddeerbb && val != NULL) {
d2e443764d6855a4a06c8eb2b7708ab9b465ddeerbb maxage_req = apr_atoi64(val);
d2e443764d6855a4a06c8eb2b7708ab9b465ddeerbb }
d2e443764d6855a4a06c8eb2b7708ab9b465ddeerbb else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding maxage_req = -1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
e7daae8d2ef0165e83627188bf33655c0b8266e8wrowe /* extract max-age from response */
e7daae8d2ef0165e83627188bf33655c0b8266e8wrowe if (cc_cresp && ap_cache_liststr(r->pool, cc_cresp, "max-age", &val)
a1de70bda57b72d8cc6b47ea744e0973ed424f56wrowe && val != NULL) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm maxage_cresp = apr_atoi64(val);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding maxage_cresp = -1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * if both maxage request and response, the smaller one takes priority
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (maxage_req == -1) {
3d96ee83babeec32482c9082c9426340cee8c44dwrowe maxage = maxage_cresp;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
0942697a9b5de44865676345a3828741c827efe6rbb else if (maxage_cresp == -1) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding maxage = maxage_req;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding maxage = MIN(maxage_req, maxage_cresp);
3597d6b10c88c252f156fe8061b659a38dea38c2rbb }
3597d6b10c88c252f156fe8061b659a38dea38c2rbb
3597d6b10c88c252f156fe8061b659a38dea38c2rbb /* extract max-stale */
3597d6b10c88c252f156fe8061b659a38dea38c2rbb if (cc_req && ap_cache_liststr(r->pool, cc_req, "max-stale", &val)) {
3d96ee83babeec32482c9082c9426340cee8c44dwrowe if(val != NULL) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm maxstale = apr_atoi64(val);
3597d6b10c88c252f156fe8061b659a38dea38c2rbb }
3597d6b10c88c252f156fe8061b659a38dea38c2rbb else {
3597d6b10c88c252f156fe8061b659a38dea38c2rbb /*
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.
3d96ee83babeec32482c9082c9426340cee8c44dwrowe */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm maxstale = APR_INT64_C(86400*365);
0942697a9b5de44865676345a3828741c827efe6rbb }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding maxstale = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
3597d6b10c88c252f156fe8061b659a38dea38c2rbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* extract min-fresh */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!conf->ignorecachecontrol
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding && cc_req && ap_cache_liststr(r->pool, cc_req, "min-fresh", &val)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding && val != NULL) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding minfresh = apr_atoi64(val);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding minfresh = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* override maxstale if must-revalidate or proxy-revalidate */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (maxstale && ((cc_cresp &&
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_cache_liststr(NULL, cc_cresp,
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm "must-revalidate", NULL)) ||
3a9bc6532fbe8439fc748d8ffedb87415904d16ajim (cc_cresp &&
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_cache_liststr(NULL, cc_cresp,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "proxy-revalidate", NULL)))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding maxstale = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* handle expiration */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (((smaxage != -1) && (age < (smaxage - minfresh))) ||
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ((maxage != -1) && (age < (maxage + maxstale - minfresh))) ||
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ((smaxage == -1) && (maxage == -1) &&
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (info->expire != APR_DATE_BAD) &&
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (age < (apr_time_sec(info->expire - info->date) + maxstale - minfresh)))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding warn_head = apr_table_get(h->resp_hdrs, "Warning");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* it's fresh darlings... */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* set age header on response */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_table_set(h->resp_hdrs, "Age",
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_psprintf(r->pool, "%lu", (unsigned long)age));
061c3874622b67beff724876b9bd1b1f291f2eaafielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* add warning if maxstale overrode freshness calculation */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!(((smaxage != -1) && age < smaxage) ||
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ((maxage != -1) && age < maxage) ||
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (info->expire != APR_DATE_BAD &&
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (apr_time_sec(info->expire - info->date)) > age))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* make sure we don't stomp on a previous warning */
0942697a9b5de44865676345a3828741c827efe6rbb if ((warn_head == NULL) ||
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ((warn_head != NULL) && (ap_strstr_c(warn_head, "110") == NULL))) {
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb apr_table_merge(h->resp_hdrs, "Warning",
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb "110 Response is stale");
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
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
cf5ace98ead10d803fda70674cb4dda30fddb832trawick */
cf5ace98ead10d803fda70674cb4dda30fddb832trawick if ((maxage_cresp == -1) && (smaxage == -1) &&
2b484455736f4c30447aa852764f53282cbeb5ddrbb (expstr == NULL) && (age > 86400)) {
2b484455736f4c30447aa852764f53282cbeb5ddrbb
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.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((warn_head == NULL) ||
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ((warn_head != NULL) && (ap_strstr_c(warn_head, "113") == NULL))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding apr_table_merge(h->resp_hdrs, "Warning",
3d96ee83babeec32482c9082c9426340cee8c44dwrowe "113 Heuristic expiration");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe }
6db54801798f6021bb464de7b3fdcc9dc3150f7bdgaudet return 1; /* Cache object is fresh (enough) */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
2b484455736f4c30447aa852764f53282cbeb5ddrbb
2b484455736f4c30447aa852764f53282cbeb5ddrbb /*
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 *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * So.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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.
7646be5b9ab5b098bc81d3a08a47e1931dc87597rbb *
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.
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb *
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.
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb *
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * At any time, a request marked "no-cache" will force a refresh,
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb * ignoring the lock, ensuring an extended lockout is impossible.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm *
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * A lock that exceeds a maximum age will be deleted, and another
e4c4fcc82268e0192db234c74a6db784b879fffdrbb * request gets to make a new lock and try again.
e4c4fcc82268e0192db234c74a6db784b879fffdrbb */
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb status = ap_cache_try_lock(conf, r, (char *)h->cache_obj->key);
2b484455736f4c30447aa852764f53282cbeb5ddrbb if (APR_SUCCESS == status) {
e4c4fcc82268e0192db234c74a6db784b879fffdrbb /* we obtained a lock, follow the stale path */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb "Cache lock obtained for stale cached URL, "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "revalidating entry: %s",
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb r->unparsed_uri);
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb return 0;
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb }
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb else if (APR_EEXIST == status) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* lock already exists, return stale data anyway, with a warning */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
db2ef76dc744b13b9a9929ebb9e6f94712a81717rbb "Cache already locked for stale cached URL, "
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein "pretend it is fresh: %s",
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein r->unparsed_uri);
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb /* make sure we don't stomp on a previous warning */
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb warn_head = apr_table_get(h->resp_hdrs, "Warning");
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if ((warn_head == NULL) ||
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb ((warn_head != NULL) && (ap_strstr_c(warn_head, "110") == NULL))) {
fa4e5ac791dd1c84df616b28d4ee9751efe9c64frbb apr_table_merge(h->resp_hdrs, "Warning",
20c85ca0d404e29972fb94c3d6236a264d1c77abstoddard "110 Response is stale");
f17e3ffa43ed4f41d984925d22eb9f8139fcbf1dstoddard }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb return 1;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
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",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->unparsed_uri);
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
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.
d7150b84735ff36eeb3cde9c79ab2e69ee7ffd67rbb */
d7150b84735ff36eeb3cde9c79ab2e69ee7ffd67rbbCACHE_DECLARE(int) ap_cache_liststr(apr_pool_t *p, const char *list,
d7150b84735ff36eeb3cde9c79ab2e69ee7ffd67rbb const char *key, char **val)
d7150b84735ff36eeb3cde9c79ab2e69ee7ffd67rbb{
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb apr_size_t key_len;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb const char *next;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!list) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
e4c4fcc82268e0192db234c74a6db784b879fffdrbb key_len = strlen(key);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding next = list;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
e4c4fcc82268e0192db234c74a6db784b879fffdrbb for (;;) {
2b484455736f4c30447aa852764f53282cbeb5ddrbb
2b484455736f4c30447aa852764f53282cbeb5ddrbb /* skip whitespace and commas to find the start of the next key */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm while (*next && (apr_isspace(*next) || (*next == ','))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding next++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb if (!*next) {
ffb048955d9939c0ab6ff99c2efc412d8e7a191dwrowe return 0;
ffb048955d9939c0ab6ff99c2efc412d8e7a191dwrowe }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!strncasecmp(next, key, key_len)) {
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)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding next += key_len;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!*next || (*next == '=') || apr_isspace(*next) ||
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (*next == ',')) {
7f1290cbf118bf3d34aecc68cff2e5bd0a23e126rbb /* valid match */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (val) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (*next && (*next != '=') && (*next != ',')) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding next++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (*next == '=') {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm next++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (*next && apr_isspace(*next )) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding next++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb if (!*next) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm *val = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm else {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm const char *val_start = next;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm while (*next && !apr_isspace(*next) &&
2a7d0035b5594ddc0907a9cc545e14ae5140c541manoj (*next != ',')) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding next++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *val = apr_pstrmemdup(p, val_start,
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm next - val_start);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm *val = NULL;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
2a7d0035b5594ddc0907a9cc545e14ae5140c541manoj }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* skip to the next field */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding do {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm next++;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!*next) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm return 0;
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm } while (*next != ',');
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
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)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm{
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_size_t i;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding s = ap_strchr_c(list, ',');
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (s != NULL) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding i = s - list;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding do
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb s++;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb while (apr_isspace(*s))
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ; /* noop */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb else
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb i = strlen(list);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb while (i > 0 && apr_isspace(list[i - 1]))
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding i--;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
3d96ee83babeec32482c9082c9426340cee8c44dwrowe *str = s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (i)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return apr_pstrndup(p, list, i);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm else
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb return NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
a952e0c6ece342d88f2bc04e20dcfc563398eed3rbb
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb/*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Converts apr_time_t expressed as hex digits to
2b484455736f4c30447aa852764f53282cbeb5ddrbb * a true apr_time_t.
2b484455736f4c30447aa852764f53282cbeb5ddrbb */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingCACHE_DECLARE(apr_time_t) ap_cache_hex2usec(const char *x)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
2cfdca5be0c69f65b43a888d6d3da846489b8fa5rbb int i, ch;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_time_t j;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0, j = 0; i < sizeof(j) * 2; i++) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ch = x[i];
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb j <<= 4;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (apr_isdigit(ch))
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding j |= ch - '0';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (apr_isupper(ch))
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding j |= ch - ('A' - 10);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb j |= ch - ('a' - 10);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
3d96ee83babeec32482c9082c9426340cee8c44dwrowe return j;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb * 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 *)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);
}
/*
* Create a new table consisting of those elements from an
* headers table that are allowed to be stored in a cache.
*/
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;
/* Short circuit the common case that there are not
* (yet) any headers populated.
*/
if (t == NULL) {
return apr_table_make(pool, 10);
};
/* 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
*/
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;
}
/*
* Legacy call - functionally equivalent to ap_cache_cacheable_headers.
* @deprecated @see ap_cache_cacheable_headers
*/
CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_hdrs_out(apr_pool_t *p,
apr_table_t *t,
server_rec *s)
{
return ap_cache_cacheable_headers(p,t,s);
}
/*
* 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_headers_in(request_rec *r)
{
return ap_cache_cacheable_headers(r->pool, r->headers_in, r->server);
}
/*
* Create a new table consisting of those elements from an output
* headers table that are allowed to be stored in a cache;
* ensure there is a content type and capture any errors.
*/
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);
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;
}