mod_cache.c
来自「apache服务器源代码(版本号:2.2.2)」· C语言 代码 · 共 1,250 行 · 第 1/3 页
C
1,250 行
/* 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_save_subreq_filter_handle;static ap_filter_rec_t *cache_out_filter_handle;static ap_filter_rec_t *cache_out_subreq_filter_handle;static ap_filter_rec_t *cache_remove_url_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 *auth; cache_provider_list *providers; 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; } 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, r->parsed_uri))) { 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 */ 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. */ if (auth) { 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(r); if (rv != OK) { if (rv == DECLINED) { if (!lookup) { /* * Add cache_save filter to cache this request. Choose * the correct filter by checking if we are a subrequest * or not. */ if (r->main) { ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server, "Adding CACHE_SAVE_SUBREQ filter for %s", r->uri); ap_add_output_filter_handle(cache_save_subreq_filter_handle, NULL, r, r->connection); } else { ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server, "Adding CACHE_SAVE filter for %s", r->uri); ap_add_output_filter_handle(cache_save_filter_handle, NULL, r, r->connection); } ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server, "Adding CACHE_REMOVE_URL filter for %s", r->uri); /* Add cache_remove_url filter to this request to remove a * stale cache entry if needed. Also put the current cache * request rec in the filter context, as the request that * is available later during running the filter maybe * different due to an internal redirect. */ cache->remove_url_filter = ap_add_output_filter_handle(cache_remove_url_filter_handle, cache, r, r->connection); } else { if (cache->stale_headers) { ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server, "Restoring request headers for %s", r->uri); r->headers_in = cache->stale_headers; } /* Delete our per-request configuration. */ ap_set_module_config(r->request_config, &cache_module, NULL); } } 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; } /* if we are a lookup, we are exiting soon one way or another; Restore * the headers. */ if (lookup) { if (cache->stale_headers) { ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server, "Restoring request headers."); r->headers_in = cache->stale_headers; } /* Delete our per-request configuration. */ ap_set_module_config(r->request_config, &cache_module, NULL); } rv = ap_meets_conditions(r); if (rv != OK) { /* If we are a lookup, we have to return DECLINED as we have no * way of knowing if we will be able to serve the content. */ if (lookup) { return DECLINED; } /* 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); /* * Add cache_out filter to serve this request. Choose * the correct filter by checking if we are a subrequest * or not. */ if (r->main) { ap_add_output_filter_handle(cache_out_subreq_filter_handle, NULL, r, r->connection); } else { 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"); /* restore status of cached response */ /* XXX: This exposes a bug in mem_cache, since it does not * restore the status into it's handle. */ r->status = cache->handle->cache_obj->info.status; /* recall_headers() was called in cache_select() */ 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 = !OK; int date_in_errhdr = 0; request_rec *r = f->r; cache_request_rec *cache; cache_server_conf *conf; const char *cc_out, *cl; const char *exps, *lastmods, *dates, *etag; apr_time_t exp, date, lastmod, now; apr_off_t size; cache_info *info = NULL; char *reason; apr_pool_t *p; conf = (cache_server_conf *) ap_get_module_config(r->server->module_config, &cache_module); /* 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_log_error(APLOG_MARK, APLOG_DEBUG, rv, r->server, "cache: Cache provider's store_body failed!"); 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. */ /* read expiry date; if a bad date, then leave it so the client can * read it */ exps = apr_table_get(r->err_headers_out, "Expires"); if (exps == NULL) { exps = apr_table_get(r->headers_out, "Expires"); } if (exps != NULL) { if (APR_DATE_BAD == (exp = apr_date_parse_http(exps))) { exps = NULL; } } else { exp = APR_DATE_BAD; } /* read the last-modified date; if the date is bad, then delete it */ lastmods = apr_table_get(r->err_headers_out, "Last-Modified"); if (lastmods == NULL) { lastmods = apr_table_get(r->headers_out, "Last-Modified"); } if (lastmods != NULL) { lastmod = apr_date_parse_http(lastmods); if (lastmod == APR_DATE_BAD) { lastmods = NULL; } } else { lastmod = APR_DATE_BAD; } /* read the etag and cache-control from the entity */ etag = apr_table_get(r->err_headers_out, "Etag"); if (etag == NULL) { etag = apr_table_get(r->headers_out, "Etag"); } cc_out = apr_table_get(r->err_headers_out, "Cache-Control"); if (cc_out == NULL) { cc_out = apr_table_get(r->headers_out, "Cache-Control"); } /* * what responses should we not cache? * * At this point we decide based on the response headers whether it * is appropriate _NOT_ to cache the data from the server. There are * a whole lot of conditions that prevent us from caching this data. * They are tested here one by one to be clear and unambiguous. */ if (r->status != HTTP_OK && r->status != HTTP_NON_AUTHORITATIVE && r->status != HTTP_MULTIPLE_CHOICES && r->status != HTTP_MOVED_PERMANENTLY && r->status != HTTP_NOT_MODIFIED) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?