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.
18 #include "apr_file_io.h"
19 #include "apr_strings.h"
20 #include "mod_cache.h"
21 #include "mod_cache_disk.h"
22 #include "http_config.h"
24 #include "http_core.h"
25 #include "ap_provider.h"
26 #include "util_filter.h"
27 #include "util_script.h"
28 #include "util_charset.h"
31 * mod_cache_disk: Disk Based HTTP 1.1 Cache.
33 * Flow to Find the .data file:
34 * Incoming client requests URI /foo/bar/baz
35 * Generate <hash> off of /foo/bar/baz
37 * Read in <hash>.header file (may contain Format #1 or Format #2)
38 * If format #1 (Contains a list of Vary Headers):
39 * Use each header name (from .header) with our request values (headers_in) to
40 * regenerate <hash> using HeaderName+HeaderValue+.../foo/bar/baz
41 * re-read in <hash>.header (must be format #2)
45 * apr_uint32_t format;
47 * apr_array_t vary_headers (delimited by CRLF)
50 * disk_cache_info_t (first sizeof(apr_uint32_t) bytes is the format)
51 * entity name (dobj->name) [length is in disk_cache_info_t->name_len]
52 * r->headers_out (delimited by CRLF)
54 * r->headers_in (delimited by CRLF)
58 module AP_MODULE_DECLARE_DATA cache_disk_module;
60 /* Forward declarations */
61 static int remove_entity(cache_handle_t *h);
62 static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *i);
63 static apr_status_t store_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *in,
64 apr_bucket_brigade *out);
65 static apr_status_t recall_headers(cache_handle_t *h, request_rec *r);
66 static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
67 static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
71 * Local static functions
74 static char *header_file(apr_pool_t *p, disk_cache_conf *conf,
75 disk_cache_object_t *dobj, const char *name)
77 if (!dobj->hashfile) {
78 dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
79 conf->dirlength, name);
83 return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX "/",
84 dobj->hashfile, CACHE_HEADER_SUFFIX, NULL);
87 return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
88 CACHE_HEADER_SUFFIX, NULL);
92 static char *data_file(apr_pool_t *p, disk_cache_conf *conf,
93 disk_cache_object_t *dobj, const char *name)
95 if (!dobj->hashfile) {
96 dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
97 conf->dirlength, name);
101 return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX "/",
102 dobj->hashfile, CACHE_DATA_SUFFIX, NULL);
105 return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
106 CACHE_DATA_SUFFIX, NULL);
110 static apr_status_t mkdir_structure(disk_cache_conf *conf, const char *file, apr_pool_t *pool)
115 for (p = (char*)file + conf->cache_root_len + 1;;) {
121 rv = apr_dir_make(file,
122 APR_UREAD|APR_UWRITE|APR_UEXECUTE, pool);
123 if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) {
132 /* htcacheclean may remove directories underneath us.
133 * So, we'll try renaming three times at a cost of 0.002 seconds.
135 static apr_status_t safe_file_rename(disk_cache_conf *conf,
136 const char *src, const char *dest,
141 rv = apr_file_rename(src, dest, pool);
143 if (rv != APR_SUCCESS) {
146 for (i = 0; i < 2 && rv != APR_SUCCESS; i++) {
147 /* 1000 micro-seconds aka 0.001 seconds. */
150 rv = mkdir_structure(conf, dest, pool);
151 if (rv != APR_SUCCESS)
154 rv = apr_file_rename(src, dest, pool);
161 static apr_status_t file_cache_el_final(disk_cache_conf *conf, disk_cache_file_t *file,
164 apr_status_t rv = APR_SUCCESS;
166 /* This assumes that the tempfiles are on the same file system
167 * as the cache_root. If not, then we need a file copy/move
168 * rather than a rename.
171 /* move the file over */
174 rv = safe_file_rename(conf, file->tempfile, file->file, file->pool);
175 if (rv != APR_SUCCESS) {
176 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00699)
177 "rename tempfile to file failed:"
178 " %s -> %s", file->tempfile, file->file);
179 apr_file_remove(file->tempfile, file->pool);
188 static apr_status_t file_cache_temp_cleanup(void *dummy)
190 disk_cache_file_t *file = (disk_cache_file_t *)dummy;
192 /* clean up the temporary file */
194 apr_file_remove(file->tempfile, file->pool);
197 file->tempfile = NULL;
203 static apr_status_t file_cache_create(disk_cache_conf *conf, disk_cache_file_t *file,
207 file->tempfile = apr_pstrcat(pool, conf->cache_root, AP_TEMPFILE, NULL);
209 apr_pool_cleanup_register(pool, file, file_cache_temp_cleanup, apr_pool_cleanup_null);
214 /* These two functions get and put state information into the data
215 * file for an ap_cache_el, this state information will be read
216 * and written transparent to clients of this module
218 static int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,
219 disk_cache_object_t *dobj, request_rec *r)
225 /* read the data from the cache file */
226 len = sizeof(disk_cache_info_t);
227 rv = apr_file_read_full(fd, &dobj->disk_info, len, &len);
228 if (rv != APR_SUCCESS) {
232 /* Store it away so we can get it later. */
233 info->status = dobj->disk_info.status;
234 info->date = dobj->disk_info.date;
235 info->expire = dobj->disk_info.expire;
236 info->request_time = dobj->disk_info.request_time;
237 info->response_time = dobj->disk_info.response_time;
239 memcpy(&info->control, &dobj->disk_info.control, sizeof(cache_control_t));
241 /* Note that we could optimize this by conditionally doing the palloc
242 * depending upon the size. */
243 urlbuff = apr_palloc(r->pool, dobj->disk_info.name_len + 1);
244 len = dobj->disk_info.name_len;
245 rv = apr_file_read_full(fd, urlbuff, len, &len);
246 if (rv != APR_SUCCESS) {
249 urlbuff[dobj->disk_info.name_len] = '\0';
251 /* check that we have the same URL */
252 /* Would strncmp be correct? */
253 if (strcmp(urlbuff, dobj->name) != 0) {
260 static const char* regen_key(apr_pool_t *p, apr_table_t *headers,
261 apr_array_header_t *varray, const char *oldkey)
269 nvec = (varray->nelts * 2) + 1;
270 iov = apr_palloc(p, sizeof(struct iovec) * nvec);
271 elts = (const char **) varray->elts;
274 * - Handle multiple-value headers better. (sort them?)
275 * - Handle Case in-sensitive Values better.
276 * This isn't the end of the world, since it just lowers the cache
277 * hit rate, but it would be nice to fix.
279 * The majority are case insenstive if they are values (encoding etc).
280 * Most of rfc2616 is case insensitive on header contents.
282 * So the better solution may be to identify headers which should be
283 * treated case-sensitive?
284 * HTTP URI's (3.2.3) [host and scheme are insensitive]
285 * HTTP method (5.1.1)
286 * HTTP-date values (3.3.1)
287 * 3.7 Media Types [exerpt]
288 * The type, subtype, and parameter attribute names are case-
289 * insensitive. Parameter values might or might not be case-sensitive,
290 * depending on the semantics of the parameter name.
291 * 4.20 Except [exerpt]
292 * Comparison of expectation values is case-insensitive for unquoted
293 * tokens (including the 100-continue token), and is case-sensitive for
294 * quoted-string expectation-extensions.
297 for (i=0, k=0; i < varray->nelts; i++) {
298 header = apr_table_get(headers, elts[i]);
302 iov[k].iov_base = (char*) elts[i];
303 iov[k].iov_len = strlen(elts[i]);
305 iov[k].iov_base = (char*) header;
306 iov[k].iov_len = strlen(header);
309 iov[k].iov_base = (char*) oldkey;
310 iov[k].iov_len = strlen(oldkey);
313 return apr_pstrcatv(p, iov, k, NULL);
316 static int array_alphasort(const void *fn1, const void *fn2)
318 return strcmp(*(char**)fn1, *(char**)fn2);
321 static void tokens_to_array(apr_pool_t *p, const char *data,
322 apr_array_header_t *arr)
326 while ((token = ap_get_list_item(p, &data)) != NULL) {
327 *((const char **) apr_array_push(arr)) = token;
330 /* Sort it so that "Vary: A, B" and "Vary: B, A" are stored the same. */
331 qsort((void *) arr->elts, arr->nelts,
332 sizeof(char *), array_alphasort);
336 * Hook and mod_cache callback functions
338 static int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len,
339 apr_bucket_brigade *bb)
341 disk_cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &cache_disk_module);
342 disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
345 disk_cache_object_t *dobj;
348 if (conf->cache_root == NULL) {
352 /* we don't support caching of range requests (yet) */
353 if (r->status == HTTP_PARTIAL_CONTENT) {
354 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00700)
355 "URL %s partial content response not cached",
360 /* Note, len is -1 if unknown so don't trust it too hard */
361 if (len > dconf->maxfs) {
362 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00701)
363 "URL %s failed the size check "
364 "(%" APR_OFF_T_FMT " > %" APR_OFF_T_FMT ")",
365 key, len, dconf->maxfs);
368 if (len >= 0 && len < dconf->minfs) {
369 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00702)
370 "URL %s failed the size check "
371 "(%" APR_OFF_T_FMT " < %" APR_OFF_T_FMT ")",
372 key, len, dconf->minfs);
376 /* Allocate and initialize cache_object_t and disk_cache_object_t */
377 h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj));
378 obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj));
380 obj->key = apr_pstrdup(r->pool, key);
382 dobj->name = obj->key;
384 /* Save the cache root */
385 dobj->root = apr_pstrmemdup(r->pool, conf->cache_root, conf->cache_root_len);
386 dobj->root_len = conf->cache_root_len;
388 apr_pool_create(&pool, r->pool);
389 apr_pool_tag(pool, "mod_cache (create_entity)");
391 file_cache_create(conf, &dobj->hdrs, pool);
392 file_cache_create(conf, &dobj->vary, pool);
393 file_cache_create(conf, &dobj->data, pool);
395 dobj->data.file = data_file(r->pool, conf, dobj, key);
396 dobj->hdrs.file = header_file(r->pool, conf, dobj, key);
397 dobj->vary.file = header_file(r->pool, conf, dobj, key);
399 dobj->disk_info.header_only = r->header_only;
404 static int open_entity(cache_handle_t *h, request_rec *r, const char *key)
410 static int error_logged = 0;
411 disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
413 #ifdef APR_SENDFILE_ENABLED
414 core_dir_config *coreconf = ap_get_core_module_config(r->per_dir_config);
419 disk_cache_object_t *dobj;
425 /* Look up entity keyed to 'url' */
426 if (conf->cache_root == NULL) {
429 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00703)
430 "Cannot cache files to disk without a CacheRoot specified.");
435 /* Create and init the cache object */
436 obj = apr_pcalloc(r->pool, sizeof(cache_object_t));
437 dobj = apr_pcalloc(r->pool, sizeof(disk_cache_object_t));
441 /* Open the headers file */
444 /* Save the cache root */
445 dobj->root = apr_pstrmemdup(r->pool, conf->cache_root, conf->cache_root_len);
446 dobj->root_len = conf->cache_root_len;
448 dobj->vary.file = header_file(r->pool, conf, dobj, key);
449 flags = APR_READ|APR_BINARY|APR_BUFFERED;
450 rc = apr_file_open(&dobj->vary.fd, dobj->vary.file, flags, 0, r->pool);
451 if (rc != APR_SUCCESS) {
455 /* read the format from the cache file */
456 len = sizeof(format);
457 apr_file_read_full(dobj->vary.fd, &format, len, &len);
459 if (format == VARY_FORMAT_VERSION) {
460 apr_array_header_t* varray;
463 len = sizeof(expire);
464 apr_file_read_full(dobj->vary.fd, &expire, len, &len);
466 varray = apr_array_make(r->pool, 5, sizeof(char*));
467 rc = read_array(r, varray, dobj->vary.fd);
468 if (rc != APR_SUCCESS) {
469 ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r, APLOGNO(00704)
470 "Cannot parse vary header file: %s",
472 apr_file_close(dobj->vary.fd);
475 apr_file_close(dobj->vary.fd);
477 nkey = regen_key(r->pool, r->headers_in, varray, key);
479 dobj->hashfile = NULL;
480 dobj->prefix = dobj->vary.file;
481 dobj->hdrs.file = header_file(r->pool, conf, dobj, nkey);
483 flags = APR_READ|APR_BINARY|APR_BUFFERED;
484 rc = apr_file_open(&dobj->hdrs.fd, dobj->hdrs.file, flags, 0, r->pool);
485 if (rc != APR_SUCCESS) {
489 else if (format != DISK_FORMAT_VERSION) {
490 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00705)
491 "File '%s' has a version mismatch. File had version: %d.",
492 dobj->vary.file, format);
493 apr_file_close(dobj->vary.fd);
497 apr_off_t offset = 0;
499 /* oops, not vary as it turns out */
500 dobj->hdrs.fd = dobj->vary.fd;
501 dobj->vary.fd = NULL;
502 dobj->hdrs.file = dobj->vary.file;
504 /* This wasn't a Vary Format file, so we must seek to the
505 * start of the file again, so that later reads work.
507 apr_file_seek(dobj->hdrs.fd, APR_SET, &offset);
515 apr_pool_create(&pool, r->pool);
516 apr_pool_tag(pool, "mod_cache (open_entity)");
518 file_cache_create(conf, &dobj->hdrs, pool);
519 file_cache_create(conf, &dobj->vary, pool);
520 file_cache_create(conf, &dobj->data, pool);
522 dobj->data.file = data_file(r->pool, conf, dobj, nkey);
524 /* Read the bytes to setup the cache_info fields */
525 rc = file_cache_recall_mydata(dobj->hdrs.fd, info, dobj, r);
526 if (rc != APR_SUCCESS) {
527 ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r, APLOGNO(00706)
528 "Cannot read header file %s", dobj->hdrs.file);
529 apr_file_close(dobj->hdrs.fd);
534 /* Is this a cached HEAD request? */
535 if (dobj->disk_info.header_only && !r->header_only) {
536 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00707)
537 "HEAD request cached, non-HEAD requested, ignoring: %s",
539 apr_file_close(dobj->hdrs.fd);
543 /* Open the data file */
544 if (dobj->disk_info.has_body) {
545 flags = APR_READ | APR_BINARY;
546 #ifdef APR_SENDFILE_ENABLED
547 /* When we are in the quick handler we don't have the per-directory
548 * configuration, so this check only takes the global setting of
549 * the EnableSendFile directive into account.
551 flags |= AP_SENDFILE_ENABLED(coreconf->enable_sendfile);
553 rc = apr_file_open(&dobj->data.fd, dobj->data.file, flags, 0, r->pool);
554 if (rc != APR_SUCCESS) {
555 ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r, APLOGNO(00708)
556 "Cannot open data file %s", dobj->data.file);
557 apr_file_close(dobj->hdrs.fd);
561 rc = apr_file_info_get(&finfo, APR_FINFO_SIZE | APR_FINFO_IDENT,
563 if (rc == APR_SUCCESS) {
564 dobj->file_size = finfo.size;
567 /* Atomic check - does the body file belong to the header file? */
568 if (dobj->disk_info.inode == finfo.inode &&
569 dobj->disk_info.device == finfo.device) {
571 /* Initialize the cache_handle callback functions */
572 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00709)
573 "Recalled cached URL info header %s", dobj->name);
575 /* make the configuration stick */
585 /* make the configuration stick */
592 /* Oh dear, no luck matching header to the body */
593 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00710)
594 "Cached URL info header '%s' didn't match body, ignoring this entry",
597 apr_file_close(dobj->hdrs.fd);
601 static void close_disk_cache_fd(disk_cache_file_t *file)
603 if (file->fd != NULL) {
604 apr_file_close(file->fd);
607 if (file->tempfd != NULL) {
608 apr_file_close(file->tempfd);
613 static int remove_entity(cache_handle_t *h)
615 disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
617 close_disk_cache_fd(&(dobj->hdrs));
618 close_disk_cache_fd(&(dobj->vary));
619 close_disk_cache_fd(&(dobj->data));
621 /* Null out the cache object pointer so next time we start from scratch */
626 static int remove_url(cache_handle_t *h, request_rec *r)
629 disk_cache_object_t *dobj;
631 /* Get disk cache object from cache handle */
632 dobj = (disk_cache_object_t *) h->cache_obj->vobj;
637 /* Delete headers file */
638 if (dobj->hdrs.file) {
639 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00711)
640 "Deleting %s from cache.", dobj->hdrs.file);
642 rc = apr_file_remove(dobj->hdrs.file, r->pool);
643 if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
644 /* Will only result in an output if httpd is started with -e debug.
645 * For reason see log_error_core for the case s == NULL.
647 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rc, r, APLOGNO(00712)
648 "Failed to delete headers file %s from cache.",
654 /* Delete data file */
655 if (dobj->data.file) {
656 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00713)
657 "Deleting %s from cache.", dobj->data.file);
659 rc = apr_file_remove(dobj->data.file, r->pool);
660 if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
661 /* Will only result in an output if httpd is started with -e debug.
662 * For reason see log_error_core for the case s == NULL.
664 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rc, r, APLOGNO(00714)
665 "Failed to delete data file %s from cache.",
671 /* now delete directories as far as possible up to our cache root */
673 const char *str_to_copy;
675 str_to_copy = dobj->hdrs.file ? dobj->hdrs.file : dobj->data.file;
677 char *dir, *slash, *q;
679 dir = apr_pstrdup(r->pool, str_to_copy);
681 /* remove filename */
682 slash = strrchr(dir, '/');
686 * now walk our way back to the cache root, delete everything
687 * in the way as far as possible
689 * Note: due to the way we constructed the file names in
690 * header_file and data_file, we are guaranteed that the
691 * cache_root is suffixed by at least one '/' which will be
692 * turned into a terminating null by this loop. Therefore,
693 * we won't either delete or go above our cache root.
695 for (q = dir + dobj->root_len; *q ; ) {
696 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00715)
697 "Deleting directory %s from cache", dir);
699 rc = apr_dir_remove(dir, r->pool);
700 if (rc != APR_SUCCESS && !APR_STATUS_IS_ENOENT(rc)) {
703 slash = strrchr(q, '/');
712 static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
715 char w[MAX_STRING_LEN];
720 rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
721 if (rv != APR_SUCCESS) {
722 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00716)
723 "Premature end of vary array.");
728 if (p > 0 && w[p - 1] == '\n') {
729 if (p > 1 && w[p - 2] == CR) {
737 /* If we've finished reading the array, break out of the loop. */
742 *((const char **) apr_array_push(arr)) = apr_pstrdup(r->pool, w);
748 static apr_status_t store_array(apr_file_t *fd, apr_array_header_t* arr)
756 elts = (const char **) arr->elts;
758 for (i = 0; i < arr->nelts; i++) {
759 iov[0].iov_base = (char*) elts[i];
760 iov[0].iov_len = strlen(elts[i]);
761 iov[1].iov_base = CRLF;
762 iov[1].iov_len = sizeof(CRLF) - 1;
764 rv = apr_file_writev_full(fd, (const struct iovec *) &iov, 2, &amt);
765 if (rv != APR_SUCCESS) {
770 iov[0].iov_base = CRLF;
771 iov[0].iov_len = sizeof(CRLF) - 1;
773 return apr_file_writev_full(fd, (const struct iovec *) &iov, 1, &amt);
776 static apr_status_t read_table(cache_handle_t *handle, request_rec *r,
777 apr_table_t *table, apr_file_t *file)
779 char w[MAX_STRING_LEN];
786 /* ### What about APR_EOF? */
787 rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
788 if (rv != APR_SUCCESS) {
789 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00717)
790 "Premature end of cache headers.");
794 /* Delete terminal (CR?)LF */
797 /* Indeed, the host's '\n':
798 '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
799 -- whatever the script generates.
801 if (p > 0 && w[p - 1] == '\n') {
802 if (p > 1 && w[p - 2] == CR) {
810 /* If we've finished reading the headers, break out of the loop. */
815 #if APR_CHARSET_EBCDIC
816 /* Chances are that we received an ASCII header text instead of
817 * the expected EBCDIC header lines. Try to auto-detect:
819 if (!(l = strchr(w, ':'))) {
820 int maybeASCII = 0, maybeEBCDIC = 0;
821 unsigned char *cp, native;
822 apr_size_t inbytes_left, outbytes_left;
824 for (cp = w; *cp != '\0'; ++cp) {
825 native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
826 if (apr_isprint(*cp) && !apr_isprint(native))
828 if (!apr_isprint(*cp) && apr_isprint(native))
831 if (maybeASCII > maybeEBCDIC) {
832 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00718)
833 "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
835 inbytes_left = outbytes_left = cp - w;
836 apr_xlate_conv_buffer(ap_hdrs_from_ascii,
837 w, &inbytes_left, w, &outbytes_left);
840 #endif /*APR_CHARSET_EBCDIC*/
842 /* if we see a bogus header don't ignore it. Shout and scream */
843 if (!(l = strchr(w, ':'))) {
848 while (apr_isspace(*l)) {
852 apr_table_add(table, w, l);
859 * Reads headers from a buffer and returns an array of headers.
860 * Returns NULL on file error
861 * This routine tries to deal with too long lines and continuation lines.
862 * @@@: XXX: FIXME: currently the headers are passed thru un-merged.
863 * Is that okay, or should they be collapsed where possible?
865 static apr_status_t recall_headers(cache_handle_t *h, request_rec *r)
867 disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
870 /* This case should not happen... */
871 if (!dobj->hdrs.fd) {
872 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00719)
873 "recalling headers; but no header fd for %s", dobj->name);
877 h->req_hdrs = apr_table_make(r->pool, 20);
878 h->resp_hdrs = apr_table_make(r->pool, 20);
880 /* Call routine to read the header lines/status line */
881 rv = read_table(h, r, h->resp_hdrs, dobj->hdrs.fd);
882 if (rv != APR_SUCCESS) {
883 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02987)
884 "Error reading response headers from %s for %s",
885 dobj->hdrs.file, dobj->name);
887 rv = read_table(h, r, h->req_hdrs, dobj->hdrs.fd);
888 if (rv != APR_SUCCESS) {
889 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02988)
890 "Error reading request headers from %s for %s",
891 dobj->hdrs.file, dobj->name);
894 apr_file_close(dobj->hdrs.fd);
896 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00720)
897 "Recalled headers for URL %s", dobj->name);
901 static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb)
903 disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
906 apr_brigade_insert_file(bb, dobj->data.fd, 0, dobj->file_size, p);
912 static apr_status_t store_table(apr_file_t *fd, apr_table_t *table)
918 apr_table_entry_t *elts;
920 elts = (apr_table_entry_t *) apr_table_elts(table)->elts;
921 for (i = 0; i < apr_table_elts(table)->nelts; ++i) {
922 if (elts[i].key != NULL) {
923 iov[0].iov_base = elts[i].key;
924 iov[0].iov_len = strlen(elts[i].key);
925 iov[1].iov_base = ": ";
926 iov[1].iov_len = sizeof(": ") - 1;
927 iov[2].iov_base = elts[i].val;
928 iov[2].iov_len = strlen(elts[i].val);
929 iov[3].iov_base = CRLF;
930 iov[3].iov_len = sizeof(CRLF) - 1;
932 rv = apr_file_writev_full(fd, (const struct iovec *) &iov, 4, &amt);
933 if (rv != APR_SUCCESS) {
938 iov[0].iov_base = CRLF;
939 iov[0].iov_len = sizeof(CRLF) - 1;
940 rv = apr_file_writev_full(fd, (const struct iovec *) &iov, 1, &amt);
944 static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *info)
946 disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
948 memcpy(&h->cache_obj->info, info, sizeof(cache_info));
950 if (r->headers_out) {
951 dobj->headers_out = ap_cache_cacheable_headers_out(r);
955 dobj->headers_in = ap_cache_cacheable_headers_in(r);
958 if (r->header_only && r->status != HTTP_NOT_MODIFIED) {
959 dobj->disk_info.header_only = 1;
965 static apr_status_t write_headers(cache_handle_t *h, request_rec *r)
967 disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
971 disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
973 disk_cache_info_t disk_info;
976 memset(&disk_info, 0, sizeof(disk_cache_info_t));
978 if (dobj->headers_out) {
981 tmp = apr_table_get(dobj->headers_out, "Vary");
984 apr_array_header_t* varray;
985 apr_uint32_t format = VARY_FORMAT_VERSION;
987 /* If we were initially opened as a vary format, rollback
988 * that internal state for the moment so we can recreate the
989 * vary format hints in the appropriate directory.
992 dobj->hdrs.file = dobj->prefix;
996 rv = mkdir_structure(conf, dobj->hdrs.file, r->pool);
997 if (rv == APR_SUCCESS) {
998 rv = apr_file_mktemp(&dobj->vary.tempfd, dobj->vary.tempfile,
999 APR_CREATE | APR_WRITE | APR_BINARY | APR_EXCL,
1003 if (rv != APR_SUCCESS) {
1004 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00721)
1005 "could not create vary file %s",
1006 dobj->vary.tempfile);
1010 amt = sizeof(format);
1011 rv = apr_file_write_full(dobj->vary.tempfd, &format, amt, NULL);
1012 if (rv != APR_SUCCESS) {
1013 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00722)
1014 "could not write to vary file %s",
1015 dobj->vary.tempfile);
1016 apr_file_close(dobj->vary.tempfd);
1017 apr_pool_destroy(dobj->vary.pool);
1021 amt = sizeof(h->cache_obj->info.expire);
1022 rv = apr_file_write_full(dobj->vary.tempfd,
1023 &h->cache_obj->info.expire, amt, NULL);
1024 if (rv != APR_SUCCESS) {
1025 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00723)
1026 "could not write to vary file %s",
1027 dobj->vary.tempfile);
1028 apr_file_close(dobj->vary.tempfd);
1029 apr_pool_destroy(dobj->vary.pool);
1033 varray = apr_array_make(r->pool, 6, sizeof(char*));
1034 tokens_to_array(r->pool, tmp, varray);
1036 store_array(dobj->vary.tempfd, varray);
1038 rv = apr_file_close(dobj->vary.tempfd);
1039 if (rv != APR_SUCCESS) {
1040 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00724)
1041 "could not close vary file %s",
1042 dobj->vary.tempfile);
1043 apr_pool_destroy(dobj->vary.pool);
1047 tmp = regen_key(r->pool, dobj->headers_in, varray, dobj->name);
1048 dobj->prefix = dobj->hdrs.file;
1049 dobj->hashfile = NULL;
1050 dobj->data.file = data_file(r->pool, conf, dobj, tmp);
1051 dobj->hdrs.file = header_file(r->pool, conf, dobj, tmp);
1056 rv = apr_file_mktemp(&dobj->hdrs.tempfd, dobj->hdrs.tempfile,
1057 APR_CREATE | APR_WRITE | APR_BINARY |
1058 APR_BUFFERED | APR_EXCL, dobj->hdrs.pool);
1060 if (rv != APR_SUCCESS) {
1061 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00725)
1062 "could not create header file %s",
1063 dobj->hdrs.tempfile);
1067 disk_info.format = DISK_FORMAT_VERSION;
1068 disk_info.date = h->cache_obj->info.date;
1069 disk_info.expire = h->cache_obj->info.expire;
1070 disk_info.entity_version = dobj->disk_info.entity_version++;
1071 disk_info.request_time = h->cache_obj->info.request_time;
1072 disk_info.response_time = h->cache_obj->info.response_time;
1073 disk_info.status = h->cache_obj->info.status;
1074 disk_info.inode = dobj->disk_info.inode;
1075 disk_info.device = dobj->disk_info.device;
1076 disk_info.has_body = dobj->disk_info.has_body;
1077 disk_info.header_only = dobj->disk_info.header_only;
1079 disk_info.name_len = strlen(dobj->name);
1081 memcpy(&disk_info.control, &h->cache_obj->info.control, sizeof(cache_control_t));
1083 iov[0].iov_base = (void*)&disk_info;
1084 iov[0].iov_len = sizeof(disk_cache_info_t);
1085 iov[1].iov_base = (void*)dobj->name;
1086 iov[1].iov_len = disk_info.name_len;
1088 rv = apr_file_writev_full(dobj->hdrs.tempfd, (const struct iovec *) &iov,
1090 if (rv != APR_SUCCESS) {
1091 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00726)
1092 "could not write info to header file %s",
1093 dobj->hdrs.tempfile);
1094 apr_file_close(dobj->hdrs.tempfd);
1095 apr_pool_destroy(dobj->hdrs.pool);
1099 if (dobj->headers_out) {
1100 rv = store_table(dobj->hdrs.tempfd, dobj->headers_out);
1101 if (rv != APR_SUCCESS) {
1102 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00727)
1103 "could not write out-headers to header file %s",
1104 dobj->hdrs.tempfile);
1105 apr_file_close(dobj->hdrs.tempfd);
1106 apr_pool_destroy(dobj->hdrs.pool);
1111 /* Parse the vary header and dump those fields from the headers_in. */
1112 /* FIXME: Make call to the same thing cache_select calls to crack Vary. */
1113 if (dobj->headers_in) {
1114 rv = store_table(dobj->hdrs.tempfd, dobj->headers_in);
1115 if (rv != APR_SUCCESS) {
1116 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00728)
1117 "could not write in-headers to header file %s",
1118 dobj->hdrs.tempfile);
1119 apr_file_close(dobj->hdrs.tempfd);
1120 apr_pool_destroy(dobj->hdrs.pool);
1125 rv = apr_file_close(dobj->hdrs.tempfd); /* flush and close */
1126 if (rv != APR_SUCCESS) {
1127 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00729)
1128 "could not close header file %s",
1129 dobj->hdrs.tempfile);
1130 apr_pool_destroy(dobj->hdrs.pool);
1137 static apr_status_t store_body(cache_handle_t *h, request_rec *r,
1138 apr_bucket_brigade *in, apr_bucket_brigade *out)
1141 apr_status_t rv = APR_SUCCESS;
1142 disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
1143 disk_cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &cache_disk_module);
1146 if (!dobj->offset) {
1147 dobj->offset = dconf->readsize;
1149 if (!dobj->timeout && dconf->readtime) {
1150 dobj->timeout = apr_time_now() + dconf->readtime;
1154 apr_brigade_partition(in, dobj->offset, &e);
1157 while (APR_SUCCESS == rv && !APR_BRIGADE_EMPTY(in)) {
1159 apr_size_t length, written;
1161 e = APR_BRIGADE_FIRST(in);
1163 /* are we done completely? if so, pass any trailing buckets right through */
1164 if (dobj->done || !dobj->data.pool) {
1165 APR_BUCKET_REMOVE(e);
1166 APR_BRIGADE_INSERT_TAIL(out, e);
1170 /* have we seen eos yet? */
1171 if (APR_BUCKET_IS_EOS(e)) {
1174 APR_BUCKET_REMOVE(e);
1175 APR_BRIGADE_INSERT_TAIL(out, e);
1179 /* honour flush buckets, we'll get called again */
1180 if (APR_BUCKET_IS_FLUSH(e)) {
1181 APR_BUCKET_REMOVE(e);
1182 APR_BRIGADE_INSERT_TAIL(out, e);
1186 /* metadata buckets are preserved as is */
1187 if (APR_BUCKET_IS_METADATA(e)) {
1188 APR_BUCKET_REMOVE(e);
1189 APR_BRIGADE_INSERT_TAIL(out, e);
1193 /* read the bucket, write to the cache */
1194 rv = apr_bucket_read(e, &str, &length, APR_BLOCK_READ);
1195 APR_BUCKET_REMOVE(e);
1196 APR_BRIGADE_INSERT_TAIL(out, e);
1197 if (rv != APR_SUCCESS) {
1198 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00730)
1199 "Error when reading bucket for URL %s",
1201 /* Remove the intermediate cache file and return non-APR_SUCCESS */
1202 apr_pool_destroy(dobj->data.pool);
1206 /* don't write empty buckets to the cache */
1211 if (!dobj->disk_info.header_only) {
1213 /* Attempt to create the data file at the last possible moment, if
1214 * the body is empty, we don't write a file at all, and save an inode.
1216 if (!dobj->data.tempfd) {
1218 rv = apr_file_mktemp(&dobj->data.tempfd, dobj->data.tempfile,
1219 APR_CREATE | APR_WRITE | APR_BINARY | APR_BUFFERED
1220 | APR_EXCL, dobj->data.pool);
1221 if (rv != APR_SUCCESS) {
1222 apr_pool_destroy(dobj->data.pool);
1225 dobj->file_size = 0;
1226 rv = apr_file_info_get(&finfo, APR_FINFO_IDENT,
1228 if (rv != APR_SUCCESS) {
1229 apr_pool_destroy(dobj->data.pool);
1232 dobj->disk_info.device = finfo.device;
1233 dobj->disk_info.inode = finfo.inode;
1234 dobj->disk_info.has_body = 1;
1237 /* write to the cache, leave if we fail */
1238 rv = apr_file_write_full(dobj->data.tempfd, str, length, &written);
1239 if (rv != APR_SUCCESS) {
1241 APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00731) "Error when writing cache file for URL %s", h->cache_obj->key);
1242 /* Remove the intermediate cache file and return non-APR_SUCCESS */
1243 apr_pool_destroy(dobj->data.pool);
1246 dobj->file_size += written;
1247 if (dobj->file_size > dconf->maxfs) {
1249 APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00732) "URL %s failed the size check "
1250 "(%" APR_OFF_T_FMT ">%" APR_OFF_T_FMT ")", h->cache_obj->key, dobj->file_size, dconf->maxfs);
1251 /* Remove the intermediate cache file and return non-APR_SUCCESS */
1252 apr_pool_destroy(dobj->data.pool);
1253 return APR_EGENERAL;
1258 /* have we reached the limit of how much we're prepared to write in one
1259 * go? If so, leave, we'll get called again. This prevents us from trying
1260 * to swallow too much data at once, or taking so long to write the data
1261 * the client times out.
1263 dobj->offset -= length;
1264 if (dobj->offset <= 0) {
1268 if ((dconf->readtime && apr_time_now() > dobj->timeout)) {
1275 /* Was this the final bucket? If yes, close the temp file and perform
1279 const char *cl_header = apr_table_get(r->headers_out, "Content-Length");
1281 if (!dobj->disk_info.header_only) {
1283 if (dobj->data.tempfd) {
1284 rv = apr_file_close(dobj->data.tempfd);
1285 if (rv != APR_SUCCESS) {
1286 /* Buffered write failed, abandon attempt to write */
1287 apr_pool_destroy(dobj->data.pool);
1292 if (r->connection->aborted || r->no_cache) {
1294 APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00733) "Discarding body for URL %s "
1295 "because connection has been aborted.", h->cache_obj->key);
1296 /* Remove the intermediate cache file and return non-APR_SUCCESS */
1297 apr_pool_destroy(dobj->data.pool);
1298 return APR_EGENERAL;
1300 if (dobj->file_size < dconf->minfs) {
1302 APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00734) "URL %s failed the size check "
1303 "(%" APR_OFF_T_FMT "<%" APR_OFF_T_FMT ")", h->cache_obj->key, dobj->file_size, dconf->minfs);
1304 /* Remove the intermediate cache file and return non-APR_SUCCESS */
1305 apr_pool_destroy(dobj->data.pool);
1306 return APR_EGENERAL;
1309 apr_int64_t cl = apr_atoi64(cl_header);
1310 if ((errno == 0) && (dobj->file_size != cl)) {
1312 APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00735) "URL %s didn't receive complete response, not caching", h->cache_obj->key);
1313 /* Remove the intermediate cache file and return non-APR_SUCCESS */
1314 apr_pool_destroy(dobj->data.pool);
1315 return APR_EGENERAL;
1321 /* All checks were fine, we're good to go when the commit comes */
1327 static apr_status_t commit_entity(cache_handle_t *h, request_rec *r)
1329 disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
1330 &cache_disk_module);
1331 disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
1334 /* write the headers to disk at the last possible moment */
1335 rv = write_headers(h, r);
1337 /* move header and data tempfiles to the final destination */
1338 if (APR_SUCCESS == rv) {
1339 rv = file_cache_el_final(conf, &dobj->hdrs, r);
1341 if (APR_SUCCESS == rv) {
1342 rv = file_cache_el_final(conf, &dobj->vary, r);
1344 if (APR_SUCCESS == rv) {
1345 if (!dobj->disk_info.header_only) {
1346 rv = file_cache_el_final(conf, &dobj->data, r);
1348 else if (dobj->data.file) {
1349 rv = apr_file_remove(dobj->data.file, dobj->data.pool);
1353 /* remove the cached items completely on any failure */
1354 if (APR_SUCCESS != rv) {
1356 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00736)
1357 "commit_entity: URL '%s' not cached due to earlier disk error.",
1361 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00737)
1362 "commit_entity: Headers and body for URL %s cached.",
1366 apr_pool_destroy(dobj->data.pool);
1371 static apr_status_t invalidate_entity(cache_handle_t *h, request_rec *r)
1375 rv = recall_headers(h, r);
1376 if (rv != APR_SUCCESS) {
1380 /* mark the entity as invalidated */
1381 h->cache_obj->info.control.invalidated = 1;
1383 return commit_entity(h, r);
1386 static void *create_dir_config(apr_pool_t *p, char *dummy)
1388 disk_cache_dir_conf *dconf = apr_pcalloc(p, sizeof(disk_cache_dir_conf));
1390 dconf->maxfs = DEFAULT_MAX_FILE_SIZE;
1391 dconf->minfs = DEFAULT_MIN_FILE_SIZE;
1392 dconf->readsize = DEFAULT_READSIZE;
1393 dconf->readtime = DEFAULT_READTIME;
1398 static void *merge_dir_config(apr_pool_t *p, void *basev, void *addv)
1400 disk_cache_dir_conf *new = (disk_cache_dir_conf *) apr_pcalloc(p, sizeof(disk_cache_dir_conf));
1401 disk_cache_dir_conf *add = (disk_cache_dir_conf *) addv;
1402 disk_cache_dir_conf *base = (disk_cache_dir_conf *) basev;
1404 new->maxfs = (add->maxfs_set == 0) ? base->maxfs : add->maxfs;
1405 new->maxfs_set = add->maxfs_set || base->maxfs_set;
1406 new->minfs = (add->minfs_set == 0) ? base->minfs : add->minfs;
1407 new->minfs_set = add->minfs_set || base->minfs_set;
1408 new->readsize = (add->readsize_set == 0) ? base->readsize : add->readsize;
1409 new->readsize_set = add->readsize_set || base->readsize_set;
1410 new->readtime = (add->readtime_set == 0) ? base->readtime : add->readtime;
1411 new->readtime_set = add->readtime_set || base->readtime_set;
1416 static void *create_config(apr_pool_t *p, server_rec *s)
1418 disk_cache_conf *conf = apr_pcalloc(p, sizeof(disk_cache_conf));
1420 /* XXX: Set default values */
1421 conf->dirlevels = DEFAULT_DIRLEVELS;
1422 conf->dirlength = DEFAULT_DIRLENGTH;
1424 conf->cache_root = NULL;
1425 conf->cache_root_len = 0;
1431 * mod_cache_disk configuration directives handlers.
1434 *set_cache_root(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1436 disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1437 &cache_disk_module);
1438 conf->cache_root = arg;
1439 conf->cache_root_len = strlen(arg);
1440 /* TODO: canonicalize cache_root and strip off any trailing slashes */
1446 * Consider eliminating the next two directives in favor of
1447 * Ian's prime number hash...
1448 * key = hash_fn( r->uri)
1449 * filename = "/key % prime1 /key %prime2/key %prime3"
1452 *set_cache_dirlevels(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1454 disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1455 &cache_disk_module);
1456 int val = atoi(arg);
1458 return "CacheDirLevels value must be an integer greater than 0";
1459 if (val * conf->dirlength > CACHEFILE_LEN)
1460 return "CacheDirLevels*CacheDirLength value must not be higher than 20";
1461 conf->dirlevels = val;
1465 *set_cache_dirlength(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1467 disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1468 &cache_disk_module);
1469 int val = atoi(arg);
1471 return "CacheDirLength value must be an integer greater than 0";
1472 if (val * conf->dirlevels > CACHEFILE_LEN)
1473 return "CacheDirLevels*CacheDirLength value must not be higher than 20";
1475 conf->dirlength = val;
1480 *set_cache_minfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1482 disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
1484 if (apr_strtoff(&dconf->minfs, arg, NULL, 10) != APR_SUCCESS ||
1487 return "CacheMinFileSize argument must be a non-negative integer representing the min size of a file to cache in bytes.";
1489 dconf->minfs_set = 1;
1494 *set_cache_maxfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1496 disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
1498 if (apr_strtoff(&dconf->maxfs, arg, NULL, 10) != APR_SUCCESS ||
1501 return "CacheMaxFileSize argument must be a non-negative integer representing the max size of a file to cache in bytes.";
1503 dconf->maxfs_set = 1;
1508 *set_cache_readsize(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1510 disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
1512 if (apr_strtoff(&dconf->readsize, arg, NULL, 10) != APR_SUCCESS ||
1513 dconf->readsize < 0)
1515 return "CacheReadSize argument must be a non-negative integer representing the max amount of data to cache in go.";
1517 dconf->readsize_set = 1;
1522 *set_cache_readtime(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1524 disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
1525 apr_off_t milliseconds;
1527 if (apr_strtoff(&milliseconds, arg, NULL, 10) != APR_SUCCESS ||
1530 return "CacheReadTime argument must be a non-negative integer representing the max amount of time taken to cache in go.";
1532 dconf->readtime = apr_time_from_msec(milliseconds);
1533 dconf->readtime_set = 1;
1537 static const command_rec disk_cache_cmds[] =
1539 AP_INIT_TAKE1("CacheRoot", set_cache_root, NULL, RSRC_CONF,
1540 "The directory to store cache files"),
1541 AP_INIT_TAKE1("CacheDirLevels", set_cache_dirlevels, NULL, RSRC_CONF,
1542 "The number of levels of subdirectories in the cache"),
1543 AP_INIT_TAKE1("CacheDirLength", set_cache_dirlength, NULL, RSRC_CONF,
1544 "The number of characters in subdirectory names"),
1545 AP_INIT_TAKE1("CacheMinFileSize", set_cache_minfs, NULL, RSRC_CONF | ACCESS_CONF,
1546 "The minimum file size to cache a document"),
1547 AP_INIT_TAKE1("CacheMaxFileSize", set_cache_maxfs, NULL, RSRC_CONF | ACCESS_CONF,
1548 "The maximum file size to cache a document"),
1549 AP_INIT_TAKE1("CacheReadSize", set_cache_readsize, NULL, RSRC_CONF | ACCESS_CONF,
1550 "The maximum quantity of data to attempt to read and cache in one go"),
1551 AP_INIT_TAKE1("CacheReadTime", set_cache_readtime, NULL, RSRC_CONF | ACCESS_CONF,
1552 "The maximum time taken to attempt to read and cache in go"),
1556 static const cache_provider cache_disk_provider =
1570 static void disk_cache_register_hook(apr_pool_t *p)
1572 /* cache initializer */
1573 ap_register_provider(p, CACHE_PROVIDER_GROUP, "disk", "0",
1574 &cache_disk_provider);
1577 AP_DECLARE_MODULE(cache_disk) = {
1578 STANDARD20_MODULE_STUFF,
1579 create_dir_config, /* create per-directory config structure */
1580 merge_dir_config, /* merge per-directory config structures */
1581 create_config, /* create per-server config structure */
1582 NULL, /* merge per-server config structures */
1583 disk_cache_cmds, /* command apr_table_t */
1584 disk_cache_register_hook /* register hooks */