Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mod_cache.h"
#include "cache_storage.h"
#include "cache_util.h"
module AP_MODULE_DECLARE_DATA cache_module;
APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
/* -------------------------------------------------------------- */
/* Handles for cache filters, resolved at startup to eliminate
* a name-to-function mapping on each request
*/
static ap_filter_rec_t *cache_filter_handle;
static ap_filter_rec_t *cache_save_filter_handle;
static ap_filter_rec_t *cache_save_subreq_filter_handle;
static ap_filter_rec_t *cache_out_filter_handle;
static ap_filter_rec_t *cache_out_subreq_filter_handle;
static ap_filter_rec_t *cache_remove_url_filter_handle;
static ap_filter_rec_t *cache_invalidate_filter_handle;
/**
* Entity headers' names
*/
static const char *MOD_CACHE_ENTITY_HEADERS[] = {
"Allow",
"Content-Encoding",
"Content-Language",
"Content-Length",
"Content-Location",
"Content-MD5",
"Content-Range",
"Content-Type",
"Last-Modified",
NULL
};
/*
* CACHE handler
* -------------
*
* Can we deliver this request from the cache?
* If yes:
* deliver the content by installing the CACHE_OUT filter.
* If no:
* check whether we're allowed to try cache it
* If yes:
* add CACHE_SAVE filter
* If No:
* oh well.
*
* By default, the cache handler runs in the quick handler, bypassing
* virtually all server processing and offering the cache its optimal
* performance. In this mode, the cache bolts onto the front of the
* server, and behaves as a discrete RFC2616 caching proxy
* implementation.
*
* Under certain circumstances, an admin might want to run the cache as
* a normal handler instead of a quick handler, allowing the cache to
* run after the authorisation hooks, or by allowing fine control over
* the placement of the cache in the filter chain. This option comes at
* a performance penalty, and should only be used to achieve specific
* caching goals where the admin understands what they are doing.
*/
static int cache_quick_handler(request_rec *r, int lookup)
{
apr_status_t rv;
const char *auth;
cache_provider_list *providers;
cache_request_rec *cache;
apr_bucket_brigade *out;
apr_bucket *e;
ap_filter_t *next;
ap_filter_rec_t *cache_out_handle;
cache_server_conf *conf;
conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
&cache_module);
/* only run if the quick handler is enabled */
if (!conf->quick) {
return DECLINED;
}
/*
* Which cache module (if any) should handle this request?
*/
if (!(providers = cache_get_providers(r, conf))) {
return DECLINED;
}
/* make space for the per request config */
cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
cache->size = -1;
cache->out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
/* save away the possible providers */
cache->providers = providers;
/*
* Are we allowed to serve cached info at all?
*/
if (!ap_cache_check_no_store(cache, r)) {
return DECLINED;
}
/* find certain cache controlling headers */
auth = apr_table_get(r->headers_in, "Authorization");
/* First things first - does the request allow us to return
* cached information at all? If not, just decline the request.
*/
if (auth) {
return DECLINED;
}
/* Are we PUT/POST/DELETE? If so, prepare to invalidate the cached entities.
*/
switch (r->method_number) {
case M_PUT:
case M_POST:
case M_DELETE:
{
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(02461)
"PUT/POST/DELETE: Adding CACHE_INVALIDATE filter for %s",
r->uri);
/* Add cache_invalidate filter to this request to force a
* cache entry to be invalidated if the response is
* ultimately successful (2xx).
*/
ap_add_output_filter_handle(
cache_invalidate_filter_handle, cache, r,
r->connection);
return DECLINED;
}
case M_GET: {
break;
}
default : {
ap_log_rerror(
APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(02462) "cache: Method '%s' not cacheable by mod_cache, ignoring: %s", r->method, r->uri);
return DECLINED;
}
}
/*
* Try to serve this request from the cache.
*
* If no existing cache file (DECLINED)
* add cache_save filter
* If cached file (OK)
* clear filter stack
* add cache_out filter
* return OK
*/
rv = cache_select(cache, r);
if (rv != OK) {
if (rv == DECLINED) {
if (!lookup) {
/* try to obtain a cache lock at this point. if we succeed,
* we are the first to try and cache this url. if we fail,
* it means someone else is already trying to cache this
* url, and we should just let the request through to the
* backend without any attempt to cache. this stops
* duplicated simultaneous attempts to cache an entity.
*/
rv = cache_try_lock(conf, cache, r);
if (APR_SUCCESS == rv) {
/*
* Add cache_save filter to cache this request. Choose
* the correct filter by checking if we are a subrequest
* or not.
*/
if (r->main) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r, APLOGNO(00749) "Adding CACHE_SAVE_SUBREQ filter for %s",
r->uri);
cache->save_filter = ap_add_output_filter_handle(
cache_save_subreq_filter_handle, cache, r,
r->connection);
}
else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r, APLOGNO(00750) "Adding CACHE_SAVE filter for %s",
r->uri);
cache->save_filter = ap_add_output_filter_handle(
cache_save_filter_handle, cache, r,
r->connection);
}
apr_pool_userdata_setn(cache, CACHE_CTX_KEY, NULL, r->pool);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00751)
"Adding CACHE_REMOVE_URL filter for %s",
r->uri);
/* Add cache_remove_url filter to this request to remove a
* stale cache entry if needed. Also put the current cache
* request rec in the filter context, as the request that
* is available later during running the filter may be
* different due to an internal redirect.
*/
cache->remove_url_filter = ap_add_output_filter_handle(
cache_remove_url_filter_handle, cache, r,
r->connection);
}
else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv,
r, APLOGNO(00752) "Cache locked for url, not caching "
"response: %s", r->uri);
/* cache_select() may have added conditional headers */
if (cache->stale_headers) {
r->headers_in = cache->stale_headers;
}
}
}
else {
if (cache->stale_headers) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r, APLOGNO(00753) "Restoring request headers for %s",
r->uri);
r->headers_in = cache->stale_headers;
}
}
}
else {
/* error */
return rv;
}
return DECLINED;
}
/* we've got a cache hit! tell everyone who cares */
cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_HIT,
"cache hit");
/* if we are a lookup, we are exiting soon one way or another; Restore
* the headers. */
if (lookup) {
if (cache->stale_headers) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00754)
"Restoring request headers.");
r->headers_in = cache->stale_headers;
}
}
rv = ap_meets_conditions(r);
if (rv != OK) {
/* If we are a lookup, we have to return DECLINED as we have no
* way of knowing if we will be able to serve the content.
*/
if (lookup) {
return DECLINED;
}
/* Return cached status. */
return rv;
}
/* If we're a lookup, we can exit now instead of serving the content. */
if (lookup) {
return OK;
}
/* Serve up the content */
/* We are in the quick handler hook, which means that no output
* filters have been set. So lets run the insert_filter hook.
*/
ap_run_insert_filter(r);
/*
* Add cache_out filter to serve this request. Choose
* the correct filter by checking if we are a subrequest
* or not.
*/
if (r->main) {
cache_out_handle = cache_out_subreq_filter_handle;
}
else {
cache_out_handle = cache_out_filter_handle;
}
ap_add_output_filter_handle(cache_out_handle, cache, r, r->connection);
/*
* Remove all filters that are before the cache_out filter. This ensures
* that we kick off the filter stack with our cache_out filter being the
* first in the chain. This make sense because we want to restore things
* in the same manner as we saved them.
* There may be filters before our cache_out filter, because
*
* 1. We call ap_set_content_type during cache_select. This causes
* Content-Type specific filters to be added.
* 2. We call the insert_filter hook. This causes filters e.g. like
* the ones set with SetOutputFilter to be added.
*/
next = r->output_filters;
while (next && (next->frec != cache_out_handle)) {
ap_remove_output_filter(next);
next = next->next;
}
/* kick off the filter stack */
out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
e = apr_bucket_eos_create(out->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(out, e);
return ap_pass_brigade_fchk(r, out,
"cache_quick_handler(%s): ap_pass_brigade returned",
cache->provider_name);
}
/**
* If the two filter handles are present within the filter chain, replace
* the last instance of the first filter with the last instance of the
* second filter, and return true. If the second filter is not present at
* all, the first filter is removed, and false is returned. If neither
* filter is present, false is returned and this function does nothing.
* If a stop filter is specified, processing will stop once this filter is
* reached.
*/
static int cache_replace_filter(ap_filter_t *next, ap_filter_rec_t *from,
ap_filter_rec_t *to, ap_filter_rec_t *stop) {
ap_filter_t *ffrom = NULL, *fto = NULL;
while (next && next->frec != stop) {
if (next->frec == from) {
ffrom = next;
}
if (next->frec == to) {
fto = next;
}
next = next->next;
}
if (ffrom && fto) {
ffrom->frec = fto->frec;
ffrom->ctx = fto->ctx;
ap_remove_output_filter(fto);
return 1;
}
if (ffrom) {
ap_remove_output_filter(ffrom);
}
return 0;
}
/**
* Find the given filter, and return it if found, or NULL otherwise.
*/
static ap_filter_t *cache_get_filter(ap_filter_t *next, ap_filter_rec_t *rec) {
while (next) {
if (next->frec == rec && next->ctx) {
break;
}
next = next->next;
}
return next;
}
/**
* The cache handler is functionally similar to the cache_quick_hander,
* however a number of steps that are required by the quick handler are
* not required here, as the normal httpd processing has already handled
* these steps.
*/
static int cache_handler(request_rec *r)
{
apr_status_t rv;
cache_provider_list *providers;
cache_request_rec *cache;
apr_bucket_brigade *out;
apr_bucket *e;
ap_filter_t *next;
ap_filter_rec_t *cache_out_handle;
ap_filter_rec_t *cache_save_handle;
cache_server_conf *conf;
conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
&cache_module);
/* only run if the quick handler is disabled */
if (conf->quick) {
return DECLINED;
}
/*
* Which cache module (if any) should handle this request?
*/
if (!(providers = cache_get_providers(r, conf))) {
return DECLINED;
}
/* make space for the per request config */
cache = apr_pcalloc(r->pool, sizeof(cache_request_rec));
cache->size = -1;
cache->out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
/* save away the possible providers */
cache->providers = providers;
/*
* Are we allowed to serve cached info at all?
*/
if (!ap_cache_check_no_store(cache, r)) {
return DECLINED;
}
/* Are we PUT/POST/DELETE? If so, prepare to invalidate the cached entities.
*/
switch (r->method_number) {
case M_PUT:
case M_POST:
case M_DELETE:
{
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(02463)
"PUT/POST/DELETE: Adding CACHE_INVALIDATE filter for %s",
r->uri);
/* Add cache_invalidate filter to this request to force a
* cache entry to be invalidated if the response is
* ultimately successful (2xx).
*/
ap_add_output_filter_handle(
cache_invalidate_filter_handle, cache, r,
r->connection);
return DECLINED;
}
case M_GET: {
break;
}
default : {
ap_log_rerror(
APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(02464) "cache: Method '%s' not cacheable by mod_cache, ignoring: %s", r->method, r->uri);
return DECLINED;
}
}
/*
* Try to serve this request from the cache.
*
* If no existing cache file (DECLINED)
* add cache_save filter
* If cached file (OK)
* clear filter stack
* add cache_out filter
* return OK
*/
rv = cache_select(cache, r);
if (rv != OK) {
if (rv == DECLINED) {
/* try to obtain a cache lock at this point. if we succeed,
* we are the first to try and cache this url. if we fail,
* it means someone else is already trying to cache this
* url, and we should just let the request through to the
* backend without any attempt to cache. this stops
* duplicated simultaneous attempts to cache an entity.
*/
rv = cache_try_lock(conf, cache, r);
if (APR_SUCCESS == rv) {
/*
* Add cache_save filter to cache this request. Choose
* the correct filter by checking if we are a subrequest
* or not.
*/
if (r->main) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r, APLOGNO(00756) "Adding CACHE_SAVE_SUBREQ filter for %s",
r->uri);
cache_save_handle = cache_save_subreq_filter_handle;
}
else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r, APLOGNO(00757) "Adding CACHE_SAVE filter for %s",
r->uri);
cache_save_handle = cache_save_filter_handle;
}
ap_add_output_filter_handle(cache_save_handle, cache, r,
r->connection);
/*
* Did the user indicate the precise location of the
* CACHE_SAVE filter by inserting the CACHE filter as a
* marker?
*
* If so, we get cunning and replace CACHE with the
* CACHE_SAVE filter. This has the effect of inserting
* the CACHE_SAVE filter at the precise location where
* the admin wants to cache the content. All filters that
* lie before and after the original location of the CACHE
* filter will remain in place.
*/
if (cache_replace_filter(r->output_filters,
cache_filter_handle, cache_save_handle,
ap_get_input_filter_handle("SUBREQ_CORE"))) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r, APLOGNO(00758) "Replacing CACHE with CACHE_SAVE "
"filter for %s", r->uri);
}
/* save away the save filter stack */
cache->save_filter = cache_get_filter(r->output_filters,
cache_save_filter_handle);
apr_pool_userdata_setn(cache, CACHE_CTX_KEY, NULL, r->pool);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00759)
"Adding CACHE_REMOVE_URL filter for %s",
r->uri);
/* Add cache_remove_url filter to this request to remove a
* stale cache entry if needed. Also put the current cache
* request rec in the filter context, as the request that
* is available later during running the filter may be
* different due to an internal redirect.
*/
cache->remove_url_filter
= ap_add_output_filter_handle(
cache_remove_url_filter_handle, cache, r,
r->connection);
}
else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv,
r, APLOGNO(00760) "Cache locked for url, not caching "
"response: %s", r->uri);
}
}
else {
/* error */
return rv;
}
return DECLINED;
}
/* we've got a cache hit! tell everyone who cares */
cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_HIT,
"cache hit");
rv = ap_meets_conditions(r);
if (rv != OK) {
return rv;
}
/* Serve up the content */
/*
* Add cache_out filter to serve this request. Choose
* the correct filter by checking if we are a subrequest
* or not.
*/
if (r->main) {
cache_out_handle = cache_out_subreq_filter_handle;
}
else {
cache_out_handle = cache_out_filter_handle;
}
ap_add_output_filter_handle(cache_out_handle, cache, r, r->connection);
/*
* Did the user indicate the precise location of the CACHE_OUT filter by
* inserting the CACHE filter as a marker?
*
* If so, we get cunning and replace CACHE with the CACHE_OUT filters.
* This has the effect of inserting the CACHE_OUT filter at the precise
* location where the admin wants to cache the content. All filters that
* lie *after* the original location of the CACHE filter will remain in
* place.
*/
if (cache_replace_filter(r->output_filters, cache_filter_handle,
cache_out_handle, ap_get_input_filter_handle("SUBREQ_CORE"))) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
r, APLOGNO(00761) "Replacing CACHE with CACHE_OUT filter for %s",
r->uri);
}
/*
* Remove all filters that are before the cache_out filter. This ensures
* that we kick off the filter stack with our cache_out filter being the
* first in the chain. This make sense because we want to restore things
* in the same manner as we saved them.
* There may be filters before our cache_out filter, because
*
* 1. We call ap_set_content_type during cache_select. This causes
* Content-Type specific filters to be added.
* 2. We call the insert_filter hook. This causes filters e.g. like
* the ones set with SetOutputFilter to be added.
*/
next = r->output_filters;
while (next && (next->frec != cache_out_handle)) {
ap_remove_output_filter(next);
next = next->next;
}
/* kick off the filter stack */
out = apr_brigade_create(r->pool, r->connection->bucket_alloc);
e = apr_bucket_eos_create(out->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(out, e);
return ap_pass_brigade_fchk(r, out, "cache(%s): ap_pass_brigade returned",
cache->provider_name);
}
/*
* CACHE_OUT filter
* ----------------
*
* Deliver cached content (headers and body) up the stack.
*/
static apr_status_t cache_out_filter(ap_filter_t *f, apr_bucket_brigade *in)
{
request_rec *r = f->r;
cache_request_rec *cache = (cache_request_rec *)f->ctx;
if (!cache) {
/* user likely configured CACHE_OUT manually; they should use mod_cache
* configuration to do that */
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00762)
"CACHE/CACHE_OUT filter enabled while caching is disabled, ignoring");
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, in);
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00763)
"cache: running CACHE_OUT filter");
/* clean out any previous response up to EOS, if any */
while (!APR_BRIGADE_EMPTY(in)) {
apr_bucket *e = APR_BRIGADE_FIRST(in);
if (APR_BUCKET_IS_EOS(e)) {
apr_bucket_brigade *bb = apr_brigade_create(r->pool,
r->connection->bucket_alloc);
/* restore content type of cached response if available */
/* Needed especially when stale content gets served. */
const char *ct = apr_table_get(cache->handle->resp_hdrs, "Content-Type");
if (ct) {
ap_set_content_type(r, ct);
}
/* restore status of cached response */
r->status = cache->handle->cache_obj->info.status;
/* recall_headers() was called in cache_select() */
cache->provider->recall_body(cache->handle, r->pool, bb);
APR_BRIGADE_PREPEND(in, bb);
/* This filter is done once it has served up its content */
ap_remove_output_filter(f);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00764)
"cache: serving %s", r->uri);
return ap_pass_brigade(f->next, in);
}
apr_bucket_delete(e);
}
return APR_SUCCESS;
}
/*
* Having jumped through all the hoops and decided to cache the
* response, call store_body() for each brigade, handling the
* case where the provider can't swallow the full brigade. In this
* case, we write the brigade we were passed out downstream, and
* loop around to try and cache some more until the in brigade is
* completely empty. As soon as the out brigade contains eos, call
* commit_entity() to finalise the cached element.
*/
static int cache_save_store(ap_filter_t *f, apr_bucket_brigade *in,
cache_server_conf *conf, cache_request_rec *cache)
{
int rv = APR_SUCCESS;
apr_bucket *e;
/* pass the brigade in into the cache provider, which is then
* expected to move cached buckets to the out brigade, for us
* to pass up the filter stack. repeat until in is empty, or
* we fail.
*/
while (APR_SUCCESS == rv && !APR_BRIGADE_EMPTY(in)) {
rv = cache->provider->store_body(cache->handle, f->r, in, cache->out);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, f->r, APLOGNO(00765)
"cache: Cache provider's store_body failed for URI %s", f->r->uri);
ap_remove_output_filter(f);
/* give someone else the chance to cache the file */
cache_remove_lock(conf, cache, f->r, NULL);
/* give up trying to cache, just step out the way */
APR_BRIGADE_PREPEND(in, cache->out);
return ap_pass_brigade(f->next, in);
}
/* does the out brigade contain eos? if so, we're done, commit! */
for (e = APR_BRIGADE_FIRST(cache->out);
e != APR_BRIGADE_SENTINEL(cache->out);
e = APR_BUCKET_NEXT(e))
{
if (APR_BUCKET_IS_EOS(e)) {
rv = cache->provider->commit_entity(cache->handle, f->r);
break;
}
}
/* conditionally remove the lock as soon as we see the eos bucket */
cache_remove_lock(conf, cache, f->r, cache->out);
if (APR_BRIGADE_EMPTY(cache->out)) {
if (APR_BRIGADE_EMPTY(in)) {
/* cache provider wants more data before passing the brigade
* upstream, oblige the provider by leaving to fetch more.
*/
break;
}
else {
/* oops, no data out, but not all data read in either, be
* safe and stand down to prevent a spin.
*/
ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, f->r, APLOGNO(00766)
"cache: Cache provider's store_body returned an "
"empty brigade, but didn't consume all of the "
"input brigade, standing down to prevent a spin");
ap_remove_output_filter(f);
/* give someone else the chance to cache the file */
cache_remove_lock(conf, cache, f->r, NULL);
return ap_pass_brigade(f->next, in);
}
}
rv = ap_pass_brigade(f->next, cache->out);
}
return rv;
}
/**
* Sanity check for 304 Not Modified responses, as per RFC2616 Section 10.3.5.
*/
static int cache_header_cmp(apr_pool_t *pool, apr_table_t *left,
apr_table_t *right, const char *key)
{
const char *h1, *h2;
if ((h1 = cache_table_getm(pool, left, key))
&& (h2 = cache_table_getm(pool, right, key)) && (strcmp(h1, h2))) {
return 1;
}
return 0;
}
/*
* CACHE_SAVE filter
* ---------------
*
* Decide whether or not this content should be cached.
* If we decide no it should not:
* remove the filter from the chain
* If we decide yes it should:
* Have we already started saving the response?
* If we have started, pass the data to the storage manager via store_body
* Otherwise:
* Check to see if we *can* save this particular response.
* If we can, call cache_create_entity() and save the headers and body
* Finally, pass the data to the next filter (the network or whatever)
*
* After the various failure cases, the cache lock is proactively removed, so
* that another request is given the opportunity to attempt to cache without
* waiting for a potentially slow client to acknowledge the failure.
*/
static apr_status_t cache_save_filter(ap_filter_t *f, apr_bucket_brigade *in)
{
int rv = !OK;
request_rec *r = f->r;
cache_request_rec *cache = (cache_request_rec *)f->ctx;
cache_server_conf *conf;
cache_dir_conf *dconf;
cache_control_t control;
const char *cc_out, *cl, *pragma;
const char *exps, *lastmods, *dates, *etag;
apr_time_t exp, date, lastmod, now;
apr_off_t size = -1;
cache_info *info = NULL;
const char *reason, **eh;
apr_pool_t *p;
apr_bucket *e;
apr_table_t *headers;
const char *query;
conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
&cache_module);
/* Setup cache_request_rec */
if (!cache) {
/* user likely configured CACHE_SAVE manually; they should really use
* mod_cache configuration to do that
*/
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00767)
"CACHE/CACHE_SAVE filter enabled while caching is disabled, ignoring");
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, in);
}
reason = NULL;
p = r->pool;
/*
* Pass Data to Cache
* ------------------
* This section passes the brigades into the cache modules, but only
* if the setup section (see below) is complete.
*/
if (cache->block_response) {
/* We've already sent down the response and EOS. So, ignore
* whatever comes now.
*/
return APR_SUCCESS;
}
/* have we already run the cacheability check and set up the
* cached file handle?
*/
if (cache->in_checked) {
return cache_save_store(f, in, conf, cache);
}
/*
* Setup Data in Cache
* -------------------
* This section opens the cache entity and sets various caching
* parameters, and decides whether this URL should be cached at
* all. This section is* run before the above section.
*/
dconf = ap_get_module_config(r->per_dir_config, &cache_module);
/* RFC2616 13.8 Errors or Incomplete Response Cache Behavior:
* If a cache receives a 5xx response while attempting to revalidate an
* entry, it MAY either forward this response to the requesting client,
* or act as if the server failed to respond. In the latter case, it MAY
* return a previously received response unless the cached entry
* includes the "must-revalidate" cache-control directive (see section
* 14.9).
*
* This covers the case where an error was generated behind us, for example
* by a backend server via mod_proxy.
*/
if (dconf->stale_on_error && r->status >= HTTP_INTERNAL_SERVER_ERROR) {
ap_remove_output_filter(cache->remove_url_filter);
if (cache->stale_handle
&& !cache->stale_handle->cache_obj->info.control.must_revalidate
&& !cache->stale_handle->cache_obj->info.control.proxy_revalidate) {
const char *warn_head;
/* morph the current save filter into the out filter, and serve from
* cache.
*/
cache->handle = cache->stale_handle;
if (r->main) {
f->frec = cache_out_subreq_filter_handle;
}
else {
f->frec = cache_out_filter_handle;
}
r->headers_out = cache->stale_handle->resp_hdrs;
ap_set_content_type(r, apr_table_get(
cache->stale_handle->resp_hdrs, "Content-Type"));
/* add a revalidation warning */
warn_head = apr_table_get(r->err_headers_out, "Warning");
if ((warn_head == NULL) || ((warn_head != NULL)
&& (ap_strstr_c(warn_head, "111") == NULL))) {
apr_table_mergen(r->err_headers_out, "Warning",
"111 Revalidation failed");
}
cache_run_cache_status(cache->handle, r, r->headers_out, AP_CACHE_HIT,
apr_psprintf(r->pool,
"cache hit: %d status; stale content returned",
r->status));
/* give someone else the chance to cache the file */
cache_remove_lock(conf, cache, f->r, NULL);
/* pass brigade to our morphed out filter */
return ap_pass_brigade(f, in);
}
}
query = cache_use_early_url(r) ? r->parsed_uri.query : r->args;
/* read expiry date; if a bad date, then leave it so the client can
* read it
*/
exps = apr_table_get(r->err_headers_out, "Expires");
if (exps == NULL) {
exps = apr_table_get(r->headers_out, "Expires");
}
if (exps != NULL) {
exp = apr_date_parse_http(exps);
}
else {
exp = APR_DATE_BAD;
}
/* read the last-modified date; if the date is bad, then delete it */
lastmods = apr_table_get(r->err_headers_out, "Last-Modified");
if (lastmods == NULL) {
lastmods = apr_table_get(r->headers_out, "Last-Modified");
}
if (lastmods != NULL) {
lastmod = apr_date_parse_http(lastmods);
if (lastmod == APR_DATE_BAD) {
lastmods = NULL;
}
}
else {
lastmod = APR_DATE_BAD;
}
/* read the etag and cache-control from the entity */
etag = apr_table_get(r->err_headers_out, "Etag");
if (etag == NULL) {
etag = apr_table_get(r->headers_out, "Etag");
}
cc_out = cache_table_getm(r->pool, r->err_headers_out, "Cache-Control");
pragma = cache_table_getm(r->pool, r->err_headers_out, "Pragma");
headers = r->err_headers_out;
if (!cc_out && !pragma) {
cc_out = cache_table_getm(r->pool, r->headers_out, "Cache-Control");
pragma = cache_table_getm(r->pool, r->headers_out, "Pragma");
headers = r->headers_out;
}
/* Have we received a 304 response without any headers at all? Fall back to
* the original headers in the original cached request.
*/
if (r->status == HTTP_NOT_MODIFIED && cache->stale_handle) {
if (!cc_out && !pragma) {
cc_out = cache_table_getm(r->pool, cache->stale_handle->resp_hdrs,
"Cache-Control");
pragma = cache_table_getm(r->pool, cache->stale_handle->resp_hdrs,
"Pragma");
}
/* 304 does not contain Content-Type and mod_mime regenerates the
* Content-Type based on the r->filename. This would lead to original
* Content-Type to be lost (overwriten by whatever mod_mime generates).
* We preserves the original Content-Type here. */
ap_set_content_type(r, apr_table_get(
cache->stale_handle->resp_hdrs, "Content-Type"));
}
/* Parse the cache control header */
memset(&control, 0, sizeof(cache_control_t));
ap_cache_control(r, &control, cc_out, pragma, headers);
/*
* what responses should we not cache?