⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mod_disk_cache.c

📁 Apache官方在今天放出产品系列2.2的最新版本2.2.11的源码包 最流行的HTTP服务器软件之一
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include "apr_file_io.h"#include "apr_strings.h"#include "mod_cache.h"#include "mod_disk_cache.h"#include "ap_provider.h"#include "util_filter.h"#include "util_script.h"#include "util_charset.h"/* * mod_disk_cache: Disk Based HTTP 1.1 Cache. * * Flow to Find the .data file: *   Incoming client requests URI /foo/bar/baz *   Generate <hash> off of /foo/bar/baz *   Open <hash>.header *   Read in <hash>.header file (may contain Format #1 or Format #2) *   If format #1 (Contains a list of Vary Headers): *      Use each header name (from .header) with our request values (headers_in) to *      regenerate <hash> using HeaderName+HeaderValue+.../foo/bar/baz *      re-read in <hash>.header (must be format #2) *   read in <hash>.data * * Format #1: *   apr_uint32_t format; *   apr_time_t expire; *   apr_array_t vary_headers (delimited by CRLF) * * Format #2: *   disk_cache_info_t (first sizeof(apr_uint32_t) bytes is the format) *   entity name (dobj->name) [length is in disk_cache_info_t->name_len] *   r->headers_out (delimited by CRLF) *   CRLF *   r->headers_in (delimited by CRLF) *   CRLF */module AP_MODULE_DECLARE_DATA disk_cache_module;/* Forward declarations */static int remove_entity(cache_handle_t *h);static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *i);static apr_status_t store_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *b);static apr_status_t recall_headers(cache_handle_t *h, request_rec *r);static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,                               apr_file_t *file);/* * Local static functions */static char *header_file(apr_pool_t *p, disk_cache_conf *conf,                         disk_cache_object_t *dobj, const char *name){    if (!dobj->hashfile) {        dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,                                                conf->dirlength, name);    }    if (dobj->prefix) {        return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",                           dobj->hashfile, CACHE_HEADER_SUFFIX, NULL);     }     else {        return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,                           CACHE_HEADER_SUFFIX, NULL);     }}static char *data_file(apr_pool_t *p, disk_cache_conf *conf,                       disk_cache_object_t *dobj, const char *name){    if (!dobj->hashfile) {        dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,                                                conf->dirlength, name);    }    if (dobj->prefix) {        return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",                           dobj->hashfile, CACHE_DATA_SUFFIX, NULL);     }     else {        return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,                           CACHE_DATA_SUFFIX, NULL);     }}static void mkdir_structure(disk_cache_conf *conf, const char *file, apr_pool_t *pool){    apr_status_t rv;    char *p;    for (p = (char*)file + conf->cache_root_len + 1;;) {        p = strchr(p, '/');        if (!p)            break;        *p = '\0';        rv = apr_dir_make(file,                          APR_UREAD|APR_UWRITE|APR_UEXECUTE, pool);        if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) {            /* XXX */        }        *p = '/';        ++p;    }}/* htcacheclean may remove directories underneath us. * So, we'll try renaming three times at a cost of 0.002 seconds. */static apr_status_t safe_file_rename(disk_cache_conf *conf,                                     const char *src, const char *dest,                                     apr_pool_t *pool){    apr_status_t rv;    rv = apr_file_rename(src, dest, pool);    if (rv != APR_SUCCESS) {        int i;        for (i = 0; i < 2 && rv != APR_SUCCESS; i++) {            /* 1000 micro-seconds aka 0.001 seconds. */            apr_sleep(1000);            mkdir_structure(conf, dest, pool);            rv = apr_file_rename(src, dest, pool);        }    }    return rv;}static apr_status_t file_cache_el_final(disk_cache_object_t *dobj,                                        request_rec *r){    /* move the data over */    if (dobj->tfd) {        apr_status_t rv;        apr_file_close(dobj->tfd);        /* This assumes that the tempfile is on the same file system         * as the cache_root. If not, then we need a file copy/move         * rather than a rename.         */        rv = apr_file_rename(dobj->tempfile, dobj->datafile, r->pool);        if (rv != APR_SUCCESS) {            ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,                         "disk_cache: rename tempfile to datafile failed:"                         " %s -> %s", dobj->tempfile, dobj->datafile);            apr_file_remove(dobj->tempfile, r->pool);        }        dobj->tfd = NULL;    }    return APR_SUCCESS;}static apr_status_t file_cache_errorcleanup(disk_cache_object_t *dobj, request_rec *r){    /* Remove the header file and the body file. */    apr_file_remove(dobj->hdrsfile, r->pool);    apr_file_remove(dobj->datafile, r->pool);    /* If we opened the temporary data file, close and remove it. */    if (dobj->tfd) {        apr_file_close(dobj->tfd);        apr_file_remove(dobj->tempfile, r->pool);        dobj->tfd = NULL;    }    return APR_SUCCESS;}/* These two functions get and put state information into the data * file for an ap_cache_el, this state information will be read * and written transparent to clients of this module */static int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,                                    disk_cache_object_t *dobj, request_rec *r){    apr_status_t rv;    char *urlbuff;    disk_cache_info_t disk_info;    apr_size_t len;    /* read the data from the cache file */    len = sizeof(disk_cache_info_t);    rv = apr_file_read_full(fd, &disk_info, len, &len);    if (rv != APR_SUCCESS) {        return rv;    }    /* Store it away so we can get it later. */    dobj->disk_info = disk_info;    info->status = disk_info.status;    info->date = disk_info.date;    info->expire = disk_info.expire;    info->request_time = disk_info.request_time;    info->response_time = disk_info.response_time;    /* Note that we could optimize this by conditionally doing the palloc     * depending upon the size. */    urlbuff = apr_palloc(r->pool, disk_info.name_len + 1);    len = disk_info.name_len;    rv = apr_file_read_full(fd, urlbuff, len, &len);    if (rv != APR_SUCCESS) {        return rv;    }    urlbuff[disk_info.name_len] = '\0';    /* check that we have the same URL */    /* Would strncmp be correct? */    if (strcmp(urlbuff, dobj->name) != 0) {        return APR_EGENERAL;    }    return APR_SUCCESS;}static const char* regen_key(apr_pool_t *p, apr_table_t *headers,                             apr_array_header_t *varray, const char *oldkey){    struct iovec *iov;    int i, k;    int nvec;    const char *header;    const char **elts;    nvec = (varray->nelts * 2) + 1;    iov = apr_palloc(p, sizeof(struct iovec) * nvec);    elts = (const char **) varray->elts;    /* TODO:     *    - Handle multiple-value headers better. (sort them?)     *    - Handle Case in-sensitive Values better.     *        This isn't the end of the world, since it just lowers the cache     *        hit rate, but it would be nice to fix.     *     * The majority are case insenstive if they are values (encoding etc).     * Most of rfc2616 is case insensitive on header contents.     *     * So the better solution may be to identify headers which should be     * treated case-sensitive?     *  HTTP URI's (3.2.3) [host and scheme are insensitive]     *  HTTP method (5.1.1)     *  HTTP-date values (3.3.1)     *  3.7 Media Types [exerpt]     *     The type, subtype, and parameter attribute names are case-     *     insensitive. Parameter values might or might not be case-sensitive,     *     depending on the semantics of the parameter name.     *  4.20 Except [exerpt]     *     Comparison of expectation values is case-insensitive for unquoted     *     tokens (including the 100-continue token), and is case-sensitive for     *     quoted-string expectation-extensions.     */    for(i=0, k=0; i < varray->nelts; i++) {        header = apr_table_get(headers, elts[i]);        if (!header) {            header = "";        }        iov[k].iov_base = (char*) elts[i];        iov[k].iov_len = strlen(elts[i]);        k++;        iov[k].iov_base = (char*) header;        iov[k].iov_len = strlen(header);        k++;    }    iov[k].iov_base = (char*) oldkey;    iov[k].iov_len = strlen(oldkey);    k++;    return apr_pstrcatv(p, iov, k, NULL);}static int array_alphasort(const void *fn1, const void *fn2){    return strcmp(*(char**)fn1, *(char**)fn2);}static void tokens_to_array(apr_pool_t *p, const char *data,                            apr_array_header_t *arr){    char *token;    while ((token = ap_get_list_item(p, &data)) != NULL) {        *((const char **) apr_array_push(arr)) = token;    }    /* Sort it so that "Vary: A, B" and "Vary: B, A" are stored the same. */    qsort((void *) arr->elts, arr->nelts,         sizeof(char *), array_alphasort);}/* * Hook and mod_cache callback functions */static int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len){    disk_cache_conf *conf = ap_get_module_config(r->server->module_config,                                                 &disk_cache_module);    cache_object_t *obj;    disk_cache_object_t *dobj;    if (conf->cache_root == NULL) {        return DECLINED;    }    /* Allocate and initialize cache_object_t and disk_cache_object_t */    h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj));    obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj));    obj->key = apr_pstrdup(r->pool, key);    dobj->name = obj->key;    dobj->prefix = NULL;    /* Save the cache root */    dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);    dobj->root_len = conf->cache_root_len;    dobj->datafile = data_file(r->pool, conf, dobj, key);    dobj->hdrsfile = header_file(r->pool, conf, dobj, key);    dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);    return OK;}static int open_entity(cache_handle_t *h, request_rec *r, const char *key){    apr_uint32_t format;    apr_size_t len;    const char *nkey;    apr_status_t rc;    static int error_logged = 0;    disk_cache_conf *conf = ap_get_module_config(r->server->module_config,                                                 &disk_cache_module);    apr_finfo_t finfo;    cache_object_t *obj;    cache_info *info;    disk_cache_object_t *dobj;    int flags;    h->cache_obj = NULL;    /* Look up entity keyed to 'url' */    if (conf->cache_root == NULL) {        if (!error_logged) {            error_logged = 1;            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,                         "disk_cache: Cannot cache files to disk without a CacheRoot specified.");        }        return DECLINED;    }    /* Create and init the cache object */    h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(cache_object_t));    obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(disk_cache_object_t));    info = &(obj->info);    /* Open the headers file */    dobj->prefix = NULL;    /* Save the cache root */    dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);    dobj->root_len = conf->cache_root_len;    dobj->hdrsfile = header_file(r->pool, conf, dobj, key);    flags = APR_READ|APR_BINARY|APR_BUFFERED;    rc = apr_file_open(&dobj->hfd, dobj->hdrsfile, flags, 0, r->pool);    if (rc != APR_SUCCESS) {        return DECLINED;    }    /* read the format from the cache file */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -