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