blob: bb437bff19cc8db438b062758bf770d5ebaa08a8 [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 Tarreauc6dfef72022-05-05 16:46:13 +020018#include <haproxy/applet.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020020#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020021#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020023#include <haproxy/filters.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/hash.h>
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +020025#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020026#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020027#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020028#include <haproxy/http_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/htx.h>
30#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +010032#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020033#include <haproxy/sc_strm.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020034#include <haproxy/shctx.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020035#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/stream.h>
Willy Tarreauce6700a2021-05-08 13:03:55 +020037#include <haproxy/tools.h>
William Lallemand41db4602017-10-30 11:15:51 +010038
Christopher Faulet27d93c32018-12-15 22:32:02 +010039#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010040 * the filter keyword) */
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +020041#define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */
Christopher Fauletafd819c2018-12-11 08:57:45 +010042
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010043const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010044
Willy Tarreau2231b632019-03-29 18:26:52 +010045extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010046
47struct flt_ops cache_ops;
48
49struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010050 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010051 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010052 unsigned int maxage; /* max-age */
53 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020054 unsigned int maxobjsz; /* max-object-size (in bytes) */
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +010055 unsigned int max_secondary_entries; /* maximum number of secondary entries with the same primary hash */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +010056 uint8_t vary_processing_enabled; /* boolean : manage Vary header (disabled by default) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010057 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010058};
59
Willy Tarreauf61494c2022-05-06 11:03:39 +020060/* the appctx context of a cache applet, stored in appctx->svcctx */
61struct cache_appctx {
62 struct cache_entry *entry; /* Entry to be sent from cache. */
63 unsigned int sent; /* The number of bytes already sent for this cache entry. */
64 unsigned int offset; /* start offset of remaining data relative to beginning of the next block */
65 unsigned int rem_data; /* Remaining bytes for the last data block (HTX only, 0 means process next block) */
66 unsigned int send_notmodified:1; /* In case of conditional request, we might want to send a "304 Not Modified" response instead of the stored data. */
67 unsigned int unused:31;
68 struct shared_block *next; /* The next block of data to be sent for this cache entry. */
69};
70
Christopher Faulet95220e22018-12-07 17:34:39 +010071/* cache config for filters */
72struct cache_flt_conf {
73 union {
74 struct cache *cache; /* cache used by the filter */
75 char *name; /* cache name used during conf parsing */
76 } c;
77 unsigned int flags; /* CACHE_FLT_F_* */
78};
79
Willy Tarreauc6dfef72022-05-05 16:46:13 +020080/* CLI context used during "show cache" */
81struct show_cache_ctx {
82 struct cache *cache;
83 uint next_key;
84};
85
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +010086
87/*
88 * Vary-related structures and functions
89 */
90enum vary_header_bit {
91 VARY_ACCEPT_ENCODING = (1 << 0),
92 VARY_REFERER = (1 << 1),
93 VARY_LAST /* should always be last */
94};
95
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +010096/*
97 * Encoding list extracted from
98 * https://www.iana.org/assignments/http-parameters/http-parameters.xhtml
99 * and RFC7231#5.3.4.
100 */
101enum vary_encoding {
102 VARY_ENCODING_GZIP = (1 << 0),
103 VARY_ENCODING_DEFLATE = (1 << 1),
104 VARY_ENCODING_BR = (1 << 2),
105 VARY_ENCODING_COMPRESS = (1 << 3),
106 VARY_ENCODING_AES128GCM = (1 << 4),
107 VARY_ENCODING_EXI = (1 << 5),
108 VARY_ENCODING_PACK200_GZIP = (1 << 6),
109 VARY_ENCODING_ZSTD = (1 << 7),
110 VARY_ENCODING_IDENTITY = (1 << 8),
111 VARY_ENCODING_STAR = (1 << 9),
112 VARY_ENCODING_OTHER = (1 << 10)
113};
114
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100115struct vary_hashing_information {
116 struct ist hdr_name; /* Header name */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +0500117 enum vary_header_bit value; /* Bit representing the header in a vary signature */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100118 unsigned int hash_length; /* Size of the sub hash for this header's value */
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100119 int(*norm_fn)(struct htx*,struct ist hdr_name,char* buf,unsigned int* buf_len); /* Normalization function */
Tim Duesterhused84d842021-01-18 13:41:17 +0100120 int(*cmp_fn)(const void *ref, const void *new, unsigned int len); /* Comparison function, should return 0 if the hashes are alike */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100121};
122
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100123static int http_request_prebuild_full_secondary_key(struct stream *s);
124static int http_request_build_secondary_key(struct stream *s, int vary_signature);
125static int http_request_reduce_secondary_key(unsigned int vary_signature,
126 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]);
127
128static int parse_encoding_value(struct ist value, unsigned int *encoding_value,
129 unsigned int *has_null_weight);
130
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +0100131static int accept_encoding_normalizer(struct htx *htx, struct ist hdr_name,
132 char *buf, unsigned int *buf_len);
133static int default_normalizer(struct htx *htx, struct ist hdr_name,
134 char *buf, unsigned int *buf_len);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100135
Tim Duesterhused84d842021-01-18 13:41:17 +0100136static int accept_encoding_bitmap_cmp(const void *ref, const void *new, unsigned int len);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100137
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100138/* Warning : do not forget to update HTTP_CACHE_SEC_KEY_LEN when new items are
139 * added to this array. */
140const struct vary_hashing_information vary_information[] = {
Tim Duesterhused84d842021-01-18 13:41:17 +0100141 { IST("accept-encoding"), VARY_ACCEPT_ENCODING, sizeof(uint32_t), &accept_encoding_normalizer, &accept_encoding_bitmap_cmp },
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100142 { IST("referer"), VARY_REFERER, sizeof(int), &default_normalizer, NULL },
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100143};
144
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100145
William Lallemand41db4602017-10-30 11:15:51 +0100146/*
147 * cache ctx for filters
148 */
149struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +0100150 struct shared_block *first_block;
151};
152
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100153#define DEFAULT_MAX_SECONDARY_ENTRY 10
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100154
William Lallemand41db4602017-10-30 11:15:51 +0100155struct cache_entry {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100156 unsigned int complete; /* An entry won't be valid until complete is not null. */
William Lallemand41db4602017-10-30 11:15:51 +0100157 unsigned int latest_validation; /* latest validation date */
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100158 unsigned int expire; /* expiration date (wall clock time) */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200159 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100160
William Lallemand41db4602017-10-30 11:15:51 +0100161 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +0100162 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200163
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100164 char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */
165 unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used
166 * to build secondary keys for this cache entry. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100167 unsigned int secondary_entries_count; /* Should only be filled in the last entry of a list of dup entries */
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100168 unsigned int last_clear_ts; /* Timestamp of the last call to clear_expired_duplicates. */
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100169
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200170 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
171 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
172
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200173 time_t last_modified; /* Origin server "Last-Modified" header value converted in
174 * seconds since epoch. If no "Last-Modified"
175 * header is found, use "Date" header value,
176 * otherwise use reception time. This field will
177 * be used in case of an "If-Modified-Since"-based
178 * conditional request. */
179
William Lallemand41db4602017-10-30 11:15:51 +0100180 unsigned char data[0];
181};
182
183#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +0100184#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100185
186static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +0200187static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +0100188static struct cache *tmp_cache_config = NULL;
189
Willy Tarreau8ceae722018-11-26 11:58:30 +0100190DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
191
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100192static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry);
193static void delete_entry(struct cache_entry *del_entry);
194
William Lallemandf528fff2017-11-23 19:43:17 +0100195struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100196{
197 struct eb32_node *node;
198 struct cache_entry *entry;
199
Willy Tarreau8b507582020-02-25 09:35:07 +0100200 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100201 if (!node)
202 return NULL;
203
204 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100205
206 /* if that's not the right node */
207 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
208 return NULL;
209
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100210 if (entry->expire > date.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100211 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100212 } else {
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100213 delete_entry(entry);
William Lallemand08727662017-11-21 20:01:27 +0100214 entry->eb.key = 0;
215 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100216 return NULL;
217
218}
219
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100220
221/*
222 * Compare a newly built secondary key to the one found in a cache_entry.
223 * Every sub-part of the key is compared to the reference through the dedicated
224 * comparison function of the sub-part (that might do more than a simple
225 * memcmp).
226 * Returns 0 if the keys are alike.
227 */
228static int secondary_key_cmp(const char *ref_key, const char *new_key)
229{
230 int retval = 0;
Tim Duesterhus5897cfe2021-01-18 13:41:18 +0100231 size_t idx = 0;
232 unsigned int offset = 0;
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100233 const struct vary_hashing_information *info;
234
235 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
236 info = &vary_information[idx];
237
238 if (info->cmp_fn)
239 retval = info->cmp_fn(&ref_key[offset], &new_key[offset], info->hash_length);
240 else
241 retval = memcmp(&ref_key[offset], &new_key[offset], info->hash_length);
242
243 offset += info->hash_length;
244 }
245
246 return retval;
247}
248
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100249/*
250 * There can be multiple entries with the same primary key in the ebtree so in
251 * order to get the proper one out of the list, we use a secondary_key.
252 * This function simply iterates over all the entries with the same primary_key
253 * until it finds the right one.
254 * Returns the cache_entry in case of success, NULL otherwise.
255 */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100256struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry,
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100257 const char *secondary_key)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100258{
259 struct eb32_node *node = &entry->eb;
260
261 if (!entry->secondary_key_signature)
262 return NULL;
263
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100264 while (entry && secondary_key_cmp(entry->secondary_key, secondary_key) != 0) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100265 node = eb32_next_dup(node);
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100266
267 /* Make the best use of this iteration and clear expired entries
268 * when we find them. Calling delete_entry would be too costly
269 * so we simply call eb32_delete. The secondary_entry count will
270 * be updated when we try to insert a new entry to this list. */
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100271 if (entry->expire <= date.tv_sec) {
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100272 eb32_delete(&entry->eb);
273 entry->eb.key = 0;
274 }
275
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100276 entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL;
277 }
278
279 /* Expired entry */
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100280 if (entry && entry->expire <= date.tv_sec) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100281 eb32_delete(&entry->eb);
282 entry->eb.key = 0;
283 entry = NULL;
284 }
285
286 return entry;
287}
288
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100289
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100290/*
291 * Remove all expired entries from a list of duplicates.
292 * Return the number of alive entries in the list and sets dup_tail to the
293 * current last item of the list.
294 */
295static unsigned int clear_expired_duplicates(struct eb32_node **dup_tail)
296{
297 unsigned int entry_count = 0;
298 struct cache_entry *entry = NULL;
299 struct eb32_node *prev = *dup_tail;
300 struct eb32_node *tail = NULL;
301
302 while (prev) {
303 entry = container_of(prev, struct cache_entry, eb);
304 prev = eb32_prev_dup(prev);
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100305 if (entry->expire <= date.tv_sec) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100306 eb32_delete(&entry->eb);
307 entry->eb.key = 0;
308 }
309 else {
310 if (!tail)
311 tail = &entry->eb;
312 ++entry_count;
313 }
314 }
315
316 *dup_tail = tail;
317
318 return entry_count;
319}
320
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100321
322/*
323 * This function inserts a cache_entry in the cache's ebtree. In case of
324 * duplicate entries (vary), it then checks that the number of entries did not
325 * reach the max number of secondary entries. If this entry should not have been
326 * created, remove it.
327 * In the regular case (unique entries), this function does not do more than a
328 * simple insert. In case of secondary entries, it will at most cost an
329 * insertion+max_sec_entries time checks and entry deletion.
330 * Returns the newly inserted node in case of success, NULL otherwise.
331 */
332static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry)
333{
334 struct eb32_node *prev = NULL;
335 struct cache_entry *entry = NULL;
336 unsigned int entry_count = 0;
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100337 unsigned int last_clear_ts = date.tv_sec;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100338
339 struct eb32_node *node = eb32_insert(&cache->entries, &new_entry->eb);
340
341 /* We should not have multiple entries with the same primary key unless
342 * the entry has a non null vary signature. */
343 if (!new_entry->secondary_key_signature)
344 return node;
345
346 prev = eb32_prev_dup(node);
347 if (prev != NULL) {
348 /* The last entry of a duplicate list should contain the current
349 * number of entries in the list. */
350 entry = container_of(prev, struct cache_entry, eb);
351 entry_count = entry->secondary_entries_count;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100352 last_clear_ts = entry->last_clear_ts;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100353
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100354 if (entry_count >= cache->max_secondary_entries) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100355 /* Some entries of the duplicate list might be expired so
356 * we will iterate over all the items in order to free some
357 * space. In order to avoid going over the same list too
358 * often, we first check the timestamp of the last check
359 * performed. */
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100360 if (last_clear_ts == date.tv_sec) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100361 /* Too many entries for this primary key, clear the
362 * one that was inserted. */
363 eb32_delete(node);
364 node->key = 0;
365 return NULL;
366 }
367
368 entry_count = clear_expired_duplicates(&prev);
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100369 if (entry_count >= cache->max_secondary_entries) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100370 /* Still too many entries for this primary key, delete
371 * the newly inserted one. */
372 entry = container_of(prev, struct cache_entry, eb);
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100373 entry->last_clear_ts = date.tv_sec;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100374 eb32_delete(node);
375 node->key = 0;
376 return NULL;
377 }
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100378 }
379 }
380
381 new_entry->secondary_entries_count = entry_count + 1;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100382 new_entry->last_clear_ts = last_clear_ts;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100383
384 return node;
385}
386
387
388/*
389 * This function removes an entry from the ebtree. If the entry was a duplicate
390 * (in case of Vary), it updates the secondary entry counter in another
391 * duplicate entry (the last entry of the dup list).
392 */
393static void delete_entry(struct cache_entry *del_entry)
394{
395 struct eb32_node *prev = NULL, *next = NULL;
396 struct cache_entry *entry = NULL;
397 struct eb32_node *last = NULL;
398
399 if (del_entry->secondary_key_signature) {
400 next = &del_entry->eb;
401
402 /* Look for last entry of the duplicates list. */
403 while ((next = eb32_next_dup(next))) {
404 last = next;
405 }
406
407 if (last) {
408 entry = container_of(last, struct cache_entry, eb);
409 --entry->secondary_entries_count;
410 }
411 else {
412 /* The current entry is the last one, look for the
413 * previous one to update its counter. */
414 prev = eb32_prev_dup(&del_entry->eb);
415 if (prev) {
416 entry = container_of(prev, struct cache_entry, eb);
417 entry->secondary_entries_count = del_entry->secondary_entries_count - 1;
418 }
419 }
420 }
421 eb32_delete(&del_entry->eb);
422 del_entry->eb.key = 0;
423}
424
425
William Lallemand4da3f8a2017-10-31 14:33:34 +0100426static inline struct shared_context *shctx_ptr(struct cache *cache)
427{
Ilya Shipitsin2ca01582023-04-15 23:39:43 +0200428 return (struct shared_context *)((unsigned char *)cache - offsetof(struct shared_context, data));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100429}
430
William Lallemand77c11972017-10-31 20:43:01 +0100431static inline struct shared_block *block_ptr(struct cache_entry *entry)
432{
Ilya Shipitsin2ca01582023-04-15 23:39:43 +0200433 return (struct shared_block *)((unsigned char *)entry - offsetof(struct shared_block, data));
William Lallemand77c11972017-10-31 20:43:01 +0100434}
435
436
437
William Lallemand41db4602017-10-30 11:15:51 +0100438static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100439cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100440{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100441 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100442 return 0;
443}
444
Christopher Faulet95220e22018-12-07 17:34:39 +0100445static void
446cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
447{
448 struct cache_flt_conf *cconf = fconf->conf;
449
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200450 if (!(cconf->flags & CACHE_FLT_INIT))
451 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100452 free(cconf);
453}
454
William Lallemand4da3f8a2017-10-31 14:33:34 +0100455static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100456cache_store_check(struct proxy *px, struct flt_conf *fconf)
457{
458 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100459 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100460 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100461 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100462
William Lallemandd1d1e222019-08-28 15:22:49 +0200463 /* Find the cache corresponding to the name in the filter config. The
464 * cache will not be referenced now in the filter config because it is
465 * not fully allocated. This step will be performed during the cache
466 * post_check.
467 */
468 list_for_each_entry(cache, &caches_config, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100469 if (strcmp(cache->id, cconf->c.name) == 0)
Christopher Faulet95220e22018-12-07 17:34:39 +0100470 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100471 }
472
473 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
474 proxy_type_str(px), px->id, (char *)cconf->c.name);
475 return 1;
476
477 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100478 /* Here <cache> points on the cache the filter must use and <cconf>
479 * points on the cache filter configuration. */
480
481 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100482 * enabled and if it is after the cache. When the compression is before
483 * the cache, an error is returned. Also check if the cache filter must
484 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100485 list_for_each_entry(f, &px->filter_configs, list) {
486 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100487 /* The compression filter must be evaluated after the cache. */
488 if (comp) {
489 ha_alert("config: %s '%s': unable to enable the compression filter before "
490 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
491 return 1;
492 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100493 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200494 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100495 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200496 else if (f->id == fcgi_flt_id)
497 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100498 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
499 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200500 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100501 * declaration is required. */
502 ha_alert("config: %s '%s': require an explicit filter declaration "
503 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
504 return 1;
505 }
506
Christopher Fauletafd819c2018-12-11 08:57:45 +0100507 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100508 return 0;
509}
510
511static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100512cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100513{
Christopher Faulet65554e12020-03-06 14:52:06 +0100514 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100515
Willy Tarreauacc5b012021-03-22 15:00:49 +0100516 st = pool_alloc(pool_head_cache_st);
Christopher Faulet65554e12020-03-06 14:52:06 +0100517 if (st == NULL)
518 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100519
Christopher Faulet65554e12020-03-06 14:52:06 +0100520 st->first_block = NULL;
521 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100522
Christopher Faulet65554e12020-03-06 14:52:06 +0100523 /* Register post-analyzer on AN_RES_WAIT_HTTP */
524 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100525 return 1;
526}
527
Christopher Faulet65554e12020-03-06 14:52:06 +0100528static void
529cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100530{
531 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100532 struct cache_flt_conf *cconf = FLT_CONF(filter);
533 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100534 struct shared_context *shctx = shctx_ptr(cache);
535
William Lallemand49dc0482017-11-24 14:33:54 +0100536 /* Everything should be released in the http_end filter, but we need to do it
537 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100538 if (st && st->first_block) {
Remi Tricot-Le Bretonf5c24702023-11-28 17:08:56 +0100539 struct cache_entry *object = (struct cache_entry *)st->first_block->data;
540
William Lallemand49dc0482017-11-24 14:33:54 +0100541 shctx_lock(shctx);
Remi Tricot-Le Bretonf5c24702023-11-28 17:08:56 +0100542 if (!object->complete) {
543 /* The stream was closed but the 'complete' flag was not
544 * set which means that cache_store_http_end was not
545 * called. The stream must have been closed before we
546 * could store the full answer in the cache.
547 */
548 delete_entry(object);
549 }
William Lallemand49dc0482017-11-24 14:33:54 +0100550 shctx_row_dec_hot(shctx, st->first_block);
551 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100552 }
553 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100554 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100555 filter->ctx = NULL;
556 }
William Lallemand49dc0482017-11-24 14:33:54 +0100557}
558
Christopher Faulet839791a2019-01-07 16:12:07 +0100559static int
560cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
561 unsigned an_bit)
562{
563 struct http_txn *txn = s->txn;
564 struct http_msg *msg = &txn->rsp;
565 struct cache_st *st = filter->ctx;
566
567 if (an_bit != AN_RES_WAIT_HTTP)
568 goto end;
569
570 /* Here we need to check if any compression filter precedes the cache
571 * filter. This is only possible when the compression is configured in
572 * the frontend while the cache filter is configured on the
573 * backend. This case cannot be detected during HAProxy startup. So in
574 * such cases, the cache is disabled.
575 */
576 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
577 pool_free(pool_head_cache_st, st);
578 filter->ctx = NULL;
579 }
580
581 end:
582 return 1;
583}
William Lallemand49dc0482017-11-24 14:33:54 +0100584
585static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100586cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
587{
588 struct cache_st *st = filter->ctx;
589
William Lallemand4da3f8a2017-10-31 14:33:34 +0100590 if (!(msg->chn->flags & CF_ISRESP) || !st)
591 return 1;
592
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200593 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100594 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100595 return 1;
596}
597
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200598static inline void disable_cache_entry(struct cache_st *st,
599 struct filter *filter, struct shared_context *shctx)
600{
601 struct cache_entry *object;
602
603 object = (struct cache_entry *)st->first_block->data;
604 filter->ctx = NULL; /* disable cache */
605 shctx_lock(shctx);
606 shctx_row_dec_hot(shctx, st->first_block);
Remi Tricot-Le Breton964caaf2020-12-15 14:30:12 +0100607 eb32_delete(&object->eb);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200608 object->eb.key = 0;
609 shctx_unlock(shctx);
610 pool_free(pool_head_cache_st, st);
611}
612
William Lallemand4da3f8a2017-10-31 14:33:34 +0100613static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100614cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
615 unsigned int offset, unsigned int len)
616{
Christopher Faulet95220e22018-12-07 17:34:39 +0100617 struct cache_flt_conf *cconf = FLT_CONF(filter);
618 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100619 struct cache_st *st = filter->ctx;
620 struct htx *htx = htxbuf(&msg->chn->buf);
621 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200622 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100623 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200624 unsigned int orig_len, to_forward;
625 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100626
627 if (!len)
628 return len;
629
630 if (!st->first_block) {
631 unregister_data_filter(s, msg->chn, filter);
632 return len;
633 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100634
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200635 chunk_reset(&trash);
636 orig_len = len;
637 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100638
639 htxret = htx_find_offset(htx, offset);
640 blk = htxret.blk;
641 offset = htxret.ret;
642 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100643 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200644 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100645 struct ist v;
646
647 switch (type) {
648 case HTX_BLK_UNUSED:
649 break;
650
651 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100652 v = htx_get_blk_value(htx, blk);
Tim Duesterhus154374c2021-03-02 18:57:27 +0100653 v = istadv(v, offset);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +0100654 v = isttrim(v, len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100655
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200656 info = (type << 28) + v.len;
657 chunk_memcat(&trash, (char *)&info, sizeof(info));
Tim Duesterhus77508502022-03-15 13:11:06 +0100658 chunk_istcat(&trash, v);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100659 to_forward += v.len;
660 len -= v.len;
661 break;
662
663 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200664 /* Here offset must always be 0 because only
665 * DATA blocks can be partially transferred. */
666 if (offset)
667 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100668 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200669 goto end;
670
671 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
672 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100673 to_forward += sz;
674 len -= sz;
675 break;
676 }
677
678 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100679 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200680
681 end:
682 shctx_lock(shctx);
683 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
684 if (!fb) {
685 shctx_unlock(shctx);
686 goto no_cache;
687 }
688 shctx_unlock(shctx);
689
690 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
691 (unsigned char *)b_head(&trash), b_data(&trash));
692 if (ret < 0)
693 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100694
695 return to_forward;
696
697 no_cache:
698 disable_cache_entry(st, filter, shctx);
699 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200700 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100701}
702
703static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100704cache_store_http_end(struct stream *s, struct filter *filter,
705 struct http_msg *msg)
706{
707 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100708 struct cache_flt_conf *cconf = FLT_CONF(filter);
709 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100710 struct shared_context *shctx = shctx_ptr(cache);
711 struct cache_entry *object;
712
713 if (!(msg->chn->flags & CF_ISRESP))
714 return 1;
715
716 if (st && st->first_block) {
717
718 object = (struct cache_entry *)st->first_block->data;
719
William Lallemand4da3f8a2017-10-31 14:33:34 +0100720 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100721 /* The whole payload was cached, the entry can now be used. */
722 object->complete = 1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100723 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100724 shctx_row_dec_hot(shctx, st->first_block);
725 shctx_unlock(shctx);
726
727 }
728 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100729 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100730 filter->ctx = NULL;
731 }
732
733 return 1;
734}
735
736 /*
737 * This intends to be used when checking HTTP headers for some
738 * word=value directive. Return a pointer to the first character of value, if
Willy Tarreau94a01e12021-01-06 17:35:12 +0100739 * the word was not found or if there wasn't any value assigned to it return NULL
William Lallemand4da3f8a2017-10-31 14:33:34 +0100740 */
741char *directive_value(const char *sample, int slen, const char *word, int wlen)
742{
743 int st = 0;
744
745 if (slen < wlen)
746 return 0;
747
748 while (wlen) {
749 char c = *sample ^ *word;
750 if (c && c != ('A' ^ 'a'))
751 return NULL;
752 sample++;
753 word++;
754 slen--;
755 wlen--;
756 }
757
758 while (slen) {
759 if (st == 0) {
760 if (*sample != '=')
761 return NULL;
762 sample++;
763 slen--;
764 st = 1;
765 continue;
766 } else {
767 return (char *)sample;
768 }
769 }
770
771 return NULL;
772}
773
774/*
775 * Return the maxage in seconds of an HTTP response.
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100776 * The returned value will always take the cache's configuration into account
777 * (cache->maxage) but the actual max age of the response will be set in the
778 * true_maxage parameter. It will be used to determine if a response is already
779 * stale or not.
William Lallemand4da3f8a2017-10-31 14:33:34 +0100780 * Compute the maxage using either:
781 * - the assigned max-age of the cache
782 * - the s-maxage directive
783 * - the max-age directive
784 * - (Expires - Data) headers
785 * - the default-max-age of the cache
786 *
787 */
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100788int http_calc_maxage(struct stream *s, struct cache *cache, int *true_maxage)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100789{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200790 struct htx *htx = htxbuf(&s->res.buf);
791 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100792 long smaxage = -1;
793 long maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100794 int expires = -1;
795 struct tm tm = {};
796 time_t expires_val = 0;
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100797 char *endptr = NULL;
798 int offset = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100799
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100800 /* The Cache-Control max-age and s-maxage directives should be followed by
801 * a positive numerical value (see RFC 7234#5.2.1.1). According to the
802 * specs, a sender "should not" generate a quoted-string value but we will
803 * still accept this format since it isn't strictly forbidden. */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200804 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
805 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100806
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200807 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
808 if (value) {
809 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100810
Willy Tarreau49b04822021-11-08 11:44:47 +0100811 chunk_memcat(chk, value, ctx.value.len - 8 + 1);
812 chunk_memcat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100813 offset = (*chk->area == '"') ? 1 : 0;
814 smaxage = strtol(chk->area + offset, &endptr, 10);
Willy Tarreau1f38bdb2021-11-08 12:09:27 +0100815 if (unlikely(smaxage < 0 || endptr == chk->area + offset))
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100816 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100817 }
818
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200819 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
820 if (value) {
821 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200822
Willy Tarreau49b04822021-11-08 11:44:47 +0100823 chunk_memcat(chk, value, ctx.value.len - 7 + 1);
824 chunk_memcat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100825 offset = (*chk->area == '"') ? 1 : 0;
826 maxage = strtol(chk->area + offset, &endptr, 10);
Willy Tarreau1f38bdb2021-11-08 12:09:27 +0100827 if (unlikely(maxage < 0 || endptr == chk->area + offset))
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100828 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100829 }
830 }
831
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100832 /* Look for Expires header if no s-maxage or max-age Cache-Control data
833 * was found. */
834 if (maxage == -1 && smaxage == -1) {
835 ctx.blk = NULL;
836 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
837 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
838 expires_val = my_timegm(&tm);
839 /* A request having an expiring date earlier
840 * than the current date should be considered as
841 * stale. */
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100842 expires = (expires_val >= date.tv_sec) ?
843 (expires_val - date.tv_sec) : 0;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100844 }
845 else {
846 /* Following RFC 7234#5.3, an invalid date
847 * format must be treated as a date in the past
848 * so the cache entry must be seen as already
849 * expired. */
850 expires = 0;
851 }
852 }
853 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100854
855
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100856 if (smaxage > 0) {
857 if (true_maxage)
858 *true_maxage = smaxage;
William Lallemand49b44532017-11-24 18:53:43 +0100859 return MIN(smaxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100860 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100861
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100862 if (maxage > 0) {
863 if (true_maxage)
864 *true_maxage = maxage;
William Lallemand49b44532017-11-24 18:53:43 +0100865 return MIN(maxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100866 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100867
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100868 if (expires >= 0) {
869 if (true_maxage)
870 *true_maxage = expires;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100871 return MIN(expires, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100872 }
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100873
William Lallemand49b44532017-11-24 18:53:43 +0100874 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100875
876}
877
878
William Lallemanda400a3a2017-11-20 19:13:12 +0100879static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
880{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200881 struct cache_entry *object = (struct cache_entry *)block->data;
882
883 if (first == block && object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100884 delete_entry(object);
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200885 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100886}
887
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200888
889/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
890 * date value should be compared to a date determined by in a previous response (for
891 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
892 * value of the response's reception time (by decreasing order of priority). */
893static time_t get_last_modified_time(struct htx *htx)
894{
895 time_t last_modified = 0;
896 struct http_hdr_ctx ctx = { .blk = NULL };
897 struct tm tm = {};
898
899 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
900 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
901 last_modified = my_timegm(&tm);
902 }
903 }
904
905 if (!last_modified) {
906 ctx.blk = NULL;
907 if (http_find_header(htx, ist("date"), &ctx, 1)) {
908 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
909 last_modified = my_timegm(&tm);
910 }
911 }
912 }
913
914 /* Fallback on the current time if no "Last-Modified" or "Date" header
915 * was found. */
916 if (!last_modified)
Willy Tarreau9b5d57d2023-02-07 15:22:41 +0100917 last_modified = date.tv_sec;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200918
919 return last_modified;
920}
921
William Lallemand41db4602017-10-30 11:15:51 +0100922/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100923 * Checks the vary header's value. The headers on which vary should be applied
Ilya Shipitsinf38a0182020-12-21 01:16:17 +0500924 * must be explicitly supported in the vary_information array (see cache.c). If
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100925 * any other header is mentioned, we won't store the response.
926 * Returns 1 if Vary-based storage can work, 0 otherwise.
927 */
928static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
929{
930 unsigned int vary_idx;
931 unsigned int vary_info_count;
932 const struct vary_hashing_information *vary_info;
933 struct http_hdr_ctx ctx = { .blk = NULL };
934
935 int retval = 1;
936
937 *vary_signature = 0;
938
939 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
940 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
941 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
942 vary_info = &vary_information[vary_idx];
943 if (isteqi(ctx.value, vary_info->hdr_name)) {
944 *vary_signature |= vary_info->value;
945 break;
946 }
947 }
948 retval = (vary_idx < vary_info_count);
949 }
950
951 return retval;
952}
953
954
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100955/*
956 * Look for the accept-encoding part of the secondary_key and replace the
957 * encoding bitmap part of the hash with the actual encoding of the response,
958 * extracted from the content-encoding header value.
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100959 * Responses that have an unknown encoding will not be cached if they also
960 * "vary" on the accept-encoding value.
961 * Returns 0 if we found a known encoding in the response, -1 otherwise.
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100962 */
Remi Tricot-Le Bretonda7f2702024-04-24 14:32:19 +0200963static int set_secondary_key_encoding(struct htx *htx, unsigned int vary_signature, char *secondary_key)
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100964{
965 unsigned int resp_encoding_bitmap = 0;
966 const struct vary_hashing_information *info = vary_information;
967 unsigned int offset = 0;
968 unsigned int count = 0;
969 unsigned int hash_info_count = sizeof(vary_information)/sizeof(*vary_information);
970 unsigned int encoding_value;
971 struct http_hdr_ctx ctx = { .blk = NULL };
972
Remi Tricot-Le Bretonda7f2702024-04-24 14:32:19 +0200973 /* We must not set the accept encoding part of the secondary signature
974 * if the response does not vary on 'Accept Encoding'. */
975 if (!(vary_signature & VARY_ACCEPT_ENCODING))
976 return 0;
977
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100978 /* Look for the accept-encoding part of the secondary_key. */
979 while (count < hash_info_count && info->value != VARY_ACCEPT_ENCODING) {
980 offset += info->hash_length;
981 ++info;
982 ++count;
983 }
984
985 if (count == hash_info_count)
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100986 return -1;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100987
988 while (http_find_header(htx, ist("content-encoding"), &ctx, 0)) {
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100989 if (parse_encoding_value(ctx.value, &encoding_value, NULL))
990 return -1; /* Do not store responses with an unknown encoding */
991 resp_encoding_bitmap |= encoding_value;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100992 }
993
994 if (!resp_encoding_bitmap)
995 resp_encoding_bitmap |= VARY_ENCODING_IDENTITY;
996
997 /* Rewrite the bitmap part of the hash with the new bitmap that only
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500998 * corresponds the the response's encoding. */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100999 write_u32(secondary_key + offset, resp_encoding_bitmap);
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +01001000
1001 return 0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001002}
1003
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01001004
1005/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001006 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +01001007 * register a filter to store the data
1008 */
1009enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001010 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +01001011{
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001012 int effective_maxage = 0;
1013 int true_maxage = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001014 struct http_txn *txn = s->txn;
1015 struct http_msg *msg = &txn->rsp;
1016 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001017 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001018 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001019 struct cache *cache = cconf->c.cache;
1020 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +01001021 struct cache_st *cache_ctx = NULL;
1022 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +01001023 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001024 struct htx *htx;
1025 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +02001026 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001027 int32_t pos;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001028 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001029
William Lallemand4da3f8a2017-10-31 14:33:34 +01001030 /* Don't cache if the response came from a cache */
1031 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
1032 s->target == &http_cache_applet.obj_type) {
1033 goto out;
1034 }
1035
1036 /* cache only HTTP/1.1 */
1037 if (!(txn->req.flags & HTTP_MSGF_VER_11))
1038 goto out;
1039
Willy Tarreau6905d182019-10-01 17:59:17 +02001040 /* cache only GET method */
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001041 if (txn->meth != HTTP_METH_GET) {
1042 /* In case of successful unsafe method on a stored resource, the
1043 * cached entry must be invalidated (see RFC7234#4.4).
1044 * A "non-error response" is one with a 2xx (Successful) or 3xx
1045 * (Redirection) status code. */
1046 if (txn->status >= 200 && txn->status < 400) {
1047 switch (txn->meth) {
1048 case HTTP_METH_OPTIONS:
1049 case HTTP_METH_GET:
1050 case HTTP_METH_HEAD:
1051 case HTTP_METH_TRACE:
1052 break;
1053
1054 default: /* Any unsafe method */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001055 /* Discard any corresponding entry in case of successful
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001056 * unsafe request (such as PUT, POST or DELETE). */
1057 shctx_lock(shctx);
1058
1059 old = entry_exist(cconf->c.cache, txn->cache_hash);
1060 if (old) {
1061 eb32_delete(&old->eb);
1062 old->eb.key = 0;
1063 }
1064 shctx_unlock(shctx);
1065 }
1066 }
William Lallemand4da3f8a2017-10-31 14:33:34 +01001067 goto out;
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001068 }
William Lallemand4da3f8a2017-10-31 14:33:34 +01001069
Willy Tarreauc9036c02019-01-11 19:38:25 +01001070 /* cache key was not computed */
1071 if (!key)
1072 goto out;
1073
William Lallemand4da3f8a2017-10-31 14:33:34 +01001074 /* cache only 200 status code */
1075 if (txn->status != 200)
1076 goto out;
1077
Christopher Faulet839791a2019-01-07 16:12:07 +01001078 /* Find the corresponding filter instance for the current stream */
1079 list_for_each_entry(filter, &s->strm_flt.filters, list) {
1080 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
1081 /* No filter ctx, don't cache anything */
1082 if (!filter->ctx)
1083 goto out;
1084 cache_ctx = filter->ctx;
1085 break;
1086 }
1087 }
1088
1089 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001090 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001091
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001092 /* Do not cache too big objects. */
1093 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
1094 htx->data + htx->extra > shctx->max_obj_size)
1095 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001096
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001097 /* Only a subset of headers are supported in our Vary implementation. If
1098 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001099 * able to use the cache. Likewise, if Vary header support is disabled,
1100 * avoid caching responses that contain such a header. */
1101 ctx.blk = NULL;
1102 if (cache->vary_processing_enabled) {
1103 if (!http_check_vary_header(htx, &vary_signature))
1104 goto out;
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001105 if (vary_signature) {
1106 /* If something went wrong during the secondary key
1107 * building, do not store the response. */
1108 if (!(txn->flags & TX_CACHE_HAS_SEC_KEY))
1109 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001110 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001111 }
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001112 }
1113 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001114 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001115 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001116
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001117 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001118
Remi Tricot-Le Breton879debe2023-02-21 11:47:17 +01001119 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001120 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001121
1122 shctx_lock(shctx);
1123 old = entry_exist(cache, txn->cache_hash);
1124 if (old) {
1125 if (vary_signature)
1126 old = secondary_entry_exist(cconf->c.cache, old,
1127 txn->cache_secondary_hash);
1128 if (old) {
1129 if (!old->complete) {
1130 /* An entry with the same primary key is already being
1131 * created, we should not try to store the current
1132 * response because it will waste space in the cache. */
1133 shctx_unlock(shctx);
1134 goto out;
1135 }
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001136 delete_entry(old);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001137 old->eb.key = 0;
1138 }
1139 }
1140 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry));
1141 if (!first) {
1142 shctx_unlock(shctx);
1143 goto out;
1144 }
1145 /* the received memory is not initialized, we need at least to mark
1146 * the object as not indexed yet.
1147 */
1148 object = (struct cache_entry *)first->data;
1149 memset(object, 0, sizeof(*object));
1150 object->eb.key = key;
1151 object->secondary_key_signature = vary_signature;
1152 /* We need to temporarily set a valid expiring time until the actual one
1153 * is set by the end of this function (in case of concurrent accesses to
1154 * the same resource). This way the second access will find an existing
1155 * but not yet usable entry in the tree and will avoid storing its data. */
Willy Tarreau9b5d57d2023-02-07 15:22:41 +01001156 object->expire = date.tv_sec + 2;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001157
1158 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
1159 if (vary_signature)
1160 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
1161
1162 /* Insert the entry in the tree even if the payload is not cached yet. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001163 if (insert_entry(cache, object) != &object->eb) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001164 object->eb.key = 0;
1165 shctx_unlock(shctx);
1166 goto out;
1167 }
1168 shctx_unlock(shctx);
1169
1170 /* reserve space for the cache_entry structure */
1171 first->len = sizeof(struct cache_entry);
1172 first->last_append = NULL;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001173
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001174 /* Determine the entry's maximum age (taking into account the cache's
1175 * configuration) as well as the response's explicit max age (extracted
1176 * from cache-control directives or the expires header). */
1177 effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage);
1178
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001179 ctx.blk = NULL;
1180 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
Tim Duesterhusc2942842021-01-02 22:47:17 +01001181 long long hdr_age;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001182 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
1183 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
1184 hdr_age = CACHE_ENTRY_MAX_AGE;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001185 /* A response with an Age value greater than its
1186 * announced max age is stale and should not be stored. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001187 object->age = hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001188 if (unlikely(object->age > true_maxage))
1189 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001190 }
Remi Tricot-Le Breton51058d62020-12-03 18:19:32 +01001191 else
1192 goto out;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001193 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001194 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001195
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001196 /* Build a last-modified time that will be stored in the cache_entry and
1197 * compared to a future If-Modified-Since client header. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001198 object->last_modified = get_last_modified_time(htx);
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001199
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001200 chunk_reset(&trash);
1201 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1202 struct htx_blk *blk = htx_get_blk(htx, pos);
1203 enum htx_blk_type type = htx_get_blk_type(blk);
1204 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001205
Christopher Fauletb0667472019-09-03 22:22:12 +02001206 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001207 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
1208 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001209
1210 /* Look for optional ETag header.
1211 * We need to store the offset of the ETag value in order for
1212 * future conditional requests to be able to perform ETag
1213 * comparisons. */
1214 if (type == HTX_BLK_HDR) {
Tim Duesterhuse2fff102021-01-02 22:47:16 +01001215 struct ist header_name = htx_get_blk_name(htx, blk);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001216 if (isteq(header_name, ist("etag"))) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001217 object->etag_length = sz - istlen(header_name);
1218 object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001219 }
1220 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001221 if (type == HTX_BLK_EOH)
1222 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001223 }
1224
Christopher Fauletb0667472019-09-03 22:22:12 +02001225 /* Do not cache objects if the headers are too big. */
1226 if (hdrs_len > htx->size - global.tune.maxrewrite)
1227 goto out;
1228
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001229 /* If the response has a secondary_key, fill its key part related to
1230 * encodings with the actual encoding of the response. This way any
1231 * subsequent request having the same primary key will have its accepted
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +01001232 * encodings tested upon the cached response's one.
1233 * We will not cache a response that has an unknown encoding (not
Ilya Shipitsin7704b0e2021-01-23 02:11:59 +05001234 * explicitly supported in parse_encoding_value function). */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001235 if (cache->vary_processing_enabled && vary_signature)
Remi Tricot-Le Bretonda7f2702024-04-24 14:32:19 +02001236 if (set_secondary_key_encoding(htx, vary_signature, object->secondary_key))
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +01001237 goto out;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001238
William Lallemand4da3f8a2017-10-31 14:33:34 +01001239 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001240 if (!shctx_row_reserve_hot(shctx, first, trash.data)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +01001241 shctx_unlock(shctx);
1242 goto out;
1243 }
1244 shctx_unlock(shctx);
1245
William Lallemand4da3f8a2017-10-31 14:33:34 +01001246 /* cache the headers in a http action because it allows to chose what
1247 * to cache, for example you might want to cache a response before
1248 * modifying some HTTP headers, or on the contrary after modifying
1249 * those headers.
1250 */
William Lallemand4da3f8a2017-10-31 14:33:34 +01001251 /* does not need to be locked because it's in the "hot" list,
1252 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001253 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
1254 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001255
1256 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +01001257 if (cache_ctx) {
1258 cache_ctx->first_block = first;
Christopher Faulet839791a2019-01-07 16:12:07 +01001259 /* store latest value and expiration time */
Willy Tarreau9b5d57d2023-02-07 15:22:41 +01001260 object->latest_validation = date.tv_sec;
1261 object->expire = date.tv_sec + effective_maxage;
Christopher Faulet839791a2019-01-07 16:12:07 +01001262 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001263 }
1264
1265out:
1266 /* if does not cache */
1267 if (first) {
1268 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +01001269 first->len = 0;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001270 if (object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001271 delete_entry(object);
William Lallemand08727662017-11-21 20:01:27 +01001272 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001273 shctx_row_dec_hot(shctx, first);
1274 shctx_unlock(shctx);
1275 }
1276
William Lallemand41db4602017-10-30 11:15:51 +01001277 return ACT_RET_CONT;
1278}
1279
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001280#define HTX_CACHE_INIT 0 /* Initial state. */
1281#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
1282#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001283#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
1284#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001285
William Lallemandecb73b12017-11-24 14:33:55 +01001286static void http_cache_applet_release(struct appctx *appctx)
1287{
Willy Tarreauf61494c2022-05-06 11:03:39 +02001288 struct cache_appctx *ctx = appctx->svcctx;
Christopher Faulet95220e22018-12-07 17:34:39 +01001289 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
Willy Tarreauf61494c2022-05-06 11:03:39 +02001290 struct cache_entry *cache_ptr = ctx->entry;
Christopher Faulet95220e22018-12-07 17:34:39 +01001291 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +01001292 struct shared_block *first = block_ptr(cache_ptr);
1293
1294 shctx_lock(shctx_ptr(cache));
1295 shctx_row_dec_hot(shctx_ptr(cache), first);
1296 shctx_unlock(shctx_ptr(cache));
1297}
1298
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001299
1300static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
1301 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001302{
Willy Tarreauf61494c2022-05-06 11:03:39 +02001303 struct cache_appctx *ctx = appctx->svcctx;
Christopher Faulet95220e22018-12-07 17:34:39 +01001304 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1305 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001306 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001307 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001308 unsigned int max, total;
1309 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001310
Willy Tarreau0698c802022-05-11 14:09:57 +02001311 max = htx_get_max_blksz(htx,
Willy Tarreauc12b3212022-05-27 11:08:15 +02001312 channel_htx_recv_max(sc_ic(appctx_sc(appctx)), htx));
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001313 if (!max)
1314 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001315 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001316 ? (info & 0xff) + ((info >> 8) & 0xfffff)
1317 : info & 0xfffffff);
1318 if (blksz > max)
1319 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001320
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001321 blk = htx_add_blk(htx, type, blksz);
1322 if (!blk)
1323 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001324
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001325 blk->info = info;
1326 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001327 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001328 while (blksz) {
1329 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001330 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001331 offset += max;
1332 blksz -= max;
1333 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001334 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001335 if (blksz || offset == shctx->block_size) {
1336 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1337 offset = 0;
1338 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001339 }
Willy Tarreauf61494c2022-05-06 11:03:39 +02001340 ctx->offset = offset;
1341 ctx->next = shblk;
1342 ctx->sent += total;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001343 return total;
1344}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001345
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001346static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
1347 uint32_t info, struct shared_block *shblk, unsigned int offset)
1348{
Willy Tarreauf61494c2022-05-06 11:03:39 +02001349 struct cache_appctx *ctx = appctx->svcctx;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001350 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1351 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
1352 unsigned int max, total, rem_data;
1353 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001354
Willy Tarreau0698c802022-05-11 14:09:57 +02001355 max = htx_get_max_blksz(htx,
Willy Tarreauc12b3212022-05-27 11:08:15 +02001356 channel_htx_recv_max(sc_ic(appctx_sc(appctx)), htx));
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001357 if (!max)
1358 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001359
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001360 rem_data = 0;
Willy Tarreauf61494c2022-05-06 11:03:39 +02001361 if (ctx->rem_data) {
1362 blksz = ctx->rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001363 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001364 }
1365 else {
1366 blksz = (info & 0xfffffff);
1367 total = 4;
1368 }
1369 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001370 rem_data = blksz - max;
1371 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001372 }
1373
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001374 while (blksz) {
1375 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001376
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001377 max = MIN(blksz, shctx->block_size - offset);
1378 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1379 offset += sz;
1380 blksz -= sz;
1381 total += sz;
1382 if (sz < max)
1383 break;
1384 if (blksz || offset == shctx->block_size) {
1385 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1386 offset = 0;
1387 }
1388 }
1389
Willy Tarreauf61494c2022-05-06 11:03:39 +02001390 ctx->offset = offset;
1391 ctx->next = shblk;
1392 ctx->sent += total;
1393 ctx->rem_data = rem_data + blksz;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001394 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001395}
1396
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001397static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1398 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001399{
Willy Tarreauf61494c2022-05-06 11:03:39 +02001400 struct cache_appctx *ctx = appctx->svcctx;
Christopher Faulet95220e22018-12-07 17:34:39 +01001401 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1402 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001403 struct shared_block *shblk;
1404 unsigned int offset, sz;
1405 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001406
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001407 while (len) {
1408 enum htx_blk_type type;
1409 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001410
Willy Tarreauf61494c2022-05-06 11:03:39 +02001411 shblk = ctx->next;
1412 offset = ctx->offset;
1413 if (ctx->rem_data) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001414 type = HTX_BLK_DATA;
1415 info = 0;
1416 goto add_data_blk;
1417 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001418
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001419 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001420 sz = MIN(4, shctx->block_size - offset);
1421 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1422 offset += sz;
1423 if (sz < 4) {
1424 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1425 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1426 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001427 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001428
1429 /* Get payload of the next HTX block and insert it. */
1430 type = (info >> 28);
1431 if (type != HTX_BLK_DATA)
1432 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1433 else {
1434 add_data_blk:
1435 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001436 }
1437
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001438 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001439 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001440 total += ret;
1441 len -= ret;
1442
Willy Tarreauf61494c2022-05-06 11:03:39 +02001443 if (ctx->rem_data || type == mark)
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001444 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001445 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001446
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001447 return total;
1448}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001449
1450static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1451{
Willy Tarreauf61494c2022-05-06 11:03:39 +02001452 struct cache_appctx *ctx = appctx->svcctx;
1453 struct cache_entry *cache_ptr = ctx->entry;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001454 unsigned int age;
1455 char *end;
1456
1457 chunk_reset(&trash);
Willy Tarreau9b5d57d2023-02-07 15:22:41 +01001458 age = MAX(0, (int)(date.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001459 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1460 age = CACHE_ENTRY_MAX_AGE;
1461 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1462 b_set_data(&trash, end - b_head(&trash));
1463 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1464 return 0;
1465 return 1;
1466}
1467
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001468static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001469{
Willy Tarreauf61494c2022-05-06 11:03:39 +02001470 struct cache_appctx *ctx = appctx->svcctx;
1471 struct cache_entry *cache_ptr = ctx->entry;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001472 struct shared_block *first = block_ptr(cache_ptr);
Willy Tarreauc12b3212022-05-27 11:08:15 +02001473 struct stconn *sc = appctx_sc(appctx);
Willy Tarreauc5ddd9f2022-05-27 10:33:20 +02001474 struct channel *req = sc_oc(sc);
1475 struct channel *res = sc_ic(sc);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001476 struct htx *req_htx, *res_htx;
1477 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001478 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001479 size_t ret, total = 0;
1480
Christopher Faulet8b1eed12022-03-07 16:44:30 +01001481 res_htx = htx_from_buf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001482 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001483
Christopher Fauletf8130b22023-03-31 10:11:39 +02001484 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW))))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001485 goto out;
1486
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001487 if (appctx->st0 == HTX_CACHE_INIT) {
Christopher Fauletd2b474f2024-09-16 19:17:33 +02001488 if (!co_data(req))
1489 goto wait_request;
Willy Tarreauf61494c2022-05-06 11:03:39 +02001490 ctx->next = block_ptr(cache_ptr);
1491 ctx->offset = sizeof(*cache_ptr);
1492 ctx->sent = 0;
1493 ctx->rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001494 appctx->st0 = HTX_CACHE_HEADER;
1495 }
1496
Christopher Fauletd2b474f2024-09-16 19:17:33 +02001497 /* Check if the input buffer is available. */
1498 if (!b_size(&res->buf)) {
1499 sc_need_room(sc, 0);
1500 goto out;
1501 }
1502
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001503 if (appctx->st0 == HTX_CACHE_HEADER) {
1504 /* Headers must be dump at once. Otherwise it is an error */
Willy Tarreauf61494c2022-05-06 11:03:39 +02001505 len = first->len - sizeof(*cache_ptr) - ctx->sent;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001506 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1507 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1508 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001509 goto error;
1510
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001511 /* In case of a conditional request, we might want to send a
1512 * "304 Not Modified" response instead of the stored data. */
Willy Tarreauf61494c2022-05-06 11:03:39 +02001513 if (ctx->send_notmodified) {
Tim Duesterhuse0142342020-10-22 21:15:06 +02001514 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1515 /* If replacing the status code fails we need to send the full response. */
Willy Tarreauf61494c2022-05-06 11:03:39 +02001516 ctx->send_notmodified = 0;
Tim Duesterhuse0142342020-10-22 21:15:06 +02001517 }
1518 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001519
1520 /* Skip response body for HEAD requests or in case of "304 Not
1521 * Modified" response. */
Willy Tarreauc5ddd9f2022-05-27 10:33:20 +02001522 if (__sc_strm(sc)->txn->meth == HTTP_METH_HEAD || ctx->send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001523 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001524 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001525 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001526 }
1527
1528 if (appctx->st0 == HTX_CACHE_DATA) {
Willy Tarreauf61494c2022-05-06 11:03:39 +02001529 len = first->len - sizeof(*cache_ptr) - ctx->sent;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001530 if (len) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001531 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_UNUSED);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001532 if (ret < len) {
Christopher Fauletd6f05572023-05-09 11:31:24 +02001533 sc_need_room(sc, channel_htx_recv_max(res, res_htx) + 1);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001534 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001535 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001536 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001537 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001538 }
1539
1540 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001541 /* no more data are expected. */
1542 res_htx->flags |= HTX_FL_EOM;
Willy Tarreaud869e132022-05-17 18:05:31 +02001543 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001544 appctx->st0 = HTX_CACHE_END;
1545 }
1546
1547 end:
Christopher Fauletf8130b22023-03-31 10:11:39 +02001548 if (appctx->st0 == HTX_CACHE_END)
1549 se_fl_set(appctx->sedesc, SE_FL_EOS);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001550
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001551 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001552 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001553 if (total)
1554 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001555 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001556
1557 /* eat the whole request */
1558 if (co_data(req)) {
1559 req_htx = htx_from_buf(&req->buf);
1560 co_htx_skip(req, req_htx, co_data(req));
1561 htx_to_buf(req_htx, &req->buf);
1562 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001563 return;
1564
Christopher Fauletd2b474f2024-09-16 19:17:33 +02001565 wait_request:
1566 /* Wait for the request before starting to deliver the response */
1567 b_reset(&res->buf);
1568 applet_need_more_data(appctx);
1569 return;
1570
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001571 error:
1572 /* Sent and HTTP error 500 */
1573 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001574 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001575 res->buf.data = b_data(errmsg);
1576 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1577 res_htx = htx_from_buf(&res->buf);
1578
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001579 total = 0;
Christopher Fauletf8130b22023-03-31 10:11:39 +02001580 se_fl_set(appctx->sedesc, SE_FL_ERROR);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001581 appctx->st0 = HTX_CACHE_END;
1582 goto end;
1583}
1584
1585
Christopher Faulet95220e22018-12-07 17:34:39 +01001586static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001587{
1588 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001589 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001590
Christopher Faulet95220e22018-12-07 17:34:39 +01001591 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001592 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001593 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001594 }
1595
1596 /* check if a cache filter was already registered with this cache
1597 * name, if that's the case, must use it. */
1598 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001599 if (fconf->id == cache_store_flt_id) {
1600 cconf = fconf->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001601 if (cconf && strcmp((char *)cconf->c.name, name) == 0) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001602 rule->arg.act.p[0] = cconf;
1603 return 1;
1604 }
William Lallemand41db4602017-10-30 11:15:51 +01001605 }
1606 }
1607
Christopher Faulet95220e22018-12-07 17:34:39 +01001608 /* Create the filter cache config */
1609 cconf = calloc(1, sizeof(*cconf));
1610 if (!cconf) {
1611 memprintf(err, "out of memory\n");
1612 goto err;
1613 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001614 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001615 cconf->c.name = strdup(name);
1616 if (!cconf->c.name) {
1617 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001618 goto err;
1619 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001620
William Lallemand41db4602017-10-30 11:15:51 +01001621 /* register a filter to fill the cache buffer */
1622 fconf = calloc(1, sizeof(*fconf));
1623 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001624 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001625 goto err;
1626 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001627 fconf->id = cache_store_flt_id;
1628 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001629 fconf->ops = &cache_ops;
Willy Tarreau2b718102021-04-21 07:32:39 +02001630 LIST_APPEND(&proxy->filter_configs, &fconf->list);
William Lallemand41db4602017-10-30 11:15:51 +01001631
Christopher Faulet95220e22018-12-07 17:34:39 +01001632 rule->arg.act.p[0] = cconf;
1633 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001634
Christopher Faulet95220e22018-12-07 17:34:39 +01001635 err:
1636 free(cconf);
1637 return 0;
1638}
1639
1640enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1641 struct act_rule *rule, char **err)
1642{
1643 rule->action = ACT_CUSTOM;
1644 rule->action_ptr = http_action_store_cache;
1645
1646 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1647 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001648
Christopher Faulet95220e22018-12-07 17:34:39 +01001649 (*orig_arg)++;
1650 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001651}
1652
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001653/* This produces a sha1 hash of the concatenation of the HTTP method,
1654 * the first occurrence of the Host header followed by the path component
1655 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001656int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001657{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001658 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001659 struct htx *htx = htxbuf(&s->req.buf);
1660 struct htx_sl *sl;
1661 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001662 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001663 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001664 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001665
William Lallemandf528fff2017-11-23 19:43:17 +01001666 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001667 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001668
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001669 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001670 uri = htx_sl_req_uri(sl); // whole uri
1671 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001672 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001673
1674 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1675 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1676 * URIs are almost always sent in absolute form with their scheme. In
1677 * this case, the scheme is almost always "https". In order to support
1678 * sharing of cache objects between H1 and H2, we'll hash the absolute
1679 * URI whenever known, or prepend "https://" + the Host header for
1680 * relative URIs. The difference will only appear on absolute HTTP/1
1681 * requests sent to an origin server, which practically is never met in
1682 * the real world so we don't care about the ability to share the same
1683 * key here.URIs are normalized from the absolute URI to an origin form as
1684 * well.
1685 */
1686 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001687 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001688 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1689 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001690 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001691 }
1692
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001693 chunk_istcat(trash, uri);
William Lallemandf528fff2017-11-23 19:43:17 +01001694
1695 /* hash everything */
1696 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001697 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001698 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1699
1700 return 1;
1701}
1702
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001703/* Looks for "If-None-Match" headers in the request and compares their value
1704 * with the one that might have been stored in the cache_entry. If any of them
1705 * matches, a "304 Not Modified" response should be sent instead of the cached
1706 * data.
1707 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001708 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1709 *
1710 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1711 * header and compare its value (date) to the one stored in the cache_entry.
1712 * If the request's date is later than the cached one, we also send a
1713 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1714 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001715 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1716 */
1717static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1718 struct cache_entry *entry)
1719{
1720 int retval = 0;
1721
1722 struct http_hdr_ctx ctx = { .blk = NULL };
1723 struct ist cache_entry_etag = IST_NULL;
1724 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001725 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001726
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001727 struct tm tm = {};
1728 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001729
1730 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001731 * cache_entry's ETag in order to perform comparisons.
1732 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001733 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001734 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001735
1736 /* A '*' matches everything. */
1737 if (isteq(ctx.value, ist("*")) != 0) {
1738 retval = 1;
1739 break;
1740 }
1741
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001742 /* No need to rebuild an etag if none was stored in the cache. */
1743 if (entry->etag_length == 0)
1744 break;
1745
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001746 /* Rebuild the stored ETag. */
1747 if (etag_buffer == NULL) {
1748 etag_buffer = get_trash_chunk();
1749
1750 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1751 (unsigned char*)b_orig(etag_buffer),
1752 entry->etag_offset, entry->etag_length) == 0) {
1753 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1754 } else {
1755 /* We could not rebuild the ETag in one go, we
1756 * won't send a "304 Not Modified" response. */
1757 break;
1758 }
1759 }
1760
1761 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1762 retval = 1;
1763 break;
1764 }
1765 }
1766
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001767 /* If the request did not contain an "If-None-Match" header, we look for
1768 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1769 if (retval == 0 && if_none_match_found == 0) {
1770 ctx.blk = NULL;
1771 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1772 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1773 if_modified_since = my_timegm(&tm);
1774
1775 /* We send a "304 Not Modified" response if the
1776 * entry's last modified date is earlier than
1777 * the one found in the "If-Modified-Since"
1778 * header. */
1779 retval = (entry->last_modified <= if_modified_since);
1780 }
1781 }
1782 }
1783
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001784 return retval;
1785}
1786
William Lallemand41db4602017-10-30 11:15:51 +01001787enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1788 struct session *sess, struct stream *s, int flags)
1789{
William Lallemand77c11972017-10-31 20:43:01 +01001790
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001791 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001792 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001793 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1794 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001795 struct shared_block *entry_block;
1796
William Lallemand77c11972017-10-31 20:43:01 +01001797
Willy Tarreau6905d182019-10-01 17:59:17 +02001798 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1799 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001800 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001801 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001802 txn->flags |= TX_CACHE_IGNORE;
1803
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001804 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001805
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001806 /* The request's hash has to be calculated for all requests, even POSTs
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001807 * or PUTs for instance because RFC7234 specifies that a successful
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001808 * "unsafe" method on a stored resource must invalidate it
1809 * (see RFC7234#4.4). */
1810 if (!sha1_hosturi(s))
Willy Tarreau504455c2017-12-22 17:47:35 +01001811 return ACT_RET_CONT;
1812
Willy Tarreau504455c2017-12-22 17:47:35 +01001813 if (s->txn->flags & TX_CACHE_IGNORE)
1814 return ACT_RET_CONT;
1815
Willy Tarreaua1214a52018-12-14 14:00:25 +01001816 if (px == strm_fe(s))
Willy Tarreau4781b152021-04-06 13:53:36 +02001817 _HA_ATOMIC_INC(&px->fe_counters.p.http.cache_lookups);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001818 else
Willy Tarreau4781b152021-04-06 13:53:36 +02001819 _HA_ATOMIC_INC(&px->be_counters.p.http.cache_lookups);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001820
William Lallemanda400a3a2017-11-20 19:13:12 +01001821 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001822 res = entry_exist(cache, s->txn->cache_hash);
Remi Tricot-Le Breton25917cd2023-02-21 17:42:04 +01001823 /* We must not use an entry that is not complete but the check will be
1824 * performed after we look for a potential secondary entry (in case of
1825 * Vary). */
1826 if (res) {
William Lallemand77c11972017-10-31 20:43:01 +01001827 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001828 entry_block = block_ptr(res);
1829 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001830 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001831
1832 /* In case of Vary, we could have multiple entries with the same
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001833 * primary hash. We need to calculate the secondary hash in order
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001834 * to find the actual entry we want (if it exists). */
1835 if (res->secondary_key_signature) {
1836 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1837 shctx_lock(shctx_ptr(cache));
1838 sec_entry = secondary_entry_exist(cache, res,
1839 s->txn->cache_secondary_hash);
1840 if (sec_entry && sec_entry != res) {
1841 /* The wrong row was added to the hot list. */
1842 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1843 entry_block = block_ptr(sec_entry);
1844 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1845 }
1846 res = sec_entry;
1847 shctx_unlock(shctx_ptr(cache));
1848 }
1849 else
1850 res = NULL;
1851 }
1852
Remi Tricot-Le Breton25917cd2023-02-21 17:42:04 +01001853 /* We either looked for a valid secondary entry and could not
1854 * find one, or the entry we want to use is not complete. We
1855 * can't use the cache's entry and must forward the request to
1856 * the server. */
1857 if (!res || !res->complete) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001858 shctx_lock(shctx_ptr(cache));
1859 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1860 shctx_unlock(shctx_ptr(cache));
1861 return ACT_RET_CONT;
1862 }
1863
William Lallemand77c11972017-10-31 20:43:01 +01001864 s->target = &http_cache_applet.obj_type;
Willy Tarreaua0b58b52022-05-27 08:33:53 +02001865 if ((appctx = sc_applet_create(s->scb, objt_applet(s->target)))) {
Willy Tarreauf61494c2022-05-06 11:03:39 +02001866 struct cache_appctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1867
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001868 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001869 appctx->rule = rule;
Willy Tarreauf61494c2022-05-06 11:03:39 +02001870 ctx->entry = res;
1871 ctx->next = NULL;
1872 ctx->sent = 0;
1873 ctx->send_notmodified =
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001874 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001875
1876 if (px == strm_fe(s))
Willy Tarreau4781b152021-04-06 13:53:36 +02001877 _HA_ATOMIC_INC(&px->fe_counters.p.http.cache_hits);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001878 else
Willy Tarreau4781b152021-04-06 13:53:36 +02001879 _HA_ATOMIC_INC(&px->be_counters.p.http.cache_hits);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001880 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001881 } else {
Christopher Faulet1d216c72022-04-21 11:30:43 +02001882 s->target = NULL;
William Lallemand55e76742017-11-21 20:01:28 +01001883 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001884 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001885 shctx_unlock(shctx_ptr(cache));
Christopher Faulet1d216c72022-04-21 11:30:43 +02001886 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001887 }
1888 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001889 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001890
1891 /* Shared context does not need to be locked while we calculate the
1892 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001893 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001894 /* Build a complete secondary hash until the server response
1895 * tells us which fields should be kept (if any). */
1896 http_request_prebuild_full_secondary_key(s);
1897 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001898 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001899}
1900
1901
1902enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1903 struct act_rule *rule, char **err)
1904{
William Lallemand41db4602017-10-30 11:15:51 +01001905 rule->action = ACT_CUSTOM;
1906 rule->action_ptr = http_action_req_cache_use;
1907
Christopher Faulet95220e22018-12-07 17:34:39 +01001908 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001909 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001910
1911 (*orig_arg)++;
1912 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001913}
1914
1915int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1916{
1917 int err_code = 0;
1918
1919 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1920
1921 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001922 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001923 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001924 err_code |= ERR_ALERT | ERR_ABORT;
1925 goto out;
1926 }
1927
1928 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1929 err_code |= ERR_ABORT;
1930 goto out;
1931 }
1932
1933 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001934 struct cache *cache_config;
1935
William Lallemand41db4602017-10-30 11:15:51 +01001936 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1937 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001938 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001939 err_code |= ERR_ALERT | ERR_ABORT;
1940 goto out;
1941 }
1942
1943 strlcpy2(tmp_cache_config->id, args[1], 33);
1944 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001945 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001946 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001947 err_code |= ERR_WARN;
1948 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001949
1950 list_for_each_entry(cache_config, &caches_config, list) {
1951 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1952 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1953 file, linenum, tmp_cache_config->id);
1954 err_code |= ERR_ALERT | ERR_ABORT;
1955 goto out;
1956 }
1957 }
1958
William Lallemand49b44532017-11-24 18:53:43 +01001959 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001960 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001961 tmp_cache_config->maxobjsz = 0;
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +01001962 tmp_cache_config->max_secondary_entries = DEFAULT_MAX_SECONDARY_ENTRY;
William Lallemand41db4602017-10-30 11:15:51 +01001963 }
1964 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001965 unsigned long int maxsize;
1966 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001967
1968 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1969 err_code |= ERR_ABORT;
1970 goto out;
1971 }
1972
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001973 maxsize = strtoul(args[1], &err, 10);
1974 if (err == args[1] || *err != '\0') {
1975 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1976 file, linenum, args[1]);
1977 err_code |= ERR_ABORT;
1978 goto out;
1979 }
1980
1981 if (maxsize > (UINT_MAX >> 20)) {
1982 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1983 file, linenum, args[1], UINT_MAX >> 20);
1984 err_code |= ERR_ABORT;
1985 goto out;
1986 }
1987
William Lallemand41db4602017-10-30 11:15:51 +01001988 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001989 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001990 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001991 } else if (strcmp(args[0], "max-age") == 0) {
1992 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1993 err_code |= ERR_ABORT;
1994 goto out;
1995 }
1996
1997 if (!*args[1]) {
1998 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1999 file, linenum, args[0]);
2000 err_code |= ERR_WARN;
2001 }
2002
2003 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002004 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02002005 unsigned int maxobjsz;
2006 char *err;
2007
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002008 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
2009 err_code |= ERR_ABORT;
2010 goto out;
2011 }
2012
2013 if (!*args[1]) {
2014 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
2015 file, linenum, args[0]);
2016 err_code |= ERR_WARN;
2017 }
2018
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02002019 maxobjsz = strtoul(args[1], &err, 10);
2020 if (err == args[1] || *err != '\0') {
2021 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
2022 file, linenum, args[1]);
2023 err_code |= ERR_ABORT;
2024 goto out;
2025 }
2026 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01002027 } else if (strcmp(args[0], "process-vary") == 0) {
2028 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
2029 err_code |= ERR_ABORT;
2030 goto out;
2031 }
2032
2033 if (!*args[1]) {
Remi Tricot-Le Bretone6cc5b52020-12-23 18:13:53 +01002034 ha_warning("parsing [%s:%d]: '%s' expects \"on\" or \"off\" (enable or disable vary processing).\n",
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01002035 file, linenum, args[0]);
2036 err_code |= ERR_WARN;
2037 }
Remi Tricot-Le Bretone6cc5b52020-12-23 18:13:53 +01002038 if (strcmp(args[1], "on") == 0)
2039 tmp_cache_config->vary_processing_enabled = 1;
2040 else if (strcmp(args[1], "off") == 0)
2041 tmp_cache_config->vary_processing_enabled = 0;
2042 else {
2043 ha_warning("parsing [%s:%d]: '%s' expects \"on\" or \"off\" (enable or disable vary processing).\n",
2044 file, linenum, args[0]);
2045 err_code |= ERR_WARN;
2046 }
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +01002047 } else if (strcmp(args[0], "max-secondary-entries") == 0) {
2048 unsigned int max_sec_entries;
2049 char *err;
2050
2051 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
2052 err_code |= ERR_ABORT;
2053 goto out;
2054 }
2055
2056 if (!*args[1]) {
2057 ha_warning("parsing [%s:%d]: '%s' expects a strictly positive number.\n",
2058 file, linenum, args[0]);
2059 err_code |= ERR_WARN;
2060 }
2061
2062 max_sec_entries = strtoul(args[1], &err, 10);
2063 if (err == args[1] || *err != '\0' || max_sec_entries == 0) {
2064 ha_warning("parsing [%s:%d]: max-secondary-entries wrong value '%s'\n",
2065 file, linenum, args[1]);
2066 err_code |= ERR_ABORT;
2067 goto out;
2068 }
2069 tmp_cache_config->max_secondary_entries = max_sec_entries;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002070 }
2071 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002072 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01002073 err_code |= ERR_ALERT | ERR_FATAL;
2074 goto out;
2075 }
2076out:
2077 return err_code;
2078}
2079
2080/* once the cache section is parsed */
2081
2082int cfg_post_parse_section_cache()
2083{
William Lallemand41db4602017-10-30 11:15:51 +01002084 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01002085
2086 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01002087
2088 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002089 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01002090 err_code |= ERR_FATAL | ERR_ALERT;
2091 goto out;
2092 }
2093
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02002094 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002095 /* Default max. file size is a 256th of the cache size. */
2096 tmp_cache_config->maxobjsz =
2097 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02002098 }
2099 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
2100 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
2101 err_code |= ERR_FATAL | ERR_ALERT;
2102 goto out;
2103 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002104
William Lallemandd1d1e222019-08-28 15:22:49 +02002105 /* add to the list of cache to init and reinit tmp_cache_config
2106 * for next cache section, if any.
2107 */
Willy Tarreau2b718102021-04-21 07:32:39 +02002108 LIST_APPEND(&caches_config, &tmp_cache_config->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02002109 tmp_cache_config = NULL;
2110 return err_code;
2111 }
2112out:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002113 ha_free(&tmp_cache_config);
William Lallemandd1d1e222019-08-28 15:22:49 +02002114 return err_code;
2115
2116}
2117
2118int post_check_cache()
2119{
2120 struct proxy *px;
2121 struct cache *back, *cache_config, *cache;
2122 struct shared_context *shctx;
2123 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002124 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02002125
2126 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
2127
2128 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
2129 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01002130
Frédéric Lécaillebc584492018-10-25 20:18:59 +02002131 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01002132 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01002133 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01002134 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01002135 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01002136
2137 err_code |= ERR_FATAL | ERR_ALERT;
2138 goto out;
2139 }
William Lallemanda400a3a2017-11-20 19:13:12 +01002140 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02002141 /* the cache structure is stored in the shctx and added to the
2142 * caches list, we can remove the entry from the caches_config
2143 * list */
2144 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01002145 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01002146 cache->entries = EB_ROOT;
Willy Tarreau2b718102021-04-21 07:32:39 +02002147 LIST_APPEND(&caches, &cache->list);
2148 LIST_DELETE(&cache_config->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02002149 free(cache_config);
2150
2151 /* Find all references for this cache in the existing filters
2152 * (over all proxies) and reference it in matching filters.
2153 */
2154 for (px = proxies_list; px; px = px->next) {
2155 struct flt_conf *fconf;
2156 struct cache_flt_conf *cconf;
2157
2158 list_for_each_entry(fconf, &px->filter_configs, list) {
2159 if (fconf->id != cache_store_flt_id)
2160 continue;
2161
2162 cconf = fconf->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002163 if (strcmp(cache->id, cconf->c.name) == 0) {
William Lallemandd1d1e222019-08-28 15:22:49 +02002164 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02002165 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02002166 cconf->c.cache = cache;
2167 break;
2168 }
2169 }
2170 }
William Lallemand41db4602017-10-30 11:15:51 +01002171 }
William Lallemandd1d1e222019-08-28 15:22:49 +02002172
William Lallemand41db4602017-10-30 11:15:51 +01002173out:
William Lallemand41db4602017-10-30 11:15:51 +01002174 return err_code;
2175
William Lallemand41db4602017-10-30 11:15:51 +01002176}
2177
William Lallemand41db4602017-10-30 11:15:51 +01002178struct flt_ops cache_ops = {
2179 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01002180 .check = cache_store_check,
2181 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01002182
Christopher Faulet65554e12020-03-06 14:52:06 +01002183 /* Handle stream init/deinit */
2184 .attach = cache_store_strm_init,
2185 .detach = cache_store_strm_deinit,
2186
William Lallemand4da3f8a2017-10-31 14:33:34 +01002187 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01002188 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01002189
2190 /* Filter HTTP requests and responses */
2191 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01002192 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01002193 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01002194};
2195
Christopher Faulet99a17a22018-12-11 09:18:27 +01002196
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002197#define CHECK_ENCODING(str, encoding_name, encoding_value) \
2198 ({ \
2199 int retval = 0; \
2200 if (istmatch(str, (struct ist){ .ptr = encoding_name+1, .len = sizeof(encoding_name) - 2 })) { \
2201 retval = encoding_value; \
2202 encoding = istadv(encoding, sizeof(encoding_name) - 2); \
2203 } \
2204 (retval); \
2205 })
2206
2207/*
2208 * Parse the encoding <encoding> and try to match the encoding part upon an
2209 * encoding list of explicitly supported encodings (which all have a specific
2210 * bit in an encoding bitmap). If a weight is included in the value, find out if
2211 * it is null or not. The bit value will be set in the <encoding_value>
2212 * parameter and the <has_null_weight> will be set to 1 if the weight is strictly
2213 * 0, 1 otherwise.
2214 * The encodings list is extracted from
2215 * https://www.iana.org/assignments/http-parameters/http-parameters.xhtml.
2216 * Returns 0 in case of success and -1 in case of error.
2217 */
2218static int parse_encoding_value(struct ist encoding, unsigned int *encoding_value,
2219 unsigned int *has_null_weight)
2220{
2221 int retval = 0;
2222
2223 if (!encoding_value)
2224 return -1;
2225
2226 if (!istlen(encoding))
2227 return -1; /* Invalid encoding */
2228
2229 *encoding_value = 0;
2230 if (has_null_weight)
2231 *has_null_weight = 0;
2232
2233 switch (*encoding.ptr) {
2234 case 'a':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002235 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002236 *encoding_value = CHECK_ENCODING(encoding, "aes128gcm", VARY_ENCODING_AES128GCM);
2237 break;
2238 case 'b':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002239 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002240 *encoding_value = CHECK_ENCODING(encoding, "br", VARY_ENCODING_BR);
2241 break;
2242 case 'c':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002243 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002244 *encoding_value = CHECK_ENCODING(encoding, "compress", VARY_ENCODING_COMPRESS);
2245 break;
2246 case 'd':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002247 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002248 *encoding_value = CHECK_ENCODING(encoding, "deflate", VARY_ENCODING_DEFLATE);
2249 break;
2250 case 'e':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002251 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002252 *encoding_value = CHECK_ENCODING(encoding, "exi", VARY_ENCODING_EXI);
2253 break;
2254 case 'g':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002255 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002256 *encoding_value = CHECK_ENCODING(encoding, "gzip", VARY_ENCODING_GZIP);
2257 break;
2258 case 'i':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002259 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002260 *encoding_value = CHECK_ENCODING(encoding, "identity", VARY_ENCODING_IDENTITY);
2261 break;
2262 case 'p':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002263 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002264 *encoding_value = CHECK_ENCODING(encoding, "pack200-gzip", VARY_ENCODING_PACK200_GZIP);
2265 break;
2266 case 'x':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002267 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002268 *encoding_value = CHECK_ENCODING(encoding, "x-gzip", VARY_ENCODING_GZIP);
2269 if (!*encoding_value)
2270 *encoding_value = CHECK_ENCODING(encoding, "x-compress", VARY_ENCODING_COMPRESS);
2271 break;
2272 case 'z':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002273 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002274 *encoding_value = CHECK_ENCODING(encoding, "zstd", VARY_ENCODING_ZSTD);
2275 break;
2276 case '*':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002277 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002278 *encoding_value = VARY_ENCODING_STAR;
2279 break;
2280 default:
2281 retval = -1; /* Unmanaged encoding */
2282 break;
2283 }
2284
2285 /* Process the optional weight part of the encoding. */
2286 if (*encoding_value) {
2287 encoding = http_trim_leading_spht(encoding);
2288 if (istlen(encoding)) {
2289 if (*encoding.ptr != ';')
2290 return -1;
2291
2292 if (has_null_weight) {
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002293 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002294
2295 encoding = http_trim_leading_spht(encoding);
2296
2297 *has_null_weight = isteq(encoding, ist("q=0"));
2298 }
2299 }
2300 }
2301
2302 return retval;
2303}
2304
Tim Duesterhus23b29452020-11-24 22:22:56 +01002305#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002306/*
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002307 * Build a bitmap of the accept-encoding header.
2308 *
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002309 * The bitmap is built by matching every sub-part of the accept-encoding value
2310 * with a subset of explicitly supported encodings, which all have their own bit
2311 * in the bitmap. This bitmap will be used to determine if a response can be
2312 * served to a client (that is if it has an encoding that is accepted by the
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002313 * client). Any unknown encodings will be indicated by the VARY_ENCODING_OTHER
2314 * bit.
2315 *
2316 * Returns 0 in case of success and -1 in case of error.
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002317 */
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002318static int accept_encoding_normalizer(struct htx *htx, struct ist hdr_name,
2319 char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002320{
Tim Duesterhus23b29452020-11-24 22:22:56 +01002321 size_t count = 0;
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002322 uint32_t encoding_bitmap = 0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002323 unsigned int encoding_bmp_bl = -1;
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002324 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002325 unsigned int encoding_value;
2326 unsigned int rejected_encoding;
2327
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05002328 /* A user agent always accepts an unencoded value unless it explicitly
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002329 * refuses it through an "identity;q=0" accept-encoding value. */
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002330 encoding_bitmap |= VARY_ENCODING_IDENTITY;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002331
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002332 /* Iterate over all the ACCEPT_ENCODING_MAX_ENTRIES first accept-encoding
2333 * values that might span acrosse multiple accept-encoding headers. */
2334 while (http_find_header(htx, hdr_name, &ctx, 0) && count < ACCEPT_ENCODING_MAX_ENTRIES) {
Tim Duesterhus3bc6af42021-06-18 15:09:28 +02002335 count++;
2336
2337 /* As per RFC7231#5.3.4, "An Accept-Encoding header field with a
2338 * combined field-value that is empty implies that the user agent
2339 * does not want any content-coding in response."
2340 *
2341 * We must (and did) count the existence of this empty header to not
2342 * hit the `count == 0` case below, but must ignore the value to not
2343 * include VARY_ENCODING_OTHER into the final bitmap.
2344 */
2345 if (istlen(ctx.value) == 0)
2346 continue;
2347
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002348 /* Turn accept-encoding value to lower case */
2349 ist2bin_lc(istptr(ctx.value), ctx.value);
Tim Duesterhus23b29452020-11-24 22:22:56 +01002350
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002351 /* Try to identify a known encoding and to manage null weights. */
2352 if (!parse_encoding_value(ctx.value, &encoding_value, &rejected_encoding)) {
2353 if (rejected_encoding)
2354 encoding_bmp_bl &= ~encoding_value;
2355 else
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002356 encoding_bitmap |= encoding_value;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002357 }
2358 else {
2359 /* Unknown encoding */
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002360 encoding_bitmap |= VARY_ENCODING_OTHER;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002361 }
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002362 }
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002363
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002364 /* If a "*" was found in the accepted encodings (without a null weight),
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05002365 * all the encoding are accepted except the ones explicitly rejected. */
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002366 if (encoding_bitmap & VARY_ENCODING_STAR) {
2367 encoding_bitmap = ~0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002368 }
2369
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05002370 /* Clear explicitly rejected encodings from the bitmap */
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002371 encoding_bitmap &= encoding_bmp_bl;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002372
2373 /* As per RFC7231#5.3.4, "If no Accept-Encoding field is in the request,
2374 * any content-coding is considered acceptable by the user agent". */
2375 if (count == 0)
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002376 encoding_bitmap = ~0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002377
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002378 /* A request with more than ACCEPT_ENCODING_MAX_ENTRIES accepted
2379 * encodings might be illegitimate so we will not use it. */
2380 if (count == ACCEPT_ENCODING_MAX_ENTRIES)
2381 return -1;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002382
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002383 write_u32(buf, encoding_bitmap);
2384 *buf_len = sizeof(encoding_bitmap);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002385
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002386 /* This function fills the hash buffer correctly even if no header was
2387 * found, hence the 0 return value (success). */
Tim Duesterhus23b29452020-11-24 22:22:56 +01002388 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002389}
Tim Duesterhus23b29452020-11-24 22:22:56 +01002390#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002391
2392/*
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002393 * Normalizer used by default for the Referer header. It only
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002394 * calculates a simple crc of the whole value.
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002395 * Only the first occurrence of the header will be taken into account in the
2396 * hash.
2397 * Returns 0 in case of success, 1 if the hash buffer should be filled with 0s
2398 * and -1 in case of error.
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002399 */
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002400static int default_normalizer(struct htx *htx, struct ist hdr_name,
2401 char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002402{
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002403 int retval = 1;
2404 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002405
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002406 if (http_find_header(htx, hdr_name, &ctx, 1)) {
2407 retval = 0;
2408 write_u32(buf, hash_crc32(istptr(ctx.value), istlen(ctx.value)));
2409 *buf_len = sizeof(int);
2410 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002411
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002412 return retval;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002413}
2414
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002415/*
Tim Duesterhused84d842021-01-18 13:41:17 +01002416 * Accept-Encoding bitmap comparison function.
2417 * Returns 0 if the bitmaps are compatible.
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002418 */
Tim Duesterhused84d842021-01-18 13:41:17 +01002419static int accept_encoding_bitmap_cmp(const void *ref, const void *new, unsigned int len)
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002420{
Tim Duesterhused84d842021-01-18 13:41:17 +01002421 uint32_t ref_bitmap = read_u32(ref);
2422 uint32_t new_bitmap = read_u32(new);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002423
Tim Duesterhused84d842021-01-18 13:41:17 +01002424 if (!(ref_bitmap & VARY_ENCODING_OTHER)) {
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002425 /* All the bits set in the reference bitmap correspond to the
2426 * stored response' encoding and should all be set in the new
2427 * encoding bitmap in order for the client to be able to manage
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002428 * the response.
2429 *
2430 * If this is the case the cached response has encodings that
2431 * are accepted by the client. It can be served directly by
2432 * the cache (as far as the accept-encoding part is concerned).
2433 */
2434
Tim Duesterhused84d842021-01-18 13:41:17 +01002435 return (ref_bitmap & new_bitmap) != ref_bitmap;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002436 }
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002437 else {
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002438 return 1;
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002439 }
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002440}
2441
2442
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002443/*
2444 * Pre-calculate the hashes of all the supported headers (in our Vary
2445 * implementation) of a given request. We have to calculate all the hashes
2446 * in advance because the actual Vary signature won't be known until the first
2447 * response.
2448 * Only the first occurrence of every header will be taken into account in the
2449 * hash.
2450 * If the header is not present, the hash portion of the given header will be
2451 * filled with zeros.
2452 * Returns 0 in case of success.
2453 */
2454static int http_request_prebuild_full_secondary_key(struct stream *s)
2455{
Remi Tricot-Le Bretonbba29122020-12-23 18:13:44 +01002456 /* The fake signature (second parameter) will ensure that every part of the
2457 * secondary key is calculated. */
2458 return http_request_build_secondary_key(s, ~0);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002459}
2460
2461
2462/*
2463 * Calculate the secondary key for a request for which we already have a known
2464 * vary signature. The key is made by aggregating hashes calculated for every
2465 * header mentioned in the vary signature.
2466 * Only the first occurrence of every header will be taken into account in the
2467 * hash.
2468 * If the header is not present, the hash portion of the given header will be
2469 * filled with zeros.
2470 * Returns 0 in case of success.
2471 */
2472static int http_request_build_secondary_key(struct stream *s, int vary_signature)
2473{
2474 struct http_txn *txn = s->txn;
2475 struct htx *htx = htxbuf(&s->req.buf);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002476
2477 unsigned int idx;
2478 const struct vary_hashing_information *info = NULL;
2479 unsigned int hash_length = 0;
2480 int retval = 0;
2481 int offset = 0;
2482
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002483 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && retval >= 0; ++idx) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002484 info = &vary_information[idx];
2485
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002486 /* The normalizing functions will be in charge of getting the
2487 * header values from the htx. This way they can manage multiple
2488 * occurrences of their processed header. */
2489 if ((vary_signature & info->value) && info->norm_fn != NULL &&
2490 !(retval = info->norm_fn(htx, info->hdr_name, &txn->cache_secondary_hash[offset], &hash_length))) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002491 offset += hash_length;
2492 }
2493 else {
2494 /* Fill hash with 0s. */
2495 hash_length = info->hash_length;
2496 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
2497 offset += hash_length;
2498 }
2499 }
2500
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01002501 if (retval >= 0)
2502 txn->flags |= TX_CACHE_HAS_SEC_KEY;
2503
2504 return (retval < 0);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002505}
2506
2507/*
2508 * Build the actual secondary key of a given request out of the prebuilt key and
2509 * the actual vary signature (extracted from the response).
2510 * Returns 0 in case of success.
2511 */
2512static int http_request_reduce_secondary_key(unsigned int vary_signature,
2513 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
2514{
2515 int offset = 0;
2516 int global_offset = 0;
2517 int vary_info_count = 0;
2518 int keep = 0;
2519 unsigned int vary_idx;
2520 const struct vary_hashing_information *vary_info;
2521
2522 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
2523 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
2524 vary_info = &vary_information[vary_idx];
2525 keep = (vary_signature & vary_info->value) ? 0xff : 0;
2526
2527 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
2528 prebuilt_key[global_offset] &= keep;
2529 }
2530 }
2531
2532 return 0;
2533}
2534
2535
Christopher Faulet99a17a22018-12-11 09:18:27 +01002536
2537static int
2538parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
2539 struct flt_conf *fconf, char **err, void *private)
2540{
2541 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01002542 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002543 char *name = NULL;
2544 int pos = *cur_arg;
2545
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002546 /* Get the cache filter name. <pos> point on "cache" keyword */
2547 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02002548 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002549 goto error;
2550 }
2551 name = strdup(args[pos + 1]);
2552 if (!name) {
2553 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
2554 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002555 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002556 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002557
2558 /* Check if an implicit filter with the same name already exists. If so,
2559 * we remove the implicit filter to use the explicit one. */
2560 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
2561 if (f->id != cache_store_flt_id)
2562 continue;
2563
2564 cconf = f->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002565 if (strcmp(name, cconf->c.name) != 0) {
Christopher Faulet99a17a22018-12-11 09:18:27 +01002566 cconf = NULL;
2567 continue;
2568 }
2569
2570 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2571 cconf = NULL;
2572 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2573 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002574 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002575 }
2576
2577 /* Remove the implicit filter. <cconf> is kept for the explicit one */
Willy Tarreau2b718102021-04-21 07:32:39 +02002578 LIST_DELETE(&f->list);
Christopher Faulet99a17a22018-12-11 09:18:27 +01002579 free(f);
2580 free(name);
2581 break;
2582 }
2583
2584 /* No implicit cache filter found, create configuration for the explicit one */
2585 if (!cconf) {
2586 cconf = calloc(1, sizeof(*cconf));
2587 if (!cconf) {
2588 memprintf(err, "%s: out of memory", args[*cur_arg]);
2589 goto error;
2590 }
2591 cconf->c.name = name;
2592 }
2593
2594 cconf->flags = 0;
2595 fconf->id = cache_store_flt_id;
2596 fconf->conf = cconf;
2597 fconf->ops = &cache_ops;
2598
2599 *cur_arg = pos;
2600 return 0;
2601
2602 error:
2603 free(name);
2604 free(cconf);
2605 return -1;
2606}
2607
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002608/* It reserves a struct show_cache_ctx for the local variables */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002609static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002610{
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002611 struct show_cache_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
2612
William Lallemand1f49a362017-11-21 20:01:26 +01002613 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2614 return 1;
2615
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002616 ctx->cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
William Lallemand1f49a362017-11-21 20:01:26 +01002617 return 0;
2618}
2619
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002620/* It uses a struct show_cache_ctx for the local variables */
William Lallemand1f49a362017-11-21 20:01:26 +01002621static int cli_io_handler_show_cache(struct appctx *appctx)
2622{
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002623 struct show_cache_ctx *ctx = appctx->svcctx;
2624 struct cache* cache = ctx->cache;
William Lallemand1f49a362017-11-21 20:01:26 +01002625
William Lallemand1f49a362017-11-21 20:01:26 +01002626 list_for_each_entry_from(cache, &caches, list) {
2627 struct eb32_node *node = NULL;
2628 unsigned int next_key;
2629 struct cache_entry *entry;
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002630 unsigned int i;
William Lallemand1f49a362017-11-21 20:01:26 +01002631
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002632 next_key = ctx->next_key;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002633 if (!next_key) {
2634 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002635 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreauafe1de52018-04-04 11:56:43 +02002636 return 0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002637 }
William Lallemand1f49a362017-11-21 20:01:26 +01002638
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002639 ctx->cache = cache;
William Lallemand1f49a362017-11-21 20:01:26 +01002640
2641 while (1) {
2642
2643 shctx_lock(shctx_ptr(cache));
Christopher Faulet27f88a92021-11-23 16:03:05 +01002644 node = eb32_lookup_ge(&cache->entries, next_key);
William Lallemand1f49a362017-11-21 20:01:26 +01002645 if (!node) {
2646 shctx_unlock(shctx_ptr(cache));
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002647 ctx->next_key = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002648 break;
2649 }
2650
2651 entry = container_of(node, struct cache_entry, eb);
William Lallemand1f49a362017-11-21 20:01:26 +01002652 next_key = node->key + 1;
Willy Tarreauf1de1b52022-04-13 11:21:39 +02002653
Willy Tarreau9b5d57d2023-02-07 15:22:41 +01002654 if (entry->expire > date.tv_sec) {
Willy Tarreauf1de1b52022-04-13 11:21:39 +02002655 chunk_printf(&trash, "%p hash:%u vary:0x", entry, read_u32(entry->hash));
2656 for (i = 0; i < HTTP_CACHE_SEC_KEY_LEN; ++i)
2657 chunk_appendf(&trash, "%02x", (unsigned char)entry->secondary_key[i]);
2658 chunk_appendf(&trash, " size:%u (%u blocks), refcount:%u, expire:%d\n",
2659 block_ptr(entry)->len, block_ptr(entry)->block_count,
Willy Tarreau9b5d57d2023-02-07 15:22:41 +01002660 block_ptr(entry)->refcount, entry->expire - (int)date.tv_sec);
Willy Tarreauf1de1b52022-04-13 11:21:39 +02002661 } else {
2662 /* time to remove that one */
2663 delete_entry(entry);
2664 entry->eb.key = 0;
2665 }
2666
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002667 ctx->next_key = next_key;
William Lallemand1f49a362017-11-21 20:01:26 +01002668
2669 shctx_unlock(shctx_ptr(cache));
2670
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002671 if (applet_putchk(appctx, &trash) == -1)
William Lallemand1f49a362017-11-21 20:01:26 +01002672 return 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002673 }
2674
2675 }
2676
2677 return 1;
2678
2679}
2680
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002681
2682/*
2683 * boolean, returns true if response was built out of a cache entry.
2684 */
2685static int
2686smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2687 const char *kw, void *private)
2688{
2689 smp->data.type = SMP_T_BOOL;
2690 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2691
2692 return 1;
2693}
2694
2695/*
2696 * string, returns cache name (if response came from a cache).
2697 */
2698static int
2699smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2700 const char *kw, void *private)
2701{
2702 struct appctx *appctx = NULL;
2703
2704 struct cache_flt_conf *cconf = NULL;
2705 struct cache *cache = NULL;
2706
2707 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2708 return 0;
2709
Willy Tarreau4596fe22022-05-17 19:07:51 +02002710 /* Get appctx from the stream connector. */
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02002711 appctx = sc_appctx(smp->strm->scb);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002712 if (appctx && appctx->rule) {
2713 cconf = appctx->rule->arg.act.p[0];
2714 if (cconf) {
2715 cache = cconf->c.cache;
2716
2717 smp->data.type = SMP_T_STR;
2718 smp->flags = SMP_F_CONST;
2719 smp->data.u.str.area = cache->id;
2720 smp->data.u.str.data = strlen(cache->id);
2721 return 1;
2722 }
2723 }
2724
2725 return 0;
2726}
2727
Christopher Faulet99a17a22018-12-11 09:18:27 +01002728/* Declare the filter parser for "cache" keyword */
2729static struct flt_kw_list filter_kws = { "CACHE", { }, {
2730 { "cache", parse_cache_flt, NULL },
2731 { NULL, NULL, NULL },
2732 }
2733};
2734
2735INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2736
William Lallemand1f49a362017-11-21 20:01:26 +01002737static struct cli_kw_list cli_kws = {{},{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02002738 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
William Lallemande899af82017-11-22 16:41:26 +01002739 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002740}};
2741
Willy Tarreau0108d902018-11-25 19:14:37 +01002742INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002743
William Lallemand41db4602017-10-30 11:15:51 +01002744static struct action_kw_list http_res_actions = {
2745 .kw = {
2746 { "cache-store", parse_cache_store },
2747 { NULL, NULL }
2748 }
2749};
2750
Willy Tarreau0108d902018-11-25 19:14:37 +01002751INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2752
William Lallemand41db4602017-10-30 11:15:51 +01002753static struct action_kw_list http_req_actions = {
2754 .kw = {
2755 { "cache-use", parse_cache_use },
2756 { NULL, NULL }
2757 }
2758};
2759
Willy Tarreau0108d902018-11-25 19:14:37 +01002760INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2761
Willy Tarreau2231b632019-03-29 18:26:52 +01002762struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002763 .obj_type = OBJ_TYPE_APPLET,
2764 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002765 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002766 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002767};
2768
Willy Tarreaue6552512018-11-26 11:33:13 +01002769/* config parsers for this section */
2770REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002771REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002772
2773
2774/* Note: must not be declared <const> as its list will be overwritten */
2775static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2776 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2777 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2778 { /* END */ },
2779 }
2780};
2781
2782INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);