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

📄 mod_disk_cache.c

📁 Apache官方在今天放出产品系列2.2的最新版本2.2.11的源码包 最流行的HTTP服务器软件之一
💻 C
📖 第 1 页 / 共 3 页
字号:
    rv = apr_file_writev(fd, (const struct iovec *) &iov, 1,                         &amt);    return rv;}static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *info){    disk_cache_conf *conf = ap_get_module_config(r->server->module_config,                                                 &disk_cache_module);    apr_status_t rv;    apr_size_t amt;    disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;    disk_cache_info_t disk_info;    struct iovec iov[2];    /* This is flaky... we need to manage the cache_info differently */    h->cache_obj->info = *info;    if (r->headers_out) {        const char *tmp;        tmp = apr_table_get(r->headers_out, "Vary");        if (tmp) {            apr_array_header_t* varray;            apr_uint32_t format = VARY_FORMAT_VERSION;            /* If we were initially opened as a vary format, rollback             * that internal state for the moment so we can recreate the             * vary format hints in the appropriate directory.             */            if (dobj->prefix) {                dobj->hdrsfile = dobj->prefix;                dobj->prefix = NULL;            }            mkdir_structure(conf, dobj->hdrsfile, r->pool);            rv = apr_file_mktemp(&dobj->tfd, dobj->tempfile,                                 APR_CREATE | APR_WRITE | APR_BINARY | APR_EXCL,                                 r->pool);            if (rv != APR_SUCCESS) {                return rv;            }            amt = sizeof(format);            apr_file_write(dobj->tfd, &format, &amt);            amt = sizeof(info->expire);            apr_file_write(dobj->tfd, &info->expire, &amt);            varray = apr_array_make(r->pool, 6, sizeof(char*));            tokens_to_array(r->pool, tmp, varray);            store_array(dobj->tfd, varray);            apr_file_close(dobj->tfd);            dobj->tfd = NULL;            rv = safe_file_rename(conf, dobj->tempfile, dobj->hdrsfile,                                  r->pool);            if (rv != APR_SUCCESS) {                ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,                    "disk_cache: rename tempfile to varyfile failed: %s -> %s",                    dobj->tempfile, dobj->hdrsfile);                apr_file_remove(dobj->tempfile, r->pool);                return rv;            }            dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);            tmp = regen_key(r->pool, r->headers_in, varray, dobj->name);            dobj->prefix = dobj->hdrsfile;            dobj->hashfile = NULL;            dobj->datafile = data_file(r->pool, conf, dobj, tmp);            dobj->hdrsfile = header_file(r->pool, conf, dobj, tmp);        }    }    rv = apr_file_mktemp(&dobj->hfd, dobj->tempfile,                         APR_CREATE | APR_WRITE | APR_BINARY |                         APR_BUFFERED | APR_EXCL, r->pool);    if (rv != APR_SUCCESS) {        return rv;    }    disk_info.format = DISK_FORMAT_VERSION;    disk_info.date = info->date;    disk_info.expire = info->expire;    disk_info.entity_version = dobj->disk_info.entity_version++;    disk_info.request_time = info->request_time;    disk_info.response_time = info->response_time;    disk_info.status = info->status;    disk_info.name_len = strlen(dobj->name);    iov[0].iov_base = (void*)&disk_info;    iov[0].iov_len = sizeof(disk_cache_info_t);    iov[1].iov_base = (void*)dobj->name;    iov[1].iov_len = disk_info.name_len;    rv = apr_file_writev(dobj->hfd, (const struct iovec *) &iov, 2, &amt);    if (rv != APR_SUCCESS) {        return rv;    }    if (r->headers_out) {        apr_table_t *headers_out;        headers_out = ap_cache_cacheable_hdrs_out(r->pool, r->headers_out,                                                  r->server);        if (!apr_table_get(headers_out, "Content-Type")            && r->content_type) {            apr_table_setn(headers_out, "Content-Type",                           ap_make_content_type(r, r->content_type));        }        headers_out = apr_table_overlay(r->pool, headers_out,                                        r->err_headers_out);        rv = store_table(dobj->hfd, headers_out);        if (rv != APR_SUCCESS) {            return rv;        }    }    /* Parse the vary header and dump those fields from the headers_in. */    /* FIXME: Make call to the same thing cache_select calls to crack Vary. */    if (r->headers_in) {        apr_table_t *headers_in;        headers_in = ap_cache_cacheable_hdrs_out(r->pool, r->headers_in,                                                 r->server);        rv = store_table(dobj->hfd, headers_in);        if (rv != APR_SUCCESS) {            return rv;        }    }    apr_file_close(dobj->hfd); /* flush and close */    /* Remove old file with the same name. If remove fails, then     * perhaps we need to create the directory tree where we are     * about to write the new headers file.     */    rv = apr_file_remove(dobj->hdrsfile, r->pool);    if (rv != APR_SUCCESS) {        mkdir_structure(conf, dobj->hdrsfile, r->pool);    }    rv = safe_file_rename(conf, dobj->tempfile, dobj->hdrsfile, r->pool);    if (rv != APR_SUCCESS) {        ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,                     "disk_cache: rename tempfile to hdrsfile failed: %s -> %s",                     dobj->tempfile, dobj->hdrsfile);        apr_file_remove(dobj->tempfile, r->pool);        return rv;    }    dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,                 "disk_cache: Stored headers for URL %s",  dobj->name);    return APR_SUCCESS;}static apr_status_t store_body(cache_handle_t *h, request_rec *r,                               apr_bucket_brigade *bb){    apr_bucket *e;    apr_status_t rv;    disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;    disk_cache_conf *conf = ap_get_module_config(r->server->module_config,                                                 &disk_cache_module);    /* We write to a temp file and then atomically rename the file over     * in file_cache_el_final().     */    if (!dobj->tfd) {        rv = apr_file_mktemp(&dobj->tfd, dobj->tempfile,                             APR_CREATE | APR_WRITE | APR_BINARY |                             APR_BUFFERED | APR_EXCL, r->pool);        if (rv != APR_SUCCESS) {            return rv;        }        dobj->file_size = 0;    }    for (e = APR_BRIGADE_FIRST(bb);         e != APR_BRIGADE_SENTINEL(bb);         e = APR_BUCKET_NEXT(e))    {        const char *str;        apr_size_t length, written;        rv = apr_bucket_read(e, &str, &length, APR_BLOCK_READ);        if (rv != APR_SUCCESS) {            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,                         "cache_disk: Error when reading bucket for URL %s",                         h->cache_obj->key);            /* Remove the intermediate cache file and return non-APR_SUCCESS */            file_cache_errorcleanup(dobj, r);            return rv;        }        rv = apr_file_write_full(dobj->tfd, str, length, &written);        if (rv != APR_SUCCESS) {            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,                         "cache_disk: Error when writing cache file for URL %s",                         h->cache_obj->key);            /* Remove the intermediate cache file and return non-APR_SUCCESS */            file_cache_errorcleanup(dobj, r);            return rv;        }        dobj->file_size += written;        if (dobj->file_size > conf->maxfs) {            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,                         "cache_disk: URL %s failed the size check "                         "(%" APR_OFF_T_FMT " > %" APR_OFF_T_FMT ")",                         h->cache_obj->key, dobj->file_size, conf->maxfs);            /* Remove the intermediate cache file and return non-APR_SUCCESS */            file_cache_errorcleanup(dobj, r);            return APR_EGENERAL;        }    }    /* Was this the final bucket? If yes, close the temp file and perform     * sanity checks.     */    if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {        if (r->connection->aborted || r->no_cache) {            ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,                         "disk_cache: Discarding body for URL %s "                         "because connection has been aborted.",                         h->cache_obj->key);            /* Remove the intermediate cache file and return non-APR_SUCCESS */            file_cache_errorcleanup(dobj, r);            return APR_EGENERAL;        }        if (dobj->file_size < conf->minfs) {            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,                         "cache_disk: URL %s failed the size check "                         "(%" APR_OFF_T_FMT " < %" APR_OFF_T_FMT ")",                         h->cache_obj->key, dobj->file_size, conf->minfs);            /* Remove the intermediate cache file and return non-APR_SUCCESS */            file_cache_errorcleanup(dobj, r);            return APR_EGENERAL;        }        /* All checks were fine. Move tempfile to final destination */        /* Link to the perm file, and close the descriptor */        file_cache_el_final(dobj, r);        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,                     "disk_cache: Body for URL %s cached.",  dobj->name);    }    return APR_SUCCESS;}static void *create_config(apr_pool_t *p, server_rec *s){    disk_cache_conf *conf = apr_pcalloc(p, sizeof(disk_cache_conf));    /* XXX: Set default values */    conf->dirlevels = DEFAULT_DIRLEVELS;    conf->dirlength = DEFAULT_DIRLENGTH;    conf->maxfs = DEFAULT_MAX_FILE_SIZE;    conf->minfs = DEFAULT_MIN_FILE_SIZE;    conf->cache_root = NULL;    conf->cache_root_len = 0;    return conf;}/* * mod_disk_cache configuration directives handlers. */static const char*set_cache_root(cmd_parms *parms, void *in_struct_ptr, const char *arg){    disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,                                                 &disk_cache_module);    conf->cache_root = arg;    conf->cache_root_len = strlen(arg);    /* TODO: canonicalize cache_root and strip off any trailing slashes */    return NULL;}/* * Consider eliminating the next two directives in favor of * Ian's prime number hash... * key = hash_fn( r->uri) * filename = "/key % prime1 /key %prime2/key %prime3" */static const char*set_cache_dirlevels(cmd_parms *parms, void *in_struct_ptr, const char *arg){    disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,                                                 &disk_cache_module);    int val = atoi(arg);    if (val < 1)        return "CacheDirLevels value must be an integer greater than 0";    if (val * conf->dirlength > CACHEFILE_LEN)        return "CacheDirLevels*CacheDirLength value must not be higher than 20";    conf->dirlevels = val;    return NULL;}static const char*set_cache_dirlength(cmd_parms *parms, void *in_struct_ptr, const char *arg){    disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,                                                 &disk_cache_module);    int val = atoi(arg);    if (val < 1)        return "CacheDirLength value must be an integer greater than 0";    if (val * conf->dirlevels > CACHEFILE_LEN)        return "CacheDirLevels*CacheDirLength value must not be higher than 20";    conf->dirlength = val;    return NULL;}static const char*set_cache_minfs(cmd_parms *parms, void *in_struct_ptr, const char *arg){    disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,                                                 &disk_cache_module);    if (apr_strtoff(&conf->minfs, arg, NULL, 0) != APR_SUCCESS ||            conf->minfs < 0)     {        return "CacheMinFileSize argument must be a non-negative integer representing the min size of a file to cache in bytes.";    }    return NULL;}static const char*set_cache_maxfs(cmd_parms *parms, void *in_struct_ptr, const char *arg){    disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,                                                 &disk_cache_module);    if (apr_strtoff(&conf->maxfs, arg, NULL, 0) != APR_SUCCESS ||            conf->maxfs < 0)     {        return "CacheMaxFileSize argument must be a non-negative integer representing the max size of a file to cache in bytes.";    }    return NULL;}static const command_rec disk_cache_cmds[] ={    AP_INIT_TAKE1("CacheRoot", set_cache_root, NULL, RSRC_CONF,                 "The directory to store cache files"),    AP_INIT_TAKE1("CacheDirLevels", set_cache_dirlevels, NULL, RSRC_CONF,                  "The number of levels of subdirectories in the cache"),    AP_INIT_TAKE1("CacheDirLength", set_cache_dirlength, NULL, RSRC_CONF,                  "The number of characters in subdirectory names"),    AP_INIT_TAKE1("CacheMinFileSize", set_cache_minfs, NULL, RSRC_CONF,                  "The minimum file size to cache a document"),    AP_INIT_TAKE1("CacheMaxFileSize", set_cache_maxfs, NULL, RSRC_CONF,                  "The maximum file size to cache a document"),    {NULL}};static const cache_provider cache_disk_provider ={    &remove_entity,    &store_headers,    &store_body,    &recall_headers,    &recall_body,    &create_entity,    &open_entity,    &remove_url,};static void disk_cache_register_hook(apr_pool_t *p){    /* cache initializer */    ap_register_provider(p, CACHE_PROVIDER_GROUP, "disk", "0",                         &cache_disk_provider);}module AP_MODULE_DECLARE_DATA disk_cache_module = {    STANDARD20_MODULE_STUFF,    NULL,                       /* create per-directory config structure */    NULL,                       /* merge per-directory config structures */    create_config,              /* create per-server config structure */    NULL,                       /* merge per-server config structures */    disk_cache_cmds,            /* command apr_table_t */    disk_cache_register_hook    /* register hooks */};

⌨️ 快捷键说明

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