Blame modules/cache/cache_util.h

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
/**
Packit 90a5c9
 * @file cache_util.h
Packit 90a5c9
 * @brief Cache Storage Functions
Packit 90a5c9
 *
Packit 90a5c9
 * @defgroup Cache_util  Cache Utility Functions
Packit 90a5c9
 * @ingroup  MOD_CACHE
Packit 90a5c9
 * @{
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#ifndef CACHE_UTIL_H
Packit 90a5c9
#define CACHE_UTIL_H
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
extern "C" {
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#include "mod_cache.h"
Packit 90a5c9
Packit 90a5c9
#include "apr_hooks.h"
Packit 90a5c9
#include "apr.h"
Packit 90a5c9
#include "apr_lib.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_buckets.h"
Packit 90a5c9
#include "apr_md5.h"
Packit 90a5c9
#include "apr_pools.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_optional.h"
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_vhost.h"
Packit 90a5c9
#include "http_main.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_connection.h"
Packit 90a5c9
#include "util_filter.h"
Packit 90a5c9
#include "apr_uri.h"
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_NETDB_H
Packit 90a5c9
#include <netdb.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_SYS_SOCKET_H
Packit 90a5c9
#include <sys/socket.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_NETINET_IN_H
Packit 90a5c9
#include <netinet/in.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_ARPA_INET_H
Packit 90a5c9
#include <arpa/inet.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#include "apr_atomic.h"
Packit 90a5c9
Packit 90a5c9
#ifndef MAX
Packit 90a5c9
#define MAX(a,b)                ((a) > (b) ? (a) : (b))
Packit 90a5c9
#endif
Packit 90a5c9
#ifndef MIN
Packit 90a5c9
#define MIN(a,b)                ((a) < (b) ? (a) : (b))
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#define MSEC_ONE_DAY    ((apr_time_t)(86400*APR_USEC_PER_SEC)) /* one day, in microseconds */
Packit 90a5c9
#define MSEC_ONE_HR     ((apr_time_t)(3600*APR_USEC_PER_SEC))  /* one hour, in microseconds */
Packit 90a5c9
#define MSEC_ONE_MIN    ((apr_time_t)(60*APR_USEC_PER_SEC))    /* one minute, in microseconds */
Packit 90a5c9
#define MSEC_ONE_SEC    ((apr_time_t)(APR_USEC_PER_SEC))       /* one second, in microseconds */
Packit 90a5c9
Packit 90a5c9
#define DEFAULT_CACHE_MAXEXPIRE MSEC_ONE_DAY
Packit 90a5c9
#define DEFAULT_CACHE_MINEXPIRE 0
Packit 90a5c9
#define DEFAULT_CACHE_EXPIRE    MSEC_ONE_HR
Packit 90a5c9
#define DEFAULT_CACHE_LMFACTOR  (0.1)
Packit 90a5c9
#define DEFAULT_CACHE_MAXAGE    5
Packit 90a5c9
#define DEFAULT_X_CACHE         0
Packit 90a5c9
#define DEFAULT_X_CACHE_DETAIL  0
Packit 90a5c9
#define DEFAULT_CACHE_STALE_ON_ERROR 1
Packit 90a5c9
#define DEFAULT_CACHE_LOCKPATH "/mod_cache-lock"
Packit 90a5c9
#define CACHE_LOCKNAME_KEY "mod_cache-lockname"
Packit 90a5c9
#define CACHE_LOCKFILE_KEY "mod_cache-lockfile"
Packit 90a5c9
#define CACHE_CTX_KEY "mod_cache-ctx"
Packit 90a5c9
#define CACHE_SEPARATOR ", \t"
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * cache_util.c
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
struct cache_enable {
Packit 90a5c9
    apr_uri_t url;
Packit 90a5c9
    const char *type;
Packit 90a5c9
    apr_size_t pathlen;
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
struct cache_disable {
Packit 90a5c9
    apr_uri_t url;
Packit 90a5c9
    apr_size_t pathlen;
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
/* static information about the local cache */
Packit 90a5c9
typedef struct {
Packit 90a5c9
    apr_array_header_t *cacheenable;    /* URLs to cache */
Packit 90a5c9
    apr_array_header_t *cachedisable;   /* URLs not to cache */
Packit 90a5c9
    /** store the headers that should not be stored in the cache */
Packit 90a5c9
    apr_array_header_t *ignore_headers;
Packit 90a5c9
    /** store the identifiers that should not be used for key calculation */
Packit 90a5c9
    apr_array_header_t *ignore_session_id;
Packit 90a5c9
    const char *lockpath;
Packit 90a5c9
    apr_time_t lockmaxage;
Packit 90a5c9
    apr_uri_t *base_uri;
Packit 90a5c9
    /** ignore client's requests for uncached responses */
Packit 90a5c9
    unsigned int ignorecachecontrol:1;
Packit 90a5c9
    /** ignore query-string when caching */
Packit 90a5c9
    unsigned int ignorequerystring:1;
Packit 90a5c9
    /** run within the quick handler */
Packit 90a5c9
    unsigned int quick:1;
Packit 90a5c9
    /* thundering herd lock */
Packit 90a5c9
    unsigned int lock:1;
Packit 90a5c9
    unsigned int x_cache:1;
Packit 90a5c9
    unsigned int x_cache_detail:1;
Packit 90a5c9
    /* flag if CacheIgnoreHeader has been set */
Packit 90a5c9
    #define CACHE_IGNORE_HEADERS_SET   1
Packit 90a5c9
    #define CACHE_IGNORE_HEADERS_UNSET 0
Packit 90a5c9
    unsigned int ignore_headers_set:1;
Packit 90a5c9
    /* flag if CacheIgnoreURLSessionIdentifiers has been set */
Packit 90a5c9
    #define CACHE_IGNORE_SESSION_ID_SET   1
Packit 90a5c9
    #define CACHE_IGNORE_SESSION_ID_UNSET 0
Packit 90a5c9
    unsigned int ignore_session_id_set:1;
Packit 90a5c9
    unsigned int base_uri_set:1;
Packit 90a5c9
    unsigned int ignorecachecontrol_set:1;
Packit 90a5c9
    unsigned int ignorequerystring_set:1;
Packit 90a5c9
    unsigned int quick_set:1;
Packit 90a5c9
    unsigned int lock_set:1;
Packit 90a5c9
    unsigned int lockpath_set:1;
Packit 90a5c9
    unsigned int lockmaxage_set:1;
Packit 90a5c9
    unsigned int x_cache_set:1;
Packit 90a5c9
    unsigned int x_cache_detail_set:1;
Packit 90a5c9
} cache_server_conf;
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    /* Minimum time to keep cached files in msecs */
Packit 90a5c9
    apr_time_t minex;
Packit 90a5c9
    /* Maximum time to keep cached files in msecs */
Packit 90a5c9
    apr_time_t maxex;
Packit 90a5c9
    /* default time to keep cached file in msecs */
Packit 90a5c9
    apr_time_t defex;
Packit 90a5c9
    /* factor for estimating expires date */
Packit 90a5c9
    double factor;
Packit 90a5c9
    /* cache enabled for this location */
Packit 90a5c9
    apr_array_header_t *cacheenable;
Packit 90a5c9
    /* cache disabled for this location */
Packit 90a5c9
    unsigned int disable:1;
Packit 90a5c9
    /* set X-Cache headers */
Packit 90a5c9
    unsigned int x_cache:1;
Packit 90a5c9
    unsigned int x_cache_detail:1;
Packit 90a5c9
    /* serve stale on error */
Packit 90a5c9
    unsigned int stale_on_error:1;
Packit 90a5c9
    /** ignore the last-modified header when deciding to cache this request */
Packit 90a5c9
    unsigned int no_last_mod_ignore:1;
Packit 90a5c9
    /** ignore expiration date from server */
Packit 90a5c9
    unsigned int store_expired:1;
Packit 90a5c9
    /** ignore Cache-Control: private header from server */
Packit 90a5c9
    unsigned int store_private:1;
Packit 90a5c9
    /** ignore Cache-Control: no-store header from client or server */
Packit 90a5c9
    unsigned int store_nostore:1;
Packit 90a5c9
    unsigned int minex_set:1;
Packit 90a5c9
    unsigned int maxex_set:1;
Packit 90a5c9
    unsigned int defex_set:1;
Packit 90a5c9
    unsigned int factor_set:1;
Packit 90a5c9
    unsigned int x_cache_set:1;
Packit 90a5c9
    unsigned int x_cache_detail_set:1;
Packit 90a5c9
    unsigned int stale_on_error_set:1;
Packit 90a5c9
    unsigned int no_last_mod_ignore_set:1;
Packit 90a5c9
    unsigned int store_expired_set:1;
Packit 90a5c9
    unsigned int store_private_set:1;
Packit 90a5c9
    unsigned int store_nostore_set:1;
Packit 90a5c9
    unsigned int enable_set:1;
Packit 90a5c9
    unsigned int disable_set:1;
Packit 0ce3b3
    /* treat maxex as hard limit */
Packit 0ce3b3
    unsigned int hardmaxex:1;
Packit 0ce3b3
    unsigned int hardmaxex_set:1;
Packit 90a5c9
} cache_dir_conf;
Packit 90a5c9
Packit 90a5c9
/* A linked-list of authn providers. */
Packit 90a5c9
typedef struct cache_provider_list cache_provider_list;
Packit 90a5c9
Packit 90a5c9
struct cache_provider_list {
Packit 90a5c9
    const char *provider_name;
Packit 90a5c9
    const cache_provider *provider;
Packit 90a5c9
    cache_provider_list *next;
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
/* per request cache information */
Packit 90a5c9
typedef struct {
Packit 90a5c9
    cache_provider_list *providers;     /* possible cache providers */
Packit 90a5c9
    const cache_provider *provider;     /* current cache provider */
Packit 90a5c9
    const char *provider_name;          /* current cache provider name */
Packit 90a5c9
    int fresh;                          /* is the entity fresh? */
Packit 90a5c9
    cache_handle_t *handle;             /* current cache handle */
Packit 90a5c9
    cache_handle_t *stale_handle;       /* stale cache handle */
Packit 90a5c9
    apr_table_t *stale_headers;         /* original request headers. */
Packit 90a5c9
    int in_checked;                     /* CACHE_SAVE must cache the entity */
Packit 90a5c9
    int block_response;                 /* CACHE_SAVE must block response. */
Packit 90a5c9
    apr_bucket_brigade *saved_brigade;  /* copy of partial response */
Packit 90a5c9
    apr_off_t saved_size;               /* length of saved_brigade */
Packit 90a5c9
    apr_time_t exp;                     /* expiration */
Packit 90a5c9
    apr_time_t lastmod;                 /* last-modified time */
Packit 90a5c9
    cache_info *info;                   /* current cache info */
Packit 90a5c9
    ap_filter_t *save_filter;           /* Enable us to restore the filter on error */
Packit 90a5c9
    ap_filter_t *remove_url_filter;     /* Enable us to remove the filter */
Packit 90a5c9
    const char *key;                    /* The cache key created for this
Packit 90a5c9
                                         * request
Packit 90a5c9
                                         */
Packit 90a5c9
    apr_off_t size;                     /* the content length from the headers, or -1 */
Packit 90a5c9
    apr_bucket_brigade *out;            /* brigade to reuse for upstream responses */
Packit 90a5c9
    cache_control_t control_in;         /* cache control incoming */
Packit 90a5c9
} cache_request_rec;
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Check the whether the request allows a cached object to be served as per RFC2616
Packit 90a5c9
 * section 14.9.4 (Cache Revalidation and Reload Controls)
Packit 90a5c9
 * @param cache cache_request_rec
Packit 90a5c9
 * @param r request_rec
Packit 90a5c9
 * @return 0 ==> cache object may not be served, 1 ==> cache object may be served
Packit 90a5c9
 */
Packit 90a5c9
int ap_cache_check_no_cache(cache_request_rec *cache, request_rec *r);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Check the whether the request allows a cached object to be stored as per RFC2616
Packit 90a5c9
 * section 14.9.2 (What May be Stored by Caches)
Packit 90a5c9
 * @param cache cache_request_rec
Packit 90a5c9
 * @param r request_rec
Packit 90a5c9
 * @return 0 ==> cache object may not be served, 1 ==> cache object may be served
Packit 90a5c9
 */
Packit 90a5c9
int ap_cache_check_no_store(cache_request_rec *cache, request_rec *r);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Check the freshness of the cache object per RFC2616 section 13.2 (Expiration Model)
Packit 90a5c9
 * @param h cache_handle_t
Packit 90a5c9
 * @param cache cache_request_rec
Packit 90a5c9
 * @param r request_rec
Packit 90a5c9
 * @return 0 ==> cache object is stale, 1 ==> cache object is fresh
Packit 90a5c9
 */
Packit 90a5c9
int cache_check_freshness(cache_handle_t *h, cache_request_rec *cache,
Packit 90a5c9
        request_rec *r);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Try obtain a cache wide lock on the given cache key.
Packit 90a5c9
 *
Packit 90a5c9
 * If we return APR_SUCCESS, we obtained the lock, and we are clear to
Packit 90a5c9
 * proceed to the backend. If we return APR_EEXISTS, then the lock is
Packit 90a5c9
 * already locked, someone else has gone to refresh the backend data
Packit 90a5c9
 * already, so we must return stale data with a warning in the mean
Packit 90a5c9
 * time. If we return anything else, then something has gone pear
Packit 90a5c9
 * shaped, and we allow the request through to the backend regardless.
Packit 90a5c9
 *
Packit 90a5c9
 * This lock is created from the request pool, meaning that should
Packit 90a5c9
 * something go wrong and the lock isn't deleted on return of the
Packit 90a5c9
 * request headers from the backend for whatever reason, at worst the
Packit 90a5c9
 * lock will be cleaned up when the request is dies or finishes.
Packit 90a5c9
 *
Packit 90a5c9
 * If something goes truly bananas and the lock isn't deleted when the
Packit 90a5c9
 * request dies, the lock will be trashed when its max-age is reached,
Packit 90a5c9
 * or when a request arrives containing a Cache-Control: no-cache. At
Packit 90a5c9
 * no point is it possible for this lock to permanently deny access to
Packit 90a5c9
 * the backend.
Packit 90a5c9
 */
Packit 90a5c9
apr_status_t cache_try_lock(cache_server_conf *conf, cache_request_rec *cache,
Packit 90a5c9
        request_rec *r);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Remove the cache lock, if present.
Packit 90a5c9
 *
Packit 90a5c9
 * First, try to close the file handle, whose delete-on-close should
Packit 90a5c9
 * kill the file. Otherwise, just delete the file by name.
Packit 90a5c9
 *
Packit 90a5c9
 * If no lock name has yet been calculated, do the calculation of the
Packit 90a5c9
 * lock name first before trying to delete the file.
Packit 90a5c9
 *
Packit 90a5c9
 * If an optional bucket brigade is passed, the lock will only be
Packit 90a5c9
 * removed if the bucket brigade contains an EOS bucket.
Packit 90a5c9
 */
Packit 90a5c9
apr_status_t cache_remove_lock(cache_server_conf *conf,
Packit 90a5c9
        cache_request_rec *cache, request_rec *r, apr_bucket_brigade *bb);
Packit 90a5c9
Packit 90a5c9
cache_provider_list *cache_get_providers(request_rec *r,
Packit 90a5c9
                                         cache_server_conf *conf);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Get a value from a table, where the table may contain multiple
Packit 90a5c9
 * values for a given key.
Packit 90a5c9
 *
Packit 90a5c9
 * When the table contains a single value, that value is returned
Packit 90a5c9
 * unchanged.
Packit 90a5c9
 *
Packit 90a5c9
 * When the table contains two or more values for a key, all values
Packit 90a5c9
 * for the key are returned, separated by commas.
Packit 90a5c9
 */
Packit 90a5c9
const char *cache_table_getm(apr_pool_t *p, const apr_table_t *t,
Packit 90a5c9
        const char *key);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * String tokenizer that ignores separator characters within quoted strings
Packit 90a5c9
 * and escaped characters, as per RFC2616 section 2.2.
Packit 90a5c9
 */
Packit 90a5c9
char *cache_strqtok(char *str, const char *sep, char **last);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Merge err_headers_out into headers_out and add request's Content-Type and
Packit 90a5c9
 * Content-Encoding if available.
Packit 90a5c9
 */
Packit 90a5c9
apr_table_t *cache_merge_headers_out(request_rec *r);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Return whether to use request's path/query from early stage (r->parsed_uri)
Packit 90a5c9
 * or the current/rewritable ones (r->uri/r->args).
Packit 90a5c9
 */
Packit 90a5c9
int cache_use_early_url(request_rec *r);
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
}
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#endif /* !CACHE_UTIL_H */
Packit 90a5c9
/** @} */