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 | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 18 | #include <haproxy/applet.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 19 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 20 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 21 | #include <haproxy/cli.h> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 22 | #include <haproxy/conn_stream.h> |
| 23 | #include <haproxy/cs_utils.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 24 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 25 | #include <haproxy/filters.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 26 | #include <haproxy/hash.h> |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 27 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 28 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 29 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 30 | #include <haproxy/http_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 31 | #include <haproxy/htx.h> |
| 32 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 33 | #include <haproxy/proxy.h> |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 34 | #include <haproxy/sample.h> |
Willy Tarreau | 334099c | 2020-06-03 18:38:48 +0200 | [diff] [blame] | 35 | #include <haproxy/shctx.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 36 | #include <haproxy/stream.h> |
Willy Tarreau | ce6700a | 2021-05-08 13:03:55 +0200 | [diff] [blame] | 37 | #include <haproxy/tools.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 38 | |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 39 | #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] | 40 | * the filter keyword) */ |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 41 | #define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 42 | |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 43 | const char *cache_store_flt_id = "cache store filter"; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 44 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 45 | extern struct applet http_cache_applet; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 46 | |
| 47 | struct flt_ops cache_ops; |
| 48 | |
| 49 | struct cache { |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 50 | struct list list; /* cache linked list */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 51 | struct eb_root entries; /* head of cache entries based on keys */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 52 | unsigned int maxage; /* max-age */ |
| 53 | unsigned int maxblocks; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 54 | unsigned int maxobjsz; /* max-object-size (in bytes) */ |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 55 | unsigned int max_secondary_entries; /* maximum number of secondary entries with the same primary hash */ |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 56 | uint8_t vary_processing_enabled; /* boolean : manage Vary header (disabled by default) */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 57 | char id[33]; /* cache name */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 60 | /* cache config for filters */ |
| 61 | struct cache_flt_conf { |
| 62 | union { |
| 63 | struct cache *cache; /* cache used by the filter */ |
| 64 | char *name; /* cache name used during conf parsing */ |
| 65 | } c; |
| 66 | unsigned int flags; /* CACHE_FLT_F_* */ |
| 67 | }; |
| 68 | |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 69 | /* CLI context used during "show cache" */ |
| 70 | struct show_cache_ctx { |
| 71 | struct cache *cache; |
| 72 | uint next_key; |
| 73 | }; |
| 74 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * Vary-related structures and functions |
| 78 | */ |
| 79 | enum vary_header_bit { |
| 80 | VARY_ACCEPT_ENCODING = (1 << 0), |
| 81 | VARY_REFERER = (1 << 1), |
| 82 | VARY_LAST /* should always be last */ |
| 83 | }; |
| 84 | |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 85 | /* |
| 86 | * Encoding list extracted from |
| 87 | * https://www.iana.org/assignments/http-parameters/http-parameters.xhtml |
| 88 | * and RFC7231#5.3.4. |
| 89 | */ |
| 90 | enum vary_encoding { |
| 91 | VARY_ENCODING_GZIP = (1 << 0), |
| 92 | VARY_ENCODING_DEFLATE = (1 << 1), |
| 93 | VARY_ENCODING_BR = (1 << 2), |
| 94 | VARY_ENCODING_COMPRESS = (1 << 3), |
| 95 | VARY_ENCODING_AES128GCM = (1 << 4), |
| 96 | VARY_ENCODING_EXI = (1 << 5), |
| 97 | VARY_ENCODING_PACK200_GZIP = (1 << 6), |
| 98 | VARY_ENCODING_ZSTD = (1 << 7), |
| 99 | VARY_ENCODING_IDENTITY = (1 << 8), |
| 100 | VARY_ENCODING_STAR = (1 << 9), |
| 101 | VARY_ENCODING_OTHER = (1 << 10) |
| 102 | }; |
| 103 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 104 | struct vary_hashing_information { |
| 105 | struct ist hdr_name; /* Header name */ |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 106 | enum vary_header_bit value; /* Bit representing the header in a vary signature */ |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 107 | unsigned int hash_length; /* Size of the sub hash for this header's value */ |
Remi Tricot-Le Breton | 6a34b2b | 2020-12-23 18:13:47 +0100 | [diff] [blame] | 108 | int(*norm_fn)(struct htx*,struct ist hdr_name,char* buf,unsigned int* buf_len); /* Normalization function */ |
Tim Duesterhus | ed84d84 | 2021-01-18 13:41:17 +0100 | [diff] [blame] | 109 | int(*cmp_fn)(const void *ref, const void *new, unsigned int len); /* Comparison function, should return 0 if the hashes are alike */ |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 110 | }; |
| 111 | |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 112 | static int http_request_prebuild_full_secondary_key(struct stream *s); |
| 113 | static int http_request_build_secondary_key(struct stream *s, int vary_signature); |
| 114 | static int http_request_reduce_secondary_key(unsigned int vary_signature, |
| 115 | char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]); |
| 116 | |
| 117 | static int parse_encoding_value(struct ist value, unsigned int *encoding_value, |
| 118 | unsigned int *has_null_weight); |
| 119 | |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 120 | static int accept_encoding_normalizer(struct htx *htx, struct ist hdr_name, |
| 121 | char *buf, unsigned int *buf_len); |
| 122 | static int default_normalizer(struct htx *htx, struct ist hdr_name, |
| 123 | char *buf, unsigned int *buf_len); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 124 | |
Tim Duesterhus | ed84d84 | 2021-01-18 13:41:17 +0100 | [diff] [blame] | 125 | static int accept_encoding_bitmap_cmp(const void *ref, const void *new, unsigned int len); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 126 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 127 | /* Warning : do not forget to update HTTP_CACHE_SEC_KEY_LEN when new items are |
| 128 | * added to this array. */ |
| 129 | const struct vary_hashing_information vary_information[] = { |
Tim Duesterhus | ed84d84 | 2021-01-18 13:41:17 +0100 | [diff] [blame] | 130 | { IST("accept-encoding"), VARY_ACCEPT_ENCODING, sizeof(uint32_t), &accept_encoding_normalizer, &accept_encoding_bitmap_cmp }, |
Remi Tricot-Le Breton | 6a34b2b | 2020-12-23 18:13:47 +0100 | [diff] [blame] | 131 | { IST("referer"), VARY_REFERER, sizeof(int), &default_normalizer, NULL }, |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 132 | }; |
| 133 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 134 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 135 | /* |
| 136 | * cache ctx for filters |
| 137 | */ |
| 138 | struct cache_st { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 139 | struct shared_block *first_block; |
| 140 | }; |
| 141 | |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 142 | #define DEFAULT_MAX_SECONDARY_ENTRY 10 |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 143 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 144 | struct cache_entry { |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 145 | 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] | 146 | unsigned int latest_validation; /* latest validation date */ |
| 147 | unsigned int expire; /* expiration date */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 148 | unsigned int age; /* Origin server "Age" header value */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 149 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 150 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 151 | char hash[20]; |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 152 | |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 153 | char secondary_key[HTTP_CACHE_SEC_KEY_LEN]; /* Optional secondary key. */ |
| 154 | unsigned int secondary_key_signature; /* Bitfield of the HTTP headers that should be used |
| 155 | * to build secondary keys for this cache entry. */ |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 156 | unsigned int secondary_entries_count; /* Should only be filled in the last entry of a list of dup entries */ |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 157 | unsigned int last_clear_ts; /* Timestamp of the last call to clear_expired_duplicates. */ |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 158 | |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 159 | unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */ |
| 160 | unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */ |
| 161 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 162 | time_t last_modified; /* Origin server "Last-Modified" header value converted in |
| 163 | * seconds since epoch. If no "Last-Modified" |
| 164 | * header is found, use "Date" header value, |
| 165 | * otherwise use reception time. This field will |
| 166 | * be used in case of an "If-Modified-Since"-based |
| 167 | * conditional request. */ |
| 168 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 169 | unsigned char data[0]; |
| 170 | }; |
| 171 | |
| 172 | #define CACHE_BLOCKSIZE 1024 |
Willy Tarreau | 96062a1 | 2018-11-11 14:00:28 +0100 | [diff] [blame] | 173 | #define CACHE_ENTRY_MAX_AGE 2147483648U |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 174 | |
| 175 | static struct list caches = LIST_HEAD_INIT(caches); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 176 | 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] | 177 | static struct cache *tmp_cache_config = NULL; |
| 178 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 179 | DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st)); |
| 180 | |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 181 | static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry); |
| 182 | static void delete_entry(struct cache_entry *del_entry); |
| 183 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 184 | struct cache_entry *entry_exist(struct cache *cache, char *hash) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 185 | { |
| 186 | struct eb32_node *node; |
| 187 | struct cache_entry *entry; |
| 188 | |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 189 | node = eb32_lookup(&cache->entries, read_u32(hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 190 | if (!node) |
| 191 | return NULL; |
| 192 | |
| 193 | entry = eb32_entry(node, struct cache_entry, eb); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 194 | |
| 195 | /* if that's not the right node */ |
| 196 | if (memcmp(entry->hash, hash, sizeof(entry->hash))) |
| 197 | return NULL; |
| 198 | |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 199 | if (entry->expire > now.tv_sec) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 200 | return entry; |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 201 | } else { |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 202 | delete_entry(entry); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 203 | entry->eb.key = 0; |
| 204 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 205 | return NULL; |
| 206 | |
| 207 | } |
| 208 | |
Remi Tricot-Le Breton | 6a34b2b | 2020-12-23 18:13:47 +0100 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Compare a newly built secondary key to the one found in a cache_entry. |
| 212 | * Every sub-part of the key is compared to the reference through the dedicated |
| 213 | * comparison function of the sub-part (that might do more than a simple |
| 214 | * memcmp). |
| 215 | * Returns 0 if the keys are alike. |
| 216 | */ |
| 217 | static int secondary_key_cmp(const char *ref_key, const char *new_key) |
| 218 | { |
| 219 | int retval = 0; |
Tim Duesterhus | 5897cfe | 2021-01-18 13:41:18 +0100 | [diff] [blame] | 220 | size_t idx = 0; |
| 221 | unsigned int offset = 0; |
Remi Tricot-Le Breton | 6a34b2b | 2020-12-23 18:13:47 +0100 | [diff] [blame] | 222 | const struct vary_hashing_information *info; |
| 223 | |
| 224 | for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && !retval; ++idx) { |
| 225 | info = &vary_information[idx]; |
| 226 | |
| 227 | if (info->cmp_fn) |
| 228 | retval = info->cmp_fn(&ref_key[offset], &new_key[offset], info->hash_length); |
| 229 | else |
| 230 | retval = memcmp(&ref_key[offset], &new_key[offset], info->hash_length); |
| 231 | |
| 232 | offset += info->hash_length; |
| 233 | } |
| 234 | |
| 235 | return retval; |
| 236 | } |
| 237 | |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 238 | /* |
| 239 | * There can be multiple entries with the same primary key in the ebtree so in |
| 240 | * order to get the proper one out of the list, we use a secondary_key. |
| 241 | * This function simply iterates over all the entries with the same primary_key |
| 242 | * until it finds the right one. |
| 243 | * Returns the cache_entry in case of success, NULL otherwise. |
| 244 | */ |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 245 | struct cache_entry *secondary_entry_exist(struct cache *cache, struct cache_entry *entry, |
Remi Tricot-Le Breton | 6a34b2b | 2020-12-23 18:13:47 +0100 | [diff] [blame] | 246 | const char *secondary_key) |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 247 | { |
| 248 | struct eb32_node *node = &entry->eb; |
| 249 | |
| 250 | if (!entry->secondary_key_signature) |
| 251 | return NULL; |
| 252 | |
Remi Tricot-Le Breton | 6a34b2b | 2020-12-23 18:13:47 +0100 | [diff] [blame] | 253 | while (entry && secondary_key_cmp(entry->secondary_key, secondary_key) != 0) { |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 254 | node = eb32_next_dup(node); |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 255 | |
| 256 | /* Make the best use of this iteration and clear expired entries |
| 257 | * when we find them. Calling delete_entry would be too costly |
| 258 | * so we simply call eb32_delete. The secondary_entry count will |
| 259 | * be updated when we try to insert a new entry to this list. */ |
| 260 | if (entry->expire <= now.tv_sec) { |
| 261 | eb32_delete(&entry->eb); |
| 262 | entry->eb.key = 0; |
| 263 | } |
| 264 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 265 | entry = node ? eb32_entry(node, struct cache_entry, eb) : NULL; |
| 266 | } |
| 267 | |
| 268 | /* Expired entry */ |
| 269 | if (entry && entry->expire <= now.tv_sec) { |
| 270 | eb32_delete(&entry->eb); |
| 271 | entry->eb.key = 0; |
| 272 | entry = NULL; |
| 273 | } |
| 274 | |
| 275 | return entry; |
| 276 | } |
| 277 | |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 278 | |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 279 | /* |
| 280 | * Remove all expired entries from a list of duplicates. |
| 281 | * Return the number of alive entries in the list and sets dup_tail to the |
| 282 | * current last item of the list. |
| 283 | */ |
| 284 | static unsigned int clear_expired_duplicates(struct eb32_node **dup_tail) |
| 285 | { |
| 286 | unsigned int entry_count = 0; |
| 287 | struct cache_entry *entry = NULL; |
| 288 | struct eb32_node *prev = *dup_tail; |
| 289 | struct eb32_node *tail = NULL; |
| 290 | |
| 291 | while (prev) { |
| 292 | entry = container_of(prev, struct cache_entry, eb); |
| 293 | prev = eb32_prev_dup(prev); |
| 294 | if (entry->expire <= now.tv_sec) { |
| 295 | eb32_delete(&entry->eb); |
| 296 | entry->eb.key = 0; |
| 297 | } |
| 298 | else { |
| 299 | if (!tail) |
| 300 | tail = &entry->eb; |
| 301 | ++entry_count; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | *dup_tail = tail; |
| 306 | |
| 307 | return entry_count; |
| 308 | } |
| 309 | |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * This function inserts a cache_entry in the cache's ebtree. In case of |
| 313 | * duplicate entries (vary), it then checks that the number of entries did not |
| 314 | * reach the max number of secondary entries. If this entry should not have been |
| 315 | * created, remove it. |
| 316 | * In the regular case (unique entries), this function does not do more than a |
| 317 | * simple insert. In case of secondary entries, it will at most cost an |
| 318 | * insertion+max_sec_entries time checks and entry deletion. |
| 319 | * Returns the newly inserted node in case of success, NULL otherwise. |
| 320 | */ |
| 321 | static struct eb32_node *insert_entry(struct cache *cache, struct cache_entry *new_entry) |
| 322 | { |
| 323 | struct eb32_node *prev = NULL; |
| 324 | struct cache_entry *entry = NULL; |
| 325 | unsigned int entry_count = 0; |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 326 | unsigned int last_clear_ts = now.tv_sec; |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 327 | |
| 328 | struct eb32_node *node = eb32_insert(&cache->entries, &new_entry->eb); |
| 329 | |
| 330 | /* We should not have multiple entries with the same primary key unless |
| 331 | * the entry has a non null vary signature. */ |
| 332 | if (!new_entry->secondary_key_signature) |
| 333 | return node; |
| 334 | |
| 335 | prev = eb32_prev_dup(node); |
| 336 | if (prev != NULL) { |
| 337 | /* The last entry of a duplicate list should contain the current |
| 338 | * number of entries in the list. */ |
| 339 | entry = container_of(prev, struct cache_entry, eb); |
| 340 | entry_count = entry->secondary_entries_count; |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 341 | last_clear_ts = entry->last_clear_ts; |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 342 | |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 343 | if (entry_count >= cache->max_secondary_entries) { |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 344 | /* Some entries of the duplicate list might be expired so |
| 345 | * we will iterate over all the items in order to free some |
| 346 | * space. In order to avoid going over the same list too |
| 347 | * often, we first check the timestamp of the last check |
| 348 | * performed. */ |
| 349 | if (last_clear_ts == now.tv_sec) { |
| 350 | /* Too many entries for this primary key, clear the |
| 351 | * one that was inserted. */ |
| 352 | eb32_delete(node); |
| 353 | node->key = 0; |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | entry_count = clear_expired_duplicates(&prev); |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 358 | if (entry_count >= cache->max_secondary_entries) { |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 359 | /* Still too many entries for this primary key, delete |
| 360 | * the newly inserted one. */ |
| 361 | entry = container_of(prev, struct cache_entry, eb); |
| 362 | entry->last_clear_ts = now.tv_sec; |
| 363 | eb32_delete(node); |
| 364 | node->key = 0; |
| 365 | return NULL; |
| 366 | } |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | new_entry->secondary_entries_count = entry_count + 1; |
Remi Tricot-Le Breton | 73be796 | 2020-12-10 17:58:42 +0100 | [diff] [blame] | 371 | new_entry->last_clear_ts = last_clear_ts; |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 372 | |
| 373 | return node; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /* |
| 378 | * This function removes an entry from the ebtree. If the entry was a duplicate |
| 379 | * (in case of Vary), it updates the secondary entry counter in another |
| 380 | * duplicate entry (the last entry of the dup list). |
| 381 | */ |
| 382 | static void delete_entry(struct cache_entry *del_entry) |
| 383 | { |
| 384 | struct eb32_node *prev = NULL, *next = NULL; |
| 385 | struct cache_entry *entry = NULL; |
| 386 | struct eb32_node *last = NULL; |
| 387 | |
| 388 | if (del_entry->secondary_key_signature) { |
| 389 | next = &del_entry->eb; |
| 390 | |
| 391 | /* Look for last entry of the duplicates list. */ |
| 392 | while ((next = eb32_next_dup(next))) { |
| 393 | last = next; |
| 394 | } |
| 395 | |
| 396 | if (last) { |
| 397 | entry = container_of(last, struct cache_entry, eb); |
| 398 | --entry->secondary_entries_count; |
| 399 | } |
| 400 | else { |
| 401 | /* The current entry is the last one, look for the |
| 402 | * previous one to update its counter. */ |
| 403 | prev = eb32_prev_dup(&del_entry->eb); |
| 404 | if (prev) { |
| 405 | entry = container_of(prev, struct cache_entry, eb); |
| 406 | entry->secondary_entries_count = del_entry->secondary_entries_count - 1; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | eb32_delete(&del_entry->eb); |
| 411 | del_entry->eb.key = 0; |
| 412 | } |
| 413 | |
| 414 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 415 | static inline struct shared_context *shctx_ptr(struct cache *cache) |
| 416 | { |
| 417 | return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data); |
| 418 | } |
| 419 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 420 | static inline struct shared_block *block_ptr(struct cache_entry *entry) |
| 421 | { |
| 422 | return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 427 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 428 | cache_store_init(struct proxy *px, struct flt_conf *fconf) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 429 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 430 | fconf->flags |= FLT_CFG_FL_HTX; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 434 | static void |
| 435 | cache_store_deinit(struct proxy *px, struct flt_conf *fconf) |
| 436 | { |
| 437 | struct cache_flt_conf *cconf = fconf->conf; |
| 438 | |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 439 | if (!(cconf->flags & CACHE_FLT_INIT)) |
| 440 | free(cconf->c.name); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 441 | free(cconf); |
| 442 | } |
| 443 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 444 | static int |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 445 | cache_store_check(struct proxy *px, struct flt_conf *fconf) |
| 446 | { |
| 447 | struct cache_flt_conf *cconf = fconf->conf; |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 448 | struct flt_conf *f; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 449 | struct cache *cache; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 450 | int comp = 0; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 451 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 452 | /* Find the cache corresponding to the name in the filter config. The |
| 453 | * cache will not be referenced now in the filter config because it is |
| 454 | * not fully allocated. This step will be performed during the cache |
| 455 | * post_check. |
| 456 | */ |
| 457 | list_for_each_entry(cache, &caches_config, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 458 | if (strcmp(cache->id, cconf->c.name) == 0) |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 459 | goto found; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 463 | proxy_type_str(px), px->id, (char *)cconf->c.name); |
| 464 | return 1; |
| 465 | |
| 466 | found: |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 467 | /* Here <cache> points on the cache the filter must use and <cconf> |
| 468 | * points on the cache filter configuration. */ |
| 469 | |
| 470 | /* Check all filters for proxy <px> to know if the compression is |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 471 | * enabled and if it is after the cache. When the compression is before |
| 472 | * the cache, an error is returned. Also check if the cache filter must |
| 473 | * be explicitly declaired or not. */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 474 | list_for_each_entry(f, &px->filter_configs, list) { |
| 475 | if (f == fconf) { |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 476 | /* The compression filter must be evaluated after the cache. */ |
| 477 | if (comp) { |
| 478 | ha_alert("config: %s '%s': unable to enable the compression filter before " |
| 479 | "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 480 | return 1; |
| 481 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 482 | } |
Christopher Faulet | 8f7fe1c | 2019-07-15 15:08:25 +0200 | [diff] [blame] | 483 | else if (f->id == http_comp_flt_id) |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 484 | comp = 1; |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 485 | else if (f->id == fcgi_flt_id) |
| 486 | continue; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 487 | else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 488 | /* Implicit declaration is only allowed with the |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 489 | * compression and fcgi. For other filters, an implicit |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 490 | * declaration is required. */ |
| 491 | ha_alert("config: %s '%s': require an explicit filter declaration " |
| 492 | "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 493 | return 1; |
| 494 | } |
| 495 | |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 496 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static int |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 501 | cache_store_strm_init(struct stream *s, struct filter *filter) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 502 | { |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 503 | struct cache_st *st; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 504 | |
Willy Tarreau | acc5b01 | 2021-03-22 15:00:49 +0100 | [diff] [blame] | 505 | st = pool_alloc(pool_head_cache_st); |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 506 | if (st == NULL) |
| 507 | return -1; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 508 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 509 | st->first_block = NULL; |
| 510 | filter->ctx = st; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 511 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 512 | /* Register post-analyzer on AN_RES_WAIT_HTTP */ |
| 513 | filter->post_analyzers |= AN_RES_WAIT_HTTP; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 514 | return 1; |
| 515 | } |
| 516 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 517 | static void |
| 518 | cache_store_strm_deinit(struct stream *s, struct filter *filter) |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 519 | { |
| 520 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 521 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 522 | struct cache *cache = cconf->c.cache; |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 523 | struct shared_context *shctx = shctx_ptr(cache); |
| 524 | |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 525 | /* Everything should be released in the http_end filter, but we need to do it |
| 526 | * there too, in case of errors */ |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 527 | if (st && st->first_block) { |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 528 | shctx_lock(shctx); |
| 529 | shctx_row_dec_hot(shctx, st->first_block); |
| 530 | shctx_unlock(shctx); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 531 | } |
| 532 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 533 | pool_free(pool_head_cache_st, st); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 534 | filter->ctx = NULL; |
| 535 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 538 | static int |
| 539 | cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn, |
| 540 | unsigned an_bit) |
| 541 | { |
| 542 | struct http_txn *txn = s->txn; |
| 543 | struct http_msg *msg = &txn->rsp; |
| 544 | struct cache_st *st = filter->ctx; |
| 545 | |
| 546 | if (an_bit != AN_RES_WAIT_HTTP) |
| 547 | goto end; |
| 548 | |
| 549 | /* Here we need to check if any compression filter precedes the cache |
| 550 | * filter. This is only possible when the compression is configured in |
| 551 | * the frontend while the cache filter is configured on the |
| 552 | * backend. This case cannot be detected during HAProxy startup. So in |
| 553 | * such cases, the cache is disabled. |
| 554 | */ |
| 555 | if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) { |
| 556 | pool_free(pool_head_cache_st, st); |
| 557 | filter->ctx = NULL; |
| 558 | } |
| 559 | |
| 560 | end: |
| 561 | return 1; |
| 562 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 563 | |
| 564 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 565 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 566 | { |
| 567 | struct cache_st *st = filter->ctx; |
| 568 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 569 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 570 | return 1; |
| 571 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 572 | if (st->first_block) |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 573 | register_data_filter(s, msg->chn, filter); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 574 | return 1; |
| 575 | } |
| 576 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 577 | static inline void disable_cache_entry(struct cache_st *st, |
| 578 | struct filter *filter, struct shared_context *shctx) |
| 579 | { |
| 580 | struct cache_entry *object; |
| 581 | |
| 582 | object = (struct cache_entry *)st->first_block->data; |
| 583 | filter->ctx = NULL; /* disable cache */ |
| 584 | shctx_lock(shctx); |
| 585 | shctx_row_dec_hot(shctx, st->first_block); |
Remi Tricot-Le Breton | 964caaf | 2020-12-15 14:30:12 +0100 | [diff] [blame] | 586 | eb32_delete(&object->eb); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 587 | object->eb.key = 0; |
| 588 | shctx_unlock(shctx); |
| 589 | pool_free(pool_head_cache_st, st); |
| 590 | } |
| 591 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 592 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 593 | cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 594 | unsigned int offset, unsigned int len) |
| 595 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 596 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 597 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 598 | struct cache_st *st = filter->ctx; |
| 599 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 600 | struct htx_blk *blk; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 601 | struct shared_block *fb; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 602 | struct htx_ret htxret; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 603 | unsigned int orig_len, to_forward; |
| 604 | int ret; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 605 | |
| 606 | if (!len) |
| 607 | return len; |
| 608 | |
| 609 | if (!st->first_block) { |
| 610 | unregister_data_filter(s, msg->chn, filter); |
| 611 | return len; |
| 612 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 613 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 614 | chunk_reset(&trash); |
| 615 | orig_len = len; |
| 616 | to_forward = 0; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 617 | |
| 618 | htxret = htx_find_offset(htx, offset); |
| 619 | blk = htxret.blk; |
| 620 | offset = htxret.ret; |
| 621 | for (; blk && len; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 622 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 623 | uint32_t info, sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 624 | struct ist v; |
| 625 | |
| 626 | switch (type) { |
| 627 | case HTX_BLK_UNUSED: |
| 628 | break; |
| 629 | |
| 630 | case HTX_BLK_DATA: |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 631 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 154374c | 2021-03-02 18:57:27 +0100 | [diff] [blame] | 632 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 633 | v = isttrim(v, len); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 634 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 635 | info = (type << 28) + v.len; |
| 636 | chunk_memcat(&trash, (char *)&info, sizeof(info)); |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 637 | chunk_istcat(&trash, v); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 638 | to_forward += v.len; |
| 639 | len -= v.len; |
| 640 | break; |
| 641 | |
| 642 | default: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 643 | /* Here offset must always be 0 because only |
| 644 | * DATA blocks can be partially transferred. */ |
| 645 | if (offset) |
| 646 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 647 | if (sz > len) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 648 | goto end; |
| 649 | |
| 650 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 651 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 652 | to_forward += sz; |
| 653 | len -= sz; |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | offset = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 658 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 659 | |
| 660 | end: |
| 661 | shctx_lock(shctx); |
| 662 | fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data); |
| 663 | if (!fb) { |
| 664 | shctx_unlock(shctx); |
| 665 | goto no_cache; |
| 666 | } |
| 667 | shctx_unlock(shctx); |
| 668 | |
| 669 | ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append, |
| 670 | (unsigned char *)b_head(&trash), b_data(&trash)); |
| 671 | if (ret < 0) |
| 672 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 673 | |
| 674 | return to_forward; |
| 675 | |
| 676 | no_cache: |
| 677 | disable_cache_entry(st, filter, shctx); |
| 678 | unregister_data_filter(s, msg->chn, filter); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 679 | return orig_len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 683 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 684 | struct http_msg *msg) |
| 685 | { |
| 686 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 687 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 688 | struct cache *cache = cconf->c.cache; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 689 | struct shared_context *shctx = shctx_ptr(cache); |
| 690 | struct cache_entry *object; |
| 691 | |
| 692 | if (!(msg->chn->flags & CF_ISRESP)) |
| 693 | return 1; |
| 694 | |
| 695 | if (st && st->first_block) { |
| 696 | |
| 697 | object = (struct cache_entry *)st->first_block->data; |
| 698 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 699 | shctx_lock(shctx); |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 700 | /* The whole payload was cached, the entry can now be used. */ |
| 701 | object->complete = 1; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 702 | /* remove from the hotlist */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 703 | shctx_row_dec_hot(shctx, st->first_block); |
| 704 | shctx_unlock(shctx); |
| 705 | |
| 706 | } |
| 707 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 708 | pool_free(pool_head_cache_st, st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 709 | filter->ctx = NULL; |
| 710 | } |
| 711 | |
| 712 | return 1; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * This intends to be used when checking HTTP headers for some |
| 717 | * word=value directive. Return a pointer to the first character of value, if |
Willy Tarreau | 94a01e1 | 2021-01-06 17:35:12 +0100 | [diff] [blame] | 718 | * the word was not found or if there wasn't any value assigned to it return NULL |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 719 | */ |
| 720 | char *directive_value(const char *sample, int slen, const char *word, int wlen) |
| 721 | { |
| 722 | int st = 0; |
| 723 | |
| 724 | if (slen < wlen) |
| 725 | return 0; |
| 726 | |
| 727 | while (wlen) { |
| 728 | char c = *sample ^ *word; |
| 729 | if (c && c != ('A' ^ 'a')) |
| 730 | return NULL; |
| 731 | sample++; |
| 732 | word++; |
| 733 | slen--; |
| 734 | wlen--; |
| 735 | } |
| 736 | |
| 737 | while (slen) { |
| 738 | if (st == 0) { |
| 739 | if (*sample != '=') |
| 740 | return NULL; |
| 741 | sample++; |
| 742 | slen--; |
| 743 | st = 1; |
| 744 | continue; |
| 745 | } else { |
| 746 | return (char *)sample; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | return NULL; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Return the maxage in seconds of an HTTP response. |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 755 | * The returned value will always take the cache's configuration into account |
| 756 | * (cache->maxage) but the actual max age of the response will be set in the |
| 757 | * true_maxage parameter. It will be used to determine if a response is already |
| 758 | * stale or not. |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 759 | * Compute the maxage using either: |
| 760 | * - the assigned max-age of the cache |
| 761 | * - the s-maxage directive |
| 762 | * - the max-age directive |
| 763 | * - (Expires - Data) headers |
| 764 | * - the default-max-age of the cache |
| 765 | * |
| 766 | */ |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 767 | 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] | 768 | { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 769 | struct htx *htx = htxbuf(&s->res.buf); |
| 770 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 771 | long smaxage = -1; |
| 772 | long maxage = -1; |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 773 | int expires = -1; |
| 774 | struct tm tm = {}; |
| 775 | time_t expires_val = 0; |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 776 | char *endptr = NULL; |
| 777 | int offset = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 778 | |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 779 | /* The Cache-Control max-age and s-maxage directives should be followed by |
| 780 | * a positive numerical value (see RFC 7234#5.2.1.1). According to the |
| 781 | * specs, a sender "should not" generate a quoted-string value but we will |
| 782 | * still accept this format since it isn't strictly forbidden. */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 783 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 784 | char *value; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 785 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 786 | value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8); |
| 787 | if (value) { |
| 788 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 789 | |
Willy Tarreau | 49b0482 | 2021-11-08 11:44:47 +0100 | [diff] [blame] | 790 | chunk_memcat(chk, value, ctx.value.len - 8 + 1); |
| 791 | chunk_memcat(chk, "", 1); |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 792 | offset = (*chk->area == '"') ? 1 : 0; |
| 793 | smaxage = strtol(chk->area + offset, &endptr, 10); |
Willy Tarreau | 1f38bdb | 2021-11-08 12:09:27 +0100 | [diff] [blame] | 794 | if (unlikely(smaxage < 0 || endptr == chk->area + offset)) |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 795 | return -1; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 798 | value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7); |
| 799 | if (value) { |
| 800 | struct buffer *chk = get_trash_chunk(); |
Christopher Faulet | 5f2c49f | 2019-07-15 20:49:46 +0200 | [diff] [blame] | 801 | |
Willy Tarreau | 49b0482 | 2021-11-08 11:44:47 +0100 | [diff] [blame] | 802 | chunk_memcat(chk, value, ctx.value.len - 7 + 1); |
| 803 | chunk_memcat(chk, "", 1); |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 804 | offset = (*chk->area == '"') ? 1 : 0; |
| 805 | maxage = strtol(chk->area + offset, &endptr, 10); |
Willy Tarreau | 1f38bdb | 2021-11-08 12:09:27 +0100 | [diff] [blame] | 806 | if (unlikely(maxage < 0 || endptr == chk->area + offset)) |
Remi Tricot-Le Breton | fcea374 | 2020-12-03 18:19:30 +0100 | [diff] [blame] | 807 | return -1; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 811 | /* Look for Expires header if no s-maxage or max-age Cache-Control data |
| 812 | * was found. */ |
| 813 | if (maxage == -1 && smaxage == -1) { |
| 814 | ctx.blk = NULL; |
| 815 | if (http_find_header(htx, ist("expires"), &ctx, 1)) { |
| 816 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 817 | expires_val = my_timegm(&tm); |
| 818 | /* A request having an expiring date earlier |
| 819 | * than the current date should be considered as |
| 820 | * stale. */ |
| 821 | expires = (expires_val >= now.tv_sec) ? |
| 822 | (expires_val - now.tv_sec) : 0; |
| 823 | } |
| 824 | else { |
| 825 | /* Following RFC 7234#5.3, an invalid date |
| 826 | * format must be treated as a date in the past |
| 827 | * so the cache entry must be seen as already |
| 828 | * expired. */ |
| 829 | expires = 0; |
| 830 | } |
| 831 | } |
| 832 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 833 | |
| 834 | |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 835 | if (smaxage > 0) { |
| 836 | if (true_maxage) |
| 837 | *true_maxage = smaxage; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 838 | return MIN(smaxage, cache->maxage); |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 839 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 840 | |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 841 | if (maxage > 0) { |
| 842 | if (true_maxage) |
| 843 | *true_maxage = maxage; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 844 | return MIN(maxage, cache->maxage); |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 845 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 846 | |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 847 | if (expires >= 0) { |
| 848 | if (true_maxage) |
| 849 | *true_maxage = expires; |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 850 | return MIN(expires, cache->maxage); |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 851 | } |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 852 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 853 | return cache->maxage; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 854 | |
| 855 | } |
| 856 | |
| 857 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 858 | static void cache_free_blocks(struct shared_block *first, struct shared_block *block) |
| 859 | { |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 860 | struct cache_entry *object = (struct cache_entry *)block->data; |
| 861 | |
| 862 | if (first == block && object->eb.key) |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 863 | delete_entry(object); |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 864 | object->eb.key = 0; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 865 | } |
| 866 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 867 | |
| 868 | /* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the |
| 869 | * date value should be compared to a date determined by in a previous response (for |
| 870 | * the same entity). This date could either be the "Last-Modified" value, or the "Date" |
| 871 | * value of the response's reception time (by decreasing order of priority). */ |
| 872 | static time_t get_last_modified_time(struct htx *htx) |
| 873 | { |
| 874 | time_t last_modified = 0; |
| 875 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 876 | struct tm tm = {}; |
| 877 | |
| 878 | if (http_find_header(htx, ist("last-modified"), &ctx, 1)) { |
| 879 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 880 | last_modified = my_timegm(&tm); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | if (!last_modified) { |
| 885 | ctx.blk = NULL; |
| 886 | if (http_find_header(htx, ist("date"), &ctx, 1)) { |
| 887 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 888 | last_modified = my_timegm(&tm); |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /* Fallback on the current time if no "Last-Modified" or "Date" header |
| 894 | * was found. */ |
| 895 | if (!last_modified) |
| 896 | last_modified = now.tv_sec; |
| 897 | |
| 898 | return last_modified; |
| 899 | } |
| 900 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 901 | /* |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 902 | * Checks the vary header's value. The headers on which vary should be applied |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 903 | * must be explicitly supported in the vary_information array (see cache.c). If |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 904 | * any other header is mentioned, we won't store the response. |
| 905 | * Returns 1 if Vary-based storage can work, 0 otherwise. |
| 906 | */ |
| 907 | static int http_check_vary_header(struct htx *htx, unsigned int *vary_signature) |
| 908 | { |
| 909 | unsigned int vary_idx; |
| 910 | unsigned int vary_info_count; |
| 911 | const struct vary_hashing_information *vary_info; |
| 912 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 913 | |
| 914 | int retval = 1; |
| 915 | |
| 916 | *vary_signature = 0; |
| 917 | |
| 918 | vary_info_count = sizeof(vary_information)/sizeof(*vary_information); |
| 919 | while (retval && http_find_header(htx, ist("Vary"), &ctx, 0)) { |
| 920 | for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) { |
| 921 | vary_info = &vary_information[vary_idx]; |
| 922 | if (isteqi(ctx.value, vary_info->hdr_name)) { |
| 923 | *vary_signature |= vary_info->value; |
| 924 | break; |
| 925 | } |
| 926 | } |
| 927 | retval = (vary_idx < vary_info_count); |
| 928 | } |
| 929 | |
| 930 | return retval; |
| 931 | } |
| 932 | |
| 933 | |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 934 | /* |
| 935 | * Look for the accept-encoding part of the secondary_key and replace the |
| 936 | * encoding bitmap part of the hash with the actual encoding of the response, |
| 937 | * extracted from the content-encoding header value. |
Remi Tricot-Le Breton | 6ca8916 | 2021-01-07 14:50:51 +0100 | [diff] [blame] | 938 | * Responses that have an unknown encoding will not be cached if they also |
| 939 | * "vary" on the accept-encoding value. |
| 940 | * Returns 0 if we found a known encoding in the response, -1 otherwise. |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 941 | */ |
Remi Tricot-Le Breton | 6ca8916 | 2021-01-07 14:50:51 +0100 | [diff] [blame] | 942 | static int set_secondary_key_encoding(struct htx *htx, char *secondary_key) |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 943 | { |
| 944 | unsigned int resp_encoding_bitmap = 0; |
| 945 | const struct vary_hashing_information *info = vary_information; |
| 946 | unsigned int offset = 0; |
| 947 | unsigned int count = 0; |
| 948 | unsigned int hash_info_count = sizeof(vary_information)/sizeof(*vary_information); |
| 949 | unsigned int encoding_value; |
| 950 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 951 | |
| 952 | /* Look for the accept-encoding part of the secondary_key. */ |
| 953 | while (count < hash_info_count && info->value != VARY_ACCEPT_ENCODING) { |
| 954 | offset += info->hash_length; |
| 955 | ++info; |
| 956 | ++count; |
| 957 | } |
| 958 | |
| 959 | if (count == hash_info_count) |
Remi Tricot-Le Breton | 6ca8916 | 2021-01-07 14:50:51 +0100 | [diff] [blame] | 960 | return -1; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 961 | |
| 962 | while (http_find_header(htx, ist("content-encoding"), &ctx, 0)) { |
Remi Tricot-Le Breton | 6ca8916 | 2021-01-07 14:50:51 +0100 | [diff] [blame] | 963 | if (parse_encoding_value(ctx.value, &encoding_value, NULL)) |
| 964 | return -1; /* Do not store responses with an unknown encoding */ |
| 965 | resp_encoding_bitmap |= encoding_value; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | if (!resp_encoding_bitmap) |
| 969 | resp_encoding_bitmap |= VARY_ENCODING_IDENTITY; |
| 970 | |
| 971 | /* Rewrite the bitmap part of the hash with the new bitmap that only |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 972 | * corresponds the the response's encoding. */ |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 973 | write_u32(secondary_key + offset, resp_encoding_bitmap); |
Remi Tricot-Le Breton | 6ca8916 | 2021-01-07 14:50:51 +0100 | [diff] [blame] | 974 | |
| 975 | return 0; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 978 | |
| 979 | /* |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 980 | * 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] | 981 | * register a filter to store the data |
| 982 | */ |
| 983 | 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] | 984 | struct session *sess, struct stream *s, int flags) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 985 | { |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 986 | int effective_maxage = 0; |
| 987 | int true_maxage = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 988 | struct http_txn *txn = s->txn; |
| 989 | struct http_msg *msg = &txn->rsp; |
| 990 | struct filter *filter; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 991 | struct shared_block *first = NULL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 992 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 993 | struct cache *cache = cconf->c.cache; |
| 994 | struct shared_context *shctx = shctx_ptr(cache); |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 995 | struct cache_st *cache_ctx = NULL; |
| 996 | struct cache_entry *object, *old; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 997 | unsigned int key = read_u32(txn->cache_hash); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 998 | struct htx *htx; |
| 999 | struct http_hdr_ctx ctx; |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 1000 | size_t hdrs_len = 0; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1001 | int32_t pos; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1002 | unsigned int vary_signature = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1003 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1004 | /* Don't cache if the response came from a cache */ |
| 1005 | if ((obj_type(s->target) == OBJ_TYPE_APPLET) && |
| 1006 | s->target == &http_cache_applet.obj_type) { |
| 1007 | goto out; |
| 1008 | } |
| 1009 | |
| 1010 | /* cache only HTTP/1.1 */ |
| 1011 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 1012 | goto out; |
| 1013 | |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1014 | /* cache only GET method */ |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 1015 | if (txn->meth != HTTP_METH_GET) { |
| 1016 | /* In case of successful unsafe method on a stored resource, the |
| 1017 | * cached entry must be invalidated (see RFC7234#4.4). |
| 1018 | * A "non-error response" is one with a 2xx (Successful) or 3xx |
| 1019 | * (Redirection) status code. */ |
| 1020 | if (txn->status >= 200 && txn->status < 400) { |
| 1021 | switch (txn->meth) { |
| 1022 | case HTTP_METH_OPTIONS: |
| 1023 | case HTTP_METH_GET: |
| 1024 | case HTTP_METH_HEAD: |
| 1025 | case HTTP_METH_TRACE: |
| 1026 | break; |
| 1027 | |
| 1028 | default: /* Any unsafe method */ |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 1029 | /* Discard any corresponding entry in case of successful |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 1030 | * unsafe request (such as PUT, POST or DELETE). */ |
| 1031 | shctx_lock(shctx); |
| 1032 | |
| 1033 | old = entry_exist(cconf->c.cache, txn->cache_hash); |
| 1034 | if (old) { |
| 1035 | eb32_delete(&old->eb); |
| 1036 | old->eb.key = 0; |
| 1037 | } |
| 1038 | shctx_unlock(shctx); |
| 1039 | } |
| 1040 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1041 | goto out; |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 1042 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1043 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 1044 | /* cache key was not computed */ |
| 1045 | if (!key) |
| 1046 | goto out; |
| 1047 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1048 | /* cache only 200 status code */ |
| 1049 | if (txn->status != 200) |
| 1050 | goto out; |
| 1051 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1052 | /* Find the corresponding filter instance for the current stream */ |
| 1053 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 1054 | if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) { |
| 1055 | /* No filter ctx, don't cache anything */ |
| 1056 | if (!filter->ctx) |
| 1057 | goto out; |
| 1058 | cache_ctx = filter->ctx; |
| 1059 | break; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | /* from there, cache_ctx is always defined */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1064 | htx = htxbuf(&s->res.buf); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1065 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1066 | /* Do not cache too big objects. */ |
| 1067 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 && |
| 1068 | htx->data + htx->extra > shctx->max_obj_size) |
| 1069 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1070 | |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1071 | /* Only a subset of headers are supported in our Vary implementation. If |
| 1072 | * 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] | 1073 | * able to use the cache. Likewise, if Vary header support is disabled, |
| 1074 | * avoid caching responses that contain such a header. */ |
| 1075 | ctx.blk = NULL; |
| 1076 | if (cache->vary_processing_enabled) { |
| 1077 | if (!http_check_vary_header(htx, &vary_signature)) |
| 1078 | goto out; |
Remi Tricot-Le Breton | 2b5c5cb | 2020-12-23 18:13:45 +0100 | [diff] [blame] | 1079 | if (vary_signature) { |
| 1080 | /* If something went wrong during the secondary key |
| 1081 | * building, do not store the response. */ |
| 1082 | if (!(txn->flags & TX_CACHE_HAS_SEC_KEY)) |
| 1083 | goto out; |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1084 | http_request_reduce_secondary_key(vary_signature, txn->cache_secondary_hash); |
Remi Tricot-Le Breton | 2b5c5cb | 2020-12-23 18:13:45 +0100 | [diff] [blame] | 1085 | } |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 1086 | } |
| 1087 | else if (http_find_header(htx, ist("Vary"), &ctx, 0)) { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1088 | goto out; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1089 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1090 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1091 | http_check_response_for_cacheability(s, &s->res); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1092 | |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 1093 | 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] | 1094 | goto out; |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1095 | |
| 1096 | shctx_lock(shctx); |
| 1097 | old = entry_exist(cache, txn->cache_hash); |
| 1098 | if (old) { |
| 1099 | if (vary_signature) |
| 1100 | old = secondary_entry_exist(cconf->c.cache, old, |
| 1101 | txn->cache_secondary_hash); |
| 1102 | if (old) { |
| 1103 | if (!old->complete) { |
| 1104 | /* An entry with the same primary key is already being |
| 1105 | * created, we should not try to store the current |
| 1106 | * response because it will waste space in the cache. */ |
| 1107 | shctx_unlock(shctx); |
| 1108 | goto out; |
| 1109 | } |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 1110 | delete_entry(old); |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1111 | old->eb.key = 0; |
| 1112 | } |
| 1113 | } |
| 1114 | first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry)); |
| 1115 | if (!first) { |
| 1116 | shctx_unlock(shctx); |
| 1117 | goto out; |
| 1118 | } |
| 1119 | /* the received memory is not initialized, we need at least to mark |
| 1120 | * the object as not indexed yet. |
| 1121 | */ |
| 1122 | object = (struct cache_entry *)first->data; |
| 1123 | memset(object, 0, sizeof(*object)); |
| 1124 | object->eb.key = key; |
| 1125 | object->secondary_key_signature = vary_signature; |
| 1126 | /* We need to temporarily set a valid expiring time until the actual one |
| 1127 | * is set by the end of this function (in case of concurrent accesses to |
| 1128 | * the same resource). This way the second access will find an existing |
| 1129 | * but not yet usable entry in the tree and will avoid storing its data. */ |
| 1130 | object->expire = now.tv_sec + 2; |
| 1131 | |
| 1132 | memcpy(object->hash, txn->cache_hash, sizeof(object->hash)); |
| 1133 | if (vary_signature) |
| 1134 | memcpy(object->secondary_key, txn->cache_secondary_hash, HTTP_CACHE_SEC_KEY_LEN); |
| 1135 | |
| 1136 | /* Insert the entry in the tree even if the payload is not cached yet. */ |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 1137 | if (insert_entry(cache, object) != &object->eb) { |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1138 | object->eb.key = 0; |
| 1139 | shctx_unlock(shctx); |
| 1140 | goto out; |
| 1141 | } |
| 1142 | shctx_unlock(shctx); |
| 1143 | |
| 1144 | /* reserve space for the cache_entry structure */ |
| 1145 | first->len = sizeof(struct cache_entry); |
| 1146 | first->last_append = NULL; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1147 | |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 1148 | /* Determine the entry's maximum age (taking into account the cache's |
| 1149 | * configuration) as well as the response's explicit max age (extracted |
| 1150 | * from cache-control directives or the expires header). */ |
| 1151 | effective_maxage = http_calc_maxage(s, cconf->c.cache, &true_maxage); |
| 1152 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1153 | ctx.blk = NULL; |
| 1154 | if (http_find_header(htx, ist("Age"), &ctx, 0)) { |
Tim Duesterhus | c294284 | 2021-01-02 22:47:17 +0100 | [diff] [blame] | 1155 | long long hdr_age; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1156 | if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) { |
| 1157 | if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE)) |
| 1158 | hdr_age = CACHE_ENTRY_MAX_AGE; |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 1159 | /* A response with an Age value greater than its |
| 1160 | * announced max age is stale and should not be stored. */ |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1161 | object->age = hdr_age; |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 1162 | if (unlikely(object->age > true_maxage)) |
| 1163 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1164 | } |
Remi Tricot-Le Breton | 51058d6 | 2020-12-03 18:19:32 +0100 | [diff] [blame] | 1165 | else |
| 1166 | goto out; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1167 | http_remove_header(htx, &ctx); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1168 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1169 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 1170 | /* Build a last-modified time that will be stored in the cache_entry and |
| 1171 | * compared to a future If-Modified-Since client header. */ |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1172 | object->last_modified = get_last_modified_time(htx); |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 1173 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1174 | chunk_reset(&trash); |
| 1175 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 1176 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1177 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1178 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1179 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 1180 | hdrs_len += sizeof(*blk) + sz; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1181 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 1182 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 1183 | |
| 1184 | /* Look for optional ETag header. |
| 1185 | * We need to store the offset of the ETag value in order for |
| 1186 | * future conditional requests to be able to perform ETag |
| 1187 | * comparisons. */ |
| 1188 | if (type == HTX_BLK_HDR) { |
Tim Duesterhus | e2fff10 | 2021-01-02 22:47:16 +0100 | [diff] [blame] | 1189 | struct ist header_name = htx_get_blk_name(htx, blk); |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 1190 | if (isteq(header_name, ist("etag"))) { |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1191 | object->etag_length = sz - istlen(header_name); |
| 1192 | 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] | 1193 | } |
| 1194 | } |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1195 | if (type == HTX_BLK_EOH) |
| 1196 | break; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 1199 | /* Do not cache objects if the headers are too big. */ |
| 1200 | if (hdrs_len > htx->size - global.tune.maxrewrite) |
| 1201 | goto out; |
| 1202 | |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 1203 | /* If the response has a secondary_key, fill its key part related to |
| 1204 | * encodings with the actual encoding of the response. This way any |
| 1205 | * subsequent request having the same primary key will have its accepted |
Remi Tricot-Le Breton | 6ca8916 | 2021-01-07 14:50:51 +0100 | [diff] [blame] | 1206 | * encodings tested upon the cached response's one. |
| 1207 | * We will not cache a response that has an unknown encoding (not |
Ilya Shipitsin | 7704b0e | 2021-01-23 02:11:59 +0500 | [diff] [blame] | 1208 | * explicitly supported in parse_encoding_value function). */ |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 1209 | if (cache->vary_processing_enabled && vary_signature) |
Remi Tricot-Le Breton | 6ca8916 | 2021-01-07 14:50:51 +0100 | [diff] [blame] | 1210 | if (set_secondary_key_encoding(htx, object->secondary_key)) |
| 1211 | goto out; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 1212 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1213 | shctx_lock(shctx); |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1214 | if (!shctx_row_reserve_hot(shctx, first, trash.data)) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1215 | shctx_unlock(shctx); |
| 1216 | goto out; |
| 1217 | } |
| 1218 | shctx_unlock(shctx); |
| 1219 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1220 | /* cache the headers in a http action because it allows to chose what |
| 1221 | * to cache, for example you might want to cache a response before |
| 1222 | * modifying some HTTP headers, or on the contrary after modifying |
| 1223 | * those headers. |
| 1224 | */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1225 | /* does not need to be locked because it's in the "hot" list, |
| 1226 | * copy the headers */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1227 | if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0) |
| 1228 | goto out; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1229 | |
| 1230 | /* register the buffer in the filter ctx for filling it with data*/ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1231 | if (cache_ctx) { |
| 1232 | cache_ctx->first_block = first; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1233 | /* store latest value and expiration time */ |
| 1234 | object->latest_validation = now.tv_sec; |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 1235 | object->expire = now.tv_sec + effective_maxage; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1236 | return ACT_RET_CONT; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | out: |
| 1240 | /* if does not cache */ |
| 1241 | if (first) { |
| 1242 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 1243 | first->len = 0; |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1244 | if (object->eb.key) |
Remi Tricot-Le Breton | 65904e4 | 2020-12-10 17:58:41 +0100 | [diff] [blame] | 1245 | delete_entry(object); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 1246 | object->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1247 | shctx_row_dec_hot(shctx, first); |
| 1248 | shctx_unlock(shctx); |
| 1249 | } |
| 1250 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1251 | return ACT_RET_CONT; |
| 1252 | } |
| 1253 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1254 | #define HTX_CACHE_INIT 0 /* Initial state. */ |
| 1255 | #define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */ |
| 1256 | #define HTX_CACHE_DATA 2 /* Cache entry data forwarding */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1257 | #define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */ |
| 1258 | #define HTX_CACHE_END 4 /* Cache entry treatment terminated */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1259 | |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1260 | static void http_cache_applet_release(struct appctx *appctx) |
| 1261 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1262 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1263 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1264 | struct cache *cache = cconf->c.cache; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1265 | struct shared_block *first = block_ptr(cache_ptr); |
| 1266 | |
| 1267 | shctx_lock(shctx_ptr(cache)); |
| 1268 | shctx_row_dec_hot(shctx_ptr(cache), first); |
| 1269 | shctx_unlock(shctx_ptr(cache)); |
| 1270 | } |
| 1271 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1272 | |
| 1273 | static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type, |
| 1274 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1275 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1276 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 1277 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1278 | struct htx_blk *blk; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 1279 | char *ptr; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1280 | unsigned int max, total; |
| 1281 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1282 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1283 | max = htx_get_max_blksz(htx, channel_htx_recv_max(cs_ic(appctx->owner), htx)); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1284 | if (!max) |
| 1285 | return 0; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1286 | blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1287 | ? (info & 0xff) + ((info >> 8) & 0xfffff) |
| 1288 | : info & 0xfffffff); |
| 1289 | if (blksz > max) |
| 1290 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1291 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1292 | blk = htx_add_blk(htx, type, blksz); |
| 1293 | if (!blk) |
| 1294 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1295 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1296 | blk->info = info; |
| 1297 | total = 4; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 1298 | ptr = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1299 | while (blksz) { |
| 1300 | max = MIN(blksz, shctx->block_size - offset); |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 1301 | memcpy(ptr, (const char *)shblk->data + offset, max); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1302 | offset += max; |
| 1303 | blksz -= max; |
| 1304 | total += max; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 1305 | ptr += max; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1306 | if (blksz || offset == shctx->block_size) { |
| 1307 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 1308 | offset = 0; |
| 1309 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1310 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1311 | appctx->ctx.cache.offset = offset; |
| 1312 | appctx->ctx.cache.next = shblk; |
| 1313 | appctx->ctx.cache.sent += total; |
| 1314 | return total; |
| 1315 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1316 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1317 | static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx, |
| 1318 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
| 1319 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1320 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1321 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 1322 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
| 1323 | unsigned int max, total, rem_data; |
| 1324 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1325 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1326 | max = htx_get_max_blksz(htx, channel_htx_recv_max(cs_ic(appctx->owner), htx)); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1327 | if (!max) |
| 1328 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1329 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1330 | rem_data = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 1331 | if (appctx->ctx.cache.rem_data) { |
| 1332 | blksz = appctx->ctx.cache.rem_data; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1333 | total = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 1334 | } |
| 1335 | else { |
| 1336 | blksz = (info & 0xfffffff); |
| 1337 | total = 4; |
| 1338 | } |
| 1339 | if (blksz > max) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1340 | rem_data = blksz - max; |
| 1341 | blksz = max; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1342 | } |
| 1343 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1344 | while (blksz) { |
| 1345 | size_t sz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1346 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1347 | max = MIN(blksz, shctx->block_size - offset); |
| 1348 | sz = htx_add_data(htx, ist2(shblk->data + offset, max)); |
| 1349 | offset += sz; |
| 1350 | blksz -= sz; |
| 1351 | total += sz; |
| 1352 | if (sz < max) |
| 1353 | break; |
| 1354 | if (blksz || offset == shctx->block_size) { |
| 1355 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 1356 | offset = 0; |
| 1357 | } |
| 1358 | } |
| 1359 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1360 | appctx->ctx.cache.offset = offset; |
| 1361 | appctx->ctx.cache.next = shblk; |
| 1362 | appctx->ctx.cache.sent += total; |
| 1363 | appctx->ctx.cache.rem_data = rem_data + blksz; |
| 1364 | return total; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1367 | static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len, |
| 1368 | enum htx_blk_type mark) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1369 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1370 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 1371 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1372 | struct shared_block *shblk; |
| 1373 | unsigned int offset, sz; |
| 1374 | unsigned int ret, total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1375 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1376 | while (len) { |
| 1377 | enum htx_blk_type type; |
| 1378 | uint32_t info; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1379 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1380 | shblk = appctx->ctx.cache.next; |
| 1381 | offset = appctx->ctx.cache.offset; |
| 1382 | if (appctx->ctx.cache.rem_data) { |
| 1383 | type = HTX_BLK_DATA; |
| 1384 | info = 0; |
| 1385 | goto add_data_blk; |
| 1386 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1387 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1388 | /* 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] | 1389 | sz = MIN(4, shctx->block_size - offset); |
| 1390 | memcpy((char *)&info, (const char *)shblk->data + offset, sz); |
| 1391 | offset += sz; |
| 1392 | if (sz < 4) { |
| 1393 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 1394 | memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz); |
| 1395 | offset = (4 - sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1396 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1397 | |
| 1398 | /* Get payload of the next HTX block and insert it. */ |
| 1399 | type = (info >> 28); |
| 1400 | if (type != HTX_BLK_DATA) |
| 1401 | ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset); |
| 1402 | else { |
| 1403 | add_data_blk: |
| 1404 | ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1405 | } |
| 1406 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1407 | if (!ret) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1408 | break; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1409 | total += ret; |
| 1410 | len -= ret; |
| 1411 | |
| 1412 | if (appctx->ctx.cache.rem_data || type == mark) |
| 1413 | break; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1414 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1415 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1416 | return total; |
| 1417 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1418 | |
| 1419 | static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx) |
| 1420 | { |
| 1421 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 1422 | unsigned int age; |
| 1423 | char *end; |
| 1424 | |
| 1425 | chunk_reset(&trash); |
| 1426 | age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age; |
| 1427 | if (unlikely(age > CACHE_ENTRY_MAX_AGE)) |
| 1428 | age = CACHE_ENTRY_MAX_AGE; |
| 1429 | end = ultoa_o(age, b_head(&trash), b_size(&trash)); |
| 1430 | b_set_data(&trash, end - b_head(&trash)); |
| 1431 | if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash)))) |
| 1432 | return 0; |
| 1433 | return 1; |
| 1434 | } |
| 1435 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1436 | static void http_cache_io_handler(struct appctx *appctx) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1437 | { |
| 1438 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 1439 | struct shared_block *first = block_ptr(cache_ptr); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1440 | struct conn_stream *cs = appctx->owner; |
| 1441 | struct channel *req = cs_oc(cs); |
| 1442 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1443 | struct htx *req_htx, *res_htx; |
| 1444 | struct buffer *errmsg; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1445 | unsigned int len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1446 | size_t ret, total = 0; |
| 1447 | |
Christopher Faulet | 8b1eed1 | 2022-03-07 16:44:30 +0100 | [diff] [blame] | 1448 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1449 | total = res_htx->data; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1450 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1451 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1452 | goto out; |
| 1453 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1454 | /* Check if the input buffer is available. */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1455 | if (!b_size(&res->buf)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1456 | cs_rx_room_blk(cs); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1457 | goto out; |
| 1458 | } |
| 1459 | |
Willy Tarreau | efef323 | 2018-12-16 00:37:45 +0100 | [diff] [blame] | 1460 | if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW)) |
Willy Tarreau | 273e964 | 2018-12-16 00:35:15 +0100 | [diff] [blame] | 1461 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1462 | |
| 1463 | if (appctx->st0 == HTX_CACHE_INIT) { |
| 1464 | appctx->ctx.cache.next = block_ptr(cache_ptr); |
| 1465 | appctx->ctx.cache.offset = sizeof(*cache_ptr); |
| 1466 | appctx->ctx.cache.sent = 0; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1467 | appctx->ctx.cache.rem_data = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1468 | appctx->st0 = HTX_CACHE_HEADER; |
| 1469 | } |
| 1470 | |
| 1471 | if (appctx->st0 == HTX_CACHE_HEADER) { |
| 1472 | /* Headers must be dump at once. Otherwise it is an error */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1473 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1474 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH); |
| 1475 | if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) || |
| 1476 | !htx_cache_add_age_hdr(appctx, res_htx)) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1477 | goto error; |
| 1478 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1479 | /* In case of a conditional request, we might want to send a |
| 1480 | * "304 Not Modified" response instead of the stored data. */ |
Tim Duesterhus | e014234 | 2020-10-22 21:15:06 +0200 | [diff] [blame] | 1481 | if (appctx->ctx.cache.send_notmodified) { |
| 1482 | if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) { |
| 1483 | /* If replacing the status code fails we need to send the full response. */ |
| 1484 | appctx->ctx.cache.send_notmodified = 0; |
| 1485 | } |
| 1486 | } |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1487 | |
| 1488 | /* Skip response body for HEAD requests or in case of "304 Not |
| 1489 | * Modified" response. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1490 | if (__cs_strm(cs)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified) |
Christopher Faulet | f0dd037 | 2019-02-25 11:08:34 +0100 | [diff] [blame] | 1491 | appctx->st0 = HTX_CACHE_EOM; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1492 | else |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1493 | appctx->st0 = HTX_CACHE_DATA; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | if (appctx->st0 == HTX_CACHE_DATA) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1497 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1498 | if (len) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1499 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_UNUSED); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1500 | if (ret < len) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1501 | cs_rx_room_blk(cs); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1502 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1503 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1504 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1505 | appctx->st0 = HTX_CACHE_EOM; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | if (appctx->st0 == HTX_CACHE_EOM) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1509 | /* no more data are expected. */ |
| 1510 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1511 | cs->endp->flags |= CS_EP_EOI; |
Christopher Faulet | dbf1e88 | 2022-03-07 15:53:57 +0100 | [diff] [blame] | 1512 | res->flags |= CF_EOI; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1513 | appctx->st0 = HTX_CACHE_END; |
| 1514 | } |
| 1515 | |
| 1516 | end: |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1517 | if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1518 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 1519 | cs_shutr(cs); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1520 | } |
| 1521 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1522 | out: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1523 | total = res_htx->data - total; |
Christopher Faulet | 6112391 | 2019-01-02 14:10:01 +0100 | [diff] [blame] | 1524 | if (total) |
| 1525 | channel_add_input(res, total); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1526 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1527 | |
| 1528 | /* eat the whole request */ |
| 1529 | if (co_data(req)) { |
| 1530 | req_htx = htx_from_buf(&req->buf); |
| 1531 | co_htx_skip(req, req_htx, co_data(req)); |
| 1532 | htx_to_buf(req_htx, &req->buf); |
| 1533 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1534 | return; |
| 1535 | |
| 1536 | error: |
| 1537 | /* Sent and HTTP error 500 */ |
| 1538 | b_reset(&res->buf); |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 1539 | errmsg = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1540 | res->buf.data = b_data(errmsg); |
| 1541 | memcpy(res->buf.area, b_head(errmsg), b_data(errmsg)); |
| 1542 | res_htx = htx_from_buf(&res->buf); |
| 1543 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1544 | total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1545 | appctx->st0 = HTX_CACHE_END; |
| 1546 | goto end; |
| 1547 | } |
| 1548 | |
| 1549 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1550 | 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] | 1551 | { |
| 1552 | struct flt_conf *fconf; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1553 | struct cache_flt_conf *cconf = NULL; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1554 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1555 | if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1556 | memprintf(err, "expects a cache name"); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1557 | goto err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /* check if a cache filter was already registered with this cache |
| 1561 | * name, if that's the case, must use it. */ |
| 1562 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1563 | if (fconf->id == cache_store_flt_id) { |
| 1564 | cconf = fconf->conf; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1565 | if (cconf && strcmp((char *)cconf->c.name, name) == 0) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1566 | rule->arg.act.p[0] = cconf; |
| 1567 | return 1; |
| 1568 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1572 | /* Create the filter cache config */ |
| 1573 | cconf = calloc(1, sizeof(*cconf)); |
| 1574 | if (!cconf) { |
| 1575 | memprintf(err, "out of memory\n"); |
| 1576 | goto err; |
| 1577 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1578 | cconf->flags = CACHE_FLT_F_IMPLICIT_DECL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1579 | cconf->c.name = strdup(name); |
| 1580 | if (!cconf->c.name) { |
| 1581 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1582 | goto err; |
| 1583 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1584 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1585 | /* register a filter to fill the cache buffer */ |
| 1586 | fconf = calloc(1, sizeof(*fconf)); |
| 1587 | if (!fconf) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1588 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1589 | goto err; |
| 1590 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1591 | fconf->id = cache_store_flt_id; |
| 1592 | fconf->conf = cconf; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1593 | fconf->ops = &cache_ops; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1594 | LIST_APPEND(&proxy->filter_configs, &fconf->list); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1595 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1596 | rule->arg.act.p[0] = cconf; |
| 1597 | return 1; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1598 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1599 | err: |
| 1600 | free(cconf); |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 1605 | struct act_rule *rule, char **err) |
| 1606 | { |
| 1607 | rule->action = ACT_CUSTOM; |
| 1608 | rule->action_ptr = http_action_store_cache; |
| 1609 | |
| 1610 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
| 1611 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1612 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1613 | (*orig_arg)++; |
| 1614 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1615 | } |
| 1616 | |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1617 | /* This produces a sha1 hash of the concatenation of the HTTP method, |
| 1618 | * the first occurrence of the Host header followed by the path component |
| 1619 | * if it begins with a slash ('/'). */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1620 | int sha1_hosturi(struct stream *s) |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1621 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1622 | struct http_txn *txn = s->txn; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1623 | struct htx *htx = htxbuf(&s->req.buf); |
| 1624 | struct htx_sl *sl; |
| 1625 | struct http_hdr_ctx ctx; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1626 | struct ist uri; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1627 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1628 | struct buffer *trash; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1629 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1630 | trash = get_trash_chunk(); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1631 | ctx.blk = NULL; |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1632 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1633 | sl = http_get_stline(htx); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1634 | uri = htx_sl_req_uri(sl); // whole uri |
| 1635 | if (!uri.len) |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1636 | return 0; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1637 | |
| 1638 | /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'), |
| 1639 | * unless haproxy is deployed in front of an outbound cache. In HTTP/2, |
| 1640 | * URIs are almost always sent in absolute form with their scheme. In |
| 1641 | * this case, the scheme is almost always "https". In order to support |
| 1642 | * sharing of cache objects between H1 and H2, we'll hash the absolute |
| 1643 | * URI whenever known, or prepend "https://" + the Host header for |
| 1644 | * relative URIs. The difference will only appear on absolute HTTP/1 |
| 1645 | * requests sent to an origin server, which practically is never met in |
| 1646 | * the real world so we don't care about the ability to share the same |
| 1647 | * key here.URIs are normalized from the absolute URI to an origin form as |
| 1648 | * well. |
| 1649 | */ |
| 1650 | if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) { |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1651 | chunk_istcat(trash, ist("https://")); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1652 | if (!http_find_header(htx, ist("Host"), &ctx, 0)) |
| 1653 | return 0; |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1654 | chunk_istcat(trash, ctx.value); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1655 | } |
| 1656 | |
Tim Duesterhus | 9f7ed8a | 2021-11-08 09:05:04 +0100 | [diff] [blame] | 1657 | chunk_istcat(trash, uri); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* hash everything */ |
| 1660 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1661 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1662 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 1663 | |
| 1664 | return 1; |
| 1665 | } |
| 1666 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1667 | /* Looks for "If-None-Match" headers in the request and compares their value |
| 1668 | * with the one that might have been stored in the cache_entry. If any of them |
| 1669 | * matches, a "304 Not Modified" response should be sent instead of the cached |
| 1670 | * data. |
| 1671 | * 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] | 1672 | * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2). |
| 1673 | * |
| 1674 | * If no "If-None-Match" header was found, look for an "If-Modified-Since" |
| 1675 | * header and compare its value (date) to the one stored in the cache_entry. |
| 1676 | * If the request's date is later than the cached one, we also send a |
| 1677 | * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2). |
| 1678 | * |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1679 | * Returns 1 if "304 Not Modified" should be sent, 0 otherwise. |
| 1680 | */ |
| 1681 | static int should_send_notmodified_response(struct cache *cache, struct htx *htx, |
| 1682 | struct cache_entry *entry) |
| 1683 | { |
| 1684 | int retval = 0; |
| 1685 | |
| 1686 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 1687 | struct ist cache_entry_etag = IST_NULL; |
| 1688 | struct buffer *etag_buffer = NULL; |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1689 | int if_none_match_found = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1690 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1691 | struct tm tm = {}; |
| 1692 | time_t if_modified_since = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1693 | |
| 1694 | /* 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] | 1695 | * cache_entry's ETag in order to perform comparisons. |
| 1696 | * There could be multiple "if-none-match" header lines. */ |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1697 | 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] | 1698 | if_none_match_found = 1; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1699 | |
| 1700 | /* A '*' matches everything. */ |
| 1701 | if (isteq(ctx.value, ist("*")) != 0) { |
| 1702 | retval = 1; |
| 1703 | break; |
| 1704 | } |
| 1705 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1706 | /* No need to rebuild an etag if none was stored in the cache. */ |
| 1707 | if (entry->etag_length == 0) |
| 1708 | break; |
| 1709 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1710 | /* Rebuild the stored ETag. */ |
| 1711 | if (etag_buffer == NULL) { |
| 1712 | etag_buffer = get_trash_chunk(); |
| 1713 | |
| 1714 | if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry), |
| 1715 | (unsigned char*)b_orig(etag_buffer), |
| 1716 | entry->etag_offset, entry->etag_length) == 0) { |
| 1717 | cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length); |
| 1718 | } else { |
| 1719 | /* We could not rebuild the ETag in one go, we |
| 1720 | * won't send a "304 Not Modified" response. */ |
| 1721 | break; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | if (http_compare_etags(cache_entry_etag, ctx.value) == 1) { |
| 1726 | retval = 1; |
| 1727 | break; |
| 1728 | } |
| 1729 | } |
| 1730 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1731 | /* If the request did not contain an "If-None-Match" header, we look for |
| 1732 | * an "If-Modified-Since" header (see RFC 7232#3.3). */ |
| 1733 | if (retval == 0 && if_none_match_found == 0) { |
| 1734 | ctx.blk = NULL; |
| 1735 | if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) { |
| 1736 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 1737 | if_modified_since = my_timegm(&tm); |
| 1738 | |
| 1739 | /* We send a "304 Not Modified" response if the |
| 1740 | * entry's last modified date is earlier than |
| 1741 | * the one found in the "If-Modified-Since" |
| 1742 | * header. */ |
| 1743 | retval = (entry->last_modified <= if_modified_since); |
| 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1748 | return retval; |
| 1749 | } |
| 1750 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1751 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 1752 | struct session *sess, struct stream *s, int flags) |
| 1753 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1754 | |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1755 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1756 | struct cache_entry *res, *sec_entry = NULL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1757 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
| 1758 | struct cache *cache = cconf->c.cache; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1759 | struct shared_block *entry_block; |
| 1760 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1762 | /* Ignore cache for HTTP/1.0 requests and for requests other than GET |
| 1763 | * and HEAD */ |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1764 | if (!(txn->req.flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1765 | (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)) |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1766 | txn->flags |= TX_CACHE_IGNORE; |
| 1767 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1768 | http_check_request_for_cacheability(s, &s->req); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1769 | |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 1770 | /* The request's hash has to be calculated for all requests, even POSTs |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 1771 | * or PUTs for instance because RFC7234 specifies that a successful |
Remi Tricot-Le Breton | 72cffaf | 2020-12-03 18:19:31 +0100 | [diff] [blame] | 1772 | * "unsafe" method on a stored resource must invalidate it |
| 1773 | * (see RFC7234#4.4). */ |
| 1774 | if (!sha1_hosturi(s)) |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1775 | return ACT_RET_CONT; |
| 1776 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1777 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 1778 | return ACT_RET_CONT; |
| 1779 | |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1780 | if (px == strm_fe(s)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1781 | _HA_ATOMIC_INC(&px->fe_counters.p.http.cache_lookups); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1782 | else |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1783 | _HA_ATOMIC_INC(&px->be_counters.p.http.cache_lookups); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1784 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1785 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1786 | res = entry_exist(cache, s->txn->cache_hash); |
Remi Tricot-Le Breton | 3243447 | 2020-11-25 10:09:43 +0100 | [diff] [blame] | 1787 | /* We must not use an entry that is not complete. */ |
| 1788 | if (res && res->complete) { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1789 | struct appctx *appctx; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1790 | entry_block = block_ptr(res); |
| 1791 | shctx_row_inc_hot(shctx_ptr(cache), entry_block); |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1792 | shctx_unlock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1793 | |
| 1794 | /* In case of Vary, we could have multiple entries with the same |
Remi Tricot-Le Breton | 2b5c5cb | 2020-12-23 18:13:45 +0100 | [diff] [blame] | 1795 | * primary hash. We need to calculate the secondary hash in order |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1796 | * to find the actual entry we want (if it exists). */ |
| 1797 | if (res->secondary_key_signature) { |
| 1798 | if (!http_request_build_secondary_key(s, res->secondary_key_signature)) { |
| 1799 | shctx_lock(shctx_ptr(cache)); |
| 1800 | sec_entry = secondary_entry_exist(cache, res, |
| 1801 | s->txn->cache_secondary_hash); |
| 1802 | if (sec_entry && sec_entry != res) { |
| 1803 | /* The wrong row was added to the hot list. */ |
| 1804 | shctx_row_dec_hot(shctx_ptr(cache), entry_block); |
| 1805 | entry_block = block_ptr(sec_entry); |
| 1806 | shctx_row_inc_hot(shctx_ptr(cache), entry_block); |
| 1807 | } |
| 1808 | res = sec_entry; |
| 1809 | shctx_unlock(shctx_ptr(cache)); |
| 1810 | } |
| 1811 | else |
| 1812 | res = NULL; |
| 1813 | } |
| 1814 | |
| 1815 | /* We looked for a valid secondary entry and could not find one, |
| 1816 | * the request must be forwarded to the server. */ |
| 1817 | if (!res) { |
| 1818 | shctx_lock(shctx_ptr(cache)); |
| 1819 | shctx_row_dec_hot(shctx_ptr(cache), entry_block); |
| 1820 | shctx_unlock(shctx_ptr(cache)); |
| 1821 | return ACT_RET_CONT; |
| 1822 | } |
| 1823 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1824 | s->target = &http_cache_applet.obj_type; |
Christopher Faulet | 1336ccf | 2022-04-12 18:15:16 +0200 | [diff] [blame] | 1825 | if ((appctx = cs_applet_create(s->csb, objt_applet(s->target)))) { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1826 | appctx->st0 = HTX_CACHE_INIT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1827 | appctx->rule = rule; |
| 1828 | appctx->ctx.cache.entry = res; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1829 | appctx->ctx.cache.next = NULL; |
| 1830 | appctx->ctx.cache.sent = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1831 | appctx->ctx.cache.send_notmodified = |
| 1832 | should_send_notmodified_response(cache, htxbuf(&s->req.buf), res); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1833 | |
| 1834 | if (px == strm_fe(s)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1835 | _HA_ATOMIC_INC(&px->fe_counters.p.http.cache_hits); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1836 | else |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1837 | _HA_ATOMIC_INC(&px->be_counters.p.http.cache_hits); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1838 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1839 | } else { |
Christopher Faulet | 1d216c7 | 2022-04-21 11:30:43 +0200 | [diff] [blame] | 1840 | s->target = NULL; |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 1841 | shctx_lock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1842 | shctx_row_dec_hot(shctx_ptr(cache), entry_block); |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 1843 | shctx_unlock(shctx_ptr(cache)); |
Christopher Faulet | 1d216c7 | 2022-04-21 11:30:43 +0200 | [diff] [blame] | 1844 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1845 | } |
| 1846 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1847 | shctx_unlock(shctx_ptr(cache)); |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1848 | |
| 1849 | /* Shared context does not need to be locked while we calculate the |
| 1850 | * secondary hash. */ |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 1851 | if (!res && cache->vary_processing_enabled) { |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 1852 | /* Build a complete secondary hash until the server response |
| 1853 | * tells us which fields should be kept (if any). */ |
| 1854 | http_request_prebuild_full_secondary_key(s); |
| 1855 | } |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1856 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 1861 | struct act_rule *rule, char **err) |
| 1862 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1863 | rule->action = ACT_CUSTOM; |
| 1864 | rule->action_ptr = http_action_req_cache_use; |
| 1865 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1866 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1867 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1868 | |
| 1869 | (*orig_arg)++; |
| 1870 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 1874 | { |
| 1875 | int err_code = 0; |
| 1876 | |
| 1877 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 1878 | |
| 1879 | if (!*args[1]) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1880 | ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1881 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1882 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1883 | goto out; |
| 1884 | } |
| 1885 | |
| 1886 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1887 | err_code |= ERR_ABORT; |
| 1888 | goto out; |
| 1889 | } |
| 1890 | |
| 1891 | if (tmp_cache_config == NULL) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1892 | struct cache *cache_config; |
| 1893 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1894 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 1895 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1896 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1897 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1898 | goto out; |
| 1899 | } |
| 1900 | |
| 1901 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 1902 | if (strlen(args[1]) > 32) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1903 | 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] | 1904 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1905 | err_code |= ERR_WARN; |
| 1906 | } |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1907 | |
| 1908 | list_for_each_entry(cache_config, &caches_config, list) { |
| 1909 | if (strcmp(tmp_cache_config->id, cache_config->id) == 0) { |
| 1910 | ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n", |
| 1911 | file, linenum, tmp_cache_config->id); |
| 1912 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1913 | goto out; |
| 1914 | } |
| 1915 | } |
| 1916 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1917 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1918 | tmp_cache_config->maxblocks = 0; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1919 | tmp_cache_config->maxobjsz = 0; |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 1920 | tmp_cache_config->max_secondary_entries = DEFAULT_MAX_SECONDARY_ENTRY; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1921 | } |
| 1922 | } else if (strcmp(args[0], "total-max-size") == 0) { |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1923 | unsigned long int maxsize; |
| 1924 | char *err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1925 | |
| 1926 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1927 | err_code |= ERR_ABORT; |
| 1928 | goto out; |
| 1929 | } |
| 1930 | |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1931 | maxsize = strtoul(args[1], &err, 10); |
| 1932 | if (err == args[1] || *err != '\0') { |
| 1933 | ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n", |
| 1934 | file, linenum, args[1]); |
| 1935 | err_code |= ERR_ABORT; |
| 1936 | goto out; |
| 1937 | } |
| 1938 | |
| 1939 | if (maxsize > (UINT_MAX >> 20)) { |
| 1940 | ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n", |
| 1941 | file, linenum, args[1], UINT_MAX >> 20); |
| 1942 | err_code |= ERR_ABORT; |
| 1943 | goto out; |
| 1944 | } |
| 1945 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1946 | /* size in megabytes */ |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1947 | maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1948 | tmp_cache_config->maxblocks = maxsize; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1949 | } else if (strcmp(args[0], "max-age") == 0) { |
| 1950 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1951 | err_code |= ERR_ABORT; |
| 1952 | goto out; |
| 1953 | } |
| 1954 | |
| 1955 | if (!*args[1]) { |
| 1956 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 1957 | file, linenum, args[0]); |
| 1958 | err_code |= ERR_WARN; |
| 1959 | } |
| 1960 | |
| 1961 | tmp_cache_config->maxage = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1962 | } else if (strcmp(args[0], "max-object-size") == 0) { |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1963 | unsigned int maxobjsz; |
| 1964 | char *err; |
| 1965 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1966 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1967 | err_code |= ERR_ABORT; |
| 1968 | goto out; |
| 1969 | } |
| 1970 | |
| 1971 | if (!*args[1]) { |
| 1972 | ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n", |
| 1973 | file, linenum, args[0]); |
| 1974 | err_code |= ERR_WARN; |
| 1975 | } |
| 1976 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1977 | maxobjsz = strtoul(args[1], &err, 10); |
| 1978 | if (err == args[1] || *err != '\0') { |
| 1979 | ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n", |
| 1980 | file, linenum, args[1]); |
| 1981 | err_code |= ERR_ABORT; |
| 1982 | goto out; |
| 1983 | } |
| 1984 | tmp_cache_config->maxobjsz = maxobjsz; |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 1985 | } else if (strcmp(args[0], "process-vary") == 0) { |
| 1986 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1987 | err_code |= ERR_ABORT; |
| 1988 | goto out; |
| 1989 | } |
| 1990 | |
| 1991 | if (!*args[1]) { |
Remi Tricot-Le Breton | e6cc5b5 | 2020-12-23 18:13:53 +0100 | [diff] [blame] | 1992 | ha_warning("parsing [%s:%d]: '%s' expects \"on\" or \"off\" (enable or disable vary processing).\n", |
Remi Tricot-Le Breton | 754b242 | 2020-11-16 15:56:10 +0100 | [diff] [blame] | 1993 | file, linenum, args[0]); |
| 1994 | err_code |= ERR_WARN; |
| 1995 | } |
Remi Tricot-Le Breton | e6cc5b5 | 2020-12-23 18:13:53 +0100 | [diff] [blame] | 1996 | if (strcmp(args[1], "on") == 0) |
| 1997 | tmp_cache_config->vary_processing_enabled = 1; |
| 1998 | else if (strcmp(args[1], "off") == 0) |
| 1999 | tmp_cache_config->vary_processing_enabled = 0; |
| 2000 | else { |
| 2001 | ha_warning("parsing [%s:%d]: '%s' expects \"on\" or \"off\" (enable or disable vary processing).\n", |
| 2002 | file, linenum, args[0]); |
| 2003 | err_code |= ERR_WARN; |
| 2004 | } |
Remi Tricot-Le Breton | 5853c0c | 2020-12-10 17:58:43 +0100 | [diff] [blame] | 2005 | } else if (strcmp(args[0], "max-secondary-entries") == 0) { |
| 2006 | unsigned int max_sec_entries; |
| 2007 | char *err; |
| 2008 | |
| 2009 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 2010 | err_code |= ERR_ABORT; |
| 2011 | goto out; |
| 2012 | } |
| 2013 | |
| 2014 | if (!*args[1]) { |
| 2015 | ha_warning("parsing [%s:%d]: '%s' expects a strictly positive number.\n", |
| 2016 | file, linenum, args[0]); |
| 2017 | err_code |= ERR_WARN; |
| 2018 | } |
| 2019 | |
| 2020 | max_sec_entries = strtoul(args[1], &err, 10); |
| 2021 | if (err == args[1] || *err != '\0' || max_sec_entries == 0) { |
| 2022 | ha_warning("parsing [%s:%d]: max-secondary-entries wrong value '%s'\n", |
| 2023 | file, linenum, args[1]); |
| 2024 | err_code |= ERR_ABORT; |
| 2025 | goto out; |
| 2026 | } |
| 2027 | tmp_cache_config->max_secondary_entries = max_sec_entries; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 2028 | } |
| 2029 | else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2030 | 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] | 2031 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2032 | goto out; |
| 2033 | } |
| 2034 | out: |
| 2035 | return err_code; |
| 2036 | } |
| 2037 | |
| 2038 | /* once the cache section is parsed */ |
| 2039 | |
| 2040 | int cfg_post_parse_section_cache() |
| 2041 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2042 | int err_code = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2043 | |
| 2044 | if (tmp_cache_config) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2045 | |
| 2046 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2047 | 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] | 2048 | err_code |= ERR_FATAL | ERR_ALERT; |
| 2049 | goto out; |
| 2050 | } |
| 2051 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 2052 | if (!tmp_cache_config->maxobjsz) { |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 2053 | /* Default max. file size is a 256th of the cache size. */ |
| 2054 | tmp_cache_config->maxobjsz = |
| 2055 | (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 2056 | } |
| 2057 | else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) { |
| 2058 | ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2); |
| 2059 | err_code |= ERR_FATAL | ERR_ALERT; |
| 2060 | goto out; |
| 2061 | } |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 2062 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2063 | /* add to the list of cache to init and reinit tmp_cache_config |
| 2064 | * for next cache section, if any. |
| 2065 | */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2066 | LIST_APPEND(&caches_config, &tmp_cache_config->list); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2067 | tmp_cache_config = NULL; |
| 2068 | return err_code; |
| 2069 | } |
| 2070 | out: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2071 | ha_free(&tmp_cache_config); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2072 | return err_code; |
| 2073 | |
| 2074 | } |
| 2075 | |
| 2076 | int post_check_cache() |
| 2077 | { |
| 2078 | struct proxy *px; |
| 2079 | struct cache *back, *cache_config, *cache; |
| 2080 | struct shared_context *shctx; |
| 2081 | int ret_shctx; |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 2082 | int err_code = ERR_NONE; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2083 | |
| 2084 | list_for_each_entry_safe(cache_config, back, &caches_config, list) { |
| 2085 | |
| 2086 | ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE, |
| 2087 | cache_config->maxobjsz, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 2088 | |
Frédéric Lécaille | bc58449 | 2018-10-25 20:18:59 +0200 | [diff] [blame] | 2089 | if (ret_shctx <= 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2090 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2091 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2092 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2093 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2094 | |
| 2095 | err_code |= ERR_FATAL | ERR_ALERT; |
| 2096 | goto out; |
| 2097 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 2098 | shctx->free_block = cache_free_blocks; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2099 | /* the cache structure is stored in the shctx and added to the |
| 2100 | * caches list, we can remove the entry from the caches_config |
| 2101 | * list */ |
| 2102 | memcpy(shctx->data, cache_config, sizeof(struct cache)); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2103 | cache = (struct cache *)shctx->data; |
Remi Tricot-Le Breton | 1785f3d | 2020-11-16 15:56:09 +0100 | [diff] [blame] | 2104 | cache->entries = EB_ROOT; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2105 | LIST_APPEND(&caches, &cache->list); |
| 2106 | LIST_DELETE(&cache_config->list); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2107 | free(cache_config); |
| 2108 | |
| 2109 | /* Find all references for this cache in the existing filters |
| 2110 | * (over all proxies) and reference it in matching filters. |
| 2111 | */ |
| 2112 | for (px = proxies_list; px; px = px->next) { |
| 2113 | struct flt_conf *fconf; |
| 2114 | struct cache_flt_conf *cconf; |
| 2115 | |
| 2116 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 2117 | if (fconf->id != cache_store_flt_id) |
| 2118 | continue; |
| 2119 | |
| 2120 | cconf = fconf->conf; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 2121 | if (strcmp(cache->id, cconf->c.name) == 0) { |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2122 | free(cconf->c.name); |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 2123 | cconf->flags |= CACHE_FLT_INIT; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2124 | cconf->c.cache = cache; |
| 2125 | break; |
| 2126 | } |
| 2127 | } |
| 2128 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2129 | } |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2130 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2131 | out: |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2132 | return err_code; |
| 2133 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2134 | } |
| 2135 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2136 | struct flt_ops cache_ops = { |
| 2137 | .init = cache_store_init, |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 2138 | .check = cache_store_check, |
| 2139 | .deinit = cache_store_deinit, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2140 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 2141 | /* Handle stream init/deinit */ |
| 2142 | .attach = cache_store_strm_init, |
| 2143 | .detach = cache_store_strm_deinit, |
| 2144 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 2145 | /* Handle channels activity */ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 2146 | .channel_post_analyze = cache_store_post_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 2147 | |
| 2148 | /* Filter HTTP requests and responses */ |
| 2149 | .http_headers = cache_store_http_headers, |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 2150 | .http_payload = cache_store_http_payload, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 2151 | .http_end = cache_store_http_end, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2152 | }; |
| 2153 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2154 | |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2155 | #define CHECK_ENCODING(str, encoding_name, encoding_value) \ |
| 2156 | ({ \ |
| 2157 | int retval = 0; \ |
| 2158 | if (istmatch(str, (struct ist){ .ptr = encoding_name+1, .len = sizeof(encoding_name) - 2 })) { \ |
| 2159 | retval = encoding_value; \ |
| 2160 | encoding = istadv(encoding, sizeof(encoding_name) - 2); \ |
| 2161 | } \ |
| 2162 | (retval); \ |
| 2163 | }) |
| 2164 | |
| 2165 | /* |
| 2166 | * Parse the encoding <encoding> and try to match the encoding part upon an |
| 2167 | * encoding list of explicitly supported encodings (which all have a specific |
| 2168 | * bit in an encoding bitmap). If a weight is included in the value, find out if |
| 2169 | * it is null or not. The bit value will be set in the <encoding_value> |
| 2170 | * parameter and the <has_null_weight> will be set to 1 if the weight is strictly |
| 2171 | * 0, 1 otherwise. |
| 2172 | * The encodings list is extracted from |
| 2173 | * https://www.iana.org/assignments/http-parameters/http-parameters.xhtml. |
| 2174 | * Returns 0 in case of success and -1 in case of error. |
| 2175 | */ |
| 2176 | static int parse_encoding_value(struct ist encoding, unsigned int *encoding_value, |
| 2177 | unsigned int *has_null_weight) |
| 2178 | { |
| 2179 | int retval = 0; |
| 2180 | |
| 2181 | if (!encoding_value) |
| 2182 | return -1; |
| 2183 | |
| 2184 | if (!istlen(encoding)) |
| 2185 | return -1; /* Invalid encoding */ |
| 2186 | |
| 2187 | *encoding_value = 0; |
| 2188 | if (has_null_weight) |
| 2189 | *has_null_weight = 0; |
| 2190 | |
| 2191 | switch (*encoding.ptr) { |
| 2192 | case 'a': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2193 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2194 | *encoding_value = CHECK_ENCODING(encoding, "aes128gcm", VARY_ENCODING_AES128GCM); |
| 2195 | break; |
| 2196 | case 'b': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2197 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2198 | *encoding_value = CHECK_ENCODING(encoding, "br", VARY_ENCODING_BR); |
| 2199 | break; |
| 2200 | case 'c': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2201 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2202 | *encoding_value = CHECK_ENCODING(encoding, "compress", VARY_ENCODING_COMPRESS); |
| 2203 | break; |
| 2204 | case 'd': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2205 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2206 | *encoding_value = CHECK_ENCODING(encoding, "deflate", VARY_ENCODING_DEFLATE); |
| 2207 | break; |
| 2208 | case 'e': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2209 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2210 | *encoding_value = CHECK_ENCODING(encoding, "exi", VARY_ENCODING_EXI); |
| 2211 | break; |
| 2212 | case 'g': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2213 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2214 | *encoding_value = CHECK_ENCODING(encoding, "gzip", VARY_ENCODING_GZIP); |
| 2215 | break; |
| 2216 | case 'i': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2217 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2218 | *encoding_value = CHECK_ENCODING(encoding, "identity", VARY_ENCODING_IDENTITY); |
| 2219 | break; |
| 2220 | case 'p': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2221 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2222 | *encoding_value = CHECK_ENCODING(encoding, "pack200-gzip", VARY_ENCODING_PACK200_GZIP); |
| 2223 | break; |
| 2224 | case 'x': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2225 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2226 | *encoding_value = CHECK_ENCODING(encoding, "x-gzip", VARY_ENCODING_GZIP); |
| 2227 | if (!*encoding_value) |
| 2228 | *encoding_value = CHECK_ENCODING(encoding, "x-compress", VARY_ENCODING_COMPRESS); |
| 2229 | break; |
| 2230 | case 'z': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2231 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2232 | *encoding_value = CHECK_ENCODING(encoding, "zstd", VARY_ENCODING_ZSTD); |
| 2233 | break; |
| 2234 | case '*': |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2235 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2236 | *encoding_value = VARY_ENCODING_STAR; |
| 2237 | break; |
| 2238 | default: |
| 2239 | retval = -1; /* Unmanaged encoding */ |
| 2240 | break; |
| 2241 | } |
| 2242 | |
| 2243 | /* Process the optional weight part of the encoding. */ |
| 2244 | if (*encoding_value) { |
| 2245 | encoding = http_trim_leading_spht(encoding); |
| 2246 | if (istlen(encoding)) { |
| 2247 | if (*encoding.ptr != ';') |
| 2248 | return -1; |
| 2249 | |
| 2250 | if (has_null_weight) { |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 2251 | encoding = istnext(encoding); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2252 | |
| 2253 | encoding = http_trim_leading_spht(encoding); |
| 2254 | |
| 2255 | *has_null_weight = isteq(encoding, ist("q=0")); |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | return retval; |
| 2261 | } |
| 2262 | |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2263 | #define ACCEPT_ENCODING_MAX_ENTRIES 16 |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2264 | /* |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2265 | * Build a bitmap of the accept-encoding header. |
| 2266 | * |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2267 | * The bitmap is built by matching every sub-part of the accept-encoding value |
| 2268 | * with a subset of explicitly supported encodings, which all have their own bit |
| 2269 | * in the bitmap. This bitmap will be used to determine if a response can be |
| 2270 | * served to a client (that is if it has an encoding that is accepted by the |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2271 | * client). Any unknown encodings will be indicated by the VARY_ENCODING_OTHER |
| 2272 | * bit. |
| 2273 | * |
| 2274 | * Returns 0 in case of success and -1 in case of error. |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2275 | */ |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2276 | static int accept_encoding_normalizer(struct htx *htx, struct ist hdr_name, |
| 2277 | char *buf, unsigned int *buf_len) |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2278 | { |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2279 | size_t count = 0; |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2280 | uint32_t encoding_bitmap = 0; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2281 | unsigned int encoding_bmp_bl = -1; |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2282 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2283 | unsigned int encoding_value; |
| 2284 | unsigned int rejected_encoding; |
| 2285 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2286 | /* A user agent always accepts an unencoded value unless it explicitly |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2287 | * refuses it through an "identity;q=0" accept-encoding value. */ |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2288 | encoding_bitmap |= VARY_ENCODING_IDENTITY; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2289 | |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2290 | /* Iterate over all the ACCEPT_ENCODING_MAX_ENTRIES first accept-encoding |
| 2291 | * values that might span acrosse multiple accept-encoding headers. */ |
| 2292 | while (http_find_header(htx, hdr_name, &ctx, 0) && count < ACCEPT_ENCODING_MAX_ENTRIES) { |
Tim Duesterhus | 3bc6af4 | 2021-06-18 15:09:28 +0200 | [diff] [blame] | 2293 | count++; |
| 2294 | |
| 2295 | /* As per RFC7231#5.3.4, "An Accept-Encoding header field with a |
| 2296 | * combined field-value that is empty implies that the user agent |
| 2297 | * does not want any content-coding in response." |
| 2298 | * |
| 2299 | * We must (and did) count the existence of this empty header to not |
| 2300 | * hit the `count == 0` case below, but must ignore the value to not |
| 2301 | * include VARY_ENCODING_OTHER into the final bitmap. |
| 2302 | */ |
| 2303 | if (istlen(ctx.value) == 0) |
| 2304 | continue; |
| 2305 | |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2306 | /* Turn accept-encoding value to lower case */ |
| 2307 | ist2bin_lc(istptr(ctx.value), ctx.value); |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2308 | |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2309 | /* Try to identify a known encoding and to manage null weights. */ |
| 2310 | if (!parse_encoding_value(ctx.value, &encoding_value, &rejected_encoding)) { |
| 2311 | if (rejected_encoding) |
| 2312 | encoding_bmp_bl &= ~encoding_value; |
| 2313 | else |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2314 | encoding_bitmap |= encoding_value; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2315 | } |
| 2316 | else { |
| 2317 | /* Unknown encoding */ |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2318 | encoding_bitmap |= VARY_ENCODING_OTHER; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2319 | } |
Remi Tricot-Le Breton | 8bb72aa | 2020-11-30 17:06:03 +0100 | [diff] [blame] | 2320 | } |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2321 | |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2322 | /* If a "*" was found in the accepted encodings (without a null weight), |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2323 | * all the encoding are accepted except the ones explicitly rejected. */ |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2324 | if (encoding_bitmap & VARY_ENCODING_STAR) { |
| 2325 | encoding_bitmap = ~0; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2326 | } |
| 2327 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2328 | /* Clear explicitly rejected encodings from the bitmap */ |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2329 | encoding_bitmap &= encoding_bmp_bl; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2330 | |
| 2331 | /* As per RFC7231#5.3.4, "If no Accept-Encoding field is in the request, |
| 2332 | * any content-coding is considered acceptable by the user agent". */ |
| 2333 | if (count == 0) |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2334 | encoding_bitmap = ~0; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2335 | |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2336 | /* A request with more than ACCEPT_ENCODING_MAX_ENTRIES accepted |
| 2337 | * encodings might be illegitimate so we will not use it. */ |
| 2338 | if (count == ACCEPT_ENCODING_MAX_ENTRIES) |
| 2339 | return -1; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2340 | |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2341 | write_u32(buf, encoding_bitmap); |
| 2342 | *buf_len = sizeof(encoding_bitmap); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2343 | |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2344 | /* This function fills the hash buffer correctly even if no header was |
| 2345 | * found, hence the 0 return value (success). */ |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2346 | return 0; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2347 | } |
Tim Duesterhus | 23b2945 | 2020-11-24 22:22:56 +0100 | [diff] [blame] | 2348 | #undef ACCEPT_ENCODING_MAX_ENTRIES |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2349 | |
| 2350 | /* |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2351 | * Normalizer used by default for the Referer header. It only |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2352 | * calculates a simple crc of the whole value. |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2353 | * Only the first occurrence of the header will be taken into account in the |
| 2354 | * hash. |
| 2355 | * Returns 0 in case of success, 1 if the hash buffer should be filled with 0s |
| 2356 | * and -1 in case of error. |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2357 | */ |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2358 | static int default_normalizer(struct htx *htx, struct ist hdr_name, |
| 2359 | char *buf, unsigned int *buf_len) |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2360 | { |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2361 | int retval = 1; |
| 2362 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2363 | |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2364 | if (http_find_header(htx, hdr_name, &ctx, 1)) { |
| 2365 | retval = 0; |
| 2366 | write_u32(buf, hash_crc32(istptr(ctx.value), istlen(ctx.value))); |
| 2367 | *buf_len = sizeof(int); |
| 2368 | } |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2369 | |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2370 | return retval; |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2371 | } |
| 2372 | |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2373 | /* |
Tim Duesterhus | ed84d84 | 2021-01-18 13:41:17 +0100 | [diff] [blame] | 2374 | * Accept-Encoding bitmap comparison function. |
| 2375 | * Returns 0 if the bitmaps are compatible. |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2376 | */ |
Tim Duesterhus | ed84d84 | 2021-01-18 13:41:17 +0100 | [diff] [blame] | 2377 | static int accept_encoding_bitmap_cmp(const void *ref, const void *new, unsigned int len) |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2378 | { |
Tim Duesterhus | ed84d84 | 2021-01-18 13:41:17 +0100 | [diff] [blame] | 2379 | uint32_t ref_bitmap = read_u32(ref); |
| 2380 | uint32_t new_bitmap = read_u32(new); |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2381 | |
Tim Duesterhus | ed84d84 | 2021-01-18 13:41:17 +0100 | [diff] [blame] | 2382 | if (!(ref_bitmap & VARY_ENCODING_OTHER)) { |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2383 | /* All the bits set in the reference bitmap correspond to the |
| 2384 | * stored response' encoding and should all be set in the new |
| 2385 | * encoding bitmap in order for the client to be able to manage |
Tim Duesterhus | dc38bc4 | 2020-12-29 12:43:53 +0100 | [diff] [blame] | 2386 | * the response. |
| 2387 | * |
| 2388 | * If this is the case the cached response has encodings that |
| 2389 | * are accepted by the client. It can be served directly by |
| 2390 | * the cache (as far as the accept-encoding part is concerned). |
| 2391 | */ |
| 2392 | |
Tim Duesterhus | ed84d84 | 2021-01-18 13:41:17 +0100 | [diff] [blame] | 2393 | return (ref_bitmap & new_bitmap) != ref_bitmap; |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2394 | } |
Tim Duesterhus | dc38bc4 | 2020-12-29 12:43:53 +0100 | [diff] [blame] | 2395 | else { |
Tim Duesterhus | 1d66e39 | 2021-01-18 13:41:16 +0100 | [diff] [blame] | 2396 | return 1; |
Tim Duesterhus | dc38bc4 | 2020-12-29 12:43:53 +0100 | [diff] [blame] | 2397 | } |
Remi Tricot-Le Breton | ce9e7b2 | 2020-12-23 18:13:49 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2401 | /* |
| 2402 | * Pre-calculate the hashes of all the supported headers (in our Vary |
| 2403 | * implementation) of a given request. We have to calculate all the hashes |
| 2404 | * in advance because the actual Vary signature won't be known until the first |
| 2405 | * response. |
| 2406 | * Only the first occurrence of every header will be taken into account in the |
| 2407 | * hash. |
| 2408 | * If the header is not present, the hash portion of the given header will be |
| 2409 | * filled with zeros. |
| 2410 | * Returns 0 in case of success. |
| 2411 | */ |
| 2412 | static int http_request_prebuild_full_secondary_key(struct stream *s) |
| 2413 | { |
Remi Tricot-Le Breton | bba2912 | 2020-12-23 18:13:44 +0100 | [diff] [blame] | 2414 | /* The fake signature (second parameter) will ensure that every part of the |
| 2415 | * secondary key is calculated. */ |
| 2416 | return http_request_build_secondary_key(s, ~0); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | |
| 2420 | /* |
| 2421 | * Calculate the secondary key for a request for which we already have a known |
| 2422 | * vary signature. The key is made by aggregating hashes calculated for every |
| 2423 | * header mentioned in the vary signature. |
| 2424 | * Only the first occurrence of every header will be taken into account in the |
| 2425 | * hash. |
| 2426 | * If the header is not present, the hash portion of the given header will be |
| 2427 | * filled with zeros. |
| 2428 | * Returns 0 in case of success. |
| 2429 | */ |
| 2430 | static int http_request_build_secondary_key(struct stream *s, int vary_signature) |
| 2431 | { |
| 2432 | struct http_txn *txn = s->txn; |
| 2433 | struct htx *htx = htxbuf(&s->req.buf); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2434 | |
| 2435 | unsigned int idx; |
| 2436 | const struct vary_hashing_information *info = NULL; |
| 2437 | unsigned int hash_length = 0; |
| 2438 | int retval = 0; |
| 2439 | int offset = 0; |
| 2440 | |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2441 | for (idx = 0; idx < sizeof(vary_information)/sizeof(*vary_information) && retval >= 0; ++idx) { |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2442 | info = &vary_information[idx]; |
| 2443 | |
Remi Tricot-Le Breton | e4421de | 2020-12-23 18:13:46 +0100 | [diff] [blame] | 2444 | /* The normalizing functions will be in charge of getting the |
| 2445 | * header values from the htx. This way they can manage multiple |
| 2446 | * occurrences of their processed header. */ |
| 2447 | if ((vary_signature & info->value) && info->norm_fn != NULL && |
| 2448 | !(retval = info->norm_fn(htx, info->hdr_name, &txn->cache_secondary_hash[offset], &hash_length))) { |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2449 | offset += hash_length; |
| 2450 | } |
| 2451 | else { |
| 2452 | /* Fill hash with 0s. */ |
| 2453 | hash_length = info->hash_length; |
| 2454 | memset(&txn->cache_secondary_hash[offset], 0, hash_length); |
| 2455 | offset += hash_length; |
| 2456 | } |
| 2457 | } |
| 2458 | |
Remi Tricot-Le Breton | 2b5c5cb | 2020-12-23 18:13:45 +0100 | [diff] [blame] | 2459 | if (retval >= 0) |
| 2460 | txn->flags |= TX_CACHE_HAS_SEC_KEY; |
| 2461 | |
| 2462 | return (retval < 0); |
Remi Tricot-Le Breton | 3d08236 | 2020-11-16 15:56:08 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | /* |
| 2466 | * Build the actual secondary key of a given request out of the prebuilt key and |
| 2467 | * the actual vary signature (extracted from the response). |
| 2468 | * Returns 0 in case of success. |
| 2469 | */ |
| 2470 | static int http_request_reduce_secondary_key(unsigned int vary_signature, |
| 2471 | char prebuilt_key[HTTP_CACHE_SEC_KEY_LEN]) |
| 2472 | { |
| 2473 | int offset = 0; |
| 2474 | int global_offset = 0; |
| 2475 | int vary_info_count = 0; |
| 2476 | int keep = 0; |
| 2477 | unsigned int vary_idx; |
| 2478 | const struct vary_hashing_information *vary_info; |
| 2479 | |
| 2480 | vary_info_count = sizeof(vary_information)/sizeof(*vary_information); |
| 2481 | for (vary_idx = 0; vary_idx < vary_info_count; ++vary_idx) { |
| 2482 | vary_info = &vary_information[vary_idx]; |
| 2483 | keep = (vary_signature & vary_info->value) ? 0xff : 0; |
| 2484 | |
| 2485 | for (offset = 0; offset < vary_info->hash_length; ++offset,++global_offset) { |
| 2486 | prebuilt_key[global_offset] &= keep; |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | return 0; |
| 2491 | } |
| 2492 | |
| 2493 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2494 | |
| 2495 | static int |
| 2496 | parse_cache_flt(char **args, int *cur_arg, struct proxy *px, |
| 2497 | struct flt_conf *fconf, char **err, void *private) |
| 2498 | { |
| 2499 | struct flt_conf *f, *back; |
Willy Tarreau | a73da1e | 2018-12-14 10:19:28 +0100 | [diff] [blame] | 2500 | struct cache_flt_conf *cconf = NULL; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2501 | char *name = NULL; |
| 2502 | int pos = *cur_arg; |
| 2503 | |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 2504 | /* Get the cache filter name. <pos> point on "cache" keyword */ |
| 2505 | if (!*args[pos + 1]) { |
Tim Duesterhus | ea969f6 | 2020-08-18 22:06:51 +0200 | [diff] [blame] | 2506 | memprintf(err, "%s : expects a <name> argument", args[pos]); |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 2507 | goto error; |
| 2508 | } |
| 2509 | name = strdup(args[pos + 1]); |
| 2510 | if (!name) { |
| 2511 | memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]); |
| 2512 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2513 | } |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 2514 | pos += 2; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2515 | |
| 2516 | /* Check if an implicit filter with the same name already exists. If so, |
| 2517 | * we remove the implicit filter to use the explicit one. */ |
| 2518 | list_for_each_entry_safe(f, back, &px->filter_configs, list) { |
| 2519 | if (f->id != cache_store_flt_id) |
| 2520 | continue; |
| 2521 | |
| 2522 | cconf = f->conf; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 2523 | if (strcmp(name, cconf->c.name) != 0) { |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2524 | cconf = NULL; |
| 2525 | continue; |
| 2526 | } |
| 2527 | |
| 2528 | if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 2529 | cconf = NULL; |
| 2530 | memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'", |
| 2531 | px->id, name); |
Tim Duesterhus | d34b1ce | 2020-01-18 01:46:18 +0100 | [diff] [blame] | 2532 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | /* Remove the implicit filter. <cconf> is kept for the explicit one */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2536 | LIST_DELETE(&f->list); |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2537 | free(f); |
| 2538 | free(name); |
| 2539 | break; |
| 2540 | } |
| 2541 | |
| 2542 | /* No implicit cache filter found, create configuration for the explicit one */ |
| 2543 | if (!cconf) { |
| 2544 | cconf = calloc(1, sizeof(*cconf)); |
| 2545 | if (!cconf) { |
| 2546 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 2547 | goto error; |
| 2548 | } |
| 2549 | cconf->c.name = name; |
| 2550 | } |
| 2551 | |
| 2552 | cconf->flags = 0; |
| 2553 | fconf->id = cache_store_flt_id; |
| 2554 | fconf->conf = cconf; |
| 2555 | fconf->ops = &cache_ops; |
| 2556 | |
| 2557 | *cur_arg = pos; |
| 2558 | return 0; |
| 2559 | |
| 2560 | error: |
| 2561 | free(name); |
| 2562 | free(cconf); |
| 2563 | return -1; |
| 2564 | } |
| 2565 | |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 2566 | /* It reserves a struct show_cache_ctx for the local variables */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 2567 | 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] | 2568 | { |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 2569 | struct show_cache_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 2570 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2571 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2572 | return 1; |
| 2573 | |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 2574 | ctx->cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2575 | return 0; |
| 2576 | } |
| 2577 | |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 2578 | /* It uses a struct show_cache_ctx for the local variables */ |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2579 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 2580 | { |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 2581 | struct show_cache_ctx *ctx = appctx->svcctx; |
| 2582 | struct cache* cache = ctx->cache; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2583 | struct conn_stream *cs = appctx->owner; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2584 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2585 | list_for_each_entry_from(cache, &caches, list) { |
| 2586 | struct eb32_node *node = NULL; |
| 2587 | unsigned int next_key; |
| 2588 | struct cache_entry *entry; |
Remi Tricot-Le Breton | e3e1e5f | 2020-11-27 15:48:40 +0100 | [diff] [blame] | 2589 | unsigned int i; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2590 | |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 2591 | next_key = ctx->next_key; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 2592 | if (!next_key) { |
| 2593 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2594 | if (ci_putchk(cs_ic(cs), &trash) == -1) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 2595 | cs_rx_room_blk(cs); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 2596 | return 0; |
| 2597 | } |
| 2598 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2599 | |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 2600 | ctx->cache = cache; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2601 | |
| 2602 | while (1) { |
| 2603 | |
| 2604 | shctx_lock(shctx_ptr(cache)); |
Christopher Faulet | 27f88a9 | 2021-11-23 16:03:05 +0100 | [diff] [blame] | 2605 | node = eb32_lookup_ge(&cache->entries, next_key); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2606 | if (!node) { |
| 2607 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 2608 | ctx->next_key = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2609 | break; |
| 2610 | } |
| 2611 | |
| 2612 | entry = container_of(node, struct cache_entry, eb); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2613 | next_key = node->key + 1; |
Willy Tarreau | f1de1b5 | 2022-04-13 11:21:39 +0200 | [diff] [blame] | 2614 | |
| 2615 | if (entry->expire > now.tv_sec) { |
| 2616 | chunk_printf(&trash, "%p hash:%u vary:0x", entry, read_u32(entry->hash)); |
| 2617 | for (i = 0; i < HTTP_CACHE_SEC_KEY_LEN; ++i) |
| 2618 | chunk_appendf(&trash, "%02x", (unsigned char)entry->secondary_key[i]); |
| 2619 | chunk_appendf(&trash, " size:%u (%u blocks), refcount:%u, expire:%d\n", |
| 2620 | block_ptr(entry)->len, block_ptr(entry)->block_count, |
| 2621 | block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec); |
| 2622 | } else { |
| 2623 | /* time to remove that one */ |
| 2624 | delete_entry(entry); |
| 2625 | entry->eb.key = 0; |
| 2626 | } |
| 2627 | |
Willy Tarreau | c6dfef7 | 2022-05-05 16:46:13 +0200 | [diff] [blame] | 2628 | ctx->next_key = next_key; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2629 | |
| 2630 | shctx_unlock(shctx_ptr(cache)); |
| 2631 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2632 | if (ci_putchk(cs_ic(cs), &trash) == -1) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 2633 | cs_rx_room_blk(cs); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2634 | return 0; |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | } |
| 2639 | |
| 2640 | return 1; |
| 2641 | |
| 2642 | } |
| 2643 | |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 2644 | |
| 2645 | /* |
| 2646 | * boolean, returns true if response was built out of a cache entry. |
| 2647 | */ |
| 2648 | static int |
| 2649 | smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp, |
| 2650 | const char *kw, void *private) |
| 2651 | { |
| 2652 | smp->data.type = SMP_T_BOOL; |
| 2653 | smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0); |
| 2654 | |
| 2655 | return 1; |
| 2656 | } |
| 2657 | |
| 2658 | /* |
| 2659 | * string, returns cache name (if response came from a cache). |
| 2660 | */ |
| 2661 | static int |
| 2662 | smp_fetch_res_cache_name(const struct arg *args, struct sample *smp, |
| 2663 | const char *kw, void *private) |
| 2664 | { |
| 2665 | struct appctx *appctx = NULL; |
| 2666 | |
| 2667 | struct cache_flt_conf *cconf = NULL; |
| 2668 | struct cache *cache = NULL; |
| 2669 | |
| 2670 | if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type) |
| 2671 | return 0; |
| 2672 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2673 | /* Get appctx from the conn-stream. */ |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 2674 | appctx = cs_appctx(smp->strm->csb); |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 2675 | if (appctx && appctx->rule) { |
| 2676 | cconf = appctx->rule->arg.act.p[0]; |
| 2677 | if (cconf) { |
| 2678 | cache = cconf->c.cache; |
| 2679 | |
| 2680 | smp->data.type = SMP_T_STR; |
| 2681 | smp->flags = SMP_F_CONST; |
| 2682 | smp->data.u.str.area = cache->id; |
| 2683 | smp->data.u.str.data = strlen(cache->id); |
| 2684 | return 1; |
| 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | return 0; |
| 2689 | } |
| 2690 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 2691 | /* Declare the filter parser for "cache" keyword */ |
| 2692 | static struct flt_kw_list filter_kws = { "CACHE", { }, { |
| 2693 | { "cache", parse_cache_flt, NULL }, |
| 2694 | { NULL, NULL, NULL }, |
| 2695 | } |
| 2696 | }; |
| 2697 | |
| 2698 | INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws); |
| 2699 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2700 | static struct cli_kw_list cli_kws = {{},{ |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 2701 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 2702 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2703 | }}; |
| 2704 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2705 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 2706 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2707 | static struct action_kw_list http_res_actions = { |
| 2708 | .kw = { |
| 2709 | { "cache-store", parse_cache_store }, |
| 2710 | { NULL, NULL } |
| 2711 | } |
| 2712 | }; |
| 2713 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2714 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
| 2715 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2716 | static struct action_kw_list http_req_actions = { |
| 2717 | .kw = { |
| 2718 | { "cache-use", parse_cache_use }, |
| 2719 | { NULL, NULL } |
| 2720 | } |
| 2721 | }; |
| 2722 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2723 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 2724 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 2725 | struct applet http_cache_applet = { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2726 | .obj_type = OBJ_TYPE_APPLET, |
| 2727 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 2728 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 2729 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 2730 | }; |
| 2731 | |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 2732 | /* config parsers for this section */ |
| 2733 | REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 2734 | REGISTER_POST_CHECK(post_check_cache); |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 2735 | |
| 2736 | |
| 2737 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2738 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 2739 | { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE }, |
| 2740 | { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE }, |
| 2741 | { /* END */ }, |
| 2742 | } |
| 2743 | }; |
| 2744 | |
| 2745 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |