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.
19 #include "mod_cache.h"
21 module AP_MODULE_DECLARE_DATA cache_module;
22 APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
24 /* -------------------------------------------------------------- */
27 /* Handles for cache filters, resolved at startup to eliminate
28 * a name-to-function mapping on each request
30 static ap_filter_rec_t *cache_save_filter_handle;
31 static ap_filter_rec_t *cache_out_filter_handle;
37 * Can we deliver this request from the cache?
39 * deliver the content by installing the CACHE_OUT filter.
41 * check whether we're allowed to try cache it
43 * add CACHE_SAVE filter
48 static int cache_url_handler(request_rec *r, int lookup)
51 const char *pragma, *auth;
55 cache_provider_list *providers;
57 cache_request_rec *cache;
58 cache_server_conf *conf;
59 apr_bucket_brigade *out;
61 /* Delay initialization until we know we are handling a GET */
62 if (r->method_number != M_GET) {
67 url = r->unparsed_uri;
71 conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
75 * Which cache module (if any) should handle this request?
77 if (!(providers = ap_cache_get_providers(r, conf, path))) {
81 /* make space for the per request config */
82 cache = (cache_request_rec *) ap_get_module_config(r->request_config,
85 cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
86 ap_set_module_config(r->request_config, &cache_module, cache);
89 /* save away the possible providers */
90 cache->providers = providers;
93 * Are we allowed to serve cached info at all?
96 /* find certain cache controlling headers */
97 pragma = apr_table_get(r->headers_in, "Pragma");
98 auth = apr_table_get(r->headers_in, "Authorization");
100 /* first things first - does the request allow us to return
101 * cached information at all? If not, just decline the request.
103 * Note that there is a big difference between not being allowed
104 * to cache a request (no-store) and not being allowed to return
105 * a cached request without revalidation (max-age=0).
107 * Caching is forbidden under the following circumstances:
109 * - RFC2616 14.9.2 Cache-Control: no-store
111 * - Any requests requiring authorization.
113 if (conf->ignorecachecontrol == 1 && auth == NULL) {
114 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
115 "incoming request is asking for a uncached version of "
116 "%s, but we know better and are ignoring it", url);
119 if (ap_cache_liststr(NULL, pragma, "no-cache", NULL) ||
121 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
122 "cache: no-cache or authorization forbids caching "
129 * Try to serve this request from the cache.
131 * If no existing cache file (DECLINED)
132 * add cache_save filter
133 * If cached file (OK)
135 * add cache_out filter
138 rv = cache_select_url(r, url);
140 if (rv == DECLINED) {
142 /* add cache_save filter to cache this request */
143 ap_add_output_filter_handle(cache_save_filter_handle, NULL, r,
149 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
150 "cache: error returned while checking for cached "
151 "file by %s cache", cache->provider_name);
156 /* We have located a suitable cache file now. */
157 info = &(cache->handle->cache_obj->info);
159 if (info && info->lastmod) {
160 ap_update_mtime(r, info->lastmod);
163 rv = ap_meets_conditions(r);
165 /* Return cached status. */
169 /* If we're a lookup, we can exit now instead of serving the content. */
174 /* Serve up the content */
176 /* We are in the quick handler hook, which means that no output
177 * filters have been set. So lets run the insert_filter hook.
179 ap_run_insert_filter(r);
180 ap_add_output_filter_handle(cache_out_filter_handle, NULL,
183 /* kick off the filter stack */
184 out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
185 rv = ap_pass_brigade(r->output_filters, out);
186 if (rv != APR_SUCCESS) {
187 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
188 "cache: error returned while trying to return %s "
190 cache->provider_name);
201 * Deliver cached content (headers and body) up the stack.
203 static int cache_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
205 request_rec *r = f->r;
206 cache_request_rec *cache;
208 cache = (cache_request_rec *) ap_get_module_config(r->request_config,
212 /* user likely configured CACHE_OUT manually; they should use mod_cache
213 * configuration to do that */
214 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
215 "CACHE_OUT enabled unexpectedly");
216 ap_remove_output_filter(f);
217 return ap_pass_brigade(f->next, bb);
220 ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
221 "cache: running CACHE_OUT filter");
223 /* restore status of cached response */
224 r->status = cache->handle->status;
226 /* recall_headers() was called in cache_select_url() */
227 cache->provider->recall_body(cache->handle, r->pool, bb);
229 /* This filter is done once it has served up its content */
230 ap_remove_output_filter(f);
232 ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
233 "cache: serving %s", r->uri);
234 return ap_pass_brigade(f->next, bb);
242 * Decide whether or not this content should be cached.
243 * If we decide no it should not:
244 * remove the filter from the chain
245 * If we decide yes it should:
246 * Have we already started saving the response?
247 * If we have started, pass the data to the storage manager via store_body
249 * Check to see if we *can* save this particular response.
250 * If we can, call cache_create_entity() and save the headers and body
251 * Finally, pass the data to the next filter (the network or whatever)
254 static int cache_save_filter(ap_filter_t *f, apr_bucket_brigade *in)
257 int date_in_errhdr = 0;
258 request_rec *r = f->r;
259 cache_request_rec *cache;
260 cache_server_conf *conf;
261 char *url = r->unparsed_uri;
262 const char *cc_in, *cc_out, *cl, *vary_out;
263 const char *exps, *lastmods, *dates, *etag;
264 apr_time_t exp, date, lastmod, now;
270 /* check first whether running this filter has any point or not */
271 /* If the user has Cache-Control: no-store from RFC 2616, don't store! */
272 cc_in = apr_table_get(r->headers_in, "Cache-Control");
273 vary_out = apr_table_get(r->headers_out, "Vary");
274 if (r->no_cache || ap_cache_liststr(NULL, cc_in, "no-store", NULL) ||
275 ap_cache_liststr(NULL, vary_out, "*", NULL)) {
276 ap_remove_output_filter(f);
277 return ap_pass_brigade(f->next, in);
280 /* Setup cache_request_rec */
281 cache = (cache_request_rec *) ap_get_module_config(r->request_config,
284 /* user likely configured CACHE_SAVE manually; they should really use
285 * mod_cache configuration to do that
287 cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
288 ap_set_module_config(r->request_config, &cache_module, cache);
296 * This section passes the brigades into the cache modules, but only
297 * if the setup section (see below) is complete.
299 if (cache->block_response) {
300 /* We've already sent down the response and EOS. So, ignore
301 * whatever comes now.
306 /* have we already run the cachability check and set up the
307 * cached file handle?
309 if (cache->in_checked) {
310 /* pass the brigades into the cache, then pass them
311 * up the filter stack
313 rv = cache->provider->store_body(cache->handle, r, in);
314 if (rv != APR_SUCCESS) {
315 ap_remove_output_filter(f);
317 return ap_pass_brigade(f->next, in);
321 * Setup Data in Cache
322 * -------------------
323 * This section opens the cache entity and sets various caching
324 * parameters, and decides whether this URL should be cached at
325 * all. This section is* run before the above section.
328 /* read expiry date; if a bad date, then leave it so the client can
331 exps = apr_table_get(r->err_headers_out, "Expires");
333 exps = apr_table_get(r->headers_out, "Expires");
336 if (APR_DATE_BAD == (exp = apr_date_parse_http(exps))) {
344 /* read the last-modified date; if the date is bad, then delete it */
345 lastmods = apr_table_get(r->err_headers_out, "Last-Modified");
346 if (lastmods == NULL) {
347 lastmods = apr_table_get(r->headers_out, "Last-Modified");
349 if (lastmods != NULL) {
350 if (APR_DATE_BAD == (lastmod = apr_date_parse_http(lastmods))) {
355 lastmod = APR_DATE_BAD;
358 conf = (cache_server_conf *) ap_get_module_config(r->server->module_config, &cache_module);
359 /* read the etag and cache-control from the entity */
360 etag = apr_table_get(r->err_headers_out, "Etag");
362 etag = apr_table_get(r->headers_out, "Etag");
364 cc_out = apr_table_get(r->err_headers_out, "Cache-Control");
365 if (cc_out == NULL) {
366 cc_out = apr_table_get(r->headers_out, "Cache-Control");
370 * what responses should we not cache?
372 * At this point we decide based on the response headers whether it
373 * is appropriate _NOT_ to cache the data from the server. There are
374 * a whole lot of conditions that prevent us from caching this data.
375 * They are tested here one by one to be clear and unambiguous.
377 if (r->status != HTTP_OK && r->status != HTTP_NON_AUTHORITATIVE
378 && r->status != HTTP_MULTIPLE_CHOICES
379 && r->status != HTTP_MOVED_PERMANENTLY
380 && r->status != HTTP_NOT_MODIFIED) {
381 /* RFC2616 13.4 we are allowed to cache 200, 203, 206, 300, 301 or 410
382 * We don't cache 206, because we don't (yet) cache partial responses.
383 * We include 304 Not Modified here too as this is the origin server
384 * telling us to serve the cached copy.
386 reason = apr_psprintf(p, "Response status %d", r->status);
388 else if (exps != NULL && exp == APR_DATE_BAD) {
389 /* if a broken Expires header is present, don't cache it */
390 reason = apr_pstrcat(p, "Broken expires header: ", exps, NULL);
392 else if (r->args && exps == NULL) {
393 /* if query string present but no expiration time, don't cache it
396 reason = "Query string present but no expires header";
398 else if (r->status == HTTP_NOT_MODIFIED &&
399 !cache->handle && !cache->stale_handle) {
400 /* if the server said 304 Not Modified but we have no cache
401 * file - pass this untouched to the user agent, it's not for us.
403 reason = "HTTP Status 304 Not Modified";
405 else if (r->status == HTTP_OK && lastmods == NULL && etag == NULL
406 && (exps == NULL) && (conf->no_last_mod_ignore ==0)) {
407 /* 200 OK response from HTTP/1.0 and up without Last-Modified,
408 * Etag, or Expires headers.
410 /* Note: mod-include clears last_modified/expires/etags - this
411 * is why we have an optional function for a key-gen ;-)
413 reason = "No Last-Modified, Etag, or Expires headers";
415 else if (r->header_only) {
417 reason = "HTTP HEAD request";
419 else if (ap_cache_liststr(NULL, cc_out, "no-store", NULL)) {
420 /* RFC2616 14.9.2 Cache-Control: no-store response
421 * indicating do not cache, or stop now if you are
422 * trying to cache it */
423 reason = "Cache-Control: no-store present";
425 else if (ap_cache_liststr(NULL, cc_out, "private", NULL)) {
426 /* RFC2616 14.9.1 Cache-Control: private
427 * this object is marked for this user's eyes only. Behave
430 reason = "Cache-Control: private present";
432 else if (apr_table_get(r->headers_in, "Authorization") != NULL
433 && !(ap_cache_liststr(NULL, cc_out, "s-maxage", NULL)
434 || ap_cache_liststr(NULL, cc_out, "must-revalidate", NULL)
435 || ap_cache_liststr(NULL, cc_out, "public", NULL))) {
436 /* RFC2616 14.8 Authorisation:
437 * if authorisation is included in the request, we don't cache,
438 * but we can cache if the following exceptions are true:
439 * 1) If Cache-Control: s-maxage is included
440 * 2) If Cache-Control: must-revalidate is included
441 * 3) If Cache-Control: public is included
443 reason = "Authorization required";
445 else if (r->no_cache) {
446 /* or we've been asked not to cache it above */
447 reason = "no_cache present";
451 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
452 "cache: %s not cached. Reason: %s", url, reason);
453 /* remove this object from the cache
454 * BillS Asks.. Why do we need to make this call to remove_url?
455 * leave it in for now..
457 cache_remove_url(r, url);
459 /* remove this filter from the chain */
460 ap_remove_output_filter(f);
462 /* ship the data up the stack */
463 return ap_pass_brigade(f->next, in);
466 /* Make it so that we don't execute this path again. */
467 cache->in_checked = 1;
469 /* Set the content length if known.
471 cl = apr_table_get(r->err_headers_out, "Content-Length");
473 cl = apr_table_get(r->headers_out, "Content-Length");
478 if (apr_strtoff(&size, cl, &errp, 10) || *errp || size < 0) {
479 cl = NULL; /* parse error, see next 'if' block */
482 size = apr_atoi64(cl);
490 /* if we don't get the content-length, see if we have all the
491 * buckets and use their length to calculate the size
494 int all_buckets_here=0;
495 int unresolved_length = 0;
497 for (e = APR_BRIGADE_FIRST(in);
498 e != APR_BRIGADE_SENTINEL(in);
499 e = APR_BUCKET_NEXT(e))
501 if (APR_BUCKET_IS_EOS(e)) {
505 if (APR_BUCKET_IS_FLUSH(e)) {
506 unresolved_length = 1;
509 if (e->length == (apr_size_t)-1) {
514 if (!all_buckets_here) {
519 /* It's safe to cache the response.
521 * There are two possiblities at this point:
522 * - cache->handle == NULL. In this case there is no previously
523 * cached entity anywhere on the system. We must create a brand
524 * new entity and store the response in it.
525 * - cache->stale_handle != NULL. In this case there is a stale
526 * entity in the system which needs to be replaced by new
527 * content (unless the result was 304 Not Modified, which means
528 * the cached entity is actually fresh, and we should update
532 /* Did we have a stale cache entry that really is stale? */
533 if (cache->stale_handle) {
534 if (r->status == HTTP_NOT_MODIFIED) {
535 /* Oh, hey. It isn't that stale! Yay! */
536 cache->handle = cache->stale_handle;
537 info = &cache->handle->cache_obj->info;
540 /* Oh, well. Toss it. */
541 cache->provider->remove_entity(cache->stale_handle);
542 /* Treat the request as if it wasn't conditional. */
543 cache->stale_handle = NULL;
547 /* no cache handle, create a new entity */
548 if (!cache->handle) {
549 rv = cache_create_entity(r, url, size);
550 info = apr_pcalloc(r->pool, sizeof(cache_info));
551 /* We only set info->status upon the initial creation. */
552 info->status = r->status;
556 /* Caching layer declined the opportunity to cache the response */
557 ap_remove_output_filter(f);
558 return ap_pass_brigade(f->next, in);
561 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
562 "cache: Caching url: %s", url);
565 * We now want to update the cache file header information with
566 * the new date, last modified, expire and content length and write
567 * it away to our cache file. First, we determine these values from
568 * the response, using heuristics if appropriate.
570 * In addition, we make HTTP/1.1 age calculations and write them away
574 /* Read the date. Generate one if one is not supplied */
575 dates = apr_table_get(r->err_headers_out, "Date");
580 dates = apr_table_get(r->headers_out, "Date");
583 info->date = apr_date_parse_http(dates);
586 info->date = APR_DATE_BAD;
589 now = apr_time_now();
590 if (info->date == APR_DATE_BAD) { /* No, or bad date */
592 /* no date header (or bad header)! */
593 /* add one; N.B. use the time _now_ rather than when we were checking
596 if (date_in_errhdr == 1) {
597 apr_table_unset(r->err_headers_out, "Date");
600 dates = apr_pcalloc(r->pool, MAX_STRING_LEN);
601 apr_rfc822_date(dates, now);
602 apr_table_set(r->headers_out, "Date", dates);
603 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
604 "cache: Added date header");
611 /* set response_time for HTTP/1.1 age calculations */
612 info->response_time = now;
614 /* get the request time */
615 info->request_time = r->request_time;
617 /* check last-modified date */
618 if (lastmod != APR_DATE_BAD && lastmod > date) {
619 /* if it's in the future, then replace by date */
622 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
624 "cache: Last modified is in the future, "
625 "replacing with now");
627 info->lastmod = lastmod;
629 /* if no expiry date then
631 * expiry date = date + min((date - lastmod) * factor, maxexpire)
633 * expire date = date + defaultexpire
635 if (exp == APR_DATE_BAD) {
636 /* if lastmod == date then you get 0*conf->factor which results in
637 * an expiration time of now. This causes some problems with
638 * freshness calculations, so we choose the else path...
640 if ((lastmod != APR_DATE_BAD) && (lastmod < date)) {
641 apr_time_t x = (apr_time_t) ((date - lastmod) * conf->factor);
643 if (x > conf->maxex) {
649 exp = date + conf->defex;
654 info->content_type = apr_pstrdup(r->pool, r->content_type);
655 info->etag = apr_pstrdup(r->pool, etag);
656 info->lastmods = apr_pstrdup(r->pool, lastmods);
657 info->filename = apr_pstrdup(r->pool, r->filename);
660 * Write away header information to cache.
662 rv = cache->provider->store_headers(cache->handle, r, info);
664 /* Did we actually find an entity before, but it wasn't really stale? */
665 if (rv == APR_SUCCESS && cache->stale_handle) {
666 apr_bucket_brigade *bb;
669 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
671 /* Were we initially a conditional request? */
672 if (ap_cache_request_is_conditional(cache->stale_headers)) {
673 /* FIXME: Should we now go and make sure it's really not
674 * modified since what the user thought?
676 bkt = apr_bucket_eos_create(bb->bucket_alloc);
677 APR_BRIGADE_INSERT_TAIL(bb, bkt);
680 r->status = info->status;
681 cache->provider->recall_body(cache->handle, r->pool, bb);
684 cache->block_response = 1;
685 return ap_pass_brigade(f->next, bb);
688 if (rv == APR_SUCCESS) {
689 rv = cache->provider->store_body(cache->handle, r, in);
691 if (rv != APR_SUCCESS) {
692 ap_remove_output_filter(f);
695 return ap_pass_brigade(f->next, in);
698 /* -------------------------------------------------------------- */
699 /* Setup configurable data */
701 static void * create_cache_config(apr_pool_t *p, server_rec *s)
703 cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf));
705 /* array of URL prefixes for which caching is enabled */
706 ps->cacheenable = apr_array_make(p, 10, sizeof(struct cache_enable));
707 /* array of URL prefixes for which caching is disabled */
708 ps->cachedisable = apr_array_make(p, 10, sizeof(struct cache_disable));
709 /* maximum time to cache a document */
710 ps->maxex = DEFAULT_CACHE_MAXEXPIRE;
712 /* default time to cache a document */
713 ps->defex = DEFAULT_CACHE_EXPIRE;
715 /* factor used to estimate Expires date from LastModified date */
716 ps->factor = DEFAULT_CACHE_LMFACTOR;
718 /* default percentage to force cache completion */
719 ps->complete = DEFAULT_CACHE_COMPLETION;
720 ps->complete_set = 0;
721 ps->no_last_mod_ignore_set = 0;
722 ps->no_last_mod_ignore = 0;
723 ps->ignorecachecontrol = 0;
724 ps->ignorecachecontrol_set = 0 ;
725 /* array of headers that should not be stored in cache */
726 ps->ignore_headers = apr_array_make(p, 10, sizeof(char *));
727 ps->ignore_headers_set = CACHE_IGNORE_HEADERS_UNSET;
731 static void * merge_cache_config(apr_pool_t *p, void *basev, void *overridesv)
733 cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf));
734 cache_server_conf *base = (cache_server_conf *) basev;
735 cache_server_conf *overrides = (cache_server_conf *) overridesv;
737 /* array of URL prefixes for which caching is disabled */
738 ps->cachedisable = apr_array_append(p,
740 overrides->cachedisable);
741 /* array of URL prefixes for which caching is enabled */
742 ps->cacheenable = apr_array_append(p,
744 overrides->cacheenable);
745 /* maximum time to cache a document */
746 ps->maxex = (overrides->maxex_set == 0) ? base->maxex : overrides->maxex;
747 /* default time to cache a document */
748 ps->defex = (overrides->defex_set == 0) ? base->defex : overrides->defex;
749 /* factor used to estimate Expires date from LastModified date */
751 (overrides->factor_set == 0) ? base->factor : overrides->factor;
752 /* default percentage to force cache completion */
754 (overrides->complete_set == 0) ? base->complete : overrides->complete;
756 ps->no_last_mod_ignore =
757 (overrides->no_last_mod_ignore_set == 0)
758 ? base->no_last_mod_ignore
759 : overrides->no_last_mod_ignore;
760 ps->ignorecachecontrol =
761 (overrides->ignorecachecontrol_set == 0)
762 ? base->ignorecachecontrol
763 : overrides->ignorecachecontrol;
765 (overrides->ignore_headers_set == CACHE_IGNORE_HEADERS_UNSET)
766 ? base->ignore_headers
767 : overrides->ignore_headers;
770 static const char *set_cache_ignore_no_last_mod(cmd_parms *parms, void *dummy,
773 cache_server_conf *conf;
776 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
778 conf->no_last_mod_ignore = flag;
779 conf->no_last_mod_ignore_set = 1;
784 static const char *set_cache_ignore_cachecontrol(cmd_parms *parms,
785 void *dummy, int flag)
787 cache_server_conf *conf;
790 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
792 conf->ignorecachecontrol = flag;
793 conf->ignorecachecontrol_set = 1;
797 static const char *add_ignore_header(cmd_parms *parms, void *dummy,
800 cache_server_conf *conf;
804 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
806 if (!strncasecmp(header, "None", 4)) {
807 /* if header None is listed clear array */
808 conf->ignore_headers->nelts = 0;
811 if ((conf->ignore_headers_set == CACHE_IGNORE_HEADERS_UNSET) ||
812 (conf->ignore_headers->nelts)) {
813 /* Only add header if no "None" has been found in header list
815 * (When 'None' is passed, IGNORE_HEADERS_SET && nelts == 0.)
817 new = (char **)apr_array_push(conf->ignore_headers);
818 (*new) = (char*)header;
821 conf->ignore_headers_set = CACHE_IGNORE_HEADERS_SET;
825 static const char *add_cache_enable(cmd_parms *parms, void *dummy,
829 cache_server_conf *conf;
830 struct cache_enable *new;
833 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
835 new = apr_array_push(conf->cacheenable);
838 new->urllen = strlen(url);
842 static const char *add_cache_disable(cmd_parms *parms, void *dummy,
845 cache_server_conf *conf;
846 struct cache_disable *new;
849 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
851 new = apr_array_push(conf->cachedisable);
853 new->urllen = strlen(url);
857 static const char *set_cache_maxex(cmd_parms *parms, void *dummy,
860 cache_server_conf *conf;
863 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
865 conf->maxex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
870 static const char *set_cache_defex(cmd_parms *parms, void *dummy,
873 cache_server_conf *conf;
876 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
878 conf->defex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
883 static const char *set_cache_factor(cmd_parms *parms, void *dummy,
886 cache_server_conf *conf;
890 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
892 if (sscanf(arg, "%lg", &val) != 1) {
893 return "CacheLastModifiedFactor value must be a float";
896 conf->factor_set = 1;
900 static const char *set_cache_complete(cmd_parms *parms, void *dummy,
903 cache_server_conf *conf;
907 (cache_server_conf *)ap_get_module_config(parms->server->module_config,
909 if (sscanf(arg, "%u", &val) != 1) {
910 return "CacheForceCompletion value must be a percentage";
912 conf->complete = val;
913 conf->complete_set = 1;
917 static int cache_post_config(apr_pool_t *p, apr_pool_t *plog,
918 apr_pool_t *ptemp, server_rec *s)
920 /* This is the means by which unusual (non-unix) os's may find alternate
921 * means to run a given command (e.g. shebang/registry parsing on Win32)
923 cache_generate_key = APR_RETRIEVE_OPTIONAL_FN(ap_cache_generate_key);
924 if (!cache_generate_key) {
925 cache_generate_key = cache_generate_key_default;
930 static const command_rec cache_cmds[] =
933 * Consider a new config directive that enables loading specific cache
934 * implememtations (like mod_cache_mem, mod_cache_file, etc.).
935 * Rather than using a LoadModule directive, admin would use something
936 * like CacheModule mem_cache_module | file_cache_module, etc,
937 * which would cause the approprpriate cache module to be loaded.
938 * This is more intuitive that requiring a LoadModule directive.
941 AP_INIT_TAKE2("CacheEnable", add_cache_enable, NULL, RSRC_CONF,
942 "A cache type and partial URL prefix below which "
943 "caching is enabled"),
944 AP_INIT_TAKE1("CacheDisable", add_cache_disable, NULL, RSRC_CONF,
945 "A partial URL prefix below which caching is disabled"),
946 AP_INIT_TAKE1("CacheMaxExpire", set_cache_maxex, NULL, RSRC_CONF,
947 "The maximum time in seconds to cache a document"),
948 AP_INIT_TAKE1("CacheDefaultExpire", set_cache_defex, NULL, RSRC_CONF,
949 "The default time in seconds to cache a document"),
950 AP_INIT_FLAG("CacheIgnoreNoLastMod", set_cache_ignore_no_last_mod, NULL,
952 "Ignore Responses where there is no Last Modified Header"),
953 AP_INIT_FLAG("CacheIgnoreCacheControl", set_cache_ignore_cachecontrol,
956 "Ignore requests from the client for uncached content"),
957 AP_INIT_ITERATE("CacheIgnoreHeaders", add_ignore_header, NULL, RSRC_CONF,
958 "A space separated list of headers that should not be "
959 "stored by the cache"),
960 AP_INIT_TAKE1("CacheLastModifiedFactor", set_cache_factor, NULL, RSRC_CONF,
961 "The factor used to estimate Expires date from "
962 "LastModified date"),
963 AP_INIT_TAKE1("CacheForceCompletion", set_cache_complete, NULL, RSRC_CONF,
964 "Percentage of download to arrive for the cache to force "
965 "complete transfer"),
969 static void register_hooks(apr_pool_t *p)
971 /* cache initializer */
973 ap_hook_quick_handler(cache_url_handler, NULL, NULL, APR_HOOK_FIRST);
975 * XXX The cache filters need to run right after the handlers and before
976 * any other filters. Consider creating AP_FTYPE_CACHE for this purpose.
977 * Make them AP_FTYPE_CONTENT for now.
978 * XXX ianhH:they should run AFTER all the other content filters.
980 cache_save_filter_handle =
981 ap_register_output_filter("CACHE_SAVE",
984 AP_FTYPE_CONTENT_SET-1);
985 /* CACHE_OUT must go into the filter chain before SUBREQ_CORE to
986 * handle subrequsts. Decrementing filter type by 1 ensures this
989 cache_out_filter_handle =
990 ap_register_output_filter("CACHE_OUT",
993 AP_FTYPE_CONTENT_SET-1);
994 ap_hook_post_config(cache_post_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
997 module AP_MODULE_DECLARE_DATA cache_module =
999 STANDARD20_MODULE_STUFF,
1000 NULL, /* create per-directory config structure */
1001 NULL, /* merge per-directory config structures */
1002 create_cache_config, /* create per-server config structure */
1003 merge_cache_config, /* merge per-server config structures */
1004 cache_cmds, /* command apr_table_t */