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.
21 * Main include file for the Apache Transparent Cache
26 #include "apr_hooks.h"
29 #include "apr_strings.h"
30 #include "apr_buckets.h"
32 #include "apr_pools.h"
33 #include "apr_strings.h"
34 #include "apr_optional.h"
35 #define APR_WANT_STRFUNC
39 #include "http_config.h"
40 #include "ap_config.h"
41 #include "http_core.h"
42 #include "http_protocol.h"
43 #include "http_request.h"
44 #include "http_vhost.h"
45 #include "http_main.h"
47 #include "http_connection.h"
48 #include "util_filter.h"
56 #ifdef HAVE_SYS_SOCKET_H
57 #include <sys/socket.h>
60 #ifdef HAVE_NETINET_IN_H
61 #include <netinet/in.h>
64 #ifdef HAVE_ARPA_INET_H
65 #include <arpa/inet.h>
68 #include "apr_atomic.h"
71 #define MAX(a,b) ((a) > (b) ? (a) : (b))
74 #define MIN(a,b) ((a) < (b) ? (a) : (b))
77 /* default completion is 60% */
78 #define DEFAULT_CACHE_COMPLETION (60)
79 #define MSEC_ONE_DAY ((apr_time_t)(86400*APR_USEC_PER_SEC)) /* one day, in microseconds */
80 #define MSEC_ONE_HR ((apr_time_t)(3600*APR_USEC_PER_SEC)) /* one hour, in microseconds */
81 #define MSEC_ONE_MIN ((apr_time_t)(60*APR_USEC_PER_SEC)) /* one minute, in microseconds */
82 #define MSEC_ONE_SEC ((apr_time_t)(APR_USEC_PER_SEC)) /* one second, in microseconds */
83 #define DEFAULT_CACHE_MAXEXPIRE MSEC_ONE_DAY
84 #define DEFAULT_CACHE_EXPIRE MSEC_ONE_HR
85 #define DEFAULT_CACHE_LMFACTOR (0.1)
87 /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and
88 * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
91 #define CACHE_DECLARE(type) type
92 #define CACHE_DECLARE_NONSTD(type) type
93 #define CACHE_DECLARE_DATA
94 #elif defined(CACHE_DECLARE_STATIC)
95 #define CACHE_DECLARE(type) type __stdcall
96 #define CACHE_DECLARE_NONSTD(type) type
97 #define CACHE_DECLARE_DATA
98 #elif defined(CACHE_DECLARE_EXPORT)
99 #define CACHE_DECLARE(type) __declspec(dllexport) type __stdcall
100 #define CACHE_DECLARE_NONSTD(type) __declspec(dllexport) type
101 #define CACHE_DECLARE_DATA __declspec(dllexport)
103 #define CACHE_DECLARE(type) __declspec(dllimport) type __stdcall
104 #define CACHE_DECLARE_NONSTD(type) __declspec(dllimport) type
105 #define CACHE_DECLARE_DATA __declspec(dllimport)
108 struct cache_enable {
114 struct cache_disable {
119 /* static information about the local cache */
121 apr_array_header_t *cacheenable; /* URLs to cache */
122 apr_array_header_t *cachedisable; /* URLs not to cache */
123 apr_time_t maxex; /* Maximum time to keep cached files in msecs */
125 apr_time_t defex; /* default time to keep cached file in msecs */
127 double factor; /* factor for estimating expires date */
129 int complete; /* Force cache completion after this point */
131 /** ignore the last-modified header when deciding to cache this request */
132 int no_last_mod_ignore_set;
133 int no_last_mod_ignore;
134 /** ignore client's requests for uncached responses */
135 int ignorecachecontrol;
136 int ignorecachecontrol_set;
137 /** store the headers that should not be stored in the cache */
138 apr_array_header_t *ignore_headers;
139 /* flag if CacheIgnoreHeader has been set */
140 #define CACHE_IGNORE_HEADERS_SET 1
141 #define CACHE_IGNORE_HEADERS_UNSET 0
142 int ignore_headers_set;
145 /* cache info information */
146 typedef struct cache_info cache_info;
151 char *lastmods; /* last modified of cache entity */
155 char lastmod_str[APR_RFC822_DATE_LEN];
157 apr_time_t request_time;
158 apr_time_t response_time;
160 apr_time_t ims; /* If-Modified_Since header value */
161 apr_time_t ius; /* If-UnModified_Since header value */
162 const char *im; /* If-Match header value */
163 const char *inm; /* If-None-Match header value */
166 /* cache handle information */
168 /* XXX TODO On the next structure change/MMN bump,
169 * count must become an apr_off_t, representing
170 * the potential size of disk cached objects.
172 * "XXX Bad Temporary Cast - see cache_object_t notes"
174 typedef struct cache_object cache_object_t;
175 struct cache_object {
177 cache_object_t *next;
179 void *vobj; /* Opaque portion (specific to the cache implementation) of the cache object */
180 apr_size_t count; /* Number of body bytes written to the cache so far */
182 apr_atomic_t refcount;
186 typedef struct cache_handle cache_handle_t;
188 #define CACHE_PROVIDER_GROUP "cache"
191 int (*remove_entity) (cache_handle_t *h);
192 apr_status_t (*store_headers)(cache_handle_t *h, request_rec *r, cache_info *i);
193 apr_status_t (*store_body)(cache_handle_t *h, request_rec *r, apr_bucket_brigade *b);
194 apr_status_t (*recall_headers) (cache_handle_t *h, request_rec *r);
195 apr_status_t (*recall_body) (cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
196 int (*create_entity) (cache_handle_t *h, request_rec *r,
197 const char *urlkey, apr_off_t len);
198 int (*open_entity) (cache_handle_t *h, request_rec *r,
200 int (*remove_url) (const char *urlkey);
203 /* A linked-list of authn providers. */
204 typedef struct cache_provider_list cache_provider_list;
206 struct cache_provider_list {
207 const char *provider_name;
208 const cache_provider *provider;
209 cache_provider_list *next;
212 struct cache_handle {
213 cache_object_t *cache_obj;
214 apr_table_t *req_hdrs; /* cached request headers */
215 apr_table_t *resp_hdrs; /* cached response headers */
216 apr_table_t *resp_err_hdrs; /* cached response err headers */
217 const char *content_type; /* cached content type */
218 int status; /* cached status */
221 /* per request cache information */
223 cache_provider_list *providers; /* possible cache providers */
224 const cache_provider *provider; /* current cache provider */
225 const char *provider_name; /* current cache provider name */
226 int fresh; /* is the entitey fresh? */
227 cache_handle_t *handle; /* current cache handle */
228 cache_handle_t *stale_handle; /* stale cache handle */
229 apr_table_t *stale_headers; /* original request headers. */
230 int in_checked; /* CACHE_SAVE must cache the entity */
231 int block_response; /* CACHE_SAVE must block response. */
232 apr_bucket_brigade *saved_brigade; /* copy of partial response */
233 apr_off_t saved_size; /* length of saved_brigade */
234 apr_time_t exp; /* expiration */
235 apr_time_t lastmod; /* last-modified time */
236 cache_info *info; /* current cache info */
241 /* do a HTTP/1.1 age calculation */
242 CACHE_DECLARE(apr_time_t) ap_cache_current_age(cache_info *info, const apr_time_t age_value,
246 * Check the freshness of the cache object per RFC2616 section 13.2 (Expiration Model)
247 * @param h cache_handle_t
248 * @param r request_rec
249 * @return 0 ==> cache object is stale, 1 ==> cache object is fresh
251 CACHE_DECLARE(int) ap_cache_check_freshness(cache_handle_t *h, request_rec *r);
252 CACHE_DECLARE(apr_time_t) ap_cache_hex2usec(const char *x);
253 CACHE_DECLARE(void) ap_cache_usec2hex(apr_time_t j, char *y);
254 CACHE_DECLARE(char *) generate_name(apr_pool_t *p, int dirlevels,
257 CACHE_DECLARE(int) ap_cache_request_is_conditional(apr_table_t *table);
258 CACHE_DECLARE(cache_provider_list *)ap_cache_get_providers(request_rec *r, cache_server_conf *conf, const char *url);
259 CACHE_DECLARE(int) ap_cache_liststr(apr_pool_t *p, const char *list,
260 const char *key, char **val);
261 CACHE_DECLARE(const char *)ap_cache_tokstr(apr_pool_t *p, const char *list, const char **str);
263 /* Create a new table consisting of those elements from a request_rec's
264 * headers_out that are allowed to be stored in a cache
266 CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_hdrs_out(apr_pool_t *pool,
273 int cache_remove_url(request_rec *r, char *url);
274 int cache_create_entity(request_rec *r, char *url, apr_off_t size);
275 int cache_select_url(request_rec *r, char *url);
276 apr_status_t cache_generate_key_default( request_rec *r, apr_pool_t*p, char**key );
278 * create a key for the cache based on the request record
279 * this is the 'default' version, which can be overridden by a default function
281 const char* cache_create_key( request_rec*r );
284 apr_status_t cache_store_entity_headers(cache_handle_t *h, request_rec *r, cache_info *info);
285 apr_status_t cache_store_entity_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *bb);
287 apr_status_t cache_recall_entity_headers(cache_handle_t *h, request_rec *r);
288 apr_status_t cache_recall_entity_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
293 /* Create a set of CACHE_DECLARE(type), CACHE_DECLARE_NONSTD(type) and
294 * CACHE_DECLARE_DATA with appropriate export and import tags for the platform
297 #define CACHE_DECLARE(type) type
298 #define CACHE_DECLARE_NONSTD(type) type
299 #define CACHE_DECLARE_DATA
300 #elif defined(CACHE_DECLARE_STATIC)
301 #define CACHE_DECLARE(type) type __stdcall
302 #define CACHE_DECLARE_NONSTD(type) type
303 #define CACHE_DECLARE_DATA
304 #elif defined(CACHE_DECLARE_EXPORT)
305 #define CACHE_DECLARE(type) __declspec(dllexport) type __stdcall
306 #define CACHE_DECLARE_NONSTD(type) __declspec(dllexport) type
307 #define CACHE_DECLARE_DATA __declspec(dllexport)
309 #define CACHE_DECLARE(type) __declspec(dllimport) type __stdcall
310 #define CACHE_DECLARE_NONSTD(type) __declspec(dllimport) type
311 #define CACHE_DECLARE_DATA __declspec(dllimport)
314 APR_DECLARE_OPTIONAL_FN(apr_status_t,
315 ap_cache_generate_key,
316 (request_rec *r, apr_pool_t*p, char**key ));
319 #endif /*MOD_CACHE_H*/