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 #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 static void 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,
144 APR_OVERLAP_TABLES_SET);
145 apr_table_overlap(r->err_headers_out, h->resp_err_hdrs,
146 APR_OVERLAP_TABLES_SET);
147 if (!apr_is_empty_table(cookie_table)) {
148 r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
154 * select a specific URL entity in the cache
156 * It is possible to store more than one entity per URL. Content
157 * negotiation is used to select an entity. Once an entity is
158 * selected, details of it are stored in the per request
159 * config to save time when serving the request later.
161 * This function returns OK if successful, DECLINED if no
162 * cached entity fits the bill.
164 int cache_select_url(request_rec *r, char *url)
166 cache_provider_list *list;
170 cache_request_rec *cache = (cache_request_rec *)
171 ap_get_module_config(r->request_config, &cache_module);
173 rv = cache_generate_key(r, r->pool, &key);
174 if (rv != APR_SUCCESS) {
177 /* go through the cache types till we get a match */
178 h = apr_palloc(r->pool, sizeof(cache_handle_t));
180 list = cache->providers;
183 switch ((rv = list->provider->open_entity(h, r, key))) {
186 const char *varyhdr = NULL;
189 if (list->provider->recall_headers(h, r) != APR_SUCCESS) {
190 /* TODO: Handle this error */
195 * Check Content-Negotiation - Vary
197 * At this point we need to make sure that the object we found in
198 * the cache is the same object that would be delivered to the
199 * client, when the effects of content negotiation are taken into
202 * In plain english, we want to make sure that a language-negotiated
203 * document in one language is not given to a client asking for a
204 * language negotiated document in a different language by mistake.
206 * This code makes the assumption that the storage manager will
207 * cache the req_hdrs if the response contains a Vary
210 * RFC2616 13.6 and 14.44 describe the Vary mechanism.
212 if ((varyhdr = apr_table_get(h->resp_err_hdrs, "Vary")) == NULL) {
213 varyhdr = apr_table_get(h->resp_hdrs, "Vary");
215 vary = apr_pstrdup(r->pool, varyhdr);
216 while (vary && *vary) {
220 /* isolate header name */
221 while (*vary && !apr_isspace(*vary) && (*vary != ','))
223 while (*vary && (apr_isspace(*vary) || (*vary == ','))) {
229 * is this header in the request and the header in the cached
230 * request identical? If not, we give up and do a straight get
232 h1 = apr_table_get(r->headers_in, name);
233 h2 = apr_table_get(h->req_hdrs, name);
235 /* both headers NULL, so a match - do nothing */
237 else if (h1 && h2 && !strcmp(h1, h2)) {
238 /* both headers exist and are equal - do nothing */
241 /* headers do not match, so Vary failed */
242 ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
244 "cache_select_url(): Vary header mismatch.");
249 cache->provider = list->provider;
250 cache->provider_name = list->provider_name;
252 /* Is our cached response fresh enough? */
253 fresh = ap_cache_check_freshness(h, r);
255 cache_info *info = &(h->cache_obj->info);
257 /* Make response into a conditional */
258 /* FIXME: What if the request is already conditional? */
259 if (info && info->etag) {
260 /* if we have a cached etag */
261 cache->stale_headers = apr_table_copy(r->pool,
263 apr_table_set(r->headers_in, "If-None-Match", info->etag);
264 cache->stale_handle = h;
266 else if (info && info->lastmods) {
267 /* if we have a cached Last-Modified header */
268 cache->stale_headers = apr_table_copy(r->pool,
270 apr_table_set(r->headers_in, "If-Modified-Since",
272 cache->stale_handle = h;
278 /* Okay, this response looks okay. Merge in our stuff and go. */
279 apr_table_setn(r->headers_out, "Content-Type",
280 ap_make_content_type(r, h->content_type));
281 r->filename = apr_pstrdup(r->pool, h->cache_obj->info.filename);
282 accept_headers(h, r);
288 /* try again with next cache type */
293 /* oo-er! an error */
301 apr_status_t cache_generate_key_default( request_rec *r, apr_pool_t*p, char**key )
304 *key = apr_pstrcat(p, r->hostname, r->uri, "?", r->args, NULL);
307 *key = apr_pstrcat(p, r->uri, "?", r->args, NULL);