1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#include "apr_file_io.h"
18
#include "apr_strings.h"
19
#include "mod_cache.h"
20
#include "mod_disk_cache.h"
21
#include "ap_provider.h"
22
#include "util_filter.h"
23
#include "util_script.h"
24
#include "util_charset.h"
27
* mod_disk_cache: Disk Based HTTP 1.1 Cache.
29
* Flow to Find the .data file:
30
* Incoming client requests URI /foo/bar/baz
31
* Generate <hash> off of /foo/bar/baz
33
* Read in <hash>.header file (may contain Format #1 or Format #2)
34
* If format #1 (Contains a list of Vary Headers):
35
* Use each header name (from .header) with our request values (headers_in) to
36
* regenerate <hash> using HeaderName+HeaderValue+.../foo/bar/baz
37
* re-read in <hash>.header (must be format #2)
41
* apr_uint32_t format;
43
* apr_array_t vary_headers (delimited by CRLF)
46
* disk_cache_info_t (first sizeof(apr_uint32_t) bytes is the format)
47
* entity name (dobj->name) [length is in disk_cache_info_t->name_len]
48
* r->headers_out (delimited by CRLF)
50
* r->headers_in (delimited by CRLF)
54
module AP_MODULE_DECLARE_DATA disk_cache_module;
56
/* Forward declarations */
57
static int remove_entity(cache_handle_t *h);
58
static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *i);
59
static apr_status_t store_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *b);
60
static apr_status_t recall_headers(cache_handle_t *h, request_rec *r);
61
static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
62
static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
66
* Local static functions
69
static char *header_file(apr_pool_t *p, disk_cache_conf *conf,
70
disk_cache_object_t *dobj, const char *name)
72
if (!dobj->hashfile) {
73
dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
74
conf->dirlength, name);
78
return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
79
dobj->hashfile, CACHE_HEADER_SUFFIX, NULL);
82
return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
83
CACHE_HEADER_SUFFIX, NULL);
87
static char *data_file(apr_pool_t *p, disk_cache_conf *conf,
88
disk_cache_object_t *dobj, const char *name)
90
if (!dobj->hashfile) {
91
dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
92
conf->dirlength, name);
96
return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
97
dobj->hashfile, CACHE_DATA_SUFFIX, NULL);
100
return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
101
CACHE_DATA_SUFFIX, NULL);
105
static void mkdir_structure(disk_cache_conf *conf, const char *file, apr_pool_t *pool)
110
for (p = (char*)file + conf->cache_root_len + 1;;) {
116
rv = apr_dir_make(file,
117
APR_UREAD|APR_UWRITE|APR_UEXECUTE, pool);
118
if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) {
126
/* htcacheclean may remove directories underneath us.
127
* So, we'll try renaming three times at a cost of 0.002 seconds.
129
static apr_status_t safe_file_rename(disk_cache_conf *conf,
130
const char *src, const char *dest,
135
rv = apr_file_rename(src, dest, pool);
137
if (rv != APR_SUCCESS) {
140
for (i = 0; i < 2 && rv != APR_SUCCESS; i++) {
141
/* 1000 micro-seconds aka 0.001 seconds. */
144
mkdir_structure(conf, dest, pool);
146
rv = apr_file_rename(src, dest, pool);
153
static apr_status_t file_cache_el_final(disk_cache_object_t *dobj,
156
/* move the data over */
160
apr_file_close(dobj->tfd);
162
/* This assumes that the tempfile is on the same file system
163
* as the cache_root. If not, then we need a file copy/move
164
* rather than a rename.
166
rv = apr_file_rename(dobj->tempfile, dobj->datafile, r->pool);
167
if (rv != APR_SUCCESS) {
177
static apr_status_t file_cache_errorcleanup(disk_cache_object_t *dobj, request_rec *r)
179
/* Remove the header file and the body file. */
180
apr_file_remove(dobj->hdrsfile, r->pool);
181
apr_file_remove(dobj->datafile, r->pool);
183
/* If we opened the temporary data file, close and remove it. */
185
apr_file_close(dobj->tfd);
186
apr_file_remove(dobj->tempfile, r->pool);
194
/* These two functions get and put state information into the data
195
* file for an ap_cache_el, this state information will be read
196
* and written transparent to clients of this module
198
static int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,
199
disk_cache_object_t *dobj, request_rec *r)
203
disk_cache_info_t disk_info;
206
/* read the data from the cache file */
207
len = sizeof(disk_cache_info_t);
208
rv = apr_file_read_full(fd, &disk_info, len, &len);
209
if (rv != APR_SUCCESS) {
213
/* Store it away so we can get it later. */
214
dobj->disk_info = disk_info;
216
info->status = disk_info.status;
217
info->date = disk_info.date;
218
info->expire = disk_info.expire;
219
info->request_time = disk_info.request_time;
220
info->response_time = disk_info.response_time;
222
/* Note that we could optimize this by conditionally doing the palloc
223
* depending upon the size. */
224
urlbuff = apr_palloc(r->pool, disk_info.name_len + 1);
225
len = disk_info.name_len;
226
rv = apr_file_read_full(fd, urlbuff, len, &len);
227
if (rv != APR_SUCCESS) {
230
urlbuff[disk_info.name_len] = '\0';
232
/* check that we have the same URL */
233
/* Would strncmp be correct? */
234
if (strcmp(urlbuff, dobj->name) != 0) {
241
static const char* regen_key(apr_pool_t *p, apr_table_t *headers,
242
apr_array_header_t *varray, const char *oldkey)
250
nvec = (varray->nelts * 2) + 1;
251
iov = apr_palloc(p, sizeof(struct iovec) * nvec);
252
elts = (const char **) varray->elts;
255
* - Handle multiple-value headers better. (sort them?)
256
* - Handle Case in-sensitive Values better.
257
* This isn't the end of the world, since it just lowers the cache
258
* hit rate, but it would be nice to fix.
260
* The majority are case insenstive if they are values (encoding etc).
261
* Most of rfc2616 is case insensitive on header contents.
263
* So the better solution may be to identify headers which should be
264
* treated case-sensitive?
265
* HTTP URI's (3.2.3) [host and scheme are insensitive]
266
* HTTP method (5.1.1)
267
* HTTP-date values (3.3.1)
268
* 3.7 Media Types [exerpt]
269
* The type, subtype, and parameter attribute names are case-
270
* insensitive. Parameter values might or might not be case-sensitive,
271
* depending on the semantics of the parameter name.
272
* 4.20 Except [exerpt]
273
* Comparison of expectation values is case-insensitive for unquoted
274
* tokens (including the 100-continue token), and is case-sensitive for
275
* quoted-string expectation-extensions.
278
for(i=0, k=0; i < varray->nelts; i++) {
279
header = apr_table_get(headers, elts[i]);
283
iov[k].iov_base = (char*) elts[i];
284
iov[k].iov_len = strlen(elts[i]);
286
iov[k].iov_base = (char*) header;
287
iov[k].iov_len = strlen(header);
290
iov[k].iov_base = (char*) oldkey;
291
iov[k].iov_len = strlen(oldkey);
294
return apr_pstrcatv(p, iov, k, NULL);
297
static int array_alphasort(const void *fn1, const void *fn2)
299
return strcmp(*(char**)fn1, *(char**)fn2);
302
static void tokens_to_array(apr_pool_t *p, const char *data,
303
apr_array_header_t *arr)
307
while ((token = ap_get_list_item(p, &data)) != NULL) {
308
*((const char **) apr_array_push(arr)) = token;
311
/* Sort it so that "Vary: A, B" and "Vary: B, A" are stored the same. */
312
qsort((void *) arr->elts, arr->nelts,
313
sizeof(char *), array_alphasort);
317
* Hook and mod_cache callback functions
319
static int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len)
321
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
324
disk_cache_object_t *dobj;
326
if (conf->cache_root == NULL) {
330
/* Allocate and initialize cache_object_t and disk_cache_object_t */
331
h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj));
332
obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj));
334
obj->key = apr_pstrdup(r->pool, key);
336
dobj->name = obj->key;
338
/* Save the cache root */
339
dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
340
dobj->root_len = conf->cache_root_len;
341
dobj->datafile = data_file(r->pool, conf, dobj, key);
342
dobj->hdrsfile = header_file(r->pool, conf, dobj, key);
343
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
348
static int open_entity(cache_handle_t *h, request_rec *r, const char *key)
354
static int error_logged = 0;
355
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
360
disk_cache_object_t *dobj;
365
/* Look up entity keyed to 'url' */
366
if (conf->cache_root == NULL) {
369
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
370
"disk_cache: Cannot cache files to disk without a CacheRoot specified.");
375
/* Create and init the cache object */
376
h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(cache_object_t));
377
obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(disk_cache_object_t));
381
/* Open the headers file */
384
/* Save the cache root */
385
dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
386
dobj->root_len = conf->cache_root_len;
388
dobj->hdrsfile = header_file(r->pool, conf, dobj, key);
389
flags = APR_READ|APR_BINARY|APR_BUFFERED;
390
rc = apr_file_open(&dobj->hfd, dobj->hdrsfile, flags, 0, r->pool);
391
if (rc != APR_SUCCESS) {
395
/* read the format from the cache file */
396
len = sizeof(format);
397
apr_file_read_full(dobj->hfd, &format, len, &len);
399
if (format == VARY_FORMAT_VERSION) {
400
apr_array_header_t* varray;
403
len = sizeof(expire);
404
apr_file_read_full(dobj->hfd, &expire, len, &len);
406
if (expire < r->request_time) {
410
varray = apr_array_make(r->pool, 5, sizeof(char*));
411
rc = read_array(r, varray, dobj->hfd);
412
if (rc != APR_SUCCESS) {
413
ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
414
"disk_cache: Cannot parse vary header file: %s",
418
apr_file_close(dobj->hfd);
420
nkey = regen_key(r->pool, r->headers_in, varray, key);
422
dobj->hashfile = NULL;
423
dobj->prefix = dobj->hdrsfile;
424
dobj->hdrsfile = header_file(r->pool, conf, dobj, nkey);
426
flags = APR_READ|APR_BINARY|APR_BUFFERED;
427
rc = apr_file_open(&dobj->hfd, dobj->hdrsfile, flags, 0, r->pool);
428
if (rc != APR_SUCCESS) {
432
else if (format != DISK_FORMAT_VERSION) {
433
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
434
"cache_disk: File '%s' has a version mismatch. File had version: %d.",
435
dobj->hdrsfile, format);
439
apr_off_t offset = 0;
440
/* This wasn't a Vary Format file, so we must seek to the
441
* start of the file again, so that later reads work.
443
apr_file_seek(dobj->hfd, APR_SET, &offset);
450
dobj->datafile = data_file(r->pool, conf, dobj, nkey);
451
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
453
/* Open the data file */
454
flags = APR_READ|APR_BINARY;
455
#ifdef APR_SENDFILE_ENABLED
456
flags |= APR_SENDFILE_ENABLED;
458
rc = apr_file_open(&dobj->fd, dobj->datafile, flags, 0, r->pool);
459
if (rc != APR_SUCCESS) {
460
/* XXX: Log message */
464
rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, dobj->fd);
465
if (rc == APR_SUCCESS) {
466
dobj->file_size = finfo.size;
469
/* Read the bytes to setup the cache_info fields */
470
rc = file_cache_recall_mydata(dobj->hfd, info, dobj, r);
471
if (rc != APR_SUCCESS) {
472
/* XXX log message */
476
/* Initialize the cache_handle callback functions */
477
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
478
"disk_cache: Recalled cached URL info header %s", dobj->name);
482
static int remove_entity(cache_handle_t *h)
484
/* Null out the cache object pointer so next time we start from scratch */
489
static int remove_url(cache_handle_t *h, apr_pool_t *p)
492
disk_cache_object_t *dobj;
494
/* Get disk cache object from cache handle */
495
dobj = (disk_cache_object_t *) h->cache_obj->vobj;
500
/* Delete headers file */
501
if (dobj->hdrsfile) {
502
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
503
"disk_cache: Deleting %s from cache.", dobj->hdrsfile);
505
rc = apr_file_remove(dobj->hdrsfile, p);
506
if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
507
/* Will only result in an output if httpd is started with -e debug.
508
* For reason see log_error_core for the case s == NULL.
510
ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
511
"disk_cache: Failed to delete headers file %s from cache.",
517
/* Delete data file */
518
if (dobj->datafile) {
519
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
520
"disk_cache: Deleting %s from cache.", dobj->datafile);
522
rc = apr_file_remove(dobj->datafile, p);
523
if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
524
/* Will only result in an output if httpd is started with -e debug.
525
* For reason see log_error_core for the case s == NULL.
527
ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
528
"disk_cache: Failed to delete data file %s from cache.",
534
/* now delete directories as far as possible up to our cache root */
536
const char *str_to_copy;
538
str_to_copy = dobj->hdrsfile ? dobj->hdrsfile : dobj->datafile;
540
char *dir, *slash, *q;
542
dir = apr_pstrdup(p, str_to_copy);
544
/* remove filename */
545
slash = strrchr(dir, '/');
549
* now walk our way back to the cache root, delete everything
550
* in the way as far as possible
552
* Note: due to the way we constructed the file names in
553
* header_file and data_file, we are guaranteed that the
554
* cache_root is suffixed by at least one '/' which will be
555
* turned into a terminating null by this loop. Therefore,
556
* we won't either delete or go above our cache root.
558
for (q = dir + dobj->root_len; *q ; ) {
559
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
560
"disk_cache: Deleting directory %s from cache",
563
rc = apr_dir_remove(dir, p);
564
if (rc != APR_SUCCESS && !APR_STATUS_IS_ENOENT(rc)) {
567
slash = strrchr(q, '/');
576
static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
579
char w[MAX_STRING_LEN];
584
rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
585
if (rv != APR_SUCCESS) {
586
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
587
"Premature end of vary array.");
592
if (p > 0 && w[p - 1] == '\n') {
593
if (p > 1 && w[p - 2] == CR) {
601
/* If we've finished reading the array, break out of the loop. */
606
*((const char **) apr_array_push(arr)) = apr_pstrdup(r->pool, w);
612
static apr_status_t store_array(apr_file_t *fd, apr_array_header_t* arr)
620
elts = (const char **) arr->elts;
622
for (i = 0; i < arr->nelts; i++) {
623
iov[0].iov_base = (char*) elts[i];
624
iov[0].iov_len = strlen(elts[i]);
625
iov[1].iov_base = CRLF;
626
iov[1].iov_len = sizeof(CRLF) - 1;
628
rv = apr_file_writev(fd, (const struct iovec *) &iov, 2,
630
if (rv != APR_SUCCESS) {
635
iov[0].iov_base = CRLF;
636
iov[0].iov_len = sizeof(CRLF) - 1;
638
return apr_file_writev(fd, (const struct iovec *) &iov, 1,
642
static apr_status_t read_table(cache_handle_t *handle, request_rec *r,
643
apr_table_t *table, apr_file_t *file)
645
char w[MAX_STRING_LEN];
652
/* ### What about APR_EOF? */
653
rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
654
if (rv != APR_SUCCESS) {
655
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
656
"Premature end of cache headers.");
660
/* Delete terminal (CR?)LF */
663
/* Indeed, the host's '\n':
664
'\012' for UNIX; '\015' for MacOS; '\025' for OS/390
665
-- whatever the script generates.
667
if (p > 0 && w[p - 1] == '\n') {
668
if (p > 1 && w[p - 2] == CR) {
676
/* If we've finished reading the headers, break out of the loop. */
681
#if APR_CHARSET_EBCDIC
682
/* Chances are that we received an ASCII header text instead of
683
* the expected EBCDIC header lines. Try to auto-detect:
685
if (!(l = strchr(w, ':'))) {
686
int maybeASCII = 0, maybeEBCDIC = 0;
687
unsigned char *cp, native;
688
apr_size_t inbytes_left, outbytes_left;
690
for (cp = w; *cp != '\0'; ++cp) {
691
native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
692
if (apr_isprint(*cp) && !apr_isprint(native))
694
if (!apr_isprint(*cp) && apr_isprint(native))
697
if (maybeASCII > maybeEBCDIC) {
698
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
699
"CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
701
inbytes_left = outbytes_left = cp - w;
702
apr_xlate_conv_buffer(ap_hdrs_from_ascii,
703
w, &inbytes_left, w, &outbytes_left);
706
#endif /*APR_CHARSET_EBCDIC*/
708
/* if we see a bogus header don't ignore it. Shout and scream */
709
if (!(l = strchr(w, ':'))) {
714
while (*l && apr_isspace(*l)) {
718
apr_table_add(table, w, l);
725
* Reads headers from a buffer and returns an array of headers.
726
* Returns NULL on file error
727
* This routine tries to deal with too long lines and continuation lines.
728
* @@@: XXX: FIXME: currently the headers are passed thru un-merged.
729
* Is that okay, or should they be collapsed where possible?
731
static apr_status_t recall_headers(cache_handle_t *h, request_rec *r)
733
disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
735
/* This case should not happen... */
737
/* XXX log message */
741
h->req_hdrs = apr_table_make(r->pool, 20);
742
h->resp_hdrs = apr_table_make(r->pool, 20);
744
/* Call routine to read the header lines/status line */
745
read_table(h, r, h->resp_hdrs, dobj->hfd);
746
read_table(h, r, h->req_hdrs, dobj->hfd);
748
apr_file_close(dobj->hfd);
750
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
751
"disk_cache: Recalled headers for URL %s", dobj->name);
755
static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb)
758
disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
760
e = apr_bucket_file_create(dobj->fd, 0, (apr_size_t) dobj->file_size, p,
762
APR_BRIGADE_INSERT_HEAD(bb, e);
763
e = apr_bucket_eos_create(bb->bucket_alloc);
764
APR_BRIGADE_INSERT_TAIL(bb, e);
769
static apr_status_t store_table(apr_file_t *fd, apr_table_t *table)
775
apr_table_entry_t *elts;
777
elts = (apr_table_entry_t *) apr_table_elts(table)->elts;
778
for (i = 0; i < apr_table_elts(table)->nelts; ++i) {
779
if (elts[i].key != NULL) {
780
iov[0].iov_base = elts[i].key;
781
iov[0].iov_len = strlen(elts[i].key);
782
iov[1].iov_base = ": ";
783
iov[1].iov_len = sizeof(": ") - 1;
784
iov[2].iov_base = elts[i].val;
785
iov[2].iov_len = strlen(elts[i].val);
786
iov[3].iov_base = CRLF;
787
iov[3].iov_len = sizeof(CRLF) - 1;
789
rv = apr_file_writev(fd, (const struct iovec *) &iov, 4,
791
if (rv != APR_SUCCESS) {
796
iov[0].iov_base = CRLF;
797
iov[0].iov_len = sizeof(CRLF) - 1;
798
rv = apr_file_writev(fd, (const struct iovec *) &iov, 1,
803
static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *info)
805
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
809
disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
811
disk_cache_info_t disk_info;
814
/* This is flaky... we need to manage the cache_info differently */
815
h->cache_obj->info = *info;
817
if (r->headers_out) {
820
tmp = apr_table_get(r->headers_out, "Vary");
823
apr_array_header_t* varray;
824
apr_uint32_t format = VARY_FORMAT_VERSION;
826
mkdir_structure(conf, dobj->hdrsfile, r->pool);
828
rv = apr_file_mktemp(&dobj->tfd, dobj->tempfile,
829
APR_CREATE | APR_WRITE | APR_BINARY | APR_EXCL,
832
if (rv != APR_SUCCESS) {
836
amt = sizeof(format);
837
apr_file_write(dobj->tfd, &format, &amt);
839
amt = sizeof(info->expire);
840
apr_file_write(dobj->tfd, &info->expire, &amt);
842
varray = apr_array_make(r->pool, 6, sizeof(char*));
843
tokens_to_array(r->pool, tmp, varray);
845
store_array(dobj->tfd, varray);
847
apr_file_close(dobj->tfd);
851
rv = safe_file_rename(conf, dobj->tempfile, dobj->hdrsfile,
853
if (rv != APR_SUCCESS) {
854
ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server,
855
"disk_cache: rename tempfile to varyfile failed: %s -> %s",
856
dobj->tempfile, dobj->hdrsfile);
860
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
861
tmp = regen_key(r->pool, r->headers_in, varray, dobj->name);
862
dobj->prefix = dobj->hdrsfile;
863
dobj->hashfile = NULL;
864
dobj->datafile = data_file(r->pool, conf, dobj, tmp);
865
dobj->hdrsfile = header_file(r->pool, conf, dobj, tmp);
870
rv = apr_file_mktemp(&dobj->hfd, dobj->tempfile,
871
APR_CREATE | APR_WRITE | APR_BINARY |
872
APR_BUFFERED | APR_EXCL, r->pool);
874
if (rv != APR_SUCCESS) {
878
dobj->name = h->cache_obj->key;
880
disk_info.format = DISK_FORMAT_VERSION;
881
disk_info.date = info->date;
882
disk_info.expire = info->expire;
883
disk_info.entity_version = dobj->disk_info.entity_version++;
884
disk_info.request_time = info->request_time;
885
disk_info.response_time = info->response_time;
886
disk_info.status = info->status;
888
disk_info.name_len = strlen(dobj->name);
890
iov[0].iov_base = (void*)&disk_info;
891
iov[0].iov_len = sizeof(disk_cache_info_t);
892
iov[1].iov_base = (void*)dobj->name;
893
iov[1].iov_len = disk_info.name_len;
895
rv = apr_file_writev(dobj->hfd, (const struct iovec *) &iov, 2, &amt);
896
if (rv != APR_SUCCESS) {
900
if (r->headers_out) {
901
apr_table_t *headers_out;
903
headers_out = ap_cache_cacheable_hdrs_out(r->pool, r->headers_out,
906
if (!apr_table_get(headers_out, "Content-Type")
907
&& r->content_type) {
908
apr_table_setn(headers_out, "Content-Type",
909
ap_make_content_type(r, r->content_type));
912
headers_out = apr_table_overlay(r->pool, headers_out,
914
rv = store_table(dobj->hfd, headers_out);
915
if (rv != APR_SUCCESS) {
920
/* Parse the vary header and dump those fields from the headers_in. */
921
/* FIXME: Make call to the same thing cache_select calls to crack Vary. */
923
apr_table_t *headers_in;
925
headers_in = ap_cache_cacheable_hdrs_out(r->pool, r->headers_in,
927
rv = store_table(dobj->hfd, headers_in);
928
if (rv != APR_SUCCESS) {
933
apr_file_close(dobj->hfd); /* flush and close */
935
/* Remove old file with the same name. If remove fails, then
936
* perhaps we need to create the directory tree where we are
937
* about to write the new headers file.
939
rv = apr_file_remove(dobj->hdrsfile, r->pool);
940
if (rv != APR_SUCCESS) {
941
mkdir_structure(conf, dobj->hdrsfile, r->pool);
944
rv = safe_file_rename(conf, dobj->tempfile, dobj->hdrsfile, r->pool);
945
if (rv != APR_SUCCESS) {
946
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
947
"disk_cache: rename tempfile to hdrsfile failed: %s -> %s",
948
dobj->tempfile, dobj->hdrsfile);
952
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
954
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
955
"disk_cache: Stored headers for URL %s", dobj->name);
959
static apr_status_t store_body(cache_handle_t *h, request_rec *r,
960
apr_bucket_brigade *bb)
964
disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
965
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
968
/* We write to a temp file and then atomically rename the file over
969
* in file_cache_el_final().
972
rv = apr_file_mktemp(&dobj->tfd, dobj->tempfile,
973
APR_CREATE | APR_WRITE | APR_BINARY |
974
APR_BUFFERED | APR_EXCL, r->pool);
975
if (rv != APR_SUCCESS) {
981
for (e = APR_BRIGADE_FIRST(bb);
982
e != APR_BRIGADE_SENTINEL(bb);
983
e = APR_BUCKET_NEXT(e))
986
apr_size_t length, written;
987
rv = apr_bucket_read(e, &str, &length, APR_BLOCK_READ);
988
if (rv != APR_SUCCESS) {
989
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
990
"cache_disk: Error when reading bucket for URL %s",
992
/* Remove the intermediate cache file and return non-APR_SUCCESS */
993
file_cache_errorcleanup(dobj, r);
996
rv = apr_file_write_full(dobj->tfd, str, length, &written);
997
if (rv != APR_SUCCESS) {
998
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
999
"cache_disk: Error when writing cache file for URL %s",
1001
/* Remove the intermediate cache file and return non-APR_SUCCESS */
1002
file_cache_errorcleanup(dobj, r);
1005
dobj->file_size += written;
1006
if (dobj->file_size > conf->maxfs) {
1007
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1008
"cache_disk: URL %s failed the size check "
1009
"(%" APR_OFF_T_FMT ">%" APR_SIZE_T_FMT ")",
1010
h->cache_obj->key, dobj->file_size, conf->maxfs);
1011
/* Remove the intermediate cache file and return non-APR_SUCCESS */
1012
file_cache_errorcleanup(dobj, r);
1013
return APR_EGENERAL;
1017
/* Was this the final bucket? If yes, close the temp file and perform
1020
if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
1021
if (r->connection->aborted || r->no_cache) {
1022
ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
1023
"disk_cache: Discarding body for URL %s "
1024
"because connection has been aborted.",
1026
/* Remove the intermediate cache file and return non-APR_SUCCESS */
1027
file_cache_errorcleanup(dobj, r);
1028
return APR_EGENERAL;
1030
if (dobj->file_size < conf->minfs) {
1031
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1032
"cache_disk: URL %s failed the size check "
1033
"(%" APR_OFF_T_FMT "<%" APR_SIZE_T_FMT ")",
1034
h->cache_obj->key, dobj->file_size, conf->minfs);
1035
/* Remove the intermediate cache file and return non-APR_SUCCESS */
1036
file_cache_errorcleanup(dobj, r);
1037
return APR_EGENERAL;
1040
/* All checks were fine. Move tempfile to final destination */
1041
/* Link to the perm file, and close the descriptor */
1042
file_cache_el_final(dobj, r);
1043
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1044
"disk_cache: Body for URL %s cached.", dobj->name);
1050
static void *create_config(apr_pool_t *p, server_rec *s)
1052
disk_cache_conf *conf = apr_pcalloc(p, sizeof(disk_cache_conf));
1054
/* XXX: Set default values */
1055
conf->dirlevels = DEFAULT_DIRLEVELS;
1056
conf->dirlength = DEFAULT_DIRLENGTH;
1057
conf->maxfs = DEFAULT_MAX_FILE_SIZE;
1058
conf->minfs = DEFAULT_MIN_FILE_SIZE;
1060
conf->cache_root = NULL;
1061
conf->cache_root_len = 0;
1067
* mod_disk_cache configuration directives handlers.
1070
*set_cache_root(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1072
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1073
&disk_cache_module);
1074
conf->cache_root = arg;
1075
conf->cache_root_len = strlen(arg);
1076
/* TODO: canonicalize cache_root and strip off any trailing slashes */
1082
* Consider eliminating the next two directives in favor of
1083
* Ian's prime number hash...
1084
* key = hash_fn( r->uri)
1085
* filename = "/key % prime1 /key %prime2/key %prime3"
1088
*set_cache_dirlevels(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1090
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1091
&disk_cache_module);
1092
int val = atoi(arg);
1094
return "CacheDirLevels value must be an integer greater than 0";
1095
if (val * conf->dirlength > CACHEFILE_LEN)
1096
return "CacheDirLevels*CacheDirLength value must not be higher than 20";
1097
conf->dirlevels = val;
1101
*set_cache_dirlength(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1103
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1104
&disk_cache_module);
1105
int val = atoi(arg);
1107
return "CacheDirLength value must be an integer greater than 0";
1108
if (val * conf->dirlevels > CACHEFILE_LEN)
1109
return "CacheDirLevels*CacheDirLength value must not be higher than 20";
1111
conf->dirlength = val;
1116
*set_cache_minfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1118
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1119
&disk_cache_module);
1120
conf->minfs = atoi(arg);
1124
*set_cache_maxfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1126
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1127
&disk_cache_module);
1128
conf->maxfs = atoi(arg);
1132
static const command_rec disk_cache_cmds[] =
1134
AP_INIT_TAKE1("CacheRoot", set_cache_root, NULL, RSRC_CONF,
1135
"The directory to store cache files"),
1136
AP_INIT_TAKE1("CacheDirLevels", set_cache_dirlevels, NULL, RSRC_CONF,
1137
"The number of levels of subdirectories in the cache"),
1138
AP_INIT_TAKE1("CacheDirLength", set_cache_dirlength, NULL, RSRC_CONF,
1139
"The number of characters in subdirectory names"),
1140
AP_INIT_TAKE1("CacheMinFileSize", set_cache_minfs, NULL, RSRC_CONF,
1141
"The minimum file size to cache a document"),
1142
AP_INIT_TAKE1("CacheMaxFileSize", set_cache_maxfs, NULL, RSRC_CONF,
1143
"The maximum file size to cache a document"),
1147
static const cache_provider cache_disk_provider =
1159
static void disk_cache_register_hook(apr_pool_t *p)
1161
/* cache initializer */
1162
ap_register_provider(p, CACHE_PROVIDER_GROUP, "disk", "0",
1163
&cache_disk_provider);
1166
module AP_MODULE_DECLARE_DATA disk_cache_module = {
1167
STANDARD20_MODULE_STUFF,
1168
NULL, /* create per-directory config structure */
1169
NULL, /* merge per-directory config structures */
1170
create_config, /* create per-server config structure */
1171
NULL, /* merge per-server config structures */
1172
disk_cache_cmds, /* command apr_table_t */
1173
disk_cache_register_hook /* register hooks */