Blame modules/cache/cache_storage.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "mod_cache.h"
Packit 90a5c9
Packit 90a5c9
#include "cache_storage.h"
Packit 90a5c9
#include "cache_util.h"
Packit 90a5c9
Packit 90a5c9
APLOG_USE_MODULE(cache);
Packit 90a5c9
Packit 90a5c9
extern APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
Packit 90a5c9
Packit 90a5c9
extern module AP_MODULE_DECLARE_DATA cache_module;
Packit 90a5c9
Packit 90a5c9
/* -------------------------------------------------------------- */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * delete all URL entities from the cache
Packit 90a5c9
 *
Packit 90a5c9
 */
Packit 90a5c9
int cache_remove_url(cache_request_rec *cache, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    cache_provider_list *list;
Packit 90a5c9
    cache_handle_t *h;
Packit 90a5c9
Packit 90a5c9
    list = cache->providers;
Packit 90a5c9
Packit 90a5c9
    /* Remove the stale cache entry if present. If not, we're
Packit 90a5c9
     * being called from outside of a request; remove the
Packit 90a5c9
     * non-stale handle.
Packit 90a5c9
     */
Packit 90a5c9
    h = cache->stale_handle ? cache->stale_handle : cache->handle;
Packit 90a5c9
    if (!h) {
Packit 90a5c9
       return OK;
Packit 90a5c9
    }
Packit 90a5c9
    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00691)
Packit 90a5c9
                 "cache: Removing url %s from the cache", h->cache_obj->key);
Packit 90a5c9
Packit 90a5c9
    /* for each specified cache type, delete the URL */
Packit 90a5c9
    while (list) {
Packit 90a5c9
        list->provider->remove_url(h, r);
Packit 90a5c9
        list = list->next;
Packit 90a5c9
    }
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * create a new URL entity in the cache
Packit 90a5c9
 *
Packit 90a5c9
 * It is possible to store more than once entity per URL. This
Packit 90a5c9
 * function will always create a new entity, regardless of whether
Packit 90a5c9
 * other entities already exist for the same URL.
Packit 90a5c9
 *
Packit 90a5c9
 * The size of the entity is provided so that a cache module can
Packit 90a5c9
 * decide whether or not it wants to cache this particular entity.
Packit 90a5c9
 * If the size is unknown, a size of -1 should be set.
Packit 90a5c9
 */
Packit 90a5c9
int cache_create_entity(cache_request_rec *cache, request_rec *r,
Packit 90a5c9
                        apr_off_t size, apr_bucket_brigade *in)
