cache_util.c revision ffae06377667a5d8f9699ac7512134de7000a83d
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mod_cache.h"
#include "cache_util.h"
#include <ap_provider.h>
/* -------------------------------------------------------------- */
#define CACHE_SEPARATOR ", "
/* Determine if "url" matches the hostname, scheme and port and path
* in "filter". All but the path comparisons are case-insensitive.
*/
{
/* Scheme, hostname port and local part. The filter URI and the
* URI we test may have the following shapes:
* /<path>
* <scheme>[:://<hostname>[:<port>][/<path>]]
* That is, if there is no scheme then there must be only the path,
* and we check only the path; if there is a scheme, we check the
* scheme for equality, and then if present we match the hostname,
* and then if present match the port, and finally the path if any.
*
* Note that this means that "/<path>" only matches local paths,
* and to match proxied paths one *must* specify the scheme.
*/
/* Is the filter is just for a local path or a proxy URI? */
return 0;
}
}
else {
/* The URI scheme must be present and identical except for case. */
return 0;
}
/* If the filter hostname is null or empty it matches any hostname,
* if it begins with a "*" it matches the _end_ of the URI hostname
* excluding the "*", if it begins with a "." it matches the _end_
* of the URI * hostname including the ".", otherwise it must match
* the URI hostname exactly. */
return 0;
}
}
return 0;
}
}
return 0;
}
}
/* If the filter port is empty it matches any URL port.
* If the filter or URL port are missing, or the URL port is
* empty, they default to the port for their scheme. */
/* NOTE: ap_port_of_scheme will return 0 if given NULL input */
return 0;
}
}
}
/* For HTTP caching purposes, an empty (NULL) path is equivalent to
* a single "/" path. RFCs 3986/2396
*/
return 1;
}
else {
return 0;
}
}
/* Url has met all of the filter conditions so far, determine
* if the paths match.
*/
}
{
/* Fetch from global config and add to the list. */
"0");
if (!provider) {
/* Log an error! */
}
else {
if (!providers) {
}
else {
return providers;
}
}
return providers;
}
}
}
return providers;
}
{
int i;
/* per directory cache disable */
return NULL;
}
/* global cache disable */
struct cache_disable *ent =
/* Stop searching now. */
return NULL;
}
}
/* loop through all the per directory cacheenable entries */
struct cache_enable *ent =
}
/* loop through all the global cacheenable entries */
struct cache_enable *ent =
}
}
return providers;
}
/* do a HTTP/1.1 age calculation */
const apr_time_t age_value,
{
/* Perform an HTTP/1.1 age calculation. (RFC2616 13.2.3) */
if (current_age < 0) {
current_age = 0;
}
return apr_time_sec(current_age);
}
/**
* Try obtain a cache wide lock on the given cache key.
*
* If we return APR_SUCCESS, we obtained the lock, and we are clear to
* proceed to the backend. If we return APR_EEXISTS, then the lock is
* already locked, someone else has gone to refresh the backend data
* already, so we must return stale data with a warning in the mean
* time. If we return anything else, then something has gone pear
* shaped, and we allow the request through to the backend regardless.
*
* This lock is created from the request pool, meaning that should
* something go wrong and the lock isn't deleted on return of the
* request headers from the backend for whatever reason, at worst the
* lock will be cleaned up when the request dies or finishes.
*
* If something goes truly bananas and the lock isn't deleted when the
* request dies, the lock will be trashed when its max-age is reached,
* or when a request arrives containing a Cache-Control: no-cache. At
* no point is it possible for this lock to permanently deny access to
* the backend.
*/
request_rec *r)
{
const char *lockname;
const char *path;
char dir[5];
void *dummy;
/* no locks configured, leave */
return APR_SUCCESS;
}
/* lock already obtained earlier? if so, success */
if (dummy) {
return APR_SUCCESS;
}
/* create the key if it doesn't exist */
}
/* create a hashed filename from the key, and save it for later */
/* lock files represent discrete just-went-stale URLs "in flight", so
* we support a simple two level directory structure, more is overkill.
*/
dir[0] = '/';
dir[4] = 0;
/* make the directories */
"Could not create a cache lock directory: %s",
path);
return status;
}
/* is an existing lock file too old? */
"Could not stat a cache lock file: %s",
lockname);
return status;
}
"Cache lock file for '%s' too old, removing: %s",
}
/* try obtain a lock on the file */
}
return status;
}
/**
* Remove the cache lock, if present.
*
* First, try to close the file handle, whose delete-on-close should
* kill the file. Otherwise, just delete the file by name.
*
* If no lock name has yet been calculated, do the calculation of the
* lock name first before trying to delete the file.
*
* If an optional bucket brigade is passed, the lock will only be
* removed if the bucket brigade contains an EOS bucket.
*/
{
void *dummy;
const char *lockname;
/* no locks configured, leave */
return APR_SUCCESS;
}
if (bb) {
apr_bucket *e;
int eos_found = 0;
for (e = APR_BRIGADE_FIRST(bb);
e != APR_BRIGADE_SENTINEL(bb);
e = APR_BUCKET_NEXT(e))
{
if (APR_BUCKET_IS_EOS(e)) {
eos_found = 1;
break;
}
}
if (!eos_found) {
/* no eos found in brigade, don't delete anything just yet,
* we are not done.
*/
return APR_SUCCESS;
}
}
if (dummy) {
}
if (!lockname) {
char dir[5];
/* create the key if it doesn't exist */
}
/* create a hashed filename from the key, and save it for later */
/* lock files represent discrete just-went-stale URLs "in flight", so
* we support a simple two level directory structure, more is overkill.
*/
dir[0] = '/';
dir[4] = 0;
}
}
const char *cc_req;
const char *pragma;
&cache_module);
/*
* At this point, we may have data cached, but the request may have
* specified that cached data may not be used in a response.
*
* This is covered under RFC2616 section 14.9.4 (Cache Revalidation and
* Reload Controls).
*
* - RFC2616 14.9.4 End to end reload, Cache-Control: no-cache, or Pragma:
* no-cache. The server MUST NOT use a cached copy when responding to such
* a request.
*
* - RFC2616 14.9.2 What May be Stored by Caches. If Cache-Control:
* no-store arrives, do not serve from the cache.
*/
/* This value comes from the client's initial request. */
if (!conf->ignorecachecontrol) {
return 0;
}
else {
"Incoming request is asking for an uncached version of "
"%s, but we have been configured to ignore it and serve "
"cached content anyway", r->unparsed_uri);
}
}
if (!conf->ignorecachecontrol) {
/* We're not allowed to serve a cached copy */
return 0;
}
else {
"Incoming request is asking for a no-store version of "
"%s, but we have been configured to ignore it and serve "
"cached content anyway", r->unparsed_uri);
}
}
return 1;
}
request_rec *r)
{
const char *cc_req;
const char *pragma;
apr_time_t age_c = 0;
const char *warn_head;
&cache_module);
/*
* We now want to check if our cached data is still fresh. This depends
* on a few things, in this order:
*
* - RFC2616 14.9.4 End to end reload, Cache-Control: no-cache. no-cache
* in either the request or the cached response means that we must
* perform the request unconditionally, and ignore cached content. We
* should never reach here, but if we do, mark the content as stale,
* as this is the best we can do.
*
* - RFC2616 14.32 Pragma: no-cache This is treated the same as
* Cache-Control: no-cache.
*
* - RFC2616 14.9.3 Cache-Control: max-stale, must-revalidate,
* proxy-revalidate if the max-stale request header exists, modify the
* stale calculations below so that an object can be at most <max-stale>
* seconds stale before we request a revalidation, _UNLESS_ a
* must-revalidate or proxy-revalidate cached response header exists to
* stop us doing this.
*
* - RFC2616 14.9.3 Cache-Control: s-maxage the origin server specifies the
* maximum age an object can be before it is considered stale. This
* directive has the effect of proxy|must revalidate, which in turn means
* simple ignore any max-stale setting.
*
* - RFC2616 14.9.4 Cache-Control: max-age this header can appear in both
* requests and responses. If both are specified, the smaller of the two
* takes priority.
*
* - RFC2616 14.21 Expires: if this request header exists in the cached
* entity, and it's value is in the past, it has expired.
*
*/
/* This value comes from the client's initial request. */
if (!conf->ignorecachecontrol) {
/* Treat as stale, causing revalidation */
return 0;
}
"Incoming request is asking for a uncached version of "
"%s, but we have been configured to ignore it and "
"serve a cached response anyway",
r->unparsed_uri);
}
/* These come from the cached entity. */
/*
* The cached entity contained Cache-Control: no-cache, or a
* no-cache with a header present, or a private with a header
* present, so treat as stale causing revalidation.
*/
return 0;
}
}
/* calculate age of object */
/* extract s-maxage */
/* extract max-age from request */
maxage_req = -1;
if (!conf->ignorecachecontrol) {
}
/*
* extract max-age from response, if both s-maxage and max-age, s-maxage
* takes priority
*/
if (smaxage != -1) {
}
else {
}
/*
* if both maxage request and response, the smaller one takes priority
*/
if (maxage_req == -1) {
}
else if (maxage_cresp == -1) {
maxage = maxage_req;
}
else {
}
/* extract max-stale */
}
else {
/*
* If no value is assigned to max-stale, then the client is willing
* to accept a stale response of any age (RFC2616 14.9.3). We will
* set it to one year in this case as this situation is somewhat
* similar to a "never expires" Expires header (RFC2616 14.21)
* which is set to a date one year from the time the response is
* sent in this case.
*/
}
}
else {
maxstale = 0;
}
/* extract min-fresh */
}
else {
minfresh = 0;
}
/* override maxstale if must-revalidate, proxy-revalidate or s-maxage */
maxstale = 0;
}
/* handle expiration */
/* it's fresh darlings... */
/* set age header on response */
/* add warning if maxstale overrode freshness calculation */
/* make sure we don't stomp on a previous warning */
"110 Response is stale");
}
}
/*
* If none of Expires, Cache-Control: max-age, or Cache-Control:
* s-maxage appears in the response, and the response header age
* calculated is more than 24 hours add the warning 113
*/
/* Make sure we don't stomp on a previous warning, and don't dup
* a 113 marning that is already present. Also, make sure to add
* the new warning to the correct *headers_out location.
*/
"113 Heuristic expiration");
}
}
return 1; /* Cache object is fresh (enough) */
}
/*
* At this point we are stale, but: if we are under load, we may let
* a significant number of stale requests through before the first
* stale request successfully revalidates itself, causing a sudden
* unexpected thundering herd which in turn brings angst and drama.
*
* So.
*
* We want the first stale request to go through as normal. But the
* second and subsequent request, we must pretend to be fresh until
* the first request comes back with either new content or confirmation
* that the stale content is still fresh.
*
* To achieve this, we create a very simple file based lock based on
* the key of the cached object. We attempt to open the lock file with
* exclusive write access. If we succeed, woohoo! we're first, and we
* follow the stale path to the backend server. If we fail, oh well,
* we follow the fresh path, and avoid being a thundering herd.
*
* The lock lives only as long as the stale request that went on ahead.
* If the request succeeds, the lock is deleted. If the request fails,
* the lock is deleted, and another request gets to make a new lock
* and try again.
*
* At any time, a request marked "no-cache" will force a refresh,
* ignoring the lock, ensuring an extended lockout is impossible.
*
* A lock that exceeds a maximum age will be deleted, and another
* request gets to make a new lock and try again.
*/
if (APR_SUCCESS == status) {
/* we obtained a lock, follow the stale path */
"Cache lock obtained for stale cached URL, "
"revalidating entry: %s",
r->unparsed_uri);
return 0;
}
else if (APR_EEXIST == status) {
/* lock already exists, return stale data anyway, with a warning */
"Cache already locked for stale cached URL, "
"pretend it is fresh: %s",
r->unparsed_uri);
/* make sure we don't stomp on a previous warning */
"110 Response is stale");
}
return 1;
}
else {
/* some other error occurred, just treat the object as stale */
"Attempt to obtain a cache lock for stale "
"cached URL failed, revalidating entry anyway: %s",
r->unparsed_uri);
return 0;
}
}
/*
* list is a comma-separated list of case-insensitive tokens, with
* optional whitespace around the tokens.
* The return returns 1 if the token val is found in the list, or 0
* otherwise.
*/
{
const char *next;
if (!list) {
return 0;
}
for (;;) {
/* skip whitespace and commas to find the start of the next key */
next++;
}
if (!*next) {
return 0;
}
/* this field matches the key (though it might just be
* a prefix match, so make sure the match is followed
* by either a space or an equals sign)
*/
(*next == ',')) {
/* valid match */
if (val) {
next++;
}
if (*next == '=') {
next++;
next++;
}
if (!*next) {
}
else {
(*next != ',')) {
/* EAT QUOTED STRING */
if (!*next) {
return 0;
}
else if (*next == '\\') {
++next;
}
}
}
next++;
}
}
}
else {
}
}
return 1;
}
}
/* skip to the next field */
do {
/* EAT QUOTED STRING */
if (!*next) {
return 0;
}
else if (*next == '\\') {
++next;
}
}
}
next++;
if (!*next) {
return 0;
}
} while (*next != ',');
}
}
/* return each comma separated token, one at a time */
const char **str)
{
apr_size_t i;
const char *s;
if (s != NULL) {
i = s - list;
do
s++;
while (apr_isspace(*s))
; /* noop */
}
else
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.
*/
{
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))
else
}
return j;
}
/*
* Converts apr_time_t to apr_time_t expressed as hex digits.
*/
{
int i, ch;
for (i = (sizeof(j) * 2)-1; i >= 0; i--) {
ch = (int)(j & 0xF);
j >>= 4;
if (ch >= 10)
else
y[i] = ch + '0';
}
y[sizeof(j) * 2] = '\0';
}
{
unsigned char digest[16];
char tmp[22];
int i, k, d;
unsigned int x;
static const char enc_table[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
/* 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) {
}
/* one byte left */
x = digest[15];
/* now split into directory levels */
for (i = k = d = 0; d < ndepth; ++d) {
k += nlength;
i += nlength + 1;
}
}
{
char hashfile[66];
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.
*/
apr_table_t *t,
server_rec *s)
{
char **header;
int i;
/* Short circuit the common case that there are not
* (yet) any headers populated.
*/
if (t == NULL) {
};
/* 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
*/
&cache_module);
/* Remove the user defined headers set with CacheIgnoreHeaders.
* This may break RFC 2616 compliance on behalf of the administrator.
*/
}
return headers_out;
}
/*
* Create a new table consisting of those elements from an input
* headers table that are allowed to be stored in a cache.
*/
{
}
/*
* 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.
*/
{
r->err_headers_out);
r->server);
&& r->content_type) {
ap_make_content_type(r, r->content_type));
}
&& r->content_encoding) {
r->content_encoding);
}
return headers_out;
}
/**
* String tokenizer that ignores separator characters within quoted strings
* and escaped characters, as per RFC2616 section 2.2.
*/
{
char *token;
int quoted = 0;
if (!str) { /* subsequent call */
}
/* skip characters in sep (will terminate at '\0') */
++str;
}
if (!*str) { /* no more tokens */
return NULL;
}
/* skip valid token characters to terminate token and
* prepare for the next call (will terminate at '\0)
* on the way, ignore all quoted strings, and within
* quoted strings, escaped characters.
*/
while (**last) {
if (!quoted) {
if (**last == '\"') {
quoted = 1;
++*last;
}
++*last;
}
else {
break;
}
}
else {
if (**last == '\"') {
quoted = 0;
++*last;
}
else if (**last == '\\') {
++*last;
if (**last) {
++*last;
}
}
else {
++*last;
}
}
}
if (**last) {
**last = '\0';
++*last;
}
return token;
}
/**
* Parse the Cache-Control and Pragma headers in one go, marking
* which tokens appear within the header. Populate the structure
* passed in.
*/
{
char *last;
}
if (pragma_header) {
while (token) {
/* handle most common quickest case... */
}
/* ...then try slowest case */
}
}
cc->pragma = 1;
}
if (cc_header) {
while (token) {
switch (token[0]) {
case 'n':
case 'N': {
/* handle most common quickest cases... */
}
}
/* ...then try slowest cases */
}
}
else if (!token[8]) {
}
break;
}
}
}
break;
}
case 'm':
case 'M': {
/* handle most common quickest cases... */
cc->max_age_value = 0;
}
}
/* ...then try slowest cases */
}
break;
}
}
else if (!token[10]) {
}
break;
}
}
break;
}
}
break;
}
case 'o':
case 'O': {
}
break;
}
case 'p':
case 'P': {
/* handle most common quickest cases... */
}
/* ...then try slowest cases */
}
}
}
else if (!token[7]) {
}
break;
}
}
break;
}
case 's':
case 'S': {
}
break;
}
break;
}
}
}
}
}