📄 mod_cache.c
字号:
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed 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. */#define CORE_PRIVATE#include "mod_cache.h"module AP_MODULE_DECLARE_DATA cache_module;APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;/* -------------------------------------------------------------- *//* Handles for cache filters, resolved at startup to eliminate * a name-to-function mapping on each request */static ap_filter_rec_t *cache_save_filter_handle;static ap_filter_rec_t *cache_out_filter_handle;/* * CACHE handler * ------------- * * Can we deliver this request from the cache? * If yes: * deliver the content by installing the CACHE_OUT filter. * If no: * check whether we're allowed to try cache it * If yes: * add CACHE_SAVE filter * If No: * oh well. */static int cache_url_handler(request_rec *r, int lookup){ apr_status_t rv; const char *pragma, *auth; apr_uri_t uri; char *url; char *path; cache_provider_list *providers; cache_info *info; cache_request_rec *cache; cache_server_conf *conf; apr_bucket_brigade *out; /* Delay initialization until we know we are handling a GET */ if (r->method_number != M_GET) { return DECLINED; } uri = r->parsed_uri; url = r->unparsed_uri; path = uri.path; info = NULL; conf = (cache_server_conf *) ap_get_module_config(r->server->module_config, &cache_module); /* * Which cache module (if any) should handle this request? */ if (!(providers = ap_cache_get_providers(r, conf, path))) { return DECLINED; } /* make space for the per request config */ cache = (cache_request_rec *) ap_get_module_config(r->request_config, &cache_module); if (!cache) { cache = apr_pcalloc(r->pool, sizeof(cache_request_rec)); ap_set_module_config(r->request_config, &cache_module, cache); } /* save away the possible providers */ cache->providers = providers; /* * Are we allowed to serve cached info at all? */ /* find certain cache controlling headers */ pragma = apr_table_get(r->headers_in, "Pragma"); auth = apr_table_get(r->headers_in, "Authorization"); /* first things first - does the request allow us to return * cached information at all? If not, just decline the request. * * Note that there is a big difference between not being allowed * to cache a request (no-store) and not being allowed to return * a cached request without revalidation (max-age=0). * * Caching is forbidden under the following circumstances: * * - RFC2616 14.9.2 Cache-Control: no-store * - Pragma: no-cache * - Any requests requiring authorization. */ if (conf->ignorecachecontrol == 1 && auth == NULL) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "incoming request is asking for a uncached version of " "%s, but we know better and are ignoring it", url); } else { if (ap_cache_liststr(NULL, pragma, "no-cache", NULL) || auth != NULL) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "cache: no-cache or authorization forbids caching " "of %s", url); return DECLINED; } } /* * Try to serve this request from the cache. * * If no existing cache file (DECLINED) * add cache_save filter * If cached file (OK) * clear filter stack * add cache_out filter * return OK */ rv = cache_select_url(r, url); if (rv != OK) { if (rv == DECLINED) { if (!lookup) { /* add cache_save filter to cache this request */ ap_add_output_filter_handle(cache_save_filter_handle, NULL, r, r->connection); } } else { /* error */ ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server, "cache: error returned while checking for cached " "file by %s cache", cache->provider_name); } return DECLINED; } /* We have located a suitable cache file now. */ info = &(cache->handle->cache_obj->info); if (info && info->lastmod) { ap_update_mtime(r, info->lastmod); } rv = ap_meets_conditions(r); if (rv != OK) { /* Return cached status. */ return rv; } /* If we're a lookup, we can exit now instead of serving the content. */ if (lookup) { return OK; } /* Serve up the content */ /* We are in the quick handler hook, which means that no output * filters have been set. So lets run the insert_filter hook. */ ap_run_insert_filter(r); ap_add_output_filter_handle(cache_out_filter_handle, NULL, r, r->connection); /* kick off the filter stack */ out = apr_brigade_create(r->pool, r->connection->bucket_alloc); rv = ap_pass_brigade(r->output_filters, out); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server, "cache: error returned while trying to return %s " "cached data", cache->provider_name); return rv; } return OK;}/* * CACHE_OUT filter * ---------------- * * Deliver cached content (headers and body) up the stack. */static int cache_out_filter(ap_filter_t *f, apr_bucket_brigade *bb){ request_rec *r = f->r; cache_request_rec *cache; cache = (cache_request_rec *) ap_get_module_config(r->request_config, &cache_module); if (!cache) { /* user likely configured CACHE_OUT manually; they should use mod_cache * configuration to do that */ ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "CACHE_OUT enabled unexpectedly"); ap_remove_output_filter(f); return ap_pass_brigade(f->next, bb); } ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server, "cache: running CACHE_OUT filter"); /* recall_headers() was called in cache_select_url() */ cache->provider->recall_body(cache->handle, r->pool, bb); /* This filter is done once it has served up its content */ ap_remove_output_filter(f); ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server, "cache: serving %s", r->uri); return ap_pass_brigade(f->next, bb);}/* * CACHE_SAVE filter * --------------- * * Decide whether or not this content should be cached. * If we decide no it should not: * remove the filter from the chain * If we decide yes it should: * Have we already started saving the response? * If we have started, pass the data to the storage manager via store_body * Otherwise: * Check to see if we *can* save this particular response. * If we can, call cache_create_entity() and save the headers and body * Finally, pass the data to the next filter (the network or whatever) */static int cache_save_filter(ap_filter_t *f, apr_bucket_brigade *in){ int rv; int date_in_errhdr = 0; request_rec *r = f->r; cache_request_rec *cache; cache_server_conf *conf; char *url = r->unparsed_uri; const char *cc_in, *cc_out, *cl; const char *exps, *lastmods, *dates, *etag; apr_time_t exp, date, lastmod, now; apr_off_t size; cache_info *info; char *reason; apr_pool_t *p; /* check first whether running this filter has any point or not */ /* If the user has Cache-Control: no-store from RFC 2616, don't store! */ cc_in = apr_table_get(r->headers_in, "Cache-Control"); if (r->no_cache || ap_cache_liststr(NULL, cc_in, "no-store", NULL)) { ap_remove_output_filter(f); return ap_pass_brigade(f->next, in); } /* Setup cache_request_rec */ cache = (cache_request_rec *) ap_get_module_config(r->request_config, &cache_module); if (!cache) { /* user likely configured CACHE_SAVE manually; they should really use * mod_cache configuration to do that */ cache = apr_pcalloc(r->pool, sizeof(cache_request_rec)); ap_set_module_config(r->request_config, &cache_module, cache); } reason = NULL; p = r->pool; /* * Pass Data to Cache * ------------------ * This section passes the brigades into the cache modules, but only * if the setup section (see below) is complete. */ if (cache->block_response) { /* We've already sent down the response and EOS. So, ignore * whatever comes now. */ return APR_SUCCESS; } /* have we already run the cachability check and set up the * cached file handle? */ if (cache->in_checked) { /* pass the brigades into the cache, then pass them * up the filter stack */ rv = cache->provider->store_body(cache->handle, r, in); if (rv != APR_SUCCESS) { ap_remove_output_filter(f); } return ap_pass_brigade(f->next, in); } /* * Setup Data in Cache * ------------------- * This section opens the cache entity and sets various caching * parameters, and decides whether this URL should be cached at * all. This section is* run before the above section. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -