]> granicus.if.org Git - apache/blob - modules/cache/mod_cache.c
Fix a spelling mistake.
[apache] / modules / cache / mod_cache.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "mod_cache.h"
18
19 #include "cache_storage.h"
20 #include "cache_util.h"
21
22 module AP_MODULE_DECLARE_DATA cache_module;
23 APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
24
25 /* -------------------------------------------------------------- */
26
27
28 /* Handles for cache filters, resolved at startup to eliminate
29  * a name-to-function mapping on each request
30  */
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;
37
38 /*
39  * CACHE handler
40  * -------------
41  *
42  * Can we deliver this request from the cache?
43  * If yes:
44  *   deliver the content by installing the CACHE_OUT filter.
45  * If no:
46  *   check whether we're allowed to try cache it
47  *   If yes:
48  *     add CACHE_SAVE filter
49  *   If No:
50  *     oh well.
51  *
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
56  * implementation.
57  *
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.
64  */
65
66 static int cache_quick_handler(request_rec *r, int lookup)
67 {
68     apr_status_t rv;
69     const char *auth;
70     cache_provider_list *providers;
71     cache_request_rec *cache;
72     apr_bucket_brigade *out;
73     apr_bucket *e;
74     ap_filter_t *next;
75     ap_filter_rec_t *cache_out_handle;
76     cache_server_conf *conf;
77
78     /* Delay initialization until we know we are handling a GET */
79     if (r->method_number != M_GET) {
80         return DECLINED;
81     }
82
83     conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
84                                                       &cache_module);
85
86     /* only run if the quick handler is enabled */
87     if (!conf->quick) {
88         return DECLINED;
89     }
90
91     /*
92      * Which cache module (if any) should handle this request?
93      */
94     if (!(providers = cache_get_providers(r, conf, r->parsed_uri))) {
95         return DECLINED;
96     }
97
98     /* make space for the per request config */
99     cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
100     cache->size = -1;
101     cache->out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
102
103     /* save away the possible providers */
104     cache->providers = providers;
105
106     /*
107      * Are we allowed to serve cached info at all?
108      */
109
110     /* find certain cache controlling headers */
111     auth = apr_table_get(r->headers_in, "Authorization");
112
113     /* First things first - does the request allow us to return
114      * cached information at all? If not, just decline the request.
115      */
116     if (auth) {
117         return DECLINED;
118     }
119
120     /*
121      * Try to serve this request from the cache.
122      *
123      * If no existing cache file (DECLINED)
124      *   add cache_save filter
125      * If cached file (OK)
126      *   clear filter stack
127      *   add cache_out filter
128      *   return OK
129      */
130     rv = cache_select(cache, r);
131     if (rv != OK) {
132         if (rv == DECLINED) {
133             if (!lookup) {
134
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.
141                  */
142                 rv = cache_try_lock(conf, cache, r);
143                 if (APR_SUCCESS == rv) {
144
145                     /*
146                      * Add cache_save filter to cache this request. Choose
147                      * the correct filter by checking if we are a subrequest
148                      * or not.
149                      */
150                     if (r->main) {
151                         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
152                                 r, "Adding CACHE_SAVE_SUBREQ filter for %s",
153                                 r->uri);
154                         cache->save_filter = ap_add_output_filter_handle(
155                                 cache_save_subreq_filter_handle, cache, r,
156                                 r->connection);
157                     }
158                     else {
159                         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
160                                 r, "Adding CACHE_SAVE filter for %s",
161                                 r->uri);
162                         cache->save_filter = ap_add_output_filter_handle(
163                                 cache_save_filter_handle, cache, r,
164                                 r->connection);
165                     }
166
167                     apr_pool_userdata_setn(cache, CACHE_CTX_KEY, NULL, r->pool);
168
169                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
170                             "Adding CACHE_REMOVE_URL filter for %s",
171                             r->uri);
172
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.
178                      */
179                     cache->remove_url_filter =
180                         ap_add_output_filter_handle(cache_remove_url_filter_handle,
181                                 cache, r, r->connection);
182                 }
183                 else {
184                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv,
185                             r, "Cache locked for url, not caching "
186                             "response: %s", r->uri);
187                 }
188             }
189             else {
190                 if (cache->stale_headers) {
191                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
192                             r, "Restoring request headers for %s",
193                             r->uri);
194
195                     r->headers_in = cache->stale_headers;
196                 }
197             }
198         }
199         else {
200             /* error */
201             return rv;
202         }
203         return DECLINED;
204     }
205
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,
208             "cache hit");
209
210     /* if we are a lookup, we are exiting soon one way or another; Restore
211      * the headers. */
212     if (lookup) {
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;
217         }
218     }
219
220     rv = ap_meets_conditions(r);
221     if (rv != OK) {
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.
224          */
225         if (lookup) {
226             return DECLINED;
227         }
228
229         /* Return cached status. */
230         return rv;
231     }
232
233     /* If we're a lookup, we can exit now instead of serving the content. */
234     if (lookup) {
235         return OK;
236     }
237
238     /* Serve up the content */
239
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.
242      */
243     ap_run_insert_filter(r);
244
245     /*
246      * Add cache_out filter to serve this request. Choose
247      * the correct filter by checking if we are a subrequest
248      * or not.
249      */
250     if (r->main) {
251         cache_out_handle = cache_out_subreq_filter_handle;
252     }
253     else {
254         cache_out_handle = cache_out_filter_handle;
255     }
256     ap_add_output_filter_handle(cache_out_handle, cache, r, r->connection);
257
258     /*
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
264      *
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.
269      */
270     next = r->output_filters;
271     while (next && (next->frec != cache_out_handle)) {
272         ap_remove_output_filter(next);
273         next = next->next;
274     }
275
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;
288         }
289         return rv;
290     }
291
292     return OK;
293 }
294
295 /**
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.
301  */
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;
305     while (next) {
306         if (next->frec == from && !next->ctx) {
307             ffrom = next;
308         }
309         if (next->frec == to && !next->ctx) {
310             fto = next;
311         }
312         next = next->next;
313     }
314     if (ffrom && fto) {
315         ffrom->frec = fto->frec;
316         ffrom->ctx = fto->ctx;
317         ap_remove_output_filter(fto);
318         return 1;
319     }
320     if (ffrom) {
321         ap_remove_output_filter(ffrom);
322     }
323     return 0;
324 }
325
326 /**
327  * Find the given filter, and return it if found, or NULL otherwise.
328  */
329 static ap_filter_t *cache_get_filter(ap_filter_t *next, ap_filter_rec_t *rec) {
330     while (next) {
331         if (next->frec == rec && next->ctx) {
332             break;
333         }
334         next = next->next;
335     }
336     return next;
337 }
338
339 /**
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
343  * these steps.
344  */
345 static int cache_handler(request_rec *r)
346 {
347     apr_status_t rv;
348     cache_provider_list *providers;
349     cache_request_rec *cache;
350     apr_bucket_brigade *out;
351     apr_bucket *e;
352     ap_filter_t *next;
353     ap_filter_rec_t *cache_out_handle;
354     ap_filter_rec_t *cache_save_handle;
355     cache_server_conf *conf;
356
357     /* Delay initialization until we know we are handling a GET */
358     if (r->method_number != M_GET) {
359         return DECLINED;
360     }
361
362     conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
363                                                       &cache_module);
364
365     /* only run if the quick handler is disabled */
366     if (conf->quick) {
367         return DECLINED;
368     }
369
370     /*
371      * Which cache module (if any) should handle this request?
372      */
373     if (!(providers = cache_get_providers(r, conf, r->parsed_uri))) {
374         return DECLINED;
375     }
376
377     /* make space for the per request config */
378     cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
379     cache->size = -1;
380     cache->out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
381
382     /* save away the possible providers */
383     cache->providers = providers;
384
385     /*
386      * Try to serve this request from the cache.
387      *
388      * If no existing cache file (DECLINED)
389      *   add cache_save filter
390      * If cached file (OK)
391      *   clear filter stack
392      *   add cache_out filter
393      *   return OK
394      */
395     rv = cache_select(cache, r);
396     if (rv != OK) {
397         if (rv == DECLINED) {
398
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.
405              */
406             rv = cache_try_lock(conf, cache, r);
407             if (APR_SUCCESS == rv) {
408
409                 /*
410                  * Add cache_save filter to cache this request. Choose
411                  * the correct filter by checking if we are a subrequest
412                  * or not.
413                  */
414                 if (r->main) {
415                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
416                             r, "Adding CACHE_SAVE_SUBREQ filter for %s",
417                             r->uri);
418                     cache_save_handle = cache_save_subreq_filter_handle;
419                 }
420                 else {
421                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
422                             r, "Adding CACHE_SAVE filter for %s",
423                             r->uri);
424                     cache_save_handle = cache_save_filter_handle;
425                 }
426                 ap_add_output_filter_handle(cache_save_handle, cache, r,
427                         r->connection);
428
429                 /*
430                  * Did the user indicate the precise location of the
431                  * CACHE_SAVE filter by inserting the CACHE filter as a
432                  * marker?
433                  *
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.
440                  */
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);
446                 }
447
448                 /* save away the save filter stack */
449                 cache->save_filter = cache_get_filter(r->output_filters,
450                         cache_save_filter_handle);
451
452                 apr_pool_userdata_setn(cache, CACHE_CTX_KEY, NULL, r->pool);
453
454                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
455                         "Adding CACHE_REMOVE_URL filter for %s",
456                         r->uri);
457
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.
463                  */
464                 cache->remove_url_filter =
465                     ap_add_output_filter_handle(cache_remove_url_filter_handle,
466                             cache, r, r->connection);
467
468             }
469             else {
470                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv,
471                         r, "Cache locked for url, not caching "
472                         "response: %s", r->uri);
473             }
474         }
475         else {
476             /* error */
477             return rv;
478         }
479         return DECLINED;
480     }
481
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,
484             "cache hit");
485
486     rv = ap_meets_conditions(r);
487     if (rv != OK) {
488         return rv;
489     }
490
491     /* Serve up the content */
492
493     /*
494      * Add cache_out filter to serve this request. Choose
495      * the correct filter by checking if we are a subrequest
496      * or not.
497      */
498     if (r->main) {
499         cache_out_handle = cache_out_subreq_filter_handle;
500     }
501     else {
502         cache_out_handle = cache_out_filter_handle;
503     }
504     ap_add_output_filter_handle(cache_out_handle, cache, r, r->connection);
505
506     /*
507      * Did the user indicate the precise location of the CACHE_OUT filter by
508      * inserting the CACHE filter as a marker?
509      *
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
514      * place.
515      */
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",
519                 r->uri);
520     }
521
522     /*
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
528      *
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.
533      */
534     next = r->output_filters;
535     while (next && (next->frec != cache_out_handle)) {
536         ap_remove_output_filter(next);
537         next = next->next;
538     }
539
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;
552         }
553         return rv;
554     }
555
556     return OK;
557 }
558
559 /*
560  * CACHE_OUT filter
561  * ----------------
562  *
563  * Deliver cached content (headers and body) up the stack.
564  */
565 static int cache_out_filter(ap_filter_t *f, apr_bucket_brigade *in)
566 {
567     request_rec *r = f->r;
568     apr_bucket *e;
569     cache_request_rec *cache = (cache_request_rec *)f->ctx;
570
571     if (!cache) {
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);
578     }
579
580     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r,
581             "cache: running CACHE_OUT filter");
582
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))
587     {
588         if (APR_BUCKET_IS_EOS(e)) {
589             apr_bucket_brigade *bb = apr_brigade_create(r->pool,
590                     r->connection->bucket_alloc);
591
592             /* restore status of cached response */
593             r->status = cache->handle->cache_obj->info.status;
594
595             /* recall_headers() was called in cache_select() */
596             cache->provider->recall_body(cache->handle, r->pool, bb);
597             APR_BRIGADE_PREPEND(in, bb);
598
599             /* This filter is done once it has served up its content */
600             ap_remove_output_filter(f);
601
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);
605
606         }
607         apr_bucket_delete(e);
608     }
609
610     return APR_SUCCESS;
611 }
612
613 /*
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.
621  */
622 static int cache_save_store(ap_filter_t *f, apr_bucket_brigade *in,
623         cache_server_conf *conf, cache_request_rec *cache)
624 {
625     int rv = APR_SUCCESS;
626     apr_bucket *e;
627
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
631      * we fail.
632      */
633     while (APR_SUCCESS == rv && !APR_BRIGADE_EMPTY(in)) {
634
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);
640
641             /* give someone else the chance to cache the file */
642             cache_remove_lock(conf, cache, f->r, NULL);
643
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);
647
648         }
649
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))
654         {
655             if (APR_BUCKET_IS_EOS(e)) {
656                 rv = cache->provider->commit_entity(cache->handle, f->r);
657                 break;
658             }
659         }
660
661         /* conditionally remove the lock as soon as we see the eos bucket */
662         cache_remove_lock(conf, cache, f->r, cache->out);
663
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.
668                  */
669                 break;
670             }
671             else {
672                 /* oops, no data out, but not all data read in either, be
673                  * safe and stand down to prevent a spin.
674                  */
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);
680
681                 /* give someone else the chance to cache the file */
682                 cache_remove_lock(conf, cache, f->r, NULL);
683
684                 return ap_pass_brigade(f->next, in);
685             }
686         }
687
688         rv = ap_pass_brigade(f->next, cache->out);
689     }
690
691     return rv;
692 }
693
694 /*
695  * CACHE_SAVE filter
696  * ---------------
697  *
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
704  *      Otherwise:
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)
708  *
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.
712  */
713
714 static int cache_save_filter(ap_filter_t *f, apr_bucket_brigade *in)
715 {
716     int rv = !OK;
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;
725     apr_off_t size = -1;
726     cache_info *info = NULL;
727     char *reason;
728     apr_pool_t *p;
729     apr_bucket *e;
730     apr_table_t *headers;
731
732     conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
733                                                       &cache_module);
734
735     /* Setup cache_request_rec */
736     if (!cache) {
737         /* user likely configured CACHE_SAVE manually; they should really use
738          * mod_cache configuration to do that
739          */
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);
744     }
745
746     reason = NULL;
747     p = r->pool;
748     /*
749      * Pass Data to Cache
750      * ------------------
751      * This section passes the brigades into the cache modules, but only
752      * if the setup section (see below) is complete.
753      */
754     if (cache->block_response) {
755         /* We've already sent down the response and EOS.  So, ignore
756          * whatever comes now.
757          */
758         return APR_SUCCESS;
759     }
760
761     /* have we already run the cacheability check and set up the
762      * cached file handle?
763      */
764     if (cache->in_checked) {
765         return cache_save_store(f, in, conf, cache);
766     }
767
768     /*
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.
774      */
775
776     dconf = ap_get_module_config(r->per_dir_config, &cache_module);
777
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
784      * 14.9).
785      *
786      * This covers the case where an error was generated behind us, for example
787      * by a backend server via mod_proxy.
788      */
789     if (dconf->stale_on_error && r->status >= HTTP_INTERNAL_SERVER_ERROR) {
790
791         ap_remove_output_filter(cache->remove_url_filter);
792
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;
797
798             /* morph the current save filter into the out filter, and serve from
799              * cache.
800              */
801             cache->handle = cache->stale_handle;
802             if (r->main) {
803                 f->frec = cache_out_subreq_filter_handle;
804             }
805             else {
806                 f->frec = cache_out_filter_handle;
807             }
808
809             r->headers_out = cache->stale_handle->resp_hdrs;
810
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");
817             }
818
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",
822                             r->status));
823
824             /* give someone else the chance to cache the file */
825             cache_remove_lock(conf, cache, f->r, NULL);
826
827             /* pass brigade to our morphed out filter */
828             return ap_pass_brigade(f, in);
829         }
830     }
831
832     /* read expiry date; if a bad date, then leave it so the client can
833      * read it
834      */
835     exps = apr_table_get(r->err_headers_out, "Expires");
836     if (exps == NULL) {
837         exps = apr_table_get(r->headers_out, "Expires");
838     }
839     if (exps != NULL) {
840         if (APR_DATE_BAD == (exp = apr_date_parse_http(exps))) {
841             exps = NULL;
842         }
843     }
844     else {
845         exp = APR_DATE_BAD;
846     }
847
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");
852     }
853     if (lastmods != NULL) {
854         lastmod = apr_date_parse_http(lastmods);
855         if (lastmod == APR_DATE_BAD) {
856             lastmods = NULL;
857         }
858     }
859     else {
860         lastmod = APR_DATE_BAD;
861     }
862
863     /* read the etag and cache-control from the entity */
864     etag = apr_table_get(r->err_headers_out, "Etag");
865     if (etag == NULL) {
866         etag = apr_table_get(r->headers_out, "Etag");
867     }
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;
875     }
876
877     /* Have we received a 304 response without any headers at all? Fall back to
878      * the original headers in the original cached request.
879      */
880     if (r->status == HTTP_NOT_MODIFIED && cache->stale_handle && !cc_out
881             && !pragma) {
882         cc_out = apr_table_get(cache->stale_handle->resp_hdrs, "Cache-Control");
883         pragma = apr_table_get(cache->stale_handle->resp_hdrs, "Pragma");
884     }
885
886     /* Parse the cache control header */
887     memset(&control, 0, sizeof(cache_control_t));
888     ap_cache_control(r, &control, cc_out, pragma, headers);
889
890     /*
891      * what responses should we not cache?
892      *
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.
897      */
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.
908          */
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
913              * the RFC:
914              *
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).
922              */
923         }
924         else {
925             reason = apr_psprintf(p, "Response status %d", r->status);
926         }
927     }
928
929     if (reason) {
930         /* noop */
931     }
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);
935     }
936     else if (!dconf->store_expired && exp != APR_DATE_BAD
937             && exp < r->request_time)
938     {
939         /* if a Expires header is in the past, don't cache it */
940         reason = "Expires header already expired; not cacheable";
941     }
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)
946          */
947         reason = "Query string present but no explicit expiration time";
948     }
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.
953          */
954         reason = "HTTP Status 304 Not Modified";
955     }
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
961          * headers.
962          */
963         /* Note: mod-include clears last_modified/expires/etags - this
964          * is why we have an optional function for a key-gen ;-)
965          */
966         reason = "No Last-Modified; Etag; Expires; Cache-Control:max-age or Cache-Control:s-maxage headers";
967     }
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.
972          */
973         reason = "Cache-Control: no-store present";
974     }
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
978          * as a tunnel.
979          */
980         reason = "Cache-Control: private present";
981     }
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
991          */
992         reason = "Authorization required";
993     }
994     else if (ap_cache_liststr(NULL,
995                               apr_table_get(r->headers_out, "Vary"),
996                               "*", NULL)) {
997         reason = "Vary header contains '*'";
998     }
999     else if (apr_table_get(r->subprocess_env, "no-cache") != NULL) {
1000         reason = "environment variable 'no-cache' is set";
1001     }
1002     else if (r->no_cache) {
1003         /* or we've been asked not to cache it above */
1004         reason = "r->no_cache present";
1005     }
1006
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.
1016      */
1017     if (reason && r->status == HTTP_NOT_MODIFIED &&
1018              cache->stale_handle) {
1019         apr_bucket_brigade *bb;
1020         apr_bucket *bkt;
1021         int status;
1022
1023         cache->handle = cache->stale_handle;
1024         info = &cache->handle->cache_obj->info;
1025
1026         /* Load in the saved status and clear the status line. */
1027         r->status = info->status;
1028         r->status_line = NULL;
1029
1030         bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1031
1032         r->headers_in = cache->stale_headers;
1033         status = ap_meets_conditions(r);
1034         if (status != OK) {
1035             r->status = status;
1036
1037             bkt = apr_bucket_flush_create(bb->bucket_alloc);
1038             APR_BRIGADE_INSERT_TAIL(bb, bkt);
1039         }
1040         else {
1041             cache->provider->recall_body(cache->handle, r->pool, bb);
1042
1043             bkt = apr_bucket_eos_create(bb->bucket_alloc);
1044             APR_BRIGADE_INSERT_TAIL(bb, bkt);
1045         }
1046
1047         cache->block_response = 1;
1048
1049         /* we've got a cache conditional hit! tell anyone who cares */
1050         cache_run_cache_status(
1051                 cache->handle,
1052                 r,
1053                 r->headers_out,
1054                 AP_CACHE_REVALIDATE,
1055                 apr_psprintf(
1056                         r->pool,
1057                         "conditional cache hit: 304 was uncacheable though (%s); entity removed",
1058                         reason));
1059
1060         /* let someone else attempt to cache */
1061         cache_remove_lock(conf, cache, r, NULL);
1062
1063         return ap_pass_brigade(f->next, bb);
1064     }
1065
1066     if (reason) {
1067         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1068                 "cache: %s not cached. Reason: %s", r->unparsed_uri,
1069                 reason);
1070
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,
1073                 reason);
1074
1075         /* remove this filter from the chain */
1076         ap_remove_output_filter(f);
1077
1078         /* remove the lock file unconditionally */
1079         cache_remove_lock(conf, cache, r, NULL);
1080
1081         /* ship the data up the stack */
1082         return ap_pass_brigade(f->next, in);
1083     }
1084
1085     /* Make it so that we don't execute this path again. */
1086     cache->in_checked = 1;
1087
1088     /* Set the content length if known.
1089      */
1090     cl = apr_table_get(r->err_headers_out, "Content-Length");
1091     if (cl == NULL) {
1092         cl = apr_table_get(r->headers_out, "Content-Length");
1093     }
1094     if (cl) {
1095         char *errp;
1096         if (apr_strtoff(&size, cl, &errp, 10) || *errp || size < 0) {
1097             cl = NULL; /* parse error, see next 'if' block */
1098         }
1099     }
1100
1101     if (!cl) {
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
1104          */
1105         int all_buckets_here=0;
1106         size=0;
1107         for (e = APR_BRIGADE_FIRST(in);
1108              e != APR_BRIGADE_SENTINEL(in);
1109              e = APR_BUCKET_NEXT(e))
1110         {
1111             if (APR_BUCKET_IS_EOS(e)) {
1112                 all_buckets_here=1;
1113                 break;
1114             }
1115             if (APR_BUCKET_IS_FLUSH(e)) {
1116                 continue;
1117             }
1118             if (e->length == (apr_size_t)-1) {
1119                 break;
1120             }
1121             size += e->length;
1122         }
1123         if (!all_buckets_here) {
1124             size = -1;
1125         }
1126     }
1127
1128     /* remember content length to check response size against later */
1129     cache->size = size;
1130
1131     /* It's safe to cache the response.
1132      *
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
1141      * the headers).
1142      */
1143
1144     /* Did we have a stale cache entry that really is stale?
1145      */
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;
1151             rv = OK;
1152         }
1153         else {
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;
1158             /*
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.
1162              */
1163             r->headers_in = cache->stale_headers;
1164         }
1165     }
1166
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;
1173     }
1174
1175     if (rv != OK) {
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");
1179
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);
1184     }
1185
1186     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1187             "cache: Caching url: %s", r->unparsed_uri);
1188
1189     /* We are actually caching this response. So it does not
1190      * make sense to remove this entity any more.
1191      */
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);
1195
1196     /*
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.
1201      *
1202      * In addition, we make HTTP/1.1 age calculations and write them away
1203      * too.
1204      */
1205
1206     /* store away the previously parsed cache control headers */
1207     memcpy(&info->control, &control, sizeof(cache_control_t));
1208
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");
1213     }
1214     if (dates != NULL) {
1215         info->date = apr_date_parse_http(dates);
1216     }
1217     else {
1218         info->date = APR_DATE_BAD;
1219     }
1220
1221     now = apr_time_now();
1222     if (info->date == APR_DATE_BAD) {  /* No, or bad date */
1223         /* no date header (or bad header)! */
1224         info->date = now;
1225     }
1226     date = info->date;
1227
1228     /* set response_time for HTTP/1.1 age calculations */
1229     info->response_time = now;
1230
1231     /* get the request time */
1232     info->request_time = r->request_time;
1233
1234     /* check last-modified date */
1235     if (lastmod != APR_DATE_BAD && lastmod > date) {
1236         /* if it's in the future, then replace by date */
1237         lastmod = date;
1238         lastmods = dates;
1239         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0,
1240                 r, "cache: Last modified is in the future, "
1241                 "replacing with now");
1242     }
1243
1244     /* if no expiry date then
1245      *   if Cache-Control: max-age
1246      *      expiry date = date + max-age
1247      *   else if lastmod
1248      *      expiry date = date + min((date - lastmod) * factor, maxexpire)
1249      *   else
1250      *      expire date = date + defaultexpire
1251      */
1252     if (exp == APR_DATE_BAD) {
1253
1254         if (control.max_age) {
1255             apr_int64_t x;
1256
1257             errno = 0;
1258             x = control.max_age_value;
1259             if (errno) {
1260                 x = dconf->defex;
1261             }
1262             else {
1263                 x = x * MSEC_ONE_SEC;
1264             }
1265             if (x < dconf->minex) {
1266                 x = dconf->minex;
1267             }
1268             if (x > dconf->maxex) {
1269                 x = dconf->maxex;
1270             }
1271             exp = date + x;
1272         }
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...
1277              */
1278             apr_time_t x = (apr_time_t) ((date - lastmod) * dconf->factor);
1279
1280             if (x < dconf->minex) {
1281                 x = dconf->minex;
1282             }
1283             if (x > dconf->maxex) {
1284                 x = dconf->maxex;
1285             }
1286             exp = date + x;
1287         }
1288         else {
1289             exp = date + dconf->defex;
1290         }
1291     }
1292     info->expire = exp;
1293
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;
1299
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.
1304          *
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.
1308          */
1309         r->headers_out = ap_cache_cacheable_headers_out(r);
1310
1311         /* Merge in our cached headers.  However, keep any updated values. */
1312         cache_accept_headers(cache->handle, r, 1);
1313     }
1314
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.
1317      *
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
1320      * later.
1321      */
1322     rv = cache->provider->store_headers(cache->handle, r, info);
1323
1324     /* Did we just update the cached headers on a revalidated response?
1325      *
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.
1329      */
1330     if (cache->stale_handle) {
1331         apr_bucket_brigade *bb;
1332         apr_bucket *bkt;
1333         int status;
1334
1335         /* We're just saving response headers, so we are done. Commit
1336          * the response at this point, unless there was a previous error.
1337          */
1338         if (rv == APR_SUCCESS) {
1339             rv = cache->provider->commit_entity(cache->handle, r);
1340         }
1341
1342         bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1343
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).
1347          */
1348         r->headers_in = cache->stale_headers;
1349         status = ap_meets_conditions(r);
1350         if (status != OK) {
1351             r->status = status;
1352
1353             bkt = apr_bucket_flush_create(bb->bucket_alloc);
1354             APR_BRIGADE_INSERT_TAIL(bb, bkt);
1355         }
1356         else {
1357             cache->provider->recall_body(cache->handle, r->pool, bb);
1358
1359             bkt = apr_bucket_eos_create(bb->bucket_alloc);
1360             APR_BRIGADE_INSERT_TAIL(bb, bkt);
1361         }
1362
1363         cache->block_response = 1;
1364
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.
1369          */
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.");
1374
1375             rv = cache->provider->remove_url(cache->stale_handle, r);
1376             if (rv != OK) {
1377                 /* Probably a mod_disk_cache cache area has been (re)mounted
1378                  * read-only, or that there is a permissions problem.
1379                  */
1380                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
1381                         "cache: attempt to remove url from cache unsuccessful.");
1382             }
1383
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");
1388
1389         }
1390         else {
1391
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");
1396
1397         }
1398
1399         /* let someone else attempt to cache */
1400         cache_remove_lock(conf, cache, r, NULL);
1401
1402         return ap_pass_brigade(f->next, bb);
1403     }
1404
1405     if (rv != APR_SUCCESS) {
1406         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
1407                 "cache: store_headers failed");
1408
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");
1412
1413         ap_remove_output_filter(f);
1414         cache_remove_lock(conf, cache, r, NULL);
1415         return ap_pass_brigade(f->next, in);
1416     }
1417
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");
1421
1422     return cache_save_store(f, in, conf, cache);
1423 }
1424
1425 /*
1426  * CACHE_REMOVE_URL filter
1427  * -----------------------
1428  *
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
1431  * the cache.
1432  *
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.
1436  *
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.
1440  *
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
1444  * existing files.
1445  */
1446 static int cache_remove_url_filter(ap_filter_t *f, apr_bucket_brigade *in)
1447 {
1448     request_rec *r = f->r;
1449     cache_request_rec *cache;
1450
1451     /* Setup cache_request_rec */
1452     cache = (cache_request_rec *) f->ctx;
1453
1454     if (!cache) {
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
1459          */
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);
1464     }
1465
1466     /* Now remove this cache entry from the cache */
1467     cache_remove_url(cache, r);
1468
1469     /* remove ourselves */
1470     ap_remove_output_filter(f);
1471     return ap_pass_brigade(f->next, in);
1472 }
1473
1474 /*
1475  * CACHE filter
1476  * ------------
1477  *
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.
1481  *
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.
1486  *
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.
1490  */
1491 static int cache_filter(ap_filter_t *f, apr_bucket_brigade *in)
1492 {
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);
1499 }
1500
1501 /**
1502  * If configured, add the status of the caching attempt to the subprocess
1503  * environment, and if configured, to headers in the response.
1504  *
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.
1508  *
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.
1514  */
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)
1517 {
1518     cache_server_conf
1519             *conf =
1520                     (cache_server_conf *) ap_get_module_config(r->server->module_config,
1521                             &cache_module);
1522
1523     cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &cache_module);
1524     int x_cache = 0, x_cache_detail = 0;
1525
1526     switch (status) {
1527     case AP_CACHE_HIT: {
1528         apr_table_setn(r->subprocess_env, AP_CACHE_HIT_ENV, reason);
1529         break;
1530     }
1531     case AP_CACHE_REVALIDATE: {
1532         apr_table_setn(r->subprocess_env, AP_CACHE_REVALIDATE_ENV, reason);
1533         break;
1534     }
1535     case AP_CACHE_MISS: {
1536         apr_table_setn(r->subprocess_env, AP_CACHE_MISS_ENV, reason);
1537         break;
1538     }
1539     }
1540
1541     apr_table_setn(r->subprocess_env, AP_CACHE_STATUS_ENV, reason);
1542
1543     if (dconf && dconf->x_cache_set) {
1544         x_cache = dconf->x_cache;
1545     }
1546     else {
1547         x_cache = conf->x_cache;
1548     }
1549     if (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));
1555     }
1556
1557     if (dconf && dconf->x_cache_detail_set) {
1558         x_cache_detail = dconf->x_cache_detail;
1559     }
1560     else {
1561         x_cache_detail = conf->x_cache_detail;
1562     }
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));
1566     }
1567
1568     return OK;
1569 }
1570
1571 /**
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.
1575  */
1576 static void cache_insert_error_filter(request_rec *r)
1577 {
1578     void *dummy;
1579     cache_dir_conf *dconf;
1580
1581     /* ignore everything except for 5xx errors */
1582     if (r->status < HTTP_INTERNAL_SERVER_ERROR) {
1583         return;
1584     }
1585
1586     dconf = ap_get_module_config(r->per_dir_config, &cache_module);
1587
1588     if (!dconf->stale_on_error) {
1589         return;
1590     }
1591
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
1598      * 14.9).
1599      *
1600      * This covers the case where the error was generated by our server via
1601      * ap_die().
1602      */
1603     apr_pool_userdata_get(&dummy, CACHE_CTX_KEY, r->pool);
1604     if (dummy) {
1605         cache_request_rec *cache = (cache_request_rec *) dummy;
1606
1607         ap_remove_output_filter(cache->remove_url_filter);
1608
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;
1613             cache_server_conf
1614                     *conf =
1615                             (cache_server_conf *) ap_get_module_config(r->server->module_config,
1616                                     &cache_module);
1617
1618             /* morph the current save filter into the out filter, and serve from
1619              * cache.
1620              */
1621             cache->handle = cache->stale_handle;
1622             if (r->main) {
1623                 cache->save_filter->frec = cache_out_subreq_filter_handle;
1624             }
1625             else {
1626                 cache->save_filter->frec = cache_out_filter_handle;
1627             }
1628
1629             r->output_filters = cache->save_filter;
1630
1631             r->err_headers_out = cache->stale_handle->resp_hdrs;
1632
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");
1639             }
1640
1641             cache_run_cache_status(
1642                     cache->handle,
1643                     r,
1644                     r->err_headers_out,
1645                     AP_CACHE_HIT,
1646                     apr_psprintf(
1647                             r->pool,
1648                             "cache hit: %d status; stale content returned",
1649                             r->status));
1650
1651             /* give someone else the chance to cache the file */
1652             cache_remove_lock(conf, cache, r, NULL);
1653
1654         }
1655     }
1656
1657     return;
1658 }
1659
1660 /* -------------------------------------------------------------- */
1661 /* Setup configurable data */
1662
1663 static void *create_dir_config(apr_pool_t *p, char *dummy)
1664 {
1665     cache_dir_conf *dconf = apr_pcalloc(p, sizeof(cache_dir_conf));
1666
1667     dconf->no_last_mod_ignore = 0;
1668     dconf->store_expired = 0;
1669     dconf->store_private = 0;
1670     dconf->store_nostore = 0;
1671
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;
1677
1678     /* factor used to estimate Expires date from LastModified date */
1679     dconf->factor = DEFAULT_CACHE_LMFACTOR;
1680
1681     dconf->x_cache = DEFAULT_X_CACHE;
1682     dconf->x_cache_detail = DEFAULT_X_CACHE_DETAIL;
1683
1684     dconf->stale_on_error = DEFAULT_CACHE_STALE_ON_ERROR;
1685
1686     return dconf;
1687 }
1688
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;
1693
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;
1696
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;
1703
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;
1709
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;
1713
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;
1717
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;
1724
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;
1729
1730     return new;
1731 }
1732
1733 static void * create_cache_config(apr_pool_t *p, server_rec *s)
1734 {
1735     const char *tmppath;
1736     cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf));
1737
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 */
1751     ps->quick = 1;
1752     ps->quick_set = 0;
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 */
1757     ps->lock_set = 0;
1758     apr_temp_dir_get(&tmppath, p);
1759     if (tmppath) {
1760         ps->lockpath = apr_pstrcat(p, tmppath, DEFAULT_CACHE_LOCKPATH, NULL);
1761     }
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;
1765     return ps;
1766 }
1767
1768 static void * merge_cache_config(apr_pool_t *p, void *basev, void *overridesv)
1769 {
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;
1773
1774     /* array of URL prefixes for which caching is disabled */
1775     ps->cachedisable = apr_array_append(p,
1776                                         base->cachedisable,
1777                                         overrides->cachedisable);
1778     /* array of URL prefixes for which caching is enabled */
1779     ps->cacheenable = apr_array_append(p,
1780                                        base->cacheenable,
1781                                        overrides->cacheenable);
1782
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;
1799     ps->lock =
1800         (overrides->lock_set == 0)
1801         ? base->lock
1802         : overrides->lock;
1803     ps->lockpath =
1804         (overrides->lockpath_set == 0)
1805         ? base->lockpath
1806         : overrides->lockpath;
1807     ps->lockmaxage =
1808         (overrides->lockmaxage_set == 0)
1809         ? base->lockmaxage
1810         : overrides->lockmaxage;
1811     ps->quick =
1812         (overrides->quick_set == 0)
1813         ? base->quick
1814         : overrides->quick;
1815     ps->x_cache =
1816         (overrides->x_cache_set == 0)
1817         ? base->x_cache
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;
1823     ps->base_uri =
1824         (overrides->base_uri_set == 0)
1825         ? base->base_uri
1826         : overrides->base_uri;
1827     return ps;
1828 }
1829
1830 static const char *set_cache_quick_handler(cmd_parms *parms, void *dummy,
1831                                            int flag)
1832 {
1833     cache_server_conf *conf;
1834
1835     conf =
1836         (cache_server_conf *)ap_get_module_config(parms->server->module_config
1837 ,
1838                                                   &cache_module);
1839     conf->quick = flag;
1840     conf->quick_set = 1;
1841     return NULL;
1842
1843 }
1844
1845 static const char *set_cache_ignore_no_last_mod(cmd_parms *parms, void *dummy,
1846                                                 int flag)
1847 {
1848     cache_dir_conf *dconf = (cache_dir_conf *)dummy;
1849
1850     dconf->no_last_mod_ignore = flag;
1851     dconf->no_last_mod_ignore_set = 1;
1852     return NULL;
1853
1854 }
1855
1856 static const char *set_cache_ignore_cachecontrol(cmd_parms *parms,
1857                                                  void *dummy, int flag)
1858 {
1859     cache_server_conf *conf;
1860
1861     conf =
1862         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
1863                                                   &cache_module);
1864     conf->ignorecachecontrol = flag;
1865     conf->ignorecachecontrol_set = 1;
1866     return NULL;
1867 }
1868
1869 static const char *set_cache_store_expired(cmd_parms *parms, void *dummy,
1870                                            int flag)
1871 {
1872     cache_dir_conf *dconf = (cache_dir_conf *)dummy;
1873
1874     dconf->store_expired = flag;
1875     dconf->store_expired_set = 1;
1876     return NULL;
1877 }
1878
1879 static const char *set_cache_store_private(cmd_parms *parms, void *dummy,
1880                                            int flag)
1881 {
1882     cache_dir_conf *dconf = (cache_dir_conf *)dummy;
1883
1884     dconf->store_private = flag;
1885     dconf->store_private_set = 1;
1886     return NULL;
1887 }
1888
1889 static const char *set_cache_store_nostore(cmd_parms *parms, void *dummy,
1890                                            int flag)
1891 {
1892     cache_dir_conf *dconf = (cache_dir_conf *)dummy;
1893
1894     dconf->store_nostore = flag;
1895     dconf->store_nostore_set = 1;
1896     return NULL;
1897 }
1898
1899 static const char *add_ignore_header(cmd_parms *parms, void *dummy,
1900                                      const char *header)
1901 {
1902     cache_server_conf *conf;
1903     char **new;
1904
1905     conf =
1906         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
1907                                                   &cache_module);
1908     if (!strcasecmp(header, "None")) {
1909         /* if header None is listed clear array */
1910         conf->ignore_headers->nelts = 0;
1911     }
1912     else {
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
1916              * so far.
1917              * (When 'None' is passed, IGNORE_HEADERS_SET && nelts == 0.)
1918              */
1919             new = (char **)apr_array_push(conf->ignore_headers);
1920             (*new) = (char *)header;
1921         }
1922     }
1923     conf->ignore_headers_set = CACHE_IGNORE_HEADERS_SET;
1924     return NULL;
1925 }
1926
1927 static const char *add_ignore_session_id(cmd_parms *parms, void *dummy,
1928                                          const char *identifier)
1929 {
1930     cache_server_conf *conf;
1931     char **new;
1932
1933     conf =
1934         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
1935                                                   &cache_module);
1936     if (!strcasecmp(identifier, "None")) {
1937         /* if identifier None is listed clear array */
1938         conf->ignore_session_id->nelts = 0;
1939     }
1940     else {
1941         if ((conf->ignore_session_id_set == CACHE_IGNORE_SESSION_ID_UNSET) ||
1942             (conf->ignore_session_id->nelts)) {
1943             /*
1944              * Only add identifier if no "None" has been found in identifier
1945              * list so far.
1946              */
1947             new = (char **)apr_array_push(conf->ignore_session_id);
1948             (*new) = (char *)identifier;
1949         }
1950     }
1951     conf->ignore_session_id_set = CACHE_IGNORE_SESSION_ID_SET;
1952     return NULL;
1953 }
1954
1955 static const char *add_cache_enable(cmd_parms *parms, void *dummy,
1956                                     const char *type,
1957                                     const char *url)
1958 {
1959     cache_server_conf *conf;
1960     struct cache_enable *new;
1961
1962     const char *err = ap_check_cmd_context(parms,
1963                                            NOT_IN_DIRECTORY|NOT_IN_LIMIT|NOT_IN_FILES);
1964     if (err != NULL) {
1965         return err;
1966     }
1967
1968     if (*type == '/') {
1969         return apr_psprintf(parms->pool,
1970           "provider (%s) starts with a '/'.  Are url and provider switched?",
1971           type);
1972     }
1973
1974     if (!url) {
1975         url = parms->path;
1976     }
1977     if (!url) {
1978         return apr_psprintf(parms->pool,
1979           "CacheEnable provider (%s) is missing an URL.", type);
1980     }
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 "
1983         "that location.";
1984     }
1985
1986     conf =
1987         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
1988                                                   &cache_module);
1989     new = apr_array_push(conf->cacheenable);
1990     new->type = type;
1991     if (apr_uri_parse(parms->pool, url, &(new->url))) {
1992         return NULL;
1993     }
1994     if (new->url.path) {
1995         new->pathlen = strlen(new->url.path);
1996     } else {
1997         new->pathlen = 1;
1998         new->url.path = "/";
1999     }
2000     return NULL;
2001 }
2002
2003 static const char *add_cache_disable(cmd_parms *parms, void *dummy,
2004                                      const char *url)
2005 {
2006     cache_server_conf *conf;
2007     struct cache_disable *new;
2008
2009     const char *err = ap_check_cmd_context(parms,
2010                                            NOT_IN_DIRECTORY|NOT_IN_LIMIT|NOT_IN_FILES);
2011     if (err != NULL) {
2012         return err;
2013     }
2014
2015     if (parms->path && !strcmp(url, "on")) {
2016         url = parms->path;
2017     }
2018     if (url[0] != '/' && !ap_strchr_c(url, ':')) {
2019         return "CacheDisable must specify a path or an URL, or when in a Location, "
2020             "the word 'on'.";
2021     }
2022
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 "
2025         "that location.";
2026     }
2027
2028     conf =
2029         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2030                                                   &cache_module);
2031     new = apr_array_push(conf->cachedisable);
2032     if (apr_uri_parse(parms->pool, url, &(new->url))) {
2033         return NULL;
2034     }
2035     if (new->url.path) {
2036         new->pathlen = strlen(new->url.path);
2037     } else {
2038         new->pathlen = 1;
2039         new->url.path = "/";
2040     }
2041     return NULL;
2042 }
2043
2044 static const char *set_cache_maxex(cmd_parms *parms, void *dummy,
2045                                    const char *arg)
2046 {
2047     cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2048
2049     dconf->maxex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
2050     dconf->maxex_set = 1;
2051     return NULL;
2052 }
2053
2054 static const char *set_cache_minex(cmd_parms *parms, void *dummy,
2055                                    const char *arg)
2056 {
2057     cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2058
2059     dconf->minex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
2060     dconf->minex_set = 1;
2061     return NULL;
2062 }
2063
2064 static const char *set_cache_defex(cmd_parms *parms, void *dummy,
2065                                    const char *arg)
2066 {
2067     cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2068
2069     dconf->defex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC);
2070     dconf->defex_set = 1;
2071     return NULL;
2072 }
2073
2074 static const char *set_cache_factor(cmd_parms *parms, void *dummy,
2075                                     const char *arg)
2076 {
2077     cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2078     double val;
2079
2080     if (sscanf(arg, "%lg", &val) != 1) {
2081         return "CacheLastModifiedFactor value must be a float";
2082     }
2083     dconf->factor = val;
2084     dconf->factor_set = 1;
2085     return NULL;
2086 }
2087
2088 static const char *set_cache_ignore_querystring(cmd_parms *parms, void *dummy,
2089                                                 int flag)
2090 {
2091     cache_server_conf *conf;
2092
2093     conf =
2094         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2095                                                   &cache_module);
2096     conf->ignorequerystring = flag;
2097     conf->ignorequerystring_set = 1;
2098     return NULL;
2099 }
2100
2101 static const char *set_cache_lock(cmd_parms *parms, void *dummy,
2102                                                 int flag)
2103 {
2104     cache_server_conf *conf;
2105
2106     conf =
2107         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2108                                                   &cache_module);
2109     conf->lock = flag;
2110     conf->lock_set = 1;
2111     return NULL;
2112 }
2113
2114 static const char *set_cache_lock_path(cmd_parms *parms, void *dummy,
2115                                     const char *arg)
2116 {
2117     cache_server_conf *conf;
2118
2119     conf =
2120         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2121                                                   &cache_module);
2122
2123     conf->lockpath = ap_server_root_relative(parms->pool, arg);
2124     if (!conf->lockpath) {
2125         return apr_pstrcat(parms->pool, "Invalid CacheLockPath path ",
2126                            arg, NULL);
2127     }
2128     conf->lockpath_set = 1;
2129     return NULL;
2130 }
2131
2132 static const char *set_cache_lock_maxage(cmd_parms *parms, void *dummy,
2133                                     const char *arg)
2134 {
2135     cache_server_conf *conf;
2136     apr_int64_t seconds;
2137
2138     conf =
2139         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2140                                                   &cache_module);
2141     seconds = apr_atoi64(arg);
2142     if (seconds <= 0) {
2143         return "CacheLockMaxAge value must be a non-zero positive integer";
2144     }
2145     conf->lockmaxage = apr_time_from_sec(seconds);
2146     conf->lockmaxage_set = 1;
2147     return NULL;
2148 }
2149
2150 static const char *set_cache_x_cache(cmd_parms *parms, void *dummy, int flag)
2151 {
2152
2153     if (parms->path) {
2154         cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2155
2156         dconf->x_cache = flag;
2157         dconf->x_cache_set = 1;
2158
2159     }
2160     else {
2161         cache_server_conf *conf =
2162             (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2163                                                       &cache_module);
2164
2165         conf->x_cache = flag;
2166         conf->x_cache_set = 1;
2167
2168     }
2169
2170     return NULL;
2171 }
2172
2173 static const char *set_cache_x_cache_detail(cmd_parms *parms, void *dummy, int flag)
2174 {
2175
2176     if (parms->path) {
2177         cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2178
2179         dconf->x_cache_detail = flag;
2180         dconf->x_cache_detail_set = 1;
2181
2182     }
2183     else {
2184         cache_server_conf *conf =
2185             (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2186                                                       &cache_module);
2187
2188         conf->x_cache_detail = flag;
2189         conf->x_cache_detail_set = 1;
2190
2191     }
2192
2193     return NULL;
2194 }
2195
2196 static const char *set_cache_key_base_url(cmd_parms *parms, void *dummy,
2197         const char *arg)
2198 {
2199     cache_server_conf *conf;
2200     apr_status_t rv;
2201
2202     conf =
2203         (cache_server_conf *)ap_get_module_config(parms->server->module_config,
2204                                                   &cache_module);
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);
2209     }
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);
2213     }
2214     conf->base_uri_set = 1;
2215     return NULL;
2216 }
2217
2218 static const char *set_cache_stale_on_error(cmd_parms *parms, void *dummy,
2219         int flag)
2220 {
2221     cache_dir_conf *dconf = (cache_dir_conf *)dummy;
2222
2223     dconf->stale_on_error = flag;
2224     dconf->stale_on_error_set = 1;
2225     return NULL;
2226 }
2227
2228 static int cache_post_config(apr_pool_t *p, apr_pool_t *plog,
2229                              apr_pool_t *ptemp, server_rec *s)
2230 {
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)
2233      */
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;
2237     }
2238     return OK;
2239 }
2240
2241
2242 static const command_rec cache_cmds[] =
2243 {
2244     /* XXX
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.
2251      */
2252
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,
2265                  RSRC_CONF,
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,
2271                  NULL, RSRC_CONF,
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,
2287                  NULL, RSRC_CONF,
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,
2297                  NULL, RSRC_CONF,
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 "
2302                   "temp directory."),
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."),
2315     {NULL}
2316 };
2317
2318 static void register_hooks(apr_pool_t *p)
2319 {
2320     /* cache initializer */
2321     /* cache quick handler */
2322     ap_hook_quick_handler(cache_quick_handler, NULL, NULL, APR_HOOK_FIRST);
2323     /* cache handler */
2324     ap_hook_handler(cache_handler, NULL, NULL, APR_HOOK_REALLY_FIRST);
2325     /* cache status */
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);
2329     /* cache filters
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.
2332      *
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
2337      * main requests and
2338      * cache_save_subreq_filter_handle / cache_out_subreq_filter_handle
2339      * to be run by subrequest
2340      */
2341     /*
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.
2347      */
2348     cache_filter_handle =
2349         ap_register_output_filter("CACHE",
2350                                   cache_filter,
2351                                   NULL,
2352                                   AP_FTYPE_RESOURCE);
2353     /*
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.
2357      */
2358     cache_save_filter_handle =
2359         ap_register_output_filter("CACHE_SAVE",
2360                                   cache_save_filter,
2361                                   NULL,
2362                                   AP_FTYPE_CONTENT_SET+1);
2363     /*
2364      * CACHE_SAVE_SUBREQ must go into the filter chain before SUBREQ_CORE to
2365      * handle subrequsts. Decrementing filter type by 1 ensures this
2366      * happens.
2367      */
2368     cache_save_subreq_filter_handle =
2369         ap_register_output_filter("CACHE_SAVE_SUBREQ",
2370                                   cache_save_filter,
2371                                   NULL,
2372                                   AP_FTYPE_CONTENT_SET-1);
2373     /*
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
2377      * this happens.
2378      */
2379     cache_out_filter_handle =
2380         ap_register_output_filter("CACHE_OUT",
2381                                   cache_out_filter,
2382                                   NULL,
2383                                   AP_FTYPE_CONTENT_SET+1);
2384     /*
2385      * CACHE_OUT_SUBREQ must go into the filter chain before SUBREQ_CORE to
2386      * handle subrequsts. Decrementing filter type by 1 ensures this
2387      * happens.
2388      */
2389     cache_out_subreq_filter_handle =
2390         ap_register_output_filter("CACHE_OUT_SUBREQ",
2391                                   cache_out_filter,
2392                                   NULL,
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.
2397      */
2398     cache_remove_url_filter_handle =
2399         ap_register_output_filter("CACHE_REMOVE_URL",
2400                                   cache_remove_url_filter,
2401                                   NULL,
2402                                   AP_FTYPE_PROTOCOL);
2403     ap_hook_post_config(cache_post_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
2404 }
2405
2406 AP_DECLARE_MODULE(cache) =
2407 {
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 */
2414     register_hooks
2415 };
2416
2417 APR_HOOK_STRUCT(
2418     APR_HOOK_LINK(cache_status)
2419 )
2420
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),
2425         OK, DECLINED)