blob: 80bc545d4db1b22b737b788a292183f2134c345a [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 Tarreau87735332020-06-04 09:08:41 +020017#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020018#include <haproxy/http_rules.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020019#include <haproxy/shctx.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020020#include <haproxy/stream_interface.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020021#include <import/eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010022#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010023
William Lallemand75d93292017-11-21 20:01:24 +010024#include <types/filters.h>
25#include <types/proxy.h>
William Lallemand75d93292017-11-21 20:01:24 +010026
William Lallemand41db4602017-10-30 11:15:51 +010027#include <proto/proxy.h>
William Lallemand41db4602017-10-30 11:15:51 +010028#include <proto/filters.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020029#include <proto/http_ana.h>
William Lallemand41db4602017-10-30 11:15:51 +010030#include <proto/log.h>
31#include <proto/stream.h>
William Lallemand41db4602017-10-30 11:15:51 +010032
William Lallemand41db4602017-10-30 11:15:51 +010033
34#include <common/cfgparse.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020035#include <haproxy/hash.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020036#include <haproxy/htx.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020037#include <haproxy/net_helper.h>
William Lallemand41db4602017-10-30 11:15:51 +010038
Christopher Faulet27d93c32018-12-15 22:32:02 +010039#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010040 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010041
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010042const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010043
Willy Tarreau2231b632019-03-29 18:26:52 +010044extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010045
46struct flt_ops cache_ops;
47
48struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010049 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010050 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 unsigned int maxage; /* max-age */
52 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020053 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010054 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010055};
56
Christopher Faulet95220e22018-12-07 17:34:39 +010057/* cache config for filters */
58struct cache_flt_conf {
59 union {
60 struct cache *cache; /* cache used by the filter */
61 char *name; /* cache name used during conf parsing */
62 } c;
63 unsigned int flags; /* CACHE_FLT_F_* */
64};
65
William Lallemand41db4602017-10-30 11:15:51 +010066/*
67 * cache ctx for filters
68 */
69struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010070 struct shared_block *first_block;
71};
72
73struct cache_entry {
74 unsigned int latest_validation; /* latest validation date */
75 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020076 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010077
William Lallemand41db4602017-10-30 11:15:51 +010078 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010079 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010080 unsigned char data[0];
81};
82
83#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010084#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010085
86static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020087static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010088static struct cache *tmp_cache_config = NULL;
89
Willy Tarreau8ceae722018-11-26 11:58:30 +010090DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
91
William Lallemandf528fff2017-11-23 19:43:17 +010092struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010093{
94 struct eb32_node *node;
95 struct cache_entry *entry;
96
Willy Tarreau8b507582020-02-25 09:35:07 +010097 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010098 if (!node)
99 return NULL;
100
101 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100102
103 /* if that's not the right node */
104 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
105 return NULL;
106
William Lallemand08727662017-11-21 20:01:27 +0100107 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100108 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100109 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100110 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100111 entry->eb.key = 0;
112 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100113 return NULL;
114
115}
116
117static inline struct shared_context *shctx_ptr(struct cache *cache)
118{
119 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
120}
121
William Lallemand77c11972017-10-31 20:43:01 +0100122static inline struct shared_block *block_ptr(struct cache_entry *entry)
123{
124 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
125}
126
127
128
William Lallemand41db4602017-10-30 11:15:51 +0100129static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100130cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100131{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100132 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100133 return 0;
134}
135
Christopher Faulet95220e22018-12-07 17:34:39 +0100136static void
137cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
138{
139 struct cache_flt_conf *cconf = fconf->conf;
140
141 free(cconf);
142}
143
William Lallemand4da3f8a2017-10-31 14:33:34 +0100144static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100145cache_store_check(struct proxy *px, struct flt_conf *fconf)
146{
147 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100148 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100149 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100150 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100151
William Lallemandd1d1e222019-08-28 15:22:49 +0200152 /* Find the cache corresponding to the name in the filter config. The
153 * cache will not be referenced now in the filter config because it is
154 * not fully allocated. This step will be performed during the cache
155 * post_check.
156 */
157 list_for_each_entry(cache, &caches_config, list) {
158 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100159 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100160 }
161
162 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
163 proxy_type_str(px), px->id, (char *)cconf->c.name);
164 return 1;
165
166 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100167 /* Here <cache> points on the cache the filter must use and <cconf>
168 * points on the cache filter configuration. */
169
170 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100171 * enabled and if it is after the cache. When the compression is before
172 * the cache, an error is returned. Also check if the cache filter must
173 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100174 list_for_each_entry(f, &px->filter_configs, list) {
175 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100176 /* The compression filter must be evaluated after the cache. */
177 if (comp) {
178 ha_alert("config: %s '%s': unable to enable the compression filter before "
179 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
180 return 1;
181 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100182 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200183 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100184 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200185 else if (f->id == fcgi_flt_id)
186 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100187 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
188 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200189 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100190 * declaration is required. */
191 ha_alert("config: %s '%s': require an explicit filter declaration "
192 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
193 return 1;
194 }
195
Christopher Fauletafd819c2018-12-11 08:57:45 +0100196 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100197 return 0;
198}
199
200static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100201cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100202{
Christopher Faulet65554e12020-03-06 14:52:06 +0100203 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100204
Christopher Faulet65554e12020-03-06 14:52:06 +0100205 st = pool_alloc_dirty(pool_head_cache_st);
206 if (st == NULL)
207 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100208
Christopher Faulet65554e12020-03-06 14:52:06 +0100209 st->first_block = NULL;
210 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100211
Christopher Faulet65554e12020-03-06 14:52:06 +0100212 /* Register post-analyzer on AN_RES_WAIT_HTTP */
213 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100214 return 1;
215}
216
Christopher Faulet65554e12020-03-06 14:52:06 +0100217static void
218cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100219{
220 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100221 struct cache_flt_conf *cconf = FLT_CONF(filter);
222 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100223 struct shared_context *shctx = shctx_ptr(cache);
224
William Lallemand49dc0482017-11-24 14:33:54 +0100225 /* Everything should be released in the http_end filter, but we need to do it
226 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100227 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100228 shctx_lock(shctx);
229 shctx_row_dec_hot(shctx, st->first_block);
230 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100231 }
232 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100233 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100234 filter->ctx = NULL;
235 }
William Lallemand49dc0482017-11-24 14:33:54 +0100236}
237
Christopher Faulet839791a2019-01-07 16:12:07 +0100238static int
239cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
240 unsigned an_bit)
241{
242 struct http_txn *txn = s->txn;
243 struct http_msg *msg = &txn->rsp;
244 struct cache_st *st = filter->ctx;
245
246 if (an_bit != AN_RES_WAIT_HTTP)
247 goto end;
248
249 /* Here we need to check if any compression filter precedes the cache
250 * filter. This is only possible when the compression is configured in
251 * the frontend while the cache filter is configured on the
252 * backend. This case cannot be detected during HAProxy startup. So in
253 * such cases, the cache is disabled.
254 */
255 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
256 pool_free(pool_head_cache_st, st);
257 filter->ctx = NULL;
258 }
259
260 end:
261 return 1;
262}
William Lallemand49dc0482017-11-24 14:33:54 +0100263
264static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100265cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
266{
267 struct cache_st *st = filter->ctx;
268
William Lallemand4da3f8a2017-10-31 14:33:34 +0100269 if (!(msg->chn->flags & CF_ISRESP) || !st)
270 return 1;
271
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200272 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100273 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100274 return 1;
275}
276
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200277static inline void disable_cache_entry(struct cache_st *st,
278 struct filter *filter, struct shared_context *shctx)
279{
280 struct cache_entry *object;
281
282 object = (struct cache_entry *)st->first_block->data;
283 filter->ctx = NULL; /* disable cache */
284 shctx_lock(shctx);
285 shctx_row_dec_hot(shctx, st->first_block);
286 object->eb.key = 0;
287 shctx_unlock(shctx);
288 pool_free(pool_head_cache_st, st);
289}
290
William Lallemand4da3f8a2017-10-31 14:33:34 +0100291static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100292cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
293 unsigned int offset, unsigned int len)
294{
Christopher Faulet95220e22018-12-07 17:34:39 +0100295 struct cache_flt_conf *cconf = FLT_CONF(filter);
296 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100297 struct cache_st *st = filter->ctx;
298 struct htx *htx = htxbuf(&msg->chn->buf);
299 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200300 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100301 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200302 unsigned int orig_len, to_forward;
303 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100304
305 if (!len)
306 return len;
307
308 if (!st->first_block) {
309 unregister_data_filter(s, msg->chn, filter);
310 return len;
311 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100312
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200313 chunk_reset(&trash);
314 orig_len = len;
315 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100316
317 htxret = htx_find_offset(htx, offset);
318 blk = htxret.blk;
319 offset = htxret.ret;
320 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100321 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200322 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100323 struct ist v;
324
325 switch (type) {
326 case HTX_BLK_UNUSED:
327 break;
328
329 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100330 v = htx_get_blk_value(htx, blk);
331 v.ptr += offset;
332 v.len -= offset;
333 if (v.len > len)
334 v.len = len;
335
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200336 info = (type << 28) + v.len;
337 chunk_memcat(&trash, (char *)&info, sizeof(info));
338 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100339 to_forward += v.len;
340 len -= v.len;
341 break;
342
343 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200344 /* Here offset must always be 0 because only
345 * DATA blocks can be partially transferred. */
346 if (offset)
347 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100348 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200349 goto end;
350
351 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
352 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100353 to_forward += sz;
354 len -= sz;
355 break;
356 }
357
358 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100359 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200360
361 end:
362 shctx_lock(shctx);
363 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
364 if (!fb) {
365 shctx_unlock(shctx);
366 goto no_cache;
367 }
368 shctx_unlock(shctx);
369
370 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
371 (unsigned char *)b_head(&trash), b_data(&trash));
372 if (ret < 0)
373 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100374
375 return to_forward;
376
377 no_cache:
378 disable_cache_entry(st, filter, shctx);
379 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200380 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100381}
382
383static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100384cache_store_http_end(struct stream *s, struct filter *filter,
385 struct http_msg *msg)
386{
387 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100388 struct cache_flt_conf *cconf = FLT_CONF(filter);
389 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100390 struct shared_context *shctx = shctx_ptr(cache);
391 struct cache_entry *object;
392
393 if (!(msg->chn->flags & CF_ISRESP))
394 return 1;
395
396 if (st && st->first_block) {
397
398 object = (struct cache_entry *)st->first_block->data;
399
400 /* does not need to test if the insertion worked, if it
401 * doesn't, the blocks will be reused anyway */
402
403 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100404 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
405 object->eb.key = 0;
406 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100407 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100408 shctx_row_dec_hot(shctx, st->first_block);
409 shctx_unlock(shctx);
410
411 }
412 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100413 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100414 filter->ctx = NULL;
415 }
416
417 return 1;
418}
419
420 /*
421 * This intends to be used when checking HTTP headers for some
422 * word=value directive. Return a pointer to the first character of value, if
423 * the word was not found or if there wasn't any value assigned ot it return NULL
424 */
425char *directive_value(const char *sample, int slen, const char *word, int wlen)
426{
427 int st = 0;
428
429 if (slen < wlen)
430 return 0;
431
432 while (wlen) {
433 char c = *sample ^ *word;
434 if (c && c != ('A' ^ 'a'))
435 return NULL;
436 sample++;
437 word++;
438 slen--;
439 wlen--;
440 }
441
442 while (slen) {
443 if (st == 0) {
444 if (*sample != '=')
445 return NULL;
446 sample++;
447 slen--;
448 st = 1;
449 continue;
450 } else {
451 return (char *)sample;
452 }
453 }
454
455 return NULL;
456}
457
458/*
459 * Return the maxage in seconds of an HTTP response.
460 * Compute the maxage using either:
461 * - the assigned max-age of the cache
462 * - the s-maxage directive
463 * - the max-age directive
464 * - (Expires - Data) headers
465 * - the default-max-age of the cache
466 *
467 */
William Lallemand49b44532017-11-24 18:53:43 +0100468int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100469{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200470 struct htx *htx = htxbuf(&s->res.buf);
471 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100472 int smaxage = -1;
473 int maxage = -1;
474
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200475 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
476 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100477
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200478 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
479 if (value) {
480 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100481
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200482 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
483 chunk_strncat(chk, "", 1);
484 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485 }
486
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200487 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
488 if (value) {
489 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200490
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200491 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
492 chunk_strncat(chk, "", 1);
493 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100494 }
495 }
496
497 /* TODO: Expires - Data */
498
499
500 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100501 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502
503 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100504 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100505
William Lallemand49b44532017-11-24 18:53:43 +0100506 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100507
508}
509
510
William Lallemanda400a3a2017-11-20 19:13:12 +0100511static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
512{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200513 struct cache_entry *object = (struct cache_entry *)block->data;
514
515 if (first == block && object->eb.key)
516 eb32_delete(&object->eb);
517 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100518}
519
William Lallemand41db4602017-10-30 11:15:51 +0100520/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500521 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100522 * register a filter to store the data
523 */
524enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200525 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100526{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200527 unsigned int age;
528 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100529 struct http_txn *txn = s->txn;
530 struct http_msg *msg = &txn->rsp;
531 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100532 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100533 struct cache_flt_conf *cconf = rule->arg.act.p[0];
534 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100535 struct cache_st *cache_ctx = NULL;
536 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100537 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200538 struct htx *htx;
539 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200540 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200541 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100542
William Lallemand4da3f8a2017-10-31 14:33:34 +0100543 /* Don't cache if the response came from a cache */
544 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
545 s->target == &http_cache_applet.obj_type) {
546 goto out;
547 }
548
549 /* cache only HTTP/1.1 */
550 if (!(txn->req.flags & HTTP_MSGF_VER_11))
551 goto out;
552
Willy Tarreau6905d182019-10-01 17:59:17 +0200553 /* cache only GET method */
554 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100555 goto out;
556
Willy Tarreauc9036c02019-01-11 19:38:25 +0100557 /* cache key was not computed */
558 if (!key)
559 goto out;
560
William Lallemand4da3f8a2017-10-31 14:33:34 +0100561 /* cache only 200 status code */
562 if (txn->status != 200)
563 goto out;
564
Christopher Faulet839791a2019-01-07 16:12:07 +0100565 /* Find the corresponding filter instance for the current stream */
566 list_for_each_entry(filter, &s->strm_flt.filters, list) {
567 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
568 /* No filter ctx, don't cache anything */
569 if (!filter->ctx)
570 goto out;
571 cache_ctx = filter->ctx;
572 break;
573 }
574 }
575
576 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200577 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100578
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200579 /* Do not cache too big objects. */
580 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
581 htx->data + htx->extra > shctx->max_obj_size)
582 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100583
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200584 /* Does not manage Vary at the moment. We will need a secondary key later for that */
585 ctx.blk = NULL;
586 if (http_find_header(htx, ist("Vary"), &ctx, 0))
587 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100588
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200589 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100590
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200591 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
592 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100593
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200594 age = 0;
595 ctx.blk = NULL;
596 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
597 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
598 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
599 hdr_age = CACHE_ENTRY_MAX_AGE;
600 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100601 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200602 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100603 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100604
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200605 chunk_reset(&trash);
606 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
607 struct htx_blk *blk = htx_get_blk(htx, pos);
608 enum htx_blk_type type = htx_get_blk_type(blk);
609 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100610
Christopher Fauletb0667472019-09-03 22:22:12 +0200611 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200612 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
613 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
614 if (type == HTX_BLK_EOH)
615 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200616 }
617
Christopher Fauletb0667472019-09-03 22:22:12 +0200618 /* Do not cache objects if the headers are too big. */
619 if (hdrs_len > htx->size - global.tune.maxrewrite)
620 goto out;
621
William Lallemand4da3f8a2017-10-31 14:33:34 +0100622 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200623 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100624 if (!first) {
625 shctx_unlock(shctx);
626 goto out;
627 }
628 shctx_unlock(shctx);
629
Willy Tarreau1093a452018-04-06 19:02:25 +0200630 /* the received memory is not initialized, we need at least to mark
631 * the object as not indexed yet.
632 */
633 object = (struct cache_entry *)first->data;
634 object->eb.node.leaf_p = NULL;
635 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200636 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200637
William Lallemand4da3f8a2017-10-31 14:33:34 +0100638 /* reserve space for the cache_entry structure */
639 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200640 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100641 /* cache the headers in a http action because it allows to chose what
642 * to cache, for example you might want to cache a response before
643 * modifying some HTTP headers, or on the contrary after modifying
644 * those headers.
645 */
646
647 /* does not need to be locked because it's in the "hot" list,
648 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200649 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
650 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100651
652 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100653 if (cache_ctx) {
654 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100655
Willy Tarreauc9036c02019-01-11 19:38:25 +0100656 object->eb.key = key;
657
Christopher Faulet839791a2019-01-07 16:12:07 +0100658 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
659 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100660
Christopher Faulet839791a2019-01-07 16:12:07 +0100661 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100662
Christopher Faulet839791a2019-01-07 16:12:07 +0100663 old = entry_exist(cconf->c.cache, txn->cache_hash);
664 if (old) {
665 eb32_delete(&old->eb);
666 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100667 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100668 shctx_unlock(shctx);
669
670 /* store latest value and expiration time */
671 object->latest_validation = now.tv_sec;
672 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
673 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100674 }
675
676out:
677 /* if does not cache */
678 if (first) {
679 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100680 first->len = 0;
681 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100682 shctx_row_dec_hot(shctx, first);
683 shctx_unlock(shctx);
684 }
685
William Lallemand41db4602017-10-30 11:15:51 +0100686 return ACT_RET_CONT;
687}
688
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100689#define HTX_CACHE_INIT 0 /* Initial state. */
690#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
691#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200692#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
693#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100694
William Lallemandecb73b12017-11-24 14:33:55 +0100695static void http_cache_applet_release(struct appctx *appctx)
696{
Christopher Faulet95220e22018-12-07 17:34:39 +0100697 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100698 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100699 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100700 struct shared_block *first = block_ptr(cache_ptr);
701
702 shctx_lock(shctx_ptr(cache));
703 shctx_row_dec_hot(shctx_ptr(cache), first);
704 shctx_unlock(shctx_ptr(cache));
705}
706
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200707
708static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
709 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100710{
Christopher Faulet95220e22018-12-07 17:34:39 +0100711 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
712 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200713 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200714 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200715 unsigned int max, total;
716 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100717
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200718 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
719 if (!max)
720 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200721 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200722 ? (info & 0xff) + ((info >> 8) & 0xfffff)
723 : info & 0xfffffff);
724 if (blksz > max)
725 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100726
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200727 blk = htx_add_blk(htx, type, blksz);
728 if (!blk)
729 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100730
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200731 blk->info = info;
732 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200733 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200734 while (blksz) {
735 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200736 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200737 offset += max;
738 blksz -= max;
739 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200740 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200741 if (blksz || offset == shctx->block_size) {
742 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
743 offset = 0;
744 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100745 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200746 appctx->ctx.cache.offset = offset;
747 appctx->ctx.cache.next = shblk;
748 appctx->ctx.cache.sent += total;
749 return total;
750}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100751
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200752static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
753 uint32_t info, struct shared_block *shblk, unsigned int offset)
754{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100755
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200756 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
757 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
758 unsigned int max, total, rem_data;
759 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100760
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200761 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
762 if (!max)
763 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100764
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200765 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200766 if (appctx->ctx.cache.rem_data) {
767 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200768 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200769 }
770 else {
771 blksz = (info & 0xfffffff);
772 total = 4;
773 }
774 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200775 rem_data = blksz - max;
776 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100777 }
778
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200779 while (blksz) {
780 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100781
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200782 max = MIN(blksz, shctx->block_size - offset);
783 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
784 offset += sz;
785 blksz -= sz;
786 total += sz;
787 if (sz < max)
788 break;
789 if (blksz || offset == shctx->block_size) {
790 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
791 offset = 0;
792 }
793 }
794
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200795 appctx->ctx.cache.offset = offset;
796 appctx->ctx.cache.next = shblk;
797 appctx->ctx.cache.sent += total;
798 appctx->ctx.cache.rem_data = rem_data + blksz;
799 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100800}
801
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200802static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
803 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100804{
Christopher Faulet95220e22018-12-07 17:34:39 +0100805 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
806 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200807 struct shared_block *shblk;
808 unsigned int offset, sz;
809 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100810
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200811 while (len) {
812 enum htx_blk_type type;
813 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100814
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200815 shblk = appctx->ctx.cache.next;
816 offset = appctx->ctx.cache.offset;
817 if (appctx->ctx.cache.rem_data) {
818 type = HTX_BLK_DATA;
819 info = 0;
820 goto add_data_blk;
821 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100822
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500823 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200824 sz = MIN(4, shctx->block_size - offset);
825 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
826 offset += sz;
827 if (sz < 4) {
828 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
829 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
830 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100831 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200832
833 /* Get payload of the next HTX block and insert it. */
834 type = (info >> 28);
835 if (type != HTX_BLK_DATA)
836 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
837 else {
838 add_data_blk:
839 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100840 }
841
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200842 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100843 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200844 total += ret;
845 len -= ret;
846
847 if (appctx->ctx.cache.rem_data || type == mark)
848 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100849 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100850
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100851 return total;
852}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200853
854static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
855{
856 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
857 unsigned int age;
858 char *end;
859
860 chunk_reset(&trash);
861 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
862 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
863 age = CACHE_ENTRY_MAX_AGE;
864 end = ultoa_o(age, b_head(&trash), b_size(&trash));
865 b_set_data(&trash, end - b_head(&trash));
866 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
867 return 0;
868 return 1;
869}
870
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200871static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100872{
873 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
874 struct shared_block *first = block_ptr(cache_ptr);
875 struct stream_interface *si = appctx->owner;
876 struct channel *req = si_oc(si);
877 struct channel *res = si_ic(si);
878 struct htx *req_htx, *res_htx;
879 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200880 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100881 size_t ret, total = 0;
882
883 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200884 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100885
886 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
887 goto out;
888
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500889 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100890 if (!b_size(&res->buf)) {
891 si_rx_room_blk(si);
892 goto out;
893 }
894
Willy Tarreauefef3232018-12-16 00:37:45 +0100895 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100896 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100897
898 if (appctx->st0 == HTX_CACHE_INIT) {
899 appctx->ctx.cache.next = block_ptr(cache_ptr);
900 appctx->ctx.cache.offset = sizeof(*cache_ptr);
901 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200902 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100903 appctx->st0 = HTX_CACHE_HEADER;
904 }
905
906 if (appctx->st0 == HTX_CACHE_HEADER) {
907 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200908 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
909 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
910 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
911 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100912 goto error;
913
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200914 /* Skip response body for HEAD requests */
915 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100916 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100917 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200918 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100919 }
920
921 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200922 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
923 if (len) {
924 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
925 if (ret < len) {
926 si_rx_room_blk(si);
927 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100928 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100929 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200930 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100931 }
932
933 if (appctx->st0 == HTX_CACHE_EOM) {
934 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
935 si_rx_room_blk(si);
936 goto out;
937 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100938 appctx->st0 = HTX_CACHE_END;
939 }
940
941 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100942 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100943 res->flags |= CF_READ_NULL;
944 si_shutr(si);
945 }
946
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100947 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200948 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100949 if (total)
950 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100951 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100952
953 /* eat the whole request */
954 if (co_data(req)) {
955 req_htx = htx_from_buf(&req->buf);
956 co_htx_skip(req, req_htx, co_data(req));
957 htx_to_buf(req_htx, &req->buf);
958 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100959 return;
960
961 error:
962 /* Sent and HTTP error 500 */
963 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200964 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100965 res->buf.data = b_data(errmsg);
966 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
967 res_htx = htx_from_buf(&res->buf);
968
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200969 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100970 appctx->st0 = HTX_CACHE_END;
971 goto end;
972}
973
974
Christopher Faulet95220e22018-12-07 17:34:39 +0100975static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100976{
977 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100978 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100979
Christopher Faulet95220e22018-12-07 17:34:39 +0100980 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100981 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100982 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100983 }
984
985 /* check if a cache filter was already registered with this cache
986 * name, if that's the case, must use it. */
987 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100988 if (fconf->id == cache_store_flt_id) {
989 cconf = fconf->conf;
990 if (cconf && !strcmp((char *)cconf->c.name, name)) {
991 rule->arg.act.p[0] = cconf;
992 return 1;
993 }
William Lallemand41db4602017-10-30 11:15:51 +0100994 }
995 }
996
Christopher Faulet95220e22018-12-07 17:34:39 +0100997 /* Create the filter cache config */
998 cconf = calloc(1, sizeof(*cconf));
999 if (!cconf) {
1000 memprintf(err, "out of memory\n");
1001 goto err;
1002 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001003 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001004 cconf->c.name = strdup(name);
1005 if (!cconf->c.name) {
1006 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001007 goto err;
1008 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001009
William Lallemand41db4602017-10-30 11:15:51 +01001010 /* register a filter to fill the cache buffer */
1011 fconf = calloc(1, sizeof(*fconf));
1012 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001013 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001014 goto err;
1015 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001016 fconf->id = cache_store_flt_id;
1017 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001018 fconf->ops = &cache_ops;
1019 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1020
Christopher Faulet95220e22018-12-07 17:34:39 +01001021 rule->arg.act.p[0] = cconf;
1022 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001023
Christopher Faulet95220e22018-12-07 17:34:39 +01001024 err:
1025 free(cconf);
1026 return 0;
1027}
1028
1029enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1030 struct act_rule *rule, char **err)
1031{
1032 rule->action = ACT_CUSTOM;
1033 rule->action_ptr = http_action_store_cache;
1034
1035 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1036 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001037
Christopher Faulet95220e22018-12-07 17:34:39 +01001038 (*orig_arg)++;
1039 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001040}
1041
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001042/* This produces a sha1 hash of the concatenation of the HTTP method,
1043 * the first occurrence of the Host header followed by the path component
1044 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001045int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001046{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001047 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001048 struct htx *htx = htxbuf(&s->req.buf);
1049 struct htx_sl *sl;
1050 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001051 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001052 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001053 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001054
William Lallemandf528fff2017-11-23 19:43:17 +01001055 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001056 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001057
1058 switch (txn->meth) {
1059 case HTTP_METH_HEAD:
1060 case HTTP_METH_GET:
1061 chunk_memcat(trash, "GET", 3);
1062 break;
1063 default:
1064 return 0;
1065 }
1066
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001067 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001068 uri = htx_sl_req_uri(sl); // whole uri
1069 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001070 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001071
1072 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1073 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1074 * URIs are almost always sent in absolute form with their scheme. In
1075 * this case, the scheme is almost always "https". In order to support
1076 * sharing of cache objects between H1 and H2, we'll hash the absolute
1077 * URI whenever known, or prepend "https://" + the Host header for
1078 * relative URIs. The difference will only appear on absolute HTTP/1
1079 * requests sent to an origin server, which practically is never met in
1080 * the real world so we don't care about the ability to share the same
1081 * key here.URIs are normalized from the absolute URI to an origin form as
1082 * well.
1083 */
1084 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001085 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001086 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1087 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001088 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001089 }
1090
1091 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001092
1093 /* hash everything */
1094 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001095 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001096 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1097
1098 return 1;
1099}
1100
William Lallemand41db4602017-10-30 11:15:51 +01001101enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1102 struct session *sess, struct stream *s, int flags)
1103{
William Lallemand77c11972017-10-31 20:43:01 +01001104
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001105 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001106 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001107 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1108 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001109
Willy Tarreau6905d182019-10-01 17:59:17 +02001110 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1111 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001112 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001113 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001114 txn->flags |= TX_CACHE_IGNORE;
1115
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001116 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001117
Willy Tarreau504455c2017-12-22 17:47:35 +01001118 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1119 return ACT_RET_CONT;
1120
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001121 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001122 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001123
Willy Tarreau504455c2017-12-22 17:47:35 +01001124 if (s->txn->flags & TX_CACHE_IGNORE)
1125 return ACT_RET_CONT;
1126
Willy Tarreaua1214a52018-12-14 14:00:25 +01001127 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001128 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001129 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001130 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001131
William Lallemanda400a3a2017-11-20 19:13:12 +01001132 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001133 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001134 if (res) {
1135 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001136 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1137 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001138 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001139 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001140 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001141 appctx->rule = rule;
1142 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001143 appctx->ctx.cache.next = NULL;
1144 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001145
1146 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001147 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001148 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001149 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001150 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001151 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001152 shctx_lock(shctx_ptr(cache));
1153 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1154 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001155 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001156 }
1157 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001158 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001159 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001160}
1161
1162
1163enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1164 struct act_rule *rule, char **err)
1165{
William Lallemand41db4602017-10-30 11:15:51 +01001166 rule->action = ACT_CUSTOM;
1167 rule->action_ptr = http_action_req_cache_use;
1168
Christopher Faulet95220e22018-12-07 17:34:39 +01001169 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001170 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001171
1172 (*orig_arg)++;
1173 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001174}
1175
1176int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1177{
1178 int err_code = 0;
1179
1180 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1181
1182 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001183 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1184 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001185 err_code |= ERR_ALERT | ERR_ABORT;
1186 goto out;
1187 }
1188
1189 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1190 err_code |= ERR_ABORT;
1191 goto out;
1192 }
1193
1194 if (tmp_cache_config == NULL) {
1195 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1196 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001197 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001198 err_code |= ERR_ALERT | ERR_ABORT;
1199 goto out;
1200 }
1201
1202 strlcpy2(tmp_cache_config->id, args[1], 33);
1203 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001204 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1205 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001206 err_code |= ERR_WARN;
1207 }
William Lallemand49b44532017-11-24 18:53:43 +01001208 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001209 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001210 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001211 }
1212 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001213 unsigned long int maxsize;
1214 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001215
1216 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1217 err_code |= ERR_ABORT;
1218 goto out;
1219 }
1220
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001221 maxsize = strtoul(args[1], &err, 10);
1222 if (err == args[1] || *err != '\0') {
1223 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1224 file, linenum, args[1]);
1225 err_code |= ERR_ABORT;
1226 goto out;
1227 }
1228
1229 if (maxsize > (UINT_MAX >> 20)) {
1230 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1231 file, linenum, args[1], UINT_MAX >> 20);
1232 err_code |= ERR_ABORT;
1233 goto out;
1234 }
1235
William Lallemand41db4602017-10-30 11:15:51 +01001236 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001237 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001238 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001239 } else if (strcmp(args[0], "max-age") == 0) {
1240 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1241 err_code |= ERR_ABORT;
1242 goto out;
1243 }
1244
1245 if (!*args[1]) {
1246 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1247 file, linenum, args[0]);
1248 err_code |= ERR_WARN;
1249 }
1250
1251 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001252 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001253 unsigned int maxobjsz;
1254 char *err;
1255
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001256 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1257 err_code |= ERR_ABORT;
1258 goto out;
1259 }
1260
1261 if (!*args[1]) {
1262 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1263 file, linenum, args[0]);
1264 err_code |= ERR_WARN;
1265 }
1266
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001267 maxobjsz = strtoul(args[1], &err, 10);
1268 if (err == args[1] || *err != '\0') {
1269 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1270 file, linenum, args[1]);
1271 err_code |= ERR_ABORT;
1272 goto out;
1273 }
1274 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001275 }
1276 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001277 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001278 err_code |= ERR_ALERT | ERR_FATAL;
1279 goto out;
1280 }
1281out:
1282 return err_code;
1283}
1284
1285/* once the cache section is parsed */
1286
1287int cfg_post_parse_section_cache()
1288{
William Lallemand41db4602017-10-30 11:15:51 +01001289 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001290
1291 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001292
1293 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001294 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001295 err_code |= ERR_FATAL | ERR_ALERT;
1296 goto out;
1297 }
1298
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001299 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001300 /* Default max. file size is a 256th of the cache size. */
1301 tmp_cache_config->maxobjsz =
1302 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001303 }
1304 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1305 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1306 err_code |= ERR_FATAL | ERR_ALERT;
1307 goto out;
1308 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001309
William Lallemandd1d1e222019-08-28 15:22:49 +02001310 /* add to the list of cache to init and reinit tmp_cache_config
1311 * for next cache section, if any.
1312 */
1313 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1314 tmp_cache_config = NULL;
1315 return err_code;
1316 }
1317out:
1318 free(tmp_cache_config);
1319 tmp_cache_config = NULL;
1320 return err_code;
1321
1322}
1323
1324int post_check_cache()
1325{
1326 struct proxy *px;
1327 struct cache *back, *cache_config, *cache;
1328 struct shared_context *shctx;
1329 int ret_shctx;
1330 int err_code = 0;
1331
1332 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1333
1334 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1335 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001336
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001337 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001338 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001339 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001340 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001341 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001342
1343 err_code |= ERR_FATAL | ERR_ALERT;
1344 goto out;
1345 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001346 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001347 /* the cache structure is stored in the shctx and added to the
1348 * caches list, we can remove the entry from the caches_config
1349 * list */
1350 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001351 cache = (struct cache *)shctx->data;
1352 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001353 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001354 LIST_DEL(&cache_config->list);
1355 free(cache_config);
1356
1357 /* Find all references for this cache in the existing filters
1358 * (over all proxies) and reference it in matching filters.
1359 */
1360 for (px = proxies_list; px; px = px->next) {
1361 struct flt_conf *fconf;
1362 struct cache_flt_conf *cconf;
1363
1364 list_for_each_entry(fconf, &px->filter_configs, list) {
1365 if (fconf->id != cache_store_flt_id)
1366 continue;
1367
1368 cconf = fconf->conf;
1369 if (!strcmp(cache->id, cconf->c.name)) {
1370 free(cconf->c.name);
1371 cconf->c.cache = cache;
1372 break;
1373 }
1374 }
1375 }
William Lallemand41db4602017-10-30 11:15:51 +01001376 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001377
William Lallemand41db4602017-10-30 11:15:51 +01001378out:
William Lallemand41db4602017-10-30 11:15:51 +01001379 return err_code;
1380
William Lallemand41db4602017-10-30 11:15:51 +01001381}
1382
William Lallemand41db4602017-10-30 11:15:51 +01001383struct flt_ops cache_ops = {
1384 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001385 .check = cache_store_check,
1386 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001387
Christopher Faulet65554e12020-03-06 14:52:06 +01001388 /* Handle stream init/deinit */
1389 .attach = cache_store_strm_init,
1390 .detach = cache_store_strm_deinit,
1391
William Lallemand4da3f8a2017-10-31 14:33:34 +01001392 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001393 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001394
1395 /* Filter HTTP requests and responses */
1396 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001397 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001398 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001399};
1400
Christopher Faulet99a17a22018-12-11 09:18:27 +01001401
1402
1403static int
1404parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1405 struct flt_conf *fconf, char **err, void *private)
1406{
1407 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001408 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001409 char *name = NULL;
1410 int pos = *cur_arg;
1411
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001412 /* Get the cache filter name. <pos> point on "cache" keyword */
1413 if (!*args[pos + 1]) {
1414 memprintf(err, "%s : expects an <id> argument", args[pos]);
1415 goto error;
1416 }
1417 name = strdup(args[pos + 1]);
1418 if (!name) {
1419 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1420 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001421 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001422 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001423
1424 /* Check if an implicit filter with the same name already exists. If so,
1425 * we remove the implicit filter to use the explicit one. */
1426 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1427 if (f->id != cache_store_flt_id)
1428 continue;
1429
1430 cconf = f->conf;
1431 if (strcmp(name, cconf->c.name)) {
1432 cconf = NULL;
1433 continue;
1434 }
1435
1436 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1437 cconf = NULL;
1438 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1439 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001440 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001441 }
1442
1443 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1444 LIST_DEL(&f->list);
1445 free(f);
1446 free(name);
1447 break;
1448 }
1449
1450 /* No implicit cache filter found, create configuration for the explicit one */
1451 if (!cconf) {
1452 cconf = calloc(1, sizeof(*cconf));
1453 if (!cconf) {
1454 memprintf(err, "%s: out of memory", args[*cur_arg]);
1455 goto error;
1456 }
1457 cconf->c.name = name;
1458 }
1459
1460 cconf->flags = 0;
1461 fconf->id = cache_store_flt_id;
1462 fconf->conf = cconf;
1463 fconf->ops = &cache_ops;
1464
1465 *cur_arg = pos;
1466 return 0;
1467
1468 error:
1469 free(name);
1470 free(cconf);
1471 return -1;
1472}
1473
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001474static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001475{
1476 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1477 return 1;
1478
1479 return 0;
1480}
1481
1482static int cli_io_handler_show_cache(struct appctx *appctx)
1483{
1484 struct cache* cache = appctx->ctx.cli.p0;
1485 struct stream_interface *si = appctx->owner;
1486
William Lallemand1f49a362017-11-21 20:01:26 +01001487 if (cache == NULL) {
1488 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1489 }
1490
1491 list_for_each_entry_from(cache, &caches, list) {
1492 struct eb32_node *node = NULL;
1493 unsigned int next_key;
1494 struct cache_entry *entry;
1495
William Lallemand1f49a362017-11-21 20:01:26 +01001496 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001497 if (!next_key) {
1498 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1499 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001500 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001501 return 0;
1502 }
1503 }
William Lallemand1f49a362017-11-21 20:01:26 +01001504
1505 appctx->ctx.cli.p0 = cache;
1506
1507 while (1) {
1508
1509 shctx_lock(shctx_ptr(cache));
1510 node = eb32_lookup_ge(&cache->entries, next_key);
1511 if (!node) {
1512 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001513 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001514 break;
1515 }
1516
1517 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001518 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 +01001519
1520 next_key = node->key + 1;
1521 appctx->ctx.cli.i0 = next_key;
1522
1523 shctx_unlock(shctx_ptr(cache));
1524
1525 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001526 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001527 return 0;
1528 }
1529 }
1530
1531 }
1532
1533 return 1;
1534
1535}
1536
Christopher Faulet99a17a22018-12-11 09:18:27 +01001537/* Declare the filter parser for "cache" keyword */
1538static struct flt_kw_list filter_kws = { "CACHE", { }, {
1539 { "cache", parse_cache_flt, NULL },
1540 { NULL, NULL, NULL },
1541 }
1542};
1543
1544INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1545
William Lallemand1f49a362017-11-21 20:01:26 +01001546static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001547 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1548 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001549}};
1550
Willy Tarreau0108d902018-11-25 19:14:37 +01001551INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001552
William Lallemand41db4602017-10-30 11:15:51 +01001553static struct action_kw_list http_res_actions = {
1554 .kw = {
1555 { "cache-store", parse_cache_store },
1556 { NULL, NULL }
1557 }
1558};
1559
Willy Tarreau0108d902018-11-25 19:14:37 +01001560INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1561
William Lallemand41db4602017-10-30 11:15:51 +01001562static struct action_kw_list http_req_actions = {
1563 .kw = {
1564 { "cache-use", parse_cache_use },
1565 { NULL, NULL }
1566 }
1567};
1568
Willy Tarreau0108d902018-11-25 19:14:37 +01001569INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1570
Willy Tarreau2231b632019-03-29 18:26:52 +01001571struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001572 .obj_type = OBJ_TYPE_APPLET,
1573 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001574 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001575 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001576};
1577
Willy Tarreaue6552512018-11-26 11:33:13 +01001578/* config parsers for this section */
1579REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001580REGISTER_POST_CHECK(post_check_cache);