blob: 9ed7b97621a051fcc8add0992e07d0edc69930e5 [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);
Remi Tricot-Le Breton964caaf2020-12-15 14:30:12 +0100368 eb32_delete(&object->eb);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200369 object->eb.key = 0;
370 shctx_unlock(shctx);
371 pool_free(pool_head_cache_st, st);
372}
373
William Lallemand4da3f8a2017-10-31 14:33:34 +0100374static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100375cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
376 unsigned int offset, unsigned int len)
377{
Christopher Faulet95220e22018-12-07 17:34:39 +0100378 struct cache_flt_conf *cconf = FLT_CONF(filter);
379 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100380 struct cache_st *st = filter->ctx;
381 struct htx *htx = htxbuf(&msg->chn->buf);
382 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200383 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100384 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200385 unsigned int orig_len, to_forward;
386 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100387
388 if (!len)
389 return len;
390
391 if (!st->first_block) {
392 unregister_data_filter(s, msg->chn, filter);
393 return len;
394 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100395
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200396 chunk_reset(&trash);
397 orig_len = len;
398 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100399
400 htxret = htx_find_offset(htx, offset);
401 blk = htxret.blk;
402 offset = htxret.ret;
403 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100404 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200405 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100406 struct ist v;
407
408 switch (type) {
409 case HTX_BLK_UNUSED:
410 break;
411
412 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100413 v = htx_get_blk_value(htx, blk);
414 v.ptr += offset;
415 v.len -= offset;
416 if (v.len > len)
417 v.len = len;
418
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200419 info = (type << 28) + v.len;
420 chunk_memcat(&trash, (char *)&info, sizeof(info));
421 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100422 to_forward += v.len;
423 len -= v.len;
424 break;
425
426 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200427 /* Here offset must always be 0 because only
428 * DATA blocks can be partially transferred. */
429 if (offset)
430 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100431 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200432 goto end;
433
434 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
435 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100436 to_forward += sz;
437 len -= sz;
438 break;
439 }
440
441 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100442 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200443
444 end:
445 shctx_lock(shctx);
446 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
447 if (!fb) {
448 shctx_unlock(shctx);
449 goto no_cache;
450 }
451 shctx_unlock(shctx);
452
453 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
454 (unsigned char *)b_head(&trash), b_data(&trash));
455 if (ret < 0)
456 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100457
458 return to_forward;
459
460 no_cache:
461 disable_cache_entry(st, filter, shctx);
462 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200463 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100464}
465
466static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100467cache_store_http_end(struct stream *s, struct filter *filter,
468 struct http_msg *msg)
469{
470 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100471 struct cache_flt_conf *cconf = FLT_CONF(filter);
472 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100473 struct shared_context *shctx = shctx_ptr(cache);
474 struct cache_entry *object;
475
476 if (!(msg->chn->flags & CF_ISRESP))
477 return 1;
478
479 if (st && st->first_block) {
480
481 object = (struct cache_entry *)st->first_block->data;
482
William Lallemand4da3f8a2017-10-31 14:33:34 +0100483 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100484 /* The whole payload was cached, the entry can now be used. */
485 object->complete = 1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100486 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100487 shctx_row_dec_hot(shctx, st->first_block);
488 shctx_unlock(shctx);
489
490 }
491 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100492 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100493 filter->ctx = NULL;
494 }
495
496 return 1;
497}
498
499 /*
500 * This intends to be used when checking HTTP headers for some
501 * word=value directive. Return a pointer to the first character of value, if
502 * the word was not found or if there wasn't any value assigned ot it return NULL
503 */
504char *directive_value(const char *sample, int slen, const char *word, int wlen)
505{
506 int st = 0;
507
508 if (slen < wlen)
509 return 0;
510
511 while (wlen) {
512 char c = *sample ^ *word;
513 if (c && c != ('A' ^ 'a'))
514 return NULL;
515 sample++;
516 word++;
517 slen--;
518 wlen--;
519 }
520
521 while (slen) {
522 if (st == 0) {
523 if (*sample != '=')
524 return NULL;
525 sample++;
526 slen--;
527 st = 1;
528 continue;
529 } else {
530 return (char *)sample;
531 }
532 }
533
534 return NULL;
535}
536
537/*
538 * Return the maxage in seconds of an HTTP response.
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100539 * The returned value will always take the cache's configuration into account
540 * (cache->maxage) but the actual max age of the response will be set in the
541 * true_maxage parameter. It will be used to determine if a response is already
542 * stale or not.
William Lallemand4da3f8a2017-10-31 14:33:34 +0100543 * Compute the maxage using either:
544 * - the assigned max-age of the cache
545 * - the s-maxage directive
546 * - the max-age directive
547 * - (Expires - Data) headers
548 * - the default-max-age of the cache
549 *
550 */
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100551int http_calc_maxage(struct stream *s, struct cache *cache, int *true_maxage)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100552{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200553 struct htx *htx = htxbuf(&s->res.buf);
554 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100555 long smaxage = -1;
556 long maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100557 int expires = -1;
558 struct tm tm = {};
559 time_t expires_val = 0;
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100560 char *endptr = NULL;
561 int offset = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100562
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100563 /* The Cache-Control max-age and s-maxage directives should be followed by
564 * a positive numerical value (see RFC 7234#5.2.1.1). According to the
565 * specs, a sender "should not" generate a quoted-string value but we will
566 * still accept this format since it isn't strictly forbidden. */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200567 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
568 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100569
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200570 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
571 if (value) {
572 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100573
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200574 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
575 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100576 offset = (*chk->area == '"') ? 1 : 0;
577 smaxage = strtol(chk->area + offset, &endptr, 10);
578 if (unlikely(smaxage < 0 || endptr == chk->area))
579 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100580 }
581
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200582 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
583 if (value) {
584 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200585
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200586 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
587 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100588 offset = (*chk->area == '"') ? 1 : 0;
589 maxage = strtol(chk->area + offset, &endptr, 10);
590 if (unlikely(maxage < 0 || endptr == chk->area))
591 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100592 }
593 }
594
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100595 /* Look for Expires header if no s-maxage or max-age Cache-Control data
596 * was found. */
597 if (maxage == -1 && smaxage == -1) {
598 ctx.blk = NULL;
599 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
600 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
601 expires_val = my_timegm(&tm);
602 /* A request having an expiring date earlier
603 * than the current date should be considered as
604 * stale. */
605 expires = (expires_val >= now.tv_sec) ?
606 (expires_val - now.tv_sec) : 0;
607 }
608 else {
609 /* Following RFC 7234#5.3, an invalid date
610 * format must be treated as a date in the past
611 * so the cache entry must be seen as already
612 * expired. */
613 expires = 0;
614 }
615 }
616 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100617
618
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100619 if (smaxage > 0) {
620 if (true_maxage)
621 *true_maxage = smaxage;
William Lallemand49b44532017-11-24 18:53:43 +0100622 return MIN(smaxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100623 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100624
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100625 if (maxage > 0) {
626 if (true_maxage)
627 *true_maxage = maxage;
William Lallemand49b44532017-11-24 18:53:43 +0100628 return MIN(maxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100629 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100630
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100631 if (expires >= 0) {
632 if (true_maxage)
633 *true_maxage = expires;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100634 return MIN(expires, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100635 }
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100636
William Lallemand49b44532017-11-24 18:53:43 +0100637 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100638
639}
640
641
William Lallemanda400a3a2017-11-20 19:13:12 +0100642static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
643{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200644 struct cache_entry *object = (struct cache_entry *)block->data;
645
646 if (first == block && object->eb.key)
647 eb32_delete(&object->eb);
648 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100649}
650
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200651
652/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
653 * date value should be compared to a date determined by in a previous response (for
654 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
655 * value of the response's reception time (by decreasing order of priority). */
656static time_t get_last_modified_time(struct htx *htx)
657{
658 time_t last_modified = 0;
659 struct http_hdr_ctx ctx = { .blk = NULL };
660 struct tm tm = {};
661
662 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
663 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
664 last_modified = my_timegm(&tm);
665 }
666 }
667
668 if (!last_modified) {
669 ctx.blk = NULL;
670 if (http_find_header(htx, ist("date"), &ctx, 1)) {
671 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
672 last_modified = my_timegm(&tm);
673 }
674 }
675 }
676
677 /* Fallback on the current time if no "Last-Modified" or "Date" header
678 * was found. */
679 if (!last_modified)
680 last_modified = now.tv_sec;
681
682 return last_modified;
683}
684
William Lallemand41db4602017-10-30 11:15:51 +0100685/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100686 * Checks the vary header's value. The headers on which vary should be applied
687 * must be explicitely supported in the vary_information array (see cache.c). If
688 * any other header is mentioned, we won't store the response.
689 * Returns 1 if Vary-based storage can work, 0 otherwise.
690 */
691static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
692{
693 unsigned int vary_idx;
694 unsigned int vary_info_count;
695 const struct vary_hashing_information *vary_info;
696 struct http_hdr_ctx ctx = { .blk = NULL };
697
698 int retval = 1;
699
700 *vary_signature = 0;
701
702 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
703 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
704 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
705 vary_info = &vary_information[vary_idx];
706 if (isteqi(ctx.value, vary_info->hdr_name)) {
707 *vary_signature |= vary_info->value;
708 break;
709 }
710 }
711 retval = (vary_idx < vary_info_count);
712 }
713
714 return retval;
715}
716
717
718
719/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500720 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100721 * register a filter to store the data
722 */
723enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200724 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100725{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200726 long long hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100727 int effective_maxage = 0;
728 int true_maxage = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100729 struct http_txn *txn = s->txn;
730 struct http_msg *msg = &txn->rsp;
731 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100732 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100733 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100734 struct cache *cache = cconf->c.cache;
735 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100736 struct cache_st *cache_ctx = NULL;
737 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100738 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200739 struct htx *htx;
740 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200741 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200742 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200743 struct ist header_name = IST_NULL;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100744 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100745
William Lallemand4da3f8a2017-10-31 14:33:34 +0100746 /* Don't cache if the response came from a cache */
747 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
748 s->target == &http_cache_applet.obj_type) {
749 goto out;
750 }
751
752 /* cache only HTTP/1.1 */
753 if (!(txn->req.flags & HTTP_MSGF_VER_11))
754 goto out;
755
Willy Tarreau6905d182019-10-01 17:59:17 +0200756 /* cache only GET method */
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +0100757 if (txn->meth != HTTP_METH_GET) {
758 /* In case of successful unsafe method on a stored resource, the
759 * cached entry must be invalidated (see RFC7234#4.4).
760 * A "non-error response" is one with a 2xx (Successful) or 3xx
761 * (Redirection) status code. */
762 if (txn->status >= 200 && txn->status < 400) {
763 switch (txn->meth) {
764 case HTTP_METH_OPTIONS:
765 case HTTP_METH_GET:
766 case HTTP_METH_HEAD:
767 case HTTP_METH_TRACE:
768 break;
769
770 default: /* Any unsafe method */
771 /* Discard any corresponding entry in case of sucessful
772 * unsafe request (such as PUT, POST or DELETE). */
773 shctx_lock(shctx);
774
775 old = entry_exist(cconf->c.cache, txn->cache_hash);
776 if (old) {
777 eb32_delete(&old->eb);
778 old->eb.key = 0;
779 }
780 shctx_unlock(shctx);
781 }
782 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100783 goto out;
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +0100784 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100785
Willy Tarreauc9036c02019-01-11 19:38:25 +0100786 /* cache key was not computed */
787 if (!key)
788 goto out;
789
William Lallemand4da3f8a2017-10-31 14:33:34 +0100790 /* cache only 200 status code */
791 if (txn->status != 200)
792 goto out;
793
Christopher Faulet839791a2019-01-07 16:12:07 +0100794 /* Find the corresponding filter instance for the current stream */
795 list_for_each_entry(filter, &s->strm_flt.filters, list) {
796 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
797 /* No filter ctx, don't cache anything */
798 if (!filter->ctx)
799 goto out;
800 cache_ctx = filter->ctx;
801 break;
802 }
803 }
804
805 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200806 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100807
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200808 /* Do not cache too big objects. */
809 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
810 htx->data + htx->extra > shctx->max_obj_size)
811 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100812
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100813 /* Only a subset of headers are supported in our Vary implementation. If
814 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100815 * able to use the cache. Likewise, if Vary header support is disabled,
816 * avoid caching responses that contain such a header. */
817 ctx.blk = NULL;
818 if (cache->vary_processing_enabled) {
819 if (!http_check_vary_header(htx, &vary_signature))
820 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100821 if (vary_signature)
822 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100823 }
824 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200825 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100826 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100827
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200828 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100829
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100830 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200831 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100832
833 shctx_lock(shctx);
834 old = entry_exist(cache, txn->cache_hash);
835 if (old) {
836 if (vary_signature)
837 old = secondary_entry_exist(cconf->c.cache, old,
838 txn->cache_secondary_hash);
839 if (old) {
840 if (!old->complete) {
841 /* An entry with the same primary key is already being
842 * created, we should not try to store the current
843 * response because it will waste space in the cache. */
844 shctx_unlock(shctx);
845 goto out;
846 }
847 eb32_delete(&old->eb);
848 old->eb.key = 0;
849 }
850 }
851 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry));
852 if (!first) {
853 shctx_unlock(shctx);
854 goto out;
855 }
856 /* the received memory is not initialized, we need at least to mark
857 * the object as not indexed yet.
858 */
859 object = (struct cache_entry *)first->data;
860 memset(object, 0, sizeof(*object));
861 object->eb.key = key;
862 object->secondary_key_signature = vary_signature;
863 /* We need to temporarily set a valid expiring time until the actual one
864 * is set by the end of this function (in case of concurrent accesses to
865 * the same resource). This way the second access will find an existing
866 * but not yet usable entry in the tree and will avoid storing its data. */
867 object->expire = now.tv_sec + 2;
868
869 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
870 if (vary_signature)
871 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
872
873 /* Insert the entry in the tree even if the payload is not cached yet. */
874 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
875 object->eb.key = 0;
876 shctx_unlock(shctx);
877 goto out;
878 }
879 shctx_unlock(shctx);
880
881 /* reserve space for the cache_entry structure */
882 first->len = sizeof(struct cache_entry);
883 first->last_append = NULL;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100884
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100885 /* Determine the entry's maximum age (taking into account the cache's
886 * configuration) as well as the response's explicit max age (extracted
887 * from cache-control directives or the expires header). */
888 effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage);
889
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200890 ctx.blk = NULL;
891 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
892 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
893 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
894 hdr_age = CACHE_ENTRY_MAX_AGE;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100895 /* A response with an Age value greater than its
896 * announced max age is stale and should not be stored. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100897 object->age = hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100898 if (unlikely(object->age > true_maxage))
899 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100900 }
Remi Tricot-Le Breton51058d62020-12-03 18:19:32 +0100901 else
902 goto out;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200903 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100904 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100905
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200906 /* Build a last-modified time that will be stored in the cache_entry and
907 * compared to a future If-Modified-Since client header. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100908 object->last_modified = get_last_modified_time(htx);
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200909
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200910 chunk_reset(&trash);
911 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
912 struct htx_blk *blk = htx_get_blk(htx, pos);
913 enum htx_blk_type type = htx_get_blk_type(blk);
914 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100915
Christopher Fauletb0667472019-09-03 22:22:12 +0200916 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200917 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
918 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200919
920 /* Look for optional ETag header.
921 * We need to store the offset of the ETag value in order for
922 * future conditional requests to be able to perform ETag
923 * comparisons. */
924 if (type == HTX_BLK_HDR) {
925 header_name = htx_get_blk_name(htx, blk);
926 if (isteq(header_name, ist("etag"))) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100927 object->etag_length = sz - istlen(header_name);
928 object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200929 }
930 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200931 if (type == HTX_BLK_EOH)
932 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200933 }
934
Christopher Fauletb0667472019-09-03 22:22:12 +0200935 /* Do not cache objects if the headers are too big. */
936 if (hdrs_len > htx->size - global.tune.maxrewrite)
937 goto out;
938
William Lallemand4da3f8a2017-10-31 14:33:34 +0100939 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100940 if (!shctx_row_reserve_hot(shctx, first, trash.data)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100941 shctx_unlock(shctx);
942 goto out;
943 }
944 shctx_unlock(shctx);
945
William Lallemand4da3f8a2017-10-31 14:33:34 +0100946 /* cache the headers in a http action because it allows to chose what
947 * to cache, for example you might want to cache a response before
948 * modifying some HTTP headers, or on the contrary after modifying
949 * those headers.
950 */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100951 /* does not need to be locked because it's in the "hot" list,
952 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200953 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
954 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100955
956 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100957 if (cache_ctx) {
958 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100959
Christopher Faulet839791a2019-01-07 16:12:07 +0100960 /* store latest value and expiration time */
961 object->latest_validation = now.tv_sec;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100962 object->expire = now.tv_sec + effective_maxage;
Christopher Faulet839791a2019-01-07 16:12:07 +0100963 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100964 }
965
966out:
967 /* if does not cache */
968 if (first) {
969 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100970 first->len = 0;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100971 if (object->eb.key)
972 eb32_delete(&object->eb);
William Lallemand08727662017-11-21 20:01:27 +0100973 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100974 shctx_row_dec_hot(shctx, first);
975 shctx_unlock(shctx);
976 }
977
William Lallemand41db4602017-10-30 11:15:51 +0100978 return ACT_RET_CONT;
979}
980
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100981#define HTX_CACHE_INIT 0 /* Initial state. */
982#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
983#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200984#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
985#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100986
William Lallemandecb73b12017-11-24 14:33:55 +0100987static void http_cache_applet_release(struct appctx *appctx)
988{
Christopher Faulet95220e22018-12-07 17:34:39 +0100989 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100990 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100991 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100992 struct shared_block *first = block_ptr(cache_ptr);
993
994 shctx_lock(shctx_ptr(cache));
995 shctx_row_dec_hot(shctx_ptr(cache), first);
996 shctx_unlock(shctx_ptr(cache));
997}
998
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200999
1000static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
1001 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001002{
Christopher Faulet95220e22018-12-07 17:34:39 +01001003 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1004 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001005 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001006 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001007 unsigned int max, total;
1008 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001009
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001010 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1011 if (!max)
1012 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001013 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001014 ? (info & 0xff) + ((info >> 8) & 0xfffff)
1015 : info & 0xfffffff);
1016 if (blksz > max)
1017 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001018
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001019 blk = htx_add_blk(htx, type, blksz);
1020 if (!blk)
1021 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001022
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001023 blk->info = info;
1024 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001025 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001026 while (blksz) {
1027 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001028 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001029 offset += max;
1030 blksz -= max;
1031 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001032 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001033 if (blksz || offset == shctx->block_size) {
1034 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1035 offset = 0;
1036 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001037 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001038 appctx->ctx.cache.offset = offset;
1039 appctx->ctx.cache.next = shblk;
1040 appctx->ctx.cache.sent += total;
1041 return total;
1042}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001043
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001044static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
1045 uint32_t info, struct shared_block *shblk, unsigned int offset)
1046{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001047
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001048 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1049 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
1050 unsigned int max, total, rem_data;
1051 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001052
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001053 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1054 if (!max)
1055 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001056
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001057 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001058 if (appctx->ctx.cache.rem_data) {
1059 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001060 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001061 }
1062 else {
1063 blksz = (info & 0xfffffff);
1064 total = 4;
1065 }
1066 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001067 rem_data = blksz - max;
1068 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001069 }
1070
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001071 while (blksz) {
1072 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001073
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001074 max = MIN(blksz, shctx->block_size - offset);
1075 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1076 offset += sz;
1077 blksz -= sz;
1078 total += sz;
1079 if (sz < max)
1080 break;
1081 if (blksz || offset == shctx->block_size) {
1082 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1083 offset = 0;
1084 }
1085 }
1086
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001087 appctx->ctx.cache.offset = offset;
1088 appctx->ctx.cache.next = shblk;
1089 appctx->ctx.cache.sent += total;
1090 appctx->ctx.cache.rem_data = rem_data + blksz;
1091 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001092}
1093
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001094static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1095 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001096{
Christopher Faulet95220e22018-12-07 17:34:39 +01001097 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1098 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001099 struct shared_block *shblk;
1100 unsigned int offset, sz;
1101 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001102
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001103 while (len) {
1104 enum htx_blk_type type;
1105 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001106
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001107 shblk = appctx->ctx.cache.next;
1108 offset = appctx->ctx.cache.offset;
1109 if (appctx->ctx.cache.rem_data) {
1110 type = HTX_BLK_DATA;
1111 info = 0;
1112 goto add_data_blk;
1113 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001114
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001115 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001116 sz = MIN(4, shctx->block_size - offset);
1117 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1118 offset += sz;
1119 if (sz < 4) {
1120 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1121 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1122 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001123 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001124
1125 /* Get payload of the next HTX block and insert it. */
1126 type = (info >> 28);
1127 if (type != HTX_BLK_DATA)
1128 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1129 else {
1130 add_data_blk:
1131 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001132 }
1133
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001134 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001135 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001136 total += ret;
1137 len -= ret;
1138
1139 if (appctx->ctx.cache.rem_data || type == mark)
1140 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001141 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001142
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001143 return total;
1144}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001145
1146static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1147{
1148 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1149 unsigned int age;
1150 char *end;
1151
1152 chunk_reset(&trash);
1153 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1154 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1155 age = CACHE_ENTRY_MAX_AGE;
1156 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1157 b_set_data(&trash, end - b_head(&trash));
1158 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1159 return 0;
1160 return 1;
1161}
1162
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001163static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001164{
1165 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1166 struct shared_block *first = block_ptr(cache_ptr);
1167 struct stream_interface *si = appctx->owner;
1168 struct channel *req = si_oc(si);
1169 struct channel *res = si_ic(si);
1170 struct htx *req_htx, *res_htx;
1171 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001172 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001173 size_t ret, total = 0;
1174
1175 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001176 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001177
1178 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1179 goto out;
1180
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001181 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001182 if (!b_size(&res->buf)) {
1183 si_rx_room_blk(si);
1184 goto out;
1185 }
1186
Willy Tarreauefef3232018-12-16 00:37:45 +01001187 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001188 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001189
1190 if (appctx->st0 == HTX_CACHE_INIT) {
1191 appctx->ctx.cache.next = block_ptr(cache_ptr);
1192 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1193 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001194 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001195 appctx->st0 = HTX_CACHE_HEADER;
1196 }
1197
1198 if (appctx->st0 == HTX_CACHE_HEADER) {
1199 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001200 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1201 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1202 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1203 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001204 goto error;
1205
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001206 /* In case of a conditional request, we might want to send a
1207 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001208 if (appctx->ctx.cache.send_notmodified) {
1209 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1210 /* If replacing the status code fails we need to send the full response. */
1211 appctx->ctx.cache.send_notmodified = 0;
1212 }
1213 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001214
1215 /* Skip response body for HEAD requests or in case of "304 Not
1216 * Modified" response. */
1217 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001218 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001219 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001220 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001221 }
1222
1223 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001224 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1225 if (len) {
1226 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1227 if (ret < len) {
1228 si_rx_room_blk(si);
1229 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001230 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001231 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001232 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001233 }
1234
1235 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001236 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001237 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1238 si_rx_room_blk(si);
1239 goto out;
1240 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001241 appctx->st0 = HTX_CACHE_END;
1242 }
1243
1244 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001245 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001246 res->flags |= CF_READ_NULL;
1247 si_shutr(si);
1248 }
1249
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001250 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001251 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001252 if (total)
1253 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001254 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001255
1256 /* eat the whole request */
1257 if (co_data(req)) {
1258 req_htx = htx_from_buf(&req->buf);
1259 co_htx_skip(req, req_htx, co_data(req));
1260 htx_to_buf(req_htx, &req->buf);
1261 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001262 return;
1263
1264 error:
1265 /* Sent and HTTP error 500 */
1266 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001267 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001268 res->buf.data = b_data(errmsg);
1269 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1270 res_htx = htx_from_buf(&res->buf);
1271
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001272 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001273 appctx->st0 = HTX_CACHE_END;
1274 goto end;
1275}
1276
1277
Christopher Faulet95220e22018-12-07 17:34:39 +01001278static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001279{
1280 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001281 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001282
Christopher Faulet95220e22018-12-07 17:34:39 +01001283 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001284 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001285 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001286 }
1287
1288 /* check if a cache filter was already registered with this cache
1289 * name, if that's the case, must use it. */
1290 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001291 if (fconf->id == cache_store_flt_id) {
1292 cconf = fconf->conf;
1293 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1294 rule->arg.act.p[0] = cconf;
1295 return 1;
1296 }
William Lallemand41db4602017-10-30 11:15:51 +01001297 }
1298 }
1299
Christopher Faulet95220e22018-12-07 17:34:39 +01001300 /* Create the filter cache config */
1301 cconf = calloc(1, sizeof(*cconf));
1302 if (!cconf) {
1303 memprintf(err, "out of memory\n");
1304 goto err;
1305 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001306 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001307 cconf->c.name = strdup(name);
1308 if (!cconf->c.name) {
1309 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001310 goto err;
1311 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001312
William Lallemand41db4602017-10-30 11:15:51 +01001313 /* register a filter to fill the cache buffer */
1314 fconf = calloc(1, sizeof(*fconf));
1315 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001316 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001317 goto err;
1318 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001319 fconf->id = cache_store_flt_id;
1320 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001321 fconf->ops = &cache_ops;
1322 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1323
Christopher Faulet95220e22018-12-07 17:34:39 +01001324 rule->arg.act.p[0] = cconf;
1325 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001326
Christopher Faulet95220e22018-12-07 17:34:39 +01001327 err:
1328 free(cconf);
1329 return 0;
1330}
1331
1332enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1333 struct act_rule *rule, char **err)
1334{
1335 rule->action = ACT_CUSTOM;
1336 rule->action_ptr = http_action_store_cache;
1337
1338 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1339 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001340
Christopher Faulet95220e22018-12-07 17:34:39 +01001341 (*orig_arg)++;
1342 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001343}
1344
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001345/* This produces a sha1 hash of the concatenation of the HTTP method,
1346 * the first occurrence of the Host header followed by the path component
1347 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001348int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001349{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001350 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001351 struct htx *htx = htxbuf(&s->req.buf);
1352 struct htx_sl *sl;
1353 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001354 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001355 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001356 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001357
William Lallemandf528fff2017-11-23 19:43:17 +01001358 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001359 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001360
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001361 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001362 uri = htx_sl_req_uri(sl); // whole uri
1363 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001364 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001365
1366 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1367 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1368 * URIs are almost always sent in absolute form with their scheme. In
1369 * this case, the scheme is almost always "https". In order to support
1370 * sharing of cache objects between H1 and H2, we'll hash the absolute
1371 * URI whenever known, or prepend "https://" + the Host header for
1372 * relative URIs. The difference will only appear on absolute HTTP/1
1373 * requests sent to an origin server, which practically is never met in
1374 * the real world so we don't care about the ability to share the same
1375 * key here.URIs are normalized from the absolute URI to an origin form as
1376 * well.
1377 */
1378 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001379 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001380 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1381 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001382 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001383 }
1384
1385 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001386
1387 /* hash everything */
1388 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001389 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001390 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1391
1392 return 1;
1393}
1394
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001395/* Looks for "If-None-Match" headers in the request and compares their value
1396 * with the one that might have been stored in the cache_entry. If any of them
1397 * matches, a "304 Not Modified" response should be sent instead of the cached
1398 * data.
1399 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001400 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1401 *
1402 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1403 * header and compare its value (date) to the one stored in the cache_entry.
1404 * If the request's date is later than the cached one, we also send a
1405 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1406 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001407 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1408 */
1409static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1410 struct cache_entry *entry)
1411{
1412 int retval = 0;
1413
1414 struct http_hdr_ctx ctx = { .blk = NULL };
1415 struct ist cache_entry_etag = IST_NULL;
1416 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001417 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001418
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001419 struct tm tm = {};
1420 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001421
1422 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001423 * cache_entry's ETag in order to perform comparisons.
1424 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001425 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001426 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001427
1428 /* A '*' matches everything. */
1429 if (isteq(ctx.value, ist("*")) != 0) {
1430 retval = 1;
1431 break;
1432 }
1433
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001434 /* No need to rebuild an etag if none was stored in the cache. */
1435 if (entry->etag_length == 0)
1436 break;
1437
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001438 /* Rebuild the stored ETag. */
1439 if (etag_buffer == NULL) {
1440 etag_buffer = get_trash_chunk();
1441
1442 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1443 (unsigned char*)b_orig(etag_buffer),
1444 entry->etag_offset, entry->etag_length) == 0) {
1445 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1446 } else {
1447 /* We could not rebuild the ETag in one go, we
1448 * won't send a "304 Not Modified" response. */
1449 break;
1450 }
1451 }
1452
1453 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1454 retval = 1;
1455 break;
1456 }
1457 }
1458
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001459 /* If the request did not contain an "If-None-Match" header, we look for
1460 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1461 if (retval == 0 && if_none_match_found == 0) {
1462 ctx.blk = NULL;
1463 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1464 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1465 if_modified_since = my_timegm(&tm);
1466
1467 /* We send a "304 Not Modified" response if the
1468 * entry's last modified date is earlier than
1469 * the one found in the "If-Modified-Since"
1470 * header. */
1471 retval = (entry->last_modified <= if_modified_since);
1472 }
1473 }
1474 }
1475
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001476 return retval;
1477}
1478
William Lallemand41db4602017-10-30 11:15:51 +01001479enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1480 struct session *sess, struct stream *s, int flags)
1481{
William Lallemand77c11972017-10-31 20:43:01 +01001482
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001483 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001484 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001485 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1486 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001487 struct shared_block *entry_block;
1488
William Lallemand77c11972017-10-31 20:43:01 +01001489
Willy Tarreau6905d182019-10-01 17:59:17 +02001490 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1491 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001492 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001493 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001494 txn->flags |= TX_CACHE_IGNORE;
1495
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001496 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001497
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001498 /* The request's hash has to be calculated for all requests, even POSTs
1499 * or PUTs for instance because RFC7234 specifies that a sucessful
1500 * "unsafe" method on a stored resource must invalidate it
1501 * (see RFC7234#4.4). */
1502 if (!sha1_hosturi(s))
Willy Tarreau504455c2017-12-22 17:47:35 +01001503 return ACT_RET_CONT;
1504
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001505 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001506 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001507
Willy Tarreau504455c2017-12-22 17:47:35 +01001508 if (s->txn->flags & TX_CACHE_IGNORE)
1509 return ACT_RET_CONT;
1510
Willy Tarreaua1214a52018-12-14 14:00:25 +01001511 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001512 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001513 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001514 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001515
William Lallemanda400a3a2017-11-20 19:13:12 +01001516 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001517 res = entry_exist(cache, s->txn->cache_hash);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001518 /* We must not use an entry that is not complete. */
1519 if (res && res->complete) {
William Lallemand77c11972017-10-31 20:43:01 +01001520 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001521 entry_block = block_ptr(res);
1522 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001523 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001524
1525 /* In case of Vary, we could have multiple entries with the same
1526 * primary hash. We need to calculate the secondary has in order
1527 * to find the actual entry we want (if it exists). */
1528 if (res->secondary_key_signature) {
1529 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1530 shctx_lock(shctx_ptr(cache));
1531 sec_entry = secondary_entry_exist(cache, res,
1532 s->txn->cache_secondary_hash);
1533 if (sec_entry && sec_entry != res) {
1534 /* The wrong row was added to the hot list. */
1535 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1536 entry_block = block_ptr(sec_entry);
1537 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1538 }
1539 res = sec_entry;
1540 shctx_unlock(shctx_ptr(cache));
1541 }
1542 else
1543 res = NULL;
1544 }
1545
1546 /* We looked for a valid secondary entry and could not find one,
1547 * the request must be forwarded to the server. */
1548 if (!res) {
1549 shctx_lock(shctx_ptr(cache));
1550 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1551 shctx_unlock(shctx_ptr(cache));
1552 return ACT_RET_CONT;
1553 }
1554
William Lallemand77c11972017-10-31 20:43:01 +01001555 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001556 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001557 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001558 appctx->rule = rule;
1559 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001560 appctx->ctx.cache.next = NULL;
1561 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001562 appctx->ctx.cache.send_notmodified =
1563 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001564
1565 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001566 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001567 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001568 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001569 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001570 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001571 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001572 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001573 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001574 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001575 }
1576 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001577 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001578
1579 /* Shared context does not need to be locked while we calculate the
1580 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001581 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001582 /* Build a complete secondary hash until the server response
1583 * tells us which fields should be kept (if any). */
1584 http_request_prebuild_full_secondary_key(s);
1585 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001586 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001587}
1588
1589
1590enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1591 struct act_rule *rule, char **err)
1592{
William Lallemand41db4602017-10-30 11:15:51 +01001593 rule->action = ACT_CUSTOM;
1594 rule->action_ptr = http_action_req_cache_use;
1595
Christopher Faulet95220e22018-12-07 17:34:39 +01001596 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001597 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001598
1599 (*orig_arg)++;
1600 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001601}
1602
1603int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1604{
1605 int err_code = 0;
1606
1607 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1608
1609 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001610 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001611 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001612 err_code |= ERR_ALERT | ERR_ABORT;
1613 goto out;
1614 }
1615
1616 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1617 err_code |= ERR_ABORT;
1618 goto out;
1619 }
1620
1621 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001622 struct cache *cache_config;
1623
William Lallemand41db4602017-10-30 11:15:51 +01001624 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1625 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001626 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001627 err_code |= ERR_ALERT | ERR_ABORT;
1628 goto out;
1629 }
1630
1631 strlcpy2(tmp_cache_config->id, args[1], 33);
1632 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001633 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001634 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001635 err_code |= ERR_WARN;
1636 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001637
1638 list_for_each_entry(cache_config, &caches_config, list) {
1639 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1640 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1641 file, linenum, tmp_cache_config->id);
1642 err_code |= ERR_ALERT | ERR_ABORT;
1643 goto out;
1644 }
1645 }
1646
William Lallemand49b44532017-11-24 18:53:43 +01001647 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001648 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001649 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001650 }
1651 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001652 unsigned long int maxsize;
1653 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001654
1655 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1656 err_code |= ERR_ABORT;
1657 goto out;
1658 }
1659
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001660 maxsize = strtoul(args[1], &err, 10);
1661 if (err == args[1] || *err != '\0') {
1662 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1663 file, linenum, args[1]);
1664 err_code |= ERR_ABORT;
1665 goto out;
1666 }
1667
1668 if (maxsize > (UINT_MAX >> 20)) {
1669 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1670 file, linenum, args[1], UINT_MAX >> 20);
1671 err_code |= ERR_ABORT;
1672 goto out;
1673 }
1674
William Lallemand41db4602017-10-30 11:15:51 +01001675 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001676 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001677 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001678 } else if (strcmp(args[0], "max-age") == 0) {
1679 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1680 err_code |= ERR_ABORT;
1681 goto out;
1682 }
1683
1684 if (!*args[1]) {
1685 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1686 file, linenum, args[0]);
1687 err_code |= ERR_WARN;
1688 }
1689
1690 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001691 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001692 unsigned int maxobjsz;
1693 char *err;
1694
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001695 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1696 err_code |= ERR_ABORT;
1697 goto out;
1698 }
1699
1700 if (!*args[1]) {
1701 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1702 file, linenum, args[0]);
1703 err_code |= ERR_WARN;
1704 }
1705
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001706 maxobjsz = strtoul(args[1], &err, 10);
1707 if (err == args[1] || *err != '\0') {
1708 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1709 file, linenum, args[1]);
1710 err_code |= ERR_ABORT;
1711 goto out;
1712 }
1713 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001714 } else if (strcmp(args[0], "process-vary") == 0) {
1715 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1716 err_code |= ERR_ABORT;
1717 goto out;
1718 }
1719
1720 if (!*args[1]) {
1721 ha_warning("parsing [%s:%d]: '%s' expects 0 or 1 (disable or enable vary processing).\n",
1722 file, linenum, args[0]);
1723 err_code |= ERR_WARN;
1724 }
1725
1726 tmp_cache_config->vary_processing_enabled = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001727 }
1728 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001729 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001730 err_code |= ERR_ALERT | ERR_FATAL;
1731 goto out;
1732 }
1733out:
1734 return err_code;
1735}
1736
1737/* once the cache section is parsed */
1738
1739int cfg_post_parse_section_cache()
1740{
William Lallemand41db4602017-10-30 11:15:51 +01001741 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001742
1743 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001744
1745 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001746 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001747 err_code |= ERR_FATAL | ERR_ALERT;
1748 goto out;
1749 }
1750
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001751 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001752 /* Default max. file size is a 256th of the cache size. */
1753 tmp_cache_config->maxobjsz =
1754 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001755 }
1756 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1757 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1758 err_code |= ERR_FATAL | ERR_ALERT;
1759 goto out;
1760 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001761
William Lallemandd1d1e222019-08-28 15:22:49 +02001762 /* add to the list of cache to init and reinit tmp_cache_config
1763 * for next cache section, if any.
1764 */
1765 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1766 tmp_cache_config = NULL;
1767 return err_code;
1768 }
1769out:
1770 free(tmp_cache_config);
1771 tmp_cache_config = NULL;
1772 return err_code;
1773
1774}
1775
1776int post_check_cache()
1777{
1778 struct proxy *px;
1779 struct cache *back, *cache_config, *cache;
1780 struct shared_context *shctx;
1781 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001782 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02001783
1784 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1785
1786 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1787 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001788
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001789 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001790 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001791 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001792 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001793 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001794
1795 err_code |= ERR_FATAL | ERR_ALERT;
1796 goto out;
1797 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001798 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001799 /* the cache structure is stored in the shctx and added to the
1800 * caches list, we can remove the entry from the caches_config
1801 * list */
1802 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001803 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001804 cache->entries = EB_ROOT;
William Lallemand41db4602017-10-30 11:15:51 +01001805 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001806 LIST_DEL(&cache_config->list);
1807 free(cache_config);
1808
1809 /* Find all references for this cache in the existing filters
1810 * (over all proxies) and reference it in matching filters.
1811 */
1812 for (px = proxies_list; px; px = px->next) {
1813 struct flt_conf *fconf;
1814 struct cache_flt_conf *cconf;
1815
1816 list_for_each_entry(fconf, &px->filter_configs, list) {
1817 if (fconf->id != cache_store_flt_id)
1818 continue;
1819
1820 cconf = fconf->conf;
1821 if (!strcmp(cache->id, cconf->c.name)) {
1822 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001823 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001824 cconf->c.cache = cache;
1825 break;
1826 }
1827 }
1828 }
William Lallemand41db4602017-10-30 11:15:51 +01001829 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001830
William Lallemand41db4602017-10-30 11:15:51 +01001831out:
William Lallemand41db4602017-10-30 11:15:51 +01001832 return err_code;
1833
William Lallemand41db4602017-10-30 11:15:51 +01001834}
1835
William Lallemand41db4602017-10-30 11:15:51 +01001836struct flt_ops cache_ops = {
1837 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001838 .check = cache_store_check,
1839 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001840
Christopher Faulet65554e12020-03-06 14:52:06 +01001841 /* Handle stream init/deinit */
1842 .attach = cache_store_strm_init,
1843 .detach = cache_store_strm_deinit,
1844
William Lallemand4da3f8a2017-10-31 14:33:34 +01001845 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001846 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001847
1848 /* Filter HTTP requests and responses */
1849 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001850 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001851 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001852};
1853
Christopher Faulet99a17a22018-12-11 09:18:27 +01001854
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001855int accept_encoding_cmp(const void *a, const void *b)
1856{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001857 unsigned int int_a = *(unsigned int*)a;
1858 unsigned int int_b = *(unsigned int*)b;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001859
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001860 if (int_a < int_b)
1861 return -1;
1862 if (int_a > int_b)
1863 return 1;
1864 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001865}
1866
Tim Duesterhus23b29452020-11-24 22:22:56 +01001867#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001868/*
1869 * Build a hash of the accept-encoding header. The different parts of the
1870 * header value are first sorted, appended and then a crc is calculated
1871 * for the newly constructed buffer.
1872 * Returns 0 in case of success.
1873 */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001874static int accept_encoding_normalizer(struct ist full_value, char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001875{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001876 unsigned int values[ACCEPT_ENCODING_MAX_ENTRIES] = {};
Tim Duesterhus23b29452020-11-24 22:22:56 +01001877 size_t count = 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001878 char *comma = NULL;
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001879 unsigned int hash_value = 0;
1880 unsigned int prev = 0, curr = 0;
1881
1882 /* Turn accept-encoding value to lower case */
1883 full_value = ist2bin_lc(istptr(full_value), full_value);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001884
1885 /* The hash will be built out of a sorted list of accepted encodings. */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001886 while (count < (ACCEPT_ENCODING_MAX_ENTRIES - 1) && (comma = istchr(full_value, ',')) != NULL) {
1887 size_t length = comma - istptr(full_value);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001888
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001889 values[count++] = hash_crc32(istptr(full_value), length);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001890
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001891 full_value = istadv(full_value, length + 1);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001892
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001893 }
1894 values[count++] = hash_crc32(istptr(full_value), istlen(full_value));
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001895
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001896 /* Sort the values alphabetically. */
1897 qsort(values, count, sizeof(*values), &accept_encoding_cmp);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001898
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001899 while (count) {
1900 curr = values[--count];
1901 if (curr != prev) {
1902 hash_value ^= curr;
1903 }
1904 prev = curr;
1905 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001906
1907 memcpy(buf, &hash_value, sizeof(hash_value));
1908 *buf_len = sizeof(hash_value);
1909
Tim Duesterhus23b29452020-11-24 22:22:56 +01001910 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001911}
Tim Duesterhus23b29452020-11-24 22:22:56 +01001912#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001913
1914/*
1915 * Normalizer used by default for User-Agent and Referer headers. It only
1916 * calculates a simple crc of the whole value.
1917 * Returns 0 in case of success.
1918 */
1919static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len)
1920{
1921 int hash_value = 0;
1922
1923 hash_value = hash_crc32(istptr(value), istlen(value));
1924
1925 memcpy(buf, &hash_value, sizeof(hash_value));
1926 *buf_len = sizeof(hash_value);
1927
1928 return 0;
1929}
1930
1931
1932/*
1933 * Pre-calculate the hashes of all the supported headers (in our Vary
1934 * implementation) of a given request. We have to calculate all the hashes
1935 * in advance because the actual Vary signature won't be known until the first
1936 * response.
1937 * Only the first occurrence of every header will be taken into account in the
1938 * hash.
1939 * If the header is not present, the hash portion of the given header will be
1940 * filled with zeros.
1941 * Returns 0 in case of success.
1942 */
1943static int http_request_prebuild_full_secondary_key(struct stream *s)
1944{
1945 struct http_txn *txn = s->txn;
1946 struct htx *htx = htxbuf(&s->req.buf);
1947 struct http_hdr_ctx ctx = { .blk = NULL };
1948
1949 unsigned int idx;
1950 const struct vary_hashing_information *info = NULL;
1951 unsigned int hash_length = 0;
1952 int retval = 0;
1953 int offset = 0;
1954
1955 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1956 info = &vary_information[idx];
1957
1958 ctx.blk = NULL;
1959 if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) {
1960 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1961 offset += hash_length;
1962 }
1963 else {
1964 /* Fill hash with 0s. */
1965 hash_length = info->hash_length;
1966 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1967 offset += hash_length;
1968 }
1969 }
1970
1971 return retval;
1972}
1973
1974
1975/*
1976 * Calculate the secondary key for a request for which we already have a known
1977 * vary signature. The key is made by aggregating hashes calculated for every
1978 * header mentioned in the vary signature.
1979 * Only the first occurrence of every header will be taken into account in the
1980 * hash.
1981 * If the header is not present, the hash portion of the given header will be
1982 * filled with zeros.
1983 * Returns 0 in case of success.
1984 */
1985static int http_request_build_secondary_key(struct stream *s, int vary_signature)
1986{
1987 struct http_txn *txn = s->txn;
1988 struct htx *htx = htxbuf(&s->req.buf);
1989 struct http_hdr_ctx ctx = { .blk = NULL };
1990
1991 unsigned int idx;
1992 const struct vary_hashing_information *info = NULL;
1993 unsigned int hash_length = 0;
1994 int retval = 0;
1995 int offset = 0;
1996
1997 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1998 info = &vary_information[idx];
1999
2000 ctx.blk = NULL;
2001 if ((vary_signature & info->value) && info->norm_fn != NULL &&
2002 http_find_header(htx, info->hdr_name, &ctx, 1)) {
2003 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
2004 offset += hash_length;
2005 }
2006 else {
2007 /* Fill hash with 0s. */
2008 hash_length = info->hash_length;
2009 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
2010 offset += hash_length;
2011 }
2012 }
2013
2014 return retval;
2015}
2016
2017/*
2018 * Build the actual secondary key of a given request out of the prebuilt key and
2019 * the actual vary signature (extracted from the response).
2020 * Returns 0 in case of success.
2021 */
2022static int http_request_reduce_secondary_key(unsigned int vary_signature,
2023 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
2024{
2025 int offset = 0;
2026 int global_offset = 0;
2027 int vary_info_count = 0;
2028 int keep = 0;
2029 unsigned int vary_idx;
2030 const struct vary_hashing_information *vary_info;
2031
2032 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
2033 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
2034 vary_info = &vary_information[vary_idx];
2035 keep = (vary_signature & vary_info->value) ? 0xff : 0;
2036
2037 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
2038 prebuilt_key[global_offset] &= keep;
2039 }
2040 }
2041
2042 return 0;
2043}
2044
2045
Christopher Faulet99a17a22018-12-11 09:18:27 +01002046
2047static int
2048parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
2049 struct flt_conf *fconf, char **err, void *private)
2050{
2051 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01002052 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002053 char *name = NULL;
2054 int pos = *cur_arg;
2055
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002056 /* Get the cache filter name. <pos> point on "cache" keyword */
2057 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02002058 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002059 goto error;
2060 }
2061 name = strdup(args[pos + 1]);
2062 if (!name) {
2063 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
2064 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002065 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002066 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002067
2068 /* Check if an implicit filter with the same name already exists. If so,
2069 * we remove the implicit filter to use the explicit one. */
2070 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
2071 if (f->id != cache_store_flt_id)
2072 continue;
2073
2074 cconf = f->conf;
2075 if (strcmp(name, cconf->c.name)) {
2076 cconf = NULL;
2077 continue;
2078 }
2079
2080 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2081 cconf = NULL;
2082 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2083 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002084 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002085 }
2086
2087 /* Remove the implicit filter. <cconf> is kept for the explicit one */
2088 LIST_DEL(&f->list);
2089 free(f);
2090 free(name);
2091 break;
2092 }
2093
2094 /* No implicit cache filter found, create configuration for the explicit one */
2095 if (!cconf) {
2096 cconf = calloc(1, sizeof(*cconf));
2097 if (!cconf) {
2098 memprintf(err, "%s: out of memory", args[*cur_arg]);
2099 goto error;
2100 }
2101 cconf->c.name = name;
2102 }
2103
2104 cconf->flags = 0;
2105 fconf->id = cache_store_flt_id;
2106 fconf->conf = cconf;
2107 fconf->ops = &cache_ops;
2108
2109 *cur_arg = pos;
2110 return 0;
2111
2112 error:
2113 free(name);
2114 free(cconf);
2115 return -1;
2116}
2117
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002118static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002119{
2120 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2121 return 1;
2122
2123 return 0;
2124}
2125
2126static int cli_io_handler_show_cache(struct appctx *appctx)
2127{
2128 struct cache* cache = appctx->ctx.cli.p0;
2129 struct stream_interface *si = appctx->owner;
2130
William Lallemand1f49a362017-11-21 20:01:26 +01002131 if (cache == NULL) {
2132 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
2133 }
2134
2135 list_for_each_entry_from(cache, &caches, list) {
2136 struct eb32_node *node = NULL;
2137 unsigned int next_key;
2138 struct cache_entry *entry;
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002139 unsigned int i;
William Lallemand1f49a362017-11-21 20:01:26 +01002140
William Lallemand1f49a362017-11-21 20:01:26 +01002141 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002142 if (!next_key) {
2143 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
2144 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002145 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002146 return 0;
2147 }
2148 }
William Lallemand1f49a362017-11-21 20:01:26 +01002149
2150 appctx->ctx.cli.p0 = cache;
2151
2152 while (1) {
2153
2154 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002155 if (!node || (node = eb32_next_dup(node)) == NULL)
2156 node = eb32_lookup_ge(&cache->entries, next_key);
William Lallemand1f49a362017-11-21 20:01:26 +01002157 if (!node) {
2158 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02002159 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002160 break;
2161 }
2162
2163 entry = container_of(node, struct cache_entry, eb);
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002164 chunk_printf(&trash, "%p hash:%u vary:0x", entry, read_u32(entry->hash));
2165 for (i = 0; i < HTTP_CACHE_SEC_KEY_LEN; ++i)
2166 chunk_appendf(&trash, "%02x", (unsigned char)entry->secondary_key[i]);
2167 chunk_appendf(&trash, " size:%u (%u blocks), refcount:%u, expire:%d\n", 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 +01002168
2169 next_key = node->key + 1;
2170 appctx->ctx.cli.i0 = next_key;
2171
2172 shctx_unlock(shctx_ptr(cache));
2173
2174 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002175 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01002176 return 0;
2177 }
2178 }
2179
2180 }
2181
2182 return 1;
2183
2184}
2185
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002186
2187/*
2188 * boolean, returns true if response was built out of a cache entry.
2189 */
2190static int
2191smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2192 const char *kw, void *private)
2193{
2194 smp->data.type = SMP_T_BOOL;
2195 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2196
2197 return 1;
2198}
2199
2200/*
2201 * string, returns cache name (if response came from a cache).
2202 */
2203static int
2204smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2205 const char *kw, void *private)
2206{
2207 struct appctx *appctx = NULL;
2208
2209 struct cache_flt_conf *cconf = NULL;
2210 struct cache *cache = NULL;
2211
2212 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2213 return 0;
2214
2215 /* Get appctx from the stream_interface. */
2216 appctx = si_appctx(&smp->strm->si[1]);
2217 if (appctx && appctx->rule) {
2218 cconf = appctx->rule->arg.act.p[0];
2219 if (cconf) {
2220 cache = cconf->c.cache;
2221
2222 smp->data.type = SMP_T_STR;
2223 smp->flags = SMP_F_CONST;
2224 smp->data.u.str.area = cache->id;
2225 smp->data.u.str.data = strlen(cache->id);
2226 return 1;
2227 }
2228 }
2229
2230 return 0;
2231}
2232
Christopher Faulet99a17a22018-12-11 09:18:27 +01002233/* Declare the filter parser for "cache" keyword */
2234static struct flt_kw_list filter_kws = { "CACHE", { }, {
2235 { "cache", parse_cache_flt, NULL },
2236 { NULL, NULL, NULL },
2237 }
2238};
2239
2240INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2241
William Lallemand1f49a362017-11-21 20:01:26 +01002242static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01002243 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
2244 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002245}};
2246
Willy Tarreau0108d902018-11-25 19:14:37 +01002247INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002248
William Lallemand41db4602017-10-30 11:15:51 +01002249static struct action_kw_list http_res_actions = {
2250 .kw = {
2251 { "cache-store", parse_cache_store },
2252 { NULL, NULL }
2253 }
2254};
2255
Willy Tarreau0108d902018-11-25 19:14:37 +01002256INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2257
William Lallemand41db4602017-10-30 11:15:51 +01002258static struct action_kw_list http_req_actions = {
2259 .kw = {
2260 { "cache-use", parse_cache_use },
2261 { NULL, NULL }
2262 }
2263};
2264
Willy Tarreau0108d902018-11-25 19:14:37 +01002265INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2266
Willy Tarreau2231b632019-03-29 18:26:52 +01002267struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002268 .obj_type = OBJ_TYPE_APPLET,
2269 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002270 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002271 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002272};
2273
Willy Tarreaue6552512018-11-26 11:33:13 +01002274/* config parsers for this section */
2275REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002276REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002277
2278
2279/* Note: must not be declared <const> as its list will be overwritten */
2280static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2281 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2282 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2283 { /* END */ },
2284 }
2285};
2286
2287INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);