blob: 3b248ef465c207f48fb912d6543fd1b2ee0e0a7b [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
William Lallemand41db4602017-10-30 11:15:51 +010013#include <eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010014#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010015
William Lallemand75d93292017-11-21 20:01:24 +010016#include <types/action.h>
William Lallemand1f49a362017-11-21 20:01:26 +010017#include <types/cli.h>
William Lallemand75d93292017-11-21 20:01:24 +010018#include <types/filters.h>
19#include <types/proxy.h>
20#include <types/shctx.h>
21
William Lallemand41db4602017-10-30 11:15:51 +010022#include <proto/channel.h>
William Lallemand1f49a362017-11-21 20:01:26 +010023#include <proto/cli.h>
William Lallemand41db4602017-10-30 11:15:51 +010024#include <proto/proxy.h>
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010025#include <proto/http_htx.h>
William Lallemand41db4602017-10-30 11:15:51 +010026#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020027#include <proto/http_rules.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020028#include <proto/http_ana.h>
William Lallemand41db4602017-10-30 11:15:51 +010029#include <proto/log.h>
30#include <proto/stream.h>
31#include <proto/stream_interface.h>
32#include <proto/shctx.h>
33
William Lallemand41db4602017-10-30 11:15:51 +010034
35#include <common/cfgparse.h>
36#include <common/hash.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010037#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010038#include <common/initcall.h>
Willy Tarreau8b507582020-02-25 09:35:07 +010039#include <common/net_helper.h>
William Lallemand41db4602017-10-30 11:15:51 +010040
Christopher Faulet27d93c32018-12-15 22:32:02 +010041#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010042 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010043
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010044const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010045
Willy Tarreau2231b632019-03-29 18:26:52 +010046extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010047
48struct flt_ops cache_ops;
49
50struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010052 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010053 unsigned int maxage; /* max-age */
54 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020055 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010056 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010057};
58
Christopher Faulet95220e22018-12-07 17:34:39 +010059/* cache config for filters */
60struct cache_flt_conf {
61 union {
62 struct cache *cache; /* cache used by the filter */
63 char *name; /* cache name used during conf parsing */
64 } c;
65 unsigned int flags; /* CACHE_FLT_F_* */
66};
67
William Lallemand41db4602017-10-30 11:15:51 +010068/*
69 * cache ctx for filters
70 */
71struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010072 struct shared_block *first_block;
73};
74
75struct cache_entry {
76 unsigned int latest_validation; /* latest validation date */
77 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020078 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010079
William Lallemand41db4602017-10-30 11:15:51 +010080 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010081 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010082 unsigned char data[0];
83};
84
85#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010086#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010087
88static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020089static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010090static struct cache *tmp_cache_config = NULL;
91
Willy Tarreau8ceae722018-11-26 11:58:30 +010092DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
93
William Lallemandf528fff2017-11-23 19:43:17 +010094struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010095{
96 struct eb32_node *node;
97 struct cache_entry *entry;
98
Willy Tarreau8b507582020-02-25 09:35:07 +010099 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100100 if (!node)
101 return NULL;
102
103 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100104
105 /* if that's not the right node */
106 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
107 return NULL;
108
William Lallemand08727662017-11-21 20:01:27 +0100109 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100110 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100111 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100112 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100113 entry->eb.key = 0;
114 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100115 return NULL;
116
117}
118
119static inline struct shared_context *shctx_ptr(struct cache *cache)
120{
121 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
122}
123
William Lallemand77c11972017-10-31 20:43:01 +0100124static inline struct shared_block *block_ptr(struct cache_entry *entry)
125{
126 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
127}
128
129
130
William Lallemand41db4602017-10-30 11:15:51 +0100131static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100132cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100133{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100134 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100135 return 0;
136}
137
Christopher Faulet95220e22018-12-07 17:34:39 +0100138static void
139cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
140{
141 struct cache_flt_conf *cconf = fconf->conf;
142
143 free(cconf);
144}
145
William Lallemand4da3f8a2017-10-31 14:33:34 +0100146static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100147cache_store_check(struct proxy *px, struct flt_conf *fconf)
148{
149 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100150 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100151 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100152 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100153
William Lallemandd1d1e222019-08-28 15:22:49 +0200154 /* Find the cache corresponding to the name in the filter config. The
155 * cache will not be referenced now in the filter config because it is
156 * not fully allocated. This step will be performed during the cache
157 * post_check.
158 */
159 list_for_each_entry(cache, &caches_config, list) {
160 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100161 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100162 }
163
164 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
165 proxy_type_str(px), px->id, (char *)cconf->c.name);
166 return 1;
167
168 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100169 /* Here <cache> points on the cache the filter must use and <cconf>
170 * points on the cache filter configuration. */
171
172 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100173 * enabled and if it is after the cache. When the compression is before
174 * the cache, an error is returned. Also check if the cache filter must
175 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100176 list_for_each_entry(f, &px->filter_configs, list) {
177 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100178 /* The compression filter must be evaluated after the cache. */
179 if (comp) {
180 ha_alert("config: %s '%s': unable to enable the compression filter before "
181 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
182 return 1;
183 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100184 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200185 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100186 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200187 else if (f->id == fcgi_flt_id)
188 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100189 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
190 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200191 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100192 * declaration is required. */
193 ha_alert("config: %s '%s': require an explicit filter declaration "
194 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
195 return 1;
196 }
197
Christopher Fauletafd819c2018-12-11 08:57:45 +0100198 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100199 return 0;
200}
201
202static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100203cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100204{
Christopher Faulet65554e12020-03-06 14:52:06 +0100205 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100206
Christopher Faulet65554e12020-03-06 14:52:06 +0100207 st = pool_alloc_dirty(pool_head_cache_st);
208 if (st == NULL)
209 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100210
Christopher Faulet65554e12020-03-06 14:52:06 +0100211 st->first_block = NULL;
212 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100213
Christopher Faulet65554e12020-03-06 14:52:06 +0100214 /* Register post-analyzer on AN_RES_WAIT_HTTP */
215 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100216 return 1;
217}
218
Christopher Faulet65554e12020-03-06 14:52:06 +0100219static void
220cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100221{
222 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100223 struct cache_flt_conf *cconf = FLT_CONF(filter);
224 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100225 struct shared_context *shctx = shctx_ptr(cache);
226
William Lallemand49dc0482017-11-24 14:33:54 +0100227 /* Everything should be released in the http_end filter, but we need to do it
228 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100229 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100230 shctx_lock(shctx);
231 shctx_row_dec_hot(shctx, st->first_block);
232 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100233 }
234 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100235 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100236 filter->ctx = NULL;
237 }
William Lallemand49dc0482017-11-24 14:33:54 +0100238}
239
Christopher Faulet839791a2019-01-07 16:12:07 +0100240static int
241cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
242 unsigned an_bit)
243{
244 struct http_txn *txn = s->txn;
245 struct http_msg *msg = &txn->rsp;
246 struct cache_st *st = filter->ctx;
247
248 if (an_bit != AN_RES_WAIT_HTTP)
249 goto end;
250
251 /* Here we need to check if any compression filter precedes the cache
252 * filter. This is only possible when the compression is configured in
253 * the frontend while the cache filter is configured on the
254 * backend. This case cannot be detected during HAProxy startup. So in
255 * such cases, the cache is disabled.
256 */
257 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
258 pool_free(pool_head_cache_st, st);
259 filter->ctx = NULL;
260 }
261
262 end:
263 return 1;
264}
William Lallemand49dc0482017-11-24 14:33:54 +0100265
266static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100267cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
268{
269 struct cache_st *st = filter->ctx;
270
William Lallemand4da3f8a2017-10-31 14:33:34 +0100271 if (!(msg->chn->flags & CF_ISRESP) || !st)
272 return 1;
273
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200274 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100275 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100276 return 1;
277}
278
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200279static inline void disable_cache_entry(struct cache_st *st,
280 struct filter *filter, struct shared_context *shctx)
281{
282 struct cache_entry *object;
283
284 object = (struct cache_entry *)st->first_block->data;
285 filter->ctx = NULL; /* disable cache */
286 shctx_lock(shctx);
287 shctx_row_dec_hot(shctx, st->first_block);
288 object->eb.key = 0;
289 shctx_unlock(shctx);
290 pool_free(pool_head_cache_st, st);
291}
292
William Lallemand4da3f8a2017-10-31 14:33:34 +0100293static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100294cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
295 unsigned int offset, unsigned int len)
296{
Christopher Faulet95220e22018-12-07 17:34:39 +0100297 struct cache_flt_conf *cconf = FLT_CONF(filter);
298 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100299 struct cache_st *st = filter->ctx;
300 struct htx *htx = htxbuf(&msg->chn->buf);
301 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200302 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100303 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200304 unsigned int orig_len, to_forward;
305 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100306
307 if (!len)
308 return len;
309
310 if (!st->first_block) {
311 unregister_data_filter(s, msg->chn, filter);
312 return len;
313 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100314
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200315 chunk_reset(&trash);
316 orig_len = len;
317 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100318
319 htxret = htx_find_offset(htx, offset);
320 blk = htxret.blk;
321 offset = htxret.ret;
322 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100323 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200324 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100325 struct ist v;
326
327 switch (type) {
328 case HTX_BLK_UNUSED:
329 break;
330
331 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100332 v = htx_get_blk_value(htx, blk);
333 v.ptr += offset;
334 v.len -= offset;
335 if (v.len > len)
336 v.len = len;
337
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200338 info = (type << 28) + v.len;
339 chunk_memcat(&trash, (char *)&info, sizeof(info));
340 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100341 to_forward += v.len;
342 len -= v.len;
343 break;
344
345 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200346 /* Here offset must always be 0 because only
347 * DATA blocks can be partially transferred. */
348 if (offset)
349 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100350 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200351 goto end;
352
353 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
354 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100355 to_forward += sz;
356 len -= sz;
357 break;
358 }
359
360 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100361 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200362
363 end:
364 shctx_lock(shctx);
365 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
366 if (!fb) {
367 shctx_unlock(shctx);
368 goto no_cache;
369 }
370 shctx_unlock(shctx);
371
372 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
373 (unsigned char *)b_head(&trash), b_data(&trash));
374 if (ret < 0)
375 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100376
377 return to_forward;
378
379 no_cache:
380 disable_cache_entry(st, filter, shctx);
381 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200382 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100383}
384
385static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100386cache_store_http_end(struct stream *s, struct filter *filter,
387 struct http_msg *msg)
388{
389 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100390 struct cache_flt_conf *cconf = FLT_CONF(filter);
391 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100392 struct shared_context *shctx = shctx_ptr(cache);
393 struct cache_entry *object;
394
395 if (!(msg->chn->flags & CF_ISRESP))
396 return 1;
397
398 if (st && st->first_block) {
399
400 object = (struct cache_entry *)st->first_block->data;
401
402 /* does not need to test if the insertion worked, if it
403 * doesn't, the blocks will be reused anyway */
404
405 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100406 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
407 object->eb.key = 0;
408 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100409 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100410 shctx_row_dec_hot(shctx, st->first_block);
411 shctx_unlock(shctx);
412
413 }
414 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100415 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100416 filter->ctx = NULL;
417 }
418
419 return 1;
420}
421
422 /*
423 * This intends to be used when checking HTTP headers for some
424 * word=value directive. Return a pointer to the first character of value, if
425 * the word was not found or if there wasn't any value assigned ot it return NULL
426 */
427char *directive_value(const char *sample, int slen, const char *word, int wlen)
428{
429 int st = 0;
430
431 if (slen < wlen)
432 return 0;
433
434 while (wlen) {
435 char c = *sample ^ *word;
436 if (c && c != ('A' ^ 'a'))
437 return NULL;
438 sample++;
439 word++;
440 slen--;
441 wlen--;
442 }
443
444 while (slen) {
445 if (st == 0) {
446 if (*sample != '=')
447 return NULL;
448 sample++;
449 slen--;
450 st = 1;
451 continue;
452 } else {
453 return (char *)sample;
454 }
455 }
456
457 return NULL;
458}
459
460/*
461 * Return the maxage in seconds of an HTTP response.
462 * Compute the maxage using either:
463 * - the assigned max-age of the cache
464 * - the s-maxage directive
465 * - the max-age directive
466 * - (Expires - Data) headers
467 * - the default-max-age of the cache
468 *
469 */
William Lallemand49b44532017-11-24 18:53:43 +0100470int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100471{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200472 struct htx *htx = htxbuf(&s->res.buf);
473 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100474 int smaxage = -1;
475 int maxage = -1;
476
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200477 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
478 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100479
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200480 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
481 if (value) {
482 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100483
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200484 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
485 chunk_strncat(chk, "", 1);
486 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100487 }
488
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200489 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
490 if (value) {
491 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200492
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200493 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
494 chunk_strncat(chk, "", 1);
495 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100496 }
497 }
498
499 /* TODO: Expires - Data */
500
501
502 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100503 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100504
505 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100506 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100507
William Lallemand49b44532017-11-24 18:53:43 +0100508 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100509
510}
511
512
William Lallemanda400a3a2017-11-20 19:13:12 +0100513static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
514{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200515 struct cache_entry *object = (struct cache_entry *)block->data;
516
517 if (first == block && object->eb.key)
518 eb32_delete(&object->eb);
519 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100520}
521
William Lallemand41db4602017-10-30 11:15:51 +0100522/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500523 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100524 * register a filter to store the data
525 */
526enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200527 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100528{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200529 unsigned int age;
530 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100531 struct http_txn *txn = s->txn;
532 struct http_msg *msg = &txn->rsp;
533 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100534 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100535 struct cache_flt_conf *cconf = rule->arg.act.p[0];
536 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100537 struct cache_st *cache_ctx = NULL;
538 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100539 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200540 struct htx *htx;
541 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200542 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200543 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100544
William Lallemand4da3f8a2017-10-31 14:33:34 +0100545 /* Don't cache if the response came from a cache */
546 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
547 s->target == &http_cache_applet.obj_type) {
548 goto out;
549 }
550
551 /* cache only HTTP/1.1 */
552 if (!(txn->req.flags & HTTP_MSGF_VER_11))
553 goto out;
554
Willy Tarreau6905d182019-10-01 17:59:17 +0200555 /* cache only GET method */
556 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100557 goto out;
558
Willy Tarreauc9036c02019-01-11 19:38:25 +0100559 /* cache key was not computed */
560 if (!key)
561 goto out;
562
William Lallemand4da3f8a2017-10-31 14:33:34 +0100563 /* cache only 200 status code */
564 if (txn->status != 200)
565 goto out;
566
Christopher Faulet839791a2019-01-07 16:12:07 +0100567 /* Find the corresponding filter instance for the current stream */
568 list_for_each_entry(filter, &s->strm_flt.filters, list) {
569 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
570 /* No filter ctx, don't cache anything */
571 if (!filter->ctx)
572 goto out;
573 cache_ctx = filter->ctx;
574 break;
575 }
576 }
577
578 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200579 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100580
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200581 /* Do not cache too big objects. */
582 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
583 htx->data + htx->extra > shctx->max_obj_size)
584 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100585
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200586 /* Does not manage Vary at the moment. We will need a secondary key later for that */
587 ctx.blk = NULL;
588 if (http_find_header(htx, ist("Vary"), &ctx, 0))
589 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100590
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200591 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100592
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200593 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
594 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100595
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200596 age = 0;
597 ctx.blk = NULL;
598 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
599 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
600 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
601 hdr_age = CACHE_ENTRY_MAX_AGE;
602 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100603 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200604 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100605 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100606
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200607 chunk_reset(&trash);
608 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
609 struct htx_blk *blk = htx_get_blk(htx, pos);
610 enum htx_blk_type type = htx_get_blk_type(blk);
611 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100612
Christopher Fauletb0667472019-09-03 22:22:12 +0200613 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200614 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
615 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
616 if (type == HTX_BLK_EOH)
617 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200618 }
619
Christopher Fauletb0667472019-09-03 22:22:12 +0200620 /* Do not cache objects if the headers are too big. */
621 if (hdrs_len > htx->size - global.tune.maxrewrite)
622 goto out;
623
William Lallemand4da3f8a2017-10-31 14:33:34 +0100624 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200625 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100626 if (!first) {
627 shctx_unlock(shctx);
628 goto out;
629 }
630 shctx_unlock(shctx);
631
Willy Tarreau1093a452018-04-06 19:02:25 +0200632 /* the received memory is not initialized, we need at least to mark
633 * the object as not indexed yet.
634 */
635 object = (struct cache_entry *)first->data;
636 object->eb.node.leaf_p = NULL;
637 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200638 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200639
William Lallemand4da3f8a2017-10-31 14:33:34 +0100640 /* reserve space for the cache_entry structure */
641 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200642 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100643 /* cache the headers in a http action because it allows to chose what
644 * to cache, for example you might want to cache a response before
645 * modifying some HTTP headers, or on the contrary after modifying
646 * those headers.
647 */
648
649 /* does not need to be locked because it's in the "hot" list,
650 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200651 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
652 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100653
654 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100655 if (cache_ctx) {
656 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100657
Willy Tarreauc9036c02019-01-11 19:38:25 +0100658 object->eb.key = key;
659
Christopher Faulet839791a2019-01-07 16:12:07 +0100660 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
661 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100662
Christopher Faulet839791a2019-01-07 16:12:07 +0100663 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100664
Christopher Faulet839791a2019-01-07 16:12:07 +0100665 old = entry_exist(cconf->c.cache, txn->cache_hash);
666 if (old) {
667 eb32_delete(&old->eb);
668 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100669 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100670 shctx_unlock(shctx);
671
672 /* store latest value and expiration time */
673 object->latest_validation = now.tv_sec;
674 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
675 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100676 }
677
678out:
679 /* if does not cache */
680 if (first) {
681 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100682 first->len = 0;
683 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100684 shctx_row_dec_hot(shctx, first);
685 shctx_unlock(shctx);
686 }
687
William Lallemand41db4602017-10-30 11:15:51 +0100688 return ACT_RET_CONT;
689}
690
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100691#define HTX_CACHE_INIT 0 /* Initial state. */
692#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
693#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200694#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
695#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100696
William Lallemandecb73b12017-11-24 14:33:55 +0100697static void http_cache_applet_release(struct appctx *appctx)
698{
Christopher Faulet95220e22018-12-07 17:34:39 +0100699 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100700 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100701 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100702 struct shared_block *first = block_ptr(cache_ptr);
703
704 shctx_lock(shctx_ptr(cache));
705 shctx_row_dec_hot(shctx_ptr(cache), first);
706 shctx_unlock(shctx_ptr(cache));
707}
708
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200709
710static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
711 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100712{
Christopher Faulet95220e22018-12-07 17:34:39 +0100713 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
714 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200715 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200716 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200717 unsigned int max, total;
718 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100719
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200720 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
721 if (!max)
722 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200723 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200724 ? (info & 0xff) + ((info >> 8) & 0xfffff)
725 : info & 0xfffffff);
726 if (blksz > max)
727 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100728
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200729 blk = htx_add_blk(htx, type, blksz);
730 if (!blk)
731 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100732
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200733 blk->info = info;
734 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200735 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200736 while (blksz) {
737 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200738 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200739 offset += max;
740 blksz -= max;
741 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200742 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200743 if (blksz || offset == shctx->block_size) {
744 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
745 offset = 0;
746 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100747 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200748 appctx->ctx.cache.offset = offset;
749 appctx->ctx.cache.next = shblk;
750 appctx->ctx.cache.sent += total;
751 return total;
752}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100753
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200754static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
755 uint32_t info, struct shared_block *shblk, unsigned int offset)
756{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100757
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200758 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
759 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
760 unsigned int max, total, rem_data;
761 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100762
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200763 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
764 if (!max)
765 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100766
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200767 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200768 if (appctx->ctx.cache.rem_data) {
769 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200770 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200771 }
772 else {
773 blksz = (info & 0xfffffff);
774 total = 4;
775 }
776 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200777 rem_data = blksz - max;
778 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100779 }
780
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200781 while (blksz) {
782 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100783
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200784 max = MIN(blksz, shctx->block_size - offset);
785 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
786 offset += sz;
787 blksz -= sz;
788 total += sz;
789 if (sz < max)
790 break;
791 if (blksz || offset == shctx->block_size) {
792 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
793 offset = 0;
794 }
795 }
796
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200797 appctx->ctx.cache.offset = offset;
798 appctx->ctx.cache.next = shblk;
799 appctx->ctx.cache.sent += total;
800 appctx->ctx.cache.rem_data = rem_data + blksz;
801 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100802}
803
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200804static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
805 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100806{
Christopher Faulet95220e22018-12-07 17:34:39 +0100807 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
808 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200809 struct shared_block *shblk;
810 unsigned int offset, sz;
811 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100812
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200813 while (len) {
814 enum htx_blk_type type;
815 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100816
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200817 shblk = appctx->ctx.cache.next;
818 offset = appctx->ctx.cache.offset;
819 if (appctx->ctx.cache.rem_data) {
820 type = HTX_BLK_DATA;
821 info = 0;
822 goto add_data_blk;
823 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100824
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500825 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200826 sz = MIN(4, shctx->block_size - offset);
827 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
828 offset += sz;
829 if (sz < 4) {
830 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
831 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
832 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100833 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200834
835 /* Get payload of the next HTX block and insert it. */
836 type = (info >> 28);
837 if (type != HTX_BLK_DATA)
838 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
839 else {
840 add_data_blk:
841 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100842 }
843
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200844 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100845 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200846 total += ret;
847 len -= ret;
848
849 if (appctx->ctx.cache.rem_data || type == mark)
850 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100851 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100852
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100853 return total;
854}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200855
856static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
857{
858 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
859 unsigned int age;
860 char *end;
861
862 chunk_reset(&trash);
863 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
864 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
865 age = CACHE_ENTRY_MAX_AGE;
866 end = ultoa_o(age, b_head(&trash), b_size(&trash));
867 b_set_data(&trash, end - b_head(&trash));
868 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
869 return 0;
870 return 1;
871}
872
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200873static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100874{
875 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
876 struct shared_block *first = block_ptr(cache_ptr);
877 struct stream_interface *si = appctx->owner;
878 struct channel *req = si_oc(si);
879 struct channel *res = si_ic(si);
880 struct htx *req_htx, *res_htx;
881 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200882 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100883 size_t ret, total = 0;
884
885 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200886 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100887
888 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
889 goto out;
890
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500891 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100892 if (!b_size(&res->buf)) {
893 si_rx_room_blk(si);
894 goto out;
895 }
896
Willy Tarreauefef3232018-12-16 00:37:45 +0100897 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100898 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100899
900 if (appctx->st0 == HTX_CACHE_INIT) {
901 appctx->ctx.cache.next = block_ptr(cache_ptr);
902 appctx->ctx.cache.offset = sizeof(*cache_ptr);
903 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200904 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100905 appctx->st0 = HTX_CACHE_HEADER;
906 }
907
908 if (appctx->st0 == HTX_CACHE_HEADER) {
909 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200910 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
911 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
912 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
913 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100914 goto error;
915
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200916 /* Skip response body for HEAD requests */
917 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100918 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100919 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200920 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100921 }
922
923 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200924 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
925 if (len) {
926 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
927 if (ret < len) {
928 si_rx_room_blk(si);
929 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100930 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100931 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200932 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100933 }
934
935 if (appctx->st0 == HTX_CACHE_EOM) {
936 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
937 si_rx_room_blk(si);
938 goto out;
939 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100940 appctx->st0 = HTX_CACHE_END;
941 }
942
943 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100944 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100945 res->flags |= CF_READ_NULL;
946 si_shutr(si);
947 }
948
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100949 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200950 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100951 if (total)
952 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100953 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100954
955 /* eat the whole request */
956 if (co_data(req)) {
957 req_htx = htx_from_buf(&req->buf);
958 co_htx_skip(req, req_htx, co_data(req));
959 htx_to_buf(req_htx, &req->buf);
960 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100961 return;
962
963 error:
964 /* Sent and HTTP error 500 */
965 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200966 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100967 res->buf.data = b_data(errmsg);
968 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
969 res_htx = htx_from_buf(&res->buf);
970
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200971 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100972 appctx->st0 = HTX_CACHE_END;
973 goto end;
974}
975
976
Christopher Faulet95220e22018-12-07 17:34:39 +0100977static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100978{
979 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100980 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100981
Christopher Faulet95220e22018-12-07 17:34:39 +0100982 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100983 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100984 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100985 }
986
987 /* check if a cache filter was already registered with this cache
988 * name, if that's the case, must use it. */
989 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100990 if (fconf->id == cache_store_flt_id) {
991 cconf = fconf->conf;
992 if (cconf && !strcmp((char *)cconf->c.name, name)) {
993 rule->arg.act.p[0] = cconf;
994 return 1;
995 }
William Lallemand41db4602017-10-30 11:15:51 +0100996 }
997 }
998
Christopher Faulet95220e22018-12-07 17:34:39 +0100999 /* Create the filter cache config */
1000 cconf = calloc(1, sizeof(*cconf));
1001 if (!cconf) {
1002 memprintf(err, "out of memory\n");
1003 goto err;
1004 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001005 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001006 cconf->c.name = strdup(name);
1007 if (!cconf->c.name) {
1008 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
William Lallemand41db4602017-10-30 11:15:51 +01001012 /* register a filter to fill the cache buffer */
1013 fconf = calloc(1, sizeof(*fconf));
1014 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001015 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001016 goto err;
1017 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001018 fconf->id = cache_store_flt_id;
1019 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001020 fconf->ops = &cache_ops;
1021 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1022
Christopher Faulet95220e22018-12-07 17:34:39 +01001023 rule->arg.act.p[0] = cconf;
1024 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001025
Christopher Faulet95220e22018-12-07 17:34:39 +01001026 err:
1027 free(cconf);
1028 return 0;
1029}
1030
1031enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1032 struct act_rule *rule, char **err)
1033{
1034 rule->action = ACT_CUSTOM;
1035 rule->action_ptr = http_action_store_cache;
1036
1037 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1038 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001039
Christopher Faulet95220e22018-12-07 17:34:39 +01001040 (*orig_arg)++;
1041 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001042}
1043
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001044/* This produces a sha1 hash of the concatenation of the HTTP method,
1045 * the first occurrence of the Host header followed by the path component
1046 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001047int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001048{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001049 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001050 struct htx *htx = htxbuf(&s->req.buf);
1051 struct htx_sl *sl;
1052 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001053 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001054 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001055 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001056
William Lallemandf528fff2017-11-23 19:43:17 +01001057 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001058 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001059
1060 switch (txn->meth) {
1061 case HTTP_METH_HEAD:
1062 case HTTP_METH_GET:
1063 chunk_memcat(trash, "GET", 3);
1064 break;
1065 default:
1066 return 0;
1067 }
1068
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001069 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001070 uri = htx_sl_req_uri(sl); // whole uri
1071 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001072 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001073
1074 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1075 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1076 * URIs are almost always sent in absolute form with their scheme. In
1077 * this case, the scheme is almost always "https". In order to support
1078 * sharing of cache objects between H1 and H2, we'll hash the absolute
1079 * URI whenever known, or prepend "https://" + the Host header for
1080 * relative URIs. The difference will only appear on absolute HTTP/1
1081 * requests sent to an origin server, which practically is never met in
1082 * the real world so we don't care about the ability to share the same
1083 * key here.URIs are normalized from the absolute URI to an origin form as
1084 * well.
1085 */
1086 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001087 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001088 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1089 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001090 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001091 }
1092
1093 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001094
1095 /* hash everything */
1096 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001097 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001098 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1099
1100 return 1;
1101}
1102
William Lallemand41db4602017-10-30 11:15:51 +01001103enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1104 struct session *sess, struct stream *s, int flags)
1105{
William Lallemand77c11972017-10-31 20:43:01 +01001106
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001107 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001108 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001109 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1110 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001111
Willy Tarreau6905d182019-10-01 17:59:17 +02001112 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1113 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001114 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001115 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001116 txn->flags |= TX_CACHE_IGNORE;
1117
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001118 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001119
Willy Tarreau504455c2017-12-22 17:47:35 +01001120 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1121 return ACT_RET_CONT;
1122
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001123 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001124 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001125
Willy Tarreau504455c2017-12-22 17:47:35 +01001126 if (s->txn->flags & TX_CACHE_IGNORE)
1127 return ACT_RET_CONT;
1128
Willy Tarreaua1214a52018-12-14 14:00:25 +01001129 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001130 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001131 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001132 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001133
William Lallemanda400a3a2017-11-20 19:13:12 +01001134 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001135 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001136 if (res) {
1137 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001138 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1139 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001140 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001141 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001142 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001143 appctx->rule = rule;
1144 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001145 appctx->ctx.cache.next = NULL;
1146 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001147
1148 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001149 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001150 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001151 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001152 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001153 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001154 shctx_lock(shctx_ptr(cache));
1155 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1156 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001157 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001158 }
1159 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001160 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001161 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001162}
1163
1164
1165enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1166 struct act_rule *rule, char **err)
1167{
William Lallemand41db4602017-10-30 11:15:51 +01001168 rule->action = ACT_CUSTOM;
1169 rule->action_ptr = http_action_req_cache_use;
1170
Christopher Faulet95220e22018-12-07 17:34:39 +01001171 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001172 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001173
1174 (*orig_arg)++;
1175 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001176}
1177
1178int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1179{
1180 int err_code = 0;
1181
1182 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1183
1184 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001185 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1186 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001187 err_code |= ERR_ALERT | ERR_ABORT;
1188 goto out;
1189 }
1190
1191 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1192 err_code |= ERR_ABORT;
1193 goto out;
1194 }
1195
1196 if (tmp_cache_config == NULL) {
1197 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1198 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001200 err_code |= ERR_ALERT | ERR_ABORT;
1201 goto out;
1202 }
1203
1204 strlcpy2(tmp_cache_config->id, args[1], 33);
1205 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001206 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1207 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001208 err_code |= ERR_WARN;
1209 }
William Lallemand49b44532017-11-24 18:53:43 +01001210 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001211 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001212 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001213 }
1214 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001215 unsigned long int maxsize;
1216 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001217
1218 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1219 err_code |= ERR_ABORT;
1220 goto out;
1221 }
1222
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001223 maxsize = strtoul(args[1], &err, 10);
1224 if (err == args[1] || *err != '\0') {
1225 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1226 file, linenum, args[1]);
1227 err_code |= ERR_ABORT;
1228 goto out;
1229 }
1230
1231 if (maxsize > (UINT_MAX >> 20)) {
1232 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1233 file, linenum, args[1], UINT_MAX >> 20);
1234 err_code |= ERR_ABORT;
1235 goto out;
1236 }
1237
William Lallemand41db4602017-10-30 11:15:51 +01001238 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001239 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001240 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001241 } else if (strcmp(args[0], "max-age") == 0) {
1242 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1243 err_code |= ERR_ABORT;
1244 goto out;
1245 }
1246
1247 if (!*args[1]) {
1248 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1249 file, linenum, args[0]);
1250 err_code |= ERR_WARN;
1251 }
1252
1253 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001254 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001255 unsigned int maxobjsz;
1256 char *err;
1257
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001258 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1259 err_code |= ERR_ABORT;
1260 goto out;
1261 }
1262
1263 if (!*args[1]) {
1264 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1265 file, linenum, args[0]);
1266 err_code |= ERR_WARN;
1267 }
1268
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001269 maxobjsz = strtoul(args[1], &err, 10);
1270 if (err == args[1] || *err != '\0') {
1271 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1272 file, linenum, args[1]);
1273 err_code |= ERR_ABORT;
1274 goto out;
1275 }
1276 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001277 }
1278 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001279 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001280 err_code |= ERR_ALERT | ERR_FATAL;
1281 goto out;
1282 }
1283out:
1284 return err_code;
1285}
1286
1287/* once the cache section is parsed */
1288
1289int cfg_post_parse_section_cache()
1290{
William Lallemand41db4602017-10-30 11:15:51 +01001291 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001292
1293 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001294
1295 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001296 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001297 err_code |= ERR_FATAL | ERR_ALERT;
1298 goto out;
1299 }
1300
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001301 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001302 /* Default max. file size is a 256th of the cache size. */
1303 tmp_cache_config->maxobjsz =
1304 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001305 }
1306 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1307 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1308 err_code |= ERR_FATAL | ERR_ALERT;
1309 goto out;
1310 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001311
William Lallemandd1d1e222019-08-28 15:22:49 +02001312 /* add to the list of cache to init and reinit tmp_cache_config
1313 * for next cache section, if any.
1314 */
1315 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1316 tmp_cache_config = NULL;
1317 return err_code;
1318 }
1319out:
1320 free(tmp_cache_config);
1321 tmp_cache_config = NULL;
1322 return err_code;
1323
1324}
1325
1326int post_check_cache()
1327{
1328 struct proxy *px;
1329 struct cache *back, *cache_config, *cache;
1330 struct shared_context *shctx;
1331 int ret_shctx;
1332 int err_code = 0;
1333
1334 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1335
1336 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1337 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001338
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001339 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001340 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001341 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001342 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001343 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001344
1345 err_code |= ERR_FATAL | ERR_ALERT;
1346 goto out;
1347 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001348 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001349 /* the cache structure is stored in the shctx and added to the
1350 * caches list, we can remove the entry from the caches_config
1351 * list */
1352 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001353 cache = (struct cache *)shctx->data;
1354 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001355 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001356 LIST_DEL(&cache_config->list);
1357 free(cache_config);
1358
1359 /* Find all references for this cache in the existing filters
1360 * (over all proxies) and reference it in matching filters.
1361 */
1362 for (px = proxies_list; px; px = px->next) {
1363 struct flt_conf *fconf;
1364 struct cache_flt_conf *cconf;
1365
1366 list_for_each_entry(fconf, &px->filter_configs, list) {
1367 if (fconf->id != cache_store_flt_id)
1368 continue;
1369
1370 cconf = fconf->conf;
1371 if (!strcmp(cache->id, cconf->c.name)) {
1372 free(cconf->c.name);
1373 cconf->c.cache = cache;
1374 break;
1375 }
1376 }
1377 }
William Lallemand41db4602017-10-30 11:15:51 +01001378 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001379
William Lallemand41db4602017-10-30 11:15:51 +01001380out:
William Lallemand41db4602017-10-30 11:15:51 +01001381 return err_code;
1382
William Lallemand41db4602017-10-30 11:15:51 +01001383}
1384
William Lallemand41db4602017-10-30 11:15:51 +01001385struct flt_ops cache_ops = {
1386 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001387 .check = cache_store_check,
1388 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001389
Christopher Faulet65554e12020-03-06 14:52:06 +01001390 /* Handle stream init/deinit */
1391 .attach = cache_store_strm_init,
1392 .detach = cache_store_strm_deinit,
1393
William Lallemand4da3f8a2017-10-31 14:33:34 +01001394 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001395 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001396
1397 /* Filter HTTP requests and responses */
1398 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001399 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001400 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001401};
1402
Christopher Faulet99a17a22018-12-11 09:18:27 +01001403
1404
1405static int
1406parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1407 struct flt_conf *fconf, char **err, void *private)
1408{
1409 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001410 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001411 char *name = NULL;
1412 int pos = *cur_arg;
1413
1414 /* Get the cache filter name*/
1415 if (!strcmp(args[pos], "cache")) {
1416 if (!*args[pos + 1]) {
1417 memprintf(err, "%s : expects an <id> argument", args[pos]);
1418 goto error;
1419 }
1420 name = strdup(args[pos + 1]);
1421 if (!name) {
1422 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1423 goto error;
1424 }
1425 pos += 2;
1426 }
1427
1428 /* Check if an implicit filter with the same name already exists. If so,
1429 * we remove the implicit filter to use the explicit one. */
1430 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1431 if (f->id != cache_store_flt_id)
1432 continue;
1433
1434 cconf = f->conf;
1435 if (strcmp(name, cconf->c.name)) {
1436 cconf = NULL;
1437 continue;
1438 }
1439
1440 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1441 cconf = NULL;
1442 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1443 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001444 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001445 }
1446
1447 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1448 LIST_DEL(&f->list);
1449 free(f);
1450 free(name);
1451 break;
1452 }
1453
1454 /* No implicit cache filter found, create configuration for the explicit one */
1455 if (!cconf) {
1456 cconf = calloc(1, sizeof(*cconf));
1457 if (!cconf) {
1458 memprintf(err, "%s: out of memory", args[*cur_arg]);
1459 goto error;
1460 }
1461 cconf->c.name = name;
1462 }
1463
1464 cconf->flags = 0;
1465 fconf->id = cache_store_flt_id;
1466 fconf->conf = cconf;
1467 fconf->ops = &cache_ops;
1468
1469 *cur_arg = pos;
1470 return 0;
1471
1472 error:
1473 free(name);
1474 free(cconf);
1475 return -1;
1476}
1477
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001478static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001479{
1480 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1481 return 1;
1482
1483 return 0;
1484}
1485
1486static int cli_io_handler_show_cache(struct appctx *appctx)
1487{
1488 struct cache* cache = appctx->ctx.cli.p0;
1489 struct stream_interface *si = appctx->owner;
1490
William Lallemand1f49a362017-11-21 20:01:26 +01001491 if (cache == NULL) {
1492 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1493 }
1494
1495 list_for_each_entry_from(cache, &caches, list) {
1496 struct eb32_node *node = NULL;
1497 unsigned int next_key;
1498 struct cache_entry *entry;
1499
William Lallemand1f49a362017-11-21 20:01:26 +01001500 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001501 if (!next_key) {
1502 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1503 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001504 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001505 return 0;
1506 }
1507 }
William Lallemand1f49a362017-11-21 20:01:26 +01001508
1509 appctx->ctx.cli.p0 = cache;
1510
1511 while (1) {
1512
1513 shctx_lock(shctx_ptr(cache));
1514 node = eb32_lookup_ge(&cache->entries, next_key);
1515 if (!node) {
1516 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001517 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001518 break;
1519 }
1520
1521 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001522 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 +01001523
1524 next_key = node->key + 1;
1525 appctx->ctx.cli.i0 = next_key;
1526
1527 shctx_unlock(shctx_ptr(cache));
1528
1529 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001530 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001531 return 0;
1532 }
1533 }
1534
1535 }
1536
1537 return 1;
1538
1539}
1540
Christopher Faulet99a17a22018-12-11 09:18:27 +01001541/* Declare the filter parser for "cache" keyword */
1542static struct flt_kw_list filter_kws = { "CACHE", { }, {
1543 { "cache", parse_cache_flt, NULL },
1544 { NULL, NULL, NULL },
1545 }
1546};
1547
1548INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1549
William Lallemand1f49a362017-11-21 20:01:26 +01001550static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001551 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1552 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001553}};
1554
Willy Tarreau0108d902018-11-25 19:14:37 +01001555INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001556
William Lallemand41db4602017-10-30 11:15:51 +01001557static struct action_kw_list http_res_actions = {
1558 .kw = {
1559 { "cache-store", parse_cache_store },
1560 { NULL, NULL }
1561 }
1562};
1563
Willy Tarreau0108d902018-11-25 19:14:37 +01001564INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1565
William Lallemand41db4602017-10-30 11:15:51 +01001566static struct action_kw_list http_req_actions = {
1567 .kw = {
1568 { "cache-use", parse_cache_use },
1569 { NULL, NULL }
1570 }
1571};
1572
Willy Tarreau0108d902018-11-25 19:14:37 +01001573INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1574
Willy Tarreau2231b632019-03-29 18:26:52 +01001575struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001576 .obj_type = OBJ_TYPE_APPLET,
1577 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001578 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001579 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001580};
1581
Willy Tarreaue6552512018-11-26 11:33:13 +01001582/* config parsers for this section */
1583REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001584REGISTER_POST_CHECK(post_check_cache);