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 *
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * http://www.apache.org/licenses/LICENSE-2.0
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *
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 */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "apr_lib.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "apr_file_io.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "apr_strings.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "mod_cache.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "mod_disk_cache.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "http_config.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "http_log.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "http_core.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "ap_provider.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "util_filter.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "util_script.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#include "util_charset.h"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/*
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * mod_disk_cache: Disk Based HTTP 1.1 Cache.
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *
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 *
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Format #1:
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * apr_uint32_t format;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * apr_time_t expire;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * apr_array_t vary_headers (delimited by CRLF)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * Format #2:
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 * CRLF
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * r->headers_in (delimited by CRLF)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * CRLF
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannmodule AP_MODULE_DECLARE_DATA disk_cache_module;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/* Forward declarations */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic int remove_entity(cache_handle_t *h);
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 Holtmann apr_bucket_brigade *out);
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 apr_file_t *file);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/*
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Local static functions
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic char *header_file(apr_pool_t *p, disk_cache_conf *conf,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_object_t *dobj, const char *name)
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (!dobj->hashfile) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann conf->dirlength, name);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (dobj->prefix) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hashfile, CACHE_HEADER_SUFFIX, NULL);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann else {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann CACHE_HEADER_SUFFIX, NULL);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (!dobj->hashfile) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann conf->dirlength, name);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (dobj->prefix) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hashfile, CACHE_DATA_SUFFIX, NULL);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann else {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann CACHE_DATA_SUFFIX, NULL);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t mkdir_structure(disk_cache_conf *conf, const char *file, apr_pool_t *pool)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_status_t rv;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann char *p;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann for (p = (char*)file + conf->cache_root_len + 1;;) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann p = strchr(p, '/');
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (!p)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann break;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *p = '\0';
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rv = apr_dir_make(file,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann APR_UREAD|APR_UWRITE|APR_UEXECUTE, pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return rv;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *p = '/';
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ++p;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return APR_SUCCESS;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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 Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t safe_file_rename(disk_cache_conf *conf,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann const char *src, const char *dest,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_pool_t *pool)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_status_t rv;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rv = apr_file_rename(src, dest, pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rv != APR_SUCCESS) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann int i;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann for (i = 0; i < 2 && rv != APR_SUCCESS; i++) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* 1000 micro-seconds aka 0.001 seconds. */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_sleep(1000);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rv = mkdir_structure(conf, dest, pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rv != APR_SUCCESS)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann continue;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rv = apr_file_rename(src, dest, pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return rv;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic apr_status_t file_cache_el_final(disk_cache_conf *conf, disk_cache_file_t *file,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann request_rec *r)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_status_t rv = APR_SUCCESS;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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 */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* move the file over */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (file->tempfd) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rv = safe_file_rename(conf, file->tempfile, file->file, file->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rv != APR_SUCCESS) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: rename tempfile to file failed:"
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann " %s -> %s", file->tempfile, file->file);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_remove(file->tempfile, file->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file->tempfd = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return rv;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* clean up the temporary file */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (file->tempfd) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann apr_file_remove(file->tempfile, file->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file->tempfd = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann file->tempfile = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file->pool = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return APR_SUCCESS;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannstatic apr_status_t file_cache_create(disk_cache_conf *conf, disk_cache_file_t *file,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_pool_t *pool)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file->pool = pool;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file->tempfile = apr_pstrcat(pool, conf->cache_root, AP_TEMPFILE, NULL);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_pool_cleanup_register(pool, file, file_cache_temp_cleanup, apr_pool_cleanup_null);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return APR_SUCCESS;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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 Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_object_t *dobj, request_rec *r)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_status_t rv;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann char *urlbuff;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_size_t len;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* read the data from the cache file */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann len = sizeof(disk_cache_info_t);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rv = apr_file_read_full(fd, &dobj->disk_info, len, &len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rv != APR_SUCCESS) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return rv;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Store it away so we can get it later. */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann info->status = dobj->disk_info.status;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann info->date = dobj->disk_info.date;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann info->expire = dobj->disk_info.expire;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann info->request_time = dobj->disk_info.request_time;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann info->response_time = dobj->disk_info.response_time;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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 len = dobj->disk_info.name_len;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rv = apr_file_read_full(fd, urlbuff, len, &len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rv != APR_SUCCESS) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return rv;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann urlbuff[dobj->disk_info.name_len] = '\0';
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* check that we have the same URL */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Would strncmp be correct? */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (strcmp(urlbuff, dobj->name) != 0) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return APR_EGENERAL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return APR_SUCCESS;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann struct iovec *iov;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann int i, k;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann int nvec;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann const char *header;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann const char **elts;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann nvec = (varray->nelts * 2) + 1;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann iov = apr_palloc(p, sizeof(struct iovec) * nvec);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann elts = (const char **) varray->elts;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* TODO:
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 *
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 *
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 Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann for(i=0, k=0; i < varray->nelts; i++) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann header = apr_table_get(headers, elts[i]);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (!header) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann header = "";
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann iov[k].iov_base = (char*) elts[i];
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann iov[k].iov_len = strlen(elts[i]);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann k++;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann iov[k].iov_base = (char*) header;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann iov[k].iov_len = strlen(header);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann k++;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann iov[k].iov_base = (char*) oldkey;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann iov[k].iov_len = strlen(oldkey);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann k++;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return apr_pstrcatv(p, iov, k, NULL);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic int array_alphasort(const void *fn1, const void *fn2)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return strcmp(*(char**)fn1, *(char**)fn2);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic void tokens_to_array(apr_pool_t *p, const char *data,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_array_header_t *arr)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann char *token;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann while ((token = ap_get_list_item(p, &data)) != NULL) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann *((const char **) apr_array_push(arr)) = token;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Sort it so that "Vary: A, B" and "Vary: B, A" are stored the same. */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann qsort((void *) arr->elts, arr->nelts,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann sizeof(char *), array_alphasort);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann/*
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann * Hook and mod_cache callback functions
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_bucket_brigade *bb)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
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 &disk_cache_module);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann cache_object_t *obj;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_object_t *dobj;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_pool_t *pool;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (conf->cache_root == NULL) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* we don't support caching of range requests (yet) */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (r->status == HTTP_PARTIAL_CONTENT) {
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 key);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Note, len is -1 if unknown so don't trust it too hard */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (len > dconf->maxfs) {
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 key, len, dconf->maxfs);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (len >= 0 && len < dconf->minfs) {
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 ")",
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann key, len, dconf->minfs);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann return DECLINED;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann obj->key = apr_pstrdup(r->pool, key);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->name = obj->key;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->prefix = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Save the cache root */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->root_len = conf->cache_root_len;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann apr_pool_create(&pool, r->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file_cache_create(conf, &dobj->hdrs, pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file_cache_create(conf, &dobj->vary, pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file_cache_create(conf, &dobj->data, pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->disk_info.header_only = r->header_only;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return OK;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann}
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmannstatic int open_entity(cache_handle_t *h, request_rec *r, const char *key)
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann{
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_uint32_t format;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_size_t len;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann const char *nkey;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_status_t rc;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann static int error_logged = 0;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann &disk_cache_module);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#ifdef APR_SENDFILE_ENABLED
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann core_dir_config *coreconf = ap_get_module_config(r->per_dir_config,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann &core_module);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann#endif
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_finfo_t finfo;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann cache_object_t *obj;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann cache_info *info;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann disk_cache_object_t *dobj;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann int flags;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_pool_t *pool;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann h->cache_obj = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Look up entity keyed to 'url' */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (conf->cache_root == NULL) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (!error_logged) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann error_logged = 1;
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 }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann info = &(obj->info);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Open the headers file */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dobj->prefix = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Save the cache root */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->root_len = conf->cache_root_len;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dobj->vary.file = header_file(r->pool, conf, dobj, key);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann flags = APR_READ|APR_BINARY|APR_BUFFERED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rc = apr_file_open(&dobj->vary.fd, dobj->vary.file, flags, 0, r->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rc != APR_SUCCESS) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* read the format from the cache file */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann len = sizeof(format);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_read_full(dobj->vary.fd, &format, len, &len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (format == VARY_FORMAT_VERSION) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_array_header_t* varray;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_time_t expire;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann len = sizeof(expire);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_read_full(dobj->vary.fd, &expire, len, &len);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann varray = apr_array_make(r->pool, 5, sizeof(char*));
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rc = read_array(r, varray, dobj->vary.fd);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rc != APR_SUCCESS) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann "disk_cache: Cannot parse vary header file: %s",
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->vary.file);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_close(dobj->vary.fd);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_close(dobj->vary.fd);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann nkey = regen_key(r->pool, r->headers_in, varray, key);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hashfile = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->prefix = dobj->vary.file;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hdrs.file = header_file(r->pool, conf, dobj, nkey);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann flags = APR_READ|APR_BINARY|APR_BUFFERED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann rc = apr_file_open(&dobj->hdrs.fd, dobj->hdrs.file, flags, 0, r->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (rc != APR_SUCCESS) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann else if (format != DISK_FORMAT_VERSION) {
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 dobj->vary.file, format);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_close(dobj->vary.fd);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann else {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_off_t offset = 0;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* oops, not vary as it turns out */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hdrs.fd = dobj->vary.fd;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->vary.fd = NULL;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->hdrs.file = dobj->vary.file;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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 */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_seek(dobj->hdrs.fd, APR_SET, &offset);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann nkey = key;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann obj->key = nkey;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->key = nkey;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->name = key;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_pool_create(&pool, r->pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file_cache_create(conf, &dobj->hdrs, pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file_cache_create(conf, &dobj->vary, pool);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann file_cache_create(conf, &dobj->data, pool);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann dobj->data.file = data_file(r->pool, conf, dobj, nkey);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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 if (rc != APR_SUCCESS) {
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 apr_file_close(dobj->hdrs.fd);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann apr_file_close(dobj->hdrs.fd);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
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 dobj->hdrs.file);
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann return DECLINED;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann }
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann /* Open the data file */
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann if (dobj->disk_info.has_body) {
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann flags = APR_READ | APR_BINARY;
94f5bbc626f2a4102debd9b17c964170a887cb49Marcel Holtmann#ifdef APR_SENDFILE_ENABLED
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 */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann flags |= AP_SENDFILE_ENABLED(coreconf->enable_sendfile);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann#endif
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rc = apr_file_open(&dobj->data.fd, dobj->data.file, flags, 0, r->pool);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (rc != APR_SUCCESS) {
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 apr_file_close(dobj->hdrs.fd);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return DECLINED;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rc = apr_file_info_get(&finfo, APR_FINFO_SIZE | APR_FINFO_IDENT,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dobj->data.fd);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (rc == APR_SUCCESS) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dobj->file_size = finfo.size;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Atomic check - does the body file belong to the header file? */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (dobj->disk_info.inode == finfo.inode &&
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dobj->disk_info.device == finfo.device) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
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
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return OK;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann else {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return OK;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
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 dobj->name);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return DECLINED;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann}
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannstatic int remove_entity(cache_handle_t *h)
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann{
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Null out the cache object pointer so next time we start from scratch */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann h->cache_obj = NULL;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return OK;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann}
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannstatic int remove_url(cache_handle_t *h, apr_pool_t *p)
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann{
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann apr_status_t rc;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann disk_cache_object_t *dobj;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Get disk cache object from cache handle */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dobj = (disk_cache_object_t *) h->cache_obj->vobj;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (!dobj) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return DECLINED;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Delete headers file */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (dobj->hdrs.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
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rc = apr_file_remove(dobj->hdrs.file, p);
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 */
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 dobj->hdrs.file);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return DECLINED;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* Delete data file */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (dobj->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
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rc = apr_file_remove(dobj->data.file, p);
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 */
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 dobj->data.file);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return DECLINED;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* now delete directories as far as possible up to our cache root */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (dobj->root) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann const char *str_to_copy;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann str_to_copy = dobj->hdrs.file ? dobj->hdrs.file : dobj->data.file;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (str_to_copy) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann char *dir, *slash, *q;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dir = apr_pstrdup(p, str_to_copy);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* remove filename */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann slash = strrchr(dir, '/');
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann *slash = '\0';
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /*
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * now walk our way back to the cache root, delete everything
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann * in the way as far as possible
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann *
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 */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann for (q = dir + dobj->root_len; *q ; ) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "disk_cache: Deleting directory %s from cache",
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann dir);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rc = apr_dir_remove(dir, p);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (rc != APR_SUCCESS && !APR_STATUS_IS_ENOENT(rc)) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann break;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann slash = strrchr(q, '/');
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann *slash = '\0';
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return OK;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann}
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannstatic apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann apr_file_t *file)
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann{
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann char w[MAX_STRING_LEN];
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann int p;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann apr_status_t rv;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann while (1) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (rv != APR_SUCCESS) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann "Premature end of vary array.");
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return rv;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann p = strlen(w);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (p > 0 && w[p - 1] == '\n') {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (p > 1 && w[p - 2] == CR) {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann w[p - 2] = '\0';
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann else {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann w[p - 1] = '\0';
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann /* If we've finished reading the array, break out of the loop. */
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann if (w[0] == '\0') {
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann break;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann *((const char **) apr_array_push(arr)) = apr_pstrdup(r->pool, w);
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann }
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann return APR_SUCCESS;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann}
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmannstatic apr_status_t store_array(apr_file_t *fd, apr_array_header_t* arr)
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann{
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann int i;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann apr_status_t rv;
5476ad087c0f2d45ab0dab1bab9ef3e9d70418bcMarcel Holtmann struct iovec iov[2];
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann apr_size_t amt;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann const char **elts;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann elts = (const char **) arr->elts;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann for (i = 0; i < arr->nelts; i++) {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann iov[0].iov_base = (char*) elts[i];
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann iov[0].iov_len = strlen(elts[i]);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann iov[1].iov_base = CRLF;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann iov[1].iov_len = sizeof(CRLF) - 1;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann rv = apr_file_writev(fd, (const struct iovec *) &iov, 2,
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann &amt);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (rv != APR_SUCCESS) {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann return rv;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann iov[0].iov_base = CRLF;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann iov[0].iov_len = sizeof(CRLF) - 1;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann return apr_file_writev(fd, (const struct iovec *) &iov, 1,
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann &amt);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann}
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmannstatic apr_status_t read_table(cache_handle_t *handle, request_rec *r,
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann apr_table_t *table, apr_file_t *file)
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann{
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann char w[MAX_STRING_LEN];
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann char *l;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann int p;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann apr_status_t rv;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann while (1) {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* ### What about APR_EOF? */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (rv != APR_SUCCESS) {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann "Premature end of cache headers.");
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann return rv;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* Delete terminal (CR?)LF */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann p = strlen(w);
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 */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (p > 0 && w[p - 1] == '\n') {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (p > 1 && w[p - 2] == CR) {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann w[p - 2] = '\0';
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann else {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann w[p - 1] = '\0';
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* If we've finished reading the headers, break out of the loop. */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (w[0] == '\0') {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann break;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann#if APR_CHARSET_EBCDIC
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 */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (!(l = strchr(w, ':'))) {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann int maybeASCII = 0, maybeEBCDIC = 0;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann unsigned char *cp, native;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann apr_size_t inbytes_left, outbytes_left;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann for (cp = w; *cp != '\0'; ++cp) {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (apr_isprint(*cp) && !apr_isprint(native))
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann ++maybeEBCDIC;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (!apr_isprint(*cp) && apr_isprint(native))
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann ++maybeASCII;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (maybeASCII > maybeEBCDIC) {
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 r->filename);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann inbytes_left = outbytes_left = cp - w;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann apr_xlate_conv_buffer(ap_hdrs_from_ascii,
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann w, &inbytes_left, w, &outbytes_left);
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann#endif /*APR_CHARSET_EBCDIC*/
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann /* if we see a bogus header don't ignore it. Shout and scream */
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann if (!(l = strchr(w, ':'))) {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann return APR_EGENERAL;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann *l++ = '\0';
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann while (*l && apr_isspace(*l)) {
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann ++l;
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann }
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann
c6f3f5b4b3ec439ae911ca0644237d96fd31893eMarcel Holtmann apr_table_add(table, w, l);
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann }
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann return APR_SUCCESS;
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann}
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann/*
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 Holtmann */
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmannstatic apr_status_t recall_headers(cache_handle_t *h, request_rec *r)
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann{
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann /* This case should not happen... */
0234c599218518b1eb478d64d9883ba46e4ce5d8Marcel Holtmann if (!dobj->hdrs.fd) {
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 return APR_NOTFOUND;
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann }
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann h->req_hdrs = apr_table_make(r->pool, 20);
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann h->resp_hdrs = apr_table_make(r->pool, 20);
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann
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);
c4a3a17ee919e8b1197328394709edfe36d8b710Marcel Holtmann
apr_file_close(dobj->hdrs.fd);
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)
{
disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
if (dobj->data.fd) {
apr_bucket *e = apr_bucket_file_create(dobj->data.fd, 0,
dobj->file_size, p, bb->bucket_alloc);
APR_BRIGADE_INSERT_HEAD(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_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
memcpy(&h->cache_obj->info, info, sizeof(cache_info));
if (r->headers_out) {
dobj->headers_out = ap_cache_cacheable_headers_out(r);
}
if (r->headers_in) {
dobj->headers_in = ap_cache_cacheable_headers_in(r);
}
return APR_SUCCESS;
}
static apr_status_t write_headers(cache_handle_t *h, request_rec *r)
{
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];
memset(&disk_info, 0, sizeof(disk_cache_info_t));
if (dobj->headers_out) {
const char *tmp;
tmp = apr_table_get(dobj->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->hdrs.file = dobj->prefix;
dobj->prefix = NULL;
}
rv = mkdir_structure(conf, dobj->hdrs.file, r->pool);
rv = apr_file_mktemp(&dobj->vary.tempfd, dobj->vary.tempfile,
APR_CREATE | APR_WRITE | APR_BINARY | APR_EXCL,
dobj->vary.pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: could not create temp file %s",
dobj->vary.tempfile);
return rv;
}
amt = sizeof(format);
apr_file_write(dobj->vary.tempfd, &format, &amt);
amt = sizeof(h->cache_obj->info.expire);
apr_file_write(dobj->vary.tempfd, &h->cache_obj->info.expire, &amt);
varray = apr_array_make(r->pool, 6, sizeof(char*));
tokens_to_array(r->pool, tmp, varray);
store_array(dobj->vary.tempfd, varray);
apr_file_close(dobj->vary.tempfd);
tmp = regen_key(r->pool, dobj->headers_in, varray, dobj->name);
dobj->prefix = dobj->hdrs.file;
dobj->hashfile = NULL;
dobj->data.file = data_file(r->pool, conf, dobj, tmp);
dobj->hdrs.file = header_file(r->pool, conf, dobj, tmp);
}
}
rv = apr_file_mktemp(&dobj->hdrs.tempfd, dobj->hdrs.tempfile,
APR_CREATE | APR_WRITE | APR_BINARY |
APR_BUFFERED | APR_EXCL, dobj->hdrs.pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: could not create temp file %s",
dobj->hdrs.tempfile);
return rv;
}
disk_info.format = DISK_FORMAT_VERSION;
disk_info.date = h->cache_obj->info.date;
disk_info.expire = h->cache_obj->info.expire;
disk_info.entity_version = dobj->disk_info.entity_version++;
disk_info.request_time = h->cache_obj->info.request_time;
disk_info.response_time = h->cache_obj->info.response_time;
disk_info.status = h->cache_obj->info.status;
disk_info.inode = dobj->disk_info.inode;
disk_info.device = dobj->disk_info.device;
disk_info.has_body = dobj->disk_info.has_body;
disk_info.header_only = dobj->disk_info.header_only;
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->hdrs.tempfd, (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->hdrs.tempfile);
apr_file_close(dobj->hdrs.tempfd);
return rv;
}
if (dobj->headers_out) {
rv = store_table(dobj->hdrs.tempfd, dobj->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->hdrs.tempfile);
apr_file_close(dobj->hdrs.tempfd);
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 (dobj->headers_in) {
rv = store_table(dobj->hdrs.tempfd, dobj->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->hdrs.tempfile);
apr_file_close(dobj->hdrs.tempfd);
return rv;
}
}
apr_file_close(dobj->hdrs.tempfd); /* flush and close */
return APR_SUCCESS;
}
static apr_status_t store_body(cache_handle_t *h, request_rec *r,
apr_bucket_brigade *in, apr_bucket_brigade *out)
{
apr_bucket *e;
apr_status_t rv = APR_SUCCESS;
disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
disk_cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &disk_cache_module);
int seen_eos = 0;
if (!dobj->bb) {
dobj->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
}
if (!dobj->offset) {
dobj->offset = dconf->readsize;
}
if (!dobj->timeout && dconf->readtime) {
dobj->timeout = apr_time_now() + dconf->readtime;
}
if (dobj->offset) {
apr_brigade_partition(in, dobj->offset, &e);
}
while (APR_SUCCESS == rv && !APR_BRIGADE_EMPTY(in)) {
const char *str;
apr_size_t length, written;
e = APR_BRIGADE_FIRST(in);
/* are we done completely? if so, pass any trailing buckets right through */
if (dobj->done) {
APR_BUCKET_REMOVE(e);
APR_BRIGADE_INSERT_TAIL(out, e);
continue;
}
/* have we seen eos yet? */
if (APR_BUCKET_IS_EOS(e)) {
seen_eos = 1;
dobj->done = 1;
APR_BUCKET_REMOVE(e);
APR_BRIGADE_CONCAT(out, dobj->bb);
APR_BRIGADE_INSERT_TAIL(out, e);
break;
}
/* honour flush buckets, we'll get called again */
if (APR_BUCKET_IS_FLUSH(e)) {
APR_BUCKET_REMOVE(e);
APR_BRIGADE_CONCAT(out, dobj->bb);
APR_BRIGADE_INSERT_TAIL(out, e);
break;
}
/* metadata buckets are preserved as is */
if (APR_BUCKET_IS_METADATA(e)) {
APR_BUCKET_REMOVE(e);
APR_BRIGADE_INSERT_TAIL(dobj->bb, e);
continue;
}
/* read the bucket, write to the cache */
rv = apr_bucket_read(e, &str, &length, APR_BLOCK_READ);
APR_BUCKET_REMOVE(e);
APR_BRIGADE_INSERT_TAIL(dobj->bb, e);
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 */
apr_pool_destroy(dobj->data.pool);
APR_BRIGADE_CONCAT(out, dobj->bb);
return rv;
}
/* don't write empty buckets to the cache */
if (!length) {
continue;
}
/* Attempt to create the data file at the last possible moment, if
* the body is empty, we don't write a file at all, and save an inode.
*/
if (!dobj->data.tempfd) {
apr_finfo_t finfo;
rv = apr_file_mktemp(&dobj->data.tempfd, dobj->data.tempfile,
APR_CREATE | APR_WRITE | APR_BINARY |
APR_BUFFERED | APR_EXCL, dobj->data.pool);
if (rv != APR_SUCCESS) {
return rv;
}
dobj->file_size = 0;
rv = apr_file_info_get(&finfo, APR_FINFO_IDENT,
dobj->data.tempfd);
if (rv != APR_SUCCESS) {
return rv;
}
dobj->disk_info.device = finfo.device;
dobj->disk_info.inode = finfo.inode;
dobj->disk_info.has_body = 1;
}
/* write to the cache, leave if we fail */
rv = apr_file_write_full(dobj->data.tempfd, 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 */
apr_pool_destroy(dobj->data.pool);
APR_BRIGADE_CONCAT(out, dobj->bb);
return rv;
}
dobj->file_size += written;
if (dobj->file_size > dconf->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, dconf->maxfs);
/* Remove the intermediate cache file and return non-APR_SUCCESS */
apr_pool_destroy(dobj->data.pool);
APR_BRIGADE_CONCAT(out, dobj->bb);
return APR_EGENERAL;
}
/* have we reached the limit of how much we're prepared to write in one
* go? If so, leave, we'll get called again. This prevents us from trying
* to swallow too much data at once, or taking so long to write the data
* the client times out.
*/
dobj->offset -= length;
if (dobj->offset <= 0) {
dobj->offset = 0;
APR_BRIGADE_CONCAT(out, dobj->bb);
break;
}
if ((dconf->readtime && apr_time_now() > dobj->timeout)) {
dobj->timeout = 0;
APR_BRIGADE_CONCAT(out, dobj->bb);
break;
}
}
/* Was this the final bucket? If yes, close the temp file and perform
* sanity checks.
*/
if (seen_eos) {
const char *cl_header = apr_table_get(r->headers_out, "Content-Length");
if (dobj->data.tempfd) {
apr_file_close(dobj->data.tempfd);
}
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 */
apr_pool_destroy(dobj->data.pool);
return APR_EGENERAL;
}
if (dobj->file_size < dconf->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, dconf->minfs);
/* Remove the intermediate cache file and return non-APR_SUCCESS */
apr_pool_destroy(dobj->data.pool);
return APR_EGENERAL;
}
if (cl_header) {
apr_int64_t cl = 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 */
apr_pool_destroy(dobj->data.pool);
return APR_EGENERAL;
}
}
/* All checks were fine, we're good to go when the commit comes */
}
return APR_SUCCESS;
}
static apr_status_t commit_entity(cache_handle_t *h, request_rec *r)
{
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
&disk_cache_module);
disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
apr_status_t rv;
/* write the headers to disk at the last possible moment */
rv = write_headers(h, r);
/* move header and data tempfiles to the final destination */
if (APR_SUCCESS == rv) {
rv = file_cache_el_final(conf, &dobj->hdrs, r);
}
if (APR_SUCCESS == rv) {
rv = file_cache_el_final(conf, &dobj->vary, r);
}
if (APR_SUCCESS == rv) {
rv = file_cache_el_final(conf, &dobj->data, r);
}
/* remove the cached items completely on any failure */
if (APR_SUCCESS != rv) {
remove_url(h, r->pool);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: commit_entity: URL '%s' not cached due to earlier disk error.",
dobj->name);
}
else {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: commit_entity: Headers and body for URL %s cached.",
dobj->name);
}
apr_pool_destroy(dobj->data.pool);
return APR_SUCCESS;
}
static void *create_dir_config(apr_pool_t *p, char *dummy)
{
disk_cache_dir_conf *dconf = apr_pcalloc(p, sizeof(disk_cache_dir_conf));
dconf->maxfs = DEFAULT_MAX_FILE_SIZE;
dconf->minfs = DEFAULT_MIN_FILE_SIZE;
dconf->readsize = DEFAULT_READSIZE;
dconf->readtime = DEFAULT_READTIME;
return dconf;
}
static void *merge_dir_config(apr_pool_t *p, void *basev, void *addv) {
disk_cache_dir_conf *new = (disk_cache_dir_conf *) apr_pcalloc(p, sizeof(disk_cache_dir_conf));
disk_cache_dir_conf *add = (disk_cache_dir_conf *) addv;
disk_cache_dir_conf *base = (disk_cache_dir_conf *) basev;
new->maxfs = (add->maxfs_set == 0) ? base->maxfs : add->maxfs;
new->maxfs_set = add->maxfs_set || base->maxfs_set;
new->minfs = (add->minfs_set == 0) ? base->minfs : add->minfs;
new->minfs_set = add->minfs_set || base->minfs_set;
new->readsize = (add->readsize_set == 0) ? base->readsize : add->readsize;
new->readsize_set = add->readsize_set || base->readsize_set;
new->readtime = (add->readtime_set == 0) ? base->readtime : add->readtime;
new->readtime_set = add->readtime_set || base->readtime_set;
return new;
}
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->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_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
if (apr_strtoff(&dconf->minfs, arg, NULL, 0) != APR_SUCCESS ||
dconf->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_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
if (apr_strtoff(&dconf->maxfs, arg, NULL, 0) != APR_SUCCESS ||
dconf->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 char
*set_cache_readsize(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
if (apr_strtoff(&dconf->readsize, arg, NULL, 0) != APR_SUCCESS ||
dconf->readsize < 0)
{
return "CacheReadSize argument must be a non-negative integer representing the max amount of data to cache in go.";
}
dconf->readsize_set = 1;
return NULL;
}
static const char
*set_cache_readtime(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
apr_off_t milliseconds;
if (apr_strtoff(&milliseconds, arg, NULL, 0) != APR_SUCCESS ||
milliseconds < 0)
{
return "CacheReadTime argument must be a non-negative integer representing the max amount of time taken to cache in go.";
}
dconf->readtime = apr_time_from_msec(milliseconds);
dconf->readtime_set = 1;
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 | ACCESS_CONF,
"The minimum file size to cache a document"),
AP_INIT_TAKE1("CacheMaxFileSize", set_cache_maxfs, NULL, RSRC_CONF | ACCESS_CONF,
"The maximum file size to cache a document"),
AP_INIT_TAKE1("CacheReadSize", set_cache_readsize, NULL, RSRC_CONF | ACCESS_CONF,
"The maximum quantity of data to attempt to read and cache in one go"),
AP_INIT_TAKE1("CacheReadTime", set_cache_readtime, NULL, RSRC_CONF | ACCESS_CONF,
"The maximum time taken to attempt to read and cache in go"),
{NULL}
};
static const cache_provider cache_disk_provider =
{
&remove_entity,
&store_headers,
&store_body,
&recall_headers,
&recall_body,
&create_entity,
&open_entity,
&remove_url,
&commit_entity
};
static void disk_cache_register_hook(apr_pool_t *p)
{
/* cache initializer */
ap_register_provider(p, CACHE_PROVIDER_GROUP, "disk", "0",
&cache_disk_provider);
}
AP_DECLARE_MODULE(disk_cache) = {
STANDARD20_MODULE_STUFF,
create_dir_config, /* create per-directory config structure */
merge_dir_config, /* 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 */
};