blob: ce69af4b5fadf281f830ee7bc878a43f36fca5af [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
76typedef int(*http_header_normalizer)(struct ist value, char *buf, unsigned int *buf_len);
77
78struct vary_hashing_information {
79 struct ist hdr_name; /* Header name */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050080 enum vary_header_bit value; /* Bit representing the header in a vary signature */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +010081 unsigned int hash_length; /* Size of the sub hash for this header's value */
82 http_header_normalizer norm_fn; /* Normalization function */
83};
84
85static int accept_encoding_normalizer(struct ist value, char *buf, unsigned int *buf_len);
86static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len);
87
88/* Warning : do not forget to update HTTP_CACHE_SEC_KEY_LEN when new items are
89 * added to this array. */
90const struct vary_hashing_information vary_information[] = {
91 { IST("accept-encoding"), VARY_ACCEPT_ENCODING, sizeof(int), &accept_encoding_normalizer },
92 { IST("referer"), VARY_REFERER, sizeof(int), &default_normalizer },
93};
94
95static int http_request_prebuild_full_secondary_key(struct stream *s);
96static int http_request_build_secondary_key(struct stream *s, int vary_signature);
97static int http_request_reduce_secondary_key(unsigned int vary_signature,
98 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]);
99
100
William Lallemand41db4602017-10-30 11:15:51 +0100101/*
102 * cache ctx for filters
103 */
104struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +0100105 struct shared_block *first_block;
106};
107
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100108#define DEFAULT_MAX_SECONDARY_ENTRY 10
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100109
William Lallemand41db4602017-10-30 11:15:51 +0100110struct cache_entry {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100111 unsigned int complete; /* An entry won't be valid until complete is not null. */
William Lallemand41db4602017-10-30 11:15:51 +0100112 unsigned int latest_validation; /* latest validation date */
113 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200114 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100115
William Lallemand41db4602017-10-30 11:15:51 +0100116 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +0100117 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200118
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100119 char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */
120 unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used
121 * to build secondary keys for this cache entry. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100122 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 +0100123 unsigned int last_clear_ts; /* Timestamp of the last call to clear_expired_duplicates. */
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100124
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200125 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
126 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
127
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200128 time_t last_modified; /* Origin server "Last-Modified" header value converted in
129 * seconds since epoch. If no "Last-Modified"
130 * header is found, use "Date" header value,
131 * otherwise use reception time. This field will
132 * be used in case of an "If-Modified-Since"-based
133 * conditional request. */
134
William Lallemand41db4602017-10-30 11:15:51 +0100135 unsigned char data[0];
136};
137
138#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +0100139#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100140
141static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +0200142static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +0100143static struct cache *tmp_cache_config = NULL;
144
Willy Tarreau8ceae722018-11-26 11:58:30 +0100145DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
146
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100147static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry);
148static void delete_entry(struct cache_entry *del_entry);
149
William Lallemandf528fff2017-11-23 19:43:17 +0100150struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100151{
152 struct eb32_node *node;
153 struct cache_entry *entry;
154
Willy Tarreau8b507582020-02-25 09:35:07 +0100155 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100156 if (!node)
157 return NULL;
158
159 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100160
161 /* if that's not the right node */
162 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
163 return NULL;
164
William Lallemand08727662017-11-21 20:01:27 +0100165 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100166 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100167 } else {
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100168 delete_entry(entry);
William Lallemand08727662017-11-21 20:01:27 +0100169 entry->eb.key = 0;
170 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100171 return NULL;
172
173}
174
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100175/*
176 * There can be multiple entries with the same primary key in the ebtree so in
177 * order to get the proper one out of the list, we use a secondary_key.
178 * This function simply iterates over all the entries with the same primary_key
179 * until it finds the right one.
180 * Returns the cache_entry in case of success, NULL otherwise.
181 */
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100182struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry,
183 char *secondary_key)
184{
185 struct eb32_node *node = &entry->eb;
186
187 if (!entry->secondary_key_signature)
188 return NULL;
189
190 while (entry && memcmp(entry->secondary_key, secondary_key, HTTP_CACHE_SEC_KEY_LEN) != 0) {
191 node = eb32_next_dup(node);
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100192
193 /* Make the best use of this iteration and clear expired entries
194 * when we find them. Calling delete_entry would be too costly
195 * so we simply call eb32_delete. The secondary_entry count will
196 * be updated when we try to insert a new entry to this list. */
197 if (entry->expire <= now.tv_sec) {
198 eb32_delete(&entry->eb);
199 entry->eb.key = 0;
200 }
201
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100202 entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL;
203 }
204
205 /* Expired entry */
206 if (entry && entry->expire <= now.tv_sec) {
207 eb32_delete(&entry->eb);
208 entry->eb.key = 0;
209 entry = NULL;
210 }
211
212 return entry;
213}
214
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100215
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100216/*
217 * Remove all expired entries from a list of duplicates.
218 * Return the number of alive entries in the list and sets dup_tail to the
219 * current last item of the list.
220 */
221static unsigned int clear_expired_duplicates(struct eb32_node **dup_tail)
222{
223 unsigned int entry_count = 0;
224 struct cache_entry *entry = NULL;
225 struct eb32_node *prev = *dup_tail;
226 struct eb32_node *tail = NULL;
227
228 while (prev) {
229 entry = container_of(prev, struct cache_entry, eb);
230 prev = eb32_prev_dup(prev);
231 if (entry->expire <= now.tv_sec) {
232 eb32_delete(&entry->eb);
233 entry->eb.key = 0;
234 }
235 else {
236 if (!tail)
237 tail = &entry->eb;
238 ++entry_count;
239 }
240 }
241
242 *dup_tail = tail;
243
244 return entry_count;
245}
246
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100247
248/*
249 * This function inserts a cache_entry in the cache's ebtree. In case of
250 * duplicate entries (vary), it then checks that the number of entries did not
251 * reach the max number of secondary entries. If this entry should not have been
252 * created, remove it.
253 * In the regular case (unique entries), this function does not do more than a
254 * simple insert. In case of secondary entries, it will at most cost an
255 * insertion+max_sec_entries time checks and entry deletion.
256 * Returns the newly inserted node in case of success, NULL otherwise.
257 */
258static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry)
259{
260 struct eb32_node *prev = NULL;
261 struct cache_entry *entry = NULL;
262 unsigned int entry_count = 0;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100263 unsigned int last_clear_ts = now.tv_sec;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100264
265 struct eb32_node *node = eb32_insert(&cache->entries, &new_entry->eb);
266
267 /* We should not have multiple entries with the same primary key unless
268 * the entry has a non null vary signature. */
269 if (!new_entry->secondary_key_signature)
270 return node;
271
272 prev = eb32_prev_dup(node);
273 if (prev != NULL) {
274 /* The last entry of a duplicate list should contain the current
275 * number of entries in the list. */
276 entry = container_of(prev, struct cache_entry, eb);
277 entry_count = entry->secondary_entries_count;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100278 last_clear_ts = entry->last_clear_ts;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100279
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100280 if (entry_count >= cache->max_secondary_entries) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100281 /* Some entries of the duplicate list might be expired so
282 * we will iterate over all the items in order to free some
283 * space. In order to avoid going over the same list too
284 * often, we first check the timestamp of the last check
285 * performed. */
286 if (last_clear_ts == now.tv_sec) {
287 /* Too many entries for this primary key, clear the
288 * one that was inserted. */
289 eb32_delete(node);
290 node->key = 0;
291 return NULL;
292 }
293
294 entry_count = clear_expired_duplicates(&prev);
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +0100295 if (entry_count >= cache->max_secondary_entries) {
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100296 /* Still too many entries for this primary key, delete
297 * the newly inserted one. */
298 entry = container_of(prev, struct cache_entry, eb);
299 entry->last_clear_ts = now.tv_sec;
300 eb32_delete(node);
301 node->key = 0;
302 return NULL;
303 }
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100304 }
305 }
306
307 new_entry->secondary_entries_count = entry_count + 1;
Remi Tricot-Le Breton73be7962020-12-10 17:58:42 +0100308 new_entry->last_clear_ts = last_clear_ts;
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100309
310 return node;
311}
312
313
314/*
315 * This function removes an entry from the ebtree. If the entry was a duplicate
316 * (in case of Vary), it updates the secondary entry counter in another
317 * duplicate entry (the last entry of the dup list).
318 */
319static void delete_entry(struct cache_entry *del_entry)
320{
321 struct eb32_node *prev = NULL, *next = NULL;
322 struct cache_entry *entry = NULL;
323 struct eb32_node *last = NULL;
324
325 if (del_entry->secondary_key_signature) {
326 next = &del_entry->eb;
327
328 /* Look for last entry of the duplicates list. */
329 while ((next = eb32_next_dup(next))) {
330 last = next;
331 }
332
333 if (last) {
334 entry = container_of(last, struct cache_entry, eb);
335 --entry->secondary_entries_count;
336 }
337 else {
338 /* The current entry is the last one, look for the
339 * previous one to update its counter. */
340 prev = eb32_prev_dup(&del_entry->eb);
341 if (prev) {
342 entry = container_of(prev, struct cache_entry, eb);
343 entry->secondary_entries_count = del_entry->secondary_entries_count - 1;
344 }
345 }
346 }
347 eb32_delete(&del_entry->eb);
348 del_entry->eb.key = 0;
349}
350
351
William Lallemand4da3f8a2017-10-31 14:33:34 +0100352static inline struct shared_context *shctx_ptr(struct cache *cache)
353{
354 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
355}
356
William Lallemand77c11972017-10-31 20:43:01 +0100357static inline struct shared_block *block_ptr(struct cache_entry *entry)
358{
359 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
360}
361
362
363
William Lallemand41db4602017-10-30 11:15:51 +0100364static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100365cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100366{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100367 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100368 return 0;
369}
370
Christopher Faulet95220e22018-12-07 17:34:39 +0100371static void
372cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
373{
374 struct cache_flt_conf *cconf = fconf->conf;
375
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200376 if (!(cconf->flags & CACHE_FLT_INIT))
377 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100378 free(cconf);
379}
380
William Lallemand4da3f8a2017-10-31 14:33:34 +0100381static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100382cache_store_check(struct proxy *px, struct flt_conf *fconf)
383{
384 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100385 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100386 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100387 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100388
William Lallemandd1d1e222019-08-28 15:22:49 +0200389 /* Find the cache corresponding to the name in the filter config. The
390 * cache will not be referenced now in the filter config because it is
391 * not fully allocated. This step will be performed during the cache
392 * post_check.
393 */
394 list_for_each_entry(cache, &caches_config, list) {
395 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100396 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100397 }
398
399 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
400 proxy_type_str(px), px->id, (char *)cconf->c.name);
401 return 1;
402
403 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100404 /* Here <cache> points on the cache the filter must use and <cconf>
405 * points on the cache filter configuration. */
406
407 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100408 * enabled and if it is after the cache. When the compression is before
409 * the cache, an error is returned. Also check if the cache filter must
410 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100411 list_for_each_entry(f, &px->filter_configs, list) {
412 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100413 /* The compression filter must be evaluated after the cache. */
414 if (comp) {
415 ha_alert("config: %s '%s': unable to enable the compression filter before "
416 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
417 return 1;
418 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100419 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200420 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100421 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200422 else if (f->id == fcgi_flt_id)
423 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100424 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
425 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200426 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100427 * declaration is required. */
428 ha_alert("config: %s '%s': require an explicit filter declaration "
429 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
430 return 1;
431 }
432
Christopher Fauletafd819c2018-12-11 08:57:45 +0100433 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100434 return 0;
435}
436
437static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100438cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100439{
Christopher Faulet65554e12020-03-06 14:52:06 +0100440 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100441
Christopher Faulet65554e12020-03-06 14:52:06 +0100442 st = pool_alloc_dirty(pool_head_cache_st);
443 if (st == NULL)
444 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100445
Christopher Faulet65554e12020-03-06 14:52:06 +0100446 st->first_block = NULL;
447 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100448
Christopher Faulet65554e12020-03-06 14:52:06 +0100449 /* Register post-analyzer on AN_RES_WAIT_HTTP */
450 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100451 return 1;
452}
453
Christopher Faulet65554e12020-03-06 14:52:06 +0100454static void
455cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100456{
457 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100458 struct cache_flt_conf *cconf = FLT_CONF(filter);
459 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100460 struct shared_context *shctx = shctx_ptr(cache);
461
William Lallemand49dc0482017-11-24 14:33:54 +0100462 /* Everything should be released in the http_end filter, but we need to do it
463 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100464 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100465 shctx_lock(shctx);
466 shctx_row_dec_hot(shctx, st->first_block);
467 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100468 }
469 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100470 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100471 filter->ctx = NULL;
472 }
William Lallemand49dc0482017-11-24 14:33:54 +0100473}
474
Christopher Faulet839791a2019-01-07 16:12:07 +0100475static int
476cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
477 unsigned an_bit)
478{
479 struct http_txn *txn = s->txn;
480 struct http_msg *msg = &txn->rsp;
481 struct cache_st *st = filter->ctx;
482
483 if (an_bit != AN_RES_WAIT_HTTP)
484 goto end;
485
486 /* Here we need to check if any compression filter precedes the cache
487 * filter. This is only possible when the compression is configured in
488 * the frontend while the cache filter is configured on the
489 * backend. This case cannot be detected during HAProxy startup. So in
490 * such cases, the cache is disabled.
491 */
492 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
493 pool_free(pool_head_cache_st, st);
494 filter->ctx = NULL;
495 }
496
497 end:
498 return 1;
499}
William Lallemand49dc0482017-11-24 14:33:54 +0100500
501static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
503{
504 struct cache_st *st = filter->ctx;
505
William Lallemand4da3f8a2017-10-31 14:33:34 +0100506 if (!(msg->chn->flags & CF_ISRESP) || !st)
507 return 1;
508
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200509 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100510 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100511 return 1;
512}
513
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200514static inline void disable_cache_entry(struct cache_st *st,
515 struct filter *filter, struct shared_context *shctx)
516{
517 struct cache_entry *object;
518
519 object = (struct cache_entry *)st->first_block->data;
520 filter->ctx = NULL; /* disable cache */
521 shctx_lock(shctx);
522 shctx_row_dec_hot(shctx, st->first_block);
Remi Tricot-Le Breton964caaf2020-12-15 14:30:12 +0100523 eb32_delete(&object->eb);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200524 object->eb.key = 0;
525 shctx_unlock(shctx);
526 pool_free(pool_head_cache_st, st);
527}
528
William Lallemand4da3f8a2017-10-31 14:33:34 +0100529static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100530cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
531 unsigned int offset, unsigned int len)
532{
Christopher Faulet95220e22018-12-07 17:34:39 +0100533 struct cache_flt_conf *cconf = FLT_CONF(filter);
534 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100535 struct cache_st *st = filter->ctx;
536 struct htx *htx = htxbuf(&msg->chn->buf);
537 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200538 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100539 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200540 unsigned int orig_len, to_forward;
541 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100542
543 if (!len)
544 return len;
545
546 if (!st->first_block) {
547 unregister_data_filter(s, msg->chn, filter);
548 return len;
549 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100550
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200551 chunk_reset(&trash);
552 orig_len = len;
553 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100554
555 htxret = htx_find_offset(htx, offset);
556 blk = htxret.blk;
557 offset = htxret.ret;
558 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100559 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200560 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100561 struct ist v;
562
563 switch (type) {
564 case HTX_BLK_UNUSED:
565 break;
566
567 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100568 v = htx_get_blk_value(htx, blk);
569 v.ptr += offset;
570 v.len -= offset;
571 if (v.len > len)
572 v.len = len;
573
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200574 info = (type << 28) + v.len;
575 chunk_memcat(&trash, (char *)&info, sizeof(info));
576 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100577 to_forward += v.len;
578 len -= v.len;
579 break;
580
581 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200582 /* Here offset must always be 0 because only
583 * DATA blocks can be partially transferred. */
584 if (offset)
585 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100586 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200587 goto end;
588
589 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
590 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100591 to_forward += sz;
592 len -= sz;
593 break;
594 }
595
596 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100597 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200598
599 end:
600 shctx_lock(shctx);
601 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
602 if (!fb) {
603 shctx_unlock(shctx);
604 goto no_cache;
605 }
606 shctx_unlock(shctx);
607
608 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
609 (unsigned char *)b_head(&trash), b_data(&trash));
610 if (ret < 0)
611 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100612
613 return to_forward;
614
615 no_cache:
616 disable_cache_entry(st, filter, shctx);
617 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200618 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100619}
620
621static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100622cache_store_http_end(struct stream *s, struct filter *filter,
623 struct http_msg *msg)
624{
625 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100626 struct cache_flt_conf *cconf = FLT_CONF(filter);
627 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100628 struct shared_context *shctx = shctx_ptr(cache);
629 struct cache_entry *object;
630
631 if (!(msg->chn->flags & CF_ISRESP))
632 return 1;
633
634 if (st && st->first_block) {
635
636 object = (struct cache_entry *)st->first_block->data;
637
William Lallemand4da3f8a2017-10-31 14:33:34 +0100638 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100639 /* The whole payload was cached, the entry can now be used. */
640 object->complete = 1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100641 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100642 shctx_row_dec_hot(shctx, st->first_block);
643 shctx_unlock(shctx);
644
645 }
646 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100647 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100648 filter->ctx = NULL;
649 }
650
651 return 1;
652}
653
654 /*
655 * This intends to be used when checking HTTP headers for some
656 * word=value directive. Return a pointer to the first character of value, if
657 * the word was not found or if there wasn't any value assigned ot it return NULL
658 */
659char *directive_value(const char *sample, int slen, const char *word, int wlen)
660{
661 int st = 0;
662
663 if (slen < wlen)
664 return 0;
665
666 while (wlen) {
667 char c = *sample ^ *word;
668 if (c && c != ('A' ^ 'a'))
669 return NULL;
670 sample++;
671 word++;
672 slen--;
673 wlen--;
674 }
675
676 while (slen) {
677 if (st == 0) {
678 if (*sample != '=')
679 return NULL;
680 sample++;
681 slen--;
682 st = 1;
683 continue;
684 } else {
685 return (char *)sample;
686 }
687 }
688
689 return NULL;
690}
691
692/*
693 * Return the maxage in seconds of an HTTP response.
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100694 * The returned value will always take the cache's configuration into account
695 * (cache->maxage) but the actual max age of the response will be set in the
696 * true_maxage parameter. It will be used to determine if a response is already
697 * stale or not.
William Lallemand4da3f8a2017-10-31 14:33:34 +0100698 * Compute the maxage using either:
699 * - the assigned max-age of the cache
700 * - the s-maxage directive
701 * - the max-age directive
702 * - (Expires - Data) headers
703 * - the default-max-age of the cache
704 *
705 */
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100706int http_calc_maxage(struct stream *s, struct cache *cache, int *true_maxage)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100707{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200708 struct htx *htx = htxbuf(&s->res.buf);
709 struct http_hdr_ctx ctx = { .blk = NULL };
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100710 long smaxage = -1;
711 long maxage = -1;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100712 int expires = -1;
713 struct tm tm = {};
714 time_t expires_val = 0;
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100715 char *endptr = NULL;
716 int offset = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100717
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100718 /* The Cache-Control max-age and s-maxage directives should be followed by
719 * a positive numerical value (see RFC 7234#5.2.1.1). According to the
720 * specs, a sender "should not" generate a quoted-string value but we will
721 * still accept this format since it isn't strictly forbidden. */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200722 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
723 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100724
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200725 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
726 if (value) {
727 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100728
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200729 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
730 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100731 offset = (*chk->area == '"') ? 1 : 0;
732 smaxage = strtol(chk->area + offset, &endptr, 10);
733 if (unlikely(smaxage < 0 || endptr == chk->area))
734 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100735 }
736
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200737 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
738 if (value) {
739 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200740
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200741 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
742 chunk_strncat(chk, "", 1);
Remi Tricot-Le Bretonfcea3742020-12-03 18:19:30 +0100743 offset = (*chk->area == '"') ? 1 : 0;
744 maxage = strtol(chk->area + offset, &endptr, 10);
745 if (unlikely(maxage < 0 || endptr == chk->area))
746 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100747 }
748 }
749
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100750 /* Look for Expires header if no s-maxage or max-age Cache-Control data
751 * was found. */
752 if (maxage == -1 && smaxage == -1) {
753 ctx.blk = NULL;
754 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
755 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
756 expires_val = my_timegm(&tm);
757 /* A request having an expiring date earlier
758 * than the current date should be considered as
759 * stale. */
760 expires = (expires_val >= now.tv_sec) ?
761 (expires_val - now.tv_sec) : 0;
762 }
763 else {
764 /* Following RFC 7234#5.3, an invalid date
765 * format must be treated as a date in the past
766 * so the cache entry must be seen as already
767 * expired. */
768 expires = 0;
769 }
770 }
771 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100772
773
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100774 if (smaxage > 0) {
775 if (true_maxage)
776 *true_maxage = smaxage;
William Lallemand49b44532017-11-24 18:53:43 +0100777 return MIN(smaxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100778 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100779
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100780 if (maxage > 0) {
781 if (true_maxage)
782 *true_maxage = maxage;
William Lallemand49b44532017-11-24 18:53:43 +0100783 return MIN(maxage, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100784 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100785
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100786 if (expires >= 0) {
787 if (true_maxage)
788 *true_maxage = expires;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100789 return MIN(expires, cache->maxage);
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100790 }
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100791
William Lallemand49b44532017-11-24 18:53:43 +0100792 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100793
794}
795
796
William Lallemanda400a3a2017-11-20 19:13:12 +0100797static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
798{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200799 struct cache_entry *object = (struct cache_entry *)block->data;
800
801 if (first == block && object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +0100802 delete_entry(object);
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200803 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100804}
805
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200806
807/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
808 * date value should be compared to a date determined by in a previous response (for
809 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
810 * value of the response's reception time (by decreasing order of priority). */
811static time_t get_last_modified_time(struct htx *htx)
812{
813 time_t last_modified = 0;
814 struct http_hdr_ctx ctx = { .blk = NULL };
815 struct tm tm = {};
816
817 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
818 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
819 last_modified = my_timegm(&tm);
820 }
821 }
822
823 if (!last_modified) {
824 ctx.blk = NULL;
825 if (http_find_header(htx, ist("date"), &ctx, 1)) {
826 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
827 last_modified = my_timegm(&tm);
828 }
829 }
830 }
831
832 /* Fallback on the current time if no "Last-Modified" or "Date" header
833 * was found. */
834 if (!last_modified)
835 last_modified = now.tv_sec;
836
837 return last_modified;
838}
839
William Lallemand41db4602017-10-30 11:15:51 +0100840/*
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100841 * Checks the vary header's value. The headers on which vary should be applied
Ilya Shipitsinf38a0182020-12-21 01:16:17 +0500842 * must be explicitly supported in the vary_information array (see cache.c). If
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +0100843 * any other header is mentioned, we won't store the response.
844 * Returns 1 if Vary-based storage can work, 0 otherwise.
845 */
846static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature)
847{
848 unsigned int vary_idx;
849 unsigned int vary_info_count;
850 const struct vary_hashing_information *vary_info;
851 struct http_hdr_ctx ctx = { .blk = NULL };
852
853 int retval = 1;
854
855 *vary_signature = 0;
856
857 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
858 while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) {
859 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
860 vary_info = &vary_information[vary_idx];
861 if (isteqi(ctx.value, vary_info->hdr_name)) {
862 *vary_signature |= vary_info->value;
863 break;
864 }
865 }
866 retval = (vary_idx < vary_info_count);
867 }
868
869 return retval;
870}
871
872
873
874/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500875 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100876 * register a filter to store the data
877 */
878enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200879 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100880{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200881 long long hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100882 int effective_maxage = 0;
883 int true_maxage = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100884 struct http_txn *txn = s->txn;
885 struct http_msg *msg = &txn->rsp;
886 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100887 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100888 struct cache_flt_conf *cconf = rule->arg.act.p[0];
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100889 struct cache *cache = cconf->c.cache;
890 struct shared_context *shctx = shctx_ptr(cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100891 struct cache_st *cache_ctx = NULL;
892 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100893 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200894 struct htx *htx;
895 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200896 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200897 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200898 struct ist header_name = IST_NULL;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100899 unsigned int vary_signature = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100900
William Lallemand4da3f8a2017-10-31 14:33:34 +0100901 /* Don't cache if the response came from a cache */
902 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
903 s->target == &http_cache_applet.obj_type) {
904 goto out;
905 }
906
907 /* cache only HTTP/1.1 */
908 if (!(txn->req.flags & HTTP_MSGF_VER_11))
909 goto out;
910
Willy Tarreau6905d182019-10-01 17:59:17 +0200911 /* cache only GET method */
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +0100912 if (txn->meth != HTTP_METH_GET) {
913 /* In case of successful unsafe method on a stored resource, the
914 * cached entry must be invalidated (see RFC7234#4.4).
915 * A "non-error response" is one with a 2xx (Successful) or 3xx
916 * (Redirection) status code. */
917 if (txn->status >= 200 && txn->status < 400) {
918 switch (txn->meth) {
919 case HTTP_METH_OPTIONS:
920 case HTTP_METH_GET:
921 case HTTP_METH_HEAD:
922 case HTTP_METH_TRACE:
923 break;
924
925 default: /* Any unsafe method */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +0500926 /* Discard any corresponding entry in case of successful
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +0100927 * unsafe request (such as PUT, POST or DELETE). */
928 shctx_lock(shctx);
929
930 old = entry_exist(cconf->c.cache, txn->cache_hash);
931 if (old) {
932 eb32_delete(&old->eb);
933 old->eb.key = 0;
934 }
935 shctx_unlock(shctx);
936 }
937 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100938 goto out;
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +0100939 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100940
Willy Tarreauc9036c02019-01-11 19:38:25 +0100941 /* cache key was not computed */
942 if (!key)
943 goto out;
944
William Lallemand4da3f8a2017-10-31 14:33:34 +0100945 /* cache only 200 status code */
946 if (txn->status != 200)
947 goto out;
948
Christopher Faulet839791a2019-01-07 16:12:07 +0100949 /* Find the corresponding filter instance for the current stream */
950 list_for_each_entry(filter, &s->strm_flt.filters, list) {
951 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
952 /* No filter ctx, don't cache anything */
953 if (!filter->ctx)
954 goto out;
955 cache_ctx = filter->ctx;
956 break;
957 }
958 }
959
960 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200961 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100962
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200963 /* Do not cache too big objects. */
964 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
965 htx->data + htx->extra > shctx->max_obj_size)
966 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100967
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100968 /* Only a subset of headers are supported in our Vary implementation. If
969 * any other header is present in the Vary header value, we won't be
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100970 * able to use the cache. Likewise, if Vary header support is disabled,
971 * avoid caching responses that contain such a header. */
972 ctx.blk = NULL;
973 if (cache->vary_processing_enabled) {
974 if (!http_check_vary_header(htx, &vary_signature))
975 goto out;
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +0100976 if (vary_signature) {
977 /* If something went wrong during the secondary key
978 * building, do not store the response. */
979 if (!(txn->flags & TX_CACHE_HAS_SEC_KEY))
980 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100981 http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash);
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +0100982 }
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +0100983 }
984 else if (http_find_header(htx, ist("Vary"), &ctx, 0)) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200985 goto out;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +0100986 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100987
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200988 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100989
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100990 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE))
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200991 goto out;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +0100992
993 shctx_lock(shctx);
994 old = entry_exist(cache, txn->cache_hash);
995 if (old) {
996 if (vary_signature)
997 old = secondary_entry_exist(cconf->c.cache, old,
998 txn->cache_secondary_hash);
999 if (old) {
1000 if (!old->complete) {
1001 /* An entry with the same primary key is already being
1002 * created, we should not try to store the current
1003 * response because it will waste space in the cache. */
1004 shctx_unlock(shctx);
1005 goto out;
1006 }
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001007 delete_entry(old);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001008 old->eb.key = 0;
1009 }
1010 }
1011 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry));
1012 if (!first) {
1013 shctx_unlock(shctx);
1014 goto out;
1015 }
1016 /* the received memory is not initialized, we need at least to mark
1017 * the object as not indexed yet.
1018 */
1019 object = (struct cache_entry *)first->data;
1020 memset(object, 0, sizeof(*object));
1021 object->eb.key = key;
1022 object->secondary_key_signature = vary_signature;
1023 /* We need to temporarily set a valid expiring time until the actual one
1024 * is set by the end of this function (in case of concurrent accesses to
1025 * the same resource). This way the second access will find an existing
1026 * but not yet usable entry in the tree and will avoid storing its data. */
1027 object->expire = now.tv_sec + 2;
1028
1029 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
1030 if (vary_signature)
1031 memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN);
1032
1033 /* Insert the entry in the tree even if the payload is not cached yet. */
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001034 if (insert_entry(cache, object) != &object->eb) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001035 object->eb.key = 0;
1036 shctx_unlock(shctx);
1037 goto out;
1038 }
1039 shctx_unlock(shctx);
1040
1041 /* reserve space for the cache_entry structure */
1042 first->len = sizeof(struct cache_entry);
1043 first->last_append = NULL;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001044
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001045 /* Determine the entry's maximum age (taking into account the cache's
1046 * configuration) as well as the response's explicit max age (extracted
1047 * from cache-control directives or the expires header). */
1048 effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage);
1049
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001050 ctx.blk = NULL;
1051 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
1052 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
1053 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
1054 hdr_age = CACHE_ENTRY_MAX_AGE;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001055 /* A response with an Age value greater than its
1056 * announced max age is stale and should not be stored. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001057 object->age = hdr_age;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001058 if (unlikely(object->age > true_maxage))
1059 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001060 }
Remi Tricot-Le Breton51058d62020-12-03 18:19:32 +01001061 else
1062 goto out;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001063 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001064 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001065
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001066 /* Build a last-modified time that will be stored in the cache_entry and
1067 * compared to a future If-Modified-Since client header. */
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001068 object->last_modified = get_last_modified_time(htx);
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +02001069
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001070 chunk_reset(&trash);
1071 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1072 struct htx_blk *blk = htx_get_blk(htx, pos);
1073 enum htx_blk_type type = htx_get_blk_type(blk);
1074 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001075
Christopher Fauletb0667472019-09-03 22:22:12 +02001076 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001077 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
1078 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001079
1080 /* Look for optional ETag header.
1081 * We need to store the offset of the ETag value in order for
1082 * future conditional requests to be able to perform ETag
1083 * comparisons. */
1084 if (type == HTX_BLK_HDR) {
1085 header_name = htx_get_blk_name(htx, blk);
1086 if (isteq(header_name, ist("etag"))) {
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001087 object->etag_length = sz - istlen(header_name);
1088 object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +02001089 }
1090 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001091 if (type == HTX_BLK_EOH)
1092 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001093 }
1094
Christopher Fauletb0667472019-09-03 22:22:12 +02001095 /* Do not cache objects if the headers are too big. */
1096 if (hdrs_len > htx->size - global.tune.maxrewrite)
1097 goto out;
1098
William Lallemand4da3f8a2017-10-31 14:33:34 +01001099 shctx_lock(shctx);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001100 if (!shctx_row_reserve_hot(shctx, first, trash.data)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +01001101 shctx_unlock(shctx);
1102 goto out;
1103 }
1104 shctx_unlock(shctx);
1105
William Lallemand4da3f8a2017-10-31 14:33:34 +01001106 /* cache the headers in a http action because it allows to chose what
1107 * to cache, for example you might want to cache a response before
1108 * modifying some HTTP headers, or on the contrary after modifying
1109 * those headers.
1110 */
William Lallemand4da3f8a2017-10-31 14:33:34 +01001111 /* does not need to be locked because it's in the "hot" list,
1112 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001113 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
1114 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001115
1116 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +01001117 if (cache_ctx) {
1118 cache_ctx->first_block = first;
Christopher Faulet839791a2019-01-07 16:12:07 +01001119 /* store latest value and expiration time */
1120 object->latest_validation = now.tv_sec;
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01001121 object->expire = now.tv_sec + effective_maxage;
Christopher Faulet839791a2019-01-07 16:12:07 +01001122 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001123 }
1124
1125out:
1126 /* if does not cache */
1127 if (first) {
1128 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +01001129 first->len = 0;
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001130 if (object->eb.key)
Remi Tricot-Le Breton65904e42020-12-10 17:58:41 +01001131 delete_entry(object);
William Lallemand08727662017-11-21 20:01:27 +01001132 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +01001133 shctx_row_dec_hot(shctx, first);
1134 shctx_unlock(shctx);
1135 }
1136
William Lallemand41db4602017-10-30 11:15:51 +01001137 return ACT_RET_CONT;
1138}
1139
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001140#define HTX_CACHE_INIT 0 /* Initial state. */
1141#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
1142#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001143#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
1144#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001145
William Lallemandecb73b12017-11-24 14:33:55 +01001146static void http_cache_applet_release(struct appctx *appctx)
1147{
Christopher Faulet95220e22018-12-07 17:34:39 +01001148 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +01001149 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +01001150 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +01001151 struct shared_block *first = block_ptr(cache_ptr);
1152
1153 shctx_lock(shctx_ptr(cache));
1154 shctx_row_dec_hot(shctx_ptr(cache), first);
1155 shctx_unlock(shctx_ptr(cache));
1156}
1157
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001158
1159static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
1160 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001161{
Christopher Faulet95220e22018-12-07 17:34:39 +01001162 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1163 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001164 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001165 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001166 unsigned int max, total;
1167 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001168
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001169 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1170 if (!max)
1171 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001172 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001173 ? (info & 0xff) + ((info >> 8) & 0xfffff)
1174 : info & 0xfffffff);
1175 if (blksz > max)
1176 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001177
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001178 blk = htx_add_blk(htx, type, blksz);
1179 if (!blk)
1180 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001181
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001182 blk->info = info;
1183 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001184 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001185 while (blksz) {
1186 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001187 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001188 offset += max;
1189 blksz -= max;
1190 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +02001191 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001192 if (blksz || offset == shctx->block_size) {
1193 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1194 offset = 0;
1195 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001196 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001197 appctx->ctx.cache.offset = offset;
1198 appctx->ctx.cache.next = shblk;
1199 appctx->ctx.cache.sent += total;
1200 return total;
1201}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001202
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001203static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
1204 uint32_t info, struct shared_block *shblk, unsigned int offset)
1205{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001206
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001207 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1208 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
1209 unsigned int max, total, rem_data;
1210 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001211
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001212 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
1213 if (!max)
1214 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001215
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001216 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001217 if (appctx->ctx.cache.rem_data) {
1218 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001219 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +02001220 }
1221 else {
1222 blksz = (info & 0xfffffff);
1223 total = 4;
1224 }
1225 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001226 rem_data = blksz - max;
1227 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001228 }
1229
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001230 while (blksz) {
1231 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001232
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001233 max = MIN(blksz, shctx->block_size - offset);
1234 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
1235 offset += sz;
1236 blksz -= sz;
1237 total += sz;
1238 if (sz < max)
1239 break;
1240 if (blksz || offset == shctx->block_size) {
1241 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1242 offset = 0;
1243 }
1244 }
1245
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001246 appctx->ctx.cache.offset = offset;
1247 appctx->ctx.cache.next = shblk;
1248 appctx->ctx.cache.sent += total;
1249 appctx->ctx.cache.rem_data = rem_data + blksz;
1250 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001251}
1252
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001253static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1254 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001255{
Christopher Faulet95220e22018-12-07 17:34:39 +01001256 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1257 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001258 struct shared_block *shblk;
1259 unsigned int offset, sz;
1260 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001261
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001262 while (len) {
1263 enum htx_blk_type type;
1264 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001265
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001266 shblk = appctx->ctx.cache.next;
1267 offset = appctx->ctx.cache.offset;
1268 if (appctx->ctx.cache.rem_data) {
1269 type = HTX_BLK_DATA;
1270 info = 0;
1271 goto add_data_blk;
1272 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001273
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001274 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001275 sz = MIN(4, shctx->block_size - offset);
1276 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1277 offset += sz;
1278 if (sz < 4) {
1279 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1280 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1281 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001282 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001283
1284 /* Get payload of the next HTX block and insert it. */
1285 type = (info >> 28);
1286 if (type != HTX_BLK_DATA)
1287 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1288 else {
1289 add_data_blk:
1290 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001291 }
1292
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001293 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001294 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001295 total += ret;
1296 len -= ret;
1297
1298 if (appctx->ctx.cache.rem_data || type == mark)
1299 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001300 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001301
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001302 return total;
1303}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001304
1305static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1306{
1307 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1308 unsigned int age;
1309 char *end;
1310
1311 chunk_reset(&trash);
1312 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1313 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1314 age = CACHE_ENTRY_MAX_AGE;
1315 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1316 b_set_data(&trash, end - b_head(&trash));
1317 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1318 return 0;
1319 return 1;
1320}
1321
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001322static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001323{
1324 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1325 struct shared_block *first = block_ptr(cache_ptr);
1326 struct stream_interface *si = appctx->owner;
1327 struct channel *req = si_oc(si);
1328 struct channel *res = si_ic(si);
1329 struct htx *req_htx, *res_htx;
1330 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001331 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001332 size_t ret, total = 0;
1333
1334 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001335 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001336
1337 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1338 goto out;
1339
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001340 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001341 if (!b_size(&res->buf)) {
1342 si_rx_room_blk(si);
1343 goto out;
1344 }
1345
Willy Tarreauefef3232018-12-16 00:37:45 +01001346 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001347 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001348
1349 if (appctx->st0 == HTX_CACHE_INIT) {
1350 appctx->ctx.cache.next = block_ptr(cache_ptr);
1351 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1352 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001353 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001354 appctx->st0 = HTX_CACHE_HEADER;
1355 }
1356
1357 if (appctx->st0 == HTX_CACHE_HEADER) {
1358 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001359 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1360 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1361 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1362 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001363 goto error;
1364
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001365 /* In case of a conditional request, we might want to send a
1366 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001367 if (appctx->ctx.cache.send_notmodified) {
1368 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1369 /* If replacing the status code fails we need to send the full response. */
1370 appctx->ctx.cache.send_notmodified = 0;
1371 }
1372 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001373
1374 /* Skip response body for HEAD requests or in case of "304 Not
1375 * Modified" response. */
1376 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001377 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001378 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001379 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001380 }
1381
1382 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001383 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1384 if (len) {
1385 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1386 if (ret < len) {
1387 si_rx_room_blk(si);
1388 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001389 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001390 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001391 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001392 }
1393
1394 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001395 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001396 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1397 si_rx_room_blk(si);
1398 goto out;
1399 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001400 appctx->st0 = HTX_CACHE_END;
1401 }
1402
1403 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001404 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001405 res->flags |= CF_READ_NULL;
1406 si_shutr(si);
1407 }
1408
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001409 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001410 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001411 if (total)
1412 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001413 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001414
1415 /* eat the whole request */
1416 if (co_data(req)) {
1417 req_htx = htx_from_buf(&req->buf);
1418 co_htx_skip(req, req_htx, co_data(req));
1419 htx_to_buf(req_htx, &req->buf);
1420 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001421 return;
1422
1423 error:
1424 /* Sent and HTTP error 500 */
1425 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001426 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001427 res->buf.data = b_data(errmsg);
1428 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1429 res_htx = htx_from_buf(&res->buf);
1430
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001431 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001432 appctx->st0 = HTX_CACHE_END;
1433 goto end;
1434}
1435
1436
Christopher Faulet95220e22018-12-07 17:34:39 +01001437static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001438{
1439 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001440 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001441
Christopher Faulet95220e22018-12-07 17:34:39 +01001442 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001443 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001444 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001445 }
1446
1447 /* check if a cache filter was already registered with this cache
1448 * name, if that's the case, must use it. */
1449 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001450 if (fconf->id == cache_store_flt_id) {
1451 cconf = fconf->conf;
1452 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1453 rule->arg.act.p[0] = cconf;
1454 return 1;
1455 }
William Lallemand41db4602017-10-30 11:15:51 +01001456 }
1457 }
1458
Christopher Faulet95220e22018-12-07 17:34:39 +01001459 /* Create the filter cache config */
1460 cconf = calloc(1, sizeof(*cconf));
1461 if (!cconf) {
1462 memprintf(err, "out of memory\n");
1463 goto err;
1464 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001465 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001466 cconf->c.name = strdup(name);
1467 if (!cconf->c.name) {
1468 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001469 goto err;
1470 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001471
William Lallemand41db4602017-10-30 11:15:51 +01001472 /* register a filter to fill the cache buffer */
1473 fconf = calloc(1, sizeof(*fconf));
1474 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001475 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001476 goto err;
1477 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001478 fconf->id = cache_store_flt_id;
1479 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001480 fconf->ops = &cache_ops;
1481 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1482
Christopher Faulet95220e22018-12-07 17:34:39 +01001483 rule->arg.act.p[0] = cconf;
1484 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001485
Christopher Faulet95220e22018-12-07 17:34:39 +01001486 err:
1487 free(cconf);
1488 return 0;
1489}
1490
1491enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1492 struct act_rule *rule, char **err)
1493{
1494 rule->action = ACT_CUSTOM;
1495 rule->action_ptr = http_action_store_cache;
1496
1497 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1498 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001499
Christopher Faulet95220e22018-12-07 17:34:39 +01001500 (*orig_arg)++;
1501 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001502}
1503
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001504/* This produces a sha1 hash of the concatenation of the HTTP method,
1505 * the first occurrence of the Host header followed by the path component
1506 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001507int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001508{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001509 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001510 struct htx *htx = htxbuf(&s->req.buf);
1511 struct htx_sl *sl;
1512 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001513 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001514 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001515 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001516
William Lallemandf528fff2017-11-23 19:43:17 +01001517 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001518 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001519
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001520 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001521 uri = htx_sl_req_uri(sl); // whole uri
1522 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001523 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001524
1525 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1526 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1527 * URIs are almost always sent in absolute form with their scheme. In
1528 * this case, the scheme is almost always "https". In order to support
1529 * sharing of cache objects between H1 and H2, we'll hash the absolute
1530 * URI whenever known, or prepend "https://" + the Host header for
1531 * relative URIs. The difference will only appear on absolute HTTP/1
1532 * requests sent to an origin server, which practically is never met in
1533 * the real world so we don't care about the ability to share the same
1534 * key here.URIs are normalized from the absolute URI to an origin form as
1535 * well.
1536 */
1537 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001538 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001539 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1540 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001541 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001542 }
1543
1544 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001545
1546 /* hash everything */
1547 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001548 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001549 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1550
1551 return 1;
1552}
1553
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001554/* Looks for "If-None-Match" headers in the request and compares their value
1555 * with the one that might have been stored in the cache_entry. If any of them
1556 * matches, a "304 Not Modified" response should be sent instead of the cached
1557 * data.
1558 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001559 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1560 *
1561 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1562 * header and compare its value (date) to the one stored in the cache_entry.
1563 * If the request's date is later than the cached one, we also send a
1564 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1565 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001566 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1567 */
1568static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1569 struct cache_entry *entry)
1570{
1571 int retval = 0;
1572
1573 struct http_hdr_ctx ctx = { .blk = NULL };
1574 struct ist cache_entry_etag = IST_NULL;
1575 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001576 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001577
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001578 struct tm tm = {};
1579 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001580
1581 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001582 * cache_entry's ETag in order to perform comparisons.
1583 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001584 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001585 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001586
1587 /* A '*' matches everything. */
1588 if (isteq(ctx.value, ist("*")) != 0) {
1589 retval = 1;
1590 break;
1591 }
1592
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001593 /* No need to rebuild an etag if none was stored in the cache. */
1594 if (entry->etag_length == 0)
1595 break;
1596
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001597 /* Rebuild the stored ETag. */
1598 if (etag_buffer == NULL) {
1599 etag_buffer = get_trash_chunk();
1600
1601 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1602 (unsigned char*)b_orig(etag_buffer),
1603 entry->etag_offset, entry->etag_length) == 0) {
1604 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1605 } else {
1606 /* We could not rebuild the ETag in one go, we
1607 * won't send a "304 Not Modified" response. */
1608 break;
1609 }
1610 }
1611
1612 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1613 retval = 1;
1614 break;
1615 }
1616 }
1617
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001618 /* If the request did not contain an "If-None-Match" header, we look for
1619 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1620 if (retval == 0 && if_none_match_found == 0) {
1621 ctx.blk = NULL;
1622 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1623 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1624 if_modified_since = my_timegm(&tm);
1625
1626 /* We send a "304 Not Modified" response if the
1627 * entry's last modified date is earlier than
1628 * the one found in the "If-Modified-Since"
1629 * header. */
1630 retval = (entry->last_modified <= if_modified_since);
1631 }
1632 }
1633 }
1634
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001635 return retval;
1636}
1637
William Lallemand41db4602017-10-30 11:15:51 +01001638enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1639 struct session *sess, struct stream *s, int flags)
1640{
William Lallemand77c11972017-10-31 20:43:01 +01001641
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001642 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001643 struct cache_entry *res, *sec_entry = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001644 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1645 struct cache *cache = cconf->c.cache;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001646 struct shared_block *entry_block;
1647
William Lallemand77c11972017-10-31 20:43:01 +01001648
Willy Tarreau6905d182019-10-01 17:59:17 +02001649 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1650 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001651 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001652 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001653 txn->flags |= TX_CACHE_IGNORE;
1654
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001655 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001656
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001657 /* The request's hash has to be calculated for all requests, even POSTs
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001658 * or PUTs for instance because RFC7234 specifies that a successful
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001659 * "unsafe" method on a stored resource must invalidate it
1660 * (see RFC7234#4.4). */
1661 if (!sha1_hosturi(s))
Willy Tarreau504455c2017-12-22 17:47:35 +01001662 return ACT_RET_CONT;
1663
Remi Tricot-Le Breton72cffaf2020-12-03 18:19:31 +01001664 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001665 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001666
Willy Tarreau504455c2017-12-22 17:47:35 +01001667 if (s->txn->flags & TX_CACHE_IGNORE)
1668 return ACT_RET_CONT;
1669
Willy Tarreaua1214a52018-12-14 14:00:25 +01001670 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001671 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001672 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001673 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001674
William Lallemanda400a3a2017-11-20 19:13:12 +01001675 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001676 res = entry_exist(cache, s->txn->cache_hash);
Remi Tricot-Le Breton32434472020-11-25 10:09:43 +01001677 /* We must not use an entry that is not complete. */
1678 if (res && res->complete) {
William Lallemand77c11972017-10-31 20:43:01 +01001679 struct appctx *appctx;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001680 entry_block = block_ptr(res);
1681 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
William Lallemanda400a3a2017-11-20 19:13:12 +01001682 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001683
1684 /* In case of Vary, we could have multiple entries with the same
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01001685 * primary hash. We need to calculate the secondary hash in order
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001686 * to find the actual entry we want (if it exists). */
1687 if (res->secondary_key_signature) {
1688 if (!http_request_build_secondary_key(s, res->secondary_key_signature)) {
1689 shctx_lock(shctx_ptr(cache));
1690 sec_entry = secondary_entry_exist(cache, res,
1691 s->txn->cache_secondary_hash);
1692 if (sec_entry && sec_entry != res) {
1693 /* The wrong row was added to the hot list. */
1694 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1695 entry_block = block_ptr(sec_entry);
1696 shctx_row_inc_hot(shctx_ptr(cache), entry_block);
1697 }
1698 res = sec_entry;
1699 shctx_unlock(shctx_ptr(cache));
1700 }
1701 else
1702 res = NULL;
1703 }
1704
1705 /* We looked for a valid secondary entry and could not find one,
1706 * the request must be forwarded to the server. */
1707 if (!res) {
1708 shctx_lock(shctx_ptr(cache));
1709 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
1710 shctx_unlock(shctx_ptr(cache));
1711 return ACT_RET_CONT;
1712 }
1713
William Lallemand77c11972017-10-31 20:43:01 +01001714 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001715 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001716 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001717 appctx->rule = rule;
1718 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001719 appctx->ctx.cache.next = NULL;
1720 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001721 appctx->ctx.cache.send_notmodified =
1722 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001723
1724 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001725 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001726 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001727 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001728 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001729 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001730 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001731 shctx_row_dec_hot(shctx_ptr(cache), entry_block);
William Lallemand55e76742017-11-21 20:01:28 +01001732 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001733 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001734 }
1735 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001736 shctx_unlock(shctx_ptr(cache));
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001737
1738 /* Shared context does not need to be locked while we calculate the
1739 * secondary hash. */
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001740 if (!res && cache->vary_processing_enabled) {
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001741 /* Build a complete secondary hash until the server response
1742 * tells us which fields should be kept (if any). */
1743 http_request_prebuild_full_secondary_key(s);
1744 }
Olivier Houchardfccf8402017-11-01 14:04:02 +01001745 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001746}
1747
1748
1749enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1750 struct act_rule *rule, char **err)
1751{
William Lallemand41db4602017-10-30 11:15:51 +01001752 rule->action = ACT_CUSTOM;
1753 rule->action_ptr = http_action_req_cache_use;
1754
Christopher Faulet95220e22018-12-07 17:34:39 +01001755 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001756 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001757
1758 (*orig_arg)++;
1759 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001760}
1761
1762int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1763{
1764 int err_code = 0;
1765
1766 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1767
1768 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001769 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001770 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001771 err_code |= ERR_ALERT | ERR_ABORT;
1772 goto out;
1773 }
1774
1775 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1776 err_code |= ERR_ABORT;
1777 goto out;
1778 }
1779
1780 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001781 struct cache *cache_config;
1782
William Lallemand41db4602017-10-30 11:15:51 +01001783 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1784 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001785 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001786 err_code |= ERR_ALERT | ERR_ABORT;
1787 goto out;
1788 }
1789
1790 strlcpy2(tmp_cache_config->id, args[1], 33);
1791 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001792 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001793 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001794 err_code |= ERR_WARN;
1795 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001796
1797 list_for_each_entry(cache_config, &caches_config, list) {
1798 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1799 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1800 file, linenum, tmp_cache_config->id);
1801 err_code |= ERR_ALERT | ERR_ABORT;
1802 goto out;
1803 }
1804 }
1805
William Lallemand49b44532017-11-24 18:53:43 +01001806 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001807 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001808 tmp_cache_config->maxobjsz = 0;
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +01001809 tmp_cache_config->max_secondary_entries = DEFAULT_MAX_SECONDARY_ENTRY;
William Lallemand41db4602017-10-30 11:15:51 +01001810 }
1811 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001812 unsigned long int maxsize;
1813 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001814
1815 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1816 err_code |= ERR_ABORT;
1817 goto out;
1818 }
1819
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001820 maxsize = strtoul(args[1], &err, 10);
1821 if (err == args[1] || *err != '\0') {
1822 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1823 file, linenum, args[1]);
1824 err_code |= ERR_ABORT;
1825 goto out;
1826 }
1827
1828 if (maxsize > (UINT_MAX >> 20)) {
1829 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1830 file, linenum, args[1], UINT_MAX >> 20);
1831 err_code |= ERR_ABORT;
1832 goto out;
1833 }
1834
William Lallemand41db4602017-10-30 11:15:51 +01001835 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001836 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001837 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001838 } else if (strcmp(args[0], "max-age") == 0) {
1839 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1840 err_code |= ERR_ABORT;
1841 goto out;
1842 }
1843
1844 if (!*args[1]) {
1845 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1846 file, linenum, args[0]);
1847 err_code |= ERR_WARN;
1848 }
1849
1850 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001851 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001852 unsigned int maxobjsz;
1853 char *err;
1854
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001855 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1856 err_code |= ERR_ABORT;
1857 goto out;
1858 }
1859
1860 if (!*args[1]) {
1861 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1862 file, linenum, args[0]);
1863 err_code |= ERR_WARN;
1864 }
1865
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001866 maxobjsz = strtoul(args[1], &err, 10);
1867 if (err == args[1] || *err != '\0') {
1868 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1869 file, linenum, args[1]);
1870 err_code |= ERR_ABORT;
1871 goto out;
1872 }
1873 tmp_cache_config->maxobjsz = maxobjsz;
Remi Tricot-Le Breton754b2422020-11-16 15:56:10 +01001874 } else if (strcmp(args[0], "process-vary") == 0) {
1875 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1876 err_code |= ERR_ABORT;
1877 goto out;
1878 }
1879
1880 if (!*args[1]) {
1881 ha_warning("parsing [%s:%d]: '%s' expects 0 or 1 (disable or enable vary processing).\n",
1882 file, linenum, args[0]);
1883 err_code |= ERR_WARN;
1884 }
1885
1886 tmp_cache_config->vary_processing_enabled = atoi(args[1]);
Remi Tricot-Le Breton5853c0c2020-12-10 17:58:43 +01001887 } else if (strcmp(args[0], "max-secondary-entries") == 0) {
1888 unsigned int max_sec_entries;
1889 char *err;
1890
1891 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1892 err_code |= ERR_ABORT;
1893 goto out;
1894 }
1895
1896 if (!*args[1]) {
1897 ha_warning("parsing [%s:%d]: '%s' expects a strictly positive number.\n",
1898 file, linenum, args[0]);
1899 err_code |= ERR_WARN;
1900 }
1901
1902 max_sec_entries = strtoul(args[1], &err, 10);
1903 if (err == args[1] || *err != '\0' || max_sec_entries == 0) {
1904 ha_warning("parsing [%s:%d]: max-secondary-entries wrong value '%s'\n",
1905 file, linenum, args[1]);
1906 err_code |= ERR_ABORT;
1907 goto out;
1908 }
1909 tmp_cache_config->max_secondary_entries = max_sec_entries;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001910 }
1911 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001912 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001913 err_code |= ERR_ALERT | ERR_FATAL;
1914 goto out;
1915 }
1916out:
1917 return err_code;
1918}
1919
1920/* once the cache section is parsed */
1921
1922int cfg_post_parse_section_cache()
1923{
William Lallemand41db4602017-10-30 11:15:51 +01001924 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001925
1926 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001927
1928 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001929 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001930 err_code |= ERR_FATAL | ERR_ALERT;
1931 goto out;
1932 }
1933
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001934 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001935 /* Default max. file size is a 256th of the cache size. */
1936 tmp_cache_config->maxobjsz =
1937 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001938 }
1939 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1940 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1941 err_code |= ERR_FATAL | ERR_ALERT;
1942 goto out;
1943 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001944
William Lallemandd1d1e222019-08-28 15:22:49 +02001945 /* add to the list of cache to init and reinit tmp_cache_config
1946 * for next cache section, if any.
1947 */
1948 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1949 tmp_cache_config = NULL;
1950 return err_code;
1951 }
1952out:
1953 free(tmp_cache_config);
1954 tmp_cache_config = NULL;
1955 return err_code;
1956
1957}
1958
1959int post_check_cache()
1960{
1961 struct proxy *px;
1962 struct cache *back, *cache_config, *cache;
1963 struct shared_context *shctx;
1964 int ret_shctx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001965 int err_code = ERR_NONE;
William Lallemandd1d1e222019-08-28 15:22:49 +02001966
1967 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1968
1969 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1970 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001971
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001972 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001973 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001974 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001975 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001976 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001977
1978 err_code |= ERR_FATAL | ERR_ALERT;
1979 goto out;
1980 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001981 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001982 /* the cache structure is stored in the shctx and added to the
1983 * caches list, we can remove the entry from the caches_config
1984 * list */
1985 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001986 cache = (struct cache *)shctx->data;
Remi Tricot-Le Breton1785f3d2020-11-16 15:56:09 +01001987 cache->entries = EB_ROOT;
William Lallemand41db4602017-10-30 11:15:51 +01001988 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001989 LIST_DEL(&cache_config->list);
1990 free(cache_config);
1991
1992 /* Find all references for this cache in the existing filters
1993 * (over all proxies) and reference it in matching filters.
1994 */
1995 for (px = proxies_list; px; px = px->next) {
1996 struct flt_conf *fconf;
1997 struct cache_flt_conf *cconf;
1998
1999 list_for_each_entry(fconf, &px->filter_configs, list) {
2000 if (fconf->id != cache_store_flt_id)
2001 continue;
2002
2003 cconf = fconf->conf;
2004 if (!strcmp(cache->id, cconf->c.name)) {
2005 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02002006 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02002007 cconf->c.cache = cache;
2008 break;
2009 }
2010 }
2011 }
William Lallemand41db4602017-10-30 11:15:51 +01002012 }
William Lallemandd1d1e222019-08-28 15:22:49 +02002013
William Lallemand41db4602017-10-30 11:15:51 +01002014out:
William Lallemand41db4602017-10-30 11:15:51 +01002015 return err_code;
2016
William Lallemand41db4602017-10-30 11:15:51 +01002017}
2018
William Lallemand41db4602017-10-30 11:15:51 +01002019struct flt_ops cache_ops = {
2020 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01002021 .check = cache_store_check,
2022 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01002023
Christopher Faulet65554e12020-03-06 14:52:06 +01002024 /* Handle stream init/deinit */
2025 .attach = cache_store_strm_init,
2026 .detach = cache_store_strm_deinit,
2027
William Lallemand4da3f8a2017-10-31 14:33:34 +01002028 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01002029 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01002030
2031 /* Filter HTTP requests and responses */
2032 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01002033 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01002034 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01002035};
2036
Christopher Faulet99a17a22018-12-11 09:18:27 +01002037
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002038int accept_encoding_cmp(const void *a, const void *b)
2039{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002040 unsigned int int_a = *(unsigned int*)a;
2041 unsigned int int_b = *(unsigned int*)b;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002042
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002043 if (int_a < int_b)
2044 return -1;
2045 if (int_a > int_b)
2046 return 1;
2047 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002048}
2049
Tim Duesterhus23b29452020-11-24 22:22:56 +01002050#define ACCEPT_ENCODING_MAX_ENTRIES 16
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002051/*
2052 * Build a hash of the accept-encoding header. The different parts of the
2053 * header value are first sorted, appended and then a crc is calculated
2054 * for the newly constructed buffer.
2055 * Returns 0 in case of success.
2056 */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002057static int accept_encoding_normalizer(struct ist full_value, char *buf, unsigned int *buf_len)
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002058{
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002059 unsigned int values[ACCEPT_ENCODING_MAX_ENTRIES] = {};
Tim Duesterhus23b29452020-11-24 22:22:56 +01002060 size_t count = 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002061 char *comma = NULL;
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002062 unsigned int hash_value = 0;
2063 unsigned int prev = 0, curr = 0;
2064
2065 /* Turn accept-encoding value to lower case */
2066 full_value = ist2bin_lc(istptr(full_value), full_value);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002067
2068 /* The hash will be built out of a sorted list of accepted encodings. */
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002069 while (count < (ACCEPT_ENCODING_MAX_ENTRIES - 1) && (comma = istchr(full_value, ',')) != NULL) {
2070 size_t length = comma - istptr(full_value);
Tim Duesterhus23b29452020-11-24 22:22:56 +01002071
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002072 values[count++] = hash_crc32(istptr(full_value), length);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002073
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002074 full_value = istadv(full_value, length + 1);
Tim Duesterhus23b29452020-11-24 22:22:56 +01002075
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002076 }
2077 values[count++] = hash_crc32(istptr(full_value), istlen(full_value));
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002078
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002079 /* Sort the values alphabetically. */
2080 qsort(values, count, sizeof(*values), &accept_encoding_cmp);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002081
Remi Tricot-Le Breton8bb72aa2020-11-30 17:06:03 +01002082 while (count) {
2083 curr = values[--count];
2084 if (curr != prev) {
2085 hash_value ^= curr;
2086 }
2087 prev = curr;
2088 }
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002089
2090 memcpy(buf, &hash_value, sizeof(hash_value));
2091 *buf_len = sizeof(hash_value);
2092
Tim Duesterhus23b29452020-11-24 22:22:56 +01002093 return 0;
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002094}
Tim Duesterhus23b29452020-11-24 22:22:56 +01002095#undef ACCEPT_ENCODING_MAX_ENTRIES
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002096
2097/*
2098 * Normalizer used by default for User-Agent and Referer headers. It only
2099 * calculates a simple crc of the whole value.
2100 * Returns 0 in case of success.
2101 */
2102static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len)
2103{
2104 int hash_value = 0;
2105
2106 hash_value = hash_crc32(istptr(value), istlen(value));
2107
2108 memcpy(buf, &hash_value, sizeof(hash_value));
2109 *buf_len = sizeof(hash_value);
2110
2111 return 0;
2112}
2113
2114
2115/*
2116 * Pre-calculate the hashes of all the supported headers (in our Vary
2117 * implementation) of a given request. We have to calculate all the hashes
2118 * in advance because the actual Vary signature won't be known until the first
2119 * response.
2120 * Only the first occurrence of every header will be taken into account in the
2121 * hash.
2122 * If the header is not present, the hash portion of the given header will be
2123 * filled with zeros.
2124 * Returns 0 in case of success.
2125 */
2126static int http_request_prebuild_full_secondary_key(struct stream *s)
2127{
Remi Tricot-Le Bretonbba29122020-12-23 18:13:44 +01002128 /* The fake signature (second parameter) will ensure that every part of the
2129 * secondary key is calculated. */
2130 return http_request_build_secondary_key(s, ~0);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002131}
2132
2133
2134/*
2135 * Calculate the secondary key for a request for which we already have a known
2136 * vary signature. The key is made by aggregating hashes calculated for every
2137 * header mentioned in the vary signature.
2138 * Only the first occurrence of every header will be taken into account in the
2139 * hash.
2140 * If the header is not present, the hash portion of the given header will be
2141 * filled with zeros.
2142 * Returns 0 in case of success.
2143 */
2144static int http_request_build_secondary_key(struct stream *s, int vary_signature)
2145{
2146 struct http_txn *txn = s->txn;
2147 struct htx *htx = htxbuf(&s->req.buf);
2148 struct http_hdr_ctx ctx = { .blk = NULL };
2149
2150 unsigned int idx;
2151 const struct vary_hashing_information *info = NULL;
2152 unsigned int hash_length = 0;
2153 int retval = 0;
2154 int offset = 0;
2155
2156 for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) {
2157 info = &vary_information[idx];
2158
2159 ctx.blk = NULL;
Remi Tricot-Le Bretonbba29122020-12-23 18:13:44 +01002160 if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) {
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002161 retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length);
2162 offset += hash_length;
2163 }
2164 else {
2165 /* Fill hash with 0s. */
2166 hash_length = info->hash_length;
2167 memset(&txn->cache_secondary_hash[offset], 0, hash_length);
2168 offset += hash_length;
2169 }
2170 }
2171
Remi Tricot-Le Breton2b5c5cb2020-12-23 18:13:45 +01002172 if (retval >= 0)
2173 txn->flags |= TX_CACHE_HAS_SEC_KEY;
2174
2175 return (retval < 0);
Remi Tricot-Le Breton3d082362020-11-16 15:56:08 +01002176}
2177
2178/*
2179 * Build the actual secondary key of a given request out of the prebuilt key and
2180 * the actual vary signature (extracted from the response).
2181 * Returns 0 in case of success.
2182 */
2183static int http_request_reduce_secondary_key(unsigned int vary_signature,
2184 char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN])
2185{
2186 int offset = 0;
2187 int global_offset = 0;
2188 int vary_info_count = 0;
2189 int keep = 0;
2190 unsigned int vary_idx;
2191 const struct vary_hashing_information *vary_info;
2192
2193 vary_info_count = sizeof(vary_information)/sizeof(*vary_information);
2194 for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) {
2195 vary_info = &vary_information[vary_idx];
2196 keep = (vary_signature & vary_info->value) ? 0xff : 0;
2197
2198 for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) {
2199 prebuilt_key[global_offset] &= keep;
2200 }
2201 }
2202
2203 return 0;
2204}
2205
2206
Christopher Faulet99a17a22018-12-11 09:18:27 +01002207
2208static int
2209parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
2210 struct flt_conf *fconf, char **err, void *private)
2211{
2212 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01002213 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002214 char *name = NULL;
2215 int pos = *cur_arg;
2216
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002217 /* Get the cache filter name. <pos> point on "cache" keyword */
2218 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02002219 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002220 goto error;
2221 }
2222 name = strdup(args[pos + 1]);
2223 if (!name) {
2224 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
2225 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002226 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02002227 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002228
2229 /* Check if an implicit filter with the same name already exists. If so,
2230 * we remove the implicit filter to use the explicit one. */
2231 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
2232 if (f->id != cache_store_flt_id)
2233 continue;
2234
2235 cconf = f->conf;
2236 if (strcmp(name, cconf->c.name)) {
2237 cconf = NULL;
2238 continue;
2239 }
2240
2241 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
2242 cconf = NULL;
2243 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
2244 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01002245 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01002246 }
2247
2248 /* Remove the implicit filter. <cconf> is kept for the explicit one */
2249 LIST_DEL(&f->list);
2250 free(f);
2251 free(name);
2252 break;
2253 }
2254
2255 /* No implicit cache filter found, create configuration for the explicit one */
2256 if (!cconf) {
2257 cconf = calloc(1, sizeof(*cconf));
2258 if (!cconf) {
2259 memprintf(err, "%s: out of memory", args[*cur_arg]);
2260 goto error;
2261 }
2262 cconf->c.name = name;
2263 }
2264
2265 cconf->flags = 0;
2266 fconf->id = cache_store_flt_id;
2267 fconf->conf = cconf;
2268 fconf->ops = &cache_ops;
2269
2270 *cur_arg = pos;
2271 return 0;
2272
2273 error:
2274 free(name);
2275 free(cconf);
2276 return -1;
2277}
2278
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002279static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01002280{
2281 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2282 return 1;
2283
2284 return 0;
2285}
2286
2287static int cli_io_handler_show_cache(struct appctx *appctx)
2288{
2289 struct cache* cache = appctx->ctx.cli.p0;
2290 struct stream_interface *si = appctx->owner;
2291
William Lallemand1f49a362017-11-21 20:01:26 +01002292 if (cache == NULL) {
2293 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
2294 }
2295
2296 list_for_each_entry_from(cache, &caches, list) {
2297 struct eb32_node *node = NULL;
2298 unsigned int next_key;
2299 struct cache_entry *entry;
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002300 unsigned int i;
William Lallemand1f49a362017-11-21 20:01:26 +01002301
William Lallemand1f49a362017-11-21 20:01:26 +01002302 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02002303 if (!next_key) {
2304 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
2305 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002306 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02002307 return 0;
2308 }
2309 }
William Lallemand1f49a362017-11-21 20:01:26 +01002310
2311 appctx->ctx.cli.p0 = cache;
2312
2313 while (1) {
2314
2315 shctx_lock(shctx_ptr(cache));
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002316 if (!node || (node = eb32_next_dup(node)) == NULL)
2317 node = eb32_lookup_ge(&cache->entries, next_key);
William Lallemand1f49a362017-11-21 20:01:26 +01002318 if (!node) {
2319 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02002320 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01002321 break;
2322 }
2323
2324 entry = container_of(node, struct cache_entry, eb);
Remi Tricot-Le Bretone3e1e5f2020-11-27 15:48:40 +01002325 chunk_printf(&trash, "%p hash:%u vary:0x", entry, read_u32(entry->hash));
2326 for (i = 0; i < HTTP_CACHE_SEC_KEY_LEN; ++i)
2327 chunk_appendf(&trash, "%02x", (unsigned char)entry->secondary_key[i]);
2328 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 +01002329
2330 next_key = node->key + 1;
2331 appctx->ctx.cli.i0 = next_key;
2332
2333 shctx_unlock(shctx_ptr(cache));
2334
2335 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002336 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01002337 return 0;
2338 }
2339 }
2340
2341 }
2342
2343 return 1;
2344
2345}
2346
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002347
2348/*
2349 * boolean, returns true if response was built out of a cache entry.
2350 */
2351static int
2352smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
2353 const char *kw, void *private)
2354{
2355 smp->data.type = SMP_T_BOOL;
2356 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
2357
2358 return 1;
2359}
2360
2361/*
2362 * string, returns cache name (if response came from a cache).
2363 */
2364static int
2365smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
2366 const char *kw, void *private)
2367{
2368 struct appctx *appctx = NULL;
2369
2370 struct cache_flt_conf *cconf = NULL;
2371 struct cache *cache = NULL;
2372
2373 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
2374 return 0;
2375
2376 /* Get appctx from the stream_interface. */
2377 appctx = si_appctx(&smp->strm->si[1]);
2378 if (appctx && appctx->rule) {
2379 cconf = appctx->rule->arg.act.p[0];
2380 if (cconf) {
2381 cache = cconf->c.cache;
2382
2383 smp->data.type = SMP_T_STR;
2384 smp->flags = SMP_F_CONST;
2385 smp->data.u.str.area = cache->id;
2386 smp->data.u.str.data = strlen(cache->id);
2387 return 1;
2388 }
2389 }
2390
2391 return 0;
2392}
2393
Christopher Faulet99a17a22018-12-11 09:18:27 +01002394/* Declare the filter parser for "cache" keyword */
2395static struct flt_kw_list filter_kws = { "CACHE", { }, {
2396 { "cache", parse_cache_flt, NULL },
2397 { NULL, NULL, NULL },
2398 }
2399};
2400
2401INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
2402
William Lallemand1f49a362017-11-21 20:01:26 +01002403static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01002404 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
2405 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01002406}};
2407
Willy Tarreau0108d902018-11-25 19:14:37 +01002408INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01002409
William Lallemand41db4602017-10-30 11:15:51 +01002410static struct action_kw_list http_res_actions = {
2411 .kw = {
2412 { "cache-store", parse_cache_store },
2413 { NULL, NULL }
2414 }
2415};
2416
Willy Tarreau0108d902018-11-25 19:14:37 +01002417INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
2418
William Lallemand41db4602017-10-30 11:15:51 +01002419static struct action_kw_list http_req_actions = {
2420 .kw = {
2421 { "cache-use", parse_cache_use },
2422 { NULL, NULL }
2423 }
2424};
2425
Willy Tarreau0108d902018-11-25 19:14:37 +01002426INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2427
Willy Tarreau2231b632019-03-29 18:26:52 +01002428struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01002429 .obj_type = OBJ_TYPE_APPLET,
2430 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01002431 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01002432 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01002433};
2434
Willy Tarreaue6552512018-11-26 11:33:13 +01002435/* config parsers for this section */
2436REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02002437REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01002438
2439
2440/* Note: must not be declared <const> as its list will be overwritten */
2441static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2442 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2443 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
2444 { /* END */ },
2445 }
2446};
2447
2448INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);