blob: 6489f168a5b748547ca1140e7c5bd58e42a9e11a [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 {
108 unsigned int latest_validation; /* latest validation date */
109 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200110 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100111
William Lallemand41db4602017-10-30 11:15:51 +0100112 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +0100113 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200114
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100115 char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */
116 unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used
117 * to build secondary keys for this cache entry. */
118
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200119 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
120 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
121
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200122 time_t last_modified; /* Origin server "Last-Modified" header value converted in
123 * seconds since epoch. If no "Last-Modified"
124 * header is found, use "Date" header value,
125 * otherwise use reception time. This field will
126 * be used in case of an "If-Modified-Since"-based
127 * conditional request. */
128
William Lallemand41db4602017-10-30 11:15:51 +0100129 unsigned char data[0];
130};
131
132#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +0100133#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100134
135static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +0200136static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +0100137static struct cache *tmp_cache_config = NULL;
138
Willy Tarreau8ceae722018-11-26 11:58:30 +0100139DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
140
William Lallemandf528fff2017-11-23 19:43:17 +0100141struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100142{
143 struct eb32_node *node;
144 struct cache_entry *entry;
145
Willy Tarreau8b507582020-02-25 09:35:07 +0100146 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100147 if (!node)
148 return NULL;
149
150 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100151
152 /* if that's not the right node */
153 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
154 return NULL;
155
William Lallemand08727662017-11-21 20:01:27 +0100156 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100157 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100158 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100159 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100160 entry->eb.key = 0;
161 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100162 return NULL;
163
164}
165
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100166/*
167 * There can be multiple entries with the same primary key in the ebtree so in
168 * order to get the proper one out of the list, we use a secondary_key.
169 * This function simply iterates over all the entries with the same primary_key
170 * until it finds the right one.
171 * Returns the cache_entry in case of success, NULL otherwise.
172 */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100173struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry,
174 char *secondary_key)
175{
176 struct eb32_node *node = &entry->eb;
177
178 if (!entry->secondary_key_signature)
179 return NULL;
180
181 while (entry && memcmp(entry->secondary_key, secondary_key, HTTP_CACHE_SEC_KEY_LEN) != 0) {
182 node = eb32_next_dup(node);
183 entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL;
184 }
185
186 /* Expired entry */
187 if (entry && entry->expire <= now.tv_sec) {
188 eb32_delete(&entry->eb);
189 entry->eb.key = 0;
190 entry = NULL;
191 }
192
193 return entry;
194}
195
William Lallemand4da3f8a2017-10-31 14:33:34 +0100196static inline struct shared_context *shctx_ptr(struct cache *cache)
197{
198 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
199}
200
William Lallemand77c11972017-10-31 20:43:01 +0100201static inline struct shared_block *block_ptr(struct cache_entry *entry)
202{
203 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
204}
205
206
207
William Lallemand41db4602017-10-30 11:15:51 +0100208static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100209cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100210{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100211 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100212 return 0;
213}
214
Christopher Faulet95220e22018-12-07 17:34:39 +0100215static void
216cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
217{
218 struct cache_flt_conf *cconf = fconf->conf;
219
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200220 if (!(cconf->flags & CACHE_FLT_INIT))
221 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100222 free(cconf);
223}
224
William Lallemand4da3f8a2017-10-31 14:33:34 +0100225static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100226cache_store_check(struct proxy *px, struct flt_conf *fconf)
227{
228 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100229 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100230 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100231 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100232
William Lallemandd1d1e222019-08-28 15:22:49 +0200233 /* Find the cache corresponding to the name in the filter config. The
234 * cache will not be referenced now in the filter config because it is
235 * not fully allocated. This step will be performed during the cache
236 * post_check.
237 */
238 list_for_each_entry(cache, &caches_config, list) {
239 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100240 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100241 }
242
243 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
244 proxy_type_str(px), px->id, (char *)cconf->c.name);
245 return 1;
246
247 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100248 /* Here <cache> points on the cache the filter must use and <cconf>
249 * points on the cache filter configuration. */
250
251 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100252 * enabled and if it is after the cache. When the compression is before
253 * the cache, an error is returned. Also check if the cache filter must
254 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100255 list_for_each_entry(f, &px->filter_configs, list) {
256 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100257 /* The compression filter must be evaluated after the cache. */
258 if (comp) {
259 ha_alert("config: %s '%s': unable to enable the compression filter before "
260 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
261 return 1;
262 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100263 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200264 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100265 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200266 else if (f->id == fcgi_flt_id)
267 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100268 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
269 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200270 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100271 * declaration is required. */
272 ha_alert("config: %s '%s': require an explicit filter declaration "
273 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
274 return 1;
275 }
276
Christopher Fauletafd819c2018-12-11 08:57:45 +0100277 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100278 return 0;
279}
280
281static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100282cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100283{
Christopher Faulet65554e12020-03-06 14:52:06 +0100284 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100285
Christopher Faulet65554e12020-03-06 14:52:06 +0100286 st = pool_alloc_dirty(pool_head_cache_st);
287 if (st == NULL)
288 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100289
Christopher Faulet65554e12020-03-06 14:52:06 +0100290 st->first_block = NULL;
291 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100292
Christopher Faulet65554e12020-03-06 14:52:06 +0100293 /* Register post-analyzer on AN_RES_WAIT_HTTP */
294 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100295 return 1;
296}
297
Christopher Faulet65554e12020-03-06 14:52:06 +0100298static void
299cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100300{
301 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100302 struct cache_flt_conf *cconf = FLT_CONF(filter);
303 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100304 struct shared_context *shctx = shctx_ptr(cache);
305
William Lallemand49dc0482017-11-24 14:33:54 +0100306 /* Everything should be released in the http_end filter, but we need to do it
307 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100308 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100309 shctx_lock(shctx);
310 shctx_row_dec_hot(shctx, st->first_block);
311 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100312 }
313 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100314 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100315 filter->ctx = NULL;
316 }
William Lallemand49dc0482017-11-24 14:33:54 +0100317}
318
Christopher Faulet839791a2019-01-07 16:12:07 +0100319static int
320cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
321 unsigned an_bit)
322{
323 struct http_txn *txn = s->txn;
324 struct http_msg *msg = &txn->rsp;
325 struct cache_st *st = filter->ctx;
326
327 if (an_bit != AN_RES_WAIT_HTTP)
328 goto end;
329
330 /* Here we need to check if any compression filter precedes the cache
331 * filter. This is only possible when the compression is configured in
332 * the frontend while the cache filter is configured on the
333 * backend. This case cannot be detected during HAProxy startup. So in
334 * such cases, the cache is disabled.
335 */
336 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
337 pool_free(pool_head_cache_st, st);
338 filter->ctx = NULL;
339 }
340
341 end:
342 return 1;
343}
William Lallemand49dc0482017-11-24 14:33:54 +0100344
345static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100346cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
347{
348 struct cache_st *st = filter->ctx;
349
William Lallemand4da3f8a2017-10-31 14:33:34 +0100350 if (!(msg->chn->flags & CF_ISRESP) || !st)
351 return 1;
352
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200353 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100354 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100355 return 1;
356}
357
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200358static inline void disable_cache_entry(struct cache_st *st,
359 struct filter *filter, struct shared_context *shctx)
360{
361 struct cache_entry *object;
362
363 object = (struct cache_entry *)st->first_block->data;
364 filter->ctx = NULL; /* disable cache */
365 shctx_lock(shctx);
366 shctx_row_dec_hot(shctx, st->first_block);
367 object->eb.key = 0;
368 shctx_unlock(shctx);
369 pool_free(pool_head_cache_st, st);
370}
371
William Lallemand4da3f8a2017-10-31 14:33:34 +0100372static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100373cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
374 unsigned int offset, unsigned int len)
375{
Christopher Faulet95220e22018-12-07 17:34:39 +0100376 struct cache_flt_conf *cconf = FLT_CONF(filter);
377 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100378 struct cache_st *st = filter->ctx;
379 struct htx *htx = htxbuf(&msg->chn->buf);
380 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200381 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100382 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200383 unsigned int orig_len, to_forward;
384 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100385
386 if (!len)
387 return len;
388
389 if (!st->first_block) {
390 unregister_data_filter(s, msg->chn, filter);
391 return len;
392 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100393
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200394 chunk_reset(&trash);
395 orig_len = len;
396 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100397
398 htxret = htx_find_offset(htx, offset);
399 blk = htxret.blk;
400 offset = htxret.ret;
401 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100402 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200403 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100404 struct ist v;
405
406 switch (type) {
407 case HTX_BLK_UNUSED:
408 break;
409
410 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100411 v = htx_get_blk_value(htx, blk);
412 v.ptr += offset;
413 v.len -= offset;
414 if (v.len > len)
415 v.len = len;
416
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200417 info = (type << 28) + v.len;
418 chunk_memcat(&trash, (char *)&info, sizeof(info));
419 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100420 to_forward += v.len;
421 len -= v.len;
422 break;
423
424 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200425 /* Here offset must always be 0 because only
426 * DATA blocks can be partially transferred. */
427 if (offset)
428 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100429 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200430 goto end;
431
432 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
433 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100434 to_forward += sz;
435 len -= sz;
436 break;
437 }
438
439 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100440 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200441
442 end:
443 shctx_lock(shctx);
444 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
445 if (!fb) {
446 shctx_unlock(shctx);
447 goto no_cache;
448 }
449 shctx_unlock(shctx);
450
451 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
452 (unsigned char *)b_head(&trash), b_data(&trash));
453 if (ret < 0)
454 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100455
456 return to_forward;
457
458 no_cache:
459 disable_cache_entry(st, filter, shctx);
460 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200461 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100462}
463
464static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100465cache_store_http_end(struct stream *s, struct filter *filter,
466 struct http_msg *msg)
467{
468 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100469 struct cache_flt_conf *cconf = FLT_CONF(filter);
470 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100471 struct shared_context *shctx = shctx_ptr(cache);
472 struct cache_entry *object;
473
474 if (!(msg->chn->flags & CF_ISRESP))
475 return 1;
476
477 if (st && st->first_block) {
478
479 object = (struct cache_entry *)st->first_block->data;
480
481 /* does not need to test if the insertion worked, if it
482 * doesn't, the blocks will be reused anyway */
483
484 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100485 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
486 object->eb.key = 0;
487 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100488 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100489 shctx_row_dec_hot(shctx, st->first_block);
490 shctx_unlock(shctx);
491
492 }
493 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100494 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100495 filter->ctx = NULL;
496 }
497
498 return 1;
499}
500
501 /*
502 * This intends to be used when checking HTTP headers for some
503 * word=value directive. Return a pointer to the first character of value, if
504 * the word was not found or if there wasn't any value assigned ot it return NULL
505 */
506char *directive_value(const char *sample, int slen, const char *word, int wlen)
507{
508 int st = 0;
509
510 if (slen < wlen)
511 return 0;
512
513 while (wlen) {
514 char c = *sample ^ *word;
515 if (c && c != ('A' ^ 'a'))
516 return NULL;
517 sample++;
518 word++;
519 slen--;
520 wlen--;
521 }
522
523 while (slen) {
524 if (st == 0) {
525 if (*sample != '=')
526 return NULL;
527 sample++;
528 slen--;
529 st = 1;
530 continue;
531 } else {
532 return (char *)sample;
533 }
534 }
535
536 return NULL;
537}
538
539/*
540 * Return the maxage in seconds of an HTTP response.
541 * Compute the maxage using either:
542 * - the assigned max-age of the cache
543 * - the s-maxage directive
544 * - the max-age directive
545 * - (Expires - Data) headers
546 * - the default-max-age of the cache
547 *
548 */
William Lallemand49b44532017-11-24 18:53:43 +0100549int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100550{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200551 struct htx *htx = htxbuf(&s->res.buf);
552 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100553 int smaxage = -1;
554 int maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100555 int expires = -1;
556 struct tm tm = {};
557 time_t expires_val = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100558
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200559 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
560 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100561
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200562 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
563 if (value) {
564 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100565
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200566 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
567 chunk_strncat(chk, "", 1);
Remi Tricot-Le Breton8c2db712020-10-30 14:26:13 +0100568 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100569 }
570
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200571 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
572 if (value) {
573 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200574
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200575 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
576 chunk_strncat(chk, "", 1);
Remi Tricot-Le Breton8c2db712020-10-30 14:26:13 +0100577 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100578 }
579 }
580
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100581 /* Look for Expires header if no s-maxage or max-age Cache-Control data
582 * was found. */
583 if (maxage == -1 && smaxage == -1) {
584 ctx.blk = NULL;
585 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
586 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
587 expires_val = my_timegm(&tm);
588 /* A request having an expiring date earlier
589 * than the current date should be considered as
590 * stale. */
591 expires = (expires_val >= now.tv_sec) ?
592 (expires_val - now.tv_sec) : 0;
593 }
594 else {
595 /* Following RFC 7234#5.3, an invalid date
596 * format must be treated as a date in the past
597 * so the cache entry must be seen as already
598 * expired. */
599 expires = 0;
600 }
601 }
602 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100603
604
605 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100606 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100607
608 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100609 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100610
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100611 if (expires >= 0)
612 return MIN(expires, cache->maxage);
613
William Lallemand49b44532017-11-24 18:53:43 +0100614 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100615
616}
617
618
William Lallemanda400a3a2017-11-20 19:13:12 +0100619static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
620{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200621 struct cache_entry *object = (struct cache_entry *)block->data;
622
623 if (first == block && object->eb.key)
624 eb32_delete(&object->eb);
625 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100626}
627
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200628
629/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
630 * date value should be compared to a date determined by in a previous response (for
631 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
632 * value of the response's reception time (by decreasing order of priority). */
633static time_t get_last_modified_time(struct htx *htx)
634{
635 time_t last_modified = 0;
636 struct http_hdr_ctx ctx = { .blk = NULL };
637 struct tm tm = {};
638
639 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
640 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
641 last_modified = my_timegm(&tm);
642 }
643 }
644
645 if (!last_modified) {
646 ctx.blk = NULL;
647 if (http_find_header(htx, ist("date"), &ctx, 1)) {
648 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
649 last_modified = my_timegm(&tm);
650 }
651 }
652 }
653
654 /* Fallback on the current time if no "Last-Modified" or "Date" header
655 * was found. */
656 if (!last_modified)
657 last_modified = now.tv_sec;
658
659 return last_modified;
660}
661
William Lallemand41db4602017-10-30 11:15:51 +0100662/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100663 * Checks the vary header's value. The headers on which vary should be applied
664 * must be explicitely supported in the vary_information array (see cache.c). If
665 * any other header is mentioned, we won't store the response.
666 * Returns 1 if Vary-based storage can work, 0 otherwise.
667 */
668static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
669{
670 unsigned int vary_idx;
671 unsigned int vary_info_count;
672 const struct vary_hashing_information *vary_info;
673 struct http_hdr_ctx ctx = { .blk = NULL };
674
675 int retval = 1;
676
677 *vary_signature = 0;
678
679 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
680 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
681 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
682 vary_info = &vary_information[vary_idx];
683 if (isteqi(ctx.value, vary_info->hdr_name)) {
684 *vary_signature |= vary_info->value;
685 break;
686 }
687 }
688 retval = (vary_idx < vary_info_count);
689 }
690
691 return retval;
692}
693
694
695
696/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500697 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100698 * register a filter to store the data
699 */
700enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200701 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100702{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200703 unsigned int age;
704 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100705 struct http_txn *txn = s->txn;
706 struct http_msg *msg = &txn->rsp;
707 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100708 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100709 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100710 struct cache *cache = cconf->c.cache;
711 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100712 struct cache_st *cache_ctx = NULL;
713 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100714 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200715 struct htx *htx;
716 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200717 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200718 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200719 unsigned int etag_length = 0;
720 unsigned int etag_offset = 0;
721 struct ist header_name = IST_NULL;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200722 time_t last_modified = 0;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100723 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100724
William Lallemand4da3f8a2017-10-31 14:33:34 +0100725 /* Don't cache if the response came from a cache */
726 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
727 s->target == &http_cache_applet.obj_type) {
728 goto out;
729 }
730
731 /* cache only HTTP/1.1 */
732 if (!(txn->req.flags & HTTP_MSGF_VER_11))
733 goto out;
734
Willy Tarreau6905d182019-10-01 17:59:17 +0200735 /* cache only GET method */
736 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100737 goto out;
738
Willy Tarreauc9036c02019-01-11 19:38:25 +0100739 /* cache key was not computed */
740 if (!key)
741 goto out;
742
William Lallemand4da3f8a2017-10-31 14:33:34 +0100743 /* cache only 200 status code */
744 if (txn->status != 200)
745 goto out;
746
Christopher Faulet839791a2019-01-07 16:12:07 +0100747 /* Find the corresponding filter instance for the current stream */
748 list_for_each_entry(filter, &s->strm_flt.filters, list) {
749 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
750 /* No filter ctx, don't cache anything */
751 if (!filter->ctx)
752 goto out;
753 cache_ctx = filter->ctx;
754 break;
755 }
756 }
757
758 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200759 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100760
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200761 /* Do not cache too big objects. */
762 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
763 htx->data + htx->extra > shctx->max_obj_size)
764 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100765
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100766 /* Only a subset of headers are supported in our Vary implementation. If
767 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100768 * able to use the cache. Likewise, if Vary header support is disabled,
769 * avoid caching responses that contain such a header. */
770 ctx.blk = NULL;
771 if (cache->vary_processing_enabled) {
772 if (!http_check_vary_header(htx, &vary_signature))
773 goto out;
774 }
775 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200776 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100777 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100778
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200779 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100780
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100781 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200782 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100783
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200784 age = 0;
785 ctx.blk = NULL;
786 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
787 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
788 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
789 hdr_age = CACHE_ENTRY_MAX_AGE;
790 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100791 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200792 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100793 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100794
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200795 /* Build a last-modified time that will be stored in the cache_entry and
796 * compared to a future If-Modified-Since client header. */
797 last_modified = get_last_modified_time(htx);
798
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200799 chunk_reset(&trash);
800 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
801 struct htx_blk *blk = htx_get_blk(htx, pos);
802 enum htx_blk_type type = htx_get_blk_type(blk);
803 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100804
Christopher Fauletb0667472019-09-03 22:22:12 +0200805 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200806 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
807 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200808
809 /* Look for optional ETag header.
810 * We need to store the offset of the ETag value in order for
811 * future conditional requests to be able to perform ETag
812 * comparisons. */
813 if (type == HTX_BLK_HDR) {
814 header_name = htx_get_blk_name(htx, blk);
815 if (isteq(header_name, ist("etag"))) {
816 etag_length = sz - istlen(header_name);
817 etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
818 }
819 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200820 if (type == HTX_BLK_EOH)
821 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200822 }
823
Christopher Fauletb0667472019-09-03 22:22:12 +0200824 /* Do not cache objects if the headers are too big. */
825 if (hdrs_len > htx->size - global.tune.maxrewrite)
826 goto out;
827
William Lallemand4da3f8a2017-10-31 14:33:34 +0100828 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200829 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100830 if (!first) {
831 shctx_unlock(shctx);
832 goto out;
833 }
834 shctx_unlock(shctx);
835
Willy Tarreau1093a452018-04-06 19:02:25 +0200836 /* the received memory is not initialized, we need at least to mark
837 * the object as not indexed yet.
838 */
839 object = (struct cache_entry *)first->data;
840 object->eb.node.leaf_p = NULL;
841 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200842 object->age = age;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200843 object->last_modified = last_modified;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100844 object->secondary_key_signature = vary_signature;
Willy Tarreau1093a452018-04-06 19:02:25 +0200845
William Lallemand4da3f8a2017-10-31 14:33:34 +0100846 /* reserve space for the cache_entry structure */
847 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200848 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100849 /* cache the headers in a http action because it allows to chose what
850 * to cache, for example you might want to cache a response before
851 * modifying some HTTP headers, or on the contrary after modifying
852 * those headers.
853 */
854
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200855 /* Write the ETag information in the cache_entry if needed. */
856 object->etag_length = etag_length;
857 object->etag_offset = etag_offset;
858
William Lallemand4da3f8a2017-10-31 14:33:34 +0100859 /* does not need to be locked because it's in the "hot" list,
860 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200861 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
862 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100863
864 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100865 if (cache_ctx) {
866 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100867
Willy Tarreauc9036c02019-01-11 19:38:25 +0100868 object->eb.key = key;
869
Christopher Faulet839791a2019-01-07 16:12:07 +0100870 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100871
872 /* Add the current request's secondary key to the buffer if needed. */
873 if (vary_signature) {
874 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
875 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
876 }
877
Christopher Faulet839791a2019-01-07 16:12:07 +0100878 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100879
Christopher Faulet839791a2019-01-07 16:12:07 +0100880 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100881
Christopher Faulet839791a2019-01-07 16:12:07 +0100882 old = entry_exist(cconf->c.cache, txn->cache_hash);
883 if (old) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100884 if (vary_signature)
885 old = secondary_entry_exist(cconf->c.cache, old,
886 txn->cache_secondary_hash);
887
888 if (old) {
889 eb32_delete(&old->eb);
890 old->eb.key = 0;
891 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100892 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100893 shctx_unlock(shctx);
894
895 /* store latest value and expiration time */
896 object->latest_validation = now.tv_sec;
897 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
898 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100899 }
900
901out:
902 /* if does not cache */
903 if (first) {
904 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100905 first->len = 0;
906 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100907 shctx_row_dec_hot(shctx, first);
908 shctx_unlock(shctx);
909 }
910
William Lallemand41db4602017-10-30 11:15:51 +0100911 return ACT_RET_CONT;
912}
913
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100914#define HTX_CACHE_INIT 0 /* Initial state. */
915#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
916#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200917#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
918#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100919
William Lallemandecb73b12017-11-24 14:33:55 +0100920static void http_cache_applet_release(struct appctx *appctx)
921{
Christopher Faulet95220e22018-12-07 17:34:39 +0100922 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100923 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100924 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100925 struct shared_block *first = block_ptr(cache_ptr);
926
927 shctx_lock(shctx_ptr(cache));
928 shctx_row_dec_hot(shctx_ptr(cache), first);
929 shctx_unlock(shctx_ptr(cache));
930}
931
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200932
933static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
934 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100935{
Christopher Faulet95220e22018-12-07 17:34:39 +0100936 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
937 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200938 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200939 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200940 unsigned int max, total;
941 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100942
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200943 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
944 if (!max)
945 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200946 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200947 ? (info & 0xff) + ((info >> 8) & 0xfffff)
948 : info & 0xfffffff);
949 if (blksz > max)
950 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100951
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200952 blk = htx_add_blk(htx, type, blksz);
953 if (!blk)
954 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100955
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200956 blk->info = info;
957 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200958 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200959 while (blksz) {
960 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200961 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200962 offset += max;
963 blksz -= max;
964 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200965 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200966 if (blksz || offset == shctx->block_size) {
967 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
968 offset = 0;
969 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100970 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200971 appctx->ctx.cache.offset = offset;
972 appctx->ctx.cache.next = shblk;
973 appctx->ctx.cache.sent += total;
974 return total;
975}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100976
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200977static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
978 uint32_t info, struct shared_block *shblk, unsigned int offset)
979{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100980
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200981 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
982 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
983 unsigned int max, total, rem_data;
984 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100985
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200986 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
987 if (!max)
988 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100989
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200990 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200991 if (appctx->ctx.cache.rem_data) {
992 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200993 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200994 }
995 else {
996 blksz = (info & 0xfffffff);
997 total = 4;
998 }
999 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001000 rem_data = blksz - max;
1001 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001002 }
1003
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001004 while (blksz) {
1005 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001006
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001007 max = MIN(blksz, shctx->block_size - offset);
1008 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1009 offset += sz;
1010 blksz -= sz;
1011 total += sz;
1012 if (sz < max)
1013 break;
1014 if (blksz || offset == shctx->block_size) {
1015 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1016 offset = 0;
1017 }
1018 }
1019
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001020 appctx->ctx.cache.offset = offset;
1021 appctx->ctx.cache.next = shblk;
1022 appctx->ctx.cache.sent += total;
1023 appctx->ctx.cache.rem_data = rem_data + blksz;
1024 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001025}
1026
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001027static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1028 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001029{
Christopher Faulet95220e22018-12-07 17:34:39 +01001030 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1031 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001032 struct shared_block *shblk;
1033 unsigned int offset, sz;
1034 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001035
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001036 while (len) {
1037 enum htx_blk_type type;
1038 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001039
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001040 shblk = appctx->ctx.cache.next;
1041 offset = appctx->ctx.cache.offset;
1042 if (appctx->ctx.cache.rem_data) {
1043 type = HTX_BLK_DATA;
1044 info = 0;
1045 goto add_data_blk;
1046 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001047
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001048 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001049 sz = MIN(4, shctx->block_size - offset);
1050 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1051 offset += sz;
1052 if (sz < 4) {
1053 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1054 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1055 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001056 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001057
1058 /* Get payload of the next HTX block and insert it. */
1059 type = (info >> 28);
1060 if (type != HTX_BLK_DATA)
1061 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1062 else {
1063 add_data_blk:
1064 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001065 }
1066
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001067 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001068 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001069 total += ret;
1070 len -= ret;
1071
1072 if (appctx->ctx.cache.rem_data || type == mark)
1073 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001074 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001075
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001076 return total;
1077}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001078
1079static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1080{
1081 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1082 unsigned int age;
1083 char *end;
1084
1085 chunk_reset(&trash);
1086 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1087 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1088 age = CACHE_ENTRY_MAX_AGE;
1089 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1090 b_set_data(&trash, end - b_head(&trash));
1091 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1092 return 0;
1093 return 1;
1094}
1095
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001096static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001097{
1098 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1099 struct shared_block *first = block_ptr(cache_ptr);
1100 struct stream_interface *si = appctx->owner;
1101 struct channel *req = si_oc(si);
1102 struct channel *res = si_ic(si);
1103 struct htx *req_htx, *res_htx;
1104 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001105 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001106 size_t ret, total = 0;
1107
1108 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001109 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001110
1111 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1112 goto out;
1113
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001114 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001115 if (!b_size(&res->buf)) {
1116 si_rx_room_blk(si);
1117 goto out;
1118 }
1119
Willy Tarreauefef3232018-12-16 00:37:45 +01001120 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001121 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001122
1123 if (appctx->st0 == HTX_CACHE_INIT) {
1124 appctx->ctx.cache.next = block_ptr(cache_ptr);
1125 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1126 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001127 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001128 appctx->st0 = HTX_CACHE_HEADER;
1129 }
1130
1131 if (appctx->st0 == HTX_CACHE_HEADER) {
1132 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001133 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1134 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1135 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1136 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001137 goto error;
1138
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001139 /* In case of a conditional request, we might want to send a
1140 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001141 if (appctx->ctx.cache.send_notmodified) {
1142 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1143 /* If replacing the status code fails we need to send the full response. */
1144 appctx->ctx.cache.send_notmodified = 0;
1145 }
1146 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001147
1148 /* Skip response body for HEAD requests or in case of "304 Not
1149 * Modified" response. */
1150 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001151 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001152 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001153 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001154 }
1155
1156 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001157 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1158 if (len) {
1159 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1160 if (ret < len) {
1161 si_rx_room_blk(si);
1162 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001163 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001164 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001165 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001166 }
1167
1168 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001169 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001170 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1171 si_rx_room_blk(si);
1172 goto out;
1173 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001174 appctx->st0 = HTX_CACHE_END;
1175 }
1176
1177 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001178 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001179 res->flags |= CF_READ_NULL;
1180 si_shutr(si);
1181 }
1182
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001183 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001184 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001185 if (total)
1186 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001187 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001188
1189 /* eat the whole request */
1190 if (co_data(req)) {
1191 req_htx = htx_from_buf(&req->buf);
1192 co_htx_skip(req, req_htx, co_data(req));
1193 htx_to_buf(req_htx, &req->buf);
1194 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001195 return;
1196
1197 error:
1198 /* Sent and HTTP error 500 */
1199 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001200 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001201 res->buf.data = b_data(errmsg);
1202 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1203 res_htx = htx_from_buf(&res->buf);
1204
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001205 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001206 appctx->st0 = HTX_CACHE_END;
1207 goto end;
1208}
1209
1210
Christopher Faulet95220e22018-12-07 17:34:39 +01001211static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001212{
1213 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001214 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001215
Christopher Faulet95220e22018-12-07 17:34:39 +01001216 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001217 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001218 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001219 }
1220
1221 /* check if a cache filter was already registered with this cache
1222 * name, if that's the case, must use it. */
1223 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001224 if (fconf->id == cache_store_flt_id) {
1225 cconf = fconf->conf;
1226 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1227 rule->arg.act.p[0] = cconf;
1228 return 1;
1229 }
William Lallemand41db4602017-10-30 11:15:51 +01001230 }
1231 }
1232
Christopher Faulet95220e22018-12-07 17:34:39 +01001233 /* Create the filter cache config */
1234 cconf = calloc(1, sizeof(*cconf));
1235 if (!cconf) {
1236 memprintf(err, "out of memory\n");
1237 goto err;
1238 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001239 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001240 cconf->c.name = strdup(name);
1241 if (!cconf->c.name) {
1242 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001243 goto err;
1244 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001245
William Lallemand41db4602017-10-30 11:15:51 +01001246 /* register a filter to fill the cache buffer */
1247 fconf = calloc(1, sizeof(*fconf));
1248 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001249 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001250 goto err;
1251 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001252 fconf->id = cache_store_flt_id;
1253 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001254 fconf->ops = &cache_ops;
1255 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1256
Christopher Faulet95220e22018-12-07 17:34:39 +01001257 rule->arg.act.p[0] = cconf;
1258 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001259
Christopher Faulet95220e22018-12-07 17:34:39 +01001260 err:
1261 free(cconf);
1262 return 0;
1263}
1264
1265enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1266 struct act_rule *rule, char **err)
1267{
1268 rule->action = ACT_CUSTOM;
1269 rule->action_ptr = http_action_store_cache;
1270
1271 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1272 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001273
Christopher Faulet95220e22018-12-07 17:34:39 +01001274 (*orig_arg)++;
1275 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001276}
1277
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001278/* This produces a sha1 hash of the concatenation of the HTTP method,
1279 * the first occurrence of the Host header followed by the path component
1280 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001281int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001282{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001283 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001284 struct htx *htx = htxbuf(&s->req.buf);
1285 struct htx_sl *sl;
1286 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001287 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001288 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001289 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001290
William Lallemandf528fff2017-11-23 19:43:17 +01001291 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001292 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001293
1294 switch (txn->meth) {
1295 case HTTP_METH_HEAD:
1296 case HTTP_METH_GET:
1297 chunk_memcat(trash, "GET", 3);
1298 break;
1299 default:
1300 return 0;
1301 }
1302
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001303 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001304 uri = htx_sl_req_uri(sl); // whole uri
1305 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001306 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001307
1308 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1309 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1310 * URIs are almost always sent in absolute form with their scheme. In
1311 * this case, the scheme is almost always "https". In order to support
1312 * sharing of cache objects between H1 and H2, we'll hash the absolute
1313 * URI whenever known, or prepend "https://" + the Host header for
1314 * relative URIs. The difference will only appear on absolute HTTP/1
1315 * requests sent to an origin server, which practically is never met in
1316 * the real world so we don't care about the ability to share the same
1317 * key here.URIs are normalized from the absolute URI to an origin form as
1318 * well.
1319 */
1320 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001321 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001322 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1323 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001324 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001325 }
1326
1327 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001328
1329 /* hash everything */
1330 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001331 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001332 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1333
1334 return 1;
1335}
1336
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001337/* Looks for "If-None-Match" headers in the request and compares their value
1338 * with the one that might have been stored in the cache_entry. If any of them
1339 * matches, a "304 Not Modified" response should be sent instead of the cached
1340 * data.
1341 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001342 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1343 *
1344 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1345 * header and compare its value (date) to the one stored in the cache_entry.
1346 * If the request's date is later than the cached one, we also send a
1347 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1348 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001349 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1350 */
1351static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1352 struct cache_entry *entry)
1353{
1354 int retval = 0;
1355
1356 struct http_hdr_ctx ctx = { .blk = NULL };
1357 struct ist cache_entry_etag = IST_NULL;
1358 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001359 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001360
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001361 struct tm tm = {};
1362 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001363
1364 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001365 * cache_entry's ETag in order to perform comparisons.
1366 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001367 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001368 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001369
1370 /* A '*' matches everything. */
1371 if (isteq(ctx.value, ist("*")) != 0) {
1372 retval = 1;
1373 break;
1374 }
1375
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001376 /* No need to rebuild an etag if none was stored in the cache. */
1377 if (entry->etag_length == 0)
1378 break;
1379
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001380 /* Rebuild the stored ETag. */
1381 if (etag_buffer == NULL) {
1382 etag_buffer = get_trash_chunk();
1383
1384 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1385 (unsigned char*)b_orig(etag_buffer),
1386 entry->etag_offset, entry->etag_length) == 0) {
1387 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1388 } else {
1389 /* We could not rebuild the ETag in one go, we
1390 * won't send a "304 Not Modified" response. */
1391 break;
1392 }
1393 }
1394
1395 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1396 retval = 1;
1397 break;
1398 }
1399 }
1400
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001401 /* If the request did not contain an "If-None-Match" header, we look for
1402 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1403 if (retval == 0 && if_none_match_found == 0) {
1404 ctx.blk = NULL;
1405 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1406 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1407 if_modified_since = my_timegm(&tm);
1408
1409 /* We send a "304 Not Modified" response if the
1410 * entry's last modified date is earlier than
1411 * the one found in the "If-Modified-Since"
1412 * header. */
1413 retval = (entry->last_modified <= if_modified_since);
1414 }
1415 }
1416 }
1417
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001418 return retval;
1419}
1420
William Lallemand41db4602017-10-30 11:15:51 +01001421enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1422 struct session *sess, struct stream *s, int flags)
1423{
William Lallemand77c11972017-10-31 20:43:01 +01001424
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001425 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001426 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001427 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1428 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001429 struct shared_block *entry_block;
1430
William Lallemand77c11972017-10-31 20:43:01 +01001431
Willy Tarreau6905d182019-10-01 17:59:17 +02001432 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1433 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001434 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001435 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001436 txn->flags |= TX_CACHE_IGNORE;
1437
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001438 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001439
Willy Tarreau504455c2017-12-22 17:47:35 +01001440 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1441 return ACT_RET_CONT;
1442
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001443 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001444 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001445
Willy Tarreau504455c2017-12-22 17:47:35 +01001446 if (s->txn->flags & TX_CACHE_IGNORE)
1447 return ACT_RET_CONT;
1448
Willy Tarreaua1214a52018-12-14 14:00:25 +01001449 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001450 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001451 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001452 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001453
William Lallemanda400a3a2017-11-20 19:13:12 +01001454 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001455 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001456 if (res) {
1457 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001458 entry_block = block_ptr(res);
1459 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001460 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001461
1462 /* In case of Vary, we could have multiple entries with the same
1463 * primary hash. We need to calculate the secondary has in order
1464 * to find the actual entry we want (if it exists). */
1465 if (res->secondary_key_signature) {
1466 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1467 shctx_lock(shctx_ptr(cache));
1468 sec_entry = secondary_entry_exist(cache, res,
1469 s->txn->cache_secondary_hash);
1470 if (sec_entry && sec_entry != res) {
1471 /* The wrong row was added to the hot list. */
1472 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1473 entry_block = block_ptr(sec_entry);
1474 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1475 }
1476 res = sec_entry;
1477 shctx_unlock(shctx_ptr(cache));
1478 }
1479 else
1480 res = NULL;
1481 }
1482
1483 /* We looked for a valid secondary entry and could not find one,
1484 * the request must be forwarded to the server. */
1485 if (!res) {
1486 shctx_lock(shctx_ptr(cache));
1487 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1488 shctx_unlock(shctx_ptr(cache));
1489 return ACT_RET_CONT;
1490 }
1491
William Lallemand77c11972017-10-31 20:43:01 +01001492 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001493 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001494 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001495 appctx->rule = rule;
1496 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001497 appctx->ctx.cache.next = NULL;
1498 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001499 appctx->ctx.cache.send_notmodified =
1500 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001501
1502 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001503 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001504 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001505 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001506 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001507 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001508 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001509 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001510 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001511 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001512 }
1513 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001514 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001515
1516 /* Shared context does not need to be locked while we calculate the
1517 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001518 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001519 /* Build a complete secondary hash until the server response
1520 * tells us which fields should be kept (if any). */
1521 http_request_prebuild_full_secondary_key(s);
1522 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001523 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001524}
1525
1526
1527enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1528 struct act_rule *rule, char **err)
1529{
William Lallemand41db4602017-10-30 11:15:51 +01001530 rule->action = ACT_CUSTOM;
1531 rule->action_ptr = http_action_req_cache_use;
1532
Christopher Faulet95220e22018-12-07 17:34:39 +01001533 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001534 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001535
1536 (*orig_arg)++;
1537 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001538}
1539
1540int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1541{
1542 int err_code = 0;
1543
1544 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1545
1546 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001547 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001548 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001549 err_code |= ERR_ALERT | ERR_ABORT;
1550 goto out;
1551 }
1552
1553 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1554 err_code |= ERR_ABORT;
1555 goto out;
1556 }
1557
1558 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001559 struct cache *cache_config;
1560
William Lallemand41db4602017-10-30 11:15:51 +01001561 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1562 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001563 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001564 err_code |= ERR_ALERT | ERR_ABORT;
1565 goto out;
1566 }
1567
1568 strlcpy2(tmp_cache_config->id, args[1], 33);
1569 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001570 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001571 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001572 err_code |= ERR_WARN;
1573 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001574
1575 list_for_each_entry(cache_config, &caches_config, list) {
1576 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1577 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1578 file, linenum, tmp_cache_config->id);
1579 err_code |= ERR_ALERT | ERR_ABORT;
1580 goto out;
1581 }
1582 }
1583
William Lallemand49b44532017-11-24 18:53:43 +01001584 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001585 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001586 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001587 }
1588 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001589 unsigned long int maxsize;
1590 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001591
1592 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1593 err_code |= ERR_ABORT;
1594 goto out;
1595 }
1596
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001597 maxsize = strtoul(args[1], &err, 10);
1598 if (err == args[1] || *err != '\0') {
1599 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1600 file, linenum, args[1]);
1601 err_code |= ERR_ABORT;
1602 goto out;
1603 }
1604
1605 if (maxsize > (UINT_MAX >> 20)) {
1606 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1607 file, linenum, args[1], UINT_MAX >> 20);
1608 err_code |= ERR_ABORT;
1609 goto out;
1610 }
1611
William Lallemand41db4602017-10-30 11:15:51 +01001612 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001613 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001614 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001615 } else if (strcmp(args[0], "max-age") == 0) {
1616 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1617 err_code |= ERR_ABORT;
1618 goto out;
1619 }
1620
1621 if (!*args[1]) {
1622 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1623 file, linenum, args[0]);
1624 err_code |= ERR_WARN;
1625 }
1626
1627 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001628 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001629 unsigned int maxobjsz;
1630 char *err;
1631
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001632 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1633 err_code |= ERR_ABORT;
1634 goto out;
1635 }
1636
1637 if (!*args[1]) {
1638 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1639 file, linenum, args[0]);
1640 err_code |= ERR_WARN;
1641 }
1642
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001643 maxobjsz = strtoul(args[1], &err, 10);
1644 if (err == args[1] || *err != '\0') {
1645 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1646 file, linenum, args[1]);
1647 err_code |= ERR_ABORT;
1648 goto out;
1649 }
1650 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001651 } else if (strcmp(args[0], "process-vary") == 0) {
1652 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1653 err_code |= ERR_ABORT;
1654 goto out;
1655 }
1656
1657 if (!*args[1]) {
1658 ha_warning("parsing [%s:%d]: '%s' expects 0 or 1 (disable or enable vary processing).\n",
1659 file, linenum, args[0]);
1660 err_code |= ERR_WARN;
1661 }
1662
1663 tmp_cache_config->vary_processing_enabled = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001664 }
1665 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001666 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001667 err_code |= ERR_ALERT | ERR_FATAL;
1668 goto out;
1669 }
1670out:
1671 return err_code;
1672}
1673
1674/* once the cache section is parsed */
1675
1676int cfg_post_parse_section_cache()
1677{
William Lallemand41db4602017-10-30 11:15:51 +01001678 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001679
1680 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001681
1682 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001683 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001684 err_code |= ERR_FATAL | ERR_ALERT;
1685 goto out;
1686 }
1687
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001688 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001689 /* Default max. file size is a 256th of the cache size. */
1690 tmp_cache_config->maxobjsz =
1691 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001692 }
1693 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1694 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1695 err_code |= ERR_FATAL | ERR_ALERT;
1696 goto out;
1697 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001698
William Lallemandd1d1e222019-08-28 15:22:49 +02001699 /* add to the list of cache to init and reinit tmp_cache_config
1700 * for next cache section, if any.
1701 */
1702 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1703 tmp_cache_config = NULL;
1704 return err_code;
1705 }
1706out:
1707 free(tmp_cache_config);
1708 tmp_cache_config = NULL;
1709 return err_code;
1710
1711}
1712
1713int post_check_cache()
1714{
1715 struct proxy *px;
1716 struct cache *back, *cache_config, *cache;
1717 struct shared_context *shctx;
1718 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001719 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02001720
1721 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1722
1723 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1724 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001725
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001726 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001727 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001728 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001729 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001730 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001731
1732 err_code |= ERR_FATAL | ERR_ALERT;
1733 goto out;
1734 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001735 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001736 /* the cache structure is stored in the shctx and added to the
1737 * caches list, we can remove the entry from the caches_config
1738 * list */
1739 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001740 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001741 cache->entries = EB_ROOT;
William Lallemand41db4602017-10-30 11:15:51 +01001742 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001743 LIST_DEL(&cache_config->list);
1744 free(cache_config);
1745
1746 /* Find all references for this cache in the existing filters
1747 * (over all proxies) and reference it in matching filters.
1748 */
1749 for (px = proxies_list; px; px = px->next) {
1750 struct flt_conf *fconf;
1751 struct cache_flt_conf *cconf;
1752
1753 list_for_each_entry(fconf, &px->filter_configs, list) {
1754 if (fconf->id != cache_store_flt_id)
1755 continue;
1756
1757 cconf = fconf->conf;
1758 if (!strcmp(cache->id, cconf->c.name)) {
1759 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001760 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001761 cconf->c.cache = cache;
1762 break;
1763 }
1764 }
1765 }
William Lallemand41db4602017-10-30 11:15:51 +01001766 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001767
William Lallemand41db4602017-10-30 11:15:51 +01001768out:
William Lallemand41db4602017-10-30 11:15:51 +01001769 return err_code;
1770
William Lallemand41db4602017-10-30 11:15:51 +01001771}
1772
William Lallemand41db4602017-10-30 11:15:51 +01001773struct flt_ops cache_ops = {
1774 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001775 .check = cache_store_check,
1776 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001777
Christopher Faulet65554e12020-03-06 14:52:06 +01001778 /* Handle stream init/deinit */
1779 .attach = cache_store_strm_init,
1780 .detach = cache_store_strm_deinit,
1781
William Lallemand4da3f8a2017-10-31 14:33:34 +01001782 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001783 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001784
1785 /* Filter HTTP requests and responses */
1786 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001787 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001788 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001789};
1790
Christopher Faulet99a17a22018-12-11 09:18:27 +01001791
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001792int accept_encoding_cmp(const void *a, const void *b)
1793{
1794 const struct ist ist_a = *(const struct ist*)a;
1795 const struct ist ist_b = *(const struct ist*)b;
1796
1797 return istdiff(ist_a, ist_b);
1798}
1799
Tim Duesterhus23b29452020-11-24 22:22:56 +01001800#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001801/*
1802 * Build a hash of the accept-encoding header. The different parts of the
1803 * header value are first sorted, appended and then a crc is calculated
1804 * for the newly constructed buffer.
1805 * Returns 0 in case of success.
1806 */
1807static int accept_encoding_normalizer(struct ist value, char *buf, unsigned int *buf_len)
1808{
Tim Duesterhus23b29452020-11-24 22:22:56 +01001809 struct ist values[ACCEPT_ENCODING_MAX_ENTRIES] = {{}};
1810 size_t count = 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001811 char *comma = NULL;
1812 struct buffer *trash = get_trash_chunk();
1813 int hash_value = 0;
1814
1815 /* The hash will be built out of a sorted list of accepted encodings. */
Tim Duesterhus23b29452020-11-24 22:22:56 +01001816 while (count < (ACCEPT_ENCODING_MAX_ENTRIES - 1) && (comma = istchr(value, ',')) != NULL) {
1817 size_t length = comma - istptr(value);
1818
1819 values[count++] = isttrim(value, length);
1820 value = istadv(value, length + 1);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001821 }
1822 values[count++] = value;
1823
Tim Duesterhus23b29452020-11-24 22:22:56 +01001824 if (count == ACCEPT_ENCODING_MAX_ENTRIES)
1825 return 1;
1826
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001827 /* Sort the values alphabetically. */
1828 qsort(values, count, sizeof(struct ist), &accept_encoding_cmp);
1829
1830 while (count)
1831 chunk_istcat(trash, values[--count]);
1832
1833 hash_value = hash_crc32(b_orig(trash), b_data(trash));
1834
1835 memcpy(buf, &hash_value, sizeof(hash_value));
1836 *buf_len = sizeof(hash_value);
1837
Tim Duesterhus23b29452020-11-24 22:22:56 +01001838 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001839}
Tim Duesterhus23b29452020-11-24 22:22:56 +01001840#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001841
1842/*
1843 * Normalizer used by default for User-Agent and Referer headers. It only
1844 * calculates a simple crc of the whole value.
1845 * Returns 0 in case of success.
1846 */
1847static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len)
1848{
1849 int hash_value = 0;
1850
1851 hash_value = hash_crc32(istptr(value), istlen(value));
1852
1853 memcpy(buf, &hash_value, sizeof(hash_value));
1854 *buf_len = sizeof(hash_value);
1855
1856 return 0;
1857}
1858
1859
1860/*
1861 * Pre-calculate the hashes of all the supported headers (in our Vary
1862 * implementation) of a given request. We have to calculate all the hashes
1863 * in advance because the actual Vary signature won't be known until the first
1864 * response.
1865 * Only the first occurrence of every header will be taken into account in the
1866 * hash.
1867 * If the header is not present, the hash portion of the given header will be
1868 * filled with zeros.
1869 * Returns 0 in case of success.
1870 */
1871static int http_request_prebuild_full_secondary_key(struct stream *s)
1872{
1873 struct http_txn *txn = s->txn;
1874 struct htx *htx = htxbuf(&s->req.buf);
1875 struct http_hdr_ctx ctx = { .blk = NULL };
1876
1877 unsigned int idx;
1878 const struct vary_hashing_information *info = NULL;
1879 unsigned int hash_length = 0;
1880 int retval = 0;
1881 int offset = 0;
1882
1883 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1884 info = &vary_information[idx];
1885
1886 ctx.blk = NULL;
1887 if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) {
1888 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1889 offset += hash_length;
1890 }
1891 else {
1892 /* Fill hash with 0s. */
1893 hash_length = info->hash_length;
1894 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1895 offset += hash_length;
1896 }
1897 }
1898
1899 return retval;
1900}
1901
1902
1903/*
1904 * Calculate the secondary key for a request for which we already have a known
1905 * vary signature. The key is made by aggregating hashes calculated for every
1906 * header mentioned in the vary signature.
1907 * Only the first occurrence of every header will be taken into account in the
1908 * hash.
1909 * If the header is not present, the hash portion of the given header will be
1910 * filled with zeros.
1911 * Returns 0 in case of success.
1912 */
1913static int http_request_build_secondary_key(struct stream *s, int vary_signature)
1914{
1915 struct http_txn *txn = s->txn;
1916 struct htx *htx = htxbuf(&s->req.buf);
1917 struct http_hdr_ctx ctx = { .blk = NULL };
1918
1919 unsigned int idx;
1920 const struct vary_hashing_information *info = NULL;
1921 unsigned int hash_length = 0;
1922 int retval = 0;
1923 int offset = 0;
1924
1925 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
1926 info = &vary_information[idx];
1927
1928 ctx.blk = NULL;
1929 if ((vary_signature & info->value) && info->norm_fn != NULL &&
1930 http_find_header(htx, info->hdr_name, &ctx, 1)) {
1931 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
1932 offset += hash_length;
1933 }
1934 else {
1935 /* Fill hash with 0s. */
1936 hash_length = info->hash_length;
1937 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
1938 offset += hash_length;
1939 }
1940 }
1941
1942 return retval;
1943}
1944
1945/*
1946 * Build the actual secondary key of a given request out of the prebuilt key and
1947 * the actual vary signature (extracted from the response).
1948 * Returns 0 in case of success.
1949 */
1950static int http_request_reduce_secondary_key(unsigned int vary_signature,
1951 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
1952{
1953 int offset = 0;
1954 int global_offset = 0;
1955 int vary_info_count = 0;
1956 int keep = 0;
1957 unsigned int vary_idx;
1958 const struct vary_hashing_information *vary_info;
1959
1960 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
1961 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
1962 vary_info = &vary_information[vary_idx];
1963 keep = (vary_signature & vary_info->value) ? 0xff : 0;
1964
1965 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
1966 prebuilt_key[global_offset] &= keep;
1967 }
1968 }
1969
1970 return 0;
1971}
1972
1973
Christopher Faulet99a17a22018-12-11 09:18:27 +01001974
1975static int
1976parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1977 struct flt_conf *fconf, char **err, void *private)
1978{
1979 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001980 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001981 char *name = NULL;
1982 int pos = *cur_arg;
1983
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001984 /* Get the cache filter name. <pos> point on "cache" keyword */
1985 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02001986 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001987 goto error;
1988 }
1989 name = strdup(args[pos + 1]);
1990 if (!name) {
1991 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1992 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001993 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001994 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001995
1996 /* Check if an implicit filter with the same name already exists. If so,
1997 * we remove the implicit filter to use the explicit one. */
1998 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1999 if (f->id != cache_store_flt_id)
2000 continue;
2001
2002 cconf = f->conf;
2003 if (strcmp(name, cconf->c.name)) {
2004 cconf = NULL;
2005 continue;
2006 }
2007
2008 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2009 cconf = NULL;
2010 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2011 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002012 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002013 }
2014
2015 /* Remove the implicit filter. <cconf> is kept for the explicit one */
2016 LIST_DEL(&f->list);
2017 free(f);
2018 free(name);
2019 break;
2020 }
2021
2022 /* No implicit cache filter found, create configuration for the explicit one */
2023 if (!cconf) {
2024 cconf = calloc(1, sizeof(*cconf));
2025 if (!cconf) {
2026 memprintf(err, "%s: out of memory", args[*cur_arg]);
2027 goto error;
2028 }
2029 cconf->c.name = name;
2030 }
2031
2032 cconf->flags = 0;
2033 fconf->id = cache_store_flt_id;
2034 fconf->conf = cconf;
2035 fconf->ops = &cache_ops;
2036
2037 *cur_arg = pos;
2038 return 0;
2039
2040 error:
2041 free(name);
2042 free(cconf);
2043 return -1;
2044}
2045
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002046static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002047{
2048 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2049 return 1;
2050
2051 return 0;
2052}
2053
2054static int cli_io_handler_show_cache(struct appctx *appctx)
2055{
2056 struct cache* cache = appctx->ctx.cli.p0;
2057 struct stream_interface *si = appctx->owner;
2058
William Lallemand1f49a362017-11-21 20:01:26 +01002059 if (cache == NULL) {
2060 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
2061 }
2062
2063 list_for_each_entry_from(cache, &caches, list) {
2064 struct eb32_node *node = NULL;
2065 unsigned int next_key;
2066 struct cache_entry *entry;
2067
William Lallemand1f49a362017-11-21 20:01:26 +01002068 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002069 if (!next_key) {
2070 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
2071 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002072 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002073 return 0;
2074 }
2075 }
William Lallemand1f49a362017-11-21 20:01:26 +01002076
2077 appctx->ctx.cli.p0 = cache;
2078
2079 while (1) {
2080
2081 shctx_lock(shctx_ptr(cache));
2082 node = eb32_lookup_ge(&cache->entries, next_key);
2083 if (!node) {
2084 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02002085 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002086 break;
2087 }
2088
2089 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01002090 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 +01002091
2092 next_key = node->key + 1;
2093 appctx->ctx.cli.i0 = next_key;
2094
2095 shctx_unlock(shctx_ptr(cache));
2096
2097 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002098 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01002099 return 0;
2100 }
2101 }
2102
2103 }
2104
2105 return 1;
2106
2107}
2108
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002109
2110/*
2111 * boolean, returns true if response was built out of a cache entry.
2112 */
2113static int
2114smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2115 const char *kw, void *private)
2116{
2117 smp->data.type = SMP_T_BOOL;
2118 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2119
2120 return 1;
2121}
2122
2123/*
2124 * string, returns cache name (if response came from a cache).
2125 */
2126static int
2127smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2128 const char *kw, void *private)
2129{
2130 struct appctx *appctx = NULL;
2131
2132 struct cache_flt_conf *cconf = NULL;
2133 struct cache *cache = NULL;
2134
2135 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2136 return 0;
2137
2138 /* Get appctx from the stream_interface. */
2139 appctx = si_appctx(&smp->strm->si[1]);
2140 if (appctx && appctx->rule) {
2141 cconf = appctx->rule->arg.act.p[0];
2142 if (cconf) {
2143 cache = cconf->c.cache;
2144
2145 smp->data.type = SMP_T_STR;
2146 smp->flags = SMP_F_CONST;
2147 smp->data.u.str.area = cache->id;
2148 smp->data.u.str.data = strlen(cache->id);
2149 return 1;
2150 }
2151 }
2152
2153 return 0;
2154}
2155
Christopher Faulet99a17a22018-12-11 09:18:27 +01002156/* Declare the filter parser for "cache" keyword */
2157static struct flt_kw_list filter_kws = { "CACHE", { }, {
2158 { "cache", parse_cache_flt, NULL },
2159 { NULL, NULL, NULL },
2160 }
2161};
2162
2163INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2164
William Lallemand1f49a362017-11-21 20:01:26 +01002165static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01002166 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
2167 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002168}};
2169
Willy Tarreau0108d902018-11-25 19:14:37 +01002170INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002171
William Lallemand41db4602017-10-30 11:15:51 +01002172static struct action_kw_list http_res_actions = {
2173 .kw = {
2174 { "cache-store", parse_cache_store },
2175 { NULL, NULL }
2176 }
2177};
2178
Willy Tarreau0108d902018-11-25 19:14:37 +01002179INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2180
William Lallemand41db4602017-10-30 11:15:51 +01002181static struct action_kw_list http_req_actions = {
2182 .kw = {
2183 { "cache-use", parse_cache_use },
2184 { NULL, NULL }
2185 }
2186};
2187
Willy Tarreau0108d902018-11-25 19:14:37 +01002188INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2189
Willy Tarreau2231b632019-03-29 18:26:52 +01002190struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002191 .obj_type = OBJ_TYPE_APPLET,
2192 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002193 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002194 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002195};
2196
Willy Tarreaue6552512018-11-26 11:33:13 +01002197/* config parsers for this section */
2198REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002199REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002200
2201
2202/* Note: must not be declared <const> as its list will be overwritten */
2203static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2204 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2205 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2206 { /* END */ },
2207 }
2208};
2209
2210INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);