mod_disk_cache.c revision 4c25fdfa5f370d29e55aea846eb9fe6c1d51ede3
181e56d8b348d301d615ccf5465ae600fee2867berikabele/* Licensed to the Apache Software Foundation (ASF) under one or more
181e56d8b348d301d615ccf5465ae600fee2867berikabele * contributor license agreements. See the NOTICE file distributed with
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * this work for additional information regarding copyright ownership.
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * The ASF licenses this file to You under the Apache License, Version 2.0
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * (the "License"); you may not use this file except in compliance with
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive * the License. You may obtain a copy of the License at
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive *
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive * http://www.apache.org/licenses/LICENSE-2.0
5a58787efeb02a1c3f06569d019ad81fd2efa06end *
96ad5d81ee4a2cc66a4ae19893efc8aa6d06fae7jailletc * Unless required by applicable law or agreed to in writing, software
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim * distributed under the License is distributed on an "AS IS" BASIS,
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * See the License for the specific language governing permissions and
2e545ce2450a9953665f701bb05350f0d3f26275nd * limitations under the License.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen */
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim#include "apr_file_io.h"
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "apr_strings.h"
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen#include "mod_cache.h"
3f08db06526d6901aa08c110b5bc7dde6bc39905nd#include "mod_disk_cache.h"
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim#include "ap_provider.h"
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim#include "util_filter.h"
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "util_script.h"
3f08db06526d6901aa08c110b5bc7dde6bc39905nd#include "util_charset.h"
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim/*
ee649f9236fe7fcf255bbfa11f2cce080f996521sf * mod_disk_cache: Disk Based HTTP 1.1 Cache.
7f5b59ccc63c0c0e3e678a168f09ee6a2f51f9d0nd *
1ac39787115a288f5e848344b1b1e8dccb1c58f1nd * Flow to Find the .data file:
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * Incoming client requests URI /foo/bar/baz
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd * Generate <hash> off of /foo/bar/baz
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd * Open <hash>.header
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * Read in <hash>.header file (may contain Format #1 or Format #2)
c68aa7f213d409d464eaa6b963afb28678548f4frbowen * If format #1 (Contains a list of Vary Headers):
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive * Use each header name (from .header) with our request values (headers_in) to
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * regenerate <hash> using HeaderName+HeaderValue+.../foo/bar/baz
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * re-read in <hash>.header (must be format #2)
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive * read in <hash>.data
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive *
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * Format #1:
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * apr_uint32_t format;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive * apr_time_t expire;
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim * apr_array_t vary_headers (delimited by CRLF)
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl *
5a58787efeb02a1c3f06569d019ad81fd2efa06end * Format #2:
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim * disk_cache_info_t (first sizeof(apr_uint32_t) bytes is the format)
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen * entity name (dobj->name) [length is in disk_cache_info_t->name_len]
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim * r->headers_out (delimited by CRLF)
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim * CRLF
5a58787efeb02a1c3f06569d019ad81fd2efa06end * r->headers_in (delimited by CRLF)
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim * CRLF
5a58787efeb02a1c3f06569d019ad81fd2efa06end */
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim
7add1372edb1ee95a2c4d1314df4c7567bda7c62jimmodule AP_MODULE_DECLARE_DATA disk_cache_module;
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim/* Forward declarations */
5a58787efeb02a1c3f06569d019ad81fd2efa06endstatic int remove_entity(cache_handle_t *h);
5a58787efeb02a1c3f06569d019ad81fd2efa06endstatic apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *i);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic apr_status_t store_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *b);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic apr_status_t recall_headers(cache_handle_t *h, request_rec *r);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_file_t *file);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen/*
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl * Local static functions
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic char *header_file(apr_pool_t *p, disk_cache_conf *conf,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen disk_cache_object_t *dobj, const char *name)
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen{
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive if (!dobj->hashfile) {
181e56d8b348d301d615ccf5465ae600fee2867berikabele dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen conf->dirlength, name);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (dobj->prefix) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->hashfile, CACHE_HEADER_SUFFIX, NULL);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen else {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen CACHE_HEADER_SUFFIX, NULL);
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive }
181e56d8b348d301d615ccf5465ae600fee2867berikabele}
181e56d8b348d301d615ccf5465ae600fee2867berikabele
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slivestatic char *data_file(apr_pool_t *p, disk_cache_conf *conf,
181e56d8b348d301d615ccf5465ae600fee2867berikabele disk_cache_object_t *dobj, const char *name)
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive{
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen if (!dobj->hashfile) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl conf->dirlength, name);
198c35e2b8696f42f3b2a4130f2ae1225b26d2c6humbedooh }
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (dobj->prefix) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl dobj->hashfile, CACHE_DATA_SUFFIX, NULL);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen else {
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen CACHE_DATA_SUFFIX, NULL);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen}
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic apr_status_t mkdir_structure(disk_cache_conf *conf, const char *file, apr_pool_t *pool)
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen{
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_status_t rv;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen char *p;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen for (p = (char*)file + conf->cache_root_len + 1;;) {
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim p = strchr(p, '/');
5a58787efeb02a1c3f06569d019ad81fd2efa06end if (!p)
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen break;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen *p = '\0';
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen rv = apr_dir_make(file,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen APR_UREAD|APR_UWRITE|APR_UEXECUTE, pool);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return rv;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen *p = '/';
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen ++p;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return APR_SUCCESS;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen}
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen/* htcacheclean may remove directories underneath us.
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * So, we'll try renaming three times at a cost of 0.002 seconds.
181e56d8b348d301d615ccf5465ae600fee2867berikabele */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic apr_status_t safe_file_rename(disk_cache_conf *conf,
20189240503ef2c8f5dc6e2248b57faab4b23b5and const char *src, const char *dest,
20189240503ef2c8f5dc6e2248b57faab4b23b5and apr_pool_t *pool)
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen{
20189240503ef2c8f5dc6e2248b57faab4b23b5and apr_status_t rv;
20189240503ef2c8f5dc6e2248b57faab4b23b5and
20189240503ef2c8f5dc6e2248b57faab4b23b5and rv = apr_file_rename(src, dest, pool);
20189240503ef2c8f5dc6e2248b57faab4b23b5and
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl if (rv != APR_SUCCESS) {
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coar int i;
25bc0cc439abc002c56f746a04ae3d6ed15fbd7ehumbedooh
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl for (i = 0; i < 2 && rv != APR_SUCCESS; i++) {
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive /* 1000 micro-seconds aka 0.001 seconds. */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_sleep(1000);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen rv = mkdir_structure(conf, dest, pool);
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen if (rv != APR_SUCCESS)
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen continue;
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf rv = apr_file_rename(src, dest, pool);
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return rv;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen}
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowenstatic apr_status_t file_cache_el_final(disk_cache_object_t *dobj,
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen request_rec *r)
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen{
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen /* move the data over */
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen if (dobj->tfd) {
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen apr_status_t rv;
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive apr_file_close(dobj->tfd);
181e56d8b348d301d615ccf5465ae600fee2867berikabele
181e56d8b348d301d615ccf5465ae600fee2867berikabele /* This assumes that the tempfile is on the same file system
181e56d8b348d301d615ccf5465ae600fee2867berikabele * as the cache_root. If not, then we need a file copy/move
181e56d8b348d301d615ccf5465ae600fee2867berikabele * rather than a rename.
181e56d8b348d301d615ccf5465ae600fee2867berikabele */
181e56d8b348d301d615ccf5465ae600fee2867berikabele rv = apr_file_rename(dobj->tempfile, dobj->datafile, r->pool);
181e56d8b348d301d615ccf5465ae600fee2867berikabele if (rv != APR_SUCCESS) {
181e56d8b348d301d615ccf5465ae600fee2867berikabele ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
181e56d8b348d301d615ccf5465ae600fee2867berikabele "disk_cache: rename tempfile to datafile failed:"
181e56d8b348d301d615ccf5465ae600fee2867berikabele " %s -> %s", dobj->tempfile, dobj->datafile);
181e56d8b348d301d615ccf5465ae600fee2867berikabele apr_file_remove(dobj->tempfile, r->pool);
b9f7b2acbe4a228c3eaeb6293554ca9488330c83rbowen }
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->tfd = NULL;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl return APR_SUCCESS;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive}
181e56d8b348d301d615ccf5465ae600fee2867berikabele
181e56d8b348d301d615ccf5465ae600fee2867berikabelestatic apr_status_t file_cache_errorcleanup(disk_cache_object_t *dobj, request_rec *r)
181e56d8b348d301d615ccf5465ae600fee2867berikabele{
181e56d8b348d301d615ccf5465ae600fee2867berikabele /* Remove the header file and the body file. */
181e56d8b348d301d615ccf5465ae600fee2867berikabele apr_file_remove(dobj->hdrsfile, r->pool);
181e56d8b348d301d615ccf5465ae600fee2867berikabele apr_file_remove(dobj->datafile, r->pool);
181e56d8b348d301d615ccf5465ae600fee2867berikabele
181e56d8b348d301d615ccf5465ae600fee2867berikabele /* If we opened the temporary data file, close and remove it. */
181e56d8b348d301d615ccf5465ae600fee2867berikabele if (dobj->tfd) {
181e56d8b348d301d615ccf5465ae600fee2867berikabele apr_file_close(dobj->tfd);
181e56d8b348d301d615ccf5465ae600fee2867berikabele apr_file_remove(dobj->tempfile, r->pool);
181e56d8b348d301d615ccf5465ae600fee2867berikabele dobj->tfd = NULL;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return APR_SUCCESS;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen}
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl/* These two functions get and put state information into the data
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl * file for an ap_cache_el, this state information will be read
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * and written transparent to clients of this module
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjsl */
2684d5de7d8996ac96df3a37e8f8a49c502f26dfjslstatic int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen disk_cache_object_t *dobj, request_rec *r)
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen{
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_status_t rv;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen char *urlbuff;
f6066dc0a6ad0432b74774e290c04c3cc4aa2dafrbowen disk_cache_info_t disk_info;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive apr_size_t len;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* read the data from the cache file */
181e56d8b348d301d615ccf5465ae600fee2867berikabele len = sizeof(disk_cache_info_t);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen rv = apr_file_read_full(fd, &disk_info, len, &len);
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive if (rv != APR_SUCCESS) {
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim return rv;
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim }
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive /* Store it away so we can get it later. */
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive dobj->disk_info = disk_info;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen info->status = disk_info.status;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen info->date = disk_info.date;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen info->expire = disk_info.expire;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen info->request_time = disk_info.request_time;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen info->response_time = disk_info.response_time;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* Note that we could optimize this by conditionally doing the palloc
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * depending upon the size. */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen urlbuff = apr_palloc(r->pool, disk_info.name_len + 1);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen len = disk_info.name_len;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen rv = apr_file_read_full(fd, urlbuff, len, &len);
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive if (rv != APR_SUCCESS) {
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim return rv;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive }
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive urlbuff[disk_info.name_len] = '\0';
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coar
25bc0cc439abc002c56f746a04ae3d6ed15fbd7ehumbedooh /* check that we have the same URL */
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive /* Would strncmp be correct? */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (strcmp(urlbuff, dobj->name) != 0) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return APR_EGENERAL;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive return APR_SUCCESS;
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive}
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive
7add1372edb1ee95a2c4d1314df4c7567bda7c62jimstatic const char* regen_key(apr_pool_t *p, apr_table_t *headers,
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive apr_array_header_t *varray, const char *oldkey)
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive{
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive struct iovec *iov;
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim int i, k;
5a58787efeb02a1c3f06569d019ad81fd2efa06end int nvec;
5a58787efeb02a1c3f06569d019ad81fd2efa06end const char *header;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive const char **elts;
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen nvec = (varray->nelts * 2) + 1;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov = apr_palloc(p, sizeof(struct iovec) * nvec);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen elts = (const char **) varray->elts;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen /* TODO:
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * - Handle multiple-value headers better. (sort them?)
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive * - Handle Case in-sensitive Values better.
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * This isn't the end of the world, since it just lowers the cache
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * hit rate, but it would be nice to fix.
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen *
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * The majority are case insenstive if they are values (encoding etc).
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen * Most of rfc2616 is case insensitive on header contents.
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen *
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * So the better solution may be to identify headers which should be
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen * treated case-sensitive?
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * HTTP URI's (3.2.3) [host and scheme are insensitive]
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen * HTTP method (5.1.1)
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * HTTP-date values (3.3.1)
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * 3.7 Media Types [exerpt]
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * The type, subtype, and parameter attribute names are case-
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * insensitive. Parameter values might or might not be case-sensitive,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * depending on the semantics of the parameter name.
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * 4.20 Except [exerpt]
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * Comparison of expectation values is case-insensitive for unquoted
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * tokens (including the 100-continue token), and is case-sensitive for
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen * quoted-string expectation-extensions.
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen for(i=0, k=0; i < varray->nelts; i++) {
181e56d8b348d301d615ccf5465ae600fee2867berikabele header = apr_table_get(headers, elts[i]);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (!header) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen header = "";
9fc1345bb54ea7f68c2e59ff3a618c1237a30918yoshiki }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[k].iov_base = (char*) elts[i];
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[k].iov_len = strlen(elts[i]);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen k++;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[k].iov_base = (char*) header;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[k].iov_len = strlen(header);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen k++;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[k].iov_base = (char*) oldkey;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[k].iov_len = strlen(oldkey);
70fce89e67f707c03f70d437a64e189a205ffc35jsl k++;
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl return apr_pstrcatv(p, iov, k, NULL);
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl}
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic int array_alphasort(const void *fn1, const void *fn2)
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen{
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return strcmp(*(char**)fn1, *(char**)fn2);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen}
70fce89e67f707c03f70d437a64e189a205ffc35jsl
f92d094f13138be62bcc1a4b8187d72705d2cb05jslstatic void tokens_to_array(apr_pool_t *p, const char *data,
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl apr_array_header_t *arr)
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl{
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen char *token;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen while ((token = ap_get_list_item(p, &data)) != NULL) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen *((const char **) apr_array_push(arr)) = token;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
70fce89e67f707c03f70d437a64e189a205ffc35jsl
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl /* Sort it so that "Vary: A, B" and "Vary: B, A" are stored the same. */
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl qsort((void *) arr->elts, arr->nelts,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen sizeof(char *), array_alphasort);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen}
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen/*
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * Hook and mod_cache callback functions
70fce89e67f707c03f70d437a64e189a205ffc35jsl */
f92d094f13138be62bcc1a4b8187d72705d2cb05jslstatic int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len)
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl{
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen &disk_cache_module);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen cache_object_t *obj;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen disk_cache_object_t *dobj;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen if (conf->cache_root == NULL) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return DECLINED;
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* Note, len is -1 if unknown so don't trust it too hard */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (len > conf->maxfs) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen "disk_cache: URL %s failed the size check "
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen "(%" APR_OFF_T_FMT " > %" APR_OFF_T_FMT ")",
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen key, len, conf->maxfs);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return DECLINED;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (len >= 0 && len < conf->minfs) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen "disk_cache: URL %s failed the size check "
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen "(%" APR_OFF_T_FMT " < %" APR_OFF_T_FMT ")",
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen key, len, conf->minfs);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return DECLINED;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* Allocate and initialize cache_object_t and disk_cache_object_t */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj));
70fce89e67f707c03f70d437a64e189a205ffc35jsl obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj));
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl obj->key = apr_pstrdup(r->pool, key);
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->name = obj->key;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->prefix = NULL;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* Save the cache root */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->root_len = conf->cache_root_len;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->datafile = data_file(r->pool, conf, dobj, key);
70fce89e67f707c03f70d437a64e189a205ffc35jsl dobj->hdrsfile = header_file(r->pool, conf, dobj, key);
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return OK;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen}
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic int open_entity(cache_handle_t *h, request_rec *r, const char *key)
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen{
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_uint32_t format;
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl apr_size_t len;
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl const char *nkey;
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl apr_status_t rc;
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl static int error_logged = 0;
f92d094f13138be62bcc1a4b8187d72705d2cb05jsl disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen &disk_cache_module);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen#ifdef APR_SENDFILE_ENABLED
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen core_dir_config *coreconf = ap_get_module_config(r->per_dir_config,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen &core_module);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen#endif
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_finfo_t finfo;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen cache_object_t *obj;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen cache_info *info;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen disk_cache_object_t *dobj;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen int flags;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen h->cache_obj = NULL;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* Look up entity keyed to 'url' */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (conf->cache_root == NULL) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (!error_logged) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen error_logged = 1;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen "disk_cache: Cannot cache files to disk without a CacheRoot specified.");
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return DECLINED;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* Create and init the cache object */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(cache_object_t));
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(disk_cache_object_t));
70fce89e67f707c03f70d437a64e189a205ffc35jsl
70fce89e67f707c03f70d437a64e189a205ffc35jsl info = &(obj->info);
70fce89e67f707c03f70d437a64e189a205ffc35jsl
70fce89e67f707c03f70d437a64e189a205ffc35jsl /* Open the headers file */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->prefix = NULL;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* Save the cache root */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->root_len = conf->cache_root_len;
70fce89e67f707c03f70d437a64e189a205ffc35jsl
70fce89e67f707c03f70d437a64e189a205ffc35jsl dobj->hdrsfile = header_file(r->pool, conf, dobj, key);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen flags = APR_READ|APR_BINARY|APR_BUFFERED;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen rc = apr_file_open(&dobj->hfd, dobj->hdrsfile, flags, 0, r->pool);
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim if (rc != APR_SUCCESS) {
5a58787efeb02a1c3f06569d019ad81fd2efa06end return DECLINED;
5a58787efeb02a1c3f06569d019ad81fd2efa06end }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* read the format from the cache file */
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen len = sizeof(format);
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen apr_file_read_full(dobj->hfd, &format, len, &len);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (format == VARY_FORMAT_VERSION) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_array_header_t* varray;
70fce89e67f707c03f70d437a64e189a205ffc35jsl apr_time_t expire;
70fce89e67f707c03f70d437a64e189a205ffc35jsl
70fce89e67f707c03f70d437a64e189a205ffc35jsl len = sizeof(expire);
70fce89e67f707c03f70d437a64e189a205ffc35jsl apr_file_read_full(dobj->hfd, &expire, len, &len);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
181e56d8b348d301d615ccf5465ae600fee2867berikabele varray = apr_array_make(r->pool, 5, sizeof(char*));
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen rc = read_array(r, varray, dobj->hfd);
70fce89e67f707c03f70d437a64e189a205ffc35jsl if (rc != APR_SUCCESS) {
70fce89e67f707c03f70d437a64e189a205ffc35jsl ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
70fce89e67f707c03f70d437a64e189a205ffc35jsl "disk_cache: Cannot parse vary header file: %s",
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->hdrsfile);
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive return DECLINED;
181e56d8b348d301d615ccf5465ae600fee2867berikabele }
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive apr_file_close(dobj->hfd);
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
e2108029921973b4522af1fe5ecd634a2a55ae07slive nkey = regen_key(r->pool, r->headers_in, varray, key);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive dobj->hashfile = NULL;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive dobj->prefix = dobj->hdrsfile;
181e56d8b348d301d615ccf5465ae600fee2867berikabele dobj->hdrsfile = header_file(r->pool, conf, dobj, nkey);
cdf912a26be170e1528d1f6c9db8a2a8f9937420pcs
181e56d8b348d301d615ccf5465ae600fee2867berikabele flags = APR_READ|APR_BINARY|APR_BUFFERED;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive rc = apr_file_open(&dobj->hfd, dobj->hdrsfile, flags, 0, r->pool);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (rc != APR_SUCCESS) {
e2108029921973b4522af1fe5ecd634a2a55ae07slive return DECLINED;
e2108029921973b4522af1fe5ecd634a2a55ae07slive }
181e56d8b348d301d615ccf5465ae600fee2867berikabele }
cdf912a26be170e1528d1f6c9db8a2a8f9937420pcs else if (format != DISK_FORMAT_VERSION) {
181e56d8b348d301d615ccf5465ae600fee2867berikabele ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive "disk_cache: File '%s' has a version mismatch. File had version: %d.",
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->hdrsfile, format);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return DECLINED;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen else {
70fce89e67f707c03f70d437a64e189a205ffc35jsl apr_off_t offset = 0;
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen /* This wasn't a Vary Format file, so we must seek to the
70fce89e67f707c03f70d437a64e189a205ffc35jsl * start of the file again, so that later reads work.
70fce89e67f707c03f70d437a64e189a205ffc35jsl */
70fce89e67f707c03f70d437a64e189a205ffc35jsl apr_file_seek(dobj->hfd, APR_SET, &offset);
70fce89e67f707c03f70d437a64e189a205ffc35jsl nkey = key;
70fce89e67f707c03f70d437a64e189a205ffc35jsl }
70fce89e67f707c03f70d437a64e189a205ffc35jsl
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen obj->key = nkey;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive dobj->key = nkey;
181e56d8b348d301d615ccf5465ae600fee2867berikabele dobj->name = key;
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive dobj->datafile = data_file(r->pool, conf, dobj, nkey);
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
103f414dc9a63f76c48bc1d81162ea6f3c5ef495slive
103f414dc9a63f76c48bc1d81162ea6f3c5ef495slive /* Open the data file */
103f414dc9a63f76c48bc1d81162ea6f3c5ef495slive flags = APR_READ|APR_BINARY;
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive#ifdef APR_SENDFILE_ENABLED
70fce89e67f707c03f70d437a64e189a205ffc35jsl /* When we are in the quick handler we don't have the per-directory
70fce89e67f707c03f70d437a64e189a205ffc35jsl * configuration, so this check only takes the globel setting of
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive * the EnableSendFile directive into account.
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive */
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen flags |= ((coreconf->enable_sendfile == ENABLE_SENDFILE_OFF)
70fce89e67f707c03f70d437a64e189a205ffc35jsl ? 0 : APR_SENDFILE_ENABLED);
70fce89e67f707c03f70d437a64e189a205ffc35jsl#endif
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim rc = apr_file_open(&dobj->fd, dobj->datafile, flags, 0, r->pool);
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive if (rc != APR_SUCCESS) {
70fce89e67f707c03f70d437a64e189a205ffc35jsl ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim "disk_cache: Cannot open info header file %s", dobj->datafile);
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive return DECLINED;
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive }
103f414dc9a63f76c48bc1d81162ea6f3c5ef495slive
103f414dc9a63f76c48bc1d81162ea6f3c5ef495slive rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, dobj->fd);
103f414dc9a63f76c48bc1d81162ea6f3c5ef495slive if (rc == APR_SUCCESS) {
103f414dc9a63f76c48bc1d81162ea6f3c5ef495slive dobj->file_size = finfo.size;
103f414dc9a63f76c48bc1d81162ea6f3c5ef495slive }
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive
2df40fa998d3364133c4dd29eb395f5ae70dfc1fslive /* Read the bytes to setup the cache_info fields */
70fce89e67f707c03f70d437a64e189a205ffc35jsl rc = file_cache_recall_mydata(dobj->hfd, info, dobj, r);
70fce89e67f707c03f70d437a64e189a205ffc35jsl if (rc != APR_SUCCESS) {
70fce89e67f707c03f70d437a64e189a205ffc35jsl ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
70fce89e67f707c03f70d437a64e189a205ffc35jsl "disk_cache: Cannot read header file %s", dobj->hdrsfile);
70fce89e67f707c03f70d437a64e189a205ffc35jsl return DECLINED;
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim }
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen /* Initialize the cache_handle callback functions */
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive "disk_cache: Recalled cached URL info header %s", dobj->name);
e6b1bd85e7da3cac168dfef875298c55829c7ce1nd return OK;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive}
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slivestatic int remove_entity(cache_handle_t *h)
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive{
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive /* Null out the cache object pointer so next time we start from scratch */
70fce89e67f707c03f70d437a64e189a205ffc35jsl h->cache_obj = NULL;
5d81a8bafa2703e966db5bfd5abb33b176d47589nd return OK;
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen}
5d81a8bafa2703e966db5bfd5abb33b176d47589nd
5d81a8bafa2703e966db5bfd5abb33b176d47589ndstatic int remove_url(cache_handle_t *h, apr_pool_t *p)
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim{
5d81a8bafa2703e966db5bfd5abb33b176d47589nd apr_status_t rc;
5d81a8bafa2703e966db5bfd5abb33b176d47589nd disk_cache_object_t *dobj;
5d81a8bafa2703e966db5bfd5abb33b176d47589nd
4aa603e6448b99f9371397d439795c91a93637eand /* Get disk cache object from cache handle */
4aa603e6448b99f9371397d439795c91a93637eand dobj = (disk_cache_object_t *) h->cache_obj->vobj;
25bc0cc439abc002c56f746a04ae3d6ed15fbd7ehumbedooh if (!dobj) {
5d81a8bafa2703e966db5bfd5abb33b176d47589nd return DECLINED;
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim }
5a58787efeb02a1c3f06569d019ad81fd2efa06end
5a58787efeb02a1c3f06569d019ad81fd2efa06end /* Delete headers file */
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf if (dobj->hdrsfile) {
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen "disk_cache: Deleting %s from cache.", dobj->hdrsfile);
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive rc = apr_file_remove(dobj->hdrsfile, p);
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive /* Will only result in an output if httpd is started with -e debug.
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive * For reason see log_error_core for the case s == NULL.
70fce89e67f707c03f70d437a64e189a205ffc35jsl */
70fce89e67f707c03f70d437a64e189a205ffc35jsl ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
70fce89e67f707c03f70d437a64e189a205ffc35jsl "disk_cache: Failed to delete headers file %s from cache.",
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim dobj->hdrsfile);
5a58787efeb02a1c3f06569d019ad81fd2efa06end return DECLINED;
5a58787efeb02a1c3f06569d019ad81fd2efa06end }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* Delete data file */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (dobj->datafile) {
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen "disk_cache: Deleting %s from cache.", dobj->datafile);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen rc = apr_file_remove(dobj->datafile, p);
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive /* Will only result in an output if httpd is started with -e debug.
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive * For reason see log_error_core for the case s == NULL.
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen "disk_cache: Failed to delete data file %s from cache.",
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dobj->datafile);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return DECLINED;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* now delete directories as far as possible up to our cache root */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (dobj->root) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen const char *str_to_copy;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen str_to_copy = dobj->hdrsfile ? dobj->hdrsfile : dobj->datafile;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (str_to_copy) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen char *dir, *slash, *q;
181e56d8b348d301d615ccf5465ae600fee2867berikabele
9fc1345bb54ea7f68c2e59ff3a618c1237a30918yoshiki dir = apr_pstrdup(p, str_to_copy);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /* remove filename */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen slash = strrchr(dir, '/');
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen *slash = '\0';
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen /*
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * now walk our way back to the cache root, delete everything
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * in the way as far as possible
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen *
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * Note: due to the way we constructed the file names in
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * header_file and data_file, we are guaranteed that the
181e56d8b348d301d615ccf5465ae600fee2867berikabele * cache_root is suffixed by at least one '/' which will be
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * turned into a terminating null by this loop. Therefore,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen * we won't either delete or go above our cache root.
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen */
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen for (q = dir + dobj->root_len; *q ; ) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen "disk_cache: Deleting directory %s from cache",
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen dir);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen rc = apr_dir_remove(dir, p);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (rc != APR_SUCCESS && !APR_STATUS_IS_ENOENT(rc)) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen break;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen slash = strrchr(q, '/');
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen *slash = '\0';
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
181e56d8b348d301d615ccf5465ae600fee2867berikabele }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return OK;
181e56d8b348d301d615ccf5465ae600fee2867berikabele}
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_file_t *file)
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen{
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen char w[MAX_STRING_LEN];
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen int p;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_status_t rv;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
181e56d8b348d301d615ccf5465ae600fee2867berikabele while (1) {
181e56d8b348d301d615ccf5465ae600fee2867berikabele rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (rv != APR_SUCCESS) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen "Premature end of vary array.");
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen return rv;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
181e56d8b348d301d615ccf5465ae600fee2867berikabele p = strlen(w);
181e56d8b348d301d615ccf5465ae600fee2867berikabele if (p > 0 && w[p - 1] == '\n') {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (p > 1 && w[p - 2] == CR) {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen w[p - 2] = '\0';
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen else {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen w[p - 1] = '\0';
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
181e56d8b348d301d615ccf5465ae600fee2867berikabele /* If we've finished reading the array, break out of the loop. */
181e56d8b348d301d615ccf5465ae600fee2867berikabele if (w[0] == '\0') {
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen break;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen *((const char **) apr_array_push(arr)) = apr_pstrdup(r->pool, w);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
e884f58207082fa2136d5fc86635c31252338948erikabele return APR_SUCCESS;
e884f58207082fa2136d5fc86635c31252338948erikabele}
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowenstatic apr_status_t store_array(apr_file_t *fd, apr_array_header_t* arr)
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive{
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive int i;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_status_t rv;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen struct iovec iov[2];
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen apr_size_t amt;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive const char **elts;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen elts = (const char **) arr->elts;
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim for (i = 0; i < arr->nelts; i++) {
5a58787efeb02a1c3f06569d019ad81fd2efa06end iov[0].iov_base = (char*) elts[i];
5a58787efeb02a1c3f06569d019ad81fd2efa06end iov[0].iov_len = strlen(elts[i]);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[1].iov_base = CRLF;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[1].iov_len = sizeof(CRLF) - 1;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen rv = apr_file_writev(fd, (const struct iovec *) &iov, 2,
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen &amt);
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen if (rv != APR_SUCCESS) {
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen return rv;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen }
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen }
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[0].iov_base = CRLF;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen iov[0].iov_len = sizeof(CRLF) - 1;
57d0156f7bbd9ea3a72342cf9912aba61d118702rbowen
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim return apr_file_writev(fd, (const struct iovec *) &iov, 1,
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive &amt);
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive}
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slive
dd9f0e560e29dc86fba5f5d4fa5e72cda5cefb16slivestatic apr_status_t read_table(cache_handle_t *handle, request_rec *r,
221e8a19d2201546e7efe590924383c2a7f0fd72slive apr_table_t *table, apr_file_t *file)
cfebc848e619d381e71d40b6f489db4aac180ee5rbowen{
221e8a19d2201546e7efe590924383c2a7f0fd72slive char w[MAX_STRING_LEN];
221e8a19d2201546e7efe590924383c2a7f0fd72slive char *l;
221e8a19d2201546e7efe590924383c2a7f0fd72slive int p;
221e8a19d2201546e7efe590924383c2a7f0fd72slive apr_status_t rv;
221e8a19d2201546e7efe590924383c2a7f0fd72slive
221e8a19d2201546e7efe590924383c2a7f0fd72slive while (1) {
5a58787efeb02a1c3f06569d019ad81fd2efa06end
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd /* ### What about APR_EOF? */
7add1372edb1ee95a2c4d1314df4c7567bda7c62jim rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
ee649f9236fe7fcf255bbfa11f2cce080f996521sf if (rv != APR_SUCCESS) {
7f5b59ccc63c0c0e3e678a168f09ee6a2f51f9d0nd ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1ac39787115a288f5e848344b1b1e8dccb1c58f1nd "Premature end of cache headers.");
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung return rv;
727872d18412fc021f03969b8641810d8896820bhumbedooh }
0d0ba3a410038e179b695446bb149cce6264e0abnd
727872d18412fc021f03969b8641810d8896820bhumbedooh /* Delete terminal (CR?)LF */
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh
0d0ba3a410038e179b695446bb149cce6264e0abnd p = strlen(w);
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh /* Indeed, the host's '\n':
727872d18412fc021f03969b8641810d8896820bhumbedooh '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
0d0ba3a410038e179b695446bb149cce6264e0abnd -- whatever the script generates.
0d0ba3a410038e179b695446bb149cce6264e0abnd */
0d0ba3a410038e179b695446bb149cce6264e0abnd if (p > 0 && w[p - 1] == '\n') {
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh if (p > 1 && w[p - 2] == CR) {
0d0ba3a410038e179b695446bb149cce6264e0abnd w[p - 2] = '\0';
0d0ba3a410038e179b695446bb149cce6264e0abnd }
0d0ba3a410038e179b695446bb149cce6264e0abnd else {
727872d18412fc021f03969b8641810d8896820bhumbedooh w[p - 1] = '\0';
0d0ba3a410038e179b695446bb149cce6264e0abnd }
0d0ba3a410038e179b695446bb149cce6264e0abnd }
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
205f749042ed530040a4f0080dbcb47ceae8a374rjung /* If we've finished reading the headers, break out of the loop. */
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen if (w[0] == '\0') {
0d0ba3a410038e179b695446bb149cce6264e0abnd break;
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd }
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd#if APR_CHARSET_EBCDIC
5a58787efeb02a1c3f06569d019ad81fd2efa06end /* Chances are that we received an ASCII header text instead of
* the expected EBCDIC header lines. Try to auto-detect:
*/
if (!(l = strchr(w, ':'))) {
int maybeASCII = 0, maybeEBCDIC = 0;
unsigned char *cp, native;
apr_size_t inbytes_left, outbytes_left;
for (cp = w; *cp != '\0'; ++cp) {
native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
if (apr_isprint(*cp) && !apr_isprint(native))
++maybeEBCDIC;
if (!apr_isprint(*cp) && apr_isprint(native))
++maybeASCII;
}
if (maybeASCII > maybeEBCDIC) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
r->filename);
inbytes_left = outbytes_left = cp - w;
apr_xlate_conv_buffer(ap_hdrs_from_ascii,
w, &inbytes_left, w, &outbytes_left);
}
}
#endif /*APR_CHARSET_EBCDIC*/
/* if we see a bogus header don't ignore it. Shout and scream */
if (!(l = strchr(w, ':'))) {
return APR_EGENERAL;
}
*l++ = '\0';
while (*l && apr_isspace(*l)) {
++l;
}
apr_table_add(table, w, l);
}
return APR_SUCCESS;
}
/*
* Reads headers from a buffer and returns an array of headers.
* Returns NULL on file error
* This routine tries to deal with too long lines and continuation lines.
* @@@: XXX: FIXME: currently the headers are passed thru un-merged.
* Is that okay, or should they be collapsed where possible?
*/
static apr_status_t recall_headers(cache_handle_t *h, request_rec *r)
{
disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
/* This case should not happen... */
if (!dobj->hfd) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"disk_cache: recalling headers; but no header fd for %s", dobj->name);
return APR_NOTFOUND;
}
h->req_hdrs = apr_table_make(r->pool, 20);
h->resp_hdrs = apr_table_make(r->pool, 20);
/* Call routine to read the header lines/status line */
read_table(h, r, h->resp_hdrs, dobj->hfd);
read_table(h, r, h->req_hdrs, dobj->hfd);
apr_file_close(dobj->hfd);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: Recalled headers for URL %s", dobj->name);
return APR_SUCCESS;
}
static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb)
{
apr_bucket *e;
disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
apr_brigade_insert_file(bb, dobj->fd, 0, dobj->file_size, p);
e = apr_bucket_eos_create(bb->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, e);
return APR_SUCCESS;
}
static apr_status_t store_table(apr_file_t *fd, apr_table_t *table)
{
int i;
apr_status_t rv;
struct iovec iov[4];
apr_size_t amt;
apr_table_entry_t *elts;
elts = (apr_table_entry_t *) apr_table_elts(table)->elts;
for (i = 0; i < apr_table_elts(table)->nelts; ++i) {
if (elts[i].key != NULL) {
iov[0].iov_base = elts[i].key;
iov[0].iov_len = strlen(elts[i].key);
iov[1].iov_base = ": ";
iov[1].iov_len = sizeof(": ") - 1;
iov[2].iov_base = elts[i].val;
iov[2].iov_len = strlen(elts[i].val);
iov[3].iov_base = CRLF;
iov[3].iov_len = sizeof(CRLF) - 1;
rv = apr_file_writev(fd, (const struct iovec *) &iov, 4,
&amt);
if (rv != APR_SUCCESS) {
return rv;
}
}
}
iov[0].iov_base = CRLF;
iov[0].iov_len = sizeof(CRLF) - 1;
rv = apr_file_writev(fd, (const struct iovec *) &iov, 1,
&amt);
return rv;
}
static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *info)
{
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
&disk_cache_module);
apr_status_t rv;
apr_size_t amt;
disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
disk_cache_info_t disk_info;
struct iovec iov[2];
/* This is flaky... we need to manage the cache_info differently */
h->cache_obj->info = *info;
if (r->headers_out) {
const char *tmp;
tmp = apr_table_get(r->headers_out, "Vary");
if (tmp) {
apr_array_header_t* varray;
apr_uint32_t format = VARY_FORMAT_VERSION;
/* If we were initially opened as a vary format, rollback
* that internal state for the moment so we can recreate the
* vary format hints in the appropriate directory.
*/
if (dobj->prefix) {
dobj->hdrsfile = dobj->prefix;
dobj->prefix = NULL;
}
rv = mkdir_structure(conf, dobj->hdrsfile, r->pool);
rv = apr_file_mktemp(&dobj->tfd, dobj->tempfile,
APR_CREATE | APR_WRITE | APR_BINARY | APR_EXCL,
r->pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: could not create temp file %s",
dobj->tempfile);
return rv;
}
amt = sizeof(format);
apr_file_write(dobj->tfd, &format, &amt);
amt = sizeof(info->expire);
apr_file_write(dobj->tfd, &info->expire, &amt);
varray = apr_array_make(r->pool, 6, sizeof(char*));
tokens_to_array(r->pool, tmp, varray);
store_array(dobj->tfd, varray);
apr_file_close(dobj->tfd);
dobj->tfd = NULL;
rv = safe_file_rename(conf, dobj->tempfile, dobj->hdrsfile,
r->pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: rename tempfile to varyfile failed: %s -> %s",
dobj->tempfile, dobj->hdrsfile);
apr_file_remove(dobj->tempfile, r->pool);
return rv;
}
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
tmp = regen_key(r->pool, r->headers_in, varray, dobj->name);
dobj->prefix = dobj->hdrsfile;
dobj->hashfile = NULL;
dobj->datafile = data_file(r->pool, conf, dobj, tmp);
dobj->hdrsfile = header_file(r->pool, conf, dobj, tmp);
}
}
rv = apr_file_mktemp(&dobj->hfd, dobj->tempfile,
APR_CREATE | APR_WRITE | APR_BINARY |
APR_BUFFERED | APR_EXCL, r->pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: could not create temp file %s",
dobj->tempfile);
return rv;
}
disk_info.format = DISK_FORMAT_VERSION;
disk_info.date = info->date;
disk_info.expire = info->expire;
disk_info.entity_version = dobj->disk_info.entity_version++;
disk_info.request_time = info->request_time;
disk_info.response_time = info->response_time;
disk_info.status = info->status;
disk_info.name_len = strlen(dobj->name);
iov[0].iov_base = (void*)&disk_info;
iov[0].iov_len = sizeof(disk_cache_info_t);
iov[1].iov_base = (void*)dobj->name;
iov[1].iov_len = disk_info.name_len;
rv = apr_file_writev(dobj->hfd, (const struct iovec *) &iov, 2, &amt);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: could not write info to header file %s",
dobj->hdrsfile);
return rv;
}
if (r->headers_out) {
apr_table_t *headers_out;
headers_out = ap_cache_cacheable_headers_out(r);
rv = store_table(dobj->hfd, headers_out);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: could not write out-headers to header file %s",
dobj->hdrsfile);
return rv;
}
}
/* Parse the vary header and dump those fields from the headers_in. */
/* FIXME: Make call to the same thing cache_select calls to crack Vary. */
if (r->headers_in) {
apr_table_t *headers_in;
headers_in = ap_cache_cacheable_headers_in(r);
rv = store_table(dobj->hfd, headers_in);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: could not write in-headers to header file %s",
dobj->hdrsfile);
return rv;
}
}
apr_file_close(dobj->hfd); /* flush and close */
/* Remove old file with the same name. If remove fails, then
* perhaps we need to create the directory tree where we are
* about to write the new headers file.
*/
rv = apr_file_remove(dobj->hdrsfile, r->pool);
if (rv != APR_SUCCESS) {
rv = mkdir_structure(conf, dobj->hdrsfile, r->pool);
}
rv = safe_file_rename(conf, dobj->tempfile, dobj->hdrsfile, r->pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: rename tempfile to hdrsfile failed: %s -> %s",
dobj->tempfile, dobj->hdrsfile);
apr_file_remove(dobj->tempfile, r->pool);
return rv;
}
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: Stored headers for URL %s", dobj->name);
return APR_SUCCESS;
}
static apr_status_t store_body(cache_handle_t *h, request_rec *r,
apr_bucket_brigade *bb)
{
apr_bucket *e;
apr_status_t rv;
disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
&disk_cache_module);
/* We write to a temp file and then atomically rename the file over
* in file_cache_el_final().
*/
if (!dobj->tfd) {
rv = apr_file_mktemp(&dobj->tfd, dobj->tempfile,
APR_CREATE | APR_WRITE | APR_BINARY |
APR_BUFFERED | APR_EXCL, r->pool);
if (rv != APR_SUCCESS) {
return rv;
}
dobj->file_size = 0;
}
for (e = APR_BRIGADE_FIRST(bb);
e != APR_BRIGADE_SENTINEL(bb);
e = APR_BUCKET_NEXT(e))
{
const char *str;
apr_size_t length, written;
rv = apr_bucket_read(e, &str, &length, APR_BLOCK_READ);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"disk_cache: Error when reading bucket for URL %s",
h->cache_obj->key);
/* Remove the intermediate cache file and return non-APR_SUCCESS */
file_cache_errorcleanup(dobj, r);
return rv;
}
rv = apr_file_write_full(dobj->tfd, str, length, &written);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"disk_cache: Error when writing cache file for URL %s",
h->cache_obj->key);
/* Remove the intermediate cache file and return non-APR_SUCCESS */
file_cache_errorcleanup(dobj, r);
return rv;
}
dobj->file_size += written;
if (dobj->file_size > conf->maxfs) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: URL %s failed the size check "
"(%" APR_OFF_T_FMT ">%" APR_OFF_T_FMT ")",
h->cache_obj->key, dobj->file_size, conf->maxfs);
/* Remove the intermediate cache file and return non-APR_SUCCESS */
file_cache_errorcleanup(dobj, r);
return APR_EGENERAL;
}
}
/* Was this the final bucket? If yes, close the temp file and perform
* sanity checks.
*/
if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
const char *cl_header = apr_table_get(r->headers_out, "Content-Length");
if (r->connection->aborted || r->no_cache) {
ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
"disk_cache: Discarding body for URL %s "
"because connection has been aborted.",
h->cache_obj->key);
/* Remove the intermediate cache file and return non-APR_SUCCESS */
file_cache_errorcleanup(dobj, r);
return APR_EGENERAL;
}
if (dobj->file_size < conf->minfs) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: URL %s failed the size check "
"(%" APR_OFF_T_FMT "<%" APR_OFF_T_FMT ")",
h->cache_obj->key, dobj->file_size, conf->minfs);
/* Remove the intermediate cache file and return non-APR_SUCCESS */
file_cache_errorcleanup(dobj, r);
return APR_EGENERAL;
}
if (cl_header > 0) {
apr_size_t cl = (apr_size_t) apr_atoi64(cl_header);
if ((errno == 0) && (dobj->file_size != cl)) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: URL %s didn't receive complete response, not caching",
h->cache_obj->key);
/* Remove the intermediate cache file and return non-APR_SUCCESS */
file_cache_errorcleanup(dobj, r);
return APR_EGENERAL;
}
}
/* All checks were fine. Move tempfile to final destination */
/* Link to the perm file, and close the descriptor */
file_cache_el_final(dobj, r);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: Body for URL %s cached.", dobj->name);
}
return APR_SUCCESS;
}
static void *create_config(apr_pool_t *p, server_rec *s)
{
disk_cache_conf *conf = apr_pcalloc(p, sizeof(disk_cache_conf));
/* XXX: Set default values */
conf->dirlevels = DEFAULT_DIRLEVELS;
conf->dirlength = DEFAULT_DIRLENGTH;
conf->maxfs = DEFAULT_MAX_FILE_SIZE;
conf->minfs = DEFAULT_MIN_FILE_SIZE;
conf->cache_root = NULL;
conf->cache_root_len = 0;
return conf;
}
/*
* mod_disk_cache configuration directives handlers.
*/
static const char
*set_cache_root(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
conf->cache_root = arg;
conf->cache_root_len = strlen(arg);
/* TODO: canonicalize cache_root and strip off any trailing slashes */
return NULL;
}
/*
* Consider eliminating the next two directives in favor of
* Ian's prime number hash...
* key = hash_fn( r->uri)
* filename = "/key % prime1 /key %prime2/key %prime3"
*/
static const char
*set_cache_dirlevels(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
int val = atoi(arg);
if (val < 1)
return "CacheDirLevels value must be an integer greater than 0";
if (val * conf->dirlength > CACHEFILE_LEN)
return "CacheDirLevels*CacheDirLength value must not be higher than 20";
conf->dirlevels = val;
return NULL;
}
static const char
*set_cache_dirlength(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
int val = atoi(arg);
if (val < 1)
return "CacheDirLength value must be an integer greater than 0";
if (val * conf->dirlevels > CACHEFILE_LEN)
return "CacheDirLevels*CacheDirLength value must not be higher than 20";
conf->dirlength = val;
return NULL;
}
static const char
*set_cache_minfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
if (apr_strtoff(&conf->minfs, arg, NULL, 0) != APR_SUCCESS ||
conf->minfs < 0)
{
return "CacheMinFileSize argument must be a non-negative integer representing the min size of a file to cache in bytes.";
}
return NULL;
}
static const char
*set_cache_maxfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
if (apr_strtoff(&conf->maxfs, arg, NULL, 0) != APR_SUCCESS ||
conf->maxfs < 0)
{
return "CacheMaxFileSize argument must be a non-negative integer representing the max size of a file to cache in bytes.";
}
return NULL;
}
static const command_rec disk_cache_cmds[] =
{
AP_INIT_TAKE1("CacheRoot", set_cache_root, NULL, RSRC_CONF,
"The directory to store cache files"),
AP_INIT_TAKE1("CacheDirLevels", set_cache_dirlevels, NULL, RSRC_CONF,
"The number of levels of subdirectories in the cache"),
AP_INIT_TAKE1("CacheDirLength", set_cache_dirlength, NULL, RSRC_CONF,
"The number of characters in subdirectory names"),
AP_INIT_TAKE1("CacheMinFileSize", set_cache_minfs, NULL, RSRC_CONF,
"The minimum file size to cache a document"),
AP_INIT_TAKE1("CacheMaxFileSize", set_cache_maxfs, NULL, RSRC_CONF,
"The maximum file size to cache a document"),
{NULL}
};
static const cache_provider cache_disk_provider =
{
&remove_entity,
&store_headers,
&store_body,
&recall_headers,
&recall_body,
&create_entity,
&open_entity,
&remove_url,
};
static void disk_cache_register_hook(apr_pool_t *p)
{
/* cache initializer */
ap_register_provider(p, CACHE_PROVIDER_GROUP, "disk", "0",
&cache_disk_provider);
}
module AP_MODULE_DECLARE_DATA disk_cache_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
create_config, /* create per-server config structure */
NULL, /* merge per-server config structures */
disk_cache_cmds, /* command apr_table_t */
disk_cache_register_hook /* register hooks */
};