From 4c9ab928f36ec74a32497d74c0ce76ec11ea1a4e Mon Sep 17 00:00:00 2001 From: Justin Erenkrantz Date: Mon, 2 Aug 2004 17:25:24 +0000 Subject: [PATCH] * modules/experimental/mod_cache.c (cache_url_handler): Rewrite to be more understandable. Also, remove bogus checks for MAX_URL_LENGTH and declining '/'-terminated URLs. Reviewed by: Bill Stoddard git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@104448 13f79535-47bb-0310-9956-ffa450edef68 --- modules/experimental/mod_cache.c | 264 ++++++++++++------------------- 1 file changed, 97 insertions(+), 167 deletions(-) diff --git a/modules/experimental/mod_cache.c b/modules/experimental/mod_cache.c index f2bc9c5c0d..2e978000d2 100644 --- a/modules/experimental/mod_cache.c +++ b/modules/experimental/mod_cache.c @@ -49,23 +49,29 @@ static int cache_url_handler(request_rec *r, int lookup) { apr_status_t rv; const char *cc_in, *pragma, *auth; - apr_uri_t uri = r->parsed_uri; - char *url = r->unparsed_uri; + apr_uri_t uri; + char *url; apr_size_t urllen; - char *path = uri.path; + char *path; const char *types; - cache_info *info = NULL; + cache_info *info; cache_request_rec *cache; cache_server_conf *conf; + apr_bucket_brigade *out; - conf = (cache_server_conf *) ap_get_module_config(r->server->module_config, - &cache_module); - - /* we don't handle anything but GET */ + /* 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? */ @@ -73,19 +79,8 @@ static int cache_url_handler(request_rec *r, int lookup) return DECLINED; } - urllen = strlen(url); - if (urllen > MAX_URL_LENGTH) { - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, - "cache: URL exceeds length threshold: %s", url); - return DECLINED; - } - /* DECLINE urls ending in / ??? EGP: why? */ - if (url[urllen-1] == '/') { - return DECLINED; - } - /* make space for the per request config */ - cache = (cache_request_rec *) ap_get_module_config(r->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)); @@ -137,171 +132,106 @@ static int cache_url_handler(request_rec *r, int lookup) /* * Try to serve this request from the cache. * - * If no existing cache file + * If no existing cache file (DECLINED) * add cache_in filter - * If stale cache file - * If conditional request - * add cache_in filter - * If non-conditional request - * fudge response into a conditional - * add cache_conditional filter - * If fresh cache file - * clear filter stack - * add cache_out filter + * If cached file (OK) + * If fresh cache file + * clear filter stack + * add cache_out filter + * return OK + * If stale cache file + * add cache_conditional filter (which updates cache) */ rv = cache_select_url(r, cache->types, url); - if (DECLINED == rv) { - if (!lookup) { - /* no existing cache file */ - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, - "cache: no cache - add cache_in filter and DECLINE"); - /* add cache_in filter to cache this request */ - ap_add_output_filter_handle(cache_in_filter_handle, NULL, r, - r->connection); + if (rv != OK) { + if (rv == DECLINED) { + if (!lookup) { + /* add cache_in filter to cache this request */ + ap_add_output_filter_handle(cache_in_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->type); } return DECLINED; } - else if (OK == rv) { - /* RFC2616 13.2 - Check cache object expiration */ - cache->fresh = ap_cache_check_freshness(cache, r); - if (cache->fresh) { - /* fresh data available */ - apr_bucket_brigade *out; - conn_rec *c = r->connection; - - if (lookup) { - return OK; - } - info = &(cache->handle->cache_obj->info); + /* We have located a suitable cache file now. */ - if (info && info->lastmod) { - ap_update_mtime(r, info->lastmod); - } + /* RFC2616 13.2 - Check cache object expiration */ + cache->fresh = ap_cache_check_freshness(cache, r); - rv = ap_meets_conditions(r); - if (rv != OK) { - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, - "cache: fresh cache - returning status %d", rv); - return rv; - } + /* What we have in our cache isn't fresh. */ + if (!cache->fresh) { + /* If our stale cached response was conditional... */ + if (!lookup && ap_cache_request_is_conditional(r)) { + info = &(cache->handle->cache_obj->info); - /* - * Not a conditionl request. Serve up the content - */ - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, - "cache: fresh cache - add cache_out filter and " - "handle request"); - - /* 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, c->bucket_alloc); - if (APR_SUCCESS - != (rv = ap_pass_brigade(r->output_filters, out))) { - ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server, - "cache: error returned while trying to return %s " - "cached data", - cache->type); - return rv; + /* fudge response into a conditional */ + if (info && info->etag) { + /* if we have a cached etag */ + apr_table_set(r->headers_in, "If-None-Match", info->etag); } - return OK; - } - else { - if (!r->err_headers_out) { - r->err_headers_out = apr_table_make(r->pool, 3); - } - /* stale data available */ - if (lookup) { - return DECLINED; + else if (info && info->lastmods) { + /* if we have a cached IMS */ + apr_table_set(r->headers_in, "If-Modified-Since", + info->lastmods); } + } - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, - "cache: stale cache - test conditional"); - /* if conditional request */ - if (ap_cache_request_is_conditional(r)) { - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - r->server, - "cache: conditional - add cache_in filter and " - "DECLINE"); - /* Why not add CACHE_CONDITIONAL? */ - ap_add_output_filter_handle(cache_in_filter_handle, NULL, - r, r->connection); - - return DECLINED; - } - /* else if non-conditional request */ - else { - /* Temporarily hack this to work the way it had been. Its broken, - * but its broken the way it was before. I'm working on figuring - * out why the filter add in the conditional filter doesn't work. pjr - * - * info = &(cache->handle->cache_obj->info); - * - * Uncomment the above when the code in cache_conditional_filter_handle - * is properly fixed... pjr - */ - - /* fudge response into a conditional */ - if (info && info->etag) { - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - r->server, - "cache: nonconditional - fudge conditional " - "by etag"); - /* if we have a cached etag */ - apr_table_set(r->headers_in, "If-None-Match", info->etag); - } - else if (info && info->lastmods) { - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - r->server, - "cache: nonconditional - fudge conditional " - "by lastmod"); - /* if we have a cached IMS */ - apr_table_set(r->headers_in, - "If-Modified-Since", - info->lastmods); - } - else { - /* something else - pretend there was no cache */ - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - r->server, - "cache: nonconditional - no cached " - "etag/lastmods - add cache_in and DECLINE"); - - ap_add_output_filter_handle(cache_in_filter_handle, NULL, - r, r->connection); - - return DECLINED; - } - /* add cache_conditional filter */ - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - r->server, - "cache: nonconditional - add cache_conditional " - "and DECLINE"); - ap_add_output_filter_handle(cache_conditional_filter_handle, - NULL, - r, - r->connection); + /* Add cache_conditional_filter to see if we can salvage + * later. + */ + ap_add_output_filter_handle(cache_conditional_filter_handle, + NULL, r, r->connection); + return DECLINED; + } - return DECLINED; - } - } + /* fresh data available */ + + info = &(cache->handle->cache_obj->info); + + if (info && info->lastmod) { + ap_update_mtime(r, info->lastmod); } - else { - /* error */ - ap_log_error(APLOG_MARK, APLOG_ERR, rv, - r->server, - "cache: error returned while checking for cached file by " - "%s cache", + + 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->type); - return DECLINED; + return rv; } + + return OK; } /* -- 2.40.0