Blame modules/cache/mod_cache_disk.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr_lib.h"
Packit 90a5c9
#include "apr_file_io.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "mod_cache.h"
Packit 90a5c9
#include "mod_cache_disk.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "ap_provider.h"
Packit 90a5c9
#include "util_filter.h"
Packit 90a5c9
#include "util_script.h"
Packit 90a5c9
#include "util_charset.h"
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * mod_cache_disk: Disk Based HTTP 1.1 Cache.
Packit 90a5c9
 *
Packit 90a5c9
 * Flow to Find the .data file:
Packit 90a5c9
 *   Incoming client requests URI /foo/bar/baz
Packit 90a5c9
 *   Generate <hash> off of /foo/bar/baz
Packit 90a5c9
 *   Open <hash>.header
Packit 90a5c9
 *   Read in <hash>.header file (may contain Format #1 or Format #2)
Packit 90a5c9
 *   If format #1 (Contains a list of Vary Headers):
Packit 90a5c9
 *      Use each header name (from .header) with our request values (headers_in) to
Packit 90a5c9
 *      regenerate <hash> using HeaderName+HeaderValue+.../foo/bar/baz
Packit 90a5c9
 *      re-read in <hash>.header (must be format #2)
Packit 90a5c9
 *   read in <hash>.data
Packit 90a5c9
 *
Packit 90a5c9
 * Format #1:
Packit 90a5c9
 *   apr_uint32_t format;
Packit 90a5c9
 *   apr_time_t expire;
Packit 90a5c9
 *   apr_array_t vary_headers (delimited by CRLF)
Packit 90a5c9
 *
Packit 90a5c9
 * Format #2:
Packit 90a5c9
 *   disk_cache_info_t (first sizeof(apr_uint32_t) bytes is the format)
Packit 90a5c9
 *   entity name (dobj->name) [length is in disk_cache_info_t->name_len]
Packit 90a5c9
 *   r->headers_out (delimited by CRLF)
Packit 90a5c9
 *   CRLF
Packit 90a5c9
 *   r->headers_in (delimited by CRLF)
Packit 90a5c9
 *   CRLF
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA cache_disk_module;
Packit 90a5c9
Packit 90a5c9
/* Forward declarations */
Packit 90a5c9
static int remove_entity(cache_handle_t *h);
Packit 90a5c9
static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *i);
Packit 90a5c9
static apr_status_t store_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *in,
Packit 90a5c9
                               apr_bucket_brigade *out);
Packit 90a5c9
static apr_status_t recall_headers(cache_handle_t *h, request_rec *r);
Packit 90a5c9
static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
Packit 90a5c9
static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
Packit 90a5c9
                               apr_file_t *file);
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Local static functions
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
static char *header_file(apr_pool_t *p, disk_cache_conf *conf,
Packit 90a5c9
                         disk_cache_object_t *dobj, const char *name)
Packit 90a5c9
{
Packit 90a5c9
    if (!dobj->hashfile) {
Packit 90a5c9
        dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
Packit 90a5c9
                                                conf->dirlength, name);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (dobj->prefix) {
Packit 90a5c9
        return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX "/",
Packit 90a5c9
                           dobj->hashfile, CACHE_HEADER_SUFFIX, NULL);
Packit 90a5c9
     }
Packit 90a5c9
     else {
Packit 90a5c9
        return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
Packit 90a5c9
                           CACHE_HEADER_SUFFIX, NULL);
Packit 90a5c9
     }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static char *data_file(apr_pool_t *p, disk_cache_conf *conf,
Packit 90a5c9
                       disk_cache_object_t *dobj, const char *name)
Packit 90a5c9
{
Packit 90a5c9
    if (!dobj->hashfile) {
Packit 90a5c9
        dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
Packit 90a5c9
                                                conf->dirlength, name);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (dobj->prefix) {
Packit 90a5c9
        return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX "/",
Packit 90a5c9
                           dobj->hashfile, CACHE_DATA_SUFFIX, NULL);
Packit 90a5c9
     }
Packit 90a5c9
     else {
Packit 90a5c9
        return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
Packit 90a5c9
                           CACHE_DATA_SUFFIX, NULL);
Packit 90a5c9
     }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t mkdir_structure(disk_cache_conf *conf, const char *file, apr_pool_t *pool)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    char *p;
Packit 90a5c9
Packit 90a5c9
    for (p = (char*)file + conf->cache_root_len + 1;;) {
Packit 90a5c9
        p = strchr(p, '/');
Packit 90a5c9
        if (!p)
Packit 90a5c9
            break;
Packit 90a5c9
        *p = '\0';
Packit 90a5c9
Packit 90a5c9
        rv = apr_dir_make(file,
Packit 90a5c9
                          APR_UREAD|APR_UWRITE|APR_UEXECUTE, pool);
Packit 90a5c9
        if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) {
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
        *p = '/';
Packit 90a5c9
        ++p;
Packit 90a5c9
    }
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* htcacheclean may remove directories underneath us.
Packit 90a5c9
 * So, we'll try renaming three times at a cost of 0.002 seconds.
Packit 90a5c9
 */
Packit 90a5c9
static apr_status_t safe_file_rename(disk_cache_conf *conf,
Packit 90a5c9
                                     const char *src, const char *dest,
Packit 90a5c9
                                     apr_pool_t *pool)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    rv = apr_file_rename(src, dest, pool);
Packit 90a5c9
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        int i;
Packit 90a5c9
Packit 90a5c9
        for (i = 0; i < 2 && rv != APR_SUCCESS; i++) {
Packit 90a5c9
            /* 1000 micro-seconds aka 0.001 seconds. */
Packit 90a5c9
            apr_sleep(1000);
Packit 90a5c9
Packit 90a5c9
            rv = mkdir_structure(conf, dest, pool);
Packit 90a5c9
            if (rv != APR_SUCCESS)
Packit 90a5c9
                continue;
Packit 90a5c9
Packit 90a5c9
            rv = apr_file_rename(src, dest, pool);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return rv;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t file_cache_el_final(disk_cache_conf *conf, disk_cache_file_t *file,
Packit 90a5c9
                                        request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv = APR_SUCCESS;
Packit 90a5c9
Packit 90a5c9
    /* This assumes that the tempfiles are on the same file system
Packit 90a5c9
     * as the cache_root. If not, then we need a file copy/move
Packit 90a5c9
     * rather than a rename.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    /* move the file over */
Packit 90a5c9
    if (file->tempfd) {
Packit 90a5c9
Packit 90a5c9
        rv = safe_file_rename(conf, file->tempfile, file->file, file->pool);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00699)
Packit 90a5c9
                    "rename tempfile to file failed:"
Packit 90a5c9
                    " %s -> %s", file->tempfile, file->file);
Packit 90a5c9
            apr_file_remove(file->tempfile, file->pool);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        file->tempfd = NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return rv;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t file_cache_temp_cleanup(void *dummy)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_file_t *file = (disk_cache_file_t *)dummy;
Packit 90a5c9
Packit 90a5c9
    /* clean up the temporary file */
Packit 90a5c9
    if (file->tempfd) {
Packit 90a5c9
        apr_file_remove(file->tempfile, file->pool);
Packit 90a5c9
        file->tempfd = NULL;
Packit 90a5c9
    }
Packit 90a5c9
    file->tempfile = NULL;
Packit 90a5c9
    file->pool = NULL;
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t file_cache_create(disk_cache_conf *conf, disk_cache_file_t *file,
Packit 90a5c9
                                      apr_pool_t *pool)
Packit 90a5c9
{
Packit 90a5c9
    file->pool = pool;
Packit 90a5c9
    file->tempfile = apr_pstrcat(pool, conf->cache_root, AP_TEMPFILE, NULL);
Packit 90a5c9
Packit 90a5c9
    apr_pool_cleanup_register(pool, file, file_cache_temp_cleanup, apr_pool_cleanup_null);
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* These two functions get and put state information into the data
Packit 90a5c9
 * file for an ap_cache_el, this state information will be read
Packit 90a5c9
 * and written transparent to clients of this module
Packit 90a5c9
 */
Packit 90a5c9
static int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,
Packit 90a5c9
                                    disk_cache_object_t *dobj, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    char *urlbuff;
Packit 90a5c9
    apr_size_t len;
Packit 90a5c9
Packit 90a5c9
    /* read the data from the cache file */
Packit 90a5c9
    len = sizeof(disk_cache_info_t);
Packit 90a5c9
    rv = apr_file_read_full(fd, &dobj->disk_info, len, &len;;
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Store it away so we can get it later. */
Packit 90a5c9
    info->status = dobj->disk_info.status;
Packit 90a5c9
    info->date = dobj->disk_info.date;
Packit 90a5c9
    info->expire = dobj->disk_info.expire;
Packit 90a5c9
    info->request_time = dobj->disk_info.request_time;
Packit 90a5c9
    info->response_time = dobj->disk_info.response_time;
Packit 90a5c9
Packit 90a5c9
    memcpy(&info->control, &dobj->disk_info.control, sizeof(cache_control_t));
Packit 90a5c9
Packit 90a5c9
    /* Note that we could optimize this by conditionally doing the palloc
Packit 90a5c9
     * depending upon the size. */
Packit 90a5c9
    urlbuff = apr_palloc(r->pool, dobj->disk_info.name_len + 1);
Packit 90a5c9
    len = dobj->disk_info.name_len;
Packit 90a5c9
    rv = apr_file_read_full(fd, urlbuff, len, &len;;
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
    urlbuff[dobj->disk_info.name_len] = '\0';
Packit 90a5c9
Packit 90a5c9
    /* check that we have the same URL */
Packit 90a5c9
    /* Would strncmp be correct? */
Packit 90a5c9
    if (strcmp(urlbuff, dobj->name) != 0) {
Packit 90a5c9
        return APR_EGENERAL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char* regen_key(apr_pool_t *p, apr_table_t *headers,
Packit 90a5c9
                             apr_array_header_t *varray, const char *oldkey)
Packit 90a5c9
{
Packit 90a5c9
    struct iovec *iov;
Packit 90a5c9
    int i, k;
Packit 90a5c9
    int nvec;
Packit 90a5c9
    const char *header;
Packit 90a5c9
    const char **elts;
Packit 90a5c9
Packit 90a5c9
    nvec = (varray->nelts * 2) + 1;
Packit 90a5c9
    iov = apr_palloc(p, sizeof(struct iovec) * nvec);
Packit 90a5c9
    elts = (const char **) varray->elts;
Packit 90a5c9
Packit 90a5c9
    /* TODO:
Packit 90a5c9
     *    - Handle multiple-value headers better. (sort them?)
Packit 90a5c9
     *    - Handle Case in-sensitive Values better.
Packit 90a5c9
     *        This isn't the end of the world, since it just lowers the cache
Packit 90a5c9
     *        hit rate, but it would be nice to fix.
Packit 90a5c9
     *
Packit 90a5c9
     * The majority are case insenstive if they are values (encoding etc).
Packit 90a5c9
     * Most of rfc2616 is case insensitive on header contents.
Packit 90a5c9
     *
Packit 90a5c9
     * So the better solution may be to identify headers which should be
Packit 90a5c9
     * treated case-sensitive?
Packit 90a5c9
     *  HTTP URI's (3.2.3) [host and scheme are insensitive]
Packit 90a5c9
     *  HTTP method (5.1.1)
Packit 90a5c9
     *  HTTP-date values (3.3.1)
Packit 90a5c9
     *  3.7 Media Types [exerpt]
Packit 90a5c9
     *     The type, subtype, and parameter attribute names are case-
Packit 90a5c9
     *     insensitive. Parameter values might or might not be case-sensitive,
Packit 90a5c9
     *     depending on the semantics of the parameter name.
Packit 90a5c9
     *  4.20 Except [exerpt]
Packit 90a5c9
     *     Comparison of expectation values is case-insensitive for unquoted
Packit 90a5c9
     *     tokens (including the 100-continue token), and is case-sensitive for
Packit 90a5c9
     *     quoted-string expectation-extensions.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    for (i=0, k=0; i < varray->nelts; i++) {
Packit 90a5c9
        header = apr_table_get(headers, elts[i]);
Packit 90a5c9
        if (!header) {
Packit 90a5c9
            header = "";
Packit 90a5c9
        }
Packit 90a5c9
        iov[k].iov_base = (char*) elts[i];
Packit 90a5c9
        iov[k].iov_len = strlen(elts[i]);
Packit 90a5c9
        k++;
Packit 90a5c9
        iov[k].iov_base = (char*) header;
Packit 90a5c9
        iov[k].iov_len = strlen(header);
Packit 90a5c9
        k++;
Packit 90a5c9
    }
Packit 90a5c9
    iov[k].iov_base = (char*) oldkey;
Packit 90a5c9
    iov[k].iov_len = strlen(oldkey);
Packit 90a5c9
    k++;
Packit 90a5c9
Packit 90a5c9
    return apr_pstrcatv(p, iov, k, NULL);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int array_alphasort(const void *fn1, const void *fn2)
Packit 90a5c9
{
Packit 90a5c9
    return strcmp(*(char**)fn1, *(char**)fn2);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void tokens_to_array(apr_pool_t *p, const char *data,
Packit 90a5c9
                            apr_array_header_t *arr)
Packit 90a5c9
{
Packit 90a5c9
    char *token;
Packit 90a5c9
Packit 90a5c9
    while ((token = ap_get_list_item(p, &data)) != NULL) {
Packit 90a5c9
        *((const char **) apr_array_push(arr)) = token;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Sort it so that "Vary: A, B" and "Vary: B, A" are stored the same. */
Packit 90a5c9
    qsort((void *) arr->elts, arr->nelts,
Packit 90a5c9
         sizeof(char *), array_alphasort);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Hook and mod_cache callback functions
Packit 90a5c9
 */
Packit 90a5c9
static int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len,
Packit 90a5c9
                         apr_bucket_brigade *bb)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &cache_disk_module);
Packit 90a5c9
    disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
Packit 90a5c9
                                                 &cache_disk_module);
Packit 90a5c9
    cache_object_t *obj;
Packit 90a5c9
    disk_cache_object_t *dobj;
Packit 90a5c9
    apr_pool_t *pool;
Packit 90a5c9
Packit 90a5c9
    if (conf->cache_root == NULL) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* we don't support caching of range requests (yet) */
Packit 90a5c9
    if (r->status == HTTP_PARTIAL_CONTENT) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00700)
Packit 90a5c9
                "URL %s partial content response not cached",
Packit 90a5c9
                key);
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Note, len is -1 if unknown so don't trust it too hard */
Packit 90a5c9
    if (len > dconf->maxfs) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00701)
Packit 90a5c9
                "URL %s failed the size check "
Packit 90a5c9
                "(%" APR_OFF_T_FMT " > %" APR_OFF_T_FMT ")",
Packit 90a5c9
                key, len, dconf->maxfs);
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    if (len >= 0 && len < dconf->minfs) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00702)
Packit 90a5c9
                "URL %s failed the size check "
Packit 90a5c9
                "(%" APR_OFF_T_FMT " < %" APR_OFF_T_FMT ")",
Packit 90a5c9
                key, len, dconf->minfs);
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Allocate and initialize cache_object_t and disk_cache_object_t */
Packit 90a5c9
    h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj));
Packit 90a5c9
    obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj));
Packit 90a5c9
Packit 90a5c9
    obj->key = apr_pstrdup(r->pool, key);
Packit 90a5c9
Packit 90a5c9
    dobj->name = obj->key;
Packit 90a5c9
    dobj->prefix = NULL;
Packit 90a5c9
    /* Save the cache root */
Packit 90a5c9
    dobj->root = apr_pstrmemdup(r->pool, conf->cache_root, conf->cache_root_len);
Packit 90a5c9
    dobj->root_len = conf->cache_root_len;
Packit 90a5c9
Packit 90a5c9
    apr_pool_create(&pool, r->pool);
Packit 90a5c9
    apr_pool_tag(pool, "mod_cache (create_entity)");
Packit 90a5c9
Packit 90a5c9
    file_cache_create(conf, &dobj->hdrs, pool);
Packit 90a5c9
    file_cache_create(conf, &dobj->vary, pool);
Packit 90a5c9
    file_cache_create(conf, &dobj->data, pool);
Packit 90a5c9
Packit 90a5c9
    dobj->data.file = data_file(r->pool, conf, dobj, key);
Packit 90a5c9
    dobj->hdrs.file = header_file(r->pool, conf, dobj, key);
Packit 90a5c9
    dobj->vary.file = header_file(r->pool, conf, dobj, key);
Packit 90a5c9
Packit 90a5c9
    dobj->disk_info.header_only = r->header_only;
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int open_entity(cache_handle_t *h, request_rec *r, const char *key)
Packit 90a5c9
{
Packit 90a5c9
    apr_uint32_t format;
Packit 90a5c9
    apr_size_t len;
Packit 90a5c9
    const char *nkey;
Packit 90a5c9
    apr_status_t rc;
Packit 90a5c9
    static int error_logged = 0;
Packit 90a5c9
    disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
Packit 90a5c9
                                                 &cache_disk_module);
