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