1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "mod_cache.h"
19 #include "cache_util.h"
20 #include <ap_provider.h>
22 #include "test_char.h"
24 APLOG_USE_MODULE(cache);
26 /* -------------------------------------------------------------- */
28 extern APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
30 extern module AP_MODULE_DECLARE_DATA cache_module;
32 /* Determine if "url" matches the hostname, scheme and port and path
33 * in "filter". All but the path comparisons are case-insensitive.
35 static int uri_meets_conditions(const apr_uri_t *filter, const int pathlen,
36 const apr_uri_t *url, const char *path)
38 /* Scheme, hostname port and local part. The filter URI and the
39 * URI we test may have the following shapes:
41 * <scheme>[:://<hostname>[:<port>][/<path>]]
42 * That is, if there is no scheme then there must be only the path,
43 * and we check only the path; if there is a scheme, we check the
44 * scheme for equality, and then if present we match the hostname,
45 * and then if present match the port, and finally the path if any.
47 * Note that this means that "/<path>" only matches local paths,
48 * and to match proxied paths one *must* specify the scheme.
51 /* Is the filter is just for a local path or a proxy URI? */
52 if (!filter->scheme) {
53 if (url->scheme || url->hostname) {
58 /* The URI scheme must be present and identical except for case. */
59 if (!url->scheme || ap_cstr_casecmp(filter->scheme, url->scheme)) {
63 /* If the filter hostname is null or empty it matches any hostname,
64 * if it begins with a "*" it matches the _end_ of the URI hostname
65 * excluding the "*", if it begins with a "." it matches the _end_
66 * of the URI * hostname including the ".", otherwise it must match
67 * the URI hostname exactly. */
69 if (filter->hostname && filter->hostname[0]) {
70 if (filter->hostname[0] == '.') {
71 const size_t fhostlen = strlen(filter->hostname);
72 const size_t uhostlen = url->hostname ? strlen(url->hostname) : 0;
74 if (fhostlen > uhostlen
76 && strcasecmp(filter->hostname,
77 url->hostname + uhostlen - fhostlen))) {
81 else if (filter->hostname[0] == '*') {
82 const size_t fhostlen = strlen(filter->hostname + 1);
83 const size_t uhostlen = url->hostname ? strlen(url->hostname) : 0;
85 if (fhostlen > uhostlen
87 && strcasecmp(filter->hostname + 1,
88 url->hostname + uhostlen - fhostlen))) {
92 else if (!url->hostname || strcasecmp(filter->hostname, url->hostname)) {
97 /* If the filter port is empty it matches any URL port.
98 * If the filter or URL port are missing, or the URL port is
99 * empty, they default to the port for their scheme. */
101 if (!(filter->port_str && !filter->port_str[0])) {
102 /* NOTE: ap_port_of_scheme will return 0 if given NULL input */
103 const unsigned fport = filter->port_str ? filter->port
104 : apr_uri_port_of_scheme(filter->scheme);
105 const unsigned uport = (url->port_str && url->port_str[0])
106 ? url->port : apr_uri_port_of_scheme(url->scheme);
108 if (fport != uport) {
114 /* For HTTP caching purposes, an empty (NULL) path is equivalent to
115 * a single "/" path. RFCs 3986/2396
118 if (*filter->path == '/' && pathlen == 1) {
126 /* Url has met all of the filter conditions so far, determine
127 * if the paths match.
129 return !strncmp(filter->path, path, pathlen);
132 int cache_use_early_url(request_rec *r)
134 cache_server_conf *conf;
136 if (r->proxyreq == PROXYREQ_PROXY) {
140 conf = ap_get_module_config(r->server->module_config, &cache_module);
148 static cache_provider_list *get_provider(request_rec *r, struct cache_enable *ent,
149 cache_provider_list *providers)
151 /* Fetch from global config and add to the list. */
152 cache_provider *provider;
153 provider = ap_lookup_provider(CACHE_PROVIDER_GROUP, ent->type,
159 cache_provider_list *newp;
160 newp = apr_pcalloc(r->pool, sizeof(cache_provider_list));
161 newp->provider_name = ent->type;
162 newp->provider = provider;
168 cache_provider_list *last = providers;
171 if (last->provider == provider) {
176 if (last->provider == provider) {
186 cache_provider_list *cache_get_providers(request_rec *r,
187 cache_server_conf *conf)
189 cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &cache_module);
190 cache_provider_list *providers = NULL;
194 /* per directory cache disable */
195 if (dconf->disable) {
199 path = cache_use_early_url(r) ? r->parsed_uri.path : r->uri;
201 /* global cache disable */
202 for (i = 0; i < conf->cachedisable->nelts; i++) {
203 struct cache_disable *ent =
204 (struct cache_disable *)conf->cachedisable->elts;
205 if (uri_meets_conditions(&ent[i].url, ent[i].pathlen,
206 &r->parsed_uri, path)) {
207 /* Stop searching now. */
212 /* loop through all the per directory cacheenable entries */
213 for (i = 0; i < dconf->cacheenable->nelts; i++) {
214 struct cache_enable *ent =
215 (struct cache_enable *)dconf->cacheenable->elts;
216 providers = get_provider(r, &ent[i], providers);
219 /* loop through all the global cacheenable entries */
220 for (i = 0; i < conf->cacheenable->nelts; i++) {
221 struct cache_enable *ent =
222 (struct cache_enable *)conf->cacheenable->elts;
223 if (uri_meets_conditions(&ent[i].url, ent[i].pathlen,
224 &r->parsed_uri, path)) {
225 providers = get_provider(r, &ent[i], providers);
233 /* do a HTTP/1.1 age calculation */
234 CACHE_DECLARE(apr_int64_t) ap_cache_current_age(cache_info *info,
235 const apr_time_t age_value,
238 apr_time_t apparent_age, corrected_received_age, response_delay,
239 corrected_initial_age, resident_time, current_age,
242 age_value_usec = apr_time_from_sec(age_value);
244 /* Perform an HTTP/1.1 age calculation. (RFC2616 13.2.3) */
246 apparent_age = MAX(0, info->response_time - info->date);
247 corrected_received_age = MAX(apparent_age, age_value_usec);
248 response_delay = info->response_time - info->request_time;
249 corrected_initial_age = corrected_received_age + response_delay;
250 resident_time = now - info->response_time;
251 current_age = corrected_initial_age + resident_time;
253 if (current_age < 0) {
257 return apr_time_sec(current_age);
261 * Try obtain a cache wide lock on the given cache key.
263 * If we return APR_SUCCESS, we obtained the lock, and we are clear to
264 * proceed to the backend. If we return APR_EEXIST, then the lock is
265 * already locked, someone else has gone to refresh the backend data
266 * already, so we must return stale data with a warning in the mean
267 * time. If we return anything else, then something has gone pear
268 * shaped, and we allow the request through to the backend regardless.
270 * This lock is created from the request pool, meaning that should
271 * something go wrong and the lock isn't deleted on return of the
272 * request headers from the backend for whatever reason, at worst the
273 * lock will be cleaned up when the request dies or finishes.
275 * If something goes truly bananas and the lock isn't deleted when the
276 * request dies, the lock will be trashed when its max-age is reached,
277 * or when a request arrives containing a Cache-Control: no-cache. At
278 * no point is it possible for this lock to permanently deny access to
281 apr_status_t cache_try_lock(cache_server_conf *conf, cache_request_rec *cache,
285 const char *lockname;
288 apr_time_t now = apr_time_now();
290 apr_file_t *lockfile;
295 if (!conf || !conf->lock || !conf->lockpath) {
296 /* no locks configured, leave */
300 /* lock already obtained earlier? if so, success */
301 apr_pool_userdata_get(&dummy, CACHE_LOCKFILE_KEY, r->pool);
306 /* create the key if it doesn't exist */
310 * Try to use the key of a possible open but stale cache
311 * entry if we have one.
313 if (cache->handle != NULL) {
317 h = cache->stale_handle;
320 (h->cache_obj != NULL) &&
321 (h->cache_obj->key != NULL)) {
322 cache->key = apr_pstrdup(r->pool, h->cache_obj->key);
325 cache_generate_key(r, r->pool, &cache->key);
329 /* create a hashed filename from the key, and save it for later */
330 lockname = ap_cache_generate_name(r->pool, 0, 0, cache->key);
332 /* lock files represent discrete just-went-stale URLs "in flight", so
333 * we support a simple two level directory structure, more is overkill.
336 dir[1] = lockname[0];
338 dir[3] = lockname[1];
341 /* make the directories */
342 path = apr_pstrcat(r->pool, conf->lockpath, dir, NULL);
343 if (APR_SUCCESS != (status = apr_dir_make_recursive(path,
344 APR_UREAD|APR_UWRITE|APR_UEXECUTE, r->pool))) {
345 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00778)
346 "Could not create a cache lock directory: %s",
350 lockname = apr_pstrcat(r->pool, path, "/", lockname, NULL);
351 apr_pool_userdata_set(lockname, CACHE_LOCKNAME_KEY, NULL, r->pool);
353 /* is an existing lock file too old? */
354 status = apr_stat(&finfo, lockname,
355 APR_FINFO_MTIME | APR_FINFO_NLINK, r->pool);
356 if (!(APR_STATUS_IS_ENOENT(status)) && APR_SUCCESS != status) {
357 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00779)
358 "Could not stat a cache lock file: %s",
362 if ((status == APR_SUCCESS) && (((now - finfo.mtime) > conf->lockmaxage)
363 || (now < finfo.mtime))) {
364 ap_log_rerror(APLOG_MARK, APLOG_INFO, status, r, APLOGNO(00780)
365 "Cache lock file for '%s' too old, removing: %s",
367 apr_file_remove(lockname, r->pool);
370 /* try obtain a lock on the file */
371 if (APR_SUCCESS == (status = apr_file_open(&lockfile, lockname,
372 APR_WRITE | APR_CREATE | APR_EXCL | APR_DELONCLOSE,
373 APR_UREAD | APR_UWRITE, r->pool))) {
374 apr_pool_userdata_set(lockfile, CACHE_LOCKFILE_KEY, NULL, r->pool);
381 * Remove the cache lock, if present.
383 * First, try to close the file handle, whose delete-on-close should
384 * kill the file. Otherwise, just delete the file by name.
386 * If no lock name has yet been calculated, do the calculation of the
387 * lock name first before trying to delete the file.
389 * If an optional bucket brigade is passed, the lock will only be
390 * removed if the bucket brigade contains an EOS bucket.
392 apr_status_t cache_remove_lock(cache_server_conf *conf,
393 cache_request_rec *cache, request_rec *r, apr_bucket_brigade *bb)
396 const char *lockname;
398 if (!conf || !conf->lock || !conf->lockpath) {
399 /* no locks configured, leave */
405 for (e = APR_BRIGADE_FIRST(bb);
406 e != APR_BRIGADE_SENTINEL(bb);
407 e = APR_BUCKET_NEXT(e))
409 if (APR_BUCKET_IS_EOS(e)) {
415 /* no eos found in brigade, don't delete anything just yet,
421 apr_pool_userdata_get(&dummy, CACHE_LOCKFILE_KEY, r->pool);
423 return apr_file_close((apr_file_t *)dummy);
425 apr_pool_userdata_get(&dummy, CACHE_LOCKNAME_KEY, r->pool);
426 lockname = (const char *)dummy;
430 /* create the key if it doesn't exist */
432 cache_generate_key(r, r->pool, &cache->key);
435 /* create a hashed filename from the key, and save it for later */
436 lockname = ap_cache_generate_name(r->pool, 0, 0, cache->key);
438 /* lock files represent discrete just-went-stale URLs "in flight", so
439 * we support a simple two level directory structure, more is overkill.
442 dir[1] = lockname[0];
444 dir[3] = lockname[1];
447 lockname = apr_pstrcat(r->pool, conf->lockpath, dir, "/", lockname, NULL);
449 return apr_file_remove(lockname, r->pool);
452 int ap_cache_check_no_cache(cache_request_rec *cache, request_rec *r)
455 cache_server_conf *conf =
456 (cache_server_conf *)ap_get_module_config(r->server->module_config,
460 * At this point, we may have data cached, but the request may have
461 * specified that cached data may not be used in a response.
463 * This is covered under RFC2616 section 14.9.4 (Cache Revalidation and
466 * - RFC2616 14.9.4 End to end reload, Cache-Control: no-cache, or Pragma:
467 * no-cache. The server MUST NOT use a cached copy when responding to such
471 /* This value comes from the client's initial request. */
472 if (!cache->control_in.parsed) {
473 const char *cc_req = cache_table_getm(r->pool, r->headers_in,
475 const char *pragma = cache_table_getm(r->pool, r->headers_in, "Pragma");
476 ap_cache_control(r, &cache->control_in, cc_req, pragma, r->headers_in);
479 if (cache->control_in.no_cache) {
481 if (!conf->ignorecachecontrol) {
485 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02657)
486 "Incoming request is asking for an uncached version of "
487 "%s, but we have been configured to ignore it and serve "
488 "cached content anyway", r->unparsed_uri);
495 int ap_cache_check_no_store(cache_request_rec *cache, request_rec *r)
498 cache_server_conf *conf =
499 (cache_server_conf *)ap_get_module_config(r->server->module_config,
503 * At this point, we may have data cached, but the request may have
504 * specified that cached data may not be used in a response.
506 * - RFC2616 14.9.2 What May be Stored by Caches. If Cache-Control:
507 * no-store arrives, do not serve from or store to the cache.
510 /* This value comes from the client's initial request. */
511 if (!cache->control_in.parsed) {
512 const char *cc_req = cache_table_getm(r->pool, r->headers_in,
514 const char *pragma = cache_table_getm(r->pool, r->headers_in, "Pragma");
515 ap_cache_control(r, &cache->control_in, cc_req, pragma, r->headers_in);
518 if (cache->control_in.no_store) {
520 if (!conf->ignorecachecontrol) {
521 /* We're not allowed to serve a cached copy */
525 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02658)
526 "Incoming request is asking for a no-store version of "
527 "%s, but we have been configured to ignore it and serve "
528 "cached content anyway", r->unparsed_uri);
535 int cache_check_freshness(cache_handle_t *h, cache_request_rec *cache,
539 apr_int64_t age, maxage_req, maxage_cresp, maxage, smaxage, maxstale;
540 apr_int64_t minfresh;
543 const char *agestr = NULL;
544 apr_time_t age_c = 0;
545 cache_info *info = &(h->cache_obj->info);
546 const char *warn_head;
547 cache_server_conf *conf =
548 (cache_server_conf *)ap_get_module_config(r->server->module_config,
552 * We now want to check if our cached data is still fresh. This depends
553 * on a few things, in this order:
555 * - RFC2616 14.9.4 End to end reload, Cache-Control: no-cache. no-cache
556 * in either the request or the cached response means that we must
557 * perform the request unconditionally, and ignore cached content. We
558 * should never reach here, but if we do, mark the content as stale,
559 * as this is the best we can do.
561 * - RFC2616 14.32 Pragma: no-cache This is treated the same as
562 * Cache-Control: no-cache.
564 * - RFC2616 14.9.3 Cache-Control: max-stale, must-revalidate,
565 * proxy-revalidate if the max-stale request header exists, modify the
566 * stale calculations below so that an object can be at most <max-stale>
567 * seconds stale before we request a revalidation, _UNLESS_ a
568 * must-revalidate or proxy-revalidate cached response header exists to
569 * stop us doing this.
571 * - RFC2616 14.9.3 Cache-Control: s-maxage the origin server specifies the
572 * maximum age an object can be before it is considered stale. This
573 * directive has the effect of proxy|must revalidate, which in turn means
574 * simple ignore any max-stale setting.
576 * - RFC2616 14.9.4 Cache-Control: max-age this header can appear in both
577 * requests and responses. If both are specified, the smaller of the two
580 * - RFC2616 14.21 Expires: if this request header exists in the cached
581 * entity, and it's value is in the past, it has expired.
585 /* This value comes from the client's initial request. */
586 cc_req = apr_table_get(r->headers_in, "Cache-Control");
587 pragma = apr_table_get(r->headers_in, "Pragma");
589 ap_cache_control(r, &cache->control_in, cc_req, pragma, r->headers_in);
591 if (cache->control_in.no_cache) {
593 if (!conf->ignorecachecontrol) {
594 /* Treat as stale, causing revalidation */
598 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00781)
599 "Incoming request is asking for a uncached version of "
600 "%s, but we have been configured to ignore it and "
601 "serve a cached response anyway",
605 /* These come from the cached entity. */
606 if (h->cache_obj->info.control.no_cache
607 || h->cache_obj->info.control.invalidated) {
609 * The cached entity contained Cache-Control: no-cache, or a
610 * no-cache with a header present, or a private with a header
611 * present, or the cached entity has been invalidated in the
612 * past, so treat as stale causing revalidation.
617 if ((agestr = apr_table_get(h->resp_hdrs, "Age"))) {
620 if (!apr_strtoff(&offt, agestr, &endp, 10)
621 && endp > agestr && !*endp) {
626 /* calculate age of object */
627 age = ap_cache_current_age(info, age_c, r->request_time);
629 /* extract s-maxage */
630 smaxage = h->cache_obj->info.control.s_maxage_value;
632 /* extract max-age from request */
634 if (!conf->ignorecachecontrol) {
635 maxage_req = cache->control_in.max_age_value;
639 * extract max-age from response, if both s-maxage and max-age, s-maxage
643 maxage_cresp = smaxage;
646 maxage_cresp = h->cache_obj->info.control.max_age_value;
650 * if both maxage request and response, the smaller one takes priority
652 if (maxage_req == -1) {
653 maxage = maxage_cresp;
655 else if (maxage_cresp == -1) {
659 maxage = MIN(maxage_req, maxage_cresp);
662 /* extract max-stale */
663 if (cache->control_in.max_stale) {
664 if (cache->control_in.max_stale_value != -1) {
665 maxstale = cache->control_in.max_stale_value;
669 * If no value is assigned to max-stale, then the client is willing
670 * to accept a stale response of any age (RFC2616 14.9.3). We will
671 * set it to one year in this case as this situation is somewhat
672 * similar to a "never expires" Expires header (RFC2616 14.21)
673 * which is set to a date one year from the time the response is
676 maxstale = APR_INT64_C(86400*365);
683 /* extract min-fresh */
684 if (!conf->ignorecachecontrol && cache->control_in.min_fresh) {
685 minfresh = cache->control_in.min_fresh_value;
691 /* override maxstale if must-revalidate, proxy-revalidate or s-maxage */
692 if (maxstale && (h->cache_obj->info.control.must_revalidate
693 || h->cache_obj->info.control.proxy_revalidate || smaxage != -1)) {
697 /* handle expiration */
698 if (((maxage != -1) && (age < (maxage + maxstale - minfresh))) ||
699 ((smaxage == -1) && (maxage == -1) &&
700 (info->expire != APR_DATE_BAD) &&
701 (age < (apr_time_sec(info->expire - info->date) + maxstale - minfresh)))) {
703 warn_head = apr_table_get(h->resp_hdrs, "Warning");
705 /* it's fresh darlings... */
706 /* set age header on response */
707 apr_table_set(h->resp_hdrs, "Age",
708 apr_psprintf(r->pool, "%lu", (unsigned long)age));
710 /* add warning if maxstale overrode freshness calculation */
711 if (!(((maxage != -1) && age < maxage) ||
712 (info->expire != APR_DATE_BAD &&
713 (apr_time_sec(info->expire - info->date)) > age))) {
714 /* make sure we don't stomp on a previous warning */
715 if ((warn_head == NULL) ||
716 (ap_strstr_c(warn_head, "110") == NULL)) {
717 apr_table_mergen(h->resp_hdrs, "Warning",
718 "110 Response is stale");
723 * If none of Expires, Cache-Control: max-age, or Cache-Control:
724 * s-maxage appears in the response, and the response header age
725 * calculated is more than 24 hours add the warning 113
727 if ((maxage_cresp == -1) && (smaxage == -1) && (apr_table_get(
728 h->resp_hdrs, "Expires") == NULL) && (age > 86400)) {
730 /* Make sure we don't stomp on a previous warning, and don't dup
731 * a 113 marning that is already present. Also, make sure to add
732 * the new warning to the correct *headers_out location.
734 if ((warn_head == NULL) ||
735 (ap_strstr_c(warn_head, "113") == NULL)) {
736 apr_table_mergen(h->resp_hdrs, "Warning",
737 "113 Heuristic expiration");
740 return 1; /* Cache object is fresh (enough) */
744 * At this point we are stale, but: if we are under load, we may let
745 * a significant number of stale requests through before the first
746 * stale request successfully revalidates itself, causing a sudden
747 * unexpected thundering herd which in turn brings angst and drama.
751 * We want the first stale request to go through as normal. But the
752 * second and subsequent request, we must pretend to be fresh until
753 * the first request comes back with either new content or confirmation
754 * that the stale content is still fresh.
756 * To achieve this, we create a very simple file based lock based on
757 * the key of the cached object. We attempt to open the lock file with
758 * exclusive write access. If we succeed, woohoo! we're first, and we
759 * follow the stale path to the backend server. If we fail, oh well,
760 * we follow the fresh path, and avoid being a thundering herd.
762 * The lock lives only as long as the stale request that went on ahead.
763 * If the request succeeds, the lock is deleted. If the request fails,
764 * the lock is deleted, and another request gets to make a new lock
767 * At any time, a request marked "no-cache" will force a refresh,
768 * ignoring the lock, ensuring an extended lockout is impossible.
770 * A lock that exceeds a maximum age will be deleted, and another
771 * request gets to make a new lock and try again.
773 status = cache_try_lock(conf, cache, r);
774 if (APR_SUCCESS == status) {
775 /* we obtained a lock, follow the stale path */
776 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00782)
777 "Cache lock obtained for stale cached URL, "
778 "revalidating entry: %s",
782 else if (APR_STATUS_IS_EEXIST(status)) {
783 /* lock already exists, return stale data anyway, with a warning */
784 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00783)
785 "Cache already locked for stale cached URL, "
786 "pretend it is fresh: %s",
789 /* make sure we don't stomp on a previous warning */
790 warn_head = apr_table_get(h->resp_hdrs, "Warning");
791 if ((warn_head == NULL) ||
792 (ap_strstr_c(warn_head, "110") == NULL)) {
793 apr_table_mergen(h->resp_hdrs, "Warning",
794 "110 Response is stale");
800 /* some other error occurred, just treat the object as stale */
801 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00784)
802 "Attempt to obtain a cache lock for stale "
803 "cached URL failed, revalidating entry anyway: %s",
810 /* return each comma separated token, one at a time */
811 CACHE_DECLARE(const char *)ap_cache_tokstr(apr_pool_t *p, const char *list,
817 s = ap_strchr_c(list, ',');
822 while (apr_isspace(*s))
828 while (i > 0 && apr_isspace(list[i - 1]))
833 return apr_pstrmemdup(p, list, i);
839 * Converts apr_time_t expressed as hex digits to
842 CACHE_DECLARE(apr_time_t) ap_cache_hex2usec(const char *x)
846 for (i = 0, j = 0; i < sizeof(j) * 2; i++) {
851 else if (apr_isupper(ch))
852 j |= ch - ('A' - 10);
854 j |= ch - ('a' - 10);
860 * Converts apr_time_t to apr_time_t expressed as hex digits.
862 CACHE_DECLARE(void) ap_cache_usec2hex(apr_time_t j, char *y)
866 for (i = (sizeof(j) * 2)-1; i >= 0; i--) {
870 y[i] = ch + ('A' - 10);
874 y[sizeof(j) * 2] = '\0';
877 static void cache_hash(const char *it, char *val, int ndepth, int nlength)
879 apr_md5_ctx_t context;
880 unsigned char digest[16];
884 static const char enc_table[64] =
885 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
887 apr_md5_init(&context);
888 apr_md5_update(&context, (const unsigned char *) it, strlen(it));
889 apr_md5_final(digest, &context);
891 /* encode 128 bits as 22 characters, using a modified uuencoding
892 * the encoding is 3 bytes -> 4 characters* i.e. 128 bits is
893 * 5 x 3 bytes + 1 byte -> 5 * 4 characters + 2 characters
895 for (i = 0, k = 0; i < 15; i += 3) {
896 x = (digest[i] << 16) | (digest[i + 1] << 8) | digest[i + 2];
897 tmp[k++] = enc_table[x >> 18];
898 tmp[k++] = enc_table[(x >> 12) & 0x3f];
899 tmp[k++] = enc_table[(x >> 6) & 0x3f];
900 tmp[k++] = enc_table[x & 0x3f];
905 tmp[k++] = enc_table[x >> 2]; /* use up 6 bits */
906 tmp[k++] = enc_table[(x << 4) & 0x3f];
908 /* now split into directory levels */
909 for (i = k = d = 0; d < ndepth; ++d) {
910 memcpy(&val[i], &tmp[k], nlength);
912 val[i + nlength] = '/';
915 memcpy(&val[i], &tmp[k], 22 - k);
916 val[i + 22 - k] = '\0';
919 CACHE_DECLARE(char *)ap_cache_generate_name(apr_pool_t *p, int dirlevels,
920 int dirlength, const char *name)
923 cache_hash(name, hashfile, dirlevels, dirlength);
924 return apr_pstrdup(p, hashfile);
928 * String tokenizer per RFC 7234 section 5.2 (1#token[=["]arg["]]).
929 * If any (and arg not NULL), the argument is also returned (unquoted).
931 apr_status_t cache_strqtok(char *str, char **token, char **arg, char **last)
933 #define CACHE_TOKEN_SEPS "\t ,"
934 apr_status_t rv = APR_SUCCESS;
943 if (!str) { /* subsequent call */
944 str = *last; /* start where we left off */
946 if (!str) { /* no more tokens */
950 /* skip separators (will terminate at '\0') */
951 while (*str && TEST_CHAR(*str, T_HTTP_TOKEN_STOP)) {
952 if (!ap_strchr_c(CACHE_TOKEN_SEPS, *str)) {
957 if (!*str) { /* no more tokens */
966 /* skip valid token characters to terminate token and
967 * prepare for the next call (will terminate at '\0)
968 * on the way, handle quoted strings, and within
969 * quoted strings, escaped characters.
971 for (wpos = str; *str; ++str) {
977 if (arg) *arg = wpos;
990 if (TEST_CHAR(*str, T_HTTP_TOKEN_STOP)) {
996 AP_DEBUG_ASSERT(state == IN_QUOTES);
1001 if (*str == '\\' && *(str + 1)) {
1009 /* anything after should be trailing OWS or comma */
1010 for (; *str; ++str) {
1015 if (*str != '\t' && *str != ' ') {
1027 * Parse the Cache-Control and Pragma headers in one go, marking
1028 * which tokens appear within the header. Populate the structure
1031 int ap_cache_control(request_rec *r, cache_control_t *cc,
1032 const char *cc_header, const char *pragma_header, apr_table_t *headers)
1038 return cc->cache_control || cc->pragma;
1042 cc->max_age_value = -1;
1043 cc->max_stale_value = -1;
1044 cc->min_fresh_value = -1;
1045 cc->s_maxage_value = -1;
1047 if (pragma_header) {
1048 char *header = apr_pstrdup(r->pool, pragma_header), *token;
1049 for (rv = cache_strqtok(header, &token, NULL, &last);
1051 rv = cache_strqtok(NULL, &token, NULL, &last)) {
1052 if (!ap_cstr_casecmp(token, "no-cache")) {
1060 char *header = apr_pstrdup(r->pool, cc_header), *token, *arg;
1061 for (rv = cache_strqtok(header, &token, &arg, &last);
1063 rv = cache_strqtok(NULL, &token, &arg, &last)) {
1070 if (!ap_cstr_casecmp(token, "no-cache")) {
1075 cc->no_cache_header = 1;
1078 else if (!ap_cstr_casecmp(token, "no-store")) {
1081 else if (!ap_cstr_casecmp(token, "no-transform")) {
1082 cc->no_transform = 1;
1088 if (arg && !ap_cstr_casecmp(token, "max-age")) {
1089 if (!apr_strtoff(&offt, arg, &endp, 10)
1090 && endp > arg && !*endp) {
1092 cc->max_age_value = offt;
1095 else if (!ap_cstr_casecmp(token, "must-revalidate")) {
1096 cc->must_revalidate = 1;
1098 else if (!ap_cstr_casecmp(token, "max-stale")) {
1101 cc->max_stale_value = -1;
1103 else if (!apr_strtoff(&offt, arg, &endp, 10)
1104 && endp > arg && !*endp) {
1106 cc->max_stale_value = offt;
1109 else if (arg && !ap_cstr_casecmp(token, "min-fresh")) {
1110 if (!apr_strtoff(&offt, arg, &endp, 10)
1111 && endp > arg && !*endp) {
1113 cc->min_fresh_value = offt;
1120 if (!ap_cstr_casecmp(token, "only-if-cached")) {
1121 cc->only_if_cached = 1;
1127 if (!ap_cstr_casecmp(token, "public")) {
1130 else if (!ap_cstr_casecmp(token, "private")) {
1135 cc->private_header = 1;
1138 else if (!ap_cstr_casecmp(token, "proxy-revalidate")) {
1139 cc->proxy_revalidate = 1;
1145 if (arg && !ap_cstr_casecmp(token, "s-maxage")) {
1146 if (!apr_strtoff(&offt, arg, &endp, 10)
1147 && endp > arg && !*endp) {
1149 cc->s_maxage_value = offt;
1155 cc->cache_control = 1;
1158 return (cc_header != NULL || pragma_header != NULL);
1162 * Parse the Cache-Control, identifying and removing headers that
1163 * exist as tokens after the no-cache and private tokens.
1165 static int cache_control_remove(request_rec *r, const char *cc_header,
1166 apr_table_t *headers)
1168 char *last, *slast, *sheader;
1173 char *header = apr_pstrdup(r->pool, cc_header), *token, *arg;
1174 for (rv = cache_strqtok(header, &token, &arg, &last);
1176 rv = cache_strqtok(NULL, &token, &arg, &last)) {
1184 if (!ap_cstr_casecmp(token, "no-cache")) {
1185 for (rv = cache_strqtok(arg, &sheader, NULL, &slast);
1187 rv = cache_strqtok(NULL, &sheader, NULL, &slast)) {
1188 apr_table_unset(headers, sheader);
1196 if (!ap_cstr_casecmp(token, "private")) {
1197 for (rv = cache_strqtok(arg, &sheader, NULL, &slast);
1199 rv = cache_strqtok(NULL, &sheader, NULL, &slast)) {
1200 apr_table_unset(headers, sheader);
1213 * Create a new table consisting of those elements from an
1214 * headers table that are allowed to be stored in a cache.
1216 CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_headers(apr_pool_t *pool,
1220 cache_server_conf *conf;
1223 apr_table_t *headers_out;
1225 /* Short circuit the common case that there are not
1226 * (yet) any headers populated.
1229 return apr_table_make(pool, 10);
1232 /* Make a copy of the headers, and remove from
1233 * the copy any hop-by-hop headers, as defined in Section
1234 * 13.5.1 of RFC 2616
1236 headers_out = apr_table_copy(pool, t);
1238 apr_table_unset(headers_out, "Connection");
1239 apr_table_unset(headers_out, "Keep-Alive");
1240 apr_table_unset(headers_out, "Proxy-Authenticate");
1241 apr_table_unset(headers_out, "Proxy-Authorization");
1242 apr_table_unset(headers_out, "TE");
1243 apr_table_unset(headers_out, "Trailers");
1244 apr_table_unset(headers_out, "Transfer-Encoding");
1245 apr_table_unset(headers_out, "Upgrade");
1247 conf = (cache_server_conf *)ap_get_module_config(s->module_config,
1250 /* Remove the user defined headers set with CacheIgnoreHeaders.
1251 * This may break RFC 2616 compliance on behalf of the administrator.
1253 header = (char **)conf->ignore_headers->elts;
1254 for (i = 0; i < conf->ignore_headers->nelts; i++) {
1255 apr_table_unset(headers_out, header[i]);
1261 * Create a new table consisting of those elements from an input
1262 * headers table that are allowed to be stored in a cache.
1264 CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_headers_in(request_rec *r)
1266 return ap_cache_cacheable_headers(r->pool, r->headers_in, r->server);
1270 * Create a new table consisting of those elements from an output
1271 * headers table that are allowed to be stored in a cache;
1272 * ensure there is a content type and capture any errors.
1274 CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_headers_out(request_rec *r)
1276 apr_table_t *headers_out;
1278 headers_out = ap_cache_cacheable_headers(r->pool,
1279 cache_merge_headers_out(r),
1282 cache_control_remove(r,
1283 cache_table_getm(r->pool, headers_out, "Cache-Control"),
1289 apr_table_t *cache_merge_headers_out(request_rec *r)
1291 apr_table_t *headers_out;
1293 headers_out = apr_table_overlay(r->pool, r->headers_out,
1294 r->err_headers_out);
1297 && !apr_table_get(headers_out, "Content-Type")) {
1298 const char *ctype = ap_make_content_type(r, r->content_type);
1300 apr_table_setn(headers_out, "Content-Type", ctype);
1304 if (r->content_encoding
1305 && !apr_table_get(headers_out, "Content-Encoding")) {
1306 apr_table_setn(headers_out, "Content-Encoding",
1307 r->content_encoding);
1317 apr_array_header_t *merged;
1318 } cache_table_getm_t;
1320 static int cache_table_getm_do(void *v, const char *key, const char *val)
1322 cache_table_getm_t *state = (cache_table_getm_t *) v;
1324 if (!state->first) {
1326 * The most common case is a single header, and this is covered by
1327 * a fast path that doesn't allocate any memory. On the second and
1328 * subsequent header, an array is created and the array concatenated
1329 * together to form the final value.
1335 if (!state->merged) {
1336 state->merged = apr_array_make(state->p, 10, sizeof(const char *));
1337 elt = apr_array_push(state->merged);
1338 *elt = state->first;
1340 elt = apr_array_push(state->merged);
1346 const char *cache_table_getm(apr_pool_t *p, const apr_table_t *t,
1349 cache_table_getm_t state;
1353 state.merged = NULL;
1355 apr_table_do(cache_table_getm_do, &state, t, key, NULL);
1360 else if (!state.merged) {
1364 return apr_array_pstrcat(p, state.merged, ',');