Packit 90a5c9
#ifdef APR_SENDFILE_ENABLED
Packit 90a5c9
    core_dir_config *coreconf = ap_get_core_module_config(r->per_dir_config);
Packit 90a5c9
#endif
Packit 90a5c9
    apr_finfo_t finfo;
Packit 90a5c9
    cache_object_t *obj;
Packit 90a5c9
    cache_info *info;
Packit 90a5c9
    disk_cache_object_t *dobj;
Packit 90a5c9
    int flags;
Packit 90a5c9
    apr_pool_t *pool;
Packit 90a5c9
Packit 90a5c9
    h->cache_obj = NULL;
Packit 90a5c9
Packit 90a5c9
    /* Look up entity keyed to 'url' */
Packit 90a5c9
    if (conf->cache_root == NULL) {
Packit 90a5c9
        if (!error_logged) {
Packit 90a5c9
            error_logged = 1;
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00703)
Packit 90a5c9
                    "Cannot cache files to disk without a CacheRoot specified.");
Packit 90a5c9
        }
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Create and init the cache object */
Packit 90a5c9
    obj = apr_pcalloc(r->pool, sizeof(cache_object_t));
Packit 90a5c9
    dobj = apr_pcalloc(r->pool, sizeof(disk_cache_object_t));
Packit 90a5c9
Packit 90a5c9
    info = &(obj->info);
Packit 90a5c9
Packit 90a5c9
    /* Open the headers file */
Packit 90a5c9
    dobj->prefix = NULL;
Packit 90a5c9
Packit 90a5c9
    /* Save the cache root */
Packit 90a5c9
    dobj->root = apr_pstrmemdup(r->pool, conf->cache_root, conf->cache_root_len);
Packit 90a5c9
    dobj->root_len = conf->cache_root_len;
Packit 90a5c9
Packit 90a5c9
    dobj->vary.file = header_file(r->pool, conf, dobj, key);
Packit 90a5c9
    flags = APR_READ|APR_BINARY|APR_BUFFERED;
Packit 90a5c9
    rc = apr_file_open(&dobj->vary.fd, dobj->vary.file, flags, 0, r->pool);
Packit 90a5c9
    if (rc != APR_SUCCESS) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* read the format from the cache file */
Packit 90a5c9
    len = sizeof(format);
Packit 90a5c9
    apr_file_read_full(dobj->vary.fd, &format, len, &len;;
Packit 90a5c9
Packit 90a5c9
    if (format == VARY_FORMAT_VERSION) {
Packit 90a5c9
        apr_array_header_t* varray;
Packit 90a5c9
        apr_time_t expire;
Packit 90a5c9
Packit 90a5c9
        len = sizeof(expire);
Packit 90a5c9
        apr_file_read_full(dobj->vary.fd, &expire, len, &len;;
Packit 90a5c9
Packit 90a5c9
        varray = apr_array_make(r->pool, 5, sizeof(char*));
Packit 90a5c9
        rc = read_array(r, varray, dobj->vary.fd);
Packit 90a5c9
        if (rc != APR_SUCCESS) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r, APLOGNO(00704)
Packit 90a5c9
                    "Cannot parse vary header file: %s",
Packit 90a5c9
                    dobj->vary.file);
Packit 90a5c9
            apr_file_close(dobj->vary.fd);
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
        apr_file_close(dobj->vary.fd);
Packit 90a5c9
Packit 90a5c9
        nkey = regen_key(r->pool, r->headers_in, varray, key);
Packit 90a5c9
Packit 90a5c9
        dobj->hashfile = NULL;
Packit 90a5c9
        dobj->prefix = dobj->vary.file;
Packit 90a5c9
        dobj->hdrs.file = header_file(r->pool, conf, dobj, nkey);
Packit 90a5c9
Packit 90a5c9
        flags = APR_READ|APR_BINARY|APR_BUFFERED;
Packit 90a5c9
        rc = apr_file_open(&dobj->hdrs.fd, dobj->hdrs.file, flags, 0, r->pool);
Packit 90a5c9
        if (rc != APR_SUCCESS) {
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else if (format != DISK_FORMAT_VERSION) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00705)
Packit 90a5c9
                "File '%s' has a version mismatch. File had version: %d.",
Packit 90a5c9
                dobj->vary.file, format);
Packit 90a5c9
        apr_file_close(dobj->vary.fd);
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        apr_off_t offset = 0;
Packit 90a5c9
Packit 90a5c9
        /* oops, not vary as it turns out */
Packit 90a5c9
        dobj->hdrs.fd = dobj->vary.fd;
Packit 90a5c9
        dobj->vary.fd = NULL;
Packit 90a5c9
        dobj->hdrs.file = dobj->vary.file;
Packit 90a5c9
Packit 90a5c9
        /* This wasn't a Vary Format file, so we must seek to the
Packit 90a5c9
         * start of the file again, so that later reads work.
Packit 90a5c9
         */
Packit 90a5c9
        apr_file_seek(dobj->hdrs.fd, APR_SET, &offset);
Packit 90a5c9
        nkey = key;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    obj->key = nkey;
Packit 90a5c9
    dobj->key = nkey;
Packit 90a5c9
    dobj->name = key;
Packit 90a5c9
Packit 90a5c9
    apr_pool_create(&pool, r->pool);
Packit 90a5c9
    apr_pool_tag(pool, "mod_cache (open_entity)");
Packit 90a5c9
Packit 90a5c9
    file_cache_create(conf, &dobj->hdrs, pool);
Packit 90a5c9
    file_cache_create(conf, &dobj->vary, pool);
Packit 90a5c9
    file_cache_create(conf, &dobj->data, pool);
Packit 90a5c9
Packit 90a5c9
    dobj->data.file = data_file(r->pool, conf, dobj, nkey);
Packit 90a5c9
Packit 90a5c9
    /* Read the bytes to setup the cache_info fields */
Packit 90a5c9
    rc = file_cache_recall_mydata(dobj->hdrs.fd, info, dobj, r);
Packit 90a5c9
    if (rc != APR_SUCCESS) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r, APLOGNO(00706)
Packit 90a5c9
                "Cannot read header file %s", dobj->hdrs.file);
Packit 90a5c9
        apr_file_close(dobj->hdrs.fd);
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
    /* Is this a cached HEAD request? */
Packit 90a5c9
    if (dobj->disk_info.header_only && !r->header_only) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00707)
Packit 90a5c9
                "HEAD request cached, non-HEAD requested, ignoring: %s",
Packit 90a5c9
                dobj->hdrs.file);
Packit 90a5c9
        apr_file_close(dobj->hdrs.fd);
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Open the data file */
Packit 90a5c9
    if (dobj->disk_info.has_body) {
Packit 90a5c9
        flags = APR_READ | APR_BINARY;
Packit 90a5c9
#ifdef APR_SENDFILE_ENABLED
Packit 90a5c9
        /* When we are in the quick handler we don't have the per-directory
Packit 90a5c9
         * configuration, so this check only takes the global setting of
Packit 90a5c9
         * the EnableSendFile directive into account.
Packit 90a5c9
         */
Packit 90a5c9
        flags |= AP_SENDFILE_ENABLED(coreconf->enable_sendfile);
Packit 90a5c9
#endif
Packit 90a5c9
        rc = apr_file_open(&dobj->data.fd, dobj->data.file, flags, 0, r->pool);
Packit 90a5c9
        if (rc != APR_SUCCESS) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r, APLOGNO(00708)
Packit 90a5c9
                    "Cannot open data file %s", dobj->data.file);
Packit 90a5c9
            apr_file_close(dobj->hdrs.fd);
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        rc = apr_file_info_get(&finfo, APR_FINFO_SIZE | APR_FINFO_IDENT,
Packit 90a5c9
                dobj->data.fd);
Packit 90a5c9
        if (rc == APR_SUCCESS) {
Packit 90a5c9
            dobj->file_size = finfo.size;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Atomic check - does the body file belong to the header file? */
Packit 90a5c9
        if (dobj->disk_info.inode == finfo.inode &&
Packit 90a5c9
                dobj->disk_info.device == finfo.device) {
Packit 90a5c9
Packit 90a5c9
            /* Initialize the cache_handle callback functions */
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00709)
Packit 90a5c9
                    "Recalled cached URL info header %s", dobj->name);
Packit 90a5c9
Packit 90a5c9
            /* make the configuration stick */
Packit 90a5c9
            h->cache_obj = obj;
Packit 90a5c9
            obj->vobj = dobj;
Packit 90a5c9
Packit 90a5c9
            return OK;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
Packit 90a5c9
        /* make the configuration stick */
