bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / experimental / mod_cache.h
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef MOD_CACHE_H
18 #define MOD_CACHE_H 
19
20 /*
21  * Main include file for the Apache Transparent Cache
22  */
23
24 #define CORE_PRIVATE
25
26 #include "apr_hooks.h"
27 #include "apr.h"
28 #include "apr_lib.h"
29 #include "apr_strings.h"
30 #include "apr_buckets.h"
31 #include "apr_md5.h"
32 #include "apr_pools.h"
33 #include "apr_strings.h"
34 #include "apr_optional.h"
35 #define APR_WANT_STRFUNC
36 #include "apr_want.h"
37
38 #include "httpd.h"
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"
46 #include "http_log.h"
47 #include "http_connection.h"
48 #include "util_filter.h"
49 #include "apr_date.h"
50 #include "apr_uri.h"
51
52 #ifdef HAVE_NETDB_H
53 #include <netdb.h>
54 #endif
55
56 #ifdef HAVE_SYS_SOCKET_H
57 #include <sys/socket.h>
58 #endif
59
60 #ifdef HAVE_NETINET_IN_H
61 #include <netinet/in.h>
62 #endif
63
64 #ifdef HAVE_ARPA_INET_H
65 #include <arpa/inet.h>
66 #endif
67
68 #include "apr_atomic.h"
69
70 #ifndef MAX
71 #define MAX(a,b)                ((a) > (b) ? (a) : (b))
72 #endif
73 #ifndef MIN
74 #define MIN(a,b)                ((a) < (b) ? (a) : (b))
75 #endif
76
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)
86
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
89  */
90 #if !defined(WIN32)
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)
102 #else
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)
106 #endif
107
108 struct cache_enable {
109     const char *url;
110     const char *type;
111     apr_size_t urllen;
112 };
113
114 struct cache_disable {
115     const char *url;
116     apr_size_t urllen;
117 };
118
119 /* static information about the local cache */
120 typedef struct {
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 */
124     int maxex_set;
125     apr_time_t defex;           /* default time to keep cached file in msecs */
126     int defex_set;
127     double factor;              /* factor for estimating expires date */
128     int factor_set;
129     int complete;               /* Force cache completion after this point */
130     int complete_set;
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;
143 } cache_server_conf;
144
145 /* cache info information */
146 typedef struct cache_info cache_info;
147 struct cache_info {
148     int status;
149     char *content_type;
150     char *etag;
151     char *lastmods;         /* last modified of cache entity */
152     char *filename;   
153     apr_time_t date;
154     apr_time_t lastmod;
155     char lastmod_str[APR_RFC822_DATE_LEN];
156     apr_time_t expire;
157     apr_time_t request_time;
158     apr_time_t response_time;
159     apr_size_t len;
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 */
164 };
165
166 /* cache handle information */
167
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.
171  * Then dig for
172  * "XXX Bad Temporary Cast - see cache_object_t notes" 
173  */
174 typedef struct cache_object cache_object_t;
175 struct cache_object {
176     char *key;
177     cache_object_t *next;
178     cache_info info;
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 */
181     int complete;
182     apr_atomic_t refcount;
183     apr_size_t cleanup;
184 };
185
186 typedef struct cache_handle cache_handle_t;
187
188 #define CACHE_PROVIDER_GROUP "cache"
189
190 typedef struct {
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,
199                            const char *urlkey);
200     int (*remove_url) (const char *urlkey);
201 } cache_provider;
202
203 /* A linked-list of authn providers. */
204 typedef struct cache_provider_list cache_provider_list;
205
206 struct cache_provider_list {
207     const char *provider_name;
208     const cache_provider *provider;
209     cache_provider_list *next;
210 };
211
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 */
219 };
220
221 /* per request cache information */
222 typedef struct {
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 */
237 } cache_request_rec;
238
239
240 /* cache_util.c */
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,
243                                                apr_time_t now);
244
245 /**
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
250  */
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, 
255                                     int dirlength, 
256                                     const char *name);
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);
262
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
265  */
266 CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_hdrs_out(apr_pool_t *pool,
267                                                         apr_table_t *t,
268                                                         server_rec *s);
269
270 /**
271  * cache_storage.c
272  */
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 );
277 /**
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
280  */
281 const char* cache_create_key( request_rec*r );
282
283 /*
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);
286
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);
289 */
290
291 /* hooks */
292
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
295  */
296 #if !defined(WIN32)
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)
308 #else
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)
312 #endif
313
314 APR_DECLARE_OPTIONAL_FN(apr_status_t, 
315                         ap_cache_generate_key, 
316                         (request_rec *r, apr_pool_t*p, char**key ));
317
318
319 #endif /*MOD_CACHE_H*/