]> granicus.if.org Git - apache/blob - modules/cache/mod_disk_cache.c
f902c0f95d9f68ac93b8378ad6f39a9f7d4fe3fc
[apache] / modules / cache / mod_disk_cache.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "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"
25
26 /*
27  * mod_disk_cache: Disk Based HTTP 1.1 Cache.
28  *
29  * Flow to Find the .data file:
30  *   Incoming client requests URI /foo/bar/baz
31  *   Generate <hash> off of /foo/bar/baz
32  *   Open <hash>.header
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)
38  *   read in <hash>.data
39  *
40  * Format #1:
41  *   apr_uint32_t format;
42  *   apr_time_t expire;
43  *   apr_array_t vary_headers (delimited by CRLF)
44  *
45  * Format #2:
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)
49  *   CRLF
50  *   r->headers_in (delimited by CRLF)
51  *   CRLF
52  */
53
54 module AP_MODULE_DECLARE_DATA disk_cache_module;
55
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 *in,
60                                apr_bucket_brigade *out);
61 static apr_status_t recall_headers(cache_handle_t *h, request_rec *r);
62 static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
63 static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
64                                apr_file_t *file);
65
66 /*
67  * Local static functions
68  */
69
70 static char *header_file(apr_pool_t *p, disk_cache_conf *conf,
71                          disk_cache_object_t *dobj, const char *name)
72 {
73     if (!dobj->hashfile) {
74         dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
75                                                 conf->dirlength, name);
76     }
77
78     if (dobj->prefix) {
79         return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
80                            dobj->hashfile, CACHE_HEADER_SUFFIX, NULL);
81      }
82      else {
83         return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
84                            CACHE_HEADER_SUFFIX, NULL);
85      }
86 }
87
88 static char *data_file(apr_pool_t *p, disk_cache_conf *conf,
89                        disk_cache_object_t *dobj, const char *name)
90 {
91     if (!dobj->hashfile) {
92         dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
93                                                 conf->dirlength, name);
94     }
95
96     if (dobj->prefix) {
97         return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
98                            dobj->hashfile, CACHE_DATA_SUFFIX, NULL);
99      }
100      else {
101         return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
102                            CACHE_DATA_SUFFIX, NULL);
103      }
104 }
105
106 static apr_status_t mkdir_structure(disk_cache_conf *conf, const char *file, apr_pool_t *pool)
107 {
108     apr_status_t rv;
109     char *p;
110
111     for (p = (char*)file + conf->cache_root_len + 1;;) {
112         p = strchr(p, '/');
113         if (!p)
114             break;
115         *p = '\0';
116
117         rv = apr_dir_make(file,
118                           APR_UREAD|APR_UWRITE|APR_UEXECUTE, pool);
119         if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) {
120             return rv;
121         }
122         *p = '/';
123         ++p;
124     }
125     return APR_SUCCESS;
126 }
127
128 /* htcacheclean may remove directories underneath us.
129  * So, we'll try renaming three times at a cost of 0.002 seconds.
130  */
131 static apr_status_t safe_file_rename(disk_cache_conf *conf,
132                                      const char *src, const char *dest,
133                                      apr_pool_t *pool)
134 {
135     apr_status_t rv;
136
137     rv = apr_file_rename(src, dest, pool);
138
139     if (rv != APR_SUCCESS) {
140         int i;
141
142         for (i = 0; i < 2 && rv != APR_SUCCESS; i++) {
143             /* 1000 micro-seconds aka 0.001 seconds. */
144             apr_sleep(1000);
145
146             rv = mkdir_structure(conf, dest, pool);
147             if (rv != APR_SUCCESS)
148                 continue;
149
150             rv = apr_file_rename(src, dest, pool);
151         }
152     }
153
154     return rv;
155 }
156
157 static apr_status_t file_cache_el_final(disk_cache_conf *conf, disk_cache_file_t *file,
158                                         request_rec *r)
159 {
160     apr_status_t rv;
161
162     /* This assumes that the tempfiles are on the same file system
163      * as the cache_root. If not, then we need a file copy/move
164      * rather than a rename.
165      */
166
167     /* move the file over */
168     if (file->tempfd) {
169
170         rv = safe_file_rename(conf, file->tempfile, file->file, file->pool);
171         if (rv != APR_SUCCESS) {
172             ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
173                          "disk_cache: rename tempfile to file failed:"
174                          " %s -> %s", file->tempfile, file->file);
175             apr_file_remove(file->tempfile, file->pool);
176         }
177
178         file->tempfd = NULL;
179     }
180
181     return rv;
182 }
183
184 static apr_status_t file_cache_temp_cleanup(void *dummy) {
185     disk_cache_file_t *file = (disk_cache_file_t *)dummy;
186
187     /* clean up the temporary file */
188     if (file->tempfd) {
189         apr_file_remove(file->tempfile, file->pool);
190         file->tempfd = NULL;
191     }
192     file->tempfile = NULL;
193     file->pool = NULL;
194
195     return APR_SUCCESS;
196 }
197
198 static apr_status_t file_cache_create(disk_cache_conf *conf, disk_cache_file_t *file,
199                                       apr_pool_t *pool)
200 {
201     file->pool = pool;
202     file->tempfile = apr_pstrcat(pool, conf->cache_root, AP_TEMPFILE, NULL);
203
204     apr_pool_cleanup_register(pool, file, file_cache_temp_cleanup, file_cache_temp_cleanup);
205
206     return APR_SUCCESS;
207 }
208
209 /* These two functions get and put state information into the data
210  * file for an ap_cache_el, this state information will be read
211  * and written transparent to clients of this module
212  */
213 static int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,
214                                     disk_cache_object_t *dobj, request_rec *r)
215 {
216     apr_status_t rv;
217     char *urlbuff;
218     disk_cache_info_t disk_info;
219     apr_size_t len;
220
221     /* read the data from the cache file */
222     len = sizeof(disk_cache_info_t);
223     rv = apr_file_read_full(fd, &disk_info, len, &len);
224     if (rv != APR_SUCCESS) {
225         return rv;
226     }
227
228     /* Store it away so we can get it later. */
229     dobj->disk_info = disk_info;
230
231     info->status = disk_info.status;
232     info->date = disk_info.date;
233     info->expire = disk_info.expire;
234     info->request_time = disk_info.request_time;
235     info->response_time = disk_info.response_time;
236
237     /* Note that we could optimize this by conditionally doing the palloc
238      * depending upon the size. */
239     urlbuff = apr_palloc(r->pool, disk_info.name_len + 1);
240     len = disk_info.name_len;
241     rv = apr_file_read_full(fd, urlbuff, len, &len);
242     if (rv != APR_SUCCESS) {
243         return rv;
244     }
245     urlbuff[disk_info.name_len] = '\0';
246
247     /* check that we have the same URL */
248     /* Would strncmp be correct? */
249     if (strcmp(urlbuff, dobj->name) != 0) {
250         return APR_EGENERAL;
251     }
252
253     return APR_SUCCESS;
254 }
255
256 static const char* regen_key(apr_pool_t *p, apr_table_t *headers,
257                              apr_array_header_t *varray, const char *oldkey)
258 {
259     struct iovec *iov;
260     int i, k;
261     int nvec;
262     const char *header;
263     const char **elts;
264
265     nvec = (varray->nelts * 2) + 1;
266     iov = apr_palloc(p, sizeof(struct iovec) * nvec);
267     elts = (const char **) varray->elts;
268
269     /* TODO:
270      *    - Handle multiple-value headers better. (sort them?)
271      *    - Handle Case in-sensitive Values better.
272      *        This isn't the end of the world, since it just lowers the cache
273      *        hit rate, but it would be nice to fix.
274      *
275      * The majority are case insenstive if they are values (encoding etc).
276      * Most of rfc2616 is case insensitive on header contents.
277      *
278      * So the better solution may be to identify headers which should be
279      * treated case-sensitive?
280      *  HTTP URI's (3.2.3) [host and scheme are insensitive]
281      *  HTTP method (5.1.1)
282      *  HTTP-date values (3.3.1)
283      *  3.7 Media Types [exerpt]
284      *     The type, subtype, and parameter attribute names are case-
285      *     insensitive. Parameter values might or might not be case-sensitive,
286      *     depending on the semantics of the parameter name.
287      *  4.20 Except [exerpt]
288      *     Comparison of expectation values is case-insensitive for unquoted
289      *     tokens (including the 100-continue token), and is case-sensitive for
290      *     quoted-string expectation-extensions.
291      */
292
293     for(i=0, k=0; i < varray->nelts; i++) {
294         header = apr_table_get(headers, elts[i]);
295         if (!header) {
296             header = "";
297         }
298         iov[k].iov_base = (char*) elts[i];
299         iov[k].iov_len = strlen(elts[i]);
300         k++;
301         iov[k].iov_base = (char*) header;
302         iov[k].iov_len = strlen(header);
303         k++;
304     }
305     iov[k].iov_base = (char*) oldkey;
306     iov[k].iov_len = strlen(oldkey);
307     k++;
308
309     return apr_pstrcatv(p, iov, k, NULL);
310 }
311
312 static int array_alphasort(const void *fn1, const void *fn2)
313 {
314     return strcmp(*(char**)fn1, *(char**)fn2);
315 }
316
317 static void tokens_to_array(apr_pool_t *p, const char *data,
318                             apr_array_header_t *arr)
319 {
320     char *token;
321
322     while ((token = ap_get_list_item(p, &data)) != NULL) {
323         *((const char **) apr_array_push(arr)) = token;
324     }
325
326     /* Sort it so that "Vary: A, B" and "Vary: B, A" are stored the same. */
327     qsort((void *) arr->elts, arr->nelts,
328          sizeof(char *), array_alphasort);
329 }
330
331 /*
332  * Hook and mod_cache callback functions
333  */
334 static int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len)
335 {
336     disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
337                                                  &disk_cache_module);
338     cache_object_t *obj;
339     disk_cache_object_t *dobj;
340     apr_pool_t *pool;
341
342     if (conf->cache_root == NULL) {
343         return DECLINED;
344     }
345
346     /* we don't support caching of range requests (yet) */
347     if (r->status == HTTP_PARTIAL_CONTENT) {
348         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
349                      "disk_cache: URL %s partial content response not cached",
350                      key);
351         return DECLINED;
352     }
353
354     /* Note, len is -1 if unknown so don't trust it too hard */
355     if (len > conf->maxfs) {
356         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
357                      "disk_cache: URL %s failed the size check "
358                      "(%" APR_OFF_T_FMT " > %" APR_OFF_T_FMT ")",
359                      key, len, conf->maxfs);
360         return DECLINED;
361     }
362     if (len >= 0 && len < conf->minfs) {
363         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
364                      "disk_cache: URL %s failed the size check "
365                      "(%" APR_OFF_T_FMT " < %" APR_OFF_T_FMT ")",
366                      key, len, conf->minfs);
367         return DECLINED;
368     }
369
370     /* Allocate and initialize cache_object_t and disk_cache_object_t */
371     h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj));
372     obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj));
373
374     obj->key = apr_pstrdup(r->pool, key);
375
376     dobj->name = obj->key;
377     dobj->prefix = NULL;
378     /* Save the cache root */
379     dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
380     dobj->root_len = conf->cache_root_len;
381
382     apr_pool_create(&pool, r->pool);
383
384     file_cache_create(conf, &dobj->hdrs, pool);
385     file_cache_create(conf, &dobj->vary, pool);
386     file_cache_create(conf, &dobj->data, pool);
387
388     dobj->data.file = data_file(r->pool, conf, dobj, key);
389     dobj->hdrs.file = header_file(r->pool, conf, dobj, key);
390     dobj->vary.file = header_file(r->pool, conf, dobj, key);
391
392     return OK;
393 }
394
395 static int open_entity(cache_handle_t *h, request_rec *r, const char *key)
396 {
397     apr_uint32_t format;
398     apr_size_t len;
399     const char *nkey;
400     apr_status_t rc;
401     static int error_logged = 0;
402     disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
403                                                  &disk_cache_module);
404 #ifdef APR_SENDFILE_ENABLED
405     core_dir_config *coreconf = ap_get_module_config(r->per_dir_config,
406                                                      &core_module);
407 #endif
408     apr_finfo_t finfo;
409     cache_object_t *obj;
410     cache_info *info;
411     disk_cache_object_t *dobj;
412     int flags;
413     apr_pool_t *pool;
414
415     h->cache_obj = NULL;
416
417     /* Look up entity keyed to 'url' */
418     if (conf->cache_root == NULL) {
419         if (!error_logged) {
420             error_logged = 1;
421             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
422                          "disk_cache: Cannot cache files to disk without a CacheRoot specified.");
423         }
424         return DECLINED;
425     }
426
427     /* Create and init the cache object */
428     h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(cache_object_t));
429     obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(disk_cache_object_t));
430
431     info = &(obj->info);
432
433     /* Open the headers file */
434     dobj->prefix = NULL;
435
436     /* Save the cache root */
437     dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
438     dobj->root_len = conf->cache_root_len;
439
440     dobj->vary.file = header_file(r->pool, conf, dobj, key);
441     flags = APR_READ|APR_BINARY|APR_BUFFERED;
442     rc = apr_file_open(&dobj->vary.fd, dobj->vary.file, flags, 0, r->pool);
443     if (rc != APR_SUCCESS) {
444         return DECLINED;
445     }
446
447     /* read the format from the cache file */
448     len = sizeof(format);
449     apr_file_read_full(dobj->vary.fd, &format, len, &len);
450
451     if (format == VARY_FORMAT_VERSION) {
452         apr_array_header_t* varray;
453         apr_time_t expire;
454
455         len = sizeof(expire);
456         apr_file_read_full(dobj->vary.fd, &expire, len, &len);
457
458         varray = apr_array_make(r->pool, 5, sizeof(char*));
459         rc = read_array(r, varray, dobj->vary.fd);
460         if (rc != APR_SUCCESS) {
461             ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
462                          "disk_cache: Cannot parse vary header file: %s",
463                          dobj->vary.file);
464             apr_file_close(dobj->vary.fd);
465             return DECLINED;
466         }
467         apr_file_close(dobj->vary.fd);
468
469         nkey = regen_key(r->pool, r->headers_in, varray, key);
470
471         dobj->hashfile = NULL;
472         dobj->prefix = dobj->vary.file;
473         dobj->hdrs.file = header_file(r->pool, conf, dobj, nkey);
474
475         flags = APR_READ|APR_BINARY|APR_BUFFERED;
476         rc = apr_file_open(&dobj->hdrs.fd, dobj->hdrs.file, flags, 0, r->pool);
477         if (rc != APR_SUCCESS) {
478             return DECLINED;
479         }
480     }
481     else if (format != DISK_FORMAT_VERSION) {
482         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
483                      "disk_cache: File '%s' has a version mismatch. File had version: %d.",
484                      dobj->vary.file, format);
485         apr_file_close(dobj->vary.fd);
486         return DECLINED;
487     }
488     else {
489         apr_off_t offset = 0;
490
491         /* oops, not vary as it turns out */
492         dobj->hdrs.fd = dobj->vary.fd;
493         dobj->vary.fd = NULL;
494         dobj->hdrs.file = dobj->vary.file;
495
496         /* This wasn't a Vary Format file, so we must seek to the
497          * start of the file again, so that later reads work.
498          */
499         apr_file_seek(dobj->hdrs.fd, APR_SET, &offset);
500         nkey = key;
501     }
502
503     obj->key = nkey;
504     dobj->key = nkey;
505     dobj->name = key;
506
507     apr_pool_create(&pool, r->pool);
508
509     file_cache_create(conf, &dobj->hdrs, pool);
510     file_cache_create(conf, &dobj->vary, pool);
511     file_cache_create(conf, &dobj->data, pool);
512
513     dobj->data.file = data_file(r->pool, conf, dobj, nkey);
514
515     /* Open the data file */
516     flags = APR_READ|APR_BINARY;
517 #ifdef APR_SENDFILE_ENABLED
518     /* When we are in the quick handler we don't have the per-directory
519      * configuration, so this check only takes the global setting of
520      * the EnableSendFile directive into account.
521      */
522     flags |= AP_SENDFILE_ENABLED(coreconf->enable_sendfile);
523 #endif
524     rc = apr_file_open(&dobj->data.fd, dobj->data.file, flags, 0, r->pool);
525     if (rc != APR_SUCCESS) {
526         ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
527                  "disk_cache: Cannot open data file %s",  dobj->data.file);
528         apr_file_close(dobj->hdrs.fd);
529         return DECLINED;
530     }
531
532     rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, dobj->data.fd);
533     if (rc == APR_SUCCESS) {
534         dobj->file_size = finfo.size;
535     }
536
537     /* Read the bytes to setup the cache_info fields */
538     rc = file_cache_recall_mydata(dobj->hdrs.fd, info, dobj, r);
539     if (rc != APR_SUCCESS) {
540         ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
541                  "disk_cache: Cannot read header file %s",  dobj->hdrs.file);
542         apr_file_close(dobj->hdrs.fd);
543         return DECLINED;
544     }
545
546     /* Initialize the cache_handle callback functions */
547     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
548                  "disk_cache: Recalled cached URL info header %s",  dobj->name);
549
550     apr_file_close(dobj->hdrs.fd);
551
552     return OK;
553 }
554
555 static int remove_entity(cache_handle_t *h)
556 {
557     /* Null out the cache object pointer so next time we start from scratch  */
558     h->cache_obj = NULL;
559     return OK;
560 }
561
562 static int remove_url(cache_handle_t *h, apr_pool_t *p)
563 {
564     apr_status_t rc;
565     disk_cache_object_t *dobj;
566
567     /* Get disk cache object from cache handle */
568     dobj = (disk_cache_object_t *) h->cache_obj->vobj;
569     if (!dobj) {
570         return DECLINED;
571     }
572
573     /* Delete headers file */
574     if (dobj->hdrs.file) {
575         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
576                      "disk_cache: Deleting %s from cache.", dobj->hdrs.file);
577
578         rc = apr_file_remove(dobj->hdrs.file, p);
579         if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
580             /* Will only result in an output if httpd is started with -e debug.
581              * For reason see log_error_core for the case s == NULL.
582              */
583             ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
584                    "disk_cache: Failed to delete headers file %s from cache.",
585                          dobj->hdrs.file);
586             return DECLINED;
587         }
588     }
589
590     /* Delete data file */
591     if (dobj->data.file) {
592         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
593                      "disk_cache: Deleting %s from cache.", dobj->data.file);
594
595         rc = apr_file_remove(dobj->data.file, p);
596         if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
597             /* Will only result in an output if httpd is started with -e debug.
598              * For reason see log_error_core for the case s == NULL.
599              */
600             ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
601                       "disk_cache: Failed to delete data file %s from cache.",
602                          dobj->data.file);
603             return DECLINED;
604         }
605     }
606
607     /* now delete directories as far as possible up to our cache root */
608     if (dobj->root) {
609         const char *str_to_copy;
610
611         str_to_copy = dobj->hdrs.file ? dobj->hdrs.file : dobj->data.file;
612         if (str_to_copy) {
613             char *dir, *slash, *q;
614
615             dir = apr_pstrdup(p, str_to_copy);
616
617             /* remove filename */
618             slash = strrchr(dir, '/');
619             *slash = '\0';
620
621             /*
622              * now walk our way back to the cache root, delete everything
623              * in the way as far as possible
624              *
625              * Note: due to the way we constructed the file names in
626              * header_file and data_file, we are guaranteed that the
627              * cache_root is suffixed by at least one '/' which will be
628              * turned into a terminating null by this loop.  Therefore,
629              * we won't either delete or go above our cache root.
630              */
631             for (q = dir + dobj->root_len; *q ; ) {
632                  ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
633                               "disk_cache: Deleting directory %s from cache",
634                               dir);
635
636                  rc = apr_dir_remove(dir, p);
637                  if (rc != APR_SUCCESS && !APR_STATUS_IS_ENOENT(rc)) {
638                     break;
639                  }
640                  slash = strrchr(q, '/');
641                  *slash = '\0';
642             }
643         }
644     }
645
646     return OK;
647 }
648
649 static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
650                                apr_file_t *file)
651 {
652     char w[MAX_STRING_LEN];
653     int p;
654     apr_status_t rv;
655
656     while (1) {
657         rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
658         if (rv != APR_SUCCESS) {
659             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
660                           "Premature end of vary array.");
661             return rv;
662         }
663
664         p = strlen(w);
665         if (p > 0 && w[p - 1] == '\n') {
666             if (p > 1 && w[p - 2] == CR) {
667                 w[p - 2] = '\0';
668             }
669             else {
670                 w[p - 1] = '\0';
671             }
672         }
673
674         /* If we've finished reading the array, break out of the loop. */
675         if (w[0] == '\0') {
676             break;
677         }
678
679        *((const char **) apr_array_push(arr)) = apr_pstrdup(r->pool, w);
680     }
681
682     return APR_SUCCESS;
683 }
684
685 static apr_status_t store_array(apr_file_t *fd, apr_array_header_t* arr)
686 {
687     int i;
688     apr_status_t rv;
689     struct iovec iov[2];
690     apr_size_t amt;
691     const char **elts;
692
693     elts = (const char **) arr->elts;
694
695     for (i = 0; i < arr->nelts; i++) {
696         iov[0].iov_base = (char*) elts[i];
697         iov[0].iov_len = strlen(elts[i]);
698         iov[1].iov_base = CRLF;
699         iov[1].iov_len = sizeof(CRLF) - 1;
700
701         rv = apr_file_writev(fd, (const struct iovec *) &iov, 2,
702                              &amt);
703         if (rv != APR_SUCCESS) {
704             return rv;
705         }
706     }
707
708     iov[0].iov_base = CRLF;
709     iov[0].iov_len = sizeof(CRLF) - 1;
710
711     return apr_file_writev(fd, (const struct iovec *) &iov, 1,
712                          &amt);
713 }
714
715 static apr_status_t read_table(cache_handle_t *handle, request_rec *r,
716                                apr_table_t *table, apr_file_t *file)
717 {
718     char w[MAX_STRING_LEN];
719     char *l;
720     int p;
721     apr_status_t rv;
722
723     while (1) {
724
725         /* ### What about APR_EOF? */
726         rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
727         if (rv != APR_SUCCESS) {
728             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
729                           "Premature end of cache headers.");
730             return rv;
731         }
732
733         /* Delete terminal (CR?)LF */
734
735         p = strlen(w);
736         /* Indeed, the host's '\n':
737            '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
738            -- whatever the script generates.
739         */
740         if (p > 0 && w[p - 1] == '\n') {
741             if (p > 1 && w[p - 2] == CR) {
742                 w[p - 2] = '\0';
743             }
744             else {
745                 w[p - 1] = '\0';
746             }
747         }
748
749         /* If we've finished reading the headers, break out of the loop. */
750         if (w[0] == '\0') {
751             break;
752         }
753
754 #if APR_CHARSET_EBCDIC
755         /* Chances are that we received an ASCII header text instead of
756          * the expected EBCDIC header lines. Try to auto-detect:
757          */
758         if (!(l = strchr(w, ':'))) {
759             int maybeASCII = 0, maybeEBCDIC = 0;
760             unsigned char *cp, native;
761             apr_size_t inbytes_left, outbytes_left;
762
763             for (cp = w; *cp != '\0'; ++cp) {
764                 native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
765                 if (apr_isprint(*cp) && !apr_isprint(native))
766                     ++maybeEBCDIC;
767                 if (!apr_isprint(*cp) && apr_isprint(native))
768                     ++maybeASCII;
769             }
770             if (maybeASCII > maybeEBCDIC) {
771                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
772                              "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
773                              r->filename);
774                 inbytes_left = outbytes_left = cp - w;
775                 apr_xlate_conv_buffer(ap_hdrs_from_ascii,
776                                       w, &inbytes_left, w, &outbytes_left);
777             }
778         }
779 #endif /*APR_CHARSET_EBCDIC*/
780
781         /* if we see a bogus header don't ignore it. Shout and scream */
782         if (!(l = strchr(w, ':'))) {
783             return APR_EGENERAL;
784         }
785
786         *l++ = '\0';
787         while (*l && apr_isspace(*l)) {
788             ++l;
789         }
790
791         apr_table_add(table, w, l);
792     }
793
794     return APR_SUCCESS;
795 }
796
797 /*
798  * Reads headers from a buffer and returns an array of headers.
799  * Returns NULL on file error
800  * This routine tries to deal with too long lines and continuation lines.
801  * @@@: XXX: FIXME: currently the headers are passed thru un-merged.
802  * Is that okay, or should they be collapsed where possible?
803  */
804 static apr_status_t recall_headers(cache_handle_t *h, request_rec *r)
805 {
806     disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
807
808     /* This case should not happen... */
809     if (!dobj->hdrs.fd) {
810         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
811                  "disk_cache: recalling headers; but no header fd for %s", dobj->name);
812         return APR_NOTFOUND;
813     }
814
815     h->req_hdrs = apr_table_make(r->pool, 20);
816     h->resp_hdrs = apr_table_make(r->pool, 20);
817
818     /* Call routine to read the header lines/status line */
819     read_table(h, r, h->resp_hdrs, dobj->hdrs.fd);
820     read_table(h, r, h->req_hdrs, dobj->hdrs.fd);
821
822     apr_file_close(dobj->hdrs.fd);
823
824     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
825                  "disk_cache: Recalled headers for URL %s",  dobj->name);
826     return APR_SUCCESS;
827 }
828
829 static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb)
830 {
831     apr_bucket *e;
832     disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
833
834     apr_brigade_insert_file(bb, dobj->data.fd, 0, dobj->file_size, p);
835
836     e = apr_bucket_eos_create(bb->bucket_alloc);
837     APR_BRIGADE_INSERT_TAIL(bb, e);
838
839     return APR_SUCCESS;
840 }
841
842 static apr_status_t store_table(apr_file_t *fd, apr_table_t *table)
843 {
844     int i;
845     apr_status_t rv;
846     struct iovec iov[4];
847     apr_size_t amt;
848     apr_table_entry_t *elts;
849
850     elts = (apr_table_entry_t *) apr_table_elts(table)->elts;
851     for (i = 0; i < apr_table_elts(table)->nelts; ++i) {
852         if (elts[i].key != NULL) {
853             iov[0].iov_base = elts[i].key;
854             iov[0].iov_len = strlen(elts[i].key);
855             iov[1].iov_base = ": ";
856             iov[1].iov_len = sizeof(": ") - 1;
857             iov[2].iov_base = elts[i].val;
858             iov[2].iov_len = strlen(elts[i].val);
859             iov[3].iov_base = CRLF;
860             iov[3].iov_len = sizeof(CRLF) - 1;
861
862             rv = apr_file_writev(fd, (const struct iovec *) &iov, 4,
863                                  &amt);
864             if (rv != APR_SUCCESS) {
865                 return rv;
866             }
867         }
868     }
869     iov[0].iov_base = CRLF;
870     iov[0].iov_len = sizeof(CRLF) - 1;
871     rv = apr_file_writev(fd, (const struct iovec *) &iov, 1,
872                          &amt);
873     return rv;
874 }
875
876 static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *info)
877 {
878     disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
879                                                  &disk_cache_module);
880     apr_status_t rv;
881     apr_size_t amt;
882     disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
883
884     disk_cache_info_t disk_info;
885     struct iovec iov[2];
886
887     /* This is flaky... we need to manage the cache_info differently */
888     h->cache_obj->info = *info;
889
890     if (r->headers_out) {
891         const char *tmp;
892
893         tmp = apr_table_get(r->headers_out, "Vary");
894
895         if (tmp) {
896             apr_array_header_t* varray;
897             apr_uint32_t format = VARY_FORMAT_VERSION;
898
899             /* If we were initially opened as a vary format, rollback
900              * that internal state for the moment so we can recreate the
901              * vary format hints in the appropriate directory.
902              */
903             if (dobj->prefix) {
904                 dobj->hdrs.file = dobj->prefix;
905                 dobj->prefix = NULL;
906             }
907
908             rv = mkdir_structure(conf, dobj->hdrs.file, r->pool);
909
910             rv = apr_file_mktemp(&dobj->vary.tempfd, dobj->vary.tempfile,
911                                  APR_CREATE | APR_WRITE | APR_BINARY | APR_EXCL,
912                                  dobj->vary.pool);
913
914             if (rv != APR_SUCCESS) {
915                 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
916                     "disk_cache: could not create temp file %s",
917                     dobj->vary.tempfile);
918                 return rv;
919             }
920
921             amt = sizeof(format);
922             apr_file_write(dobj->vary.tempfd, &format, &amt);
923
924             amt = sizeof(info->expire);
925             apr_file_write(dobj->vary.tempfd, &info->expire, &amt);
926
927             varray = apr_array_make(r->pool, 6, sizeof(char*));
928             tokens_to_array(r->pool, tmp, varray);
929
930             store_array(dobj->vary.tempfd, varray);
931
932             apr_file_close(dobj->vary.tempfd);
933
934             tmp = regen_key(r->pool, r->headers_in, varray, dobj->name);
935             dobj->prefix = dobj->hdrs.file;
936             dobj->hashfile = NULL;
937             dobj->data.file = data_file(r->pool, conf, dobj, tmp);
938             dobj->hdrs.file = header_file(r->pool, conf, dobj, tmp);
939         }
940     }
941
942
943     rv = apr_file_mktemp(&dobj->hdrs.tempfd, dobj->hdrs.tempfile,
944                          APR_CREATE | APR_WRITE | APR_BINARY |
945                          APR_BUFFERED | APR_EXCL, dobj->hdrs.pool);
946
947     if (rv != APR_SUCCESS) {
948        ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
949            "disk_cache: could not create temp file %s",
950            dobj->hdrs.tempfile);
951         return rv;
952     }
953
954     disk_info.format = DISK_FORMAT_VERSION;
955     disk_info.date = info->date;
956     disk_info.expire = info->expire;
957     disk_info.entity_version = dobj->disk_info.entity_version++;
958     disk_info.request_time = info->request_time;
959     disk_info.response_time = info->response_time;
960     disk_info.status = info->status;
961
962     disk_info.name_len = strlen(dobj->name);
963
964     iov[0].iov_base = (void*)&disk_info;
965     iov[0].iov_len = sizeof(disk_cache_info_t);
966     iov[1].iov_base = (void*)dobj->name;
967     iov[1].iov_len = disk_info.name_len;
968
969     rv = apr_file_writev(dobj->hdrs.tempfd, (const struct iovec *) &iov, 2, &amt);
970     if (rv != APR_SUCCESS) {
971         ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
972            "disk_cache: could not write info to header file %s",
973            dobj->hdrs.tempfile);
974         apr_file_close(dobj->hdrs.tempfd);
975         return rv;
976     }
977
978     if (r->headers_out) {
979         apr_table_t *headers_out;
980
981         headers_out = ap_cache_cacheable_headers_out(r);
982
983         rv = store_table(dobj->hdrs.tempfd, headers_out);
984         if (rv != APR_SUCCESS) {
985             ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
986                "disk_cache: could not write out-headers to header file %s",
987                dobj->hdrs.tempfile);
988             apr_file_close(dobj->hdrs.tempfd);
989             return rv;
990         }
991     }
992
993     /* Parse the vary header and dump those fields from the headers_in. */
994     /* FIXME: Make call to the same thing cache_select calls to crack Vary. */
995     if (r->headers_in) {
996         apr_table_t *headers_in;
997
998         headers_in = ap_cache_cacheable_headers_in(r);
999
1000         rv = store_table(dobj->hdrs.tempfd, headers_in);
1001         if (rv != APR_SUCCESS) {
1002             ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
1003                "disk_cache: could not write in-headers to header file %s",
1004                dobj->hdrs.tempfile);
1005             apr_file_close(dobj->hdrs.tempfd);
1006             return rv;
1007         }
1008     }
1009
1010     apr_file_close(dobj->hdrs.tempfd); /* flush and close */
1011
1012     return APR_SUCCESS;
1013 }
1014
1015 static apr_status_t store_body(cache_handle_t *h, request_rec *r,
1016                                apr_bucket_brigade *in, apr_bucket_brigade *out)
1017 {
1018     apr_bucket *e;
1019     apr_status_t rv = APR_SUCCESS;
1020     disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
1021     disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
1022                                                  &disk_cache_module);
1023     disk_cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &disk_cache_module);
1024     int seen_eos = 0;
1025
1026     /* We write to a temp file and then atomically rename the file over
1027      * in file_cache_el_final().
1028      */
1029     if (!dobj->data.tempfd) {
1030         rv = apr_file_mktemp(&dobj->data.tempfd, dobj->data.tempfile,
1031                              APR_CREATE | APR_WRITE | APR_BINARY |
1032                              APR_BUFFERED | APR_EXCL, dobj->data.pool);
1033         if (rv != APR_SUCCESS) {
1034             return rv;
1035         }
1036         dobj->file_size = 0;
1037     }
1038     if (!dobj->bb) {
1039         dobj->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1040     }
1041     if (!dobj->offset) {
1042         dobj->offset = dconf->readsize;
1043     }
1044     if (!dobj->timeout && dconf->readtime) {
1045         dobj->timeout = apr_time_now() + dconf->readtime;
1046     }
1047
1048     if (dobj->offset) {
1049         apr_brigade_partition(in, dobj->offset, &e);
1050     }
1051
1052     while (APR_SUCCESS == rv && !APR_BRIGADE_EMPTY(in)) {
1053         const char *str;
1054         apr_size_t length, written;
1055
1056         e = APR_BRIGADE_FIRST(in);
1057
1058         /* are we done completely? if so, pass any trailing buckets right through */
1059         if (dobj->done) {
1060             APR_BUCKET_REMOVE(e);
1061             APR_BRIGADE_INSERT_TAIL(out, e);
1062             continue;
1063         }
1064
1065         /* have we seen eos yet? */
1066         if (APR_BUCKET_IS_EOS(e)) {
1067             seen_eos = 1;
1068             dobj->done = 1;
1069             APR_BUCKET_REMOVE(e);
1070             APR_BRIGADE_CONCAT(out, dobj->bb);
1071             APR_BRIGADE_INSERT_TAIL(out, e);
1072             break;
1073         }
1074
1075         /* honour flush buckets, we'll get called again */
1076         if (APR_BUCKET_IS_FLUSH(e)) {
1077             APR_BUCKET_REMOVE(e);
1078             APR_BRIGADE_CONCAT(out, dobj->bb);
1079             APR_BRIGADE_INSERT_TAIL(out, e);
1080             break;
1081         }
1082
1083         /* metadata buckets are preserved as is */
1084         if (APR_BUCKET_IS_METADATA(e)) {
1085             APR_BUCKET_REMOVE(e);
1086             APR_BRIGADE_INSERT_TAIL(dobj->bb, e);
1087             continue;
1088         }
1089
1090         /* read the bucket, write to the cache */
1091         rv = apr_bucket_read(e, &str, &length, APR_BLOCK_READ);
1092         APR_BUCKET_REMOVE(e);
1093         APR_BRIGADE_INSERT_TAIL(dobj->bb, e);
1094         if (rv != APR_SUCCESS) {
1095             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
1096                          "disk_cache: Error when reading bucket for URL %s",
1097                          h->cache_obj->key);
1098             /* Remove the intermediate cache file and return non-APR_SUCCESS */
1099             apr_pool_destroy(dobj->data.pool);
1100             APR_BRIGADE_CONCAT(out, dobj->bb);
1101             return rv;
1102         }
1103
1104         /* write to the cache, leave if we fail */
1105         rv = apr_file_write_full(dobj->data.tempfd, str, length, &written);
1106         if (rv != APR_SUCCESS) {
1107             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
1108                          "disk_cache: Error when writing cache file for URL %s",
1109                          h->cache_obj->key);
1110             /* Remove the intermediate cache file and return non-APR_SUCCESS */
1111             apr_pool_destroy(dobj->data.pool);
1112             APR_BRIGADE_CONCAT(out, dobj->bb);
1113             return rv;
1114         }
1115         dobj->file_size += written;
1116         if (dobj->file_size > conf->maxfs) {
1117             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1118                          "disk_cache: URL %s failed the size check "
1119                          "(%" APR_OFF_T_FMT ">%" APR_OFF_T_FMT ")",
1120                          h->cache_obj->key, dobj->file_size, conf->maxfs);
1121             /* Remove the intermediate cache file and return non-APR_SUCCESS */
1122             apr_pool_destroy(dobj->data.pool);
1123             APR_BRIGADE_CONCAT(out, dobj->bb);
1124             return APR_EGENERAL;
1125         }
1126
1127         /* have we reached the limit of how much we're prepared to write in one
1128          * go? If so, leave, we'll get called again. This prevents us from trying
1129          * to swallow too much data at once, or taking so long to write the data
1130          * the client times out.
1131          */
1132         dobj->offset -= length;
1133         if (dobj->offset <= 0) {
1134             dobj->offset = 0;
1135             APR_BRIGADE_CONCAT(out, dobj->bb);
1136             break;
1137         }
1138         if ((dconf->readtime && apr_time_now() > dobj->timeout)) {
1139             dobj->timeout = 0;
1140             APR_BRIGADE_CONCAT(out, dobj->bb);
1141             break;
1142         }
1143
1144     }
1145
1146     /* Was this the final bucket? If yes, close the temp file and perform
1147      * sanity checks.
1148      */
1149     if (seen_eos) {
1150         const char *cl_header = apr_table_get(r->headers_out, "Content-Length");
1151
1152         apr_file_close(dobj->data.tempfd);
1153
1154         if (r->connection->aborted || r->no_cache) {
1155             ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
1156                          "disk_cache: Discarding body for URL %s "
1157                          "because connection has been aborted.",
1158                          h->cache_obj->key);
1159             /* Remove the intermediate cache file and return non-APR_SUCCESS */
1160             apr_pool_destroy(dobj->data.pool);
1161             return APR_EGENERAL;
1162         }
1163         if (dobj->file_size < conf->minfs) {
1164             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1165                          "disk_cache: URL %s failed the size check "
1166                          "(%" APR_OFF_T_FMT "<%" APR_OFF_T_FMT ")",
1167                          h->cache_obj->key, dobj->file_size, conf->minfs);
1168             /* Remove the intermediate cache file and return non-APR_SUCCESS */
1169             apr_pool_destroy(dobj->data.pool);
1170             return APR_EGENERAL;
1171         }
1172         if (cl_header) {
1173             apr_int64_t cl = apr_atoi64(cl_header);
1174             if ((errno == 0) && (dobj->file_size != cl)) {
1175                 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1176                              "disk_cache: URL %s didn't receive complete response, not caching",
1177                              h->cache_obj->key);
1178                 /* Remove the intermediate cache file and return non-APR_SUCCESS */
1179                 apr_pool_destroy(dobj->data.pool);
1180                 return APR_EGENERAL;
1181             }
1182         }
1183
1184         /* All checks were fine, we're good to go when the commit comes */
1185     }
1186
1187     return APR_SUCCESS;
1188 }
1189
1190 static apr_status_t commit_entity(cache_handle_t *h, request_rec *r)
1191 {
1192     disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
1193                                                  &disk_cache_module);
1194     disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
1195     apr_status_t rv;
1196
1197     /* move header and data tempfiles to the final destination */
1198     rv = file_cache_el_final(conf, &dobj->hdrs, r);
1199     if (APR_SUCCESS == rv) {
1200         rv = file_cache_el_final(conf, &dobj->vary, r);
1201     }
1202     if (APR_SUCCESS == rv) {
1203         rv = file_cache_el_final(conf, &dobj->data, r);
1204     }
1205
1206     /* remove the cached items completely on any failure */
1207     if (APR_SUCCESS != rv) {
1208         remove_url(h, r->pool);
1209         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1210                      "disk_cache: commit_entity: URL '%s' not cached due to earlier disk error.",
1211                      dobj->name);
1212     }
1213     else {
1214         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1215                      "disk_cache: commit_entity: Headers and body for URL %s cached.",
1216                      dobj->name);
1217     }
1218
1219     apr_pool_destroy(dobj->data.pool);
1220
1221     return APR_SUCCESS;
1222 }
1223
1224 static void *create_dir_config(apr_pool_t *p, char *dummy)
1225 {
1226     disk_cache_dir_conf *dconf = apr_pcalloc(p, sizeof(disk_cache_dir_conf));
1227
1228     dconf->readsize = DEFAULT_READSIZE;
1229     dconf->readtime = DEFAULT_READTIME;
1230
1231     return dconf;
1232 }
1233
1234 static void *merge_dir_config(apr_pool_t *p, void *basev, void *addv) {
1235     disk_cache_dir_conf *new = (disk_cache_dir_conf *) apr_pcalloc(p, sizeof(disk_cache_dir_conf));
1236     disk_cache_dir_conf *add = (disk_cache_dir_conf *) addv;
1237     disk_cache_dir_conf *base = (disk_cache_dir_conf *) basev;
1238
1239     new->readsize = (add->readsize_set == 0) ? base->readsize : add->readsize;
1240     new->readsize_set = add->readsize_set || base->readsize_set;
1241     new->readtime = (add->readtime_set == 0) ? base->readtime : add->readtime;
1242     new->readtime_set = add->readtime_set || base->readtime_set;
1243
1244     return new;
1245 }
1246
1247 static void *create_config(apr_pool_t *p, server_rec *s)
1248 {
1249     disk_cache_conf *conf = apr_pcalloc(p, sizeof(disk_cache_conf));
1250
1251     /* XXX: Set default values */
1252     conf->dirlevels = DEFAULT_DIRLEVELS;
1253     conf->dirlength = DEFAULT_DIRLENGTH;
1254     conf->maxfs = DEFAULT_MAX_FILE_SIZE;
1255     conf->minfs = DEFAULT_MIN_FILE_SIZE;
1256
1257     conf->cache_root = NULL;
1258     conf->cache_root_len = 0;
1259
1260     return conf;
1261 }
1262
1263 /*
1264  * mod_disk_cache configuration directives handlers.
1265  */
1266 static const char
1267 *set_cache_root(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1268 {
1269     disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1270                                                  &disk_cache_module);
1271     conf->cache_root = arg;
1272     conf->cache_root_len = strlen(arg);
1273     /* TODO: canonicalize cache_root and strip off any trailing slashes */
1274
1275     return NULL;
1276 }
1277
1278 /*
1279  * Consider eliminating the next two directives in favor of
1280  * Ian's prime number hash...
1281  * key = hash_fn( r->uri)
1282  * filename = "/key % prime1 /key %prime2/key %prime3"
1283  */
1284 static const char
1285 *set_cache_dirlevels(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1286 {
1287     disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1288                                                  &disk_cache_module);
1289     int val = atoi(arg);
1290     if (val < 1)
1291         return "CacheDirLevels value must be an integer greater than 0";
1292     if (val * conf->dirlength > CACHEFILE_LEN)
1293         return "CacheDirLevels*CacheDirLength value must not be higher than 20";
1294     conf->dirlevels = val;
1295     return NULL;
1296 }
1297 static const char
1298 *set_cache_dirlength(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1299 {
1300     disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1301                                                  &disk_cache_module);
1302     int val = atoi(arg);
1303     if (val < 1)
1304         return "CacheDirLength value must be an integer greater than 0";
1305     if (val * conf->dirlevels > CACHEFILE_LEN)
1306         return "CacheDirLevels*CacheDirLength value must not be higher than 20";
1307
1308     conf->dirlength = val;
1309     return NULL;
1310 }
1311
1312 static const char
1313 *set_cache_minfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1314 {
1315     disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1316                                                  &disk_cache_module);
1317
1318     if (apr_strtoff(&conf->minfs, arg, NULL, 0) != APR_SUCCESS ||
1319             conf->minfs < 0) 
1320     {
1321         return "CacheMinFileSize argument must be a non-negative integer representing the min size of a file to cache in bytes.";
1322     }
1323     return NULL;
1324 }
1325
1326 static const char
1327 *set_cache_maxfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1328 {
1329     disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
1330                                                  &disk_cache_module);
1331     if (apr_strtoff(&conf->maxfs, arg, NULL, 0) != APR_SUCCESS ||
1332             conf->maxfs < 0) 
1333     {
1334         return "CacheMaxFileSize argument must be a non-negative integer representing the max size of a file to cache in bytes.";
1335     }
1336     return NULL;
1337 }
1338
1339 static const char
1340 *set_cache_readsize(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1341 {
1342     disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
1343
1344     if (apr_strtoff(&dconf->readsize, arg, NULL, 0) != APR_SUCCESS ||
1345             dconf->readsize < 0)
1346     {
1347         return "CacheReadSize argument must be a non-negative integer representing the max amount of data to cache in go.";
1348     }
1349     dconf->readsize_set = 1;
1350     return NULL;
1351 }
1352
1353 static const char
1354 *set_cache_readtime(cmd_parms *parms, void *in_struct_ptr, const char *arg)
1355 {
1356     disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
1357     apr_off_t milliseconds;
1358
1359     if (apr_strtoff(&milliseconds, arg, NULL, 0) != APR_SUCCESS ||
1360             milliseconds < 0)
1361     {
1362         return "CacheReadTime argument must be a non-negative integer representing the max amount of time taken to cache in go.";
1363     }
1364     dconf->readtime = apr_time_from_msec(milliseconds);
1365     dconf->readtime_set = 1;
1366     return NULL;
1367 }
1368
1369 static const command_rec disk_cache_cmds[] =
1370 {
1371     AP_INIT_TAKE1("CacheRoot", set_cache_root, NULL, RSRC_CONF,
1372                  "The directory to store cache files"),
1373     AP_INIT_TAKE1("CacheDirLevels", set_cache_dirlevels, NULL, RSRC_CONF,
1374                   "The number of levels of subdirectories in the cache"),
1375     AP_INIT_TAKE1("CacheDirLength", set_cache_dirlength, NULL, RSRC_CONF,
1376                   "The number of characters in subdirectory names"),
1377     AP_INIT_TAKE1("CacheMinFileSize", set_cache_minfs, NULL, RSRC_CONF,
1378                   "The minimum file size to cache a document"),
1379     AP_INIT_TAKE1("CacheMaxFileSize", set_cache_maxfs, NULL, RSRC_CONF,
1380                   "The maximum file size to cache a document"),
1381     AP_INIT_TAKE1("CacheReadSize", set_cache_readsize, NULL, RSRC_CONF,
1382                   "The maximum quantity of data to attempt to read and cache in one go"),
1383     AP_INIT_TAKE1("CacheReadTime", set_cache_readtime, NULL, RSRC_CONF,
1384                   "The maximum time taken to attempt to read and cache in go"),
1385     {NULL}
1386 };
1387
1388 static const cache_provider cache_disk_provider =
1389 {
1390     &remove_entity,
1391     &store_headers,
1392     &store_body,
1393     &recall_headers,
1394     &recall_body,
1395     &create_entity,
1396     &open_entity,
1397     &remove_url,
1398     &commit_entity
1399 };
1400
1401 static void disk_cache_register_hook(apr_pool_t *p)
1402 {
1403     /* cache initializer */
1404     ap_register_provider(p, CACHE_PROVIDER_GROUP, "disk", "0",
1405                          &cache_disk_provider);
1406 }
1407
1408 AP_DECLARE_MODULE(disk_cache) = {
1409     STANDARD20_MODULE_STUFF,
1410     create_dir_config,          /* create per-directory config structure */
1411     merge_dir_config,           /* merge per-directory config structures */
1412     create_config,              /* create per-server config structure */
1413     NULL,                       /* merge per-server config structures */
1414     disk_cache_cmds,            /* command apr_table_t */
1415     disk_cache_register_hook    /* register hooks */
1416 };