Packit 90a5c9
        h->cache_obj = obj;
Packit 90a5c9
        obj->vobj = dobj;
Packit 90a5c9
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Oh dear, no luck matching header to the body */
Packit 90a5c9
    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00710)
Packit 90a5c9
            "Cached URL info header '%s' didn't match body, ignoring this entry",
Packit 90a5c9
            dobj->name);
Packit 90a5c9
Packit 90a5c9
    apr_file_close(dobj->hdrs.fd);
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void close_disk_cache_fd(disk_cache_file_t *file)
Packit 90a5c9
{
Packit 90a5c9
   if (file->fd != NULL) {
Packit 90a5c9
       apr_file_close(file->fd);
Packit 90a5c9
       file->fd = NULL;
Packit 90a5c9
   }
Packit 90a5c9
   if (file->tempfd != NULL) {
Packit 90a5c9
       apr_file_close(file->tempfd);
Packit 90a5c9
       file->tempfd = NULL;
Packit 90a5c9
   }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int remove_entity(cache_handle_t *h)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
Packit 90a5c9
Packit 90a5c9
    close_disk_cache_fd(&(dobj->hdrs));
Packit 90a5c9
    close_disk_cache_fd(&(dobj->vary));
Packit 90a5c9
    close_disk_cache_fd(&(dobj->data));
Packit 90a5c9
Packit 90a5c9
    /* Null out the cache object pointer so next time we start from scratch  */
Packit 90a5c9
    h->cache_obj = NULL;
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int remove_url(cache_handle_t *h, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rc;
Packit 90a5c9
    disk_cache_object_t *dobj;
Packit 90a5c9
Packit 90a5c9
    /* Get disk cache object from cache handle */
Packit 90a5c9
    dobj = (disk_cache_object_t *) h->cache_obj->vobj;
Packit 90a5c9
    if (!dobj) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Delete headers file */
Packit 90a5c9
    if (dobj->hdrs.file) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00711)
Packit 90a5c9
                "Deleting %s from cache.", dobj->hdrs.file);
Packit 90a5c9
Packit 90a5c9
        rc = apr_file_remove(dobj->hdrs.file, r->pool);
Packit 90a5c9
        if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
Packit 90a5c9
            /* Will only result in an output if httpd is started with -e debug.
Packit 90a5c9
             * For reason see log_error_core for the case s == NULL.
Packit 90a5c9
             */
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rc, r, APLOGNO(00712)
Packit 90a5c9
                    "Failed to delete headers file %s from cache.",
Packit 90a5c9
                    dobj->hdrs.file);
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Delete data file */
Packit 90a5c9
    if (dobj->data.file) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00713)
Packit 90a5c9
                "Deleting %s from cache.", dobj->data.file);
Packit 90a5c9
Packit 90a5c9
        rc = apr_file_remove(dobj->data.file, r->pool);
Packit 90a5c9
        if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
Packit 90a5c9
            /* Will only result in an output if httpd is started with -e debug.
Packit 90a5c9
             * For reason see log_error_core for the case s == NULL.
Packit 90a5c9
             */
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rc, r, APLOGNO(00714)
Packit 90a5c9
                    "Failed to delete data file %s from cache.",
Packit 90a5c9
                    dobj->data.file);
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* now delete directories as far as possible up to our cache root */
Packit 90a5c9
    if (dobj->root) {
Packit 90a5c9
        const char *str_to_copy;
Packit 90a5c9
Packit 90a5c9
        str_to_copy = dobj->hdrs.file ? dobj->hdrs.file : dobj->data.file;
Packit 90a5c9
        if (str_to_copy) {
Packit 90a5c9
            char *dir, *slash, *q;
Packit 90a5c9
Packit 90a5c9
            dir = apr_pstrdup(r->pool, str_to_copy);
Packit 90a5c9
Packit 90a5c9
            /* remove filename */
Packit 90a5c9
            slash = strrchr(dir, '/');
Packit 90a5c9
            *slash = '\0';
Packit 90a5c9
Packit 90a5c9
            /*
Packit 90a5c9
             * now walk our way back to the cache root, delete everything
Packit 90a5c9
             * in the way as far as possible
Packit 90a5c9
             *
Packit 90a5c9
             * Note: due to the way we constructed the file names in
Packit 90a5c9
             * header_file and data_file, we are guaranteed that the
Packit 90a5c9
             * cache_root is suffixed by at least one '/' which will be
Packit 90a5c9
             * turned into a terminating null by this loop.  Therefore,
Packit 90a5c9
             * we won't either delete or go above our cache root.
Packit 90a5c9
             */
Packit 90a5c9
            for (q = dir + dobj->root_len; *q ; ) {
Packit 90a5c9
                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00715)
Packit 90a5c9
                        "Deleting directory %s from cache", dir);
Packit 90a5c9
Packit 90a5c9
                 rc = apr_dir_remove(dir, r->pool);
Packit 90a5c9
                 if (rc != APR_SUCCESS && !APR_STATUS_IS_ENOENT(rc)) {
Packit 90a5c9
                    break;
Packit 90a5c9
                 }
Packit 90a5c9
                 slash = strrchr(q, '/');
Packit 90a5c9
                 *slash = '\0';
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
Packit 90a5c9
                               apr_file_t *file)