Packit 90a5c9
{
Packit 90a5c9
    cache_provider_list *list;
Packit 90a5c9
    cache_handle_t *h = apr_pcalloc(r->pool, sizeof(cache_handle_t));
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    if (!cache) {
Packit 90a5c9
        /* This should never happen */
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, r, APLOGNO(00692)
Packit 90a5c9
                "cache: No cache request information available for key"
Packit 90a5c9
                " generation");
Packit 90a5c9
        return APR_EGENERAL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!cache->key) {
Packit 90a5c9
        rv = cache_generate_key(r, r->pool, &cache->key);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    list = cache->providers;
Packit 90a5c9
    /* for each specified cache type, delete the URL */
Packit 90a5c9
    while (list) {
Packit 90a5c9
        switch (rv = list->provider->create_entity(h, r, cache->key, size, in)) {
Packit 90a5c9
        case OK: {
Packit 90a5c9
            cache->handle = h;
Packit 90a5c9
            cache->provider = list->provider;
Packit 90a5c9
            cache->provider_name = list->provider_name;
Packit 90a5c9
            return OK;
Packit 90a5c9
        }
Packit 90a5c9
        case DECLINED: {
Packit 90a5c9
            list = list->next;
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
        default: {
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int filter_header_do(void *v, const char *key, const char *val)
Packit 90a5c9
{
Packit 90a5c9
    if ((*key == 'W' || *key == 'w') && !ap_cstr_casecmp(key, "Warning")
Packit 90a5c9
            && *val == '1') {
Packit 90a5c9
        /* any stored Warning headers with warn-code 1xx (see section
Packit 90a5c9
         * 14.46) MUST be deleted from the cache entry and the forwarded
Packit 90a5c9
         * response.
Packit 90a5c9
         */
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        apr_table_addn(v, key, val);
Packit 90a5c9
    }
Packit 90a5c9
    return 1;
Packit 90a5c9
}
Packit 90a5c9
static int remove_header_do(void *v, const char *key, const char *val)
Packit 90a5c9
{
Packit 90a5c9
    if ((*key == 'W' || *key == 'w') && !ap_cstr_casecmp(key, "Warning")) {
Packit 90a5c9
        /* any stored Warning headers with warn-code 2xx MUST be retained
Packit 90a5c9
         * in the cache entry and the forwarded response.
Packit 90a5c9
         */
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        apr_table_unset(v, key);
Packit 90a5c9
    }
Packit 90a5c9
    return 1;
Packit 90a5c9
}
Packit 90a5c9
static int add_header_do(void *v, const char *key, const char *val)
Packit 90a5c9
{
Packit 90a5c9
    apr_table_addn(v, key, val);
Packit 90a5c9
    return 1;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Take two sets of headers, sandwich them together, and apply the result to
Packit 90a5c9
 * r->headers_out.
Packit 90a5c9
 *
Packit 90a5c9
 * To complicate this, a header may be duplicated in either table. Should a
Packit 90a5c9
 * header exist in the top table, all matching headers will be removed from
Packit 90a5c9
 * the bottom table before the headers are combined. The Warning headers are
Packit 90a5c9
 * handled specially. Warnings are added rather than being replaced, while
Packit 90a5c9
 * in the case of revalidation 1xx Warnings are stripped.
Packit 90a5c9
 *
Packit 90a5c9
 * The Content-Type and Last-Modified headers are then re-parsed and inserted
Packit 90a5c9
 * into the request.
Packit 90a5c9
 */
Packit 90a5c9
void cache_accept_headers(cache_handle_t *h, request_rec *r, apr_table_t *top,
Packit 90a5c9
        apr_table_t *bottom, int revalidation)
Packit 90a5c9
{
Packit 90a5c9
    const char *v;
Packit 90a5c9
Packit 90a5c9
    if (revalidation) {
Packit 90a5c9
        r->headers_out = apr_table_make(r->pool, 10);
Packit 90a5c9
        apr_table_do(filter_header_do, r->headers_out, bottom, NULL);
Packit 90a5c9
    }
Packit 90a5c9
    else if (r->headers_out != bottom) {
Packit 90a5c9
        r->headers_out = apr_table_copy(r->pool, bottom);
Packit 90a5c9
    }
Packit 90a5c9
    apr_table_do(remove_header_do, r->headers_out, top, NULL);
Packit 90a5c9
    apr_table_do(add_header_do, r->headers_out, top, NULL);
Packit 90a5c9
Packit 90a5c9
    v = apr_table_get(r->headers_out, "Content-Type");
Packit 90a5c9
    if (v) {
Packit 90a5c9
        ap_set_content_type(r, v);
Packit 90a5c9
        /*
Packit 90a5c9
         * Also unset possible Content-Type headers in r->headers_out and
Packit 90a5c9
         * r->err_headers_out as they may be different to what we have received
Packit 90a5c9
         * from the cache.
Packit 90a5c9
         * Actually they are not needed as r->content_type set by
Packit 90a5c9
         * ap_set_content_type above will be used in the store_headers functions
Packit 90a5c9
         * of the storage providers as a fallback and the HTTP_HEADER filter
Packit 90a5c9
         * does overwrite the Content-Type header with r->content_type anyway.
Packit 90a5c9
         */
Packit 90a5c9
        apr_table_unset(r->headers_out, "Content-Type");
Packit 90a5c9
        apr_table_unset(r->err_headers_out, "Content-Type");
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* If the cache gave us a Last-Modified header, we can't just
Packit 90a5c9
     * pass it on blindly because of restrictions on future values.
Packit 90a5c9
     */
Packit 90a5c9
    v = apr_table_get(r->headers_out, "Last-Modified");
Packit 90a5c9
    if (v) {
Packit 90a5c9
        ap_update_mtime(r, apr_date_parse_http(v));
Packit 90a5c9
        ap_set_last_modified(r);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * select a specific URL entity in the cache
Packit 90a5c9
 *
Packit 90a5c9
 * It is possible to store more than one entity per URL. Content
Packit 90a5c9
 * negotiation is used to select an entity. Once an entity is
Packit 90a5c9
 * selected, details of it are stored in the per request
Packit 90a5c9
 * config to save time when serving the request later.
Packit 90a5c9
 *
Packit 90a5c9
 * This function returns OK if successful, DECLINED if no
Packit 90a5c9
 * cached entity fits the bill.
Packit 90a5c9
 */
Packit 90a5c9
int cache_select(cache_request_rec *cache, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    cache_provider_list *list;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    cache_handle_t *h;
Packit 90a5c9
Packit 90a5c9
    if (!cache) {
Packit 90a5c9
        /* This should never happen */
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, r, APLOGNO(00693)
Packit 90a5c9
                "cache: No cache request information available for key"
Packit 90a5c9
                " generation");
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* if no-cache, we can't serve from the cache, but we may store to the
Packit 90a5c9
     * cache.
Packit 90a5c9
     */
Packit 90a5c9
    if (!ap_cache_check_no_cache(cache, r)) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!cache->key) {
Packit 90a5c9
        rv = cache_generate_key(r, r->pool, &cache->key);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* go through the cache types till we get a match */
Packit 90a5c9
    h = apr_palloc(r->pool, sizeof(cache_handle_t));
Packit 90a5c9
Packit 90a5c9
    list = cache->providers;
Packit 90a5c9
Packit 90a5c9
    while (list) {
Packit 90a5c9
        switch ((rv = list->provider->open_entity(h, r, cache->key))) {
Packit 90a5c9
        case OK: {
Packit 90a5c9
            char *vary = NULL;
Packit 90a5c9
            int mismatch = 0;
Packit 90a5c9
            char *last = NULL;
Packit 90a5c9
Packit 90a5c9
            if (list->provider->recall_headers(h, r) != APR_SUCCESS) {
Packit 90a5c9
                /* try again with next cache type */
Packit 90a5c9
                list = list->next;
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /*
Packit 90a5c9
             * Check Content-Negotiation - Vary
Packit 90a5c9
             *
Packit 90a5c9
             * At this point we need to make sure that the object we found in
Packit 90a5c9
             * the cache is the same object that would be delivered to the
Packit 90a5c9
             * client, when the effects of content negotiation are taken into
Packit 90a5c9
             * effect.
Packit 90a5c9
             *
Packit 90a5c9
             * In plain english, we want to make sure that a language-negotiated
Packit 90a5c9
             * document in one language is not given to a client asking for a
Packit 90a5c9
             * language negotiated document in a different language by mistake.
Packit 90a5c9
             *
Packit 90a5c9
             * This code makes the assumption that the storage manager will
Packit 90a5c9
             * cache the req_hdrs if the response contains a Vary
Packit 90a5c9
             * header.
Packit 90a5c9
             *
Packit 90a5c9
             * RFC2616 13.6 and 14.44 describe the Vary mechanism.
Packit 90a5c9
             */
Packit 90a5c9
            vary = cache_strqtok(
Packit 90a5c9
                    apr_pstrdup(r->pool,
Packit 90a5c9
                            cache_table_getm(r->pool, h->resp_hdrs, "Vary")),
Packit 90a5c9
                    CACHE_SEPARATOR, &last);
Packit 90a5c9
            while (vary) {
Packit 90a5c9
                const char *h1, *h2;
Packit 90a5c9
Packit 90a5c9
                /*
Packit 90a5c9
                 * is this header in the request and the header in the cached
Packit 90a5c9
                 * request identical? If not, we give up and do a straight get
Packit 90a5c9
                 */
Packit 90a5c9
                h1 = cache_table_getm(r->pool, r->headers_in, vary);
Packit 90a5c9
                h2 = cache_table_getm(r->pool, h->req_hdrs, vary);
Packit 90a5c9
                if (h1 == h2) {
Packit 90a5c9
                    /* both headers NULL, so a match - do nothing */
Packit 90a5c9
                }
Packit 90a5c9
                else if (h1 && h2 && !strcmp(h1, h2)) {
Packit 90a5c9
                    /* both headers exist and are equal - do nothing */
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    /* headers do not match, so Vary failed */
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
Packit 90a5c9
                            r, APLOGNO(00694) "cache_select(): Vary header mismatch.");
Packit 90a5c9
                    mismatch = 1;
Packit 90a5c9
                    break;
Packit 90a5c9
                }
Packit 90a5c9
                vary = cache_strqtok(NULL, CACHE_SEPARATOR, &last);
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* no vary match, try next provider */
Packit 90a5c9
            if (mismatch) {
Packit 90a5c9
                /* try again with next cache type */
Packit 90a5c9
                list = list->next;
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            cache->provider = list->provider;
Packit 90a5c9
            cache->provider_name = list->provider_name;
Packit 90a5c9
Packit 90a5c9
            /*
Packit 90a5c9
             * RFC2616 13.3.4 Rules for When to Use Entity Tags and Last-Modified
Packit 90a5c9
             * Dates: An HTTP/1.1 caching proxy, upon receiving a conditional request
Packit 90a5c9
             * that includes both a Last-Modified date and one or more entity tags as
Packit 90a5c9
             * cache validators, MUST NOT return a locally cached response to the
Packit 90a5c9
             * client unless that cached response is consistent with all of the
Packit 90a5c9
             * conditional header fields in the request.
Packit 90a5c9
             */
Packit 90a5c9
            if (ap_condition_if_match(r, h->resp_hdrs) == AP_CONDITION_NOMATCH
Packit 90a5c9
                    || ap_condition_if_unmodified_since(r, h->resp_hdrs)
Packit 90a5c9
                            == AP_CONDITION_NOMATCH
Packit 90a5c9
                    || ap_condition_if_none_match(r, h->resp_hdrs)
Packit 90a5c9
                            == AP_CONDITION_NOMATCH
Packit 90a5c9
                    || ap_condition_if_modified_since(r, h->resp_hdrs)
Packit 90a5c9
                            == AP_CONDITION_NOMATCH
Packit 90a5c9
                    || ap_condition_if_range(r, h->resp_hdrs) == AP_CONDITION_NOMATCH) {
Packit 90a5c9
                mismatch = 1;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Is our cached response fresh enough? */
Packit 90a5c9
            if (mismatch || !cache_check_freshness(h, cache, r)) {
Packit 90a5c9
                const char *etag, *lastmod;
Packit 90a5c9
Packit 90a5c9
                /* Cache-Control: only-if-cached and revalidation required, try
Packit 90a5c9
                 * the next provider
Packit 90a5c9
                 */
Packit 90a5c9
                if (cache->control_in.only_if_cached) {
Packit 90a5c9
                    /* try again with next cache type */
Packit 90a5c9
                    list = list->next;
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                /* set aside the stale entry for accessing later */
Packit 90a5c9
                cache->stale_headers = apr_table_copy(r->pool,
Packit 90a5c9
                        r->headers_in);
Packit 90a5c9
                cache->stale_handle = h;
Packit 90a5c9
Packit 90a5c9
                /* if no existing conditionals, use conditionals of our own */
Packit 90a5c9
                if (!mismatch) {
Packit 90a5c9
Packit 90a5c9
                    ap_log_rerror(
Packit 90a5c9
                            APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00695) "Cached response for %s isn't fresh. Adding "
Packit 90a5c9
                            "conditional request headers.", r->uri);
Packit 90a5c9
Packit 90a5c9
                    /* Remove existing conditionals that might conflict with ours */
Packit 90a5c9
                    apr_table_unset(r->headers_in, "If-Match");
Packit 90a5c9
                    apr_table_unset(r->headers_in, "If-Modified-Since");
Packit 90a5c9
                    apr_table_unset(r->headers_in, "If-None-Match");
Packit 90a5c9
                    apr_table_unset(r->headers_in, "If-Range");
Packit 90a5c9
                    apr_table_unset(r->headers_in, "If-Unmodified-Since");
Packit 90a5c9
Packit 90a5c9
                    etag = apr_table_get(h->resp_hdrs, "ETag");
Packit 90a5c9
                    lastmod = apr_table_get(h->resp_hdrs, "Last-Modified");
Packit 90a5c9
Packit 90a5c9
                    if (etag || lastmod) {
Packit 90a5c9
                        /* If we have a cached etag and/or Last-Modified add in
Packit 90a5c9
                         * our own conditionals.
Packit 90a5c9
                         */
Packit 90a5c9
Packit 90a5c9
                        if (etag) {
Packit 90a5c9
                            apr_table_set(r->headers_in, "If-None-Match", etag);
Packit 90a5c9
                        }
Packit 90a5c9
Packit 90a5c9
                        if (lastmod) {
Packit 90a5c9
                            apr_table_set(r->headers_in, "If-Modified-Since",
Packit 90a5c9
                                    lastmod);
Packit 90a5c9
                        }
Packit 90a5c9
Packit 90a5c9
                        /*
Packit 90a5c9
                         * Do not do Range requests with our own conditionals: If
Packit 90a5c9
                         * we get 304 the Range does not matter and otherwise the
Packit 90a5c9
                         * entity changed and we want to have the complete entity
Packit 90a5c9
                         */
Packit 90a5c9
                        apr_table_unset(r->headers_in, "Range");
Packit 90a5c9
Packit 90a5c9
                    }
Packit 90a5c9
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                /* ready to revalidate, pretend we were never here */
Packit 90a5c9
                return DECLINED;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Okay, this response looks okay.  Merge in our stuff and go. */
Packit 90a5c9
            cache_accept_headers(h, r, h->resp_hdrs, r->headers_out, 0);
Packit 90a5c9
Packit 90a5c9
            cache->handle = h;
Packit 90a5c9
            return OK;
Packit 90a5c9
        }
Packit 90a5c9
        case DECLINED: {
Packit 90a5c9
            /* try again with next cache type */
Packit 90a5c9
            list = list->next;
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
        default: {
Packit 90a5c9
            /* oo-er! an error */
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* if Cache-Control: only-if-cached, and not cached, return 504 */
Packit 90a5c9
    if (cache->control_in.only_if_cached) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00696)
Packit 90a5c9
                "cache: 'only-if-cached' requested and no cached entity, "
Packit 90a5c9
                "returning 504 Gateway Timeout for: %s", r->uri);
Packit 90a5c9
        return HTTP_GATEWAY_TIME_OUT;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t cache_canonicalise_key(request_rec *r, apr_pool_t* p,
Packit 90a5c9
                                           const char *path, const char *query,
Packit 90a5c9
                                           apr_uri_t *parsed_uri,
Packit 90a5c9
                                           const char **key)
Packit 90a5c9
{
Packit 90a5c9
    cache_server_conf *conf;
Packit 90a5c9
    char *port_str, *hn, *lcs;
Packit 90a5c9
    const char *hostname, *scheme;
Packit 90a5c9
    int i;
Packit 90a5c9
    const char *kpath;
Packit 90a5c9
    const char *kquery;
Packit 90a5c9
Packit 90a5c9
    if (*key) {
Packit 90a5c9
        /*
Packit 90a5c9
         * We have been here before during the processing of this request.
Packit 90a5c9
         */
Packit 90a5c9
        return APR_SUCCESS;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Get the module configuration. We need this for the CacheIgnoreQueryString
Packit 90a5c9
     * option below.
Packit 90a5c9
     */
Packit 90a5c9
    conf = (cache_server_conf *) ap_get_module_config(r->server->module_config,
Packit 90a5c9
            &cache_module);
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Use the canonical name to improve cache hit rate, but only if this is
Packit 90a5c9
     * not a proxy request or if this is a reverse proxy request.
Packit 90a5c9
     * We need to handle both cases in the same manner as for the reverse proxy
Packit 90a5c9
     * case we have the following situation:
Packit 90a5c9
     *
Packit 90a5c9
     * If a cached entry is looked up by mod_cache's quick handler r->proxyreq
Packit 90a5c9
     * is still unset in the reverse proxy case as it only gets set in the
Packit 90a5c9
     * translate name hook (either by ProxyPass or mod_rewrite) which is run
Packit 90a5c9
     * after the quick handler hook. This is different to the forward proxy
Packit 90a5c9
     * case where it gets set before the quick handler is run (in the
Packit 90a5c9
     * post_read_request hook).
Packit 90a5c9
     * If a cache entry is created by the CACHE_SAVE filter we always have
Packit 90a5c9
     * r->proxyreq set correctly.
Packit 90a5c9
     * So we must ensure that in the reverse proxy case we use the same code
Packit 90a5c9
     * path and using the canonical name seems to be the right thing to do
Packit 90a5c9
     * in the reverse proxy case.
Packit 90a5c9
     */
Packit 90a5c9
    if (!r->proxyreq || (r->proxyreq == PROXYREQ_REVERSE)) {
Packit 90a5c9
        if (conf->base_uri && conf->base_uri->hostname) {
Packit 90a5c9
            hostname = conf->base_uri->hostname;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            /* Use _default_ as the hostname if none present, as in mod_vhost */
Packit 90a5c9
            hostname = ap_get_server_name(r);
Packit 90a5c9
            if (!hostname) {
Packit 90a5c9
                hostname = "_default_";
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else if (parsed_uri->hostname) {
Packit 90a5c9
        /* Copy the parsed uri hostname */
Packit 90a5c9
        hn = apr_pstrdup(p, parsed_uri->hostname);
Packit 90a5c9
        ap_str_tolower(hn);
Packit 90a5c9
        /* const work-around */
Packit 90a5c9
        hostname = hn;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* We are a proxied request, with no hostname. Unlikely
Packit 90a5c9
         * to get very far - but just in case */
Packit 90a5c9
        hostname = "_default_";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Copy the scheme, ensuring that it is lower case. If the parsed uri
Packit 90a5c9
     * contains no string or if this is not a proxy request get the http
Packit 90a5c9
     * scheme for this request. As r->parsed_uri.scheme is not set if this
Packit 90a5c9
     * is a reverse proxy request, it is ensured that the cases
Packit 90a5c9
     * "no proxy request" and "reverse proxy request" are handled in the same
Packit 90a5c9
     * manner (see above why this is needed).
Packit 90a5c9
     */
Packit 90a5c9
    if (r->proxyreq && parsed_uri->scheme) {
Packit 90a5c9
        /* Copy the scheme and lower-case it */
Packit 90a5c9
        lcs = apr_pstrdup(p, parsed_uri->scheme);
Packit 90a5c9
        ap_str_tolower(lcs);
Packit 90a5c9
        /* const work-around */
Packit 90a5c9
        scheme = lcs;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        if (conf->base_uri && conf->base_uri->scheme) {
Packit 90a5c9
            scheme = conf->base_uri->scheme;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            scheme = ap_http_scheme(r);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * If this is a proxy request, but not a reverse proxy request (see comment
Packit 90a5c9
     * above why these cases must be handled in the same manner), copy the
Packit 90a5c9
     * URI's port-string (which may be a service name). If the URI contains
Packit 90a5c9
     * no port-string, use apr-util's notion of the default port for that
Packit 90a5c9
     * scheme - if available. Otherwise use the port-number of the current
Packit 90a5c9
     * server.
Packit 90a5c9
     */
Packit 90a5c9
    if (r->proxyreq && (r->proxyreq != PROXYREQ_REVERSE)) {
Packit 90a5c9
        if (parsed_uri->port_str) {
Packit 90a5c9
            port_str = apr_pcalloc(p, strlen(parsed_uri->port_str) + 2);
Packit 90a5c9
            port_str[0] = ':';
Packit 90a5c9
            for (i = 0; parsed_uri->port_str[i]; i++) {
Packit 90a5c9
                port_str[i + 1] = apr_tolower(parsed_uri->port_str[i]);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else if (apr_uri_port_of_scheme(scheme)) {
Packit 90a5c9
            port_str = apr_psprintf(p, ":%u", apr_uri_port_of_scheme(scheme));
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            /* No port string given in the AbsoluteUri, and we have no
Packit 90a5c9
             * idea what the default port for the scheme is. Leave it
Packit 90a5c9
             * blank and live with the inefficiency of some extra cached
Packit 90a5c9
             * entities.
Packit 90a5c9
             */
Packit 90a5c9
            port_str = "";
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        if (conf->base_uri && conf->base_uri->port_str) {
Packit 90a5c9
            port_str = conf->base_uri->port_str;
Packit 90a5c9
        }
Packit 90a5c9
        else if (conf->base_uri && conf->base_uri->hostname) {
Packit 90a5c9
            port_str = "";
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            /* Use the server port */
Packit 90a5c9
            port_str = apr_psprintf(p, ":%u", ap_get_server_port(r));
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Check if we need to ignore session identifiers in the URL and do so
Packit 90a5c9
     * if needed.
Packit 90a5c9
     */
Packit 90a5c9
    kpath = path;
Packit 90a5c9
    kquery = conf->ignorequerystring ? NULL : query;
Packit 90a5c9
    if (conf->ignore_session_id->nelts) {
Packit 90a5c9
        int i;
Packit 90a5c9
        char **identifier;
Packit 90a5c9
Packit 90a5c9
        identifier = (char **) conf->ignore_session_id->elts;
Packit 90a5c9
        for (i = 0; i < conf->ignore_session_id->nelts; i++, identifier++) {
Packit 90a5c9
            int len;
Packit 90a5c9
            const char *param;
Packit 90a5c9
Packit 90a5c9
            len = strlen(*identifier);
Packit 90a5c9
            /*
Packit 90a5c9
             * Check that we have a parameter separator in the last segment
Packit 90a5c9
             * of the path and that the parameter matches our identifier
Packit 90a5c9
             */
Packit 90a5c9
            if ((param = ap_strrchr_c(kpath, ';'))
Packit 90a5c9
                    && !strncmp(param + 1, *identifier, len)
Packit 90a5c9
                    && (*(param + len + 1) == '=')
Packit 90a5c9
                    && !ap_strchr_c(param + len + 2, '/')) {
Packit 90a5c9
                kpath = apr_pstrmemdup(p, kpath, param - kpath);
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
            /*
Packit 90a5c9
             * Check if the identifier is in the query string and cut it out.
Packit 90a5c9
             */
Packit 90a5c9
            if (kquery && *kquery) {
Packit 90a5c9
                /*
Packit 90a5c9
                 * First check if the identifier is at the beginning of the
Packit 90a5c9
                 * query string and followed by a '='
Packit 90a5c9
                 */
Packit 90a5c9
                if (!strncmp(kquery, *identifier, len) && kquery[len] == '=') {
Packit 90a5c9
                    param = kquery;
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    char *complete;
Packit 90a5c9
Packit 90a5c9
                    /*
Packit 90a5c9
                     * In order to avoid subkey matching (PR 48401) prepend
Packit 90a5c9
                     * identifier with a '&' and append a '='
Packit 90a5c9
                     */
Packit 90a5c9
                    complete = apr_pstrcat(p, "&", *identifier, "=", NULL);
Packit 90a5c9
                    param = ap_strstr_c(kquery, complete);
Packit 90a5c9
                    /* If we found something we are sitting on the '&' */
Packit 90a5c9
                    if (param) {
Packit 90a5c9
                        param++;
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
                if (param) {
Packit 90a5c9
                    const char *amp;
Packit 90a5c9
                    char *dup = NULL;
Packit 90a5c9
Packit 90a5c9
                    if (kquery != param) {
Packit 90a5c9
                        dup = apr_pstrmemdup(p, kquery, param - kquery);
Packit 90a5c9
                        kquery = dup;
Packit 90a5c9
                    }
Packit 90a5c9
                    else {
Packit 90a5c9
                        kquery = "";
Packit 90a5c9
                    }
Packit 90a5c9
Packit 90a5c9
                    if ((amp = ap_strchr_c(param + len + 1, '&'))) {
Packit 90a5c9
                        kquery = apr_pstrcat(p, kquery, amp + 1, NULL);
Packit 90a5c9
                    }
Packit 90a5c9
                    else {
Packit 90a5c9
                        /*
Packit 90a5c9
                         * If query string is not "", then we have the case
Packit 90a5c9
                         * that the identifier parameter we removed was the
Packit 90a5c9
                         * last one in the original query string. Hence we have
Packit 90a5c9
                         * a trailing '&' which needs to be removed.
Packit 90a5c9
                         */
Packit 90a5c9
                        if (dup) {
Packit 90a5c9
                            dup[strlen(dup) - 1] = '\0';
Packit 90a5c9
                        }
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Key format is a URI, optionally without the query-string (NULL
Packit 90a5c9
     * per above if conf->ignorequerystring)
Packit 90a5c9
     */
Packit 90a5c9
    *key = apr_pstrcat(p, scheme, "://", hostname, port_str,
Packit 90a5c9
                       kpath, "?", kquery, NULL);
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Store the key in the request_config for the cache as r->parsed_uri
Packit 90a5c9
     * might have changed in the time from our first visit here triggered by the
Packit 90a5c9
     * quick handler and our possible second visit triggered by the CACHE_SAVE
Packit 90a5c9
     * filter (e.g. r->parsed_uri got unescaped). In this case we would save the
Packit 90a5c9
     * resource in the cache under a key where it is never found by the quick
Packit 90a5c9
     * handler during following requests.
Packit 90a5c9
     */
Packit 90a5c9
    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(00698)
Packit 90a5c9
                  "cache: Key for entity %s?%s is %s", path, query, *key);
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
apr_status_t cache_generate_key_default(request_rec *r, apr_pool_t* p,
Packit 90a5c9
                                        const char **key)
Packit 90a5c9
{
Packit 90a5c9
    /* In early processing (quick-handler, forward proxy), we want the initial
Packit 90a5c9
     * query-string from r->parsed_uri, since any change before CACHE_SAVE
Packit 90a5c9
     * shouldn't modify the key. Otherwise we want the actual query-string.
Packit 90a5c9
     */
Packit 90a5c9
    const char *path = r->uri;
Packit 90a5c9
    const char *query = r->args;
Packit 90a5c9
    if (cache_use_early_url(r)) {
Packit 90a5c9
        path = r->parsed_uri.path;
Packit 90a5c9
        query = r->parsed_uri.query;
Packit 90a5c9
    }
Packit 90a5c9
    return cache_canonicalise_key(r, p, path, query, &r->parsed_uri, key);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Invalidate a specific URL entity in all caches
Packit 90a5c9
 *
Packit 90a5c9
 * All cached entities for this URL are removed, usually in
Packit 90a5c9
 * response to a POST/PUT or DELETE.
Packit 90a5c9
 *
Packit 90a5c9
 * This function returns OK if at least one entity was found and
Packit 90a5c9
 * removed, and DECLINED if no cached entities were removed.
Packit 90a5c9
 */
Packit 90a5c9
int cache_invalidate(cache_request_rec *cache, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    cache_provider_list *list;
Packit 90a5c9
    apr_status_t rv, status = DECLINED;
Packit 90a5c9
    cache_handle_t *h;
Packit 90a5c9
    apr_uri_t location_uri;
Packit 90a5c9
    apr_uri_t content_location_uri;
Packit 90a5c9
Packit 90a5c9
    const char *location, *location_key = NULL;
Packit 90a5c9
    const char *content_location, *content_location_key = NULL;
Packit 90a5c9
Packit 90a5c9
    if (!cache) {
Packit 90a5c9
        /* This should never happen */
Packit 90a5c9
        ap_log_rerror(
Packit 90a5c9
                APLOG_MARK, APLOG_ERR, APR_EGENERAL, r, APLOGNO(00697) "cache: No cache request information available for key"
Packit 90a5c9
                " generation");
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!cache->key) {
Packit 90a5c9
        rv = cache_generate_key(r, r->pool, &cache->key);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    location = apr_table_get(r->headers_out, "Location");
Packit 90a5c9
    if (location) {
Packit 90a5c9
        if (apr_uri_parse(r->pool, location, &location_uri)
Packit 90a5c9
                || cache_canonicalise_key(r, r->pool,
Packit 90a5c9
                                          location_uri.path,
Packit 90a5c9
                                          location_uri.query,
Packit 90a5c9
                                          &location_uri, &location_key)
Packit 90a5c9
                || !(r->parsed_uri.hostname
Packit 90a5c9
                     && location_uri.hostname
Packit 90a5c9
                     && !strcmp(r->parsed_uri.hostname,
Packit 90a5c9
                                location_uri.hostname))) {
Packit 90a5c9
            location_key = NULL;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    content_location = apr_table_get(r->headers_out, "Content-Location");
Packit 90a5c9
    if (content_location) {
Packit 90a5c9
        if (apr_uri_parse(r->pool, content_location,
Packit 90a5c9
                          &content_location_uri)
Packit 90a5c9
                || cache_canonicalise_key(r, r->pool,
Packit 90a5c9
                                          content_location_uri.path,
Packit 90a5c9
                                          content_location_uri.query,
Packit 90a5c9
                                          &content_location_uri,
Packit 90a5c9
                                          &content_location_key)
Packit 90a5c9
                || !(r->parsed_uri.hostname
Packit 90a5c9
                     && content_location_uri.hostname
Packit 90a5c9
                     && !strcmp(r->parsed_uri.hostname,
Packit 90a5c9
                                content_location_uri.hostname))) {
Packit 90a5c9
            content_location_key = NULL;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* go through the cache types */
Packit 90a5c9
    h = apr_palloc(r->pool, sizeof(cache_handle_t));
Packit 90a5c9
Packit 90a5c9
    list = cache->providers;
Packit 90a5c9
Packit 90a5c9
    while (list) {
Packit 90a5c9
Packit 90a5c9
        /* invalidate the request uri */
Packit 90a5c9
        rv = list->provider->open_entity(h, r, cache->key);
Packit 90a5c9
        if (OK == rv) {
Packit 90a5c9
            rv = list->provider->invalidate_entity(h, r);
Packit 90a5c9
            status = OK;
Packit 90a5c9
        }
Packit 90a5c9
        ap_log_rerror(
Packit 90a5c9
                APLOG_MARK, APLOG_DEBUG, rv, r, APLOGNO(02468) "cache: Attempted to invalidate cached entity with key: %s", cache->key);
Packit 90a5c9
Packit 90a5c9
        /* invalidate the Location */
Packit 90a5c9
        if (location_key) {
Packit 90a5c9
            rv = list->provider->open_entity(h, r, location_key);
Packit 90a5c9
            if (OK == rv) {
Packit 90a5c9
                rv = list->provider->invalidate_entity(h, r);
Packit 90a5c9
                status = OK;
Packit 90a5c9
            }
Packit 90a5c9
            ap_log_rerror(
Packit 90a5c9
                    APLOG_MARK, APLOG_DEBUG, rv, r, APLOGNO(02469) "cache: Attempted to invalidate cached entity with key: %s", location_key);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* invalidate the Content-Location */
Packit 90a5c9
        if (content_location_key) {
Packit 90a5c9
            rv = list->provider->open_entity(h, r, content_location_key);
Packit 90a5c9
            if (OK == rv) {
Packit 90a5c9
                rv = list->provider->invalidate_entity(h, r);
Packit 90a5c9
                status = OK;
Packit 90a5c9
            }
Packit 90a5c9
            ap_log_rerror(
Packit 90a5c9
                    APLOG_MARK, APLOG_DEBUG, rv, r, APLOGNO(02470) "cache: Attempted to invalidate cached entity with key: %s", content_location_key);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        list = list->next;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return status;
Packit 90a5c9
}