blob: bba757f7799c50d51377f9fe186c459088fc621e [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
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100107#define SECONDARY_ENTRY_MAX_COUNT 10
108
William Lallemand41db4602017-10-30 11:15:51 +0100109struct cache_entry {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100110 unsigned int complete; /* An entry won't be valid until complete is not null. */
William Lallemand41db4602017-10-30 11:15:51 +0100111 unsigned int latest_validation; /* latest validation date */
112 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200113 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100114
William Lallemand41db4602017-10-30 11:15:51 +0100115 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +0100116 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200117
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100118 char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */
119 unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used
120 * to build secondary keys for this cache entry. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100121 unsigned int secondary_entries_count; /* Should only be filled in the last entry of a list of dup entries */
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100122
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200123 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
124 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
125
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200126 time_t last_modified; /* Origin server "Last-Modified" header value converted in
127 * seconds since epoch. If no "Last-Modified"
128 * header is found, use "Date" header value,
129 * otherwise use reception time. This field will
130 * be used in case of an "If-Modified-Since"-based
131 * conditional request. */
132
William Lallemand41db4602017-10-30 11:15:51 +0100133 unsigned char data[0];
134};
135
136#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +0100137#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100138
139static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +0200140static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +0100141static struct cache *tmp_cache_config = NULL;
142
Willy Tarreau8ceae722018-11-26 11:58:30 +0100143DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
144
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100145static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry);
146static void delete_entry(struct cache_entry *del_entry);
147
William Lallemandf528fff2017-11-23 19:43:17 +0100148struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100149{
150 struct eb32_node *node;
151 struct cache_entry *entry;
152
Willy Tarreau8b507582020-02-25 09:35:07 +0100153 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100154 if (!node)
155 return NULL;
156
157 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100158
159 /* if that's not the right node */
160 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
161 return NULL;
162
William Lallemand08727662017-11-21 20:01:27 +0100163 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100164 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100165 } else {
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100166 delete_entry(entry);
William Lallemand08727662017-11-21 20:01:27 +0100167 entry->eb.key = 0;
168 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100169 return NULL;
170
171}
172
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100173/*
174 * There can be multiple entries with the same primary key in the ebtree so in
175 * order to get the proper one out of the list, we use a secondary_key.
176 * This function simply iterates over all the entries with the same primary_key
177 * until it finds the right one.
178 * Returns the cache_entry in case of success, NULL otherwise.
179 */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100180struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry,
181 char *secondary_key)
182{
183 struct eb32_node *node = &entry->eb;
184
185 if (!entry->secondary_key_signature)
186 return NULL;
187
188 while (entry && memcmp(entry->secondary_key, secondary_key, HTTP_CACHE_SEC_KEY_LEN) != 0) {
189 node = eb32_next_dup(node);
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100190
191 /* Make the best use of this iteration and clear expired entries
192 * when we find them. Calling delete_entry would be too costly
193 * so we simply call eb32_delete. The secondary_entry count will
194 * be updated when we try to insert a new entry to this list. */
195 if (entry->expire <= now.tv_sec) {
196 eb32_delete(&entry->eb);
197 entry->eb.key = 0;
198 }
199
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100200 entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL;
201 }
202
203 /* Expired entry */
204 if (entry && entry->expire <= now.tv_sec) {
205 eb32_delete(&entry->eb);
206 entry->eb.key = 0;
207 entry = NULL;
208 }
209
210 return entry;
211}
212
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100213
214
215/*
216 * This function inserts a cache_entry in the cache's ebtree. In case of
217 * duplicate entries (vary), it then checks that the number of entries did not
218 * reach the max number of secondary entries. If this entry should not have been
219 * created, remove it.
220 * In the regular case (unique entries), this function does not do more than a
221 * simple insert. In case of secondary entries, it will at most cost an
222 * insertion+max_sec_entries time checks and entry deletion.
223 * Returns the newly inserted node in case of success, NULL otherwise.
224 */
225static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry)
226{
227 struct eb32_node *prev = NULL;
228 struct cache_entry *entry = NULL;
229 unsigned int entry_count = 0;
230
231 struct eb32_node *node = eb32_insert(&cache->entries, &new_entry->eb);
232
233 /* We should not have multiple entries with the same primary key unless
234 * the entry has a non null vary signature. */
235 if (!new_entry->secondary_key_signature)
236 return node;
237
238 prev = eb32_prev_dup(node);
239 if (prev != NULL) {
240 /* The last entry of a duplicate list should contain the current
241 * number of entries in the list. */
242 entry = container_of(prev, struct cache_entry, eb);
243 entry_count = entry->secondary_entries_count;
244
245 if (entry_count >= SECONDARY_ENTRY_MAX_COUNT) {
246 /* Too many entries for this primary key, delete
247 * the newly inserted one. */
248 entry = container_of(prev, struct cache_entry, eb);
249 eb32_delete(node);
250 node->key = 0;
251 return NULL;
252 }
253 }
254
255 new_entry->secondary_entries_count = entry_count + 1;
256
257 return node;
258}
259
260
261/*
262 * This function removes an entry from the ebtree. If the entry was a duplicate
263 * (in case of Vary), it updates the secondary entry counter in another
264 * duplicate entry (the last entry of the dup list).
265 */
266static void delete_entry(struct cache_entry *del_entry)
267{
268 struct eb32_node *prev = NULL, *next = NULL;
269 struct cache_entry *entry = NULL;
270 struct eb32_node *last = NULL;
271
272 if (del_entry->secondary_key_signature) {
273 next = &del_entry->eb;
274
275 /* Look for last entry of the duplicates list. */
276 while ((next = eb32_next_dup(next))) {
277 last = next;
278 }
279
280 if (last) {
281 entry = container_of(last, struct cache_entry, eb);
282 --entry->secondary_entries_count;
283 }
284 else {
285 /* The current entry is the last one, look for the
286 * previous one to update its counter. */
287 prev = eb32_prev_dup(&del_entry->eb);
288 if (prev) {
289 entry = container_of(prev, struct cache_entry, eb);
290 entry->secondary_entries_count = del_entry->secondary_entries_count - 1;
291 }
292 }
293 }
294 eb32_delete(&del_entry->eb);
295 del_entry->eb.key = 0;
296}
297
298
William Lallemand4da3f8a2017-10-31 14:33:34 +0100299static inline struct shared_context *shctx_ptr(struct cache *cache)
300{
301 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
302}
303
William Lallemand77c11972017-10-31 20:43:01 +0100304static inline struct shared_block *block_ptr(struct cache_entry *entry)
305{
306 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
307}
308
309
310
William Lallemand41db4602017-10-30 11:15:51 +0100311static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100312cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100313{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100314 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100315 return 0;
316}
317
Christopher Faulet95220e22018-12-07 17:34:39 +0100318static void
319cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
320{
321 struct cache_flt_conf *cconf = fconf->conf;
322
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200323 if (!(cconf->flags & CACHE_FLT_INIT))
324 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100325 free(cconf);
326}
327
William Lallemand4da3f8a2017-10-31 14:33:34 +0100328static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100329cache_store_check(struct proxy *px, struct flt_conf *fconf)
330{
331 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100332 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100333 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100334 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100335
William Lallemandd1d1e222019-08-28 15:22:49 +0200336 /* Find the cache corresponding to the name in the filter config. The
337 * cache will not be referenced now in the filter config because it is
338 * not fully allocated. This step will be performed during the cache
339 * post_check.
340 */
341 list_for_each_entry(cache, &caches_config, list) {
342 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100343 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100344 }
345
346 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
347 proxy_type_str(px), px->id, (char *)cconf->c.name);
348 return 1;
349
350 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100351 /* Here <cache> points on the cache the filter must use and <cconf>
352 * points on the cache filter configuration. */
353
354 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100355 * enabled and if it is after the cache. When the compression is before
356 * the cache, an error is returned. Also check if the cache filter must
357 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100358 list_for_each_entry(f, &px->filter_configs, list) {
359 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100360 /* The compression filter must be evaluated after the cache. */
361 if (comp) {
362 ha_alert("config: %s '%s': unable to enable the compression filter before "
363 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
364 return 1;
365 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100366 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200367 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100368 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200369 else if (f->id == fcgi_flt_id)
370 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100371 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
372 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200373 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100374 * declaration is required. */
375 ha_alert("config: %s '%s': require an explicit filter declaration "
376 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
377 return 1;
378 }
379
Christopher Fauletafd819c2018-12-11 08:57:45 +0100380 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100381 return 0;
382}
383
384static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100385cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100386{
Christopher Faulet65554e12020-03-06 14:52:06 +0100387 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100388
Christopher Faulet65554e12020-03-06 14:52:06 +0100389 st = pool_alloc_dirty(pool_head_cache_st);
390 if (st == NULL)
391 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100392
Christopher Faulet65554e12020-03-06 14:52:06 +0100393 st->first_block = NULL;
394 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100395
Christopher Faulet65554e12020-03-06 14:52:06 +0100396 /* Register post-analyzer on AN_RES_WAIT_HTTP */
397 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100398 return 1;
399}
400
Christopher Faulet65554e12020-03-06 14:52:06 +0100401static void
402cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100403{
404 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100405 struct cache_flt_conf *cconf = FLT_CONF(filter);
406 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100407 struct shared_context *shctx = shctx_ptr(cache);
408
William Lallemand49dc0482017-11-24 14:33:54 +0100409 /* Everything should be released in the http_end filter, but we need to do it
410 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100411 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100412 shctx_lock(shctx);
413 shctx_row_dec_hot(shctx, st->first_block);
414 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100415 }
416 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100417 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100418 filter->ctx = NULL;
419 }
William Lallemand49dc0482017-11-24 14:33:54 +0100420}
421
Christopher Faulet839791a2019-01-07 16:12:07 +0100422static int
423cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
424 unsigned an_bit)
425{
426 struct http_txn *txn = s->txn;
427 struct http_msg *msg = &txn->rsp;
428 struct cache_st *st = filter->ctx;
429
430 if (an_bit != AN_RES_WAIT_HTTP)
431 goto end;
432
433 /* Here we need to check if any compression filter precedes the cache
434 * filter. This is only possible when the compression is configured in
435 * the frontend while the cache filter is configured on the
436 * backend. This case cannot be detected during HAProxy startup. So in
437 * such cases, the cache is disabled.
438 */
439 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
440 pool_free(pool_head_cache_st, st);
441 filter->ctx = NULL;
442 }
443
444 end:
445 return 1;
446}
William Lallemand49dc0482017-11-24 14:33:54 +0100447
448static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100449cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
450{
451 struct cache_st *st = filter->ctx;
452
William Lallemand4da3f8a2017-10-31 14:33:34 +0100453 if (!(msg->chn->flags & CF_ISRESP) || !st)
454 return 1;
455
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200456 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100457 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100458 return 1;
459}
460
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200461static inline void disable_cache_entry(struct cache_st *st,
462 struct filter *filter, struct shared_context *shctx)
463{
464 struct cache_entry *object;
465
466 object = (struct cache_entry *)st->first_block->data;
467 filter->ctx = NULL; /* disable cache */
468 shctx_lock(shctx);
469 shctx_row_dec_hot(shctx, st->first_block);
Remi Tricot-Le Breton964caaf2020-12-15 14:30:12 +0100470 eb32_delete(&object->eb);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200471 object->eb.key = 0;
472 shctx_unlock(shctx);
473 pool_free(pool_head_cache_st, st);
474}
475
William Lallemand4da3f8a2017-10-31 14:33:34 +0100476static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100477cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
478 unsigned int offset, unsigned int len)
479{
Christopher Faulet95220e22018-12-07 17:34:39 +0100480 struct cache_flt_conf *cconf = FLT_CONF(filter);
481 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100482 struct cache_st *st = filter->ctx;
483 struct htx *htx = htxbuf(&msg->chn->buf);
484 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200485 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100486 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200487 unsigned int orig_len, to_forward;
488 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100489
490 if (!len)
491 return len;
492
493 if (!st->first_block) {
494 unregister_data_filter(s, msg->chn, filter);
495 return len;
496 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100497
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200498 chunk_reset(&trash);
499 orig_len = len;
500 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100501
502 htxret = htx_find_offset(htx, offset);
503 blk = htxret.blk;
504 offset = htxret.ret;
505 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100506 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200507 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100508 struct ist v;
509
510 switch (type) {
511 case HTX_BLK_UNUSED:
512 break;
513
514 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100515 v = htx_get_blk_value(htx, blk);
516 v.ptr += offset;
517 v.len -= offset;
518 if (v.len > len)
519 v.len = len;
520
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200521 info = (type << 28) + v.len;
522 chunk_memcat(&trash, (char *)&info, sizeof(info));
523 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100524 to_forward += v.len;
525 len -= v.len;
526 break;
527
528 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200529 /* Here offset must always be 0 because only
530 * DATA blocks can be partially transferred. */
531 if (offset)
532 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100533 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200534 goto end;
535
536 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
537 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100538 to_forward += sz;
539 len -= sz;
540 break;
541 }
542
543 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100544 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200545
546 end:
547 shctx_lock(shctx);
548 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
549 if (!fb) {
550 shctx_unlock(shctx);
551 goto no_cache;
552 }
553 shctx_unlock(shctx);
554
555 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
556 (unsigned char *)b_head(&trash), b_data(&trash));
557 if (ret < 0)
558 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100559
560 return to_forward;
561
562 no_cache:
563 disable_cache_entry(st, filter, shctx);
564 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200565 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100566}
567
568static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100569cache_store_http_end(struct stream *s, struct filter *filter,
570 struct http_msg *msg)
571{
572 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100573 struct cache_flt_conf *cconf = FLT_CONF(filter);
574 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100575 struct shared_context *shctx = shctx_ptr(cache);
576 struct cache_entry *object;
577
578 if (!(msg->chn->flags & CF_ISRESP))
579 return 1;
580
581 if (st && st->first_block) {
582
583 object = (struct cache_entry *)st->first_block->data;
584
William Lallemand4da3f8a2017-10-31 14:33:34 +0100585 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100586 /* The whole payload was cached, the entry can now be used. */
587 object->complete = 1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100588 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100589 shctx_row_dec_hot(shctx, st->first_block);
590 shctx_unlock(shctx);
591
592 }
593 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100594 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100595 filter->ctx = NULL;
596 }
597
598 return 1;
599}
600
601 /*
602 * This intends to be used when checking HTTP headers for some
603 * word=value directive. Return a pointer to the first character of value, if
604 * the word was not found or if there wasn't any value assigned ot it return NULL
605 */
606char *directive_value(const char *sample, int slen, const char *word, int wlen)
607{
608 int st = 0;
609
610 if (slen < wlen)
611 return 0;
612
613 while (wlen) {
614 char c = *sample ^ *word;
615 if (c && c != ('A' ^ 'a'))
616 return NULL;
617 sample++;
618 word++;
619 slen--;
620 wlen--;
621 }
622
623 while (slen) {
624 if (st == 0) {
625 if (*sample != '=')
626 return NULL;
627 sample++;
628 slen--;
629 st = 1;
630 continue;
631 } else {
632 return (char *)sample;
633 }
634 }
635
636 return NULL;
637}
638
639/*
640 * Return the maxage in seconds of an HTTP response.
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100641 * The returned value will always take the cache's configuration into account
642 * (cache->maxage) but the actual max age of the response will be set in the
643 * true_maxage parameter. It will be used to determine if a response is already
644 * stale or not.
William Lallemand4da3f8a2017-10-31 14:33:34 +0100645 * Compute the maxage using either:
646 * - the assigned max-age of the cache
647 * - the s-maxage directive
648 * - the max-age directive
649 * - (Expires - Data) headers
650 * - the default-max-age of the cache
651 *
652 */
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100653int http_calc_maxage(struct stream *s, struct cache *cache, int *true_maxage)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100654{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200655 struct htx *htx = htxbuf(&s->res.buf);
656 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100657 long smaxage = -1;
658 long maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100659 int expires = -1;
660 struct tm tm = {};
661 time_t expires_val = 0;
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100662 char *endptr = NULL;
663 int offset = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100664
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100665 /* The Cache-Control max-age and s-maxage directives should be followed by
666 * a positive numerical value (see RFC 7234#5.2.1.1). According to the
667 * specs, a sender "should not" generate a quoted-string value but we will
668 * still accept this format since it isn't strictly forbidden. */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200669 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
670 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100671
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200672 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
673 if (value) {
674 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100675
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200676 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
677 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100678 offset = (*chk->area == '"') ? 1 : 0;
679 smaxage = strtol(chk->area + offset, &endptr, 10);
680 if (unlikely(smaxage < 0 || endptr == chk->area))
681 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100682 }
683
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200684 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
685 if (value) {
686 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200687
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200688 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
689 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100690 offset = (*chk->area == '"') ? 1 : 0;
691 maxage = strtol(chk->area + offset, &endptr, 10);
692 if (unlikely(maxage < 0 || endptr == chk->area))
693 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100694 }
695 }
696
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100697 /* Look for Expires header if no s-maxage or max-age Cache-Control data
698 * was found. */
699 if (maxage == -1 && smaxage == -1) {
700 ctx.blk = NULL;
701 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
702 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
703 expires_val = my_timegm(&tm);
704 /* A request having an expiring date earlier
705 * than the current date should be considered as
706 * stale. */
707 expires = (expires_val >= now.tv_sec) ?
708 (expires_val - now.tv_sec) : 0;
709 }
710 else {
711 /* Following RFC 7234#5.3, an invalid date
712 * format must be treated as a date in the past
713 * so the cache entry must be seen as already
714 * expired. */
715 expires = 0;
716 }
717 }
718 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100719
720
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100721 if (smaxage > 0) {
722 if (true_maxage)
723 *true_maxage = smaxage;
William Lallemand49b44532017-11-24 18:53:43 +0100724 return MIN(smaxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100725 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100726
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100727 if (maxage > 0) {
728 if (true_maxage)
729 *true_maxage = maxage;
William Lallemand49b44532017-11-24 18:53:43 +0100730 return MIN(maxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100731 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100732
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100733 if (expires >= 0) {
734 if (true_maxage)
735 *true_maxage = expires;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100736 return MIN(expires, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100737 }
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100738
William Lallemand49b44532017-11-24 18:53:43 +0100739 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100740
741}
742
743
William Lallemanda400a3a2017-11-20 19:13:12 +0100744static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
745{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200746 struct cache_entry *object = (struct cache_entry *)block->data;
747
748 if (first == block && object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100749 delete_entry(object);
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200750 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100751}
752
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200753
754/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
755 * date value should be compared to a date determined by in a previous response (for
756 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
757 * value of the response's reception time (by decreasing order of priority). */
758static time_t get_last_modified_time(struct htx *htx)
759{
760 time_t last_modified = 0;
761 struct http_hdr_ctx ctx = { .blk = NULL };
762 struct tm tm = {};
763
764 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
765 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
766 last_modified = my_timegm(&tm);
767 }
768 }
769
770 if (!last_modified) {
771 ctx.blk = NULL;
772 if (http_find_header(htx, ist("date"), &ctx, 1)) {
773 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
774 last_modified = my_timegm(&tm);
775 }
776 }
777 }
778
779 /* Fallback on the current time if no "Last-Modified" or "Date" header
780 * was found. */
781 if (!last_modified)
782 last_modified = now.tv_sec;
783
784 return last_modified;
785}
786
William Lallemand41db4602017-10-30 11:15:51 +0100787/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100788 * Checks the vary header's value. The headers on which vary should be applied
789 * must be explicitely supported in the vary_information array (see cache.c). If
790 * any other header is mentioned, we won't store the response.
791 * Returns 1 if Vary-based storage can work, 0 otherwise.
792 */
793static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
794{
795 unsigned int vary_idx;
796 unsigned int vary_info_count;
797 const struct vary_hashing_information *vary_info;
798 struct http_hdr_ctx ctx = { .blk = NULL };
799
800 int retval = 1;
801
802 *vary_signature = 0;
803
804 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
805 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
806 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
807 vary_info = &vary_information[vary_idx];
808 if (isteqi(ctx.value, vary_info->hdr_name)) {
809 *vary_signature |= vary_info->value;
810 break;
811 }
812 }
813 retval = (vary_idx < vary_info_count);
814 }
815
816 return retval;
817}
818
819
820
821/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500822 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100823 * register a filter to store the data
824 */
825enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200826 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100827{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200828 long long hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100829 int effective_maxage = 0;
830 int true_maxage = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100831 struct http_txn *txn = s->txn;
832 struct http_msg *msg = &txn->rsp;
833 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100834 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100835 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100836 struct cache *cache = cconf->c.cache;
837 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100838 struct cache_st *cache_ctx = NULL;
839 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100840 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200841 struct htx *htx;
842 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200843 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200844 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200845 struct ist header_name = IST_NULL;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100846 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100847
William Lallemand4da3f8a2017-10-31 14:33:34 +0100848 /* Don't cache if the response came from a cache */
849 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
850 s->target == &http_cache_applet.obj_type) {
851 goto out;
852 }
853
854 /* cache only HTTP/1.1 */
855 if (!(txn->req.flags & HTTP_MSGF_VER_11))
856 goto out;
857
Willy Tarreau6905d182019-10-01 17:59:17 +0200858 /* cache only GET method */
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +0100859 if (txn->meth != HTTP_METH_GET) {
860 /* In case of successful unsafe method on a stored resource, the
861 * cached entry must be invalidated (see RFC7234#4.4).
862 * A "non-error response" is one with a 2xx (Successful) or 3xx
863 * (Redirection) status code. */
864 if (txn->status >= 200 && txn->status < 400) {
865 switch (txn->meth) {
866 case HTTP_METH_OPTIONS:
867 case HTTP_METH_GET:
868 case HTTP_METH_HEAD:
869 case HTTP_METH_TRACE:
870 break;
871
872 default: /* Any unsafe method */
873 /* Discard any corresponding entry in case of sucessful
874 * unsafe request (such as PUT, POST or DELETE). */
875 shctx_lock(shctx);
876
877 old = entry_exist(cconf->c.cache, txn->cache_hash);
878 if (old) {
879 eb32_delete(&old->eb);
880 old->eb.key = 0;
881 }
882 shctx_unlock(shctx);
883 }
884 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100885 goto out;
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +0100886 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100887
Willy Tarreauc9036c02019-01-11 19:38:25 +0100888 /* cache key was not computed */
889 if (!key)
890 goto out;
891
William Lallemand4da3f8a2017-10-31 14:33:34 +0100892 /* cache only 200 status code */
893 if (txn->status != 200)
894 goto out;
895
Christopher Faulet839791a2019-01-07 16:12:07 +0100896 /* Find the corresponding filter instance for the current stream */
897 list_for_each_entry(filter, &s->strm_flt.filters, list) {
898 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
899 /* No filter ctx, don't cache anything */
900 if (!filter->ctx)
901 goto out;
902 cache_ctx = filter->ctx;
903 break;
904 }
905 }
906
907 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200908 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100909
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200910 /* Do not cache too big objects. */
911 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
912 htx->data + htx->extra > shctx->max_obj_size)
913 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100914
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100915 /* Only a subset of headers are supported in our Vary implementation. If
916 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100917 * able to use the cache. Likewise, if Vary header support is disabled,
918 * avoid caching responses that contain such a header. */
919 ctx.blk = NULL;
920 if (cache->vary_processing_enabled) {
921 if (!http_check_vary_header(htx, &vary_signature))
922 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100923 if (vary_signature)
924 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100925 }
926 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200927 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100928 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100929
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200930 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100931
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100932 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200933 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100934
935 shctx_lock(shctx);
936 old = entry_exist(cache, txn->cache_hash);
937 if (old) {
938 if (vary_signature)
939 old = secondary_entry_exist(cconf->c.cache, old,
940 txn->cache_secondary_hash);
941 if (old) {
942 if (!old->complete) {
943 /* An entry with the same primary key is already being
944 * created, we should not try to store the current
945 * response because it will waste space in the cache. */
946 shctx_unlock(shctx);
947 goto out;
948 }
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100949 delete_entry(old);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100950 old->eb.key = 0;
951 }
952 }
953 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry));
954 if (!first) {
955 shctx_unlock(shctx);
956 goto out;
957 }
958 /* the received memory is not initialized, we need at least to mark
959 * the object as not indexed yet.
960 */
961 object = (struct cache_entry *)first->data;
962 memset(object, 0, sizeof(*object));
963 object->eb.key = key;
964 object->secondary_key_signature = vary_signature;
965 /* We need to temporarily set a valid expiring time until the actual one
966 * is set by the end of this function (in case of concurrent accesses to
967 * the same resource). This way the second access will find an existing
968 * but not yet usable entry in the tree and will avoid storing its data. */
969 object->expire = now.tv_sec + 2;
970
971 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
972 if (vary_signature)
973 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
974
975 /* Insert the entry in the tree even if the payload is not cached yet. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100976 if (insert_entry(cache, object) != &object->eb) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100977 object->eb.key = 0;
978 shctx_unlock(shctx);
979 goto out;
980 }
981 shctx_unlock(shctx);
982
983 /* reserve space for the cache_entry structure */
984 first->len = sizeof(struct cache_entry);
985 first->last_append = NULL;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100986
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100987 /* Determine the entry's maximum age (taking into account the cache's
988 * configuration) as well as the response's explicit max age (extracted
989 * from cache-control directives or the expires header). */
990 effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage);
991
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200992 ctx.blk = NULL;
993 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
994 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
995 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
996 hdr_age = CACHE_ENTRY_MAX_AGE;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100997 /* A response with an Age value greater than its
998 * announced max age is stale and should not be stored. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100999 object->age = hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001000 if (unlikely(object->age > true_maxage))
1001 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001002 }
Remi Tricot-Le Breton51058d62020-12-03 18:19:32 +01001003 else
1004 goto out;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001005 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001006 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001007
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001008 /* Build a last-modified time that will be stored in the cache_entry and
1009 * compared to a future If-Modified-Since client header. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001010 object->last_modified = get_last_modified_time(htx);
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001011
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001012 chunk_reset(&trash);
1013 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1014 struct htx_blk *blk = htx_get_blk(htx, pos);
1015 enum htx_blk_type type = htx_get_blk_type(blk);
1016 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001017
Christopher Fauletb0667472019-09-03 22:22:12 +02001018 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001019 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
1020 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001021
1022 /* Look for optional ETag header.
1023 * We need to store the offset of the ETag value in order for
1024 * future conditional requests to be able to perform ETag
1025 * comparisons. */
1026 if (type == HTX_BLK_HDR) {
1027 header_name = htx_get_blk_name(htx, blk);
1028 if (isteq(header_name, ist("etag"))) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001029 object->etag_length = sz - istlen(header_name);
1030 object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001031 }
1032 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001033 if (type == HTX_BLK_EOH)
1034 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001035 }
1036
Christopher Fauletb0667472019-09-03 22:22:12 +02001037 /* Do not cache objects if the headers are too big. */
1038 if (hdrs_len > htx->size - global.tune.maxrewrite)
1039 goto out;
1040
William Lallemand4da3f8a2017-10-31 14:33:34 +01001041 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001042 if (!shctx_row_reserve_hot(shctx, first, trash.data)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +01001043 shctx_unlock(shctx);
1044 goto out;
1045 }
1046 shctx_unlock(shctx);
1047
William Lallemand4da3f8a2017-10-31 14:33:34 +01001048 /* cache the headers in a http action because it allows to chose what
1049 * to cache, for example you might want to cache a response before
1050 * modifying some HTTP headers, or on the contrary after modifying
1051 * those headers.
1052 */
William Lallemand4da3f8a2017-10-31 14:33:34 +01001053 /* does not need to be locked because it's in the "hot" list,
1054 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001055 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
1056 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001057
1058 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +01001059 if (cache_ctx) {
1060 cache_ctx->first_block = first;
Christopher Faulet839791a2019-01-07 16:12:07 +01001061 /* store latest value and expiration time */
1062 object->latest_validation = now.tv_sec;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001063 object->expire = now.tv_sec + effective_maxage;
Christopher Faulet839791a2019-01-07 16:12:07 +01001064 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001065 }
1066
1067out:
1068 /* if does not cache */
1069 if (first) {
1070 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +01001071 first->len = 0;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001072 if (object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001073 delete_entry(object);
William Lallemand08727662017-11-21 20:01:27 +01001074 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001075 shctx_row_dec_hot(shctx, first);
1076 shctx_unlock(shctx);
1077 }
1078
William Lallemand41db4602017-10-30 11:15:51 +01001079 return ACT_RET_CONT;
1080}
1081
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001082#define HTX_CACHE_INIT 0 /* Initial state. */
1083#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
1084#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001085#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
1086#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001087
William Lallemandecb73b12017-11-24 14:33:55 +01001088static void http_cache_applet_release(struct appctx *appctx)
1089{
Christopher Faulet95220e22018-12-07 17:34:39 +01001090 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +01001091 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +01001092 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +01001093 struct shared_block *first = block_ptr(cache_ptr);
1094
1095 shctx_lock(shctx_ptr(cache));
1096 shctx_row_dec_hot(shctx_ptr(cache), first);
1097 shctx_unlock(shctx_ptr(cache));
1098}
1099
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001100
1101static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
1102 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001103{
Christopher Faulet95220e22018-12-07 17:34:39 +01001104 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1105 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001106 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001107 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001108 unsigned int max, total;
1109 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001110
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001111 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1112 if (!max)
1113 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001114 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001115 ? (info & 0xff) + ((info >> 8) & 0xfffff)
1116 : info & 0xfffffff);
1117 if (blksz > max)
1118 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001119
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001120 blk = htx_add_blk(htx, type, blksz);
1121 if (!blk)
1122 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001123
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001124 blk->info = info;
1125 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001126 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001127 while (blksz) {
1128 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001129 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001130 offset += max;
1131 blksz -= max;
1132 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001133 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001134 if (blksz || offset == shctx->block_size) {
1135 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1136 offset = 0;
1137 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001138 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001139 appctx->ctx.cache.offset = offset;
1140 appctx->ctx.cache.next = shblk;
1141 appctx->ctx.cache.sent += total;
1142 return total;
1143}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001144
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001145static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
1146 uint32_t info, struct shared_block *shblk, unsigned int offset)
1147{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001148
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001149 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1150 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
1151 unsigned int max, total, rem_data;
1152 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001153
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001154 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1155 if (!max)
1156 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001157
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001158 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001159 if (appctx->ctx.cache.rem_data) {
1160 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001161 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001162 }
1163 else {
1164 blksz = (info & 0xfffffff);
1165 total = 4;
1166 }
1167 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001168 rem_data = blksz - max;
1169 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001170 }
1171
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001172 while (blksz) {
1173 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001174
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001175 max = MIN(blksz, shctx->block_size - offset);
1176 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1177 offset += sz;
1178 blksz -= sz;
1179 total += sz;
1180 if (sz < max)
1181 break;
1182 if (blksz || offset == shctx->block_size) {
1183 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1184 offset = 0;
1185 }
1186 }
1187
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001188 appctx->ctx.cache.offset = offset;
1189 appctx->ctx.cache.next = shblk;
1190 appctx->ctx.cache.sent += total;
1191 appctx->ctx.cache.rem_data = rem_data + blksz;
1192 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001193}
1194
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001195static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1196 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001197{
Christopher Faulet95220e22018-12-07 17:34:39 +01001198 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1199 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001200 struct shared_block *shblk;
1201 unsigned int offset, sz;
1202 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001203
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001204 while (len) {
1205 enum htx_blk_type type;
1206 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001207
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001208 shblk = appctx->ctx.cache.next;
1209 offset = appctx->ctx.cache.offset;
1210 if (appctx->ctx.cache.rem_data) {
1211 type = HTX_BLK_DATA;
1212 info = 0;
1213 goto add_data_blk;
1214 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001215
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001216 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001217 sz = MIN(4, shctx->block_size - offset);
1218 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1219 offset += sz;
1220 if (sz < 4) {
1221 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1222 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1223 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001224 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001225
1226 /* Get payload of the next HTX block and insert it. */
1227 type = (info >> 28);
1228 if (type != HTX_BLK_DATA)
1229 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1230 else {
1231 add_data_blk:
1232 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001233 }
1234
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001235 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001236 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001237 total += ret;
1238 len -= ret;
1239
1240 if (appctx->ctx.cache.rem_data || type == mark)
1241 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001242 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001243
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001244 return total;
1245}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001246
1247static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1248{
1249 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1250 unsigned int age;
1251 char *end;
1252
1253 chunk_reset(&trash);
1254 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1255 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1256 age = CACHE_ENTRY_MAX_AGE;
1257 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1258 b_set_data(&trash, end - b_head(&trash));
1259 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1260 return 0;
1261 return 1;
1262}
1263
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001264static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001265{
1266 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1267 struct shared_block *first = block_ptr(cache_ptr);
1268 struct stream_interface *si = appctx->owner;
1269 struct channel *req = si_oc(si);
1270 struct channel *res = si_ic(si);
1271 struct htx *req_htx, *res_htx;
1272 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001273 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001274 size_t ret, total = 0;
1275
1276 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001277 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001278
1279 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1280 goto out;
1281
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001282 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001283 if (!b_size(&res->buf)) {
1284 si_rx_room_blk(si);
1285 goto out;
1286 }
1287
Willy Tarreauefef3232018-12-16 00:37:45 +01001288 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001289 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001290
1291 if (appctx->st0 == HTX_CACHE_INIT) {
1292 appctx->ctx.cache.next = block_ptr(cache_ptr);
1293 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1294 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001295 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001296 appctx->st0 = HTX_CACHE_HEADER;
1297 }
1298
1299 if (appctx->st0 == HTX_CACHE_HEADER) {
1300 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001301 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1302 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1303 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1304 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001305 goto error;
1306
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001307 /* In case of a conditional request, we might want to send a
1308 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001309 if (appctx->ctx.cache.send_notmodified) {
1310 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1311 /* If replacing the status code fails we need to send the full response. */
1312 appctx->ctx.cache.send_notmodified = 0;
1313 }
1314 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001315
1316 /* Skip response body for HEAD requests or in case of "304 Not
1317 * Modified" response. */
1318 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001319 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001320 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001321 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001322 }
1323
1324 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001325 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1326 if (len) {
1327 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1328 if (ret < len) {
1329 si_rx_room_blk(si);
1330 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001331 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001332 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001333 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001334 }
1335
1336 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001337 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001338 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1339 si_rx_room_blk(si);
1340 goto out;
1341 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001342 appctx->st0 = HTX_CACHE_END;
1343 }
1344
1345 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001346 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001347 res->flags |= CF_READ_NULL;
1348 si_shutr(si);
1349 }
1350
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001351 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001352 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001353 if (total)
1354 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001355 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001356
1357 /* eat the whole request */
1358 if (co_data(req)) {
1359 req_htx = htx_from_buf(&req->buf);
1360 co_htx_skip(req, req_htx, co_data(req));
1361 htx_to_buf(req_htx, &req->buf);
1362 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001363 return;
1364
1365 error:
1366 /* Sent and HTTP error 500 */
1367 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001368 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001369 res->buf.data = b_data(errmsg);
1370 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1371 res_htx = htx_from_buf(&res->buf);
1372
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001373 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001374 appctx->st0 = HTX_CACHE_END;
1375 goto end;
1376}
1377
1378
Christopher Faulet95220e22018-12-07 17:34:39 +01001379static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001380{
1381 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001382 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001383
Christopher Faulet95220e22018-12-07 17:34:39 +01001384 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001385 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001386 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001387 }
1388
1389 /* check if a cache filter was already registered with this cache
1390 * name, if that's the case, must use it. */
1391 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001392 if (fconf->id == cache_store_flt_id) {
1393 cconf = fconf->conf;
1394 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1395 rule->arg.act.p[0] = cconf;
1396 return 1;
1397 }
William Lallemand41db4602017-10-30 11:15:51 +01001398 }
1399 }
1400
Christopher Faulet95220e22018-12-07 17:34:39 +01001401 /* Create the filter cache config */
1402 cconf = calloc(1, sizeof(*cconf));
1403 if (!cconf) {
1404 memprintf(err, "out of memory\n");
1405 goto err;
1406 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001407 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001408 cconf->c.name = strdup(name);
1409 if (!cconf->c.name) {
1410 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001411 goto err;
1412 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001413
William Lallemand41db4602017-10-30 11:15:51 +01001414 /* register a filter to fill the cache buffer */
1415 fconf = calloc(1, sizeof(*fconf));
1416 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001417 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001418 goto err;
1419 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001420 fconf->id = cache_store_flt_id;
1421 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001422 fconf->ops = &cache_ops;
1423 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1424
Christopher Faulet95220e22018-12-07 17:34:39 +01001425 rule->arg.act.p[0] = cconf;
1426 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001427
Christopher Faulet95220e22018-12-07 17:34:39 +01001428 err:
1429 free(cconf);
1430 return 0;
1431}
1432
1433enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1434 struct act_rule *rule, char **err)
1435{
1436 rule->action = ACT_CUSTOM;
1437 rule->action_ptr = http_action_store_cache;
1438
1439 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1440 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001441
Christopher Faulet95220e22018-12-07 17:34:39 +01001442 (*orig_arg)++;
1443 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001444}
1445
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001446/* This produces a sha1 hash of the concatenation of the HTTP method,
1447 * the first occurrence of the Host header followed by the path component
1448 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001449int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001450{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001451 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001452 struct htx *htx = htxbuf(&s->req.buf);
1453 struct htx_sl *sl;
1454 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001455 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001456 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001457 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001458
William Lallemandf528fff2017-11-23 19:43:17 +01001459 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001460 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001461
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001462 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001463 uri = htx_sl_req_uri(sl); // whole uri
1464 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001465 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001466
1467 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1468 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1469 * URIs are almost always sent in absolute form with their scheme. In
1470 * this case, the scheme is almost always "https". In order to support
1471 * sharing of cache objects between H1 and H2, we'll hash the absolute
1472 * URI whenever known, or prepend "https://" + the Host header for
1473 * relative URIs. The difference will only appear on absolute HTTP/1
1474 * requests sent to an origin server, which practically is never met in
1475 * the real world so we don't care about the ability to share the same
1476 * key here.URIs are normalized from the absolute URI to an origin form as
1477 * well.
1478 */
1479 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001480 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001481 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1482 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001483 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001484 }
1485
1486 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001487
1488 /* hash everything */
1489 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001490 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001491 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1492
1493 return 1;
1494}
1495
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001496/* Looks for "If-None-Match" headers in the request and compares their value
1497 * with the one that might have been stored in the cache_entry. If any of them
1498 * matches, a "304 Not Modified" response should be sent instead of the cached
1499 * data.
1500 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001501 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1502 *
1503 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1504 * header and compare its value (date) to the one stored in the cache_entry.
1505 * If the request's date is later than the cached one, we also send a
1506 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1507 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001508 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1509 */
1510static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1511 struct cache_entry *entry)
1512{
1513 int retval = 0;
1514
1515 struct http_hdr_ctx ctx = { .blk = NULL };
1516 struct ist cache_entry_etag = IST_NULL;
1517 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001518 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001519
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001520 struct tm tm = {};
1521 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001522
1523 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001524 * cache_entry's ETag in order to perform comparisons.
1525 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001526 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001527 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001528
1529 /* A '*' matches everything. */
1530 if (isteq(ctx.value, ist("*")) != 0) {
1531 retval = 1;
1532 break;
1533 }
1534
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001535 /* No need to rebuild an etag if none was stored in the cache. */
1536 if (entry->etag_length == 0)
1537 break;
1538
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001539 /* Rebuild the stored ETag. */
1540 if (etag_buffer == NULL) {
1541 etag_buffer = get_trash_chunk();
1542
1543 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1544 (unsigned char*)b_orig(etag_buffer),
1545 entry->etag_offset, entry->etag_length) == 0) {
1546 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1547 } else {
1548 /* We could not rebuild the ETag in one go, we
1549 * won't send a "304 Not Modified" response. */
1550 break;
1551 }
1552 }
1553
1554 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1555 retval = 1;
1556 break;
1557 }
1558 }
1559
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001560 /* If the request did not contain an "If-None-Match" header, we look for
1561 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1562 if (retval == 0 && if_none_match_found == 0) {
1563 ctx.blk = NULL;
1564 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1565 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1566 if_modified_since = my_timegm(&tm);
1567
1568 /* We send a "304 Not Modified" response if the
1569 * entry's last modified date is earlier than
1570 * the one found in the "If-Modified-Since"
1571 * header. */
1572 retval = (entry->last_modified <= if_modified_since);
1573 }
1574 }
1575 }
1576
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001577 return retval;
1578}
1579
William Lallemand41db4602017-10-30 11:15:51 +01001580enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1581 struct session *sess, struct stream *s, int flags)
1582{
William Lallemand77c11972017-10-31 20:43:01 +01001583
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001584 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001585 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001586 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1587 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001588 struct shared_block *entry_block;
1589
William Lallemand77c11972017-10-31 20:43:01 +01001590
Willy Tarreau6905d182019-10-01 17:59:17 +02001591 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1592 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001593 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001594 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001595 txn->flags |= TX_CACHE_IGNORE;
1596
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001597 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001598
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001599 /* The request's hash has to be calculated for all requests, even POSTs
1600 * or PUTs for instance because RFC7234 specifies that a sucessful
1601 * "unsafe" method on a stored resource must invalidate it
1602 * (see RFC7234#4.4). */
1603 if (!sha1_hosturi(s))
Willy Tarreau504455c2017-12-22 17:47:35 +01001604 return ACT_RET_CONT;
1605
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001606 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001607 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001608
Willy Tarreau504455c2017-12-22 17:47:35 +01001609 if (s->txn->flags & TX_CACHE_IGNORE)
1610 return ACT_RET_CONT;
1611
Willy Tarreaua1214a52018-12-14 14:00:25 +01001612 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001613 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001614 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001615 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001616
William Lallemanda400a3a2017-11-20 19:13:12 +01001617 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001618 res = entry_exist(cache, s->txn->cache_hash);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001619 /* We must not use an entry that is not complete. */
1620 if (res && res->complete) {
William Lallemand77c11972017-10-31 20:43:01 +01001621 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001622 entry_block = block_ptr(res);
1623 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001624 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001625
1626 /* In case of Vary, we could have multiple entries with the same
1627 * primary hash. We need to calculate the secondary has in order
1628 * to find the actual entry we want (if it exists). */
1629 if (res->secondary_key_signature) {
1630 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1631 shctx_lock(shctx_ptr(cache));
1632 sec_entry = secondary_entry_exist(cache, res,
1633 s->txn->cache_secondary_hash);
1634 if (sec_entry && sec_entry != res) {
1635 /* The wrong row was added to the hot list. */
1636 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1637 entry_block = block_ptr(sec_entry);
1638 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1639 }
1640 res = sec_entry;
1641 shctx_unlock(shctx_ptr(cache));
1642 }
1643 else
1644 res = NULL;
1645 }
1646
1647 /* We looked for a valid secondary entry and could not find one,
1648 * the request must be forwarded to the server. */
1649 if (!res) {
1650 shctx_lock(shctx_ptr(cache));
1651 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1652 shctx_unlock(shctx_ptr(cache));
1653 return ACT_RET_CONT;
1654 }
1655
William Lallemand77c11972017-10-31 20:43:01 +01001656 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001657 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001658 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001659 appctx->rule = rule;
1660 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001661 appctx->ctx.cache.next = NULL;
1662 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001663 appctx->ctx.cache.send_notmodified =
1664 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001665
1666 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001667 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001668 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001669 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001670 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001671 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001672 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001673 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001674 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001675 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001676 }
1677 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001678 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001679
1680 /* Shared context does not need to be locked while we calculate the
1681 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001682 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001683 /* Build a complete secondary hash until the server response
1684 * tells us which fields should be kept (if any). */
1685 http_request_prebuild_full_secondary_key(s);
1686 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001687 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001688}
1689
1690
1691enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1692 struct act_rule *rule, char **err)
1693{
William Lallemand41db4602017-10-30 11:15:51 +01001694 rule->action = ACT_CUSTOM;
1695 rule->action_ptr = http_action_req_cache_use;
1696
Christopher Faulet95220e22018-12-07 17:34:39 +01001697 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001698 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001699
1700 (*orig_arg)++;
1701 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001702}
1703
1704int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1705{
1706 int err_code = 0;
1707
1708 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1709
1710 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001711 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001712 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001713 err_code |= ERR_ALERT | ERR_ABORT;
1714 goto out;
1715 }
1716
1717 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1718 err_code |= ERR_ABORT;
1719 goto out;
1720 }
1721
1722 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001723 struct cache *cache_config;
1724
William Lallemand41db4602017-10-30 11:15:51 +01001725 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1726 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001727 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001728 err_code |= ERR_ALERT | ERR_ABORT;
1729 goto out;
1730 }
1731
1732 strlcpy2(tmp_cache_config->id, args[1], 33);
1733 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001734 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001735 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001736 err_code |= ERR_WARN;
1737 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001738
1739 list_for_each_entry(cache_config, &caches_config, list) {
1740 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1741 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1742 file, linenum, tmp_cache_config->id);
1743 err_code |= ERR_ALERT | ERR_ABORT;
1744 goto out;
1745 }
1746 }
1747
William Lallemand49b44532017-11-24 18:53:43 +01001748 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001749 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001750 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001751 }
1752 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001753 unsigned long int maxsize;
1754 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001755
1756 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1757 err_code |= ERR_ABORT;
1758 goto out;
1759 }
1760
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001761 maxsize = strtoul(args[1], &err, 10);
1762 if (err == args[1] || *err != '\0') {
1763 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1764 file, linenum, args[1]);
1765 err_code |= ERR_ABORT;
1766 goto out;
1767 }
1768
1769 if (maxsize > (UINT_MAX >> 20)) {
1770 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1771 file, linenum, args[1], UINT_MAX >> 20);
1772 err_code |= ERR_ABORT;
1773 goto out;
1774 }
1775
William Lallemand41db4602017-10-30 11:15:51 +01001776 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001777 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001778 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001779 } else if (strcmp(args[0], "max-age") == 0) {
1780 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1781 err_code |= ERR_ABORT;
1782 goto out;
1783 }
1784
1785 if (!*args[1]) {
1786 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1787 file, linenum, args[0]);
1788 err_code |= ERR_WARN;
1789 }
1790
1791 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001792 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001793 unsigned int maxobjsz;
1794 char *err;
1795
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001796 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1797 err_code |= ERR_ABORT;
1798 goto out;
1799 }
1800
1801 if (!*args[1]) {
1802 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1803 file, linenum, args[0]);
1804 err_code |= ERR_WARN;
1805 }
1806
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001807 maxobjsz = strtoul(args[1], &err, 10);
1808 if (err == args[1] || *err != '\0') {
1809 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1810 file, linenum, args[1]);
1811 err_code |= ERR_ABORT;
1812 goto out;
1813 }
1814 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001815 } else if (strcmp(args[0], "process-vary") == 0) {
1816 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1817 err_code |= ERR_ABORT;
1818 goto out;
1819 }
1820
1821 if (!*args[1]) {
1822 ha_warning("parsing [%s:%d]: '%s' expects 0 or 1 (disable or enable vary processing).\n",
1823 file, linenum, args[0]);
1824 err_code |= ERR_WARN;
1825 }
1826
1827 tmp_cache_config->vary_processing_enabled = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001828 }
1829 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001830 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001831 err_code |= ERR_ALERT | ERR_FATAL;
1832 goto out;
1833 }
1834out:
1835 return err_code;
1836}
1837
1838/* once the cache section is parsed */
1839
1840int cfg_post_parse_section_cache()
1841{
William Lallemand41db4602017-10-30 11:15:51 +01001842 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001843
1844 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001845
1846 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001847 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001848 err_code |= ERR_FATAL | ERR_ALERT;
1849 goto out;
1850 }
1851
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001852 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001853 /* Default max. file size is a 256th of the cache size. */
1854 tmp_cache_config->maxobjsz =
1855 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001856 }
1857 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1858 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1859 err_code |= ERR_FATAL | ERR_ALERT;
1860 goto out;
1861 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001862
William Lallemandd1d1e222019-08-28 15:22:49 +02001863 /* add to the list of cache to init and reinit tmp_cache_config
1864 * for next cache section, if any.
1865 */
1866 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1867 tmp_cache_config = NULL;
1868 return err_code;
1869 }
1870out:
1871 free(tmp_cache_config);
1872 tmp_cache_config = NULL;
1873 return err_code;
1874
1875}
1876
1877int post_check_cache()
1878{
1879 struct proxy *px;
1880 struct cache *back, *cache_config, *cache;
1881 struct shared_context *shctx;
1882 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001883 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02001884
1885 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1886
1887 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1888 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001889
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001890 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001891 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001892 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001893 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001894 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001895
1896 err_code |= ERR_FATAL | ERR_ALERT;
1897 goto out;
1898 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001899 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001900 /* the cache structure is stored in the shctx and added to the
1901 * caches list, we can remove the entry from the caches_config
1902 * list */
1903 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001904 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001905 cache->entries = EB_ROOT;
William Lallemand41db4602017-10-30 11:15:51 +01001906 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001907 LIST_DEL(&cache_config->list);
1908 free(cache_config);
1909
1910 /* Find all references for this cache in the existing filters
1911 * (over all proxies) and reference it in matching filters.
1912 */
1913 for (px = proxies_list; px; px = px->next) {
1914 struct flt_conf *fconf;
1915 struct cache_flt_conf *cconf;
1916
1917 list_for_each_entry(fconf, &px->filter_configs, list) {
1918 if (fconf->id != cache_store_flt_id)
1919 continue;
1920
1921 cconf = fconf->conf;
1922 if (!strcmp(cache->id, cconf->c.name)) {
1923 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001924 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001925 cconf->c.cache = cache;
1926 break;
1927 }
1928 }
1929 }
William Lallemand41db4602017-10-30 11:15:51 +01001930 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001931
William Lallemand41db4602017-10-30 11:15:51 +01001932out:
William Lallemand41db4602017-10-30 11:15:51 +01001933 return err_code;
1934
William Lallemand41db4602017-10-30 11:15:51 +01001935}
1936
William Lallemand41db4602017-10-30 11:15:51 +01001937struct flt_ops cache_ops = {
1938 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001939 .check = cache_store_check,
1940 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001941
Christopher Faulet65554e12020-03-06 14:52:06 +01001942 /* Handle stream init/deinit */
1943 .attach = cache_store_strm_init,
1944 .detach = cache_store_strm_deinit,
1945
William Lallemand4da3f8a2017-10-31 14:33:34 +01001946 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001947 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001948
1949 /* Filter HTTP requests and responses */
1950 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001951 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001952 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001953};
1954
Christopher Faulet99a17a22018-12-11 09:18:27 +01001955
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001956int accept_encoding_cmp(const void *a, const void *b)
1957{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001958 unsigned int int_a = *(unsigned int*)a;
1959 unsigned int int_b = *(unsigned int*)b;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001960
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001961 if (int_a < int_b)
1962 return -1;
1963 if (int_a > int_b)
1964 return 1;
1965 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001966}
1967
Tim Duesterhus23b29452020-11-24 22:22:56 +01001968#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001969/*
1970 * Build a hash of the accept-encoding header. The different parts of the
1971 * header value are first sorted, appended and then a crc is calculated
1972 * for the newly constructed buffer.
1973 * Returns 0 in case of success.
1974 */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001975static int accept_encoding_normalizer(struct ist full_value, char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001976{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001977 unsigned int values[ACCEPT_ENCODING_MAX_ENTRIES] = {};
Tim Duesterhus23b29452020-11-24 22:22:56 +01001978 size_t count = 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001979 char *comma = NULL;
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001980 unsigned int hash_value = 0;
1981 unsigned int prev = 0, curr = 0;
1982
1983 /* Turn accept-encoding value to lower case */
1984 full_value = ist2bin_lc(istptr(full_value), full_value);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001985
1986 /* The hash will be built out of a sorted list of accepted encodings. */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001987 while (count < (ACCEPT_ENCODING_MAX_ENTRIES - 1) && (comma = istchr(full_value, ',')) != NULL) {
1988 size_t length = comma - istptr(full_value);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001989
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001990 values[count++] = hash_crc32(istptr(full_value), length);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001991
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001992 full_value = istadv(full_value, length + 1);
Tim Duesterhus23b29452020-11-24 22:22:56 +01001993
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001994 }
1995 values[count++] = hash_crc32(istptr(full_value), istlen(full_value));
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001996
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01001997 /* Sort the values alphabetically. */
1998 qsort(values, count, sizeof(*values), &accept_encoding_cmp);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001999
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002000 while (count) {
2001 curr = values[--count];
2002 if (curr != prev) {
2003 hash_value ^= curr;
2004 }
2005 prev = curr;
2006 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002007
2008 memcpy(buf, &hash_value, sizeof(hash_value));
2009 *buf_len = sizeof(hash_value);
2010
Tim Duesterhus23b29452020-11-24 22:22:56 +01002011 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002012}
Tim Duesterhus23b29452020-11-24 22:22:56 +01002013#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002014
2015/*
2016 * Normalizer used by default for User-Agent and Referer headers. It only
2017 * calculates a simple crc of the whole value.
2018 * Returns 0 in case of success.
2019 */
2020static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len)
2021{
2022 int hash_value = 0;
2023
2024 hash_value = hash_crc32(istptr(value), istlen(value));
2025
2026 memcpy(buf, &hash_value, sizeof(hash_value));
2027 *buf_len = sizeof(hash_value);
2028
2029 return 0;
2030}
2031
2032
2033/*
2034 * Pre-calculate the hashes of all the supported headers (in our Vary
2035 * implementation) of a given request. We have to calculate all the hashes
2036 * in advance because the actual Vary signature won't be known until the first
2037 * response.
2038 * Only the first occurrence of every header will be taken into account in the
2039 * hash.
2040 * If the header is not present, the hash portion of the given header will be
2041 * filled with zeros.
2042 * Returns 0 in case of success.
2043 */
2044static int http_request_prebuild_full_secondary_key(struct stream *s)
2045{
2046 struct http_txn *txn = s->txn;
2047 struct htx *htx = htxbuf(&s->req.buf);
2048 struct http_hdr_ctx ctx = { .blk = NULL };
2049
2050 unsigned int idx;
2051 const struct vary_hashing_information *info = NULL;
2052 unsigned int hash_length = 0;
2053 int retval = 0;
2054 int offset = 0;
2055
2056 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
2057 info = &vary_information[idx];
2058
2059 ctx.blk = NULL;
2060 if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) {
2061 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
2062 offset += hash_length;
2063 }
2064 else {
2065 /* Fill hash with 0s. */
2066 hash_length = info->hash_length;
2067 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
2068 offset += hash_length;
2069 }
2070 }
2071
2072 return retval;
2073}
2074
2075
2076/*
2077 * Calculate the secondary key for a request for which we already have a known
2078 * vary signature. The key is made by aggregating hashes calculated for every
2079 * header mentioned in the vary signature.
2080 * Only the first occurrence of every header will be taken into account in the
2081 * hash.
2082 * If the header is not present, the hash portion of the given header will be
2083 * filled with zeros.
2084 * Returns 0 in case of success.
2085 */
2086static int http_request_build_secondary_key(struct stream *s, int vary_signature)
2087{
2088 struct http_txn *txn = s->txn;
2089 struct htx *htx = htxbuf(&s->req.buf);
2090 struct http_hdr_ctx ctx = { .blk = NULL };
2091
2092 unsigned int idx;
2093 const struct vary_hashing_information *info = NULL;
2094 unsigned int hash_length = 0;
2095 int retval = 0;
2096 int offset = 0;
2097
2098 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
2099 info = &vary_information[idx];
2100
2101 ctx.blk = NULL;
2102 if ((vary_signature & info->value) && info->norm_fn != NULL &&
2103 http_find_header(htx, info->hdr_name, &ctx, 1)) {
2104 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
2105 offset += hash_length;
2106 }
2107 else {
2108 /* Fill hash with 0s. */
2109 hash_length = info->hash_length;
2110 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
2111 offset += hash_length;
2112 }
2113 }
2114
2115 return retval;
2116}
2117
2118/*
2119 * Build the actual secondary key of a given request out of the prebuilt key and
2120 * the actual vary signature (extracted from the response).
2121 * Returns 0 in case of success.
2122 */
2123static int http_request_reduce_secondary_key(unsigned int vary_signature,
2124 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
2125{
2126 int offset = 0;
2127 int global_offset = 0;
2128 int vary_info_count = 0;
2129 int keep = 0;
2130 unsigned int vary_idx;
2131 const struct vary_hashing_information *vary_info;
2132
2133 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
2134 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
2135 vary_info = &vary_information[vary_idx];
2136 keep = (vary_signature & vary_info->value) ? 0xff : 0;
2137
2138 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
2139 prebuilt_key[global_offset] &= keep;
2140 }
2141 }
2142
2143 return 0;
2144}
2145
2146
Christopher Faulet99a17a22018-12-11 09:18:27 +01002147
2148static int
2149parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
2150 struct flt_conf *fconf, char **err, void *private)
2151{
2152 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01002153 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002154 char *name = NULL;
2155 int pos = *cur_arg;
2156
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002157 /* Get the cache filter name. <pos> point on "cache" keyword */
2158 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02002159 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002160 goto error;
2161 }
2162 name = strdup(args[pos + 1]);
2163 if (!name) {
2164 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
2165 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002166 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002167 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002168
2169 /* Check if an implicit filter with the same name already exists. If so,
2170 * we remove the implicit filter to use the explicit one. */
2171 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
2172 if (f->id != cache_store_flt_id)
2173 continue;
2174
2175 cconf = f->conf;
2176 if (strcmp(name, cconf->c.name)) {
2177 cconf = NULL;
2178 continue;
2179 }
2180
2181 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2182 cconf = NULL;
2183 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2184 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002185 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002186 }
2187
2188 /* Remove the implicit filter. <cconf> is kept for the explicit one */
2189 LIST_DEL(&f->list);
2190 free(f);
2191 free(name);
2192 break;
2193 }
2194
2195 /* No implicit cache filter found, create configuration for the explicit one */
2196 if (!cconf) {
2197 cconf = calloc(1, sizeof(*cconf));
2198 if (!cconf) {
2199 memprintf(err, "%s: out of memory", args[*cur_arg]);
2200 goto error;
2201 }
2202 cconf->c.name = name;
2203 }
2204
2205 cconf->flags = 0;
2206 fconf->id = cache_store_flt_id;
2207 fconf->conf = cconf;
2208 fconf->ops = &cache_ops;
2209
2210 *cur_arg = pos;
2211 return 0;
2212
2213 error:
2214 free(name);
2215 free(cconf);
2216 return -1;
2217}
2218
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002219static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002220{
2221 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2222 return 1;
2223
2224 return 0;
2225}
2226
2227static int cli_io_handler_show_cache(struct appctx *appctx)
2228{
2229 struct cache* cache = appctx->ctx.cli.p0;
2230 struct stream_interface *si = appctx->owner;
2231
William Lallemand1f49a362017-11-21 20:01:26 +01002232 if (cache == NULL) {
2233 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
2234 }
2235
2236 list_for_each_entry_from(cache, &caches, list) {
2237 struct eb32_node *node = NULL;
2238 unsigned int next_key;
2239 struct cache_entry *entry;
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002240 unsigned int i;
William Lallemand1f49a362017-11-21 20:01:26 +01002241
William Lallemand1f49a362017-11-21 20:01:26 +01002242 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002243 if (!next_key) {
2244 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
2245 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002246 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002247 return 0;
2248 }
2249 }
William Lallemand1f49a362017-11-21 20:01:26 +01002250
2251 appctx->ctx.cli.p0 = cache;
2252
2253 while (1) {
2254
2255 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002256 if (!node || (node = eb32_next_dup(node)) == NULL)
2257 node = eb32_lookup_ge(&cache->entries, next_key);
William Lallemand1f49a362017-11-21 20:01:26 +01002258 if (!node) {
2259 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02002260 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002261 break;
2262 }
2263
2264 entry = container_of(node, struct cache_entry, eb);
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002265 chunk_printf(&trash, "%p hash:%u vary:0x", entry, read_u32(entry->hash));
2266 for (i = 0; i < HTTP_CACHE_SEC_KEY_LEN; ++i)
2267 chunk_appendf(&trash, "%02x", (unsigned char)entry->secondary_key[i]);
2268 chunk_appendf(&trash, " size:%u (%u blocks), refcount:%u, expire:%d\n", block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec);
William Lallemand1f49a362017-11-21 20:01:26 +01002269
2270 next_key = node->key + 1;
2271 appctx->ctx.cli.i0 = next_key;
2272
2273 shctx_unlock(shctx_ptr(cache));
2274
2275 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002276 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01002277 return 0;
2278 }
2279 }
2280
2281 }
2282
2283 return 1;
2284
2285}
2286
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002287
2288/*
2289 * boolean, returns true if response was built out of a cache entry.
2290 */
2291static int
2292smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2293 const char *kw, void *private)
2294{
2295 smp->data.type = SMP_T_BOOL;
2296 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2297
2298 return 1;
2299}
2300
2301/*
2302 * string, returns cache name (if response came from a cache).
2303 */
2304static int
2305smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2306 const char *kw, void *private)
2307{
2308 struct appctx *appctx = NULL;
2309
2310 struct cache_flt_conf *cconf = NULL;
2311 struct cache *cache = NULL;
2312
2313 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2314 return 0;
2315
2316 /* Get appctx from the stream_interface. */
2317 appctx = si_appctx(&smp->strm->si[1]);
2318 if (appctx && appctx->rule) {
2319 cconf = appctx->rule->arg.act.p[0];
2320 if (cconf) {
2321 cache = cconf->c.cache;
2322
2323 smp->data.type = SMP_T_STR;
2324 smp->flags = SMP_F_CONST;
2325 smp->data.u.str.area = cache->id;
2326 smp->data.u.str.data = strlen(cache->id);
2327 return 1;
2328 }
2329 }
2330
2331 return 0;
2332}
2333
Christopher Faulet99a17a22018-12-11 09:18:27 +01002334/* Declare the filter parser for "cache" keyword */
2335static struct flt_kw_list filter_kws = { "CACHE", { }, {
2336 { "cache", parse_cache_flt, NULL },
2337 { NULL, NULL, NULL },
2338 }
2339};
2340
2341INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2342
William Lallemand1f49a362017-11-21 20:01:26 +01002343static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01002344 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
2345 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002346}};
2347
Willy Tarreau0108d902018-11-25 19:14:37 +01002348INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002349
William Lallemand41db4602017-10-30 11:15:51 +01002350static struct action_kw_list http_res_actions = {
2351 .kw = {
2352 { "cache-store", parse_cache_store },
2353 { NULL, NULL }
2354 }
2355};
2356
Willy Tarreau0108d902018-11-25 19:14:37 +01002357INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2358
William Lallemand41db4602017-10-30 11:15:51 +01002359static struct action_kw_list http_req_actions = {
2360 .kw = {
2361 { "cache-use", parse_cache_use },
2362 { NULL, NULL }
2363 }
2364};
2365
Willy Tarreau0108d902018-11-25 19:14:37 +01002366INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2367
Willy Tarreau2231b632019-03-29 18:26:52 +01002368struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002369 .obj_type = OBJ_TYPE_APPLET,
2370 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002371 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002372 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002373};
2374
Willy Tarreaue6552512018-11-26 11:33:13 +01002375/* config parsers for this section */
2376REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002377REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002378
2379
2380/* Note: must not be declared <const> as its list will be overwritten */
2381static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2382 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2383 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2384 { /* END */ },
2385 }
2386};
2387
2388INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);