blob: b42262948a80904d31d08d24b2171b50f7a932e4 [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) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010036
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010037const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010038
Willy Tarreau2231b632019-03-29 18:26:52 +010039extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010040
41struct flt_ops cache_ops;
42
43struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010044 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010045 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010046 unsigned int maxage; /* max-age */
47 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020048 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010049 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010050};
51
Christopher Faulet95220e22018-12-07 17:34:39 +010052/* cache config for filters */
53struct cache_flt_conf {
54 union {
55 struct cache *cache; /* cache used by the filter */
56 char *name; /* cache name used during conf parsing */
57 } c;
58 unsigned int flags; /* CACHE_FLT_F_* */
59};
60
William Lallemand41db4602017-10-30 11:15:51 +010061/*
62 * cache ctx for filters
63 */
64struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010065 struct shared_block *first_block;
66};
67
68struct cache_entry {
69 unsigned int latest_validation; /* latest validation date */
70 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020071 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010072
William Lallemand41db4602017-10-30 11:15:51 +010073 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010074 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010075 unsigned char data[0];
76};
77
78#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010079#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010080
81static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020082static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010083static struct cache *tmp_cache_config = NULL;
84
Willy Tarreau8ceae722018-11-26 11:58:30 +010085DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
86
William Lallemandf528fff2017-11-23 19:43:17 +010087struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010088{
89 struct eb32_node *node;
90 struct cache_entry *entry;
91
Willy Tarreau8b507582020-02-25 09:35:07 +010092 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010093 if (!node)
94 return NULL;
95
96 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010097
98 /* if that's not the right node */
99 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
100 return NULL;
101
William Lallemand08727662017-11-21 20:01:27 +0100102 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100103 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100104 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100105 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100106 entry->eb.key = 0;
107 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100108 return NULL;
109
110}
111
112static inline struct shared_context *shctx_ptr(struct cache *cache)
113{
114 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
115}
116
William Lallemand77c11972017-10-31 20:43:01 +0100117static inline struct shared_block *block_ptr(struct cache_entry *entry)
118{
119 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
120}
121
122
123
William Lallemand41db4602017-10-30 11:15:51 +0100124static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100125cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100126{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100127 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100128 return 0;
129}
130
Christopher Faulet95220e22018-12-07 17:34:39 +0100131static void
132cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
133{
134 struct cache_flt_conf *cconf = fconf->conf;
135
136 free(cconf);
137}
138
William Lallemand4da3f8a2017-10-31 14:33:34 +0100139static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100140cache_store_check(struct proxy *px, struct flt_conf *fconf)
141{
142 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100143 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100144 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100145 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100146
William Lallemandd1d1e222019-08-28 15:22:49 +0200147 /* Find the cache corresponding to the name in the filter config. The
148 * cache will not be referenced now in the filter config because it is
149 * not fully allocated. This step will be performed during the cache
150 * post_check.
151 */
152 list_for_each_entry(cache, &caches_config, list) {
153 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100154 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100155 }
156
157 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
158 proxy_type_str(px), px->id, (char *)cconf->c.name);
159 return 1;
160
161 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100162 /* Here <cache> points on the cache the filter must use and <cconf>
163 * points on the cache filter configuration. */
164
165 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100166 * enabled and if it is after the cache. When the compression is before
167 * the cache, an error is returned. Also check if the cache filter must
168 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100169 list_for_each_entry(f, &px->filter_configs, list) {
170 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100171 /* The compression filter must be evaluated after the cache. */
172 if (comp) {
173 ha_alert("config: %s '%s': unable to enable the compression filter before "
174 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
175 return 1;
176 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100177 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200178 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100179 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200180 else if (f->id == fcgi_flt_id)
181 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100182 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
183 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200184 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100185 * declaration is required. */
186 ha_alert("config: %s '%s': require an explicit filter declaration "
187 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
188 return 1;
189 }
190
Christopher Fauletafd819c2018-12-11 08:57:45 +0100191 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100192 return 0;
193}
194
195static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100196cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100197{
Christopher Faulet65554e12020-03-06 14:52:06 +0100198 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100199
Christopher Faulet65554e12020-03-06 14:52:06 +0100200 st = pool_alloc_dirty(pool_head_cache_st);
201 if (st == NULL)
202 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100203
Christopher Faulet65554e12020-03-06 14:52:06 +0100204 st->first_block = NULL;
205 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100206
Christopher Faulet65554e12020-03-06 14:52:06 +0100207 /* Register post-analyzer on AN_RES_WAIT_HTTP */
208 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100209 return 1;
210}
211
Christopher Faulet65554e12020-03-06 14:52:06 +0100212static void
213cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100214{
215 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100216 struct cache_flt_conf *cconf = FLT_CONF(filter);
217 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100218 struct shared_context *shctx = shctx_ptr(cache);
219
William Lallemand49dc0482017-11-24 14:33:54 +0100220 /* Everything should be released in the http_end filter, but we need to do it
221 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100222 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100223 shctx_lock(shctx);
224 shctx_row_dec_hot(shctx, st->first_block);
225 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100226 }
227 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100228 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100229 filter->ctx = NULL;
230 }
William Lallemand49dc0482017-11-24 14:33:54 +0100231}
232
Christopher Faulet839791a2019-01-07 16:12:07 +0100233static int
234cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
235 unsigned an_bit)
236{
237 struct http_txn *txn = s->txn;
238 struct http_msg *msg = &txn->rsp;
239 struct cache_st *st = filter->ctx;
240
241 if (an_bit != AN_RES_WAIT_HTTP)
242 goto end;
243
244 /* Here we need to check if any compression filter precedes the cache
245 * filter. This is only possible when the compression is configured in
246 * the frontend while the cache filter is configured on the
247 * backend. This case cannot be detected during HAProxy startup. So in
248 * such cases, the cache is disabled.
249 */
250 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
251 pool_free(pool_head_cache_st, st);
252 filter->ctx = NULL;
253 }
254
255 end:
256 return 1;
257}
William Lallemand49dc0482017-11-24 14:33:54 +0100258
259static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100260cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
261{
262 struct cache_st *st = filter->ctx;
263
William Lallemand4da3f8a2017-10-31 14:33:34 +0100264 if (!(msg->chn->flags & CF_ISRESP) || !st)
265 return 1;
266
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200267 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100268 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100269 return 1;
270}
271
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200272static inline void disable_cache_entry(struct cache_st *st,
273 struct filter *filter, struct shared_context *shctx)
274{
275 struct cache_entry *object;
276
277 object = (struct cache_entry *)st->first_block->data;
278 filter->ctx = NULL; /* disable cache */
279 shctx_lock(shctx);
280 shctx_row_dec_hot(shctx, st->first_block);
281 object->eb.key = 0;
282 shctx_unlock(shctx);
283 pool_free(pool_head_cache_st, st);
284}
285
William Lallemand4da3f8a2017-10-31 14:33:34 +0100286static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100287cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
288 unsigned int offset, unsigned int len)
289{
Christopher Faulet95220e22018-12-07 17:34:39 +0100290 struct cache_flt_conf *cconf = FLT_CONF(filter);
291 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100292 struct cache_st *st = filter->ctx;
293 struct htx *htx = htxbuf(&msg->chn->buf);
294 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200295 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100296 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200297 unsigned int orig_len, to_forward;
298 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100299
300 if (!len)
301 return len;
302
303 if (!st->first_block) {
304 unregister_data_filter(s, msg->chn, filter);
305 return len;
306 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100307
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200308 chunk_reset(&trash);
309 orig_len = len;
310 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100311
312 htxret = htx_find_offset(htx, offset);
313 blk = htxret.blk;
314 offset = htxret.ret;
315 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100316 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200317 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100318 struct ist v;
319
320 switch (type) {
321 case HTX_BLK_UNUSED:
322 break;
323
324 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100325 v = htx_get_blk_value(htx, blk);
326 v.ptr += offset;
327 v.len -= offset;
328 if (v.len > len)
329 v.len = len;
330
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200331 info = (type << 28) + v.len;
332 chunk_memcat(&trash, (char *)&info, sizeof(info));
333 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100334 to_forward += v.len;
335 len -= v.len;
336 break;
337
338 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200339 /* Here offset must always be 0 because only
340 * DATA blocks can be partially transferred. */
341 if (offset)
342 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100343 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200344 goto end;
345
346 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
347 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100348 to_forward += sz;
349 len -= sz;
350 break;
351 }
352
353 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100354 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200355
356 end:
357 shctx_lock(shctx);
358 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
359 if (!fb) {
360 shctx_unlock(shctx);
361 goto no_cache;
362 }
363 shctx_unlock(shctx);
364
365 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
366 (unsigned char *)b_head(&trash), b_data(&trash));
367 if (ret < 0)
368 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100369
370 return to_forward;
371
372 no_cache:
373 disable_cache_entry(st, filter, shctx);
374 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200375 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100376}
377
378static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100379cache_store_http_end(struct stream *s, struct filter *filter,
380 struct http_msg *msg)
381{
382 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100383 struct cache_flt_conf *cconf = FLT_CONF(filter);
384 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100385 struct shared_context *shctx = shctx_ptr(cache);
386 struct cache_entry *object;
387
388 if (!(msg->chn->flags & CF_ISRESP))
389 return 1;
390
391 if (st && st->first_block) {
392
393 object = (struct cache_entry *)st->first_block->data;
394
395 /* does not need to test if the insertion worked, if it
396 * doesn't, the blocks will be reused anyway */
397
398 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100399 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
400 object->eb.key = 0;
401 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100402 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100403 shctx_row_dec_hot(shctx, st->first_block);
404 shctx_unlock(shctx);
405
406 }
407 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100408 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100409 filter->ctx = NULL;
410 }
411
412 return 1;
413}
414
415 /*
416 * This intends to be used when checking HTTP headers for some
417 * word=value directive. Return a pointer to the first character of value, if
418 * the word was not found or if there wasn't any value assigned ot it return NULL
419 */
420char *directive_value(const char *sample, int slen, const char *word, int wlen)
421{
422 int st = 0;
423
424 if (slen < wlen)
425 return 0;
426
427 while (wlen) {
428 char c = *sample ^ *word;
429 if (c && c != ('A' ^ 'a'))
430 return NULL;
431 sample++;
432 word++;
433 slen--;
434 wlen--;
435 }
436
437 while (slen) {
438 if (st == 0) {
439 if (*sample != '=')
440 return NULL;
441 sample++;
442 slen--;
443 st = 1;
444 continue;
445 } else {
446 return (char *)sample;
447 }
448 }
449
450 return NULL;
451}
452
453/*
454 * Return the maxage in seconds of an HTTP response.
455 * Compute the maxage using either:
456 * - the assigned max-age of the cache
457 * - the s-maxage directive
458 * - the max-age directive
459 * - (Expires - Data) headers
460 * - the default-max-age of the cache
461 *
462 */
William Lallemand49b44532017-11-24 18:53:43 +0100463int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100464{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200465 struct htx *htx = htxbuf(&s->res.buf);
466 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100467 int smaxage = -1;
468 int maxage = -1;
469
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200470 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
471 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100472
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200473 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
474 if (value) {
475 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100476
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200477 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
478 chunk_strncat(chk, "", 1);
479 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100480 }
481
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200482 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
483 if (value) {
484 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200485
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200486 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
487 chunk_strncat(chk, "", 1);
488 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100489 }
490 }
491
492 /* TODO: Expires - Data */
493
494
495 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100496 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100497
498 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100499 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100500
William Lallemand49b44532017-11-24 18:53:43 +0100501 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502
503}
504
505
William Lallemanda400a3a2017-11-20 19:13:12 +0100506static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
507{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200508 struct cache_entry *object = (struct cache_entry *)block->data;
509
510 if (first == block && object->eb.key)
511 eb32_delete(&object->eb);
512 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100513}
514
William Lallemand41db4602017-10-30 11:15:51 +0100515/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500516 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100517 * register a filter to store the data
518 */
519enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200520 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100521{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200522 unsigned int age;
523 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100524 struct http_txn *txn = s->txn;
525 struct http_msg *msg = &txn->rsp;
526 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100527 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100528 struct cache_flt_conf *cconf = rule->arg.act.p[0];
529 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100530 struct cache_st *cache_ctx = NULL;
531 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100532 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200533 struct htx *htx;
534 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200535 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200536 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100537
William Lallemand4da3f8a2017-10-31 14:33:34 +0100538 /* Don't cache if the response came from a cache */
539 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
540 s->target == &http_cache_applet.obj_type) {
541 goto out;
542 }
543
544 /* cache only HTTP/1.1 */
545 if (!(txn->req.flags & HTTP_MSGF_VER_11))
546 goto out;
547
Willy Tarreau6905d182019-10-01 17:59:17 +0200548 /* cache only GET method */
549 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100550 goto out;
551
Willy Tarreauc9036c02019-01-11 19:38:25 +0100552 /* cache key was not computed */
553 if (!key)
554 goto out;
555
William Lallemand4da3f8a2017-10-31 14:33:34 +0100556 /* cache only 200 status code */
557 if (txn->status != 200)
558 goto out;
559
Christopher Faulet839791a2019-01-07 16:12:07 +0100560 /* Find the corresponding filter instance for the current stream */
561 list_for_each_entry(filter, &s->strm_flt.filters, list) {
562 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
563 /* No filter ctx, don't cache anything */
564 if (!filter->ctx)
565 goto out;
566 cache_ctx = filter->ctx;
567 break;
568 }
569 }
570
571 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200572 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100573
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200574 /* Do not cache too big objects. */
575 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
576 htx->data + htx->extra > shctx->max_obj_size)
577 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100578
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200579 /* Does not manage Vary at the moment. We will need a secondary key later for that */
580 ctx.blk = NULL;
581 if (http_find_header(htx, ist("Vary"), &ctx, 0))
582 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100583
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200584 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100585
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200586 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
587 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100588
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200589 age = 0;
590 ctx.blk = NULL;
591 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
592 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
593 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
594 hdr_age = CACHE_ENTRY_MAX_AGE;
595 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100596 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200597 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100598 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100599
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200600 chunk_reset(&trash);
601 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
602 struct htx_blk *blk = htx_get_blk(htx, pos);
603 enum htx_blk_type type = htx_get_blk_type(blk);
604 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100605
Christopher Fauletb0667472019-09-03 22:22:12 +0200606 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200607 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
608 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
609 if (type == HTX_BLK_EOH)
610 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200611 }
612
Christopher Fauletb0667472019-09-03 22:22:12 +0200613 /* Do not cache objects if the headers are too big. */
614 if (hdrs_len > htx->size - global.tune.maxrewrite)
615 goto out;
616
William Lallemand4da3f8a2017-10-31 14:33:34 +0100617 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200618 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100619 if (!first) {
620 shctx_unlock(shctx);
621 goto out;
622 }
623 shctx_unlock(shctx);
624
Willy Tarreau1093a452018-04-06 19:02:25 +0200625 /* the received memory is not initialized, we need at least to mark
626 * the object as not indexed yet.
627 */
628 object = (struct cache_entry *)first->data;
629 object->eb.node.leaf_p = NULL;
630 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200631 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200632
William Lallemand4da3f8a2017-10-31 14:33:34 +0100633 /* reserve space for the cache_entry structure */
634 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200635 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100636 /* cache the headers in a http action because it allows to chose what
637 * to cache, for example you might want to cache a response before
638 * modifying some HTTP headers, or on the contrary after modifying
639 * those headers.
640 */
641
642 /* does not need to be locked because it's in the "hot" list,
643 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200644 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
645 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100646
647 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100648 if (cache_ctx) {
649 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100650
Willy Tarreauc9036c02019-01-11 19:38:25 +0100651 object->eb.key = key;
652
Christopher Faulet839791a2019-01-07 16:12:07 +0100653 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
654 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100655
Christopher Faulet839791a2019-01-07 16:12:07 +0100656 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100657
Christopher Faulet839791a2019-01-07 16:12:07 +0100658 old = entry_exist(cconf->c.cache, txn->cache_hash);
659 if (old) {
660 eb32_delete(&old->eb);
661 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100662 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100663 shctx_unlock(shctx);
664
665 /* store latest value and expiration time */
666 object->latest_validation = now.tv_sec;
667 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
668 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100669 }
670
671out:
672 /* if does not cache */
673 if (first) {
674 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100675 first->len = 0;
676 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100677 shctx_row_dec_hot(shctx, first);
678 shctx_unlock(shctx);
679 }
680
William Lallemand41db4602017-10-30 11:15:51 +0100681 return ACT_RET_CONT;
682}
683
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100684#define HTX_CACHE_INIT 0 /* Initial state. */
685#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
686#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200687#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
688#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100689
William Lallemandecb73b12017-11-24 14:33:55 +0100690static void http_cache_applet_release(struct appctx *appctx)
691{
Christopher Faulet95220e22018-12-07 17:34:39 +0100692 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100693 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100694 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100695 struct shared_block *first = block_ptr(cache_ptr);
696
697 shctx_lock(shctx_ptr(cache));
698 shctx_row_dec_hot(shctx_ptr(cache), first);
699 shctx_unlock(shctx_ptr(cache));
700}
701
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200702
703static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
704 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100705{
Christopher Faulet95220e22018-12-07 17:34:39 +0100706 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
707 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200708 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200709 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200710 unsigned int max, total;
711 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100712
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200713 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
714 if (!max)
715 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200716 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200717 ? (info & 0xff) + ((info >> 8) & 0xfffff)
718 : info & 0xfffffff);
719 if (blksz > max)
720 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100721
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200722 blk = htx_add_blk(htx, type, blksz);
723 if (!blk)
724 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100725
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200726 blk->info = info;
727 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200728 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200729 while (blksz) {
730 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200731 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200732 offset += max;
733 blksz -= max;
734 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200735 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200736 if (blksz || offset == shctx->block_size) {
737 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
738 offset = 0;
739 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100740 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200741 appctx->ctx.cache.offset = offset;
742 appctx->ctx.cache.next = shblk;
743 appctx->ctx.cache.sent += total;
744 return total;
745}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100746
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200747static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
748 uint32_t info, struct shared_block *shblk, unsigned int offset)
749{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100750
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200751 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
752 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
753 unsigned int max, total, rem_data;
754 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100755
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200756 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
757 if (!max)
758 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100759
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200760 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200761 if (appctx->ctx.cache.rem_data) {
762 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200763 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200764 }
765 else {
766 blksz = (info & 0xfffffff);
767 total = 4;
768 }
769 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200770 rem_data = blksz - max;
771 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100772 }
773
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200774 while (blksz) {
775 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100776
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200777 max = MIN(blksz, shctx->block_size - offset);
778 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
779 offset += sz;
780 blksz -= sz;
781 total += sz;
782 if (sz < max)
783 break;
784 if (blksz || offset == shctx->block_size) {
785 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
786 offset = 0;
787 }
788 }
789
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200790 appctx->ctx.cache.offset = offset;
791 appctx->ctx.cache.next = shblk;
792 appctx->ctx.cache.sent += total;
793 appctx->ctx.cache.rem_data = rem_data + blksz;
794 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100795}
796
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200797static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
798 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100799{
Christopher Faulet95220e22018-12-07 17:34:39 +0100800 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
801 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200802 struct shared_block *shblk;
803 unsigned int offset, sz;
804 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100805
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200806 while (len) {
807 enum htx_blk_type type;
808 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100809
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200810 shblk = appctx->ctx.cache.next;
811 offset = appctx->ctx.cache.offset;
812 if (appctx->ctx.cache.rem_data) {
813 type = HTX_BLK_DATA;
814 info = 0;
815 goto add_data_blk;
816 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100817
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500818 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200819 sz = MIN(4, shctx->block_size - offset);
820 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
821 offset += sz;
822 if (sz < 4) {
823 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
824 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
825 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100826 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200827
828 /* Get payload of the next HTX block and insert it. */
829 type = (info >> 28);
830 if (type != HTX_BLK_DATA)
831 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
832 else {
833 add_data_blk:
834 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100835 }
836
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200837 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100838 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200839 total += ret;
840 len -= ret;
841
842 if (appctx->ctx.cache.rem_data || type == mark)
843 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100844 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100845
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100846 return total;
847}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200848
849static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
850{
851 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
852 unsigned int age;
853 char *end;
854
855 chunk_reset(&trash);
856 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
857 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
858 age = CACHE_ENTRY_MAX_AGE;
859 end = ultoa_o(age, b_head(&trash), b_size(&trash));
860 b_set_data(&trash, end - b_head(&trash));
861 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
862 return 0;
863 return 1;
864}
865
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200866static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100867{
868 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
869 struct shared_block *first = block_ptr(cache_ptr);
870 struct stream_interface *si = appctx->owner;
871 struct channel *req = si_oc(si);
872 struct channel *res = si_ic(si);
873 struct htx *req_htx, *res_htx;
874 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200875 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100876 size_t ret, total = 0;
877
878 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200879 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100880
881 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
882 goto out;
883
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500884 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100885 if (!b_size(&res->buf)) {
886 si_rx_room_blk(si);
887 goto out;
888 }
889
Willy Tarreauefef3232018-12-16 00:37:45 +0100890 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100891 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100892
893 if (appctx->st0 == HTX_CACHE_INIT) {
894 appctx->ctx.cache.next = block_ptr(cache_ptr);
895 appctx->ctx.cache.offset = sizeof(*cache_ptr);
896 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200897 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100898 appctx->st0 = HTX_CACHE_HEADER;
899 }
900
901 if (appctx->st0 == HTX_CACHE_HEADER) {
902 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200903 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
904 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
905 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
906 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100907 goto error;
908
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200909 /* Skip response body for HEAD requests */
910 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100911 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100912 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200913 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100914 }
915
916 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200917 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
918 if (len) {
919 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
920 if (ret < len) {
921 si_rx_room_blk(si);
922 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100923 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100924 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200925 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100926 }
927
928 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +0200929 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100930 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
931 si_rx_room_blk(si);
932 goto out;
933 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100934 appctx->st0 = HTX_CACHE_END;
935 }
936
937 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100938 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100939 res->flags |= CF_READ_NULL;
940 si_shutr(si);
941 }
942
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100943 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200944 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100945 if (total)
946 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100947 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100948
949 /* eat the whole request */
950 if (co_data(req)) {
951 req_htx = htx_from_buf(&req->buf);
952 co_htx_skip(req, req_htx, co_data(req));
953 htx_to_buf(req_htx, &req->buf);
954 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100955 return;
956
957 error:
958 /* Sent and HTTP error 500 */
959 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200960 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100961 res->buf.data = b_data(errmsg);
962 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
963 res_htx = htx_from_buf(&res->buf);
964
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200965 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100966 appctx->st0 = HTX_CACHE_END;
967 goto end;
968}
969
970
Christopher Faulet95220e22018-12-07 17:34:39 +0100971static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100972{
973 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100974 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100975
Christopher Faulet95220e22018-12-07 17:34:39 +0100976 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100977 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100978 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100979 }
980
981 /* check if a cache filter was already registered with this cache
982 * name, if that's the case, must use it. */
983 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100984 if (fconf->id == cache_store_flt_id) {
985 cconf = fconf->conf;
986 if (cconf && !strcmp((char *)cconf->c.name, name)) {
987 rule->arg.act.p[0] = cconf;
988 return 1;
989 }
William Lallemand41db4602017-10-30 11:15:51 +0100990 }
991 }
992
Christopher Faulet95220e22018-12-07 17:34:39 +0100993 /* Create the filter cache config */
994 cconf = calloc(1, sizeof(*cconf));
995 if (!cconf) {
996 memprintf(err, "out of memory\n");
997 goto err;
998 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100999 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001000 cconf->c.name = strdup(name);
1001 if (!cconf->c.name) {
1002 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001003 goto err;
1004 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001005
William Lallemand41db4602017-10-30 11:15:51 +01001006 /* register a filter to fill the cache buffer */
1007 fconf = calloc(1, sizeof(*fconf));
1008 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001009 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001010 goto err;
1011 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001012 fconf->id = cache_store_flt_id;
1013 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001014 fconf->ops = &cache_ops;
1015 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1016
Christopher Faulet95220e22018-12-07 17:34:39 +01001017 rule->arg.act.p[0] = cconf;
1018 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001019
Christopher Faulet95220e22018-12-07 17:34:39 +01001020 err:
1021 free(cconf);
1022 return 0;
1023}
1024
1025enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1026 struct act_rule *rule, char **err)
1027{
1028 rule->action = ACT_CUSTOM;
1029 rule->action_ptr = http_action_store_cache;
1030
1031 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1032 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001033
Christopher Faulet95220e22018-12-07 17:34:39 +01001034 (*orig_arg)++;
1035 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001036}
1037
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001038/* This produces a sha1 hash of the concatenation of the HTTP method,
1039 * the first occurrence of the Host header followed by the path component
1040 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001041int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001042{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001043 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001044 struct htx *htx = htxbuf(&s->req.buf);
1045 struct htx_sl *sl;
1046 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001047 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001048 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001049 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001050
William Lallemandf528fff2017-11-23 19:43:17 +01001051 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001052 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001053
1054 switch (txn->meth) {
1055 case HTTP_METH_HEAD:
1056 case HTTP_METH_GET:
1057 chunk_memcat(trash, "GET", 3);
1058 break;
1059 default:
1060 return 0;
1061 }
1062
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001063 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001064 uri = htx_sl_req_uri(sl); // whole uri
1065 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001066 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001067
1068 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1069 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1070 * URIs are almost always sent in absolute form with their scheme. In
1071 * this case, the scheme is almost always "https". In order to support
1072 * sharing of cache objects between H1 and H2, we'll hash the absolute
1073 * URI whenever known, or prepend "https://" + the Host header for
1074 * relative URIs. The difference will only appear on absolute HTTP/1
1075 * requests sent to an origin server, which practically is never met in
1076 * the real world so we don't care about the ability to share the same
1077 * key here.URIs are normalized from the absolute URI to an origin form as
1078 * well.
1079 */
1080 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001081 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001082 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1083 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001084 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001085 }
1086
1087 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001088
1089 /* hash everything */
1090 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001091 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001092 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1093
1094 return 1;
1095}
1096
William Lallemand41db4602017-10-30 11:15:51 +01001097enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1098 struct session *sess, struct stream *s, int flags)
1099{
William Lallemand77c11972017-10-31 20:43:01 +01001100
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001101 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001102 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001103 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1104 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001105
Willy Tarreau6905d182019-10-01 17:59:17 +02001106 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1107 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001108 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001109 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001110 txn->flags |= TX_CACHE_IGNORE;
1111
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001112 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001113
Willy Tarreau504455c2017-12-22 17:47:35 +01001114 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1115 return ACT_RET_CONT;
1116
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001117 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001118 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001119
Willy Tarreau504455c2017-12-22 17:47:35 +01001120 if (s->txn->flags & TX_CACHE_IGNORE)
1121 return ACT_RET_CONT;
1122
Willy Tarreaua1214a52018-12-14 14:00:25 +01001123 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001124 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001125 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001126 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001127
William Lallemanda400a3a2017-11-20 19:13:12 +01001128 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001129 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001130 if (res) {
1131 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001132 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1133 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001134 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001135 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001136 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001137 appctx->rule = rule;
1138 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001139 appctx->ctx.cache.next = NULL;
1140 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001141
1142 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001143 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001144 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001145 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001146 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001147 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001148 shctx_lock(shctx_ptr(cache));
1149 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1150 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001151 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001152 }
1153 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001154 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001155 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001156}
1157
1158
1159enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1160 struct act_rule *rule, char **err)
1161{
William Lallemand41db4602017-10-30 11:15:51 +01001162 rule->action = ACT_CUSTOM;
1163 rule->action_ptr = http_action_req_cache_use;
1164
Christopher Faulet95220e22018-12-07 17:34:39 +01001165 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001166 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001167
1168 (*orig_arg)++;
1169 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001170}
1171
1172int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1173{
1174 int err_code = 0;
1175
1176 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1177
1178 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001179 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001180 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001181 err_code |= ERR_ALERT | ERR_ABORT;
1182 goto out;
1183 }
1184
1185 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1186 err_code |= ERR_ABORT;
1187 goto out;
1188 }
1189
1190 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001191 struct cache *cache_config;
1192
William Lallemand41db4602017-10-30 11:15:51 +01001193 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1194 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001195 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001196 err_code |= ERR_ALERT | ERR_ABORT;
1197 goto out;
1198 }
1199
1200 strlcpy2(tmp_cache_config->id, args[1], 33);
1201 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001202 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001203 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001204 err_code |= ERR_WARN;
1205 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001206
1207 list_for_each_entry(cache_config, &caches_config, list) {
1208 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1209 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1210 file, linenum, tmp_cache_config->id);
1211 err_code |= ERR_ALERT | ERR_ABORT;
1212 goto out;
1213 }
1214 }
1215
William Lallemand49b44532017-11-24 18:53:43 +01001216 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001217 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001218 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001219 }
1220 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001221 unsigned long int maxsize;
1222 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001223
1224 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1225 err_code |= ERR_ABORT;
1226 goto out;
1227 }
1228
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001229 maxsize = strtoul(args[1], &err, 10);
1230 if (err == args[1] || *err != '\0') {
1231 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1232 file, linenum, args[1]);
1233 err_code |= ERR_ABORT;
1234 goto out;
1235 }
1236
1237 if (maxsize > (UINT_MAX >> 20)) {
1238 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1239 file, linenum, args[1], UINT_MAX >> 20);
1240 err_code |= ERR_ABORT;
1241 goto out;
1242 }
1243
William Lallemand41db4602017-10-30 11:15:51 +01001244 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001245 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001246 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001247 } else if (strcmp(args[0], "max-age") == 0) {
1248 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1249 err_code |= ERR_ABORT;
1250 goto out;
1251 }
1252
1253 if (!*args[1]) {
1254 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1255 file, linenum, args[0]);
1256 err_code |= ERR_WARN;
1257 }
1258
1259 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001260 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001261 unsigned int maxobjsz;
1262 char *err;
1263
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001264 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1265 err_code |= ERR_ABORT;
1266 goto out;
1267 }
1268
1269 if (!*args[1]) {
1270 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1271 file, linenum, args[0]);
1272 err_code |= ERR_WARN;
1273 }
1274
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001275 maxobjsz = strtoul(args[1], &err, 10);
1276 if (err == args[1] || *err != '\0') {
1277 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1278 file, linenum, args[1]);
1279 err_code |= ERR_ABORT;
1280 goto out;
1281 }
1282 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001283 }
1284 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001285 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001286 err_code |= ERR_ALERT | ERR_FATAL;
1287 goto out;
1288 }
1289out:
1290 return err_code;
1291}
1292
1293/* once the cache section is parsed */
1294
1295int cfg_post_parse_section_cache()
1296{
William Lallemand41db4602017-10-30 11:15:51 +01001297 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001298
1299 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001300
1301 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001302 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001303 err_code |= ERR_FATAL | ERR_ALERT;
1304 goto out;
1305 }
1306
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001307 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001308 /* Default max. file size is a 256th of the cache size. */
1309 tmp_cache_config->maxobjsz =
1310 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001311 }
1312 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1313 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1314 err_code |= ERR_FATAL | ERR_ALERT;
1315 goto out;
1316 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001317
William Lallemandd1d1e222019-08-28 15:22:49 +02001318 /* add to the list of cache to init and reinit tmp_cache_config
1319 * for next cache section, if any.
1320 */
1321 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1322 tmp_cache_config = NULL;
1323 return err_code;
1324 }
1325out:
1326 free(tmp_cache_config);
1327 tmp_cache_config = NULL;
1328 return err_code;
1329
1330}
1331
1332int post_check_cache()
1333{
1334 struct proxy *px;
1335 struct cache *back, *cache_config, *cache;
1336 struct shared_context *shctx;
1337 int ret_shctx;
1338 int err_code = 0;
1339
1340 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1341
1342 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1343 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001344
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001345 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001346 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001347 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001348 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001349 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001350
1351 err_code |= ERR_FATAL | ERR_ALERT;
1352 goto out;
1353 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001354 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001355 /* the cache structure is stored in the shctx and added to the
1356 * caches list, we can remove the entry from the caches_config
1357 * list */
1358 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001359 cache = (struct cache *)shctx->data;
1360 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001361 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001362 LIST_DEL(&cache_config->list);
1363 free(cache_config);
1364
1365 /* Find all references for this cache in the existing filters
1366 * (over all proxies) and reference it in matching filters.
1367 */
1368 for (px = proxies_list; px; px = px->next) {
1369 struct flt_conf *fconf;
1370 struct cache_flt_conf *cconf;
1371
1372 list_for_each_entry(fconf, &px->filter_configs, list) {
1373 if (fconf->id != cache_store_flt_id)
1374 continue;
1375
1376 cconf = fconf->conf;
1377 if (!strcmp(cache->id, cconf->c.name)) {
1378 free(cconf->c.name);
1379 cconf->c.cache = cache;
1380 break;
1381 }
1382 }
1383 }
William Lallemand41db4602017-10-30 11:15:51 +01001384 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001385
William Lallemand41db4602017-10-30 11:15:51 +01001386out:
William Lallemand41db4602017-10-30 11:15:51 +01001387 return err_code;
1388
William Lallemand41db4602017-10-30 11:15:51 +01001389}
1390
William Lallemand41db4602017-10-30 11:15:51 +01001391struct flt_ops cache_ops = {
1392 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001393 .check = cache_store_check,
1394 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001395
Christopher Faulet65554e12020-03-06 14:52:06 +01001396 /* Handle stream init/deinit */
1397 .attach = cache_store_strm_init,
1398 .detach = cache_store_strm_deinit,
1399
William Lallemand4da3f8a2017-10-31 14:33:34 +01001400 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001401 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001402
1403 /* Filter HTTP requests and responses */
1404 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001405 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001406 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001407};
1408
Christopher Faulet99a17a22018-12-11 09:18:27 +01001409
1410
1411static int
1412parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1413 struct flt_conf *fconf, char **err, void *private)
1414{
1415 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001416 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001417 char *name = NULL;
1418 int pos = *cur_arg;
1419
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001420 /* Get the cache filter name. <pos> point on "cache" keyword */
1421 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02001422 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001423 goto error;
1424 }
1425 name = strdup(args[pos + 1]);
1426 if (!name) {
1427 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1428 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001429 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001430 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001431
1432 /* Check if an implicit filter with the same name already exists. If so,
1433 * we remove the implicit filter to use the explicit one. */
1434 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1435 if (f->id != cache_store_flt_id)
1436 continue;
1437
1438 cconf = f->conf;
1439 if (strcmp(name, cconf->c.name)) {
1440 cconf = NULL;
1441 continue;
1442 }
1443
1444 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1445 cconf = NULL;
1446 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1447 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001448 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001449 }
1450
1451 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1452 LIST_DEL(&f->list);
1453 free(f);
1454 free(name);
1455 break;
1456 }
1457
1458 /* No implicit cache filter found, create configuration for the explicit one */
1459 if (!cconf) {
1460 cconf = calloc(1, sizeof(*cconf));
1461 if (!cconf) {
1462 memprintf(err, "%s: out of memory", args[*cur_arg]);
1463 goto error;
1464 }
1465 cconf->c.name = name;
1466 }
1467
1468 cconf->flags = 0;
1469 fconf->id = cache_store_flt_id;
1470 fconf->conf = cconf;
1471 fconf->ops = &cache_ops;
1472
1473 *cur_arg = pos;
1474 return 0;
1475
1476 error:
1477 free(name);
1478 free(cconf);
1479 return -1;
1480}
1481
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001482static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001483{
1484 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1485 return 1;
1486
1487 return 0;
1488}
1489
1490static int cli_io_handler_show_cache(struct appctx *appctx)
1491{
1492 struct cache* cache = appctx->ctx.cli.p0;
1493 struct stream_interface *si = appctx->owner;
1494
William Lallemand1f49a362017-11-21 20:01:26 +01001495 if (cache == NULL) {
1496 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1497 }
1498
1499 list_for_each_entry_from(cache, &caches, list) {
1500 struct eb32_node *node = NULL;
1501 unsigned int next_key;
1502 struct cache_entry *entry;
1503
William Lallemand1f49a362017-11-21 20:01:26 +01001504 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001505 if (!next_key) {
1506 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1507 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001508 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001509 return 0;
1510 }
1511 }
William Lallemand1f49a362017-11-21 20:01:26 +01001512
1513 appctx->ctx.cli.p0 = cache;
1514
1515 while (1) {
1516
1517 shctx_lock(shctx_ptr(cache));
1518 node = eb32_lookup_ge(&cache->entries, next_key);
1519 if (!node) {
1520 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001521 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001522 break;
1523 }
1524
1525 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001526 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 +01001527
1528 next_key = node->key + 1;
1529 appctx->ctx.cli.i0 = next_key;
1530
1531 shctx_unlock(shctx_ptr(cache));
1532
1533 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001534 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001535 return 0;
1536 }
1537 }
1538
1539 }
1540
1541 return 1;
1542
1543}
1544
Christopher Faulet99a17a22018-12-11 09:18:27 +01001545/* Declare the filter parser for "cache" keyword */
1546static struct flt_kw_list filter_kws = { "CACHE", { }, {
1547 { "cache", parse_cache_flt, NULL },
1548 { NULL, NULL, NULL },
1549 }
1550};
1551
1552INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1553
William Lallemand1f49a362017-11-21 20:01:26 +01001554static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001555 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1556 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001557}};
1558
Willy Tarreau0108d902018-11-25 19:14:37 +01001559INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001560
William Lallemand41db4602017-10-30 11:15:51 +01001561static struct action_kw_list http_res_actions = {
1562 .kw = {
1563 { "cache-store", parse_cache_store },
1564 { NULL, NULL }
1565 }
1566};
1567
Willy Tarreau0108d902018-11-25 19:14:37 +01001568INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1569
William Lallemand41db4602017-10-30 11:15:51 +01001570static struct action_kw_list http_req_actions = {
1571 .kw = {
1572 { "cache-use", parse_cache_use },
1573 { NULL, NULL }
1574 }
1575};
1576
Willy Tarreau0108d902018-11-25 19:14:37 +01001577INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1578
Willy Tarreau2231b632019-03-29 18:26:52 +01001579struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001580 .obj_type = OBJ_TYPE_APPLET,
1581 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001582 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001583 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001584};
1585
Willy Tarreaue6552512018-11-26 11:33:13 +01001586/* config parsers for this section */
1587REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001588REGISTER_POST_CHECK(post_check_cache);