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 | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 52 | uint8_t vary_processing_enabled; /* boolean : manage Vary header (disabled by default) */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 53 | char id[33]; /* cache name */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 56 | /* cache config for filters */ |
| 57 | struct cache_flt_conf { |
| 58 | union { |
| 59 | struct cache *cache; /* cache used by the filter */ |
| 60 | char *name; /* cache name used during conf parsing */ |
| 61 | } c; |
| 62 | unsigned int flags; /* CACHE_FLT_F_* */ |
| 63 | }; |
| 64 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Vary-related structures and functions |
| 68 | */ |
| 69 | enum vary_header_bit { |
| 70 | VARY_ACCEPT_ENCODING = (1 << 0), |
| 71 | VARY_REFERER = (1 << 1), |
| 72 | VARY_LAST /* should always be last */ |
| 73 | }; |
| 74 | |
| 75 | typedef int(*http_header_normalizer)(struct ist value, char *buf, unsigned int *buf_len); |
| 76 | |
| 77 | struct vary_hashing_information { |
| 78 | struct ist hdr_name; /* Header name */ |
| 79 | enum vary_header_bit value; /* Bit repesenting the header in a vary signature */ |
| 80 | unsigned int hash_length; /* Size of the sub hash for this header's value */ |
| 81 | http_header_normalizer norm_fn; /* Normalization function */ |
| 82 | }; |
| 83 | |
| 84 | static int accept_encoding_normalizer(struct ist value, char *buf, unsigned int *buf_len); |
| 85 | static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len); |
| 86 | |
| 87 | /* Warning : do not forget to update HTTP_CACHE_SEC_KEY_LEN when new items are |
| 88 | * added to this array. */ |
| 89 | const struct vary_hashing_information vary_information[] = { |
| 90 | { IST("accept-encoding"), VARY_ACCEPT_ENCODING, sizeof(int), &accept_encoding_normalizer }, |
| 91 | { IST("referer"), VARY_REFERER, sizeof(int), &default_normalizer }, |
| 92 | }; |
| 93 | |
| 94 | static int http_request_prebuild_full_secondary_key(struct stream *s); |
| 95 | static int http_request_build_secondary_key(struct stream *s, int vary_signature); |
| 96 | static int http_request_reduce_secondary_key(unsigned int vary_signature, |
| 97 | char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]); |
| 98 | |
| 99 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 100 | /* |
| 101 | * cache ctx for filters |
| 102 | */ |
| 103 | struct cache_st { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 104 | struct shared_block *first_block; |
| 105 | }; |
| 106 | |
| 107 | struct cache_entry { |
| 108 | unsigned int latest_validation; /* latest validation date */ |
| 109 | unsigned int expire; /* expiration date */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 110 | unsigned int age; /* Origin server "Age" header value */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 111 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 112 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 113 | char hash[20]; |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 114 | |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 115 | char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */ |
| 116 | unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used |
| 117 | * to build secondary keys for this cache entry. */ |
| 118 | |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 119 | unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */ |
| 120 | unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */ |
| 121 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 122 | time_t last_modified; /* Origin server "Last-Modified" header value converted in |
| 123 | * seconds since epoch. If no "Last-Modified" |
| 124 | * header is found, use "Date" header value, |
| 125 | * otherwise use reception time. This field will |
| 126 | * be used in case of an "If-Modified-Since"-based |
| 127 | * conditional request. */ |
| 128 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 129 | unsigned char data[0]; |
| 130 | }; |
| 131 | |
| 132 | #define CACHE_BLOCKSIZE 1024 |
Willy Tarreau | 96062a1 | 2018-11-11 14:00:28 +0100 | [diff] [blame] | 133 | #define CACHE_ENTRY_MAX_AGE 2147483648U |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 134 | |
| 135 | static struct list caches = LIST_HEAD_INIT(caches); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 136 | 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] | 137 | static struct cache *tmp_cache_config = NULL; |
| 138 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 139 | DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st)); |
| 140 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 141 | struct cache_entry *entry_exist(struct cache *cache, char *hash) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 142 | { |
| 143 | struct eb32_node *node; |
| 144 | struct cache_entry *entry; |
| 145 | |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 146 | node = eb32_lookup(&cache->entries, read_u32(hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 147 | if (!node) |
| 148 | return NULL; |
| 149 | |
| 150 | entry = eb32_entry(node, struct cache_entry, eb); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 151 | |
| 152 | /* if that's not the right node */ |
| 153 | if (memcmp(entry->hash, hash, sizeof(entry->hash))) |
| 154 | return NULL; |
| 155 | |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 156 | if (entry->expire > now.tv_sec) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 157 | return entry; |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 158 | } else { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 159 | eb32_delete(node); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 160 | entry->eb.key = 0; |
| 161 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 162 | return NULL; |
| 163 | |
| 164 | } |
| 165 | |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 166 | /* |
| 167 | * There can be multiple entries with the same primary key in the ebtree so in |
| 168 | * order to get the proper one out of the list, we use a secondary_key. |
| 169 | * This function simply iterates over all the entries with the same primary_key |
| 170 | * until it finds the right one. |
| 171 | * Returns the cache_entry in case of success, NULL otherwise. |
| 172 | */ |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 173 | struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry, |
| 174 | char *secondary_key) |
| 175 | { |
| 176 | struct eb32_node *node = &entry->eb; |
| 177 | |
| 178 | if (!entry->secondary_key_signature) |
| 179 | return NULL; |
| 180 | |
| 181 | while (entry && memcmp(entry->secondary_key, secondary_key, HTTP_CACHE_SEC_KEY_LEN) != 0) { |
| 182 | node = eb32_next_dup(node); |
| 183 | entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL; |
| 184 | } |
| 185 | |
| 186 | /* Expired entry */ |
| 187 | if (entry && entry->expire <= now.tv_sec) { |
| 188 | eb32_delete(&entry->eb); |
| 189 | entry->eb.key = 0; |
| 190 | entry = NULL; |
| 191 | } |
| 192 | |
| 193 | return entry; |
| 194 | } |
| 195 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 196 | static inline struct shared_context *shctx_ptr(struct cache *cache) |
| 197 | { |
| 198 | return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data); |
| 199 | } |
| 200 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 201 | static inline struct shared_block *block_ptr(struct cache_entry *entry) |
| 202 | { |
| 203 | return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 208 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 209 | cache_store_init(struct proxy *px, struct flt_conf *fconf) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 210 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 211 | fconf->flags |= FLT_CFG_FL_HTX; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 215 | static void |
| 216 | cache_store_deinit(struct proxy *px, struct flt_conf *fconf) |
| 217 | { |
| 218 | struct cache_flt_conf *cconf = fconf->conf; |
| 219 | |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 220 | if (!(cconf->flags & CACHE_FLT_INIT)) |
| 221 | free(cconf->c.name); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 222 | free(cconf); |
| 223 | } |
| 224 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 225 | static int |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 226 | cache_store_check(struct proxy *px, struct flt_conf *fconf) |
| 227 | { |
| 228 | struct cache_flt_conf *cconf = fconf->conf; |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 229 | struct flt_conf *f; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 230 | struct cache *cache; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 231 | int comp = 0; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 232 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 233 | /* Find the cache corresponding to the name in the filter config. The |
| 234 | * cache will not be referenced now in the filter config because it is |
| 235 | * not fully allocated. This step will be performed during the cache |
| 236 | * post_check. |
| 237 | */ |
| 238 | list_for_each_entry(cache, &caches_config, list) { |
| 239 | if (!strcmp(cache->id, cconf->c.name)) |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 240 | goto found; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 244 | proxy_type_str(px), px->id, (char *)cconf->c.name); |
| 245 | return 1; |
| 246 | |
| 247 | found: |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 248 | /* Here <cache> points on the cache the filter must use and <cconf> |
| 249 | * points on the cache filter configuration. */ |
| 250 | |
| 251 | /* Check all filters for proxy <px> to know if the compression is |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 252 | * enabled and if it is after the cache. When the compression is before |
| 253 | * the cache, an error is returned. Also check if the cache filter must |
| 254 | * be explicitly declaired or not. */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 255 | list_for_each_entry(f, &px->filter_configs, list) { |
| 256 | if (f == fconf) { |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 257 | /* The compression filter must be evaluated after the cache. */ |
| 258 | if (comp) { |
| 259 | ha_alert("config: %s '%s': unable to enable the compression filter before " |
| 260 | "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 261 | return 1; |
| 262 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 263 | } |
Christopher Faulet | 8f7fe1c | 2019-07-15 15:08:25 +0200 | [diff] [blame] | 264 | else if (f->id == http_comp_flt_id) |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 265 | comp = 1; |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 266 | else if (f->id == fcgi_flt_id) |
| 267 | continue; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 268 | else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 269 | /* Implicit declaration is only allowed with the |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 270 | * compression and fcgi. For other filters, an implicit |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 271 | * declaration is required. */ |
| 272 | ha_alert("config: %s '%s': require an explicit filter declaration " |
| 273 | "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 274 | return 1; |
| 275 | } |
| 276 | |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 277 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 282 | cache_store_strm_init(struct stream *s, struct filter *filter) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 283 | { |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 284 | struct cache_st *st; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 285 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 286 | st = pool_alloc_dirty(pool_head_cache_st); |
| 287 | if (st == NULL) |
| 288 | return -1; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 289 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 290 | st->first_block = NULL; |
| 291 | filter->ctx = st; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 292 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 293 | /* Register post-analyzer on AN_RES_WAIT_HTTP */ |
| 294 | filter->post_analyzers |= AN_RES_WAIT_HTTP; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 295 | return 1; |
| 296 | } |
| 297 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 298 | static void |
| 299 | cache_store_strm_deinit(struct stream *s, struct filter *filter) |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 300 | { |
| 301 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 302 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 303 | struct cache *cache = cconf->c.cache; |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 304 | struct shared_context *shctx = shctx_ptr(cache); |
| 305 | |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 306 | /* Everything should be released in the http_end filter, but we need to do it |
| 307 | * there too, in case of errors */ |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 308 | if (st && st->first_block) { |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 309 | shctx_lock(shctx); |
| 310 | shctx_row_dec_hot(shctx, st->first_block); |
| 311 | shctx_unlock(shctx); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 312 | } |
| 313 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 314 | pool_free(pool_head_cache_st, st); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 315 | filter->ctx = NULL; |
| 316 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 319 | static int |
| 320 | cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn, |
| 321 | unsigned an_bit) |
| 322 | { |
| 323 | struct http_txn *txn = s->txn; |
| 324 | struct http_msg *msg = &txn->rsp; |
| 325 | struct cache_st *st = filter->ctx; |
| 326 | |
| 327 | if (an_bit != AN_RES_WAIT_HTTP) |
| 328 | goto end; |
| 329 | |
| 330 | /* Here we need to check if any compression filter precedes the cache |
| 331 | * filter. This is only possible when the compression is configured in |
| 332 | * the frontend while the cache filter is configured on the |
| 333 | * backend. This case cannot be detected during HAProxy startup. So in |
| 334 | * such cases, the cache is disabled. |
| 335 | */ |
| 336 | if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) { |
| 337 | pool_free(pool_head_cache_st, st); |
| 338 | filter->ctx = NULL; |
| 339 | } |
| 340 | |
| 341 | end: |
| 342 | return 1; |
| 343 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 344 | |
| 345 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 346 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 347 | { |
| 348 | struct cache_st *st = filter->ctx; |
| 349 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 350 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 351 | return 1; |
| 352 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 353 | if (st->first_block) |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 354 | register_data_filter(s, msg->chn, filter); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 355 | return 1; |
| 356 | } |
| 357 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 358 | static inline void disable_cache_entry(struct cache_st *st, |
| 359 | struct filter *filter, struct shared_context *shctx) |
| 360 | { |
| 361 | struct cache_entry *object; |
| 362 | |
| 363 | object = (struct cache_entry *)st->first_block->data; |
| 364 | filter->ctx = NULL; /* disable cache */ |
| 365 | shctx_lock(shctx); |
| 366 | shctx_row_dec_hot(shctx, st->first_block); |
| 367 | object->eb.key = 0; |
| 368 | shctx_unlock(shctx); |
| 369 | pool_free(pool_head_cache_st, st); |
| 370 | } |
| 371 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 372 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 373 | cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 374 | unsigned int offset, unsigned int len) |
| 375 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 376 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 377 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 378 | struct cache_st *st = filter->ctx; |
| 379 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 380 | struct htx_blk *blk; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 381 | struct shared_block *fb; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 382 | struct htx_ret htxret; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 383 | unsigned int orig_len, to_forward; |
| 384 | int ret; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 385 | |
| 386 | if (!len) |
| 387 | return len; |
| 388 | |
| 389 | if (!st->first_block) { |
| 390 | unregister_data_filter(s, msg->chn, filter); |
| 391 | return len; |
| 392 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 393 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 394 | chunk_reset(&trash); |
| 395 | orig_len = len; |
| 396 | to_forward = 0; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 397 | |
| 398 | htxret = htx_find_offset(htx, offset); |
| 399 | blk = htxret.blk; |
| 400 | offset = htxret.ret; |
| 401 | for (; blk && len; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 402 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 403 | uint32_t info, sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 404 | struct ist v; |
| 405 | |
| 406 | switch (type) { |
| 407 | case HTX_BLK_UNUSED: |
| 408 | break; |
| 409 | |
| 410 | case HTX_BLK_DATA: |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 411 | v = htx_get_blk_value(htx, blk); |
| 412 | v.ptr += offset; |
| 413 | v.len -= offset; |
| 414 | if (v.len > len) |
| 415 | v.len = len; |
| 416 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 417 | info = (type << 28) + v.len; |
| 418 | chunk_memcat(&trash, (char *)&info, sizeof(info)); |
| 419 | chunk_memcat(&trash, v.ptr, v.len); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 420 | to_forward += v.len; |
| 421 | len -= v.len; |
| 422 | break; |
| 423 | |
| 424 | default: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 425 | /* Here offset must always be 0 because only |
| 426 | * DATA blocks can be partially transferred. */ |
| 427 | if (offset) |
| 428 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 429 | if (sz > len) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 430 | goto end; |
| 431 | |
| 432 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 433 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 434 | to_forward += sz; |
| 435 | len -= sz; |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | offset = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 440 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 441 | |
| 442 | end: |
| 443 | shctx_lock(shctx); |
| 444 | fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data); |
| 445 | if (!fb) { |
| 446 | shctx_unlock(shctx); |
| 447 | goto no_cache; |
| 448 | } |
| 449 | shctx_unlock(shctx); |
| 450 | |
| 451 | ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append, |
| 452 | (unsigned char *)b_head(&trash), b_data(&trash)); |
| 453 | if (ret < 0) |
| 454 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 455 | |
| 456 | return to_forward; |
| 457 | |
| 458 | no_cache: |
| 459 | disable_cache_entry(st, filter, shctx); |
| 460 | unregister_data_filter(s, msg->chn, filter); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 461 | return orig_len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 465 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 466 | struct http_msg *msg) |
| 467 | { |
| 468 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 469 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 470 | struct cache *cache = cconf->c.cache; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 471 | struct shared_context *shctx = shctx_ptr(cache); |
| 472 | struct cache_entry *object; |
| 473 | |
| 474 | if (!(msg->chn->flags & CF_ISRESP)) |
| 475 | return 1; |
| 476 | |
| 477 | if (st && st->first_block) { |
| 478 | |
| 479 | object = (struct cache_entry *)st->first_block->data; |
| 480 | |
| 481 | /* does not need to test if the insertion worked, if it |
| 482 | * doesn't, the blocks will be reused anyway */ |
| 483 | |
| 484 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 485 | if (eb32_insert(&cache->entries, &object->eb) != &object->eb) { |
| 486 | object->eb.key = 0; |
| 487 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 488 | /* remove from the hotlist */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 489 | shctx_row_dec_hot(shctx, st->first_block); |
| 490 | shctx_unlock(shctx); |
| 491 | |
| 492 | } |
| 493 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 494 | pool_free(pool_head_cache_st, st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 495 | filter->ctx = NULL; |
| 496 | } |
| 497 | |
| 498 | return 1; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * This intends to be used when checking HTTP headers for some |
| 503 | * word=value directive. Return a pointer to the first character of value, if |
| 504 | * the word was not found or if there wasn't any value assigned ot it return NULL |
| 505 | */ |
| 506 | char *directive_value(const char *sample, int slen, const char *word, int wlen) |
| 507 | { |
| 508 | int st = 0; |
| 509 | |
| 510 | if (slen < wlen) |
| 511 | return 0; |
| 512 | |
| 513 | while (wlen) { |
| 514 | char c = *sample ^ *word; |
| 515 | if (c && c != ('A' ^ 'a')) |
| 516 | return NULL; |
| 517 | sample++; |
| 518 | word++; |
| 519 | slen--; |
| 520 | wlen--; |
| 521 | } |
| 522 | |
| 523 | while (slen) { |
| 524 | if (st == 0) { |
| 525 | if (*sample != '=') |
| 526 | return NULL; |
| 527 | sample++; |
| 528 | slen--; |
| 529 | st = 1; |
| 530 | continue; |
| 531 | } else { |
| 532 | return (char *)sample; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | return NULL; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Return the maxage in seconds of an HTTP response. |
| 541 | * Compute the maxage using either: |
| 542 | * - the assigned max-age of the cache |
| 543 | * - the s-maxage directive |
| 544 | * - the max-age directive |
| 545 | * - (Expires - Data) headers |
| 546 | * - the default-max-age of the cache |
| 547 | * |
| 548 | */ |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 549 | int http_calc_maxage(struct stream *s, struct cache *cache) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 550 | { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 551 | struct htx *htx = htxbuf(&s->res.buf); |
| 552 | struct http_hdr_ctx ctx = { .blk = NULL }; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 553 | int smaxage = -1; |
| 554 | int maxage = -1; |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 555 | int expires = -1; |
| 556 | struct tm tm = {}; |
| 557 | time_t expires_val = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 558 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 559 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 560 | char *value; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 561 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 562 | value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8); |
| 563 | if (value) { |
| 564 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 565 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 566 | chunk_strncat(chk, value, ctx.value.len - 8 + 1); |
| 567 | chunk_strncat(chk, "", 1); |
Remi Tricot-Le Breton | 8c2db71 | 2020-10-30 14:26:13 +0100 | [diff] [blame] | 568 | smaxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 571 | value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7); |
| 572 | if (value) { |
| 573 | struct buffer *chk = get_trash_chunk(); |
Christopher Faulet | 5f2c49f | 2019-07-15 20:49:46 +0200 | [diff] [blame] | 574 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 575 | chunk_strncat(chk, value, ctx.value.len - 7 + 1); |
| 576 | chunk_strncat(chk, "", 1); |
Remi Tricot-Le Breton | 8c2db71 | 2020-10-30 14:26:13 +0100 | [diff] [blame] | 577 | maxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 581 | /* Look for Expires header if no s-maxage or max-age Cache-Control data |
| 582 | * was found. */ |
| 583 | if (maxage == -1 && smaxage == -1) { |
| 584 | ctx.blk = NULL; |
| 585 | if (http_find_header(htx, ist("expires"), &ctx, 1)) { |
| 586 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 587 | expires_val = my_timegm(&tm); |
| 588 | /* A request having an expiring date earlier |
| 589 | * than the current date should be considered as |
| 590 | * stale. */ |
| 591 | expires = (expires_val >= now.tv_sec) ? |
| 592 | (expires_val - now.tv_sec) : 0; |
| 593 | } |
| 594 | else { |
| 595 | /* Following RFC 7234#5.3, an invalid date |
| 596 | * format must be treated as a date in the past |
| 597 | * so the cache entry must be seen as already |
| 598 | * expired. */ |
| 599 | expires = 0; |
| 600 | } |
| 601 | } |
| 602 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 603 | |
| 604 | |
| 605 | if (smaxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 606 | return MIN(smaxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 607 | |
| 608 | if (maxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 609 | return MIN(maxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 610 | |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 611 | if (expires >= 0) |
| 612 | return MIN(expires, cache->maxage); |
| 613 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 614 | return cache->maxage; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 615 | |
| 616 | } |
| 617 | |
| 618 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 619 | static void cache_free_blocks(struct shared_block *first, struct shared_block *block) |
| 620 | { |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 621 | struct cache_entry *object = (struct cache_entry *)block->data; |
| 622 | |
| 623 | if (first == block && object->eb.key) |
| 624 | eb32_delete(&object->eb); |
| 625 | object->eb.key = 0; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 626 | } |
| 627 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 628 | |
| 629 | /* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the |
| 630 | * date value should be compared to a date determined by in a previous response (for |
| 631 | * the same entity). This date could either be the "Last-Modified" value, or the "Date" |
| 632 | * value of the response's reception time (by decreasing order of priority). */ |
| 633 | static time_t get_last_modified_time(struct htx *htx) |
| 634 | { |
| 635 | time_t last_modified = 0; |
| 636 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 637 | struct tm tm = {}; |
| 638 | |
| 639 | if (http_find_header(htx, ist("last-modified"), &ctx, 1)) { |
| 640 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 641 | last_modified = my_timegm(&tm); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | if (!last_modified) { |
| 646 | ctx.blk = NULL; |
| 647 | if (http_find_header(htx, ist("date"), &ctx, 1)) { |
| 648 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 649 | last_modified = my_timegm(&tm); |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /* Fallback on the current time if no "Last-Modified" or "Date" header |
| 655 | * was found. */ |
| 656 | if (!last_modified) |
| 657 | last_modified = now.tv_sec; |
| 658 | |
| 659 | return last_modified; |
| 660 | } |
| 661 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 662 | /* |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 663 | * Checks the vary header's value. The headers on which vary should be applied |
| 664 | * must be explicitely supported in the vary_information array (see cache.c). If |
| 665 | * any other header is mentioned, we won't store the response. |
| 666 | * Returns 1 if Vary-based storage can work, 0 otherwise. |
| 667 | */ |
| 668 | static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature) |
| 669 | { |
| 670 | unsigned int vary_idx; |
| 671 | unsigned int vary_info_count; |
| 672 | const struct vary_hashing_information *vary_info; |
| 673 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 674 | |
| 675 | int retval = 1; |
| 676 | |
| 677 | *vary_signature = 0; |
| 678 | |
| 679 | vary_info_count = sizeof(vary_information)/sizeof(*vary_information); |
| 680 | while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) { |
| 681 | for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) { |
| 682 | vary_info = &vary_information[vary_idx]; |
| 683 | if (isteqi(ctx.value, vary_info->hdr_name)) { |
| 684 | *vary_signature |= vary_info->value; |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | retval = (vary_idx < vary_info_count); |
| 689 | } |
| 690 | |
| 691 | return retval; |
| 692 | } |
| 693 | |
| 694 | |
| 695 | |
| 696 | /* |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 697 | * 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] | 698 | * register a filter to store the data |
| 699 | */ |
| 700 | 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] | 701 | struct session *sess, struct stream *s, int flags) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 702 | { |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 703 | unsigned int age; |
| 704 | long long hdr_age; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 705 | struct http_txn *txn = s->txn; |
| 706 | struct http_msg *msg = &txn->rsp; |
| 707 | struct filter *filter; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 708 | struct shared_block *first = NULL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 709 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 710 | struct cache *cache = cconf->c.cache; |
| 711 | struct shared_context *shctx = shctx_ptr(cache); |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 712 | struct cache_st *cache_ctx = NULL; |
| 713 | struct cache_entry *object, *old; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 714 | unsigned int key = read_u32(txn->cache_hash); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 715 | struct htx *htx; |
| 716 | struct http_hdr_ctx ctx; |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 717 | size_t hdrs_len = 0; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 718 | int32_t pos; |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 719 | unsigned int etag_length = 0; |
| 720 | unsigned int etag_offset = 0; |
| 721 | struct ist header_name = IST_NULL; |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 722 | time_t last_modified = 0; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 723 | unsigned int vary_signature = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 724 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 725 | /* Don't cache if the response came from a cache */ |
| 726 | if ((obj_type(s->target) == OBJ_TYPE_APPLET) && |
| 727 | s->target == &http_cache_applet.obj_type) { |
| 728 | goto out; |
| 729 | } |
| 730 | |
| 731 | /* cache only HTTP/1.1 */ |
| 732 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 733 | goto out; |
| 734 | |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 735 | /* cache only GET method */ |
| 736 | if (txn->meth != HTTP_METH_GET) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 737 | goto out; |
| 738 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 739 | /* cache key was not computed */ |
| 740 | if (!key) |
| 741 | goto out; |
| 742 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 743 | /* cache only 200 status code */ |
| 744 | if (txn->status != 200) |
| 745 | goto out; |
| 746 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 747 | /* Find the corresponding filter instance for the current stream */ |
| 748 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 749 | if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) { |
| 750 | /* No filter ctx, don't cache anything */ |
| 751 | if (!filter->ctx) |
| 752 | goto out; |
| 753 | cache_ctx = filter->ctx; |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /* from there, cache_ctx is always defined */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 759 | htx = htxbuf(&s->res.buf); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 760 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 761 | /* Do not cache too big objects. */ |
| 762 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 && |
| 763 | htx->data + htx->extra > shctx->max_obj_size) |
| 764 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 765 | |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 766 | /* Only a subset of headers are supported in our Vary implementation. If |
| 767 | * 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] | 768 | * able to use the cache. Likewise, if Vary header support is disabled, |
| 769 | * avoid caching responses that contain such a header. */ |
| 770 | ctx.blk = NULL; |
| 771 | if (cache->vary_processing_enabled) { |
| 772 | if (!http_check_vary_header(htx, &vary_signature)) |
| 773 | goto out; |
| 774 | } |
| 775 | else if (http_find_header(htx, ist("Vary"), &ctx, 0)) { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 776 | goto out; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 777 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 778 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 779 | http_check_response_for_cacheability(s, &s->res); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 780 | |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 781 | 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] | 782 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 783 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 784 | age = 0; |
| 785 | ctx.blk = NULL; |
| 786 | if (http_find_header(htx, ist("Age"), &ctx, 0)) { |
| 787 | if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) { |
| 788 | if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE)) |
| 789 | hdr_age = CACHE_ENTRY_MAX_AGE; |
| 790 | age = hdr_age; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 791 | } |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 792 | http_remove_header(htx, &ctx); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 793 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 794 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 795 | /* Build a last-modified time that will be stored in the cache_entry and |
| 796 | * compared to a future If-Modified-Since client header. */ |
| 797 | last_modified = get_last_modified_time(htx); |
| 798 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 799 | chunk_reset(&trash); |
| 800 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 801 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 802 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 803 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 804 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 805 | hdrs_len += sizeof(*blk) + sz; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 806 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 807 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 808 | |
| 809 | /* Look for optional ETag header. |
| 810 | * We need to store the offset of the ETag value in order for |
| 811 | * future conditional requests to be able to perform ETag |
| 812 | * comparisons. */ |
| 813 | if (type == HTX_BLK_HDR) { |
| 814 | header_name = htx_get_blk_name(htx, blk); |
| 815 | if (isteq(header_name, ist("etag"))) { |
| 816 | etag_length = sz - istlen(header_name); |
| 817 | etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name); |
| 818 | } |
| 819 | } |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 820 | if (type == HTX_BLK_EOH) |
| 821 | break; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 824 | /* Do not cache objects if the headers are too big. */ |
| 825 | if (hdrs_len > htx->size - global.tune.maxrewrite) |
| 826 | goto out; |
| 827 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 828 | shctx_lock(shctx); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 829 | first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 830 | if (!first) { |
| 831 | shctx_unlock(shctx); |
| 832 | goto out; |
| 833 | } |
| 834 | shctx_unlock(shctx); |
| 835 | |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 836 | /* the received memory is not initialized, we need at least to mark |
| 837 | * the object as not indexed yet. |
| 838 | */ |
| 839 | object = (struct cache_entry *)first->data; |
| 840 | object->eb.node.leaf_p = NULL; |
| 841 | object->eb.key = 0; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 842 | object->age = age; |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 843 | object->last_modified = last_modified; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 844 | object->secondary_key_signature = vary_signature; |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 845 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 846 | /* reserve space for the cache_entry structure */ |
| 847 | first->len = sizeof(struct cache_entry); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 848 | first->last_append = NULL; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 849 | /* cache the headers in a http action because it allows to chose what |
| 850 | * to cache, for example you might want to cache a response before |
| 851 | * modifying some HTTP headers, or on the contrary after modifying |
| 852 | * those headers. |
| 853 | */ |
| 854 | |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 855 | /* Write the ETag information in the cache_entry if needed. */ |
| 856 | object->etag_length = etag_length; |
| 857 | object->etag_offset = etag_offset; |
| 858 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 859 | /* does not need to be locked because it's in the "hot" list, |
| 860 | * copy the headers */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 861 | if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0) |
| 862 | goto out; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 863 | |
| 864 | /* register the buffer in the filter ctx for filling it with data*/ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 865 | if (cache_ctx) { |
| 866 | cache_ctx->first_block = first; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 867 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 868 | object->eb.key = key; |
| 869 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 870 | memcpy(object->hash, txn->cache_hash, sizeof(object->hash)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 871 | |
| 872 | /* Add the current request's secondary key to the buffer if needed. */ |
| 873 | if (vary_signature) { |
| 874 | http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash); |
| 875 | memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN); |
| 876 | } |
| 877 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 878 | /* Insert the node later on caching success */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 879 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 880 | shctx_lock(shctx); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 881 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 882 | old = entry_exist(cconf->c.cache, txn->cache_hash); |
| 883 | if (old) { |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 884 | if (vary_signature) |
| 885 | old = secondary_entry_exist(cconf->c.cache, old, |
| 886 | txn->cache_secondary_hash); |
| 887 | |
| 888 | if (old) { |
| 889 | eb32_delete(&old->eb); |
| 890 | old->eb.key = 0; |
| 891 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 892 | } |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 893 | shctx_unlock(shctx); |
| 894 | |
| 895 | /* store latest value and expiration time */ |
| 896 | object->latest_validation = now.tv_sec; |
| 897 | object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache); |
| 898 | return ACT_RET_CONT; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | out: |
| 902 | /* if does not cache */ |
| 903 | if (first) { |
| 904 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 905 | first->len = 0; |
| 906 | object->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 907 | shctx_row_dec_hot(shctx, first); |
| 908 | shctx_unlock(shctx); |
| 909 | } |
| 910 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 911 | return ACT_RET_CONT; |
| 912 | } |
| 913 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 914 | #define HTX_CACHE_INIT 0 /* Initial state. */ |
| 915 | #define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */ |
| 916 | #define HTX_CACHE_DATA 2 /* Cache entry data forwarding */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 917 | #define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */ |
| 918 | #define HTX_CACHE_END 4 /* Cache entry treatment terminated */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 919 | |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 920 | static void http_cache_applet_release(struct appctx *appctx) |
| 921 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 922 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 923 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 924 | struct cache *cache = cconf->c.cache; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 925 | struct shared_block *first = block_ptr(cache_ptr); |
| 926 | |
| 927 | shctx_lock(shctx_ptr(cache)); |
| 928 | shctx_row_dec_hot(shctx_ptr(cache), first); |
| 929 | shctx_unlock(shctx_ptr(cache)); |
| 930 | } |
| 931 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 932 | |
| 933 | static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type, |
| 934 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 935 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 936 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 937 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 938 | struct htx_blk *blk; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 939 | char *ptr; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 940 | unsigned int max, total; |
| 941 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 942 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 943 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 944 | if (!max) |
| 945 | return 0; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 946 | blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 947 | ? (info & 0xff) + ((info >> 8) & 0xfffff) |
| 948 | : info & 0xfffffff); |
| 949 | if (blksz > max) |
| 950 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 951 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 952 | blk = htx_add_blk(htx, type, blksz); |
| 953 | if (!blk) |
| 954 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 955 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 956 | blk->info = info; |
| 957 | total = 4; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 958 | ptr = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 959 | while (blksz) { |
| 960 | max = MIN(blksz, shctx->block_size - offset); |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 961 | memcpy(ptr, (const char *)shblk->data + offset, max); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 962 | offset += max; |
| 963 | blksz -= max; |
| 964 | total += max; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 965 | ptr += max; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 966 | if (blksz || offset == shctx->block_size) { |
| 967 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 968 | offset = 0; |
| 969 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 970 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 971 | appctx->ctx.cache.offset = offset; |
| 972 | appctx->ctx.cache.next = shblk; |
| 973 | appctx->ctx.cache.sent += total; |
| 974 | return total; |
| 975 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 976 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 977 | static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx, |
| 978 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
| 979 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 980 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 981 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 982 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
| 983 | unsigned int max, total, rem_data; |
| 984 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 985 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 986 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 987 | if (!max) |
| 988 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 989 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 990 | rem_data = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 991 | if (appctx->ctx.cache.rem_data) { |
| 992 | blksz = appctx->ctx.cache.rem_data; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 993 | total = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 994 | } |
| 995 | else { |
| 996 | blksz = (info & 0xfffffff); |
| 997 | total = 4; |
| 998 | } |
| 999 | if (blksz > max) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1000 | rem_data = blksz - max; |
| 1001 | blksz = max; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1004 | while (blksz) { |
| 1005 | size_t sz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1006 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1007 | max = MIN(blksz, shctx->block_size - offset); |
| 1008 | sz = htx_add_data(htx, ist2(shblk->data + offset, max)); |
| 1009 | offset += sz; |
| 1010 | blksz -= sz; |
| 1011 | total += sz; |
| 1012 | if (sz < max) |
| 1013 | break; |
| 1014 | if (blksz || offset == shctx->block_size) { |
| 1015 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 1016 | offset = 0; |
| 1017 | } |
| 1018 | } |
| 1019 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1020 | appctx->ctx.cache.offset = offset; |
| 1021 | appctx->ctx.cache.next = shblk; |
| 1022 | appctx->ctx.cache.sent += total; |
| 1023 | appctx->ctx.cache.rem_data = rem_data + blksz; |
| 1024 | return total; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1027 | static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len, |
| 1028 | enum htx_blk_type mark) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1029 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1030 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 1031 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1032 | struct shared_block *shblk; |
| 1033 | unsigned int offset, sz; |
| 1034 | unsigned int ret, total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1035 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1036 | while (len) { |
| 1037 | enum htx_blk_type type; |
| 1038 | uint32_t info; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1039 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1040 | shblk = appctx->ctx.cache.next; |
| 1041 | offset = appctx->ctx.cache.offset; |
| 1042 | if (appctx->ctx.cache.rem_data) { |
| 1043 | type = HTX_BLK_DATA; |
| 1044 | info = 0; |
| 1045 | goto add_data_blk; |
| 1046 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1047 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1048 | /* 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] | 1049 | sz = MIN(4, shctx->block_size - offset); |
| 1050 | memcpy((char *)&info, (const char *)shblk->data + offset, sz); |
| 1051 | offset += sz; |
| 1052 | if (sz < 4) { |
| 1053 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 1054 | memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz); |
| 1055 | offset = (4 - sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1056 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1057 | |
| 1058 | /* Get payload of the next HTX block and insert it. */ |
| 1059 | type = (info >> 28); |
| 1060 | if (type != HTX_BLK_DATA) |
| 1061 | ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset); |
| 1062 | else { |
| 1063 | add_data_blk: |
| 1064 | ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1067 | if (!ret) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1068 | break; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1069 | total += ret; |
| 1070 | len -= ret; |
| 1071 | |
| 1072 | if (appctx->ctx.cache.rem_data || type == mark) |
| 1073 | break; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1074 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1075 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1076 | return total; |
| 1077 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1078 | |
| 1079 | static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx) |
| 1080 | { |
| 1081 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 1082 | unsigned int age; |
| 1083 | char *end; |
| 1084 | |
| 1085 | chunk_reset(&trash); |
| 1086 | age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age; |
| 1087 | if (unlikely(age > CACHE_ENTRY_MAX_AGE)) |
| 1088 | age = CACHE_ENTRY_MAX_AGE; |
| 1089 | end = ultoa_o(age, b_head(&trash), b_size(&trash)); |
| 1090 | b_set_data(&trash, end - b_head(&trash)); |
| 1091 | if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash)))) |
| 1092 | return 0; |
| 1093 | return 1; |
| 1094 | } |
| 1095 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1096 | static void http_cache_io_handler(struct appctx *appctx) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1097 | { |
| 1098 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 1099 | struct shared_block *first = block_ptr(cache_ptr); |
| 1100 | struct stream_interface *si = appctx->owner; |
| 1101 | struct channel *req = si_oc(si); |
| 1102 | struct channel *res = si_ic(si); |
| 1103 | struct htx *req_htx, *res_htx; |
| 1104 | struct buffer *errmsg; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1105 | unsigned int len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1106 | size_t ret, total = 0; |
| 1107 | |
| 1108 | res_htx = htxbuf(&res->buf); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1109 | total = res_htx->data; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1110 | |
| 1111 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 1112 | goto out; |
| 1113 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1114 | /* Check if the input buffer is available. */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1115 | if (!b_size(&res->buf)) { |
| 1116 | si_rx_room_blk(si); |
| 1117 | goto out; |
| 1118 | } |
| 1119 | |
Willy Tarreau | efef323 | 2018-12-16 00:37:45 +0100 | [diff] [blame] | 1120 | if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW)) |
Willy Tarreau | 273e964 | 2018-12-16 00:35:15 +0100 | [diff] [blame] | 1121 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1122 | |
| 1123 | if (appctx->st0 == HTX_CACHE_INIT) { |
| 1124 | appctx->ctx.cache.next = block_ptr(cache_ptr); |
| 1125 | appctx->ctx.cache.offset = sizeof(*cache_ptr); |
| 1126 | appctx->ctx.cache.sent = 0; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1127 | appctx->ctx.cache.rem_data = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1128 | appctx->st0 = HTX_CACHE_HEADER; |
| 1129 | } |
| 1130 | |
| 1131 | if (appctx->st0 == HTX_CACHE_HEADER) { |
| 1132 | /* Headers must be dump at once. Otherwise it is an error */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1133 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1134 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH); |
| 1135 | if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) || |
| 1136 | !htx_cache_add_age_hdr(appctx, res_htx)) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1137 | goto error; |
| 1138 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1139 | /* In case of a conditional request, we might want to send a |
| 1140 | * "304 Not Modified" response instead of the stored data. */ |
Tim Duesterhus | e014234 | 2020-10-22 21:15:06 +0200 | [diff] [blame] | 1141 | if (appctx->ctx.cache.send_notmodified) { |
| 1142 | if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) { |
| 1143 | /* If replacing the status code fails we need to send the full response. */ |
| 1144 | appctx->ctx.cache.send_notmodified = 0; |
| 1145 | } |
| 1146 | } |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1147 | |
| 1148 | /* Skip response body for HEAD requests or in case of "304 Not |
| 1149 | * Modified" response. */ |
| 1150 | 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] | 1151 | appctx->st0 = HTX_CACHE_EOM; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1152 | else |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1153 | appctx->st0 = HTX_CACHE_DATA; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | if (appctx->st0 == HTX_CACHE_DATA) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1157 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1158 | if (len) { |
| 1159 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM); |
| 1160 | if (ret < len) { |
| 1161 | si_rx_room_blk(si); |
| 1162 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1163 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1164 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1165 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | if (appctx->st0 == HTX_CACHE_EOM) { |
Christopher Faulet | 810df06 | 2020-07-22 16:20:34 +0200 | [diff] [blame] | 1169 | 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] | 1170 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 1171 | si_rx_room_blk(si); |
| 1172 | goto out; |
| 1173 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1174 | appctx->st0 = HTX_CACHE_END; |
| 1175 | } |
| 1176 | |
| 1177 | end: |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1178 | if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1179 | res->flags |= CF_READ_NULL; |
| 1180 | si_shutr(si); |
| 1181 | } |
| 1182 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1183 | out: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1184 | total = res_htx->data - total; |
Christopher Faulet | 6112391 | 2019-01-02 14:10:01 +0100 | [diff] [blame] | 1185 | if (total) |
| 1186 | channel_add_input(res, total); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1187 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1188 | |
| 1189 | /* eat the whole request */ |
| 1190 | if (co_data(req)) { |
| 1191 | req_htx = htx_from_buf(&req->buf); |
| 1192 | co_htx_skip(req, req_htx, co_data(req)); |
| 1193 | htx_to_buf(req_htx, &req->buf); |
| 1194 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1195 | return; |
| 1196 | |
| 1197 | error: |
| 1198 | /* Sent and HTTP error 500 */ |
| 1199 | b_reset(&res->buf); |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 1200 | errmsg = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1201 | res->buf.data = b_data(errmsg); |
| 1202 | memcpy(res->buf.area, b_head(errmsg), b_data(errmsg)); |
| 1203 | res_htx = htx_from_buf(&res->buf); |
| 1204 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1205 | total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1206 | appctx->st0 = HTX_CACHE_END; |
| 1207 | goto end; |
| 1208 | } |
| 1209 | |
| 1210 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1211 | 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] | 1212 | { |
| 1213 | struct flt_conf *fconf; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1214 | struct cache_flt_conf *cconf = NULL; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1215 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1216 | if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1217 | memprintf(err, "expects a cache name"); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1218 | goto err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | /* check if a cache filter was already registered with this cache |
| 1222 | * name, if that's the case, must use it. */ |
| 1223 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1224 | if (fconf->id == cache_store_flt_id) { |
| 1225 | cconf = fconf->conf; |
| 1226 | if (cconf && !strcmp((char *)cconf->c.name, name)) { |
| 1227 | rule->arg.act.p[0] = cconf; |
| 1228 | return 1; |
| 1229 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1233 | /* Create the filter cache config */ |
| 1234 | cconf = calloc(1, sizeof(*cconf)); |
| 1235 | if (!cconf) { |
| 1236 | memprintf(err, "out of memory\n"); |
| 1237 | goto err; |
| 1238 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1239 | cconf->flags = CACHE_FLT_F_IMPLICIT_DECL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1240 | cconf->c.name = strdup(name); |
| 1241 | if (!cconf->c.name) { |
| 1242 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1243 | goto err; |
| 1244 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1245 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1246 | /* register a filter to fill the cache buffer */ |
| 1247 | fconf = calloc(1, sizeof(*fconf)); |
| 1248 | if (!fconf) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1249 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1250 | goto err; |
| 1251 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1252 | fconf->id = cache_store_flt_id; |
| 1253 | fconf->conf = cconf; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1254 | fconf->ops = &cache_ops; |
| 1255 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 1256 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1257 | rule->arg.act.p[0] = cconf; |
| 1258 | return 1; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1259 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1260 | err: |
| 1261 | free(cconf); |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 1266 | struct act_rule *rule, char **err) |
| 1267 | { |
| 1268 | rule->action = ACT_CUSTOM; |
| 1269 | rule->action_ptr = http_action_store_cache; |
| 1270 | |
| 1271 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
| 1272 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1273 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1274 | (*orig_arg)++; |
| 1275 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1278 | /* This produces a sha1 hash of the concatenation of the HTTP method, |
| 1279 | * the first occurrence of the Host header followed by the path component |
| 1280 | * if it begins with a slash ('/'). */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1281 | int sha1_hosturi(struct stream *s) |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1282 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1283 | struct http_txn *txn = s->txn; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1284 | struct htx *htx = htxbuf(&s->req.buf); |
| 1285 | struct htx_sl *sl; |
| 1286 | struct http_hdr_ctx ctx; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1287 | struct ist uri; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1288 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1289 | struct buffer *trash; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1290 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1291 | trash = get_trash_chunk(); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1292 | ctx.blk = NULL; |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1293 | |
| 1294 | switch (txn->meth) { |
| 1295 | case HTTP_METH_HEAD: |
| 1296 | case HTTP_METH_GET: |
| 1297 | chunk_memcat(trash, "GET", 3); |
| 1298 | break; |
| 1299 | default: |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1303 | sl = http_get_stline(htx); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1304 | uri = htx_sl_req_uri(sl); // whole uri |
| 1305 | if (!uri.len) |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1306 | return 0; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1307 | |
| 1308 | /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'), |
| 1309 | * unless haproxy is deployed in front of an outbound cache. In HTTP/2, |
| 1310 | * URIs are almost always sent in absolute form with their scheme. In |
| 1311 | * this case, the scheme is almost always "https". In order to support |
| 1312 | * sharing of cache objects between H1 and H2, we'll hash the absolute |
| 1313 | * URI whenever known, or prepend "https://" + the Host header for |
| 1314 | * relative URIs. The difference will only appear on absolute HTTP/1 |
| 1315 | * requests sent to an origin server, which practically is never met in |
| 1316 | * the real world so we don't care about the ability to share the same |
| 1317 | * key here.URIs are normalized from the absolute URI to an origin form as |
| 1318 | * well. |
| 1319 | */ |
| 1320 | if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) { |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1321 | chunk_istcat(trash, ist("https://")); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1322 | if (!http_find_header(htx, ist("Host"), &ctx, 0)) |
| 1323 | return 0; |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1324 | chunk_istcat(trash, ctx.value); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | chunk_memcat(trash, uri.ptr, uri.len); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1328 | |
| 1329 | /* hash everything */ |
| 1330 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1331 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1332 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 1333 | |
| 1334 | return 1; |
| 1335 | } |
| 1336 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1337 | /* Looks for "If-None-Match" headers in the request and compares their value |
| 1338 | * with the one that might have been stored in the cache_entry. If any of them |
| 1339 | * matches, a "304 Not Modified" response should be sent instead of the cached |
| 1340 | * data. |
| 1341 | * 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] | 1342 | * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2). |
| 1343 | * |
| 1344 | * If no "If-None-Match" header was found, look for an "If-Modified-Since" |
| 1345 | * header and compare its value (date) to the one stored in the cache_entry. |
| 1346 | * If the request's date is later than the cached one, we also send a |
| 1347 | * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2). |
| 1348 | * |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1349 | * Returns 1 if "304 Not Modified" should be sent, 0 otherwise. |
| 1350 | */ |
| 1351 | static int should_send_notmodified_response(struct cache *cache, struct htx *htx, |
| 1352 | struct cache_entry *entry) |
| 1353 | { |
| 1354 | int retval = 0; |
| 1355 | |
| 1356 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 1357 | struct ist cache_entry_etag = IST_NULL; |
| 1358 | struct buffer *etag_buffer = NULL; |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1359 | int if_none_match_found = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1360 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1361 | struct tm tm = {}; |
| 1362 | time_t if_modified_since = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1363 | |
| 1364 | /* 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] | 1365 | * cache_entry's ETag in order to perform comparisons. |
| 1366 | * There could be multiple "if-none-match" header lines. */ |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1367 | 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] | 1368 | if_none_match_found = 1; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1369 | |
| 1370 | /* A '*' matches everything. */ |
| 1371 | if (isteq(ctx.value, ist("*")) != 0) { |
| 1372 | retval = 1; |
| 1373 | break; |
| 1374 | } |
| 1375 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1376 | /* No need to rebuild an etag if none was stored in the cache. */ |
| 1377 | if (entry->etag_length == 0) |
| 1378 | break; |
| 1379 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1380 | /* Rebuild the stored ETag. */ |
| 1381 | if (etag_buffer == NULL) { |
| 1382 | etag_buffer = get_trash_chunk(); |
| 1383 | |
| 1384 | if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry), |
| 1385 | (unsigned char*)b_orig(etag_buffer), |
| 1386 | entry->etag_offset, entry->etag_length) == 0) { |
| 1387 | cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length); |
| 1388 | } else { |
| 1389 | /* We could not rebuild the ETag in one go, we |
| 1390 | * won't send a "304 Not Modified" response. */ |
| 1391 | break; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | if (http_compare_etags(cache_entry_etag, ctx.value) == 1) { |
| 1396 | retval = 1; |
| 1397 | break; |
| 1398 | } |
| 1399 | } |
| 1400 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1401 | /* If the request did not contain an "If-None-Match" header, we look for |
| 1402 | * an "If-Modified-Since" header (see RFC 7232#3.3). */ |
| 1403 | if (retval == 0 && if_none_match_found == 0) { |
| 1404 | ctx.blk = NULL; |
| 1405 | if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) { |
| 1406 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 1407 | if_modified_since = my_timegm(&tm); |
| 1408 | |
| 1409 | /* We send a "304 Not Modified" response if the |
| 1410 | * entry's last modified date is earlier than |
| 1411 | * the one found in the "If-Modified-Since" |
| 1412 | * header. */ |
| 1413 | retval = (entry->last_modified <= if_modified_since); |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1418 | return retval; |
| 1419 | } |
| 1420 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1421 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 1422 | struct session *sess, struct stream *s, int flags) |
| 1423 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1424 | |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1425 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1426 | struct cache_entry *res, *sec_entry = NULL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1427 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
| 1428 | struct cache *cache = cconf->c.cache; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1429 | struct shared_block *entry_block; |
| 1430 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1431 | |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1432 | /* Ignore cache for HTTP/1.0 requests and for requests other than GET |
| 1433 | * and HEAD */ |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1434 | if (!(txn->req.flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1435 | (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)) |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1436 | txn->flags |= TX_CACHE_IGNORE; |
| 1437 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1438 | http_check_request_for_cacheability(s, &s->req); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1439 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1440 | if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE) |
| 1441 | return ACT_RET_CONT; |
| 1442 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1443 | if (!sha1_hosturi(s)) |
Willy Tarreau | 7704b1e | 2017-12-22 16:32:43 +0100 | [diff] [blame] | 1444 | return ACT_RET_CONT; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1445 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1446 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 1447 | return ACT_RET_CONT; |
| 1448 | |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1449 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1450 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1451 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1452 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1453 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1454 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1455 | res = entry_exist(cache, s->txn->cache_hash); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1456 | if (res) { |
| 1457 | struct appctx *appctx; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1458 | entry_block = block_ptr(res); |
| 1459 | shctx_row_inc_hot(shctx_ptr(cache), entry_block); |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1460 | shctx_unlock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1461 | |
| 1462 | /* In case of Vary, we could have multiple entries with the same |
| 1463 | * primary hash. We need to calculate the secondary has in order |
| 1464 | * to find the actual entry we want (if it exists). */ |
| 1465 | if (res->secondary_key_signature) { |
| 1466 | if (!http_request_build_secondary_key(s, res->secondary_key_signature)) { |
| 1467 | shctx_lock(shctx_ptr(cache)); |
| 1468 | sec_entry = secondary_entry_exist(cache, res, |
| 1469 | s->txn->cache_secondary_hash); |
| 1470 | if (sec_entry && sec_entry != res) { |
| 1471 | /* The wrong row was added to the hot list. */ |
| 1472 | shctx_row_dec_hot(shctx_ptr(cache), entry_block); |
| 1473 | entry_block = block_ptr(sec_entry); |
| 1474 | shctx_row_inc_hot(shctx_ptr(cache), entry_block); |
| 1475 | } |
| 1476 | res = sec_entry; |
| 1477 | shctx_unlock(shctx_ptr(cache)); |
| 1478 | } |
| 1479 | else |
| 1480 | res = NULL; |
| 1481 | } |
| 1482 | |
| 1483 | /* We looked for a valid secondary entry and could not find one, |
| 1484 | * the request must be forwarded to the server. */ |
| 1485 | if (!res) { |
| 1486 | shctx_lock(shctx_ptr(cache)); |
| 1487 | shctx_row_dec_hot(shctx_ptr(cache), entry_block); |
| 1488 | shctx_unlock(shctx_ptr(cache)); |
| 1489 | return ACT_RET_CONT; |
| 1490 | } |
| 1491 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1492 | s->target = &http_cache_applet.obj_type; |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1493 | if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1494 | appctx->st0 = HTX_CACHE_INIT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1495 | appctx->rule = rule; |
| 1496 | appctx->ctx.cache.entry = res; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1497 | appctx->ctx.cache.next = NULL; |
| 1498 | appctx->ctx.cache.sent = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1499 | appctx->ctx.cache.send_notmodified = |
| 1500 | should_send_notmodified_response(cache, htxbuf(&s->req.buf), res); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1501 | |
| 1502 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1503 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1504 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1505 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1506 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1507 | } else { |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 1508 | shctx_lock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1509 | shctx_row_dec_hot(shctx_ptr(cache), entry_block); |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 1510 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1511 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1512 | } |
| 1513 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1514 | shctx_unlock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1515 | |
| 1516 | /* Shared context does not need to be locked while we calculate the |
| 1517 | * secondary hash. */ |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 1518 | if (!res && cache->vary_processing_enabled) { |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1519 | /* Build a complete secondary hash until the server response |
| 1520 | * tells us which fields should be kept (if any). */ |
| 1521 | http_request_prebuild_full_secondary_key(s); |
| 1522 | } |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1523 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | |
| 1527 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 1528 | struct act_rule *rule, char **err) |
| 1529 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1530 | rule->action = ACT_CUSTOM; |
| 1531 | rule->action_ptr = http_action_req_cache_use; |
| 1532 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1533 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1534 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1535 | |
| 1536 | (*orig_arg)++; |
| 1537 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 1541 | { |
| 1542 | int err_code = 0; |
| 1543 | |
| 1544 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 1545 | |
| 1546 | if (!*args[1]) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1547 | ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1548 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1549 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1550 | goto out; |
| 1551 | } |
| 1552 | |
| 1553 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1554 | err_code |= ERR_ABORT; |
| 1555 | goto out; |
| 1556 | } |
| 1557 | |
| 1558 | if (tmp_cache_config == NULL) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1559 | struct cache *cache_config; |
| 1560 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1561 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 1562 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1563 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1564 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1565 | goto out; |
| 1566 | } |
| 1567 | |
| 1568 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 1569 | if (strlen(args[1]) > 32) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1570 | 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] | 1571 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1572 | err_code |= ERR_WARN; |
| 1573 | } |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1574 | |
| 1575 | list_for_each_entry(cache_config, &caches_config, list) { |
| 1576 | if (strcmp(tmp_cache_config->id, cache_config->id) == 0) { |
| 1577 | ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n", |
| 1578 | file, linenum, tmp_cache_config->id); |
| 1579 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1580 | goto out; |
| 1581 | } |
| 1582 | } |
| 1583 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1584 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1585 | tmp_cache_config->maxblocks = 0; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1586 | tmp_cache_config->maxobjsz = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1587 | } |
| 1588 | } else if (strcmp(args[0], "total-max-size") == 0) { |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1589 | unsigned long int maxsize; |
| 1590 | char *err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1591 | |
| 1592 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1593 | err_code |= ERR_ABORT; |
| 1594 | goto out; |
| 1595 | } |
| 1596 | |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1597 | maxsize = strtoul(args[1], &err, 10); |
| 1598 | if (err == args[1] || *err != '\0') { |
| 1599 | ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n", |
| 1600 | file, linenum, args[1]); |
| 1601 | err_code |= ERR_ABORT; |
| 1602 | goto out; |
| 1603 | } |
| 1604 | |
| 1605 | if (maxsize > (UINT_MAX >> 20)) { |
| 1606 | ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n", |
| 1607 | file, linenum, args[1], UINT_MAX >> 20); |
| 1608 | err_code |= ERR_ABORT; |
| 1609 | goto out; |
| 1610 | } |
| 1611 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1612 | /* size in megabytes */ |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1613 | maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1614 | tmp_cache_config->maxblocks = maxsize; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1615 | } else if (strcmp(args[0], "max-age") == 0) { |
| 1616 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1617 | err_code |= ERR_ABORT; |
| 1618 | goto out; |
| 1619 | } |
| 1620 | |
| 1621 | if (!*args[1]) { |
| 1622 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 1623 | file, linenum, args[0]); |
| 1624 | err_code |= ERR_WARN; |
| 1625 | } |
| 1626 | |
| 1627 | tmp_cache_config->maxage = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1628 | } else if (strcmp(args[0], "max-object-size") == 0) { |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1629 | unsigned int maxobjsz; |
| 1630 | char *err; |
| 1631 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1632 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1633 | err_code |= ERR_ABORT; |
| 1634 | goto out; |
| 1635 | } |
| 1636 | |
| 1637 | if (!*args[1]) { |
| 1638 | ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n", |
| 1639 | file, linenum, args[0]); |
| 1640 | err_code |= ERR_WARN; |
| 1641 | } |
| 1642 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1643 | maxobjsz = strtoul(args[1], &err, 10); |
| 1644 | if (err == args[1] || *err != '\0') { |
| 1645 | ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n", |
| 1646 | file, linenum, args[1]); |
| 1647 | err_code |= ERR_ABORT; |
| 1648 | goto out; |
| 1649 | } |
| 1650 | tmp_cache_config->maxobjsz = maxobjsz; |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 1651 | } else if (strcmp(args[0], "process-vary") == 0) { |
| 1652 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1653 | err_code |= ERR_ABORT; |
| 1654 | goto out; |
| 1655 | } |
| 1656 | |
| 1657 | if (!*args[1]) { |
| 1658 | ha_warning("parsing [%s:%d]: '%s' expects 0 or 1 (disable or enable vary processing).\n", |
| 1659 | file, linenum, args[0]); |
| 1660 | err_code |= ERR_WARN; |
| 1661 | } |
| 1662 | |
| 1663 | tmp_cache_config->vary_processing_enabled = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1664 | } |
| 1665 | else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1666 | 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] | 1667 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1668 | goto out; |
| 1669 | } |
| 1670 | out: |
| 1671 | return err_code; |
| 1672 | } |
| 1673 | |
| 1674 | /* once the cache section is parsed */ |
| 1675 | |
| 1676 | int cfg_post_parse_section_cache() |
| 1677 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1678 | int err_code = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1679 | |
| 1680 | if (tmp_cache_config) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1681 | |
| 1682 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1683 | 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] | 1684 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1685 | goto out; |
| 1686 | } |
| 1687 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1688 | if (!tmp_cache_config->maxobjsz) { |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1689 | /* Default max. file size is a 256th of the cache size. */ |
| 1690 | tmp_cache_config->maxobjsz = |
| 1691 | (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1692 | } |
| 1693 | else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) { |
| 1694 | ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2); |
| 1695 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1696 | goto out; |
| 1697 | } |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1698 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1699 | /* add to the list of cache to init and reinit tmp_cache_config |
| 1700 | * for next cache section, if any. |
| 1701 | */ |
| 1702 | LIST_ADDQ(&caches_config, &tmp_cache_config->list); |
| 1703 | tmp_cache_config = NULL; |
| 1704 | return err_code; |
| 1705 | } |
| 1706 | out: |
| 1707 | free(tmp_cache_config); |
| 1708 | tmp_cache_config = NULL; |
| 1709 | return err_code; |
| 1710 | |
| 1711 | } |
| 1712 | |
| 1713 | int post_check_cache() |
| 1714 | { |
| 1715 | struct proxy *px; |
| 1716 | struct cache *back, *cache_config, *cache; |
| 1717 | struct shared_context *shctx; |
| 1718 | int ret_shctx; |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1719 | int err_code = ERR_NONE; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1720 | |
| 1721 | list_for_each_entry_safe(cache_config, back, &caches_config, list) { |
| 1722 | |
| 1723 | ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE, |
| 1724 | cache_config->maxobjsz, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1725 | |
Frédéric Lécaille | bc58449 | 2018-10-25 20:18:59 +0200 | [diff] [blame] | 1726 | if (ret_shctx <= 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1727 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1728 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1729 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1730 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1731 | |
| 1732 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1733 | goto out; |
| 1734 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1735 | shctx->free_block = cache_free_blocks; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1736 | /* the cache structure is stored in the shctx and added to the |
| 1737 | * caches list, we can remove the entry from the caches_config |
| 1738 | * list */ |
| 1739 | memcpy(shctx->data, cache_config, sizeof(struct cache)); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1740 | cache = (struct cache *)shctx->data; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1741 | cache->entries = EB_ROOT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1742 | LIST_ADDQ(&caches, &cache->list); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1743 | LIST_DEL(&cache_config->list); |
| 1744 | free(cache_config); |
| 1745 | |
| 1746 | /* Find all references for this cache in the existing filters |
| 1747 | * (over all proxies) and reference it in matching filters. |
| 1748 | */ |
| 1749 | for (px = proxies_list; px; px = px->next) { |
| 1750 | struct flt_conf *fconf; |
| 1751 | struct cache_flt_conf *cconf; |
| 1752 | |
| 1753 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 1754 | if (fconf->id != cache_store_flt_id) |
| 1755 | continue; |
| 1756 | |
| 1757 | cconf = fconf->conf; |
| 1758 | if (!strcmp(cache->id, cconf->c.name)) { |
| 1759 | free(cconf->c.name); |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 1760 | cconf->flags |= CACHE_FLT_INIT; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1761 | cconf->c.cache = cache; |
| 1762 | break; |
| 1763 | } |
| 1764 | } |
| 1765 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1766 | } |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1767 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1768 | out: |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1769 | return err_code; |
| 1770 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1771 | } |
| 1772 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1773 | struct flt_ops cache_ops = { |
| 1774 | .init = cache_store_init, |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1775 | .check = cache_store_check, |
| 1776 | .deinit = cache_store_deinit, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1777 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 1778 | /* Handle stream init/deinit */ |
| 1779 | .attach = cache_store_strm_init, |
| 1780 | .detach = cache_store_strm_deinit, |
| 1781 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1782 | /* Handle channels activity */ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1783 | .channel_post_analyze = cache_store_post_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1784 | |
| 1785 | /* Filter HTTP requests and responses */ |
| 1786 | .http_headers = cache_store_http_headers, |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1787 | .http_payload = cache_store_http_payload, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1788 | .http_end = cache_store_http_end, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1789 | }; |
| 1790 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1791 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 1792 | int accept_encoding_cmp(const void *a, const void *b) |
| 1793 | { |
| 1794 | const struct ist ist_a = *(const struct ist*)a; |
| 1795 | const struct ist ist_b = *(const struct ist*)b; |
| 1796 | |
| 1797 | return istdiff(ist_a, ist_b); |
| 1798 | } |
| 1799 | |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 1800 | #define ACCEPT_ENCODING_MAX_ENTRIES 16 |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 1801 | /* |
| 1802 | * Build a hash of the accept-encoding header. The different parts of the |
| 1803 | * header value are first sorted, appended and then a crc is calculated |
| 1804 | * for the newly constructed buffer. |
| 1805 | * Returns 0 in case of success. |
| 1806 | */ |
| 1807 | static int accept_encoding_normalizer(struct ist value, char *buf, unsigned int *buf_len) |
| 1808 | { |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 1809 | struct ist values[ACCEPT_ENCODING_MAX_ENTRIES] = {{}}; |
| 1810 | size_t count = 0; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 1811 | char *comma = NULL; |
| 1812 | struct buffer *trash = get_trash_chunk(); |
| 1813 | int hash_value = 0; |
| 1814 | |
| 1815 | /* The hash will be built out of a sorted list of accepted encodings. */ |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 1816 | while (count < (ACCEPT_ENCODING_MAX_ENTRIES - 1) && (comma = istchr(value, ',')) != NULL) { |
| 1817 | size_t length = comma - istptr(value); |
| 1818 | |
| 1819 | values[count++] = isttrim(value, length); |
| 1820 | value = istadv(value, length + 1); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 1821 | } |
| 1822 | values[count++] = value; |
| 1823 | |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 1824 | if (count == ACCEPT_ENCODING_MAX_ENTRIES) |
| 1825 | return 1; |
| 1826 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 1827 | /* Sort the values alphabetically. */ |
| 1828 | qsort(values, count, sizeof(struct ist), &accept_encoding_cmp); |
| 1829 | |
| 1830 | while (count) |
| 1831 | chunk_istcat(trash, values[--count]); |
| 1832 | |
| 1833 | hash_value = hash_crc32(b_orig(trash), b_data(trash)); |
| 1834 | |
| 1835 | memcpy(buf, &hash_value, sizeof(hash_value)); |
| 1836 | *buf_len = sizeof(hash_value); |
| 1837 | |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 1838 | return 0; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 1839 | } |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 1840 | #undef ACCEPT_ENCODING_MAX_ENTRIES |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 1841 | |
| 1842 | /* |
| 1843 | * Normalizer used by default for User-Agent and Referer headers. It only |
| 1844 | * calculates a simple crc of the whole value. |
| 1845 | * Returns 0 in case of success. |
| 1846 | */ |
| 1847 | static int default_normalizer(struct ist value, char *buf, unsigned int *buf_len) |
| 1848 | { |
| 1849 | int hash_value = 0; |
| 1850 | |
| 1851 | hash_value = hash_crc32(istptr(value), istlen(value)); |
| 1852 | |
| 1853 | memcpy(buf, &hash_value, sizeof(hash_value)); |
| 1854 | *buf_len = sizeof(hash_value); |
| 1855 | |
| 1856 | return 0; |
| 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | /* |
| 1861 | * Pre-calculate the hashes of all the supported headers (in our Vary |
| 1862 | * implementation) of a given request. We have to calculate all the hashes |
| 1863 | * in advance because the actual Vary signature won't be known until the first |
| 1864 | * response. |
| 1865 | * Only the first occurrence of every header will be taken into account in the |
| 1866 | * hash. |
| 1867 | * If the header is not present, the hash portion of the given header will be |
| 1868 | * filled with zeros. |
| 1869 | * Returns 0 in case of success. |
| 1870 | */ |
| 1871 | static int http_request_prebuild_full_secondary_key(struct stream *s) |
| 1872 | { |
| 1873 | struct http_txn *txn = s->txn; |
| 1874 | struct htx *htx = htxbuf(&s->req.buf); |
| 1875 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 1876 | |
| 1877 | unsigned int idx; |
| 1878 | const struct vary_hashing_information *info = NULL; |
| 1879 | unsigned int hash_length = 0; |
| 1880 | int retval = 0; |
| 1881 | int offset = 0; |
| 1882 | |
| 1883 | for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) { |
| 1884 | info = &vary_information[idx]; |
| 1885 | |
| 1886 | ctx.blk = NULL; |
| 1887 | if (info->norm_fn != NULL && http_find_header(htx, info->hdr_name, &ctx, 1)) { |
| 1888 | retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length); |
| 1889 | offset += hash_length; |
| 1890 | } |
| 1891 | else { |
| 1892 | /* Fill hash with 0s. */ |
| 1893 | hash_length = info->hash_length; |
| 1894 | memset(&txn->cache_secondary_hash[offset], 0, hash_length); |
| 1895 | offset += hash_length; |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | return retval; |
| 1900 | } |
| 1901 | |
| 1902 | |
| 1903 | /* |
| 1904 | * Calculate the secondary key for a request for which we already have a known |
| 1905 | * vary signature. The key is made by aggregating hashes calculated for every |
| 1906 | * header mentioned in the vary signature. |
| 1907 | * Only the first occurrence of every header will be taken into account in the |
| 1908 | * hash. |
| 1909 | * If the header is not present, the hash portion of the given header will be |
| 1910 | * filled with zeros. |
| 1911 | * Returns 0 in case of success. |
| 1912 | */ |
| 1913 | static int http_request_build_secondary_key(struct stream *s, int vary_signature) |
| 1914 | { |
| 1915 | struct http_txn *txn = s->txn; |
| 1916 | struct htx *htx = htxbuf(&s->req.buf); |
| 1917 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 1918 | |
| 1919 | unsigned int idx; |
| 1920 | const struct vary_hashing_information *info = NULL; |
| 1921 | unsigned int hash_length = 0; |
| 1922 | int retval = 0; |
| 1923 | int offset = 0; |
| 1924 | |
| 1925 | for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) { |
| 1926 | info = &vary_information[idx]; |
| 1927 | |
| 1928 | ctx.blk = NULL; |
| 1929 | if ((vary_signature & info->value) && info->norm_fn != NULL && |
| 1930 | http_find_header(htx, info->hdr_name, &ctx, 1)) { |
| 1931 | retval = info->norm_fn(ctx.value, &txn->cache_secondary_hash[offset], &hash_length); |
| 1932 | offset += hash_length; |
| 1933 | } |
| 1934 | else { |
| 1935 | /* Fill hash with 0s. */ |
| 1936 | hash_length = info->hash_length; |
| 1937 | memset(&txn->cache_secondary_hash[offset], 0, hash_length); |
| 1938 | offset += hash_length; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | return retval; |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Build the actual secondary key of a given request out of the prebuilt key and |
| 1947 | * the actual vary signature (extracted from the response). |
| 1948 | * Returns 0 in case of success. |
| 1949 | */ |
| 1950 | static int http_request_reduce_secondary_key(unsigned int vary_signature, |
| 1951 | char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]) |
| 1952 | { |
| 1953 | int offset = 0; |
| 1954 | int global_offset = 0; |
| 1955 | int vary_info_count = 0; |
| 1956 | int keep = 0; |
| 1957 | unsigned int vary_idx; |
| 1958 | const struct vary_hashing_information *vary_info; |
| 1959 | |
| 1960 | vary_info_count = sizeof(vary_information)/sizeof(*vary_information); |
| 1961 | for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) { |
| 1962 | vary_info = &vary_information[vary_idx]; |
| 1963 | keep = (vary_signature & vary_info->value) ? 0xff : 0; |
| 1964 | |
| 1965 | for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) { |
| 1966 | prebuilt_key[global_offset] &= keep; |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | return 0; |
| 1971 | } |
| 1972 | |
| 1973 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1974 | |
| 1975 | static int |
| 1976 | parse_cache_flt(char **args, int *cur_arg, struct proxy *px, |
| 1977 | struct flt_conf *fconf, char **err, void *private) |
| 1978 | { |
| 1979 | struct flt_conf *f, *back; |
Willy Tarreau | a73da1e | 2018-12-14 10:19:28 +0100 | [diff] [blame] | 1980 | struct cache_flt_conf *cconf = NULL; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1981 | char *name = NULL; |
| 1982 | int pos = *cur_arg; |
| 1983 | |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 1984 | /* Get the cache filter name. <pos> point on "cache" keyword */ |
| 1985 | if (!*args[pos + 1]) { |
Tim Duesterhus | ea969f6 | 2020-08-18 22:06:51 +0200 | [diff] [blame] | 1986 | memprintf(err, "%s : expects a <name> argument", args[pos]); |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 1987 | goto error; |
| 1988 | } |
| 1989 | name = strdup(args[pos + 1]); |
| 1990 | if (!name) { |
| 1991 | memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]); |
| 1992 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1993 | } |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 1994 | pos += 2; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1995 | |
| 1996 | /* Check if an implicit filter with the same name already exists. If so, |
| 1997 | * we remove the implicit filter to use the explicit one. */ |
| 1998 | list_for_each_entry_safe(f, back, &px->filter_configs, list) { |
| 1999 | if (f->id != cache_store_flt_id) |
| 2000 | continue; |
| 2001 | |
| 2002 | cconf = f->conf; |
| 2003 | if (strcmp(name, cconf->c.name)) { |
| 2004 | cconf = NULL; |
| 2005 | continue; |
| 2006 | } |
| 2007 | |
| 2008 | if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 2009 | cconf = NULL; |
| 2010 | memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'", |
| 2011 | px->id, name); |
Tim Duesterhus | d34b1ce | 2020-01-18 01:46:18 +0100 | [diff] [blame] | 2012 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | /* Remove the implicit filter. <cconf> is kept for the explicit one */ |
| 2016 | LIST_DEL(&f->list); |
| 2017 | free(f); |
| 2018 | free(name); |
| 2019 | break; |
| 2020 | } |
| 2021 | |
| 2022 | /* No implicit cache filter found, create configuration for the explicit one */ |
| 2023 | if (!cconf) { |
| 2024 | cconf = calloc(1, sizeof(*cconf)); |
| 2025 | if (!cconf) { |
| 2026 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 2027 | goto error; |
| 2028 | } |
| 2029 | cconf->c.name = name; |
| 2030 | } |
| 2031 | |
| 2032 | cconf->flags = 0; |
| 2033 | fconf->id = cache_store_flt_id; |
| 2034 | fconf->conf = cconf; |
| 2035 | fconf->ops = &cache_ops; |
| 2036 | |
| 2037 | *cur_arg = pos; |
| 2038 | return 0; |
| 2039 | |
| 2040 | error: |
| 2041 | free(name); |
| 2042 | free(cconf); |
| 2043 | return -1; |
| 2044 | } |
| 2045 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 2046 | 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] | 2047 | { |
| 2048 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2049 | return 1; |
| 2050 | |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
| 2054 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 2055 | { |
| 2056 | struct cache* cache = appctx->ctx.cli.p0; |
| 2057 | struct stream_interface *si = appctx->owner; |
| 2058 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2059 | if (cache == NULL) { |
| 2060 | cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
| 2061 | } |
| 2062 | |
| 2063 | list_for_each_entry_from(cache, &caches, list) { |
| 2064 | struct eb32_node *node = NULL; |
| 2065 | unsigned int next_key; |
| 2066 | struct cache_entry *entry; |
| 2067 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2068 | next_key = appctx->ctx.cli.i0; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 2069 | if (!next_key) { |
| 2070 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
| 2071 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 2072 | si_rx_room_blk(si); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 2073 | return 0; |
| 2074 | } |
| 2075 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2076 | |
| 2077 | appctx->ctx.cli.p0 = cache; |
| 2078 | |
| 2079 | while (1) { |
| 2080 | |
| 2081 | shctx_lock(shctx_ptr(cache)); |
| 2082 | node = eb32_lookup_ge(&cache->entries, next_key); |
| 2083 | if (!node) { |
| 2084 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 2085 | appctx->ctx.cli.i0 = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2086 | break; |
| 2087 | } |
| 2088 | |
| 2089 | entry = container_of(node, struct cache_entry, eb); |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 2090 | chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, read_u32(entry->hash), 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] | 2091 | |
| 2092 | next_key = node->key + 1; |
| 2093 | appctx->ctx.cli.i0 = next_key; |
| 2094 | |
| 2095 | shctx_unlock(shctx_ptr(cache)); |
| 2096 | |
| 2097 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 2098 | si_rx_room_blk(si); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2099 | return 0; |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | } |
| 2104 | |
| 2105 | return 1; |
| 2106 | |
| 2107 | } |
| 2108 | |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 2109 | |
| 2110 | /* |
| 2111 | * boolean, returns true if response was built out of a cache entry. |
| 2112 | */ |
| 2113 | static int |
| 2114 | smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp, |
| 2115 | const char *kw, void *private) |
| 2116 | { |
| 2117 | smp->data.type = SMP_T_BOOL; |
| 2118 | smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0); |
| 2119 | |
| 2120 | return 1; |
| 2121 | } |
| 2122 | |
| 2123 | /* |
| 2124 | * string, returns cache name (if response came from a cache). |
| 2125 | */ |
| 2126 | static int |
| 2127 | smp_fetch_res_cache_name(const struct arg *args, struct sample *smp, |
| 2128 | const char *kw, void *private) |
| 2129 | { |
| 2130 | struct appctx *appctx = NULL; |
| 2131 | |
| 2132 | struct cache_flt_conf *cconf = NULL; |
| 2133 | struct cache *cache = NULL; |
| 2134 | |
| 2135 | if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type) |
| 2136 | return 0; |
| 2137 | |
| 2138 | /* Get appctx from the stream_interface. */ |
| 2139 | appctx = si_appctx(&smp->strm->si[1]); |
| 2140 | if (appctx && appctx->rule) { |
| 2141 | cconf = appctx->rule->arg.act.p[0]; |
| 2142 | if (cconf) { |
| 2143 | cache = cconf->c.cache; |
| 2144 | |
| 2145 | smp->data.type = SMP_T_STR; |
| 2146 | smp->flags = SMP_F_CONST; |
| 2147 | smp->data.u.str.area = cache->id; |
| 2148 | smp->data.u.str.data = strlen(cache->id); |
| 2149 | return 1; |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | return 0; |
| 2154 | } |
| 2155 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2156 | /* Declare the filter parser for "cache" keyword */ |
| 2157 | static struct flt_kw_list filter_kws = { "CACHE", { }, { |
| 2158 | { "cache", parse_cache_flt, NULL }, |
| 2159 | { NULL, NULL, NULL }, |
| 2160 | } |
| 2161 | }; |
| 2162 | |
| 2163 | INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws); |
| 2164 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2165 | static struct cli_kw_list cli_kws = {{},{ |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 2166 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
| 2167 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2168 | }}; |
| 2169 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2170 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2171 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2172 | static struct action_kw_list http_res_actions = { |
| 2173 | .kw = { |
| 2174 | { "cache-store", parse_cache_store }, |
| 2175 | { NULL, NULL } |
| 2176 | } |
| 2177 | }; |
| 2178 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2179 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
| 2180 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2181 | static struct action_kw_list http_req_actions = { |
| 2182 | .kw = { |
| 2183 | { "cache-use", parse_cache_use }, |
| 2184 | { NULL, NULL } |
| 2185 | } |
| 2186 | }; |
| 2187 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2188 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 2189 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 2190 | struct applet http_cache_applet = { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2191 | .obj_type = OBJ_TYPE_APPLET, |
| 2192 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 2193 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 2194 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2195 | }; |
| 2196 | |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 2197 | /* config parsers for this section */ |
| 2198 | REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2199 | REGISTER_POST_CHECK(post_check_cache); |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 2200 | |
| 2201 | |
| 2202 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2203 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 2204 | { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE }, |
| 2205 | { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE }, |
| 2206 | { /* END */ }, |
| 2207 | } |
| 2208 | }; |
| 2209 | |
| 2210 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |