blob: 07ecc03d46d826346faf78273dd5d868f89549de [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) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010052 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010053};
54
Christopher Faulet95220e22018-12-07 17:34:39 +010055/* cache config for filters */
56struct cache_flt_conf {
57 union {
58 struct cache *cache; /* cache used by the filter */
59 char *name; /* cache name used during conf parsing */
60 } c;
61 unsigned int flags; /* CACHE_FLT_F_* */
62};
63
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +010064
65/*
66 * Vary-related structures and functions
67 */
68enum vary_header_bit {
69 VARY_ACCEPT_ENCODING = (1 << 0),
70 VARY_REFERER = (1 << 1),
71 VARY_LAST /* should always be last */
72};
73
74typedef int(*http_header_normalizer)(struct ist value, char *buf, unsigned int *buf_len);
75
76struct vary_hashing_information {
77 struct ist hdr_name; /* Header name */
78 enum vary_header_bit value; /* Bit repesenting the header in a vary signature */
79 unsigned int hash_length; /* Size of the sub hash for this header's value */
80 http_header_normalizer norm_fn; /* Normalization function */
81};
82
83static int accept_encoding_normalizer(struct ist value, char *buf, unsigned int *buf_len);
84static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len);
85
86/* Warning : do not forget to update HTTP_CACHE_SEC_KEY_LEN when new items are
87 * added to this array. */
88const struct vary_hashing_information vary_information[] = {
89 { IST("accept-encoding"), VARY_ACCEPT_ENCODING, sizeof(int), &accept_encoding_normalizer },
90 { IST("referer"), VARY_REFERER, sizeof(int), &default_normalizer },
91};
92
93static int http_request_prebuild_full_secondary_key(struct stream *s);
94static int http_request_build_secondary_key(struct stream *s, int vary_signature);
95static int http_request_reduce_secondary_key(unsigned int vary_signature,
96 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]);
97
98
William Lallemand41db4602017-10-30 11:15:51 +010099/*
100 * cache ctx for filters
101 */
102struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +0100103 struct shared_block *first_block;
104};
105
106struct cache_entry {
107 unsigned int latest_validation; /* latest validation date */
108 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200109 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100110
William Lallemand41db4602017-10-30 11:15:51 +0100111 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +0100112 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200113
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100114 char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */
115 unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used
116 * to build secondary keys for this cache entry. */
117
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200118 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
119 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
120
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200121 time_t last_modified; /* Origin server "Last-Modified" header value converted in
122 * seconds since epoch. If no "Last-Modified"
123 * header is found, use "Date" header value,
124 * otherwise use reception time. This field will
125 * be used in case of an "If-Modified-Since"-based
126 * conditional request. */
127
William Lallemand41db4602017-10-30 11:15:51 +0100128 unsigned char data[0];
129};
130
131#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +0100132#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100133
134static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +0200135static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +0100136static struct cache *tmp_cache_config = NULL;
137
Willy Tarreau8ceae722018-11-26 11:58:30 +0100138DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
139
William Lallemandf528fff2017-11-23 19:43:17 +0100140struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100141{
142 struct eb32_node *node;
143 struct cache_entry *entry;
144
Willy Tarreau8b507582020-02-25 09:35:07 +0100145 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100146 if (!node)
147 return NULL;
148
149 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100150
151 /* if that's not the right node */
152 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
153 return NULL;
154
William Lallemand08727662017-11-21 20:01:27 +0100155 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100156 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100157 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100158 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100159 entry->eb.key = 0;
160 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100161 return NULL;
162
163}
164
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100165/*
166 * There can be multiple entries with the same primary key in the ebtree so in
167 * order to get the proper one out of the list, we use a secondary_key.
168 * This function simply iterates over all the entries with the same primary_key
169 * until it finds the right one.
170 * Returns the cache_entry in case of success, NULL otherwise.
171 */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100172struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry,
173 char *secondary_key)
174{
175 struct eb32_node *node = &entry->eb;
176
177 if (!entry->secondary_key_signature)
178 return NULL;
179
180 while (entry && memcmp(entry->secondary_key, secondary_key, HTTP_CACHE_SEC_KEY_LEN) != 0) {
181 node = eb32_next_dup(node);
182 entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL;
183 }
184
185 /* Expired entry */
186 if (entry && entry->expire <= now.tv_sec) {
187 eb32_delete(&entry->eb);
188 entry->eb.key = 0;
189 entry = NULL;
190 }
191
192 return entry;
193}
194
William Lallemand4da3f8a2017-10-31 14:33:34 +0100195static inline struct shared_context *shctx_ptr(struct cache *cache)
196{
197 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
198}
199
William Lallemand77c11972017-10-31 20:43:01 +0100200static inline struct shared_block *block_ptr(struct cache_entry *entry)
201{
202 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
203}
204
205
206
William Lallemand41db4602017-10-30 11:15:51 +0100207static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100208cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100209{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100210 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100211 return 0;
212}
213
Christopher Faulet95220e22018-12-07 17:34:39 +0100214static void
215cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
216{
217 struct cache_flt_conf *cconf = fconf->conf;
218
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200219 if (!(cconf->flags & CACHE_FLT_INIT))
220 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100221 free(cconf);
222}
223
William Lallemand4da3f8a2017-10-31 14:33:34 +0100224static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100225cache_store_check(struct proxy *px, struct flt_conf *fconf)
226{
227 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100228 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100229 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100230 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100231
William Lallemandd1d1e222019-08-28 15:22:49 +0200232 /* Find the cache corresponding to the name in the filter config. The
233 * cache will not be referenced now in the filter config because it is
234 * not fully allocated. This step will be performed during the cache
235 * post_check.
236 */
237 list_for_each_entry(cache, &caches_config, list) {
238 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100239 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100240 }
241
242 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
243 proxy_type_str(px), px->id, (char *)cconf->c.name);
244 return 1;
245
246 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100247 /* Here <cache> points on the cache the filter must use and <cconf>
248 * points on the cache filter configuration. */
249
250 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100251 * enabled and if it is after the cache. When the compression is before
252 * the cache, an error is returned. Also check if the cache filter must
253 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100254 list_for_each_entry(f, &px->filter_configs, list) {
255 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100256 /* The compression filter must be evaluated after the cache. */
257 if (comp) {
258 ha_alert("config: %s '%s': unable to enable the compression filter before "
259 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
260 return 1;
261 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100262 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200263 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100264 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200265 else if (f->id == fcgi_flt_id)
266 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100267 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
268 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200269 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100270 * declaration is required. */
271 ha_alert("config: %s '%s': require an explicit filter declaration "
272 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
273 return 1;
274 }
275
Christopher Fauletafd819c2018-12-11 08:57:45 +0100276 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100277 return 0;
278}
279
280static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100281cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100282{
Christopher Faulet65554e12020-03-06 14:52:06 +0100283 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100284
Christopher Faulet65554e12020-03-06 14:52:06 +0100285 st = pool_alloc_dirty(pool_head_cache_st);
286 if (st == NULL)
287 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100288
Christopher Faulet65554e12020-03-06 14:52:06 +0100289 st->first_block = NULL;
290 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100291
Christopher Faulet65554e12020-03-06 14:52:06 +0100292 /* Register post-analyzer on AN_RES_WAIT_HTTP */
293 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100294 return 1;
295}
296
Christopher Faulet65554e12020-03-06 14:52:06 +0100297static void
298cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100299{
300 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100301 struct cache_flt_conf *cconf = FLT_CONF(filter);
302 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100303 struct shared_context *shctx = shctx_ptr(cache);
304
William Lallemand49dc0482017-11-24 14:33:54 +0100305 /* Everything should be released in the http_end filter, but we need to do it
306 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100307 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100308 shctx_lock(shctx);
309 shctx_row_dec_hot(shctx, st->first_block);
310 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100311 }
312 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100313 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100314 filter->ctx = NULL;
315 }
William Lallemand49dc0482017-11-24 14:33:54 +0100316}
317
Christopher Faulet839791a2019-01-07 16:12:07 +0100318static int
319cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
320 unsigned an_bit)
321{
322 struct http_txn *txn = s->txn;
323 struct http_msg *msg = &txn->rsp;
324 struct cache_st *st = filter->ctx;
325
326 if (an_bit != AN_RES_WAIT_HTTP)
327 goto end;
328
329 /* Here we need to check if any compression filter precedes the cache
330 * filter. This is only possible when the compression is configured in
331 * the frontend while the cache filter is configured on the
332 * backend. This case cannot be detected during HAProxy startup. So in
333 * such cases, the cache is disabled.
334 */
335 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
336 pool_free(pool_head_cache_st, st);
337 filter->ctx = NULL;
338 }
339
340 end:
341 return 1;
342}
William Lallemand49dc0482017-11-24 14:33:54 +0100343
344static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100345cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
346{
347 struct cache_st *st = filter->ctx;
348
William Lallemand4da3f8a2017-10-31 14:33:34 +0100349 if (!(msg->chn->flags & CF_ISRESP) || !st)
350 return 1;
351
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200352 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100353 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100354 return 1;
355}
356
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200357static inline void disable_cache_entry(struct cache_st *st,
358 struct filter *filter, struct shared_context *shctx)
359{
360 struct cache_entry *object;
361
362 object = (struct cache_entry *)st->first_block->data;
363 filter->ctx = NULL; /* disable cache */
364 shctx_lock(shctx);
365 shctx_row_dec_hot(shctx, st->first_block);
366 object->eb.key = 0;
367 shctx_unlock(shctx);
368 pool_free(pool_head_cache_st, st);
369}
370
William Lallemand4da3f8a2017-10-31 14:33:34 +0100371static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100372cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
373 unsigned int offset, unsigned int len)
374{
Christopher Faulet95220e22018-12-07 17:34:39 +0100375 struct cache_flt_conf *cconf = FLT_CONF(filter);
376 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100377 struct cache_st *st = filter->ctx;
378 struct htx *htx = htxbuf(&msg->chn->buf);
379 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200380 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100381 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200382 unsigned int orig_len, to_forward;
383 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100384
385 if (!len)
386 return len;
387
388 if (!st->first_block) {
389 unregister_data_filter(s, msg->chn, filter);
390 return len;
391 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100392
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200393 chunk_reset(&trash);
394 orig_len = len;
395 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100396
397 htxret = htx_find_offset(htx, offset);
398 blk = htxret.blk;
399 offset = htxret.ret;
400 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100401 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200402 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100403 struct ist v;
404
405 switch (type) {
406 case HTX_BLK_UNUSED:
407 break;
408
409 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100410 v = htx_get_blk_value(htx, blk);
411 v.ptr += offset;
412 v.len -= offset;
413 if (v.len > len)
414 v.len = len;
415
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200416 info = (type << 28) + v.len;
417 chunk_memcat(&trash, (char *)&info, sizeof(info));
418 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100419 to_forward += v.len;
420 len -= v.len;
421 break;
422
423 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200424 /* Here offset must always be 0 because only
425 * DATA blocks can be partially transferred. */
426 if (offset)
427 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100428 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200429 goto end;
430
431 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
432 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100433 to_forward += sz;
434 len -= sz;
435 break;
436 }
437
438 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100439 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200440
441 end:
442 shctx_lock(shctx);
443 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
444 if (!fb) {
445 shctx_unlock(shctx);
446 goto no_cache;
447 }
448 shctx_unlock(shctx);
449
450 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
451 (unsigned char *)b_head(&trash), b_data(&trash));
452 if (ret < 0)
453 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100454
455 return to_forward;
456
457 no_cache:
458 disable_cache_entry(st, filter, shctx);
459 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200460 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100461}
462
463static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100464cache_store_http_end(struct stream *s, struct filter *filter,
465 struct http_msg *msg)
466{
467 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100468 struct cache_flt_conf *cconf = FLT_CONF(filter);
469 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100470 struct shared_context *shctx = shctx_ptr(cache);
471 struct cache_entry *object;
472
473 if (!(msg->chn->flags & CF_ISRESP))
474 return 1;
475
476 if (st && st->first_block) {
477
478 object = (struct cache_entry *)st->first_block->data;
479
480 /* does not need to test if the insertion worked, if it
481 * doesn't, the blocks will be reused anyway */
482
483 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100484 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
485 object->eb.key = 0;
486 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100487 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100488 shctx_row_dec_hot(shctx, st->first_block);
489 shctx_unlock(shctx);
490
491 }
492 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100493 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100494 filter->ctx = NULL;
495 }
496
497 return 1;
498}
499
500 /*
501 * This intends to be used when checking HTTP headers for some
502 * word=value directive. Return a pointer to the first character of value, if
503 * the word was not found or if there wasn't any value assigned ot it return NULL
504 */
505char *directive_value(const char *sample, int slen, const char *word, int wlen)
506{
507 int st = 0;
508
509 if (slen < wlen)
510 return 0;
511
512 while (wlen) {
513 char c = *sample ^ *word;
514 if (c && c != ('A' ^ 'a'))
515 return NULL;
516 sample++;
517 word++;
518 slen--;
519 wlen--;
520 }
521
522 while (slen) {
523 if (st == 0) {
524 if (*sample != '=')
525 return NULL;
526 sample++;
527 slen--;
528 st = 1;
529 continue;
530 } else {
531 return (char *)sample;
532 }
533 }
534
535 return NULL;
536}
537
538/*
539 * Return the maxage in seconds of an HTTP response.
540 * Compute the maxage using either:
541 * - the assigned max-age of the cache
542 * - the s-maxage directive
543 * - the max-age directive
544 * - (Expires - Data) headers
545 * - the default-max-age of the cache
546 *
547 */
William Lallemand49b44532017-11-24 18:53:43 +0100548int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100549{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200550 struct htx *htx = htxbuf(&s->res.buf);
551 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100552 int smaxage = -1;
553 int maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100554 int expires = -1;
555 struct tm tm = {};
556 time_t expires_val = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100557
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200558 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
559 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100560
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200561 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
562 if (value) {
563 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100564
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200565 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
566 chunk_strncat(chk, "", 1);
Remi Tricot-Le Breton8c2db712020-10-30 14:26:13 +0100567 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100568 }
569
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200570 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
571 if (value) {
572 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200573
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200574 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
575 chunk_strncat(chk, "", 1);
Remi Tricot-Le Breton8c2db712020-10-30 14:26:13 +0100576 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100577 }
578 }
579
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100580 /* Look for Expires header if no s-maxage or max-age Cache-Control data
581 * was found. */
582 if (maxage == -1 && smaxage == -1) {
583 ctx.blk = NULL;
584 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
585 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
586 expires_val = my_timegm(&tm);
587 /* A request having an expiring date earlier
588 * than the current date should be considered as
589 * stale. */
590 expires = (expires_val >= now.tv_sec) ?
591 (expires_val - now.tv_sec) : 0;
592 }
593 else {
594 /* Following RFC 7234#5.3, an invalid date
595 * format must be treated as a date in the past
596 * so the cache entry must be seen as already
597 * expired. */
598 expires = 0;
599 }
600 }
601 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100602
603
604 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100605 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100606
607 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100608 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100609
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100610 if (expires >= 0)
611 return MIN(expires, cache->maxage);
612
William Lallemand49b44532017-11-24 18:53:43 +0100613 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100614
615}
616
617
William Lallemanda400a3a2017-11-20 19:13:12 +0100618static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
619{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200620 struct cache_entry *object = (struct cache_entry *)block->data;
621
622 if (first == block && object->eb.key)
623 eb32_delete(&object->eb);
624 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100625}
626
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200627
628/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
629 * date value should be compared to a date determined by in a previous response (for
630 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
631 * value of the response's reception time (by decreasing order of priority). */
632static time_t get_last_modified_time(struct htx *htx)
633{
634 time_t last_modified = 0;
635 struct http_hdr_ctx ctx = { .blk = NULL };
636 struct tm tm = {};
637
638 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
639 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
640 last_modified = my_timegm(&tm);
641 }
642 }
643
644 if (!last_modified) {
645 ctx.blk = NULL;
646 if (http_find_header(htx, ist("date"), &ctx, 1)) {
647 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
648 last_modified = my_timegm(&tm);
649 }
650 }
651 }
652
653 /* Fallback on the current time if no "Last-Modified" or "Date" header
654 * was found. */
655 if (!last_modified)
656 last_modified = now.tv_sec;
657
658 return last_modified;
659}
660
William Lallemand41db4602017-10-30 11:15:51 +0100661/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100662 * Checks the vary header's value. The headers on which vary should be applied
663 * must be explicitely supported in the vary_information array (see cache.c). If
664 * any other header is mentioned, we won't store the response.
665 * Returns 1 if Vary-based storage can work, 0 otherwise.
666 */
667static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
668{
669 unsigned int vary_idx;
670 unsigned int vary_info_count;
671 const struct vary_hashing_information *vary_info;
672 struct http_hdr_ctx ctx = { .blk = NULL };
673
674 int retval = 1;
675
676 *vary_signature = 0;
677
678 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
679 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
680 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
681 vary_info = &vary_information[vary_idx];
682 if (isteqi(ctx.value, vary_info->hdr_name)) {
683 *vary_signature |= vary_info->value;
684 break;
685 }
686 }
687 retval = (vary_idx < vary_info_count);
688 }
689
690 return retval;
691}
692
693
694
695/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500696 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100697 * register a filter to store the data
698 */
699enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200700 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100701{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200702 unsigned int age;
703 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100704 struct http_txn *txn = s->txn;
705 struct http_msg *msg = &txn->rsp;
706 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100707 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100708 struct cache_flt_conf *cconf = rule->arg.act.p[0];
709 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100710 struct cache_st *cache_ctx = NULL;
711 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100712 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200713 struct htx *htx;
714 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200715 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200716 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200717 unsigned int etag_length = 0;
718 unsigned int etag_offset = 0;
719 struct ist header_name = IST_NULL;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200720 time_t last_modified = 0;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100721 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100722
William Lallemand4da3f8a2017-10-31 14:33:34 +0100723 /* Don't cache if the response came from a cache */
724 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
725 s->target == &http_cache_applet.obj_type) {
726 goto out;
727 }
728
729 /* cache only HTTP/1.1 */
730 if (!(txn->req.flags & HTTP_MSGF_VER_11))
731 goto out;
732
Willy Tarreau6905d182019-10-01 17:59:17 +0200733 /* cache only GET method */
734 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100735 goto out;
736
Willy Tarreauc9036c02019-01-11 19:38:25 +0100737 /* cache key was not computed */
738 if (!key)
739 goto out;
740
William Lallemand4da3f8a2017-10-31 14:33:34 +0100741 /* cache only 200 status code */
742 if (txn->status != 200)
743 goto out;
744
Christopher Faulet839791a2019-01-07 16:12:07 +0100745 /* Find the corresponding filter instance for the current stream */
746 list_for_each_entry(filter, &s->strm_flt.filters, list) {
747 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
748 /* No filter ctx, don't cache anything */
749 if (!filter->ctx)
750 goto out;
751 cache_ctx = filter->ctx;
752 break;
753 }
754 }
755
756 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200757 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100758
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200759 /* Do not cache too big objects. */
760 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
761 htx->data + htx->extra > shctx->max_obj_size)
762 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100763
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100764 /* Only a subset of headers are supported in our Vary implementation. If
765 * any other header is present in the Vary header value, we won't be
766 * able to use the cache. */
767 if (!http_check_vary_header(htx, &vary_signature)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200768 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100769 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100770
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200771 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100772
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100773 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200774 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100775
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200776 age = 0;
777 ctx.blk = NULL;
778 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
779 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
780 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
781 hdr_age = CACHE_ENTRY_MAX_AGE;
782 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100783 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200784 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100785 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100786
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200787 /* Build a last-modified time that will be stored in the cache_entry and
788 * compared to a future If-Modified-Since client header. */
789 last_modified = get_last_modified_time(htx);
790
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200791 chunk_reset(&trash);
792 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
793 struct htx_blk *blk = htx_get_blk(htx, pos);
794 enum htx_blk_type type = htx_get_blk_type(blk);
795 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100796
Christopher Fauletb0667472019-09-03 22:22:12 +0200797 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200798 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
799 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200800
801 /* Look for optional ETag header.
802 * We need to store the offset of the ETag value in order for
803 * future conditional requests to be able to perform ETag
804 * comparisons. */
805 if (type == HTX_BLK_HDR) {
806 header_name = htx_get_blk_name(htx, blk);
807 if (isteq(header_name, ist("etag"))) {
808 etag_length = sz - istlen(header_name);
809 etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
810 }
811 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200812 if (type == HTX_BLK_EOH)
813 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200814 }
815
Christopher Fauletb0667472019-09-03 22:22:12 +0200816 /* Do not cache objects if the headers are too big. */
817 if (hdrs_len > htx->size - global.tune.maxrewrite)
818 goto out;
819
William Lallemand4da3f8a2017-10-31 14:33:34 +0100820 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200821 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100822 if (!first) {
823 shctx_unlock(shctx);
824 goto out;
825 }
826 shctx_unlock(shctx);
827
Willy Tarreau1093a452018-04-06 19:02:25 +0200828 /* the received memory is not initialized, we need at least to mark
829 * the object as not indexed yet.
830 */
831 object = (struct cache_entry *)first->data;
832 object->eb.node.leaf_p = NULL;
833 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200834 object->age = age;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200835 object->last_modified = last_modified;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100836 object->secondary_key_signature = vary_signature;
Willy Tarreau1093a452018-04-06 19:02:25 +0200837
William Lallemand4da3f8a2017-10-31 14:33:34 +0100838 /* reserve space for the cache_entry structure */
839 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200840 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100841 /* cache the headers in a http action because it allows to chose what
842 * to cache, for example you might want to cache a response before
843 * modifying some HTTP headers, or on the contrary after modifying
844 * those headers.
845 */
846
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200847 /* Write the ETag information in the cache_entry if needed. */
848 object->etag_length = etag_length;
849 object->etag_offset = etag_offset;
850
William Lallemand4da3f8a2017-10-31 14:33:34 +0100851 /* does not need to be locked because it's in the "hot" list,
852 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200853 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
854 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100855
856 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100857 if (cache_ctx) {
858 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100859
Willy Tarreauc9036c02019-01-11 19:38:25 +0100860 object->eb.key = key;
861
Christopher Faulet839791a2019-01-07 16:12:07 +0100862 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100863
864 /* Add the current request's secondary key to the buffer if needed. */
865 if (vary_signature) {
866 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
867 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
868 }
869
Christopher Faulet839791a2019-01-07 16:12:07 +0100870 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100871
Christopher Faulet839791a2019-01-07 16:12:07 +0100872 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100873
Christopher Faulet839791a2019-01-07 16:12:07 +0100874 old = entry_exist(cconf->c.cache, txn->cache_hash);
875 if (old) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100876 if (vary_signature)
877 old = secondary_entry_exist(cconf->c.cache, old,
878 txn->cache_secondary_hash);
879
880 if (old) {
881 eb32_delete(&old->eb);
882 old->eb.key = 0;
883 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100884 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100885 shctx_unlock(shctx);
886
887 /* store latest value and expiration time */
888 object->latest_validation = now.tv_sec;
889 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
890 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100891 }
892
893out:
894 /* if does not cache */
895 if (first) {
896 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100897 first->len = 0;
898 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100899 shctx_row_dec_hot(shctx, first);
900 shctx_unlock(shctx);
901 }
902
William Lallemand41db4602017-10-30 11:15:51 +0100903 return ACT_RET_CONT;
904}
905
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100906#define HTX_CACHE_INIT 0 /* Initial state. */
907#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
908#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200909#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
910#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100911
William Lallemandecb73b12017-11-24 14:33:55 +0100912static void http_cache_applet_release(struct appctx *appctx)
913{
Christopher Faulet95220e22018-12-07 17:34:39 +0100914 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100915 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100916 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100917 struct shared_block *first = block_ptr(cache_ptr);
918
919 shctx_lock(shctx_ptr(cache));
920 shctx_row_dec_hot(shctx_ptr(cache), first);
921 shctx_unlock(shctx_ptr(cache));
922}
923
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200924
925static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
926 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100927{
Christopher Faulet95220e22018-12-07 17:34:39 +0100928 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
929 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200930 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200931 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200932 unsigned int max, total;
933 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100934
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200935 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
936 if (!max)
937 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200938 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200939 ? (info & 0xff) + ((info >> 8) & 0xfffff)
940 : info & 0xfffffff);
941 if (blksz > max)
942 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100943
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200944 blk = htx_add_blk(htx, type, blksz);
945 if (!blk)
946 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100947
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200948 blk->info = info;
949 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200950 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200951 while (blksz) {
952 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200953 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200954 offset += max;
955 blksz -= max;
956 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200957 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200958 if (blksz || offset == shctx->block_size) {
959 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
960 offset = 0;
961 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100962 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200963 appctx->ctx.cache.offset = offset;
964 appctx->ctx.cache.next = shblk;
965 appctx->ctx.cache.sent += total;
966 return total;
967}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100968
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200969static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
970 uint32_t info, struct shared_block *shblk, unsigned int offset)
971{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100972
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200973 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
974 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
975 unsigned int max, total, rem_data;
976 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100977
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200978 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
979 if (!max)
980 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100981
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200982 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200983 if (appctx->ctx.cache.rem_data) {
984 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200985 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200986 }
987 else {
988 blksz = (info & 0xfffffff);
989 total = 4;
990 }
991 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200992 rem_data = blksz - max;
993 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100994 }
995
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200996 while (blksz) {
997 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100998
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200999 max = MIN(blksz, shctx->block_size - offset);
1000 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1001 offset += sz;
1002 blksz -= sz;
1003 total += sz;
1004 if (sz < max)
1005 break;
1006 if (blksz || offset == shctx->block_size) {
1007 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1008 offset = 0;
1009 }
1010 }
1011
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001012 appctx->ctx.cache.offset = offset;
1013 appctx->ctx.cache.next = shblk;
1014 appctx->ctx.cache.sent += total;
1015 appctx->ctx.cache.rem_data = rem_data + blksz;
1016 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001017}
1018
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001019static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1020 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001021{
Christopher Faulet95220e22018-12-07 17:34:39 +01001022 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1023 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001024 struct shared_block *shblk;
1025 unsigned int offset, sz;
1026 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001027
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001028 while (len) {
1029 enum htx_blk_type type;
1030 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001031
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001032 shblk = appctx->ctx.cache.next;
1033 offset = appctx->ctx.cache.offset;
1034 if (appctx->ctx.cache.rem_data) {
1035 type = HTX_BLK_DATA;
1036 info = 0;
1037 goto add_data_blk;
1038 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001039
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001040 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001041 sz = MIN(4, shctx->block_size - offset);
1042 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1043 offset += sz;
1044 if (sz < 4) {
1045 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1046 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1047 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001048 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001049
1050 /* Get payload of the next HTX block and insert it. */
1051 type = (info >> 28);
1052 if (type != HTX_BLK_DATA)
1053 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1054 else {
1055 add_data_blk:
1056 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001057 }
1058
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001059 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001060 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001061 total += ret;
1062 len -= ret;
1063
1064 if (appctx->ctx.cache.rem_data || type == mark)
1065 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001066 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001067
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001068 return total;
1069}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001070
1071static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1072{
1073 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1074 unsigned int age;
1075 char *end;
1076
1077 chunk_reset(&trash);
1078 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1079 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1080 age = CACHE_ENTRY_MAX_AGE;
1081 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1082 b_set_data(&trash, end - b_head(&trash));
1083 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1084 return 0;
1085 return 1;
1086}
1087
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001088static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001089{
1090 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1091 struct shared_block *first = block_ptr(cache_ptr);
1092 struct stream_interface *si = appctx->owner;
1093 struct channel *req = si_oc(si);
1094 struct channel *res = si_ic(si);
1095 struct htx *req_htx, *res_htx;
1096 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001097 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001098 size_t ret, total = 0;
1099
1100 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001101 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001102
1103 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1104 goto out;
1105
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001106 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001107 if (!b_size(&res->buf)) {
1108 si_rx_room_blk(si);
1109 goto out;
1110 }
1111
Willy Tarreauefef3232018-12-16 00:37:45 +01001112 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001113 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001114
1115 if (appctx->st0 == HTX_CACHE_INIT) {
1116 appctx->ctx.cache.next = block_ptr(cache_ptr);
1117 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1118 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001119 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001120 appctx->st0 = HTX_CACHE_HEADER;
1121 }
1122
1123 if (appctx->st0 == HTX_CACHE_HEADER) {
1124 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001125 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1126 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1127 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1128 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001129 goto error;
1130
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001131 /* In case of a conditional request, we might want to send a
1132 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001133 if (appctx->ctx.cache.send_notmodified) {
1134 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1135 /* If replacing the status code fails we need to send the full response. */
1136 appctx->ctx.cache.send_notmodified = 0;
1137 }
1138 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001139
1140 /* Skip response body for HEAD requests or in case of "304 Not
1141 * Modified" response. */
1142 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001143 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001144 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001145 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001146 }
1147
1148 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001149 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1150 if (len) {
1151 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1152 if (ret < len) {
1153 si_rx_room_blk(si);
1154 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001155 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001156 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001157 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001158 }
1159
1160 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001161 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001162 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1163 si_rx_room_blk(si);
1164 goto out;
1165 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001166 appctx->st0 = HTX_CACHE_END;
1167 }
1168
1169 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001170 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001171 res->flags |= CF_READ_NULL;
1172 si_shutr(si);
1173 }
1174
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001175 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001176 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001177 if (total)
1178 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001179 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001180
1181 /* eat the whole request */
1182 if (co_data(req)) {
1183 req_htx = htx_from_buf(&req->buf);
1184 co_htx_skip(req, req_htx, co_data(req));
1185 htx_to_buf(req_htx, &req->buf);
1186 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001187 return;
1188
1189 error:
1190 /* Sent and HTTP error 500 */
1191 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001192 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001193 res->buf.data = b_data(errmsg);
1194 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1195 res_htx = htx_from_buf(&res->buf);
1196
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001197 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001198 appctx->st0 = HTX_CACHE_END;
1199 goto end;
1200}
1201
1202
Christopher Faulet95220e22018-12-07 17:34:39 +01001203static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001204{
1205 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001206 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001207
Christopher Faulet95220e22018-12-07 17:34:39 +01001208 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001209 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001210 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001211 }
1212
1213 /* check if a cache filter was already registered with this cache
1214 * name, if that's the case, must use it. */
1215 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001216 if (fconf->id == cache_store_flt_id) {
1217 cconf = fconf->conf;
1218 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1219 rule->arg.act.p[0] = cconf;
1220 return 1;
1221 }
William Lallemand41db4602017-10-30 11:15:51 +01001222 }
1223 }
1224
Christopher Faulet95220e22018-12-07 17:34:39 +01001225 /* Create the filter cache config */
1226 cconf = calloc(1, sizeof(*cconf));
1227 if (!cconf) {
1228 memprintf(err, "out of memory\n");
1229 goto err;
1230 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001231 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001232 cconf->c.name = strdup(name);
1233 if (!cconf->c.name) {
1234 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001235 goto err;
1236 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001237
William Lallemand41db4602017-10-30 11:15:51 +01001238 /* register a filter to fill the cache buffer */
1239 fconf = calloc(1, sizeof(*fconf));
1240 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001241 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001242 goto err;
1243 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001244 fconf->id = cache_store_flt_id;
1245 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001246 fconf->ops = &cache_ops;
1247 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1248
Christopher Faulet95220e22018-12-07 17:34:39 +01001249 rule->arg.act.p[0] = cconf;
1250 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001251
Christopher Faulet95220e22018-12-07 17:34:39 +01001252 err:
1253 free(cconf);
1254 return 0;
1255}
1256
1257enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1258 struct act_rule *rule, char **err)
1259{
1260 rule->action = ACT_CUSTOM;
1261 rule->action_ptr = http_action_store_cache;
1262
1263 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1264 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001265
Christopher Faulet95220e22018-12-07 17:34:39 +01001266 (*orig_arg)++;
1267 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001268}
1269
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001270/* This produces a sha1 hash of the concatenation of the HTTP method,
1271 * the first occurrence of the Host header followed by the path component
1272 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001273int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001274{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001275 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001276 struct htx *htx = htxbuf(&s->req.buf);
1277 struct htx_sl *sl;
1278 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001279 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001280 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001281 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001282
William Lallemandf528fff2017-11-23 19:43:17 +01001283 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001284 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001285
1286 switch (txn->meth) {
1287 case HTTP_METH_HEAD:
1288 case HTTP_METH_GET:
1289 chunk_memcat(trash, "GET", 3);
1290 break;
1291 default:
1292 return 0;
1293 }
1294
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001295 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001296 uri = htx_sl_req_uri(sl); // whole uri
1297 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001298 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001299
1300 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1301 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1302 * URIs are almost always sent in absolute form with their scheme. In
1303 * this case, the scheme is almost always "https". In order to support
1304 * sharing of cache objects between H1 and H2, we'll hash the absolute
1305 * URI whenever known, or prepend "https://" + the Host header for
1306 * relative URIs. The difference will only appear on absolute HTTP/1
1307 * requests sent to an origin server, which practically is never met in
1308 * the real world so we don't care about the ability to share the same
1309 * key here.URIs are normalized from the absolute URI to an origin form as
1310 * well.
1311 */
1312 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001313 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001314 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1315 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001316 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001317 }
1318
1319 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001320
1321 /* hash everything */
1322 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001323 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001324 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1325
1326 return 1;
1327}
1328
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001329/* Looks for "If-None-Match" headers in the request and compares their value
1330 * with the one that might have been stored in the cache_entry. If any of them
1331 * matches, a "304 Not Modified" response should be sent instead of the cached
1332 * data.
1333 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001334 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1335 *
1336 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1337 * header and compare its value (date) to the one stored in the cache_entry.
1338 * If the request's date is later than the cached one, we also send a
1339 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1340 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001341 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1342 */
1343static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1344 struct cache_entry *entry)
1345{
1346 int retval = 0;
1347
1348 struct http_hdr_ctx ctx = { .blk = NULL };
1349 struct ist cache_entry_etag = IST_NULL;
1350 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001351 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001352
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001353 struct tm tm = {};
1354 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001355
1356 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001357 * cache_entry's ETag in order to perform comparisons.
1358 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001359 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001360 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001361
1362 /* A '*' matches everything. */
1363 if (isteq(ctx.value, ist("*")) != 0) {
1364 retval = 1;
1365 break;
1366 }
1367
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001368 /* No need to rebuild an etag if none was stored in the cache. */
1369 if (entry->etag_length == 0)
1370 break;
1371
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001372 /* Rebuild the stored ETag. */
1373 if (etag_buffer == NULL) {
1374 etag_buffer = get_trash_chunk();
1375
1376 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1377 (unsigned char*)b_orig(etag_buffer),
1378 entry->etag_offset, entry->etag_length) == 0) {
1379 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1380 } else {
1381 /* We could not rebuild the ETag in one go, we
1382 * won't send a "304 Not Modified" response. */
1383 break;
1384 }
1385 }
1386
1387 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1388 retval = 1;
1389 break;
1390 }
1391 }
1392
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001393 /* If the request did not contain an "If-None-Match" header, we look for
1394 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1395 if (retval == 0 && if_none_match_found == 0) {
1396 ctx.blk = NULL;
1397 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1398 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1399 if_modified_since = my_timegm(&tm);
1400
1401 /* We send a "304 Not Modified" response if the
1402 * entry's last modified date is earlier than
1403 * the one found in the "If-Modified-Since"
1404 * header. */
1405 retval = (entry->last_modified <= if_modified_since);
1406 }
1407 }
1408 }
1409
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001410 return retval;
1411}
1412
William Lallemand41db4602017-10-30 11:15:51 +01001413enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1414 struct session *sess, struct stream *s, int flags)
1415{
William Lallemand77c11972017-10-31 20:43:01 +01001416
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001417 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001418 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001419 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1420 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001421 struct shared_block *entry_block;
1422
William Lallemand77c11972017-10-31 20:43:01 +01001423
Willy Tarreau6905d182019-10-01 17:59:17 +02001424 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1425 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001426 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001427 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001428 txn->flags |= TX_CACHE_IGNORE;
1429
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001430 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001431
Willy Tarreau504455c2017-12-22 17:47:35 +01001432 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1433 return ACT_RET_CONT;
1434
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001435 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001436 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001437
Willy Tarreau504455c2017-12-22 17:47:35 +01001438 if (s->txn->flags & TX_CACHE_IGNORE)
1439 return ACT_RET_CONT;
1440
Willy Tarreaua1214a52018-12-14 14:00:25 +01001441 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001442 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001443 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001444 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001445
William Lallemanda400a3a2017-11-20 19:13:12 +01001446 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001447 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001448 if (res) {
1449 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001450 entry_block = block_ptr(res);
1451 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001452 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001453
1454 /* In case of Vary, we could have multiple entries with the same
1455 * primary hash. We need to calculate the secondary has in order
1456 * to find the actual entry we want (if it exists). */
1457 if (res->secondary_key_signature) {
1458 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1459 shctx_lock(shctx_ptr(cache));
1460 sec_entry = secondary_entry_exist(cache, res,
1461 s->txn->cache_secondary_hash);
1462 if (sec_entry && sec_entry != res) {
1463 /* The wrong row was added to the hot list. */
1464 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1465 entry_block = block_ptr(sec_entry);
1466 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1467 }
1468 res = sec_entry;
1469 shctx_unlock(shctx_ptr(cache));
1470 }
1471 else
1472 res = NULL;
1473 }
1474
1475 /* We looked for a valid secondary entry and could not find one,
1476 * the request must be forwarded to the server. */
1477 if (!res) {
1478 shctx_lock(shctx_ptr(cache));
1479 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1480 shctx_unlock(shctx_ptr(cache));
1481 return ACT_RET_CONT;
1482 }
1483
William Lallemand77c11972017-10-31 20:43:01 +01001484 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001485 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001486 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001487 appctx->rule = rule;
1488 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001489 appctx->ctx.cache.next = NULL;
1490 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001491 appctx->ctx.cache.send_notmodified =
1492 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001493
1494 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001495 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001496 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001497 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001498 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001499 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001500 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001501 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001502 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001503 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001504 }
1505 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001506 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001507
1508 /* Shared context does not need to be locked while we calculate the
1509 * secondary hash. */
1510 if (!res) {
1511 /* Build a complete secondary hash until the server response
1512 * tells us which fields should be kept (if any). */
1513 http_request_prebuild_full_secondary_key(s);
1514 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001515 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001516}
1517
1518
1519enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1520 struct act_rule *rule, char **err)
1521{
William Lallemand41db4602017-10-30 11:15:51 +01001522 rule->action = ACT_CUSTOM;
1523 rule->action_ptr = http_action_req_cache_use;
1524
Christopher Faulet95220e22018-12-07 17:34:39 +01001525 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001526 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001527
1528 (*orig_arg)++;
1529 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001530}
1531
1532int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1533{
1534 int err_code = 0;
1535
1536 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1537
1538 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001539 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001540 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001541 err_code |= ERR_ALERT | ERR_ABORT;
1542 goto out;
1543 }
1544
1545 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1546 err_code |= ERR_ABORT;
1547 goto out;
1548 }
1549
1550 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001551 struct cache *cache_config;
1552
William Lallemand41db4602017-10-30 11:15:51 +01001553 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1554 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001555 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001556 err_code |= ERR_ALERT | ERR_ABORT;
1557 goto out;
1558 }
1559
1560 strlcpy2(tmp_cache_config->id, args[1], 33);
1561 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001562 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001563 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001564 err_code |= ERR_WARN;
1565 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001566
1567 list_for_each_entry(cache_config, &caches_config, list) {
1568 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1569 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1570 file, linenum, tmp_cache_config->id);
1571 err_code |= ERR_ALERT | ERR_ABORT;
1572 goto out;
1573 }
1574 }
1575
William Lallemand49b44532017-11-24 18:53:43 +01001576 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001577 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001578 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001579 }
1580 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001581 unsigned long int maxsize;
1582 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001583
1584 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1585 err_code |= ERR_ABORT;
1586 goto out;
1587 }
1588
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001589 maxsize = strtoul(args[1], &err, 10);
1590 if (err == args[1] || *err != '\0') {
1591 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1592 file, linenum, args[1]);
1593 err_code |= ERR_ABORT;
1594 goto out;
1595 }
1596
1597 if (maxsize > (UINT_MAX >> 20)) {
1598 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1599 file, linenum, args[1], UINT_MAX >> 20);
1600 err_code |= ERR_ABORT;
1601 goto out;
1602 }
1603
William Lallemand41db4602017-10-30 11:15:51 +01001604 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001605 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001606 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001607 } else if (strcmp(args[0], "max-age") == 0) {
1608 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1609 err_code |= ERR_ABORT;
1610 goto out;
1611 }
1612
1613 if (!*args[1]) {
1614 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1615 file, linenum, args[0]);
1616 err_code |= ERR_WARN;
1617 }
1618
1619 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001620 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001621 unsigned int maxobjsz;
1622 char *err;
1623
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001624 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1625 err_code |= ERR_ABORT;
1626 goto out;
1627 }
1628
1629 if (!*args[1]) {
1630 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1631 file, linenum, args[0]);
1632 err_code |= ERR_WARN;
1633 }
1634
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001635 maxobjsz = strtoul(args[1], &err, 10);
1636 if (err == args[1] || *err != '\0') {
1637 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1638 file, linenum, args[1]);
1639 err_code |= ERR_ABORT;
1640 goto out;
1641 }
1642 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001643 }
1644 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001645 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001646 err_code |= ERR_ALERT | ERR_FATAL;
1647 goto out;
1648 }
1649out:
1650 return err_code;
1651}
1652
1653/* once the cache section is parsed */
1654
1655int cfg_post_parse_section_cache()
1656{
William Lallemand41db4602017-10-30 11:15:51 +01001657 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001658
1659 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001660
1661 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001662 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001663 err_code |= ERR_FATAL | ERR_ALERT;
1664 goto out;
1665 }
1666
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001667 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001668 /* Default max. file size is a 256th of the cache size. */
1669 tmp_cache_config->maxobjsz =
1670 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001671 }
1672 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1673 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1674 err_code |= ERR_FATAL | ERR_ALERT;
1675 goto out;
1676 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001677
William Lallemandd1d1e222019-08-28 15:22:49 +02001678 /* add to the list of cache to init and reinit tmp_cache_config
1679 * for next cache section, if any.
1680 */
1681 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1682 tmp_cache_config = NULL;
1683 return err_code;
1684 }
1685out:
1686 free(tmp_cache_config);
1687 tmp_cache_config = NULL;
1688 return err_code;
1689
1690}
1691
1692int post_check_cache()
1693{
1694 struct proxy *px;
1695 struct cache *back, *cache_config, *cache;
1696 struct shared_context *shctx;
1697 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001698 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02001699
1700 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1701
1702 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1703 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001704
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001705 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001706 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001707 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001708 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001709 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001710
1711 err_code |= ERR_FATAL | ERR_ALERT;
1712 goto out;
1713 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001714 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001715 /* the cache structure is stored in the shctx and added to the
1716 * caches list, we can remove the entry from the caches_config
1717 * list */
1718 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001719 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001720 cache->entries = EB_ROOT;
William Lallemand41db4602017-10-30 11:15:51 +01001721 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001722 LIST_DEL(&cache_config->list);
1723 free(cache_config);
1724
1725 /* Find all references for this cache in the existing filters
1726 * (over all proxies) and reference it in matching filters.
1727 */
1728 for (px = proxies_list; px; px = px->next) {
1729 struct flt_conf *fconf;
1730 struct cache_flt_conf *cconf;
1731
1732 list_for_each_entry(fconf, &px->filter_configs, list) {
1733 if (fconf->id != cache_store_flt_id)
1734 continue;
1735
1736 cconf = fconf->conf;
1737 if (!strcmp(cache->id, cconf->c.name)) {
1738 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001739 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001740 cconf->c.cache = cache;
1741 break;
1742 }
1743 }
1744 }
William Lallemand41db4602017-10-30 11:15:51 +01001745 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001746
William Lallemand41db4602017-10-30 11:15:51 +01001747out:
William Lallemand41db4602017-10-30 11:15:51 +01001748 return err_code;
1749
William Lallemand41db4602017-10-30 11:15:51 +01001750}
1751
William Lallemand41db4602017-10-30 11:15:51 +01001752struct flt_ops cache_ops = {
1753 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001754 .check = cache_store_check,
1755 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001756
Christopher Faulet65554e12020-03-06 14:52:06 +01001757 /* Handle stream init/deinit */
1758 .attach = cache_store_strm_init,
1759 .detach = cache_store_strm_deinit,
1760
William Lallemand4da3f8a2017-10-31 14:33:34 +01001761 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001762 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001763
1764 /* Filter HTTP requests and responses */
1765 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001766 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001767 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001768};
1769
Christopher Faulet99a17a22018-12-11 09:18:27 +01001770
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001771int accept_encoding_cmp(const void *a, const void *b)
1772{
1773 const struct ist ist_a = *(const struct ist*)a;
1774 const struct ist ist_b = *(const struct ist*)b;
1775
1776 return istdiff(ist_a, ist_b);
1777}
1778
1779/*
1780 * Build a hash of the accept-encoding header. The different parts of the
1781 * header value are first sorted, appended and then a crc is calculated
1782 * for the newly constructed buffer.
1783 * Returns 0 in case of success.
1784 */
1785static int accept_encoding_normalizer(struct ist value, char *buf, unsigned int *buf_len)
1786{
1787 int retval = 0;
1788 struct ist values[16] = {{}};
1789 unsigned int count = 0;
1790 char *comma = NULL;
1791 struct buffer *trash = get_trash_chunk();
1792 int hash_value = 0;
1793
1794 /* The hash will be built out of a sorted list of accepted encodings. */
1795 while((comma = istchr(value, ',')) != NULL) {
1796 values[count++] = ist2(istptr(value), comma-istptr(value));
1797 value = ist2(comma+1, istlen(value) - (comma-istptr(value)) - 1);
1798 }
1799 values[count++] = value;
1800
1801 /* Sort the values alphabetically. */
1802 qsort(values, count, sizeof(struct ist), &accept_encoding_cmp);
1803
1804 while (count)
1805 chunk_istcat(trash, values[--count]);
1806
1807 hash_value = hash_crc32(b_orig(trash), b_data(trash));
1808
1809 memcpy(buf, &hash_value, sizeof(hash_value));
1810 *buf_len = sizeof(hash_value);
1811
1812 return retval;
1813}
1814
1815/*
1816 * Normalizer used by default for User-Agent and Referer headers. It only
1817 * calculates a simple crc of the whole value.
1818 * Returns 0 in case of success.
1819 */
1820static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len)
1821{
1822 int hash_value = 0;
1823
1824 hash_value = hash_crc32(istptr(value), istlen(value));
1825
1826 memcpy(buf, &hash_value, sizeof(hash_value));
1827 *buf_len = sizeof(hash_value);
1828
1829 return 0;
1830}
1831
1832
1833/*
1834 * Pre-calculate the hashes of all the supported headers (in our Vary
1835 * implementation) of a given request. We have to calculate all the hashes
1836 * in advance because the actual Vary signature won't be known until the first
1837 * response.
1838 * Only the first occurrence of every header will be taken into account in the
1839 * hash.
1840 * If the header is not present, the hash portion of the given header will be
1841 * filled with zeros.
1842 * Returns 0 in case of success.
1843 */
1844static int http_request_prebuild_full_secondary_key(struct stream *s)
1845{
1846 struct http_txn *txn = s->txn;
1847 struct htx *htx = htxbuf(&s->req.buf);
1848 struct http_hdr_ctx ctx = { .blk = NULL };
1849
1850 unsigned int idx;
1851 const struct vary_hashing_information *info = NULL;
1852 unsigned int hash_length = 0;
1853 int retval = 0;
1854 int offset = 0;
1855
1856 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1857 info = &vary_information[idx];
1858
1859 ctx.blk = NULL;
1860 if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) {
1861 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1862 offset += hash_length;
1863 }
1864 else {
1865 /* Fill hash with 0s. */
1866 hash_length = info->hash_length;
1867 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1868 offset += hash_length;
1869 }
1870 }
1871
1872 return retval;
1873}
1874
1875
1876/*
1877 * Calculate the secondary key for a request for which we already have a known
1878 * vary signature. The key is made by aggregating hashes calculated for every
1879 * header mentioned in the vary signature.
1880 * Only the first occurrence of every header will be taken into account in the
1881 * hash.
1882 * If the header is not present, the hash portion of the given header will be
1883 * filled with zeros.
1884 * Returns 0 in case of success.
1885 */
1886static int http_request_build_secondary_key(struct stream *s, int vary_signature)
1887{
1888 struct http_txn *txn = s->txn;
1889 struct htx *htx = htxbuf(&s->req.buf);
1890 struct http_hdr_ctx ctx = { .blk = NULL };
1891
1892 unsigned int idx;
1893 const struct vary_hashing_information *info = NULL;
1894 unsigned int hash_length = 0;
1895 int retval = 0;
1896 int offset = 0;
1897
1898 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1899 info = &vary_information[idx];
1900
1901 ctx.blk = NULL;
1902 if ((vary_signature & info->value) && info->norm_fn != NULL &&
1903 http_find_header(htx, info->hdr_name, &ctx, 1)) {
1904 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1905 offset += hash_length;
1906 }
1907 else {
1908 /* Fill hash with 0s. */
1909 hash_length = info->hash_length;
1910 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1911 offset += hash_length;
1912 }
1913 }
1914
1915 return retval;
1916}
1917
1918/*
1919 * Build the actual secondary key of a given request out of the prebuilt key and
1920 * the actual vary signature (extracted from the response).
1921 * Returns 0 in case of success.
1922 */
1923static int http_request_reduce_secondary_key(unsigned int vary_signature,
1924 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
1925{
1926 int offset = 0;
1927 int global_offset = 0;
1928 int vary_info_count = 0;
1929 int keep = 0;
1930 unsigned int vary_idx;
1931 const struct vary_hashing_information *vary_info;
1932
1933 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
1934 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
1935 vary_info = &vary_information[vary_idx];
1936 keep = (vary_signature & vary_info->value) ? 0xff : 0;
1937
1938 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
1939 prebuilt_key[global_offset] &= keep;
1940 }
1941 }
1942
1943 return 0;
1944}
1945
1946
Christopher Faulet99a17a22018-12-11 09:18:27 +01001947
1948static int
1949parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1950 struct flt_conf *fconf, char **err, void *private)
1951{
1952 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001953 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001954 char *name = NULL;
1955 int pos = *cur_arg;
1956
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001957 /* Get the cache filter name. <pos> point on "cache" keyword */
1958 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02001959 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001960 goto error;
1961 }
1962 name = strdup(args[pos + 1]);
1963 if (!name) {
1964 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1965 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001966 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001967 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001968
1969 /* Check if an implicit filter with the same name already exists. If so,
1970 * we remove the implicit filter to use the explicit one. */
1971 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1972 if (f->id != cache_store_flt_id)
1973 continue;
1974
1975 cconf = f->conf;
1976 if (strcmp(name, cconf->c.name)) {
1977 cconf = NULL;
1978 continue;
1979 }
1980
1981 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1982 cconf = NULL;
1983 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1984 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001985 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001986 }
1987
1988 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1989 LIST_DEL(&f->list);
1990 free(f);
1991 free(name);
1992 break;
1993 }
1994
1995 /* No implicit cache filter found, create configuration for the explicit one */
1996 if (!cconf) {
1997 cconf = calloc(1, sizeof(*cconf));
1998 if (!cconf) {
1999 memprintf(err, "%s: out of memory", args[*cur_arg]);
2000 goto error;
2001 }
2002 cconf->c.name = name;
2003 }
2004
2005 cconf->flags = 0;
2006 fconf->id = cache_store_flt_id;
2007 fconf->conf = cconf;
2008 fconf->ops = &cache_ops;
2009
2010 *cur_arg = pos;
2011 return 0;
2012
2013 error:
2014 free(name);
2015 free(cconf);
2016 return -1;
2017}
2018
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002019static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002020{
2021 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2022 return 1;
2023
2024 return 0;
2025}
2026
2027static int cli_io_handler_show_cache(struct appctx *appctx)
2028{
2029 struct cache* cache = appctx->ctx.cli.p0;
2030 struct stream_interface *si = appctx->owner;
2031
William Lallemand1f49a362017-11-21 20:01:26 +01002032 if (cache == NULL) {
2033 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
2034 }
2035
2036 list_for_each_entry_from(cache, &caches, list) {
2037 struct eb32_node *node = NULL;
2038 unsigned int next_key;
2039 struct cache_entry *entry;
2040
William Lallemand1f49a362017-11-21 20:01:26 +01002041 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002042 if (!next_key) {
2043 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
2044 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002045 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002046 return 0;
2047 }
2048 }
William Lallemand1f49a362017-11-21 20:01:26 +01002049
2050 appctx->ctx.cli.p0 = cache;
2051
2052 while (1) {
2053
2054 shctx_lock(shctx_ptr(cache));
2055 node = eb32_lookup_ge(&cache->entries, next_key);
2056 if (!node) {
2057 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02002058 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002059 break;
2060 }
2061
2062 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01002063 chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, read_u32(entry->hash), block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec);
William Lallemand1f49a362017-11-21 20:01:26 +01002064
2065 next_key = node->key + 1;
2066 appctx->ctx.cli.i0 = next_key;
2067
2068 shctx_unlock(shctx_ptr(cache));
2069
2070 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002071 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01002072 return 0;
2073 }
2074 }
2075
2076 }
2077
2078 return 1;
2079
2080}
2081
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002082
2083/*
2084 * boolean, returns true if response was built out of a cache entry.
2085 */
2086static int
2087smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2088 const char *kw, void *private)
2089{
2090 smp->data.type = SMP_T_BOOL;
2091 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2092
2093 return 1;
2094}
2095
2096/*
2097 * string, returns cache name (if response came from a cache).
2098 */
2099static int
2100smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2101 const char *kw, void *private)
2102{
2103 struct appctx *appctx = NULL;
2104
2105 struct cache_flt_conf *cconf = NULL;
2106 struct cache *cache = NULL;
2107
2108 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2109 return 0;
2110
2111 /* Get appctx from the stream_interface. */
2112 appctx = si_appctx(&smp->strm->si[1]);
2113 if (appctx && appctx->rule) {
2114 cconf = appctx->rule->arg.act.p[0];
2115 if (cconf) {
2116 cache = cconf->c.cache;
2117
2118 smp->data.type = SMP_T_STR;
2119 smp->flags = SMP_F_CONST;
2120 smp->data.u.str.area = cache->id;
2121 smp->data.u.str.data = strlen(cache->id);
2122 return 1;
2123 }
2124 }
2125
2126 return 0;
2127}
2128
Christopher Faulet99a17a22018-12-11 09:18:27 +01002129/* Declare the filter parser for "cache" keyword */
2130static struct flt_kw_list filter_kws = { "CACHE", { }, {
2131 { "cache", parse_cache_flt, NULL },
2132 { NULL, NULL, NULL },
2133 }
2134};
2135
2136INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2137
William Lallemand1f49a362017-11-21 20:01:26 +01002138static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01002139 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
2140 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002141}};
2142
Willy Tarreau0108d902018-11-25 19:14:37 +01002143INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002144
William Lallemand41db4602017-10-30 11:15:51 +01002145static struct action_kw_list http_res_actions = {
2146 .kw = {
2147 { "cache-store", parse_cache_store },
2148 { NULL, NULL }
2149 }
2150};
2151
Willy Tarreau0108d902018-11-25 19:14:37 +01002152INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2153
William Lallemand41db4602017-10-30 11:15:51 +01002154static struct action_kw_list http_req_actions = {
2155 .kw = {
2156 { "cache-use", parse_cache_use },
2157 { NULL, NULL }
2158 }
2159};
2160
Willy Tarreau0108d902018-11-25 19:14:37 +01002161INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2162
Willy Tarreau2231b632019-03-29 18:26:52 +01002163struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002164 .obj_type = OBJ_TYPE_APPLET,
2165 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002166 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002167 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002168};
2169
Willy Tarreaue6552512018-11-26 11:33:13 +01002170/* config parsers for this section */
2171REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002172REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002173
2174
2175/* Note: must not be declared <const> as its list will be overwritten */
2176static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2177 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2178 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2179 { /* END */ },
2180 }
2181};
2182
2183INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);