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