blob: c8350ff00704283c5fb4b13608178c22e35224d0 [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
2 * Cache management
3 *
4 * Copyright 2017 HAProxy Technologies
5 * William Lallemand <wlallemand@haproxy.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
Willy Tarreaub2551052020-06-09 09:07:15 +020013#include <import/eb32tree.h>
14#include <import/sha1.h>
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020018#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020020#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020021#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020022#include <haproxy/filters.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/hash.h>
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020027#include <haproxy/http_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/htx.h>
29#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020031#include <haproxy/shctx.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020033#include <haproxy/stream_interface.h>
William Lallemand41db4602017-10-30 11:15:51 +010034
Christopher Faulet27d93c32018-12-15 22:32:02 +010035#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010036 * the filter keyword) */
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +020037#define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */
Christopher Fauletafd819c2018-12-11 08:57:45 +010038
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010039const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010040
Willy Tarreau2231b632019-03-29 18:26:52 +010041extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010042
43struct flt_ops cache_ops;
44
45struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010046 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010047 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010048 unsigned int maxage; /* max-age */
49 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020050 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010052};
53
Christopher Faulet95220e22018-12-07 17:34:39 +010054/* cache config for filters */
55struct cache_flt_conf {
56 union {
57 struct cache *cache; /* cache used by the filter */
58 char *name; /* cache name used during conf parsing */
59 } c;
60 unsigned int flags; /* CACHE_FLT_F_* */
61};
62
William Lallemand41db4602017-10-30 11:15:51 +010063/*
64 * cache ctx for filters
65 */
66struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010067 struct shared_block *first_block;
68};
69
70struct cache_entry {
71 unsigned int latest_validation; /* latest validation date */
72 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020073 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010074
William Lallemand41db4602017-10-30 11:15:51 +010075 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010076 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +020077
78 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
79 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
80
William Lallemand41db4602017-10-30 11:15:51 +010081 unsigned char data[0];
82};
83
84#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010085#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010086
87static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020088static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010089static struct cache *tmp_cache_config = NULL;
90
Willy Tarreau8ceae722018-11-26 11:58:30 +010091DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
92
William Lallemandf528fff2017-11-23 19:43:17 +010093struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010094{
95 struct eb32_node *node;
96 struct cache_entry *entry;
97
Willy Tarreau8b507582020-02-25 09:35:07 +010098 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010099 if (!node)
100 return NULL;
101
102 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100103
104 /* if that's not the right node */
105 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
106 return NULL;
107
William Lallemand08727662017-11-21 20:01:27 +0100108 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100109 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100110 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100111 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100112 entry->eb.key = 0;
113 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100114 return NULL;
115
116}
117
118static inline struct shared_context *shctx_ptr(struct cache *cache)
119{
120 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
121}
122
William Lallemand77c11972017-10-31 20:43:01 +0100123static inline struct shared_block *block_ptr(struct cache_entry *entry)
124{
125 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
126}
127
128
129
William Lallemand41db4602017-10-30 11:15:51 +0100130static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100131cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100132{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100133 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100134 return 0;
135}
136
Christopher Faulet95220e22018-12-07 17:34:39 +0100137static void
138cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
139{
140 struct cache_flt_conf *cconf = fconf->conf;
141
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200142 if (!(cconf->flags & CACHE_FLT_INIT))
143 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100144 free(cconf);
145}
146
William Lallemand4da3f8a2017-10-31 14:33:34 +0100147static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100148cache_store_check(struct proxy *px, struct flt_conf *fconf)
149{
150 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100151 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100152 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100153 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100154
William Lallemandd1d1e222019-08-28 15:22:49 +0200155 /* Find the cache corresponding to the name in the filter config. The
156 * cache will not be referenced now in the filter config because it is
157 * not fully allocated. This step will be performed during the cache
158 * post_check.
159 */
160 list_for_each_entry(cache, &caches_config, list) {
161 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100162 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100163 }
164
165 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
166 proxy_type_str(px), px->id, (char *)cconf->c.name);
167 return 1;
168
169 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100170 /* Here <cache> points on the cache the filter must use and <cconf>
171 * points on the cache filter configuration. */
172
173 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100174 * enabled and if it is after the cache. When the compression is before
175 * the cache, an error is returned. Also check if the cache filter must
176 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100177 list_for_each_entry(f, &px->filter_configs, list) {
178 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100179 /* The compression filter must be evaluated after the cache. */
180 if (comp) {
181 ha_alert("config: %s '%s': unable to enable the compression filter before "
182 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
183 return 1;
184 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100185 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200186 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100187 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200188 else if (f->id == fcgi_flt_id)
189 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100190 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
191 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200192 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100193 * declaration is required. */
194 ha_alert("config: %s '%s': require an explicit filter declaration "
195 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
196 return 1;
197 }
198
Christopher Fauletafd819c2018-12-11 08:57:45 +0100199 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100200 return 0;
201}
202
203static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100204cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100205{
Christopher Faulet65554e12020-03-06 14:52:06 +0100206 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100207
Christopher Faulet65554e12020-03-06 14:52:06 +0100208 st = pool_alloc_dirty(pool_head_cache_st);
209 if (st == NULL)
210 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100211
Christopher Faulet65554e12020-03-06 14:52:06 +0100212 st->first_block = NULL;
213 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100214
Christopher Faulet65554e12020-03-06 14:52:06 +0100215 /* Register post-analyzer on AN_RES_WAIT_HTTP */
216 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100217 return 1;
218}
219
Christopher Faulet65554e12020-03-06 14:52:06 +0100220static void
221cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100222{
223 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100224 struct cache_flt_conf *cconf = FLT_CONF(filter);
225 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100226 struct shared_context *shctx = shctx_ptr(cache);
227
William Lallemand49dc0482017-11-24 14:33:54 +0100228 /* Everything should be released in the http_end filter, but we need to do it
229 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100230 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100231 shctx_lock(shctx);
232 shctx_row_dec_hot(shctx, st->first_block);
233 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100234 }
235 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100236 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100237 filter->ctx = NULL;
238 }
William Lallemand49dc0482017-11-24 14:33:54 +0100239}
240
Christopher Faulet839791a2019-01-07 16:12:07 +0100241static int
242cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
243 unsigned an_bit)
244{
245 struct http_txn *txn = s->txn;
246 struct http_msg *msg = &txn->rsp;
247 struct cache_st *st = filter->ctx;
248
249 if (an_bit != AN_RES_WAIT_HTTP)
250 goto end;
251
252 /* Here we need to check if any compression filter precedes the cache
253 * filter. This is only possible when the compression is configured in
254 * the frontend while the cache filter is configured on the
255 * backend. This case cannot be detected during HAProxy startup. So in
256 * such cases, the cache is disabled.
257 */
258 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
259 pool_free(pool_head_cache_st, st);
260 filter->ctx = NULL;
261 }
262
263 end:
264 return 1;
265}
William Lallemand49dc0482017-11-24 14:33:54 +0100266
267static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100268cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
269{
270 struct cache_st *st = filter->ctx;
271
William Lallemand4da3f8a2017-10-31 14:33:34 +0100272 if (!(msg->chn->flags & CF_ISRESP) || !st)
273 return 1;
274
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200275 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100276 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100277 return 1;
278}
279
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200280static inline void disable_cache_entry(struct cache_st *st,
281 struct filter *filter, struct shared_context *shctx)
282{
283 struct cache_entry *object;
284
285 object = (struct cache_entry *)st->first_block->data;
286 filter->ctx = NULL; /* disable cache */
287 shctx_lock(shctx);
288 shctx_row_dec_hot(shctx, st->first_block);
289 object->eb.key = 0;
290 shctx_unlock(shctx);
291 pool_free(pool_head_cache_st, st);
292}
293
William Lallemand4da3f8a2017-10-31 14:33:34 +0100294static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100295cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
296 unsigned int offset, unsigned int len)
297{
Christopher Faulet95220e22018-12-07 17:34:39 +0100298 struct cache_flt_conf *cconf = FLT_CONF(filter);
299 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100300 struct cache_st *st = filter->ctx;
301 struct htx *htx = htxbuf(&msg->chn->buf);
302 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200303 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100304 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200305 unsigned int orig_len, to_forward;
306 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100307
308 if (!len)
309 return len;
310
311 if (!st->first_block) {
312 unregister_data_filter(s, msg->chn, filter);
313 return len;
314 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100315
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200316 chunk_reset(&trash);
317 orig_len = len;
318 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100319
320 htxret = htx_find_offset(htx, offset);
321 blk = htxret.blk;
322 offset = htxret.ret;
323 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100324 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200325 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100326 struct ist v;
327
328 switch (type) {
329 case HTX_BLK_UNUSED:
330 break;
331
332 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100333 v = htx_get_blk_value(htx, blk);
334 v.ptr += offset;
335 v.len -= offset;
336 if (v.len > len)
337 v.len = len;
338
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200339 info = (type << 28) + v.len;
340 chunk_memcat(&trash, (char *)&info, sizeof(info));
341 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100342 to_forward += v.len;
343 len -= v.len;
344 break;
345
346 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200347 /* Here offset must always be 0 because only
348 * DATA blocks can be partially transferred. */
349 if (offset)
350 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100351 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200352 goto end;
353
354 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
355 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100356 to_forward += sz;
357 len -= sz;
358 break;
359 }
360
361 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100362 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200363
364 end:
365 shctx_lock(shctx);
366 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
367 if (!fb) {
368 shctx_unlock(shctx);
369 goto no_cache;
370 }
371 shctx_unlock(shctx);
372
373 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
374 (unsigned char *)b_head(&trash), b_data(&trash));
375 if (ret < 0)
376 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100377
378 return to_forward;
379
380 no_cache:
381 disable_cache_entry(st, filter, shctx);
382 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200383 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100384}
385
386static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100387cache_store_http_end(struct stream *s, struct filter *filter,
388 struct http_msg *msg)
389{
390 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100391 struct cache_flt_conf *cconf = FLT_CONF(filter);
392 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100393 struct shared_context *shctx = shctx_ptr(cache);
394 struct cache_entry *object;
395
396 if (!(msg->chn->flags & CF_ISRESP))
397 return 1;
398
399 if (st && st->first_block) {
400
401 object = (struct cache_entry *)st->first_block->data;
402
403 /* does not need to test if the insertion worked, if it
404 * doesn't, the blocks will be reused anyway */
405
406 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100407 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
408 object->eb.key = 0;
409 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100410 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100411 shctx_row_dec_hot(shctx, st->first_block);
412 shctx_unlock(shctx);
413
414 }
415 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100416 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100417 filter->ctx = NULL;
418 }
419
420 return 1;
421}
422
423 /*
424 * This intends to be used when checking HTTP headers for some
425 * word=value directive. Return a pointer to the first character of value, if
426 * the word was not found or if there wasn't any value assigned ot it return NULL
427 */
428char *directive_value(const char *sample, int slen, const char *word, int wlen)
429{
430 int st = 0;
431
432 if (slen < wlen)
433 return 0;
434
435 while (wlen) {
436 char c = *sample ^ *word;
437 if (c && c != ('A' ^ 'a'))
438 return NULL;
439 sample++;
440 word++;
441 slen--;
442 wlen--;
443 }
444
445 while (slen) {
446 if (st == 0) {
447 if (*sample != '=')
448 return NULL;
449 sample++;
450 slen--;
451 st = 1;
452 continue;
453 } else {
454 return (char *)sample;
455 }
456 }
457
458 return NULL;
459}
460
461/*
462 * Return the maxage in seconds of an HTTP response.
463 * Compute the maxage using either:
464 * - the assigned max-age of the cache
465 * - the s-maxage directive
466 * - the max-age directive
467 * - (Expires - Data) headers
468 * - the default-max-age of the cache
469 *
470 */
William Lallemand49b44532017-11-24 18:53:43 +0100471int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100472{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200473 struct htx *htx = htxbuf(&s->res.buf);
474 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100475 int smaxage = -1;
476 int maxage = -1;
477
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200478 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
479 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100480
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200481 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
482 if (value) {
483 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100484
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200485 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
486 chunk_strncat(chk, "", 1);
487 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100488 }
489
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200490 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
491 if (value) {
492 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200493
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200494 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
495 chunk_strncat(chk, "", 1);
496 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100497 }
498 }
499
500 /* TODO: Expires - Data */
501
502
503 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100504 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100505
506 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100507 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100508
William Lallemand49b44532017-11-24 18:53:43 +0100509 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100510
511}
512
513
William Lallemanda400a3a2017-11-20 19:13:12 +0100514static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
515{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200516 struct cache_entry *object = (struct cache_entry *)block->data;
517
518 if (first == block && object->eb.key)
519 eb32_delete(&object->eb);
520 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100521}
522
William Lallemand41db4602017-10-30 11:15:51 +0100523/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500524 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100525 * register a filter to store the data
526 */
527enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200528 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100529{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200530 unsigned int age;
531 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100532 struct http_txn *txn = s->txn;
533 struct http_msg *msg = &txn->rsp;
534 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100535 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100536 struct cache_flt_conf *cconf = rule->arg.act.p[0];
537 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100538 struct cache_st *cache_ctx = NULL;
539 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100540 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200541 struct htx *htx;
542 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200543 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200544 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200545 unsigned int etag_length = 0;
546 unsigned int etag_offset = 0;
547 struct ist header_name = IST_NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100548
William Lallemand4da3f8a2017-10-31 14:33:34 +0100549 /* Don't cache if the response came from a cache */
550 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
551 s->target == &http_cache_applet.obj_type) {
552 goto out;
553 }
554
555 /* cache only HTTP/1.1 */
556 if (!(txn->req.flags & HTTP_MSGF_VER_11))
557 goto out;
558
Willy Tarreau6905d182019-10-01 17:59:17 +0200559 /* cache only GET method */
560 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100561 goto out;
562
Willy Tarreauc9036c02019-01-11 19:38:25 +0100563 /* cache key was not computed */
564 if (!key)
565 goto out;
566
William Lallemand4da3f8a2017-10-31 14:33:34 +0100567 /* cache only 200 status code */
568 if (txn->status != 200)
569 goto out;
570
Christopher Faulet839791a2019-01-07 16:12:07 +0100571 /* Find the corresponding filter instance for the current stream */
572 list_for_each_entry(filter, &s->strm_flt.filters, list) {
573 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
574 /* No filter ctx, don't cache anything */
575 if (!filter->ctx)
576 goto out;
577 cache_ctx = filter->ctx;
578 break;
579 }
580 }
581
582 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200583 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100584
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200585 /* Do not cache too big objects. */
586 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
587 htx->data + htx->extra > shctx->max_obj_size)
588 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100589
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200590 /* Does not manage Vary at the moment. We will need a secondary key later for that */
591 ctx.blk = NULL;
592 if (http_find_header(htx, ist("Vary"), &ctx, 0))
593 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100594
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200595 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100596
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200597 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
598 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100599
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200600 age = 0;
601 ctx.blk = NULL;
602 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
603 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
604 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
605 hdr_age = CACHE_ENTRY_MAX_AGE;
606 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100607 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200608 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100609 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100610
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200611 chunk_reset(&trash);
612 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
613 struct htx_blk *blk = htx_get_blk(htx, pos);
614 enum htx_blk_type type = htx_get_blk_type(blk);
615 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100616
Christopher Fauletb0667472019-09-03 22:22:12 +0200617 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200618 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
619 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200620
621 /* Look for optional ETag header.
622 * We need to store the offset of the ETag value in order for
623 * future conditional requests to be able to perform ETag
624 * comparisons. */
625 if (type == HTX_BLK_HDR) {
626 header_name = htx_get_blk_name(htx, blk);
627 if (isteq(header_name, ist("etag"))) {
628 etag_length = sz - istlen(header_name);
629 etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
630 }
631 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200632 if (type == HTX_BLK_EOH)
633 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200634 }
635
Christopher Fauletb0667472019-09-03 22:22:12 +0200636 /* Do not cache objects if the headers are too big. */
637 if (hdrs_len > htx->size - global.tune.maxrewrite)
638 goto out;
639
William Lallemand4da3f8a2017-10-31 14:33:34 +0100640 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200641 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100642 if (!first) {
643 shctx_unlock(shctx);
644 goto out;
645 }
646 shctx_unlock(shctx);
647
Willy Tarreau1093a452018-04-06 19:02:25 +0200648 /* the received memory is not initialized, we need at least to mark
649 * the object as not indexed yet.
650 */
651 object = (struct cache_entry *)first->data;
652 object->eb.node.leaf_p = NULL;
653 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200654 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200655
William Lallemand4da3f8a2017-10-31 14:33:34 +0100656 /* reserve space for the cache_entry structure */
657 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200658 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100659 /* cache the headers in a http action because it allows to chose what
660 * to cache, for example you might want to cache a response before
661 * modifying some HTTP headers, or on the contrary after modifying
662 * those headers.
663 */
664
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200665 /* Write the ETag information in the cache_entry if needed. */
666 object->etag_length = etag_length;
667 object->etag_offset = etag_offset;
668
William Lallemand4da3f8a2017-10-31 14:33:34 +0100669 /* does not need to be locked because it's in the "hot" list,
670 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200671 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
672 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100673
674 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100675 if (cache_ctx) {
676 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100677
Willy Tarreauc9036c02019-01-11 19:38:25 +0100678 object->eb.key = key;
679
Christopher Faulet839791a2019-01-07 16:12:07 +0100680 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
681 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100682
Christopher Faulet839791a2019-01-07 16:12:07 +0100683 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100684
Christopher Faulet839791a2019-01-07 16:12:07 +0100685 old = entry_exist(cconf->c.cache, txn->cache_hash);
686 if (old) {
687 eb32_delete(&old->eb);
688 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100689 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100690 shctx_unlock(shctx);
691
692 /* store latest value and expiration time */
693 object->latest_validation = now.tv_sec;
694 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
695 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100696 }
697
698out:
699 /* if does not cache */
700 if (first) {
701 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100702 first->len = 0;
703 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100704 shctx_row_dec_hot(shctx, first);
705 shctx_unlock(shctx);
706 }
707
William Lallemand41db4602017-10-30 11:15:51 +0100708 return ACT_RET_CONT;
709}
710
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100711#define HTX_CACHE_INIT 0 /* Initial state. */
712#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
713#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200714#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
715#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100716
William Lallemandecb73b12017-11-24 14:33:55 +0100717static void http_cache_applet_release(struct appctx *appctx)
718{
Christopher Faulet95220e22018-12-07 17:34:39 +0100719 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100720 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100721 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100722 struct shared_block *first = block_ptr(cache_ptr);
723
724 shctx_lock(shctx_ptr(cache));
725 shctx_row_dec_hot(shctx_ptr(cache), first);
726 shctx_unlock(shctx_ptr(cache));
727}
728
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200729
730static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
731 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100732{
Christopher Faulet95220e22018-12-07 17:34:39 +0100733 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
734 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200735 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200736 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200737 unsigned int max, total;
738 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100739
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200740 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
741 if (!max)
742 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200743 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200744 ? (info & 0xff) + ((info >> 8) & 0xfffff)
745 : info & 0xfffffff);
746 if (blksz > max)
747 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100748
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200749 blk = htx_add_blk(htx, type, blksz);
750 if (!blk)
751 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100752
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200753 blk->info = info;
754 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200755 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200756 while (blksz) {
757 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200758 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200759 offset += max;
760 blksz -= max;
761 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200762 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200763 if (blksz || offset == shctx->block_size) {
764 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
765 offset = 0;
766 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100767 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200768 appctx->ctx.cache.offset = offset;
769 appctx->ctx.cache.next = shblk;
770 appctx->ctx.cache.sent += total;
771 return total;
772}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100773
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200774static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
775 uint32_t info, struct shared_block *shblk, unsigned int offset)
776{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100777
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200778 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
779 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
780 unsigned int max, total, rem_data;
781 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100782
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200783 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
784 if (!max)
785 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100786
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200787 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200788 if (appctx->ctx.cache.rem_data) {
789 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200790 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200791 }
792 else {
793 blksz = (info & 0xfffffff);
794 total = 4;
795 }
796 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200797 rem_data = blksz - max;
798 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100799 }
800
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200801 while (blksz) {
802 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100803
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200804 max = MIN(blksz, shctx->block_size - offset);
805 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
806 offset += sz;
807 blksz -= sz;
808 total += sz;
809 if (sz < max)
810 break;
811 if (blksz || offset == shctx->block_size) {
812 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
813 offset = 0;
814 }
815 }
816
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200817 appctx->ctx.cache.offset = offset;
818 appctx->ctx.cache.next = shblk;
819 appctx->ctx.cache.sent += total;
820 appctx->ctx.cache.rem_data = rem_data + blksz;
821 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100822}
823
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200824static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
825 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100826{
Christopher Faulet95220e22018-12-07 17:34:39 +0100827 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
828 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200829 struct shared_block *shblk;
830 unsigned int offset, sz;
831 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100832
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200833 while (len) {
834 enum htx_blk_type type;
835 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100836
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200837 shblk = appctx->ctx.cache.next;
838 offset = appctx->ctx.cache.offset;
839 if (appctx->ctx.cache.rem_data) {
840 type = HTX_BLK_DATA;
841 info = 0;
842 goto add_data_blk;
843 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100844
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500845 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200846 sz = MIN(4, shctx->block_size - offset);
847 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
848 offset += sz;
849 if (sz < 4) {
850 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
851 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
852 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100853 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200854
855 /* Get payload of the next HTX block and insert it. */
856 type = (info >> 28);
857 if (type != HTX_BLK_DATA)
858 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
859 else {
860 add_data_blk:
861 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100862 }
863
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200864 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100865 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200866 total += ret;
867 len -= ret;
868
869 if (appctx->ctx.cache.rem_data || type == mark)
870 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100871 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100872
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100873 return total;
874}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200875
876static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
877{
878 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
879 unsigned int age;
880 char *end;
881
882 chunk_reset(&trash);
883 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
884 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
885 age = CACHE_ENTRY_MAX_AGE;
886 end = ultoa_o(age, b_head(&trash), b_size(&trash));
887 b_set_data(&trash, end - b_head(&trash));
888 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
889 return 0;
890 return 1;
891}
892
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200893static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100894{
895 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
896 struct shared_block *first = block_ptr(cache_ptr);
897 struct stream_interface *si = appctx->owner;
898 struct channel *req = si_oc(si);
899 struct channel *res = si_ic(si);
900 struct htx *req_htx, *res_htx;
901 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200902 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100903 size_t ret, total = 0;
904
905 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200906 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100907
908 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
909 goto out;
910
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500911 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100912 if (!b_size(&res->buf)) {
913 si_rx_room_blk(si);
914 goto out;
915 }
916
Willy Tarreauefef3232018-12-16 00:37:45 +0100917 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100918 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100919
920 if (appctx->st0 == HTX_CACHE_INIT) {
921 appctx->ctx.cache.next = block_ptr(cache_ptr);
922 appctx->ctx.cache.offset = sizeof(*cache_ptr);
923 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200924 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100925 appctx->st0 = HTX_CACHE_HEADER;
926 }
927
928 if (appctx->st0 == HTX_CACHE_HEADER) {
929 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200930 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
931 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
932 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
933 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100934 goto error;
935
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +0200936 /* In case of a conditional request, we might want to send a
937 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +0200938 if (appctx->ctx.cache.send_notmodified) {
939 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
940 /* If replacing the status code fails we need to send the full response. */
941 appctx->ctx.cache.send_notmodified = 0;
942 }
943 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +0200944
945 /* Skip response body for HEAD requests or in case of "304 Not
946 * Modified" response. */
947 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100948 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100949 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200950 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100951 }
952
953 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200954 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
955 if (len) {
956 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
957 if (ret < len) {
958 si_rx_room_blk(si);
959 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100960 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100961 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200962 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100963 }
964
965 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +0200966 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100967 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
968 si_rx_room_blk(si);
969 goto out;
970 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100971 appctx->st0 = HTX_CACHE_END;
972 }
973
974 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100975 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100976 res->flags |= CF_READ_NULL;
977 si_shutr(si);
978 }
979
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100980 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200981 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100982 if (total)
983 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100984 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100985
986 /* eat the whole request */
987 if (co_data(req)) {
988 req_htx = htx_from_buf(&req->buf);
989 co_htx_skip(req, req_htx, co_data(req));
990 htx_to_buf(req_htx, &req->buf);
991 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100992 return;
993
994 error:
995 /* Sent and HTTP error 500 */
996 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200997 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100998 res->buf.data = b_data(errmsg);
999 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1000 res_htx = htx_from_buf(&res->buf);
1001
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001002 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001003 appctx->st0 = HTX_CACHE_END;
1004 goto end;
1005}
1006
1007
Christopher Faulet95220e22018-12-07 17:34:39 +01001008static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001009{
1010 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001011 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001012
Christopher Faulet95220e22018-12-07 17:34:39 +01001013 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001014 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001015 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001016 }
1017
1018 /* check if a cache filter was already registered with this cache
1019 * name, if that's the case, must use it. */
1020 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001021 if (fconf->id == cache_store_flt_id) {
1022 cconf = fconf->conf;
1023 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1024 rule->arg.act.p[0] = cconf;
1025 return 1;
1026 }
William Lallemand41db4602017-10-30 11:15:51 +01001027 }
1028 }
1029
Christopher Faulet95220e22018-12-07 17:34:39 +01001030 /* Create the filter cache config */
1031 cconf = calloc(1, sizeof(*cconf));
1032 if (!cconf) {
1033 memprintf(err, "out of memory\n");
1034 goto err;
1035 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001036 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001037 cconf->c.name = strdup(name);
1038 if (!cconf->c.name) {
1039 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001040 goto err;
1041 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001042
William Lallemand41db4602017-10-30 11:15:51 +01001043 /* register a filter to fill the cache buffer */
1044 fconf = calloc(1, sizeof(*fconf));
1045 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001046 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001047 goto err;
1048 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001049 fconf->id = cache_store_flt_id;
1050 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001051 fconf->ops = &cache_ops;
1052 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1053
Christopher Faulet95220e22018-12-07 17:34:39 +01001054 rule->arg.act.p[0] = cconf;
1055 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001056
Christopher Faulet95220e22018-12-07 17:34:39 +01001057 err:
1058 free(cconf);
1059 return 0;
1060}
1061
1062enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1063 struct act_rule *rule, char **err)
1064{
1065 rule->action = ACT_CUSTOM;
1066 rule->action_ptr = http_action_store_cache;
1067
1068 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1069 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001070
Christopher Faulet95220e22018-12-07 17:34:39 +01001071 (*orig_arg)++;
1072 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001073}
1074
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001075/* This produces a sha1 hash of the concatenation of the HTTP method,
1076 * the first occurrence of the Host header followed by the path component
1077 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001078int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001079{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001080 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001081 struct htx *htx = htxbuf(&s->req.buf);
1082 struct htx_sl *sl;
1083 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001084 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001085 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001086 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001087
William Lallemandf528fff2017-11-23 19:43:17 +01001088 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001089 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001090
1091 switch (txn->meth) {
1092 case HTTP_METH_HEAD:
1093 case HTTP_METH_GET:
1094 chunk_memcat(trash, "GET", 3);
1095 break;
1096 default:
1097 return 0;
1098 }
1099
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001100 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001101 uri = htx_sl_req_uri(sl); // whole uri
1102 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001103 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001104
1105 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1106 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1107 * URIs are almost always sent in absolute form with their scheme. In
1108 * this case, the scheme is almost always "https". In order to support
1109 * sharing of cache objects between H1 and H2, we'll hash the absolute
1110 * URI whenever known, or prepend "https://" + the Host header for
1111 * relative URIs. The difference will only appear on absolute HTTP/1
1112 * requests sent to an origin server, which practically is never met in
1113 * the real world so we don't care about the ability to share the same
1114 * key here.URIs are normalized from the absolute URI to an origin form as
1115 * well.
1116 */
1117 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001118 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001119 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1120 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001121 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001122 }
1123
1124 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001125
1126 /* hash everything */
1127 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001128 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001129 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1130
1131 return 1;
1132}
1133
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001134/* Looks for "If-None-Match" headers in the request and compares their value
1135 * with the one that might have been stored in the cache_entry. If any of them
1136 * matches, a "304 Not Modified" response should be sent instead of the cached
1137 * data.
1138 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
1139 * valid and should receive a "304 Not Modified" response (RFC 7434#4.3.2).
1140 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1141 */
1142static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1143 struct cache_entry *entry)
1144{
1145 int retval = 0;
1146
1147 struct http_hdr_ctx ctx = { .blk = NULL };
1148 struct ist cache_entry_etag = IST_NULL;
1149 struct buffer *etag_buffer = NULL;
1150
1151 if (entry->etag_length == 0)
1152 return 0;
1153
1154 /* If we find a "If-None-Match" header in the request, rebuild the
1155 * cache_entry's ETag in order to perform comparisons. */
1156 /* There could be multiple "if-none-match" header lines. */
1157 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
1158
1159 /* A '*' matches everything. */
1160 if (isteq(ctx.value, ist("*")) != 0) {
1161 retval = 1;
1162 break;
1163 }
1164
1165 /* Rebuild the stored ETag. */
1166 if (etag_buffer == NULL) {
1167 etag_buffer = get_trash_chunk();
1168
1169 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1170 (unsigned char*)b_orig(etag_buffer),
1171 entry->etag_offset, entry->etag_length) == 0) {
1172 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1173 } else {
1174 /* We could not rebuild the ETag in one go, we
1175 * won't send a "304 Not Modified" response. */
1176 break;
1177 }
1178 }
1179
1180 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1181 retval = 1;
1182 break;
1183 }
1184 }
1185
1186 return retval;
1187}
1188
William Lallemand41db4602017-10-30 11:15:51 +01001189enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1190 struct session *sess, struct stream *s, int flags)
1191{
William Lallemand77c11972017-10-31 20:43:01 +01001192
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001193 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001194 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001195 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1196 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001197
Willy Tarreau6905d182019-10-01 17:59:17 +02001198 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1199 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001200 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001201 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001202 txn->flags |= TX_CACHE_IGNORE;
1203
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001204 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001205
Willy Tarreau504455c2017-12-22 17:47:35 +01001206 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1207 return ACT_RET_CONT;
1208
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001209 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001210 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001211
Willy Tarreau504455c2017-12-22 17:47:35 +01001212 if (s->txn->flags & TX_CACHE_IGNORE)
1213 return ACT_RET_CONT;
1214
Willy Tarreaua1214a52018-12-14 14:00:25 +01001215 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001216 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001217 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001218 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001219
William Lallemanda400a3a2017-11-20 19:13:12 +01001220 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001221 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001222 if (res) {
1223 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001224 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1225 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001226 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001227 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001228 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001229 appctx->rule = rule;
1230 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001231 appctx->ctx.cache.next = NULL;
1232 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001233 appctx->ctx.cache.send_notmodified =
1234 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001235
1236 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001237 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001238 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001239 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001240 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001241 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001242 shctx_lock(shctx_ptr(cache));
1243 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1244 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001245 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001246 }
1247 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001248 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001249 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001250}
1251
1252
1253enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1254 struct act_rule *rule, char **err)
1255{
William Lallemand41db4602017-10-30 11:15:51 +01001256 rule->action = ACT_CUSTOM;
1257 rule->action_ptr = http_action_req_cache_use;
1258
Christopher Faulet95220e22018-12-07 17:34:39 +01001259 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001260 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001261
1262 (*orig_arg)++;
1263 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001264}
1265
1266int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1267{
1268 int err_code = 0;
1269
1270 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1271
1272 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001273 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001274 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001275 err_code |= ERR_ALERT | ERR_ABORT;
1276 goto out;
1277 }
1278
1279 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1280 err_code |= ERR_ABORT;
1281 goto out;
1282 }
1283
1284 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001285 struct cache *cache_config;
1286
William Lallemand41db4602017-10-30 11:15:51 +01001287 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1288 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001289 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001290 err_code |= ERR_ALERT | ERR_ABORT;
1291 goto out;
1292 }
1293
1294 strlcpy2(tmp_cache_config->id, args[1], 33);
1295 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001296 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001297 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001298 err_code |= ERR_WARN;
1299 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001300
1301 list_for_each_entry(cache_config, &caches_config, list) {
1302 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1303 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1304 file, linenum, tmp_cache_config->id);
1305 err_code |= ERR_ALERT | ERR_ABORT;
1306 goto out;
1307 }
1308 }
1309
William Lallemand49b44532017-11-24 18:53:43 +01001310 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001311 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001312 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001313 }
1314 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001315 unsigned long int maxsize;
1316 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001317
1318 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1319 err_code |= ERR_ABORT;
1320 goto out;
1321 }
1322
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001323 maxsize = strtoul(args[1], &err, 10);
1324 if (err == args[1] || *err != '\0') {
1325 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1326 file, linenum, args[1]);
1327 err_code |= ERR_ABORT;
1328 goto out;
1329 }
1330
1331 if (maxsize > (UINT_MAX >> 20)) {
1332 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1333 file, linenum, args[1], UINT_MAX >> 20);
1334 err_code |= ERR_ABORT;
1335 goto out;
1336 }
1337
William Lallemand41db4602017-10-30 11:15:51 +01001338 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001339 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001340 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001341 } else if (strcmp(args[0], "max-age") == 0) {
1342 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1343 err_code |= ERR_ABORT;
1344 goto out;
1345 }
1346
1347 if (!*args[1]) {
1348 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1349 file, linenum, args[0]);
1350 err_code |= ERR_WARN;
1351 }
1352
1353 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001354 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001355 unsigned int maxobjsz;
1356 char *err;
1357
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001358 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1359 err_code |= ERR_ABORT;
1360 goto out;
1361 }
1362
1363 if (!*args[1]) {
1364 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1365 file, linenum, args[0]);
1366 err_code |= ERR_WARN;
1367 }
1368
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001369 maxobjsz = strtoul(args[1], &err, 10);
1370 if (err == args[1] || *err != '\0') {
1371 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1372 file, linenum, args[1]);
1373 err_code |= ERR_ABORT;
1374 goto out;
1375 }
1376 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001377 }
1378 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001379 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001380 err_code |= ERR_ALERT | ERR_FATAL;
1381 goto out;
1382 }
1383out:
1384 return err_code;
1385}
1386
1387/* once the cache section is parsed */
1388
1389int cfg_post_parse_section_cache()
1390{
William Lallemand41db4602017-10-30 11:15:51 +01001391 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001392
1393 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001394
1395 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001396 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001397 err_code |= ERR_FATAL | ERR_ALERT;
1398 goto out;
1399 }
1400
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001401 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001402 /* Default max. file size is a 256th of the cache size. */
1403 tmp_cache_config->maxobjsz =
1404 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001405 }
1406 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1407 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1408 err_code |= ERR_FATAL | ERR_ALERT;
1409 goto out;
1410 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001411
William Lallemandd1d1e222019-08-28 15:22:49 +02001412 /* add to the list of cache to init and reinit tmp_cache_config
1413 * for next cache section, if any.
1414 */
1415 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1416 tmp_cache_config = NULL;
1417 return err_code;
1418 }
1419out:
1420 free(tmp_cache_config);
1421 tmp_cache_config = NULL;
1422 return err_code;
1423
1424}
1425
1426int post_check_cache()
1427{
1428 struct proxy *px;
1429 struct cache *back, *cache_config, *cache;
1430 struct shared_context *shctx;
1431 int ret_shctx;
1432 int err_code = 0;
1433
1434 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1435
1436 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1437 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001438
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001439 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001440 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001441 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001442 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001443 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001444
1445 err_code |= ERR_FATAL | ERR_ALERT;
1446 goto out;
1447 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001448 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001449 /* the cache structure is stored in the shctx and added to the
1450 * caches list, we can remove the entry from the caches_config
1451 * list */
1452 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001453 cache = (struct cache *)shctx->data;
1454 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001455 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001456 LIST_DEL(&cache_config->list);
1457 free(cache_config);
1458
1459 /* Find all references for this cache in the existing filters
1460 * (over all proxies) and reference it in matching filters.
1461 */
1462 for (px = proxies_list; px; px = px->next) {
1463 struct flt_conf *fconf;
1464 struct cache_flt_conf *cconf;
1465
1466 list_for_each_entry(fconf, &px->filter_configs, list) {
1467 if (fconf->id != cache_store_flt_id)
1468 continue;
1469
1470 cconf = fconf->conf;
1471 if (!strcmp(cache->id, cconf->c.name)) {
1472 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001473 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001474 cconf->c.cache = cache;
1475 break;
1476 }
1477 }
1478 }
William Lallemand41db4602017-10-30 11:15:51 +01001479 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001480
William Lallemand41db4602017-10-30 11:15:51 +01001481out:
William Lallemand41db4602017-10-30 11:15:51 +01001482 return err_code;
1483
William Lallemand41db4602017-10-30 11:15:51 +01001484}
1485
William Lallemand41db4602017-10-30 11:15:51 +01001486struct flt_ops cache_ops = {
1487 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001488 .check = cache_store_check,
1489 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001490
Christopher Faulet65554e12020-03-06 14:52:06 +01001491 /* Handle stream init/deinit */
1492 .attach = cache_store_strm_init,
1493 .detach = cache_store_strm_deinit,
1494
William Lallemand4da3f8a2017-10-31 14:33:34 +01001495 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001496 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001497
1498 /* Filter HTTP requests and responses */
1499 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001500 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001501 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001502};
1503
Christopher Faulet99a17a22018-12-11 09:18:27 +01001504
1505
1506static int
1507parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1508 struct flt_conf *fconf, char **err, void *private)
1509{
1510 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001511 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001512 char *name = NULL;
1513 int pos = *cur_arg;
1514
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001515 /* Get the cache filter name. <pos> point on "cache" keyword */
1516 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02001517 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001518 goto error;
1519 }
1520 name = strdup(args[pos + 1]);
1521 if (!name) {
1522 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1523 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001524 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001525 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001526
1527 /* Check if an implicit filter with the same name already exists. If so,
1528 * we remove the implicit filter to use the explicit one. */
1529 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1530 if (f->id != cache_store_flt_id)
1531 continue;
1532
1533 cconf = f->conf;
1534 if (strcmp(name, cconf->c.name)) {
1535 cconf = NULL;
1536 continue;
1537 }
1538
1539 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1540 cconf = NULL;
1541 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1542 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001543 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001544 }
1545
1546 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1547 LIST_DEL(&f->list);
1548 free(f);
1549 free(name);
1550 break;
1551 }
1552
1553 /* No implicit cache filter found, create configuration for the explicit one */
1554 if (!cconf) {
1555 cconf = calloc(1, sizeof(*cconf));
1556 if (!cconf) {
1557 memprintf(err, "%s: out of memory", args[*cur_arg]);
1558 goto error;
1559 }
1560 cconf->c.name = name;
1561 }
1562
1563 cconf->flags = 0;
1564 fconf->id = cache_store_flt_id;
1565 fconf->conf = cconf;
1566 fconf->ops = &cache_ops;
1567
1568 *cur_arg = pos;
1569 return 0;
1570
1571 error:
1572 free(name);
1573 free(cconf);
1574 return -1;
1575}
1576
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001577static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001578{
1579 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1580 return 1;
1581
1582 return 0;
1583}
1584
1585static int cli_io_handler_show_cache(struct appctx *appctx)
1586{
1587 struct cache* cache = appctx->ctx.cli.p0;
1588 struct stream_interface *si = appctx->owner;
1589
William Lallemand1f49a362017-11-21 20:01:26 +01001590 if (cache == NULL) {
1591 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1592 }
1593
1594 list_for_each_entry_from(cache, &caches, list) {
1595 struct eb32_node *node = NULL;
1596 unsigned int next_key;
1597 struct cache_entry *entry;
1598
William Lallemand1f49a362017-11-21 20:01:26 +01001599 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001600 if (!next_key) {
1601 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1602 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001603 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001604 return 0;
1605 }
1606 }
William Lallemand1f49a362017-11-21 20:01:26 +01001607
1608 appctx->ctx.cli.p0 = cache;
1609
1610 while (1) {
1611
1612 shctx_lock(shctx_ptr(cache));
1613 node = eb32_lookup_ge(&cache->entries, next_key);
1614 if (!node) {
1615 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001616 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001617 break;
1618 }
1619
1620 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001621 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 +01001622
1623 next_key = node->key + 1;
1624 appctx->ctx.cli.i0 = next_key;
1625
1626 shctx_unlock(shctx_ptr(cache));
1627
1628 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001629 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001630 return 0;
1631 }
1632 }
1633
1634 }
1635
1636 return 1;
1637
1638}
1639
Christopher Faulet99a17a22018-12-11 09:18:27 +01001640/* Declare the filter parser for "cache" keyword */
1641static struct flt_kw_list filter_kws = { "CACHE", { }, {
1642 { "cache", parse_cache_flt, NULL },
1643 { NULL, NULL, NULL },
1644 }
1645};
1646
1647INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1648
William Lallemand1f49a362017-11-21 20:01:26 +01001649static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001650 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1651 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001652}};
1653
Willy Tarreau0108d902018-11-25 19:14:37 +01001654INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001655
William Lallemand41db4602017-10-30 11:15:51 +01001656static struct action_kw_list http_res_actions = {
1657 .kw = {
1658 { "cache-store", parse_cache_store },
1659 { NULL, NULL }
1660 }
1661};
1662
Willy Tarreau0108d902018-11-25 19:14:37 +01001663INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1664
William Lallemand41db4602017-10-30 11:15:51 +01001665static struct action_kw_list http_req_actions = {
1666 .kw = {
1667 { "cache-use", parse_cache_use },
1668 { NULL, NULL }
1669 }
1670};
1671
Willy Tarreau0108d902018-11-25 19:14:37 +01001672INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1673
Willy Tarreau2231b632019-03-29 18:26:52 +01001674struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001675 .obj_type = OBJ_TYPE_APPLET,
1676 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001677 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001678 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001679};
1680
Willy Tarreaue6552512018-11-26 11:33:13 +01001681/* config parsers for this section */
1682REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001683REGISTER_POST_CHECK(post_check_cache);