mod_disk_cache.c revision f4a0825e91eec135b5e41c697439e9a13014fa2c
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/* Licensed to the Apache Software Foundation (ASF) under one or more
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * contributor license agreements. See the NOTICE file distributed with
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * this work for additional information regarding copyright ownership.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * The ASF licenses this file to You under the Apache License, Version 2.0
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * (the "License"); you may not use this file except in compliance with
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * the License. You may obtain a copy of the License at
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * http://www.apache.org/licenses/LICENSE-2.0
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Unless required by applicable law or agreed to in writing, software
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * distributed under the License is distributed on an "AS IS" BASIS,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * See the License for the specific language governing permissions and
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * limitations under the License.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * mod_disk_cache: Disk Based HTTP 1.1 Cache.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Flow to Find the .data file:
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Incoming client requests URI /foo/bar/baz
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Generate <hash> off of /foo/bar/baz
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Open <hash>.header
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Read in <hash>.header file (may contain Format #1 or Format #2)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * If format #1 (Contains a list of Vary Headers):
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Use each header name (from .header) with our request values (headers_in) to
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * regenerate <hash> using HeaderName+HeaderValue+.../foo/bar/baz
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * re-read in <hash>.header (must be format #2)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * read in <hash>.data
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * apr_uint32_t format;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * apr_time_t expire;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * apr_array_t vary_headers (delimited by CRLF)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * disk_cache_info_t (first sizeof(apr_uint32_t) bytes is the format)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * entity name (dobj->name) [length is in disk_cache_info_t->name_len]
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * r->headers_out (delimited by CRLF)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * r->headers_in (delimited by CRLF)
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannmodule AP_MODULE_DECLARE_DATA disk_cache_module;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/* Forward declarations */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *i);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t store_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *in,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t recall_headers(cache_handle_t *h, request_rec *r);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Local static functions
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic char *header_file(apr_pool_t *p, disk_cache_conf *conf,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_object_t *dobj, const char *name)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic char *data_file(apr_pool_t *p, disk_cache_conf *conf,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_object_t *dobj, const char *name)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t mkdir_structure(disk_cache_conf *conf, const char *file, apr_pool_t *pool)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann for (p = (char*)file + conf->cache_root_len + 1;;) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/* htcacheclean may remove directories underneath us.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * So, we'll try renaming three times at a cost of 0.002 seconds.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t safe_file_rename(disk_cache_conf *conf,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann for (i = 0; i < 2 && rv != APR_SUCCESS; i++) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* 1000 micro-seconds aka 0.001 seconds. */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t file_cache_el_final(disk_cache_conf *conf, disk_cache_file_t *file,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* This assumes that the tempfiles are on the same file system
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * as the cache_root. If not, then we need a file copy/move
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * rather than a rename.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* move the file over */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rv = safe_file_rename(conf, file->tempfile, file->file, file->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: rename tempfile to file failed:"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_remove(file->tempfile, file->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t file_cache_temp_cleanup(void *dummy) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_file_t *file = (disk_cache_file_t *)dummy;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* clean up the temporary file */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann apr_file_remove(file->tempfile, file->pool);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannstatic apr_status_t file_cache_create(disk_cache_conf *conf, disk_cache_file_t *file,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file->tempfile = apr_pstrcat(pool, conf->cache_root, AP_TEMPFILE, NULL);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_pool_cleanup_register(pool, file, file_cache_temp_cleanup, apr_pool_cleanup_null);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/* These two functions get and put state information into the data
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * file for an ap_cache_el, this state information will be read
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * and written transparent to clients of this module
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* read the data from the cache file */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rv = apr_file_read_full(fd, &dobj->disk_info, len, &len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Store it away so we can get it later. */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann info->request_time = dobj->disk_info.request_time;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann info->response_time = dobj->disk_info.response_time;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Note that we could optimize this by conditionally doing the palloc
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * depending upon the size. */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann urlbuff = apr_palloc(r->pool, dobj->disk_info.name_len + 1);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rv = apr_file_read_full(fd, urlbuff, len, &len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* check that we have the same URL */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Would strncmp be correct? */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic const char* regen_key(apr_pool_t *p, apr_table_t *headers,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_array_header_t *varray, const char *oldkey)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann const char **elts;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann iov = apr_palloc(p, sizeof(struct iovec) * nvec);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * - Handle multiple-value headers better. (sort them?)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * - Handle Case in-sensitive Values better.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * This isn't the end of the world, since it just lowers the cache
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * hit rate, but it would be nice to fix.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * The majority are case insenstive if they are values (encoding etc).
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Most of rfc2616 is case insensitive on header contents.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * So the better solution may be to identify headers which should be
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * treated case-sensitive?
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * HTTP URI's (3.2.3) [host and scheme are insensitive]
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * HTTP method (5.1.1)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * HTTP-date values (3.3.1)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * 3.7 Media Types [exerpt]
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * The type, subtype, and parameter attribute names are case-
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * insensitive. Parameter values might or might not be case-sensitive,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * depending on the semantics of the parameter name.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * 4.20 Except [exerpt]
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Comparison of expectation values is case-insensitive for unquoted
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * tokens (including the 100-continue token), and is case-sensitive for
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * quoted-string expectation-extensions.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic int array_alphasort(const void *fn1, const void *fn2)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic void tokens_to_array(apr_pool_t *p, const char *data,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann while ((token = ap_get_list_item(p, &data)) != NULL) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *((const char **) apr_array_push(arr)) = token;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Sort it so that "Vary: A, B" and "Vary: B, A" are stored the same. */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann sizeof(char *), array_alphasort);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Hook and mod_cache callback functions
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &disk_cache_module);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* we don't support caching of range requests (yet) */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: URL %s partial content response not cached",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Note, len is -1 if unknown so don't trust it too hard */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: URL %s failed the size check "
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "(%" APR_OFF_T_FMT " > %" APR_OFF_T_FMT ")",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: URL %s failed the size check "
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "(%" APR_OFF_T_FMT " < %" APR_OFF_T_FMT ")",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Allocate and initialize cache_object_t and disk_cache_object_t */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj));
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj));
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Save the cache root */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->data.file = data_file(r->pool, conf, dobj, key);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hdrs.file = header_file(r->pool, conf, dobj, key);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->vary.file = header_file(r->pool, conf, dobj, key);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->disk_info.header_only = r->header_only;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic int open_entity(cache_handle_t *h, request_rec *r, const char *key)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann static int error_logged = 0;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann core_dir_config *coreconf = ap_get_module_config(r->per_dir_config,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Look up entity keyed to 'url' */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Cannot cache files to disk without a CacheRoot specified.");
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Create and init the cache object */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(cache_object_t));
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(disk_cache_object_t));
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Open the headers file */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Save the cache root */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dobj->vary.file = header_file(r->pool, conf, dobj, key);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rc = apr_file_open(&dobj->vary.fd, dobj->vary.file, flags, 0, r->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* read the format from the cache file */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_read_full(dobj->vary.fd, &format, len, &len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_read_full(dobj->vary.fd, &expire, len, &len);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann varray = apr_array_make(r->pool, 5, sizeof(char*));
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: Cannot parse vary header file: %s",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann nkey = regen_key(r->pool, r->headers_in, varray, key);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hdrs.file = header_file(r->pool, conf, dobj, nkey);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rc = apr_file_open(&dobj->hdrs.fd, dobj->hdrs.file, flags, 0, r->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: File '%s' has a version mismatch. File had version: %d.",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* oops, not vary as it turns out */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* This wasn't a Vary Format file, so we must seek to the
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * start of the file again, so that later reads work.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_seek(dobj->hdrs.fd, APR_SET, &offset);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->data.file = data_file(r->pool, conf, dobj, nkey);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Read the bytes to setup the cache_info fields */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rc = file_cache_recall_mydata(dobj->hdrs.fd, info, dobj, r);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: Cannot read header file %s", dobj->hdrs.file);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Is this a cached HEAD request? */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (dobj->disk_info.header_only && !r->header_only) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: HEAD request cached, non-HEAD requested, ignoring: %s",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Open the data file */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* When we are in the quick handler we don't have the per-directory
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * configuration, so this check only takes the global setting of
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * the EnableSendFile directive into account.
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann flags |= AP_SENDFILE_ENABLED(coreconf->enable_sendfile);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rc = apr_file_open(&dobj->data.fd, dobj->data.file, flags, 0, r->pool);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Cannot open data file %s", dobj->data.file);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rc = apr_file_info_get(&finfo, APR_FINFO_SIZE | APR_FINFO_IDENT,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Atomic check - does the body file belong to the header file? */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Initialize the cache_handle callback functions */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Recalled cached URL info header %s", dobj->name);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Oh dear, no luck matching header to the body */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Cached URL info header '%s' didn't match body, ignoring this entry",
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Null out the cache object pointer so next time we start from scratch */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannstatic int remove_url(cache_handle_t *h, apr_pool_t *p)
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Get disk cache object from cache handle */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dobj = (disk_cache_object_t *) h->cache_obj->vobj;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Delete headers file */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Deleting %s from cache.", dobj->hdrs.file);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Will only result in an output if httpd is started with -e debug.
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * For reason see log_error_core for the case s == NULL.
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Failed to delete headers file %s from cache.",
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Delete data file */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Deleting %s from cache.", dobj->data.file);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Will only result in an output if httpd is started with -e debug.
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * For reason see log_error_core for the case s == NULL.
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Failed to delete data file %s from cache.",
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* now delete directories as far as possible up to our cache root */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann str_to_copy = dobj->hdrs.file ? dobj->hdrs.file : dobj->data.file;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* remove filename */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * now walk our way back to the cache root, delete everything
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * in the way as far as possible
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * Note: due to the way we constructed the file names in
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * header_file and data_file, we are guaranteed that the
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * cache_root is suffixed by at least one '/' which will be
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * turned into a terminating null by this loop. Therefore,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * we won't either delete or go above our cache root.
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Deleting directory %s from cache",
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (rc != APR_SUCCESS && !APR_STATUS_IS_ENOENT(rc)) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannstatic apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "Premature end of vary array.");
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* If we've finished reading the array, break out of the loop. */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (w[0] == '\0') {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann *((const char **) apr_array_push(arr)) = apr_pstrdup(r->pool, w);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannstatic apr_status_t store_array(apr_file_t *fd, apr_array_header_t* arr)
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann const char **elts;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann rv = apr_file_writev(fd, (const struct iovec *) &iov, 2,
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann return apr_file_writev(fd, (const struct iovec *) &iov, 1,
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmannstatic apr_status_t read_table(cache_handle_t *handle, request_rec *r,
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* ### What about APR_EOF? */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann "Premature end of cache headers.");
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* Delete terminal (CR?)LF */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* Indeed, the host's '\n':
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann -- whatever the script generates.
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* If we've finished reading the headers, break out of the loop. */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (w[0] == '\0') {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* Chances are that we received an ASCII header text instead of
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann * the expected EBCDIC header lines. Try to auto-detect:
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (apr_isprint(*cp) && !apr_isprint(native))
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (!apr_isprint(*cp) && apr_isprint(native))
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann#endif /*APR_CHARSET_EBCDIC*/
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* if we see a bogus header don't ignore it. Shout and scream */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann while (*l && apr_isspace(*l)) {
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann * Reads headers from a buffer and returns an array of headers.
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann * Returns NULL on file error
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann * This routine tries to deal with too long lines and continuation lines.
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann * @@@: XXX: FIXME: currently the headers are passed thru un-merged.
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann * Is that okay, or should they be collapsed where possible?
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmannstatic apr_status_t recall_headers(cache_handle_t *h, request_rec *r)
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann /* This case should not happen... */
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann "disk_cache: recalling headers; but no header fd for %s", dobj->name);
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann /* Call routine to read the header lines/status line */
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann read_table(h, r, h->resp_hdrs, dobj->hdrs.fd);
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann read_table(h, r, h->req_hdrs, dobj->hdrs.fd);
return APR_SUCCESS;
return APR_SUCCESS;
&amt);
return rv;
&amt);
return rv;
if (r->headers_out) {
if (r->headers_in) {
return APR_SUCCESS;
const char *tmp;
if (tmp) {
return rv;
return rv;
return rv;
return rv;
return rv;
return APR_SUCCESS;
apr_bucket *e;
int seen_eos = 0;
const char *str;
if (APR_BUCKET_IS_EOS(e)) {
if (APR_BUCKET_IS_FLUSH(e)) {
if (APR_BUCKET_IS_METADATA(e)) {
return rv;
if (!length) {
return rv;
return rv;
return rv;
return APR_EGENERAL;
if (seen_eos) {
return APR_EGENERAL;
return APR_EGENERAL;
if (cl_header) {
return APR_EGENERAL;
return APR_SUCCESS;
return APR_SUCCESS;
return dconf;
return new;
return conf;
return NULL;
return NULL;
return NULL;
return "CacheMinFileSize argument must be a non-negative integer representing the min size of a file to cache in bytes.";
return NULL;
return "CacheMaxFileSize argument must be a non-negative integer representing the max size of a file to cache in bytes.";
return NULL;
return "CacheReadSize argument must be a non-negative integer representing the max amount of data to cache in go.";
return NULL;
milliseconds < 0)
return "CacheReadTime argument must be a non-negative integer representing the max amount of time taken to cache in go.";
return NULL;
{NULL}