1 /* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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 #include "mod_cache.h"
21 extern APR_OPTIONAL_FN_TYPE(ap_cache_generate_key) *cache_generate_key;
23 extern module AP_MODULE_DECLARE_DATA cache_module;
25 /* -------------------------------------------------------------- */
28 * delete all URL entities from the cache
31 int cache_remove_url(request_rec *r, char *url)
33 cache_provider_list *list;
36 cache_request_rec *cache = (cache_request_rec *)
37 ap_get_module_config(r->request_config, &cache_module);
39 rv = cache_generate_key(r,r->pool,&key);
40 if (rv != APR_SUCCESS) {
44 list = cache->providers;
46 /* for each specified cache type, delete the URL */
48 list->provider->remove_url(key);
56 * create a new URL entity in the cache
58 * It is possible to store more than once entity per URL. This
59 * function will always create a new entity, regardless of whether
60 * other entities already exist for the same URL.
62 * The size of the entity is provided so that a cache module can
63 * decide whether or not it wants to cache this particular entity.
64 * If the size is unknown, a size of -1 should be set.
66 int cache_create_entity(request_rec *r, char *url, apr_off_t size)
68 cache_provider_list *list;
69 cache_handle_t *h = apr_pcalloc(r->pool, sizeof(cache_handle_t));
72 cache_request_rec *cache = (cache_request_rec *)
73 ap_get_module_config(r->request_config, &cache_module);
75 rv = cache_generate_key(r, r->pool, &key);
76 if (rv != APR_SUCCESS) {
80 list = cache->providers;
81 /* for each specified cache type, delete the URL */
83 switch (rv = list->provider->create_entity(h, r, key, size)) {
86 cache->provider = list->provider;
87 cache->provider_name = list->provider_name;
102 static int set_cookie_doo_doo(void *v, const char *key, const char *val)
104 apr_table_addn(v, key, val);
108 CACHE_DECLARE(void) ap_cache_accept_headers(cache_handle_t *h, request_rec *r)
110 apr_table_t *cookie_table;
113 v = apr_table_get(h->resp_hdrs, "Content-Type");
115 ap_set_content_type(r, v);
116 apr_table_unset(h->resp_hdrs, "Content-Type");
119 /* If the cache gave us a Last-Modified header, we can't just
120 * pass it on blindly because of restrictions on future values.
122 v = apr_table_get(h->resp_hdrs, "Last-Modified");
124 ap_update_mtime(r, apr_date_parse_http(v));
125 ap_set_last_modified(r);
126 apr_table_unset(h->resp_hdrs, "Last-Modified");
129 /* The HTTP specification says that it is legal to merge duplicate
130 * headers into one. Some browsers that support Cookies don't like
131 * merged headers and prefer that each Set-Cookie header is sent
132 * separately. Lets humour those browsers by not merging.
133 * Oh what a pain it is.
135 cookie_table = apr_table_make(r->pool, 2);
136 apr_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out,
138 apr_table_do(set_cookie_doo_doo, cookie_table, h->resp_hdrs,
140 apr_table_unset(r->err_headers_out, "Set-Cookie");
141 apr_table_unset(h->resp_hdrs, "Set-Cookie");
143 apr_table_overlap(r->headers_out, h->resp_hdrs, APR_OVERLAP_TABLES_SET);
144 if (!apr_is_empty_table(cookie_table)) {
145 r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
151 * select a specific URL entity in the cache
153 * It is possible to store more than one entity per URL. Content
154 * negotiation is used to select an entity. Once an entity is
155 * selected, details of it are stored in the per request
156 * config to save time when serving the request later.
158 * This function returns OK if successful, DECLINED if no
159 * cached entity fits the bill.
161 int cache_select_url(request_rec *r, char *url)
163 cache_provider_list *list;
167 cache_request_rec *cache = (cache_request_rec *)
168 ap_get_module_config(r->request_config, &cache_module);
170 rv = cache_generate_key(r, r->pool, &key);
171 if (rv != APR_SUCCESS) {
174 /* go through the cache types till we get a match */
175 h = apr_palloc(r->pool, sizeof(cache_handle_t));
177 list = cache->providers;
180 switch ((rv = list->provider->open_entity(h, r, key))) {
183 const char *varyhdr = NULL;
186 if (list->provider->recall_headers(h, r) != APR_SUCCESS) {
187 /* TODO: Handle this error */
192 * Check Content-Negotiation - Vary
194 * At this point we need to make sure that the object we found in
195 * the cache is the same object that would be delivered to the
196 * client, when the effects of content negotiation are taken into
199 * In plain english, we want to make sure that a language-negotiated
200 * document in one language is not given to a client asking for a
201 * language negotiated document in a different language by mistake.
203 * This code makes the assumption that the storage manager will
204 * cache the req_hdrs if the response contains a Vary
207 * RFC2616 13.6 and 14.44 describe the Vary mechanism.
209 vary = apr_pstrdup(r->pool, apr_table_get(h->resp_hdrs, "Vary"));
210 while (vary && *vary) {
214 /* isolate header name */
215 while (*vary && !apr_isspace(*vary) && (*vary != ','))
217 while (*vary && (apr_isspace(*vary) || (*vary == ','))) {
223 * is this header in the request and the header in the cached
224 * request identical? If not, we give up and do a straight get
226 h1 = apr_table_get(r->headers_in, name);
227 h2 = apr_table_get(h->req_hdrs, name);
229 /* both headers NULL, so a match - do nothing */
231 else if (h1 && h2 && !strcmp(h1, h2)) {
232 /* both headers exist and are equal - do nothing */
235 /* headers do not match, so Vary failed */
236 ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
238 "cache_select_url(): Vary header mismatch.");
243 cache->provider = list->provider;
244 cache->provider_name = list->provider_name;
246 /* Is our cached response fresh enough? */
247 fresh = ap_cache_check_freshness(h, r);
249 const char *etag, *lastmod;
251 /* Make response into a conditional */
253 /* FIXME: What if the request is already conditional? */
254 etag = apr_table_get(h->resp_hdrs, "ETag");
255 lastmod = apr_table_get(h->resp_hdrs, "Last-Modified");
257 if (etag || lastmod) {
258 /* If we have a cached etag and/or Last-Modified */
260 cache->stale_headers = apr_table_copy(r->pool,
263 apr_table_set(r->headers_in, "If-None-Match", etag);
267 apr_table_set(r->headers_in, "If-Modified-Since",
270 cache->stale_handle = h;
276 /* Okay, this response looks okay. Merge in our stuff and go. */
277 ap_cache_accept_headers(h, r);
283 /* try again with next cache type */
288 /* oo-er! an error */
296 apr_status_t cache_generate_key_default(request_rec *r, apr_pool_t* p,
300 *key = apr_pstrcat(p, r->hostname, r->uri, "?", r->args, NULL);
303 *key = apr_pstrcat(p, r->uri, "?", r->args, NULL);