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