blob: 6fc1c1487cdd381d513b268a4531850f994f9543 [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) {
929 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
930 si_rx_room_blk(si);
931 goto out;
932 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100933 appctx->st0 = HTX_CACHE_END;
934 }
935
936 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100937 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100938 res->flags |= CF_READ_NULL;
939 si_shutr(si);
940 }
941
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100942 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200943 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100944 if (total)
945 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100946 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100947
948 /* eat the whole request */
949 if (co_data(req)) {
950 req_htx = htx_from_buf(&req->buf);
951 co_htx_skip(req, req_htx, co_data(req));
952 htx_to_buf(req_htx, &req->buf);
953 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100954 return;
955
956 error:
957 /* Sent and HTTP error 500 */
958 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200959 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100960 res->buf.data = b_data(errmsg);
961 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
962 res_htx = htx_from_buf(&res->buf);
963
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200964 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100965 appctx->st0 = HTX_CACHE_END;
966 goto end;
967}
968
969
Christopher Faulet95220e22018-12-07 17:34:39 +0100970static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100971{
972 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100973 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100974
Christopher Faulet95220e22018-12-07 17:34:39 +0100975 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100976 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100977 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100978 }
979
980 /* check if a cache filter was already registered with this cache
981 * name, if that's the case, must use it. */
982 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100983 if (fconf->id == cache_store_flt_id) {
984 cconf = fconf->conf;
985 if (cconf && !strcmp((char *)cconf->c.name, name)) {
986 rule->arg.act.p[0] = cconf;
987 return 1;
988 }
William Lallemand41db4602017-10-30 11:15:51 +0100989 }
990 }
991
Christopher Faulet95220e22018-12-07 17:34:39 +0100992 /* Create the filter cache config */
993 cconf = calloc(1, sizeof(*cconf));
994 if (!cconf) {
995 memprintf(err, "out of memory\n");
996 goto err;
997 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100998 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100999 cconf->c.name = strdup(name);
1000 if (!cconf->c.name) {
1001 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001002 goto err;
1003 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001004
William Lallemand41db4602017-10-30 11:15:51 +01001005 /* register a filter to fill the cache buffer */
1006 fconf = calloc(1, sizeof(*fconf));
1007 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001008 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001009 goto err;
1010 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001011 fconf->id = cache_store_flt_id;
1012 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001013 fconf->ops = &cache_ops;
1014 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1015
Christopher Faulet95220e22018-12-07 17:34:39 +01001016 rule->arg.act.p[0] = cconf;
1017 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001018
Christopher Faulet95220e22018-12-07 17:34:39 +01001019 err:
1020 free(cconf);
1021 return 0;
1022}
1023
1024enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1025 struct act_rule *rule, char **err)
1026{
1027 rule->action = ACT_CUSTOM;
1028 rule->action_ptr = http_action_store_cache;
1029
1030 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1031 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001032
Christopher Faulet95220e22018-12-07 17:34:39 +01001033 (*orig_arg)++;
1034 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001035}
1036
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001037/* This produces a sha1 hash of the concatenation of the HTTP method,
1038 * the first occurrence of the Host header followed by the path component
1039 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001040int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001041{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001042 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001043 struct htx *htx = htxbuf(&s->req.buf);
1044 struct htx_sl *sl;
1045 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001046 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001047 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001048 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001049
William Lallemandf528fff2017-11-23 19:43:17 +01001050 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001051 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001052
1053 switch (txn->meth) {
1054 case HTTP_METH_HEAD:
1055 case HTTP_METH_GET:
1056 chunk_memcat(trash, "GET", 3);
1057 break;
1058 default:
1059 return 0;
1060 }
1061
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001062 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001063 uri = htx_sl_req_uri(sl); // whole uri
1064 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001065 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001066
1067 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1068 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1069 * URIs are almost always sent in absolute form with their scheme. In
1070 * this case, the scheme is almost always "https". In order to support
1071 * sharing of cache objects between H1 and H2, we'll hash the absolute
1072 * URI whenever known, or prepend "https://" + the Host header for
1073 * relative URIs. The difference will only appear on absolute HTTP/1
1074 * requests sent to an origin server, which practically is never met in
1075 * the real world so we don't care about the ability to share the same
1076 * key here.URIs are normalized from the absolute URI to an origin form as
1077 * well.
1078 */
1079 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001080 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001081 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1082 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001083 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001084 }
1085
1086 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001087
1088 /* hash everything */
1089 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001090 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001091 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1092
1093 return 1;
1094}
1095
William Lallemand41db4602017-10-30 11:15:51 +01001096enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1097 struct session *sess, struct stream *s, int flags)
1098{
William Lallemand77c11972017-10-31 20:43:01 +01001099
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001100 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001101 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001102 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1103 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001104
Willy Tarreau6905d182019-10-01 17:59:17 +02001105 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1106 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001107 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001108 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001109 txn->flags |= TX_CACHE_IGNORE;
1110
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001111 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001112
Willy Tarreau504455c2017-12-22 17:47:35 +01001113 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1114 return ACT_RET_CONT;
1115
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001116 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001117 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001118
Willy Tarreau504455c2017-12-22 17:47:35 +01001119 if (s->txn->flags & TX_CACHE_IGNORE)
1120 return ACT_RET_CONT;
1121
Willy Tarreaua1214a52018-12-14 14:00:25 +01001122 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001123 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001124 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001125 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001126
William Lallemanda400a3a2017-11-20 19:13:12 +01001127 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001128 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001129 if (res) {
1130 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001131 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1132 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001133 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001134 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001135 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001136 appctx->rule = rule;
1137 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001138 appctx->ctx.cache.next = NULL;
1139 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001140
1141 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001142 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001143 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001144 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001145 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001146 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001147 shctx_lock(shctx_ptr(cache));
1148 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1149 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001150 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001151 }
1152 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001153 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001154 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001155}
1156
1157
1158enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1159 struct act_rule *rule, char **err)
1160{
William Lallemand41db4602017-10-30 11:15:51 +01001161 rule->action = ACT_CUSTOM;
1162 rule->action_ptr = http_action_req_cache_use;
1163
Christopher Faulet95220e22018-12-07 17:34:39 +01001164 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001165 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001166
1167 (*orig_arg)++;
1168 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001169}
1170
1171int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1172{
1173 int err_code = 0;
1174
1175 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1176
1177 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001178 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1179 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001180 err_code |= ERR_ALERT | ERR_ABORT;
1181 goto out;
1182 }
1183
1184 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1185 err_code |= ERR_ABORT;
1186 goto out;
1187 }
1188
1189 if (tmp_cache_config == NULL) {
1190 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1191 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001192 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001193 err_code |= ERR_ALERT | ERR_ABORT;
1194 goto out;
1195 }
1196
1197 strlcpy2(tmp_cache_config->id, args[1], 33);
1198 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1200 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001201 err_code |= ERR_WARN;
1202 }
William Lallemand49b44532017-11-24 18:53:43 +01001203 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001204 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001205 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001206 }
1207 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001208 unsigned long int maxsize;
1209 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001210
1211 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1212 err_code |= ERR_ABORT;
1213 goto out;
1214 }
1215
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001216 maxsize = strtoul(args[1], &err, 10);
1217 if (err == args[1] || *err != '\0') {
1218 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1219 file, linenum, args[1]);
1220 err_code |= ERR_ABORT;
1221 goto out;
1222 }
1223
1224 if (maxsize > (UINT_MAX >> 20)) {
1225 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1226 file, linenum, args[1], UINT_MAX >> 20);
1227 err_code |= ERR_ABORT;
1228 goto out;
1229 }
1230
William Lallemand41db4602017-10-30 11:15:51 +01001231 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001232 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001233 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001234 } else if (strcmp(args[0], "max-age") == 0) {
1235 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1236 err_code |= ERR_ABORT;
1237 goto out;
1238 }
1239
1240 if (!*args[1]) {
1241 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1242 file, linenum, args[0]);
1243 err_code |= ERR_WARN;
1244 }
1245
1246 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001247 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001248 unsigned int maxobjsz;
1249 char *err;
1250
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001251 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1252 err_code |= ERR_ABORT;
1253 goto out;
1254 }
1255
1256 if (!*args[1]) {
1257 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1258 file, linenum, args[0]);
1259 err_code |= ERR_WARN;
1260 }
1261
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001262 maxobjsz = strtoul(args[1], &err, 10);
1263 if (err == args[1] || *err != '\0') {
1264 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1265 file, linenum, args[1]);
1266 err_code |= ERR_ABORT;
1267 goto out;
1268 }
1269 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001270 }
1271 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001272 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001273 err_code |= ERR_ALERT | ERR_FATAL;
1274 goto out;
1275 }
1276out:
1277 return err_code;
1278}
1279
1280/* once the cache section is parsed */
1281
1282int cfg_post_parse_section_cache()
1283{
William Lallemand41db4602017-10-30 11:15:51 +01001284 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001285
1286 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001287
1288 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001289 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001290 err_code |= ERR_FATAL | ERR_ALERT;
1291 goto out;
1292 }
1293
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001294 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001295 /* Default max. file size is a 256th of the cache size. */
1296 tmp_cache_config->maxobjsz =
1297 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001298 }
1299 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1300 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1301 err_code |= ERR_FATAL | ERR_ALERT;
1302 goto out;
1303 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001304
William Lallemandd1d1e222019-08-28 15:22:49 +02001305 /* add to the list of cache to init and reinit tmp_cache_config
1306 * for next cache section, if any.
1307 */
1308 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1309 tmp_cache_config = NULL;
1310 return err_code;
1311 }
1312out:
1313 free(tmp_cache_config);
1314 tmp_cache_config = NULL;
1315 return err_code;
1316
1317}
1318
1319int post_check_cache()
1320{
1321 struct proxy *px;
1322 struct cache *back, *cache_config, *cache;
1323 struct shared_context *shctx;
1324 int ret_shctx;
1325 int err_code = 0;
1326
1327 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1328
1329 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1330 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001331
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001332 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001333 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001334 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001335 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001336 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001337
1338 err_code |= ERR_FATAL | ERR_ALERT;
1339 goto out;
1340 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001341 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001342 /* the cache structure is stored in the shctx and added to the
1343 * caches list, we can remove the entry from the caches_config
1344 * list */
1345 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001346 cache = (struct cache *)shctx->data;
1347 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001348 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001349 LIST_DEL(&cache_config->list);
1350 free(cache_config);
1351
1352 /* Find all references for this cache in the existing filters
1353 * (over all proxies) and reference it in matching filters.
1354 */
1355 for (px = proxies_list; px; px = px->next) {
1356 struct flt_conf *fconf;
1357 struct cache_flt_conf *cconf;
1358
1359 list_for_each_entry(fconf, &px->filter_configs, list) {
1360 if (fconf->id != cache_store_flt_id)
1361 continue;
1362
1363 cconf = fconf->conf;
1364 if (!strcmp(cache->id, cconf->c.name)) {
1365 free(cconf->c.name);
1366 cconf->c.cache = cache;
1367 break;
1368 }
1369 }
1370 }
William Lallemand41db4602017-10-30 11:15:51 +01001371 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001372
William Lallemand41db4602017-10-30 11:15:51 +01001373out:
William Lallemand41db4602017-10-30 11:15:51 +01001374 return err_code;
1375
William Lallemand41db4602017-10-30 11:15:51 +01001376}
1377
William Lallemand41db4602017-10-30 11:15:51 +01001378struct flt_ops cache_ops = {
1379 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001380 .check = cache_store_check,
1381 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001382
Christopher Faulet65554e12020-03-06 14:52:06 +01001383 /* Handle stream init/deinit */
1384 .attach = cache_store_strm_init,
1385 .detach = cache_store_strm_deinit,
1386
William Lallemand4da3f8a2017-10-31 14:33:34 +01001387 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001388 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001389
1390 /* Filter HTTP requests and responses */
1391 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001392 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001393 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001394};
1395
Christopher Faulet99a17a22018-12-11 09:18:27 +01001396
1397
1398static int
1399parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1400 struct flt_conf *fconf, char **err, void *private)
1401{
1402 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001403 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001404 char *name = NULL;
1405 int pos = *cur_arg;
1406
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001407 /* Get the cache filter name. <pos> point on "cache" keyword */
1408 if (!*args[pos + 1]) {
1409 memprintf(err, "%s : expects an <id> argument", args[pos]);
1410 goto error;
1411 }
1412 name = strdup(args[pos + 1]);
1413 if (!name) {
1414 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1415 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001416 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001417 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001418
1419 /* Check if an implicit filter with the same name already exists. If so,
1420 * we remove the implicit filter to use the explicit one. */
1421 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1422 if (f->id != cache_store_flt_id)
1423 continue;
1424
1425 cconf = f->conf;
1426 if (strcmp(name, cconf->c.name)) {
1427 cconf = NULL;
1428 continue;
1429 }
1430
1431 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1432 cconf = NULL;
1433 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1434 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001435 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001436 }
1437
1438 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1439 LIST_DEL(&f->list);
1440 free(f);
1441 free(name);
1442 break;
1443 }
1444
1445 /* No implicit cache filter found, create configuration for the explicit one */
1446 if (!cconf) {
1447 cconf = calloc(1, sizeof(*cconf));
1448 if (!cconf) {
1449 memprintf(err, "%s: out of memory", args[*cur_arg]);
1450 goto error;
1451 }
1452 cconf->c.name = name;
1453 }
1454
1455 cconf->flags = 0;
1456 fconf->id = cache_store_flt_id;
1457 fconf->conf = cconf;
1458 fconf->ops = &cache_ops;
1459
1460 *cur_arg = pos;
1461 return 0;
1462
1463 error:
1464 free(name);
1465 free(cconf);
1466 return -1;
1467}
1468
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001469static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001470{
1471 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1472 return 1;
1473
1474 return 0;
1475}
1476
1477static int cli_io_handler_show_cache(struct appctx *appctx)
1478{
1479 struct cache* cache = appctx->ctx.cli.p0;
1480 struct stream_interface *si = appctx->owner;
1481
William Lallemand1f49a362017-11-21 20:01:26 +01001482 if (cache == NULL) {
1483 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1484 }
1485
1486 list_for_each_entry_from(cache, &caches, list) {
1487 struct eb32_node *node = NULL;
1488 unsigned int next_key;
1489 struct cache_entry *entry;
1490
William Lallemand1f49a362017-11-21 20:01:26 +01001491 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001492 if (!next_key) {
1493 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1494 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001495 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001496 return 0;
1497 }
1498 }
William Lallemand1f49a362017-11-21 20:01:26 +01001499
1500 appctx->ctx.cli.p0 = cache;
1501
1502 while (1) {
1503
1504 shctx_lock(shctx_ptr(cache));
1505 node = eb32_lookup_ge(&cache->entries, next_key);
1506 if (!node) {
1507 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001508 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001509 break;
1510 }
1511
1512 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001513 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 +01001514
1515 next_key = node->key + 1;
1516 appctx->ctx.cli.i0 = next_key;
1517
1518 shctx_unlock(shctx_ptr(cache));
1519
1520 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001521 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001522 return 0;
1523 }
1524 }
1525
1526 }
1527
1528 return 1;
1529
1530}
1531
Christopher Faulet99a17a22018-12-11 09:18:27 +01001532/* Declare the filter parser for "cache" keyword */
1533static struct flt_kw_list filter_kws = { "CACHE", { }, {
1534 { "cache", parse_cache_flt, NULL },
1535 { NULL, NULL, NULL },
1536 }
1537};
1538
1539INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1540
William Lallemand1f49a362017-11-21 20:01:26 +01001541static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001542 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1543 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001544}};
1545
Willy Tarreau0108d902018-11-25 19:14:37 +01001546INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001547
William Lallemand41db4602017-10-30 11:15:51 +01001548static struct action_kw_list http_res_actions = {
1549 .kw = {
1550 { "cache-store", parse_cache_store },
1551 { NULL, NULL }
1552 }
1553};
1554
Willy Tarreau0108d902018-11-25 19:14:37 +01001555INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1556
William Lallemand41db4602017-10-30 11:15:51 +01001557static struct action_kw_list http_req_actions = {
1558 .kw = {
1559 { "cache-use", parse_cache_use },
1560 { NULL, NULL }
1561 }
1562};
1563
Willy Tarreau0108d902018-11-25 19:14:37 +01001564INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1565
Willy Tarreau2231b632019-03-29 18:26:52 +01001566struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001567 .obj_type = OBJ_TYPE_APPLET,
1568 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001569 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001570 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001571};
1572
Willy Tarreaue6552512018-11-26 11:33:13 +01001573/* config parsers for this section */
1574REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001575REGISTER_POST_CHECK(post_check_cache);