📄 mod_cache.c
字号:
/* 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. */#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; ap_filter_t *next; ap_filter_rec_t *cache_out_handle; /* 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) { cache_out_handle = cache_out_subreq_filter_handle; } else { cache_out_handle = cache_out_filter_handle; } ap_add_output_filter_handle(cache_out_handle, NULL, r, r->connection); /* * Remove all filters that are before the cache_out filter. This ensures * that we kick off the filter stack with our cache_out filter being the * first in the chain. This make sense because we want to restore things * in the same manner as we saved them. * There may be filters before our cache_out filter, because * * 1. We call ap_set_content_type during cache_select. This causes * Content-Type specific filters to be added. * 2. We call the insert_filter hook. This causes filters e.g. like * the ones set with SetOutputFilter to be added. */ next = r->output_filters; while (next && (next->frec != cache_out_handle)) { ap_remove_output_filter(next); next = next->next; } /* 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) { if (rv != AP_FILTER_ERROR) { 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; 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -