mod_cache.c
来自「apache服务器源代码(版本号:2.2.2)」· C语言 代码 · 共 1,250 行 · 第 1/3 页
C
1,250 行
{ request_rec *r = f->r; cache_request_rec *cache; /* Setup cache_request_rec */ cache = (cache_request_rec *) f->ctx; if (!cache) { /* user likely configured CACHE_REMOVE_URL manually; they should really * use mod_cache configuration to do that. So: * 1. Remove ourselves * 2. Do nothing and bail out */ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "cache: CACHE_REMOVE_URL enabled unexpectedly"); ap_remove_output_filter(f); return ap_pass_brigade(f->next, in); } /* Now remove this cache entry from the cache */ cache_remove_url(cache, r->pool); /* remove ourselves */ ap_remove_output_filter(f); return ap_pass_brigade(f->next, in);}/* -------------------------------------------------------------- *//* Setup configurable data */static void * create_cache_config(apr_pool_t *p, server_rec *s){ cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf)); /* array of URL prefixes for which caching is enabled */ ps->cacheenable = apr_array_make(p, 10, sizeof(struct cache_enable)); /* array of URL prefixes for which caching is disabled */ ps->cachedisable = apr_array_make(p, 10, sizeof(struct cache_disable)); /* maximum time to cache a document */ ps->maxex = DEFAULT_CACHE_MAXEXPIRE; ps->maxex_set = 0; /* default time to cache a document */ ps->defex = DEFAULT_CACHE_EXPIRE; ps->defex_set = 0; /* factor used to estimate Expires date from LastModified date */ ps->factor = DEFAULT_CACHE_LMFACTOR; ps->factor_set = 0; ps->no_last_mod_ignore_set = 0; ps->no_last_mod_ignore = 0; ps->ignorecachecontrol = 0; ps->ignorecachecontrol_set = 0; ps->store_private = 0; ps->store_private_set = 0; ps->store_nostore = 0; ps->store_nostore_set = 0; /* array of headers that should not be stored in cache */ ps->ignore_headers = apr_array_make(p, 10, sizeof(char *)); ps->ignore_headers_set = CACHE_IGNORE_HEADERS_UNSET; return ps;}static void * merge_cache_config(apr_pool_t *p, void *basev, void *overridesv){ cache_server_conf *ps = apr_pcalloc(p, sizeof(cache_server_conf)); cache_server_conf *base = (cache_server_conf *) basev; cache_server_conf *overrides = (cache_server_conf *) overridesv; /* array of URL prefixes for which caching is disabled */ ps->cachedisable = apr_array_append(p, base->cachedisable, overrides->cachedisable); /* array of URL prefixes for which caching is enabled */ ps->cacheenable = apr_array_append(p, base->cacheenable, overrides->cacheenable); /* maximum time to cache a document */ ps->maxex = (overrides->maxex_set == 0) ? base->maxex : overrides->maxex; /* default time to cache a document */ ps->defex = (overrides->defex_set == 0) ? base->defex : overrides->defex; /* factor used to estimate Expires date from LastModified date */ ps->factor = (overrides->factor_set == 0) ? base->factor : overrides->factor; ps->no_last_mod_ignore = (overrides->no_last_mod_ignore_set == 0) ? base->no_last_mod_ignore : overrides->no_last_mod_ignore; ps->ignorecachecontrol = (overrides->ignorecachecontrol_set == 0) ? base->ignorecachecontrol : overrides->ignorecachecontrol; ps->store_private = (overrides->store_private_set == 0) ? base->store_private : overrides->store_private; ps->store_nostore = (overrides->store_nostore_set == 0) ? base->store_nostore : overrides->store_nostore; ps->ignore_headers = (overrides->ignore_headers_set == CACHE_IGNORE_HEADERS_UNSET) ? base->ignore_headers : overrides->ignore_headers; return ps;}static const char *set_cache_ignore_no_last_mod(cmd_parms *parms, void *dummy, int flag){ cache_server_conf *conf; conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); conf->no_last_mod_ignore = flag; conf->no_last_mod_ignore_set = 1; return NULL;}static const char *set_cache_ignore_cachecontrol(cmd_parms *parms, void *dummy, int flag){ cache_server_conf *conf; conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); conf->ignorecachecontrol = flag; conf->ignorecachecontrol_set = 1; return NULL;}static const char *set_cache_store_private(cmd_parms *parms, void *dummy, int flag){ cache_server_conf *conf; conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); conf->store_private = flag; conf->store_private_set = 1; return NULL;}static const char *set_cache_store_nostore(cmd_parms *parms, void *dummy, int flag){ cache_server_conf *conf; conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); conf->store_nostore = flag; conf->store_nostore_set = 1; return NULL;}static const char *add_ignore_header(cmd_parms *parms, void *dummy, const char *header){ cache_server_conf *conf; char **new; conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); if (!strncasecmp(header, "None", 4)) { /* if header None is listed clear array */ conf->ignore_headers->nelts = 0; } else { if ((conf->ignore_headers_set == CACHE_IGNORE_HEADERS_UNSET) || (conf->ignore_headers->nelts)) { /* Only add header if no "None" has been found in header list * so far. * (When 'None' is passed, IGNORE_HEADERS_SET && nelts == 0.) */ new = (char **)apr_array_push(conf->ignore_headers); (*new) = (char *)header; } } conf->ignore_headers_set = CACHE_IGNORE_HEADERS_SET; return NULL;}static const char *add_cache_enable(cmd_parms *parms, void *dummy, const char *type, const char *url){ cache_server_conf *conf; struct cache_enable *new; if (*type == '/') { return apr_psprintf(parms->pool, "provider (%s) starts with a '/'. Are url and provider switched?", type); } conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); new = apr_array_push(conf->cacheenable); new->type = type; if (apr_uri_parse(parms->pool, url, &(new->url))) { return NULL; } if (new->url.path) { new->pathlen = strlen(new->url.path); } else { new->pathlen = 1; new->url.path = "/"; } return NULL;}static const char *add_cache_disable(cmd_parms *parms, void *dummy, const char *url){ cache_server_conf *conf; struct cache_disable *new; conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); new = apr_array_push(conf->cachedisable); if (apr_uri_parse(parms->pool, url, &(new->url))) { return NULL; } if (new->url.path) { new->pathlen = strlen(new->url.path); } else { new->pathlen = 1; new->url.path = "/"; } return NULL;}static const char *set_cache_maxex(cmd_parms *parms, void *dummy, const char *arg){ cache_server_conf *conf; conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); conf->maxex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC); conf->maxex_set = 1; return NULL;}static const char *set_cache_defex(cmd_parms *parms, void *dummy, const char *arg){ cache_server_conf *conf; conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); conf->defex = (apr_time_t) (atol(arg) * MSEC_ONE_SEC); conf->defex_set = 1; return NULL;}static const char *set_cache_factor(cmd_parms *parms, void *dummy, const char *arg){ cache_server_conf *conf; double val; conf = (cache_server_conf *)ap_get_module_config(parms->server->module_config, &cache_module); if (sscanf(arg, "%lg", &val) != 1) { return "CacheLastModifiedFactor value must be a float"; } conf->factor = val; conf->factor_set = 1; return NULL;}static int cache_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s){ /* This is the means by which unusual (non-unix) os's may find alternate * means to run a given command (e.g. shebang/registry parsing on Win32) */ cache_generate_key = APR_RETRIEVE_OPTIONAL_FN(ap_cache_generate_key); if (!cache_generate_key) { cache_generate_key = cache_generate_key_default; } return OK;}static const command_rec cache_cmds[] ={ /* XXX * Consider a new config directive that enables loading specific cache * implememtations (like mod_cache_mem, mod_cache_file, etc.). * Rather than using a LoadModule directive, admin would use something * like CacheModule mem_cache_module | file_cache_module, etc, * which would cause the approprpriate cache module to be loaded. * This is more intuitive that requiring a LoadModule directive. */ AP_INIT_TAKE2("CacheEnable", add_cache_enable, NULL, RSRC_CONF, "A cache type and partial URL prefix below which " "caching is enabled"), AP_INIT_TAKE1("CacheDisable", add_cache_disable, NULL, RSRC_CONF, "A partial URL prefix below which caching is disabled"), AP_INIT_TAKE1("CacheMaxExpire", set_cache_maxex, NULL, RSRC_CONF, "The maximum time in seconds to cache a document"), AP_INIT_TAKE1("CacheDefaultExpire", set_cache_defex, NULL, RSRC_CONF, "The default time in seconds to cache a document"), AP_INIT_FLAG("CacheIgnoreNoLastMod", set_cache_ignore_no_last_mod, NULL, RSRC_CONF, "Ignore Responses where there is no Last Modified Header"), AP_INIT_FLAG("CacheIgnoreCacheControl", set_cache_ignore_cachecontrol, NULL, RSRC_CONF, "Ignore requests from the client for uncached content"), AP_INIT_FLAG("CacheStorePrivate", set_cache_store_private, NULL, RSRC_CONF, "Ignore 'Cache-Control: private' and store private content"), AP_INIT_FLAG("CacheStoreNoStore", set_cache_store_nostore, NULL, RSRC_CONF, "Ignore 'Cache-Control: no-store' and store sensitive content"), AP_INIT_ITERATE("CacheIgnoreHeaders", add_ignore_header, NULL, RSRC_CONF, "A space separated list of headers that should not be " "stored by the cache"), AP_INIT_TAKE1("CacheLastModifiedFactor", set_cache_factor, NULL, RSRC_CONF, "The factor used to estimate Expires date from " "LastModified date"), {NULL}};static void register_hooks(apr_pool_t *p){ /* cache initializer */ /* cache handler */ ap_hook_quick_handler(cache_url_handler, NULL, NULL, APR_HOOK_FIRST); /* cache filters * XXX The cache filters need to run right after the handlers and before * any other filters. Consider creating AP_FTYPE_CACHE for this purpose. * * Depending on the type of request (subrequest / main request) they * need to be run before AP_FTYPE_CONTENT_SET / after AP_FTYPE_CONTENT_SET * filters. Thus create two filter handles for each type: * cache_save_filter_handle / cache_out_filter_handle to be used by * main requests and * cache_save_subreq_filter_handle / cache_out_subreq_filter_handle * to be run by subrequest */ /* * CACHE_SAVE must go into the filter chain after a possible DEFLATE * filter to ensure that the compressed content is stored. * Incrementing filter type by 1 ensures his happens. */ cache_save_filter_handle = ap_register_output_filter("CACHE_SAVE", cache_save_filter, NULL, AP_FTYPE_CONTENT_SET+1); /* * CACHE_SAVE_SUBREQ must go into the filter chain before SUBREQ_CORE to * handle subrequsts. Decrementing filter type by 1 ensures this * happens. */ cache_save_subreq_filter_handle = ap_register_output_filter("CACHE_SAVE_SUBREQ", cache_save_filter, NULL, AP_FTYPE_CONTENT_SET-1); /* * CACHE_OUT must go into the filter chain after a possible DEFLATE * filter to ensure that already compressed cache objects do not * get compressed again. Incrementing filter type by 1 ensures * his happens. */ cache_out_filter_handle = ap_register_output_filter("CACHE_OUT", cache_out_filter, NULL, AP_FTYPE_CONTENT_SET+1); /* * CACHE_OUT_SUBREQ must go into the filter chain before SUBREQ_CORE to * handle subrequsts. Decrementing filter type by 1 ensures this * happens. */ cache_out_subreq_filter_handle = ap_register_output_filter("CACHE_OUT_SUBREQ", cache_out_filter, NULL, AP_FTYPE_CONTENT_SET-1); /* CACHE_REMOVE_URL has to be a protocol filter to ensure that is * run even if the response is a canned error message, which * removes the content filters. */ cache_remove_url_filter_handle = ap_register_output_filter("CACHE_REMOVE_URL", cache_remove_url_filter, NULL, AP_FTYPE_PROTOCOL); ap_hook_post_config(cache_post_config, NULL, NULL, APR_HOOK_REALLY_FIRST);}module AP_MODULE_DECLARE_DATA cache_module ={ STANDARD20_MODULE_STUFF, NULL, /* create per-directory config structure */ NULL, /* merge per-directory config structures */ create_cache_config, /* create per-server config structure */ merge_cache_config, /* merge per-server config structures */ cache_cmds, /* command apr_table_t */ register_hooks};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?