Packit 90a5c9
{
Packit 90a5c9
    char w[MAX_STRING_LEN];
Packit 90a5c9
    int p;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    while (1) {
Packit 90a5c9
        rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00716)
Packit 90a5c9
                          "Premature end of vary array.");
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        p = strlen(w);
Packit 90a5c9
        if (p > 0 && w[p - 1] == '\n') {
Packit 90a5c9
            if (p > 1 && w[p - 2] == CR) {
Packit 90a5c9
                w[p - 2] = '\0';
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                w[p - 1] = '\0';
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* If we've finished reading the array, break out of the loop. */
Packit 90a5c9
        if (w[0] == '\0') {
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        *((const char **) apr_array_push(arr)) = apr_pstrdup(r->pool, w);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t store_array(apr_file_t *fd, apr_array_header_t* arr)
Packit 90a5c9
{
Packit 90a5c9
    int i;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    struct iovec iov[2];
Packit 90a5c9
    apr_size_t amt;
Packit 90a5c9
    const char **elts;
Packit 90a5c9
Packit 90a5c9
    elts = (const char **) arr->elts;
Packit 90a5c9
Packit 90a5c9
    for (i = 0; i < arr->nelts; i++) {
Packit 90a5c9
        iov[0].iov_base = (char*) elts[i];
Packit 90a5c9
        iov[0].iov_len = strlen(elts[i]);
Packit 90a5c9
        iov[1].iov_base = CRLF;
Packit 90a5c9
        iov[1].iov_len = sizeof(CRLF) - 1;
Packit 90a5c9
Packit 90a5c9
        rv = apr_file_writev_full(fd, (const struct iovec *) &iov, 2, &amt;;
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    iov[0].iov_base = CRLF;
Packit 90a5c9
    iov[0].iov_len = sizeof(CRLF) - 1;
Packit 90a5c9
Packit 90a5c9
    return apr_file_writev_full(fd, (const struct iovec *) &iov, 1, &amt;;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t read_table(cache_handle_t *handle, request_rec *r,
Packit 90a5c9
                               apr_table_t *table, apr_file_t *file)
Packit 90a5c9
{
Packit 90a5c9
    char w[MAX_STRING_LEN];
Packit 90a5c9
    char *l;
Packit 90a5c9
    int p;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    while (1) {
Packit 90a5c9
Packit 90a5c9
        /* ### What about APR_EOF? */
Packit 90a5c9
        rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00717)
Packit 90a5c9
                          "Premature end of cache headers.");
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Delete terminal (CR?)LF */
Packit 90a5c9
Packit 90a5c9
        p = strlen(w);
Packit 90a5c9
        /* Indeed, the host's '\n':
Packit 90a5c9
           '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
Packit 90a5c9
           -- whatever the script generates.
Packit 90a5c9
        */
Packit 90a5c9
        if (p > 0 && w[p - 1] == '\n') {
Packit 90a5c9
            if (p > 1 && w[p - 2] == CR) {
Packit 90a5c9
                w[p - 2] = '\0';
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                w[p - 1] = '\0';
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* If we've finished reading the headers, break out of the loop. */
Packit 90a5c9
        if (w[0] == '\0') {
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
#if APR_CHARSET_EBCDIC
Packit 90a5c9
        /* Chances are that we received an ASCII header text instead of
Packit 90a5c9
         * the expected EBCDIC header lines. Try to auto-detect:
Packit 90a5c9
         */
Packit 90a5c9
        if (!(l = strchr(w, ':'))) {
Packit 90a5c9
            int maybeASCII = 0, maybeEBCDIC = 0;
Packit 90a5c9
            unsigned char *cp, native;
Packit 90a5c9
            apr_size_t inbytes_left, outbytes_left;
Packit 90a5c9
Packit 90a5c9
            for (cp = w; *cp != '\0'; ++cp) {
Packit 90a5c9
                native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
Packit 90a5c9
                if (apr_isprint(*cp) && !apr_isprint(native))
Packit 90a5c9
                    ++maybeEBCDIC;
Packit 90a5c9
                if (!apr_isprint(*cp) && apr_isprint(native))
Packit 90a5c9
                    ++maybeASCII;
Packit 90a5c9
            }
Packit 90a5c9
            if (maybeASCII > maybeEBCDIC) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00718)
Packit 90a5c9
                        "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
Packit 90a5c9
                        r->filename);
Packit 90a5c9
                inbytes_left = outbytes_left = cp - w;
Packit 90a5c9
                apr_xlate_conv_buffer(ap_hdrs_from_ascii,
Packit 90a5c9
                                      w, &inbytes_left, w, &outbytes_left);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
#endif /*APR_CHARSET_EBCDIC*/
Packit 90a5c9
Packit 90a5c9
        /* if we see a bogus header don't ignore it. Shout and scream */
Packit 90a5c9
        if (!(l = strchr(w, ':'))) {
Packit 90a5c9
            return APR_EGENERAL;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        *l++ = '\0';
Packit 90a5c9
        while (apr_isspace(*l)) {
Packit 90a5c9
            ++l;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        apr_table_add(table, w, l);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Reads headers from a buffer and returns an array of headers.
Packit 90a5c9
 * Returns NULL on file error
Packit 90a5c9
 * This routine tries to deal with too long lines and continuation lines.
Packit 90a5c9
 * @@@: XXX: FIXME: currently the headers are passed thru un-merged.
Packit 90a5c9
 * Is that okay, or should they be collapsed where possible?
Packit 90a5c9
 */
Packit 90a5c9
static apr_status_t recall_headers(cache_handle_t *h, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    /* This case should not happen... */
Packit 90a5c9
    if (!dobj->hdrs.fd) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00719)
Packit 90a5c9
                "recalling headers; but no header fd for %s", dobj->name);
Packit 90a5c9
        return APR_NOTFOUND;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    h->req_hdrs = apr_table_make(r->pool, 20);
Packit 90a5c9
    h->resp_hdrs = apr_table_make(r->pool, 20);
Packit 90a5c9
Packit 90a5c9
    /* Call routine to read the header lines/status line */
Packit 90a5c9
    rv = read_table(h, r, h->resp_hdrs, dobj->hdrs.fd);
Packit 90a5c9
    if (rv != APR_SUCCESS) { 
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02987) 
Packit 90a5c9
                      "Error reading response headers from %s for %s",
Packit 90a5c9
                      dobj->hdrs.file, dobj->name);
Packit 90a5c9
    }
Packit 90a5c9
    rv = read_table(h, r, h->req_hdrs, dobj->hdrs.fd);
Packit 90a5c9
    if (rv != APR_SUCCESS) { 
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02988) 
Packit 90a5c9
                      "Error reading request headers from %s for %s",
Packit 90a5c9
                      dobj->hdrs.file, dobj->name);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    apr_file_close(dobj->hdrs.fd);
Packit 90a5c9
Packit 90a5c9
    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00720)
Packit 90a5c9
            "Recalled headers for URL %s", dobj->name);
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
Packit 90a5c9
Packit 90a5c9
    if (dobj->data.fd) {
Packit 90a5c9
        apr_brigade_insert_file(bb, dobj->data.fd, 0, dobj->file_size, p);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t store_table(apr_file_t *fd, apr_table_t *table)
Packit 90a5c9
{
Packit 90a5c9
    int i;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    struct iovec iov[4];
Packit 90a5c9
    apr_size_t amt;
Packit 90a5c9
    apr_table_entry_t *elts;
Packit 90a5c9
Packit 90a5c9
    elts = (apr_table_entry_t *) apr_table_elts(table)->elts;
Packit 90a5c9
    for (i = 0; i < apr_table_elts(table)->nelts; ++i) {
Packit 90a5c9
        if (elts[i].key != NULL) {
Packit 90a5c9
            iov[0].iov_base = elts[i].key;
Packit 90a5c9
            iov[0].iov_len = strlen(elts[i].key);
Packit 90a5c9
            iov[1].iov_base = ": ";
Packit 90a5c9
            iov[1].iov_len = sizeof(": ") - 1;
Packit 90a5c9
            iov[2].iov_base = elts[i].val;
Packit 90a5c9
            iov[2].iov_len = strlen(elts[i].val);
Packit 90a5c9
            iov[3].iov_base = CRLF;
Packit 90a5c9
            iov[3].iov_len = sizeof(CRLF) - 1;
Packit 90a5c9
Packit 90a5c9
            rv = apr_file_writev_full(fd, (const struct iovec *) &iov, 4, &amt;;
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                return rv;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    iov[0].iov_base = CRLF;
Packit 90a5c9
    iov[0].iov_len = sizeof(CRLF) - 1;
Packit 90a5c9
    rv = apr_file_writev_full(fd, (const struct iovec *) &iov, 1, &amt;;
Packit 90a5c9
    return rv;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *info)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
Packit 90a5c9
Packit 90a5c9
    memcpy(&h->cache_obj->info, info, sizeof(cache_info));
Packit 90a5c9
Packit 90a5c9
    if (r->headers_out) {
Packit 90a5c9
        dobj->headers_out = ap_cache_cacheable_headers_out(r);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (r->headers_in) {
Packit 90a5c9
        dobj->headers_in = ap_cache_cacheable_headers_in(r);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (r->header_only && r->status != HTTP_NOT_MODIFIED) {
Packit 90a5c9
        dobj->disk_info.header_only = 1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t write_headers(cache_handle_t *h, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
Packit 90a5c9
                                                 &cache_disk_module);
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    apr_size_t amt;
Packit 90a5c9
    disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
Packit 90a5c9
Packit 90a5c9
    disk_cache_info_t disk_info;
Packit 90a5c9
    struct iovec iov[2];
Packit 90a5c9
Packit 90a5c9
    memset(&disk_info, 0, sizeof(disk_cache_info_t));
Packit 90a5c9
Packit 90a5c9
    if (dobj->headers_out) {
Packit 90a5c9
        const char *tmp;
Packit 90a5c9
Packit 90a5c9
        tmp = apr_table_get(dobj->headers_out, "Vary");
Packit 90a5c9
Packit 90a5c9
        if (tmp) {
Packit 90a5c9
            apr_array_header_t* varray;
Packit 90a5c9
            apr_uint32_t format = VARY_FORMAT_VERSION;
Packit 90a5c9
Packit 90a5c9
            /* If we were initially opened as a vary format, rollback
Packit 90a5c9
             * that internal state for the moment so we can recreate the
Packit 90a5c9
             * vary format hints in the appropriate directory.
Packit 90a5c9
             */
Packit 90a5c9
            if (dobj->prefix) {
Packit 90a5c9
                dobj->hdrs.file = dobj->prefix;
Packit 90a5c9
                dobj->prefix = NULL;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            rv = mkdir_structure(conf, dobj->hdrs.file, r->pool);
Packit 90a5c9
Packit 90a5c9
            rv = apr_file_mktemp(&dobj->vary.tempfd, dobj->vary.tempfile,
Packit 90a5c9
                                 APR_CREATE | APR_WRITE | APR_BINARY | APR_EXCL,
Packit 90a5c9
                                 dobj->vary.pool);
Packit 90a5c9
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00721)
Packit 90a5c9
                        "could not create vary file %s",
Packit 90a5c9
                        dobj->vary.tempfile);
Packit 90a5c9
                return rv;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            amt = sizeof(format);
Packit 90a5c9
            rv = apr_file_write_full(dobj->vary.tempfd, &format, amt, NULL);
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00722)
Packit 90a5c9
                        "could not write to vary file %s",
Packit 90a5c9
                        dobj->vary.tempfile);
Packit 90a5c9
                apr_file_close(dobj->vary.tempfd);
Packit 90a5c9
                apr_pool_destroy(dobj->vary.pool);
Packit 90a5c9
                return rv;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            amt = sizeof(h->cache_obj->info.expire);
Packit 90a5c9
            rv = apr_file_write_full(dobj->vary.tempfd,
Packit 90a5c9
                                     &h->cache_obj->info.expire, amt, NULL);
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00723)
Packit 90a5c9
                        "could not write to vary file %s",
Packit 90a5c9
                        dobj->vary.tempfile);
Packit 90a5c9
                apr_file_close(dobj->vary.tempfd);
Packit 90a5c9
                apr_pool_destroy(dobj->vary.pool);
Packit 90a5c9
                return rv;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            varray = apr_array_make(r->pool, 6, sizeof(char*));
Packit 90a5c9
            tokens_to_array(r->pool, tmp, varray);
Packit 90a5c9
Packit 90a5c9
            store_array(dobj->vary.tempfd, varray);
Packit 90a5c9
Packit 90a5c9
            rv = apr_file_close(dobj->vary.tempfd);
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00724)
Packit 90a5c9
                        "could not close vary file %s",
Packit 90a5c9
                        dobj->vary.tempfile);
Packit 90a5c9
                apr_pool_destroy(dobj->vary.pool);
Packit 90a5c9
                return rv;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            tmp = regen_key(r->pool, dobj->headers_in, varray, dobj->name);
Packit 90a5c9
            dobj->prefix = dobj->hdrs.file;
Packit 90a5c9
            dobj->hashfile = NULL;
Packit 90a5c9
            dobj->data.file = data_file(r->pool, conf, dobj, tmp);
Packit 90a5c9
            dobj->hdrs.file = header_file(r->pool, conf, dobj, tmp);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
    rv = apr_file_mktemp(&dobj->hdrs.tempfd, dobj->hdrs.tempfile,
Packit 90a5c9
                         APR_CREATE | APR_WRITE | APR_BINARY |
Packit 90a5c9
                         APR_BUFFERED | APR_EXCL, dobj->hdrs.pool);
Packit 90a5c9
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00725)
Packit 90a5c9
                "could not create header file %s",
Packit 90a5c9
                dobj->hdrs.tempfile);
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    disk_info.format = DISK_FORMAT_VERSION;
Packit 90a5c9
    disk_info.date = h->cache_obj->info.date;
Packit 90a5c9
    disk_info.expire = h->cache_obj->info.expire;
Packit 90a5c9
    disk_info.entity_version = dobj->disk_info.entity_version++;
Packit 90a5c9
    disk_info.request_time = h->cache_obj->info.request_time;
Packit 90a5c9
    disk_info.response_time = h->cache_obj->info.response_time;
Packit 90a5c9
    disk_info.status = h->cache_obj->info.status;
Packit 90a5c9
    disk_info.inode = dobj->disk_info.inode;
Packit 90a5c9
    disk_info.device = dobj->disk_info.device;
Packit 90a5c9
    disk_info.has_body = dobj->disk_info.has_body;
Packit 90a5c9
    disk_info.header_only = dobj->disk_info.header_only;
Packit 90a5c9
Packit 90a5c9
    disk_info.name_len = strlen(dobj->name);
Packit 90a5c9
Packit 90a5c9
    memcpy(&disk_info.control, &h->cache_obj->info.control, sizeof(cache_control_t));
Packit 90a5c9
Packit 90a5c9
    iov[0].iov_base = (void*)&disk_info;
Packit 90a5c9
    iov[0].iov_len = sizeof(disk_cache_info_t);
Packit 90a5c9
    iov[1].iov_base = (void*)dobj->name;
Packit 90a5c9
    iov[1].iov_len = disk_info.name_len;
Packit 90a5c9
Packit 90a5c9
    rv = apr_file_writev_full(dobj->hdrs.tempfd, (const struct iovec *) &iov,
Packit 90a5c9
                              2, &amt;;
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00726)
Packit 90a5c9
                "could not write info to header file %s",
Packit 90a5c9
                dobj->hdrs.tempfile);
Packit 90a5c9
        apr_file_close(dobj->hdrs.tempfd);
Packit 90a5c9
        apr_pool_destroy(dobj->hdrs.pool);
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (dobj->headers_out) {
Packit 90a5c9
        rv = store_table(dobj->hdrs.tempfd, dobj->headers_out);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00727)
Packit 90a5c9
                    "could not write out-headers to header file %s",
Packit 90a5c9
                    dobj->hdrs.tempfile);
Packit 90a5c9
            apr_file_close(dobj->hdrs.tempfd);
Packit 90a5c9
            apr_pool_destroy(dobj->hdrs.pool);
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Parse the vary header and dump those fields from the headers_in. */
Packit 90a5c9
    /* FIXME: Make call to the same thing cache_select calls to crack Vary. */
Packit 90a5c9
    if (dobj->headers_in) {
Packit 90a5c9
        rv = store_table(dobj->hdrs.tempfd, dobj->headers_in);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00728)
Packit 90a5c9
                    "could not write in-headers to header file %s",
Packit 90a5c9
                    dobj->hdrs.tempfile);
Packit 90a5c9
            apr_file_close(dobj->hdrs.tempfd);
Packit 90a5c9
            apr_pool_destroy(dobj->hdrs.pool);
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    rv = apr_file_close(dobj->hdrs.tempfd); /* flush and close */
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(00729)
Packit 90a5c9
                "could not close header file %s",
Packit 90a5c9
                dobj->hdrs.tempfile);
Packit 90a5c9
        apr_pool_destroy(dobj->hdrs.pool);
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t store_body(cache_handle_t *h, request_rec *r,
Packit 90a5c9
                               apr_bucket_brigade *in, apr_bucket_brigade *out)
Packit 90a5c9
{
Packit 90a5c9
    apr_bucket *e;
Packit 90a5c9
    apr_status_t rv = APR_SUCCESS;
Packit 90a5c9
    disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
Packit 90a5c9
    disk_cache_dir_conf *dconf = ap_get_module_config(r->per_dir_config, &cache_disk_module);
Packit 90a5c9
    int seen_eos = 0;
Packit 90a5c9
Packit 90a5c9
    if (!dobj->offset) {
Packit 90a5c9
        dobj->offset = dconf->readsize;
Packit 90a5c9
    }
Packit 90a5c9
    if (!dobj->timeout && dconf->readtime) {
Packit 90a5c9
        dobj->timeout = apr_time_now() + dconf->readtime;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (dobj->offset) {
Packit 90a5c9
        apr_brigade_partition(in, dobj->offset, &e);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    while (APR_SUCCESS == rv && !APR_BRIGADE_EMPTY(in)) {
Packit 90a5c9
        const char *str;
Packit 90a5c9
        apr_size_t length, written;
Packit 90a5c9
Packit 90a5c9
        e = APR_BRIGADE_FIRST(in);
Packit 90a5c9
Packit 90a5c9
        /* are we done completely? if so, pass any trailing buckets right through */
Packit 90a5c9
        if (dobj->done || !dobj->data.pool) {
Packit 90a5c9
            APR_BUCKET_REMOVE(e);
Packit 90a5c9
            APR_BRIGADE_INSERT_TAIL(out, e);
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* have we seen eos yet? */
Packit 90a5c9
        if (APR_BUCKET_IS_EOS(e)) {
Packit 90a5c9
            seen_eos = 1;
Packit 90a5c9
            dobj->done = 1;
Packit 90a5c9
            APR_BUCKET_REMOVE(e);
Packit 90a5c9
            APR_BRIGADE_INSERT_TAIL(out, e);
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* honour flush buckets, we'll get called again */
Packit 90a5c9
        if (APR_BUCKET_IS_FLUSH(e)) {
Packit 90a5c9
            APR_BUCKET_REMOVE(e);
Packit 90a5c9
            APR_BRIGADE_INSERT_TAIL(out, e);
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* metadata buckets are preserved as is */
Packit 90a5c9
        if (APR_BUCKET_IS_METADATA(e)) {
Packit 90a5c9
            APR_BUCKET_REMOVE(e);
Packit 90a5c9
            APR_BRIGADE_INSERT_TAIL(out, e);
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* read the bucket, write to the cache */
Packit 90a5c9
        rv = apr_bucket_read(e, &str, &length, APR_BLOCK_READ);
Packit 90a5c9
        APR_BUCKET_REMOVE(e);
Packit 90a5c9
        APR_BRIGADE_INSERT_TAIL(out, e);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00730)
Packit 90a5c9
                    "Error when reading bucket for URL %s",
Packit 90a5c9
                    h->cache_obj->key);
Packit 90a5c9
            /* Remove the intermediate cache file and return non-APR_SUCCESS */
Packit 90a5c9
            apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* don't write empty buckets to the cache */
Packit 90a5c9
        if (!length) {
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (!dobj->disk_info.header_only) {
Packit 90a5c9
Packit 90a5c9
            /* Attempt to create the data file at the last possible moment, if
Packit 90a5c9
             * the body is empty, we don't write a file at all, and save an inode.
Packit 90a5c9
             */
Packit 90a5c9
            if (!dobj->data.tempfd) {
Packit 90a5c9
                apr_finfo_t finfo;
Packit 90a5c9
                rv = apr_file_mktemp(&dobj->data.tempfd, dobj->data.tempfile,
Packit 90a5c9
                        APR_CREATE | APR_WRITE | APR_BINARY | APR_BUFFERED
Packit 90a5c9
                                | APR_EXCL, dobj->data.pool);
Packit 90a5c9
                if (rv != APR_SUCCESS) {
Packit 90a5c9
                    apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
                    return rv;
Packit 90a5c9
                }
Packit 90a5c9
                dobj->file_size = 0;
Packit 90a5c9
                rv = apr_file_info_get(&finfo, APR_FINFO_IDENT,
Packit 90a5c9
                        dobj->data.tempfd);
Packit 90a5c9
                if (rv != APR_SUCCESS) {
Packit 90a5c9
                    apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
                    return rv;
Packit 90a5c9
                }
Packit 90a5c9
                dobj->disk_info.device = finfo.device;
Packit 90a5c9
                dobj->disk_info.inode = finfo.inode;
Packit 90a5c9
                dobj->disk_info.has_body = 1;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* write to the cache, leave if we fail */
Packit 90a5c9
            rv = apr_file_write_full(dobj->data.tempfd, str, length, &written);
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                ap_log_rerror(
Packit 90a5c9
                        APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00731) "Error when writing cache file for URL %s", h->cache_obj->key);
Packit 90a5c9
                /* Remove the intermediate cache file and return non-APR_SUCCESS */
Packit 90a5c9
                apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
                return rv;
Packit 90a5c9
            }
Packit 90a5c9
            dobj->file_size += written;
Packit 90a5c9
            if (dobj->file_size > dconf->maxfs) {
Packit 90a5c9
                ap_log_rerror(
Packit 90a5c9
                        APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00732) "URL %s failed the size check "
Packit 90a5c9
                        "(%" APR_OFF_T_FMT ">%" APR_OFF_T_FMT ")", h->cache_obj->key, dobj->file_size, dconf->maxfs);
Packit 90a5c9
                /* Remove the intermediate cache file and return non-APR_SUCCESS */
Packit 90a5c9
                apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
                return APR_EGENERAL;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* have we reached the limit of how much we're prepared to write in one
Packit 90a5c9
         * go? If so, leave, we'll get called again. This prevents us from trying
Packit 90a5c9
         * to swallow too much data at once, or taking so long to write the data
Packit 90a5c9
         * the client times out.
Packit 90a5c9
         */
Packit 90a5c9
        dobj->offset -= length;
Packit 90a5c9
        if (dobj->offset <= 0) {
Packit 90a5c9
            dobj->offset = 0;
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
        if ((dconf->readtime && apr_time_now() > dobj->timeout)) {
Packit 90a5c9
            dobj->timeout = 0;
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Was this the final bucket? If yes, close the temp file and perform
Packit 90a5c9
     * sanity checks.
Packit 90a5c9
     */
Packit 90a5c9
    if (seen_eos) {
Packit 90a5c9
        const char *cl_header = apr_table_get(r->headers_out, "Content-Length");
Packit 90a5c9
Packit 90a5c9
        if (!dobj->disk_info.header_only) {
Packit 90a5c9
Packit 90a5c9
            if (dobj->data.tempfd) {
Packit 90a5c9
                rv = apr_file_close(dobj->data.tempfd);
Packit 90a5c9
                if (rv != APR_SUCCESS) {
Packit 90a5c9
                    /* Buffered write failed, abandon attempt to write */
Packit 90a5c9
                    apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
                    return rv;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (r->connection->aborted || r->no_cache) {
Packit 90a5c9
                ap_log_rerror(
Packit 90a5c9
                        APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00733) "Discarding body for URL %s "
Packit 90a5c9
                        "because connection has been aborted.", h->cache_obj->key);
Packit 90a5c9
                /* Remove the intermediate cache file and return non-APR_SUCCESS */
Packit 90a5c9
                apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
                return APR_EGENERAL;
Packit 90a5c9
            }
Packit 90a5c9
            if (dobj->file_size < dconf->minfs) {
Packit 90a5c9
                ap_log_rerror(
Packit 90a5c9
                        APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00734) "URL %s failed the size check "
Packit 90a5c9
                        "(%" APR_OFF_T_FMT "<%" APR_OFF_T_FMT ")", h->cache_obj->key, dobj->file_size, dconf->minfs);
Packit 90a5c9
                /* Remove the intermediate cache file and return non-APR_SUCCESS */
Packit 90a5c9
                apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
                return APR_EGENERAL;
Packit 90a5c9
            }
Packit 90a5c9
            if (cl_header) {
Packit 90a5c9
                apr_int64_t cl = apr_atoi64(cl_header);
Packit 90a5c9
                if ((errno == 0) && (dobj->file_size != cl)) {
Packit 90a5c9
                    ap_log_rerror(
Packit 90a5c9
                            APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00735) "URL %s didn't receive complete response, not caching", h->cache_obj->key);
Packit 90a5c9
                    /* Remove the intermediate cache file and return non-APR_SUCCESS */
Packit 90a5c9
                    apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
                    return APR_EGENERAL;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* All checks were fine, we're good to go when the commit comes */
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t commit_entity(cache_handle_t *h, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
Packit 90a5c9
                                                 &cache_disk_module);
Packit 90a5c9
    disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    /* write the headers to disk at the last possible moment */
Packit 90a5c9
    rv = write_headers(h, r);
Packit 90a5c9
Packit 90a5c9
    /* move header and data tempfiles to the final destination */
Packit 90a5c9
    if (APR_SUCCESS == rv) {
Packit 90a5c9
        rv = file_cache_el_final(conf, &dobj->hdrs, r);
Packit 90a5c9
    }
Packit 90a5c9
    if (APR_SUCCESS == rv) {
Packit 90a5c9
        rv = file_cache_el_final(conf, &dobj->vary, r);
Packit 90a5c9
    }
Packit 90a5c9
    if (APR_SUCCESS == rv) {
Packit 90a5c9
        if (!dobj->disk_info.header_only) {
Packit 90a5c9
            rv = file_cache_el_final(conf, &dobj->data, r);
Packit 90a5c9
        }
Packit 90a5c9
        else if (dobj->data.file) {
Packit 90a5c9
            rv = apr_file_remove(dobj->data.file, dobj->data.pool);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* remove the cached items completely on any failure */
Packit 90a5c9
    if (APR_SUCCESS != rv) {
Packit 90a5c9
        remove_url(h, r);
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00736)
Packit 90a5c9
                "commit_entity: URL '%s' not cached due to earlier disk error.",
Packit 90a5c9
                dobj->name);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00737)
Packit 90a5c9
                "commit_entity: Headers and body for URL %s cached.",
Packit 90a5c9
                dobj->name);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    apr_pool_destroy(dobj->data.pool);
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t invalidate_entity(cache_handle_t *h, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    rv = recall_headers(h, r);
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* mark the entity as invalidated */
Packit 90a5c9
    h->cache_obj->info.control.invalidated = 1;
Packit 90a5c9
Packit 90a5c9
    return commit_entity(h, r);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *create_dir_config(apr_pool_t *p, char *dummy)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_dir_conf *dconf = apr_pcalloc(p, sizeof(disk_cache_dir_conf));
Packit 90a5c9
Packit 90a5c9
    dconf->maxfs = DEFAULT_MAX_FILE_SIZE;
Packit 90a5c9
    dconf->minfs = DEFAULT_MIN_FILE_SIZE;
Packit 90a5c9
    dconf->readsize = DEFAULT_READSIZE;
Packit 90a5c9
    dconf->readtime = DEFAULT_READTIME;
Packit 90a5c9
Packit 90a5c9
    return dconf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *merge_dir_config(apr_pool_t *p, void *basev, void *addv)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_dir_conf *new = (disk_cache_dir_conf *) apr_pcalloc(p, sizeof(disk_cache_dir_conf));
Packit 90a5c9
    disk_cache_dir_conf *add = (disk_cache_dir_conf *) addv;
Packit 90a5c9
    disk_cache_dir_conf *base = (disk_cache_dir_conf *) basev;
Packit 90a5c9
Packit 90a5c9
    new->maxfs = (add->maxfs_set == 0) ? base->maxfs : add->maxfs;
Packit 90a5c9
    new->maxfs_set = add->maxfs_set || base->maxfs_set;
Packit 90a5c9
    new->minfs = (add->minfs_set == 0) ? base->minfs : add->minfs;
Packit 90a5c9
    new->minfs_set = add->minfs_set || base->minfs_set;
Packit 90a5c9
    new->readsize = (add->readsize_set == 0) ? base->readsize : add->readsize;
Packit 90a5c9
    new->readsize_set = add->readsize_set || base->readsize_set;
Packit 90a5c9
    new->readtime = (add->readtime_set == 0) ? base->readtime : add->readtime;
Packit 90a5c9
    new->readtime_set = add->readtime_set || base->readtime_set;
Packit 90a5c9
Packit 90a5c9
    return new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *create_config(apr_pool_t *p, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_conf *conf = apr_pcalloc(p, sizeof(disk_cache_conf));
Packit 90a5c9
Packit 90a5c9
    /* XXX: Set default values */
Packit 90a5c9
    conf->dirlevels = DEFAULT_DIRLEVELS;
Packit 90a5c9
    conf->dirlength = DEFAULT_DIRLENGTH;
Packit 90a5c9
Packit 90a5c9
    conf->cache_root = NULL;
Packit 90a5c9
    conf->cache_root_len = 0;
Packit 90a5c9
Packit 90a5c9
    return conf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * mod_cache_disk configuration directives handlers.
Packit 90a5c9
 */
Packit 90a5c9
static const char
Packit 90a5c9
*set_cache_root(cmd_parms *parms, void *in_struct_ptr, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
Packit 90a5c9
                                                 &cache_disk_module);
Packit 90a5c9
    conf->cache_root = arg;
Packit 90a5c9
    conf->cache_root_len = strlen(arg);
Packit 90a5c9
    /* TODO: canonicalize cache_root and strip off any trailing slashes */
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Consider eliminating the next two directives in favor of
Packit 90a5c9
 * Ian's prime number hash...
Packit 90a5c9
 * key = hash_fn( r->uri)
Packit 90a5c9
 * filename = "/key % prime1 /key %prime2/key %prime3"
Packit 90a5c9
 */
Packit 90a5c9
static const char
Packit 90a5c9
*set_cache_dirlevels(cmd_parms *parms, void *in_struct_ptr, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
Packit 90a5c9
                                                 &cache_disk_module);
Packit 90a5c9
    int val = atoi(arg);
Packit 90a5c9
    if (val < 1)
Packit 90a5c9
        return "CacheDirLevels value must be an integer greater than 0";
Packit 90a5c9
    if (val * conf->dirlength > CACHEFILE_LEN)
Packit 90a5c9
        return "CacheDirLevels*CacheDirLength value must not be higher than 20";
Packit 90a5c9
    conf->dirlevels = val;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
static const char
Packit 90a5c9
*set_cache_dirlength(cmd_parms *parms, void *in_struct_ptr, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
Packit 90a5c9
                                                 &cache_disk_module);
Packit 90a5c9
    int val = atoi(arg);
Packit 90a5c9
    if (val < 1)
Packit 90a5c9
        return "CacheDirLength value must be an integer greater than 0";
Packit 90a5c9
    if (val * conf->dirlevels > CACHEFILE_LEN)
Packit 90a5c9
        return "CacheDirLevels*CacheDirLength value must not be higher than 20";
Packit 90a5c9
Packit 90a5c9
    conf->dirlength = val;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char
Packit 90a5c9
*set_cache_minfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
Packit 90a5c9
Packit 90a5c9
    if (apr_strtoff(&dconf->minfs, arg, NULL, 10) != APR_SUCCESS ||
Packit 90a5c9
            dconf->minfs < 0)
Packit 90a5c9
    {
Packit 90a5c9
        return "CacheMinFileSize argument must be a non-negative integer representing the min size of a file to cache in bytes.";
Packit 90a5c9
    }
Packit 90a5c9
    dconf->minfs_set = 1;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char
Packit 90a5c9
*set_cache_maxfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
Packit 90a5c9
Packit 90a5c9
    if (apr_strtoff(&dconf->maxfs, arg, NULL, 10) != APR_SUCCESS ||
Packit 90a5c9
            dconf->maxfs < 0)
Packit 90a5c9
    {
Packit 90a5c9
        return "CacheMaxFileSize argument must be a non-negative integer representing the max size of a file to cache in bytes.";
Packit 90a5c9
    }
Packit 90a5c9
    dconf->maxfs_set = 1;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char
Packit 90a5c9
*set_cache_readsize(cmd_parms *parms, void *in_struct_ptr, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
Packit 90a5c9
Packit 90a5c9
    if (apr_strtoff(&dconf->readsize, arg, NULL, 10) != APR_SUCCESS ||
Packit 90a5c9
            dconf->readsize < 0)
Packit 90a5c9
    {
Packit 90a5c9
        return "CacheReadSize argument must be a non-negative integer representing the max amount of data to cache in go.";
Packit 90a5c9
    }
Packit 90a5c9
    dconf->readsize_set = 1;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char
Packit 90a5c9
*set_cache_readtime(cmd_parms *parms, void *in_struct_ptr, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    disk_cache_dir_conf *dconf = (disk_cache_dir_conf *)in_struct_ptr;
Packit 90a5c9
    apr_off_t milliseconds;
Packit 90a5c9
Packit 90a5c9
    if (apr_strtoff(&milliseconds, arg, NULL, 10) != APR_SUCCESS ||
Packit 90a5c9
            milliseconds < 0)
Packit 90a5c9
    {
Packit 90a5c9
        return "CacheReadTime argument must be a non-negative integer representing the max amount of time taken to cache in go.";
Packit 90a5c9
    }
Packit 90a5c9
    dconf->readtime = apr_time_from_msec(milliseconds);
Packit 90a5c9
    dconf->readtime_set = 1;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const command_rec disk_cache_cmds[] =
Packit 90a5c9
{
Packit 90a5c9
    AP_INIT_TAKE1("CacheRoot", set_cache_root, NULL, RSRC_CONF,
Packit 90a5c9
                 "The directory to store cache files"),
Packit 90a5c9
    AP_INIT_TAKE1("CacheDirLevels", set_cache_dirlevels, NULL, RSRC_CONF,
Packit 90a5c9
                  "The number of levels of subdirectories in the cache"),
Packit 90a5c9
    AP_INIT_TAKE1("CacheDirLength", set_cache_dirlength, NULL, RSRC_CONF,
Packit 90a5c9
                  "The number of characters in subdirectory names"),
Packit 90a5c9
    AP_INIT_TAKE1("CacheMinFileSize", set_cache_minfs, NULL, RSRC_CONF | ACCESS_CONF,
Packit 90a5c9
                  "The minimum file size to cache a document"),
Packit 90a5c9
    AP_INIT_TAKE1("CacheMaxFileSize", set_cache_maxfs, NULL, RSRC_CONF | ACCESS_CONF,
Packit 90a5c9
                  "The maximum file size to cache a document"),
Packit 90a5c9
    AP_INIT_TAKE1("CacheReadSize", set_cache_readsize, NULL, RSRC_CONF | ACCESS_CONF,
Packit 90a5c9
                  "The maximum quantity of data to attempt to read and cache in one go"),
Packit 90a5c9
    AP_INIT_TAKE1("CacheReadTime", set_cache_readtime, NULL, RSRC_CONF | ACCESS_CONF,
Packit 90a5c9
                  "The maximum time taken to attempt to read and cache in go"),
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static const cache_provider cache_disk_provider =
Packit 90a5c9
{
Packit 90a5c9
    &remove_entity,
Packit 90a5c9
    &store_headers,
Packit 90a5c9
    &store_body,
Packit 90a5c9
    &recall_headers,
Packit 90a5c9
    &recall_body,
Packit 90a5c9
    &create_entity,
Packit 90a5c9
    &open_entity,
Packit 90a5c9
    &remove_url,
Packit 90a5c9
    &commit_entity,
Packit 90a5c9
    &invalidate_entity
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static void disk_cache_register_hook(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    /* cache initializer */
Packit 90a5c9
    ap_register_provider(p, CACHE_PROVIDER_GROUP, "disk", "0",
Packit 90a5c9
                         &cache_disk_provider);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(cache_disk) = {
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    create_dir_config,          /* create per-directory config structure */
Packit 90a5c9
    merge_dir_config,           /* merge per-directory config structures */
Packit 90a5c9
    create_config,              /* create per-server config structure */
Packit 90a5c9
    NULL,                       /* merge per-server config structures */
Packit 90a5c9
    disk_cache_cmds,            /* command apr_table_t */
Packit 90a5c9
    disk_cache_register_hook    /* register hooks */
Packit 90a5c9
};