1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
19
* @brief Main include file for the Apache Transparent Cache
21
* @defgroup MOD_CACHE mod_cache
22
* @ingroup APACHE_MODS
31
#include "apr_hooks.h"
34
#include "apr_strings.h"
35
#include "apr_buckets.h"
37
#include "apr_pools.h"
38
#include "apr_strings.h"
39
#include "apr_optional.h"
40
#define APR_WANT_STRFUNC
44
#include "http_config.h"
45
#include "ap_config.h"
46
#include "http_core.h"
47
#include "http_protocol.h"
48
#include "http_request.h"
49
#include "http_vhost.h"
50
#include "http_main.h"
52
#include "http_connection.h"
53
#include "util_filter.h"
61
#ifdef HAVE_SYS_SOCKET_H
62
#include <sys/socket.h>
65
#ifdef HAVE_NETINET_IN_H
66
#include <netinet/in.h>
69
#ifdef HAVE_ARPA_INET_H
70
#include <arpa/inet.h>
73
#include "apr_atomic.h"
76
#define MAX(a,b) ((a) > (b) ? (a) : (b))
79
#define MIN(a,b) ((a) < (b) ? (a) : (b))
82
#define MSEC_ONE_DAY ((apr_time_t)(86400*APR_USEC_PER_SEC)) /* one day, in microseconds */
83
#define MSEC_ONE_HR ((apr_time_t)(3600*APR_USEC_PER_SEC)) /* one hour, in microseconds */
84
#define MSEC_ONE_MIN ((apr_time_t)(60*APR_USEC_PER_SEC)) /* one minute, in microseconds */
85
#define MSEC_ONE_SEC ((apr_time_t)(APR_USEC_PER_SEC)) /* one second, in microseconds */
86
#define DEFAULT_CACHE_MAXEXPIRE MSEC_ONE_DAY
87
#define DEFAULT_CACHE_EXPIRE MSEC_ONE_HR
88
#define DEFAULT_CACHE_LMFACTOR (0.1)
90
/* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and
91
* PROXY_DECLARE_DATA with appropriate export and import tags for the platform
94
#define CACHE_DECLARE(type) type
95
#define CACHE_DECLARE_NONSTD(type) type
96
#define CACHE_DECLARE_DATA
97
#elif defined(CACHE_DECLARE_STATIC)
98
#define CACHE_DECLARE(type) type __stdcall
99
#define CACHE_DECLARE_NONSTD(type) type
100
#define CACHE_DECLARE_DATA
101
#elif defined(CACHE_DECLARE_EXPORT)
102
#define CACHE_DECLARE(type) __declspec(dllexport) type __stdcall
103
#define CACHE_DECLARE_NONSTD(type) __declspec(dllexport) type
104
#define CACHE_DECLARE_DATA __declspec(dllexport)
106
#define CACHE_DECLARE(type) __declspec(dllimport) type __stdcall
107
#define CACHE_DECLARE_NONSTD(type) __declspec(dllimport) type
108
#define CACHE_DECLARE_DATA __declspec(dllimport)
111
struct cache_enable {
117
struct cache_disable {
122
/* static information about the local cache */
124
apr_array_header_t *cacheenable; /* URLs to cache */
125
apr_array_header_t *cachedisable; /* URLs not to cache */
126
/* Maximum time to keep cached files in msecs */
129
/* default time to keep cached file in msecs */
132
/* factor for estimating expires date */
135
/** ignore the last-modified header when deciding to cache this request */
136
int no_last_mod_ignore_set;
137
int no_last_mod_ignore;
138
/** ignore client's requests for uncached responses */
139
int ignorecachecontrol;
140
int ignorecachecontrol_set;
141
/** ignore Cache-Control: private header from server */
143
int store_private_set;
144
/** ignore Cache-Control: no-store header from client or server */
146
int store_nostore_set;
147
/** store the headers that should not be stored in the cache */
148
apr_array_header_t *ignore_headers;
149
/* flag if CacheIgnoreHeader has been set */
150
#define CACHE_IGNORE_HEADERS_SET 1
151
#define CACHE_IGNORE_HEADERS_UNSET 0
152
int ignore_headers_set;
155
/* cache info information */
156
typedef struct cache_info cache_info;
161
apr_time_t request_time;
162
apr_time_t response_time;
165
/* cache handle information */
167
/* XXX TODO On the next structure change/MMN bump,
168
* count must become an apr_off_t, representing
169
* the potential size of disk cached objects.
171
* "XXX Bad Temporary Cast - see cache_object_t notes"
173
typedef struct cache_object cache_object_t;
174
struct cache_object {
176
cache_object_t *next;
178
/* Opaque portion (specific to the implementation) of the cache object */
180
/* FIXME: These are only required for mod_mem_cache. */
181
apr_size_t count; /* Number of body bytes written to the cache so far */
183
apr_uint32_t refcount; /* refcount and bit flag to cleanup object */
186
typedef struct cache_handle cache_handle_t;
187
struct cache_handle {
188
cache_object_t *cache_obj;
189
apr_table_t *req_hdrs; /* cached request headers */
190
apr_table_t *resp_hdrs; /* cached response headers */
193
#define CACHE_PROVIDER_GROUP "cache"
196
int (*remove_entity) (cache_handle_t *h);
197
apr_status_t (*store_headers)(cache_handle_t *h, request_rec *r, cache_info *i);
198
apr_status_t (*store_body)(cache_handle_t *h, request_rec *r, apr_bucket_brigade *b);
199
apr_status_t (*recall_headers) (cache_handle_t *h, request_rec *r);
200
apr_status_t (*recall_body) (cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
201
int (*create_entity) (cache_handle_t *h, request_rec *r,
202
const char *urlkey, apr_off_t len);
203
int (*open_entity) (cache_handle_t *h, request_rec *r,
205
int (*remove_url) (cache_handle_t *h, apr_pool_t *p);
208
/* A linked-list of authn providers. */
209
typedef struct cache_provider_list cache_provider_list;
211
struct cache_provider_list {
212
const char *provider_name;
213
const cache_provider *provider;
214
cache_provider_list *next;
217
/* per request cache information */
219
cache_provider_list *providers; /* possible cache providers */
220
const cache_provider *provider; /* current cache provider */
221
const char *provider_name; /* current cache provider name */
222
int fresh; /* is the entitey fresh? */
223
cache_handle_t *handle; /* current cache handle */
224
cache_handle_t *stale_handle; /* stale cache handle */
225
apr_table_t *stale_headers; /* original request headers. */
226
int in_checked; /* CACHE_SAVE must cache the entity */
227
int block_response; /* CACHE_SAVE must block response. */
228
apr_bucket_brigade *saved_brigade; /* copy of partial response */
229
apr_off_t saved_size; /* length of saved_brigade */
230
apr_time_t exp; /* expiration */
231
apr_time_t lastmod; /* last-modified time */
232
cache_info *info; /* current cache info */
233
ap_filter_t *remove_url_filter; /* Enable us to remove the filter */
238
/* do a HTTP/1.1 age calculation */
239
CACHE_DECLARE(apr_time_t) ap_cache_current_age(cache_info *info, const apr_time_t age_value,
243
* Check the freshness of the cache object per RFC2616 section 13.2 (Expiration Model)
244
* @param h cache_handle_t
245
* @param r request_rec
246
* @return 0 ==> cache object is stale, 1 ==> cache object is fresh
248
CACHE_DECLARE(int) ap_cache_check_freshness(cache_handle_t *h, request_rec *r);
251
* Merge in cached headers into the response
252
* @param h cache_handle_t
253
* @param r request_rec
254
* @param preserve_orig If 1, the values in r->headers_out are preserved.
255
* Otherwise, they are overwritten by the cached value.
257
CACHE_DECLARE(void) ap_cache_accept_headers(cache_handle_t *h, request_rec *r,
260
CACHE_DECLARE(apr_time_t) ap_cache_hex2usec(const char *x);
261
CACHE_DECLARE(void) ap_cache_usec2hex(apr_time_t j, char *y);
262
CACHE_DECLARE(char *) ap_cache_generate_name(apr_pool_t *p, int dirlevels,
265
CACHE_DECLARE(cache_provider_list *)ap_cache_get_providers(request_rec *r, cache_server_conf *conf, apr_uri_t uri);
266
CACHE_DECLARE(int) ap_cache_liststr(apr_pool_t *p, const char *list,
267
const char *key, char **val);
268
CACHE_DECLARE(const char *)ap_cache_tokstr(apr_pool_t *p, const char *list, const char **str);
270
/* Create a new table consisting of those elements from a request_rec's
271
* headers_out that are allowed to be stored in a cache
273
CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_hdrs_out(apr_pool_t *pool,
280
int cache_remove_url(cache_request_rec *cache, apr_pool_t *p);
281
int cache_create_entity(request_rec *r, apr_off_t size);
282
int cache_select(request_rec *r);
283
apr_status_t cache_generate_key_default( request_rec *r, apr_pool_t*p, char**key );
285
* create a key for the cache based on the request record
286
* this is the 'default' version, which can be overridden by a default function
288
const char* cache_create_key( request_rec*r );
291
apr_status_t cache_store_entity_headers(cache_handle_t *h, request_rec *r, cache_info *info);
292
apr_status_t cache_store_entity_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *bb);
294
apr_status_t cache_recall_entity_headers(cache_handle_t *h, request_rec *r);
295
apr_status_t cache_recall_entity_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
300
/* Create a set of CACHE_DECLARE(type), CACHE_DECLARE_NONSTD(type) and
301
* CACHE_DECLARE_DATA with appropriate export and import tags for the platform
304
#define CACHE_DECLARE(type) type
305
#define CACHE_DECLARE_NONSTD(type) type
306
#define CACHE_DECLARE_DATA
307
#elif defined(CACHE_DECLARE_STATIC)
308
#define CACHE_DECLARE(type) type __stdcall
309
#define CACHE_DECLARE_NONSTD(type) type
310
#define CACHE_DECLARE_DATA
311
#elif defined(CACHE_DECLARE_EXPORT)
312
#define CACHE_DECLARE(type) __declspec(dllexport) type __stdcall
313
#define CACHE_DECLARE_NONSTD(type) __declspec(dllexport) type
314
#define CACHE_DECLARE_DATA __declspec(dllexport)
316
#define CACHE_DECLARE(type) __declspec(dllimport) type __stdcall
317
#define CACHE_DECLARE_NONSTD(type) __declspec(dllimport) type
318
#define CACHE_DECLARE_DATA __declspec(dllimport)
321
APR_DECLARE_OPTIONAL_FN(apr_status_t,
322
ap_cache_generate_key,
323
(request_rec *r, apr_pool_t*p, char**key ));
326
#endif /*MOD_CACHE_H*/