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