blob: 17d015afa4c6ed7cbf3fd1732e4ab9e396133148 [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 Tarreau122eba92020-06-04 10:15:32 +020013#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020015#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020016#include <haproxy/cli.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020017#include <haproxy/filters.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020018#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020019#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020020#include <haproxy/http_rules.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020021#include <haproxy/shctx.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020022#include <haproxy/stream_interface.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020023#include <import/eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010024#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010025
William Lallemand75d93292017-11-21 20:01:24 +010026#include <types/proxy.h>
William Lallemand75d93292017-11-21 20:01:24 +010027
William Lallemand41db4602017-10-30 11:15:51 +010028#include <proto/proxy.h>
William Lallemand41db4602017-10-30 11:15:51 +010029#include <proto/log.h>
30#include <proto/stream.h>
William Lallemand41db4602017-10-30 11:15:51 +010031
William Lallemand41db4602017-10-30 11:15:51 +010032
33#include <common/cfgparse.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020034#include <haproxy/hash.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020035#include <haproxy/htx.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020036#include <haproxy/net_helper.h>
William Lallemand41db4602017-10-30 11:15:51 +010037
Christopher Faulet27d93c32018-12-15 22:32:02 +010038#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010039 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010040
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010041const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010042
Willy Tarreau2231b632019-03-29 18:26:52 +010043extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010044
45struct flt_ops cache_ops;
46
47struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010048 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010049 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010050 unsigned int maxage; /* max-age */
51 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020052 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010053 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010054};
55
Christopher Faulet95220e22018-12-07 17:34:39 +010056/* cache config for filters */
57struct cache_flt_conf {
58 union {
59 struct cache *cache; /* cache used by the filter */
60 char *name; /* cache name used during conf parsing */
61 } c;
62 unsigned int flags; /* CACHE_FLT_F_* */
63};
64
William Lallemand41db4602017-10-30 11:15:51 +010065/*
66 * cache ctx for filters
67 */
68struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010069 struct shared_block *first_block;
70};
71
72struct cache_entry {
73 unsigned int latest_validation; /* latest validation date */
74 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020075 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010076
William Lallemand41db4602017-10-30 11:15:51 +010077 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010078 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010079 unsigned char data[0];
80};
81
82#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010083#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010084
85static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020086static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010087static struct cache *tmp_cache_config = NULL;
88
Willy Tarreau8ceae722018-11-26 11:58:30 +010089DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
90
William Lallemandf528fff2017-11-23 19:43:17 +010091struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010092{
93 struct eb32_node *node;
94 struct cache_entry *entry;
95
Willy Tarreau8b507582020-02-25 09:35:07 +010096 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010097 if (!node)
98 return NULL;
99
100 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100101
102 /* if that's not the right node */
103 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
104 return NULL;
105
William Lallemand08727662017-11-21 20:01:27 +0100106 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100107 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100108 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100109 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100110 entry->eb.key = 0;
111 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100112 return NULL;
113
114}
115
116static inline struct shared_context *shctx_ptr(struct cache *cache)
117{
118 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
119}
120
William Lallemand77c11972017-10-31 20:43:01 +0100121static inline struct shared_block *block_ptr(struct cache_entry *entry)
122{
123 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
124}
125
126
127
William Lallemand41db4602017-10-30 11:15:51 +0100128static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100129cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100130{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100131 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100132 return 0;
133}
134
Christopher Faulet95220e22018-12-07 17:34:39 +0100135static void
136cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
137{
138 struct cache_flt_conf *cconf = fconf->conf;
139
140 free(cconf);
141}
142
William Lallemand4da3f8a2017-10-31 14:33:34 +0100143static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100144cache_store_check(struct proxy *px, struct flt_conf *fconf)
145{
146 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100147 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100148 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100149 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100150
William Lallemandd1d1e222019-08-28 15:22:49 +0200151 /* Find the cache corresponding to the name in the filter config. The
152 * cache will not be referenced now in the filter config because it is
153 * not fully allocated. This step will be performed during the cache
154 * post_check.
155 */
156 list_for_each_entry(cache, &caches_config, list) {
157 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100158 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100159 }
160
161 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
162 proxy_type_str(px), px->id, (char *)cconf->c.name);
163 return 1;
164
165 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100166 /* Here <cache> points on the cache the filter must use and <cconf>
167 * points on the cache filter configuration. */
168
169 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100170 * enabled and if it is after the cache. When the compression is before
171 * the cache, an error is returned. Also check if the cache filter must
172 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100173 list_for_each_entry(f, &px->filter_configs, list) {
174 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100175 /* The compression filter must be evaluated after the cache. */
176 if (comp) {
177 ha_alert("config: %s '%s': unable to enable the compression filter before "
178 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
179 return 1;
180 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100181 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200182 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100183 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200184 else if (f->id == fcgi_flt_id)
185 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100186 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
187 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200188 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100189 * declaration is required. */
190 ha_alert("config: %s '%s': require an explicit filter declaration "
191 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
192 return 1;
193 }
194
Christopher Fauletafd819c2018-12-11 08:57:45 +0100195 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100196 return 0;
197}
198
199static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100200cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100201{
Christopher Faulet65554e12020-03-06 14:52:06 +0100202 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100203
Christopher Faulet65554e12020-03-06 14:52:06 +0100204 st = pool_alloc_dirty(pool_head_cache_st);
205 if (st == NULL)
206 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100207
Christopher Faulet65554e12020-03-06 14:52:06 +0100208 st->first_block = NULL;
209 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100210
Christopher Faulet65554e12020-03-06 14:52:06 +0100211 /* Register post-analyzer on AN_RES_WAIT_HTTP */
212 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100213 return 1;
214}
215
Christopher Faulet65554e12020-03-06 14:52:06 +0100216static void
217cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100218{
219 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100220 struct cache_flt_conf *cconf = FLT_CONF(filter);
221 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100222 struct shared_context *shctx = shctx_ptr(cache);
223
William Lallemand49dc0482017-11-24 14:33:54 +0100224 /* Everything should be released in the http_end filter, but we need to do it
225 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100226 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100227 shctx_lock(shctx);
228 shctx_row_dec_hot(shctx, st->first_block);
229 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100230 }
231 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100232 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100233 filter->ctx = NULL;
234 }
William Lallemand49dc0482017-11-24 14:33:54 +0100235}
236
Christopher Faulet839791a2019-01-07 16:12:07 +0100237static int
238cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
239 unsigned an_bit)
240{
241 struct http_txn *txn = s->txn;
242 struct http_msg *msg = &txn->rsp;
243 struct cache_st *st = filter->ctx;
244
245 if (an_bit != AN_RES_WAIT_HTTP)
246 goto end;
247
248 /* Here we need to check if any compression filter precedes the cache
249 * filter. This is only possible when the compression is configured in
250 * the frontend while the cache filter is configured on the
251 * backend. This case cannot be detected during HAProxy startup. So in
252 * such cases, the cache is disabled.
253 */
254 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
255 pool_free(pool_head_cache_st, st);
256 filter->ctx = NULL;
257 }
258
259 end:
260 return 1;
261}
William Lallemand49dc0482017-11-24 14:33:54 +0100262
263static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100264cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
265{
266 struct cache_st *st = filter->ctx;
267
William Lallemand4da3f8a2017-10-31 14:33:34 +0100268 if (!(msg->chn->flags & CF_ISRESP) || !st)
269 return 1;
270
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200271 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100272 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100273 return 1;
274}
275
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200276static inline void disable_cache_entry(struct cache_st *st,
277 struct filter *filter, struct shared_context *shctx)
278{
279 struct cache_entry *object;
280
281 object = (struct cache_entry *)st->first_block->data;
282 filter->ctx = NULL; /* disable cache */
283 shctx_lock(shctx);
284 shctx_row_dec_hot(shctx, st->first_block);
285 object->eb.key = 0;
286 shctx_unlock(shctx);
287 pool_free(pool_head_cache_st, st);
288}
289
William Lallemand4da3f8a2017-10-31 14:33:34 +0100290static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100291cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
292 unsigned int offset, unsigned int len)
293{
Christopher Faulet95220e22018-12-07 17:34:39 +0100294 struct cache_flt_conf *cconf = FLT_CONF(filter);
295 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100296 struct cache_st *st = filter->ctx;
297 struct htx *htx = htxbuf(&msg->chn->buf);
298 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200299 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100300 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200301 unsigned int orig_len, to_forward;
302 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100303
304 if (!len)
305 return len;
306
307 if (!st->first_block) {
308 unregister_data_filter(s, msg->chn, filter);
309 return len;
310 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100311
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200312 chunk_reset(&trash);
313 orig_len = len;
314 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100315
316 htxret = htx_find_offset(htx, offset);
317 blk = htxret.blk;
318 offset = htxret.ret;
319 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100320 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200321 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100322 struct ist v;
323
324 switch (type) {
325 case HTX_BLK_UNUSED:
326 break;
327
328 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100329 v = htx_get_blk_value(htx, blk);
330 v.ptr += offset;
331 v.len -= offset;
332 if (v.len > len)
333 v.len = len;
334
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200335 info = (type << 28) + v.len;
336 chunk_memcat(&trash, (char *)&info, sizeof(info));
337 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100338 to_forward += v.len;
339 len -= v.len;
340 break;
341
342 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200343 /* Here offset must always be 0 because only
344 * DATA blocks can be partially transferred. */
345 if (offset)
346 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100347 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200348 goto end;
349
350 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
351 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100352 to_forward += sz;
353 len -= sz;
354 break;
355 }
356
357 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100358 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200359
360 end:
361 shctx_lock(shctx);
362 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
363 if (!fb) {
364 shctx_unlock(shctx);
365 goto no_cache;
366 }
367 shctx_unlock(shctx);
368
369 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
370 (unsigned char *)b_head(&trash), b_data(&trash));
371 if (ret < 0)
372 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100373
374 return to_forward;
375
376 no_cache:
377 disable_cache_entry(st, filter, shctx);
378 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200379 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100380}
381
382static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100383cache_store_http_end(struct stream *s, struct filter *filter,
384 struct http_msg *msg)
385{
386 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100387 struct cache_flt_conf *cconf = FLT_CONF(filter);
388 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100389 struct shared_context *shctx = shctx_ptr(cache);
390 struct cache_entry *object;
391
392 if (!(msg->chn->flags & CF_ISRESP))
393 return 1;
394
395 if (st && st->first_block) {
396
397 object = (struct cache_entry *)st->first_block->data;
398
399 /* does not need to test if the insertion worked, if it
400 * doesn't, the blocks will be reused anyway */
401
402 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100403 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
404 object->eb.key = 0;
405 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100406 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100407 shctx_row_dec_hot(shctx, st->first_block);
408 shctx_unlock(shctx);
409
410 }
411 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100412 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100413 filter->ctx = NULL;
414 }
415
416 return 1;
417}
418
419 /*
420 * This intends to be used when checking HTTP headers for some
421 * word=value directive. Return a pointer to the first character of value, if
422 * the word was not found or if there wasn't any value assigned ot it return NULL
423 */
424char *directive_value(const char *sample, int slen, const char *word, int wlen)
425{
426 int st = 0;
427
428 if (slen < wlen)
429 return 0;
430
431 while (wlen) {
432 char c = *sample ^ *word;
433 if (c && c != ('A' ^ 'a'))
434 return NULL;
435 sample++;
436 word++;
437 slen--;
438 wlen--;
439 }
440
441 while (slen) {
442 if (st == 0) {
443 if (*sample != '=')
444 return NULL;
445 sample++;
446 slen--;
447 st = 1;
448 continue;
449 } else {
450 return (char *)sample;
451 }
452 }
453
454 return NULL;
455}
456
457/*
458 * Return the maxage in seconds of an HTTP response.
459 * Compute the maxage using either:
460 * - the assigned max-age of the cache
461 * - the s-maxage directive
462 * - the max-age directive
463 * - (Expires - Data) headers
464 * - the default-max-age of the cache
465 *
466 */
William Lallemand49b44532017-11-24 18:53:43 +0100467int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100468{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200469 struct htx *htx = htxbuf(&s->res.buf);
470 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100471 int smaxage = -1;
472 int maxage = -1;
473
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200474 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
475 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100476
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200477 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
478 if (value) {
479 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100480
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200481 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
482 chunk_strncat(chk, "", 1);
483 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100484 }
485
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200486 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
487 if (value) {
488 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200489
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200490 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
491 chunk_strncat(chk, "", 1);
492 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100493 }
494 }
495
496 /* TODO: Expires - Data */
497
498
499 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100500 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100501
502 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100503 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100504
William Lallemand49b44532017-11-24 18:53:43 +0100505 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100506
507}
508
509
William Lallemanda400a3a2017-11-20 19:13:12 +0100510static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
511{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200512 struct cache_entry *object = (struct cache_entry *)block->data;
513
514 if (first == block && object->eb.key)
515 eb32_delete(&object->eb);
516 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100517}
518
William Lallemand41db4602017-10-30 11:15:51 +0100519/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500520 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100521 * register a filter to store the data
522 */
523enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200524 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100525{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200526 unsigned int age;
527 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100528 struct http_txn *txn = s->txn;
529 struct http_msg *msg = &txn->rsp;
530 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100531 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100532 struct cache_flt_conf *cconf = rule->arg.act.p[0];
533 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100534 struct cache_st *cache_ctx = NULL;
535 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100536 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200537 struct htx *htx;
538 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200539 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200540 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100541
William Lallemand4da3f8a2017-10-31 14:33:34 +0100542 /* Don't cache if the response came from a cache */
543 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
544 s->target == &http_cache_applet.obj_type) {
545 goto out;
546 }
547
548 /* cache only HTTP/1.1 */
549 if (!(txn->req.flags & HTTP_MSGF_VER_11))
550 goto out;
551
Willy Tarreau6905d182019-10-01 17:59:17 +0200552 /* cache only GET method */
553 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100554 goto out;
555
Willy Tarreauc9036c02019-01-11 19:38:25 +0100556 /* cache key was not computed */
557 if (!key)
558 goto out;
559
William Lallemand4da3f8a2017-10-31 14:33:34 +0100560 /* cache only 200 status code */
561 if (txn->status != 200)
562 goto out;
563
Christopher Faulet839791a2019-01-07 16:12:07 +0100564 /* Find the corresponding filter instance for the current stream */
565 list_for_each_entry(filter, &s->strm_flt.filters, list) {
566 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
567 /* No filter ctx, don't cache anything */
568 if (!filter->ctx)
569 goto out;
570 cache_ctx = filter->ctx;
571 break;
572 }
573 }
574
575 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200576 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100577
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200578 /* Do not cache too big objects. */
579 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
580 htx->data + htx->extra > shctx->max_obj_size)
581 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100582
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200583 /* Does not manage Vary at the moment. We will need a secondary key later for that */
584 ctx.blk = NULL;
585 if (http_find_header(htx, ist("Vary"), &ctx, 0))
586 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100587
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200588 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100589
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200590 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
591 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100592
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200593 age = 0;
594 ctx.blk = NULL;
595 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
596 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
597 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
598 hdr_age = CACHE_ENTRY_MAX_AGE;
599 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100600 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200601 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100602 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100603
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200604 chunk_reset(&trash);
605 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
606 struct htx_blk *blk = htx_get_blk(htx, pos);
607 enum htx_blk_type type = htx_get_blk_type(blk);
608 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100609
Christopher Fauletb0667472019-09-03 22:22:12 +0200610 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200611 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
612 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
613 if (type == HTX_BLK_EOH)
614 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200615 }
616
Christopher Fauletb0667472019-09-03 22:22:12 +0200617 /* Do not cache objects if the headers are too big. */
618 if (hdrs_len > htx->size - global.tune.maxrewrite)
619 goto out;
620
William Lallemand4da3f8a2017-10-31 14:33:34 +0100621 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200622 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100623 if (!first) {
624 shctx_unlock(shctx);
625 goto out;
626 }
627 shctx_unlock(shctx);
628
Willy Tarreau1093a452018-04-06 19:02:25 +0200629 /* the received memory is not initialized, we need at least to mark
630 * the object as not indexed yet.
631 */
632 object = (struct cache_entry *)first->data;
633 object->eb.node.leaf_p = NULL;
634 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200635 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200636
William Lallemand4da3f8a2017-10-31 14:33:34 +0100637 /* reserve space for the cache_entry structure */
638 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200639 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100640 /* cache the headers in a http action because it allows to chose what
641 * to cache, for example you might want to cache a response before
642 * modifying some HTTP headers, or on the contrary after modifying
643 * those headers.
644 */
645
646 /* does not need to be locked because it's in the "hot" list,
647 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200648 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
649 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100650
651 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100652 if (cache_ctx) {
653 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100654
Willy Tarreauc9036c02019-01-11 19:38:25 +0100655 object->eb.key = key;
656
Christopher Faulet839791a2019-01-07 16:12:07 +0100657 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
658 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100659
Christopher Faulet839791a2019-01-07 16:12:07 +0100660 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100661
Christopher Faulet839791a2019-01-07 16:12:07 +0100662 old = entry_exist(cconf->c.cache, txn->cache_hash);
663 if (old) {
664 eb32_delete(&old->eb);
665 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100666 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100667 shctx_unlock(shctx);
668
669 /* store latest value and expiration time */
670 object->latest_validation = now.tv_sec;
671 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
672 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100673 }
674
675out:
676 /* if does not cache */
677 if (first) {
678 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100679 first->len = 0;
680 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100681 shctx_row_dec_hot(shctx, first);
682 shctx_unlock(shctx);
683 }
684
William Lallemand41db4602017-10-30 11:15:51 +0100685 return ACT_RET_CONT;
686}
687
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100688#define HTX_CACHE_INIT 0 /* Initial state. */
689#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
690#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200691#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
692#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100693
William Lallemandecb73b12017-11-24 14:33:55 +0100694static void http_cache_applet_release(struct appctx *appctx)
695{
Christopher Faulet95220e22018-12-07 17:34:39 +0100696 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100697 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100698 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100699 struct shared_block *first = block_ptr(cache_ptr);
700
701 shctx_lock(shctx_ptr(cache));
702 shctx_row_dec_hot(shctx_ptr(cache), first);
703 shctx_unlock(shctx_ptr(cache));
704}
705
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200706
707static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
708 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100709{
Christopher Faulet95220e22018-12-07 17:34:39 +0100710 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
711 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200712 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200713 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200714 unsigned int max, total;
715 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100716
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200717 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
718 if (!max)
719 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200720 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200721 ? (info & 0xff) + ((info >> 8) & 0xfffff)
722 : info & 0xfffffff);
723 if (blksz > max)
724 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100725
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200726 blk = htx_add_blk(htx, type, blksz);
727 if (!blk)
728 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100729
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200730 blk->info = info;
731 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200732 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200733 while (blksz) {
734 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200735 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200736 offset += max;
737 blksz -= max;
738 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200739 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200740 if (blksz || offset == shctx->block_size) {
741 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
742 offset = 0;
743 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100744 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200745 appctx->ctx.cache.offset = offset;
746 appctx->ctx.cache.next = shblk;
747 appctx->ctx.cache.sent += total;
748 return total;
749}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100750
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200751static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
752 uint32_t info, struct shared_block *shblk, unsigned int offset)
753{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100754
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200755 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
756 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
757 unsigned int max, total, rem_data;
758 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100759
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200760 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
761 if (!max)
762 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100763
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200764 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200765 if (appctx->ctx.cache.rem_data) {
766 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200767 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200768 }
769 else {
770 blksz = (info & 0xfffffff);
771 total = 4;
772 }
773 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200774 rem_data = blksz - max;
775 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100776 }
777
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200778 while (blksz) {
779 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100780
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200781 max = MIN(blksz, shctx->block_size - offset);
782 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
783 offset += sz;
784 blksz -= sz;
785 total += sz;
786 if (sz < max)
787 break;
788 if (blksz || offset == shctx->block_size) {
789 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
790 offset = 0;
791 }
792 }
793
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200794 appctx->ctx.cache.offset = offset;
795 appctx->ctx.cache.next = shblk;
796 appctx->ctx.cache.sent += total;
797 appctx->ctx.cache.rem_data = rem_data + blksz;
798 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100799}
800
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200801static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
802 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100803{
Christopher Faulet95220e22018-12-07 17:34:39 +0100804 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
805 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200806 struct shared_block *shblk;
807 unsigned int offset, sz;
808 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100809
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200810 while (len) {
811 enum htx_blk_type type;
812 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100813
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200814 shblk = appctx->ctx.cache.next;
815 offset = appctx->ctx.cache.offset;
816 if (appctx->ctx.cache.rem_data) {
817 type = HTX_BLK_DATA;
818 info = 0;
819 goto add_data_blk;
820 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100821
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500822 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200823 sz = MIN(4, shctx->block_size - offset);
824 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
825 offset += sz;
826 if (sz < 4) {
827 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
828 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
829 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100830 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200831
832 /* Get payload of the next HTX block and insert it. */
833 type = (info >> 28);
834 if (type != HTX_BLK_DATA)
835 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
836 else {
837 add_data_blk:
838 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100839 }
840
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200841 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100842 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200843 total += ret;
844 len -= ret;
845
846 if (appctx->ctx.cache.rem_data || type == mark)
847 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100848 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100849
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100850 return total;
851}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200852
853static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
854{
855 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
856 unsigned int age;
857 char *end;
858
859 chunk_reset(&trash);
860 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
861 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
862 age = CACHE_ENTRY_MAX_AGE;
863 end = ultoa_o(age, b_head(&trash), b_size(&trash));
864 b_set_data(&trash, end - b_head(&trash));
865 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
866 return 0;
867 return 1;
868}
869
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200870static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100871{
872 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
873 struct shared_block *first = block_ptr(cache_ptr);
874 struct stream_interface *si = appctx->owner;
875 struct channel *req = si_oc(si);
876 struct channel *res = si_ic(si);
877 struct htx *req_htx, *res_htx;
878 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200879 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100880 size_t ret, total = 0;
881
882 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200883 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100884
885 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
886 goto out;
887
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500888 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100889 if (!b_size(&res->buf)) {
890 si_rx_room_blk(si);
891 goto out;
892 }
893
Willy Tarreauefef3232018-12-16 00:37:45 +0100894 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100895 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100896
897 if (appctx->st0 == HTX_CACHE_INIT) {
898 appctx->ctx.cache.next = block_ptr(cache_ptr);
899 appctx->ctx.cache.offset = sizeof(*cache_ptr);
900 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200901 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100902 appctx->st0 = HTX_CACHE_HEADER;
903 }
904
905 if (appctx->st0 == HTX_CACHE_HEADER) {
906 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200907 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
908 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
909 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
910 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100911 goto error;
912
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200913 /* Skip response body for HEAD requests */
914 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100915 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100916 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200917 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100918 }
919
920 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200921 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
922 if (len) {
923 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
924 if (ret < len) {
925 si_rx_room_blk(si);
926 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100927 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100928 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200929 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100930 }
931
932 if (appctx->st0 == HTX_CACHE_EOM) {
933 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]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001182 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1183 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) {
1194 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1195 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001196 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001197 err_code |= ERR_ALERT | ERR_ABORT;
1198 goto out;
1199 }
1200
1201 strlcpy2(tmp_cache_config->id, args[1], 33);
1202 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001203 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1204 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001205 err_code |= ERR_WARN;
1206 }
William Lallemand49b44532017-11-24 18:53:43 +01001207 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001208 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001209 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001210 }
1211 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001212 unsigned long int maxsize;
1213 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001214
1215 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1216 err_code |= ERR_ABORT;
1217 goto out;
1218 }
1219
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001220 maxsize = strtoul(args[1], &err, 10);
1221 if (err == args[1] || *err != '\0') {
1222 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1223 file, linenum, args[1]);
1224 err_code |= ERR_ABORT;
1225 goto out;
1226 }
1227
1228 if (maxsize > (UINT_MAX >> 20)) {
1229 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1230 file, linenum, args[1], UINT_MAX >> 20);
1231 err_code |= ERR_ABORT;
1232 goto out;
1233 }
1234
William Lallemand41db4602017-10-30 11:15:51 +01001235 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001236 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001237 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001238 } else if (strcmp(args[0], "max-age") == 0) {
1239 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1240 err_code |= ERR_ABORT;
1241 goto out;
1242 }
1243
1244 if (!*args[1]) {
1245 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1246 file, linenum, args[0]);
1247 err_code |= ERR_WARN;
1248 }
1249
1250 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001251 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001252 unsigned int maxobjsz;
1253 char *err;
1254
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001255 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1256 err_code |= ERR_ABORT;
1257 goto out;
1258 }
1259
1260 if (!*args[1]) {
1261 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1262 file, linenum, args[0]);
1263 err_code |= ERR_WARN;
1264 }
1265
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001266 maxobjsz = strtoul(args[1], &err, 10);
1267 if (err == args[1] || *err != '\0') {
1268 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1269 file, linenum, args[1]);
1270 err_code |= ERR_ABORT;
1271 goto out;
1272 }
1273 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001274 }
1275 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001276 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001277 err_code |= ERR_ALERT | ERR_FATAL;
1278 goto out;
1279 }
1280out:
1281 return err_code;
1282}
1283
1284/* once the cache section is parsed */
1285
1286int cfg_post_parse_section_cache()
1287{
William Lallemand41db4602017-10-30 11:15:51 +01001288 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001289
1290 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001291
1292 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001293 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001294 err_code |= ERR_FATAL | ERR_ALERT;
1295 goto out;
1296 }
1297
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001298 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001299 /* Default max. file size is a 256th of the cache size. */
1300 tmp_cache_config->maxobjsz =
1301 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001302 }
1303 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1304 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1305 err_code |= ERR_FATAL | ERR_ALERT;
1306 goto out;
1307 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001308
William Lallemandd1d1e222019-08-28 15:22:49 +02001309 /* add to the list of cache to init and reinit tmp_cache_config
1310 * for next cache section, if any.
1311 */
1312 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1313 tmp_cache_config = NULL;
1314 return err_code;
1315 }
1316out:
1317 free(tmp_cache_config);
1318 tmp_cache_config = NULL;
1319 return err_code;
1320
1321}
1322
1323int post_check_cache()
1324{
1325 struct proxy *px;
1326 struct cache *back, *cache_config, *cache;
1327 struct shared_context *shctx;
1328 int ret_shctx;
1329 int err_code = 0;
1330
1331 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1332
1333 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1334 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001335
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001336 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001337 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001338 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001339 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001340 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001341
1342 err_code |= ERR_FATAL | ERR_ALERT;
1343 goto out;
1344 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001345 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001346 /* the cache structure is stored in the shctx and added to the
1347 * caches list, we can remove the entry from the caches_config
1348 * list */
1349 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001350 cache = (struct cache *)shctx->data;
1351 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001352 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001353 LIST_DEL(&cache_config->list);
1354 free(cache_config);
1355
1356 /* Find all references for this cache in the existing filters
1357 * (over all proxies) and reference it in matching filters.
1358 */
1359 for (px = proxies_list; px; px = px->next) {
1360 struct flt_conf *fconf;
1361 struct cache_flt_conf *cconf;
1362
1363 list_for_each_entry(fconf, &px->filter_configs, list) {
1364 if (fconf->id != cache_store_flt_id)
1365 continue;
1366
1367 cconf = fconf->conf;
1368 if (!strcmp(cache->id, cconf->c.name)) {
1369 free(cconf->c.name);
1370 cconf->c.cache = cache;
1371 break;
1372 }
1373 }
1374 }
William Lallemand41db4602017-10-30 11:15:51 +01001375 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001376
William Lallemand41db4602017-10-30 11:15:51 +01001377out:
William Lallemand41db4602017-10-30 11:15:51 +01001378 return err_code;
1379
William Lallemand41db4602017-10-30 11:15:51 +01001380}
1381
William Lallemand41db4602017-10-30 11:15:51 +01001382struct flt_ops cache_ops = {
1383 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001384 .check = cache_store_check,
1385 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001386
Christopher Faulet65554e12020-03-06 14:52:06 +01001387 /* Handle stream init/deinit */
1388 .attach = cache_store_strm_init,
1389 .detach = cache_store_strm_deinit,
1390
William Lallemand4da3f8a2017-10-31 14:33:34 +01001391 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001392 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001393
1394 /* Filter HTTP requests and responses */
1395 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001396 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001397 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001398};
1399
Christopher Faulet99a17a22018-12-11 09:18:27 +01001400
1401
1402static int
1403parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1404 struct flt_conf *fconf, char **err, void *private)
1405{
1406 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001407 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001408 char *name = NULL;
1409 int pos = *cur_arg;
1410
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001411 /* Get the cache filter name. <pos> point on "cache" keyword */
1412 if (!*args[pos + 1]) {
1413 memprintf(err, "%s : expects an <id> argument", args[pos]);
1414 goto error;
1415 }
1416 name = strdup(args[pos + 1]);
1417 if (!name) {
1418 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1419 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001420 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001421 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001422
1423 /* Check if an implicit filter with the same name already exists. If so,
1424 * we remove the implicit filter to use the explicit one. */
1425 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1426 if (f->id != cache_store_flt_id)
1427 continue;
1428
1429 cconf = f->conf;
1430 if (strcmp(name, cconf->c.name)) {
1431 cconf = NULL;
1432 continue;
1433 }
1434
1435 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1436 cconf = NULL;
1437 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1438 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001439 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001440 }
1441
1442 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1443 LIST_DEL(&f->list);
1444 free(f);
1445 free(name);
1446 break;
1447 }
1448
1449 /* No implicit cache filter found, create configuration for the explicit one */
1450 if (!cconf) {
1451 cconf = calloc(1, sizeof(*cconf));
1452 if (!cconf) {
1453 memprintf(err, "%s: out of memory", args[*cur_arg]);
1454 goto error;
1455 }
1456 cconf->c.name = name;
1457 }
1458
1459 cconf->flags = 0;
1460 fconf->id = cache_store_flt_id;
1461 fconf->conf = cconf;
1462 fconf->ops = &cache_ops;
1463
1464 *cur_arg = pos;
1465 return 0;
1466
1467 error:
1468 free(name);
1469 free(cconf);
1470 return -1;
1471}
1472
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001473static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001474{
1475 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1476 return 1;
1477
1478 return 0;
1479}
1480
1481static int cli_io_handler_show_cache(struct appctx *appctx)
1482{
1483 struct cache* cache = appctx->ctx.cli.p0;
1484 struct stream_interface *si = appctx->owner;
1485
William Lallemand1f49a362017-11-21 20:01:26 +01001486 if (cache == NULL) {
1487 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1488 }
1489
1490 list_for_each_entry_from(cache, &caches, list) {
1491 struct eb32_node *node = NULL;
1492 unsigned int next_key;
1493 struct cache_entry *entry;
1494
William Lallemand1f49a362017-11-21 20:01:26 +01001495 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001496 if (!next_key) {
1497 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1498 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001499 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001500 return 0;
1501 }
1502 }
William Lallemand1f49a362017-11-21 20:01:26 +01001503
1504 appctx->ctx.cli.p0 = cache;
1505
1506 while (1) {
1507
1508 shctx_lock(shctx_ptr(cache));
1509 node = eb32_lookup_ge(&cache->entries, next_key);
1510 if (!node) {
1511 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001512 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001513 break;
1514 }
1515
1516 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001517 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 +01001518
1519 next_key = node->key + 1;
1520 appctx->ctx.cli.i0 = next_key;
1521
1522 shctx_unlock(shctx_ptr(cache));
1523
1524 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001525 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001526 return 0;
1527 }
1528 }
1529
1530 }
1531
1532 return 1;
1533
1534}
1535
Christopher Faulet99a17a22018-12-11 09:18:27 +01001536/* Declare the filter parser for "cache" keyword */
1537static struct flt_kw_list filter_kws = { "CACHE", { }, {
1538 { "cache", parse_cache_flt, NULL },
1539 { NULL, NULL, NULL },
1540 }
1541};
1542
1543INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1544
William Lallemand1f49a362017-11-21 20:01:26 +01001545static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001546 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1547 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001548}};
1549
Willy Tarreau0108d902018-11-25 19:14:37 +01001550INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001551
William Lallemand41db4602017-10-30 11:15:51 +01001552static struct action_kw_list http_res_actions = {
1553 .kw = {
1554 { "cache-store", parse_cache_store },
1555 { NULL, NULL }
1556 }
1557};
1558
Willy Tarreau0108d902018-11-25 19:14:37 +01001559INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1560
William Lallemand41db4602017-10-30 11:15:51 +01001561static struct action_kw_list http_req_actions = {
1562 .kw = {
1563 { "cache-use", parse_cache_use },
1564 { NULL, NULL }
1565 }
1566};
1567
Willy Tarreau0108d902018-11-25 19:14:37 +01001568INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1569
Willy Tarreau2231b632019-03-29 18:26:52 +01001570struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001571 .obj_type = OBJ_TYPE_APPLET,
1572 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001573 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001574 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001575};
1576
Willy Tarreaue6552512018-11-26 11:33:13 +01001577/* config parsers for this section */
1578REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001579REGISTER_POST_CHECK(post_check_cache);