blob: 057d8b67c53fd0de8936f96f4b0db543ca5b570f [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>
Christopher Faulet908628c2022-03-25 16:43:49 +010022#include <haproxy/conn_stream.h>
23#include <haproxy/cs_utils.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020024#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020025#include <haproxy/filters.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/hash.h>
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +020027#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020028#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020029#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020030#include <haproxy/http_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/htx.h>
32#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +010034#include <haproxy/sample.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020035#include <haproxy/shctx.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
Christopher Faulet95220e22018-12-07 17:34:39 +010060/* cache config for filters */
61struct cache_flt_conf {
62 union {
63 struct cache *cache; /* cache used by the filter */
64 char *name; /* cache name used during conf parsing */
65 } c;
66 unsigned int flags; /* CACHE_FLT_F_* */
67};
68
Willy Tarreauc6dfef72022-05-05 16:46:13 +020069/* CLI context used during "show cache" */
70struct show_cache_ctx {
71 struct cache *cache;
72 uint next_key;
73};
74
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +010075
76/*
77 * Vary-related structures and functions
78 */
79enum vary_header_bit {
80 VARY_ACCEPT_ENCODING = (1 << 0),
81 VARY_REFERER = (1 << 1),
82 VARY_LAST /* should always be last */
83};
84
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +010085/*
86 * Encoding list extracted from
87 * https://www.iana.org/assignments/http-parameters/http-parameters.xhtml
88 * and RFC7231#5.3.4.
89 */
90enum vary_encoding {
91 VARY_ENCODING_GZIP = (1 << 0),
92 VARY_ENCODING_DEFLATE = (1 << 1),
93 VARY_ENCODING_BR = (1 << 2),
94 VARY_ENCODING_COMPRESS = (1 << 3),
95 VARY_ENCODING_AES128GCM = (1 << 4),
96 VARY_ENCODING_EXI = (1 << 5),
97 VARY_ENCODING_PACK200_GZIP = (1 << 6),
98 VARY_ENCODING_ZSTD = (1 << 7),
99 VARY_ENCODING_IDENTITY = (1 << 8),
100 VARY_ENCODING_STAR = (1 << 9),
101 VARY_ENCODING_OTHER = (1 << 10)
102};
103
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100104struct vary_hashing_information {
105 struct ist hdr_name; /* Header name */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +0500106 enum vary_header_bit value; /* Bit representing the header in a vary signature */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100107 unsigned int hash_length; /* Size of the sub hash for this header's value */
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100108 int(*norm_fn)(struct htx*,struct ist hdr_name,char* buf,unsigned int* buf_len); /* Normalization function */
Tim Duesterhused84d842021-01-18 13:41:17 +0100109 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 +0100110};
111
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100112static int http_request_prebuild_full_secondary_key(struct stream *s);
113static int http_request_build_secondary_key(struct stream *s, int vary_signature);
114static int http_request_reduce_secondary_key(unsigned int vary_signature,
115 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]);
116
117static int parse_encoding_value(struct ist value, unsigned int *encoding_value,
118 unsigned int *has_null_weight);
119
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +0100120static int accept_encoding_normalizer(struct htx *htx, struct ist hdr_name,
121 char *buf, unsigned int *buf_len);
122static int default_normalizer(struct htx *htx, struct ist hdr_name,
123 char *buf, unsigned int *buf_len);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100124
Tim Duesterhused84d842021-01-18 13:41:17 +0100125static int accept_encoding_bitmap_cmp(const void *ref, const void *new, unsigned int len);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100126
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100127/* Warning : do not forget to update HTTP_CACHE_SEC_KEY_LEN when new items are
128 * added to this array. */
129const struct vary_hashing_information vary_information[] = {
Tim Duesterhused84d842021-01-18 13:41:17 +0100130 { 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 +0100131 { IST("referer"), VARY_REFERER, sizeof(int), &default_normalizer, NULL },
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100132};
133
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100134
William Lallemand41db4602017-10-30 11:15:51 +0100135/*
136 * cache ctx for filters
137 */
138struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +0100139 struct shared_block *first_block;
140};
141
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100142#define DEFAULT_MAX_SECONDARY_ENTRY 10
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100143
William Lallemand41db4602017-10-30 11:15:51 +0100144struct cache_entry {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100145 unsigned int complete; /* An entry won't be valid until complete is not null. */
William Lallemand41db4602017-10-30 11:15:51 +0100146 unsigned int latest_validation; /* latest validation date */
147 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200148 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100149
William Lallemand41db4602017-10-30 11:15:51 +0100150 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +0100151 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200152
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100153 char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */
154 unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used
155 * to build secondary keys for this cache entry. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100156 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 +0100157 unsigned int last_clear_ts; /* Timestamp of the last call to clear_expired_duplicates. */
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100158
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200159 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
160 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
161
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200162 time_t last_modified; /* Origin server "Last-Modified" header value converted in
163 * seconds since epoch. If no "Last-Modified"
164 * header is found, use "Date" header value,
165 * otherwise use reception time. This field will
166 * be used in case of an "If-Modified-Since"-based
167 * conditional request. */
168
William Lallemand41db4602017-10-30 11:15:51 +0100169 unsigned char data[0];
170};
171
172#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +0100173#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100174
175static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +0200176static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +0100177static struct cache *tmp_cache_config = NULL;
178
Willy Tarreau8ceae722018-11-26 11:58:30 +0100179DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
180
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100181static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry);
182static void delete_entry(struct cache_entry *del_entry);
183
William Lallemandf528fff2017-11-23 19:43:17 +0100184struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100185{
186 struct eb32_node *node;
187 struct cache_entry *entry;
188
Willy Tarreau8b507582020-02-25 09:35:07 +0100189 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100190 if (!node)
191 return NULL;
192
193 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100194
195 /* if that's not the right node */
196 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
197 return NULL;
198
William Lallemand08727662017-11-21 20:01:27 +0100199 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100200 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100201 } else {
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100202 delete_entry(entry);
William Lallemand08727662017-11-21 20:01:27 +0100203 entry->eb.key = 0;
204 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100205 return NULL;
206
207}
208
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100209
210/*
211 * Compare a newly built secondary key to the one found in a cache_entry.
212 * Every sub-part of the key is compared to the reference through the dedicated
213 * comparison function of the sub-part (that might do more than a simple
214 * memcmp).
215 * Returns 0 if the keys are alike.
216 */
217static int secondary_key_cmp(const char *ref_key, const char *new_key)
218{
219 int retval = 0;
Tim Duesterhus5897cfe2021-01-18 13:41:18 +0100220 size_t idx = 0;
221 unsigned int offset = 0;
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100222 const struct vary_hashing_information *info;
223
224 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
225 info = &vary_information[idx];
226
227 if (info->cmp_fn)
228 retval = info->cmp_fn(&ref_key[offset], &new_key[offset], info->hash_length);
229 else
230 retval = memcmp(&ref_key[offset], &new_key[offset], info->hash_length);
231
232 offset += info->hash_length;
233 }
234
235 return retval;
236}
237
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100238/*
239 * There can be multiple entries with the same primary key in the ebtree so in
240 * order to get the proper one out of the list, we use a secondary_key.
241 * This function simply iterates over all the entries with the same primary_key
242 * until it finds the right one.
243 * Returns the cache_entry in case of success, NULL otherwise.
244 */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100245struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry,
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100246 const char *secondary_key)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100247{
248 struct eb32_node *node = &entry->eb;
249
250 if (!entry->secondary_key_signature)
251 return NULL;
252
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100253 while (entry && secondary_key_cmp(entry->secondary_key, secondary_key) != 0) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100254 node = eb32_next_dup(node);
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100255
256 /* Make the best use of this iteration and clear expired entries
257 * when we find them. Calling delete_entry would be too costly
258 * so we simply call eb32_delete. The secondary_entry count will
259 * be updated when we try to insert a new entry to this list. */
260 if (entry->expire <= now.tv_sec) {
261 eb32_delete(&entry->eb);
262 entry->eb.key = 0;
263 }
264
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100265 entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL;
266 }
267
268 /* Expired entry */
269 if (entry && entry->expire <= now.tv_sec) {
270 eb32_delete(&entry->eb);
271 entry->eb.key = 0;
272 entry = NULL;
273 }
274
275 return entry;
276}
277
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100278
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100279/*
280 * Remove all expired entries from a list of duplicates.
281 * Return the number of alive entries in the list and sets dup_tail to the
282 * current last item of the list.
283 */
284static unsigned int clear_expired_duplicates(struct eb32_node **dup_tail)
285{
286 unsigned int entry_count = 0;
287 struct cache_entry *entry = NULL;
288 struct eb32_node *prev = *dup_tail;
289 struct eb32_node *tail = NULL;
290
291 while (prev) {
292 entry = container_of(prev, struct cache_entry, eb);
293 prev = eb32_prev_dup(prev);
294 if (entry->expire <= now.tv_sec) {
295 eb32_delete(&entry->eb);
296 entry->eb.key = 0;
297 }
298 else {
299 if (!tail)
300 tail = &entry->eb;
301 ++entry_count;
302 }
303 }
304
305 *dup_tail = tail;
306
307 return entry_count;
308}
309
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100310
311/*
312 * This function inserts a cache_entry in the cache's ebtree. In case of
313 * duplicate entries (vary), it then checks that the number of entries did not
314 * reach the max number of secondary entries. If this entry should not have been
315 * created, remove it.
316 * In the regular case (unique entries), this function does not do more than a
317 * simple insert. In case of secondary entries, it will at most cost an
318 * insertion+max_sec_entries time checks and entry deletion.
319 * Returns the newly inserted node in case of success, NULL otherwise.
320 */
321static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry)
322{
323 struct eb32_node *prev = NULL;
324 struct cache_entry *entry = NULL;
325 unsigned int entry_count = 0;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100326 unsigned int last_clear_ts = now.tv_sec;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100327
328 struct eb32_node *node = eb32_insert(&cache->entries, &new_entry->eb);
329
330 /* We should not have multiple entries with the same primary key unless
331 * the entry has a non null vary signature. */
332 if (!new_entry->secondary_key_signature)
333 return node;
334
335 prev = eb32_prev_dup(node);
336 if (prev != NULL) {
337 /* The last entry of a duplicate list should contain the current
338 * number of entries in the list. */
339 entry = container_of(prev, struct cache_entry, eb);
340 entry_count = entry->secondary_entries_count;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100341 last_clear_ts = entry->last_clear_ts;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100342
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100343 if (entry_count >= cache->max_secondary_entries) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100344 /* Some entries of the duplicate list might be expired so
345 * we will iterate over all the items in order to free some
346 * space. In order to avoid going over the same list too
347 * often, we first check the timestamp of the last check
348 * performed. */
349 if (last_clear_ts == now.tv_sec) {
350 /* Too many entries for this primary key, clear the
351 * one that was inserted. */
352 eb32_delete(node);
353 node->key = 0;
354 return NULL;
355 }
356
357 entry_count = clear_expired_duplicates(&prev);
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100358 if (entry_count >= cache->max_secondary_entries) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100359 /* Still too many entries for this primary key, delete
360 * the newly inserted one. */
361 entry = container_of(prev, struct cache_entry, eb);
362 entry->last_clear_ts = now.tv_sec;
363 eb32_delete(node);
364 node->key = 0;
365 return NULL;
366 }
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100367 }
368 }
369
370 new_entry->secondary_entries_count = entry_count + 1;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100371 new_entry->last_clear_ts = last_clear_ts;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100372
373 return node;
374}
375
376
377/*
378 * This function removes an entry from the ebtree. If the entry was a duplicate
379 * (in case of Vary), it updates the secondary entry counter in another
380 * duplicate entry (the last entry of the dup list).
381 */
382static void delete_entry(struct cache_entry *del_entry)
383{
384 struct eb32_node *prev = NULL, *next = NULL;
385 struct cache_entry *entry = NULL;
386 struct eb32_node *last = NULL;
387
388 if (del_entry->secondary_key_signature) {
389 next = &del_entry->eb;
390
391 /* Look for last entry of the duplicates list. */
392 while ((next = eb32_next_dup(next))) {
393 last = next;
394 }
395
396 if (last) {
397 entry = container_of(last, struct cache_entry, eb);
398 --entry->secondary_entries_count;
399 }
400 else {
401 /* The current entry is the last one, look for the
402 * previous one to update its counter. */
403 prev = eb32_prev_dup(&del_entry->eb);
404 if (prev) {
405 entry = container_of(prev, struct cache_entry, eb);
406 entry->secondary_entries_count = del_entry->secondary_entries_count - 1;
407 }
408 }
409 }
410 eb32_delete(&del_entry->eb);
411 del_entry->eb.key = 0;
412}
413
414
William Lallemand4da3f8a2017-10-31 14:33:34 +0100415static inline struct shared_context *shctx_ptr(struct cache *cache)
416{
417 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
418}
419
William Lallemand77c11972017-10-31 20:43:01 +0100420static inline struct shared_block *block_ptr(struct cache_entry *entry)
421{
422 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
423}
424
425
426
William Lallemand41db4602017-10-30 11:15:51 +0100427static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100428cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100429{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100430 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100431 return 0;
432}
433
Christopher Faulet95220e22018-12-07 17:34:39 +0100434static void
435cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
436{
437 struct cache_flt_conf *cconf = fconf->conf;
438
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200439 if (!(cconf->flags & CACHE_FLT_INIT))
440 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100441 free(cconf);
442}
443
William Lallemand4da3f8a2017-10-31 14:33:34 +0100444static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100445cache_store_check(struct proxy *px, struct flt_conf *fconf)
446{
447 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100448 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100449 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100450 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100451
William Lallemandd1d1e222019-08-28 15:22:49 +0200452 /* Find the cache corresponding to the name in the filter config. The
453 * cache will not be referenced now in the filter config because it is
454 * not fully allocated. This step will be performed during the cache
455 * post_check.
456 */
457 list_for_each_entry(cache, &caches_config, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100458 if (strcmp(cache->id, cconf->c.name) == 0)
Christopher Faulet95220e22018-12-07 17:34:39 +0100459 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100460 }
461
462 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
463 proxy_type_str(px), px->id, (char *)cconf->c.name);
464 return 1;
465
466 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100467 /* Here <cache> points on the cache the filter must use and <cconf>
468 * points on the cache filter configuration. */
469
470 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100471 * enabled and if it is after the cache. When the compression is before
472 * the cache, an error is returned. Also check if the cache filter must
473 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100474 list_for_each_entry(f, &px->filter_configs, list) {
475 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100476 /* The compression filter must be evaluated after the cache. */
477 if (comp) {
478 ha_alert("config: %s '%s': unable to enable the compression filter before "
479 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
480 return 1;
481 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100482 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200483 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100484 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200485 else if (f->id == fcgi_flt_id)
486 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100487 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
488 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200489 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100490 * declaration is required. */
491 ha_alert("config: %s '%s': require an explicit filter declaration "
492 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
493 return 1;
494 }
495
Christopher Fauletafd819c2018-12-11 08:57:45 +0100496 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100497 return 0;
498}
499
500static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100501cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502{
Christopher Faulet65554e12020-03-06 14:52:06 +0100503 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100504
Willy Tarreauacc5b012021-03-22 15:00:49 +0100505 st = pool_alloc(pool_head_cache_st);
Christopher Faulet65554e12020-03-06 14:52:06 +0100506 if (st == NULL)
507 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100508
Christopher Faulet65554e12020-03-06 14:52:06 +0100509 st->first_block = NULL;
510 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100511
Christopher Faulet65554e12020-03-06 14:52:06 +0100512 /* Register post-analyzer on AN_RES_WAIT_HTTP */
513 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100514 return 1;
515}
516
Christopher Faulet65554e12020-03-06 14:52:06 +0100517static void
518cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100519{
520 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100521 struct cache_flt_conf *cconf = FLT_CONF(filter);
522 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100523 struct shared_context *shctx = shctx_ptr(cache);
524
William Lallemand49dc0482017-11-24 14:33:54 +0100525 /* Everything should be released in the http_end filter, but we need to do it
526 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100527 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100528 shctx_lock(shctx);
529 shctx_row_dec_hot(shctx, st->first_block);
530 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100531 }
532 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100533 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100534 filter->ctx = NULL;
535 }
William Lallemand49dc0482017-11-24 14:33:54 +0100536}
537
Christopher Faulet839791a2019-01-07 16:12:07 +0100538static int
539cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
540 unsigned an_bit)
541{
542 struct http_txn *txn = s->txn;
543 struct http_msg *msg = &txn->rsp;
544 struct cache_st *st = filter->ctx;
545
546 if (an_bit != AN_RES_WAIT_HTTP)
547 goto end;
548
549 /* Here we need to check if any compression filter precedes the cache
550 * filter. This is only possible when the compression is configured in
551 * the frontend while the cache filter is configured on the
552 * backend. This case cannot be detected during HAProxy startup. So in
553 * such cases, the cache is disabled.
554 */
555 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
556 pool_free(pool_head_cache_st, st);
557 filter->ctx = NULL;
558 }
559
560 end:
561 return 1;
562}
William Lallemand49dc0482017-11-24 14:33:54 +0100563
564static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100565cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
566{
567 struct cache_st *st = filter->ctx;
568
William Lallemand4da3f8a2017-10-31 14:33:34 +0100569 if (!(msg->chn->flags & CF_ISRESP) || !st)
570 return 1;
571
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200572 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100573 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100574 return 1;
575}
576
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200577static inline void disable_cache_entry(struct cache_st *st,
578 struct filter *filter, struct shared_context *shctx)
579{
580 struct cache_entry *object;
581
582 object = (struct cache_entry *)st->first_block->data;
583 filter->ctx = NULL; /* disable cache */
584 shctx_lock(shctx);
585 shctx_row_dec_hot(shctx, st->first_block);
Remi Tricot-Le Breton964caaf2020-12-15 14:30:12 +0100586 eb32_delete(&object->eb);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200587 object->eb.key = 0;
588 shctx_unlock(shctx);
589 pool_free(pool_head_cache_st, st);
590}
591
William Lallemand4da3f8a2017-10-31 14:33:34 +0100592static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100593cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
594 unsigned int offset, unsigned int len)
595{
Christopher Faulet95220e22018-12-07 17:34:39 +0100596 struct cache_flt_conf *cconf = FLT_CONF(filter);
597 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100598 struct cache_st *st = filter->ctx;
599 struct htx *htx = htxbuf(&msg->chn->buf);
600 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200601 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100602 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200603 unsigned int orig_len, to_forward;
604 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100605
606 if (!len)
607 return len;
608
609 if (!st->first_block) {
610 unregister_data_filter(s, msg->chn, filter);
611 return len;
612 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100613
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200614 chunk_reset(&trash);
615 orig_len = len;
616 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100617
618 htxret = htx_find_offset(htx, offset);
619 blk = htxret.blk;
620 offset = htxret.ret;
621 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100622 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200623 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100624 struct ist v;
625
626 switch (type) {
627 case HTX_BLK_UNUSED:
628 break;
629
630 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100631 v = htx_get_blk_value(htx, blk);
Tim Duesterhus154374c2021-03-02 18:57:27 +0100632 v = istadv(v, offset);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +0100633 v = isttrim(v, len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100634
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200635 info = (type << 28) + v.len;
636 chunk_memcat(&trash, (char *)&info, sizeof(info));
Tim Duesterhus77508502022-03-15 13:11:06 +0100637 chunk_istcat(&trash, v);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100638 to_forward += v.len;
639 len -= v.len;
640 break;
641
642 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200643 /* Here offset must always be 0 because only
644 * DATA blocks can be partially transferred. */
645 if (offset)
646 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100647 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200648 goto end;
649
650 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
651 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100652 to_forward += sz;
653 len -= sz;
654 break;
655 }
656
657 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100658 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200659
660 end:
661 shctx_lock(shctx);
662 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
663 if (!fb) {
664 shctx_unlock(shctx);
665 goto no_cache;
666 }
667 shctx_unlock(shctx);
668
669 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
670 (unsigned char *)b_head(&trash), b_data(&trash));
671 if (ret < 0)
672 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100673
674 return to_forward;
675
676 no_cache:
677 disable_cache_entry(st, filter, shctx);
678 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200679 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100680}
681
682static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100683cache_store_http_end(struct stream *s, struct filter *filter,
684 struct http_msg *msg)
685{
686 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100687 struct cache_flt_conf *cconf = FLT_CONF(filter);
688 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100689 struct shared_context *shctx = shctx_ptr(cache);
690 struct cache_entry *object;
691
692 if (!(msg->chn->flags & CF_ISRESP))
693 return 1;
694
695 if (st && st->first_block) {
696
697 object = (struct cache_entry *)st->first_block->data;
698
William Lallemand4da3f8a2017-10-31 14:33:34 +0100699 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100700 /* The whole payload was cached, the entry can now be used. */
701 object->complete = 1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100702 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100703 shctx_row_dec_hot(shctx, st->first_block);
704 shctx_unlock(shctx);
705
706 }
707 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100708 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100709 filter->ctx = NULL;
710 }
711
712 return 1;
713}
714
715 /*
716 * This intends to be used when checking HTTP headers for some
717 * word=value directive. Return a pointer to the first character of value, if
Willy Tarreau94a01e12021-01-06 17:35:12 +0100718 * the word was not found or if there wasn't any value assigned to it return NULL
William Lallemand4da3f8a2017-10-31 14:33:34 +0100719 */
720char *directive_value(const char *sample, int slen, const char *word, int wlen)
721{
722 int st = 0;
723
724 if (slen < wlen)
725 return 0;
726
727 while (wlen) {
728 char c = *sample ^ *word;
729 if (c && c != ('A' ^ 'a'))
730 return NULL;
731 sample++;
732 word++;
733 slen--;
734 wlen--;
735 }
736
737 while (slen) {
738 if (st == 0) {
739 if (*sample != '=')
740 return NULL;
741 sample++;
742 slen--;
743 st = 1;
744 continue;
745 } else {
746 return (char *)sample;
747 }
748 }
749
750 return NULL;
751}
752
753/*
754 * Return the maxage in seconds of an HTTP response.
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100755 * The returned value will always take the cache's configuration into account
756 * (cache->maxage) but the actual max age of the response will be set in the
757 * true_maxage parameter. It will be used to determine if a response is already
758 * stale or not.
William Lallemand4da3f8a2017-10-31 14:33:34 +0100759 * Compute the maxage using either:
760 * - the assigned max-age of the cache
761 * - the s-maxage directive
762 * - the max-age directive
763 * - (Expires - Data) headers
764 * - the default-max-age of the cache
765 *
766 */
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100767int http_calc_maxage(struct stream *s, struct cache *cache, int *true_maxage)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100768{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200769 struct htx *htx = htxbuf(&s->res.buf);
770 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100771 long smaxage = -1;
772 long maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100773 int expires = -1;
774 struct tm tm = {};
775 time_t expires_val = 0;
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100776 char *endptr = NULL;
777 int offset = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100778
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100779 /* The Cache-Control max-age and s-maxage directives should be followed by
780 * a positive numerical value (see RFC 7234#5.2.1.1). According to the
781 * specs, a sender "should not" generate a quoted-string value but we will
782 * still accept this format since it isn't strictly forbidden. */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200783 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
784 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100785
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200786 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
787 if (value) {
788 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100789
Willy Tarreau49b04822021-11-08 11:44:47 +0100790 chunk_memcat(chk, value, ctx.value.len - 8 + 1);
791 chunk_memcat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100792 offset = (*chk->area == '"') ? 1 : 0;
793 smaxage = strtol(chk->area + offset, &endptr, 10);
Willy Tarreau1f38bdb2021-11-08 12:09:27 +0100794 if (unlikely(smaxage < 0 || endptr == chk->area + offset))
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100795 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100796 }
797
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200798 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
799 if (value) {
800 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200801
Willy Tarreau49b04822021-11-08 11:44:47 +0100802 chunk_memcat(chk, value, ctx.value.len - 7 + 1);
803 chunk_memcat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100804 offset = (*chk->area == '"') ? 1 : 0;
805 maxage = strtol(chk->area + offset, &endptr, 10);
Willy Tarreau1f38bdb2021-11-08 12:09:27 +0100806 if (unlikely(maxage < 0 || endptr == chk->area + offset))
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100807 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100808 }
809 }
810
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100811 /* Look for Expires header if no s-maxage or max-age Cache-Control data
812 * was found. */
813 if (maxage == -1 && smaxage == -1) {
814 ctx.blk = NULL;
815 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
816 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
817 expires_val = my_timegm(&tm);
818 /* A request having an expiring date earlier
819 * than the current date should be considered as
820 * stale. */
821 expires = (expires_val >= now.tv_sec) ?
822 (expires_val - now.tv_sec) : 0;
823 }
824 else {
825 /* Following RFC 7234#5.3, an invalid date
826 * format must be treated as a date in the past
827 * so the cache entry must be seen as already
828 * expired. */
829 expires = 0;
830 }
831 }
832 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100833
834
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100835 if (smaxage > 0) {
836 if (true_maxage)
837 *true_maxage = smaxage;
William Lallemand49b44532017-11-24 18:53:43 +0100838 return MIN(smaxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100839 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100840
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100841 if (maxage > 0) {
842 if (true_maxage)
843 *true_maxage = maxage;
William Lallemand49b44532017-11-24 18:53:43 +0100844 return MIN(maxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100845 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100846
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100847 if (expires >= 0) {
848 if (true_maxage)
849 *true_maxage = expires;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100850 return MIN(expires, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100851 }
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100852
William Lallemand49b44532017-11-24 18:53:43 +0100853 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100854
855}
856
857
William Lallemanda400a3a2017-11-20 19:13:12 +0100858static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
859{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200860 struct cache_entry *object = (struct cache_entry *)block->data;
861
862 if (first == block && object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100863 delete_entry(object);
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200864 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100865}
866
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200867
868/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
869 * date value should be compared to a date determined by in a previous response (for
870 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
871 * value of the response's reception time (by decreasing order of priority). */
872static time_t get_last_modified_time(struct htx *htx)
873{
874 time_t last_modified = 0;
875 struct http_hdr_ctx ctx = { .blk = NULL };
876 struct tm tm = {};
877
878 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
879 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
880 last_modified = my_timegm(&tm);
881 }
882 }
883
884 if (!last_modified) {
885 ctx.blk = NULL;
886 if (http_find_header(htx, ist("date"), &ctx, 1)) {
887 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
888 last_modified = my_timegm(&tm);
889 }
890 }
891 }
892
893 /* Fallback on the current time if no "Last-Modified" or "Date" header
894 * was found. */
895 if (!last_modified)
896 last_modified = now.tv_sec;
897
898 return last_modified;
899}
900
William Lallemand41db4602017-10-30 11:15:51 +0100901/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100902 * Checks the vary header's value. The headers on which vary should be applied
Ilya Shipitsinf38a0182020-12-21 01:16:17 +0500903 * must be explicitly supported in the vary_information array (see cache.c). If
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100904 * any other header is mentioned, we won't store the response.
905 * Returns 1 if Vary-based storage can work, 0 otherwise.
906 */
907static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
908{
909 unsigned int vary_idx;
910 unsigned int vary_info_count;
911 const struct vary_hashing_information *vary_info;
912 struct http_hdr_ctx ctx = { .blk = NULL };
913
914 int retval = 1;
915
916 *vary_signature = 0;
917
918 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
919 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
920 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
921 vary_info = &vary_information[vary_idx];
922 if (isteqi(ctx.value, vary_info->hdr_name)) {
923 *vary_signature |= vary_info->value;
924 break;
925 }
926 }
927 retval = (vary_idx < vary_info_count);
928 }
929
930 return retval;
931}
932
933
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100934/*
935 * Look for the accept-encoding part of the secondary_key and replace the
936 * encoding bitmap part of the hash with the actual encoding of the response,
937 * extracted from the content-encoding header value.
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100938 * Responses that have an unknown encoding will not be cached if they also
939 * "vary" on the accept-encoding value.
940 * Returns 0 if we found a known encoding in the response, -1 otherwise.
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100941 */
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100942static int set_secondary_key_encoding(struct htx *htx, char *secondary_key)
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100943{
944 unsigned int resp_encoding_bitmap = 0;
945 const struct vary_hashing_information *info = vary_information;
946 unsigned int offset = 0;
947 unsigned int count = 0;
948 unsigned int hash_info_count = sizeof(vary_information)/sizeof(*vary_information);
949 unsigned int encoding_value;
950 struct http_hdr_ctx ctx = { .blk = NULL };
951
952 /* Look for the accept-encoding part of the secondary_key. */
953 while (count < hash_info_count && info->value != VARY_ACCEPT_ENCODING) {
954 offset += info->hash_length;
955 ++info;
956 ++count;
957 }
958
959 if (count == hash_info_count)
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100960 return -1;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100961
962 while (http_find_header(htx, ist("content-encoding"), &ctx, 0)) {
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100963 if (parse_encoding_value(ctx.value, &encoding_value, NULL))
964 return -1; /* Do not store responses with an unknown encoding */
965 resp_encoding_bitmap |= encoding_value;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100966 }
967
968 if (!resp_encoding_bitmap)
969 resp_encoding_bitmap |= VARY_ENCODING_IDENTITY;
970
971 /* Rewrite the bitmap part of the hash with the new bitmap that only
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500972 * corresponds the the response's encoding. */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100973 write_u32(secondary_key + offset, resp_encoding_bitmap);
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100974
975 return 0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100976}
977
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100978
979/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500980 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100981 * register a filter to store the data
982 */
983enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200984 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100985{
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100986 int effective_maxage = 0;
987 int true_maxage = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100988 struct http_txn *txn = s->txn;
989 struct http_msg *msg = &txn->rsp;
990 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100991 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100992 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100993 struct cache *cache = cconf->c.cache;
994 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100995 struct cache_st *cache_ctx = NULL;
996 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100997 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200998 struct htx *htx;
999 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +02001000 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001001 int32_t pos;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001002 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001003
William Lallemand4da3f8a2017-10-31 14:33:34 +01001004 /* Don't cache if the response came from a cache */
1005 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
1006 s->target == &http_cache_applet.obj_type) {
1007 goto out;
1008 }
1009
1010 /* cache only HTTP/1.1 */
1011 if (!(txn->req.flags & HTTP_MSGF_VER_11))
1012 goto out;
1013
Willy Tarreau6905d182019-10-01 17:59:17 +02001014 /* cache only GET method */
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001015 if (txn->meth != HTTP_METH_GET) {
1016 /* In case of successful unsafe method on a stored resource, the
1017 * cached entry must be invalidated (see RFC7234#4.4).
1018 * A "non-error response" is one with a 2xx (Successful) or 3xx
1019 * (Redirection) status code. */
1020 if (txn->status >= 200 && txn->status < 400) {
1021 switch (txn->meth) {
1022 case HTTP_METH_OPTIONS:
1023 case HTTP_METH_GET:
1024 case HTTP_METH_HEAD:
1025 case HTTP_METH_TRACE:
1026 break;
1027
1028 default: /* Any unsafe method */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001029 /* Discard any corresponding entry in case of successful
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001030 * unsafe request (such as PUT, POST or DELETE). */
1031 shctx_lock(shctx);
1032
1033 old = entry_exist(cconf->c.cache, txn->cache_hash);
1034 if (old) {
1035 eb32_delete(&old->eb);
1036 old->eb.key = 0;
1037 }
1038 shctx_unlock(shctx);
1039 }
1040 }
William Lallemand4da3f8a2017-10-31 14:33:34 +01001041 goto out;
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001042 }
William Lallemand4da3f8a2017-10-31 14:33:34 +01001043
Willy Tarreauc9036c02019-01-11 19:38:25 +01001044 /* cache key was not computed */
1045 if (!key)
1046 goto out;
1047
William Lallemand4da3f8a2017-10-31 14:33:34 +01001048 /* cache only 200 status code */
1049 if (txn->status != 200)
1050 goto out;
1051
Christopher Faulet839791a2019-01-07 16:12:07 +01001052 /* Find the corresponding filter instance for the current stream */
1053 list_for_each_entry(filter, &s->strm_flt.filters, list) {
1054 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
1055 /* No filter ctx, don't cache anything */
1056 if (!filter->ctx)
1057 goto out;
1058 cache_ctx = filter->ctx;
1059 break;
1060 }
1061 }
1062
1063 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001064 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001065
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001066 /* Do not cache too big objects. */
1067 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
1068 htx->data + htx->extra > shctx->max_obj_size)
1069 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001070
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001071 /* Only a subset of headers are supported in our Vary implementation. If
1072 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001073 * able to use the cache. Likewise, if Vary header support is disabled,
1074 * avoid caching responses that contain such a header. */
1075 ctx.blk = NULL;
1076 if (cache->vary_processing_enabled) {
1077 if (!http_check_vary_header(htx, &vary_signature))
1078 goto out;
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001079 if (vary_signature) {
1080 /* If something went wrong during the secondary key
1081 * building, do not store the response. */
1082 if (!(txn->flags & TX_CACHE_HAS_SEC_KEY))
1083 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001084 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001085 }
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001086 }
1087 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001088 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001089 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001090
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001091 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001092
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01001093 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001094 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001095
1096 shctx_lock(shctx);
1097 old = entry_exist(cache, txn->cache_hash);
1098 if (old) {
1099 if (vary_signature)
1100 old = secondary_entry_exist(cconf->c.cache, old,
1101 txn->cache_secondary_hash);
1102 if (old) {
1103 if (!old->complete) {
1104 /* An entry with the same primary key is already being
1105 * created, we should not try to store the current
1106 * response because it will waste space in the cache. */
1107 shctx_unlock(shctx);
1108 goto out;
1109 }
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001110 delete_entry(old);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001111 old->eb.key = 0;
1112 }
1113 }
1114 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry));
1115 if (!first) {
1116 shctx_unlock(shctx);
1117 goto out;
1118 }
1119 /* the received memory is not initialized, we need at least to mark
1120 * the object as not indexed yet.
1121 */
1122 object = (struct cache_entry *)first->data;
1123 memset(object, 0, sizeof(*object));
1124 object->eb.key = key;
1125 object->secondary_key_signature = vary_signature;
1126 /* We need to temporarily set a valid expiring time until the actual one
1127 * is set by the end of this function (in case of concurrent accesses to
1128 * the same resource). This way the second access will find an existing
1129 * but not yet usable entry in the tree and will avoid storing its data. */
1130 object->expire = now.tv_sec + 2;
1131
1132 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
1133 if (vary_signature)
1134 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
1135
1136 /* Insert the entry in the tree even if the payload is not cached yet. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001137 if (insert_entry(cache, object) != &object->eb) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001138 object->eb.key = 0;
1139 shctx_unlock(shctx);
1140 goto out;
1141 }
1142 shctx_unlock(shctx);
1143
1144 /* reserve space for the cache_entry structure */
1145 first->len = sizeof(struct cache_entry);
1146 first->last_append = NULL;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001147
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001148 /* Determine the entry's maximum age (taking into account the cache's
1149 * configuration) as well as the response's explicit max age (extracted
1150 * from cache-control directives or the expires header). */
1151 effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage);
1152
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001153 ctx.blk = NULL;
1154 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
Tim Duesterhusc2942842021-01-02 22:47:17 +01001155 long long hdr_age;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001156 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
1157 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
1158 hdr_age = CACHE_ENTRY_MAX_AGE;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001159 /* A response with an Age value greater than its
1160 * announced max age is stale and should not be stored. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001161 object->age = hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001162 if (unlikely(object->age > true_maxage))
1163 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001164 }
Remi Tricot-Le Breton51058d62020-12-03 18:19:32 +01001165 else
1166 goto out;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001167 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001168 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001169
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001170 /* Build a last-modified time that will be stored in the cache_entry and
1171 * compared to a future If-Modified-Since client header. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001172 object->last_modified = get_last_modified_time(htx);
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001173
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001174 chunk_reset(&trash);
1175 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1176 struct htx_blk *blk = htx_get_blk(htx, pos);
1177 enum htx_blk_type type = htx_get_blk_type(blk);
1178 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001179
Christopher Fauletb0667472019-09-03 22:22:12 +02001180 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001181 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
1182 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001183
1184 /* Look for optional ETag header.
1185 * We need to store the offset of the ETag value in order for
1186 * future conditional requests to be able to perform ETag
1187 * comparisons. */
1188 if (type == HTX_BLK_HDR) {
Tim Duesterhuse2fff102021-01-02 22:47:16 +01001189 struct ist header_name = htx_get_blk_name(htx, blk);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001190 if (isteq(header_name, ist("etag"))) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001191 object->etag_length = sz - istlen(header_name);
1192 object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001193 }
1194 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001195 if (type == HTX_BLK_EOH)
1196 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001197 }
1198
Christopher Fauletb0667472019-09-03 22:22:12 +02001199 /* Do not cache objects if the headers are too big. */
1200 if (hdrs_len > htx->size - global.tune.maxrewrite)
1201 goto out;
1202
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001203 /* If the response has a secondary_key, fill its key part related to
1204 * encodings with the actual encoding of the response. This way any
1205 * subsequent request having the same primary key will have its accepted
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +01001206 * encodings tested upon the cached response's one.
1207 * We will not cache a response that has an unknown encoding (not
Ilya Shipitsin7704b0e2021-01-23 02:11:59 +05001208 * explicitly supported in parse_encoding_value function). */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001209 if (cache->vary_processing_enabled && vary_signature)
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +01001210 if (set_secondary_key_encoding(htx, object->secondary_key))
1211 goto out;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001212
William Lallemand4da3f8a2017-10-31 14:33:34 +01001213 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001214 if (!shctx_row_reserve_hot(shctx, first, trash.data)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +01001215 shctx_unlock(shctx);
1216 goto out;
1217 }
1218 shctx_unlock(shctx);
1219
William Lallemand4da3f8a2017-10-31 14:33:34 +01001220 /* cache the headers in a http action because it allows to chose what
1221 * to cache, for example you might want to cache a response before
1222 * modifying some HTTP headers, or on the contrary after modifying
1223 * those headers.
1224 */
William Lallemand4da3f8a2017-10-31 14:33:34 +01001225 /* does not need to be locked because it's in the "hot" list,
1226 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001227 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
1228 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001229
1230 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +01001231 if (cache_ctx) {
1232 cache_ctx->first_block = first;
Christopher Faulet839791a2019-01-07 16:12:07 +01001233 /* store latest value and expiration time */
1234 object->latest_validation = now.tv_sec;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001235 object->expire = now.tv_sec + effective_maxage;
Christopher Faulet839791a2019-01-07 16:12:07 +01001236 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001237 }
1238
1239out:
1240 /* if does not cache */
1241 if (first) {
1242 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +01001243 first->len = 0;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001244 if (object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001245 delete_entry(object);
William Lallemand08727662017-11-21 20:01:27 +01001246 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001247 shctx_row_dec_hot(shctx, first);
1248 shctx_unlock(shctx);
1249 }
1250
William Lallemand41db4602017-10-30 11:15:51 +01001251 return ACT_RET_CONT;
1252}
1253
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001254#define HTX_CACHE_INIT 0 /* Initial state. */
1255#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
1256#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001257#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
1258#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001259
William Lallemandecb73b12017-11-24 14:33:55 +01001260static void http_cache_applet_release(struct appctx *appctx)
1261{
Christopher Faulet95220e22018-12-07 17:34:39 +01001262 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +01001263 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +01001264 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +01001265 struct shared_block *first = block_ptr(cache_ptr);
1266
1267 shctx_lock(shctx_ptr(cache));
1268 shctx_row_dec_hot(shctx_ptr(cache), first);
1269 shctx_unlock(shctx_ptr(cache));
1270}
1271
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001272
1273static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
1274 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001275{
Christopher Faulet95220e22018-12-07 17:34:39 +01001276 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1277 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001278 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001279 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001280 unsigned int max, total;
1281 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001282
Christopher Faulet908628c2022-03-25 16:43:49 +01001283 max = htx_get_max_blksz(htx, channel_htx_recv_max(cs_ic(appctx->owner), htx));
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001284 if (!max)
1285 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001286 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001287 ? (info & 0xff) + ((info >> 8) & 0xfffff)
1288 : info & 0xfffffff);
1289 if (blksz > max)
1290 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001291
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001292 blk = htx_add_blk(htx, type, blksz);
1293 if (!blk)
1294 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001295
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001296 blk->info = info;
1297 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001298 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001299 while (blksz) {
1300 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001301 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001302 offset += max;
1303 blksz -= max;
1304 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001305 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001306 if (blksz || offset == shctx->block_size) {
1307 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1308 offset = 0;
1309 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001310 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001311 appctx->ctx.cache.offset = offset;
1312 appctx->ctx.cache.next = shblk;
1313 appctx->ctx.cache.sent += total;
1314 return total;
1315}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001316
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001317static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
1318 uint32_t info, struct shared_block *shblk, unsigned int offset)
1319{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001320
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001321 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1322 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
1323 unsigned int max, total, rem_data;
1324 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001325
Christopher Faulet908628c2022-03-25 16:43:49 +01001326 max = htx_get_max_blksz(htx, channel_htx_recv_max(cs_ic(appctx->owner), htx));
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001327 if (!max)
1328 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001329
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001330 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001331 if (appctx->ctx.cache.rem_data) {
1332 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001333 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001334 }
1335 else {
1336 blksz = (info & 0xfffffff);
1337 total = 4;
1338 }
1339 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001340 rem_data = blksz - max;
1341 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001342 }
1343
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001344 while (blksz) {
1345 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001346
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001347 max = MIN(blksz, shctx->block_size - offset);
1348 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1349 offset += sz;
1350 blksz -= sz;
1351 total += sz;
1352 if (sz < max)
1353 break;
1354 if (blksz || offset == shctx->block_size) {
1355 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1356 offset = 0;
1357 }
1358 }
1359
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001360 appctx->ctx.cache.offset = offset;
1361 appctx->ctx.cache.next = shblk;
1362 appctx->ctx.cache.sent += total;
1363 appctx->ctx.cache.rem_data = rem_data + blksz;
1364 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001365}
1366
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001367static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1368 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001369{
Christopher Faulet95220e22018-12-07 17:34:39 +01001370 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1371 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001372 struct shared_block *shblk;
1373 unsigned int offset, sz;
1374 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001375
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001376 while (len) {
1377 enum htx_blk_type type;
1378 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001379
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001380 shblk = appctx->ctx.cache.next;
1381 offset = appctx->ctx.cache.offset;
1382 if (appctx->ctx.cache.rem_data) {
1383 type = HTX_BLK_DATA;
1384 info = 0;
1385 goto add_data_blk;
1386 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001387
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001388 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001389 sz = MIN(4, shctx->block_size - offset);
1390 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1391 offset += sz;
1392 if (sz < 4) {
1393 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1394 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1395 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001396 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001397
1398 /* Get payload of the next HTX block and insert it. */
1399 type = (info >> 28);
1400 if (type != HTX_BLK_DATA)
1401 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1402 else {
1403 add_data_blk:
1404 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001405 }
1406
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001407 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001408 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001409 total += ret;
1410 len -= ret;
1411
1412 if (appctx->ctx.cache.rem_data || type == mark)
1413 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001414 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001415
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001416 return total;
1417}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001418
1419static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1420{
1421 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1422 unsigned int age;
1423 char *end;
1424
1425 chunk_reset(&trash);
1426 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1427 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1428 age = CACHE_ENTRY_MAX_AGE;
1429 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1430 b_set_data(&trash, end - b_head(&trash));
1431 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1432 return 0;
1433 return 1;
1434}
1435
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001436static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001437{
1438 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1439 struct shared_block *first = block_ptr(cache_ptr);
Christopher Faulet908628c2022-03-25 16:43:49 +01001440 struct conn_stream *cs = appctx->owner;
1441 struct channel *req = cs_oc(cs);
1442 struct channel *res = cs_ic(cs);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001443 struct htx *req_htx, *res_htx;
1444 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001445 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001446 size_t ret, total = 0;
1447
Christopher Faulet8b1eed12022-03-07 16:44:30 +01001448 res_htx = htx_from_buf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001449 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001450
Christopher Faulet62e75742022-03-31 09:16:34 +02001451 if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001452 goto out;
1453
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001454 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001455 if (!b_size(&res->buf)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001456 cs_rx_room_blk(cs);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001457 goto out;
1458 }
1459
Willy Tarreauefef3232018-12-16 00:37:45 +01001460 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001461 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001462
1463 if (appctx->st0 == HTX_CACHE_INIT) {
1464 appctx->ctx.cache.next = block_ptr(cache_ptr);
1465 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1466 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001467 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001468 appctx->st0 = HTX_CACHE_HEADER;
1469 }
1470
1471 if (appctx->st0 == HTX_CACHE_HEADER) {
1472 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001473 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1474 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1475 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1476 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001477 goto error;
1478
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001479 /* In case of a conditional request, we might want to send a
1480 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001481 if (appctx->ctx.cache.send_notmodified) {
1482 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1483 /* If replacing the status code fails we need to send the full response. */
1484 appctx->ctx.cache.send_notmodified = 0;
1485 }
1486 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001487
1488 /* Skip response body for HEAD requests or in case of "304 Not
1489 * Modified" response. */
Christopher Faulet908628c2022-03-25 16:43:49 +01001490 if (__cs_strm(cs)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001491 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001492 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001493 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001494 }
1495
1496 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001497 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1498 if (len) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001499 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_UNUSED);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001500 if (ret < len) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001501 cs_rx_room_blk(cs);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001502 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001503 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001504 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001505 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001506 }
1507
1508 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001509 /* no more data are expected. */
1510 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet908628c2022-03-25 16:43:49 +01001511 cs->endp->flags |= CS_EP_EOI;
Christopher Fauletdbf1e882022-03-07 15:53:57 +01001512 res->flags |= CF_EOI;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001513 appctx->st0 = HTX_CACHE_END;
1514 }
1515
1516 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001517 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001518 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02001519 cs_shutr(cs);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001520 }
1521
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001522 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001523 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001524 if (total)
1525 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001526 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001527
1528 /* eat the whole request */
1529 if (co_data(req)) {
1530 req_htx = htx_from_buf(&req->buf);
1531 co_htx_skip(req, req_htx, co_data(req));
1532 htx_to_buf(req_htx, &req->buf);
1533 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001534 return;
1535
1536 error:
1537 /* Sent and HTTP error 500 */
1538 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001539 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001540 res->buf.data = b_data(errmsg);
1541 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1542 res_htx = htx_from_buf(&res->buf);
1543
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001544 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001545 appctx->st0 = HTX_CACHE_END;
1546 goto end;
1547}
1548
1549
Christopher Faulet95220e22018-12-07 17:34:39 +01001550static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001551{
1552 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001553 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001554
Christopher Faulet95220e22018-12-07 17:34:39 +01001555 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001556 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001557 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001558 }
1559
1560 /* check if a cache filter was already registered with this cache
1561 * name, if that's the case, must use it. */
1562 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001563 if (fconf->id == cache_store_flt_id) {
1564 cconf = fconf->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001565 if (cconf && strcmp((char *)cconf->c.name, name) == 0) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001566 rule->arg.act.p[0] = cconf;
1567 return 1;
1568 }
William Lallemand41db4602017-10-30 11:15:51 +01001569 }
1570 }
1571
Christopher Faulet95220e22018-12-07 17:34:39 +01001572 /* Create the filter cache config */
1573 cconf = calloc(1, sizeof(*cconf));
1574 if (!cconf) {
1575 memprintf(err, "out of memory\n");
1576 goto err;
1577 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001578 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001579 cconf->c.name = strdup(name);
1580 if (!cconf->c.name) {
1581 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001582 goto err;
1583 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001584
William Lallemand41db4602017-10-30 11:15:51 +01001585 /* register a filter to fill the cache buffer */
1586 fconf = calloc(1, sizeof(*fconf));
1587 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001588 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001589 goto err;
1590 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001591 fconf->id = cache_store_flt_id;
1592 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001593 fconf->ops = &cache_ops;
Willy Tarreau2b718102021-04-21 07:32:39 +02001594 LIST_APPEND(&proxy->filter_configs, &fconf->list);
William Lallemand41db4602017-10-30 11:15:51 +01001595
Christopher Faulet95220e22018-12-07 17:34:39 +01001596 rule->arg.act.p[0] = cconf;
1597 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001598
Christopher Faulet95220e22018-12-07 17:34:39 +01001599 err:
1600 free(cconf);
1601 return 0;
1602}
1603
1604enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1605 struct act_rule *rule, char **err)
1606{
1607 rule->action = ACT_CUSTOM;
1608 rule->action_ptr = http_action_store_cache;
1609
1610 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1611 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001612
Christopher Faulet95220e22018-12-07 17:34:39 +01001613 (*orig_arg)++;
1614 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001615}
1616
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001617/* This produces a sha1 hash of the concatenation of the HTTP method,
1618 * the first occurrence of the Host header followed by the path component
1619 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001620int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001621{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001622 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001623 struct htx *htx = htxbuf(&s->req.buf);
1624 struct htx_sl *sl;
1625 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001626 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001627 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001628 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001629
William Lallemandf528fff2017-11-23 19:43:17 +01001630 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001631 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001632
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001633 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001634 uri = htx_sl_req_uri(sl); // whole uri
1635 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001636 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001637
1638 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1639 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1640 * URIs are almost always sent in absolute form with their scheme. In
1641 * this case, the scheme is almost always "https". In order to support
1642 * sharing of cache objects between H1 and H2, we'll hash the absolute
1643 * URI whenever known, or prepend "https://" + the Host header for
1644 * relative URIs. The difference will only appear on absolute HTTP/1
1645 * requests sent to an origin server, which practically is never met in
1646 * the real world so we don't care about the ability to share the same
1647 * key here.URIs are normalized from the absolute URI to an origin form as
1648 * well.
1649 */
1650 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001651 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001652 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1653 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001654 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001655 }
1656
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001657 chunk_istcat(trash, uri);
William Lallemandf528fff2017-11-23 19:43:17 +01001658
1659 /* hash everything */
1660 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001661 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001662 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1663
1664 return 1;
1665}
1666
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001667/* Looks for "If-None-Match" headers in the request and compares their value
1668 * with the one that might have been stored in the cache_entry. If any of them
1669 * matches, a "304 Not Modified" response should be sent instead of the cached
1670 * data.
1671 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001672 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1673 *
1674 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1675 * header and compare its value (date) to the one stored in the cache_entry.
1676 * If the request's date is later than the cached one, we also send a
1677 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1678 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001679 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1680 */
1681static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1682 struct cache_entry *entry)
1683{
1684 int retval = 0;
1685
1686 struct http_hdr_ctx ctx = { .blk = NULL };
1687 struct ist cache_entry_etag = IST_NULL;
1688 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001689 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001690
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001691 struct tm tm = {};
1692 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001693
1694 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001695 * cache_entry's ETag in order to perform comparisons.
1696 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001697 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001698 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001699
1700 /* A '*' matches everything. */
1701 if (isteq(ctx.value, ist("*")) != 0) {
1702 retval = 1;
1703 break;
1704 }
1705
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001706 /* No need to rebuild an etag if none was stored in the cache. */
1707 if (entry->etag_length == 0)
1708 break;
1709
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001710 /* Rebuild the stored ETag. */
1711 if (etag_buffer == NULL) {
1712 etag_buffer = get_trash_chunk();
1713
1714 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1715 (unsigned char*)b_orig(etag_buffer),
1716 entry->etag_offset, entry->etag_length) == 0) {
1717 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1718 } else {
1719 /* We could not rebuild the ETag in one go, we
1720 * won't send a "304 Not Modified" response. */
1721 break;
1722 }
1723 }
1724
1725 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1726 retval = 1;
1727 break;
1728 }
1729 }
1730
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001731 /* If the request did not contain an "If-None-Match" header, we look for
1732 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1733 if (retval == 0 && if_none_match_found == 0) {
1734 ctx.blk = NULL;
1735 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1736 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1737 if_modified_since = my_timegm(&tm);
1738
1739 /* We send a "304 Not Modified" response if the
1740 * entry's last modified date is earlier than
1741 * the one found in the "If-Modified-Since"
1742 * header. */
1743 retval = (entry->last_modified <= if_modified_since);
1744 }
1745 }
1746 }
1747
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001748 return retval;
1749}
1750
William Lallemand41db4602017-10-30 11:15:51 +01001751enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1752 struct session *sess, struct stream *s, int flags)
1753{
William Lallemand77c11972017-10-31 20:43:01 +01001754
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001755 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001756 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001757 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1758 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001759 struct shared_block *entry_block;
1760
William Lallemand77c11972017-10-31 20:43:01 +01001761
Willy Tarreau6905d182019-10-01 17:59:17 +02001762 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1763 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001764 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001765 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001766 txn->flags |= TX_CACHE_IGNORE;
1767
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001768 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001769
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001770 /* The request's hash has to be calculated for all requests, even POSTs
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001771 * or PUTs for instance because RFC7234 specifies that a successful
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001772 * "unsafe" method on a stored resource must invalidate it
1773 * (see RFC7234#4.4). */
1774 if (!sha1_hosturi(s))
Willy Tarreau504455c2017-12-22 17:47:35 +01001775 return ACT_RET_CONT;
1776
Willy Tarreau504455c2017-12-22 17:47:35 +01001777 if (s->txn->flags & TX_CACHE_IGNORE)
1778 return ACT_RET_CONT;
1779
Willy Tarreaua1214a52018-12-14 14:00:25 +01001780 if (px == strm_fe(s))
Willy Tarreau4781b152021-04-06 13:53:36 +02001781 _HA_ATOMIC_INC(&px->fe_counters.p.http.cache_lookups);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001782 else
Willy Tarreau4781b152021-04-06 13:53:36 +02001783 _HA_ATOMIC_INC(&px->be_counters.p.http.cache_lookups);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001784
William Lallemanda400a3a2017-11-20 19:13:12 +01001785 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001786 res = entry_exist(cache, s->txn->cache_hash);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001787 /* We must not use an entry that is not complete. */
1788 if (res && res->complete) {
William Lallemand77c11972017-10-31 20:43:01 +01001789 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001790 entry_block = block_ptr(res);
1791 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001792 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001793
1794 /* In case of Vary, we could have multiple entries with the same
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001795 * primary hash. We need to calculate the secondary hash in order
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001796 * to find the actual entry we want (if it exists). */
1797 if (res->secondary_key_signature) {
1798 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1799 shctx_lock(shctx_ptr(cache));
1800 sec_entry = secondary_entry_exist(cache, res,
1801 s->txn->cache_secondary_hash);
1802 if (sec_entry && sec_entry != res) {
1803 /* The wrong row was added to the hot list. */
1804 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1805 entry_block = block_ptr(sec_entry);
1806 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1807 }
1808 res = sec_entry;
1809 shctx_unlock(shctx_ptr(cache));
1810 }
1811 else
1812 res = NULL;
1813 }
1814
1815 /* We looked for a valid secondary entry and could not find one,
1816 * the request must be forwarded to the server. */
1817 if (!res) {
1818 shctx_lock(shctx_ptr(cache));
1819 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1820 shctx_unlock(shctx_ptr(cache));
1821 return ACT_RET_CONT;
1822 }
1823
William Lallemand77c11972017-10-31 20:43:01 +01001824 s->target = &http_cache_applet.obj_type;
Christopher Faulet1336ccf2022-04-12 18:15:16 +02001825 if ((appctx = cs_applet_create(s->csb, objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001826 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001827 appctx->rule = rule;
1828 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001829 appctx->ctx.cache.next = NULL;
1830 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001831 appctx->ctx.cache.send_notmodified =
1832 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001833
1834 if (px == strm_fe(s))
Willy Tarreau4781b152021-04-06 13:53:36 +02001835 _HA_ATOMIC_INC(&px->fe_counters.p.http.cache_hits);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001836 else
Willy Tarreau4781b152021-04-06 13:53:36 +02001837 _HA_ATOMIC_INC(&px->be_counters.p.http.cache_hits);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001838 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001839 } else {
Christopher Faulet1d216c72022-04-21 11:30:43 +02001840 s->target = NULL;
William Lallemand55e76742017-11-21 20:01:28 +01001841 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001842 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001843 shctx_unlock(shctx_ptr(cache));
Christopher Faulet1d216c72022-04-21 11:30:43 +02001844 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001845 }
1846 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001847 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001848
1849 /* Shared context does not need to be locked while we calculate the
1850 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001851 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001852 /* Build a complete secondary hash until the server response
1853 * tells us which fields should be kept (if any). */
1854 http_request_prebuild_full_secondary_key(s);
1855 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001856 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001857}
1858
1859
1860enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1861 struct act_rule *rule, char **err)
1862{
William Lallemand41db4602017-10-30 11:15:51 +01001863 rule->action = ACT_CUSTOM;
1864 rule->action_ptr = http_action_req_cache_use;
1865
Christopher Faulet95220e22018-12-07 17:34:39 +01001866 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001867 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001868
1869 (*orig_arg)++;
1870 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001871}
1872
1873int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1874{
1875 int err_code = 0;
1876
1877 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1878
1879 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001880 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001881 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001882 err_code |= ERR_ALERT | ERR_ABORT;
1883 goto out;
1884 }
1885
1886 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1887 err_code |= ERR_ABORT;
1888 goto out;
1889 }
1890
1891 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001892 struct cache *cache_config;
1893
William Lallemand41db4602017-10-30 11:15:51 +01001894 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1895 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001896 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001897 err_code |= ERR_ALERT | ERR_ABORT;
1898 goto out;
1899 }
1900
1901 strlcpy2(tmp_cache_config->id, args[1], 33);
1902 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001903 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001904 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001905 err_code |= ERR_WARN;
1906 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001907
1908 list_for_each_entry(cache_config, &caches_config, list) {
1909 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1910 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1911 file, linenum, tmp_cache_config->id);
1912 err_code |= ERR_ALERT | ERR_ABORT;
1913 goto out;
1914 }
1915 }
1916
William Lallemand49b44532017-11-24 18:53:43 +01001917 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001918 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001919 tmp_cache_config->maxobjsz = 0;
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +01001920 tmp_cache_config->max_secondary_entries = DEFAULT_MAX_SECONDARY_ENTRY;
William Lallemand41db4602017-10-30 11:15:51 +01001921 }
1922 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001923 unsigned long int maxsize;
1924 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001925
1926 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1927 err_code |= ERR_ABORT;
1928 goto out;
1929 }
1930
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001931 maxsize = strtoul(args[1], &err, 10);
1932 if (err == args[1] || *err != '\0') {
1933 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1934 file, linenum, args[1]);
1935 err_code |= ERR_ABORT;
1936 goto out;
1937 }
1938
1939 if (maxsize > (UINT_MAX >> 20)) {
1940 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1941 file, linenum, args[1], UINT_MAX >> 20);
1942 err_code |= ERR_ABORT;
1943 goto out;
1944 }
1945
William Lallemand41db4602017-10-30 11:15:51 +01001946 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001947 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001948 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001949 } else if (strcmp(args[0], "max-age") == 0) {
1950 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1951 err_code |= ERR_ABORT;
1952 goto out;
1953 }
1954
1955 if (!*args[1]) {
1956 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1957 file, linenum, args[0]);
1958 err_code |= ERR_WARN;
1959 }
1960
1961 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001962 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001963 unsigned int maxobjsz;
1964 char *err;
1965
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001966 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1967 err_code |= ERR_ABORT;
1968 goto out;
1969 }
1970
1971 if (!*args[1]) {
1972 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1973 file, linenum, args[0]);
1974 err_code |= ERR_WARN;
1975 }
1976
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001977 maxobjsz = strtoul(args[1], &err, 10);
1978 if (err == args[1] || *err != '\0') {
1979 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1980 file, linenum, args[1]);
1981 err_code |= ERR_ABORT;
1982 goto out;
1983 }
1984 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001985 } else if (strcmp(args[0], "process-vary") == 0) {
1986 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1987 err_code |= ERR_ABORT;
1988 goto out;
1989 }
1990
1991 if (!*args[1]) {
Remi Tricot-Le Bretone6cc5b52020-12-23 18:13:53 +01001992 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 +01001993 file, linenum, args[0]);
1994 err_code |= ERR_WARN;
1995 }
Remi Tricot-Le Bretone6cc5b52020-12-23 18:13:53 +01001996 if (strcmp(args[1], "on") == 0)
1997 tmp_cache_config->vary_processing_enabled = 1;
1998 else if (strcmp(args[1], "off") == 0)
1999 tmp_cache_config->vary_processing_enabled = 0;
2000 else {
2001 ha_warning("parsing [%s:%d]: '%s' expects \"on\" or \"off\" (enable or disable vary processing).\n",
2002 file, linenum, args[0]);
2003 err_code |= ERR_WARN;
2004 }
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +01002005 } else if (strcmp(args[0], "max-secondary-entries") == 0) {
2006 unsigned int max_sec_entries;
2007 char *err;
2008
2009 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
2010 err_code |= ERR_ABORT;
2011 goto out;
2012 }
2013
2014 if (!*args[1]) {
2015 ha_warning("parsing [%s:%d]: '%s' expects a strictly positive number.\n",
2016 file, linenum, args[0]);
2017 err_code |= ERR_WARN;
2018 }
2019
2020 max_sec_entries = strtoul(args[1], &err, 10);
2021 if (err == args[1] || *err != '\0' || max_sec_entries == 0) {
2022 ha_warning("parsing [%s:%d]: max-secondary-entries wrong value '%s'\n",
2023 file, linenum, args[1]);
2024 err_code |= ERR_ABORT;
2025 goto out;
2026 }
2027 tmp_cache_config->max_secondary_entries = max_sec_entries;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002028 }
2029 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002030 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01002031 err_code |= ERR_ALERT | ERR_FATAL;
2032 goto out;
2033 }
2034out:
2035 return err_code;
2036}
2037
2038/* once the cache section is parsed */
2039
2040int cfg_post_parse_section_cache()
2041{
William Lallemand41db4602017-10-30 11:15:51 +01002042 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01002043
2044 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01002045
2046 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002047 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01002048 err_code |= ERR_FATAL | ERR_ALERT;
2049 goto out;
2050 }
2051
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02002052 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002053 /* Default max. file size is a 256th of the cache size. */
2054 tmp_cache_config->maxobjsz =
2055 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02002056 }
2057 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
2058 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
2059 err_code |= ERR_FATAL | ERR_ALERT;
2060 goto out;
2061 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002062
William Lallemandd1d1e222019-08-28 15:22:49 +02002063 /* add to the list of cache to init and reinit tmp_cache_config
2064 * for next cache section, if any.
2065 */
Willy Tarreau2b718102021-04-21 07:32:39 +02002066 LIST_APPEND(&caches_config, &tmp_cache_config->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02002067 tmp_cache_config = NULL;
2068 return err_code;
2069 }
2070out:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002071 ha_free(&tmp_cache_config);
William Lallemandd1d1e222019-08-28 15:22:49 +02002072 return err_code;
2073
2074}
2075
2076int post_check_cache()
2077{
2078 struct proxy *px;
2079 struct cache *back, *cache_config, *cache;
2080 struct shared_context *shctx;
2081 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002082 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02002083
2084 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
2085
2086 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
2087 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01002088
Frédéric Lécaillebc584492018-10-25 20:18:59 +02002089 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01002090 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01002091 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01002092 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01002093 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01002094
2095 err_code |= ERR_FATAL | ERR_ALERT;
2096 goto out;
2097 }
William Lallemanda400a3a2017-11-20 19:13:12 +01002098 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02002099 /* the cache structure is stored in the shctx and added to the
2100 * caches list, we can remove the entry from the caches_config
2101 * list */
2102 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01002103 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01002104 cache->entries = EB_ROOT;
Willy Tarreau2b718102021-04-21 07:32:39 +02002105 LIST_APPEND(&caches, &cache->list);
2106 LIST_DELETE(&cache_config->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02002107 free(cache_config);
2108
2109 /* Find all references for this cache in the existing filters
2110 * (over all proxies) and reference it in matching filters.
2111 */
2112 for (px = proxies_list; px; px = px->next) {
2113 struct flt_conf *fconf;
2114 struct cache_flt_conf *cconf;
2115
2116 list_for_each_entry(fconf, &px->filter_configs, list) {
2117 if (fconf->id != cache_store_flt_id)
2118 continue;
2119
2120 cconf = fconf->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002121 if (strcmp(cache->id, cconf->c.name) == 0) {
William Lallemandd1d1e222019-08-28 15:22:49 +02002122 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02002123 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02002124 cconf->c.cache = cache;
2125 break;
2126 }
2127 }
2128 }
William Lallemand41db4602017-10-30 11:15:51 +01002129 }
William Lallemandd1d1e222019-08-28 15:22:49 +02002130
William Lallemand41db4602017-10-30 11:15:51 +01002131out:
William Lallemand41db4602017-10-30 11:15:51 +01002132 return err_code;
2133
William Lallemand41db4602017-10-30 11:15:51 +01002134}
2135
William Lallemand41db4602017-10-30 11:15:51 +01002136struct flt_ops cache_ops = {
2137 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01002138 .check = cache_store_check,
2139 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01002140
Christopher Faulet65554e12020-03-06 14:52:06 +01002141 /* Handle stream init/deinit */
2142 .attach = cache_store_strm_init,
2143 .detach = cache_store_strm_deinit,
2144
William Lallemand4da3f8a2017-10-31 14:33:34 +01002145 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01002146 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01002147
2148 /* Filter HTTP requests and responses */
2149 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01002150 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01002151 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01002152};
2153
Christopher Faulet99a17a22018-12-11 09:18:27 +01002154
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002155#define CHECK_ENCODING(str, encoding_name, encoding_value) \
2156 ({ \
2157 int retval = 0; \
2158 if (istmatch(str, (struct ist){ .ptr = encoding_name+1, .len = sizeof(encoding_name) - 2 })) { \
2159 retval = encoding_value; \
2160 encoding = istadv(encoding, sizeof(encoding_name) - 2); \
2161 } \
2162 (retval); \
2163 })
2164
2165/*
2166 * Parse the encoding <encoding> and try to match the encoding part upon an
2167 * encoding list of explicitly supported encodings (which all have a specific
2168 * bit in an encoding bitmap). If a weight is included in the value, find out if
2169 * it is null or not. The bit value will be set in the <encoding_value>
2170 * parameter and the <has_null_weight> will be set to 1 if the weight is strictly
2171 * 0, 1 otherwise.
2172 * The encodings list is extracted from
2173 * https://www.iana.org/assignments/http-parameters/http-parameters.xhtml.
2174 * Returns 0 in case of success and -1 in case of error.
2175 */
2176static int parse_encoding_value(struct ist encoding, unsigned int *encoding_value,
2177 unsigned int *has_null_weight)
2178{
2179 int retval = 0;
2180
2181 if (!encoding_value)
2182 return -1;
2183
2184 if (!istlen(encoding))
2185 return -1; /* Invalid encoding */
2186
2187 *encoding_value = 0;
2188 if (has_null_weight)
2189 *has_null_weight = 0;
2190
2191 switch (*encoding.ptr) {
2192 case 'a':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002193 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002194 *encoding_value = CHECK_ENCODING(encoding, "aes128gcm", VARY_ENCODING_AES128GCM);
2195 break;
2196 case 'b':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002197 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002198 *encoding_value = CHECK_ENCODING(encoding, "br", VARY_ENCODING_BR);
2199 break;
2200 case 'c':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002201 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002202 *encoding_value = CHECK_ENCODING(encoding, "compress", VARY_ENCODING_COMPRESS);
2203 break;
2204 case 'd':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002205 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002206 *encoding_value = CHECK_ENCODING(encoding, "deflate", VARY_ENCODING_DEFLATE);
2207 break;
2208 case 'e':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002209 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002210 *encoding_value = CHECK_ENCODING(encoding, "exi", VARY_ENCODING_EXI);
2211 break;
2212 case 'g':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002213 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002214 *encoding_value = CHECK_ENCODING(encoding, "gzip", VARY_ENCODING_GZIP);
2215 break;
2216 case 'i':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002217 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002218 *encoding_value = CHECK_ENCODING(encoding, "identity", VARY_ENCODING_IDENTITY);
2219 break;
2220 case 'p':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002221 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002222 *encoding_value = CHECK_ENCODING(encoding, "pack200-gzip", VARY_ENCODING_PACK200_GZIP);
2223 break;
2224 case 'x':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002225 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002226 *encoding_value = CHECK_ENCODING(encoding, "x-gzip", VARY_ENCODING_GZIP);
2227 if (!*encoding_value)
2228 *encoding_value = CHECK_ENCODING(encoding, "x-compress", VARY_ENCODING_COMPRESS);
2229 break;
2230 case 'z':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002231 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002232 *encoding_value = CHECK_ENCODING(encoding, "zstd", VARY_ENCODING_ZSTD);
2233 break;
2234 case '*':
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002235 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002236 *encoding_value = VARY_ENCODING_STAR;
2237 break;
2238 default:
2239 retval = -1; /* Unmanaged encoding */
2240 break;
2241 }
2242
2243 /* Process the optional weight part of the encoding. */
2244 if (*encoding_value) {
2245 encoding = http_trim_leading_spht(encoding);
2246 if (istlen(encoding)) {
2247 if (*encoding.ptr != ';')
2248 return -1;
2249
2250 if (has_null_weight) {
Tim Duesterhus284fbe12021-11-04 22:35:44 +01002251 encoding = istnext(encoding);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002252
2253 encoding = http_trim_leading_spht(encoding);
2254
2255 *has_null_weight = isteq(encoding, ist("q=0"));
2256 }
2257 }
2258 }
2259
2260 return retval;
2261}
2262
Tim Duesterhus23b29452020-11-24 22:22:56 +01002263#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002264/*
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002265 * Build a bitmap of the accept-encoding header.
2266 *
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002267 * The bitmap is built by matching every sub-part of the accept-encoding value
2268 * with a subset of explicitly supported encodings, which all have their own bit
2269 * in the bitmap. This bitmap will be used to determine if a response can be
2270 * served to a client (that is if it has an encoding that is accepted by the
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002271 * client). Any unknown encodings will be indicated by the VARY_ENCODING_OTHER
2272 * bit.
2273 *
2274 * Returns 0 in case of success and -1 in case of error.
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002275 */
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002276static int accept_encoding_normalizer(struct htx *htx, struct ist hdr_name,
2277 char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002278{
Tim Duesterhus23b29452020-11-24 22:22:56 +01002279 size_t count = 0;
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002280 uint32_t encoding_bitmap = 0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002281 unsigned int encoding_bmp_bl = -1;
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002282 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002283 unsigned int encoding_value;
2284 unsigned int rejected_encoding;
2285
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05002286 /* A user agent always accepts an unencoded value unless it explicitly
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002287 * refuses it through an "identity;q=0" accept-encoding value. */
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002288 encoding_bitmap |= VARY_ENCODING_IDENTITY;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002289
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002290 /* Iterate over all the ACCEPT_ENCODING_MAX_ENTRIES first accept-encoding
2291 * values that might span acrosse multiple accept-encoding headers. */
2292 while (http_find_header(htx, hdr_name, &ctx, 0) && count < ACCEPT_ENCODING_MAX_ENTRIES) {
Tim Duesterhus3bc6af42021-06-18 15:09:28 +02002293 count++;
2294
2295 /* As per RFC7231#5.3.4, "An Accept-Encoding header field with a
2296 * combined field-value that is empty implies that the user agent
2297 * does not want any content-coding in response."
2298 *
2299 * We must (and did) count the existence of this empty header to not
2300 * hit the `count == 0` case below, but must ignore the value to not
2301 * include VARY_ENCODING_OTHER into the final bitmap.
2302 */
2303 if (istlen(ctx.value) == 0)
2304 continue;
2305
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002306 /* Turn accept-encoding value to lower case */
2307 ist2bin_lc(istptr(ctx.value), ctx.value);
Tim Duesterhus23b29452020-11-24 22:22:56 +01002308
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002309 /* Try to identify a known encoding and to manage null weights. */
2310 if (!parse_encoding_value(ctx.value, &encoding_value, &rejected_encoding)) {
2311 if (rejected_encoding)
2312 encoding_bmp_bl &= ~encoding_value;
2313 else
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002314 encoding_bitmap |= encoding_value;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002315 }
2316 else {
2317 /* Unknown encoding */
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002318 encoding_bitmap |= VARY_ENCODING_OTHER;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002319 }
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002320 }
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002321
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002322 /* If a "*" was found in the accepted encodings (without a null weight),
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05002323 * all the encoding are accepted except the ones explicitly rejected. */
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002324 if (encoding_bitmap & VARY_ENCODING_STAR) {
2325 encoding_bitmap = ~0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002326 }
2327
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05002328 /* Clear explicitly rejected encodings from the bitmap */
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002329 encoding_bitmap &= encoding_bmp_bl;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002330
2331 /* As per RFC7231#5.3.4, "If no Accept-Encoding field is in the request,
2332 * any content-coding is considered acceptable by the user agent". */
2333 if (count == 0)
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002334 encoding_bitmap = ~0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002335
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002336 /* A request with more than ACCEPT_ENCODING_MAX_ENTRIES accepted
2337 * encodings might be illegitimate so we will not use it. */
2338 if (count == ACCEPT_ENCODING_MAX_ENTRIES)
2339 return -1;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002340
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002341 write_u32(buf, encoding_bitmap);
2342 *buf_len = sizeof(encoding_bitmap);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002343
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002344 /* This function fills the hash buffer correctly even if no header was
2345 * found, hence the 0 return value (success). */
Tim Duesterhus23b29452020-11-24 22:22:56 +01002346 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002347}
Tim Duesterhus23b29452020-11-24 22:22:56 +01002348#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002349
2350/*
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002351 * Normalizer used by default for the Referer header. It only
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002352 * calculates a simple crc of the whole value.
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002353 * Only the first occurrence of the header will be taken into account in the
2354 * hash.
2355 * Returns 0 in case of success, 1 if the hash buffer should be filled with 0s
2356 * and -1 in case of error.
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002357 */
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002358static int default_normalizer(struct htx *htx, struct ist hdr_name,
2359 char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002360{
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002361 int retval = 1;
2362 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002363
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002364 if (http_find_header(htx, hdr_name, &ctx, 1)) {
2365 retval = 0;
2366 write_u32(buf, hash_crc32(istptr(ctx.value), istlen(ctx.value)));
2367 *buf_len = sizeof(int);
2368 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002369
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002370 return retval;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002371}
2372
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002373/*
Tim Duesterhused84d842021-01-18 13:41:17 +01002374 * Accept-Encoding bitmap comparison function.
2375 * Returns 0 if the bitmaps are compatible.
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002376 */
Tim Duesterhused84d842021-01-18 13:41:17 +01002377static int accept_encoding_bitmap_cmp(const void *ref, const void *new, unsigned int len)
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002378{
Tim Duesterhused84d842021-01-18 13:41:17 +01002379 uint32_t ref_bitmap = read_u32(ref);
2380 uint32_t new_bitmap = read_u32(new);
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002381
Tim Duesterhused84d842021-01-18 13:41:17 +01002382 if (!(ref_bitmap & VARY_ENCODING_OTHER)) {
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002383 /* All the bits set in the reference bitmap correspond to the
2384 * stored response' encoding and should all be set in the new
2385 * encoding bitmap in order for the client to be able to manage
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002386 * the response.
2387 *
2388 * If this is the case the cached response has encodings that
2389 * are accepted by the client. It can be served directly by
2390 * the cache (as far as the accept-encoding part is concerned).
2391 */
2392
Tim Duesterhused84d842021-01-18 13:41:17 +01002393 return (ref_bitmap & new_bitmap) != ref_bitmap;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002394 }
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002395 else {
Tim Duesterhus1d66e392021-01-18 13:41:16 +01002396 return 1;
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002397 }
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002398}
2399
2400
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002401/*
2402 * Pre-calculate the hashes of all the supported headers (in our Vary
2403 * implementation) of a given request. We have to calculate all the hashes
2404 * in advance because the actual Vary signature won't be known until the first
2405 * response.
2406 * Only the first occurrence of every header will be taken into account in the
2407 * hash.
2408 * If the header is not present, the hash portion of the given header will be
2409 * filled with zeros.
2410 * Returns 0 in case of success.
2411 */
2412static int http_request_prebuild_full_secondary_key(struct stream *s)
2413{
Remi Tricot-Le Bretonbba29122020-12-23 18:13:44 +01002414 /* The fake signature (second parameter) will ensure that every part of the
2415 * secondary key is calculated. */
2416 return http_request_build_secondary_key(s, ~0);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002417}
2418
2419
2420/*
2421 * Calculate the secondary key for a request for which we already have a known
2422 * vary signature. The key is made by aggregating hashes calculated for every
2423 * header mentioned in the vary signature.
2424 * Only the first occurrence of every header will be taken into account in the
2425 * hash.
2426 * If the header is not present, the hash portion of the given header will be
2427 * filled with zeros.
2428 * Returns 0 in case of success.
2429 */
2430static int http_request_build_secondary_key(struct stream *s, int vary_signature)
2431{
2432 struct http_txn *txn = s->txn;
2433 struct htx *htx = htxbuf(&s->req.buf);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002434
2435 unsigned int idx;
2436 const struct vary_hashing_information *info = NULL;
2437 unsigned int hash_length = 0;
2438 int retval = 0;
2439 int offset = 0;
2440
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002441 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && retval >= 0; ++idx) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002442 info = &vary_information[idx];
2443
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002444 /* The normalizing functions will be in charge of getting the
2445 * header values from the htx. This way they can manage multiple
2446 * occurrences of their processed header. */
2447 if ((vary_signature & info->value) && info->norm_fn != NULL &&
2448 !(retval = info->norm_fn(htx, info->hdr_name, &txn->cache_secondary_hash[offset], &hash_length))) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002449 offset += hash_length;
2450 }
2451 else {
2452 /* Fill hash with 0s. */
2453 hash_length = info->hash_length;
2454 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
2455 offset += hash_length;
2456 }
2457 }
2458
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01002459 if (retval >= 0)
2460 txn->flags |= TX_CACHE_HAS_SEC_KEY;
2461
2462 return (retval < 0);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002463}
2464
2465/*
2466 * Build the actual secondary key of a given request out of the prebuilt key and
2467 * the actual vary signature (extracted from the response).
2468 * Returns 0 in case of success.
2469 */
2470static int http_request_reduce_secondary_key(unsigned int vary_signature,
2471 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
2472{
2473 int offset = 0;
2474 int global_offset = 0;
2475 int vary_info_count = 0;
2476 int keep = 0;
2477 unsigned int vary_idx;
2478 const struct vary_hashing_information *vary_info;
2479
2480 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
2481 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
2482 vary_info = &vary_information[vary_idx];
2483 keep = (vary_signature & vary_info->value) ? 0xff : 0;
2484
2485 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
2486 prebuilt_key[global_offset] &= keep;
2487 }
2488 }
2489
2490 return 0;
2491}
2492
2493
Christopher Faulet99a17a22018-12-11 09:18:27 +01002494
2495static int
2496parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
2497 struct flt_conf *fconf, char **err, void *private)
2498{
2499 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01002500 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002501 char *name = NULL;
2502 int pos = *cur_arg;
2503
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002504 /* Get the cache filter name. <pos> point on "cache" keyword */
2505 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02002506 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002507 goto error;
2508 }
2509 name = strdup(args[pos + 1]);
2510 if (!name) {
2511 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
2512 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002513 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002514 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002515
2516 /* Check if an implicit filter with the same name already exists. If so,
2517 * we remove the implicit filter to use the explicit one. */
2518 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
2519 if (f->id != cache_store_flt_id)
2520 continue;
2521
2522 cconf = f->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002523 if (strcmp(name, cconf->c.name) != 0) {
Christopher Faulet99a17a22018-12-11 09:18:27 +01002524 cconf = NULL;
2525 continue;
2526 }
2527
2528 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2529 cconf = NULL;
2530 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2531 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002532 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002533 }
2534
2535 /* Remove the implicit filter. <cconf> is kept for the explicit one */
Willy Tarreau2b718102021-04-21 07:32:39 +02002536 LIST_DELETE(&f->list);
Christopher Faulet99a17a22018-12-11 09:18:27 +01002537 free(f);
2538 free(name);
2539 break;
2540 }
2541
2542 /* No implicit cache filter found, create configuration for the explicit one */
2543 if (!cconf) {
2544 cconf = calloc(1, sizeof(*cconf));
2545 if (!cconf) {
2546 memprintf(err, "%s: out of memory", args[*cur_arg]);
2547 goto error;
2548 }
2549 cconf->c.name = name;
2550 }
2551
2552 cconf->flags = 0;
2553 fconf->id = cache_store_flt_id;
2554 fconf->conf = cconf;
2555 fconf->ops = &cache_ops;
2556
2557 *cur_arg = pos;
2558 return 0;
2559
2560 error:
2561 free(name);
2562 free(cconf);
2563 return -1;
2564}
2565
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002566/* It reserves a struct show_cache_ctx for the local variables */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002567static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002568{
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002569 struct show_cache_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
2570
William Lallemand1f49a362017-11-21 20:01:26 +01002571 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2572 return 1;
2573
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002574 ctx->cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
William Lallemand1f49a362017-11-21 20:01:26 +01002575 return 0;
2576}
2577
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002578/* It uses a struct show_cache_ctx for the local variables */
William Lallemand1f49a362017-11-21 20:01:26 +01002579static int cli_io_handler_show_cache(struct appctx *appctx)
2580{
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002581 struct show_cache_ctx *ctx = appctx->svcctx;
2582 struct cache* cache = ctx->cache;
Christopher Faulet908628c2022-03-25 16:43:49 +01002583 struct conn_stream *cs = appctx->owner;
William Lallemand1f49a362017-11-21 20:01:26 +01002584
William Lallemand1f49a362017-11-21 20:01:26 +01002585 list_for_each_entry_from(cache, &caches, list) {
2586 struct eb32_node *node = NULL;
2587 unsigned int next_key;
2588 struct cache_entry *entry;
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002589 unsigned int i;
William Lallemand1f49a362017-11-21 20:01:26 +01002590
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002591 next_key = ctx->next_key;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002592 if (!next_key) {
2593 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
Christopher Faulet908628c2022-03-25 16:43:49 +01002594 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02002595 cs_rx_room_blk(cs);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002596 return 0;
2597 }
2598 }
William Lallemand1f49a362017-11-21 20:01:26 +01002599
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002600 ctx->cache = cache;
William Lallemand1f49a362017-11-21 20:01:26 +01002601
2602 while (1) {
2603
2604 shctx_lock(shctx_ptr(cache));
Christopher Faulet27f88a92021-11-23 16:03:05 +01002605 node = eb32_lookup_ge(&cache->entries, next_key);
William Lallemand1f49a362017-11-21 20:01:26 +01002606 if (!node) {
2607 shctx_unlock(shctx_ptr(cache));
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002608 ctx->next_key = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002609 break;
2610 }
2611
2612 entry = container_of(node, struct cache_entry, eb);
William Lallemand1f49a362017-11-21 20:01:26 +01002613 next_key = node->key + 1;
Willy Tarreauf1de1b52022-04-13 11:21:39 +02002614
2615 if (entry->expire > now.tv_sec) {
2616 chunk_printf(&trash, "%p hash:%u vary:0x", entry, read_u32(entry->hash));
2617 for (i = 0; i < HTTP_CACHE_SEC_KEY_LEN; ++i)
2618 chunk_appendf(&trash, "%02x", (unsigned char)entry->secondary_key[i]);
2619 chunk_appendf(&trash, " size:%u (%u blocks), refcount:%u, expire:%d\n",
2620 block_ptr(entry)->len, block_ptr(entry)->block_count,
2621 block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec);
2622 } else {
2623 /* time to remove that one */
2624 delete_entry(entry);
2625 entry->eb.key = 0;
2626 }
2627
Willy Tarreauc6dfef72022-05-05 16:46:13 +02002628 ctx->next_key = next_key;
William Lallemand1f49a362017-11-21 20:01:26 +01002629
2630 shctx_unlock(shctx_ptr(cache));
2631
Christopher Faulet908628c2022-03-25 16:43:49 +01002632 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02002633 cs_rx_room_blk(cs);
William Lallemand1f49a362017-11-21 20:01:26 +01002634 return 0;
2635 }
2636 }
2637
2638 }
2639
2640 return 1;
2641
2642}
2643
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002644
2645/*
2646 * boolean, returns true if response was built out of a cache entry.
2647 */
2648static int
2649smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2650 const char *kw, void *private)
2651{
2652 smp->data.type = SMP_T_BOOL;
2653 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2654
2655 return 1;
2656}
2657
2658/*
2659 * string, returns cache name (if response came from a cache).
2660 */
2661static int
2662smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2663 const char *kw, void *private)
2664{
2665 struct appctx *appctx = NULL;
2666
2667 struct cache_flt_conf *cconf = NULL;
2668 struct cache *cache = NULL;
2669
2670 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2671 return 0;
2672
Christopher Faulet908628c2022-03-25 16:43:49 +01002673 /* Get appctx from the conn-stream. */
Christopher Faulet95a61e82021-12-22 14:22:03 +01002674 appctx = cs_appctx(smp->strm->csb);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002675 if (appctx && appctx->rule) {
2676 cconf = appctx->rule->arg.act.p[0];
2677 if (cconf) {
2678 cache = cconf->c.cache;
2679
2680 smp->data.type = SMP_T_STR;
2681 smp->flags = SMP_F_CONST;
2682 smp->data.u.str.area = cache->id;
2683 smp->data.u.str.data = strlen(cache->id);
2684 return 1;
2685 }
2686 }
2687
2688 return 0;
2689}
2690
Christopher Faulet99a17a22018-12-11 09:18:27 +01002691/* Declare the filter parser for "cache" keyword */
2692static struct flt_kw_list filter_kws = { "CACHE", { }, {
2693 { "cache", parse_cache_flt, NULL },
2694 { NULL, NULL, NULL },
2695 }
2696};
2697
2698INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2699
William Lallemand1f49a362017-11-21 20:01:26 +01002700static struct cli_kw_list cli_kws = {{},{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02002701 { { "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 +01002702 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002703}};
2704
Willy Tarreau0108d902018-11-25 19:14:37 +01002705INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002706
William Lallemand41db4602017-10-30 11:15:51 +01002707static struct action_kw_list http_res_actions = {
2708 .kw = {
2709 { "cache-store", parse_cache_store },
2710 { NULL, NULL }
2711 }
2712};
2713
Willy Tarreau0108d902018-11-25 19:14:37 +01002714INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2715
William Lallemand41db4602017-10-30 11:15:51 +01002716static struct action_kw_list http_req_actions = {
2717 .kw = {
2718 { "cache-use", parse_cache_use },
2719 { NULL, NULL }
2720 }
2721};
2722
Willy Tarreau0108d902018-11-25 19:14:37 +01002723INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2724
Willy Tarreau2231b632019-03-29 18:26:52 +01002725struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002726 .obj_type = OBJ_TYPE_APPLET,
2727 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002728 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002729 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002730};
2731
Willy Tarreaue6552512018-11-26 11:33:13 +01002732/* config parsers for this section */
2733REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002734REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002735
2736
2737/* Note: must not be declared <const> as its list will be overwritten */
2738static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2739 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2740 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2741 { /* END */ },
2742 }
2743};
2744
2745INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);