William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1 | /* |
| 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 Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 13 | #include <import/eb32tree.h> |
| 14 | #include <import/sha1.h> |
| 15 | |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 16 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 18 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 19 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 20 | #include <haproxy/cli.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 21 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 22 | #include <haproxy/filters.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 23 | #include <haproxy/hash.h> |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 24 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 25 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 26 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 27 | #include <haproxy/http_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/htx.h> |
| 29 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 30 | #include <haproxy/proxy.h> |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 31 | #include <haproxy/sample.h> |
Willy Tarreau | 334099c | 2020-06-03 18:38:48 +0200 | [diff] [blame] | 32 | #include <haproxy/shctx.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 33 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 34 | #include <haproxy/stream_interface.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 35 | |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 36 | #define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 37 | * the filter keyword) */ |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 38 | #define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 39 | |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 40 | const char *cache_store_flt_id = "cache store filter"; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 41 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 42 | extern struct applet http_cache_applet; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 43 | |
| 44 | struct flt_ops cache_ops; |
| 45 | |
| 46 | struct cache { |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 47 | struct list list; /* cache linked list */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 48 | struct eb_root entries; /* head of cache entries based on keys */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 49 | unsigned int maxage; /* max-age */ |
| 50 | unsigned int maxblocks; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 51 | unsigned int maxobjsz; /* max-object-size (in bytes) */ |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 52 | unsigned int max_secondary_entries; /* maximum number of secondary entries with the same primary hash */ |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 53 | uint8_t vary_processing_enabled; /* boolean : manage Vary header (disabled by default) */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 54 | char id[33]; /* cache name */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 57 | /* cache config for filters */ |
| 58 | struct 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 Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Vary-related structures and functions |
| 69 | */ |
| 70 | enum vary_header_bit { |
| 71 | VARY_ACCEPT_ENCODING = (1 << 0), |
| 72 | VARY_REFERER = (1 << 1), |
| 73 | VARY_LAST /* should always be last */ |
| 74 | }; |
| 75 | |
| 76 | typedef int(*http_header_normalizer)(struct ist value, char *buf, unsigned int *buf_len); |
| 77 | |
| 78 | struct vary_hashing_information { |
| 79 | struct ist hdr_name; /* Header name */ |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 80 | enum vary_header_bit value; /* Bit representing the header in a vary signature */ |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 81 | unsigned int hash_length; /* Size of the sub hash for this header's value */ |
| 82 | http_header_normalizer norm_fn; /* Normalization function */ |
| 83 | }; |
| 84 | |
| 85 | static int accept_encoding_normalizer(struct ist value, char *buf, unsigned int *buf_len); |
| 86 | static 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. */ |
| 90 | const 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 | |
| 95 | static int http_request_prebuild_full_secondary_key(struct stream *s); |
| 96 | static int http_request_build_secondary_key(struct stream *s, int vary_signature); |
| 97 | static int http_request_reduce_secondary_key(unsigned int vary_signature, |
| 98 | char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]); |
| 99 | |
| 100 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 101 | /* |
| 102 | * cache ctx for filters |
| 103 | */ |
| 104 | struct cache_st { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 105 | struct shared_block *first_block; |
| 106 | }; |
| 107 | |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 108 | #define DEFAULT_MAX_SECONDARY_ENTRY 10 |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 109 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 110 | struct cache_entry { |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 111 | unsigned int complete; /* An entry won't be valid until complete is not null. */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 112 | unsigned int latest_validation; /* latest validation date */ |
| 113 | unsigned int expire; /* expiration date */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 114 | unsigned int age; /* Origin server "Age" header value */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 115 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 116 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 117 | char hash[20]; |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 118 | |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 119 | 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 Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 122 | unsigned int secondary_entries_count; /* Should only be filled in the last entry of a list of dup entries */ |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 123 | unsigned int last_clear_ts; /* Timestamp of the last call to clear_expired_duplicates. */ |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 124 | |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 125 | 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 Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 128 | 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 Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 135 | unsigned char data[0]; |
| 136 | }; |
| 137 | |
| 138 | #define CACHE_BLOCKSIZE 1024 |
Willy Tarreau | 96062a1 | 2018-11-11 14:00:28 +0100 | [diff] [blame] | 139 | #define CACHE_ENTRY_MAX_AGE 2147483648U |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 140 | |
| 141 | static struct list caches = LIST_HEAD_INIT(caches); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 142 | static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 143 | static struct cache *tmp_cache_config = NULL; |
| 144 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 145 | DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st)); |
| 146 | |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 147 | static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry); |
| 148 | static void delete_entry(struct cache_entry *del_entry); |
| 149 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 150 | struct cache_entry *entry_exist(struct cache *cache, char *hash) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 151 | { |
| 152 | struct eb32_node *node; |
| 153 | struct cache_entry *entry; |
| 154 | |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 155 | node = eb32_lookup(&cache->entries, read_u32(hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 156 | if (!node) |
| 157 | return NULL; |
| 158 | |
| 159 | entry = eb32_entry(node, struct cache_entry, eb); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 160 | |
| 161 | /* if that's not the right node */ |
| 162 | if (memcmp(entry->hash, hash, sizeof(entry->hash))) |
| 163 | return NULL; |
| 164 | |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 165 | if (entry->expire > now.tv_sec) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 166 | return entry; |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 167 | } else { |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 168 | delete_entry(entry); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 169 | entry->eb.key = 0; |
| 170 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 171 | return NULL; |
| 172 | |
| 173 | } |
| 174 | |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 175 | /* |
| 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 Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 182 | struct 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 Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 192 | |
| 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 Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 202 | 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 Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 215 | |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 216 | /* |
| 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 | */ |
| 221 | static 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 Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 247 | |
| 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 | */ |
| 258 | static 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 Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 263 | unsigned int last_clear_ts = now.tv_sec; |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 264 | |
| 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 Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 278 | last_clear_ts = entry->last_clear_ts; |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 279 | |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 280 | if (entry_count >= cache->max_secondary_entries) { |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 281 | /* 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 Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 295 | if (entry_count >= cache->max_secondary_entries) { |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 296 | /* 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 Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | new_entry->secondary_entries_count = entry_count + 1; |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 308 | new_entry->last_clear_ts = last_clear_ts; |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 309 | |
| 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 | */ |
| 319 | static 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 Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 352 | static 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 Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 357 | static 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 Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 364 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 365 | cache_store_init(struct proxy *px, struct flt_conf *fconf) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 366 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 367 | fconf->flags |= FLT_CFG_FL_HTX; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 371 | static void |
| 372 | cache_store_deinit(struct proxy *px, struct flt_conf *fconf) |
| 373 | { |
| 374 | struct cache_flt_conf *cconf = fconf->conf; |
| 375 | |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 376 | if (!(cconf->flags & CACHE_FLT_INIT)) |
| 377 | free(cconf->c.name); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 378 | free(cconf); |
| 379 | } |
| 380 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 381 | static int |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 382 | cache_store_check(struct proxy *px, struct flt_conf *fconf) |
| 383 | { |
| 384 | struct cache_flt_conf *cconf = fconf->conf; |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 385 | struct flt_conf *f; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 386 | struct cache *cache; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 387 | int comp = 0; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 388 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 389 | /* 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 Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 396 | goto found; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 397 | } |
| 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 Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 404 | /* 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 Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 408 | * 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 Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 411 | list_for_each_entry(f, &px->filter_configs, list) { |
| 412 | if (f == fconf) { |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 413 | /* 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 Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 419 | } |
Christopher Faulet | 8f7fe1c | 2019-07-15 15:08:25 +0200 | [diff] [blame] | 420 | else if (f->id == http_comp_flt_id) |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 421 | comp = 1; |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 422 | else if (f->id == fcgi_flt_id) |
| 423 | continue; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 424 | else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 425 | /* Implicit declaration is only allowed with the |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 426 | * compression and fcgi. For other filters, an implicit |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 427 | * 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 Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 433 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 438 | cache_store_strm_init(struct stream *s, struct filter *filter) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 439 | { |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 440 | struct cache_st *st; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 441 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 442 | st = pool_alloc_dirty(pool_head_cache_st); |
| 443 | if (st == NULL) |
| 444 | return -1; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 445 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 446 | st->first_block = NULL; |
| 447 | filter->ctx = st; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 448 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 449 | /* Register post-analyzer on AN_RES_WAIT_HTTP */ |
| 450 | filter->post_analyzers |= AN_RES_WAIT_HTTP; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 451 | return 1; |
| 452 | } |
| 453 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 454 | static void |
| 455 | cache_store_strm_deinit(struct stream *s, struct filter *filter) |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 456 | { |
| 457 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 458 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 459 | struct cache *cache = cconf->c.cache; |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 460 | struct shared_context *shctx = shctx_ptr(cache); |
| 461 | |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 462 | /* Everything should be released in the http_end filter, but we need to do it |
| 463 | * there too, in case of errors */ |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 464 | if (st && st->first_block) { |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 465 | shctx_lock(shctx); |
| 466 | shctx_row_dec_hot(shctx, st->first_block); |
| 467 | shctx_unlock(shctx); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 468 | } |
| 469 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 470 | pool_free(pool_head_cache_st, st); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 471 | filter->ctx = NULL; |
| 472 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 473 | } |
| 474 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 475 | static int |
| 476 | cache_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 Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 500 | |
| 501 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 502 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 503 | { |
| 504 | struct cache_st *st = filter->ctx; |
| 505 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 506 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 507 | return 1; |
| 508 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 509 | if (st->first_block) |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 510 | register_data_filter(s, msg->chn, filter); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 511 | return 1; |
| 512 | } |
| 513 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 514 | static 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 Breton | 964caaf | 2020-12-15 14:30:12 +0100 | [diff] [blame] | 523 | eb32_delete(&object->eb); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 524 | object->eb.key = 0; |
| 525 | shctx_unlock(shctx); |
| 526 | pool_free(pool_head_cache_st, st); |
| 527 | } |
| 528 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 529 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 530 | cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 531 | unsigned int offset, unsigned int len) |
| 532 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 533 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 534 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 535 | struct cache_st *st = filter->ctx; |
| 536 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 537 | struct htx_blk *blk; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 538 | struct shared_block *fb; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 539 | struct htx_ret htxret; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 540 | unsigned int orig_len, to_forward; |
| 541 | int ret; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 542 | |
| 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 Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 550 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 551 | chunk_reset(&trash); |
| 552 | orig_len = len; |
| 553 | to_forward = 0; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 554 | |
| 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 Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 559 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 560 | uint32_t info, sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 561 | struct ist v; |
| 562 | |
| 563 | switch (type) { |
| 564 | case HTX_BLK_UNUSED: |
| 565 | break; |
| 566 | |
| 567 | case HTX_BLK_DATA: |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 568 | 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 Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 574 | info = (type << 28) + v.len; |
| 575 | chunk_memcat(&trash, (char *)&info, sizeof(info)); |
| 576 | chunk_memcat(&trash, v.ptr, v.len); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 577 | to_forward += v.len; |
| 578 | len -= v.len; |
| 579 | break; |
| 580 | |
| 581 | default: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 582 | /* Here offset must always be 0 because only |
| 583 | * DATA blocks can be partially transferred. */ |
| 584 | if (offset) |
| 585 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 586 | if (sz > len) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 587 | 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 Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 591 | to_forward += sz; |
| 592 | len -= sz; |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | offset = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 597 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 598 | |
| 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 Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 612 | |
| 613 | return to_forward; |
| 614 | |
| 615 | no_cache: |
| 616 | disable_cache_entry(st, filter, shctx); |
| 617 | unregister_data_filter(s, msg->chn, filter); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 618 | return orig_len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 622 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 623 | struct http_msg *msg) |
| 624 | { |
| 625 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 626 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 627 | struct cache *cache = cconf->c.cache; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 628 | 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 Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 638 | shctx_lock(shctx); |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 639 | /* The whole payload was cached, the entry can now be used. */ |
| 640 | object->complete = 1; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 641 | /* remove from the hotlist */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 642 | shctx_row_dec_hot(shctx, st->first_block); |
| 643 | shctx_unlock(shctx); |
| 644 | |
| 645 | } |
| 646 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 647 | pool_free(pool_head_cache_st, st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 648 | 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 | */ |
| 659 | char *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 Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 694 | * 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 Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 698 | * 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 Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 706 | int http_calc_maxage(struct stream *s, struct cache *cache, int *true_maxage) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 707 | { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 708 | struct htx *htx = htxbuf(&s->res.buf); |
| 709 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 710 | long smaxage = -1; |
| 711 | long maxage = -1; |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 712 | int expires = -1; |
| 713 | struct tm tm = {}; |
| 714 | time_t expires_val = 0; |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 715 | char *endptr = NULL; |
| 716 | int offset = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 717 | |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 718 | /* 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 Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 722 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 723 | char *value; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 724 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 725 | value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8); |
| 726 | if (value) { |
| 727 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 728 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 729 | chunk_strncat(chk, value, ctx.value.len - 8 + 1); |
| 730 | chunk_strncat(chk, "", 1); |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 731 | 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 Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 735 | } |
| 736 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 737 | value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7); |
| 738 | if (value) { |
| 739 | struct buffer *chk = get_trash_chunk(); |
Christopher Faulet | 5f2c49f | 2019-07-15 20:49:46 +0200 | [diff] [blame] | 740 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 741 | chunk_strncat(chk, value, ctx.value.len - 7 + 1); |
| 742 | chunk_strncat(chk, "", 1); |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 743 | 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 Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 750 | /* 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 Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 772 | |
| 773 | |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 774 | if (smaxage > 0) { |
| 775 | if (true_maxage) |
| 776 | *true_maxage = smaxage; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 777 | return MIN(smaxage, cache->maxage); |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 778 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 779 | |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 780 | if (maxage > 0) { |
| 781 | if (true_maxage) |
| 782 | *true_maxage = maxage; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 783 | return MIN(maxage, cache->maxage); |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 784 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 785 | |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 786 | if (expires >= 0) { |
| 787 | if (true_maxage) |
| 788 | *true_maxage = expires; |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 789 | return MIN(expires, cache->maxage); |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 790 | } |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 791 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 792 | return cache->maxage; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 793 | |
| 794 | } |
| 795 | |
| 796 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 797 | static void cache_free_blocks(struct shared_block *first, struct shared_block *block) |
| 798 | { |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 799 | struct cache_entry *object = (struct cache_entry *)block->data; |
| 800 | |
| 801 | if (first == block && object->eb.key) |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 802 | delete_entry(object); |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 803 | object->eb.key = 0; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 806 | |
| 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). */ |
| 811 | static 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 Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 840 | /* |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 841 | * Checks the vary header's value. The headers on which vary should be applied |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 842 | * must be explicitly supported in the vary_information array (see cache.c). If |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 843 | * any other header is mentioned, we won't store the response. |
| 844 | * Returns 1 if Vary-based storage can work, 0 otherwise. |
| 845 | */ |
| 846 | static 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 Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 875 | * This function will store the headers of the response in a buffer and then |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 876 | * register a filter to store the data |
| 877 | */ |
| 878 | enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px, |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 879 | struct session *sess, struct stream *s, int flags) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 880 | { |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 881 | long long hdr_age; |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 882 | int effective_maxage = 0; |
| 883 | int true_maxage = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 884 | struct http_txn *txn = s->txn; |
| 885 | struct http_msg *msg = &txn->rsp; |
| 886 | struct filter *filter; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 887 | struct shared_block *first = NULL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 888 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 889 | struct cache *cache = cconf->c.cache; |
| 890 | struct shared_context *shctx = shctx_ptr(cache); |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 891 | struct cache_st *cache_ctx = NULL; |
| 892 | struct cache_entry *object, *old; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 893 | unsigned int key = read_u32(txn->cache_hash); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 894 | struct htx *htx; |
| 895 | struct http_hdr_ctx ctx; |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 896 | size_t hdrs_len = 0; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 897 | int32_t pos; |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 898 | struct ist header_name = IST_NULL; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 899 | unsigned int vary_signature = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 900 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 901 | /* 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 Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 911 | /* cache only GET method */ |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 912 | 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 Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 926 | /* Discard any corresponding entry in case of successful |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 927 | * 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 Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 938 | goto out; |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 939 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 940 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 941 | /* cache key was not computed */ |
| 942 | if (!key) |
| 943 | goto out; |
| 944 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 945 | /* cache only 200 status code */ |
| 946 | if (txn->status != 200) |
| 947 | goto out; |
| 948 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 949 | /* 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 Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 961 | htx = htxbuf(&s->res.buf); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 962 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 963 | /* 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 Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 967 | |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 968 | /* 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 Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 970 | * 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 Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 976 | if (vary_signature) |
| 977 | http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash); |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 978 | } |
| 979 | else if (http_find_header(htx, ist("Vary"), &ctx, 0)) { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 980 | goto out; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 981 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 982 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 983 | http_check_response_for_cacheability(s, &s->res); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 984 | |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 985 | if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK) || (txn->flags & TX_CACHE_IGNORE)) |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 986 | goto out; |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 987 | |
| 988 | shctx_lock(shctx); |
| 989 | old = entry_exist(cache, txn->cache_hash); |
| 990 | if (old) { |
| 991 | if (vary_signature) |
| 992 | old = secondary_entry_exist(cconf->c.cache, old, |
| 993 | txn->cache_secondary_hash); |
| 994 | if (old) { |
| 995 | if (!old->complete) { |
| 996 | /* An entry with the same primary key is already being |
| 997 | * created, we should not try to store the current |
| 998 | * response because it will waste space in the cache. */ |
| 999 | shctx_unlock(shctx); |
| 1000 | goto out; |
| 1001 | } |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 1002 | delete_entry(old); |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1003 | old->eb.key = 0; |
| 1004 | } |
| 1005 | } |
| 1006 | first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry)); |
| 1007 | if (!first) { |
| 1008 | shctx_unlock(shctx); |
| 1009 | goto out; |
| 1010 | } |
| 1011 | /* the received memory is not initialized, we need at least to mark |
| 1012 | * the object as not indexed yet. |
| 1013 | */ |
| 1014 | object = (struct cache_entry *)first->data; |
| 1015 | memset(object, 0, sizeof(*object)); |
| 1016 | object->eb.key = key; |
| 1017 | object->secondary_key_signature = vary_signature; |
| 1018 | /* We need to temporarily set a valid expiring time until the actual one |
| 1019 | * is set by the end of this function (in case of concurrent accesses to |
| 1020 | * the same resource). This way the second access will find an existing |
| 1021 | * but not yet usable entry in the tree and will avoid storing its data. */ |
| 1022 | object->expire = now.tv_sec + 2; |
| 1023 | |
| 1024 | memcpy(object->hash, txn->cache_hash, sizeof(object->hash)); |
| 1025 | if (vary_signature) |
| 1026 | memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN); |
| 1027 | |
| 1028 | /* Insert the entry in the tree even if the payload is not cached yet. */ |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 1029 | if (insert_entry(cache, object) != &object->eb) { |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1030 | object->eb.key = 0; |
| 1031 | shctx_unlock(shctx); |
| 1032 | goto out; |
| 1033 | } |
| 1034 | shctx_unlock(shctx); |
| 1035 | |
| 1036 | /* reserve space for the cache_entry structure */ |
| 1037 | first->len = sizeof(struct cache_entry); |
| 1038 | first->last_append = NULL; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1039 | |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 1040 | /* Determine the entry's maximum age (taking into account the cache's |
| 1041 | * configuration) as well as the response's explicit max age (extracted |
| 1042 | * from cache-control directives or the expires header). */ |
| 1043 | effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage); |
| 1044 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1045 | ctx.blk = NULL; |
| 1046 | if (http_find_header(htx, ist("Age"), &ctx, 0)) { |
| 1047 | if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) { |
| 1048 | if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE)) |
| 1049 | hdr_age = CACHE_ENTRY_MAX_AGE; |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 1050 | /* A response with an Age value greater than its |
| 1051 | * announced max age is stale and should not be stored. */ |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1052 | object->age = hdr_age; |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 1053 | if (unlikely(object->age > true_maxage)) |
| 1054 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1055 | } |
Remi Tricot-Le Breton | 51058d6 | 2020-12-03 18:19:32 +0100 | [diff] [blame] | 1056 | else |
| 1057 | goto out; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1058 | http_remove_header(htx, &ctx); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1059 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1060 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 1061 | /* Build a last-modified time that will be stored in the cache_entry and |
| 1062 | * compared to a future If-Modified-Since client header. */ |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1063 | object->last_modified = get_last_modified_time(htx); |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 1064 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1065 | chunk_reset(&trash); |
| 1066 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 1067 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1068 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1069 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1070 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 1071 | hdrs_len += sizeof(*blk) + sz; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1072 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 1073 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 1074 | |
| 1075 | /* Look for optional ETag header. |
| 1076 | * We need to store the offset of the ETag value in order for |
| 1077 | * future conditional requests to be able to perform ETag |
| 1078 | * comparisons. */ |
| 1079 | if (type == HTX_BLK_HDR) { |
| 1080 | header_name = htx_get_blk_name(htx, blk); |
| 1081 | if (isteq(header_name, ist("etag"))) { |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1082 | object->etag_length = sz - istlen(header_name); |
| 1083 | object->etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name); |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 1084 | } |
| 1085 | } |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1086 | if (type == HTX_BLK_EOH) |
| 1087 | break; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 1090 | /* Do not cache objects if the headers are too big. */ |
| 1091 | if (hdrs_len > htx->size - global.tune.maxrewrite) |
| 1092 | goto out; |
| 1093 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1094 | shctx_lock(shctx); |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1095 | if (!shctx_row_reserve_hot(shctx, first, trash.data)) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1096 | shctx_unlock(shctx); |
| 1097 | goto out; |
| 1098 | } |
| 1099 | shctx_unlock(shctx); |
| 1100 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1101 | /* cache the headers in a http action because it allows to chose what |
| 1102 | * to cache, for example you might want to cache a response before |
| 1103 | * modifying some HTTP headers, or on the contrary after modifying |
| 1104 | * those headers. |
| 1105 | */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1106 | /* does not need to be locked because it's in the "hot" list, |
| 1107 | * copy the headers */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1108 | if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0) |
| 1109 | goto out; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1110 | |
| 1111 | /* register the buffer in the filter ctx for filling it with data*/ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1112 | if (cache_ctx) { |
| 1113 | cache_ctx->first_block = first; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1114 | /* store latest value and expiration time */ |
| 1115 | object->latest_validation = now.tv_sec; |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 1116 | object->expire = now.tv_sec + effective_maxage; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1117 | return ACT_RET_CONT; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | out: |
| 1121 | /* if does not cache */ |
| 1122 | if (first) { |
| 1123 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 1124 | first->len = 0; |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1125 | if (object->eb.key) |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 1126 | delete_entry(object); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 1127 | object->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1128 | shctx_row_dec_hot(shctx, first); |
| 1129 | shctx_unlock(shctx); |
| 1130 | } |
| 1131 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1132 | return ACT_RET_CONT; |
| 1133 | } |
| 1134 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1135 | #define HTX_CACHE_INIT 0 /* Initial state. */ |
| 1136 | #define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */ |
| 1137 | #define HTX_CACHE_DATA 2 /* Cache entry data forwarding */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1138 | #define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */ |
| 1139 | #define HTX_CACHE_END 4 /* Cache entry treatment terminated */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1140 | |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1141 | static void http_cache_applet_release(struct appctx *appctx) |
| 1142 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1143 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1144 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1145 | struct cache *cache = cconf->c.cache; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1146 | struct shared_block *first = block_ptr(cache_ptr); |
| 1147 | |
| 1148 | shctx_lock(shctx_ptr(cache)); |
| 1149 | shctx_row_dec_hot(shctx_ptr(cache), first); |
| 1150 | shctx_unlock(shctx_ptr(cache)); |
| 1151 | } |
| 1152 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1153 | |
| 1154 | static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type, |
| 1155 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1156 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1157 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 1158 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1159 | struct htx_blk *blk; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 1160 | char *ptr; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1161 | unsigned int max, total; |
| 1162 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1163 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1164 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 1165 | if (!max) |
| 1166 | return 0; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1167 | blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1168 | ? (info & 0xff) + ((info >> 8) & 0xfffff) |
| 1169 | : info & 0xfffffff); |
| 1170 | if (blksz > max) |
| 1171 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1172 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1173 | blk = htx_add_blk(htx, type, blksz); |
| 1174 | if (!blk) |
| 1175 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1176 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1177 | blk->info = info; |
| 1178 | total = 4; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 1179 | ptr = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1180 | while (blksz) { |
| 1181 | max = MIN(blksz, shctx->block_size - offset); |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 1182 | memcpy(ptr, (const char *)shblk->data + offset, max); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1183 | offset += max; |
| 1184 | blksz -= max; |
| 1185 | total += max; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 1186 | ptr += max; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1187 | if (blksz || offset == shctx->block_size) { |
| 1188 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 1189 | offset = 0; |
| 1190 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1191 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1192 | appctx->ctx.cache.offset = offset; |
| 1193 | appctx->ctx.cache.next = shblk; |
| 1194 | appctx->ctx.cache.sent += total; |
| 1195 | return total; |
| 1196 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1197 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1198 | static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx, |
| 1199 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
| 1200 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1201 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1202 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 1203 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
| 1204 | unsigned int max, total, rem_data; |
| 1205 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1206 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1207 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 1208 | if (!max) |
| 1209 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1210 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1211 | rem_data = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 1212 | if (appctx->ctx.cache.rem_data) { |
| 1213 | blksz = appctx->ctx.cache.rem_data; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1214 | total = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 1215 | } |
| 1216 | else { |
| 1217 | blksz = (info & 0xfffffff); |
| 1218 | total = 4; |
| 1219 | } |
| 1220 | if (blksz > max) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1221 | rem_data = blksz - max; |
| 1222 | blksz = max; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1223 | } |
| 1224 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1225 | while (blksz) { |
| 1226 | size_t sz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1227 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1228 | max = MIN(blksz, shctx->block_size - offset); |
| 1229 | sz = htx_add_data(htx, ist2(shblk->data + offset, max)); |
| 1230 | offset += sz; |
| 1231 | blksz -= sz; |
| 1232 | total += sz; |
| 1233 | if (sz < max) |
| 1234 | break; |
| 1235 | if (blksz || offset == shctx->block_size) { |
| 1236 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 1237 | offset = 0; |
| 1238 | } |
| 1239 | } |
| 1240 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1241 | appctx->ctx.cache.offset = offset; |
| 1242 | appctx->ctx.cache.next = shblk; |
| 1243 | appctx->ctx.cache.sent += total; |
| 1244 | appctx->ctx.cache.rem_data = rem_data + blksz; |
| 1245 | return total; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1246 | } |
| 1247 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1248 | static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len, |
| 1249 | enum htx_blk_type mark) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1250 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1251 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 1252 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1253 | struct shared_block *shblk; |
| 1254 | unsigned int offset, sz; |
| 1255 | unsigned int ret, total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1256 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1257 | while (len) { |
| 1258 | enum htx_blk_type type; |
| 1259 | uint32_t info; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1260 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1261 | shblk = appctx->ctx.cache.next; |
| 1262 | offset = appctx->ctx.cache.offset; |
| 1263 | if (appctx->ctx.cache.rem_data) { |
| 1264 | type = HTX_BLK_DATA; |
| 1265 | info = 0; |
| 1266 | goto add_data_blk; |
| 1267 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1268 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1269 | /* Get info of the next HTX block. May be split on 2 shblk */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1270 | sz = MIN(4, shctx->block_size - offset); |
| 1271 | memcpy((char *)&info, (const char *)shblk->data + offset, sz); |
| 1272 | offset += sz; |
| 1273 | if (sz < 4) { |
| 1274 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 1275 | memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz); |
| 1276 | offset = (4 - sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1277 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1278 | |
| 1279 | /* Get payload of the next HTX block and insert it. */ |
| 1280 | type = (info >> 28); |
| 1281 | if (type != HTX_BLK_DATA) |
| 1282 | ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset); |
| 1283 | else { |
| 1284 | add_data_blk: |
| 1285 | ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1286 | } |
| 1287 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1288 | if (!ret) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1289 | break; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1290 | total += ret; |
| 1291 | len -= ret; |
| 1292 | |
| 1293 | if (appctx->ctx.cache.rem_data || type == mark) |
| 1294 | break; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1295 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1296 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1297 | return total; |
| 1298 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1299 | |
| 1300 | static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx) |
| 1301 | { |
| 1302 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 1303 | unsigned int age; |
| 1304 | char *end; |
| 1305 | |
| 1306 | chunk_reset(&trash); |
| 1307 | age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age; |
| 1308 | if (unlikely(age > CACHE_ENTRY_MAX_AGE)) |
| 1309 | age = CACHE_ENTRY_MAX_AGE; |
| 1310 | end = ultoa_o(age, b_head(&trash), b_size(&trash)); |
| 1311 | b_set_data(&trash, end - b_head(&trash)); |
| 1312 | if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash)))) |
| 1313 | return 0; |
| 1314 | return 1; |
| 1315 | } |
| 1316 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1317 | static void http_cache_io_handler(struct appctx *appctx) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1318 | { |
| 1319 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 1320 | struct shared_block *first = block_ptr(cache_ptr); |
| 1321 | struct stream_interface *si = appctx->owner; |
| 1322 | struct channel *req = si_oc(si); |
| 1323 | struct channel *res = si_ic(si); |
| 1324 | struct htx *req_htx, *res_htx; |
| 1325 | struct buffer *errmsg; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1326 | unsigned int len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1327 | size_t ret, total = 0; |
| 1328 | |
| 1329 | res_htx = htxbuf(&res->buf); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1330 | total = res_htx->data; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1331 | |
| 1332 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 1333 | goto out; |
| 1334 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1335 | /* Check if the input buffer is available. */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1336 | if (!b_size(&res->buf)) { |
| 1337 | si_rx_room_blk(si); |
| 1338 | goto out; |
| 1339 | } |
| 1340 | |
Willy Tarreau | efef323 | 2018-12-16 00:37:45 +0100 | [diff] [blame] | 1341 | if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW)) |
Willy Tarreau | 273e964 | 2018-12-16 00:35:15 +0100 | [diff] [blame] | 1342 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1343 | |
| 1344 | if (appctx->st0 == HTX_CACHE_INIT) { |
| 1345 | appctx->ctx.cache.next = block_ptr(cache_ptr); |
| 1346 | appctx->ctx.cache.offset = sizeof(*cache_ptr); |
| 1347 | appctx->ctx.cache.sent = 0; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1348 | appctx->ctx.cache.rem_data = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1349 | appctx->st0 = HTX_CACHE_HEADER; |
| 1350 | } |
| 1351 | |
| 1352 | if (appctx->st0 == HTX_CACHE_HEADER) { |
| 1353 | /* Headers must be dump at once. Otherwise it is an error */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1354 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1355 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH); |
| 1356 | if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) || |
| 1357 | !htx_cache_add_age_hdr(appctx, res_htx)) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1358 | goto error; |
| 1359 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1360 | /* In case of a conditional request, we might want to send a |
| 1361 | * "304 Not Modified" response instead of the stored data. */ |
Tim Duesterhus | e014234 | 2020-10-22 21:15:06 +0200 | [diff] [blame] | 1362 | if (appctx->ctx.cache.send_notmodified) { |
| 1363 | if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) { |
| 1364 | /* If replacing the status code fails we need to send the full response. */ |
| 1365 | appctx->ctx.cache.send_notmodified = 0; |
| 1366 | } |
| 1367 | } |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1368 | |
| 1369 | /* Skip response body for HEAD requests or in case of "304 Not |
| 1370 | * Modified" response. */ |
| 1371 | if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified) |
Christopher Faulet | f0dd037 | 2019-02-25 11:08:34 +0100 | [diff] [blame] | 1372 | appctx->st0 = HTX_CACHE_EOM; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1373 | else |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1374 | appctx->st0 = HTX_CACHE_DATA; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | if (appctx->st0 == HTX_CACHE_DATA) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1378 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1379 | if (len) { |
| 1380 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM); |
| 1381 | if (ret < len) { |
| 1382 | si_rx_room_blk(si); |
| 1383 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1384 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1385 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1386 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | if (appctx->st0 == HTX_CACHE_EOM) { |
Christopher Faulet | 810df06 | 2020-07-22 16:20:34 +0200 | [diff] [blame] | 1390 | res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1391 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 1392 | si_rx_room_blk(si); |
| 1393 | goto out; |
| 1394 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1395 | appctx->st0 = HTX_CACHE_END; |
| 1396 | } |
| 1397 | |
| 1398 | end: |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1399 | if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1400 | res->flags |= CF_READ_NULL; |
| 1401 | si_shutr(si); |
| 1402 | } |
| 1403 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1404 | out: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1405 | total = res_htx->data - total; |
Christopher Faulet | 6112391 | 2019-01-02 14:10:01 +0100 | [diff] [blame] | 1406 | if (total) |
| 1407 | channel_add_input(res, total); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1408 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1409 | |
| 1410 | /* eat the whole request */ |
| 1411 | if (co_data(req)) { |
| 1412 | req_htx = htx_from_buf(&req->buf); |
| 1413 | co_htx_skip(req, req_htx, co_data(req)); |
| 1414 | htx_to_buf(req_htx, &req->buf); |
| 1415 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1416 | return; |
| 1417 | |
| 1418 | error: |
| 1419 | /* Sent and HTTP error 500 */ |
| 1420 | b_reset(&res->buf); |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 1421 | errmsg = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1422 | res->buf.data = b_data(errmsg); |
| 1423 | memcpy(res->buf.area, b_head(errmsg), b_data(errmsg)); |
| 1424 | res_htx = htx_from_buf(&res->buf); |
| 1425 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1426 | total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1427 | appctx->st0 = HTX_CACHE_END; |
| 1428 | goto end; |
| 1429 | } |
| 1430 | |
| 1431 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1432 | static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1433 | { |
| 1434 | struct flt_conf *fconf; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1435 | struct cache_flt_conf *cconf = NULL; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1436 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1437 | if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1438 | memprintf(err, "expects a cache name"); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1439 | goto err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | /* check if a cache filter was already registered with this cache |
| 1443 | * name, if that's the case, must use it. */ |
| 1444 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1445 | if (fconf->id == cache_store_flt_id) { |
| 1446 | cconf = fconf->conf; |
| 1447 | if (cconf && !strcmp((char *)cconf->c.name, name)) { |
| 1448 | rule->arg.act.p[0] = cconf; |
| 1449 | return 1; |
| 1450 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1454 | /* Create the filter cache config */ |
| 1455 | cconf = calloc(1, sizeof(*cconf)); |
| 1456 | if (!cconf) { |
| 1457 | memprintf(err, "out of memory\n"); |
| 1458 | goto err; |
| 1459 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1460 | cconf->flags = CACHE_FLT_F_IMPLICIT_DECL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1461 | cconf->c.name = strdup(name); |
| 1462 | if (!cconf->c.name) { |
| 1463 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1464 | goto err; |
| 1465 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1466 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1467 | /* register a filter to fill the cache buffer */ |
| 1468 | fconf = calloc(1, sizeof(*fconf)); |
| 1469 | if (!fconf) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1470 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1471 | goto err; |
| 1472 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1473 | fconf->id = cache_store_flt_id; |
| 1474 | fconf->conf = cconf; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1475 | fconf->ops = &cache_ops; |
| 1476 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 1477 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1478 | rule->arg.act.p[0] = cconf; |
| 1479 | return 1; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1480 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1481 | err: |
| 1482 | free(cconf); |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 1487 | struct act_rule *rule, char **err) |
| 1488 | { |
| 1489 | rule->action = ACT_CUSTOM; |
| 1490 | rule->action_ptr = http_action_store_cache; |
| 1491 | |
| 1492 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
| 1493 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1494 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1495 | (*orig_arg)++; |
| 1496 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1497 | } |
| 1498 | |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1499 | /* This produces a sha1 hash of the concatenation of the HTTP method, |
| 1500 | * the first occurrence of the Host header followed by the path component |
| 1501 | * if it begins with a slash ('/'). */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1502 | int sha1_hosturi(struct stream *s) |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1503 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1504 | struct http_txn *txn = s->txn; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1505 | struct htx *htx = htxbuf(&s->req.buf); |
| 1506 | struct htx_sl *sl; |
| 1507 | struct http_hdr_ctx ctx; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1508 | struct ist uri; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1509 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1510 | struct buffer *trash; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1511 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1512 | trash = get_trash_chunk(); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1513 | ctx.blk = NULL; |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1514 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1515 | sl = http_get_stline(htx); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1516 | uri = htx_sl_req_uri(sl); // whole uri |
| 1517 | if (!uri.len) |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1518 | return 0; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1519 | |
| 1520 | /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'), |
| 1521 | * unless haproxy is deployed in front of an outbound cache. In HTTP/2, |
| 1522 | * URIs are almost always sent in absolute form with their scheme. In |
| 1523 | * this case, the scheme is almost always "https". In order to support |
| 1524 | * sharing of cache objects between H1 and H2, we'll hash the absolute |
| 1525 | * URI whenever known, or prepend "https://" + the Host header for |
| 1526 | * relative URIs. The difference will only appear on absolute HTTP/1 |
| 1527 | * requests sent to an origin server, which practically is never met in |
| 1528 | * the real world so we don't care about the ability to share the same |
| 1529 | * key here.URIs are normalized from the absolute URI to an origin form as |
| 1530 | * well. |
| 1531 | */ |
| 1532 | if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) { |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1533 | chunk_istcat(trash, ist("https://")); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1534 | if (!http_find_header(htx, ist("Host"), &ctx, 0)) |
| 1535 | return 0; |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1536 | chunk_istcat(trash, ctx.value); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | chunk_memcat(trash, uri.ptr, uri.len); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1540 | |
| 1541 | /* hash everything */ |
| 1542 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1543 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1544 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 1545 | |
| 1546 | return 1; |
| 1547 | } |
| 1548 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1549 | /* Looks for "If-None-Match" headers in the request and compares their value |
| 1550 | * with the one that might have been stored in the cache_entry. If any of them |
| 1551 | * matches, a "304 Not Modified" response should be sent instead of the cached |
| 1552 | * data. |
| 1553 | * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1554 | * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2). |
| 1555 | * |
| 1556 | * If no "If-None-Match" header was found, look for an "If-Modified-Since" |
| 1557 | * header and compare its value (date) to the one stored in the cache_entry. |
| 1558 | * If the request's date is later than the cached one, we also send a |
| 1559 | * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2). |
| 1560 | * |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1561 | * Returns 1 if "304 Not Modified" should be sent, 0 otherwise. |
| 1562 | */ |
| 1563 | static int should_send_notmodified_response(struct cache *cache, struct htx *htx, |
| 1564 | struct cache_entry *entry) |
| 1565 | { |
| 1566 | int retval = 0; |
| 1567 | |
| 1568 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 1569 | struct ist cache_entry_etag = IST_NULL; |
| 1570 | struct buffer *etag_buffer = NULL; |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1571 | int if_none_match_found = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1572 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1573 | struct tm tm = {}; |
| 1574 | time_t if_modified_since = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1575 | |
| 1576 | /* If we find a "If-None-Match" header in the request, rebuild the |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1577 | * cache_entry's ETag in order to perform comparisons. |
| 1578 | * There could be multiple "if-none-match" header lines. */ |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1579 | while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) { |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1580 | if_none_match_found = 1; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1581 | |
| 1582 | /* A '*' matches everything. */ |
| 1583 | if (isteq(ctx.value, ist("*")) != 0) { |
| 1584 | retval = 1; |
| 1585 | break; |
| 1586 | } |
| 1587 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1588 | /* No need to rebuild an etag if none was stored in the cache. */ |
| 1589 | if (entry->etag_length == 0) |
| 1590 | break; |
| 1591 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1592 | /* Rebuild the stored ETag. */ |
| 1593 | if (etag_buffer == NULL) { |
| 1594 | etag_buffer = get_trash_chunk(); |
| 1595 | |
| 1596 | if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry), |
| 1597 | (unsigned char*)b_orig(etag_buffer), |
| 1598 | entry->etag_offset, entry->etag_length) == 0) { |
| 1599 | cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length); |
| 1600 | } else { |
| 1601 | /* We could not rebuild the ETag in one go, we |
| 1602 | * won't send a "304 Not Modified" response. */ |
| 1603 | break; |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | if (http_compare_etags(cache_entry_etag, ctx.value) == 1) { |
| 1608 | retval = 1; |
| 1609 | break; |
| 1610 | } |
| 1611 | } |
| 1612 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1613 | /* If the request did not contain an "If-None-Match" header, we look for |
| 1614 | * an "If-Modified-Since" header (see RFC 7232#3.3). */ |
| 1615 | if (retval == 0 && if_none_match_found == 0) { |
| 1616 | ctx.blk = NULL; |
| 1617 | if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) { |
| 1618 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 1619 | if_modified_since = my_timegm(&tm); |
| 1620 | |
| 1621 | /* We send a "304 Not Modified" response if the |
| 1622 | * entry's last modified date is earlier than |
| 1623 | * the one found in the "If-Modified-Since" |
| 1624 | * header. */ |
| 1625 | retval = (entry->last_modified <= if_modified_since); |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1630 | return retval; |
| 1631 | } |
| 1632 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1633 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 1634 | struct session *sess, struct stream *s, int flags) |
| 1635 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1636 | |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1637 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1638 | struct cache_entry *res, *sec_entry = NULL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1639 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
| 1640 | struct cache *cache = cconf->c.cache; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1641 | struct shared_block *entry_block; |
| 1642 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1643 | |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1644 | /* Ignore cache for HTTP/1.0 requests and for requests other than GET |
| 1645 | * and HEAD */ |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1646 | if (!(txn->req.flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1647 | (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)) |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1648 | txn->flags |= TX_CACHE_IGNORE; |
| 1649 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1650 | http_check_request_for_cacheability(s, &s->req); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1651 | |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 1652 | /* The request's hash has to be calculated for all requests, even POSTs |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 1653 | * or PUTs for instance because RFC7234 specifies that a successful |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 1654 | * "unsafe" method on a stored resource must invalidate it |
| 1655 | * (see RFC7234#4.4). */ |
| 1656 | if (!sha1_hosturi(s)) |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1657 | return ACT_RET_CONT; |
| 1658 | |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 1659 | if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE) |
Willy Tarreau | 7704b1e | 2017-12-22 16:32:43 +0100 | [diff] [blame] | 1660 | return ACT_RET_CONT; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1661 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1662 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 1663 | return ACT_RET_CONT; |
| 1664 | |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1665 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1666 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1667 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1668 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1669 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1670 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1671 | res = entry_exist(cache, s->txn->cache_hash); |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1672 | /* We must not use an entry that is not complete. */ |
| 1673 | if (res && res->complete) { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1674 | struct appctx *appctx; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1675 | entry_block = block_ptr(res); |
| 1676 | shctx_row_inc_hot(shctx_ptr(cache), entry_block); |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1677 | shctx_unlock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1678 | |
| 1679 | /* In case of Vary, we could have multiple entries with the same |
| 1680 | * primary hash. We need to calculate the secondary has in order |
| 1681 | * to find the actual entry we want (if it exists). */ |
| 1682 | if (res->secondary_key_signature) { |
| 1683 | if (!http_request_build_secondary_key(s, res->secondary_key_signature)) { |
| 1684 | shctx_lock(shctx_ptr(cache)); |
| 1685 | sec_entry = secondary_entry_exist(cache, res, |
| 1686 | s->txn->cache_secondary_hash); |
| 1687 | if (sec_entry && sec_entry != res) { |
| 1688 | /* The wrong row was added to the hot list. */ |
| 1689 | shctx_row_dec_hot(shctx_ptr(cache), entry_block); |
| 1690 | entry_block = block_ptr(sec_entry); |
| 1691 | shctx_row_inc_hot(shctx_ptr(cache), entry_block); |
| 1692 | } |
| 1693 | res = sec_entry; |
| 1694 | shctx_unlock(shctx_ptr(cache)); |
| 1695 | } |
| 1696 | else |
| 1697 | res = NULL; |
| 1698 | } |
| 1699 | |
| 1700 | /* We looked for a valid secondary entry and could not find one, |
| 1701 | * the request must be forwarded to the server. */ |
| 1702 | if (!res) { |
| 1703 | shctx_lock(shctx_ptr(cache)); |
| 1704 | shctx_row_dec_hot(shctx_ptr(cache), entry_block); |
| 1705 | shctx_unlock(shctx_ptr(cache)); |
| 1706 | return ACT_RET_CONT; |
| 1707 | } |
| 1708 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1709 | s->target = &http_cache_applet.obj_type; |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1710 | if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1711 | appctx->st0 = HTX_CACHE_INIT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1712 | appctx->rule = rule; |
| 1713 | appctx->ctx.cache.entry = res; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1714 | appctx->ctx.cache.next = NULL; |
| 1715 | appctx->ctx.cache.sent = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1716 | appctx->ctx.cache.send_notmodified = |
| 1717 | should_send_notmodified_response(cache, htxbuf(&s->req.buf), res); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1718 | |
| 1719 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1720 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1721 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1722 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1723 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1724 | } else { |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 1725 | shctx_lock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1726 | shctx_row_dec_hot(shctx_ptr(cache), entry_block); |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 1727 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1728 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1729 | } |
| 1730 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1731 | shctx_unlock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1732 | |
| 1733 | /* Shared context does not need to be locked while we calculate the |
| 1734 | * secondary hash. */ |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 1735 | if (!res && cache->vary_processing_enabled) { |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1736 | /* Build a complete secondary hash until the server response |
| 1737 | * tells us which fields should be kept (if any). */ |
| 1738 | http_request_prebuild_full_secondary_key(s); |
| 1739 | } |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1740 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 1745 | struct act_rule *rule, char **err) |
| 1746 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1747 | rule->action = ACT_CUSTOM; |
| 1748 | rule->action_ptr = http_action_req_cache_use; |
| 1749 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1750 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1751 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1752 | |
| 1753 | (*orig_arg)++; |
| 1754 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 1758 | { |
| 1759 | int err_code = 0; |
| 1760 | |
| 1761 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 1762 | |
| 1763 | if (!*args[1]) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1764 | ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1765 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1766 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1767 | goto out; |
| 1768 | } |
| 1769 | |
| 1770 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1771 | err_code |= ERR_ABORT; |
| 1772 | goto out; |
| 1773 | } |
| 1774 | |
| 1775 | if (tmp_cache_config == NULL) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1776 | struct cache *cache_config; |
| 1777 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1778 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 1779 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1780 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1781 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1782 | goto out; |
| 1783 | } |
| 1784 | |
| 1785 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 1786 | if (strlen(args[1]) > 32) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1787 | ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1788 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1789 | err_code |= ERR_WARN; |
| 1790 | } |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1791 | |
| 1792 | list_for_each_entry(cache_config, &caches_config, list) { |
| 1793 | if (strcmp(tmp_cache_config->id, cache_config->id) == 0) { |
| 1794 | ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n", |
| 1795 | file, linenum, tmp_cache_config->id); |
| 1796 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1797 | goto out; |
| 1798 | } |
| 1799 | } |
| 1800 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1801 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1802 | tmp_cache_config->maxblocks = 0; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1803 | tmp_cache_config->maxobjsz = 0; |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 1804 | tmp_cache_config->max_secondary_entries = DEFAULT_MAX_SECONDARY_ENTRY; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1805 | } |
| 1806 | } else if (strcmp(args[0], "total-max-size") == 0) { |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1807 | unsigned long int maxsize; |
| 1808 | char *err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1809 | |
| 1810 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1811 | err_code |= ERR_ABORT; |
| 1812 | goto out; |
| 1813 | } |
| 1814 | |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1815 | maxsize = strtoul(args[1], &err, 10); |
| 1816 | if (err == args[1] || *err != '\0') { |
| 1817 | ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n", |
| 1818 | file, linenum, args[1]); |
| 1819 | err_code |= ERR_ABORT; |
| 1820 | goto out; |
| 1821 | } |
| 1822 | |
| 1823 | if (maxsize > (UINT_MAX >> 20)) { |
| 1824 | ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n", |
| 1825 | file, linenum, args[1], UINT_MAX >> 20); |
| 1826 | err_code |= ERR_ABORT; |
| 1827 | goto out; |
| 1828 | } |
| 1829 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1830 | /* size in megabytes */ |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1831 | maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1832 | tmp_cache_config->maxblocks = maxsize; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1833 | } else if (strcmp(args[0], "max-age") == 0) { |
| 1834 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1835 | err_code |= ERR_ABORT; |
| 1836 | goto out; |
| 1837 | } |
| 1838 | |
| 1839 | if (!*args[1]) { |
| 1840 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 1841 | file, linenum, args[0]); |
| 1842 | err_code |= ERR_WARN; |
| 1843 | } |
| 1844 | |
| 1845 | tmp_cache_config->maxage = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1846 | } else if (strcmp(args[0], "max-object-size") == 0) { |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1847 | unsigned int maxobjsz; |
| 1848 | char *err; |
| 1849 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1850 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1851 | err_code |= ERR_ABORT; |
| 1852 | goto out; |
| 1853 | } |
| 1854 | |
| 1855 | if (!*args[1]) { |
| 1856 | ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n", |
| 1857 | file, linenum, args[0]); |
| 1858 | err_code |= ERR_WARN; |
| 1859 | } |
| 1860 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1861 | maxobjsz = strtoul(args[1], &err, 10); |
| 1862 | if (err == args[1] || *err != '\0') { |
| 1863 | ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n", |
| 1864 | file, linenum, args[1]); |
| 1865 | err_code |= ERR_ABORT; |
| 1866 | goto out; |
| 1867 | } |
| 1868 | tmp_cache_config->maxobjsz = maxobjsz; |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 1869 | } else if (strcmp(args[0], "process-vary") == 0) { |
| 1870 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1871 | err_code |= ERR_ABORT; |
| 1872 | goto out; |
| 1873 | } |
| 1874 | |
| 1875 | if (!*args[1]) { |
| 1876 | ha_warning("parsing [%s:%d]: '%s' expects 0 or 1 (disable or enable vary processing).\n", |
| 1877 | file, linenum, args[0]); |
| 1878 | err_code |= ERR_WARN; |
| 1879 | } |
| 1880 | |
| 1881 | tmp_cache_config->vary_processing_enabled = atoi(args[1]); |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 1882 | } else if (strcmp(args[0], "max-secondary-entries") == 0) { |
| 1883 | unsigned int max_sec_entries; |
| 1884 | char *err; |
| 1885 | |
| 1886 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1887 | err_code |= ERR_ABORT; |
| 1888 | goto out; |
| 1889 | } |
| 1890 | |
| 1891 | if (!*args[1]) { |
| 1892 | ha_warning("parsing [%s:%d]: '%s' expects a strictly positive number.\n", |
| 1893 | file, linenum, args[0]); |
| 1894 | err_code |= ERR_WARN; |
| 1895 | } |
| 1896 | |
| 1897 | max_sec_entries = strtoul(args[1], &err, 10); |
| 1898 | if (err == args[1] || *err != '\0' || max_sec_entries == 0) { |
| 1899 | ha_warning("parsing [%s:%d]: max-secondary-entries wrong value '%s'\n", |
| 1900 | file, linenum, args[1]); |
| 1901 | err_code |= ERR_ABORT; |
| 1902 | goto out; |
| 1903 | } |
| 1904 | tmp_cache_config->max_secondary_entries = max_sec_entries; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1905 | } |
| 1906 | else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1907 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1908 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1909 | goto out; |
| 1910 | } |
| 1911 | out: |
| 1912 | return err_code; |
| 1913 | } |
| 1914 | |
| 1915 | /* once the cache section is parsed */ |
| 1916 | |
| 1917 | int cfg_post_parse_section_cache() |
| 1918 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1919 | int err_code = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1920 | |
| 1921 | if (tmp_cache_config) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1922 | |
| 1923 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1924 | ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1925 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1926 | goto out; |
| 1927 | } |
| 1928 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1929 | if (!tmp_cache_config->maxobjsz) { |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1930 | /* Default max. file size is a 256th of the cache size. */ |
| 1931 | tmp_cache_config->maxobjsz = |
| 1932 | (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1933 | } |
| 1934 | else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) { |
| 1935 | ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2); |
| 1936 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1937 | goto out; |
| 1938 | } |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1939 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1940 | /* add to the list of cache to init and reinit tmp_cache_config |
| 1941 | * for next cache section, if any. |
| 1942 | */ |
| 1943 | LIST_ADDQ(&caches_config, &tmp_cache_config->list); |
| 1944 | tmp_cache_config = NULL; |
| 1945 | return err_code; |
| 1946 | } |
| 1947 | out: |
| 1948 | free(tmp_cache_config); |
| 1949 | tmp_cache_config = NULL; |
| 1950 | return err_code; |
| 1951 | |
| 1952 | } |
| 1953 | |
| 1954 | int post_check_cache() |
| 1955 | { |
| 1956 | struct proxy *px; |
| 1957 | struct cache *back, *cache_config, *cache; |
| 1958 | struct shared_context *shctx; |
| 1959 | int ret_shctx; |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1960 | int err_code = ERR_NONE; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1961 | |
| 1962 | list_for_each_entry_safe(cache_config, back, &caches_config, list) { |
| 1963 | |
| 1964 | ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE, |
| 1965 | cache_config->maxobjsz, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1966 | |
Frédéric Lécaille | bc58449 | 2018-10-25 20:18:59 +0200 | [diff] [blame] | 1967 | if (ret_shctx <= 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1968 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1969 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1970 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1971 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1972 | |
| 1973 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1974 | goto out; |
| 1975 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1976 | shctx->free_block = cache_free_blocks; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1977 | /* the cache structure is stored in the shctx and added to the |
| 1978 | * caches list, we can remove the entry from the caches_config |
| 1979 | * list */ |
| 1980 | memcpy(shctx->data, cache_config, sizeof(struct cache)); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1981 | cache = (struct cache *)shctx->data; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1982 | cache->entries = EB_ROOT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1983 | LIST_ADDQ(&caches, &cache->list); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1984 | LIST_DEL(&cache_config->list); |
| 1985 | free(cache_config); |
| 1986 | |
| 1987 | /* Find all references for this cache in the existing filters |
| 1988 | * (over all proxies) and reference it in matching filters. |
| 1989 | */ |
| 1990 | for (px = proxies_list; px; px = px->next) { |
| 1991 | struct flt_conf *fconf; |
| 1992 | struct cache_flt_conf *cconf; |
| 1993 | |
| 1994 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 1995 | if (fconf->id != cache_store_flt_id) |
| 1996 | continue; |
| 1997 | |
| 1998 | cconf = fconf->conf; |
| 1999 | if (!strcmp(cache->id, cconf->c.name)) { |
| 2000 | free(cconf->c.name); |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 2001 | cconf->flags |= CACHE_FLT_INIT; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2002 | cconf->c.cache = cache; |
| 2003 | break; |
| 2004 | } |
| 2005 | } |
| 2006 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2007 | } |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2008 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2009 | out: |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2010 | return err_code; |
| 2011 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2012 | } |
| 2013 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2014 | struct flt_ops cache_ops = { |
| 2015 | .init = cache_store_init, |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 2016 | .check = cache_store_check, |
| 2017 | .deinit = cache_store_deinit, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2018 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 2019 | /* Handle stream init/deinit */ |
| 2020 | .attach = cache_store_strm_init, |
| 2021 | .detach = cache_store_strm_deinit, |
| 2022 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 2023 | /* Handle channels activity */ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 2024 | .channel_post_analyze = cache_store_post_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 2025 | |
| 2026 | /* Filter HTTP requests and responses */ |
| 2027 | .http_headers = cache_store_http_headers, |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 2028 | .http_payload = cache_store_http_payload, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 2029 | .http_end = cache_store_http_end, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2030 | }; |
| 2031 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2032 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2033 | int accept_encoding_cmp(const void *a, const void *b) |
| 2034 | { |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2035 | unsigned int int_a = *(unsigned int*)a; |
| 2036 | unsigned int int_b = *(unsigned int*)b; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2037 | |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2038 | if (int_a < int_b) |
| 2039 | return -1; |
| 2040 | if (int_a > int_b) |
| 2041 | return 1; |
| 2042 | return 0; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2043 | } |
| 2044 | |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2045 | #define ACCEPT_ENCODING_MAX_ENTRIES 16 |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2046 | /* |
| 2047 | * Build a hash of the accept-encoding header. The different parts of the |
| 2048 | * header value are first sorted, appended and then a crc is calculated |
| 2049 | * for the newly constructed buffer. |
| 2050 | * Returns 0 in case of success. |
| 2051 | */ |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2052 | static int accept_encoding_normalizer(struct ist full_value, char *buf, unsigned int *buf_len) |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2053 | { |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2054 | unsigned int values[ACCEPT_ENCODING_MAX_ENTRIES] = {}; |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2055 | size_t count = 0; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2056 | char *comma = NULL; |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2057 | unsigned int hash_value = 0; |
| 2058 | unsigned int prev = 0, curr = 0; |
| 2059 | |
| 2060 | /* Turn accept-encoding value to lower case */ |
| 2061 | full_value = ist2bin_lc(istptr(full_value), full_value); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2062 | |
| 2063 | /* The hash will be built out of a sorted list of accepted encodings. */ |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2064 | while (count < (ACCEPT_ENCODING_MAX_ENTRIES - 1) && (comma = istchr(full_value, ',')) != NULL) { |
| 2065 | size_t length = comma - istptr(full_value); |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2066 | |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2067 | values[count++] = hash_crc32(istptr(full_value), length); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2068 | |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2069 | full_value = istadv(full_value, length + 1); |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2070 | |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2071 | } |
| 2072 | values[count++] = hash_crc32(istptr(full_value), istlen(full_value)); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2073 | |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2074 | /* Sort the values alphabetically. */ |
| 2075 | qsort(values, count, sizeof(*values), &accept_encoding_cmp); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2076 | |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2077 | while (count) { |
| 2078 | curr = values[--count]; |
| 2079 | if (curr != prev) { |
| 2080 | hash_value ^= curr; |
| 2081 | } |
| 2082 | prev = curr; |
| 2083 | } |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2084 | |
| 2085 | memcpy(buf, &hash_value, sizeof(hash_value)); |
| 2086 | *buf_len = sizeof(hash_value); |
| 2087 | |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2088 | return 0; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2089 | } |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2090 | #undef ACCEPT_ENCODING_MAX_ENTRIES |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2091 | |
| 2092 | /* |
| 2093 | * Normalizer used by default for User-Agent and Referer headers. It only |
| 2094 | * calculates a simple crc of the whole value. |
| 2095 | * Returns 0 in case of success. |
| 2096 | */ |
| 2097 | static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len) |
| 2098 | { |
| 2099 | int hash_value = 0; |
| 2100 | |
| 2101 | hash_value = hash_crc32(istptr(value), istlen(value)); |
| 2102 | |
| 2103 | memcpy(buf, &hash_value, sizeof(hash_value)); |
| 2104 | *buf_len = sizeof(hash_value); |
| 2105 | |
| 2106 | return 0; |
| 2107 | } |
| 2108 | |
| 2109 | |
| 2110 | /* |
| 2111 | * Pre-calculate the hashes of all the supported headers (in our Vary |
| 2112 | * implementation) of a given request. We have to calculate all the hashes |
| 2113 | * in advance because the actual Vary signature won't be known until the first |
| 2114 | * response. |
| 2115 | * Only the first occurrence of every header will be taken into account in the |
| 2116 | * hash. |
| 2117 | * If the header is not present, the hash portion of the given header will be |
| 2118 | * filled with zeros. |
| 2119 | * Returns 0 in case of success. |
| 2120 | */ |
| 2121 | static int http_request_prebuild_full_secondary_key(struct stream *s) |
| 2122 | { |
| 2123 | struct http_txn *txn = s->txn; |
| 2124 | struct htx *htx = htxbuf(&s->req.buf); |
| 2125 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 2126 | |
| 2127 | unsigned int idx; |
| 2128 | const struct vary_hashing_information *info = NULL; |
| 2129 | unsigned int hash_length = 0; |
| 2130 | int retval = 0; |
| 2131 | int offset = 0; |
| 2132 | |
| 2133 | for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) { |
| 2134 | info = &vary_information[idx]; |
| 2135 | |
| 2136 | ctx.blk = NULL; |
| 2137 | if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) { |
| 2138 | retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length); |
| 2139 | offset += hash_length; |
| 2140 | } |
| 2141 | else { |
| 2142 | /* Fill hash with 0s. */ |
| 2143 | hash_length = info->hash_length; |
| 2144 | memset(&txn->cache_secondary_hash[offset], 0, hash_length); |
| 2145 | offset += hash_length; |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | return retval; |
| 2150 | } |
| 2151 | |
| 2152 | |
| 2153 | /* |
| 2154 | * Calculate the secondary key for a request for which we already have a known |
| 2155 | * vary signature. The key is made by aggregating hashes calculated for every |
| 2156 | * header mentioned in the vary signature. |
| 2157 | * Only the first occurrence of every header will be taken into account in the |
| 2158 | * hash. |
| 2159 | * If the header is not present, the hash portion of the given header will be |
| 2160 | * filled with zeros. |
| 2161 | * Returns 0 in case of success. |
| 2162 | */ |
| 2163 | static int http_request_build_secondary_key(struct stream *s, int vary_signature) |
| 2164 | { |
| 2165 | struct http_txn *txn = s->txn; |
| 2166 | struct htx *htx = htxbuf(&s->req.buf); |
| 2167 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 2168 | |
| 2169 | unsigned int idx; |
| 2170 | const struct vary_hashing_information *info = NULL; |
| 2171 | unsigned int hash_length = 0; |
| 2172 | int retval = 0; |
| 2173 | int offset = 0; |
| 2174 | |
| 2175 | for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) { |
| 2176 | info = &vary_information[idx]; |
| 2177 | |
| 2178 | ctx.blk = NULL; |
| 2179 | if ((vary_signature & info->value) && info->norm_fn != NULL && |
| 2180 | http_find_header(htx, info->hdr_name, &ctx, 1)) { |
| 2181 | retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length); |
| 2182 | offset += hash_length; |
| 2183 | } |
| 2184 | else { |
| 2185 | /* Fill hash with 0s. */ |
| 2186 | hash_length = info->hash_length; |
| 2187 | memset(&txn->cache_secondary_hash[offset], 0, hash_length); |
| 2188 | offset += hash_length; |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | return retval; |
| 2193 | } |
| 2194 | |
| 2195 | /* |
| 2196 | * Build the actual secondary key of a given request out of the prebuilt key and |
| 2197 | * the actual vary signature (extracted from the response). |
| 2198 | * Returns 0 in case of success. |
| 2199 | */ |
| 2200 | static int http_request_reduce_secondary_key(unsigned int vary_signature, |
| 2201 | char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]) |
| 2202 | { |
| 2203 | int offset = 0; |
| 2204 | int global_offset = 0; |
| 2205 | int vary_info_count = 0; |
| 2206 | int keep = 0; |
| 2207 | unsigned int vary_idx; |
| 2208 | const struct vary_hashing_information *vary_info; |
| 2209 | |
| 2210 | vary_info_count = sizeof(vary_information)/sizeof(*vary_information); |
| 2211 | for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) { |
| 2212 | vary_info = &vary_information[vary_idx]; |
| 2213 | keep = (vary_signature & vary_info->value) ? 0xff : 0; |
| 2214 | |
| 2215 | for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) { |
| 2216 | prebuilt_key[global_offset] &= keep; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
| 2223 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2224 | |
| 2225 | static int |
| 2226 | parse_cache_flt(char **args, int *cur_arg, struct proxy *px, |
| 2227 | struct flt_conf *fconf, char **err, void *private) |
| 2228 | { |
| 2229 | struct flt_conf *f, *back; |
Willy Tarreau | a73da1e | 2018-12-14 10:19:28 +0100 | [diff] [blame] | 2230 | struct cache_flt_conf *cconf = NULL; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2231 | char *name = NULL; |
| 2232 | int pos = *cur_arg; |
| 2233 | |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 2234 | /* Get the cache filter name. <pos> point on "cache" keyword */ |
| 2235 | if (!*args[pos + 1]) { |
Tim Duesterhus | ea969f6 | 2020-08-18 22:06:51 +0200 | [diff] [blame] | 2236 | memprintf(err, "%s : expects a <name> argument", args[pos]); |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 2237 | goto error; |
| 2238 | } |
| 2239 | name = strdup(args[pos + 1]); |
| 2240 | if (!name) { |
| 2241 | memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]); |
| 2242 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2243 | } |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 2244 | pos += 2; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2245 | |
| 2246 | /* Check if an implicit filter with the same name already exists. If so, |
| 2247 | * we remove the implicit filter to use the explicit one. */ |
| 2248 | list_for_each_entry_safe(f, back, &px->filter_configs, list) { |
| 2249 | if (f->id != cache_store_flt_id) |
| 2250 | continue; |
| 2251 | |
| 2252 | cconf = f->conf; |
| 2253 | if (strcmp(name, cconf->c.name)) { |
| 2254 | cconf = NULL; |
| 2255 | continue; |
| 2256 | } |
| 2257 | |
| 2258 | if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 2259 | cconf = NULL; |
| 2260 | memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'", |
| 2261 | px->id, name); |
Tim Duesterhus | d34b1ce | 2020-01-18 01:46:18 +0100 | [diff] [blame] | 2262 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | /* Remove the implicit filter. <cconf> is kept for the explicit one */ |
| 2266 | LIST_DEL(&f->list); |
| 2267 | free(f); |
| 2268 | free(name); |
| 2269 | break; |
| 2270 | } |
| 2271 | |
| 2272 | /* No implicit cache filter found, create configuration for the explicit one */ |
| 2273 | if (!cconf) { |
| 2274 | cconf = calloc(1, sizeof(*cconf)); |
| 2275 | if (!cconf) { |
| 2276 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 2277 | goto error; |
| 2278 | } |
| 2279 | cconf->c.name = name; |
| 2280 | } |
| 2281 | |
| 2282 | cconf->flags = 0; |
| 2283 | fconf->id = cache_store_flt_id; |
| 2284 | fconf->conf = cconf; |
| 2285 | fconf->ops = &cache_ops; |
| 2286 | |
| 2287 | *cur_arg = pos; |
| 2288 | return 0; |
| 2289 | |
| 2290 | error: |
| 2291 | free(name); |
| 2292 | free(cconf); |
| 2293 | return -1; |
| 2294 | } |
| 2295 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 2296 | static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2297 | { |
| 2298 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2299 | return 1; |
| 2300 | |
| 2301 | return 0; |
| 2302 | } |
| 2303 | |
| 2304 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 2305 | { |
| 2306 | struct cache* cache = appctx->ctx.cli.p0; |
| 2307 | struct stream_interface *si = appctx->owner; |
| 2308 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2309 | if (cache == NULL) { |
| 2310 | cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
| 2311 | } |
| 2312 | |
| 2313 | list_for_each_entry_from(cache, &caches, list) { |
| 2314 | struct eb32_node *node = NULL; |
| 2315 | unsigned int next_key; |
| 2316 | struct cache_entry *entry; |
Remi Tricot-Le Breton | e3e1e5f | 2020-11-27 15:48:40 +0100 | [diff] [blame] | 2317 | unsigned int i; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2318 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2319 | next_key = appctx->ctx.cli.i0; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 2320 | if (!next_key) { |
| 2321 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
| 2322 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 2323 | si_rx_room_blk(si); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 2324 | return 0; |
| 2325 | } |
| 2326 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2327 | |
| 2328 | appctx->ctx.cli.p0 = cache; |
| 2329 | |
| 2330 | while (1) { |
| 2331 | |
| 2332 | shctx_lock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | e3e1e5f | 2020-11-27 15:48:40 +0100 | [diff] [blame] | 2333 | if (!node || (node = eb32_next_dup(node)) == NULL) |
| 2334 | node = eb32_lookup_ge(&cache->entries, next_key); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2335 | if (!node) { |
| 2336 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 2337 | appctx->ctx.cli.i0 = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2338 | break; |
| 2339 | } |
| 2340 | |
| 2341 | entry = container_of(node, struct cache_entry, eb); |
Remi Tricot-Le Breton | e3e1e5f | 2020-11-27 15:48:40 +0100 | [diff] [blame] | 2342 | chunk_printf(&trash, "%p hash:%u vary:0x", entry, read_u32(entry->hash)); |
| 2343 | for (i = 0; i < HTTP_CACHE_SEC_KEY_LEN; ++i) |
| 2344 | chunk_appendf(&trash, "%02x", (unsigned char)entry->secondary_key[i]); |
| 2345 | 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 Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2346 | |
| 2347 | next_key = node->key + 1; |
| 2348 | appctx->ctx.cli.i0 = next_key; |
| 2349 | |
| 2350 | shctx_unlock(shctx_ptr(cache)); |
| 2351 | |
| 2352 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 2353 | si_rx_room_blk(si); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2354 | return 0; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | } |
| 2359 | |
| 2360 | return 1; |
| 2361 | |
| 2362 | } |
| 2363 | |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 2364 | |
| 2365 | /* |
| 2366 | * boolean, returns true if response was built out of a cache entry. |
| 2367 | */ |
| 2368 | static int |
| 2369 | smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp, |
| 2370 | const char *kw, void *private) |
| 2371 | { |
| 2372 | smp->data.type = SMP_T_BOOL; |
| 2373 | smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0); |
| 2374 | |
| 2375 | return 1; |
| 2376 | } |
| 2377 | |
| 2378 | /* |
| 2379 | * string, returns cache name (if response came from a cache). |
| 2380 | */ |
| 2381 | static int |
| 2382 | smp_fetch_res_cache_name(const struct arg *args, struct sample *smp, |
| 2383 | const char *kw, void *private) |
| 2384 | { |
| 2385 | struct appctx *appctx = NULL; |
| 2386 | |
| 2387 | struct cache_flt_conf *cconf = NULL; |
| 2388 | struct cache *cache = NULL; |
| 2389 | |
| 2390 | if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type) |
| 2391 | return 0; |
| 2392 | |
| 2393 | /* Get appctx from the stream_interface. */ |
| 2394 | appctx = si_appctx(&smp->strm->si[1]); |
| 2395 | if (appctx && appctx->rule) { |
| 2396 | cconf = appctx->rule->arg.act.p[0]; |
| 2397 | if (cconf) { |
| 2398 | cache = cconf->c.cache; |
| 2399 | |
| 2400 | smp->data.type = SMP_T_STR; |
| 2401 | smp->flags = SMP_F_CONST; |
| 2402 | smp->data.u.str.area = cache->id; |
| 2403 | smp->data.u.str.data = strlen(cache->id); |
| 2404 | return 1; |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | return 0; |
| 2409 | } |
| 2410 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2411 | /* Declare the filter parser for "cache" keyword */ |
| 2412 | static struct flt_kw_list filter_kws = { "CACHE", { }, { |
| 2413 | { "cache", parse_cache_flt, NULL }, |
| 2414 | { NULL, NULL, NULL }, |
| 2415 | } |
| 2416 | }; |
| 2417 | |
| 2418 | INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws); |
| 2419 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2420 | static struct cli_kw_list cli_kws = {{},{ |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 2421 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
| 2422 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2423 | }}; |
| 2424 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2425 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2426 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2427 | static struct action_kw_list http_res_actions = { |
| 2428 | .kw = { |
| 2429 | { "cache-store", parse_cache_store }, |
| 2430 | { NULL, NULL } |
| 2431 | } |
| 2432 | }; |
| 2433 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2434 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
| 2435 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2436 | static struct action_kw_list http_req_actions = { |
| 2437 | .kw = { |
| 2438 | { "cache-use", parse_cache_use }, |
| 2439 | { NULL, NULL } |
| 2440 | } |
| 2441 | }; |
| 2442 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2443 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 2444 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 2445 | struct applet http_cache_applet = { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2446 | .obj_type = OBJ_TYPE_APPLET, |
| 2447 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 2448 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 2449 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2450 | }; |
| 2451 | |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 2452 | /* config parsers for this section */ |
| 2453 | REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2454 | REGISTER_POST_CHECK(post_check_cache); |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 2455 | |
| 2456 | |
| 2457 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2458 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 2459 | { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE }, |
| 2460 | { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE }, |
| 2461 | { /* END */ }, |
| 2462 | } |
| 2463 | }; |
| 2464 | |
| 2465 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |