blob: 32c99cdc4aaedb08f301de0a10839acb9bf64806 [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
2 * Cache management
3 *
4 * Copyright 2017 HAProxy Technologies
5 * William Lallemand <wlallemand@haproxy.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
Willy Tarreaub2551052020-06-09 09:07:15 +020013#include <import/eb32tree.h>
14#include <import/sha1.h>
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020018#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020020#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020021#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020022#include <haproxy/filters.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/hash.h>
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020027#include <haproxy/http_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/htx.h>
29#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +010031#include <haproxy/sample.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020032#include <haproxy/shctx.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
William Lallemand41db4602017-10-30 11:15:51 +010035
Christopher Faulet27d93c32018-12-15 22:32:02 +010036#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010037 * the filter keyword) */
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +020038#define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */
Christopher Fauletafd819c2018-12-11 08:57:45 +010039
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010040const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010041
Willy Tarreau2231b632019-03-29 18:26:52 +010042extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010043
44struct flt_ops cache_ops;
45
46struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010047 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010048 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010049 unsigned int maxage; /* max-age */
50 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020051 unsigned int maxobjsz; /* max-object-size (in bytes) */
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +010052 unsigned int max_secondary_entries; /* maximum number of secondary entries with the same primary hash */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +010053 uint8_t vary_processing_enabled; /* boolean : manage Vary header (disabled by default) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010054 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010055};
56
Christopher Faulet95220e22018-12-07 17:34:39 +010057/* cache config for filters */
58struct cache_flt_conf {
59 union {
60 struct cache *cache; /* cache used by the filter */
61 char *name; /* cache name used during conf parsing */
62 } c;
63 unsigned int flags; /* CACHE_FLT_F_* */
64};
65
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +010066
67/*
68 * Vary-related structures and functions
69 */
70enum vary_header_bit {
71 VARY_ACCEPT_ENCODING = (1 << 0),
72 VARY_REFERER = (1 << 1),
73 VARY_LAST /* should always be last */
74};
75
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +010076/*
77 * Encoding list extracted from
78 * https://www.iana.org/assignments/http-parameters/http-parameters.xhtml
79 * and RFC7231#5.3.4.
80 */
81enum vary_encoding {
82 VARY_ENCODING_GZIP = (1 << 0),
83 VARY_ENCODING_DEFLATE = (1 << 1),
84 VARY_ENCODING_BR = (1 << 2),
85 VARY_ENCODING_COMPRESS = (1 << 3),
86 VARY_ENCODING_AES128GCM = (1 << 4),
87 VARY_ENCODING_EXI = (1 << 5),
88 VARY_ENCODING_PACK200_GZIP = (1 << 6),
89 VARY_ENCODING_ZSTD = (1 << 7),
90 VARY_ENCODING_IDENTITY = (1 << 8),
91 VARY_ENCODING_STAR = (1 << 9),
92 VARY_ENCODING_OTHER = (1 << 10)
93};
94
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +010095struct vary_hashing_information {
96 struct ist hdr_name; /* Header name */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050097 enum vary_header_bit value; /* Bit representing the header in a vary signature */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +010098 unsigned int hash_length; /* Size of the sub hash for this header's value */
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +010099 int(*norm_fn)(struct htx*,struct ist hdr_name,char* buf,unsigned int* buf_len); /* Normalization function */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100100 int(*cmp_fn)(const void *ref_hash, const void *new_hash, unsigned int hash_len); /* Comparison function, should return 0 if the hashes are alike */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100101};
102
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100103struct accept_encoding_hash {
104 unsigned int encoding_bitmap;
105 unsigned int hash;
106} __attribute__((packed));
107
108static int http_request_prebuild_full_secondary_key(struct stream *s);
109static int http_request_build_secondary_key(struct stream *s, int vary_signature);
110static int http_request_reduce_secondary_key(unsigned int vary_signature,
111 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]);
112
113static int parse_encoding_value(struct ist value, unsigned int *encoding_value,
114 unsigned int *has_null_weight);
115
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +0100116static int accept_encoding_normalizer(struct htx *htx, struct ist hdr_name,
117 char *buf, unsigned int *buf_len);
118static int default_normalizer(struct htx *htx, struct ist hdr_name,
119 char *buf, unsigned int *buf_len);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100120
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100121static int accept_encoding_hash_cmp(const void *ref_hash, const void *new_hash, unsigned int hash_len);
122
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100123/* Warning : do not forget to update HTTP_CACHE_SEC_KEY_LEN when new items are
124 * added to this array. */
125const struct vary_hashing_information vary_information[] = {
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100126 { IST("accept-encoding"), VARY_ACCEPT_ENCODING, sizeof(struct accept_encoding_hash), &accept_encoding_normalizer, &accept_encoding_hash_cmp },
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100127 { IST("referer"), VARY_REFERER, sizeof(int), &default_normalizer, NULL },
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100128};
129
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100130
William Lallemand41db4602017-10-30 11:15:51 +0100131/*
132 * cache ctx for filters
133 */
134struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +0100135 struct shared_block *first_block;
136};
137
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100138#define DEFAULT_MAX_SECONDARY_ENTRY 10
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100139
William Lallemand41db4602017-10-30 11:15:51 +0100140struct cache_entry {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100141 unsigned int complete; /* An entry won't be valid until complete is not null. */
William Lallemand41db4602017-10-30 11:15:51 +0100142 unsigned int latest_validation; /* latest validation date */
143 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200144 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100145
William Lallemand41db4602017-10-30 11:15:51 +0100146 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +0100147 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200148
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100149 char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */
150 unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used
151 * to build secondary keys for this cache entry. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100152 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 +0100153 unsigned int last_clear_ts; /* Timestamp of the last call to clear_expired_duplicates. */
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100154
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200155 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
156 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
157
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200158 time_t last_modified; /* Origin server "Last-Modified" header value converted in
159 * seconds since epoch. If no "Last-Modified"
160 * header is found, use "Date" header value,
161 * otherwise use reception time. This field will
162 * be used in case of an "If-Modified-Since"-based
163 * conditional request. */
164
William Lallemand41db4602017-10-30 11:15:51 +0100165 unsigned char data[0];
166};
167
168#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +0100169#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100170
171static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +0200172static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +0100173static struct cache *tmp_cache_config = NULL;
174
Willy Tarreau8ceae722018-11-26 11:58:30 +0100175DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
176
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100177static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry);
178static void delete_entry(struct cache_entry *del_entry);
179
William Lallemandf528fff2017-11-23 19:43:17 +0100180struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100181{
182 struct eb32_node *node;
183 struct cache_entry *entry;
184
Willy Tarreau8b507582020-02-25 09:35:07 +0100185 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100186 if (!node)
187 return NULL;
188
189 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100190
191 /* if that's not the right node */
192 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
193 return NULL;
194
William Lallemand08727662017-11-21 20:01:27 +0100195 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100196 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100197 } else {
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100198 delete_entry(entry);
William Lallemand08727662017-11-21 20:01:27 +0100199 entry->eb.key = 0;
200 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100201 return NULL;
202
203}
204
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100205
206/*
207 * Compare a newly built secondary key to the one found in a cache_entry.
208 * Every sub-part of the key is compared to the reference through the dedicated
209 * comparison function of the sub-part (that might do more than a simple
210 * memcmp).
211 * Returns 0 if the keys are alike.
212 */
213static int secondary_key_cmp(const char *ref_key, const char *new_key)
214{
215 int retval = 0;
216 int idx = 0;
217 int offset = 0;
218 const struct vary_hashing_information *info;
219
220 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
221 info = &vary_information[idx];
222
223 if (info->cmp_fn)
224 retval = info->cmp_fn(&ref_key[offset], &new_key[offset], info->hash_length);
225 else
226 retval = memcmp(&ref_key[offset], &new_key[offset], info->hash_length);
227
228 offset += info->hash_length;
229 }
230
231 return retval;
232}
233
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100234/*
235 * There can be multiple entries with the same primary key in the ebtree so in
236 * order to get the proper one out of the list, we use a secondary_key.
237 * This function simply iterates over all the entries with the same primary_key
238 * until it finds the right one.
239 * Returns the cache_entry in case of success, NULL otherwise.
240 */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100241struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry,
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100242 const char *secondary_key)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100243{
244 struct eb32_node *node = &entry->eb;
245
246 if (!entry->secondary_key_signature)
247 return NULL;
248
Remi Tricot-Le Breton6a34b2b2020-12-23 18:13:47 +0100249 while (entry && secondary_key_cmp(entry->secondary_key, secondary_key) != 0) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100250 node = eb32_next_dup(node);
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100251
252 /* Make the best use of this iteration and clear expired entries
253 * when we find them. Calling delete_entry would be too costly
254 * so we simply call eb32_delete. The secondary_entry count will
255 * be updated when we try to insert a new entry to this list. */
256 if (entry->expire <= now.tv_sec) {
257 eb32_delete(&entry->eb);
258 entry->eb.key = 0;
259 }
260
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100261 entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL;
262 }
263
264 /* Expired entry */
265 if (entry && entry->expire <= now.tv_sec) {
266 eb32_delete(&entry->eb);
267 entry->eb.key = 0;
268 entry = NULL;
269 }
270
271 return entry;
272}
273
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100274
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100275/*
276 * Remove all expired entries from a list of duplicates.
277 * Return the number of alive entries in the list and sets dup_tail to the
278 * current last item of the list.
279 */
280static unsigned int clear_expired_duplicates(struct eb32_node **dup_tail)
281{
282 unsigned int entry_count = 0;
283 struct cache_entry *entry = NULL;
284 struct eb32_node *prev = *dup_tail;
285 struct eb32_node *tail = NULL;
286
287 while (prev) {
288 entry = container_of(prev, struct cache_entry, eb);
289 prev = eb32_prev_dup(prev);
290 if (entry->expire <= now.tv_sec) {
291 eb32_delete(&entry->eb);
292 entry->eb.key = 0;
293 }
294 else {
295 if (!tail)
296 tail = &entry->eb;
297 ++entry_count;
298 }
299 }
300
301 *dup_tail = tail;
302
303 return entry_count;
304}
305
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100306
307/*
308 * This function inserts a cache_entry in the cache's ebtree. In case of
309 * duplicate entries (vary), it then checks that the number of entries did not
310 * reach the max number of secondary entries. If this entry should not have been
311 * created, remove it.
312 * In the regular case (unique entries), this function does not do more than a
313 * simple insert. In case of secondary entries, it will at most cost an
314 * insertion+max_sec_entries time checks and entry deletion.
315 * Returns the newly inserted node in case of success, NULL otherwise.
316 */
317static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry)
318{
319 struct eb32_node *prev = NULL;
320 struct cache_entry *entry = NULL;
321 unsigned int entry_count = 0;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100322 unsigned int last_clear_ts = now.tv_sec;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100323
324 struct eb32_node *node = eb32_insert(&cache->entries, &new_entry->eb);
325
326 /* We should not have multiple entries with the same primary key unless
327 * the entry has a non null vary signature. */
328 if (!new_entry->secondary_key_signature)
329 return node;
330
331 prev = eb32_prev_dup(node);
332 if (prev != NULL) {
333 /* The last entry of a duplicate list should contain the current
334 * number of entries in the list. */
335 entry = container_of(prev, struct cache_entry, eb);
336 entry_count = entry->secondary_entries_count;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100337 last_clear_ts = entry->last_clear_ts;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100338
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100339 if (entry_count >= cache->max_secondary_entries) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100340 /* Some entries of the duplicate list might be expired so
341 * we will iterate over all the items in order to free some
342 * space. In order to avoid going over the same list too
343 * often, we first check the timestamp of the last check
344 * performed. */
345 if (last_clear_ts == now.tv_sec) {
346 /* Too many entries for this primary key, clear the
347 * one that was inserted. */
348 eb32_delete(node);
349 node->key = 0;
350 return NULL;
351 }
352
353 entry_count = clear_expired_duplicates(&prev);
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100354 if (entry_count >= cache->max_secondary_entries) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100355 /* Still too many entries for this primary key, delete
356 * the newly inserted one. */
357 entry = container_of(prev, struct cache_entry, eb);
358 entry->last_clear_ts = now.tv_sec;
359 eb32_delete(node);
360 node->key = 0;
361 return NULL;
362 }
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100363 }
364 }
365
366 new_entry->secondary_entries_count = entry_count + 1;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100367 new_entry->last_clear_ts = last_clear_ts;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100368
369 return node;
370}
371
372
373/*
374 * This function removes an entry from the ebtree. If the entry was a duplicate
375 * (in case of Vary), it updates the secondary entry counter in another
376 * duplicate entry (the last entry of the dup list).
377 */
378static void delete_entry(struct cache_entry *del_entry)
379{
380 struct eb32_node *prev = NULL, *next = NULL;
381 struct cache_entry *entry = NULL;
382 struct eb32_node *last = NULL;
383
384 if (del_entry->secondary_key_signature) {
385 next = &del_entry->eb;
386
387 /* Look for last entry of the duplicates list. */
388 while ((next = eb32_next_dup(next))) {
389 last = next;
390 }
391
392 if (last) {
393 entry = container_of(last, struct cache_entry, eb);
394 --entry->secondary_entries_count;
395 }
396 else {
397 /* The current entry is the last one, look for the
398 * previous one to update its counter. */
399 prev = eb32_prev_dup(&del_entry->eb);
400 if (prev) {
401 entry = container_of(prev, struct cache_entry, eb);
402 entry->secondary_entries_count = del_entry->secondary_entries_count - 1;
403 }
404 }
405 }
406 eb32_delete(&del_entry->eb);
407 del_entry->eb.key = 0;
408}
409
410
William Lallemand4da3f8a2017-10-31 14:33:34 +0100411static inline struct shared_context *shctx_ptr(struct cache *cache)
412{
413 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
414}
415
William Lallemand77c11972017-10-31 20:43:01 +0100416static inline struct shared_block *block_ptr(struct cache_entry *entry)
417{
418 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
419}
420
421
422
William Lallemand41db4602017-10-30 11:15:51 +0100423static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100424cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100425{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100426 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100427 return 0;
428}
429
Christopher Faulet95220e22018-12-07 17:34:39 +0100430static void
431cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
432{
433 struct cache_flt_conf *cconf = fconf->conf;
434
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200435 if (!(cconf->flags & CACHE_FLT_INIT))
436 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100437 free(cconf);
438}
439
William Lallemand4da3f8a2017-10-31 14:33:34 +0100440static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100441cache_store_check(struct proxy *px, struct flt_conf *fconf)
442{
443 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100444 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100445 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100446 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100447
William Lallemandd1d1e222019-08-28 15:22:49 +0200448 /* Find the cache corresponding to the name in the filter config. The
449 * cache will not be referenced now in the filter config because it is
450 * not fully allocated. This step will be performed during the cache
451 * post_check.
452 */
453 list_for_each_entry(cache, &caches_config, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100454 if (strcmp(cache->id, cconf->c.name) == 0)
Christopher Faulet95220e22018-12-07 17:34:39 +0100455 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100456 }
457
458 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
459 proxy_type_str(px), px->id, (char *)cconf->c.name);
460 return 1;
461
462 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100463 /* Here <cache> points on the cache the filter must use and <cconf>
464 * points on the cache filter configuration. */
465
466 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100467 * enabled and if it is after the cache. When the compression is before
468 * the cache, an error is returned. Also check if the cache filter must
469 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100470 list_for_each_entry(f, &px->filter_configs, list) {
471 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100472 /* The compression filter must be evaluated after the cache. */
473 if (comp) {
474 ha_alert("config: %s '%s': unable to enable the compression filter before "
475 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
476 return 1;
477 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100478 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200479 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100480 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200481 else if (f->id == fcgi_flt_id)
482 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100483 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
484 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200485 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100486 * declaration is required. */
487 ha_alert("config: %s '%s': require an explicit filter declaration "
488 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
489 return 1;
490 }
491
Christopher Fauletafd819c2018-12-11 08:57:45 +0100492 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100493 return 0;
494}
495
496static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100497cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100498{
Christopher Faulet65554e12020-03-06 14:52:06 +0100499 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100500
Christopher Faulet65554e12020-03-06 14:52:06 +0100501 st = pool_alloc_dirty(pool_head_cache_st);
502 if (st == NULL)
503 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100504
Christopher Faulet65554e12020-03-06 14:52:06 +0100505 st->first_block = NULL;
506 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100507
Christopher Faulet65554e12020-03-06 14:52:06 +0100508 /* Register post-analyzer on AN_RES_WAIT_HTTP */
509 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100510 return 1;
511}
512
Christopher Faulet65554e12020-03-06 14:52:06 +0100513static void
514cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100515{
516 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100517 struct cache_flt_conf *cconf = FLT_CONF(filter);
518 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100519 struct shared_context *shctx = shctx_ptr(cache);
520
William Lallemand49dc0482017-11-24 14:33:54 +0100521 /* Everything should be released in the http_end filter, but we need to do it
522 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100523 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100524 shctx_lock(shctx);
525 shctx_row_dec_hot(shctx, st->first_block);
526 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100527 }
528 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100529 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100530 filter->ctx = NULL;
531 }
William Lallemand49dc0482017-11-24 14:33:54 +0100532}
533
Christopher Faulet839791a2019-01-07 16:12:07 +0100534static int
535cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
536 unsigned an_bit)
537{
538 struct http_txn *txn = s->txn;
539 struct http_msg *msg = &txn->rsp;
540 struct cache_st *st = filter->ctx;
541
542 if (an_bit != AN_RES_WAIT_HTTP)
543 goto end;
544
545 /* Here we need to check if any compression filter precedes the cache
546 * filter. This is only possible when the compression is configured in
547 * the frontend while the cache filter is configured on the
548 * backend. This case cannot be detected during HAProxy startup. So in
549 * such cases, the cache is disabled.
550 */
551 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
552 pool_free(pool_head_cache_st, st);
553 filter->ctx = NULL;
554 }
555
556 end:
557 return 1;
558}
William Lallemand49dc0482017-11-24 14:33:54 +0100559
560static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100561cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
562{
563 struct cache_st *st = filter->ctx;
564
William Lallemand4da3f8a2017-10-31 14:33:34 +0100565 if (!(msg->chn->flags & CF_ISRESP) || !st)
566 return 1;
567
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200568 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100569 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100570 return 1;
571}
572
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200573static inline void disable_cache_entry(struct cache_st *st,
574 struct filter *filter, struct shared_context *shctx)
575{
576 struct cache_entry *object;
577
578 object = (struct cache_entry *)st->first_block->data;
579 filter->ctx = NULL; /* disable cache */
580 shctx_lock(shctx);
581 shctx_row_dec_hot(shctx, st->first_block);
Remi Tricot-Le Breton964caaf2020-12-15 14:30:12 +0100582 eb32_delete(&object->eb);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200583 object->eb.key = 0;
584 shctx_unlock(shctx);
585 pool_free(pool_head_cache_st, st);
586}
587
William Lallemand4da3f8a2017-10-31 14:33:34 +0100588static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100589cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
590 unsigned int offset, unsigned int len)
591{
Christopher Faulet95220e22018-12-07 17:34:39 +0100592 struct cache_flt_conf *cconf = FLT_CONF(filter);
593 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100594 struct cache_st *st = filter->ctx;
595 struct htx *htx = htxbuf(&msg->chn->buf);
596 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200597 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100598 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200599 unsigned int orig_len, to_forward;
600 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100601
602 if (!len)
603 return len;
604
605 if (!st->first_block) {
606 unregister_data_filter(s, msg->chn, filter);
607 return len;
608 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100609
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200610 chunk_reset(&trash);
611 orig_len = len;
612 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100613
614 htxret = htx_find_offset(htx, offset);
615 blk = htxret.blk;
616 offset = htxret.ret;
617 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100618 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200619 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100620 struct ist v;
621
622 switch (type) {
623 case HTX_BLK_UNUSED:
624 break;
625
626 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100627 v = htx_get_blk_value(htx, blk);
628 v.ptr += offset;
629 v.len -= offset;
630 if (v.len > len)
631 v.len = len;
632
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200633 info = (type << 28) + v.len;
634 chunk_memcat(&trash, (char *)&info, sizeof(info));
635 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100636 to_forward += v.len;
637 len -= v.len;
638 break;
639
640 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200641 /* Here offset must always be 0 because only
642 * DATA blocks can be partially transferred. */
643 if (offset)
644 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100645 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200646 goto end;
647
648 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
649 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100650 to_forward += sz;
651 len -= sz;
652 break;
653 }
654
655 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100656 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200657
658 end:
659 shctx_lock(shctx);
660 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
661 if (!fb) {
662 shctx_unlock(shctx);
663 goto no_cache;
664 }
665 shctx_unlock(shctx);
666
667 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
668 (unsigned char *)b_head(&trash), b_data(&trash));
669 if (ret < 0)
670 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100671
672 return to_forward;
673
674 no_cache:
675 disable_cache_entry(st, filter, shctx);
676 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200677 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100678}
679
680static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100681cache_store_http_end(struct stream *s, struct filter *filter,
682 struct http_msg *msg)
683{
684 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100685 struct cache_flt_conf *cconf = FLT_CONF(filter);
686 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100687 struct shared_context *shctx = shctx_ptr(cache);
688 struct cache_entry *object;
689
690 if (!(msg->chn->flags & CF_ISRESP))
691 return 1;
692
693 if (st && st->first_block) {
694
695 object = (struct cache_entry *)st->first_block->data;
696
William Lallemand4da3f8a2017-10-31 14:33:34 +0100697 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100698 /* The whole payload was cached, the entry can now be used. */
699 object->complete = 1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100700 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100701 shctx_row_dec_hot(shctx, st->first_block);
702 shctx_unlock(shctx);
703
704 }
705 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100706 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100707 filter->ctx = NULL;
708 }
709
710 return 1;
711}
712
713 /*
714 * This intends to be used when checking HTTP headers for some
715 * word=value directive. Return a pointer to the first character of value, if
Willy Tarreau94a01e12021-01-06 17:35:12 +0100716 * the word was not found or if there wasn't any value assigned to it return NULL
William Lallemand4da3f8a2017-10-31 14:33:34 +0100717 */
718char *directive_value(const char *sample, int slen, const char *word, int wlen)
719{
720 int st = 0;
721
722 if (slen < wlen)
723 return 0;
724
725 while (wlen) {
726 char c = *sample ^ *word;
727 if (c && c != ('A' ^ 'a'))
728 return NULL;
729 sample++;
730 word++;
731 slen--;
732 wlen--;
733 }
734
735 while (slen) {
736 if (st == 0) {
737 if (*sample != '=')
738 return NULL;
739 sample++;
740 slen--;
741 st = 1;
742 continue;
743 } else {
744 return (char *)sample;
745 }
746 }
747
748 return NULL;
749}
750
751/*
752 * Return the maxage in seconds of an HTTP response.
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100753 * The returned value will always take the cache's configuration into account
754 * (cache->maxage) but the actual max age of the response will be set in the
755 * true_maxage parameter. It will be used to determine if a response is already
756 * stale or not.
William Lallemand4da3f8a2017-10-31 14:33:34 +0100757 * Compute the maxage using either:
758 * - the assigned max-age of the cache
759 * - the s-maxage directive
760 * - the max-age directive
761 * - (Expires - Data) headers
762 * - the default-max-age of the cache
763 *
764 */
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100765int http_calc_maxage(struct stream *s, struct cache *cache, int *true_maxage)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100766{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200767 struct htx *htx = htxbuf(&s->res.buf);
768 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100769 long smaxage = -1;
770 long maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100771 int expires = -1;
772 struct tm tm = {};
773 time_t expires_val = 0;
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100774 char *endptr = NULL;
775 int offset = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100776
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100777 /* The Cache-Control max-age and s-maxage directives should be followed by
778 * a positive numerical value (see RFC 7234#5.2.1.1). According to the
779 * specs, a sender "should not" generate a quoted-string value but we will
780 * still accept this format since it isn't strictly forbidden. */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200781 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
782 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100783
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200784 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
785 if (value) {
786 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100787
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200788 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
789 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100790 offset = (*chk->area == '"') ? 1 : 0;
791 smaxage = strtol(chk->area + offset, &endptr, 10);
792 if (unlikely(smaxage < 0 || endptr == chk->area))
793 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100794 }
795
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200796 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
797 if (value) {
798 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200799
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200800 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
801 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100802 offset = (*chk->area == '"') ? 1 : 0;
803 maxage = strtol(chk->area + offset, &endptr, 10);
804 if (unlikely(maxage < 0 || endptr == chk->area))
805 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100806 }
807 }
808
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100809 /* Look for Expires header if no s-maxage or max-age Cache-Control data
810 * was found. */
811 if (maxage == -1 && smaxage == -1) {
812 ctx.blk = NULL;
813 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
814 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
815 expires_val = my_timegm(&tm);
816 /* A request having an expiring date earlier
817 * than the current date should be considered as
818 * stale. */
819 expires = (expires_val >= now.tv_sec) ?
820 (expires_val - now.tv_sec) : 0;
821 }
822 else {
823 /* Following RFC 7234#5.3, an invalid date
824 * format must be treated as a date in the past
825 * so the cache entry must be seen as already
826 * expired. */
827 expires = 0;
828 }
829 }
830 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100831
832
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100833 if (smaxage > 0) {
834 if (true_maxage)
835 *true_maxage = smaxage;
William Lallemand49b44532017-11-24 18:53:43 +0100836 return MIN(smaxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100837 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100838
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100839 if (maxage > 0) {
840 if (true_maxage)
841 *true_maxage = maxage;
William Lallemand49b44532017-11-24 18:53:43 +0100842 return MIN(maxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100843 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100844
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100845 if (expires >= 0) {
846 if (true_maxage)
847 *true_maxage = expires;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100848 return MIN(expires, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100849 }
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100850
William Lallemand49b44532017-11-24 18:53:43 +0100851 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100852
853}
854
855
William Lallemanda400a3a2017-11-20 19:13:12 +0100856static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
857{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200858 struct cache_entry *object = (struct cache_entry *)block->data;
859
860 if (first == block && object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100861 delete_entry(object);
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200862 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100863}
864
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200865
866/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
867 * date value should be compared to a date determined by in a previous response (for
868 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
869 * value of the response's reception time (by decreasing order of priority). */
870static time_t get_last_modified_time(struct htx *htx)
871{
872 time_t last_modified = 0;
873 struct http_hdr_ctx ctx = { .blk = NULL };
874 struct tm tm = {};
875
876 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
877 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
878 last_modified = my_timegm(&tm);
879 }
880 }
881
882 if (!last_modified) {
883 ctx.blk = NULL;
884 if (http_find_header(htx, ist("date"), &ctx, 1)) {
885 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
886 last_modified = my_timegm(&tm);
887 }
888 }
889 }
890
891 /* Fallback on the current time if no "Last-Modified" or "Date" header
892 * was found. */
893 if (!last_modified)
894 last_modified = now.tv_sec;
895
896 return last_modified;
897}
898
William Lallemand41db4602017-10-30 11:15:51 +0100899/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100900 * Checks the vary header's value. The headers on which vary should be applied
Ilya Shipitsinf38a0182020-12-21 01:16:17 +0500901 * must be explicitly supported in the vary_information array (see cache.c). If
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100902 * any other header is mentioned, we won't store the response.
903 * Returns 1 if Vary-based storage can work, 0 otherwise.
904 */
905static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
906{
907 unsigned int vary_idx;
908 unsigned int vary_info_count;
909 const struct vary_hashing_information *vary_info;
910 struct http_hdr_ctx ctx = { .blk = NULL };
911
912 int retval = 1;
913
914 *vary_signature = 0;
915
916 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
917 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
918 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
919 vary_info = &vary_information[vary_idx];
920 if (isteqi(ctx.value, vary_info->hdr_name)) {
921 *vary_signature |= vary_info->value;
922 break;
923 }
924 }
925 retval = (vary_idx < vary_info_count);
926 }
927
928 return retval;
929}
930
931
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100932/*
933 * Look for the accept-encoding part of the secondary_key and replace the
934 * encoding bitmap part of the hash with the actual encoding of the response,
935 * extracted from the content-encoding header value.
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100936 * Responses that have an unknown encoding will not be cached if they also
937 * "vary" on the accept-encoding value.
938 * Returns 0 if we found a known encoding in the response, -1 otherwise.
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100939 */
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100940static int set_secondary_key_encoding(struct htx *htx, char *secondary_key)
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100941{
942 unsigned int resp_encoding_bitmap = 0;
943 const struct vary_hashing_information *info = vary_information;
944 unsigned int offset = 0;
945 unsigned int count = 0;
946 unsigned int hash_info_count = sizeof(vary_information)/sizeof(*vary_information);
947 unsigned int encoding_value;
948 struct http_hdr_ctx ctx = { .blk = NULL };
949
950 /* Look for the accept-encoding part of the secondary_key. */
951 while (count < hash_info_count && info->value != VARY_ACCEPT_ENCODING) {
952 offset += info->hash_length;
953 ++info;
954 ++count;
955 }
956
957 if (count == hash_info_count)
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100958 return -1;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100959
960 while (http_find_header(htx, ist("content-encoding"), &ctx, 0)) {
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100961 if (parse_encoding_value(ctx.value, &encoding_value, NULL))
962 return -1; /* Do not store responses with an unknown encoding */
963 resp_encoding_bitmap |= encoding_value;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100964 }
965
966 if (!resp_encoding_bitmap)
967 resp_encoding_bitmap |= VARY_ENCODING_IDENTITY;
968
969 /* Rewrite the bitmap part of the hash with the new bitmap that only
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500970 * corresponds the the response's encoding. */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100971 write_u32(secondary_key + offset, resp_encoding_bitmap);
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +0100972
973 return 0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +0100974}
975
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100976
977/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500978 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100979 * register a filter to store the data
980 */
981enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200982 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100983{
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100984 int effective_maxage = 0;
985 int true_maxage = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100986 struct http_txn *txn = s->txn;
987 struct http_msg *msg = &txn->rsp;
988 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100989 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100990 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100991 struct cache *cache = cconf->c.cache;
992 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100993 struct cache_st *cache_ctx = NULL;
994 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100995 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200996 struct htx *htx;
997 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200998 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200999 int32_t pos;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001000 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001001
William Lallemand4da3f8a2017-10-31 14:33:34 +01001002 /* Don't cache if the response came from a cache */
1003 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
1004 s->target == &http_cache_applet.obj_type) {
1005 goto out;
1006 }
1007
1008 /* cache only HTTP/1.1 */
1009 if (!(txn->req.flags & HTTP_MSGF_VER_11))
1010 goto out;
1011
Willy Tarreau6905d182019-10-01 17:59:17 +02001012 /* cache only GET method */
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001013 if (txn->meth != HTTP_METH_GET) {
1014 /* In case of successful unsafe method on a stored resource, the
1015 * cached entry must be invalidated (see RFC7234#4.4).
1016 * A "non-error response" is one with a 2xx (Successful) or 3xx
1017 * (Redirection) status code. */
1018 if (txn->status >= 200 && txn->status < 400) {
1019 switch (txn->meth) {
1020 case HTTP_METH_OPTIONS:
1021 case HTTP_METH_GET:
1022 case HTTP_METH_HEAD:
1023 case HTTP_METH_TRACE:
1024 break;
1025
1026 default: /* Any unsafe method */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001027 /* Discard any corresponding entry in case of successful
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001028 * unsafe request (such as PUT, POST or DELETE). */
1029 shctx_lock(shctx);
1030
1031 old = entry_exist(cconf->c.cache, txn->cache_hash);
1032 if (old) {
1033 eb32_delete(&old->eb);
1034 old->eb.key = 0;
1035 }
1036 shctx_unlock(shctx);
1037 }
1038 }
William Lallemand4da3f8a2017-10-31 14:33:34 +01001039 goto out;
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001040 }
William Lallemand4da3f8a2017-10-31 14:33:34 +01001041
Willy Tarreauc9036c02019-01-11 19:38:25 +01001042 /* cache key was not computed */
1043 if (!key)
1044 goto out;
1045
William Lallemand4da3f8a2017-10-31 14:33:34 +01001046 /* cache only 200 status code */
1047 if (txn->status != 200)
1048 goto out;
1049
Christopher Faulet839791a2019-01-07 16:12:07 +01001050 /* Find the corresponding filter instance for the current stream */
1051 list_for_each_entry(filter, &s->strm_flt.filters, list) {
1052 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
1053 /* No filter ctx, don't cache anything */
1054 if (!filter->ctx)
1055 goto out;
1056 cache_ctx = filter->ctx;
1057 break;
1058 }
1059 }
1060
1061 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001062 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001063
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001064 /* Do not cache too big objects. */
1065 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
1066 htx->data + htx->extra > shctx->max_obj_size)
1067 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001068
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001069 /* Only a subset of headers are supported in our Vary implementation. If
1070 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001071 * able to use the cache. Likewise, if Vary header support is disabled,
1072 * avoid caching responses that contain such a header. */
1073 ctx.blk = NULL;
1074 if (cache->vary_processing_enabled) {
1075 if (!http_check_vary_header(htx, &vary_signature))
1076 goto out;
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001077 if (vary_signature) {
1078 /* If something went wrong during the secondary key
1079 * building, do not store the response. */
1080 if (!(txn->flags & TX_CACHE_HAS_SEC_KEY))
1081 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001082 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001083 }
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001084 }
1085 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001086 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001087 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001088
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001089 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001090
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01001091 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001092 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001093
1094 shctx_lock(shctx);
1095 old = entry_exist(cache, txn->cache_hash);
1096 if (old) {
1097 if (vary_signature)
1098 old = secondary_entry_exist(cconf->c.cache, old,
1099 txn->cache_secondary_hash);
1100 if (old) {
1101 if (!old->complete) {
1102 /* An entry with the same primary key is already being
1103 * created, we should not try to store the current
1104 * response because it will waste space in the cache. */
1105 shctx_unlock(shctx);
1106 goto out;
1107 }
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001108 delete_entry(old);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001109 old->eb.key = 0;
1110 }
1111 }
1112 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry));
1113 if (!first) {
1114 shctx_unlock(shctx);
1115 goto out;
1116 }
1117 /* the received memory is not initialized, we need at least to mark
1118 * the object as not indexed yet.
1119 */
1120 object = (struct cache_entry *)first->data;
1121 memset(object, 0, sizeof(*object));
1122 object->eb.key = key;
1123 object->secondary_key_signature = vary_signature;
1124 /* We need to temporarily set a valid expiring time until the actual one
1125 * is set by the end of this function (in case of concurrent accesses to
1126 * the same resource). This way the second access will find an existing
1127 * but not yet usable entry in the tree and will avoid storing its data. */
1128 object->expire = now.tv_sec + 2;
1129
1130 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
1131 if (vary_signature)
1132 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
1133
1134 /* Insert the entry in the tree even if the payload is not cached yet. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001135 if (insert_entry(cache, object) != &object->eb) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001136 object->eb.key = 0;
1137 shctx_unlock(shctx);
1138 goto out;
1139 }
1140 shctx_unlock(shctx);
1141
1142 /* reserve space for the cache_entry structure */
1143 first->len = sizeof(struct cache_entry);
1144 first->last_append = NULL;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001145
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001146 /* Determine the entry's maximum age (taking into account the cache's
1147 * configuration) as well as the response's explicit max age (extracted
1148 * from cache-control directives or the expires header). */
1149 effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage);
1150
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001151 ctx.blk = NULL;
1152 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
Tim Duesterhusc2942842021-01-02 22:47:17 +01001153 long long hdr_age;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001154 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
1155 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
1156 hdr_age = CACHE_ENTRY_MAX_AGE;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001157 /* A response with an Age value greater than its
1158 * announced max age is stale and should not be stored. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001159 object->age = hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001160 if (unlikely(object->age > true_maxage))
1161 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001162 }
Remi Tricot-Le Breton51058d62020-12-03 18:19:32 +01001163 else
1164 goto out;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001165 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001166 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001167
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001168 /* Build a last-modified time that will be stored in the cache_entry and
1169 * compared to a future If-Modified-Since client header. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001170 object->last_modified = get_last_modified_time(htx);
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001171
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001172 chunk_reset(&trash);
1173 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1174 struct htx_blk *blk = htx_get_blk(htx, pos);
1175 enum htx_blk_type type = htx_get_blk_type(blk);
1176 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001177
Christopher Fauletb0667472019-09-03 22:22:12 +02001178 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001179 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
1180 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001181
1182 /* Look for optional ETag header.
1183 * We need to store the offset of the ETag value in order for
1184 * future conditional requests to be able to perform ETag
1185 * comparisons. */
1186 if (type == HTX_BLK_HDR) {
Tim Duesterhuse2fff102021-01-02 22:47:16 +01001187 struct ist header_name = htx_get_blk_name(htx, blk);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001188 if (isteq(header_name, ist("etag"))) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001189 object->etag_length = sz - istlen(header_name);
1190 object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001191 }
1192 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001193 if (type == HTX_BLK_EOH)
1194 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001195 }
1196
Christopher Fauletb0667472019-09-03 22:22:12 +02001197 /* Do not cache objects if the headers are too big. */
1198 if (hdrs_len > htx->size - global.tune.maxrewrite)
1199 goto out;
1200
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001201 /* If the response has a secondary_key, fill its key part related to
1202 * encodings with the actual encoding of the response. This way any
1203 * subsequent request having the same primary key will have its accepted
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +01001204 * encodings tested upon the cached response's one.
1205 * We will not cache a response that has an unknown encoding (not
1206 * explicitely supported in parse_encoding_value function). */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001207 if (cache->vary_processing_enabled && vary_signature)
Remi Tricot-Le Breton6ca89162021-01-07 14:50:51 +01001208 if (set_secondary_key_encoding(htx, object->secondary_key))
1209 goto out;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01001210
William Lallemand4da3f8a2017-10-31 14:33:34 +01001211 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001212 if (!shctx_row_reserve_hot(shctx, first, trash.data)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +01001213 shctx_unlock(shctx);
1214 goto out;
1215 }
1216 shctx_unlock(shctx);
1217
William Lallemand4da3f8a2017-10-31 14:33:34 +01001218 /* cache the headers in a http action because it allows to chose what
1219 * to cache, for example you might want to cache a response before
1220 * modifying some HTTP headers, or on the contrary after modifying
1221 * those headers.
1222 */
William Lallemand4da3f8a2017-10-31 14:33:34 +01001223 /* does not need to be locked because it's in the "hot" list,
1224 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001225 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
1226 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001227
1228 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +01001229 if (cache_ctx) {
1230 cache_ctx->first_block = first;
Christopher Faulet839791a2019-01-07 16:12:07 +01001231 /* store latest value and expiration time */
1232 object->latest_validation = now.tv_sec;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001233 object->expire = now.tv_sec + effective_maxage;
Christopher Faulet839791a2019-01-07 16:12:07 +01001234 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001235 }
1236
1237out:
1238 /* if does not cache */
1239 if (first) {
1240 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +01001241 first->len = 0;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001242 if (object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001243 delete_entry(object);
William Lallemand08727662017-11-21 20:01:27 +01001244 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001245 shctx_row_dec_hot(shctx, first);
1246 shctx_unlock(shctx);
1247 }
1248
William Lallemand41db4602017-10-30 11:15:51 +01001249 return ACT_RET_CONT;
1250}
1251
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001252#define HTX_CACHE_INIT 0 /* Initial state. */
1253#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
1254#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001255#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
1256#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001257
William Lallemandecb73b12017-11-24 14:33:55 +01001258static void http_cache_applet_release(struct appctx *appctx)
1259{
Christopher Faulet95220e22018-12-07 17:34:39 +01001260 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +01001261 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +01001262 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +01001263 struct shared_block *first = block_ptr(cache_ptr);
1264
1265 shctx_lock(shctx_ptr(cache));
1266 shctx_row_dec_hot(shctx_ptr(cache), first);
1267 shctx_unlock(shctx_ptr(cache));
1268}
1269
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001270
1271static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
1272 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001273{
Christopher Faulet95220e22018-12-07 17:34:39 +01001274 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1275 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001276 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001277 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001278 unsigned int max, total;
1279 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001280
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001281 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1282 if (!max)
1283 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001284 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001285 ? (info & 0xff) + ((info >> 8) & 0xfffff)
1286 : info & 0xfffffff);
1287 if (blksz > max)
1288 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001289
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001290 blk = htx_add_blk(htx, type, blksz);
1291 if (!blk)
1292 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001293
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001294 blk->info = info;
1295 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001296 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001297 while (blksz) {
1298 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001299 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001300 offset += max;
1301 blksz -= max;
1302 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001303 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001304 if (blksz || offset == shctx->block_size) {
1305 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1306 offset = 0;
1307 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001308 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001309 appctx->ctx.cache.offset = offset;
1310 appctx->ctx.cache.next = shblk;
1311 appctx->ctx.cache.sent += total;
1312 return total;
1313}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001314
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001315static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
1316 uint32_t info, struct shared_block *shblk, unsigned int offset)
1317{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001318
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001319 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1320 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
1321 unsigned int max, total, rem_data;
1322 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001323
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001324 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1325 if (!max)
1326 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001327
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001328 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001329 if (appctx->ctx.cache.rem_data) {
1330 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001331 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001332 }
1333 else {
1334 blksz = (info & 0xfffffff);
1335 total = 4;
1336 }
1337 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001338 rem_data = blksz - max;
1339 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001340 }
1341
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001342 while (blksz) {
1343 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001344
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001345 max = MIN(blksz, shctx->block_size - offset);
1346 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1347 offset += sz;
1348 blksz -= sz;
1349 total += sz;
1350 if (sz < max)
1351 break;
1352 if (blksz || offset == shctx->block_size) {
1353 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1354 offset = 0;
1355 }
1356 }
1357
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001358 appctx->ctx.cache.offset = offset;
1359 appctx->ctx.cache.next = shblk;
1360 appctx->ctx.cache.sent += total;
1361 appctx->ctx.cache.rem_data = rem_data + blksz;
1362 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001363}
1364
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001365static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1366 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001367{
Christopher Faulet95220e22018-12-07 17:34:39 +01001368 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1369 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001370 struct shared_block *shblk;
1371 unsigned int offset, sz;
1372 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001373
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001374 while (len) {
1375 enum htx_blk_type type;
1376 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001377
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001378 shblk = appctx->ctx.cache.next;
1379 offset = appctx->ctx.cache.offset;
1380 if (appctx->ctx.cache.rem_data) {
1381 type = HTX_BLK_DATA;
1382 info = 0;
1383 goto add_data_blk;
1384 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001385
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001386 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001387 sz = MIN(4, shctx->block_size - offset);
1388 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1389 offset += sz;
1390 if (sz < 4) {
1391 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1392 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1393 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001394 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001395
1396 /* Get payload of the next HTX block and insert it. */
1397 type = (info >> 28);
1398 if (type != HTX_BLK_DATA)
1399 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1400 else {
1401 add_data_blk:
1402 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001403 }
1404
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001405 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001406 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001407 total += ret;
1408 len -= ret;
1409
1410 if (appctx->ctx.cache.rem_data || type == mark)
1411 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001412 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001413
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001414 return total;
1415}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001416
1417static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1418{
1419 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1420 unsigned int age;
1421 char *end;
1422
1423 chunk_reset(&trash);
1424 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1425 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1426 age = CACHE_ENTRY_MAX_AGE;
1427 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1428 b_set_data(&trash, end - b_head(&trash));
1429 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1430 return 0;
1431 return 1;
1432}
1433
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001434static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001435{
1436 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1437 struct shared_block *first = block_ptr(cache_ptr);
1438 struct stream_interface *si = appctx->owner;
1439 struct channel *req = si_oc(si);
1440 struct channel *res = si_ic(si);
1441 struct htx *req_htx, *res_htx;
1442 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001443 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001444 size_t ret, total = 0;
1445
1446 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001447 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001448
1449 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1450 goto out;
1451
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001452 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001453 if (!b_size(&res->buf)) {
1454 si_rx_room_blk(si);
1455 goto out;
1456 }
1457
Willy Tarreauefef3232018-12-16 00:37:45 +01001458 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001459 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001460
1461 if (appctx->st0 == HTX_CACHE_INIT) {
1462 appctx->ctx.cache.next = block_ptr(cache_ptr);
1463 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1464 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001465 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001466 appctx->st0 = HTX_CACHE_HEADER;
1467 }
1468
1469 if (appctx->st0 == HTX_CACHE_HEADER) {
1470 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001471 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1472 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1473 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1474 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001475 goto error;
1476
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001477 /* In case of a conditional request, we might want to send a
1478 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001479 if (appctx->ctx.cache.send_notmodified) {
1480 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1481 /* If replacing the status code fails we need to send the full response. */
1482 appctx->ctx.cache.send_notmodified = 0;
1483 }
1484 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001485
1486 /* Skip response body for HEAD requests or in case of "304 Not
1487 * Modified" response. */
1488 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001489 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001490 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001491 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001492 }
1493
1494 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001495 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1496 if (len) {
1497 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1498 if (ret < len) {
1499 si_rx_room_blk(si);
1500 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001501 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001502 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001503 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001504 }
1505
1506 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001507 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001508 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1509 si_rx_room_blk(si);
1510 goto out;
1511 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001512 appctx->st0 = HTX_CACHE_END;
1513 }
1514
1515 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001516 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001517 res->flags |= CF_READ_NULL;
1518 si_shutr(si);
1519 }
1520
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001521 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001522 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001523 if (total)
1524 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001525 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001526
1527 /* eat the whole request */
1528 if (co_data(req)) {
1529 req_htx = htx_from_buf(&req->buf);
1530 co_htx_skip(req, req_htx, co_data(req));
1531 htx_to_buf(req_htx, &req->buf);
1532 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001533 return;
1534
1535 error:
1536 /* Sent and HTTP error 500 */
1537 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001538 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001539 res->buf.data = b_data(errmsg);
1540 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1541 res_htx = htx_from_buf(&res->buf);
1542
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001543 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001544 appctx->st0 = HTX_CACHE_END;
1545 goto end;
1546}
1547
1548
Christopher Faulet95220e22018-12-07 17:34:39 +01001549static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001550{
1551 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001552 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001553
Christopher Faulet95220e22018-12-07 17:34:39 +01001554 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001555 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001556 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001557 }
1558
1559 /* check if a cache filter was already registered with this cache
1560 * name, if that's the case, must use it. */
1561 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001562 if (fconf->id == cache_store_flt_id) {
1563 cconf = fconf->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001564 if (cconf && strcmp((char *)cconf->c.name, name) == 0) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001565 rule->arg.act.p[0] = cconf;
1566 return 1;
1567 }
William Lallemand41db4602017-10-30 11:15:51 +01001568 }
1569 }
1570
Christopher Faulet95220e22018-12-07 17:34:39 +01001571 /* Create the filter cache config */
1572 cconf = calloc(1, sizeof(*cconf));
1573 if (!cconf) {
1574 memprintf(err, "out of memory\n");
1575 goto err;
1576 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001577 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001578 cconf->c.name = strdup(name);
1579 if (!cconf->c.name) {
1580 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001581 goto err;
1582 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001583
William Lallemand41db4602017-10-30 11:15:51 +01001584 /* register a filter to fill the cache buffer */
1585 fconf = calloc(1, sizeof(*fconf));
1586 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001587 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001588 goto err;
1589 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001590 fconf->id = cache_store_flt_id;
1591 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001592 fconf->ops = &cache_ops;
1593 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1594
Christopher Faulet95220e22018-12-07 17:34:39 +01001595 rule->arg.act.p[0] = cconf;
1596 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001597
Christopher Faulet95220e22018-12-07 17:34:39 +01001598 err:
1599 free(cconf);
1600 return 0;
1601}
1602
1603enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1604 struct act_rule *rule, char **err)
1605{
1606 rule->action = ACT_CUSTOM;
1607 rule->action_ptr = http_action_store_cache;
1608
1609 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1610 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001611
Christopher Faulet95220e22018-12-07 17:34:39 +01001612 (*orig_arg)++;
1613 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001614}
1615
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001616/* This produces a sha1 hash of the concatenation of the HTTP method,
1617 * the first occurrence of the Host header followed by the path component
1618 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001619int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001620{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001621 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001622 struct htx *htx = htxbuf(&s->req.buf);
1623 struct htx_sl *sl;
1624 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001625 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001626 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001627 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001628
William Lallemandf528fff2017-11-23 19:43:17 +01001629 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001630 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001631
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001632 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001633 uri = htx_sl_req_uri(sl); // whole uri
1634 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001635 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001636
1637 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1638 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1639 * URIs are almost always sent in absolute form with their scheme. In
1640 * this case, the scheme is almost always "https". In order to support
1641 * sharing of cache objects between H1 and H2, we'll hash the absolute
1642 * URI whenever known, or prepend "https://" + the Host header for
1643 * relative URIs. The difference will only appear on absolute HTTP/1
1644 * requests sent to an origin server, which practically is never met in
1645 * the real world so we don't care about the ability to share the same
1646 * key here.URIs are normalized from the absolute URI to an origin form as
1647 * well.
1648 */
1649 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001650 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001651 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1652 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001653 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001654 }
1655
1656 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001657
1658 /* hash everything */
1659 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001660 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001661 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1662
1663 return 1;
1664}
1665
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001666/* Looks for "If-None-Match" headers in the request and compares their value
1667 * with the one that might have been stored in the cache_entry. If any of them
1668 * matches, a "304 Not Modified" response should be sent instead of the cached
1669 * data.
1670 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001671 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1672 *
1673 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1674 * header and compare its value (date) to the one stored in the cache_entry.
1675 * If the request's date is later than the cached one, we also send a
1676 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1677 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001678 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1679 */
1680static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1681 struct cache_entry *entry)
1682{
1683 int retval = 0;
1684
1685 struct http_hdr_ctx ctx = { .blk = NULL };
1686 struct ist cache_entry_etag = IST_NULL;
1687 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001688 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001689
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001690 struct tm tm = {};
1691 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001692
1693 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001694 * cache_entry's ETag in order to perform comparisons.
1695 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001696 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001697 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001698
1699 /* A '*' matches everything. */
1700 if (isteq(ctx.value, ist("*")) != 0) {
1701 retval = 1;
1702 break;
1703 }
1704
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001705 /* No need to rebuild an etag if none was stored in the cache. */
1706 if (entry->etag_length == 0)
1707 break;
1708
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001709 /* Rebuild the stored ETag. */
1710 if (etag_buffer == NULL) {
1711 etag_buffer = get_trash_chunk();
1712
1713 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1714 (unsigned char*)b_orig(etag_buffer),
1715 entry->etag_offset, entry->etag_length) == 0) {
1716 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1717 } else {
1718 /* We could not rebuild the ETag in one go, we
1719 * won't send a "304 Not Modified" response. */
1720 break;
1721 }
1722 }
1723
1724 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1725 retval = 1;
1726 break;
1727 }
1728 }
1729
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001730 /* If the request did not contain an "If-None-Match" header, we look for
1731 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1732 if (retval == 0 && if_none_match_found == 0) {
1733 ctx.blk = NULL;
1734 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1735 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1736 if_modified_since = my_timegm(&tm);
1737
1738 /* We send a "304 Not Modified" response if the
1739 * entry's last modified date is earlier than
1740 * the one found in the "If-Modified-Since"
1741 * header. */
1742 retval = (entry->last_modified <= if_modified_since);
1743 }
1744 }
1745 }
1746
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001747 return retval;
1748}
1749
William Lallemand41db4602017-10-30 11:15:51 +01001750enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1751 struct session *sess, struct stream *s, int flags)
1752{
William Lallemand77c11972017-10-31 20:43:01 +01001753
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001754 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001755 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001756 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1757 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001758 struct shared_block *entry_block;
1759
William Lallemand77c11972017-10-31 20:43:01 +01001760
Willy Tarreau6905d182019-10-01 17:59:17 +02001761 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1762 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001763 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001764 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001765 txn->flags |= TX_CACHE_IGNORE;
1766
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001767 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001768
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001769 /* The request's hash has to be calculated for all requests, even POSTs
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001770 * or PUTs for instance because RFC7234 specifies that a successful
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001771 * "unsafe" method on a stored resource must invalidate it
1772 * (see RFC7234#4.4). */
1773 if (!sha1_hosturi(s))
Willy Tarreau504455c2017-12-22 17:47:35 +01001774 return ACT_RET_CONT;
1775
Willy Tarreau504455c2017-12-22 17:47:35 +01001776 if (s->txn->flags & TX_CACHE_IGNORE)
1777 return ACT_RET_CONT;
1778
Willy Tarreaua1214a52018-12-14 14:00:25 +01001779 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001780 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001781 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001782 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001783
William Lallemanda400a3a2017-11-20 19:13:12 +01001784 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001785 res = entry_exist(cache, s->txn->cache_hash);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001786 /* We must not use an entry that is not complete. */
1787 if (res && res->complete) {
William Lallemand77c11972017-10-31 20:43:01 +01001788 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001789 entry_block = block_ptr(res);
1790 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001791 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001792
1793 /* In case of Vary, we could have multiple entries with the same
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001794 * primary hash. We need to calculate the secondary hash in order
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001795 * to find the actual entry we want (if it exists). */
1796 if (res->secondary_key_signature) {
1797 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1798 shctx_lock(shctx_ptr(cache));
1799 sec_entry = secondary_entry_exist(cache, res,
1800 s->txn->cache_secondary_hash);
1801 if (sec_entry && sec_entry != res) {
1802 /* The wrong row was added to the hot list. */
1803 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1804 entry_block = block_ptr(sec_entry);
1805 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1806 }
1807 res = sec_entry;
1808 shctx_unlock(shctx_ptr(cache));
1809 }
1810 else
1811 res = NULL;
1812 }
1813
1814 /* We looked for a valid secondary entry and could not find one,
1815 * the request must be forwarded to the server. */
1816 if (!res) {
1817 shctx_lock(shctx_ptr(cache));
1818 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1819 shctx_unlock(shctx_ptr(cache));
1820 return ACT_RET_CONT;
1821 }
1822
William Lallemand77c11972017-10-31 20:43:01 +01001823 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001824 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001825 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001826 appctx->rule = rule;
1827 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001828 appctx->ctx.cache.next = NULL;
1829 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001830 appctx->ctx.cache.send_notmodified =
1831 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001832
1833 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001834 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001835 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001836 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001837 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001838 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001839 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001840 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001841 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001842 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001843 }
1844 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001845 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001846
1847 /* Shared context does not need to be locked while we calculate the
1848 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001849 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001850 /* Build a complete secondary hash until the server response
1851 * tells us which fields should be kept (if any). */
1852 http_request_prebuild_full_secondary_key(s);
1853 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001854 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001855}
1856
1857
1858enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1859 struct act_rule *rule, char **err)
1860{
William Lallemand41db4602017-10-30 11:15:51 +01001861 rule->action = ACT_CUSTOM;
1862 rule->action_ptr = http_action_req_cache_use;
1863
Christopher Faulet95220e22018-12-07 17:34:39 +01001864 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001865 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001866
1867 (*orig_arg)++;
1868 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001869}
1870
1871int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1872{
1873 int err_code = 0;
1874
1875 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1876
1877 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001878 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001879 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001880 err_code |= ERR_ALERT | ERR_ABORT;
1881 goto out;
1882 }
1883
1884 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1885 err_code |= ERR_ABORT;
1886 goto out;
1887 }
1888
1889 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001890 struct cache *cache_config;
1891
William Lallemand41db4602017-10-30 11:15:51 +01001892 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1893 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001894 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001895 err_code |= ERR_ALERT | ERR_ABORT;
1896 goto out;
1897 }
1898
1899 strlcpy2(tmp_cache_config->id, args[1], 33);
1900 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001901 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001902 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001903 err_code |= ERR_WARN;
1904 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001905
1906 list_for_each_entry(cache_config, &caches_config, list) {
1907 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1908 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1909 file, linenum, tmp_cache_config->id);
1910 err_code |= ERR_ALERT | ERR_ABORT;
1911 goto out;
1912 }
1913 }
1914
William Lallemand49b44532017-11-24 18:53:43 +01001915 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001916 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001917 tmp_cache_config->maxobjsz = 0;
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +01001918 tmp_cache_config->max_secondary_entries = DEFAULT_MAX_SECONDARY_ENTRY;
William Lallemand41db4602017-10-30 11:15:51 +01001919 }
1920 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001921 unsigned long int maxsize;
1922 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001923
1924 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1925 err_code |= ERR_ABORT;
1926 goto out;
1927 }
1928
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001929 maxsize = strtoul(args[1], &err, 10);
1930 if (err == args[1] || *err != '\0') {
1931 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1932 file, linenum, args[1]);
1933 err_code |= ERR_ABORT;
1934 goto out;
1935 }
1936
1937 if (maxsize > (UINT_MAX >> 20)) {
1938 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1939 file, linenum, args[1], UINT_MAX >> 20);
1940 err_code |= ERR_ABORT;
1941 goto out;
1942 }
1943
William Lallemand41db4602017-10-30 11:15:51 +01001944 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001945 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001946 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001947 } else if (strcmp(args[0], "max-age") == 0) {
1948 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1949 err_code |= ERR_ABORT;
1950 goto out;
1951 }
1952
1953 if (!*args[1]) {
1954 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1955 file, linenum, args[0]);
1956 err_code |= ERR_WARN;
1957 }
1958
1959 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001960 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001961 unsigned int maxobjsz;
1962 char *err;
1963
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001964 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1965 err_code |= ERR_ABORT;
1966 goto out;
1967 }
1968
1969 if (!*args[1]) {
1970 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1971 file, linenum, args[0]);
1972 err_code |= ERR_WARN;
1973 }
1974
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001975 maxobjsz = strtoul(args[1], &err, 10);
1976 if (err == args[1] || *err != '\0') {
1977 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1978 file, linenum, args[1]);
1979 err_code |= ERR_ABORT;
1980 goto out;
1981 }
1982 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001983 } else if (strcmp(args[0], "process-vary") == 0) {
1984 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1985 err_code |= ERR_ABORT;
1986 goto out;
1987 }
1988
1989 if (!*args[1]) {
Remi Tricot-Le Bretone6cc5b52020-12-23 18:13:53 +01001990 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 +01001991 file, linenum, args[0]);
1992 err_code |= ERR_WARN;
1993 }
Remi Tricot-Le Bretone6cc5b52020-12-23 18:13:53 +01001994 if (strcmp(args[1], "on") == 0)
1995 tmp_cache_config->vary_processing_enabled = 1;
1996 else if (strcmp(args[1], "off") == 0)
1997 tmp_cache_config->vary_processing_enabled = 0;
1998 else {
1999 ha_warning("parsing [%s:%d]: '%s' expects \"on\" or \"off\" (enable or disable vary processing).\n",
2000 file, linenum, args[0]);
2001 err_code |= ERR_WARN;
2002 }
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +01002003 } else if (strcmp(args[0], "max-secondary-entries") == 0) {
2004 unsigned int max_sec_entries;
2005 char *err;
2006
2007 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
2008 err_code |= ERR_ABORT;
2009 goto out;
2010 }
2011
2012 if (!*args[1]) {
2013 ha_warning("parsing [%s:%d]: '%s' expects a strictly positive number.\n",
2014 file, linenum, args[0]);
2015 err_code |= ERR_WARN;
2016 }
2017
2018 max_sec_entries = strtoul(args[1], &err, 10);
2019 if (err == args[1] || *err != '\0' || max_sec_entries == 0) {
2020 ha_warning("parsing [%s:%d]: max-secondary-entries wrong value '%s'\n",
2021 file, linenum, args[1]);
2022 err_code |= ERR_ABORT;
2023 goto out;
2024 }
2025 tmp_cache_config->max_secondary_entries = max_sec_entries;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002026 }
2027 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002028 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01002029 err_code |= ERR_ALERT | ERR_FATAL;
2030 goto out;
2031 }
2032out:
2033 return err_code;
2034}
2035
2036/* once the cache section is parsed */
2037
2038int cfg_post_parse_section_cache()
2039{
William Lallemand41db4602017-10-30 11:15:51 +01002040 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01002041
2042 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01002043
2044 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002045 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01002046 err_code |= ERR_FATAL | ERR_ALERT;
2047 goto out;
2048 }
2049
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02002050 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002051 /* Default max. file size is a 256th of the cache size. */
2052 tmp_cache_config->maxobjsz =
2053 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02002054 }
2055 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
2056 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
2057 err_code |= ERR_FATAL | ERR_ALERT;
2058 goto out;
2059 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02002060
William Lallemandd1d1e222019-08-28 15:22:49 +02002061 /* add to the list of cache to init and reinit tmp_cache_config
2062 * for next cache section, if any.
2063 */
2064 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
2065 tmp_cache_config = NULL;
2066 return err_code;
2067 }
2068out:
2069 free(tmp_cache_config);
2070 tmp_cache_config = NULL;
2071 return err_code;
2072
2073}
2074
2075int post_check_cache()
2076{
2077 struct proxy *px;
2078 struct cache *back, *cache_config, *cache;
2079 struct shared_context *shctx;
2080 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002081 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02002082
2083 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
2084
2085 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
2086 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01002087
Frédéric Lécaillebc584492018-10-25 20:18:59 +02002088 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01002089 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01002090 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01002091 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01002092 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01002093
2094 err_code |= ERR_FATAL | ERR_ALERT;
2095 goto out;
2096 }
William Lallemanda400a3a2017-11-20 19:13:12 +01002097 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02002098 /* the cache structure is stored in the shctx and added to the
2099 * caches list, we can remove the entry from the caches_config
2100 * list */
2101 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01002102 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01002103 cache->entries = EB_ROOT;
William Lallemand41db4602017-10-30 11:15:51 +01002104 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02002105 LIST_DEL(&cache_config->list);
2106 free(cache_config);
2107
2108 /* Find all references for this cache in the existing filters
2109 * (over all proxies) and reference it in matching filters.
2110 */
2111 for (px = proxies_list; px; px = px->next) {
2112 struct flt_conf *fconf;
2113 struct cache_flt_conf *cconf;
2114
2115 list_for_each_entry(fconf, &px->filter_configs, list) {
2116 if (fconf->id != cache_store_flt_id)
2117 continue;
2118
2119 cconf = fconf->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002120 if (strcmp(cache->id, cconf->c.name) == 0) {
William Lallemandd1d1e222019-08-28 15:22:49 +02002121 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02002122 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02002123 cconf->c.cache = cache;
2124 break;
2125 }
2126 }
2127 }
William Lallemand41db4602017-10-30 11:15:51 +01002128 }
William Lallemandd1d1e222019-08-28 15:22:49 +02002129
William Lallemand41db4602017-10-30 11:15:51 +01002130out:
William Lallemand41db4602017-10-30 11:15:51 +01002131 return err_code;
2132
William Lallemand41db4602017-10-30 11:15:51 +01002133}
2134
William Lallemand41db4602017-10-30 11:15:51 +01002135struct flt_ops cache_ops = {
2136 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01002137 .check = cache_store_check,
2138 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01002139
Christopher Faulet65554e12020-03-06 14:52:06 +01002140 /* Handle stream init/deinit */
2141 .attach = cache_store_strm_init,
2142 .detach = cache_store_strm_deinit,
2143
William Lallemand4da3f8a2017-10-31 14:33:34 +01002144 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01002145 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01002146
2147 /* Filter HTTP requests and responses */
2148 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01002149 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01002150 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01002151};
2152
Christopher Faulet99a17a22018-12-11 09:18:27 +01002153
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002154int accept_encoding_cmp(const void *a, const void *b)
2155{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002156 unsigned int int_a = *(unsigned int*)a;
2157 unsigned int int_b = *(unsigned int*)b;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002158
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002159 if (int_a < int_b)
2160 return -1;
2161 if (int_a > int_b)
2162 return 1;
2163 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002164}
2165
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002166
2167#define CHECK_ENCODING(str, encoding_name, encoding_value) \
2168 ({ \
2169 int retval = 0; \
2170 if (istmatch(str, (struct ist){ .ptr = encoding_name+1, .len = sizeof(encoding_name) - 2 })) { \
2171 retval = encoding_value; \
2172 encoding = istadv(encoding, sizeof(encoding_name) - 2); \
2173 } \
2174 (retval); \
2175 })
2176
2177/*
2178 * Parse the encoding <encoding> and try to match the encoding part upon an
2179 * encoding list of explicitly supported encodings (which all have a specific
2180 * bit in an encoding bitmap). If a weight is included in the value, find out if
2181 * it is null or not. The bit value will be set in the <encoding_value>
2182 * parameter and the <has_null_weight> will be set to 1 if the weight is strictly
2183 * 0, 1 otherwise.
2184 * The encodings list is extracted from
2185 * https://www.iana.org/assignments/http-parameters/http-parameters.xhtml.
2186 * Returns 0 in case of success and -1 in case of error.
2187 */
2188static int parse_encoding_value(struct ist encoding, unsigned int *encoding_value,
2189 unsigned int *has_null_weight)
2190{
2191 int retval = 0;
2192
2193 if (!encoding_value)
2194 return -1;
2195
2196 if (!istlen(encoding))
2197 return -1; /* Invalid encoding */
2198
2199 *encoding_value = 0;
2200 if (has_null_weight)
2201 *has_null_weight = 0;
2202
2203 switch (*encoding.ptr) {
2204 case 'a':
2205 encoding = istadv(encoding, 1);
2206 *encoding_value = CHECK_ENCODING(encoding, "aes128gcm", VARY_ENCODING_AES128GCM);
2207 break;
2208 case 'b':
2209 encoding = istadv(encoding, 1);
2210 *encoding_value = CHECK_ENCODING(encoding, "br", VARY_ENCODING_BR);
2211 break;
2212 case 'c':
2213 encoding = istadv(encoding, 1);
2214 *encoding_value = CHECK_ENCODING(encoding, "compress", VARY_ENCODING_COMPRESS);
2215 break;
2216 case 'd':
2217 encoding = istadv(encoding, 1);
2218 *encoding_value = CHECK_ENCODING(encoding, "deflate", VARY_ENCODING_DEFLATE);
2219 break;
2220 case 'e':
2221 encoding = istadv(encoding, 1);
2222 *encoding_value = CHECK_ENCODING(encoding, "exi", VARY_ENCODING_EXI);
2223 break;
2224 case 'g':
2225 encoding = istadv(encoding, 1);
2226 *encoding_value = CHECK_ENCODING(encoding, "gzip", VARY_ENCODING_GZIP);
2227 break;
2228 case 'i':
2229 encoding = istadv(encoding, 1);
2230 *encoding_value = CHECK_ENCODING(encoding, "identity", VARY_ENCODING_IDENTITY);
2231 break;
2232 case 'p':
2233 encoding = istadv(encoding, 1);
2234 *encoding_value = CHECK_ENCODING(encoding, "pack200-gzip", VARY_ENCODING_PACK200_GZIP);
2235 break;
2236 case 'x':
2237 encoding = istadv(encoding, 1);
2238 *encoding_value = CHECK_ENCODING(encoding, "x-gzip", VARY_ENCODING_GZIP);
2239 if (!*encoding_value)
2240 *encoding_value = CHECK_ENCODING(encoding, "x-compress", VARY_ENCODING_COMPRESS);
2241 break;
2242 case 'z':
2243 encoding = istadv(encoding, 1);
2244 *encoding_value = CHECK_ENCODING(encoding, "zstd", VARY_ENCODING_ZSTD);
2245 break;
2246 case '*':
2247 encoding = istadv(encoding, 1);
2248 *encoding_value = VARY_ENCODING_STAR;
2249 break;
2250 default:
2251 retval = -1; /* Unmanaged encoding */
2252 break;
2253 }
2254
2255 /* Process the optional weight part of the encoding. */
2256 if (*encoding_value) {
2257 encoding = http_trim_leading_spht(encoding);
2258 if (istlen(encoding)) {
2259 if (*encoding.ptr != ';')
2260 return -1;
2261
2262 if (has_null_weight) {
2263 encoding = istadv(encoding, 1);
2264
2265 encoding = http_trim_leading_spht(encoding);
2266
2267 *has_null_weight = isteq(encoding, ist("q=0"));
2268 }
2269 }
2270 }
2271
2272 return retval;
2273}
2274
Tim Duesterhus23b29452020-11-24 22:22:56 +01002275#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002276/*
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002277 * Build a hash of the accept-encoding header. The hash is split into an
2278 * encoding bitmap and an actual hash of the different encodings.
2279 * The bitmap is built by matching every sub-part of the accept-encoding value
2280 * with a subset of explicitly supported encodings, which all have their own bit
2281 * in the bitmap. This bitmap will be used to determine if a response can be
2282 * served to a client (that is if it has an encoding that is accepted by the
2283 * client).
2284 * The hash part is built out of all the sub-parts of the value, which are
2285 * converted to lower case, hashed, sorted and then all the unique sub-hashes
2286 * are XORed into a single hash.
2287 * Returns 0 in case of success, 1 if the hash buffer should be filled with 0s
2288 * and -1 in case of error.
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002289 */
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002290static int accept_encoding_normalizer(struct htx *htx, struct ist hdr_name,
2291 char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002292{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002293 unsigned int values[ACCEPT_ENCODING_MAX_ENTRIES] = {};
Tim Duesterhus23b29452020-11-24 22:22:56 +01002294 size_t count = 0;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002295 struct accept_encoding_hash hash = {};
2296 unsigned int encoding_bmp_bl = -1;
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002297 unsigned int prev = 0, curr = 0;
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002298 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002299 unsigned int encoding_value;
2300 unsigned int rejected_encoding;
2301
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05002302 /* A user agent always accepts an unencoded value unless it explicitly
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002303 * refuses it through an "identity;q=0" accept-encoding value. */
2304 hash.encoding_bitmap |= VARY_ENCODING_IDENTITY;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002305
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002306 /* Iterate over all the ACCEPT_ENCODING_MAX_ENTRIES first accept-encoding
2307 * values that might span acrosse multiple accept-encoding headers. */
2308 while (http_find_header(htx, hdr_name, &ctx, 0) && count < ACCEPT_ENCODING_MAX_ENTRIES) {
2309 /* Turn accept-encoding value to lower case */
2310 ist2bin_lc(istptr(ctx.value), ctx.value);
Tim Duesterhus23b29452020-11-24 22:22:56 +01002311
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002312 /* Try to identify a known encoding and to manage null weights. */
2313 if (!parse_encoding_value(ctx.value, &encoding_value, &rejected_encoding)) {
2314 if (rejected_encoding)
2315 encoding_bmp_bl &= ~encoding_value;
2316 else
2317 hash.encoding_bitmap |= encoding_value;
2318 }
2319 else {
2320 /* Unknown encoding */
2321 hash.encoding_bitmap |= VARY_ENCODING_OTHER;
2322 }
2323
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002324 values[count++] = hash_crc32(istptr(ctx.value), istlen(ctx.value));
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002325 }
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002326
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002327 /* If a "*" was found in the accepted encodings (without a null weight),
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05002328 * all the encoding are accepted except the ones explicitly rejected. */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002329 if (hash.encoding_bitmap & VARY_ENCODING_STAR) {
2330 hash.encoding_bitmap = ~0;
2331 }
2332
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05002333 /* Clear explicitly rejected encodings from the bitmap */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002334 hash.encoding_bitmap &= encoding_bmp_bl;
2335
2336 /* As per RFC7231#5.3.4, "If no Accept-Encoding field is in the request,
2337 * any content-coding is considered acceptable by the user agent". */
2338 if (count == 0)
2339 hash.encoding_bitmap = ~0;
2340
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002341 /* A request with more than ACCEPT_ENCODING_MAX_ENTRIES accepted
2342 * encodings might be illegitimate so we will not use it. */
2343 if (count == ACCEPT_ENCODING_MAX_ENTRIES)
2344 return -1;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002345
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002346 /* Sort the values alphabetically. */
2347 qsort(values, count, sizeof(*values), &accept_encoding_cmp);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002348
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002349 while (count) {
2350 curr = values[--count];
2351 if (curr != prev) {
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002352 hash.hash ^= curr;
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002353 }
2354 prev = curr;
2355 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002356
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002357 write_u32(buf, hash.encoding_bitmap);
2358 *buf_len = sizeof(hash.encoding_bitmap);
2359 write_u32(buf+*buf_len, hash.hash);
2360 *buf_len += sizeof(hash.hash);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002361
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002362 /* This function fills the hash buffer correctly even if no header was
2363 * found, hence the 0 return value (success). */
Tim Duesterhus23b29452020-11-24 22:22:56 +01002364 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002365}
Tim Duesterhus23b29452020-11-24 22:22:56 +01002366#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002367
2368/*
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002369 * Normalizer used by default for the Referer header. It only
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002370 * calculates a simple crc of the whole value.
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002371 * Only the first occurrence of the header will be taken into account in the
2372 * hash.
2373 * Returns 0 in case of success, 1 if the hash buffer should be filled with 0s
2374 * and -1 in case of error.
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002375 */
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002376static int default_normalizer(struct htx *htx, struct ist hdr_name,
2377 char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002378{
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002379 int retval = 1;
2380 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002381
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002382 if (http_find_header(htx, hdr_name, &ctx, 1)) {
2383 retval = 0;
2384 write_u32(buf, hash_crc32(istptr(ctx.value), istlen(ctx.value)));
2385 *buf_len = sizeof(int);
2386 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002387
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002388 return retval;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002389}
2390
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002391/*
2392 * Accept-Encoding sub-hash comparison function.
2393 * Returns 0 if the hashes are alike.
2394 */
2395static int accept_encoding_hash_cmp(const void *ref_hash, const void *new_hash, unsigned int hash_len)
2396{
2397 struct accept_encoding_hash ref = {};
2398 struct accept_encoding_hash new = {};
2399
2400 ref.encoding_bitmap = read_u32(ref_hash);
2401 new.encoding_bitmap = read_u32(new_hash);
2402
2403 if (!(ref.encoding_bitmap & VARY_ENCODING_OTHER)) {
2404 /* All the bits set in the reference bitmap correspond to the
2405 * stored response' encoding and should all be set in the new
2406 * encoding bitmap in order for the client to be able to manage
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002407 * the response.
2408 *
2409 * If this is the case the cached response has encodings that
2410 * are accepted by the client. It can be served directly by
2411 * the cache (as far as the accept-encoding part is concerned).
2412 */
2413
2414 return (ref.encoding_bitmap & new.encoding_bitmap) != ref.encoding_bitmap;
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002415 }
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002416 else {
2417 /* We must compare hashes only when the the response contains
2418 * unknown encodings.
2419 * Otherwise we might serve unacceptable responses if the hash
2420 * of a client's `accept-encoding` header collides with a
2421 * known encoding.
2422 */
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002423
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002424 ref.hash = read_u32(ref_hash+sizeof(ref.encoding_bitmap));
2425 new.hash = read_u32(new_hash+sizeof(new.encoding_bitmap));
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002426
Tim Duesterhusdc38bc42020-12-29 12:43:53 +01002427 return ref.hash != new.hash;
2428 }
Remi Tricot-Le Bretonce9e7b22020-12-23 18:13:49 +01002429}
2430
2431
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002432/*
2433 * Pre-calculate the hashes of all the supported headers (in our Vary
2434 * implementation) of a given request. We have to calculate all the hashes
2435 * in advance because the actual Vary signature won't be known until the first
2436 * response.
2437 * Only the first occurrence of every header will be taken into account in the
2438 * hash.
2439 * If the header is not present, the hash portion of the given header will be
2440 * filled with zeros.
2441 * Returns 0 in case of success.
2442 */
2443static int http_request_prebuild_full_secondary_key(struct stream *s)
2444{
Remi Tricot-Le Bretonbba29122020-12-23 18:13:44 +01002445 /* The fake signature (second parameter) will ensure that every part of the
2446 * secondary key is calculated. */
2447 return http_request_build_secondary_key(s, ~0);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002448}
2449
2450
2451/*
2452 * Calculate the secondary key for a request for which we already have a known
2453 * vary signature. The key is made by aggregating hashes calculated for every
2454 * header mentioned in the vary signature.
2455 * Only the first occurrence of every header will be taken into account in the
2456 * hash.
2457 * If the header is not present, the hash portion of the given header will be
2458 * filled with zeros.
2459 * Returns 0 in case of success.
2460 */
2461static int http_request_build_secondary_key(struct stream *s, int vary_signature)
2462{
2463 struct http_txn *txn = s->txn;
2464 struct htx *htx = htxbuf(&s->req.buf);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002465
2466 unsigned int idx;
2467 const struct vary_hashing_information *info = NULL;
2468 unsigned int hash_length = 0;
2469 int retval = 0;
2470 int offset = 0;
2471
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002472 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && retval >= 0; ++idx) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002473 info = &vary_information[idx];
2474
Remi Tricot-Le Bretone4421de2020-12-23 18:13:46 +01002475 /* The normalizing functions will be in charge of getting the
2476 * header values from the htx. This way they can manage multiple
2477 * occurrences of their processed header. */
2478 if ((vary_signature & info->value) && info->norm_fn != NULL &&
2479 !(retval = info->norm_fn(htx, info->hdr_name, &txn->cache_secondary_hash[offset], &hash_length))) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002480 offset += hash_length;
2481 }
2482 else {
2483 /* Fill hash with 0s. */
2484 hash_length = info->hash_length;
2485 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
2486 offset += hash_length;
2487 }
2488 }
2489
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01002490 if (retval >= 0)
2491 txn->flags |= TX_CACHE_HAS_SEC_KEY;
2492
2493 return (retval < 0);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002494}
2495
2496/*
2497 * Build the actual secondary key of a given request out of the prebuilt key and
2498 * the actual vary signature (extracted from the response).
2499 * Returns 0 in case of success.
2500 */
2501static int http_request_reduce_secondary_key(unsigned int vary_signature,
2502 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
2503{
2504 int offset = 0;
2505 int global_offset = 0;
2506 int vary_info_count = 0;
2507 int keep = 0;
2508 unsigned int vary_idx;
2509 const struct vary_hashing_information *vary_info;
2510
2511 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
2512 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
2513 vary_info = &vary_information[vary_idx];
2514 keep = (vary_signature & vary_info->value) ? 0xff : 0;
2515
2516 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
2517 prebuilt_key[global_offset] &= keep;
2518 }
2519 }
2520
2521 return 0;
2522}
2523
2524
Christopher Faulet99a17a22018-12-11 09:18:27 +01002525
2526static int
2527parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
2528 struct flt_conf *fconf, char **err, void *private)
2529{
2530 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01002531 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002532 char *name = NULL;
2533 int pos = *cur_arg;
2534
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002535 /* Get the cache filter name. <pos> point on "cache" keyword */
2536 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02002537 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002538 goto error;
2539 }
2540 name = strdup(args[pos + 1]);
2541 if (!name) {
2542 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
2543 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002544 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002545 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002546
2547 /* Check if an implicit filter with the same name already exists. If so,
2548 * we remove the implicit filter to use the explicit one. */
2549 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
2550 if (f->id != cache_store_flt_id)
2551 continue;
2552
2553 cconf = f->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002554 if (strcmp(name, cconf->c.name) != 0) {
Christopher Faulet99a17a22018-12-11 09:18:27 +01002555 cconf = NULL;
2556 continue;
2557 }
2558
2559 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2560 cconf = NULL;
2561 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2562 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002563 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002564 }
2565
2566 /* Remove the implicit filter. <cconf> is kept for the explicit one */
2567 LIST_DEL(&f->list);
2568 free(f);
2569 free(name);
2570 break;
2571 }
2572
2573 /* No implicit cache filter found, create configuration for the explicit one */
2574 if (!cconf) {
2575 cconf = calloc(1, sizeof(*cconf));
2576 if (!cconf) {
2577 memprintf(err, "%s: out of memory", args[*cur_arg]);
2578 goto error;
2579 }
2580 cconf->c.name = name;
2581 }
2582
2583 cconf->flags = 0;
2584 fconf->id = cache_store_flt_id;
2585 fconf->conf = cconf;
2586 fconf->ops = &cache_ops;
2587
2588 *cur_arg = pos;
2589 return 0;
2590
2591 error:
2592 free(name);
2593 free(cconf);
2594 return -1;
2595}
2596
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002597static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002598{
2599 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2600 return 1;
2601
2602 return 0;
2603}
2604
2605static int cli_io_handler_show_cache(struct appctx *appctx)
2606{
2607 struct cache* cache = appctx->ctx.cli.p0;
2608 struct stream_interface *si = appctx->owner;
2609
William Lallemand1f49a362017-11-21 20:01:26 +01002610 if (cache == NULL) {
2611 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
2612 }
2613
2614 list_for_each_entry_from(cache, &caches, list) {
2615 struct eb32_node *node = NULL;
2616 unsigned int next_key;
2617 struct cache_entry *entry;
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002618 unsigned int i;
William Lallemand1f49a362017-11-21 20:01:26 +01002619
William Lallemand1f49a362017-11-21 20:01:26 +01002620 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002621 if (!next_key) {
2622 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
2623 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002624 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002625 return 0;
2626 }
2627 }
William Lallemand1f49a362017-11-21 20:01:26 +01002628
2629 appctx->ctx.cli.p0 = cache;
2630
2631 while (1) {
2632
2633 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002634 if (!node || (node = eb32_next_dup(node)) == NULL)
2635 node = eb32_lookup_ge(&cache->entries, next_key);
William Lallemand1f49a362017-11-21 20:01:26 +01002636 if (!node) {
2637 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02002638 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002639 break;
2640 }
2641
2642 entry = container_of(node, struct cache_entry, eb);
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002643 chunk_printf(&trash, "%p hash:%u vary:0x", entry, read_u32(entry->hash));
2644 for (i = 0; i < HTTP_CACHE_SEC_KEY_LEN; ++i)
2645 chunk_appendf(&trash, "%02x", (unsigned char)entry->secondary_key[i]);
2646 chunk_appendf(&trash, " size:%u (%u blocks), refcount:%u, expire:%d\n", block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec);
William Lallemand1f49a362017-11-21 20:01:26 +01002647
2648 next_key = node->key + 1;
2649 appctx->ctx.cli.i0 = next_key;
2650
2651 shctx_unlock(shctx_ptr(cache));
2652
2653 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002654 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01002655 return 0;
2656 }
2657 }
2658
2659 }
2660
2661 return 1;
2662
2663}
2664
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002665
2666/*
2667 * boolean, returns true if response was built out of a cache entry.
2668 */
2669static int
2670smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2671 const char *kw, void *private)
2672{
2673 smp->data.type = SMP_T_BOOL;
2674 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2675
2676 return 1;
2677}
2678
2679/*
2680 * string, returns cache name (if response came from a cache).
2681 */
2682static int
2683smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2684 const char *kw, void *private)
2685{
2686 struct appctx *appctx = NULL;
2687
2688 struct cache_flt_conf *cconf = NULL;
2689 struct cache *cache = NULL;
2690
2691 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2692 return 0;
2693
2694 /* Get appctx from the stream_interface. */
2695 appctx = si_appctx(&smp->strm->si[1]);
2696 if (appctx && appctx->rule) {
2697 cconf = appctx->rule->arg.act.p[0];
2698 if (cconf) {
2699 cache = cconf->c.cache;
2700
2701 smp->data.type = SMP_T_STR;
2702 smp->flags = SMP_F_CONST;
2703 smp->data.u.str.area = cache->id;
2704 smp->data.u.str.data = strlen(cache->id);
2705 return 1;
2706 }
2707 }
2708
2709 return 0;
2710}
2711
Christopher Faulet99a17a22018-12-11 09:18:27 +01002712/* Declare the filter parser for "cache" keyword */
2713static struct flt_kw_list filter_kws = { "CACHE", { }, {
2714 { "cache", parse_cache_flt, NULL },
2715 { NULL, NULL, NULL },
2716 }
2717};
2718
2719INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2720
William Lallemand1f49a362017-11-21 20:01:26 +01002721static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01002722 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
2723 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002724}};
2725
Willy Tarreau0108d902018-11-25 19:14:37 +01002726INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002727
William Lallemand41db4602017-10-30 11:15:51 +01002728static struct action_kw_list http_res_actions = {
2729 .kw = {
2730 { "cache-store", parse_cache_store },
2731 { NULL, NULL }
2732 }
2733};
2734
Willy Tarreau0108d902018-11-25 19:14:37 +01002735INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2736
William Lallemand41db4602017-10-30 11:15:51 +01002737static struct action_kw_list http_req_actions = {
2738 .kw = {
2739 { "cache-use", parse_cache_use },
2740 { NULL, NULL }
2741 }
2742};
2743
Willy Tarreau0108d902018-11-25 19:14:37 +01002744INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2745
Willy Tarreau2231b632019-03-29 18:26:52 +01002746struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002747 .obj_type = OBJ_TYPE_APPLET,
2748 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002749 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002750 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002751};
2752
Willy Tarreaue6552512018-11-26 11:33:13 +01002753/* config parsers for this section */
2754REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002755REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002756
2757
2758/* Note: must not be declared <const> as its list will be overwritten */
2759static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2760 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2761 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2762 { /* END */ },
2763 }
2764};
2765
2766INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);