blob: 420a009ef3fe6425dd4b31fd3d619dbb2d92ecb5 [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
2 * Cache management
3 *
4 * Copyright 2017 HAProxy Technologies
5 * William Lallemand <wlallemand@haproxy.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
Willy Tarreaub2551052020-06-09 09:07:15 +020013#include <import/eb32tree.h>
14#include <import/sha1.h>
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020018#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020020#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020021#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020022#include <haproxy/filters.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/hash.h>
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020027#include <haproxy/http_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/htx.h>
29#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +010031#include <haproxy/sample.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020032#include <haproxy/shctx.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
William Lallemand41db4602017-10-30 11:15:51 +010035
Christopher Faulet27d93c32018-12-15 22:32:02 +010036#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010037 * the filter keyword) */
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +020038#define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */
Christopher Fauletafd819c2018-12-11 08:57:45 +010039
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010040const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010041
Willy Tarreau2231b632019-03-29 18:26:52 +010042extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010043
44struct flt_ops cache_ops;
45
46struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010047 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010048 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010049 unsigned int maxage; /* max-age */
50 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020051 unsigned int maxobjsz; /* max-object-size (in bytes) */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +010052 uint8_t vary_processing_enabled; /* boolean : manage Vary header (disabled by default) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010053 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010054};
55
Christopher Faulet95220e22018-12-07 17:34:39 +010056/* cache config for filters */
57struct cache_flt_conf {
58 union {
59 struct cache *cache; /* cache used by the filter */
60 char *name; /* cache name used during conf parsing */
61 } c;
62 unsigned int flags; /* CACHE_FLT_F_* */
63};
64
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +010065
66/*
67 * Vary-related structures and functions
68 */
69enum vary_header_bit {
70 VARY_ACCEPT_ENCODING = (1 << 0),
71 VARY_REFERER = (1 << 1),
72 VARY_LAST /* should always be last */
73};
74
75typedef int(*http_header_normalizer)(struct ist value, char *buf, unsigned int *buf_len);
76
77struct vary_hashing_information {
78 struct ist hdr_name; /* Header name */
79 enum vary_header_bit value; /* Bit repesenting the header in a vary signature */
80 unsigned int hash_length; /* Size of the sub hash for this header's value */
81 http_header_normalizer norm_fn; /* Normalization function */
82};
83
84static int accept_encoding_normalizer(struct ist value, char *buf, unsigned int *buf_len);
85static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len);
86
87/* Warning : do not forget to update HTTP_CACHE_SEC_KEY_LEN when new items are
88 * added to this array. */
89const struct vary_hashing_information vary_information[] = {
90 { IST("accept-encoding"), VARY_ACCEPT_ENCODING, sizeof(int), &accept_encoding_normalizer },
91 { IST("referer"), VARY_REFERER, sizeof(int), &default_normalizer },
92};
93
94static int http_request_prebuild_full_secondary_key(struct stream *s);
95static int http_request_build_secondary_key(struct stream *s, int vary_signature);
96static int http_request_reduce_secondary_key(unsigned int vary_signature,
97 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]);
98
99
William Lallemand41db4602017-10-30 11:15:51 +0100100/*
101 * cache ctx for filters
102 */
103struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +0100104 struct shared_block *first_block;
105};
106
107struct cache_entry {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100108 unsigned int complete; /* An entry won't be valid until complete is not null. */
William Lallemand41db4602017-10-30 11:15:51 +0100109 unsigned int latest_validation; /* latest validation date */
110 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200111 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100112
William Lallemand41db4602017-10-30 11:15:51 +0100113 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +0100114 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200115
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100116 char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */
117 unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used
118 * to build secondary keys for this cache entry. */
119
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200120 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
121 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
122
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200123 time_t last_modified; /* Origin server "Last-Modified" header value converted in
124 * seconds since epoch. If no "Last-Modified"
125 * header is found, use "Date" header value,
126 * otherwise use reception time. This field will
127 * be used in case of an "If-Modified-Since"-based
128 * conditional request. */
129
William Lallemand41db4602017-10-30 11:15:51 +0100130 unsigned char data[0];
131};
132
133#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +0100134#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100135
136static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +0200137static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +0100138static struct cache *tmp_cache_config = NULL;
139
Willy Tarreau8ceae722018-11-26 11:58:30 +0100140DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
141
William Lallemandf528fff2017-11-23 19:43:17 +0100142struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100143{
144 struct eb32_node *node;
145 struct cache_entry *entry;
146
Willy Tarreau8b507582020-02-25 09:35:07 +0100147 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100148 if (!node)
149 return NULL;
150
151 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100152
153 /* if that's not the right node */
154 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
155 return NULL;
156
William Lallemand08727662017-11-21 20:01:27 +0100157 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100158 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100159 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100160 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100161 entry->eb.key = 0;
162 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100163 return NULL;
164
165}
166
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100167/*
168 * There can be multiple entries with the same primary key in the ebtree so in
169 * order to get the proper one out of the list, we use a secondary_key.
170 * This function simply iterates over all the entries with the same primary_key
171 * until it finds the right one.
172 * Returns the cache_entry in case of success, NULL otherwise.
173 */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100174struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry,
175 char *secondary_key)
176{
177 struct eb32_node *node = &entry->eb;
178
179 if (!entry->secondary_key_signature)
180 return NULL;
181
182 while (entry && memcmp(entry->secondary_key, secondary_key, HTTP_CACHE_SEC_KEY_LEN) != 0) {
183 node = eb32_next_dup(node);
184 entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL;
185 }
186
187 /* Expired entry */
188 if (entry && entry->expire <= now.tv_sec) {
189 eb32_delete(&entry->eb);
190 entry->eb.key = 0;
191 entry = NULL;
192 }
193
194 return entry;
195}
196
William Lallemand4da3f8a2017-10-31 14:33:34 +0100197static inline struct shared_context *shctx_ptr(struct cache *cache)
198{
199 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
200}
201
William Lallemand77c11972017-10-31 20:43:01 +0100202static inline struct shared_block *block_ptr(struct cache_entry *entry)
203{
204 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
205}
206
207
208
William Lallemand41db4602017-10-30 11:15:51 +0100209static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100210cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100211{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100212 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100213 return 0;
214}
215
Christopher Faulet95220e22018-12-07 17:34:39 +0100216static void
217cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
218{
219 struct cache_flt_conf *cconf = fconf->conf;
220
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200221 if (!(cconf->flags & CACHE_FLT_INIT))
222 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100223 free(cconf);
224}
225
William Lallemand4da3f8a2017-10-31 14:33:34 +0100226static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100227cache_store_check(struct proxy *px, struct flt_conf *fconf)
228{
229 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100230 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100231 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100232 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100233
William Lallemandd1d1e222019-08-28 15:22:49 +0200234 /* Find the cache corresponding to the name in the filter config. The
235 * cache will not be referenced now in the filter config because it is
236 * not fully allocated. This step will be performed during the cache
237 * post_check.
238 */
239 list_for_each_entry(cache, &caches_config, list) {
240 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100241 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100242 }
243
244 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
245 proxy_type_str(px), px->id, (char *)cconf->c.name);
246 return 1;
247
248 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100249 /* Here <cache> points on the cache the filter must use and <cconf>
250 * points on the cache filter configuration. */
251
252 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100253 * enabled and if it is after the cache. When the compression is before
254 * the cache, an error is returned. Also check if the cache filter must
255 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100256 list_for_each_entry(f, &px->filter_configs, list) {
257 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100258 /* The compression filter must be evaluated after the cache. */
259 if (comp) {
260 ha_alert("config: %s '%s': unable to enable the compression filter before "
261 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
262 return 1;
263 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100264 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200265 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100266 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200267 else if (f->id == fcgi_flt_id)
268 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100269 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
270 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200271 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100272 * declaration is required. */
273 ha_alert("config: %s '%s': require an explicit filter declaration "
274 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
275 return 1;
276 }
277
Christopher Fauletafd819c2018-12-11 08:57:45 +0100278 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100279 return 0;
280}
281
282static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100283cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100284{
Christopher Faulet65554e12020-03-06 14:52:06 +0100285 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100286
Christopher Faulet65554e12020-03-06 14:52:06 +0100287 st = pool_alloc_dirty(pool_head_cache_st);
288 if (st == NULL)
289 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100290
Christopher Faulet65554e12020-03-06 14:52:06 +0100291 st->first_block = NULL;
292 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100293
Christopher Faulet65554e12020-03-06 14:52:06 +0100294 /* Register post-analyzer on AN_RES_WAIT_HTTP */
295 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100296 return 1;
297}
298
Christopher Faulet65554e12020-03-06 14:52:06 +0100299static void
300cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100301{
302 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100303 struct cache_flt_conf *cconf = FLT_CONF(filter);
304 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100305 struct shared_context *shctx = shctx_ptr(cache);
306
William Lallemand49dc0482017-11-24 14:33:54 +0100307 /* Everything should be released in the http_end filter, but we need to do it
308 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100309 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100310 shctx_lock(shctx);
311 shctx_row_dec_hot(shctx, st->first_block);
312 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100313 }
314 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100315 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100316 filter->ctx = NULL;
317 }
William Lallemand49dc0482017-11-24 14:33:54 +0100318}
319
Christopher Faulet839791a2019-01-07 16:12:07 +0100320static int
321cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
322 unsigned an_bit)
323{
324 struct http_txn *txn = s->txn;
325 struct http_msg *msg = &txn->rsp;
326 struct cache_st *st = filter->ctx;
327
328 if (an_bit != AN_RES_WAIT_HTTP)
329 goto end;
330
331 /* Here we need to check if any compression filter precedes the cache
332 * filter. This is only possible when the compression is configured in
333 * the frontend while the cache filter is configured on the
334 * backend. This case cannot be detected during HAProxy startup. So in
335 * such cases, the cache is disabled.
336 */
337 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
338 pool_free(pool_head_cache_st, st);
339 filter->ctx = NULL;
340 }
341
342 end:
343 return 1;
344}
William Lallemand49dc0482017-11-24 14:33:54 +0100345
346static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100347cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
348{
349 struct cache_st *st = filter->ctx;
350
William Lallemand4da3f8a2017-10-31 14:33:34 +0100351 if (!(msg->chn->flags & CF_ISRESP) || !st)
352 return 1;
353
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200354 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100355 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100356 return 1;
357}
358
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200359static inline void disable_cache_entry(struct cache_st *st,
360 struct filter *filter, struct shared_context *shctx)
361{
362 struct cache_entry *object;
363
364 object = (struct cache_entry *)st->first_block->data;
365 filter->ctx = NULL; /* disable cache */
366 shctx_lock(shctx);
367 shctx_row_dec_hot(shctx, st->first_block);
368 object->eb.key = 0;
369 shctx_unlock(shctx);
370 pool_free(pool_head_cache_st, st);
371}
372
William Lallemand4da3f8a2017-10-31 14:33:34 +0100373static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100374cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
375 unsigned int offset, unsigned int len)
376{
Christopher Faulet95220e22018-12-07 17:34:39 +0100377 struct cache_flt_conf *cconf = FLT_CONF(filter);
378 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100379 struct cache_st *st = filter->ctx;
380 struct htx *htx = htxbuf(&msg->chn->buf);
381 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200382 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100383 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200384 unsigned int orig_len, to_forward;
385 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100386
387 if (!len)
388 return len;
389
390 if (!st->first_block) {
391 unregister_data_filter(s, msg->chn, filter);
392 return len;
393 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100394
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200395 chunk_reset(&trash);
396 orig_len = len;
397 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100398
399 htxret = htx_find_offset(htx, offset);
400 blk = htxret.blk;
401 offset = htxret.ret;
402 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100403 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200404 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100405 struct ist v;
406
407 switch (type) {
408 case HTX_BLK_UNUSED:
409 break;
410
411 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100412 v = htx_get_blk_value(htx, blk);
413 v.ptr += offset;
414 v.len -= offset;
415 if (v.len > len)
416 v.len = len;
417
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200418 info = (type << 28) + v.len;
419 chunk_memcat(&trash, (char *)&info, sizeof(info));
420 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100421 to_forward += v.len;
422 len -= v.len;
423 break;
424
425 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200426 /* Here offset must always be 0 because only
427 * DATA blocks can be partially transferred. */
428 if (offset)
429 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100430 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200431 goto end;
432
433 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
434 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100435 to_forward += sz;
436 len -= sz;
437 break;
438 }
439
440 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100441 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200442
443 end:
444 shctx_lock(shctx);
445 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
446 if (!fb) {
447 shctx_unlock(shctx);
448 goto no_cache;
449 }
450 shctx_unlock(shctx);
451
452 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
453 (unsigned char *)b_head(&trash), b_data(&trash));
454 if (ret < 0)
455 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100456
457 return to_forward;
458
459 no_cache:
460 disable_cache_entry(st, filter, shctx);
461 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200462 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100463}
464
465static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100466cache_store_http_end(struct stream *s, struct filter *filter,
467 struct http_msg *msg)
468{
469 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100470 struct cache_flt_conf *cconf = FLT_CONF(filter);
471 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100472 struct shared_context *shctx = shctx_ptr(cache);
473 struct cache_entry *object;
474
475 if (!(msg->chn->flags & CF_ISRESP))
476 return 1;
477
478 if (st && st->first_block) {
479
480 object = (struct cache_entry *)st->first_block->data;
481
William Lallemand4da3f8a2017-10-31 14:33:34 +0100482 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100483 /* The whole payload was cached, the entry can now be used. */
484 object->complete = 1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100486 shctx_row_dec_hot(shctx, st->first_block);
487 shctx_unlock(shctx);
488
489 }
490 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100491 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100492 filter->ctx = NULL;
493 }
494
495 return 1;
496}
497
498 /*
499 * This intends to be used when checking HTTP headers for some
500 * word=value directive. Return a pointer to the first character of value, if
501 * the word was not found or if there wasn't any value assigned ot it return NULL
502 */
503char *directive_value(const char *sample, int slen, const char *word, int wlen)
504{
505 int st = 0;
506
507 if (slen < wlen)
508 return 0;
509
510 while (wlen) {
511 char c = *sample ^ *word;
512 if (c && c != ('A' ^ 'a'))
513 return NULL;
514 sample++;
515 word++;
516 slen--;
517 wlen--;
518 }
519
520 while (slen) {
521 if (st == 0) {
522 if (*sample != '=')
523 return NULL;
524 sample++;
525 slen--;
526 st = 1;
527 continue;
528 } else {
529 return (char *)sample;
530 }
531 }
532
533 return NULL;
534}
535
536/*
537 * Return the maxage in seconds of an HTTP response.
538 * Compute the maxage using either:
539 * - the assigned max-age of the cache
540 * - the s-maxage directive
541 * - the max-age directive
542 * - (Expires - Data) headers
543 * - the default-max-age of the cache
544 *
545 */
William Lallemand49b44532017-11-24 18:53:43 +0100546int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100547{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200548 struct htx *htx = htxbuf(&s->res.buf);
549 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100550 int smaxage = -1;
551 int maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100552 int expires = -1;
553 struct tm tm = {};
554 time_t expires_val = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100555
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200556 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
557 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100558
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200559 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
560 if (value) {
561 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100562
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200563 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
564 chunk_strncat(chk, "", 1);
Remi Tricot-Le Breton8c2db712020-10-30 14:26:13 +0100565 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100566 }
567
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200568 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
569 if (value) {
570 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200571
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200572 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
573 chunk_strncat(chk, "", 1);
Remi Tricot-Le Breton8c2db712020-10-30 14:26:13 +0100574 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100575 }
576 }
577
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100578 /* Look for Expires header if no s-maxage or max-age Cache-Control data
579 * was found. */
580 if (maxage == -1 && smaxage == -1) {
581 ctx.blk = NULL;
582 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
583 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
584 expires_val = my_timegm(&tm);
585 /* A request having an expiring date earlier
586 * than the current date should be considered as
587 * stale. */
588 expires = (expires_val >= now.tv_sec) ?
589 (expires_val - now.tv_sec) : 0;
590 }
591 else {
592 /* Following RFC 7234#5.3, an invalid date
593 * format must be treated as a date in the past
594 * so the cache entry must be seen as already
595 * expired. */
596 expires = 0;
597 }
598 }
599 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100600
601
602 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100603 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100604
605 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100606 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100607
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100608 if (expires >= 0)
609 return MIN(expires, cache->maxage);
610
William Lallemand49b44532017-11-24 18:53:43 +0100611 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100612
613}
614
615
William Lallemanda400a3a2017-11-20 19:13:12 +0100616static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
617{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200618 struct cache_entry *object = (struct cache_entry *)block->data;
619
620 if (first == block && object->eb.key)
621 eb32_delete(&object->eb);
622 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100623}
624
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200625
626/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
627 * date value should be compared to a date determined by in a previous response (for
628 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
629 * value of the response's reception time (by decreasing order of priority). */
630static time_t get_last_modified_time(struct htx *htx)
631{
632 time_t last_modified = 0;
633 struct http_hdr_ctx ctx = { .blk = NULL };
634 struct tm tm = {};
635
636 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
637 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
638 last_modified = my_timegm(&tm);
639 }
640 }
641
642 if (!last_modified) {
643 ctx.blk = NULL;
644 if (http_find_header(htx, ist("date"), &ctx, 1)) {
645 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
646 last_modified = my_timegm(&tm);
647 }
648 }
649 }
650
651 /* Fallback on the current time if no "Last-Modified" or "Date" header
652 * was found. */
653 if (!last_modified)
654 last_modified = now.tv_sec;
655
656 return last_modified;
657}
658
William Lallemand41db4602017-10-30 11:15:51 +0100659/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100660 * Checks the vary header's value. The headers on which vary should be applied
661 * must be explicitely supported in the vary_information array (see cache.c). If
662 * any other header is mentioned, we won't store the response.
663 * Returns 1 if Vary-based storage can work, 0 otherwise.
664 */
665static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
666{
667 unsigned int vary_idx;
668 unsigned int vary_info_count;
669 const struct vary_hashing_information *vary_info;
670 struct http_hdr_ctx ctx = { .blk = NULL };
671
672 int retval = 1;
673
674 *vary_signature = 0;
675
676 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
677 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
678 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
679 vary_info = &vary_information[vary_idx];
680 if (isteqi(ctx.value, vary_info->hdr_name)) {
681 *vary_signature |= vary_info->value;
682 break;
683 }
684 }
685 retval = (vary_idx < vary_info_count);
686 }
687
688 return retval;
689}
690
691
692
693/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500694 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100695 * register a filter to store the data
696 */
697enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200698 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100699{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200700 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100701 struct http_txn *txn = s->txn;
702 struct http_msg *msg = &txn->rsp;
703 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100704 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100705 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100706 struct cache *cache = cconf->c.cache;
707 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100708 struct cache_st *cache_ctx = NULL;
709 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100710 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200711 struct htx *htx;
712 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200713 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200714 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200715 struct ist header_name = IST_NULL;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100716 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100717
William Lallemand4da3f8a2017-10-31 14:33:34 +0100718 /* Don't cache if the response came from a cache */
719 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
720 s->target == &http_cache_applet.obj_type) {
721 goto out;
722 }
723
724 /* cache only HTTP/1.1 */
725 if (!(txn->req.flags & HTTP_MSGF_VER_11))
726 goto out;
727
Willy Tarreau6905d182019-10-01 17:59:17 +0200728 /* cache only GET method */
729 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100730 goto out;
731
Willy Tarreauc9036c02019-01-11 19:38:25 +0100732 /* cache key was not computed */
733 if (!key)
734 goto out;
735
William Lallemand4da3f8a2017-10-31 14:33:34 +0100736 /* cache only 200 status code */
737 if (txn->status != 200)
738 goto out;
739
Christopher Faulet839791a2019-01-07 16:12:07 +0100740 /* Find the corresponding filter instance for the current stream */
741 list_for_each_entry(filter, &s->strm_flt.filters, list) {
742 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
743 /* No filter ctx, don't cache anything */
744 if (!filter->ctx)
745 goto out;
746 cache_ctx = filter->ctx;
747 break;
748 }
749 }
750
751 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200752 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100753
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200754 /* Do not cache too big objects. */
755 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
756 htx->data + htx->extra > shctx->max_obj_size)
757 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100758
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100759 /* Only a subset of headers are supported in our Vary implementation. If
760 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100761 * able to use the cache. Likewise, if Vary header support is disabled,
762 * avoid caching responses that contain such a header. */
763 ctx.blk = NULL;
764 if (cache->vary_processing_enabled) {
765 if (!http_check_vary_header(htx, &vary_signature))
766 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100767 if (vary_signature)
768 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100769 }
770 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200771 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100772 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100773
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200774 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100775
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100776 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200777 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100778
779 shctx_lock(shctx);
780 old = entry_exist(cache, txn->cache_hash);
781 if (old) {
782 if (vary_signature)
783 old = secondary_entry_exist(cconf->c.cache, old,
784 txn->cache_secondary_hash);
785 if (old) {
786 if (!old->complete) {
787 /* An entry with the same primary key is already being
788 * created, we should not try to store the current
789 * response because it will waste space in the cache. */
790 shctx_unlock(shctx);
791 goto out;
792 }
793 eb32_delete(&old->eb);
794 old->eb.key = 0;
795 }
796 }
797 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry));
798 if (!first) {
799 shctx_unlock(shctx);
800 goto out;
801 }
802 /* the received memory is not initialized, we need at least to mark
803 * the object as not indexed yet.
804 */
805 object = (struct cache_entry *)first->data;
806 memset(object, 0, sizeof(*object));
807 object->eb.key = key;
808 object->secondary_key_signature = vary_signature;
809 /* We need to temporarily set a valid expiring time until the actual one
810 * is set by the end of this function (in case of concurrent accesses to
811 * the same resource). This way the second access will find an existing
812 * but not yet usable entry in the tree and will avoid storing its data. */
813 object->expire = now.tv_sec + 2;
814
815 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
816 if (vary_signature)
817 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
818
819 /* Insert the entry in the tree even if the payload is not cached yet. */
820 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
821 object->eb.key = 0;
822 shctx_unlock(shctx);
823 goto out;
824 }
825 shctx_unlock(shctx);
826
827 /* reserve space for the cache_entry structure */
828 first->len = sizeof(struct cache_entry);
829 first->last_append = NULL;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100830
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200831 ctx.blk = NULL;
832 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
833 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
834 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
835 hdr_age = CACHE_ENTRY_MAX_AGE;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100836 object->age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100837 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200838 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100839 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100840
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200841 /* Build a last-modified time that will be stored in the cache_entry and
842 * compared to a future If-Modified-Since client header. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100843 object->last_modified = get_last_modified_time(htx);
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200844
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200845 chunk_reset(&trash);
846 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
847 struct htx_blk *blk = htx_get_blk(htx, pos);
848 enum htx_blk_type type = htx_get_blk_type(blk);
849 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100850
Christopher Fauletb0667472019-09-03 22:22:12 +0200851 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200852 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
853 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200854
855 /* Look for optional ETag header.
856 * We need to store the offset of the ETag value in order for
857 * future conditional requests to be able to perform ETag
858 * comparisons. */
859 if (type == HTX_BLK_HDR) {
860 header_name = htx_get_blk_name(htx, blk);
861 if (isteq(header_name, ist("etag"))) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100862 object->etag_length = sz - istlen(header_name);
863 object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200864 }
865 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200866 if (type == HTX_BLK_EOH)
867 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200868 }
869
Christopher Fauletb0667472019-09-03 22:22:12 +0200870 /* Do not cache objects if the headers are too big. */
871 if (hdrs_len > htx->size - global.tune.maxrewrite)
872 goto out;
873
William Lallemand4da3f8a2017-10-31 14:33:34 +0100874 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100875 if (!shctx_row_reserve_hot(shctx, first, trash.data)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100876 shctx_unlock(shctx);
877 goto out;
878 }
879 shctx_unlock(shctx);
880
William Lallemand4da3f8a2017-10-31 14:33:34 +0100881 /* cache the headers in a http action because it allows to chose what
882 * to cache, for example you might want to cache a response before
883 * modifying some HTTP headers, or on the contrary after modifying
884 * those headers.
885 */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100886 /* does not need to be locked because it's in the "hot" list,
887 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200888 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
889 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100890
891 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100892 if (cache_ctx) {
893 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100894
Christopher Faulet839791a2019-01-07 16:12:07 +0100895 /* store latest value and expiration time */
896 object->latest_validation = now.tv_sec;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100897 object->expire = now.tv_sec + http_calc_maxage(s, cache);
898
Christopher Faulet839791a2019-01-07 16:12:07 +0100899 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100900 }
901
902out:
903 /* if does not cache */
904 if (first) {
905 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100906 first->len = 0;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100907 if (object->eb.key)
908 eb32_delete(&object->eb);
William Lallemand08727662017-11-21 20:01:27 +0100909 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100910 shctx_row_dec_hot(shctx, first);
911 shctx_unlock(shctx);
912 }
913
William Lallemand41db4602017-10-30 11:15:51 +0100914 return ACT_RET_CONT;
915}
916
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100917#define HTX_CACHE_INIT 0 /* Initial state. */
918#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
919#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200920#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
921#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100922
William Lallemandecb73b12017-11-24 14:33:55 +0100923static void http_cache_applet_release(struct appctx *appctx)
924{
Christopher Faulet95220e22018-12-07 17:34:39 +0100925 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100926 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100927 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100928 struct shared_block *first = block_ptr(cache_ptr);
929
930 shctx_lock(shctx_ptr(cache));
931 shctx_row_dec_hot(shctx_ptr(cache), first);
932 shctx_unlock(shctx_ptr(cache));
933}
934
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200935
936static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
937 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100938{
Christopher Faulet95220e22018-12-07 17:34:39 +0100939 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
940 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200941 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200942 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200943 unsigned int max, total;
944 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100945
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200946 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
947 if (!max)
948 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200949 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200950 ? (info & 0xff) + ((info >> 8) & 0xfffff)
951 : info & 0xfffffff);
952 if (blksz > max)
953 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100954
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200955 blk = htx_add_blk(htx, type, blksz);
956 if (!blk)
957 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100958
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200959 blk->info = info;
960 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200961 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200962 while (blksz) {
963 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200964 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200965 offset += max;
966 blksz -= max;
967 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200968 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200969 if (blksz || offset == shctx->block_size) {
970 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
971 offset = 0;
972 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100973 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200974 appctx->ctx.cache.offset = offset;
975 appctx->ctx.cache.next = shblk;
976 appctx->ctx.cache.sent += total;
977 return total;
978}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100979
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200980static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
981 uint32_t info, struct shared_block *shblk, unsigned int offset)
982{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100983
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200984 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
985 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
986 unsigned int max, total, rem_data;
987 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100988
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200989 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
990 if (!max)
991 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100992
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200993 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200994 if (appctx->ctx.cache.rem_data) {
995 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200996 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200997 }
998 else {
999 blksz = (info & 0xfffffff);
1000 total = 4;
1001 }
1002 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001003 rem_data = blksz - max;
1004 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001005 }
1006
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001007 while (blksz) {
1008 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001009
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001010 max = MIN(blksz, shctx->block_size - offset);
1011 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1012 offset += sz;
1013 blksz -= sz;
1014 total += sz;
1015 if (sz < max)
1016 break;
1017 if (blksz || offset == shctx->block_size) {
1018 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1019 offset = 0;
1020 }
1021 }
1022
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001023 appctx->ctx.cache.offset = offset;
1024 appctx->ctx.cache.next = shblk;
1025 appctx->ctx.cache.sent += total;
1026 appctx->ctx.cache.rem_data = rem_data + blksz;
1027 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001028}
1029
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001030static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1031 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001032{
Christopher Faulet95220e22018-12-07 17:34:39 +01001033 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1034 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001035 struct shared_block *shblk;
1036 unsigned int offset, sz;
1037 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001038
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001039 while (len) {
1040 enum htx_blk_type type;
1041 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001042
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001043 shblk = appctx->ctx.cache.next;
1044 offset = appctx->ctx.cache.offset;
1045 if (appctx->ctx.cache.rem_data) {
1046 type = HTX_BLK_DATA;
1047 info = 0;
1048 goto add_data_blk;
1049 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001050
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001051 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001052 sz = MIN(4, shctx->block_size - offset);
1053 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1054 offset += sz;
1055 if (sz < 4) {
1056 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1057 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1058 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001059 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001060
1061 /* Get payload of the next HTX block and insert it. */
1062 type = (info >> 28);
1063 if (type != HTX_BLK_DATA)
1064 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1065 else {
1066 add_data_blk:
1067 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001068 }
1069
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001070 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001071 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001072 total += ret;
1073 len -= ret;
1074
1075 if (appctx->ctx.cache.rem_data || type == mark)
1076 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001077 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001078
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001079 return total;
1080}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001081
1082static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1083{
1084 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1085 unsigned int age;
1086 char *end;
1087
1088 chunk_reset(&trash);
1089 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1090 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1091 age = CACHE_ENTRY_MAX_AGE;
1092 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1093 b_set_data(&trash, end - b_head(&trash));
1094 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1095 return 0;
1096 return 1;
1097}
1098
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001099static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001100{
1101 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1102 struct shared_block *first = block_ptr(cache_ptr);
1103 struct stream_interface *si = appctx->owner;
1104 struct channel *req = si_oc(si);
1105 struct channel *res = si_ic(si);
1106 struct htx *req_htx, *res_htx;
1107 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001108 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001109 size_t ret, total = 0;
1110
1111 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001112 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001113
1114 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1115 goto out;
1116
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001117 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001118 if (!b_size(&res->buf)) {
1119 si_rx_room_blk(si);
1120 goto out;
1121 }
1122
Willy Tarreauefef3232018-12-16 00:37:45 +01001123 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001124 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001125
1126 if (appctx->st0 == HTX_CACHE_INIT) {
1127 appctx->ctx.cache.next = block_ptr(cache_ptr);
1128 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1129 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001130 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001131 appctx->st0 = HTX_CACHE_HEADER;
1132 }
1133
1134 if (appctx->st0 == HTX_CACHE_HEADER) {
1135 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001136 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1137 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1138 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1139 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001140 goto error;
1141
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001142 /* In case of a conditional request, we might want to send a
1143 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001144 if (appctx->ctx.cache.send_notmodified) {
1145 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1146 /* If replacing the status code fails we need to send the full response. */
1147 appctx->ctx.cache.send_notmodified = 0;
1148 }
1149 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001150
1151 /* Skip response body for HEAD requests or in case of "304 Not
1152 * Modified" response. */
1153 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001154 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001155 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001156 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001157 }
1158
1159 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001160 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1161 if (len) {
1162 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1163 if (ret < len) {
1164 si_rx_room_blk(si);
1165 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001166 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001167 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001168 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001169 }
1170
1171 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001172 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001173 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1174 si_rx_room_blk(si);
1175 goto out;
1176 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001177 appctx->st0 = HTX_CACHE_END;
1178 }
1179
1180 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001181 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001182 res->flags |= CF_READ_NULL;
1183 si_shutr(si);
1184 }
1185
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001186 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001187 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001188 if (total)
1189 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001190 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001191
1192 /* eat the whole request */
1193 if (co_data(req)) {
1194 req_htx = htx_from_buf(&req->buf);
1195 co_htx_skip(req, req_htx, co_data(req));
1196 htx_to_buf(req_htx, &req->buf);
1197 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001198 return;
1199
1200 error:
1201 /* Sent and HTTP error 500 */
1202 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001203 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001204 res->buf.data = b_data(errmsg);
1205 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1206 res_htx = htx_from_buf(&res->buf);
1207
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001208 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001209 appctx->st0 = HTX_CACHE_END;
1210 goto end;
1211}
1212
1213
Christopher Faulet95220e22018-12-07 17:34:39 +01001214static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001215{
1216 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001217 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001218
Christopher Faulet95220e22018-12-07 17:34:39 +01001219 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001220 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001221 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001222 }
1223
1224 /* check if a cache filter was already registered with this cache
1225 * name, if that's the case, must use it. */
1226 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001227 if (fconf->id == cache_store_flt_id) {
1228 cconf = fconf->conf;
1229 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1230 rule->arg.act.p[0] = cconf;
1231 return 1;
1232 }
William Lallemand41db4602017-10-30 11:15:51 +01001233 }
1234 }
1235
Christopher Faulet95220e22018-12-07 17:34:39 +01001236 /* Create the filter cache config */
1237 cconf = calloc(1, sizeof(*cconf));
1238 if (!cconf) {
1239 memprintf(err, "out of memory\n");
1240 goto err;
1241 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001242 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001243 cconf->c.name = strdup(name);
1244 if (!cconf->c.name) {
1245 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001246 goto err;
1247 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001248
William Lallemand41db4602017-10-30 11:15:51 +01001249 /* register a filter to fill the cache buffer */
1250 fconf = calloc(1, sizeof(*fconf));
1251 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001252 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001253 goto err;
1254 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001255 fconf->id = cache_store_flt_id;
1256 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001257 fconf->ops = &cache_ops;
1258 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1259
Christopher Faulet95220e22018-12-07 17:34:39 +01001260 rule->arg.act.p[0] = cconf;
1261 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001262
Christopher Faulet95220e22018-12-07 17:34:39 +01001263 err:
1264 free(cconf);
1265 return 0;
1266}
1267
1268enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1269 struct act_rule *rule, char **err)
1270{
1271 rule->action = ACT_CUSTOM;
1272 rule->action_ptr = http_action_store_cache;
1273
1274 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1275 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001276
Christopher Faulet95220e22018-12-07 17:34:39 +01001277 (*orig_arg)++;
1278 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001279}
1280
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001281/* This produces a sha1 hash of the concatenation of the HTTP method,
1282 * the first occurrence of the Host header followed by the path component
1283 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001284int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001285{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001286 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001287 struct htx *htx = htxbuf(&s->req.buf);
1288 struct htx_sl *sl;
1289 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001290 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001291 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001292 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001293
William Lallemandf528fff2017-11-23 19:43:17 +01001294 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001295 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001296
1297 switch (txn->meth) {
1298 case HTTP_METH_HEAD:
1299 case HTTP_METH_GET:
1300 chunk_memcat(trash, "GET", 3);
1301 break;
1302 default:
1303 return 0;
1304 }
1305
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001306 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001307 uri = htx_sl_req_uri(sl); // whole uri
1308 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001309 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001310
1311 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1312 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1313 * URIs are almost always sent in absolute form with their scheme. In
1314 * this case, the scheme is almost always "https". In order to support
1315 * sharing of cache objects between H1 and H2, we'll hash the absolute
1316 * URI whenever known, or prepend "https://" + the Host header for
1317 * relative URIs. The difference will only appear on absolute HTTP/1
1318 * requests sent to an origin server, which practically is never met in
1319 * the real world so we don't care about the ability to share the same
1320 * key here.URIs are normalized from the absolute URI to an origin form as
1321 * well.
1322 */
1323 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001324 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001325 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1326 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001327 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001328 }
1329
1330 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001331
1332 /* hash everything */
1333 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001334 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001335 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1336
1337 return 1;
1338}
1339
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001340/* Looks for "If-None-Match" headers in the request and compares their value
1341 * with the one that might have been stored in the cache_entry. If any of them
1342 * matches, a "304 Not Modified" response should be sent instead of the cached
1343 * data.
1344 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001345 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1346 *
1347 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1348 * header and compare its value (date) to the one stored in the cache_entry.
1349 * If the request's date is later than the cached one, we also send a
1350 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1351 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001352 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1353 */
1354static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1355 struct cache_entry *entry)
1356{
1357 int retval = 0;
1358
1359 struct http_hdr_ctx ctx = { .blk = NULL };
1360 struct ist cache_entry_etag = IST_NULL;
1361 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001362 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001363
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001364 struct tm tm = {};
1365 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001366
1367 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001368 * cache_entry's ETag in order to perform comparisons.
1369 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001370 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001371 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001372
1373 /* A '*' matches everything. */
1374 if (isteq(ctx.value, ist("*")) != 0) {
1375 retval = 1;
1376 break;
1377 }
1378
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001379 /* No need to rebuild an etag if none was stored in the cache. */
1380 if (entry->etag_length == 0)
1381 break;
1382
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001383 /* Rebuild the stored ETag. */
1384 if (etag_buffer == NULL) {
1385 etag_buffer = get_trash_chunk();
1386
1387 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1388 (unsigned char*)b_orig(etag_buffer),
1389 entry->etag_offset, entry->etag_length) == 0) {
1390 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1391 } else {
1392 /* We could not rebuild the ETag in one go, we
1393 * won't send a "304 Not Modified" response. */
1394 break;
1395 }
1396 }
1397
1398 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1399 retval = 1;
1400 break;
1401 }
1402 }
1403
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001404 /* If the request did not contain an "If-None-Match" header, we look for
1405 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1406 if (retval == 0 && if_none_match_found == 0) {
1407 ctx.blk = NULL;
1408 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1409 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1410 if_modified_since = my_timegm(&tm);
1411
1412 /* We send a "304 Not Modified" response if the
1413 * entry's last modified date is earlier than
1414 * the one found in the "If-Modified-Since"
1415 * header. */
1416 retval = (entry->last_modified <= if_modified_since);
1417 }
1418 }
1419 }
1420
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001421 return retval;
1422}
1423
William Lallemand41db4602017-10-30 11:15:51 +01001424enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1425 struct session *sess, struct stream *s, int flags)
1426{
William Lallemand77c11972017-10-31 20:43:01 +01001427
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001428 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001429 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001430 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1431 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001432 struct shared_block *entry_block;
1433
William Lallemand77c11972017-10-31 20:43:01 +01001434
Willy Tarreau6905d182019-10-01 17:59:17 +02001435 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1436 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001437 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001438 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001439 txn->flags |= TX_CACHE_IGNORE;
1440
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001441 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001442
Willy Tarreau504455c2017-12-22 17:47:35 +01001443 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1444 return ACT_RET_CONT;
1445
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001446 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001447 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001448
Willy Tarreau504455c2017-12-22 17:47:35 +01001449 if (s->txn->flags & TX_CACHE_IGNORE)
1450 return ACT_RET_CONT;
1451
Willy Tarreaua1214a52018-12-14 14:00:25 +01001452 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001453 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001454 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001455 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001456
William Lallemanda400a3a2017-11-20 19:13:12 +01001457 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001458 res = entry_exist(cache, s->txn->cache_hash);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001459 /* We must not use an entry that is not complete. */
1460 if (res && res->complete) {
William Lallemand77c11972017-10-31 20:43:01 +01001461 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001462 entry_block = block_ptr(res);
1463 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001464 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001465
1466 /* In case of Vary, we could have multiple entries with the same
1467 * primary hash. We need to calculate the secondary has in order
1468 * to find the actual entry we want (if it exists). */
1469 if (res->secondary_key_signature) {
1470 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1471 shctx_lock(shctx_ptr(cache));
1472 sec_entry = secondary_entry_exist(cache, res,
1473 s->txn->cache_secondary_hash);
1474 if (sec_entry && sec_entry != res) {
1475 /* The wrong row was added to the hot list. */
1476 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1477 entry_block = block_ptr(sec_entry);
1478 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1479 }
1480 res = sec_entry;
1481 shctx_unlock(shctx_ptr(cache));
1482 }
1483 else
1484 res = NULL;
1485 }
1486
1487 /* We looked for a valid secondary entry and could not find one,
1488 * the request must be forwarded to the server. */
1489 if (!res) {
1490 shctx_lock(shctx_ptr(cache));
1491 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1492 shctx_unlock(shctx_ptr(cache));
1493 return ACT_RET_CONT;
1494 }
1495
William Lallemand77c11972017-10-31 20:43:01 +01001496 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001497 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001498 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001499 appctx->rule = rule;
1500 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001501 appctx->ctx.cache.next = NULL;
1502 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001503 appctx->ctx.cache.send_notmodified =
1504 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001505
1506 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001507 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001508 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001509 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001510 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001511 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001512 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001513 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001514 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001515 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001516 }
1517 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001518 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001519
1520 /* Shared context does not need to be locked while we calculate the
1521 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001522 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001523 /* Build a complete secondary hash until the server response
1524 * tells us which fields should be kept (if any). */
1525 http_request_prebuild_full_secondary_key(s);
1526 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001527 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001528}
1529
1530
1531enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1532 struct act_rule *rule, char **err)
1533{
William Lallemand41db4602017-10-30 11:15:51 +01001534 rule->action = ACT_CUSTOM;
1535 rule->action_ptr = http_action_req_cache_use;
1536
Christopher Faulet95220e22018-12-07 17:34:39 +01001537 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001538 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001539
1540 (*orig_arg)++;
1541 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001542}
1543
1544int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1545{
1546 int err_code = 0;
1547
1548 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1549
1550 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001551 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001552 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001553 err_code |= ERR_ALERT | ERR_ABORT;
1554 goto out;
1555 }
1556
1557 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1558 err_code |= ERR_ABORT;
1559 goto out;
1560 }
1561
1562 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001563 struct cache *cache_config;
1564
William Lallemand41db4602017-10-30 11:15:51 +01001565 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1566 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001567 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001568 err_code |= ERR_ALERT | ERR_ABORT;
1569 goto out;
1570 }
1571
1572 strlcpy2(tmp_cache_config->id, args[1], 33);
1573 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001574 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001575 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001576 err_code |= ERR_WARN;
1577 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001578
1579 list_for_each_entry(cache_config, &caches_config, list) {
1580 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1581 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1582 file, linenum, tmp_cache_config->id);
1583 err_code |= ERR_ALERT | ERR_ABORT;
1584 goto out;
1585 }
1586 }
1587
William Lallemand49b44532017-11-24 18:53:43 +01001588 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001589 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001590 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001591 }
1592 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001593 unsigned long int maxsize;
1594 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001595
1596 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1597 err_code |= ERR_ABORT;
1598 goto out;
1599 }
1600
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001601 maxsize = strtoul(args[1], &err, 10);
1602 if (err == args[1] || *err != '\0') {
1603 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1604 file, linenum, args[1]);
1605 err_code |= ERR_ABORT;
1606 goto out;
1607 }
1608
1609 if (maxsize > (UINT_MAX >> 20)) {
1610 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1611 file, linenum, args[1], UINT_MAX >> 20);
1612 err_code |= ERR_ABORT;
1613 goto out;
1614 }
1615
William Lallemand41db4602017-10-30 11:15:51 +01001616 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001617 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001618 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001619 } else if (strcmp(args[0], "max-age") == 0) {
1620 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1621 err_code |= ERR_ABORT;
1622 goto out;
1623 }
1624
1625 if (!*args[1]) {
1626 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1627 file, linenum, args[0]);
1628 err_code |= ERR_WARN;
1629 }
1630
1631 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001632 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001633 unsigned int maxobjsz;
1634 char *err;
1635
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001636 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1637 err_code |= ERR_ABORT;
1638 goto out;
1639 }
1640
1641 if (!*args[1]) {
1642 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1643 file, linenum, args[0]);
1644 err_code |= ERR_WARN;
1645 }
1646
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001647 maxobjsz = strtoul(args[1], &err, 10);
1648 if (err == args[1] || *err != '\0') {
1649 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1650 file, linenum, args[1]);
1651 err_code |= ERR_ABORT;
1652 goto out;
1653 }
1654 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001655 } else if (strcmp(args[0], "process-vary") == 0) {
1656 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1657 err_code |= ERR_ABORT;
1658 goto out;
1659 }
1660
1661 if (!*args[1]) {
1662 ha_warning("parsing [%s:%d]: '%s' expects 0 or 1 (disable or enable vary processing).\n",
1663 file, linenum, args[0]);
1664 err_code |= ERR_WARN;
1665 }
1666
1667 tmp_cache_config->vary_processing_enabled = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001668 }
1669 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001670 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001671 err_code |= ERR_ALERT | ERR_FATAL;
1672 goto out;
1673 }
1674out:
1675 return err_code;
1676}
1677
1678/* once the cache section is parsed */
1679
1680int cfg_post_parse_section_cache()
1681{
William Lallemand41db4602017-10-30 11:15:51 +01001682 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001683
1684 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001685
1686 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001687 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001688 err_code |= ERR_FATAL | ERR_ALERT;
1689 goto out;
1690 }
1691
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001692 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001693 /* Default max. file size is a 256th of the cache size. */
1694 tmp_cache_config->maxobjsz =
1695 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001696 }
1697 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1698 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1699 err_code |= ERR_FATAL | ERR_ALERT;
1700 goto out;
1701 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001702
William Lallemandd1d1e222019-08-28 15:22:49 +02001703 /* add to the list of cache to init and reinit tmp_cache_config
1704 * for next cache section, if any.
1705 */
1706 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1707 tmp_cache_config = NULL;
1708 return err_code;
1709 }
1710out:
1711 free(tmp_cache_config);
1712 tmp_cache_config = NULL;
1713 return err_code;
1714
1715}
1716
1717int post_check_cache()
1718{
1719 struct proxy *px;
1720 struct cache *back, *cache_config, *cache;
1721 struct shared_context *shctx;
1722 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001723 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02001724
1725 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1726
1727 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1728 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001729
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001730 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001731 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001732 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001733 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001734 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001735
1736 err_code |= ERR_FATAL | ERR_ALERT;
1737 goto out;
1738 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001739 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001740 /* the cache structure is stored in the shctx and added to the
1741 * caches list, we can remove the entry from the caches_config
1742 * list */
1743 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001744 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001745 cache->entries = EB_ROOT;
William Lallemand41db4602017-10-30 11:15:51 +01001746 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001747 LIST_DEL(&cache_config->list);
1748 free(cache_config);
1749
1750 /* Find all references for this cache in the existing filters
1751 * (over all proxies) and reference it in matching filters.
1752 */
1753 for (px = proxies_list; px; px = px->next) {
1754 struct flt_conf *fconf;
1755 struct cache_flt_conf *cconf;
1756
1757 list_for_each_entry(fconf, &px->filter_configs, list) {
1758 if (fconf->id != cache_store_flt_id)
1759 continue;
1760
1761 cconf = fconf->conf;
1762 if (!strcmp(cache->id, cconf->c.name)) {
1763 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001764 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001765 cconf->c.cache = cache;
1766 break;
1767 }
1768 }
1769 }
William Lallemand41db4602017-10-30 11:15:51 +01001770 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001771
William Lallemand41db4602017-10-30 11:15:51 +01001772out:
William Lallemand41db4602017-10-30 11:15:51 +01001773 return err_code;
1774
William Lallemand41db4602017-10-30 11:15:51 +01001775}
1776
William Lallemand41db4602017-10-30 11:15:51 +01001777struct flt_ops cache_ops = {
1778 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001779 .check = cache_store_check,
1780 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001781
Christopher Faulet65554e12020-03-06 14:52:06 +01001782 /* Handle stream init/deinit */
1783 .attach = cache_store_strm_init,
1784 .detach = cache_store_strm_deinit,
1785
William Lallemand4da3f8a2017-10-31 14:33:34 +01001786 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001787 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001788
1789 /* Filter HTTP requests and responses */
1790 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001791 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001792 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001793};
1794
Christopher Faulet99a17a22018-12-11 09:18:27 +01001795
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001796int accept_encoding_cmp(const void *a, const void *b)
1797{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001798 unsigned int int_a = *(unsigned int*)a;
1799 unsigned int int_b = *(unsigned int*)b;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001800
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001801 if (int_a < int_b)
1802 return -1;
1803 if (int_a > int_b)
1804 return 1;
1805 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001806}
1807
Tim Duesterhus23b29452020-11-24 22:22:56 +01001808#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001809/*
1810 * Build a hash of the accept-encoding header. The different parts of the
1811 * header value are first sorted, appended and then a crc is calculated
1812 * for the newly constructed buffer.
1813 * Returns 0 in case of success.
1814 */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001815static int accept_encoding_normalizer(struct ist full_value, char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001816{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001817 unsigned int values[ACCEPT_ENCODING_MAX_ENTRIES] = {};
Tim Duesterhus23b29452020-11-24 22:22:56 +01001818 size_t count = 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001819 char *comma = NULL;
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001820 unsigned int hash_value = 0;
1821 unsigned int prev = 0, curr = 0;
1822
1823 /* Turn accept-encoding value to lower case */
1824 full_value = ist2bin_lc(istptr(full_value), full_value);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001825
1826 /* The hash will be built out of a sorted list of accepted encodings. */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001827 while (count < (ACCEPT_ENCODING_MAX_ENTRIES - 1) && (comma = istchr(full_value, ',')) != NULL) {
1828 size_t length = comma - istptr(full_value);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001829
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001830 values[count++] = hash_crc32(istptr(full_value), length);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001831
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001832 full_value = istadv(full_value, length + 1);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001833
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001834 }
1835 values[count++] = hash_crc32(istptr(full_value), istlen(full_value));
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001836
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001837 /* Sort the values alphabetically. */
1838 qsort(values, count, sizeof(*values), &accept_encoding_cmp);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001839
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001840 while (count) {
1841 curr = values[--count];
1842 if (curr != prev) {
1843 hash_value ^= curr;
1844 }
1845 prev = curr;
1846 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001847
1848 memcpy(buf, &hash_value, sizeof(hash_value));
1849 *buf_len = sizeof(hash_value);
1850
Tim Duesterhus23b29452020-11-24 22:22:56 +01001851 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001852}
Tim Duesterhus23b29452020-11-24 22:22:56 +01001853#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001854
1855/*
1856 * Normalizer used by default for User-Agent and Referer headers. It only
1857 * calculates a simple crc of the whole value.
1858 * Returns 0 in case of success.
1859 */
1860static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len)
1861{
1862 int hash_value = 0;
1863
1864 hash_value = hash_crc32(istptr(value), istlen(value));
1865
1866 memcpy(buf, &hash_value, sizeof(hash_value));
1867 *buf_len = sizeof(hash_value);
1868
1869 return 0;
1870}
1871
1872
1873/*
1874 * Pre-calculate the hashes of all the supported headers (in our Vary
1875 * implementation) of a given request. We have to calculate all the hashes
1876 * in advance because the actual Vary signature won't be known until the first
1877 * response.
1878 * Only the first occurrence of every header will be taken into account in the
1879 * hash.
1880 * If the header is not present, the hash portion of the given header will be
1881 * filled with zeros.
1882 * Returns 0 in case of success.
1883 */
1884static int http_request_prebuild_full_secondary_key(struct stream *s)
1885{
1886 struct http_txn *txn = s->txn;
1887 struct htx *htx = htxbuf(&s->req.buf);
1888 struct http_hdr_ctx ctx = { .blk = NULL };
1889
1890 unsigned int idx;
1891 const struct vary_hashing_information *info = NULL;
1892 unsigned int hash_length = 0;
1893 int retval = 0;
1894 int offset = 0;
1895
1896 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1897 info = &vary_information[idx];
1898
1899 ctx.blk = NULL;
1900 if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) {
1901 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1902 offset += hash_length;
1903 }
1904 else {
1905 /* Fill hash with 0s. */
1906 hash_length = info->hash_length;
1907 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1908 offset += hash_length;
1909 }
1910 }
1911
1912 return retval;
1913}
1914
1915
1916/*
1917 * Calculate the secondary key for a request for which we already have a known
1918 * vary signature. The key is made by aggregating hashes calculated for every
1919 * header mentioned in the vary signature.
1920 * Only the first occurrence of every header will be taken into account in the
1921 * hash.
1922 * If the header is not present, the hash portion of the given header will be
1923 * filled with zeros.
1924 * Returns 0 in case of success.
1925 */
1926static int http_request_build_secondary_key(struct stream *s, int vary_signature)
1927{
1928 struct http_txn *txn = s->txn;
1929 struct htx *htx = htxbuf(&s->req.buf);
1930 struct http_hdr_ctx ctx = { .blk = NULL };
1931
1932 unsigned int idx;
1933 const struct vary_hashing_information *info = NULL;
1934 unsigned int hash_length = 0;
1935 int retval = 0;
1936 int offset = 0;
1937
1938 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1939 info = &vary_information[idx];
1940
1941 ctx.blk = NULL;
1942 if ((vary_signature & info->value) && info->norm_fn != NULL &&
1943 http_find_header(htx, info->hdr_name, &ctx, 1)) {
1944 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1945 offset += hash_length;
1946 }
1947 else {
1948 /* Fill hash with 0s. */
1949 hash_length = info->hash_length;
1950 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1951 offset += hash_length;
1952 }
1953 }
1954
1955 return retval;
1956}
1957
1958/*
1959 * Build the actual secondary key of a given request out of the prebuilt key and
1960 * the actual vary signature (extracted from the response).
1961 * Returns 0 in case of success.
1962 */
1963static int http_request_reduce_secondary_key(unsigned int vary_signature,
1964 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
1965{
1966 int offset = 0;
1967 int global_offset = 0;
1968 int vary_info_count = 0;
1969 int keep = 0;
1970 unsigned int vary_idx;
1971 const struct vary_hashing_information *vary_info;
1972
1973 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
1974 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
1975 vary_info = &vary_information[vary_idx];
1976 keep = (vary_signature & vary_info->value) ? 0xff : 0;
1977
1978 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
1979 prebuilt_key[global_offset] &= keep;
1980 }
1981 }
1982
1983 return 0;
1984}
1985
1986
Christopher Faulet99a17a22018-12-11 09:18:27 +01001987
1988static int
1989parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1990 struct flt_conf *fconf, char **err, void *private)
1991{
1992 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001993 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001994 char *name = NULL;
1995 int pos = *cur_arg;
1996
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001997 /* Get the cache filter name. <pos> point on "cache" keyword */
1998 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02001999 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002000 goto error;
2001 }
2002 name = strdup(args[pos + 1]);
2003 if (!name) {
2004 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
2005 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002006 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002007 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002008
2009 /* Check if an implicit filter with the same name already exists. If so,
2010 * we remove the implicit filter to use the explicit one. */
2011 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
2012 if (f->id != cache_store_flt_id)
2013 continue;
2014
2015 cconf = f->conf;
2016 if (strcmp(name, cconf->c.name)) {
2017 cconf = NULL;
2018 continue;
2019 }
2020
2021 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2022 cconf = NULL;
2023 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2024 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002025 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002026 }
2027
2028 /* Remove the implicit filter. <cconf> is kept for the explicit one */
2029 LIST_DEL(&f->list);
2030 free(f);
2031 free(name);
2032 break;
2033 }
2034
2035 /* No implicit cache filter found, create configuration for the explicit one */
2036 if (!cconf) {
2037 cconf = calloc(1, sizeof(*cconf));
2038 if (!cconf) {
2039 memprintf(err, "%s: out of memory", args[*cur_arg]);
2040 goto error;
2041 }
2042 cconf->c.name = name;
2043 }
2044
2045 cconf->flags = 0;
2046 fconf->id = cache_store_flt_id;
2047 fconf->conf = cconf;
2048 fconf->ops = &cache_ops;
2049
2050 *cur_arg = pos;
2051 return 0;
2052
2053 error:
2054 free(name);
2055 free(cconf);
2056 return -1;
2057}
2058
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002059static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002060{
2061 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2062 return 1;
2063
2064 return 0;
2065}
2066
2067static int cli_io_handler_show_cache(struct appctx *appctx)
2068{
2069 struct cache* cache = appctx->ctx.cli.p0;
2070 struct stream_interface *si = appctx->owner;
2071
William Lallemand1f49a362017-11-21 20:01:26 +01002072 if (cache == NULL) {
2073 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
2074 }
2075
2076 list_for_each_entry_from(cache, &caches, list) {
2077 struct eb32_node *node = NULL;
2078 unsigned int next_key;
2079 struct cache_entry *entry;
2080
William Lallemand1f49a362017-11-21 20:01:26 +01002081 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002082 if (!next_key) {
2083 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
2084 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002085 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002086 return 0;
2087 }
2088 }
William Lallemand1f49a362017-11-21 20:01:26 +01002089
2090 appctx->ctx.cli.p0 = cache;
2091
2092 while (1) {
2093
2094 shctx_lock(shctx_ptr(cache));
2095 node = eb32_lookup_ge(&cache->entries, next_key);
2096 if (!node) {
2097 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02002098 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002099 break;
2100 }
2101
2102 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01002103 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 +01002104
2105 next_key = node->key + 1;
2106 appctx->ctx.cli.i0 = next_key;
2107
2108 shctx_unlock(shctx_ptr(cache));
2109
2110 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002111 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01002112 return 0;
2113 }
2114 }
2115
2116 }
2117
2118 return 1;
2119
2120}
2121
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002122
2123/*
2124 * boolean, returns true if response was built out of a cache entry.
2125 */
2126static int
2127smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2128 const char *kw, void *private)
2129{
2130 smp->data.type = SMP_T_BOOL;
2131 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2132
2133 return 1;
2134}
2135
2136/*
2137 * string, returns cache name (if response came from a cache).
2138 */
2139static int
2140smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2141 const char *kw, void *private)
2142{
2143 struct appctx *appctx = NULL;
2144
2145 struct cache_flt_conf *cconf = NULL;
2146 struct cache *cache = NULL;
2147
2148 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2149 return 0;
2150
2151 /* Get appctx from the stream_interface. */
2152 appctx = si_appctx(&smp->strm->si[1]);
2153 if (appctx && appctx->rule) {
2154 cconf = appctx->rule->arg.act.p[0];
2155 if (cconf) {
2156 cache = cconf->c.cache;
2157
2158 smp->data.type = SMP_T_STR;
2159 smp->flags = SMP_F_CONST;
2160 smp->data.u.str.area = cache->id;
2161 smp->data.u.str.data = strlen(cache->id);
2162 return 1;
2163 }
2164 }
2165
2166 return 0;
2167}
2168
Christopher Faulet99a17a22018-12-11 09:18:27 +01002169/* Declare the filter parser for "cache" keyword */
2170static struct flt_kw_list filter_kws = { "CACHE", { }, {
2171 { "cache", parse_cache_flt, NULL },
2172 { NULL, NULL, NULL },
2173 }
2174};
2175
2176INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2177
William Lallemand1f49a362017-11-21 20:01:26 +01002178static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01002179 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
2180 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002181}};
2182
Willy Tarreau0108d902018-11-25 19:14:37 +01002183INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002184
William Lallemand41db4602017-10-30 11:15:51 +01002185static struct action_kw_list http_res_actions = {
2186 .kw = {
2187 { "cache-store", parse_cache_store },
2188 { NULL, NULL }
2189 }
2190};
2191
Willy Tarreau0108d902018-11-25 19:14:37 +01002192INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2193
William Lallemand41db4602017-10-30 11:15:51 +01002194static struct action_kw_list http_req_actions = {
2195 .kw = {
2196 { "cache-use", parse_cache_use },
2197 { NULL, NULL }
2198 }
2199};
2200
Willy Tarreau0108d902018-11-25 19:14:37 +01002201INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2202
Willy Tarreau2231b632019-03-29 18:26:52 +01002203struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002204 .obj_type = OBJ_TYPE_APPLET,
2205 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002206 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002207 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002208};
2209
Willy Tarreaue6552512018-11-26 11:33:13 +01002210/* config parsers for this section */
2211REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002212REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002213
2214
2215/* Note: must not be declared <const> as its list will be overwritten */
2216static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2217 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2218 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2219 { /* END */ },
2220 }
2221};
2222
2223INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);