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_storage.h"
20 #include "cache_util.h"
22 module AP_MODULE_DECLARE_DATA cache_module;
23 APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
25 /* -------------------------------------------------------------- */
28 /* Handles for cache filters, resolved at startup to eliminate
29 * a name-to-function mapping on each request
31 static ap_filter_rec_t *cache_filter_handle;
32 static ap_filter_rec_t *cache_save_filter_handle;
33 static ap_filter_rec_t *cache_save_subreq_filter_handle;
34 static ap_filter_rec_t *cache_out_filter_handle;
35 static ap_filter_rec_t *cache_out_subreq_filter_handle;
36 static ap_filter_rec_t *cache_remove_url_filter_handle;
42 * Can we deliver this request from the cache?
44 * deliver the content by installing the CACHE_OUT filter.
46 * check whether we're allowed to try cache it
48 * add CACHE_SAVE filter
52 * By default, the cache handler runs in the quick handler, bypassing
53 * virtually all server processing and offering the cache its optimal
54 * performance. In this mode, the cache bolts onto the front of the
55 * server, and behaves as a discrete RFC2616 caching proxy
58 * Under certain circumstances, an admin might want to run the cache as
59 * a normal handler instead of a quick handler, allowing the cache to
60 * run after the authorisation hooks, or by allowing fine control over
61 * the placement of the cache in the filter chain. This option comes at
62 * a performance penalty, and should only be used to achieve specific
63 * caching goals where the admin understands what they are doing.
66 static int cache_quick_handler(request_rec *r, int lookup)
70 cache_provider_list *providers;
71 cache_request_rec *cache;
72 apr_bucket_brigade *out;
75 ap_filter_rec_t *cache_out_handle;
76 cache_server_conf *conf;
78 /* Delay initialization until we know we are handling a GET */
79 if (r->method_number != M_GET) {
83 conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
86 /* only run if the quick handler is enabled */
92 * Which cache module (if any) should handle this request?
94 if (!(providers = cache_get_providers(r, conf, r->parsed_uri))) {
98 /* make space for the per request config */
99 cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
101 cache->out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
103 /* save away the possible providers */
104 cache->providers = providers;
107 * Are we allowed to serve cached info at all?
110 /* find certain cache controlling headers */
111 auth = apr_table_get(r->headers_in, "Authorization");
113 /* First things first - does the request allow us to return
114 * cached information at all? If not, just decline the request.
121 * Try to serve this request from the cache.
123 * If no existing cache file (DECLINED)
124 * add cache_save filter
125 * If cached file (OK)
127 * add cache_out filter
130 rv = cache_select(cache, r);
132 if (rv == DECLINED) {
135 /* try to obtain a cache lock at this point. if we succeed,
136 * we are the first to try and cache this url. if we fail,
137 * it means someone else is already trying to cache this
138 * url, and we should just let the request through to the
139 * backend without any attempt to cache. this stops
140 * duplicated simultaneous attempts to cache an entity.
142 rv = cache_try_lock(conf, cache, r);
143 if (APR_SUCCESS == rv) {
146 * Add cache_save filter to cache this request. Choose
147 * the correct filter by checking if we are a subrequest
151 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
152 r, "Adding CACHE_SAVE_SUBREQ filter for %s",
154 cache->save_filter = ap_add_output_filter_handle(
155 cache_save_subreq_filter_handle, cache, r,
159 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
160 r, "Adding CACHE_SAVE filter for %s",
162 cache->save_filter = ap_add_output_filter_handle(
163 cache_save_filter_handle, cache, r,
167 apr_pool_userdata_setn(cache, CACHE_CTX_KEY, NULL, r->pool);
169 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
170 "Adding CACHE_REMOVE_URL filter for %s",
173 /* Add cache_remove_url filter to this request to remove a
174 * stale cache entry if needed. Also put the current cache
175 * request rec in the filter context, as the request that
176 * is available later during running the filter may be
177 * different due to an internal redirect.
179 cache->remove_url_filter =
180 ap_add_output_filter_handle(cache_remove_url_filter_handle,
181 cache, r, r->connection);
184 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv,
185 r, "Cache locked for url, not caching "
186 "response: %s", r->uri);
190 if (cache->stale_headers) {
191 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
192 r, "Restoring request headers for %s",
195 r->headers_in = cache->stale_headers;
206 /* we've got a cache hit! tell everyone who cares */
207 cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_HIT,
210 /* if we are a lookup, we are exiting soon one way or another; Restore
213 if (cache->stale_headers) {
214 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
215 "Restoring request headers.");
216 r->headers_in = cache->stale_headers;
220 rv = ap_meets_conditions(r);
222 /* If we are a lookup, we have to return DECLINED as we have no
223 * way of knowing if we will be able to serve the content.
229 /* Return cached status. */
233 /* If we're a lookup, we can exit now instead of serving the content. */
238 /* Serve up the content */
240 /* We are in the quick handler hook, which means that no output
241 * filters have been set. So lets run the insert_filter hook.
243 ap_run_insert_filter(r);
246 * Add cache_out filter to serve this request. Choose
247 * the correct filter by checking if we are a subrequest
251 cache_out_handle = cache_out_subreq_filter_handle;
254 cache_out_handle = cache_out_filter_handle;
256 ap_add_output_filter_handle(cache_out_handle, cache, r, r->connection);
259 * Remove all filters that are before the cache_out filter. This ensures
260 * that we kick off the filter stack with our cache_out filter being the
261 * first in the chain. This make sense because we want to restore things
262 * in the same manner as we saved them.
263 * There may be filters before our cache_out filter, because
265 * 1. We call ap_set_content_type during cache_select. This causes
266 * Content-Type specific filters to be added.
267 * 2. We call the insert_filter hook. This causes filters e.g. like
268 * the ones set with SetOutputFilter to be added.
270 next = r->output_filters;
271 while (next && (next->frec != cache_out_handle)) {
272 ap_remove_output_filter(next);
276 /* kick off the filter stack */
277 out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
278 e = apr_bucket_eos_create(out->bucket_alloc);
279 APR_BRIGADE_INSERT_TAIL(out, e);
280 rv = ap_pass_brigade(r->output_filters, out);
281 if (rv != APR_SUCCESS) {
282 if (rv != AP_FILTER_ERROR) {
283 /* no way to know what type of error occurred */
284 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
285 "cache_quick_handler(%s): ap_pass_brigade returned %i",
286 cache->provider_name, rv);
287 return HTTP_INTERNAL_SERVER_ERROR;
296 * If the two filter handles are present within the filter chain, replace
297 * the last instance of the first filter with the last instance of the
298 * second filter, and return true. If the second filter is not present at
299 * all, the first filter is removed, and false is returned. If neither
300 * filter is present, false is returned and this function does nothing.
302 static int cache_replace_filter(ap_filter_t *next, ap_filter_rec_t *from,
303 ap_filter_rec_t *to) {
304 ap_filter_t *ffrom = NULL, *fto = NULL;
306 if (next->frec == from && !next->ctx) {
309 if (next->frec == to && !next->ctx) {
315 ffrom->frec = fto->frec;
316 ffrom->ctx = fto->ctx;
317 ap_remove_output_filter(fto);
321 ap_remove_output_filter(ffrom);
327 * Find the given filter, and return it if found, or NULL otherwise.
329 static ap_filter_t *cache_get_filter(ap_filter_t *next, ap_filter_rec_t *rec) {
331 if (next->frec == rec && next->ctx) {
340 * The cache handler is functionally similar to the cache_quick_hander,
341 * however a number of steps that are required by the quick handler are
342 * not required here, as the normal httpd processing has already handled
345 static int cache_handler(request_rec *r)
348 cache_provider_list *providers;
349 cache_request_rec *cache;
350 apr_bucket_brigade *out;
353 ap_filter_rec_t *cache_out_handle;
354 ap_filter_rec_t *cache_save_handle;
355 cache_server_conf *conf;
357 /* Delay initialization until we know we are handling a GET */
358 if (r->method_number != M_GET) {
362 conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
365 /* only run if the quick handler is disabled */
371 * Which cache module (if any) should handle this request?
373 if (!(providers = cache_get_providers(r, conf, r->parsed_uri))) {
377 /* make space for the per request config */
378 cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
380 cache->out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
382 /* save away the possible providers */
383 cache->providers = providers;
386 * Try to serve this request from the cache.
388 * If no existing cache file (DECLINED)
389 * add cache_save filter
390 * If cached file (OK)
392 * add cache_out filter
395 rv = cache_select(cache, r);
397 if (rv == DECLINED) {
399 /* try to obtain a cache lock at this point. if we succeed,
400 * we are the first to try and cache this url. if we fail,
401 * it means someone else is already trying to cache this
402 * url, and we should just let the request through to the
403 * backend without any attempt to cache. this stops
404 * duplicated simultaneous attempts to cache an entity.
406 rv = cache_try_lock(conf, cache, r);
407 if (APR_SUCCESS == rv) {
410 * Add cache_save filter to cache this request. Choose
411 * the correct filter by checking if we are a subrequest
415 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
416 r, "Adding CACHE_SAVE_SUBREQ filter for %s",
418 cache_save_handle = cache_save_subreq_filter_handle;
421 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
422 r, "Adding CACHE_SAVE filter for %s",
424 cache_save_handle = cache_save_filter_handle;
426 ap_add_output_filter_handle(cache_save_handle, cache, r,
430 * Did the user indicate the precise location of the
431 * CACHE_SAVE filter by inserting the CACHE filter as a
434 * If so, we get cunning and replace CACHE with the
435 * CACHE_SAVE filter. This has the effect of inserting
436 * the CACHE_SAVE filter at the precise location where
437 * the admin wants to cache the content. All filters that
438 * lie before and after the original location of the CACHE
439 * filter will remain in place.
441 if (cache_replace_filter(r->output_filters,
442 cache_filter_handle, cache_save_handle)) {
443 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
444 r, "Replacing CACHE with CACHE_SAVE "
445 "filter for %s", r->uri);
448 /* save away the save filter stack */
449 cache->save_filter = cache_get_filter(r->output_filters,
450 cache_save_filter_handle);
452 apr_pool_userdata_setn(cache, CACHE_CTX_KEY, NULL, r->pool);
454 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
455 "Adding CACHE_REMOVE_URL filter for %s",
458 /* Add cache_remove_url filter to this request to remove a
459 * stale cache entry if needed. Also put the current cache
460 * request rec in the filter context, as the request that
461 * is available later during running the filter may be
462 * different due to an internal redirect.
464 cache->remove_url_filter =
465 ap_add_output_filter_handle(cache_remove_url_filter_handle,
466 cache, r, r->connection);
470 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv,
471 r, "Cache locked for url, not caching "
472 "response: %s", r->uri);
482 /* we've got a cache hit! tell everyone who cares */
483 cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_HIT,
486 rv = ap_meets_conditions(r);
491 /* Serve up the content */
494 * Add cache_out filter to serve this request. Choose
495 * the correct filter by checking if we are a subrequest
499 cache_out_handle = cache_out_subreq_filter_handle;
502 cache_out_handle = cache_out_filter_handle;
504 ap_add_output_filter_handle(cache_out_handle, cache, r, r->connection);
507 * Did the user indicate the precise location of the CACHE_OUT filter by
508 * inserting the CACHE filter as a marker?
510 * If so, we get cunning and replace CACHE with the CACHE_OUT filters.
511 * This has the effect of inserting the CACHE_OUT filter at the precise
512 * location where the admin wants to cache the content. All filters that
513 * lie *after* the original location of the CACHE filter will remain in
516 if (cache_replace_filter(r->output_filters, cache_filter_handle, cache_out_handle)) {
517 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
518 r, "Replacing CACHE with CACHE_OUT filter for %s",
523 * Remove all filters that are before the cache_out filter. This ensures
524 * that we kick off the filter stack with our cache_out filter being the
525 * first in the chain. This make sense because we want to restore things
526 * in the same manner as we saved them.
527 * There may be filters before our cache_out filter, because
529 * 1. We call ap_set_content_type during cache_select. This causes
530 * Content-Type specific filters to be added.
531 * 2. We call the insert_filter hook. This causes filters e.g. like
532 * the ones set with SetOutputFilter to be added.
534 next = r->output_filters;
535 while (next && (next->frec != cache_out_handle)) {
536 ap_remove_output_filter(next);
540 /* kick off the filter stack */
541 out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
542 e = apr_bucket_eos_create(out->bucket_alloc);
543 APR_BRIGADE_INSERT_TAIL(out, e);
544 rv = ap_pass_brigade(r->output_filters, out);
545 if (rv != APR_SUCCESS) {
546 if (rv != AP_FILTER_ERROR) {
547 /* no way to know what type of error occurred */
548 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
549 "cache_handler(%s): ap_pass_brigade returned %i",
550 cache->provider_name, rv);
551 return HTTP_INTERNAL_SERVER_ERROR;
563 * Deliver cached content (headers and body) up the stack.
565 static int cache_out_filter(ap_filter_t *f, apr_bucket_brigade *in)
567 request_rec *r = f->r;
569 cache_request_rec *cache = (cache_request_rec *)f->ctx;
572 /* user likely configured CACHE_OUT manually; they should use mod_cache
573 * configuration to do that */
574 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
575 "CACHE/CACHE_OUT filter enabled while caching is disabled, ignoring");
576 ap_remove_output_filter(f);
577 return ap_pass_brigade(f->next, in);
580 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
581 "cache: running CACHE_OUT filter");
583 /* clean out any previous response up to EOS, if any */
584 for (e = APR_BRIGADE_FIRST(in);
585 e != APR_BRIGADE_SENTINEL(in);
586 e = APR_BUCKET_NEXT(e))
588 if (APR_BUCKET_IS_EOS(e)) {
589 apr_bucket_brigade *bb = apr_brigade_create(r->pool,
590 r->connection->bucket_alloc);
592 /* restore status of cached response */
593 r->status = cache->handle->cache_obj->info.status;
595 /* recall_headers() was called in cache_select() */
596 cache->provider->recall_body(cache->handle, r->pool, bb);
597 APR_BRIGADE_PREPEND(in, bb);
599 /* This filter is done once it has served up its content */
600 ap_remove_output_filter(f);
602 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
603 "cache: serving %s", r->uri);
604 return ap_pass_brigade(f->next, in);
607 apr_bucket_delete(e);
614 * Having jumped through all the hoops and decided to cache the
615 * response, call store_body() for each brigade, handling the
616 * case where the provider can't swallow the full brigade. In this
617 * case, we write the brigade we were passed out downstream, and
618 * loop around to try and cache some more until the in brigade is
619 * completely empty. As soon as the out brigade contains eos, call
620 * commit_entity() to finalise the cached element.
622 static int cache_save_store(ap_filter_t *f, apr_bucket_brigade *in,
623 cache_server_conf *conf, cache_request_rec *cache)
625 int rv = APR_SUCCESS;
628 /* pass the brigade in into the cache provider, which is then
629 * expected to move cached buckets to the out brigade, for us
630 * to pass up the filter stack. repeat until in is empty, or
633 while (APR_SUCCESS == rv && !APR_BRIGADE_EMPTY(in)) {
635 rv = cache->provider->store_body(cache->handle, f->r, in, cache->out);
636 if (rv != APR_SUCCESS) {
637 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, f->r,
638 "cache: Cache provider's store_body failed!");
639 ap_remove_output_filter(f);
641 /* give someone else the chance to cache the file */
642 cache_remove_lock(conf, cache, f->r, NULL);
644 /* give up trying to cache, just step out the way */
645 APR_BRIGADE_PREPEND(in, cache->out);
646 return ap_pass_brigade(f->next, in);
650 /* does the out brigade contain eos? if so, we're done, commit! */
651 for (e = APR_BRIGADE_FIRST(cache->out);
652 e != APR_BRIGADE_SENTINEL(cache->out);
653 e = APR_BUCKET_NEXT(e))
655 if (APR_BUCKET_IS_EOS(e)) {
656 rv = cache->provider->commit_entity(cache->handle, f->r);
661 /* conditionally remove the lock as soon as we see the eos bucket */
662 cache_remove_lock(conf, cache, f->r, cache->out);
664 if (APR_BRIGADE_EMPTY(cache->out)) {
665 if (APR_BRIGADE_EMPTY(in)) {
666 /* cache provider wants more data before passing the brigade
667 * upstream, oblige the provider by leaving to fetch more.
672 /* oops, no data out, but not all data read in either, be
673 * safe and stand down to prevent a spin.
675 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, f->r,
676 "cache: Cache provider's store_body returned an "
677 "empty brigade, but didn't consume all of the"
678 "input brigade, standing down to prevent a spin");
679 ap_remove_output_filter(f);
681 /* give someone else the chance to cache the file */
682 cache_remove_lock(conf, cache, f->r, NULL);
684 return ap_pass_brigade(f->next, in);
688 rv = ap_pass_brigade(f->next, cache->out);
698 * Decide whether or not this content should be cached.
699 * If we decide no it should not:
700 * remove the filter from the chain
701 * If we decide yes it should:
702 * Have we already started saving the response?
703 * If we have started, pass the data to the storage manager via store_body
705 * Check to see if we *can* save this particular response.
706 * If we can, call cache_create_entity() and save the headers and body
707 * Finally, pass the data to the next filter (the network or whatever)
709 * After the various failure cases, the cache lock is proactively removed, so
710 * that another request is given the opportunity to attempt to cache without
711 * waiting for a potentially slow client to acknowledge the failure.
714 static int cache_save_filter(ap_filter_t *f, apr_bucket_brigade *in)
717 request_rec *r = f->r;
718 cache_request_rec *cache = (cache_request_rec *)f->ctx;
719 cache_server_conf *conf;
720 cache_dir_conf *dconf;
721 cache_control_t control;
722 const char *cc_out, *cl, *pragma;
723 const char *exps, *lastmods, *dates, *etag;
724 apr_time_t exp, date, lastmod, now;
726 cache_info *info = NULL;
730 apr_table_t *headers;
732 conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
735 /* Setup cache_request_rec */
737 /* user likely configured CACHE_SAVE manually; they should really use
738 * mod_cache configuration to do that
740 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
741 "CACHE/CACHE_SAVE filter enabled while caching is disabled, ignoring");
742 ap_remove_output_filter(f);
743 return ap_pass_brigade(f->next, in);
751 * This section passes the brigades into the cache modules, but only
752 * if the setup section (see below) is complete.
754 if (cache->block_response) {
755 /* We've already sent down the response and EOS. So, ignore
756 * whatever comes now.
761 /* have we already run the cacheability check and set up the
762 * cached file handle?
764 if (cache->in_checked) {
765 return cache_save_store(f, in, conf, cache);
769 * Setup Data in Cache
770 * -------------------
771 * This section opens the cache entity and sets various caching
772 * parameters, and decides whether this URL should be cached at
773 * all. This section is* run before the above section.
776 dconf = ap_get_module_config(r->per_dir_config, &cache_module);
778 /* RFC2616 13.8 Errors or Incomplete Response Cache Behavior:
779 * If a cache receives a 5xx response while attempting to revalidate an
780 * entry, it MAY either forward this response to the requesting client,
781 * or act as if the server failed to respond. In the latter case, it MAY
782 * return a previously received response unless the cached entry
783 * includes the "must-revalidate" cache-control directive (see section
786 * This covers the case where an error was generated behind us, for example
787 * by a backend server via mod_proxy.
789 if (dconf->stale_on_error && r->status >= HTTP_INTERNAL_SERVER_ERROR) {
791 ap_remove_output_filter(cache->remove_url_filter);
793 if (cache->stale_handle
794 && !cache->stale_handle->cache_obj->info.control.must_revalidate
795 && !cache->stale_handle->cache_obj->info.control.proxy_revalidate) {
796 const char *warn_head;
798 /* morph the current save filter into the out filter, and serve from
801 cache->handle = cache->stale_handle;
803 f->frec = cache_out_subreq_filter_handle;
806 f->frec = cache_out_filter_handle;
809 r->headers_out = cache->stale_handle->resp_hdrs;
811 /* add a revalidation warning */
812 warn_head = apr_table_get(r->err_headers_out, "Warning");
813 if ((warn_head == NULL) || ((warn_head != NULL)
814 && (ap_strstr_c(warn_head, "111") == NULL))) {
815 apr_table_mergen(r->err_headers_out, "Warning",
816 "111 Revalidation failed");
819 cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_HIT,
820 apr_psprintf(r->pool,
821 "cache hit: %d status; stale content returned",
824 /* give someone else the chance to cache the file */
825 cache_remove_lock(conf, cache, f->r, NULL);
827 /* pass brigade to our morphed out filter */
828 return ap_pass_brigade(f, in);
832 /* read expiry date; if a bad date, then leave it so the client can
835 exps = apr_table_get(r->err_headers_out, "Expires");
837 exps = apr_table_get(r->headers_out, "Expires");
840 if (APR_DATE_BAD == (exp = apr_date_parse_http(exps))) {
848 /* read the last-modified date; if the date is bad, then delete it */
849 lastmods = apr_table_get(r->err_headers_out, "Last-Modified");
850 if (lastmods == NULL) {
851 lastmods = apr_table_get(r->headers_out, "Last-Modified");
853 if (lastmods != NULL) {
854 lastmod = apr_date_parse_http(lastmods);
855 if (lastmod == APR_DATE_BAD) {
860 lastmod = APR_DATE_BAD;
863 /* read the etag and cache-control from the entity */
864 etag = apr_table_get(r->err_headers_out, "Etag");
866 etag = apr_table_get(r->headers_out, "Etag");
868 cc_out = apr_table_get(r->err_headers_out, "Cache-Control");
869 pragma = apr_table_get(r->err_headers_out, "Pragma");
870 headers = r->err_headers_out;
871 if (!cc_out && !pragma) {
872 cc_out = apr_table_get(r->headers_out, "Cache-Control");
873 pragma = apr_table_get(r->headers_out, "Pragma");
874 headers = r->headers_out;
877 /* Have we received a 304 response without any headers at all? Fall back to
878 * the original headers in the original cached request.
880 if (r->status == HTTP_NOT_MODIFIED && cache->stale_handle && !cc_out
882 cc_out = apr_table_get(cache->stale_handle->resp_hdrs, "Cache-Control");
883 pragma = apr_table_get(cache->stale_handle->resp_hdrs, "Pragma");
886 /* Parse the cache control header */
887 memset(&control, 0, sizeof(cache_control_t));
888 ap_cache_control(r, &control, cc_out, pragma, headers);
891 * what responses should we not cache?
893 * At this point we decide based on the response headers whether it
894 * is appropriate _NOT_ to cache the data from the server. There are
895 * a whole lot of conditions that prevent us from caching this data.
896 * They are tested here one by one to be clear and unambiguous.
898 if (r->status != HTTP_OK && r->status != HTTP_NON_AUTHORITATIVE
899 && r->status != HTTP_PARTIAL_CONTENT
900 && r->status != HTTP_MULTIPLE_CHOICES
901 && r->status != HTTP_MOVED_PERMANENTLY
902 && r->status != HTTP_NOT_MODIFIED) {
903 /* RFC2616 13.4 we are allowed to cache 200, 203, 206, 300, 301 or 410
904 * We allow the caching of 206, but a cache implementation might choose
905 * to decline to cache a 206 if it doesn't know how to.
906 * We include 304 Not Modified here too as this is the origin server
907 * telling us to serve the cached copy.
909 if (exps != NULL || cc_out != NULL) {
910 /* We are also allowed to cache any response given that it has a
911 * valid Expires or Cache Control header. If we find a either of
912 * those here, we pass request through the rest of the tests. From
915 * A response received with any other status code (e.g. status
916 * codes 302 and 307) MUST NOT be returned in a reply to a
917 * subsequent request unless there are cache-control directives or
918 * another header(s) that explicitly allow it. For example, these
919 * include the following: an Expires header (section 14.21); a
920 * "max-age", "s-maxage", "must-revalidate", "proxy-revalidate",
921 * "public" or "private" cache-control directive (section 14.9).
925 reason = apr_psprintf(p, "Response status %d", r->status);
932 else if (exps != NULL && exp == APR_DATE_BAD) {
933 /* if a broken Expires header is present, don't cache it */
934 reason = apr_pstrcat(p, "Broken expires header: ", exps, NULL);
936 else if (!dconf->store_expired && exp != APR_DATE_BAD
937 && exp < r->request_time)
939 /* if a Expires header is in the past, don't cache it */
940 reason = "Expires header already expired; not cacheable";
942 else if (!conf->ignorequerystring && r->parsed_uri.query && exps == NULL
943 && !control.max_age && !control.s_maxage) {
944 /* if a query string is present but no explicit expiration time,
945 * don't cache it (RFC 2616/13.9 & 13.2.1)
947 reason = "Query string present but no explicit expiration time";
949 else if (r->status == HTTP_NOT_MODIFIED &&
950 !cache->handle && !cache->stale_handle) {
951 /* if the server said 304 Not Modified but we have no cache
952 * file - pass this untouched to the user agent, it's not for us.
954 reason = "HTTP Status 304 Not Modified";
956 else if (r->status == HTTP_OK && lastmods == NULL && etag == NULL && (exps
957 == NULL) && (dconf->no_last_mod_ignore == 0) && !control.max_age
958 && !control.s_maxage) {
959 /* 200 OK response from HTTP/1.0 and up without Last-Modified,
960 * Etag, Expires, Cache-Control:max-age, or Cache-Control:s-maxage
963 /* Note: mod-include clears last_modified/expires/etags - this
964 * is why we have an optional function for a key-gen ;-)
966 reason = "No Last-Modified; Etag; Expires; Cache-Control:max-age or Cache-Control:s-maxage headers";
968 else if (!dconf->store_nostore && control.no_store) {
969 /* RFC2616 14.9.2 Cache-Control: no-store response
970 * indicating do not cache, or stop now if you are
971 * trying to cache it.
973 reason = "Cache-Control: no-store present";
975 else if (!dconf->store_private && control.private) {
976 /* RFC2616 14.9.1 Cache-Control: private response
977 * this object is marked for this user's eyes only. Behave
980 reason = "Cache-Control: private present";
982 else if (apr_table_get(r->headers_in, "Authorization")
983 && !(control.s_maxage || control.must_revalidate
984 || control.proxy_revalidate || control.public)) {
985 /* RFC2616 14.8 Authorisation:
986 * if authorisation is included in the request, we don't cache,
987 * but we can cache if the following exceptions are true:
988 * 1) If Cache-Control: s-maxage is included
989 * 2) If Cache-Control: must-revalidate is included
990 * 3) If Cache-Control: public is included
992 reason = "Authorization required";
994 else if (ap_cache_liststr(NULL,
995 apr_table_get(r->headers_out, "Vary"),
997 reason = "Vary header contains '*'";
999 else if (apr_table_get(r->subprocess_env, "no-cache") != NULL) {
1000 reason = "environment variable 'no-cache' is set";
1002 else if (r->no_cache) {
1003 /* or we've been asked not to cache it above */
1004 reason = "r->no_cache present";
1007 /* Hold the phone. Some servers might allow us to cache a 2xx, but
1008 * then make their 304 responses non cacheable. This leaves us in a
1009 * sticky position. If the 304 is in answer to our own conditional
1010 * request, we cannot send this 304 back to the client because the
1011 * client isn't expecting it. Instead, our only option is to respect
1012 * the answer to the question we asked (has it changed, answer was
1013 * no) and return the cached item to the client, and then respect
1014 * the uncacheable nature of this 304 by allowing the remove_url
1015 * filter to kick in and remove the cached entity.
1017 if (reason && r->status == HTTP_NOT_MODIFIED &&
1018 cache->stale_handle) {
1019 apr_bucket_brigade *bb;
1023 cache->handle = cache->stale_handle;
1024 info = &cache->handle->cache_obj->info;
1026 /* Load in the saved status and clear the status line. */
1027 r->status = info->status;
1028 r->status_line = NULL;
1030 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1032 r->headers_in = cache->stale_headers;
1033 status = ap_meets_conditions(r);
1037 bkt = apr_bucket_flush_create(bb->bucket_alloc);
1038 APR_BRIGADE_INSERT_TAIL(bb, bkt);
1041 cache->provider->recall_body(cache->handle, r->pool, bb);
1043 bkt = apr_bucket_eos_create(bb->bucket_alloc);
1044 APR_BRIGADE_INSERT_TAIL(bb, bkt);
1047 cache->block_response = 1;
1049 /* we've got a cache conditional hit! tell anyone who cares */
1050 cache_run_cache_status(
1054 AP_CACHE_REVALIDATE,
1057 "conditional cache hit: 304 was uncacheable though (%s); entity removed",
1060 /* let someone else attempt to cache */
1061 cache_remove_lock(conf, cache, r, NULL);
1063 return ap_pass_brigade(f->next, bb);
1067 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1068 "cache: %s not cached. Reason: %s", r->unparsed_uri,
1071 /* we've got a cache miss! tell anyone who cares */
1072 cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_MISS,
1075 /* remove this filter from the chain */
1076 ap_remove_output_filter(f);
1078 /* remove the lock file unconditionally */
1079 cache_remove_lock(conf, cache, r, NULL);
1081 /* ship the data up the stack */
1082 return ap_pass_brigade(f->next, in);
1085 /* Make it so that we don't execute this path again. */
1086 cache->in_checked = 1;
1088 /* Set the content length if known.
1090 cl = apr_table_get(r->err_headers_out, "Content-Length");
1092 cl = apr_table_get(r->headers_out, "Content-Length");
1096 if (apr_strtoff(&size, cl, &errp, 10) || *errp || size < 0) {
1097 cl = NULL; /* parse error, see next 'if' block */
1102 /* if we don't get the content-length, see if we have all the
1103 * buckets and use their length to calculate the size
1105 int all_buckets_here=0;
1107 for (e = APR_BRIGADE_FIRST(in);
1108 e != APR_BRIGADE_SENTINEL(in);
1109 e = APR_BUCKET_NEXT(e))
1111 if (APR_BUCKET_IS_EOS(e)) {
1115 if (APR_BUCKET_IS_FLUSH(e)) {
1118 if (e->length == (apr_size_t)-1) {
1123 if (!all_buckets_here) {
1128 /* remember content length to check response size against later */
1131 /* It's safe to cache the response.
1133 * There are two possiblities at this point:
1134 * - cache->handle == NULL. In this case there is no previously
1135 * cached entity anywhere on the system. We must create a brand
1136 * new entity and store the response in it.
1137 * - cache->stale_handle != NULL. In this case there is a stale
1138 * entity in the system which needs to be replaced by new
1139 * content (unless the result was 304 Not Modified, which means
1140 * the cached entity is actually fresh, and we should update
1144 /* Did we have a stale cache entry that really is stale?
1146 if (cache->stale_handle) {
1147 if (r->status == HTTP_NOT_MODIFIED) {
1148 /* Oh, hey. It isn't that stale! Yay! */
1149 cache->handle = cache->stale_handle;
1150 info = &cache->handle->cache_obj->info;
1154 /* Oh, well. Toss it. */
1155 cache->provider->remove_entity(cache->stale_handle);
1156 /* Treat the request as if it wasn't conditional. */
1157 cache->stale_handle = NULL;
1159 * Restore the original request headers as they may be needed
1160 * by further output filters like the byterange filter to make
1161 * the correct decisions.
1163 r->headers_in = cache->stale_headers;
1167 /* no cache handle, create a new entity */
1168 if (!cache->handle) {
1169 rv = cache_create_entity(cache, r, size, in);
1170 info = apr_pcalloc(r->pool, sizeof(cache_info));
1171 /* We only set info->status upon the initial creation. */
1172 info->status = r->status;
1176 /* we've got a cache miss! tell anyone who cares */
1177 cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_MISS,
1178 "cache miss: create_entity failed");
1180 /* Caching layer declined the opportunity to cache the response */
1181 ap_remove_output_filter(f);
1182 cache_remove_lock(conf, cache, r, NULL);
1183 return ap_pass_brigade(f->next, in);
1186 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1187 "cache: Caching url: %s", r->unparsed_uri);
1189 /* We are actually caching this response. So it does not
1190 * make sense to remove this entity any more.
1192 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1193 "cache: Removing CACHE_REMOVE_URL filter.");
1194 ap_remove_output_filter(cache->remove_url_filter);
1197 * We now want to update the cache file header information with
1198 * the new date, last modified, expire and content length and write
1199 * it away to our cache file. First, we determine these values from
1200 * the response, using heuristics if appropriate.
1202 * In addition, we make HTTP/1.1 age calculations and write them away
1206 /* store away the previously parsed cache control headers */
1207 memcpy(&info->control, &control, sizeof(cache_control_t));
1209 /* Read the date. Generate one if one is not supplied */
1210 dates = apr_table_get(r->err_headers_out, "Date");
1211 if (dates == NULL) {
1212 dates = apr_table_get(r->headers_out, "Date");
1214 if (dates != NULL) {
1215 info->date = apr_date_parse_http(dates);
1218 info->date = APR_DATE_BAD;
1221 now = apr_time_now();
1222 if (info->date == APR_DATE_BAD) { /* No, or bad date */
1223 /* no date header (or bad header)! */
1228 /* set response_time for HTTP/1.1 age calculations */
1229 info->response_time = now;
1231 /* get the request time */
1232 info->request_time = r->request_time;
1234 /* check last-modified date */
1235 if (lastmod != APR_DATE_BAD && lastmod > date) {
1236 /* if it's in the future, then replace by date */
1239 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0,
1240 r, "cache: Last modified is in the future, "
1241 "replacing with now");
1244 /* if no expiry date then
1245 * if Cache-Control: max-age
1246 * expiry date = date + max-age
1248 * expiry date = date + min((date - lastmod) * factor, maxexpire)
1250 * expire date = date + defaultexpire
1252 if (exp == APR_DATE_BAD) {
1254 if (control.max_age) {
1258 x = control.max_age_value;
1263 x = x * MSEC_ONE_SEC;
1265 if (x < dconf->minex) {
1268 if (x > dconf->maxex) {
1273 else if ((lastmod != APR_DATE_BAD) && (lastmod < date)) {
1274 /* if lastmod == date then you get 0*conf->factor which results in
1275 * an expiration time of now. This causes some problems with
1276 * freshness calculations, so we choose the else path...
1278 apr_time_t x = (apr_time_t) ((date - lastmod) * dconf->factor);
1280 if (x < dconf->minex) {
1283 if (x > dconf->maxex) {
1289 exp = date + dconf->defex;
1294 /* We found a stale entry which wasn't really stale. */
1295 if (cache->stale_handle) {
1296 /* Load in the saved status and clear the status line. */
1297 r->status = info->status;
1298 r->status_line = NULL;
1300 /* RFC 2616 10.3.5 states that entity headers are not supposed
1301 * to be in the 304 response. Therefore, we need to combine the
1302 * response headers with the cached headers *before* we update
1303 * the cached headers.
1305 * However, before doing that, we need to first merge in
1306 * err_headers_out and we also need to strip any hop-by-hop
1307 * headers that might have snuck in.
1309 r->headers_out = ap_cache_cacheable_headers_out(r);
1311 /* Merge in our cached headers. However, keep any updated values. */
1312 cache_accept_headers(cache->handle, r, 1);
1315 /* Write away header information to cache. It is possible that we are
1316 * trying to update headers for an entity which has already been cached.
1318 * This may fail, due to an unwritable cache area. E.g. filesystem full,
1319 * permissions problems or a read-only (re)mount. This must be handled
1322 rv = cache->provider->store_headers(cache->handle, r, info);
1324 /* Did we just update the cached headers on a revalidated response?
1326 * If so, we can now decide what to serve to the client. This is done in
1327 * the same way as with a regular response, but conditions are now checked
1328 * against the cached or merged response headers.
1330 if (cache->stale_handle) {
1331 apr_bucket_brigade *bb;
1335 /* We're just saving response headers, so we are done. Commit
1336 * the response at this point, unless there was a previous error.
1338 if (rv == APR_SUCCESS) {
1339 rv = cache->provider->commit_entity(cache->handle, r);
1342 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1344 /* Restore the original request headers and see if we need to
1345 * return anything else than the cached response (ie. the original
1346 * request was conditional).
1348 r->headers_in = cache->stale_headers;
1349 status = ap_meets_conditions(r);
1353 bkt = apr_bucket_flush_create(bb->bucket_alloc);
1354 APR_BRIGADE_INSERT_TAIL(bb, bkt);
1357 cache->provider->recall_body(cache->handle, r->pool, bb);
1359 bkt = apr_bucket_eos_create(bb->bucket_alloc);
1360 APR_BRIGADE_INSERT_TAIL(bb, bkt);
1363 cache->block_response = 1;
1365 /* Before returning we need to handle the possible case of an
1366 * unwritable cache. Rather than leaving the entity in the cache
1367 * and having it constantly re-validated, now that we have recalled
1368 * the body it is safe to try and remove the url from the cache.
1370 if (rv != APR_SUCCESS) {
1371 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
1372 "cache: updating headers with store_headers failed. "
1373 "Removing cached url.");
1375 rv = cache->provider->remove_url(cache->stale_handle, r);
1377 /* Probably a mod_disk_cache cache area has been (re)mounted
1378 * read-only, or that there is a permissions problem.
1380 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
1381 "cache: attempt to remove url from cache unsuccessful.");
1384 /* we've got a cache conditional hit! tell anyone who cares */
1385 cache_run_cache_status(cache->handle, r, r->headers_out,
1386 AP_CACHE_REVALIDATE,
1387 "conditional cache hit: entity refresh failed");
1392 /* we've got a cache conditional hit! tell anyone who cares */
1393 cache_run_cache_status(cache->handle, r, r->headers_out,
1394 AP_CACHE_REVALIDATE,
1395 "conditional cache hit: entity refreshed");
1399 /* let someone else attempt to cache */
1400 cache_remove_lock(conf, cache, r, NULL);
1402 return ap_pass_brigade(f->next, bb);
1405 if (rv != APR_SUCCESS) {
1406 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
1407 "cache: store_headers failed");
1409 /* we've got a cache miss! tell anyone who cares */
1410 cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_MISS,
1411 "cache miss: store_headers failed");
1413 ap_remove_output_filter(f);
1414 cache_remove_lock(conf, cache, r, NULL);
1415 return ap_pass_brigade(f->next, in);
1418 /* we've got a cache miss! tell anyone who cares */
1419 cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_MISS,
1420 "cache miss: attempting entity save");
1422 return cache_save_store(f, in, conf, cache);
1426 * CACHE_REMOVE_URL filter
1427 * -----------------------
1429 * This filter gets added in the quick handler every time the CACHE_SAVE filter
1430 * gets inserted. Its purpose is to remove a confirmed stale cache entry from
1433 * CACHE_REMOVE_URL has to be a protocol filter to ensure that is run even if
1434 * the response is a canned error message, which removes the content filters
1435 * and thus the CACHE_SAVE filter from the chain.
1437 * CACHE_REMOVE_URL expects cache request rec within its context because the
1438 * request this filter runs on can be different from the one whose cache entry
1439 * should be removed, due to internal redirects.
1441 * Note that CACHE_SAVE_URL (as a content-set filter, hence run before the
1442 * protocol filters) will remove this filter if it decides to cache the file.
1443 * Therefore, if this filter is left in, it must mean we need to toss any
1446 static int cache_remove_url_filter(ap_filter_t *f, apr_bucket_brigade *in)
1448 request_rec *r = f->r;
1449 cache_request_rec *cache;
1451 /* Setup cache_request_rec */
1452 cache = (cache_request_rec *) f->ctx;
1455 /* user likely configured CACHE_REMOVE_URL manually; they should really
1456 * use mod_cache configuration to do that. So:
1457 * 1. Remove ourselves
1458 * 2. Do nothing and bail out
1460 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1461 "cache: CACHE_REMOVE_URL enabled unexpectedly");
1462 ap_remove_output_filter(f);
1463 return ap_pass_brigade(f->next, in);
1466 /* Now remove this cache entry from the cache */
1467 cache_remove_url(cache, r);
1469 /* remove ourselves */
1470 ap_remove_output_filter(f);
1471 return ap_pass_brigade(f->next, in);
1478 * This filter can be optionally inserted into the filter chain by the admin as
1479 * a marker representing the precise location within the filter chain where
1480 * caching is to be performed.
1482 * When the filter chain is set up in the non-quick version of the URL handler,
1483 * the CACHE filter is replaced by the CACHE_OUT or CACHE_SAVE filter,
1484 * effectively inserting the caching filters at the point indicated by the
1485 * admin. The CACHE filter is then removed.
1487 * This allows caching to be performed before the content is passed to the
1488 * INCLUDES filter, or to a filter that might perform transformations unique
1489 * to the specific request and that would otherwise be non-cacheable.
1491 static int cache_filter(ap_filter_t *f, apr_bucket_brigade *in)
1493 /* we are just a marker, so let's just remove ourselves */
1494 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r,
1495 "cache: CACHE filter was added twice, or was added in quick "
1496 "handler mode and will be ignored.");
1497 ap_remove_output_filter(f);
1498 return ap_pass_brigade(f->next, in);
1502 * If configured, add the status of the caching attempt to the subprocess
1503 * environment, and if configured, to headers in the response.
1505 * The status is saved below the broad category of the status (hit, miss,
1506 * revalidate), as well as a single cache-status key. This can be used for
1507 * conditional logging.
1509 * The status is optionally saved to an X-Cache header, and the detail of
1510 * why a particular cache entry was cached (or not cached) is optionally
1511 * saved to an X-Cache-Detail header. This extra detail is useful for
1512 * service developers who may need to know whether their Cache-Control headers
1513 * are working correctly.
1515 static int cache_status(cache_handle_t *h, request_rec *r,
1516 apr_table_t *headers, ap_cache_status_e status, const char *reason)
1520 (cache_server_conf *) ap_get_module_config(r->server->module_config,
1523 cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &cache_module);
1524 int x_cache = 0, x_cache_detail = 0;
1527 case AP_CACHE_HIT: {
1528 apr_table_setn(r->subprocess_env, AP_CACHE_HIT_ENV, reason);
1531 case AP_CACHE_REVALIDATE: {
1532 apr_table_setn(r->subprocess_env, AP_CACHE_REVALIDATE_ENV, reason);
1535 case AP_CACHE_MISS: {
1536 apr_table_setn(r->subprocess_env, AP_CACHE_MISS_ENV, reason);
1541 apr_table_setn(r->subprocess_env, AP_CACHE_STATUS_ENV, reason);
1543 if (dconf && dconf->x_cache_set) {
1544 x_cache = dconf->x_cache;
1547 x_cache = conf->x_cache;
1550 apr_table_setn(headers, "X-Cache",
1551 apr_psprintf(r->pool, "%s from %s",
1552 status == AP_CACHE_HIT ? "HIT" : status
1553 == AP_CACHE_REVALIDATE ? "REVALIDATE" : "MISS",
1554 r->server->server_hostname));
1557 if (dconf && dconf->x_cache_detail_set) {
1558 x_cache_detail = dconf->x_cache_detail;
1561 x_cache_detail = conf->x_cache_detail;
1563 if (x_cache_detail) {
1564 apr_table_setn(headers, "X-Cache-Detail", apr_psprintf(r->pool,
1565 "\"%s\" from %s", reason, r->server->server_hostname));
1572 * If an error has occurred, but we have a stale cached entry, restore the
1573 * filter stack from the save filter onwards. The canned error message will
1574 * be discarded in the process, and replaced with the cached response.
1576 static void cache_insert_error_filter(request_rec *r)
1579 cache_dir_conf *dconf;
1581 /* ignore everything except for 5xx errors */
1582 if (r->status < HTTP_INTERNAL_SERVER_ERROR) {
1586 dconf = ap_get_module_config(r->per_dir_config, &cache_module);
1588 if (!dconf->stale_on_error) {
1592 /* RFC2616 13.8 Errors or Incomplete Response Cache Behavior:
1593 * If a cache receives a 5xx response while attempting to revalidate an
1594 * entry, it MAY either forward this response to the requesting client,
1595 * or act as if the server failed to respond. In the latter case, it MAY
1596 * return a previously received response unless the cached entry
1597 * includes the "must-revalidate" cache-control directive (see section
1600 * This covers the case where the error was generated by our server via
1603 apr_pool_userdata_get(&dummy, CACHE_CTX_KEY, r->pool);
1605 cache_request_rec *cache = (cache_request_rec *) dummy;
1607 ap_remove_output_filter(cache->remove_url_filter);
1609 if (cache->stale_handle && cache->save_filter
1610 && !cache->stale_handle->cache_obj->info.control.must_revalidate
1611 && !cache->stale_handle->cache_obj->info.control.proxy_revalidate) {
1612 const char *warn_head;
1615 (cache_server_conf *) ap_get_module_config(r->server->module_config,
1618 /* morph the current save filter into the out filter, and serve from
1621 cache->handle = cache->stale_handle;
1623 cache->save_filter->frec = cache_out_subreq_filter_handle;
1626 cache->save_filter->frec = cache_out_filter_handle;
1629 r->output_filters = cache->save_filter;
1631 r->err_headers_out = cache->stale_handle->resp_hdrs;
1633 /* add a revalidation warning */
1634 warn_head = apr_table_get(r->err_headers_out, "Warning");
1635 if ((warn_head == NULL) || ((warn_head != NULL)
1636 && (ap_strstr_c(warn_head, "111") == NULL))) {
1637 apr_table_mergen(r->err_headers_out, "Warning",
1638 "111 Revalidation failed");
1641 cache_run_cache_status(
1648 "cache hit: %d status; stale content returned",
1651 /* give someone else the chance to cache the file */
1652 cache_remove_lock(conf, cache, r, NULL);
1660 /* -------------------------------------------------------------- */
1661 /* Setup configurable data */
1663 static void *create_dir_config(apr_pool_t *p, char *dummy)
1665 cache_dir_conf *dconf = apr_pcalloc(p, sizeof(cache_dir_conf));
1667 dconf->no_last_mod_ignore = 0;
1668 dconf->store_expired = 0;
1669 dconf->store_private = 0;
1670 dconf->store_nostore = 0;
1672 /* maximum time to cache a document */
1673 dconf->maxex = DEFAULT_CACHE_MAXEXPIRE;
1674 dconf->minex = DEFAULT_CACHE_MINEXPIRE;
1675 /* default time to cache a document */
1676 dconf->defex = DEFAULT_CACHE_EXPIRE;
1678 /* factor used to estimate Expires date from LastModified date */
1679 dconf->factor = DEFAULT_CACHE_LMFACTOR;
1681 dconf->x_cache = DEFAULT_X_CACHE;
1682 dconf->x_cache_detail = DEFAULT_X_CACHE_DETAIL;
1684 dconf->stale_on_error = DEFAULT_CACHE_STALE_ON_ERROR;
1689 static void *merge_dir_config(apr_pool_t *p, void *basev, void *addv) {
1690 cache_dir_conf *new = (cache_dir_conf *) apr_pcalloc(p, sizeof(cache_dir_conf));
1691 cache_dir_conf *add = (cache_dir_conf *) addv;
1692 cache_dir_conf *base = (cache_dir_conf *) basev;
1694 new->no_last_mod_ignore = (add->no_last_mod_ignore_set == 0) ? base->no_last_mod_ignore : add->no_last_mod_ignore;
1695 new->no_last_mod_ignore_set = add->no_last_mod_ignore_set || base->no_last_mod_ignore_set;
1697 new->store_expired = (add->store_expired_set == 0) ? base->store_expired : add->store_expired;
1698 new->store_expired_set = add->store_expired_set || base->store_expired_set;
1699 new->store_private = (add->store_private_set == 0) ? base->store_private : add->store_private;
1700 new->store_private_set = add->store_private_set || base->store_private_set;
1701 new->store_nostore = (add->store_nostore_set == 0) ? base->store_nostore : add->store_nostore;
1702 new->store_nostore_set = add->store_nostore_set || base->store_nostore_set;
1704 /* maximum time to cache a document */
1705 new->maxex = (add->maxex_set == 0) ? base->maxex : add->maxex;
1706 new->maxex_set = add->maxex_set || base->maxex_set;
1707 new->minex = (add->minex_set == 0) ? base->minex : add->minex;
1708 new->minex_set = add->minex_set || base->minex_set;
1710 /* default time to cache a document */
1711 new->defex = (add->defex_set == 0) ? base->defex : add->defex;
1712 new->defex_set = add->defex_set || base->defex_set;
1714 /* factor used to estimate Expires date from LastModified date */
1715 new->factor = (add->factor_set == 0) ? base->factor : add->factor;
1716 new->factor_set = add->factor_set || base->factor_set;
1718 new->x_cache = (add->x_cache_set == 0) ? base->x_cache : add->x_cache;
1719 new->x_cache_set = add->x_cache_set || base->x_cache_set;
1720 new->x_cache_detail = (add->x_cache_detail_set == 0) ? base->x_cache_detail
1721 : add->x_cache_detail;
1722 new->x_cache_detail_set = add->x_cache_detail_set
1723 || base->x_cache_detail_set;
1725 new->stale_on_error = (add->stale_on_error_set == 0) ? base->stale_on_error
1726 : add->stale_on_error;
1727 new->stale_on_error_set = add->stale_on_error_set
1728 || base->stale_on_error_set;
1733 static void * create_cache_config(apr_pool_t *p, server_rec *s)
1735 const char *tmppath;
1736 cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf));
1738 /* array of URL prefixes for which caching is enabled */
1739 ps->cacheenable = apr_array_make(p, 10, sizeof(struct cache_enable));
1740 /* array of URL prefixes for which caching is disabled */
1741 ps->cachedisable = apr_array_make(p, 10, sizeof(struct cache_disable));
1742 ps->ignorecachecontrol = 0;
1743 ps->ignorecachecontrol_set = 0;
1744 /* array of headers that should not be stored in cache */
1745 ps->ignore_headers = apr_array_make(p, 10, sizeof(char *));
1746 ps->ignore_headers_set = CACHE_IGNORE_HEADERS_UNSET;
1747 /* flag indicating that query-string should be ignored when caching */
1748 ps->ignorequerystring = 0;
1749 ps->ignorequerystring_set = 0;
1750 /* by default, run in the quick handler */
1753 /* array of identifiers that should not be used for key calculation */
1754 ps->ignore_session_id = apr_array_make(p, 10, sizeof(char *));
1755 ps->ignore_session_id_set = CACHE_IGNORE_SESSION_ID_UNSET;
1756 ps->lock = 0; /* thundering herd lock defaults to off */
1758 apr_temp_dir_get(&tmppath, p);
1760 ps->lockpath = apr_pstrcat(p, tmppath, DEFAULT_CACHE_LOCKPATH, NULL);
1762 ps->lockmaxage = apr_time_from_sec(DEFAULT_CACHE_MAXAGE);
1763 ps->x_cache = DEFAULT_X_CACHE;
1764 ps->x_cache_detail = DEFAULT_X_CACHE_DETAIL;
1768 static void * merge_cache_config(apr_pool_t *p, void *basev, void *overridesv)
1770 cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf));
1771 cache_server_conf *base = (cache_server_conf *) basev;
1772 cache_server_conf *overrides = (cache_server_conf *) overridesv;
1774 /* array of URL prefixes for which caching is disabled */
1775 ps->cachedisable = apr_array_append(p,
1777 overrides->cachedisable);
1778 /* array of URL prefixes for which caching is enabled */
1779 ps->cacheenable = apr_array_append(p,
1781 overrides->cacheenable);
1783 ps->ignorecachecontrol =
1784 (overrides->ignorecachecontrol_set == 0)
1785 ? base->ignorecachecontrol
1786 : overrides->ignorecachecontrol;
1787 ps->ignore_headers =
1788 (overrides->ignore_headers_set == CACHE_IGNORE_HEADERS_UNSET)
1789 ? base->ignore_headers
1790 : overrides->ignore_headers;
1791 ps->ignorequerystring =
1792 (overrides->ignorequerystring_set == 0)
1793 ? base->ignorequerystring
1794 : overrides->ignorequerystring;
1795 ps->ignore_session_id =
1796 (overrides->ignore_session_id_set == CACHE_IGNORE_SESSION_ID_UNSET)
1797 ? base->ignore_session_id
1798 : overrides->ignore_session_id;
1800 (overrides->lock_set == 0)
1804 (overrides->lockpath_set == 0)
1806 : overrides->lockpath;
1808 (overrides->lockmaxage_set == 0)
1810 : overrides->lockmaxage;
1812 (overrides->quick_set == 0)
1816 (overrides->x_cache_set == 0)
1818 : overrides->x_cache;
1819 ps->x_cache_detail =
1820 (overrides->x_cache_detail_set == 0)
1821 ? base->x_cache_detail
1822 : overrides->x_cache_detail;
1824 (overrides->base_uri_set == 0)
1826 : overrides->base_uri;
1830 static const char *set_cache_quick_handler(cmd_parms *parms, void *dummy,
1833 cache_server_conf *conf;
1836 (cache_server_conf *)ap_get_module_config(parms->server->module_config
1840 conf->quick_set = 1;
1845 static const char *set_cache_ignore_no_last_mod(cmd_parms *parms, void *dummy,
1848 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
1850 dconf->no_last_mod_ignore = flag;
1851 dconf->no_last_mod_ignore_set = 1;
1856 static const char *set_cache_ignore_cachecontrol(cmd_parms *parms,
1857 void *dummy, int flag)
1859 cache_server_conf *conf;
1862 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
1864 conf->ignorecachecontrol = flag;
1865 conf->ignorecachecontrol_set = 1;
1869 static const char *set_cache_store_expired(cmd_parms *parms, void *dummy,
1872 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
1874 dconf->store_expired = flag;
1875 dconf->store_expired_set = 1;
1879 static const char *set_cache_store_private(cmd_parms *parms, void *dummy,
1882 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
1884 dconf->store_private = flag;
1885 dconf->store_private_set = 1;
1889 static const char *set_cache_store_nostore(cmd_parms *parms, void *dummy,
1892 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
1894 dconf->store_nostore = flag;
1895 dconf->store_nostore_set = 1;
1899 static const char *add_ignore_header(cmd_parms *parms, void *dummy,
1902 cache_server_conf *conf;
1906 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
1908 if (!strcasecmp(header, "None")) {
1909 /* if header None is listed clear array */
1910 conf->ignore_headers->nelts = 0;
1913 if ((conf->ignore_headers_set == CACHE_IGNORE_HEADERS_UNSET) ||
1914 (conf->ignore_headers->nelts)) {
1915 /* Only add header if no "None" has been found in header list
1917 * (When 'None' is passed, IGNORE_HEADERS_SET && nelts == 0.)
1919 new = (char **)apr_array_push(conf->ignore_headers);
1920 (*new) = (char *)header;
1923 conf->ignore_headers_set = CACHE_IGNORE_HEADERS_SET;
1927 static const char *add_ignore_session_id(cmd_parms *parms, void *dummy,
1928 const char *identifier)
1930 cache_server_conf *conf;
1934 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
1936 if (!strcasecmp(identifier, "None")) {
1937 /* if identifier None is listed clear array */
1938 conf->ignore_session_id->nelts = 0;
1941 if ((conf->ignore_session_id_set == CACHE_IGNORE_SESSION_ID_UNSET) ||
1942 (conf->ignore_session_id->nelts)) {
1944 * Only add identifier if no "None" has been found in identifier
1947 new = (char **)apr_array_push(conf->ignore_session_id);
1948 (*new) = (char *)identifier;
1951 conf->ignore_session_id_set = CACHE_IGNORE_SESSION_ID_SET;
1955 static const char *add_cache_enable(cmd_parms *parms, void *dummy,
1959 cache_server_conf *conf;
1960 struct cache_enable *new;
1962 const char *err = ap_check_cmd_context(parms,
1963 NOT_IN_DIRECTORY|NOT_IN_LIMIT|NOT_IN_FILES);
1969 return apr_psprintf(parms->pool,
1970 "provider (%s) starts with a '/'. Are url and provider switched?",
1978 return apr_psprintf(parms->pool,
1979 "CacheEnable provider (%s) is missing an URL.", type);
1981 if (parms->path && strncmp(parms->path, url, strlen(parms->path))) {
1982 return "When in a Location, CacheEnable must specify a path or an URL below "
1987 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
1989 new = apr_array_push(conf->cacheenable);
1991 if (apr_uri_parse(parms->pool, url, &(new->url))) {
1994 if (new->url.path) {
1995 new->pathlen = strlen(new->url.path);
1998 new->url.path = "/";
2003 static const char *add_cache_disable(cmd_parms *parms, void *dummy,
2006 cache_server_conf *conf;
2007 struct cache_disable *new;
2009 const char *err = ap_check_cmd_context(parms,
2010 NOT_IN_DIRECTORY|NOT_IN_LIMIT|NOT_IN_FILES);
2015 if (parms->path && !strcmp(url, "on")) {
2018 if (url[0] != '/' && !ap_strchr_c(url, ':')) {
2019 return "CacheDisable must specify a path or an URL, or when in a Location, "
2023 if (parms->path && strncmp(parms->path, url, strlen(parms->path))) {
2024 return "When in a Location, CacheDisable must specify a path or an URL below "
2029 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2031 new = apr_array_push(conf->cachedisable);
2032 if (apr_uri_parse(parms->pool, url, &(new->url))) {
2035 if (new->url.path) {
2036 new->pathlen = strlen(new->url.path);
2039 new->url.path = "/";
2044 static const char *set_cache_maxex(cmd_parms *parms, void *dummy,
2047 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2049 dconf->maxex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
2050 dconf->maxex_set = 1;
2054 static const char *set_cache_minex(cmd_parms *parms, void *dummy,
2057 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2059 dconf->minex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
2060 dconf->minex_set = 1;
2064 static const char *set_cache_defex(cmd_parms *parms, void *dummy,
2067 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2069 dconf->defex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
2070 dconf->defex_set = 1;
2074 static const char *set_cache_factor(cmd_parms *parms, void *dummy,
2077 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2080 if (sscanf(arg, "%lg", &val) != 1) {
2081 return "CacheLastModifiedFactor value must be a float";
2083 dconf->factor = val;
2084 dconf->factor_set = 1;
2088 static const char *set_cache_ignore_querystring(cmd_parms *parms, void *dummy,
2091 cache_server_conf *conf;
2094 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2096 conf->ignorequerystring = flag;
2097 conf->ignorequerystring_set = 1;
2101 static const char *set_cache_lock(cmd_parms *parms, void *dummy,
2104 cache_server_conf *conf;
2107 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2114 static const char *set_cache_lock_path(cmd_parms *parms, void *dummy,
2117 cache_server_conf *conf;
2120 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2123 conf->lockpath = ap_server_root_relative(parms->pool, arg);
2124 if (!conf->lockpath) {
2125 return apr_pstrcat(parms->pool, "Invalid CacheLockPath path ",
2128 conf->lockpath_set = 1;
2132 static const char *set_cache_lock_maxage(cmd_parms *parms, void *dummy,
2135 cache_server_conf *conf;
2136 apr_int64_t seconds;
2139 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2141 seconds = apr_atoi64(arg);
2143 return "CacheLockMaxAge value must be a non-zero positive integer";
2145 conf->lockmaxage = apr_time_from_sec(seconds);
2146 conf->lockmaxage_set = 1;
2150 static const char *set_cache_x_cache(cmd_parms *parms, void *dummy, int flag)
2154 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2156 dconf->x_cache = flag;
2157 dconf->x_cache_set = 1;
2161 cache_server_conf *conf =
2162 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2165 conf->x_cache = flag;
2166 conf->x_cache_set = 1;
2173 static const char *set_cache_x_cache_detail(cmd_parms *parms, void *dummy, int flag)
2177 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2179 dconf->x_cache_detail = flag;
2180 dconf->x_cache_detail_set = 1;
2184 cache_server_conf *conf =
2185 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2188 conf->x_cache_detail = flag;
2189 conf->x_cache_detail_set = 1;
2196 static const char *set_cache_key_base_url(cmd_parms *parms, void *dummy,
2199 cache_server_conf *conf;
2203 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2205 conf->base_uri = apr_pcalloc(parms->pool, sizeof(apr_uri_t));
2206 rv = apr_uri_parse(parms->pool, arg, conf->base_uri);
2207 if (rv != APR_SUCCESS) {
2208 return apr_psprintf(parms->pool, "Could not parse '%s' as an URL.", arg);
2210 else if (!conf->base_uri->scheme && !conf->base_uri->hostname &&
2211 !conf->base_uri->port_str) {
2212 return apr_psprintf(parms->pool, "URL '%s' must contain at least one of a scheme, a hostname or a port.", arg);
2214 conf->base_uri_set = 1;
2218 static const char *set_cache_stale_on_error(cmd_parms *parms, void *dummy,
2221 cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2223 dconf->stale_on_error = flag;
2224 dconf->stale_on_error_set = 1;
2228 static int cache_post_config(apr_pool_t *p, apr_pool_t *plog,
2229 apr_pool_t *ptemp, server_rec *s)
2231 /* This is the means by which unusual (non-unix) os's may find alternate
2232 * means to run a given command (e.g. shebang/registry parsing on Win32)
2234 cache_generate_key = APR_RETRIEVE_OPTIONAL_FN(ap_cache_generate_key);
2235 if (!cache_generate_key) {
2236 cache_generate_key = cache_generate_key_default;
2242 static const command_rec cache_cmds[] =
2245 * Consider a new config directive that enables loading specific cache
2246 * implememtations (like mod_cache_mem, mod_cache_file, etc.).
2247 * Rather than using a LoadModule directive, admin would use something
2248 * like CacheModule mem_cache_module | file_cache_module, etc,
2249 * which would cause the approprpriate cache module to be loaded.
2250 * This is more intuitive that requiring a LoadModule directive.
2253 AP_INIT_TAKE12("CacheEnable", add_cache_enable, NULL, RSRC_CONF|ACCESS_CONF,
2254 "A cache type and partial URL prefix below which "
2255 "caching is enabled"),
2256 AP_INIT_TAKE1("CacheDisable", add_cache_disable, NULL, RSRC_CONF|ACCESS_CONF,
2257 "A partial URL prefix below which caching is disabled"),
2258 AP_INIT_TAKE1("CacheMaxExpire", set_cache_maxex, NULL, RSRC_CONF|ACCESS_CONF,
2259 "The maximum time in seconds to cache a document"),
2260 AP_INIT_TAKE1("CacheMinExpire", set_cache_minex, NULL, RSRC_CONF|ACCESS_CONF,
2261 "The minimum time in seconds to cache a document"),
2262 AP_INIT_TAKE1("CacheDefaultExpire", set_cache_defex, NULL, RSRC_CONF|ACCESS_CONF,
2263 "The default time in seconds to cache a document"),
2264 AP_INIT_FLAG("CacheQuickHandler", set_cache_quick_handler, NULL,
2266 "Run the cache in the quick handler, default on"),
2267 AP_INIT_FLAG("CacheIgnoreNoLastMod", set_cache_ignore_no_last_mod, NULL,
2268 RSRC_CONF|ACCESS_CONF,
2269 "Ignore Responses where there is no Last Modified Header"),
2270 AP_INIT_FLAG("CacheIgnoreCacheControl", set_cache_ignore_cachecontrol,
2272 "Ignore requests from the client for uncached content"),
2273 AP_INIT_FLAG("CacheStoreExpired", set_cache_store_expired,
2274 NULL, RSRC_CONF|ACCESS_CONF,
2275 "Ignore expiration dates when populating cache, resulting in "
2276 "an If-Modified-Since request to the backend on retrieval"),
2277 AP_INIT_FLAG("CacheStorePrivate", set_cache_store_private,
2278 NULL, RSRC_CONF|ACCESS_CONF,
2279 "Ignore 'Cache-Control: private' and store private content"),
2280 AP_INIT_FLAG("CacheStoreNoStore", set_cache_store_nostore,
2281 NULL, RSRC_CONF|ACCESS_CONF,
2282 "Ignore 'Cache-Control: no-store' and store sensitive content"),
2283 AP_INIT_ITERATE("CacheIgnoreHeaders", add_ignore_header, NULL, RSRC_CONF,
2284 "A space separated list of headers that should not be "
2285 "stored by the cache"),
2286 AP_INIT_FLAG("CacheIgnoreQueryString", set_cache_ignore_querystring,
2288 "Ignore query-string when caching"),
2289 AP_INIT_ITERATE("CacheIgnoreURLSessionIdentifiers", add_ignore_session_id,
2290 NULL, RSRC_CONF, "A space separated list of session "
2291 "identifiers that should be ignored for creating the key "
2292 "of the cached entity."),
2293 AP_INIT_TAKE1("CacheLastModifiedFactor", set_cache_factor, NULL, RSRC_CONF|ACCESS_CONF,
2294 "The factor used to estimate Expires date from "
2295 "LastModified date"),
2296 AP_INIT_FLAG("CacheLock", set_cache_lock,
2298 "Enable or disable the thundering herd lock."),
2299 AP_INIT_TAKE1("CacheLockPath", set_cache_lock_path, NULL, RSRC_CONF,
2300 "The thundering herd lock path. Defaults to the '"
2301 DEFAULT_CACHE_LOCKPATH "' directory in the system "
2303 AP_INIT_TAKE1("CacheLockMaxAge", set_cache_lock_maxage, NULL, RSRC_CONF,
2304 "Maximum age of any thundering herd lock."),
2305 AP_INIT_FLAG("CacheHeader", set_cache_x_cache, NULL, RSRC_CONF | ACCESS_CONF,
2306 "Add a X-Cache header to responses. Default is off."),
2307 AP_INIT_FLAG("CacheDetailHeader", set_cache_x_cache_detail, NULL,
2308 RSRC_CONF | ACCESS_CONF,
2309 "Add a X-Cache-Detail header to responses. Default is off."),
2310 AP_INIT_TAKE1("CacheKeyBaseURL", set_cache_key_base_url, NULL, RSRC_CONF,
2311 "Override the base URL of reverse proxied cache keys."),
2312 AP_INIT_FLAG("CacheStaleOnError", set_cache_stale_on_error,
2313 NULL, RSRC_CONF|ACCESS_CONF,
2314 "Serve stale content on 5xx errors if present. Defaults to on."),
2318 static void register_hooks(apr_pool_t *p)
2320 /* cache initializer */
2321 /* cache quick handler */
2322 ap_hook_quick_handler(cache_quick_handler, NULL, NULL, APR_HOOK_FIRST);
2324 ap_hook_handler(cache_handler, NULL, NULL, APR_HOOK_REALLY_FIRST);
2326 cache_hook_cache_status(cache_status, NULL, NULL, APR_HOOK_MIDDLE);
2327 /* cache error handler */
2328 ap_hook_insert_error_filter(cache_insert_error_filter, NULL, NULL, APR_HOOK_MIDDLE);
2330 * XXX The cache filters need to run right after the handlers and before
2331 * any other filters. Consider creating AP_FTYPE_CACHE for this purpose.
2333 * Depending on the type of request (subrequest / main request) they
2334 * need to be run before AP_FTYPE_CONTENT_SET / after AP_FTYPE_CONTENT_SET
2335 * filters. Thus create two filter handles for each type:
2336 * cache_save_filter_handle / cache_out_filter_handle to be used by
2338 * cache_save_subreq_filter_handle / cache_out_subreq_filter_handle
2339 * to be run by subrequest
2342 * CACHE is placed into the filter chain at an admin specified location,
2343 * and when the cache_handler is run, the CACHE filter is swapped with
2344 * the CACHE_OUT filter, or CACHE_SAVE filter as appropriate. This has
2345 * the effect of offering optional fine control of where the cache is
2346 * inserted into the filter chain.
2348 cache_filter_handle =
2349 ap_register_output_filter("CACHE",
2354 * CACHE_SAVE must go into the filter chain after a possible DEFLATE
2355 * filter to ensure that the compressed content is stored.
2356 * Incrementing filter type by 1 ensures his happens.
2358 cache_save_filter_handle =
2359 ap_register_output_filter("CACHE_SAVE",
2362 AP_FTYPE_CONTENT_SET+1);
2364 * CACHE_SAVE_SUBREQ must go into the filter chain before SUBREQ_CORE to
2365 * handle subrequsts. Decrementing filter type by 1 ensures this
2368 cache_save_subreq_filter_handle =
2369 ap_register_output_filter("CACHE_SAVE_SUBREQ",
2372 AP_FTYPE_CONTENT_SET-1);
2374 * CACHE_OUT must go into the filter chain after a possible DEFLATE
2375 * filter to ensure that already compressed cache objects do not
2376 * get compressed again. Incrementing filter type by 1 ensures
2379 cache_out_filter_handle =
2380 ap_register_output_filter("CACHE_OUT",
2383 AP_FTYPE_CONTENT_SET+1);
2385 * CACHE_OUT_SUBREQ must go into the filter chain before SUBREQ_CORE to
2386 * handle subrequsts. Decrementing filter type by 1 ensures this
2389 cache_out_subreq_filter_handle =
2390 ap_register_output_filter("CACHE_OUT_SUBREQ",
2393 AP_FTYPE_CONTENT_SET-1);
2394 /* CACHE_REMOVE_URL has to be a protocol filter to ensure that is
2395 * run even if the response is a canned error message, which
2396 * removes the content filters.
2398 cache_remove_url_filter_handle =
2399 ap_register_output_filter("CACHE_REMOVE_URL",
2400 cache_remove_url_filter,
2403 ap_hook_post_config(cache_post_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
2406 AP_DECLARE_MODULE(cache) =
2408 STANDARD20_MODULE_STUFF,
2409 create_dir_config, /* create per-directory config structure */
2410 merge_dir_config, /* merge per-directory config structures */
2411 create_cache_config, /* create per-server config structure */
2412 merge_cache_config, /* merge per-server config structures */
2413 cache_cmds, /* command apr_table_t */
2418 APR_HOOK_LINK(cache_status)
2421 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL(cache, CACHE, int, cache_status,
2422 (cache_handle_t *h, request_rec *r,
2423 apr_table_t *headers, ap_cache_status_e status,
2424 const char *reason), (h, r, headers, status, reason),