blob: cc446507e7da5296bd55c4fd1354d7845043e52c [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>
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +010031#include <haproxy/sample.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020032#include <haproxy/shctx.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
William Lallemand41db4602017-10-30 11:15:51 +010035
Christopher Faulet27d93c32018-12-15 22:32:02 +010036#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010037 * the filter keyword) */
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +020038#define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */
Christopher Fauletafd819c2018-12-11 08:57:45 +010039
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010040const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010041
Willy Tarreau2231b632019-03-29 18:26:52 +010042extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010043
44struct flt_ops cache_ops;
45
46struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010047 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010048 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010049 unsigned int maxage; /* max-age */
50 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020051 unsigned int maxobjsz; /* max-object-size (in bytes) */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +010052 uint8_t vary_processing_enabled; /* boolean : manage Vary header (disabled by default) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010053 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010054};
55
Christopher Faulet95220e22018-12-07 17:34:39 +010056/* cache config for filters */
57struct cache_flt_conf {
58 union {
59 struct cache *cache; /* cache used by the filter */
60 char *name; /* cache name used during conf parsing */
61 } c;
62 unsigned int flags; /* CACHE_FLT_F_* */
63};
64
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +010065
66/*
67 * Vary-related structures and functions
68 */
69enum vary_header_bit {
70 VARY_ACCEPT_ENCODING = (1 << 0),
71 VARY_REFERER = (1 << 1),
72 VARY_LAST /* should always be last */
73};
74
75typedef int(*http_header_normalizer)(struct ist value, char *buf, unsigned int *buf_len);
76
77struct vary_hashing_information {
78 struct ist hdr_name; /* Header name */
79 enum vary_header_bit value; /* Bit repesenting the header in a vary signature */
80 unsigned int hash_length; /* Size of the sub hash for this header's value */
81 http_header_normalizer norm_fn; /* Normalization function */
82};
83
84static int accept_encoding_normalizer(struct ist value, char *buf, unsigned int *buf_len);
85static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len);
86
87/* Warning : do not forget to update HTTP_CACHE_SEC_KEY_LEN when new items are
88 * added to this array. */
89const struct vary_hashing_information vary_information[] = {
90 { IST("accept-encoding"), VARY_ACCEPT_ENCODING, sizeof(int), &accept_encoding_normalizer },
91 { IST("referer"), VARY_REFERER, sizeof(int), &default_normalizer },
92};
93
94static int http_request_prebuild_full_secondary_key(struct stream *s);
95static int http_request_build_secondary_key(struct stream *s, int vary_signature);
96static int http_request_reduce_secondary_key(unsigned int vary_signature,
97 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]);
98
99
William Lallemand41db4602017-10-30 11:15:51 +0100100/*
101 * cache ctx for filters
102 */
103struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +0100104 struct shared_block *first_block;
105};
106
107struct cache_entry {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100108 unsigned int complete; /* An entry won't be valid until complete is not null. */
William Lallemand41db4602017-10-30 11:15:51 +0100109 unsigned int latest_validation; /* latest validation date */
110 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200111 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100112
William Lallemand41db4602017-10-30 11:15:51 +0100113 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +0100114 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200115
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100116 char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */
117 unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used
118 * to build secondary keys for this cache entry. */
119
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200120 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
121 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
122
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200123 time_t last_modified; /* Origin server "Last-Modified" header value converted in
124 * seconds since epoch. If no "Last-Modified"
125 * header is found, use "Date" header value,
126 * otherwise use reception time. This field will
127 * be used in case of an "If-Modified-Since"-based
128 * conditional request. */
129
William Lallemand41db4602017-10-30 11:15:51 +0100130 unsigned char data[0];
131};
132
133#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +0100134#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100135
136static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +0200137static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +0100138static struct cache *tmp_cache_config = NULL;
139
Willy Tarreau8ceae722018-11-26 11:58:30 +0100140DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
141
William Lallemandf528fff2017-11-23 19:43:17 +0100142struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100143{
144 struct eb32_node *node;
145 struct cache_entry *entry;
146
Willy Tarreau8b507582020-02-25 09:35:07 +0100147 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100148 if (!node)
149 return NULL;
150
151 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100152
153 /* if that's not the right node */
154 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
155 return NULL;
156
William Lallemand08727662017-11-21 20:01:27 +0100157 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100158 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100159 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100160 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100161 entry->eb.key = 0;
162 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100163 return NULL;
164
165}
166
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100167/*
168 * There can be multiple entries with the same primary key in the ebtree so in
169 * order to get the proper one out of the list, we use a secondary_key.
170 * This function simply iterates over all the entries with the same primary_key
171 * until it finds the right one.
172 * Returns the cache_entry in case of success, NULL otherwise.
173 */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100174struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry,
175 char *secondary_key)
176{
177 struct eb32_node *node = &entry->eb;
178
179 if (!entry->secondary_key_signature)
180 return NULL;
181
182 while (entry && memcmp(entry->secondary_key, secondary_key, HTTP_CACHE_SEC_KEY_LEN) != 0) {
183 node = eb32_next_dup(node);
184 entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL;
185 }
186
187 /* Expired entry */
188 if (entry && entry->expire <= now.tv_sec) {
189 eb32_delete(&entry->eb);
190 entry->eb.key = 0;
191 entry = NULL;
192 }
193
194 return entry;
195}
196
William Lallemand4da3f8a2017-10-31 14:33:34 +0100197static inline struct shared_context *shctx_ptr(struct cache *cache)
198{
199 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
200}
201
William Lallemand77c11972017-10-31 20:43:01 +0100202static inline struct shared_block *block_ptr(struct cache_entry *entry)
203{
204 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
205}
206
207
208
William Lallemand41db4602017-10-30 11:15:51 +0100209static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100210cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100211{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100212 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100213 return 0;
214}
215
Christopher Faulet95220e22018-12-07 17:34:39 +0100216static void
217cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
218{
219 struct cache_flt_conf *cconf = fconf->conf;
220
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200221 if (!(cconf->flags & CACHE_FLT_INIT))
222 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100223 free(cconf);
224}
225
William Lallemand4da3f8a2017-10-31 14:33:34 +0100226static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100227cache_store_check(struct proxy *px, struct flt_conf *fconf)
228{
229 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100230 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100231 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100232 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100233
William Lallemandd1d1e222019-08-28 15:22:49 +0200234 /* Find the cache corresponding to the name in the filter config. The
235 * cache will not be referenced now in the filter config because it is
236 * not fully allocated. This step will be performed during the cache
237 * post_check.
238 */
239 list_for_each_entry(cache, &caches_config, list) {
240 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100241 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100242 }
243
244 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
245 proxy_type_str(px), px->id, (char *)cconf->c.name);
246 return 1;
247
248 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100249 /* Here <cache> points on the cache the filter must use and <cconf>
250 * points on the cache filter configuration. */
251
252 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100253 * enabled and if it is after the cache. When the compression is before
254 * the cache, an error is returned. Also check if the cache filter must
255 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100256 list_for_each_entry(f, &px->filter_configs, list) {
257 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100258 /* The compression filter must be evaluated after the cache. */
259 if (comp) {
260 ha_alert("config: %s '%s': unable to enable the compression filter before "
261 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
262 return 1;
263 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100264 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200265 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100266 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200267 else if (f->id == fcgi_flt_id)
268 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100269 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
270 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200271 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100272 * declaration is required. */
273 ha_alert("config: %s '%s': require an explicit filter declaration "
274 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
275 return 1;
276 }
277
Christopher Fauletafd819c2018-12-11 08:57:45 +0100278 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100279 return 0;
280}
281
282static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100283cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100284{
Christopher Faulet65554e12020-03-06 14:52:06 +0100285 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100286
Christopher Faulet65554e12020-03-06 14:52:06 +0100287 st = pool_alloc_dirty(pool_head_cache_st);
288 if (st == NULL)
289 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100290
Christopher Faulet65554e12020-03-06 14:52:06 +0100291 st->first_block = NULL;
292 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100293
Christopher Faulet65554e12020-03-06 14:52:06 +0100294 /* Register post-analyzer on AN_RES_WAIT_HTTP */
295 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100296 return 1;
297}
298
Christopher Faulet65554e12020-03-06 14:52:06 +0100299static void
300cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100301{
302 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100303 struct cache_flt_conf *cconf = FLT_CONF(filter);
304 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100305 struct shared_context *shctx = shctx_ptr(cache);
306
William Lallemand49dc0482017-11-24 14:33:54 +0100307 /* Everything should be released in the http_end filter, but we need to do it
308 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100309 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100310 shctx_lock(shctx);
311 shctx_row_dec_hot(shctx, st->first_block);
312 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100313 }
314 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100315 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100316 filter->ctx = NULL;
317 }
William Lallemand49dc0482017-11-24 14:33:54 +0100318}
319
Christopher Faulet839791a2019-01-07 16:12:07 +0100320static int
321cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
322 unsigned an_bit)
323{
324 struct http_txn *txn = s->txn;
325 struct http_msg *msg = &txn->rsp;
326 struct cache_st *st = filter->ctx;
327
328 if (an_bit != AN_RES_WAIT_HTTP)
329 goto end;
330
331 /* Here we need to check if any compression filter precedes the cache
332 * filter. This is only possible when the compression is configured in
333 * the frontend while the cache filter is configured on the
334 * backend. This case cannot be detected during HAProxy startup. So in
335 * such cases, the cache is disabled.
336 */
337 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
338 pool_free(pool_head_cache_st, st);
339 filter->ctx = NULL;
340 }
341
342 end:
343 return 1;
344}
William Lallemand49dc0482017-11-24 14:33:54 +0100345
346static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100347cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
348{
349 struct cache_st *st = filter->ctx;
350
William Lallemand4da3f8a2017-10-31 14:33:34 +0100351 if (!(msg->chn->flags & CF_ISRESP) || !st)
352 return 1;
353
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200354 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100355 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100356 return 1;
357}
358
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200359static inline void disable_cache_entry(struct cache_st *st,
360 struct filter *filter, struct shared_context *shctx)
361{
362 struct cache_entry *object;
363
364 object = (struct cache_entry *)st->first_block->data;
365 filter->ctx = NULL; /* disable cache */
366 shctx_lock(shctx);
367 shctx_row_dec_hot(shctx, st->first_block);
368 object->eb.key = 0;
369 shctx_unlock(shctx);
370 pool_free(pool_head_cache_st, st);
371}
372
William Lallemand4da3f8a2017-10-31 14:33:34 +0100373static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100374cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
375 unsigned int offset, unsigned int len)
376{
Christopher Faulet95220e22018-12-07 17:34:39 +0100377 struct cache_flt_conf *cconf = FLT_CONF(filter);
378 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100379 struct cache_st *st = filter->ctx;
380 struct htx *htx = htxbuf(&msg->chn->buf);
381 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200382 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100383 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200384 unsigned int orig_len, to_forward;
385 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100386
387 if (!len)
388 return len;
389
390 if (!st->first_block) {
391 unregister_data_filter(s, msg->chn, filter);
392 return len;
393 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100394
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200395 chunk_reset(&trash);
396 orig_len = len;
397 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100398
399 htxret = htx_find_offset(htx, offset);
400 blk = htxret.blk;
401 offset = htxret.ret;
402 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100403 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200404 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100405 struct ist v;
406
407 switch (type) {
408 case HTX_BLK_UNUSED:
409 break;
410
411 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100412 v = htx_get_blk_value(htx, blk);
413 v.ptr += offset;
414 v.len -= offset;
415 if (v.len > len)
416 v.len = len;
417
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200418 info = (type << 28) + v.len;
419 chunk_memcat(&trash, (char *)&info, sizeof(info));
420 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100421 to_forward += v.len;
422 len -= v.len;
423 break;
424
425 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200426 /* Here offset must always be 0 because only
427 * DATA blocks can be partially transferred. */
428 if (offset)
429 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100430 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200431 goto end;
432
433 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
434 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100435 to_forward += sz;
436 len -= sz;
437 break;
438 }
439
440 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100441 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200442
443 end:
444 shctx_lock(shctx);
445 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
446 if (!fb) {
447 shctx_unlock(shctx);
448 goto no_cache;
449 }
450 shctx_unlock(shctx);
451
452 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
453 (unsigned char *)b_head(&trash), b_data(&trash));
454 if (ret < 0)
455 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100456
457 return to_forward;
458
459 no_cache:
460 disable_cache_entry(st, filter, shctx);
461 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200462 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100463}
464
465static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100466cache_store_http_end(struct stream *s, struct filter *filter,
467 struct http_msg *msg)
468{
469 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100470 struct cache_flt_conf *cconf = FLT_CONF(filter);
471 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100472 struct shared_context *shctx = shctx_ptr(cache);
473 struct cache_entry *object;
474
475 if (!(msg->chn->flags & CF_ISRESP))
476 return 1;
477
478 if (st && st->first_block) {
479
480 object = (struct cache_entry *)st->first_block->data;
481
William Lallemand4da3f8a2017-10-31 14:33:34 +0100482 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100483 /* The whole payload was cached, the entry can now be used. */
484 object->complete = 1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100486 shctx_row_dec_hot(shctx, st->first_block);
487 shctx_unlock(shctx);
488
489 }
490 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100491 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100492 filter->ctx = NULL;
493 }
494
495 return 1;
496}
497
498 /*
499 * This intends to be used when checking HTTP headers for some
500 * word=value directive. Return a pointer to the first character of value, if
501 * the word was not found or if there wasn't any value assigned ot it return NULL
502 */
503char *directive_value(const char *sample, int slen, const char *word, int wlen)
504{
505 int st = 0;
506
507 if (slen < wlen)
508 return 0;
509
510 while (wlen) {
511 char c = *sample ^ *word;
512 if (c && c != ('A' ^ 'a'))
513 return NULL;
514 sample++;
515 word++;
516 slen--;
517 wlen--;
518 }
519
520 while (slen) {
521 if (st == 0) {
522 if (*sample != '=')
523 return NULL;
524 sample++;
525 slen--;
526 st = 1;
527 continue;
528 } else {
529 return (char *)sample;
530 }
531 }
532
533 return NULL;
534}
535
536/*
537 * Return the maxage in seconds of an HTTP response.
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100538 * The returned value will always take the cache's configuration into account
539 * (cache->maxage) but the actual max age of the response will be set in the
540 * true_maxage parameter. It will be used to determine if a response is already
541 * stale or not.
William Lallemand4da3f8a2017-10-31 14:33:34 +0100542 * Compute the maxage using either:
543 * - the assigned max-age of the cache
544 * - the s-maxage directive
545 * - the max-age directive
546 * - (Expires - Data) headers
547 * - the default-max-age of the cache
548 *
549 */
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100550int http_calc_maxage(struct stream *s, struct cache *cache, int *true_maxage)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100551{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200552 struct htx *htx = htxbuf(&s->res.buf);
553 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100554 int smaxage = -1;
555 int maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100556 int expires = -1;
557 struct tm tm = {};
558 time_t expires_val = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100559
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200560 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
561 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100562
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200563 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
564 if (value) {
565 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100566
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200567 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
568 chunk_strncat(chk, "", 1);
Remi Tricot-Le Breton8c2db712020-10-30 14:26:13 +0100569 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100570 }
571
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200572 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
573 if (value) {
574 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200575
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200576 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
577 chunk_strncat(chk, "", 1);
Remi Tricot-Le Breton8c2db712020-10-30 14:26:13 +0100578 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100579 }
580 }
581
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100582 /* Look for Expires header if no s-maxage or max-age Cache-Control data
583 * was found. */
584 if (maxage == -1 && smaxage == -1) {
585 ctx.blk = NULL;
586 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
587 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
588 expires_val = my_timegm(&tm);
589 /* A request having an expiring date earlier
590 * than the current date should be considered as
591 * stale. */
592 expires = (expires_val >= now.tv_sec) ?
593 (expires_val - now.tv_sec) : 0;
594 }
595 else {
596 /* Following RFC 7234#5.3, an invalid date
597 * format must be treated as a date in the past
598 * so the cache entry must be seen as already
599 * expired. */
600 expires = 0;
601 }
602 }
603 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100604
605
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100606 if (smaxage > 0) {
607 if (true_maxage)
608 *true_maxage = smaxage;
William Lallemand49b44532017-11-24 18:53:43 +0100609 return MIN(smaxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100610 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100611
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100612 if (maxage > 0) {
613 if (true_maxage)
614 *true_maxage = maxage;
William Lallemand49b44532017-11-24 18:53:43 +0100615 return MIN(maxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100616 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100617
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100618 if (expires >= 0) {
619 if (true_maxage)
620 *true_maxage = expires;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100621 return MIN(expires, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100622 }
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100623
William Lallemand49b44532017-11-24 18:53:43 +0100624 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100625
626}
627
628
William Lallemanda400a3a2017-11-20 19:13:12 +0100629static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
630{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200631 struct cache_entry *object = (struct cache_entry *)block->data;
632
633 if (first == block && object->eb.key)
634 eb32_delete(&object->eb);
635 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100636}
637
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200638
639/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
640 * date value should be compared to a date determined by in a previous response (for
641 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
642 * value of the response's reception time (by decreasing order of priority). */
643static time_t get_last_modified_time(struct htx *htx)
644{
645 time_t last_modified = 0;
646 struct http_hdr_ctx ctx = { .blk = NULL };
647 struct tm tm = {};
648
649 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
650 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
651 last_modified = my_timegm(&tm);
652 }
653 }
654
655 if (!last_modified) {
656 ctx.blk = NULL;
657 if (http_find_header(htx, ist("date"), &ctx, 1)) {
658 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
659 last_modified = my_timegm(&tm);
660 }
661 }
662 }
663
664 /* Fallback on the current time if no "Last-Modified" or "Date" header
665 * was found. */
666 if (!last_modified)
667 last_modified = now.tv_sec;
668
669 return last_modified;
670}
671
William Lallemand41db4602017-10-30 11:15:51 +0100672/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100673 * Checks the vary header's value. The headers on which vary should be applied
674 * must be explicitely supported in the vary_information array (see cache.c). If
675 * any other header is mentioned, we won't store the response.
676 * Returns 1 if Vary-based storage can work, 0 otherwise.
677 */
678static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
679{
680 unsigned int vary_idx;
681 unsigned int vary_info_count;
682 const struct vary_hashing_information *vary_info;
683 struct http_hdr_ctx ctx = { .blk = NULL };
684
685 int retval = 1;
686
687 *vary_signature = 0;
688
689 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
690 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
691 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
692 vary_info = &vary_information[vary_idx];
693 if (isteqi(ctx.value, vary_info->hdr_name)) {
694 *vary_signature |= vary_info->value;
695 break;
696 }
697 }
698 retval = (vary_idx < vary_info_count);
699 }
700
701 return retval;
702}
703
704
705
706/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500707 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100708 * register a filter to store the data
709 */
710enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200711 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100712{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200713 long long hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100714 int effective_maxage = 0;
715 int true_maxage = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100716 struct http_txn *txn = s->txn;
717 struct http_msg *msg = &txn->rsp;
718 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100719 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100720 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100721 struct cache *cache = cconf->c.cache;
722 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100723 struct cache_st *cache_ctx = NULL;
724 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100725 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200726 struct htx *htx;
727 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200728 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200729 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200730 struct ist header_name = IST_NULL;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100731 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100732
William Lallemand4da3f8a2017-10-31 14:33:34 +0100733 /* Don't cache if the response came from a cache */
734 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
735 s->target == &http_cache_applet.obj_type) {
736 goto out;
737 }
738
739 /* cache only HTTP/1.1 */
740 if (!(txn->req.flags & HTTP_MSGF_VER_11))
741 goto out;
742
Willy Tarreau6905d182019-10-01 17:59:17 +0200743 /* cache only GET method */
744 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100745 goto out;
746
Willy Tarreauc9036c02019-01-11 19:38:25 +0100747 /* cache key was not computed */
748 if (!key)
749 goto out;
750
William Lallemand4da3f8a2017-10-31 14:33:34 +0100751 /* cache only 200 status code */
752 if (txn->status != 200)
753 goto out;
754
Christopher Faulet839791a2019-01-07 16:12:07 +0100755 /* Find the corresponding filter instance for the current stream */
756 list_for_each_entry(filter, &s->strm_flt.filters, list) {
757 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
758 /* No filter ctx, don't cache anything */
759 if (!filter->ctx)
760 goto out;
761 cache_ctx = filter->ctx;
762 break;
763 }
764 }
765
766 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200767 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100768
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200769 /* Do not cache too big objects. */
770 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
771 htx->data + htx->extra > shctx->max_obj_size)
772 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100773
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100774 /* Only a subset of headers are supported in our Vary implementation. If
775 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100776 * able to use the cache. Likewise, if Vary header support is disabled,
777 * avoid caching responses that contain such a header. */
778 ctx.blk = NULL;
779 if (cache->vary_processing_enabled) {
780 if (!http_check_vary_header(htx, &vary_signature))
781 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100782 if (vary_signature)
783 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100784 }
785 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200786 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100787 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100788
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200789 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100790
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100791 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200792 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100793
794 shctx_lock(shctx);
795 old = entry_exist(cache, txn->cache_hash);
796 if (old) {
797 if (vary_signature)
798 old = secondary_entry_exist(cconf->c.cache, old,
799 txn->cache_secondary_hash);
800 if (old) {
801 if (!old->complete) {
802 /* An entry with the same primary key is already being
803 * created, we should not try to store the current
804 * response because it will waste space in the cache. */
805 shctx_unlock(shctx);
806 goto out;
807 }
808 eb32_delete(&old->eb);
809 old->eb.key = 0;
810 }
811 }
812 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry));
813 if (!first) {
814 shctx_unlock(shctx);
815 goto out;
816 }
817 /* the received memory is not initialized, we need at least to mark
818 * the object as not indexed yet.
819 */
820 object = (struct cache_entry *)first->data;
821 memset(object, 0, sizeof(*object));
822 object->eb.key = key;
823 object->secondary_key_signature = vary_signature;
824 /* We need to temporarily set a valid expiring time until the actual one
825 * is set by the end of this function (in case of concurrent accesses to
826 * the same resource). This way the second access will find an existing
827 * but not yet usable entry in the tree and will avoid storing its data. */
828 object->expire = now.tv_sec + 2;
829
830 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
831 if (vary_signature)
832 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
833
834 /* Insert the entry in the tree even if the payload is not cached yet. */
835 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
836 object->eb.key = 0;
837 shctx_unlock(shctx);
838 goto out;
839 }
840 shctx_unlock(shctx);
841
842 /* reserve space for the cache_entry structure */
843 first->len = sizeof(struct cache_entry);
844 first->last_append = NULL;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100845
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100846 /* Determine the entry's maximum age (taking into account the cache's
847 * configuration) as well as the response's explicit max age (extracted
848 * from cache-control directives or the expires header). */
849 effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage);
850
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200851 ctx.blk = NULL;
852 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
853 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
854 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
855 hdr_age = CACHE_ENTRY_MAX_AGE;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100856 /* A response with an Age value greater than its
857 * announced max age is stale and should not be stored. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100858 object->age = hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100859 if (unlikely(object->age > true_maxage))
860 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100861 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200862 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100863 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100864
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200865 /* Build a last-modified time that will be stored in the cache_entry and
866 * compared to a future If-Modified-Since client header. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100867 object->last_modified = get_last_modified_time(htx);
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200868
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200869 chunk_reset(&trash);
870 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
871 struct htx_blk *blk = htx_get_blk(htx, pos);
872 enum htx_blk_type type = htx_get_blk_type(blk);
873 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100874
Christopher Fauletb0667472019-09-03 22:22:12 +0200875 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200876 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
877 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200878
879 /* Look for optional ETag header.
880 * We need to store the offset of the ETag value in order for
881 * future conditional requests to be able to perform ETag
882 * comparisons. */
883 if (type == HTX_BLK_HDR) {
884 header_name = htx_get_blk_name(htx, blk);
885 if (isteq(header_name, ist("etag"))) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100886 object->etag_length = sz - istlen(header_name);
887 object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200888 }
889 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200890 if (type == HTX_BLK_EOH)
891 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200892 }
893
Christopher Fauletb0667472019-09-03 22:22:12 +0200894 /* Do not cache objects if the headers are too big. */
895 if (hdrs_len > htx->size - global.tune.maxrewrite)
896 goto out;
897
William Lallemand4da3f8a2017-10-31 14:33:34 +0100898 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100899 if (!shctx_row_reserve_hot(shctx, first, trash.data)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100900 shctx_unlock(shctx);
901 goto out;
902 }
903 shctx_unlock(shctx);
904
William Lallemand4da3f8a2017-10-31 14:33:34 +0100905 /* cache the headers in a http action because it allows to chose what
906 * to cache, for example you might want to cache a response before
907 * modifying some HTTP headers, or on the contrary after modifying
908 * those headers.
909 */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100910 /* does not need to be locked because it's in the "hot" list,
911 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200912 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
913 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100914
915 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100916 if (cache_ctx) {
917 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100918
Christopher Faulet839791a2019-01-07 16:12:07 +0100919 /* store latest value and expiration time */
920 object->latest_validation = now.tv_sec;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100921 object->expire = now.tv_sec + effective_maxage;
Christopher Faulet839791a2019-01-07 16:12:07 +0100922 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100923 }
924
925out:
926 /* if does not cache */
927 if (first) {
928 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100929 first->len = 0;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100930 if (object->eb.key)
931 eb32_delete(&object->eb);
William Lallemand08727662017-11-21 20:01:27 +0100932 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100933 shctx_row_dec_hot(shctx, first);
934 shctx_unlock(shctx);
935 }
936
William Lallemand41db4602017-10-30 11:15:51 +0100937 return ACT_RET_CONT;
938}
939
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100940#define HTX_CACHE_INIT 0 /* Initial state. */
941#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
942#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200943#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
944#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100945
William Lallemandecb73b12017-11-24 14:33:55 +0100946static void http_cache_applet_release(struct appctx *appctx)
947{
Christopher Faulet95220e22018-12-07 17:34:39 +0100948 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100949 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100950 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100951 struct shared_block *first = block_ptr(cache_ptr);
952
953 shctx_lock(shctx_ptr(cache));
954 shctx_row_dec_hot(shctx_ptr(cache), first);
955 shctx_unlock(shctx_ptr(cache));
956}
957
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200958
959static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
960 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100961{
Christopher Faulet95220e22018-12-07 17:34:39 +0100962 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
963 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200964 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200965 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200966 unsigned int max, total;
967 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100968
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200969 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
970 if (!max)
971 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200972 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200973 ? (info & 0xff) + ((info >> 8) & 0xfffff)
974 : info & 0xfffffff);
975 if (blksz > max)
976 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100977
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200978 blk = htx_add_blk(htx, type, blksz);
979 if (!blk)
980 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100981
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200982 blk->info = info;
983 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200984 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200985 while (blksz) {
986 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200987 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200988 offset += max;
989 blksz -= max;
990 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200991 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200992 if (blksz || offset == shctx->block_size) {
993 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
994 offset = 0;
995 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100996 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200997 appctx->ctx.cache.offset = offset;
998 appctx->ctx.cache.next = shblk;
999 appctx->ctx.cache.sent += total;
1000 return total;
1001}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001002
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001003static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
1004 uint32_t info, struct shared_block *shblk, unsigned int offset)
1005{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001006
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001007 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1008 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
1009 unsigned int max, total, rem_data;
1010 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001011
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001012 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1013 if (!max)
1014 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001015
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001016 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001017 if (appctx->ctx.cache.rem_data) {
1018 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001019 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001020 }
1021 else {
1022 blksz = (info & 0xfffffff);
1023 total = 4;
1024 }
1025 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001026 rem_data = blksz - max;
1027 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001028 }
1029
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001030 while (blksz) {
1031 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001032
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001033 max = MIN(blksz, shctx->block_size - offset);
1034 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1035 offset += sz;
1036 blksz -= sz;
1037 total += sz;
1038 if (sz < max)
1039 break;
1040 if (blksz || offset == shctx->block_size) {
1041 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1042 offset = 0;
1043 }
1044 }
1045
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001046 appctx->ctx.cache.offset = offset;
1047 appctx->ctx.cache.next = shblk;
1048 appctx->ctx.cache.sent += total;
1049 appctx->ctx.cache.rem_data = rem_data + blksz;
1050 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001051}
1052
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001053static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1054 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001055{
Christopher Faulet95220e22018-12-07 17:34:39 +01001056 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1057 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001058 struct shared_block *shblk;
1059 unsigned int offset, sz;
1060 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001061
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001062 while (len) {
1063 enum htx_blk_type type;
1064 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001065
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001066 shblk = appctx->ctx.cache.next;
1067 offset = appctx->ctx.cache.offset;
1068 if (appctx->ctx.cache.rem_data) {
1069 type = HTX_BLK_DATA;
1070 info = 0;
1071 goto add_data_blk;
1072 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001073
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001074 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001075 sz = MIN(4, shctx->block_size - offset);
1076 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1077 offset += sz;
1078 if (sz < 4) {
1079 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1080 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1081 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001082 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001083
1084 /* Get payload of the next HTX block and insert it. */
1085 type = (info >> 28);
1086 if (type != HTX_BLK_DATA)
1087 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1088 else {
1089 add_data_blk:
1090 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001091 }
1092
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001093 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001094 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001095 total += ret;
1096 len -= ret;
1097
1098 if (appctx->ctx.cache.rem_data || type == mark)
1099 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001100 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001101
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001102 return total;
1103}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001104
1105static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1106{
1107 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1108 unsigned int age;
1109 char *end;
1110
1111 chunk_reset(&trash);
1112 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1113 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1114 age = CACHE_ENTRY_MAX_AGE;
1115 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1116 b_set_data(&trash, end - b_head(&trash));
1117 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1118 return 0;
1119 return 1;
1120}
1121
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001122static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001123{
1124 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1125 struct shared_block *first = block_ptr(cache_ptr);
1126 struct stream_interface *si = appctx->owner;
1127 struct channel *req = si_oc(si);
1128 struct channel *res = si_ic(si);
1129 struct htx *req_htx, *res_htx;
1130 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001131 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001132 size_t ret, total = 0;
1133
1134 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001135 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001136
1137 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1138 goto out;
1139
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001140 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001141 if (!b_size(&res->buf)) {
1142 si_rx_room_blk(si);
1143 goto out;
1144 }
1145
Willy Tarreauefef3232018-12-16 00:37:45 +01001146 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001147 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001148
1149 if (appctx->st0 == HTX_CACHE_INIT) {
1150 appctx->ctx.cache.next = block_ptr(cache_ptr);
1151 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1152 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001153 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001154 appctx->st0 = HTX_CACHE_HEADER;
1155 }
1156
1157 if (appctx->st0 == HTX_CACHE_HEADER) {
1158 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001159 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1160 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1161 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1162 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001163 goto error;
1164
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001165 /* In case of a conditional request, we might want to send a
1166 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001167 if (appctx->ctx.cache.send_notmodified) {
1168 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1169 /* If replacing the status code fails we need to send the full response. */
1170 appctx->ctx.cache.send_notmodified = 0;
1171 }
1172 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001173
1174 /* Skip response body for HEAD requests or in case of "304 Not
1175 * Modified" response. */
1176 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001177 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001178 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001179 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001180 }
1181
1182 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001183 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1184 if (len) {
1185 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1186 if (ret < len) {
1187 si_rx_room_blk(si);
1188 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001189 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001190 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001191 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001192 }
1193
1194 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001195 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001196 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1197 si_rx_room_blk(si);
1198 goto out;
1199 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001200 appctx->st0 = HTX_CACHE_END;
1201 }
1202
1203 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001204 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001205 res->flags |= CF_READ_NULL;
1206 si_shutr(si);
1207 }
1208
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001209 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001210 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001211 if (total)
1212 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001213 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001214
1215 /* eat the whole request */
1216 if (co_data(req)) {
1217 req_htx = htx_from_buf(&req->buf);
1218 co_htx_skip(req, req_htx, co_data(req));
1219 htx_to_buf(req_htx, &req->buf);
1220 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001221 return;
1222
1223 error:
1224 /* Sent and HTTP error 500 */
1225 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001226 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001227 res->buf.data = b_data(errmsg);
1228 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1229 res_htx = htx_from_buf(&res->buf);
1230
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001231 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001232 appctx->st0 = HTX_CACHE_END;
1233 goto end;
1234}
1235
1236
Christopher Faulet95220e22018-12-07 17:34:39 +01001237static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001238{
1239 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001240 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001241
Christopher Faulet95220e22018-12-07 17:34:39 +01001242 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001243 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001244 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001245 }
1246
1247 /* check if a cache filter was already registered with this cache
1248 * name, if that's the case, must use it. */
1249 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001250 if (fconf->id == cache_store_flt_id) {
1251 cconf = fconf->conf;
1252 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1253 rule->arg.act.p[0] = cconf;
1254 return 1;
1255 }
William Lallemand41db4602017-10-30 11:15:51 +01001256 }
1257 }
1258
Christopher Faulet95220e22018-12-07 17:34:39 +01001259 /* Create the filter cache config */
1260 cconf = calloc(1, sizeof(*cconf));
1261 if (!cconf) {
1262 memprintf(err, "out of memory\n");
1263 goto err;
1264 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001265 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001266 cconf->c.name = strdup(name);
1267 if (!cconf->c.name) {
1268 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001269 goto err;
1270 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001271
William Lallemand41db4602017-10-30 11:15:51 +01001272 /* register a filter to fill the cache buffer */
1273 fconf = calloc(1, sizeof(*fconf));
1274 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001275 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001276 goto err;
1277 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001278 fconf->id = cache_store_flt_id;
1279 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001280 fconf->ops = &cache_ops;
1281 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1282
Christopher Faulet95220e22018-12-07 17:34:39 +01001283 rule->arg.act.p[0] = cconf;
1284 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001285
Christopher Faulet95220e22018-12-07 17:34:39 +01001286 err:
1287 free(cconf);
1288 return 0;
1289}
1290
1291enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1292 struct act_rule *rule, char **err)
1293{
1294 rule->action = ACT_CUSTOM;
1295 rule->action_ptr = http_action_store_cache;
1296
1297 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1298 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001299
Christopher Faulet95220e22018-12-07 17:34:39 +01001300 (*orig_arg)++;
1301 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001302}
1303
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001304/* This produces a sha1 hash of the concatenation of the HTTP method,
1305 * the first occurrence of the Host header followed by the path component
1306 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001307int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001308{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001309 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001310 struct htx *htx = htxbuf(&s->req.buf);
1311 struct htx_sl *sl;
1312 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001313 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001314 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001315 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001316
William Lallemandf528fff2017-11-23 19:43:17 +01001317 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001318 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001319
1320 switch (txn->meth) {
1321 case HTTP_METH_HEAD:
1322 case HTTP_METH_GET:
1323 chunk_memcat(trash, "GET", 3);
1324 break;
1325 default:
1326 return 0;
1327 }
1328
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001329 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001330 uri = htx_sl_req_uri(sl); // whole uri
1331 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001332 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001333
1334 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1335 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1336 * URIs are almost always sent in absolute form with their scheme. In
1337 * this case, the scheme is almost always "https". In order to support
1338 * sharing of cache objects between H1 and H2, we'll hash the absolute
1339 * URI whenever known, or prepend "https://" + the Host header for
1340 * relative URIs. The difference will only appear on absolute HTTP/1
1341 * requests sent to an origin server, which practically is never met in
1342 * the real world so we don't care about the ability to share the same
1343 * key here.URIs are normalized from the absolute URI to an origin form as
1344 * well.
1345 */
1346 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001347 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001348 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1349 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001350 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001351 }
1352
1353 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001354
1355 /* hash everything */
1356 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001357 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001358 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1359
1360 return 1;
1361}
1362
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001363/* Looks for "If-None-Match" headers in the request and compares their value
1364 * with the one that might have been stored in the cache_entry. If any of them
1365 * matches, a "304 Not Modified" response should be sent instead of the cached
1366 * data.
1367 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001368 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1369 *
1370 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1371 * header and compare its value (date) to the one stored in the cache_entry.
1372 * If the request's date is later than the cached one, we also send a
1373 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1374 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001375 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1376 */
1377static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1378 struct cache_entry *entry)
1379{
1380 int retval = 0;
1381
1382 struct http_hdr_ctx ctx = { .blk = NULL };
1383 struct ist cache_entry_etag = IST_NULL;
1384 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001385 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001386
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001387 struct tm tm = {};
1388 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001389
1390 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001391 * cache_entry's ETag in order to perform comparisons.
1392 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001393 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001394 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001395
1396 /* A '*' matches everything. */
1397 if (isteq(ctx.value, ist("*")) != 0) {
1398 retval = 1;
1399 break;
1400 }
1401
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001402 /* No need to rebuild an etag if none was stored in the cache. */
1403 if (entry->etag_length == 0)
1404 break;
1405
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001406 /* Rebuild the stored ETag. */
1407 if (etag_buffer == NULL) {
1408 etag_buffer = get_trash_chunk();
1409
1410 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1411 (unsigned char*)b_orig(etag_buffer),
1412 entry->etag_offset, entry->etag_length) == 0) {
1413 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1414 } else {
1415 /* We could not rebuild the ETag in one go, we
1416 * won't send a "304 Not Modified" response. */
1417 break;
1418 }
1419 }
1420
1421 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1422 retval = 1;
1423 break;
1424 }
1425 }
1426
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001427 /* If the request did not contain an "If-None-Match" header, we look for
1428 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1429 if (retval == 0 && if_none_match_found == 0) {
1430 ctx.blk = NULL;
1431 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1432 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1433 if_modified_since = my_timegm(&tm);
1434
1435 /* We send a "304 Not Modified" response if the
1436 * entry's last modified date is earlier than
1437 * the one found in the "If-Modified-Since"
1438 * header. */
1439 retval = (entry->last_modified <= if_modified_since);
1440 }
1441 }
1442 }
1443
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001444 return retval;
1445}
1446
William Lallemand41db4602017-10-30 11:15:51 +01001447enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1448 struct session *sess, struct stream *s, int flags)
1449{
William Lallemand77c11972017-10-31 20:43:01 +01001450
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001451 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001452 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001453 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1454 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001455 struct shared_block *entry_block;
1456
William Lallemand77c11972017-10-31 20:43:01 +01001457
Willy Tarreau6905d182019-10-01 17:59:17 +02001458 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1459 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001460 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001461 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001462 txn->flags |= TX_CACHE_IGNORE;
1463
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001464 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001465
Willy Tarreau504455c2017-12-22 17:47:35 +01001466 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1467 return ACT_RET_CONT;
1468
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001469 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001470 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001471
Willy Tarreau504455c2017-12-22 17:47:35 +01001472 if (s->txn->flags & TX_CACHE_IGNORE)
1473 return ACT_RET_CONT;
1474
Willy Tarreaua1214a52018-12-14 14:00:25 +01001475 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001476 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001477 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001478 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001479
William Lallemanda400a3a2017-11-20 19:13:12 +01001480 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001481 res = entry_exist(cache, s->txn->cache_hash);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001482 /* We must not use an entry that is not complete. */
1483 if (res && res->complete) {
William Lallemand77c11972017-10-31 20:43:01 +01001484 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001485 entry_block = block_ptr(res);
1486 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001487 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001488
1489 /* In case of Vary, we could have multiple entries with the same
1490 * primary hash. We need to calculate the secondary has in order
1491 * to find the actual entry we want (if it exists). */
1492 if (res->secondary_key_signature) {
1493 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1494 shctx_lock(shctx_ptr(cache));
1495 sec_entry = secondary_entry_exist(cache, res,
1496 s->txn->cache_secondary_hash);
1497 if (sec_entry && sec_entry != res) {
1498 /* The wrong row was added to the hot list. */
1499 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1500 entry_block = block_ptr(sec_entry);
1501 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1502 }
1503 res = sec_entry;
1504 shctx_unlock(shctx_ptr(cache));
1505 }
1506 else
1507 res = NULL;
1508 }
1509
1510 /* We looked for a valid secondary entry and could not find one,
1511 * the request must be forwarded to the server. */
1512 if (!res) {
1513 shctx_lock(shctx_ptr(cache));
1514 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1515 shctx_unlock(shctx_ptr(cache));
1516 return ACT_RET_CONT;
1517 }
1518
William Lallemand77c11972017-10-31 20:43:01 +01001519 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001520 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001521 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001522 appctx->rule = rule;
1523 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001524 appctx->ctx.cache.next = NULL;
1525 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001526 appctx->ctx.cache.send_notmodified =
1527 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001528
1529 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001530 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001531 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001532 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001533 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001534 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001535 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001536 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001537 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001538 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001539 }
1540 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001541 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001542
1543 /* Shared context does not need to be locked while we calculate the
1544 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001545 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001546 /* Build a complete secondary hash until the server response
1547 * tells us which fields should be kept (if any). */
1548 http_request_prebuild_full_secondary_key(s);
1549 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001550 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001551}
1552
1553
1554enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1555 struct act_rule *rule, char **err)
1556{
William Lallemand41db4602017-10-30 11:15:51 +01001557 rule->action = ACT_CUSTOM;
1558 rule->action_ptr = http_action_req_cache_use;
1559
Christopher Faulet95220e22018-12-07 17:34:39 +01001560 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001561 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001562
1563 (*orig_arg)++;
1564 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001565}
1566
1567int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1568{
1569 int err_code = 0;
1570
1571 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1572
1573 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001574 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001575 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001576 err_code |= ERR_ALERT | ERR_ABORT;
1577 goto out;
1578 }
1579
1580 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1581 err_code |= ERR_ABORT;
1582 goto out;
1583 }
1584
1585 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001586 struct cache *cache_config;
1587
William Lallemand41db4602017-10-30 11:15:51 +01001588 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1589 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001590 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001591 err_code |= ERR_ALERT | ERR_ABORT;
1592 goto out;
1593 }
1594
1595 strlcpy2(tmp_cache_config->id, args[1], 33);
1596 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001597 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001598 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001599 err_code |= ERR_WARN;
1600 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001601
1602 list_for_each_entry(cache_config, &caches_config, list) {
1603 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1604 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1605 file, linenum, tmp_cache_config->id);
1606 err_code |= ERR_ALERT | ERR_ABORT;
1607 goto out;
1608 }
1609 }
1610
William Lallemand49b44532017-11-24 18:53:43 +01001611 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001612 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001613 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001614 }
1615 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001616 unsigned long int maxsize;
1617 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001618
1619 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1620 err_code |= ERR_ABORT;
1621 goto out;
1622 }
1623
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001624 maxsize = strtoul(args[1], &err, 10);
1625 if (err == args[1] || *err != '\0') {
1626 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1627 file, linenum, args[1]);
1628 err_code |= ERR_ABORT;
1629 goto out;
1630 }
1631
1632 if (maxsize > (UINT_MAX >> 20)) {
1633 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1634 file, linenum, args[1], UINT_MAX >> 20);
1635 err_code |= ERR_ABORT;
1636 goto out;
1637 }
1638
William Lallemand41db4602017-10-30 11:15:51 +01001639 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001640 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001641 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001642 } else if (strcmp(args[0], "max-age") == 0) {
1643 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1644 err_code |= ERR_ABORT;
1645 goto out;
1646 }
1647
1648 if (!*args[1]) {
1649 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1650 file, linenum, args[0]);
1651 err_code |= ERR_WARN;
1652 }
1653
1654 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001655 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001656 unsigned int maxobjsz;
1657 char *err;
1658
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001659 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1660 err_code |= ERR_ABORT;
1661 goto out;
1662 }
1663
1664 if (!*args[1]) {
1665 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1666 file, linenum, args[0]);
1667 err_code |= ERR_WARN;
1668 }
1669
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001670 maxobjsz = strtoul(args[1], &err, 10);
1671 if (err == args[1] || *err != '\0') {
1672 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1673 file, linenum, args[1]);
1674 err_code |= ERR_ABORT;
1675 goto out;
1676 }
1677 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001678 } else if (strcmp(args[0], "process-vary") == 0) {
1679 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1680 err_code |= ERR_ABORT;
1681 goto out;
1682 }
1683
1684 if (!*args[1]) {
1685 ha_warning("parsing [%s:%d]: '%s' expects 0 or 1 (disable or enable vary processing).\n",
1686 file, linenum, args[0]);
1687 err_code |= ERR_WARN;
1688 }
1689
1690 tmp_cache_config->vary_processing_enabled = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001691 }
1692 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001693 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001694 err_code |= ERR_ALERT | ERR_FATAL;
1695 goto out;
1696 }
1697out:
1698 return err_code;
1699}
1700
1701/* once the cache section is parsed */
1702
1703int cfg_post_parse_section_cache()
1704{
William Lallemand41db4602017-10-30 11:15:51 +01001705 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001706
1707 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001708
1709 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001710 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001711 err_code |= ERR_FATAL | ERR_ALERT;
1712 goto out;
1713 }
1714
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001715 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001716 /* Default max. file size is a 256th of the cache size. */
1717 tmp_cache_config->maxobjsz =
1718 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001719 }
1720 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1721 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1722 err_code |= ERR_FATAL | ERR_ALERT;
1723 goto out;
1724 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001725
William Lallemandd1d1e222019-08-28 15:22:49 +02001726 /* add to the list of cache to init and reinit tmp_cache_config
1727 * for next cache section, if any.
1728 */
1729 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1730 tmp_cache_config = NULL;
1731 return err_code;
1732 }
1733out:
1734 free(tmp_cache_config);
1735 tmp_cache_config = NULL;
1736 return err_code;
1737
1738}
1739
1740int post_check_cache()
1741{
1742 struct proxy *px;
1743 struct cache *back, *cache_config, *cache;
1744 struct shared_context *shctx;
1745 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001746 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02001747
1748 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1749
1750 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1751 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001752
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001753 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001754 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001755 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001756 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001757 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001758
1759 err_code |= ERR_FATAL | ERR_ALERT;
1760 goto out;
1761 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001762 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001763 /* the cache structure is stored in the shctx and added to the
1764 * caches list, we can remove the entry from the caches_config
1765 * list */
1766 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001767 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001768 cache->entries = EB_ROOT;
William Lallemand41db4602017-10-30 11:15:51 +01001769 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001770 LIST_DEL(&cache_config->list);
1771 free(cache_config);
1772
1773 /* Find all references for this cache in the existing filters
1774 * (over all proxies) and reference it in matching filters.
1775 */
1776 for (px = proxies_list; px; px = px->next) {
1777 struct flt_conf *fconf;
1778 struct cache_flt_conf *cconf;
1779
1780 list_for_each_entry(fconf, &px->filter_configs, list) {
1781 if (fconf->id != cache_store_flt_id)
1782 continue;
1783
1784 cconf = fconf->conf;
1785 if (!strcmp(cache->id, cconf->c.name)) {
1786 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001787 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001788 cconf->c.cache = cache;
1789 break;
1790 }
1791 }
1792 }
William Lallemand41db4602017-10-30 11:15:51 +01001793 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001794
William Lallemand41db4602017-10-30 11:15:51 +01001795out:
William Lallemand41db4602017-10-30 11:15:51 +01001796 return err_code;
1797
William Lallemand41db4602017-10-30 11:15:51 +01001798}
1799
William Lallemand41db4602017-10-30 11:15:51 +01001800struct flt_ops cache_ops = {
1801 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001802 .check = cache_store_check,
1803 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001804
Christopher Faulet65554e12020-03-06 14:52:06 +01001805 /* Handle stream init/deinit */
1806 .attach = cache_store_strm_init,
1807 .detach = cache_store_strm_deinit,
1808
William Lallemand4da3f8a2017-10-31 14:33:34 +01001809 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001810 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001811
1812 /* Filter HTTP requests and responses */
1813 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001814 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001815 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001816};
1817
Christopher Faulet99a17a22018-12-11 09:18:27 +01001818
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001819int accept_encoding_cmp(const void *a, const void *b)
1820{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001821 unsigned int int_a = *(unsigned int*)a;
1822 unsigned int int_b = *(unsigned int*)b;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001823
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001824 if (int_a < int_b)
1825 return -1;
1826 if (int_a > int_b)
1827 return 1;
1828 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001829}
1830
Tim Duesterhus23b29452020-11-24 22:22:56 +01001831#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001832/*
1833 * Build a hash of the accept-encoding header. The different parts of the
1834 * header value are first sorted, appended and then a crc is calculated
1835 * for the newly constructed buffer.
1836 * Returns 0 in case of success.
1837 */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001838static int accept_encoding_normalizer(struct ist full_value, char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001839{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001840 unsigned int values[ACCEPT_ENCODING_MAX_ENTRIES] = {};
Tim Duesterhus23b29452020-11-24 22:22:56 +01001841 size_t count = 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001842 char *comma = NULL;
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001843 unsigned int hash_value = 0;
1844 unsigned int prev = 0, curr = 0;
1845
1846 /* Turn accept-encoding value to lower case */
1847 full_value = ist2bin_lc(istptr(full_value), full_value);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001848
1849 /* The hash will be built out of a sorted list of accepted encodings. */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001850 while (count < (ACCEPT_ENCODING_MAX_ENTRIES - 1) && (comma = istchr(full_value, ',')) != NULL) {
1851 size_t length = comma - istptr(full_value);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001852
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001853 values[count++] = hash_crc32(istptr(full_value), length);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001854
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001855 full_value = istadv(full_value, length + 1);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001856
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001857 }
1858 values[count++] = hash_crc32(istptr(full_value), istlen(full_value));
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001859
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001860 /* Sort the values alphabetically. */
1861 qsort(values, count, sizeof(*values), &accept_encoding_cmp);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001862
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001863 while (count) {
1864 curr = values[--count];
1865 if (curr != prev) {
1866 hash_value ^= curr;
1867 }
1868 prev = curr;
1869 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001870
1871 memcpy(buf, &hash_value, sizeof(hash_value));
1872 *buf_len = sizeof(hash_value);
1873
Tim Duesterhus23b29452020-11-24 22:22:56 +01001874 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001875}
Tim Duesterhus23b29452020-11-24 22:22:56 +01001876#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001877
1878/*
1879 * Normalizer used by default for User-Agent and Referer headers. It only
1880 * calculates a simple crc of the whole value.
1881 * Returns 0 in case of success.
1882 */
1883static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len)
1884{
1885 int hash_value = 0;
1886
1887 hash_value = hash_crc32(istptr(value), istlen(value));
1888
1889 memcpy(buf, &hash_value, sizeof(hash_value));
1890 *buf_len = sizeof(hash_value);
1891
1892 return 0;
1893}
1894
1895
1896/*
1897 * Pre-calculate the hashes of all the supported headers (in our Vary
1898 * implementation) of a given request. We have to calculate all the hashes
1899 * in advance because the actual Vary signature won't be known until the first
1900 * response.
1901 * Only the first occurrence of every header will be taken into account in the
1902 * hash.
1903 * If the header is not present, the hash portion of the given header will be
1904 * filled with zeros.
1905 * Returns 0 in case of success.
1906 */
1907static int http_request_prebuild_full_secondary_key(struct stream *s)
1908{
1909 struct http_txn *txn = s->txn;
1910 struct htx *htx = htxbuf(&s->req.buf);
1911 struct http_hdr_ctx ctx = { .blk = NULL };
1912
1913 unsigned int idx;
1914 const struct vary_hashing_information *info = NULL;
1915 unsigned int hash_length = 0;
1916 int retval = 0;
1917 int offset = 0;
1918
1919 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1920 info = &vary_information[idx];
1921
1922 ctx.blk = NULL;
1923 if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) {
1924 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1925 offset += hash_length;
1926 }
1927 else {
1928 /* Fill hash with 0s. */
1929 hash_length = info->hash_length;
1930 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1931 offset += hash_length;
1932 }
1933 }
1934
1935 return retval;
1936}
1937
1938
1939/*
1940 * Calculate the secondary key for a request for which we already have a known
1941 * vary signature. The key is made by aggregating hashes calculated for every
1942 * header mentioned in the vary signature.
1943 * Only the first occurrence of every header will be taken into account in the
1944 * hash.
1945 * If the header is not present, the hash portion of the given header will be
1946 * filled with zeros.
1947 * Returns 0 in case of success.
1948 */
1949static int http_request_build_secondary_key(struct stream *s, int vary_signature)
1950{
1951 struct http_txn *txn = s->txn;
1952 struct htx *htx = htxbuf(&s->req.buf);
1953 struct http_hdr_ctx ctx = { .blk = NULL };
1954
1955 unsigned int idx;
1956 const struct vary_hashing_information *info = NULL;
1957 unsigned int hash_length = 0;
1958 int retval = 0;
1959 int offset = 0;
1960
1961 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1962 info = &vary_information[idx];
1963
1964 ctx.blk = NULL;
1965 if ((vary_signature & info->value) && info->norm_fn != NULL &&
1966 http_find_header(htx, info->hdr_name, &ctx, 1)) {
1967 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1968 offset += hash_length;
1969 }
1970 else {
1971 /* Fill hash with 0s. */
1972 hash_length = info->hash_length;
1973 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1974 offset += hash_length;
1975 }
1976 }
1977
1978 return retval;
1979}
1980
1981/*
1982 * Build the actual secondary key of a given request out of the prebuilt key and
1983 * the actual vary signature (extracted from the response).
1984 * Returns 0 in case of success.
1985 */
1986static int http_request_reduce_secondary_key(unsigned int vary_signature,
1987 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
1988{
1989 int offset = 0;
1990 int global_offset = 0;
1991 int vary_info_count = 0;
1992 int keep = 0;
1993 unsigned int vary_idx;
1994 const struct vary_hashing_information *vary_info;
1995
1996 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
1997 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
1998 vary_info = &vary_information[vary_idx];
1999 keep = (vary_signature & vary_info->value) ? 0xff : 0;
2000
2001 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
2002 prebuilt_key[global_offset] &= keep;
2003 }
2004 }
2005
2006 return 0;
2007}
2008
2009
Christopher Faulet99a17a22018-12-11 09:18:27 +01002010
2011static int
2012parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
2013 struct flt_conf *fconf, char **err, void *private)
2014{
2015 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01002016 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002017 char *name = NULL;
2018 int pos = *cur_arg;
2019
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002020 /* Get the cache filter name. <pos> point on "cache" keyword */
2021 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02002022 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002023 goto error;
2024 }
2025 name = strdup(args[pos + 1]);
2026 if (!name) {
2027 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
2028 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002029 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002030 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002031
2032 /* Check if an implicit filter with the same name already exists. If so,
2033 * we remove the implicit filter to use the explicit one. */
2034 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
2035 if (f->id != cache_store_flt_id)
2036 continue;
2037
2038 cconf = f->conf;
2039 if (strcmp(name, cconf->c.name)) {
2040 cconf = NULL;
2041 continue;
2042 }
2043
2044 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2045 cconf = NULL;
2046 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2047 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002048 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002049 }
2050
2051 /* Remove the implicit filter. <cconf> is kept for the explicit one */
2052 LIST_DEL(&f->list);
2053 free(f);
2054 free(name);
2055 break;
2056 }
2057
2058 /* No implicit cache filter found, create configuration for the explicit one */
2059 if (!cconf) {
2060 cconf = calloc(1, sizeof(*cconf));
2061 if (!cconf) {
2062 memprintf(err, "%s: out of memory", args[*cur_arg]);
2063 goto error;
2064 }
2065 cconf->c.name = name;
2066 }
2067
2068 cconf->flags = 0;
2069 fconf->id = cache_store_flt_id;
2070 fconf->conf = cconf;
2071 fconf->ops = &cache_ops;
2072
2073 *cur_arg = pos;
2074 return 0;
2075
2076 error:
2077 free(name);
2078 free(cconf);
2079 return -1;
2080}
2081
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002082static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002083{
2084 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2085 return 1;
2086
2087 return 0;
2088}
2089
2090static int cli_io_handler_show_cache(struct appctx *appctx)
2091{
2092 struct cache* cache = appctx->ctx.cli.p0;
2093 struct stream_interface *si = appctx->owner;
2094
William Lallemand1f49a362017-11-21 20:01:26 +01002095 if (cache == NULL) {
2096 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
2097 }
2098
2099 list_for_each_entry_from(cache, &caches, list) {
2100 struct eb32_node *node = NULL;
2101 unsigned int next_key;
2102 struct cache_entry *entry;
2103
William Lallemand1f49a362017-11-21 20:01:26 +01002104 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002105 if (!next_key) {
2106 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
2107 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002108 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002109 return 0;
2110 }
2111 }
William Lallemand1f49a362017-11-21 20:01:26 +01002112
2113 appctx->ctx.cli.p0 = cache;
2114
2115 while (1) {
2116
2117 shctx_lock(shctx_ptr(cache));
2118 node = eb32_lookup_ge(&cache->entries, next_key);
2119 if (!node) {
2120 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02002121 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002122 break;
2123 }
2124
2125 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01002126 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 +01002127
2128 next_key = node->key + 1;
2129 appctx->ctx.cli.i0 = next_key;
2130
2131 shctx_unlock(shctx_ptr(cache));
2132
2133 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002134 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01002135 return 0;
2136 }
2137 }
2138
2139 }
2140
2141 return 1;
2142
2143}
2144
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002145
2146/*
2147 * boolean, returns true if response was built out of a cache entry.
2148 */
2149static int
2150smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2151 const char *kw, void *private)
2152{
2153 smp->data.type = SMP_T_BOOL;
2154 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2155
2156 return 1;
2157}
2158
2159/*
2160 * string, returns cache name (if response came from a cache).
2161 */
2162static int
2163smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2164 const char *kw, void *private)
2165{
2166 struct appctx *appctx = NULL;
2167
2168 struct cache_flt_conf *cconf = NULL;
2169 struct cache *cache = NULL;
2170
2171 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2172 return 0;
2173
2174 /* Get appctx from the stream_interface. */
2175 appctx = si_appctx(&smp->strm->si[1]);
2176 if (appctx && appctx->rule) {
2177 cconf = appctx->rule->arg.act.p[0];
2178 if (cconf) {
2179 cache = cconf->c.cache;
2180
2181 smp->data.type = SMP_T_STR;
2182 smp->flags = SMP_F_CONST;
2183 smp->data.u.str.area = cache->id;
2184 smp->data.u.str.data = strlen(cache->id);
2185 return 1;
2186 }
2187 }
2188
2189 return 0;
2190}
2191
Christopher Faulet99a17a22018-12-11 09:18:27 +01002192/* Declare the filter parser for "cache" keyword */
2193static struct flt_kw_list filter_kws = { "CACHE", { }, {
2194 { "cache", parse_cache_flt, NULL },
2195 { NULL, NULL, NULL },
2196 }
2197};
2198
2199INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2200
William Lallemand1f49a362017-11-21 20:01:26 +01002201static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01002202 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
2203 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002204}};
2205
Willy Tarreau0108d902018-11-25 19:14:37 +01002206INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002207
William Lallemand41db4602017-10-30 11:15:51 +01002208static struct action_kw_list http_res_actions = {
2209 .kw = {
2210 { "cache-store", parse_cache_store },
2211 { NULL, NULL }
2212 }
2213};
2214
Willy Tarreau0108d902018-11-25 19:14:37 +01002215INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2216
William Lallemand41db4602017-10-30 11:15:51 +01002217static struct action_kw_list http_req_actions = {
2218 .kw = {
2219 { "cache-use", parse_cache_use },
2220 { NULL, NULL }
2221 }
2222};
2223
Willy Tarreau0108d902018-11-25 19:14:37 +01002224INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2225
Willy Tarreau2231b632019-03-29 18:26:52 +01002226struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002227 .obj_type = OBJ_TYPE_APPLET,
2228 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002229 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002230 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002231};
2232
Willy Tarreaue6552512018-11-26 11:33:13 +01002233/* config parsers for this section */
2234REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002235REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002236
2237
2238/* Note: must not be declared <const> as its list will be overwritten */
2239static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2240 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2241 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2242 { /* END */ },
2243 }
2244};
2245
2246INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);