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