blob: ba96ede658aae641be16db4602f92655b79d852d [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
2 * Cache management
3 *
4 * Copyright 2017 HAProxy Technologies
5 * William Lallemand <wlallemand@haproxy.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
Willy Tarreau122eba92020-06-04 10:15:32 +020013#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020015#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020016#include <haproxy/cli.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020017#include <haproxy/filters.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020018#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020019#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020020#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020021#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020022#include <haproxy/proxy.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020023#include <haproxy/shctx.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020024#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020025#include <haproxy/stream_interface.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020026#include <import/eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010027#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010028
William Lallemand41db4602017-10-30 11:15:51 +010029
30#include <common/cfgparse.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/hash.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020032#include <haproxy/htx.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020033#include <haproxy/net_helper.h>
William Lallemand41db4602017-10-30 11:15:51 +010034
Christopher Faulet27d93c32018-12-15 22:32:02 +010035#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010036 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010037
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010038const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010039
Willy Tarreau2231b632019-03-29 18:26:52 +010040extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010041
42struct flt_ops cache_ops;
43
44struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010045 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010046 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010047 unsigned int maxage; /* max-age */
48 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020049 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010050 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010051};
52
Christopher Faulet95220e22018-12-07 17:34:39 +010053/* cache config for filters */
54struct cache_flt_conf {
55 union {
56 struct cache *cache; /* cache used by the filter */
57 char *name; /* cache name used during conf parsing */
58 } c;
59 unsigned int flags; /* CACHE_FLT_F_* */
60};
61
William Lallemand41db4602017-10-30 11:15:51 +010062/*
63 * cache ctx for filters
64 */
65struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010066 struct shared_block *first_block;
67};
68
69struct cache_entry {
70 unsigned int latest_validation; /* latest validation date */
71 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020072 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010073
William Lallemand41db4602017-10-30 11:15:51 +010074 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010075 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010076 unsigned char data[0];
77};
78
79#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010080#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010081
82static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020083static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010084static struct cache *tmp_cache_config = NULL;
85
Willy Tarreau8ceae722018-11-26 11:58:30 +010086DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
87
William Lallemandf528fff2017-11-23 19:43:17 +010088struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010089{
90 struct eb32_node *node;
91 struct cache_entry *entry;
92
Willy Tarreau8b507582020-02-25 09:35:07 +010093 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010094 if (!node)
95 return NULL;
96
97 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010098
99 /* if that's not the right node */
100 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
101 return NULL;
102
William Lallemand08727662017-11-21 20:01:27 +0100103 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100104 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100105 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100106 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100107 entry->eb.key = 0;
108 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100109 return NULL;
110
111}
112
113static inline struct shared_context *shctx_ptr(struct cache *cache)
114{
115 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
116}
117
William Lallemand77c11972017-10-31 20:43:01 +0100118static inline struct shared_block *block_ptr(struct cache_entry *entry)
119{
120 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
121}
122
123
124
William Lallemand41db4602017-10-30 11:15:51 +0100125static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100126cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100127{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100128 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100129 return 0;
130}
131
Christopher Faulet95220e22018-12-07 17:34:39 +0100132static void
133cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
134{
135 struct cache_flt_conf *cconf = fconf->conf;
136
137 free(cconf);
138}
139
William Lallemand4da3f8a2017-10-31 14:33:34 +0100140static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100141cache_store_check(struct proxy *px, struct flt_conf *fconf)
142{
143 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100144 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100145 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100146 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100147
William Lallemandd1d1e222019-08-28 15:22:49 +0200148 /* Find the cache corresponding to the name in the filter config. The
149 * cache will not be referenced now in the filter config because it is
150 * not fully allocated. This step will be performed during the cache
151 * post_check.
152 */
153 list_for_each_entry(cache, &caches_config, list) {
154 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100155 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100156 }
157
158 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
159 proxy_type_str(px), px->id, (char *)cconf->c.name);
160 return 1;
161
162 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100163 /* Here <cache> points on the cache the filter must use and <cconf>
164 * points on the cache filter configuration. */
165
166 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100167 * enabled and if it is after the cache. When the compression is before
168 * the cache, an error is returned. Also check if the cache filter must
169 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100170 list_for_each_entry(f, &px->filter_configs, list) {
171 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100172 /* The compression filter must be evaluated after the cache. */
173 if (comp) {
174 ha_alert("config: %s '%s': unable to enable the compression filter before "
175 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
176 return 1;
177 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100178 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200179 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100180 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200181 else if (f->id == fcgi_flt_id)
182 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100183 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
184 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200185 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100186 * declaration is required. */
187 ha_alert("config: %s '%s': require an explicit filter declaration "
188 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
189 return 1;
190 }
191
Christopher Fauletafd819c2018-12-11 08:57:45 +0100192 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100193 return 0;
194}
195
196static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100197cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100198{
Christopher Faulet65554e12020-03-06 14:52:06 +0100199 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100200
Christopher Faulet65554e12020-03-06 14:52:06 +0100201 st = pool_alloc_dirty(pool_head_cache_st);
202 if (st == NULL)
203 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100204
Christopher Faulet65554e12020-03-06 14:52:06 +0100205 st->first_block = NULL;
206 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100207
Christopher Faulet65554e12020-03-06 14:52:06 +0100208 /* Register post-analyzer on AN_RES_WAIT_HTTP */
209 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100210 return 1;
211}
212
Christopher Faulet65554e12020-03-06 14:52:06 +0100213static void
214cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100215{
216 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100217 struct cache_flt_conf *cconf = FLT_CONF(filter);
218 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100219 struct shared_context *shctx = shctx_ptr(cache);
220
William Lallemand49dc0482017-11-24 14:33:54 +0100221 /* Everything should be released in the http_end filter, but we need to do it
222 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100223 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100224 shctx_lock(shctx);
225 shctx_row_dec_hot(shctx, st->first_block);
226 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100227 }
228 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100229 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100230 filter->ctx = NULL;
231 }
William Lallemand49dc0482017-11-24 14:33:54 +0100232}
233
Christopher Faulet839791a2019-01-07 16:12:07 +0100234static int
235cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
236 unsigned an_bit)
237{
238 struct http_txn *txn = s->txn;
239 struct http_msg *msg = &txn->rsp;
240 struct cache_st *st = filter->ctx;
241
242 if (an_bit != AN_RES_WAIT_HTTP)
243 goto end;
244
245 /* Here we need to check if any compression filter precedes the cache
246 * filter. This is only possible when the compression is configured in
247 * the frontend while the cache filter is configured on the
248 * backend. This case cannot be detected during HAProxy startup. So in
249 * such cases, the cache is disabled.
250 */
251 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
252 pool_free(pool_head_cache_st, st);
253 filter->ctx = NULL;
254 }
255
256 end:
257 return 1;
258}
William Lallemand49dc0482017-11-24 14:33:54 +0100259
260static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100261cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
262{
263 struct cache_st *st = filter->ctx;
264
William Lallemand4da3f8a2017-10-31 14:33:34 +0100265 if (!(msg->chn->flags & CF_ISRESP) || !st)
266 return 1;
267
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200268 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100269 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100270 return 1;
271}
272
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200273static inline void disable_cache_entry(struct cache_st *st,
274 struct filter *filter, struct shared_context *shctx)
275{
276 struct cache_entry *object;
277
278 object = (struct cache_entry *)st->first_block->data;
279 filter->ctx = NULL; /* disable cache */
280 shctx_lock(shctx);
281 shctx_row_dec_hot(shctx, st->first_block);
282 object->eb.key = 0;
283 shctx_unlock(shctx);
284 pool_free(pool_head_cache_st, st);
285}
286
William Lallemand4da3f8a2017-10-31 14:33:34 +0100287static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100288cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
289 unsigned int offset, unsigned int len)
290{
Christopher Faulet95220e22018-12-07 17:34:39 +0100291 struct cache_flt_conf *cconf = FLT_CONF(filter);
292 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100293 struct cache_st *st = filter->ctx;
294 struct htx *htx = htxbuf(&msg->chn->buf);
295 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200296 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100297 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200298 unsigned int orig_len, to_forward;
299 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100300
301 if (!len)
302 return len;
303
304 if (!st->first_block) {
305 unregister_data_filter(s, msg->chn, filter);
306 return len;
307 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100308
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200309 chunk_reset(&trash);
310 orig_len = len;
311 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100312
313 htxret = htx_find_offset(htx, offset);
314 blk = htxret.blk;
315 offset = htxret.ret;
316 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100317 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200318 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100319 struct ist v;
320
321 switch (type) {
322 case HTX_BLK_UNUSED:
323 break;
324
325 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100326 v = htx_get_blk_value(htx, blk);
327 v.ptr += offset;
328 v.len -= offset;
329 if (v.len > len)
330 v.len = len;
331
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200332 info = (type << 28) + v.len;
333 chunk_memcat(&trash, (char *)&info, sizeof(info));
334 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100335 to_forward += v.len;
336 len -= v.len;
337 break;
338
339 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200340 /* Here offset must always be 0 because only
341 * DATA blocks can be partially transferred. */
342 if (offset)
343 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100344 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200345 goto end;
346
347 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
348 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100349 to_forward += sz;
350 len -= sz;
351 break;
352 }
353
354 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100355 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200356
357 end:
358 shctx_lock(shctx);
359 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
360 if (!fb) {
361 shctx_unlock(shctx);
362 goto no_cache;
363 }
364 shctx_unlock(shctx);
365
366 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
367 (unsigned char *)b_head(&trash), b_data(&trash));
368 if (ret < 0)
369 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100370
371 return to_forward;
372
373 no_cache:
374 disable_cache_entry(st, filter, shctx);
375 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200376 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100377}
378
379static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100380cache_store_http_end(struct stream *s, struct filter *filter,
381 struct http_msg *msg)
382{
383 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100384 struct cache_flt_conf *cconf = FLT_CONF(filter);
385 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100386 struct shared_context *shctx = shctx_ptr(cache);
387 struct cache_entry *object;
388
389 if (!(msg->chn->flags & CF_ISRESP))
390 return 1;
391
392 if (st && st->first_block) {
393
394 object = (struct cache_entry *)st->first_block->data;
395
396 /* does not need to test if the insertion worked, if it
397 * doesn't, the blocks will be reused anyway */
398
399 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100400 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
401 object->eb.key = 0;
402 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100403 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100404 shctx_row_dec_hot(shctx, st->first_block);
405 shctx_unlock(shctx);
406
407 }
408 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100409 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100410 filter->ctx = NULL;
411 }
412
413 return 1;
414}
415
416 /*
417 * This intends to be used when checking HTTP headers for some
418 * word=value directive. Return a pointer to the first character of value, if
419 * the word was not found or if there wasn't any value assigned ot it return NULL
420 */
421char *directive_value(const char *sample, int slen, const char *word, int wlen)
422{
423 int st = 0;
424
425 if (slen < wlen)
426 return 0;
427
428 while (wlen) {
429 char c = *sample ^ *word;
430 if (c && c != ('A' ^ 'a'))
431 return NULL;
432 sample++;
433 word++;
434 slen--;
435 wlen--;
436 }
437
438 while (slen) {
439 if (st == 0) {
440 if (*sample != '=')
441 return NULL;
442 sample++;
443 slen--;
444 st = 1;
445 continue;
446 } else {
447 return (char *)sample;
448 }
449 }
450
451 return NULL;
452}
453
454/*
455 * Return the maxage in seconds of an HTTP response.
456 * Compute the maxage using either:
457 * - the assigned max-age of the cache
458 * - the s-maxage directive
459 * - the max-age directive
460 * - (Expires - Data) headers
461 * - the default-max-age of the cache
462 *
463 */
William Lallemand49b44532017-11-24 18:53:43 +0100464int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100465{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200466 struct htx *htx = htxbuf(&s->res.buf);
467 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100468 int smaxage = -1;
469 int maxage = -1;
470
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200471 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
472 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100473
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200474 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
475 if (value) {
476 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100477
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200478 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
479 chunk_strncat(chk, "", 1);
480 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100481 }
482
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200483 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
484 if (value) {
485 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200486
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200487 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
488 chunk_strncat(chk, "", 1);
489 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100490 }
491 }
492
493 /* TODO: Expires - Data */
494
495
496 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100497 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100498
499 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100500 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100501
William Lallemand49b44532017-11-24 18:53:43 +0100502 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100503
504}
505
506
William Lallemanda400a3a2017-11-20 19:13:12 +0100507static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
508{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200509 struct cache_entry *object = (struct cache_entry *)block->data;
510
511 if (first == block && object->eb.key)
512 eb32_delete(&object->eb);
513 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100514}
515
William Lallemand41db4602017-10-30 11:15:51 +0100516/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500517 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100518 * register a filter to store the data
519 */
520enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200521 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100522{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200523 unsigned int age;
524 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100525 struct http_txn *txn = s->txn;
526 struct http_msg *msg = &txn->rsp;
527 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100528 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100529 struct cache_flt_conf *cconf = rule->arg.act.p[0];
530 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100531 struct cache_st *cache_ctx = NULL;
532 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100533 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200534 struct htx *htx;
535 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200536 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200537 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100538
William Lallemand4da3f8a2017-10-31 14:33:34 +0100539 /* Don't cache if the response came from a cache */
540 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
541 s->target == &http_cache_applet.obj_type) {
542 goto out;
543 }
544
545 /* cache only HTTP/1.1 */
546 if (!(txn->req.flags & HTTP_MSGF_VER_11))
547 goto out;
548
Willy Tarreau6905d182019-10-01 17:59:17 +0200549 /* cache only GET method */
550 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100551 goto out;
552
Willy Tarreauc9036c02019-01-11 19:38:25 +0100553 /* cache key was not computed */
554 if (!key)
555 goto out;
556
William Lallemand4da3f8a2017-10-31 14:33:34 +0100557 /* cache only 200 status code */
558 if (txn->status != 200)
559 goto out;
560
Christopher Faulet839791a2019-01-07 16:12:07 +0100561 /* Find the corresponding filter instance for the current stream */
562 list_for_each_entry(filter, &s->strm_flt.filters, list) {
563 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
564 /* No filter ctx, don't cache anything */
565 if (!filter->ctx)
566 goto out;
567 cache_ctx = filter->ctx;
568 break;
569 }
570 }
571
572 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200573 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100574
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200575 /* Do not cache too big objects. */
576 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
577 htx->data + htx->extra > shctx->max_obj_size)
578 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100579
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200580 /* Does not manage Vary at the moment. We will need a secondary key later for that */
581 ctx.blk = NULL;
582 if (http_find_header(htx, ist("Vary"), &ctx, 0))
583 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100584
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200585 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100586
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200587 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
588 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100589
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200590 age = 0;
591 ctx.blk = NULL;
592 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
593 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
594 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
595 hdr_age = CACHE_ENTRY_MAX_AGE;
596 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100597 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200598 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100599 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100600
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200601 chunk_reset(&trash);
602 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
603 struct htx_blk *blk = htx_get_blk(htx, pos);
604 enum htx_blk_type type = htx_get_blk_type(blk);
605 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100606
Christopher Fauletb0667472019-09-03 22:22:12 +0200607 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200608 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
609 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
610 if (type == HTX_BLK_EOH)
611 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200612 }
613
Christopher Fauletb0667472019-09-03 22:22:12 +0200614 /* Do not cache objects if the headers are too big. */
615 if (hdrs_len > htx->size - global.tune.maxrewrite)
616 goto out;
617
William Lallemand4da3f8a2017-10-31 14:33:34 +0100618 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200619 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100620 if (!first) {
621 shctx_unlock(shctx);
622 goto out;
623 }
624 shctx_unlock(shctx);
625
Willy Tarreau1093a452018-04-06 19:02:25 +0200626 /* the received memory is not initialized, we need at least to mark
627 * the object as not indexed yet.
628 */
629 object = (struct cache_entry *)first->data;
630 object->eb.node.leaf_p = NULL;
631 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200632 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200633
William Lallemand4da3f8a2017-10-31 14:33:34 +0100634 /* reserve space for the cache_entry structure */
635 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200636 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100637 /* cache the headers in a http action because it allows to chose what
638 * to cache, for example you might want to cache a response before
639 * modifying some HTTP headers, or on the contrary after modifying
640 * those headers.
641 */
642
643 /* does not need to be locked because it's in the "hot" list,
644 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200645 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
646 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100647
648 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100649 if (cache_ctx) {
650 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100651
Willy Tarreauc9036c02019-01-11 19:38:25 +0100652 object->eb.key = key;
653
Christopher Faulet839791a2019-01-07 16:12:07 +0100654 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
655 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100656
Christopher Faulet839791a2019-01-07 16:12:07 +0100657 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100658
Christopher Faulet839791a2019-01-07 16:12:07 +0100659 old = entry_exist(cconf->c.cache, txn->cache_hash);
660 if (old) {
661 eb32_delete(&old->eb);
662 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100663 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100664 shctx_unlock(shctx);
665
666 /* store latest value and expiration time */
667 object->latest_validation = now.tv_sec;
668 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
669 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100670 }
671
672out:
673 /* if does not cache */
674 if (first) {
675 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100676 first->len = 0;
677 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100678 shctx_row_dec_hot(shctx, first);
679 shctx_unlock(shctx);
680 }
681
William Lallemand41db4602017-10-30 11:15:51 +0100682 return ACT_RET_CONT;
683}
684
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100685#define HTX_CACHE_INIT 0 /* Initial state. */
686#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
687#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200688#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
689#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100690
William Lallemandecb73b12017-11-24 14:33:55 +0100691static void http_cache_applet_release(struct appctx *appctx)
692{
Christopher Faulet95220e22018-12-07 17:34:39 +0100693 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100694 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100695 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100696 struct shared_block *first = block_ptr(cache_ptr);
697
698 shctx_lock(shctx_ptr(cache));
699 shctx_row_dec_hot(shctx_ptr(cache), first);
700 shctx_unlock(shctx_ptr(cache));
701}
702
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200703
704static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
705 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100706{
Christopher Faulet95220e22018-12-07 17:34:39 +0100707 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
708 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200709 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200710 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200711 unsigned int max, total;
712 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100713
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200714 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
715 if (!max)
716 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200717 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200718 ? (info & 0xff) + ((info >> 8) & 0xfffff)
719 : info & 0xfffffff);
720 if (blksz > max)
721 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100722
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200723 blk = htx_add_blk(htx, type, blksz);
724 if (!blk)
725 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100726
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200727 blk->info = info;
728 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200729 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200730 while (blksz) {
731 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200732 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200733 offset += max;
734 blksz -= max;
735 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200736 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200737 if (blksz || offset == shctx->block_size) {
738 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
739 offset = 0;
740 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100741 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200742 appctx->ctx.cache.offset = offset;
743 appctx->ctx.cache.next = shblk;
744 appctx->ctx.cache.sent += total;
745 return total;
746}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100747
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200748static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
749 uint32_t info, struct shared_block *shblk, unsigned int offset)
750{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100751
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200752 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
753 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
754 unsigned int max, total, rem_data;
755 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100756
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200757 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
758 if (!max)
759 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100760
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200761 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200762 if (appctx->ctx.cache.rem_data) {
763 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200764 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200765 }
766 else {
767 blksz = (info & 0xfffffff);
768 total = 4;
769 }
770 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200771 rem_data = blksz - max;
772 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100773 }
774
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200775 while (blksz) {
776 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100777
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200778 max = MIN(blksz, shctx->block_size - offset);
779 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
780 offset += sz;
781 blksz -= sz;
782 total += sz;
783 if (sz < max)
784 break;
785 if (blksz || offset == shctx->block_size) {
786 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
787 offset = 0;
788 }
789 }
790
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200791 appctx->ctx.cache.offset = offset;
792 appctx->ctx.cache.next = shblk;
793 appctx->ctx.cache.sent += total;
794 appctx->ctx.cache.rem_data = rem_data + blksz;
795 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100796}
797
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200798static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
799 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100800{
Christopher Faulet95220e22018-12-07 17:34:39 +0100801 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
802 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200803 struct shared_block *shblk;
804 unsigned int offset, sz;
805 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100806
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200807 while (len) {
808 enum htx_blk_type type;
809 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100810
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200811 shblk = appctx->ctx.cache.next;
812 offset = appctx->ctx.cache.offset;
813 if (appctx->ctx.cache.rem_data) {
814 type = HTX_BLK_DATA;
815 info = 0;
816 goto add_data_blk;
817 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100818
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500819 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200820 sz = MIN(4, shctx->block_size - offset);
821 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
822 offset += sz;
823 if (sz < 4) {
824 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
825 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
826 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100827 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200828
829 /* Get payload of the next HTX block and insert it. */
830 type = (info >> 28);
831 if (type != HTX_BLK_DATA)
832 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
833 else {
834 add_data_blk:
835 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100836 }
837
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200838 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100839 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200840 total += ret;
841 len -= ret;
842
843 if (appctx->ctx.cache.rem_data || type == mark)
844 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100845 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100846
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100847 return total;
848}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200849
850static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
851{
852 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
853 unsigned int age;
854 char *end;
855
856 chunk_reset(&trash);
857 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
858 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
859 age = CACHE_ENTRY_MAX_AGE;
860 end = ultoa_o(age, b_head(&trash), b_size(&trash));
861 b_set_data(&trash, end - b_head(&trash));
862 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
863 return 0;
864 return 1;
865}
866
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200867static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100868{
869 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
870 struct shared_block *first = block_ptr(cache_ptr);
871 struct stream_interface *si = appctx->owner;
872 struct channel *req = si_oc(si);
873 struct channel *res = si_ic(si);
874 struct htx *req_htx, *res_htx;
875 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200876 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100877 size_t ret, total = 0;
878
879 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200880 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100881
882 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
883 goto out;
884
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500885 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100886 if (!b_size(&res->buf)) {
887 si_rx_room_blk(si);
888 goto out;
889 }
890
Willy Tarreauefef3232018-12-16 00:37:45 +0100891 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100892 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100893
894 if (appctx->st0 == HTX_CACHE_INIT) {
895 appctx->ctx.cache.next = block_ptr(cache_ptr);
896 appctx->ctx.cache.offset = sizeof(*cache_ptr);
897 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200898 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100899 appctx->st0 = HTX_CACHE_HEADER;
900 }
901
902 if (appctx->st0 == HTX_CACHE_HEADER) {
903 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200904 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
905 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
906 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
907 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100908 goto error;
909
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200910 /* Skip response body for HEAD requests */
911 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100912 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100913 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200914 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100915 }
916
917 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200918 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
919 if (len) {
920 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
921 if (ret < len) {
922 si_rx_room_blk(si);
923 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100924 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100925 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200926 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100927 }
928
929 if (appctx->st0 == HTX_CACHE_EOM) {
930 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]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001179 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1180 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) {
1191 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1192 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001193 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001194 err_code |= ERR_ALERT | ERR_ABORT;
1195 goto out;
1196 }
1197
1198 strlcpy2(tmp_cache_config->id, args[1], 33);
1199 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001200 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1201 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001202 err_code |= ERR_WARN;
1203 }
William Lallemand49b44532017-11-24 18:53:43 +01001204 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001205 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001206 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001207 }
1208 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001209 unsigned long int maxsize;
1210 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001211
1212 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1213 err_code |= ERR_ABORT;
1214 goto out;
1215 }
1216
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001217 maxsize = strtoul(args[1], &err, 10);
1218 if (err == args[1] || *err != '\0') {
1219 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1220 file, linenum, args[1]);
1221 err_code |= ERR_ABORT;
1222 goto out;
1223 }
1224
1225 if (maxsize > (UINT_MAX >> 20)) {
1226 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1227 file, linenum, args[1], UINT_MAX >> 20);
1228 err_code |= ERR_ABORT;
1229 goto out;
1230 }
1231
William Lallemand41db4602017-10-30 11:15:51 +01001232 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001233 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001234 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001235 } else if (strcmp(args[0], "max-age") == 0) {
1236 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1237 err_code |= ERR_ABORT;
1238 goto out;
1239 }
1240
1241 if (!*args[1]) {
1242 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1243 file, linenum, args[0]);
1244 err_code |= ERR_WARN;
1245 }
1246
1247 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001248 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001249 unsigned int maxobjsz;
1250 char *err;
1251
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001252 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1253 err_code |= ERR_ABORT;
1254 goto out;
1255 }
1256
1257 if (!*args[1]) {
1258 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1259 file, linenum, args[0]);
1260 err_code |= ERR_WARN;
1261 }
1262
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001263 maxobjsz = strtoul(args[1], &err, 10);
1264 if (err == args[1] || *err != '\0') {
1265 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1266 file, linenum, args[1]);
1267 err_code |= ERR_ABORT;
1268 goto out;
1269 }
1270 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001271 }
1272 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001273 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001274 err_code |= ERR_ALERT | ERR_FATAL;
1275 goto out;
1276 }
1277out:
1278 return err_code;
1279}
1280
1281/* once the cache section is parsed */
1282
1283int cfg_post_parse_section_cache()
1284{
William Lallemand41db4602017-10-30 11:15:51 +01001285 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001286
1287 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001288
1289 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001290 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001291 err_code |= ERR_FATAL | ERR_ALERT;
1292 goto out;
1293 }
1294
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001295 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001296 /* Default max. file size is a 256th of the cache size. */
1297 tmp_cache_config->maxobjsz =
1298 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001299 }
1300 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1301 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1302 err_code |= ERR_FATAL | ERR_ALERT;
1303 goto out;
1304 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001305
William Lallemandd1d1e222019-08-28 15:22:49 +02001306 /* add to the list of cache to init and reinit tmp_cache_config
1307 * for next cache section, if any.
1308 */
1309 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1310 tmp_cache_config = NULL;
1311 return err_code;
1312 }
1313out:
1314 free(tmp_cache_config);
1315 tmp_cache_config = NULL;
1316 return err_code;
1317
1318}
1319
1320int post_check_cache()
1321{
1322 struct proxy *px;
1323 struct cache *back, *cache_config, *cache;
1324 struct shared_context *shctx;
1325 int ret_shctx;
1326 int err_code = 0;
1327
1328 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1329
1330 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1331 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001332
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001333 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001334 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001335 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001336 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001337 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001338
1339 err_code |= ERR_FATAL | ERR_ALERT;
1340 goto out;
1341 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001342 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001343 /* the cache structure is stored in the shctx and added to the
1344 * caches list, we can remove the entry from the caches_config
1345 * list */
1346 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001347 cache = (struct cache *)shctx->data;
1348 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001349 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001350 LIST_DEL(&cache_config->list);
1351 free(cache_config);
1352
1353 /* Find all references for this cache in the existing filters
1354 * (over all proxies) and reference it in matching filters.
1355 */
1356 for (px = proxies_list; px; px = px->next) {
1357 struct flt_conf *fconf;
1358 struct cache_flt_conf *cconf;
1359
1360 list_for_each_entry(fconf, &px->filter_configs, list) {
1361 if (fconf->id != cache_store_flt_id)
1362 continue;
1363
1364 cconf = fconf->conf;
1365 if (!strcmp(cache->id, cconf->c.name)) {
1366 free(cconf->c.name);
1367 cconf->c.cache = cache;
1368 break;
1369 }
1370 }
1371 }
William Lallemand41db4602017-10-30 11:15:51 +01001372 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001373
William Lallemand41db4602017-10-30 11:15:51 +01001374out:
William Lallemand41db4602017-10-30 11:15:51 +01001375 return err_code;
1376
William Lallemand41db4602017-10-30 11:15:51 +01001377}
1378
William Lallemand41db4602017-10-30 11:15:51 +01001379struct flt_ops cache_ops = {
1380 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001381 .check = cache_store_check,
1382 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001383
Christopher Faulet65554e12020-03-06 14:52:06 +01001384 /* Handle stream init/deinit */
1385 .attach = cache_store_strm_init,
1386 .detach = cache_store_strm_deinit,
1387
William Lallemand4da3f8a2017-10-31 14:33:34 +01001388 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001389 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001390
1391 /* Filter HTTP requests and responses */
1392 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001393 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001394 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001395};
1396
Christopher Faulet99a17a22018-12-11 09:18:27 +01001397
1398
1399static int
1400parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1401 struct flt_conf *fconf, char **err, void *private)
1402{
1403 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001404 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001405 char *name = NULL;
1406 int pos = *cur_arg;
1407
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001408 /* Get the cache filter name. <pos> point on "cache" keyword */
1409 if (!*args[pos + 1]) {
1410 memprintf(err, "%s : expects an <id> argument", args[pos]);
1411 goto error;
1412 }
1413 name = strdup(args[pos + 1]);
1414 if (!name) {
1415 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1416 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001417 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001418 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001419
1420 /* Check if an implicit filter with the same name already exists. If so,
1421 * we remove the implicit filter to use the explicit one. */
1422 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1423 if (f->id != cache_store_flt_id)
1424 continue;
1425
1426 cconf = f->conf;
1427 if (strcmp(name, cconf->c.name)) {
1428 cconf = NULL;
1429 continue;
1430 }
1431
1432 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1433 cconf = NULL;
1434 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1435 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001436 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001437 }
1438
1439 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1440 LIST_DEL(&f->list);
1441 free(f);
1442 free(name);
1443 break;
1444 }
1445
1446 /* No implicit cache filter found, create configuration for the explicit one */
1447 if (!cconf) {
1448 cconf = calloc(1, sizeof(*cconf));
1449 if (!cconf) {
1450 memprintf(err, "%s: out of memory", args[*cur_arg]);
1451 goto error;
1452 }
1453 cconf->c.name = name;
1454 }
1455
1456 cconf->flags = 0;
1457 fconf->id = cache_store_flt_id;
1458 fconf->conf = cconf;
1459 fconf->ops = &cache_ops;
1460
1461 *cur_arg = pos;
1462 return 0;
1463
1464 error:
1465 free(name);
1466 free(cconf);
1467 return -1;
1468}
1469
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001470static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001471{
1472 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1473 return 1;
1474
1475 return 0;
1476}
1477
1478static int cli_io_handler_show_cache(struct appctx *appctx)
1479{
1480 struct cache* cache = appctx->ctx.cli.p0;
1481 struct stream_interface *si = appctx->owner;
1482
William Lallemand1f49a362017-11-21 20:01:26 +01001483 if (cache == NULL) {
1484 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1485 }
1486
1487 list_for_each_entry_from(cache, &caches, list) {
1488 struct eb32_node *node = NULL;
1489 unsigned int next_key;
1490 struct cache_entry *entry;
1491
William Lallemand1f49a362017-11-21 20:01:26 +01001492 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001493 if (!next_key) {
1494 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1495 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001496 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001497 return 0;
1498 }
1499 }
William Lallemand1f49a362017-11-21 20:01:26 +01001500
1501 appctx->ctx.cli.p0 = cache;
1502
1503 while (1) {
1504
1505 shctx_lock(shctx_ptr(cache));
1506 node = eb32_lookup_ge(&cache->entries, next_key);
1507 if (!node) {
1508 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001509 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001510 break;
1511 }
1512
1513 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001514 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 +01001515
1516 next_key = node->key + 1;
1517 appctx->ctx.cli.i0 = next_key;
1518
1519 shctx_unlock(shctx_ptr(cache));
1520
1521 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001522 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001523 return 0;
1524 }
1525 }
1526
1527 }
1528
1529 return 1;
1530
1531}
1532
Christopher Faulet99a17a22018-12-11 09:18:27 +01001533/* Declare the filter parser for "cache" keyword */
1534static struct flt_kw_list filter_kws = { "CACHE", { }, {
1535 { "cache", parse_cache_flt, NULL },
1536 { NULL, NULL, NULL },
1537 }
1538};
1539
1540INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1541
William Lallemand1f49a362017-11-21 20:01:26 +01001542static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001543 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1544 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001545}};
1546
Willy Tarreau0108d902018-11-25 19:14:37 +01001547INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001548
William Lallemand41db4602017-10-30 11:15:51 +01001549static struct action_kw_list http_res_actions = {
1550 .kw = {
1551 { "cache-store", parse_cache_store },
1552 { NULL, NULL }
1553 }
1554};
1555
Willy Tarreau0108d902018-11-25 19:14:37 +01001556INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1557
William Lallemand41db4602017-10-30 11:15:51 +01001558static struct action_kw_list http_req_actions = {
1559 .kw = {
1560 { "cache-use", parse_cache_use },
1561 { NULL, NULL }
1562 }
1563};
1564
Willy Tarreau0108d902018-11-25 19:14:37 +01001565INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1566
Willy Tarreau2231b632019-03-29 18:26:52 +01001567struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001568 .obj_type = OBJ_TYPE_APPLET,
1569 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001570 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001571 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001572};
1573
Willy Tarreaue6552512018-11-26 11:33:13 +01001574/* config parsers for this section */
1575REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001576REGISTER_POST_CHECK(post_check_cache);