blob: e0560e57e188e90ca78ad623da4fe730cb1bdc13 [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 };
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100554 long smaxage = -1;
555 long 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;
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100559 char *endptr = NULL;
560 int offset = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100561
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100562 /* The Cache-Control max-age and s-maxage directives should be followed by
563 * a positive numerical value (see RFC 7234#5.2.1.1). According to the
564 * specs, a sender "should not" generate a quoted-string value but we will
565 * still accept this format since it isn't strictly forbidden. */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200566 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
567 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100568
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200569 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
570 if (value) {
571 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100572
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200573 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
574 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100575 offset = (*chk->area == '"') ? 1 : 0;
576 smaxage = strtol(chk->area + offset, &endptr, 10);
577 if (unlikely(smaxage < 0 || endptr == chk->area))
578 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100579 }
580
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200581 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
582 if (value) {
583 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200584
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200585 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
586 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100587 offset = (*chk->area == '"') ? 1 : 0;
588 maxage = strtol(chk->area + offset, &endptr, 10);
589 if (unlikely(maxage < 0 || endptr == chk->area))
590 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100591 }
592 }
593
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100594 /* Look for Expires header if no s-maxage or max-age Cache-Control data
595 * was found. */
596 if (maxage == -1 && smaxage == -1) {
597 ctx.blk = NULL;
598 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
599 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
600 expires_val = my_timegm(&tm);
601 /* A request having an expiring date earlier
602 * than the current date should be considered as
603 * stale. */
604 expires = (expires_val >= now.tv_sec) ?
605 (expires_val - now.tv_sec) : 0;
606 }
607 else {
608 /* Following RFC 7234#5.3, an invalid date
609 * format must be treated as a date in the past
610 * so the cache entry must be seen as already
611 * expired. */
612 expires = 0;
613 }
614 }
615 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100616
617
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100618 if (smaxage > 0) {
619 if (true_maxage)
620 *true_maxage = smaxage;
William Lallemand49b44532017-11-24 18:53:43 +0100621 return MIN(smaxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100622 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100623
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100624 if (maxage > 0) {
625 if (true_maxage)
626 *true_maxage = maxage;
William Lallemand49b44532017-11-24 18:53:43 +0100627 return MIN(maxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100628 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100629
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100630 if (expires >= 0) {
631 if (true_maxage)
632 *true_maxage = expires;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100633 return MIN(expires, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100634 }
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100635
William Lallemand49b44532017-11-24 18:53:43 +0100636 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100637
638}
639
640
William Lallemanda400a3a2017-11-20 19:13:12 +0100641static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
642{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200643 struct cache_entry *object = (struct cache_entry *)block->data;
644
645 if (first == block && object->eb.key)
646 eb32_delete(&object->eb);
647 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100648}
649
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200650
651/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
652 * date value should be compared to a date determined by in a previous response (for
653 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
654 * value of the response's reception time (by decreasing order of priority). */
655static time_t get_last_modified_time(struct htx *htx)
656{
657 time_t last_modified = 0;
658 struct http_hdr_ctx ctx = { .blk = NULL };
659 struct tm tm = {};
660
661 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
662 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
663 last_modified = my_timegm(&tm);
664 }
665 }
666
667 if (!last_modified) {
668 ctx.blk = NULL;
669 if (http_find_header(htx, ist("date"), &ctx, 1)) {
670 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
671 last_modified = my_timegm(&tm);
672 }
673 }
674 }
675
676 /* Fallback on the current time if no "Last-Modified" or "Date" header
677 * was found. */
678 if (!last_modified)
679 last_modified = now.tv_sec;
680
681 return last_modified;
682}
683
William Lallemand41db4602017-10-30 11:15:51 +0100684/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100685 * Checks the vary header's value. The headers on which vary should be applied
686 * must be explicitely supported in the vary_information array (see cache.c). If
687 * any other header is mentioned, we won't store the response.
688 * Returns 1 if Vary-based storage can work, 0 otherwise.
689 */
690static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
691{
692 unsigned int vary_idx;
693 unsigned int vary_info_count;
694 const struct vary_hashing_information *vary_info;
695 struct http_hdr_ctx ctx = { .blk = NULL };
696
697 int retval = 1;
698
699 *vary_signature = 0;
700
701 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
702 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
703 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
704 vary_info = &vary_information[vary_idx];
705 if (isteqi(ctx.value, vary_info->hdr_name)) {
706 *vary_signature |= vary_info->value;
707 break;
708 }
709 }
710 retval = (vary_idx < vary_info_count);
711 }
712
713 return retval;
714}
715
716
717
718/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500719 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100720 * register a filter to store the data
721 */
722enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200723 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100724{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200725 long long hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100726 int effective_maxage = 0;
727 int true_maxage = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100728 struct http_txn *txn = s->txn;
729 struct http_msg *msg = &txn->rsp;
730 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100731 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100732 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100733 struct cache *cache = cconf->c.cache;
734 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100735 struct cache_st *cache_ctx = NULL;
736 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100737 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200738 struct htx *htx;
739 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200740 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200741 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200742 struct ist header_name = IST_NULL;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100743 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100744
William Lallemand4da3f8a2017-10-31 14:33:34 +0100745 /* Don't cache if the response came from a cache */
746 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
747 s->target == &http_cache_applet.obj_type) {
748 goto out;
749 }
750
751 /* cache only HTTP/1.1 */
752 if (!(txn->req.flags & HTTP_MSGF_VER_11))
753 goto out;
754
Willy Tarreau6905d182019-10-01 17:59:17 +0200755 /* cache only GET method */
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +0100756 if (txn->meth != HTTP_METH_GET) {
757 /* In case of successful unsafe method on a stored resource, the
758 * cached entry must be invalidated (see RFC7234#4.4).
759 * A "non-error response" is one with a 2xx (Successful) or 3xx
760 * (Redirection) status code. */
761 if (txn->status >= 200 && txn->status < 400) {
762 switch (txn->meth) {
763 case HTTP_METH_OPTIONS:
764 case HTTP_METH_GET:
765 case HTTP_METH_HEAD:
766 case HTTP_METH_TRACE:
767 break;
768
769 default: /* Any unsafe method */
770 /* Discard any corresponding entry in case of sucessful
771 * unsafe request (such as PUT, POST or DELETE). */
772 shctx_lock(shctx);
773
774 old = entry_exist(cconf->c.cache, txn->cache_hash);
775 if (old) {
776 eb32_delete(&old->eb);
777 old->eb.key = 0;
778 }
779 shctx_unlock(shctx);
780 }
781 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100782 goto out;
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +0100783 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100784
Willy Tarreauc9036c02019-01-11 19:38:25 +0100785 /* cache key was not computed */
786 if (!key)
787 goto out;
788
William Lallemand4da3f8a2017-10-31 14:33:34 +0100789 /* cache only 200 status code */
790 if (txn->status != 200)
791 goto out;
792
Christopher Faulet839791a2019-01-07 16:12:07 +0100793 /* Find the corresponding filter instance for the current stream */
794 list_for_each_entry(filter, &s->strm_flt.filters, list) {
795 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
796 /* No filter ctx, don't cache anything */
797 if (!filter->ctx)
798 goto out;
799 cache_ctx = filter->ctx;
800 break;
801 }
802 }
803
804 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200805 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100806
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200807 /* Do not cache too big objects. */
808 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
809 htx->data + htx->extra > shctx->max_obj_size)
810 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100811
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100812 /* Only a subset of headers are supported in our Vary implementation. If
813 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100814 * able to use the cache. Likewise, if Vary header support is disabled,
815 * avoid caching responses that contain such a header. */
816 ctx.blk = NULL;
817 if (cache->vary_processing_enabled) {
818 if (!http_check_vary_header(htx, &vary_signature))
819 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100820 if (vary_signature)
821 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100822 }
823 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200824 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100825 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100826
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200827 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100828
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100829 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200830 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100831
832 shctx_lock(shctx);
833 old = entry_exist(cache, txn->cache_hash);
834 if (old) {
835 if (vary_signature)
836 old = secondary_entry_exist(cconf->c.cache, old,
837 txn->cache_secondary_hash);
838 if (old) {
839 if (!old->complete) {
840 /* An entry with the same primary key is already being
841 * created, we should not try to store the current
842 * response because it will waste space in the cache. */
843 shctx_unlock(shctx);
844 goto out;
845 }
846 eb32_delete(&old->eb);
847 old->eb.key = 0;
848 }
849 }
850 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry));
851 if (!first) {
852 shctx_unlock(shctx);
853 goto out;
854 }
855 /* the received memory is not initialized, we need at least to mark
856 * the object as not indexed yet.
857 */
858 object = (struct cache_entry *)first->data;
859 memset(object, 0, sizeof(*object));
860 object->eb.key = key;
861 object->secondary_key_signature = vary_signature;
862 /* We need to temporarily set a valid expiring time until the actual one
863 * is set by the end of this function (in case of concurrent accesses to
864 * the same resource). This way the second access will find an existing
865 * but not yet usable entry in the tree and will avoid storing its data. */
866 object->expire = now.tv_sec + 2;
867
868 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
869 if (vary_signature)
870 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
871
872 /* Insert the entry in the tree even if the payload is not cached yet. */
873 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
874 object->eb.key = 0;
875 shctx_unlock(shctx);
876 goto out;
877 }
878 shctx_unlock(shctx);
879
880 /* reserve space for the cache_entry structure */
881 first->len = sizeof(struct cache_entry);
882 first->last_append = NULL;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100883
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100884 /* Determine the entry's maximum age (taking into account the cache's
885 * configuration) as well as the response's explicit max age (extracted
886 * from cache-control directives or the expires header). */
887 effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage);
888
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200889 ctx.blk = NULL;
890 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
891 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
892 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
893 hdr_age = CACHE_ENTRY_MAX_AGE;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100894 /* A response with an Age value greater than its
895 * announced max age is stale and should not be stored. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100896 object->age = hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100897 if (unlikely(object->age > true_maxage))
898 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100899 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200900 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100901 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100902
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200903 /* Build a last-modified time that will be stored in the cache_entry and
904 * compared to a future If-Modified-Since client header. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100905 object->last_modified = get_last_modified_time(htx);
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200906
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200907 chunk_reset(&trash);
908 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
909 struct htx_blk *blk = htx_get_blk(htx, pos);
910 enum htx_blk_type type = htx_get_blk_type(blk);
911 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100912
Christopher Fauletb0667472019-09-03 22:22:12 +0200913 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200914 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
915 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200916
917 /* Look for optional ETag header.
918 * We need to store the offset of the ETag value in order for
919 * future conditional requests to be able to perform ETag
920 * comparisons. */
921 if (type == HTX_BLK_HDR) {
922 header_name = htx_get_blk_name(htx, blk);
923 if (isteq(header_name, ist("etag"))) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100924 object->etag_length = sz - istlen(header_name);
925 object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200926 }
927 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200928 if (type == HTX_BLK_EOH)
929 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200930 }
931
Christopher Fauletb0667472019-09-03 22:22:12 +0200932 /* Do not cache objects if the headers are too big. */
933 if (hdrs_len > htx->size - global.tune.maxrewrite)
934 goto out;
935
William Lallemand4da3f8a2017-10-31 14:33:34 +0100936 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100937 if (!shctx_row_reserve_hot(shctx, first, trash.data)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100938 shctx_unlock(shctx);
939 goto out;
940 }
941 shctx_unlock(shctx);
942
William Lallemand4da3f8a2017-10-31 14:33:34 +0100943 /* cache the headers in a http action because it allows to chose what
944 * to cache, for example you might want to cache a response before
945 * modifying some HTTP headers, or on the contrary after modifying
946 * those headers.
947 */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100948 /* does not need to be locked because it's in the "hot" list,
949 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200950 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
951 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100952
953 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100954 if (cache_ctx) {
955 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100956
Christopher Faulet839791a2019-01-07 16:12:07 +0100957 /* store latest value and expiration time */
958 object->latest_validation = now.tv_sec;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100959 object->expire = now.tv_sec + effective_maxage;
Christopher Faulet839791a2019-01-07 16:12:07 +0100960 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100961 }
962
963out:
964 /* if does not cache */
965 if (first) {
966 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100967 first->len = 0;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100968 if (object->eb.key)
969 eb32_delete(&object->eb);
William Lallemand08727662017-11-21 20:01:27 +0100970 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100971 shctx_row_dec_hot(shctx, first);
972 shctx_unlock(shctx);
973 }
974
William Lallemand41db4602017-10-30 11:15:51 +0100975 return ACT_RET_CONT;
976}
977
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100978#define HTX_CACHE_INIT 0 /* Initial state. */
979#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
980#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200981#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
982#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100983
William Lallemandecb73b12017-11-24 14:33:55 +0100984static void http_cache_applet_release(struct appctx *appctx)
985{
Christopher Faulet95220e22018-12-07 17:34:39 +0100986 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100987 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100988 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100989 struct shared_block *first = block_ptr(cache_ptr);
990
991 shctx_lock(shctx_ptr(cache));
992 shctx_row_dec_hot(shctx_ptr(cache), first);
993 shctx_unlock(shctx_ptr(cache));
994}
995
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200996
997static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
998 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100999{
Christopher Faulet95220e22018-12-07 17:34:39 +01001000 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1001 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001002 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001003 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001004 unsigned int max, total;
1005 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001006
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001007 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1008 if (!max)
1009 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001010 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001011 ? (info & 0xff) + ((info >> 8) & 0xfffff)
1012 : info & 0xfffffff);
1013 if (blksz > max)
1014 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001015
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001016 blk = htx_add_blk(htx, type, blksz);
1017 if (!blk)
1018 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001019
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001020 blk->info = info;
1021 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001022 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001023 while (blksz) {
1024 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001025 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001026 offset += max;
1027 blksz -= max;
1028 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001029 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001030 if (blksz || offset == shctx->block_size) {
1031 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1032 offset = 0;
1033 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001034 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001035 appctx->ctx.cache.offset = offset;
1036 appctx->ctx.cache.next = shblk;
1037 appctx->ctx.cache.sent += total;
1038 return total;
1039}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001040
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001041static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
1042 uint32_t info, struct shared_block *shblk, unsigned int offset)
1043{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001044
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001045 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1046 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
1047 unsigned int max, total, rem_data;
1048 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001049
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001050 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1051 if (!max)
1052 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001053
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001054 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001055 if (appctx->ctx.cache.rem_data) {
1056 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001057 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001058 }
1059 else {
1060 blksz = (info & 0xfffffff);
1061 total = 4;
1062 }
1063 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001064 rem_data = blksz - max;
1065 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001066 }
1067
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001068 while (blksz) {
1069 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001070
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001071 max = MIN(blksz, shctx->block_size - offset);
1072 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1073 offset += sz;
1074 blksz -= sz;
1075 total += sz;
1076 if (sz < max)
1077 break;
1078 if (blksz || offset == shctx->block_size) {
1079 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1080 offset = 0;
1081 }
1082 }
1083
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001084 appctx->ctx.cache.offset = offset;
1085 appctx->ctx.cache.next = shblk;
1086 appctx->ctx.cache.sent += total;
1087 appctx->ctx.cache.rem_data = rem_data + blksz;
1088 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001089}
1090
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001091static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1092 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001093{
Christopher Faulet95220e22018-12-07 17:34:39 +01001094 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1095 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001096 struct shared_block *shblk;
1097 unsigned int offset, sz;
1098 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001099
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001100 while (len) {
1101 enum htx_blk_type type;
1102 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001103
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001104 shblk = appctx->ctx.cache.next;
1105 offset = appctx->ctx.cache.offset;
1106 if (appctx->ctx.cache.rem_data) {
1107 type = HTX_BLK_DATA;
1108 info = 0;
1109 goto add_data_blk;
1110 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001111
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001112 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001113 sz = MIN(4, shctx->block_size - offset);
1114 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1115 offset += sz;
1116 if (sz < 4) {
1117 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1118 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1119 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001120 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001121
1122 /* Get payload of the next HTX block and insert it. */
1123 type = (info >> 28);
1124 if (type != HTX_BLK_DATA)
1125 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1126 else {
1127 add_data_blk:
1128 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001129 }
1130
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001131 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001132 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001133 total += ret;
1134 len -= ret;
1135
1136 if (appctx->ctx.cache.rem_data || type == mark)
1137 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001138 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001139
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001140 return total;
1141}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001142
1143static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1144{
1145 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1146 unsigned int age;
1147 char *end;
1148
1149 chunk_reset(&trash);
1150 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1151 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1152 age = CACHE_ENTRY_MAX_AGE;
1153 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1154 b_set_data(&trash, end - b_head(&trash));
1155 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1156 return 0;
1157 return 1;
1158}
1159
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001160static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001161{
1162 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1163 struct shared_block *first = block_ptr(cache_ptr);
1164 struct stream_interface *si = appctx->owner;
1165 struct channel *req = si_oc(si);
1166 struct channel *res = si_ic(si);
1167 struct htx *req_htx, *res_htx;
1168 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001169 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001170 size_t ret, total = 0;
1171
1172 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001173 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001174
1175 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1176 goto out;
1177
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001178 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001179 if (!b_size(&res->buf)) {
1180 si_rx_room_blk(si);
1181 goto out;
1182 }
1183
Willy Tarreauefef3232018-12-16 00:37:45 +01001184 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001185 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001186
1187 if (appctx->st0 == HTX_CACHE_INIT) {
1188 appctx->ctx.cache.next = block_ptr(cache_ptr);
1189 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1190 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001191 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001192 appctx->st0 = HTX_CACHE_HEADER;
1193 }
1194
1195 if (appctx->st0 == HTX_CACHE_HEADER) {
1196 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001197 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1198 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1199 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1200 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001201 goto error;
1202
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001203 /* In case of a conditional request, we might want to send a
1204 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001205 if (appctx->ctx.cache.send_notmodified) {
1206 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1207 /* If replacing the status code fails we need to send the full response. */
1208 appctx->ctx.cache.send_notmodified = 0;
1209 }
1210 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001211
1212 /* Skip response body for HEAD requests or in case of "304 Not
1213 * Modified" response. */
1214 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001215 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001216 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001217 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001218 }
1219
1220 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001221 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1222 if (len) {
1223 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1224 if (ret < len) {
1225 si_rx_room_blk(si);
1226 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001227 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001228 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001229 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001230 }
1231
1232 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001233 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001234 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1235 si_rx_room_blk(si);
1236 goto out;
1237 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001238 appctx->st0 = HTX_CACHE_END;
1239 }
1240
1241 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001242 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001243 res->flags |= CF_READ_NULL;
1244 si_shutr(si);
1245 }
1246
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001247 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001248 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001249 if (total)
1250 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001251 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001252
1253 /* eat the whole request */
1254 if (co_data(req)) {
1255 req_htx = htx_from_buf(&req->buf);
1256 co_htx_skip(req, req_htx, co_data(req));
1257 htx_to_buf(req_htx, &req->buf);
1258 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001259 return;
1260
1261 error:
1262 /* Sent and HTTP error 500 */
1263 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001264 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001265 res->buf.data = b_data(errmsg);
1266 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1267 res_htx = htx_from_buf(&res->buf);
1268
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001269 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001270 appctx->st0 = HTX_CACHE_END;
1271 goto end;
1272}
1273
1274
Christopher Faulet95220e22018-12-07 17:34:39 +01001275static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001276{
1277 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001278 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001279
Christopher Faulet95220e22018-12-07 17:34:39 +01001280 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001281 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001282 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001283 }
1284
1285 /* check if a cache filter was already registered with this cache
1286 * name, if that's the case, must use it. */
1287 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001288 if (fconf->id == cache_store_flt_id) {
1289 cconf = fconf->conf;
1290 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1291 rule->arg.act.p[0] = cconf;
1292 return 1;
1293 }
William Lallemand41db4602017-10-30 11:15:51 +01001294 }
1295 }
1296
Christopher Faulet95220e22018-12-07 17:34:39 +01001297 /* Create the filter cache config */
1298 cconf = calloc(1, sizeof(*cconf));
1299 if (!cconf) {
1300 memprintf(err, "out of memory\n");
1301 goto err;
1302 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001303 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001304 cconf->c.name = strdup(name);
1305 if (!cconf->c.name) {
1306 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001307 goto err;
1308 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001309
William Lallemand41db4602017-10-30 11:15:51 +01001310 /* register a filter to fill the cache buffer */
1311 fconf = calloc(1, sizeof(*fconf));
1312 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001313 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001314 goto err;
1315 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001316 fconf->id = cache_store_flt_id;
1317 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001318 fconf->ops = &cache_ops;
1319 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1320
Christopher Faulet95220e22018-12-07 17:34:39 +01001321 rule->arg.act.p[0] = cconf;
1322 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001323
Christopher Faulet95220e22018-12-07 17:34:39 +01001324 err:
1325 free(cconf);
1326 return 0;
1327}
1328
1329enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1330 struct act_rule *rule, char **err)
1331{
1332 rule->action = ACT_CUSTOM;
1333 rule->action_ptr = http_action_store_cache;
1334
1335 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1336 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001337
Christopher Faulet95220e22018-12-07 17:34:39 +01001338 (*orig_arg)++;
1339 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001340}
1341
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001342/* This produces a sha1 hash of the concatenation of the HTTP method,
1343 * the first occurrence of the Host header followed by the path component
1344 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001345int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001346{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001347 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001348 struct htx *htx = htxbuf(&s->req.buf);
1349 struct htx_sl *sl;
1350 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001351 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001352 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001353 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001354
William Lallemandf528fff2017-11-23 19:43:17 +01001355 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001356 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001357
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001358 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001359 uri = htx_sl_req_uri(sl); // whole uri
1360 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001361 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001362
1363 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1364 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1365 * URIs are almost always sent in absolute form with their scheme. In
1366 * this case, the scheme is almost always "https". In order to support
1367 * sharing of cache objects between H1 and H2, we'll hash the absolute
1368 * URI whenever known, or prepend "https://" + the Host header for
1369 * relative URIs. The difference will only appear on absolute HTTP/1
1370 * requests sent to an origin server, which practically is never met in
1371 * the real world so we don't care about the ability to share the same
1372 * key here.URIs are normalized from the absolute URI to an origin form as
1373 * well.
1374 */
1375 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001376 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001377 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1378 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001379 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001380 }
1381
1382 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001383
1384 /* hash everything */
1385 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001386 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001387 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1388
1389 return 1;
1390}
1391
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001392/* Looks for "If-None-Match" headers in the request and compares their value
1393 * with the one that might have been stored in the cache_entry. If any of them
1394 * matches, a "304 Not Modified" response should be sent instead of the cached
1395 * data.
1396 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001397 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1398 *
1399 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1400 * header and compare its value (date) to the one stored in the cache_entry.
1401 * If the request's date is later than the cached one, we also send a
1402 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1403 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001404 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1405 */
1406static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1407 struct cache_entry *entry)
1408{
1409 int retval = 0;
1410
1411 struct http_hdr_ctx ctx = { .blk = NULL };
1412 struct ist cache_entry_etag = IST_NULL;
1413 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001414 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001415
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001416 struct tm tm = {};
1417 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001418
1419 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001420 * cache_entry's ETag in order to perform comparisons.
1421 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001422 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001423 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001424
1425 /* A '*' matches everything. */
1426 if (isteq(ctx.value, ist("*")) != 0) {
1427 retval = 1;
1428 break;
1429 }
1430
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001431 /* No need to rebuild an etag if none was stored in the cache. */
1432 if (entry->etag_length == 0)
1433 break;
1434
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001435 /* Rebuild the stored ETag. */
1436 if (etag_buffer == NULL) {
1437 etag_buffer = get_trash_chunk();
1438
1439 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1440 (unsigned char*)b_orig(etag_buffer),
1441 entry->etag_offset, entry->etag_length) == 0) {
1442 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1443 } else {
1444 /* We could not rebuild the ETag in one go, we
1445 * won't send a "304 Not Modified" response. */
1446 break;
1447 }
1448 }
1449
1450 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1451 retval = 1;
1452 break;
1453 }
1454 }
1455
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001456 /* If the request did not contain an "If-None-Match" header, we look for
1457 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1458 if (retval == 0 && if_none_match_found == 0) {
1459 ctx.blk = NULL;
1460 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1461 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1462 if_modified_since = my_timegm(&tm);
1463
1464 /* We send a "304 Not Modified" response if the
1465 * entry's last modified date is earlier than
1466 * the one found in the "If-Modified-Since"
1467 * header. */
1468 retval = (entry->last_modified <= if_modified_since);
1469 }
1470 }
1471 }
1472
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001473 return retval;
1474}
1475
William Lallemand41db4602017-10-30 11:15:51 +01001476enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1477 struct session *sess, struct stream *s, int flags)
1478{
William Lallemand77c11972017-10-31 20:43:01 +01001479
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001480 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001481 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001482 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1483 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001484 struct shared_block *entry_block;
1485
William Lallemand77c11972017-10-31 20:43:01 +01001486
Willy Tarreau6905d182019-10-01 17:59:17 +02001487 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1488 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001489 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001490 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001491 txn->flags |= TX_CACHE_IGNORE;
1492
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001493 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001494
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001495 /* The request's hash has to be calculated for all requests, even POSTs
1496 * or PUTs for instance because RFC7234 specifies that a sucessful
1497 * "unsafe" method on a stored resource must invalidate it
1498 * (see RFC7234#4.4). */
1499 if (!sha1_hosturi(s))
Willy Tarreau504455c2017-12-22 17:47:35 +01001500 return ACT_RET_CONT;
1501
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001502 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001503 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001504
Willy Tarreau504455c2017-12-22 17:47:35 +01001505 if (s->txn->flags & TX_CACHE_IGNORE)
1506 return ACT_RET_CONT;
1507
Willy Tarreaua1214a52018-12-14 14:00:25 +01001508 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001509 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001510 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001511 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001512
William Lallemanda400a3a2017-11-20 19:13:12 +01001513 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001514 res = entry_exist(cache, s->txn->cache_hash);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001515 /* We must not use an entry that is not complete. */
1516 if (res && res->complete) {
William Lallemand77c11972017-10-31 20:43:01 +01001517 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001518 entry_block = block_ptr(res);
1519 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001520 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001521
1522 /* In case of Vary, we could have multiple entries with the same
1523 * primary hash. We need to calculate the secondary has in order
1524 * to find the actual entry we want (if it exists). */
1525 if (res->secondary_key_signature) {
1526 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1527 shctx_lock(shctx_ptr(cache));
1528 sec_entry = secondary_entry_exist(cache, res,
1529 s->txn->cache_secondary_hash);
1530 if (sec_entry && sec_entry != res) {
1531 /* The wrong row was added to the hot list. */
1532 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1533 entry_block = block_ptr(sec_entry);
1534 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1535 }
1536 res = sec_entry;
1537 shctx_unlock(shctx_ptr(cache));
1538 }
1539 else
1540 res = NULL;
1541 }
1542
1543 /* We looked for a valid secondary entry and could not find one,
1544 * the request must be forwarded to the server. */
1545 if (!res) {
1546 shctx_lock(shctx_ptr(cache));
1547 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1548 shctx_unlock(shctx_ptr(cache));
1549 return ACT_RET_CONT;
1550 }
1551
William Lallemand77c11972017-10-31 20:43:01 +01001552 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001553 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001554 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001555 appctx->rule = rule;
1556 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001557 appctx->ctx.cache.next = NULL;
1558 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001559 appctx->ctx.cache.send_notmodified =
1560 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001561
1562 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001563 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001564 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001565 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001566 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001567 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001568 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001569 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001570 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001571 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001572 }
1573 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001574 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001575
1576 /* Shared context does not need to be locked while we calculate the
1577 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001578 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001579 /* Build a complete secondary hash until the server response
1580 * tells us which fields should be kept (if any). */
1581 http_request_prebuild_full_secondary_key(s);
1582 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001583 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001584}
1585
1586
1587enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1588 struct act_rule *rule, char **err)
1589{
William Lallemand41db4602017-10-30 11:15:51 +01001590 rule->action = ACT_CUSTOM;
1591 rule->action_ptr = http_action_req_cache_use;
1592
Christopher Faulet95220e22018-12-07 17:34:39 +01001593 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001594 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001595
1596 (*orig_arg)++;
1597 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001598}
1599
1600int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1601{
1602 int err_code = 0;
1603
1604 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1605
1606 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001607 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001608 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001609 err_code |= ERR_ALERT | ERR_ABORT;
1610 goto out;
1611 }
1612
1613 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1614 err_code |= ERR_ABORT;
1615 goto out;
1616 }
1617
1618 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001619 struct cache *cache_config;
1620
William Lallemand41db4602017-10-30 11:15:51 +01001621 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1622 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001623 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001624 err_code |= ERR_ALERT | ERR_ABORT;
1625 goto out;
1626 }
1627
1628 strlcpy2(tmp_cache_config->id, args[1], 33);
1629 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001630 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001631 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001632 err_code |= ERR_WARN;
1633 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001634
1635 list_for_each_entry(cache_config, &caches_config, list) {
1636 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1637 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1638 file, linenum, tmp_cache_config->id);
1639 err_code |= ERR_ALERT | ERR_ABORT;
1640 goto out;
1641 }
1642 }
1643
William Lallemand49b44532017-11-24 18:53:43 +01001644 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001645 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001646 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001647 }
1648 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001649 unsigned long int maxsize;
1650 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001651
1652 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1653 err_code |= ERR_ABORT;
1654 goto out;
1655 }
1656
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001657 maxsize = strtoul(args[1], &err, 10);
1658 if (err == args[1] || *err != '\0') {
1659 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1660 file, linenum, args[1]);
1661 err_code |= ERR_ABORT;
1662 goto out;
1663 }
1664
1665 if (maxsize > (UINT_MAX >> 20)) {
1666 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1667 file, linenum, args[1], UINT_MAX >> 20);
1668 err_code |= ERR_ABORT;
1669 goto out;
1670 }
1671
William Lallemand41db4602017-10-30 11:15:51 +01001672 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001673 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001674 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001675 } else if (strcmp(args[0], "max-age") == 0) {
1676 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1677 err_code |= ERR_ABORT;
1678 goto out;
1679 }
1680
1681 if (!*args[1]) {
1682 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1683 file, linenum, args[0]);
1684 err_code |= ERR_WARN;
1685 }
1686
1687 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001688 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001689 unsigned int maxobjsz;
1690 char *err;
1691
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001692 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1693 err_code |= ERR_ABORT;
1694 goto out;
1695 }
1696
1697 if (!*args[1]) {
1698 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1699 file, linenum, args[0]);
1700 err_code |= ERR_WARN;
1701 }
1702
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001703 maxobjsz = strtoul(args[1], &err, 10);
1704 if (err == args[1] || *err != '\0') {
1705 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1706 file, linenum, args[1]);
1707 err_code |= ERR_ABORT;
1708 goto out;
1709 }
1710 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001711 } else if (strcmp(args[0], "process-vary") == 0) {
1712 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1713 err_code |= ERR_ABORT;
1714 goto out;
1715 }
1716
1717 if (!*args[1]) {
1718 ha_warning("parsing [%s:%d]: '%s' expects 0 or 1 (disable or enable vary processing).\n",
1719 file, linenum, args[0]);
1720 err_code |= ERR_WARN;
1721 }
1722
1723 tmp_cache_config->vary_processing_enabled = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001724 }
1725 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001726 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001727 err_code |= ERR_ALERT | ERR_FATAL;
1728 goto out;
1729 }
1730out:
1731 return err_code;
1732}
1733
1734/* once the cache section is parsed */
1735
1736int cfg_post_parse_section_cache()
1737{
William Lallemand41db4602017-10-30 11:15:51 +01001738 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001739
1740 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001741
1742 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001743 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001744 err_code |= ERR_FATAL | ERR_ALERT;
1745 goto out;
1746 }
1747
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001748 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001749 /* Default max. file size is a 256th of the cache size. */
1750 tmp_cache_config->maxobjsz =
1751 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001752 }
1753 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1754 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1755 err_code |= ERR_FATAL | ERR_ALERT;
1756 goto out;
1757 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001758
William Lallemandd1d1e222019-08-28 15:22:49 +02001759 /* add to the list of cache to init and reinit tmp_cache_config
1760 * for next cache section, if any.
1761 */
1762 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1763 tmp_cache_config = NULL;
1764 return err_code;
1765 }
1766out:
1767 free(tmp_cache_config);
1768 tmp_cache_config = NULL;
1769 return err_code;
1770
1771}
1772
1773int post_check_cache()
1774{
1775 struct proxy *px;
1776 struct cache *back, *cache_config, *cache;
1777 struct shared_context *shctx;
1778 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001779 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02001780
1781 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1782
1783 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1784 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001785
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001786 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001787 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001788 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001789 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001790 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001791
1792 err_code |= ERR_FATAL | ERR_ALERT;
1793 goto out;
1794 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001795 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001796 /* the cache structure is stored in the shctx and added to the
1797 * caches list, we can remove the entry from the caches_config
1798 * list */
1799 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001800 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001801 cache->entries = EB_ROOT;
William Lallemand41db4602017-10-30 11:15:51 +01001802 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001803 LIST_DEL(&cache_config->list);
1804 free(cache_config);
1805
1806 /* Find all references for this cache in the existing filters
1807 * (over all proxies) and reference it in matching filters.
1808 */
1809 for (px = proxies_list; px; px = px->next) {
1810 struct flt_conf *fconf;
1811 struct cache_flt_conf *cconf;
1812
1813 list_for_each_entry(fconf, &px->filter_configs, list) {
1814 if (fconf->id != cache_store_flt_id)
1815 continue;
1816
1817 cconf = fconf->conf;
1818 if (!strcmp(cache->id, cconf->c.name)) {
1819 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001820 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001821 cconf->c.cache = cache;
1822 break;
1823 }
1824 }
1825 }
William Lallemand41db4602017-10-30 11:15:51 +01001826 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001827
William Lallemand41db4602017-10-30 11:15:51 +01001828out:
William Lallemand41db4602017-10-30 11:15:51 +01001829 return err_code;
1830
William Lallemand41db4602017-10-30 11:15:51 +01001831}
1832
William Lallemand41db4602017-10-30 11:15:51 +01001833struct flt_ops cache_ops = {
1834 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001835 .check = cache_store_check,
1836 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001837
Christopher Faulet65554e12020-03-06 14:52:06 +01001838 /* Handle stream init/deinit */
1839 .attach = cache_store_strm_init,
1840 .detach = cache_store_strm_deinit,
1841
William Lallemand4da3f8a2017-10-31 14:33:34 +01001842 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001843 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001844
1845 /* Filter HTTP requests and responses */
1846 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001847 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001848 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001849};
1850
Christopher Faulet99a17a22018-12-11 09:18:27 +01001851
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001852int accept_encoding_cmp(const void *a, const void *b)
1853{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001854 unsigned int int_a = *(unsigned int*)a;
1855 unsigned int int_b = *(unsigned int*)b;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001856
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001857 if (int_a < int_b)
1858 return -1;
1859 if (int_a > int_b)
1860 return 1;
1861 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001862}
1863
Tim Duesterhus23b29452020-11-24 22:22:56 +01001864#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001865/*
1866 * Build a hash of the accept-encoding header. The different parts of the
1867 * header value are first sorted, appended and then a crc is calculated
1868 * for the newly constructed buffer.
1869 * Returns 0 in case of success.
1870 */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001871static int accept_encoding_normalizer(struct ist full_value, char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001872{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001873 unsigned int values[ACCEPT_ENCODING_MAX_ENTRIES] = {};
Tim Duesterhus23b29452020-11-24 22:22:56 +01001874 size_t count = 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001875 char *comma = NULL;
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001876 unsigned int hash_value = 0;
1877 unsigned int prev = 0, curr = 0;
1878
1879 /* Turn accept-encoding value to lower case */
1880 full_value = ist2bin_lc(istptr(full_value), full_value);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001881
1882 /* The hash will be built out of a sorted list of accepted encodings. */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001883 while (count < (ACCEPT_ENCODING_MAX_ENTRIES - 1) && (comma = istchr(full_value, ',')) != NULL) {
1884 size_t length = comma - istptr(full_value);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001885
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001886 values[count++] = hash_crc32(istptr(full_value), length);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001887
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001888 full_value = istadv(full_value, length + 1);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001889
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001890 }
1891 values[count++] = hash_crc32(istptr(full_value), istlen(full_value));
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001892
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001893 /* Sort the values alphabetically. */
1894 qsort(values, count, sizeof(*values), &accept_encoding_cmp);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001895
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001896 while (count) {
1897 curr = values[--count];
1898 if (curr != prev) {
1899 hash_value ^= curr;
1900 }
1901 prev = curr;
1902 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001903
1904 memcpy(buf, &hash_value, sizeof(hash_value));
1905 *buf_len = sizeof(hash_value);
1906
Tim Duesterhus23b29452020-11-24 22:22:56 +01001907 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001908}
Tim Duesterhus23b29452020-11-24 22:22:56 +01001909#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001910
1911/*
1912 * Normalizer used by default for User-Agent and Referer headers. It only
1913 * calculates a simple crc of the whole value.
1914 * Returns 0 in case of success.
1915 */
1916static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len)
1917{
1918 int hash_value = 0;
1919
1920 hash_value = hash_crc32(istptr(value), istlen(value));
1921
1922 memcpy(buf, &hash_value, sizeof(hash_value));
1923 *buf_len = sizeof(hash_value);
1924
1925 return 0;
1926}
1927
1928
1929/*
1930 * Pre-calculate the hashes of all the supported headers (in our Vary
1931 * implementation) of a given request. We have to calculate all the hashes
1932 * in advance because the actual Vary signature won't be known until the first
1933 * response.
1934 * Only the first occurrence of every header will be taken into account in the
1935 * hash.
1936 * If the header is not present, the hash portion of the given header will be
1937 * filled with zeros.
1938 * Returns 0 in case of success.
1939 */
1940static int http_request_prebuild_full_secondary_key(struct stream *s)
1941{
1942 struct http_txn *txn = s->txn;
1943 struct htx *htx = htxbuf(&s->req.buf);
1944 struct http_hdr_ctx ctx = { .blk = NULL };
1945
1946 unsigned int idx;
1947 const struct vary_hashing_information *info = NULL;
1948 unsigned int hash_length = 0;
1949 int retval = 0;
1950 int offset = 0;
1951
1952 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1953 info = &vary_information[idx];
1954
1955 ctx.blk = NULL;
1956 if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) {
1957 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1958 offset += hash_length;
1959 }
1960 else {
1961 /* Fill hash with 0s. */
1962 hash_length = info->hash_length;
1963 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1964 offset += hash_length;
1965 }
1966 }
1967
1968 return retval;
1969}
1970
1971
1972/*
1973 * Calculate the secondary key for a request for which we already have a known
1974 * vary signature. The key is made by aggregating hashes calculated for every
1975 * header mentioned in the vary signature.
1976 * Only the first occurrence of every header will be taken into account in the
1977 * hash.
1978 * If the header is not present, the hash portion of the given header will be
1979 * filled with zeros.
1980 * Returns 0 in case of success.
1981 */
1982static int http_request_build_secondary_key(struct stream *s, int vary_signature)
1983{
1984 struct http_txn *txn = s->txn;
1985 struct htx *htx = htxbuf(&s->req.buf);
1986 struct http_hdr_ctx ctx = { .blk = NULL };
1987
1988 unsigned int idx;
1989 const struct vary_hashing_information *info = NULL;
1990 unsigned int hash_length = 0;
1991 int retval = 0;
1992 int offset = 0;
1993
1994 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1995 info = &vary_information[idx];
1996
1997 ctx.blk = NULL;
1998 if ((vary_signature & info->value) && info->norm_fn != NULL &&
1999 http_find_header(htx, info->hdr_name, &ctx, 1)) {
2000 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
2001 offset += hash_length;
2002 }
2003 else {
2004 /* Fill hash with 0s. */
2005 hash_length = info->hash_length;
2006 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
2007 offset += hash_length;
2008 }
2009 }
2010
2011 return retval;
2012}
2013
2014/*
2015 * Build the actual secondary key of a given request out of the prebuilt key and
2016 * the actual vary signature (extracted from the response).
2017 * Returns 0 in case of success.
2018 */
2019static int http_request_reduce_secondary_key(unsigned int vary_signature,
2020 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
2021{
2022 int offset = 0;
2023 int global_offset = 0;
2024 int vary_info_count = 0;
2025 int keep = 0;
2026 unsigned int vary_idx;
2027 const struct vary_hashing_information *vary_info;
2028
2029 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
2030 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
2031 vary_info = &vary_information[vary_idx];
2032 keep = (vary_signature & vary_info->value) ? 0xff : 0;
2033
2034 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
2035 prebuilt_key[global_offset] &= keep;
2036 }
2037 }
2038
2039 return 0;
2040}
2041
2042
Christopher Faulet99a17a22018-12-11 09:18:27 +01002043
2044static int
2045parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
2046 struct flt_conf *fconf, char **err, void *private)
2047{
2048 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01002049 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002050 char *name = NULL;
2051 int pos = *cur_arg;
2052
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002053 /* Get the cache filter name. <pos> point on "cache" keyword */
2054 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02002055 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002056 goto error;
2057 }
2058 name = strdup(args[pos + 1]);
2059 if (!name) {
2060 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
2061 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002062 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002063 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002064
2065 /* Check if an implicit filter with the same name already exists. If so,
2066 * we remove the implicit filter to use the explicit one. */
2067 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
2068 if (f->id != cache_store_flt_id)
2069 continue;
2070
2071 cconf = f->conf;
2072 if (strcmp(name, cconf->c.name)) {
2073 cconf = NULL;
2074 continue;
2075 }
2076
2077 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2078 cconf = NULL;
2079 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2080 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002081 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002082 }
2083
2084 /* Remove the implicit filter. <cconf> is kept for the explicit one */
2085 LIST_DEL(&f->list);
2086 free(f);
2087 free(name);
2088 break;
2089 }
2090
2091 /* No implicit cache filter found, create configuration for the explicit one */
2092 if (!cconf) {
2093 cconf = calloc(1, sizeof(*cconf));
2094 if (!cconf) {
2095 memprintf(err, "%s: out of memory", args[*cur_arg]);
2096 goto error;
2097 }
2098 cconf->c.name = name;
2099 }
2100
2101 cconf->flags = 0;
2102 fconf->id = cache_store_flt_id;
2103 fconf->conf = cconf;
2104 fconf->ops = &cache_ops;
2105
2106 *cur_arg = pos;
2107 return 0;
2108
2109 error:
2110 free(name);
2111 free(cconf);
2112 return -1;
2113}
2114
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002115static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002116{
2117 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2118 return 1;
2119
2120 return 0;
2121}
2122
2123static int cli_io_handler_show_cache(struct appctx *appctx)
2124{
2125 struct cache* cache = appctx->ctx.cli.p0;
2126 struct stream_interface *si = appctx->owner;
2127
William Lallemand1f49a362017-11-21 20:01:26 +01002128 if (cache == NULL) {
2129 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
2130 }
2131
2132 list_for_each_entry_from(cache, &caches, list) {
2133 struct eb32_node *node = NULL;
2134 unsigned int next_key;
2135 struct cache_entry *entry;
2136
William Lallemand1f49a362017-11-21 20:01:26 +01002137 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002138 if (!next_key) {
2139 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
2140 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002141 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002142 return 0;
2143 }
2144 }
William Lallemand1f49a362017-11-21 20:01:26 +01002145
2146 appctx->ctx.cli.p0 = cache;
2147
2148 while (1) {
2149
2150 shctx_lock(shctx_ptr(cache));
2151 node = eb32_lookup_ge(&cache->entries, next_key);
2152 if (!node) {
2153 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02002154 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002155 break;
2156 }
2157
2158 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01002159 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 +01002160
2161 next_key = node->key + 1;
2162 appctx->ctx.cli.i0 = next_key;
2163
2164 shctx_unlock(shctx_ptr(cache));
2165
2166 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002167 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01002168 return 0;
2169 }
2170 }
2171
2172 }
2173
2174 return 1;
2175
2176}
2177
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002178
2179/*
2180 * boolean, returns true if response was built out of a cache entry.
2181 */
2182static int
2183smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2184 const char *kw, void *private)
2185{
2186 smp->data.type = SMP_T_BOOL;
2187 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2188
2189 return 1;
2190}
2191
2192/*
2193 * string, returns cache name (if response came from a cache).
2194 */
2195static int
2196smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2197 const char *kw, void *private)
2198{
2199 struct appctx *appctx = NULL;
2200
2201 struct cache_flt_conf *cconf = NULL;
2202 struct cache *cache = NULL;
2203
2204 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2205 return 0;
2206
2207 /* Get appctx from the stream_interface. */
2208 appctx = si_appctx(&smp->strm->si[1]);
2209 if (appctx && appctx->rule) {
2210 cconf = appctx->rule->arg.act.p[0];
2211 if (cconf) {
2212 cache = cconf->c.cache;
2213
2214 smp->data.type = SMP_T_STR;
2215 smp->flags = SMP_F_CONST;
2216 smp->data.u.str.area = cache->id;
2217 smp->data.u.str.data = strlen(cache->id);
2218 return 1;
2219 }
2220 }
2221
2222 return 0;
2223}
2224
Christopher Faulet99a17a22018-12-11 09:18:27 +01002225/* Declare the filter parser for "cache" keyword */
2226static struct flt_kw_list filter_kws = { "CACHE", { }, {
2227 { "cache", parse_cache_flt, NULL },
2228 { NULL, NULL, NULL },
2229 }
2230};
2231
2232INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2233
William Lallemand1f49a362017-11-21 20:01:26 +01002234static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01002235 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
2236 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002237}};
2238
Willy Tarreau0108d902018-11-25 19:14:37 +01002239INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002240
William Lallemand41db4602017-10-30 11:15:51 +01002241static struct action_kw_list http_res_actions = {
2242 .kw = {
2243 { "cache-store", parse_cache_store },
2244 { NULL, NULL }
2245 }
2246};
2247
Willy Tarreau0108d902018-11-25 19:14:37 +01002248INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2249
William Lallemand41db4602017-10-30 11:15:51 +01002250static struct action_kw_list http_req_actions = {
2251 .kw = {
2252 { "cache-use", parse_cache_use },
2253 { NULL, NULL }
2254 }
2255};
2256
Willy Tarreau0108d902018-11-25 19:14:37 +01002257INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2258
Willy Tarreau2231b632019-03-29 18:26:52 +01002259struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002260 .obj_type = OBJ_TYPE_APPLET,
2261 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002262 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002263 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002264};
2265
Willy Tarreaue6552512018-11-26 11:33:13 +01002266/* config parsers for this section */
2267REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002268REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002269
2270
2271/* Note: must not be declared <const> as its list will be overwritten */
2272static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2273 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2274 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2275 { /* END */ },
2276 }
2277};
2278
2279INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);