blob: fe213a6aae872a0a2861fa43a150960512e3bbf2 [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
2 * Cache management
3 *
4 * Copyright 2017 HAProxy Technologies
5 * William Lallemand <wlallemand@haproxy.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
Willy Tarreaub2551052020-06-09 09:07:15 +020013#include <import/eb32tree.h>
14#include <import/sha1.h>
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020018#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020020#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020021#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020022#include <haproxy/filters.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/hash.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020024#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020025#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020026#include <haproxy/http_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/htx.h>
28#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020029#include <haproxy/proxy.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020030#include <haproxy/shctx.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020031#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020032#include <haproxy/stream_interface.h>
William Lallemand41db4602017-10-30 11:15:51 +010033
Christopher Faulet27d93c32018-12-15 22:32:02 +010034#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010035 * the filter keyword) */
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +020036#define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */
Christopher Fauletafd819c2018-12-11 08:57:45 +010037
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010038const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010039
Willy Tarreau2231b632019-03-29 18:26:52 +010040extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010041
42struct flt_ops cache_ops;
43
44struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010045 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010046 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010047 unsigned int maxage; /* max-age */
48 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020049 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010050 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010051};
52
Christopher Faulet95220e22018-12-07 17:34:39 +010053/* cache config for filters */
54struct cache_flt_conf {
55 union {
56 struct cache *cache; /* cache used by the filter */
57 char *name; /* cache name used during conf parsing */
58 } c;
59 unsigned int flags; /* CACHE_FLT_F_* */
60};
61
William Lallemand41db4602017-10-30 11:15:51 +010062/*
63 * cache ctx for filters
64 */
65struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010066 struct shared_block *first_block;
67};
68
69struct cache_entry {
70 unsigned int latest_validation; /* latest validation date */
71 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020072 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010073
William Lallemand41db4602017-10-30 11:15:51 +010074 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010075 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010076 unsigned char data[0];
77};
78
79#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010080#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010081
82static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020083static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010084static struct cache *tmp_cache_config = NULL;
85
Willy Tarreau8ceae722018-11-26 11:58:30 +010086DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
87
William Lallemandf528fff2017-11-23 19:43:17 +010088struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010089{
90 struct eb32_node *node;
91 struct cache_entry *entry;
92
Willy Tarreau8b507582020-02-25 09:35:07 +010093 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010094 if (!node)
95 return NULL;
96
97 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010098
99 /* if that's not the right node */
100 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
101 return NULL;
102
William Lallemand08727662017-11-21 20:01:27 +0100103 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100104 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100105 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100106 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100107 entry->eb.key = 0;
108 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100109 return NULL;
110
111}
112
113static inline struct shared_context *shctx_ptr(struct cache *cache)
114{
115 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
116}
117
William Lallemand77c11972017-10-31 20:43:01 +0100118static inline struct shared_block *block_ptr(struct cache_entry *entry)
119{
120 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
121}
122
123
124
William Lallemand41db4602017-10-30 11:15:51 +0100125static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100126cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100127{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100128 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100129 return 0;
130}
131
Christopher Faulet95220e22018-12-07 17:34:39 +0100132static void
133cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
134{
135 struct cache_flt_conf *cconf = fconf->conf;
136
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200137 if (!(cconf->flags & CACHE_FLT_INIT))
138 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100139 free(cconf);
140}
141
William Lallemand4da3f8a2017-10-31 14:33:34 +0100142static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100143cache_store_check(struct proxy *px, struct flt_conf *fconf)
144{
145 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100146 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100147 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100148 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100149
William Lallemandd1d1e222019-08-28 15:22:49 +0200150 /* Find the cache corresponding to the name in the filter config. The
151 * cache will not be referenced now in the filter config because it is
152 * not fully allocated. This step will be performed during the cache
153 * post_check.
154 */
155 list_for_each_entry(cache, &caches_config, list) {
156 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100157 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100158 }
159
160 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
161 proxy_type_str(px), px->id, (char *)cconf->c.name);
162 return 1;
163
164 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100165 /* Here <cache> points on the cache the filter must use and <cconf>
166 * points on the cache filter configuration. */
167
168 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100169 * enabled and if it is after the cache. When the compression is before
170 * the cache, an error is returned. Also check if the cache filter must
171 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100172 list_for_each_entry(f, &px->filter_configs, list) {
173 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100174 /* The compression filter must be evaluated after the cache. */
175 if (comp) {
176 ha_alert("config: %s '%s': unable to enable the compression filter before "
177 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
178 return 1;
179 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100180 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200181 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100182 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200183 else if (f->id == fcgi_flt_id)
184 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100185 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
186 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200187 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100188 * declaration is required. */
189 ha_alert("config: %s '%s': require an explicit filter declaration "
190 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
191 return 1;
192 }
193
Christopher Fauletafd819c2018-12-11 08:57:45 +0100194 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100195 return 0;
196}
197
198static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100199cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100200{
Christopher Faulet65554e12020-03-06 14:52:06 +0100201 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100202
Christopher Faulet65554e12020-03-06 14:52:06 +0100203 st = pool_alloc_dirty(pool_head_cache_st);
204 if (st == NULL)
205 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100206
Christopher Faulet65554e12020-03-06 14:52:06 +0100207 st->first_block = NULL;
208 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100209
Christopher Faulet65554e12020-03-06 14:52:06 +0100210 /* Register post-analyzer on AN_RES_WAIT_HTTP */
211 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100212 return 1;
213}
214
Christopher Faulet65554e12020-03-06 14:52:06 +0100215static void
216cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100217{
218 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100219 struct cache_flt_conf *cconf = FLT_CONF(filter);
220 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100221 struct shared_context *shctx = shctx_ptr(cache);
222
William Lallemand49dc0482017-11-24 14:33:54 +0100223 /* Everything should be released in the http_end filter, but we need to do it
224 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100225 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100226 shctx_lock(shctx);
227 shctx_row_dec_hot(shctx, st->first_block);
228 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100229 }
230 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100231 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100232 filter->ctx = NULL;
233 }
William Lallemand49dc0482017-11-24 14:33:54 +0100234}
235
Christopher Faulet839791a2019-01-07 16:12:07 +0100236static int
237cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
238 unsigned an_bit)
239{
240 struct http_txn *txn = s->txn;
241 struct http_msg *msg = &txn->rsp;
242 struct cache_st *st = filter->ctx;
243
244 if (an_bit != AN_RES_WAIT_HTTP)
245 goto end;
246
247 /* Here we need to check if any compression filter precedes the cache
248 * filter. This is only possible when the compression is configured in
249 * the frontend while the cache filter is configured on the
250 * backend. This case cannot be detected during HAProxy startup. So in
251 * such cases, the cache is disabled.
252 */
253 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
254 pool_free(pool_head_cache_st, st);
255 filter->ctx = NULL;
256 }
257
258 end:
259 return 1;
260}
William Lallemand49dc0482017-11-24 14:33:54 +0100261
262static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100263cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
264{
265 struct cache_st *st = filter->ctx;
266
William Lallemand4da3f8a2017-10-31 14:33:34 +0100267 if (!(msg->chn->flags & CF_ISRESP) || !st)
268 return 1;
269
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200270 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100271 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100272 return 1;
273}
274
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200275static inline void disable_cache_entry(struct cache_st *st,
276 struct filter *filter, struct shared_context *shctx)
277{
278 struct cache_entry *object;
279
280 object = (struct cache_entry *)st->first_block->data;
281 filter->ctx = NULL; /* disable cache */
282 shctx_lock(shctx);
283 shctx_row_dec_hot(shctx, st->first_block);
284 object->eb.key = 0;
285 shctx_unlock(shctx);
286 pool_free(pool_head_cache_st, st);
287}
288
William Lallemand4da3f8a2017-10-31 14:33:34 +0100289static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100290cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
291 unsigned int offset, unsigned int len)
292{
Christopher Faulet95220e22018-12-07 17:34:39 +0100293 struct cache_flt_conf *cconf = FLT_CONF(filter);
294 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100295 struct cache_st *st = filter->ctx;
296 struct htx *htx = htxbuf(&msg->chn->buf);
297 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200298 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100299 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200300 unsigned int orig_len, to_forward;
301 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100302
303 if (!len)
304 return len;
305
306 if (!st->first_block) {
307 unregister_data_filter(s, msg->chn, filter);
308 return len;
309 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100310
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200311 chunk_reset(&trash);
312 orig_len = len;
313 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100314
315 htxret = htx_find_offset(htx, offset);
316 blk = htxret.blk;
317 offset = htxret.ret;
318 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100319 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200320 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100321 struct ist v;
322
323 switch (type) {
324 case HTX_BLK_UNUSED:
325 break;
326
327 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100328 v = htx_get_blk_value(htx, blk);
329 v.ptr += offset;
330 v.len -= offset;
331 if (v.len > len)
332 v.len = len;
333
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200334 info = (type << 28) + v.len;
335 chunk_memcat(&trash, (char *)&info, sizeof(info));
336 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100337 to_forward += v.len;
338 len -= v.len;
339 break;
340
341 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200342 /* Here offset must always be 0 because only
343 * DATA blocks can be partially transferred. */
344 if (offset)
345 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100346 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200347 goto end;
348
349 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
350 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100351 to_forward += sz;
352 len -= sz;
353 break;
354 }
355
356 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100357 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200358
359 end:
360 shctx_lock(shctx);
361 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
362 if (!fb) {
363 shctx_unlock(shctx);
364 goto no_cache;
365 }
366 shctx_unlock(shctx);
367
368 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
369 (unsigned char *)b_head(&trash), b_data(&trash));
370 if (ret < 0)
371 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100372
373 return to_forward;
374
375 no_cache:
376 disable_cache_entry(st, filter, shctx);
377 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200378 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100379}
380
381static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100382cache_store_http_end(struct stream *s, struct filter *filter,
383 struct http_msg *msg)
384{
385 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100386 struct cache_flt_conf *cconf = FLT_CONF(filter);
387 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100388 struct shared_context *shctx = shctx_ptr(cache);
389 struct cache_entry *object;
390
391 if (!(msg->chn->flags & CF_ISRESP))
392 return 1;
393
394 if (st && st->first_block) {
395
396 object = (struct cache_entry *)st->first_block->data;
397
398 /* does not need to test if the insertion worked, if it
399 * doesn't, the blocks will be reused anyway */
400
401 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100402 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
403 object->eb.key = 0;
404 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100405 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100406 shctx_row_dec_hot(shctx, st->first_block);
407 shctx_unlock(shctx);
408
409 }
410 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100411 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100412 filter->ctx = NULL;
413 }
414
415 return 1;
416}
417
418 /*
419 * This intends to be used when checking HTTP headers for some
420 * word=value directive. Return a pointer to the first character of value, if
421 * the word was not found or if there wasn't any value assigned ot it return NULL
422 */
423char *directive_value(const char *sample, int slen, const char *word, int wlen)
424{
425 int st = 0;
426
427 if (slen < wlen)
428 return 0;
429
430 while (wlen) {
431 char c = *sample ^ *word;
432 if (c && c != ('A' ^ 'a'))
433 return NULL;
434 sample++;
435 word++;
436 slen--;
437 wlen--;
438 }
439
440 while (slen) {
441 if (st == 0) {
442 if (*sample != '=')
443 return NULL;
444 sample++;
445 slen--;
446 st = 1;
447 continue;
448 } else {
449 return (char *)sample;
450 }
451 }
452
453 return NULL;
454}
455
456/*
457 * Return the maxage in seconds of an HTTP response.
458 * Compute the maxage using either:
459 * - the assigned max-age of the cache
460 * - the s-maxage directive
461 * - the max-age directive
462 * - (Expires - Data) headers
463 * - the default-max-age of the cache
464 *
465 */
William Lallemand49b44532017-11-24 18:53:43 +0100466int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100467{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200468 struct htx *htx = htxbuf(&s->res.buf);
469 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100470 int smaxage = -1;
471 int maxage = -1;
472
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200473 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
474 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100475
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200476 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
477 if (value) {
478 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100479
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200480 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
481 chunk_strncat(chk, "", 1);
482 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100483 }
484
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200485 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
486 if (value) {
487 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200488
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200489 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
490 chunk_strncat(chk, "", 1);
491 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100492 }
493 }
494
495 /* TODO: Expires - Data */
496
497
498 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100499 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100500
501 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100502 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100503
William Lallemand49b44532017-11-24 18:53:43 +0100504 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100505
506}
507
508
William Lallemanda400a3a2017-11-20 19:13:12 +0100509static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
510{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200511 struct cache_entry *object = (struct cache_entry *)block->data;
512
513 if (first == block && object->eb.key)
514 eb32_delete(&object->eb);
515 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100516}
517
William Lallemand41db4602017-10-30 11:15:51 +0100518/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500519 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100520 * register a filter to store the data
521 */
522enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200523 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100524{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200525 unsigned int age;
526 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100527 struct http_txn *txn = s->txn;
528 struct http_msg *msg = &txn->rsp;
529 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100530 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100531 struct cache_flt_conf *cconf = rule->arg.act.p[0];
532 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100533 struct cache_st *cache_ctx = NULL;
534 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100535 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200536 struct htx *htx;
537 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200538 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200539 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100540
William Lallemand4da3f8a2017-10-31 14:33:34 +0100541 /* Don't cache if the response came from a cache */
542 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
543 s->target == &http_cache_applet.obj_type) {
544 goto out;
545 }
546
547 /* cache only HTTP/1.1 */
548 if (!(txn->req.flags & HTTP_MSGF_VER_11))
549 goto out;
550
Willy Tarreau6905d182019-10-01 17:59:17 +0200551 /* cache only GET method */
552 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100553 goto out;
554
Willy Tarreauc9036c02019-01-11 19:38:25 +0100555 /* cache key was not computed */
556 if (!key)
557 goto out;
558
William Lallemand4da3f8a2017-10-31 14:33:34 +0100559 /* cache only 200 status code */
560 if (txn->status != 200)
561 goto out;
562
Christopher Faulet839791a2019-01-07 16:12:07 +0100563 /* Find the corresponding filter instance for the current stream */
564 list_for_each_entry(filter, &s->strm_flt.filters, list) {
565 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
566 /* No filter ctx, don't cache anything */
567 if (!filter->ctx)
568 goto out;
569 cache_ctx = filter->ctx;
570 break;
571 }
572 }
573
574 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200575 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100576
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200577 /* Do not cache too big objects. */
578 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
579 htx->data + htx->extra > shctx->max_obj_size)
580 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100581
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200582 /* Does not manage Vary at the moment. We will need a secondary key later for that */
583 ctx.blk = NULL;
584 if (http_find_header(htx, ist("Vary"), &ctx, 0))
585 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100586
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200587 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100588
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200589 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
590 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100591
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200592 age = 0;
593 ctx.blk = NULL;
594 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
595 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
596 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
597 hdr_age = CACHE_ENTRY_MAX_AGE;
598 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100599 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200600 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100601 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100602
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200603 chunk_reset(&trash);
604 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
605 struct htx_blk *blk = htx_get_blk(htx, pos);
606 enum htx_blk_type type = htx_get_blk_type(blk);
607 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100608
Christopher Fauletb0667472019-09-03 22:22:12 +0200609 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200610 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
611 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
612 if (type == HTX_BLK_EOH)
613 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200614 }
615
Christopher Fauletb0667472019-09-03 22:22:12 +0200616 /* Do not cache objects if the headers are too big. */
617 if (hdrs_len > htx->size - global.tune.maxrewrite)
618 goto out;
619
William Lallemand4da3f8a2017-10-31 14:33:34 +0100620 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200621 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100622 if (!first) {
623 shctx_unlock(shctx);
624 goto out;
625 }
626 shctx_unlock(shctx);
627
Willy Tarreau1093a452018-04-06 19:02:25 +0200628 /* the received memory is not initialized, we need at least to mark
629 * the object as not indexed yet.
630 */
631 object = (struct cache_entry *)first->data;
632 object->eb.node.leaf_p = NULL;
633 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200634 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200635
William Lallemand4da3f8a2017-10-31 14:33:34 +0100636 /* reserve space for the cache_entry structure */
637 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200638 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100639 /* cache the headers in a http action because it allows to chose what
640 * to cache, for example you might want to cache a response before
641 * modifying some HTTP headers, or on the contrary after modifying
642 * those headers.
643 */
644
645 /* does not need to be locked because it's in the "hot" list,
646 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200647 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
648 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100649
650 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100651 if (cache_ctx) {
652 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100653
Willy Tarreauc9036c02019-01-11 19:38:25 +0100654 object->eb.key = key;
655
Christopher Faulet839791a2019-01-07 16:12:07 +0100656 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
657 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100658
Christopher Faulet839791a2019-01-07 16:12:07 +0100659 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100660
Christopher Faulet839791a2019-01-07 16:12:07 +0100661 old = entry_exist(cconf->c.cache, txn->cache_hash);
662 if (old) {
663 eb32_delete(&old->eb);
664 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100665 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100666 shctx_unlock(shctx);
667
668 /* store latest value and expiration time */
669 object->latest_validation = now.tv_sec;
670 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
671 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100672 }
673
674out:
675 /* if does not cache */
676 if (first) {
677 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100678 first->len = 0;
679 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100680 shctx_row_dec_hot(shctx, first);
681 shctx_unlock(shctx);
682 }
683
William Lallemand41db4602017-10-30 11:15:51 +0100684 return ACT_RET_CONT;
685}
686
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100687#define HTX_CACHE_INIT 0 /* Initial state. */
688#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
689#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200690#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
691#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100692
William Lallemandecb73b12017-11-24 14:33:55 +0100693static void http_cache_applet_release(struct appctx *appctx)
694{
Christopher Faulet95220e22018-12-07 17:34:39 +0100695 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100696 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100697 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100698 struct shared_block *first = block_ptr(cache_ptr);
699
700 shctx_lock(shctx_ptr(cache));
701 shctx_row_dec_hot(shctx_ptr(cache), first);
702 shctx_unlock(shctx_ptr(cache));
703}
704
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200705
706static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
707 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100708{
Christopher Faulet95220e22018-12-07 17:34:39 +0100709 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
710 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200711 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200712 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200713 unsigned int max, total;
714 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100715
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200716 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
717 if (!max)
718 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200719 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200720 ? (info & 0xff) + ((info >> 8) & 0xfffff)
721 : info & 0xfffffff);
722 if (blksz > max)
723 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100724
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200725 blk = htx_add_blk(htx, type, blksz);
726 if (!blk)
727 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100728
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200729 blk->info = info;
730 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200731 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200732 while (blksz) {
733 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200734 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200735 offset += max;
736 blksz -= max;
737 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200738 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200739 if (blksz || offset == shctx->block_size) {
740 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
741 offset = 0;
742 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100743 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200744 appctx->ctx.cache.offset = offset;
745 appctx->ctx.cache.next = shblk;
746 appctx->ctx.cache.sent += total;
747 return total;
748}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100749
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200750static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
751 uint32_t info, struct shared_block *shblk, unsigned int offset)
752{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100753
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200754 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
755 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
756 unsigned int max, total, rem_data;
757 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100758
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200759 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
760 if (!max)
761 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100762
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200763 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200764 if (appctx->ctx.cache.rem_data) {
765 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200766 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200767 }
768 else {
769 blksz = (info & 0xfffffff);
770 total = 4;
771 }
772 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200773 rem_data = blksz - max;
774 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100775 }
776
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200777 while (blksz) {
778 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100779
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200780 max = MIN(blksz, shctx->block_size - offset);
781 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
782 offset += sz;
783 blksz -= sz;
784 total += sz;
785 if (sz < max)
786 break;
787 if (blksz || offset == shctx->block_size) {
788 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
789 offset = 0;
790 }
791 }
792
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200793 appctx->ctx.cache.offset = offset;
794 appctx->ctx.cache.next = shblk;
795 appctx->ctx.cache.sent += total;
796 appctx->ctx.cache.rem_data = rem_data + blksz;
797 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100798}
799
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200800static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
801 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100802{
Christopher Faulet95220e22018-12-07 17:34:39 +0100803 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
804 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200805 struct shared_block *shblk;
806 unsigned int offset, sz;
807 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100808
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200809 while (len) {
810 enum htx_blk_type type;
811 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100812
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200813 shblk = appctx->ctx.cache.next;
814 offset = appctx->ctx.cache.offset;
815 if (appctx->ctx.cache.rem_data) {
816 type = HTX_BLK_DATA;
817 info = 0;
818 goto add_data_blk;
819 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100820
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500821 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200822 sz = MIN(4, shctx->block_size - offset);
823 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
824 offset += sz;
825 if (sz < 4) {
826 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
827 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
828 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100829 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200830
831 /* Get payload of the next HTX block and insert it. */
832 type = (info >> 28);
833 if (type != HTX_BLK_DATA)
834 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
835 else {
836 add_data_blk:
837 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100838 }
839
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200840 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100841 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200842 total += ret;
843 len -= ret;
844
845 if (appctx->ctx.cache.rem_data || type == mark)
846 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100847 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100848
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100849 return total;
850}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200851
852static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
853{
854 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
855 unsigned int age;
856 char *end;
857
858 chunk_reset(&trash);
859 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
860 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
861 age = CACHE_ENTRY_MAX_AGE;
862 end = ultoa_o(age, b_head(&trash), b_size(&trash));
863 b_set_data(&trash, end - b_head(&trash));
864 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
865 return 0;
866 return 1;
867}
868
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200869static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100870{
871 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
872 struct shared_block *first = block_ptr(cache_ptr);
873 struct stream_interface *si = appctx->owner;
874 struct channel *req = si_oc(si);
875 struct channel *res = si_ic(si);
876 struct htx *req_htx, *res_htx;
877 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200878 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100879 size_t ret, total = 0;
880
881 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200882 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100883
884 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
885 goto out;
886
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500887 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100888 if (!b_size(&res->buf)) {
889 si_rx_room_blk(si);
890 goto out;
891 }
892
Willy Tarreauefef3232018-12-16 00:37:45 +0100893 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100894 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100895
896 if (appctx->st0 == HTX_CACHE_INIT) {
897 appctx->ctx.cache.next = block_ptr(cache_ptr);
898 appctx->ctx.cache.offset = sizeof(*cache_ptr);
899 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200900 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100901 appctx->st0 = HTX_CACHE_HEADER;
902 }
903
904 if (appctx->st0 == HTX_CACHE_HEADER) {
905 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200906 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
907 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
908 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
909 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100910 goto error;
911
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200912 /* Skip response body for HEAD requests */
913 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100914 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100915 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200916 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100917 }
918
919 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200920 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
921 if (len) {
922 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
923 if (ret < len) {
924 si_rx_room_blk(si);
925 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100926 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100927 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200928 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100929 }
930
931 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +0200932 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100933 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
934 si_rx_room_blk(si);
935 goto out;
936 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100937 appctx->st0 = HTX_CACHE_END;
938 }
939
940 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100941 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100942 res->flags |= CF_READ_NULL;
943 si_shutr(si);
944 }
945
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100946 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200947 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100948 if (total)
949 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100950 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100951
952 /* eat the whole request */
953 if (co_data(req)) {
954 req_htx = htx_from_buf(&req->buf);
955 co_htx_skip(req, req_htx, co_data(req));
956 htx_to_buf(req_htx, &req->buf);
957 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100958 return;
959
960 error:
961 /* Sent and HTTP error 500 */
962 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200963 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100964 res->buf.data = b_data(errmsg);
965 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
966 res_htx = htx_from_buf(&res->buf);
967
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200968 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100969 appctx->st0 = HTX_CACHE_END;
970 goto end;
971}
972
973
Christopher Faulet95220e22018-12-07 17:34:39 +0100974static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100975{
976 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100977 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100978
Christopher Faulet95220e22018-12-07 17:34:39 +0100979 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100980 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100981 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100982 }
983
984 /* check if a cache filter was already registered with this cache
985 * name, if that's the case, must use it. */
986 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100987 if (fconf->id == cache_store_flt_id) {
988 cconf = fconf->conf;
989 if (cconf && !strcmp((char *)cconf->c.name, name)) {
990 rule->arg.act.p[0] = cconf;
991 return 1;
992 }
William Lallemand41db4602017-10-30 11:15:51 +0100993 }
994 }
995
Christopher Faulet95220e22018-12-07 17:34:39 +0100996 /* Create the filter cache config */
997 cconf = calloc(1, sizeof(*cconf));
998 if (!cconf) {
999 memprintf(err, "out of memory\n");
1000 goto err;
1001 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001002 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001003 cconf->c.name = strdup(name);
1004 if (!cconf->c.name) {
1005 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001006 goto err;
1007 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001008
William Lallemand41db4602017-10-30 11:15:51 +01001009 /* register a filter to fill the cache buffer */
1010 fconf = calloc(1, sizeof(*fconf));
1011 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001012 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001013 goto err;
1014 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001015 fconf->id = cache_store_flt_id;
1016 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001017 fconf->ops = &cache_ops;
1018 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1019
Christopher Faulet95220e22018-12-07 17:34:39 +01001020 rule->arg.act.p[0] = cconf;
1021 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001022
Christopher Faulet95220e22018-12-07 17:34:39 +01001023 err:
1024 free(cconf);
1025 return 0;
1026}
1027
1028enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1029 struct act_rule *rule, char **err)
1030{
1031 rule->action = ACT_CUSTOM;
1032 rule->action_ptr = http_action_store_cache;
1033
1034 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1035 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001036
Christopher Faulet95220e22018-12-07 17:34:39 +01001037 (*orig_arg)++;
1038 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001039}
1040
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001041/* This produces a sha1 hash of the concatenation of the HTTP method,
1042 * the first occurrence of the Host header followed by the path component
1043 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001044int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001045{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001046 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001047 struct htx *htx = htxbuf(&s->req.buf);
1048 struct htx_sl *sl;
1049 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001050 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001051 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001052 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001053
William Lallemandf528fff2017-11-23 19:43:17 +01001054 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001055 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001056
1057 switch (txn->meth) {
1058 case HTTP_METH_HEAD:
1059 case HTTP_METH_GET:
1060 chunk_memcat(trash, "GET", 3);
1061 break;
1062 default:
1063 return 0;
1064 }
1065
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001066 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001067 uri = htx_sl_req_uri(sl); // whole uri
1068 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001069 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001070
1071 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1072 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1073 * URIs are almost always sent in absolute form with their scheme. In
1074 * this case, the scheme is almost always "https". In order to support
1075 * sharing of cache objects between H1 and H2, we'll hash the absolute
1076 * URI whenever known, or prepend "https://" + the Host header for
1077 * relative URIs. The difference will only appear on absolute HTTP/1
1078 * requests sent to an origin server, which practically is never met in
1079 * the real world so we don't care about the ability to share the same
1080 * key here.URIs are normalized from the absolute URI to an origin form as
1081 * well.
1082 */
1083 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001084 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001085 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1086 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001087 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001088 }
1089
1090 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001091
1092 /* hash everything */
1093 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001094 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001095 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1096
1097 return 1;
1098}
1099
William Lallemand41db4602017-10-30 11:15:51 +01001100enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1101 struct session *sess, struct stream *s, int flags)
1102{
William Lallemand77c11972017-10-31 20:43:01 +01001103
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001104 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001105 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001106 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1107 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001108
Willy Tarreau6905d182019-10-01 17:59:17 +02001109 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1110 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001111 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001112 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001113 txn->flags |= TX_CACHE_IGNORE;
1114
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001115 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001116
Willy Tarreau504455c2017-12-22 17:47:35 +01001117 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1118 return ACT_RET_CONT;
1119
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001120 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001121 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001122
Willy Tarreau504455c2017-12-22 17:47:35 +01001123 if (s->txn->flags & TX_CACHE_IGNORE)
1124 return ACT_RET_CONT;
1125
Willy Tarreaua1214a52018-12-14 14:00:25 +01001126 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001127 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001128 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001129 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001130
William Lallemanda400a3a2017-11-20 19:13:12 +01001131 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001132 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001133 if (res) {
1134 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001135 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1136 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001137 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001138 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001139 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001140 appctx->rule = rule;
1141 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001142 appctx->ctx.cache.next = NULL;
1143 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001144
1145 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001146 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001147 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001148 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001149 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001150 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001151 shctx_lock(shctx_ptr(cache));
1152 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1153 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001154 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001155 }
1156 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001157 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001158 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001159}
1160
1161
1162enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1163 struct act_rule *rule, char **err)
1164{
William Lallemand41db4602017-10-30 11:15:51 +01001165 rule->action = ACT_CUSTOM;
1166 rule->action_ptr = http_action_req_cache_use;
1167
Christopher Faulet95220e22018-12-07 17:34:39 +01001168 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001169 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001170
1171 (*orig_arg)++;
1172 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001173}
1174
1175int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1176{
1177 int err_code = 0;
1178
1179 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1180
1181 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001182 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001183 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001184 err_code |= ERR_ALERT | ERR_ABORT;
1185 goto out;
1186 }
1187
1188 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1189 err_code |= ERR_ABORT;
1190 goto out;
1191 }
1192
1193 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001194 struct cache *cache_config;
1195
William Lallemand41db4602017-10-30 11:15:51 +01001196 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1197 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001198 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001199 err_code |= ERR_ALERT | ERR_ABORT;
1200 goto out;
1201 }
1202
1203 strlcpy2(tmp_cache_config->id, args[1], 33);
1204 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001205 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001206 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001207 err_code |= ERR_WARN;
1208 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001209
1210 list_for_each_entry(cache_config, &caches_config, list) {
1211 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1212 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1213 file, linenum, tmp_cache_config->id);
1214 err_code |= ERR_ALERT | ERR_ABORT;
1215 goto out;
1216 }
1217 }
1218
William Lallemand49b44532017-11-24 18:53:43 +01001219 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001220 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001221 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001222 }
1223 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001224 unsigned long int maxsize;
1225 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001226
1227 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1228 err_code |= ERR_ABORT;
1229 goto out;
1230 }
1231
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001232 maxsize = strtoul(args[1], &err, 10);
1233 if (err == args[1] || *err != '\0') {
1234 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1235 file, linenum, args[1]);
1236 err_code |= ERR_ABORT;
1237 goto out;
1238 }
1239
1240 if (maxsize > (UINT_MAX >> 20)) {
1241 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1242 file, linenum, args[1], UINT_MAX >> 20);
1243 err_code |= ERR_ABORT;
1244 goto out;
1245 }
1246
William Lallemand41db4602017-10-30 11:15:51 +01001247 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001248 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001249 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001250 } else if (strcmp(args[0], "max-age") == 0) {
1251 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1252 err_code |= ERR_ABORT;
1253 goto out;
1254 }
1255
1256 if (!*args[1]) {
1257 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1258 file, linenum, args[0]);
1259 err_code |= ERR_WARN;
1260 }
1261
1262 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001263 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001264 unsigned int maxobjsz;
1265 char *err;
1266
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001267 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1268 err_code |= ERR_ABORT;
1269 goto out;
1270 }
1271
1272 if (!*args[1]) {
1273 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1274 file, linenum, args[0]);
1275 err_code |= ERR_WARN;
1276 }
1277
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001278 maxobjsz = strtoul(args[1], &err, 10);
1279 if (err == args[1] || *err != '\0') {
1280 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1281 file, linenum, args[1]);
1282 err_code |= ERR_ABORT;
1283 goto out;
1284 }
1285 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001286 }
1287 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001288 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001289 err_code |= ERR_ALERT | ERR_FATAL;
1290 goto out;
1291 }
1292out:
1293 return err_code;
1294}
1295
1296/* once the cache section is parsed */
1297
1298int cfg_post_parse_section_cache()
1299{
William Lallemand41db4602017-10-30 11:15:51 +01001300 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001301
1302 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001303
1304 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001305 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001306 err_code |= ERR_FATAL | ERR_ALERT;
1307 goto out;
1308 }
1309
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001310 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001311 /* Default max. file size is a 256th of the cache size. */
1312 tmp_cache_config->maxobjsz =
1313 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001314 }
1315 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1316 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1317 err_code |= ERR_FATAL | ERR_ALERT;
1318 goto out;
1319 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001320
William Lallemandd1d1e222019-08-28 15:22:49 +02001321 /* add to the list of cache to init and reinit tmp_cache_config
1322 * for next cache section, if any.
1323 */
1324 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1325 tmp_cache_config = NULL;
1326 return err_code;
1327 }
1328out:
1329 free(tmp_cache_config);
1330 tmp_cache_config = NULL;
1331 return err_code;
1332
1333}
1334
1335int post_check_cache()
1336{
1337 struct proxy *px;
1338 struct cache *back, *cache_config, *cache;
1339 struct shared_context *shctx;
1340 int ret_shctx;
1341 int err_code = 0;
1342
1343 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1344
1345 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1346 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001347
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001348 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001349 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001350 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001351 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001352 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001353
1354 err_code |= ERR_FATAL | ERR_ALERT;
1355 goto out;
1356 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001357 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001358 /* the cache structure is stored in the shctx and added to the
1359 * caches list, we can remove the entry from the caches_config
1360 * list */
1361 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001362 cache = (struct cache *)shctx->data;
1363 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001364 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001365 LIST_DEL(&cache_config->list);
1366 free(cache_config);
1367
1368 /* Find all references for this cache in the existing filters
1369 * (over all proxies) and reference it in matching filters.
1370 */
1371 for (px = proxies_list; px; px = px->next) {
1372 struct flt_conf *fconf;
1373 struct cache_flt_conf *cconf;
1374
1375 list_for_each_entry(fconf, &px->filter_configs, list) {
1376 if (fconf->id != cache_store_flt_id)
1377 continue;
1378
1379 cconf = fconf->conf;
1380 if (!strcmp(cache->id, cconf->c.name)) {
1381 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001382 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001383 cconf->c.cache = cache;
1384 break;
1385 }
1386 }
1387 }
William Lallemand41db4602017-10-30 11:15:51 +01001388 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001389
William Lallemand41db4602017-10-30 11:15:51 +01001390out:
William Lallemand41db4602017-10-30 11:15:51 +01001391 return err_code;
1392
William Lallemand41db4602017-10-30 11:15:51 +01001393}
1394
William Lallemand41db4602017-10-30 11:15:51 +01001395struct flt_ops cache_ops = {
1396 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001397 .check = cache_store_check,
1398 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001399
Christopher Faulet65554e12020-03-06 14:52:06 +01001400 /* Handle stream init/deinit */
1401 .attach = cache_store_strm_init,
1402 .detach = cache_store_strm_deinit,
1403
William Lallemand4da3f8a2017-10-31 14:33:34 +01001404 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001405 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001406
1407 /* Filter HTTP requests and responses */
1408 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001409 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001410 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001411};
1412
Christopher Faulet99a17a22018-12-11 09:18:27 +01001413
1414
1415static int
1416parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1417 struct flt_conf *fconf, char **err, void *private)
1418{
1419 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001420 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001421 char *name = NULL;
1422 int pos = *cur_arg;
1423
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001424 /* Get the cache filter name. <pos> point on "cache" keyword */
1425 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02001426 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001427 goto error;
1428 }
1429 name = strdup(args[pos + 1]);
1430 if (!name) {
1431 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1432 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001433 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001434 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001435
1436 /* Check if an implicit filter with the same name already exists. If so,
1437 * we remove the implicit filter to use the explicit one. */
1438 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1439 if (f->id != cache_store_flt_id)
1440 continue;
1441
1442 cconf = f->conf;
1443 if (strcmp(name, cconf->c.name)) {
1444 cconf = NULL;
1445 continue;
1446 }
1447
1448 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1449 cconf = NULL;
1450 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1451 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001452 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001453 }
1454
1455 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1456 LIST_DEL(&f->list);
1457 free(f);
1458 free(name);
1459 break;
1460 }
1461
1462 /* No implicit cache filter found, create configuration for the explicit one */
1463 if (!cconf) {
1464 cconf = calloc(1, sizeof(*cconf));
1465 if (!cconf) {
1466 memprintf(err, "%s: out of memory", args[*cur_arg]);
1467 goto error;
1468 }
1469 cconf->c.name = name;
1470 }
1471
1472 cconf->flags = 0;
1473 fconf->id = cache_store_flt_id;
1474 fconf->conf = cconf;
1475 fconf->ops = &cache_ops;
1476
1477 *cur_arg = pos;
1478 return 0;
1479
1480 error:
1481 free(name);
1482 free(cconf);
1483 return -1;
1484}
1485
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001486static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001487{
1488 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1489 return 1;
1490
1491 return 0;
1492}
1493
1494static int cli_io_handler_show_cache(struct appctx *appctx)
1495{
1496 struct cache* cache = appctx->ctx.cli.p0;
1497 struct stream_interface *si = appctx->owner;
1498
William Lallemand1f49a362017-11-21 20:01:26 +01001499 if (cache == NULL) {
1500 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1501 }
1502
1503 list_for_each_entry_from(cache, &caches, list) {
1504 struct eb32_node *node = NULL;
1505 unsigned int next_key;
1506 struct cache_entry *entry;
1507
William Lallemand1f49a362017-11-21 20:01:26 +01001508 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001509 if (!next_key) {
1510 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1511 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001512 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001513 return 0;
1514 }
1515 }
William Lallemand1f49a362017-11-21 20:01:26 +01001516
1517 appctx->ctx.cli.p0 = cache;
1518
1519 while (1) {
1520
1521 shctx_lock(shctx_ptr(cache));
1522 node = eb32_lookup_ge(&cache->entries, next_key);
1523 if (!node) {
1524 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001525 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001526 break;
1527 }
1528
1529 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001530 chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, read_u32(entry->hash), block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec);
William Lallemand1f49a362017-11-21 20:01:26 +01001531
1532 next_key = node->key + 1;
1533 appctx->ctx.cli.i0 = next_key;
1534
1535 shctx_unlock(shctx_ptr(cache));
1536
1537 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001538 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001539 return 0;
1540 }
1541 }
1542
1543 }
1544
1545 return 1;
1546
1547}
1548
Christopher Faulet99a17a22018-12-11 09:18:27 +01001549/* Declare the filter parser for "cache" keyword */
1550static struct flt_kw_list filter_kws = { "CACHE", { }, {
1551 { "cache", parse_cache_flt, NULL },
1552 { NULL, NULL, NULL },
1553 }
1554};
1555
1556INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1557
William Lallemand1f49a362017-11-21 20:01:26 +01001558static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001559 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1560 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001561}};
1562
Willy Tarreau0108d902018-11-25 19:14:37 +01001563INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001564
William Lallemand41db4602017-10-30 11:15:51 +01001565static struct action_kw_list http_res_actions = {
1566 .kw = {
1567 { "cache-store", parse_cache_store },
1568 { NULL, NULL }
1569 }
1570};
1571
Willy Tarreau0108d902018-11-25 19:14:37 +01001572INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1573
William Lallemand41db4602017-10-30 11:15:51 +01001574static struct action_kw_list http_req_actions = {
1575 .kw = {
1576 { "cache-use", parse_cache_use },
1577 { NULL, NULL }
1578 }
1579};
1580
Willy Tarreau0108d902018-11-25 19:14:37 +01001581INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1582
Willy Tarreau2231b632019-03-29 18:26:52 +01001583struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001584 .obj_type = OBJ_TYPE_APPLET,
1585 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001586 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001587 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001588};
1589
Willy Tarreaue6552512018-11-26 11:33:13 +01001590/* config parsers for this section */
1591REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001592REGISTER_POST_CHECK(post_check_cache);